]> git.sur5r.net Git - openldap/blob - libraries/libldap/cyrus.c
e28da6c09204475b1fbab011cd68ebbbde01e0e7
[openldap] / libraries / libldap / cyrus.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1999-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdio.h>
10
11 #include <ac/socket.h>
12 #include <ac/stdlib.h>
13 #include <ac/string.h>
14 #include <ac/time.h>
15 #include <ac/errno.h>
16 #include <ac/ctype.h>
17
18 #include "ldap-int.h"
19
20 #ifdef HAVE_CYRUS_SASL
21
22 #ifdef LDAP_R_COMPILE
23 ldap_pvt_thread_mutex_t ldap_int_sasl_mutex;
24 #endif
25
26 #ifdef HAVE_SASL_SASL_H
27 #include <sasl/sasl.h>
28 #else
29 #include <sasl.h>
30 #endif
31
32 #if SASL_VERSION_MAJOR >= 2
33 #define SASL_CONST const
34 #else
35 #define SASL_CONST
36 #endif
37
38 /*
39 * Various Cyrus SASL related stuff.
40 */
41
42 int ldap_int_sasl_init( void )
43 {
44         /* XXX not threadsafe */
45         static int sasl_initialized = 0;
46
47         static sasl_callback_t client_callbacks[] = {
48 #ifdef SASL_CB_GETREALM
49                 { SASL_CB_GETREALM, NULL, NULL },
50 #endif
51                 { SASL_CB_USER, NULL, NULL },
52                 { SASL_CB_AUTHNAME, NULL, NULL },
53                 { SASL_CB_PASS, NULL, NULL },
54                 { SASL_CB_ECHOPROMPT, NULL, NULL },
55                 { SASL_CB_NOECHOPROMPT, NULL, NULL },
56                 { SASL_CB_LIST_END, NULL, NULL }
57         };
58
59 #ifdef HAVE_SASL_VERSION
60 #define SASL_BUILD_VERSION ((SASL_VERSION_MAJOR << 24) |\
61         (SASL_VERSION_MINOR << 16) | SASL_VERSION_STEP)
62
63         { int rc;
64         sasl_version( NULL, &rc );
65         if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
66                 (rc & 0xffff) < SASL_VERSION_STEP) {
67
68 #ifdef NEW_LOGGING
69                 LDAP_LOG( TRANSPORT, INFO,
70                 "ldap_int_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
71                         rc, SASL_BUILD_VERSION, 0 );
72 #else
73                 Debug( LDAP_DEBUG_ANY,
74                 "ldap_int_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
75                         rc, SASL_BUILD_VERSION, 0 );
76 #endif
77                 return -1;
78         }
79         }
80 #endif
81         if ( sasl_initialized ) {
82                 return 0;
83         }
84
85 /* SASL 2 takes care of its own memory completely internally */
86 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
87         sasl_set_alloc(
88                 ber_memalloc,
89                 ber_memcalloc,
90                 ber_memrealloc,
91                 ber_memfree );
92 #endif /* CSRIMALLOC */
93
94 #ifdef LDAP_R_COMPILE
95         sasl_set_mutex(
96                 ldap_pvt_sasl_mutex_new,
97                 ldap_pvt_sasl_mutex_lock,
98                 ldap_pvt_sasl_mutex_unlock,    
99                 ldap_pvt_sasl_mutex_dispose );    
100
101         ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
102 #endif
103
104         if ( sasl_client_init( client_callbacks ) == SASL_OK ) {
105                 sasl_initialized = 1;
106                 return 0;
107         }
108
109 #if SASL_VERSION_MAJOR < 2
110         /* A no-op to make sure we link with Cyrus 1.5 */
111         sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
112 #endif
113         return -1;
114 }
115
116 /*
117  * SASL encryption support for LBER Sockbufs
118  */
119
120 struct sb_sasl_data {
121         sasl_conn_t             *sasl_context;
122         unsigned                *sasl_maxbuf;
123         Sockbuf_Buf             sec_buf_in;
124         Sockbuf_Buf             buf_in;
125         Sockbuf_Buf             buf_out;
126 };
127
128 static int
129 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
130 {
131         struct sb_sasl_data     *p;
132
133         assert( sbiod != NULL );
134
135         p = LBER_MALLOC( sizeof( *p ) );
136         if ( p == NULL )
137                 return -1;
138         p->sasl_context = (sasl_conn_t *)arg;
139         ber_pvt_sb_buf_init( &p->sec_buf_in );
140         ber_pvt_sb_buf_init( &p->buf_in );
141         ber_pvt_sb_buf_init( &p->buf_out );
142         if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
143                 LBER_FREE( p );
144                 errno = ENOMEM;
145                 return -1;
146         }
147         sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
148                 (SASL_CONST void **) &p->sasl_maxbuf );
149
150         sbiod->sbiod_pvt = p;
151
152         return 0;
153 }
154
155 static int
156 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
157 {
158         struct sb_sasl_data     *p;
159
160         assert( sbiod != NULL );
161         
162         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
163 #if SASL_VERSION_MAJOR >= 2
164         /*
165          * SASLv2 encode/decode buffers are managed by
166          * libsasl2. Ensure they are not freed by liblber.
167          */
168         p->buf_in.buf_base = NULL;
169         p->buf_out.buf_base = NULL;
170 #endif
171         ber_pvt_sb_buf_destroy( &p->sec_buf_in );
172         ber_pvt_sb_buf_destroy( &p->buf_in );
173         ber_pvt_sb_buf_destroy( &p->buf_out );
174         LBER_FREE( p );
175         sbiod->sbiod_pvt = NULL;
176         return 0;
177 }
178
179 static ber_len_t
180 sb_sasl_pkt_length( const unsigned char *buf, unsigned max, int debuglevel )
181 {
182         ber_len_t               size;
183
184         assert( buf != NULL );
185
186         size = buf[0] << 24
187                 | buf[1] << 16
188                 | buf[2] << 8
189                 | buf[3];
190    
191         if ( size > SASL_MAX_BUFF_SIZE ) {
192                 /* somebody is trying to mess me up. */
193                 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
194                         "sb_sasl_pkt_length: received illegal packet length "
195                         "of %lu bytes\n", (unsigned long)size );      
196                 size = 16; /* this should lead to an error. */
197         }
198
199         return size + 4; /* include the size !!! */
200 }
201
202 /* Drop a processed packet from the input buffer */
203 static void
204 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, unsigned max, int debuglevel )
205 {
206         ber_slen_t                      len;
207
208         len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
209         if ( len > 0 )
210                 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
211                         sec_buf_in->buf_end, len );
212    
213         if ( len >= 4 ) {
214                 sec_buf_in->buf_end = sb_sasl_pkt_length( sec_buf_in->buf_base,
215                         max, debuglevel);
216         }
217         else {
218                 sec_buf_in->buf_end = 0;
219         }
220         sec_buf_in->buf_ptr = len;
221 }
222
223 static ber_slen_t
224 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
225 {
226         struct sb_sasl_data     *p;
227         ber_slen_t              ret, bufptr;
228    
229         assert( sbiod != NULL );
230         assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
231
232         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
233
234         /* Are there anything left in the buffer? */
235         ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
236         bufptr = ret;
237         len -= ret;
238
239         if ( len == 0 )
240                 return bufptr;
241
242 #if SASL_VERSION_MAJOR >= 2
243         ber_pvt_sb_buf_init( &p->buf_in );
244 #else
245         ber_pvt_sb_buf_destroy( &p->buf_in );
246 #endif
247
248         /* Read the length of the packet */
249         while ( p->sec_buf_in.buf_ptr < 4 ) {
250                 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
251                         4 - p->sec_buf_in.buf_ptr );
252 #ifdef EINTR
253                 if ( ( ret < 0 ) && ( errno == EINTR ) )
254                         continue;
255 #endif
256                 if ( ret <= 0 )
257                         return bufptr ? bufptr : ret;
258
259                 p->sec_buf_in.buf_ptr += ret;
260         }
261
262         /* The new packet always starts at p->sec_buf_in.buf_base */
263         ret = sb_sasl_pkt_length( p->sec_buf_in.buf_base,
264                 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
265
266         /* Grow the packet buffer if neccessary */
267         if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) && 
268                 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
269         {
270                 errno = ENOMEM;
271                 return -1;
272         }
273         p->sec_buf_in.buf_end = ret;
274
275         /* Did we read the whole encrypted packet? */
276         while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
277                 /* No, we have got only a part of it */
278                 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
279
280                 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
281                         p->sec_buf_in.buf_ptr, ret );
282 #ifdef EINTR
283                 if ( ( ret < 0 ) && ( errno == EINTR ) )
284                         continue;
285 #endif
286                 if ( ret <= 0 )
287                         return bufptr ? bufptr : ret;
288
289                 p->sec_buf_in.buf_ptr += ret;
290         }
291
292         /* Decode the packet */
293         ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
294                 p->sec_buf_in.buf_end,
295                 (SASL_CONST char **)&p->buf_in.buf_base,
296                 (unsigned *)&p->buf_in.buf_end );
297
298         /* Drop the packet from the input buffer */
299         sb_sasl_drop_packet( &p->sec_buf_in,
300                         *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
301
302         if ( ret != SASL_OK ) {
303                 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
304                         "sb_sasl_read: failed to decode packet: %s\n",
305                         sasl_errstring( ret, NULL, NULL ) );
306                 errno = EIO;
307                 return -1;
308         }
309         
310         p->buf_in.buf_size = p->buf_in.buf_end;
311
312         bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
313
314         return bufptr;
315 }
316
317 static ber_slen_t
318 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
319 {
320         struct sb_sasl_data     *p;
321         int                     ret;
322
323         assert( sbiod != NULL );
324         assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
325
326         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
327
328         /* Are there anything left in the buffer? */
329         if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
330                 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
331                 if ( ret < 0 )
332                         return ret;
333                 /* Still have something left?? */
334                 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
335                         errno = EAGAIN;
336                         return 0;
337                 }
338         }
339
340         /* now encode the next packet. */
341 #if SASL_VERSION_MAJOR >= 2
342         ber_pvt_sb_buf_init( &p->buf_out );
343         /* sasl v2 makes sure this number is correct */
344         if ( len > *p->sasl_maxbuf )
345                 len = *p->sasl_maxbuf;
346 #else
347         ber_pvt_sb_buf_destroy( &p->buf_out );
348         if ( len > *p->sasl_maxbuf - 100 )
349                 len = *p->sasl_maxbuf - 100;    /* For safety margin */
350 #endif
351         ret = sasl_encode( p->sasl_context, buf, len,
352                 (SASL_CONST char **)&p->buf_out.buf_base,
353                 (unsigned *)&p->buf_out.buf_size );
354         if ( ret != SASL_OK ) {
355                 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
356                         "sb_sasl_write: failed to encode packet: %s\n",
357                         sasl_errstring( ret, NULL, NULL ) );
358                 return -1;
359         }
360         p->buf_out.buf_end = p->buf_out.buf_size;
361
362         ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
363         if ( ret <= 0 )
364                 return ret;
365         return len;
366 }
367
368 static int
369 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
370 {
371         struct sb_sasl_data     *p;
372
373         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
374
375         if ( opt == LBER_SB_OPT_DATA_READY ) {
376                 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
377                         return 1;
378         }
379         
380         return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
381 }
382
383 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
384         sb_sasl_setup,          /* sbi_setup */
385         sb_sasl_remove,         /* sbi_remove */
386         sb_sasl_ctrl,           /* sbi_ctrl */
387         sb_sasl_read,           /* sbi_read */
388         sb_sasl_write,          /* sbi_write */
389         NULL                    /* sbi_close */
390 };
391
392 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
393 {
394 #ifdef NEW_LOGGING
395         LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_install\n", 0, 0, 0 );
396 #else
397         Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
398                 0, 0, 0 );
399 #endif
400
401         /* don't install the stuff unless security has been negotiated */
402
403         if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
404                         &ldap_pvt_sockbuf_io_sasl ) )
405         {
406 #ifdef LDAP_DEBUG
407                 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
408                         LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
409 #endif
410                 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
411                         LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
412         }
413
414         return LDAP_SUCCESS;
415 }
416
417 void ldap_pvt_sasl_remove( Sockbuf *sb )
418 {
419         ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
420                 LBER_SBIOD_LEVEL_APPLICATION );
421 #ifdef LDAP_DEBUG
422         ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
423                 LBER_SBIOD_LEVEL_APPLICATION );
424 #endif
425 }
426
427 static int
428 sasl_err2ldap( int saslerr )
429 {
430         int rc;
431
432         switch (saslerr) {
433                 case SASL_CONTINUE:
434                         rc = LDAP_MORE_RESULTS_TO_RETURN;
435                         break;
436                 case SASL_INTERACT:
437                         rc = LDAP_LOCAL_ERROR;
438                         break;
439                 case SASL_OK:
440                         rc = LDAP_SUCCESS;
441                         break;
442                 case SASL_FAIL:
443                         rc = LDAP_LOCAL_ERROR;
444                         break;
445                 case SASL_NOMEM:
446                         rc = LDAP_NO_MEMORY;
447                         break;
448                 case SASL_NOMECH:
449                         rc = LDAP_AUTH_UNKNOWN;
450                         break;
451                 case SASL_BADAUTH:
452                         rc = LDAP_AUTH_UNKNOWN;
453                         break;
454                 case SASL_NOAUTHZ:
455                         rc = LDAP_PARAM_ERROR;
456                         break;
457                 case SASL_TOOWEAK:
458                 case SASL_ENCRYPT:
459                         rc = LDAP_AUTH_UNKNOWN;
460                         break;
461                 default:
462                         rc = LDAP_LOCAL_ERROR;
463                         break;
464         }
465
466         assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
467         return rc;
468 }
469
470 int
471 ldap_int_sasl_open(
472         LDAP *ld, 
473         LDAPConn *lc,
474         const char * host )
475 {
476         int rc;
477         sasl_conn_t *ctx;
478
479         assert( lc->lconn_sasl_ctx == NULL );
480
481         if ( host == NULL ) {
482                 ld->ld_errno = LDAP_LOCAL_ERROR;
483                 return ld->ld_errno;
484         }
485
486 #if SASL_VERSION_MAJOR >= 2
487         rc = sasl_client_new( "ldap", host, NULL, NULL,
488                 NULL, 0, &ctx );
489 #else
490         rc = sasl_client_new( "ldap", host, NULL,
491                 SASL_SECURITY_LAYER, &ctx );
492 #endif
493
494         if ( rc != SASL_OK ) {
495                 ld->ld_errno = sasl_err2ldap( rc );
496                 return ld->ld_errno;
497         }
498
499 #ifdef NEW_LOGGING
500         LDAP_LOG ( TRANSPORT, DETAIL1, "ldap_int_sasl_open: host=%s\n", 
501                 host, 0, 0 );
502 #else
503         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
504                 host, 0, 0 );
505 #endif
506
507         lc->lconn_sasl_ctx = ctx;
508
509         return LDAP_SUCCESS;
510 }
511
512 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
513 {
514         sasl_conn_t *ctx = lc->lconn_sasl_ctx;
515
516         if( ctx != NULL ) {
517                 sasl_dispose( &ctx );
518                 lc->lconn_sasl_ctx = NULL;
519         }
520
521         return LDAP_SUCCESS;
522 }
523
524 int
525 ldap_int_sasl_bind(
526         LDAP                    *ld,
527         const char              *dn,
528         const char              *mechs,
529         LDAPControl             **sctrls,
530         LDAPControl             **cctrls,
531         unsigned                flags,
532         LDAP_SASL_INTERACT_PROC *interact,
533         void * defaults )
534 {
535         char *data;
536         const char *mech = NULL;
537         const char *pmech = NULL;
538         int                     saslrc, rc;
539         sasl_ssf_t              *ssf = NULL;
540         sasl_conn_t     *ctx;
541         sasl_interact_t *prompts = NULL;
542         unsigned credlen;
543         struct berval ccred;
544         ber_socket_t            sd;
545         void    *ssl;
546
547 #ifdef NEW_LOGGING
548         LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n", 
549                 mechs ? mechs : "<null>", 0, 0 );
550 #else
551         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
552                 mechs ? mechs : "<null>", 0, 0 );
553 #endif
554
555         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
556         if (ld->ld_version < LDAP_VERSION3) {
557                 ld->ld_errno = LDAP_NOT_SUPPORTED;
558                 return ld->ld_errno;
559         }
560
561         ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
562
563         if ( sd == AC_SOCKET_INVALID ) {
564                 /* not connected yet */
565                 int rc;
566
567                 rc = ldap_open_defconn( ld );
568                 if( rc < 0 ) return ld->ld_errno;
569
570                 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
571
572                 if( sd == AC_SOCKET_INVALID ) {
573                         ld->ld_errno = LDAP_LOCAL_ERROR;
574                         return ld->ld_errno;
575                 }
576         }   
577
578         ctx = ld->ld_defconn->lconn_sasl_ctx;
579
580         /* If we already have a context, shut it down */
581         if( ctx ) {
582                 int msgid;
583                 LDAPMessage *result;
584                 /* Do an anonymous bind to kill the server's context */
585                 msgid = ldap_simple_bind( ld, "", NULL );
586
587                 /* dispose of the old context */
588                 ldap_int_sasl_close( ld, ld->ld_defconn );
589                 ldap_pvt_sasl_remove( ld->ld_sb );
590
591                 /* The reply is sent in the clear, we can't read it
592                  * until after the context and sockbuf are torn down
593                  */
594                 rc = ldap_result( ld, msgid, 1, NULL, &result );
595                 ldap_msgfree( result );
596         }
597
598         rc = ldap_int_sasl_open( ld, ld->ld_defconn,
599                 ld->ld_defconn->lconn_server->lud_host ?
600                 ld->ld_defconn->lconn_server->lud_host : "localhost" );
601                 
602         if ( rc != LDAP_SUCCESS ) return rc;
603
604         ctx = ld->ld_defconn->lconn_sasl_ctx;
605
606         /* Check for TLS */
607         ssl = ldap_pvt_tls_sb_ctx( ld->ld_sb );
608         if ( ssl ) {
609                 struct berval authid = { 0, NULL };
610                 ber_len_t fac;
611
612                 fac = ldap_pvt_tls_get_strength( ssl );
613                 /* failure is OK, we just can't use SASL EXTERNAL */
614                 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
615
616                 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
617                 LDAP_FREE( authid.bv_val );
618         }
619
620         /* Check for local */
621         if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
622                 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
623                         "cn=peercred,cn=external,cn=auth")];
624                 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
625                         "cn=peercred,cn=external,cn=auth",
626                         (int) geteuid(), (int) getegid() );
627                 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
628         }
629
630         /* (re)set security properties */
631         sasl_setprop( ctx, SASL_SEC_PROPS,
632                 &ld->ld_options.ldo_sasl_secprops );
633
634         ccred.bv_val = NULL;
635         ccred.bv_len = 0;
636
637         do {
638                 saslrc = sasl_client_start( ctx,
639                         mechs,
640 #if SASL_VERSION_MAJOR < 2
641                         NULL,
642 #endif
643                         &prompts,
644                         (SASL_CONST char **)&ccred.bv_val,
645                         &credlen,
646                         &mech );
647
648                 if( pmech == NULL && mech != NULL ) {
649                         pmech = mech;
650
651                         if( flags != LDAP_SASL_QUIET ) {
652                                 fprintf(stderr,
653                                         "SASL/%s authentication started\n",
654                                         pmech );
655                         }
656                 }
657
658                 if( saslrc == SASL_INTERACT ) {
659                         int res;
660                         if( !interact ) break;
661                         res = (interact)( ld, flags, defaults, prompts );
662
663                         if( res != LDAP_SUCCESS ) break;
664                 }
665         } while ( saslrc == SASL_INTERACT );
666
667         ccred.bv_len = credlen;
668
669         if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
670                 rc = ld->ld_errno = sasl_err2ldap( saslrc );
671 #if SASL_VERSION_MAJOR >= 2
672                 ld->ld_error = (char *)sasl_errdetail( ctx );
673 #endif
674                 goto done;
675         }
676
677         do {
678                 struct berval *scred;
679                 unsigned credlen;
680
681                 scred = NULL;
682
683                 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
684
685                 if ( ccred.bv_val != NULL ) {
686 #if SASL_VERSION_MAJOR < 2
687                         LDAP_FREE( ccred.bv_val );
688 #endif
689                         ccred.bv_val = NULL;
690                 }
691
692                 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
693                         if( scred && scred->bv_len ) {
694                                 /* and server provided us with data? */
695 #ifdef NEW_LOGGING
696                                 LDAP_LOG ( TRANSPORT, DETAIL1, 
697                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n", 
698                                         rc, saslrc, scred->bv_len );
699 #else
700                                 Debug( LDAP_DEBUG_TRACE,
701                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
702                                         rc, saslrc, scred->bv_len );
703 #endif
704                                 ber_bvfree( scred );
705                         }
706                         rc = ld->ld_errno;
707                         goto done;
708                 }
709
710                 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
711                         /* we're done, no need to step */
712                         if( scred && scred->bv_len ) {
713                                 /* but server provided us with data! */
714 #ifdef NEW_LOGGING
715                                 LDAP_LOG ( TRANSPORT, DETAIL1, 
716                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n", 
717                                         rc, saslrc, scred->bv_len );
718 #else
719                                 Debug( LDAP_DEBUG_TRACE,
720                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
721                                         rc, saslrc, scred->bv_len );
722 #endif
723                                 ber_bvfree( scred );
724                                 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
725                                 goto done;
726                         }
727                         break;
728                 }
729
730                 do {
731                         saslrc = sasl_client_step( ctx,
732                                 (scred == NULL) ? NULL : scred->bv_val,
733                                 (scred == NULL) ? 0 : scred->bv_len,
734                                 &prompts,
735                                 (SASL_CONST char **)&ccred.bv_val,
736                                 &credlen );
737
738 #ifdef NEW_LOGGING
739                                 LDAP_LOG ( TRANSPORT, DETAIL1, 
740                                         "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
741 #else
742                         Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
743                                 saslrc, 0, 0 );
744 #endif
745
746                         if( saslrc == SASL_INTERACT ) {
747                                 int res;
748                                 if( !interact ) break;
749                                 res = (interact)( ld, flags, defaults, prompts );
750                                 if( res != LDAP_SUCCESS ) break;
751                         }
752                 } while ( saslrc == SASL_INTERACT );
753
754                 ccred.bv_len = credlen;
755                 ber_bvfree( scred );
756
757                 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
758                         ld->ld_errno = sasl_err2ldap( saslrc );
759 #if SASL_VERSION_MAJOR >= 2
760                         ld->ld_error = (char *)sasl_errdetail( ctx );
761 #endif
762                         rc = ld->ld_errno;
763                         goto done;
764                 }
765         } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
766
767         if ( rc != LDAP_SUCCESS ) goto done;
768
769         if ( saslrc != SASL_OK ) {
770 #if SASL_VERSION_MAJOR >= 2
771                 ld->ld_error = (char *)sasl_errdetail( ctx );
772 #endif
773                 rc = ld->ld_errno = sasl_err2ldap( saslrc );
774                 goto done;
775         }
776
777         if( flags != LDAP_SASL_QUIET ) {
778                 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
779                 if( saslrc == SASL_OK && data && *data ) {
780                         fprintf( stderr, "SASL username: %s\n", data );
781                 }
782
783 #if SASL_VERSION_MAJOR < 2
784                 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
785                 if( saslrc == SASL_OK && data && *data ) {
786                         fprintf( stderr, "SASL realm: %s\n", data );
787                 }
788 #endif
789         }
790
791         saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
792         if( saslrc == SASL_OK ) {
793                 if( flags != LDAP_SASL_QUIET ) {
794                         fprintf( stderr, "SASL SSF: %lu\n",
795                                 (unsigned long) *ssf );
796                 }
797
798                 if( ssf && *ssf ) {
799                         if( flags != LDAP_SASL_QUIET ) {
800                                 fprintf( stderr, "SASL installing layers\n" );
801                         }
802                         ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
803                 }
804         }
805
806 done:
807         return rc;
808 }
809
810 int
811 ldap_int_sasl_external(
812         LDAP *ld,
813         LDAPConn *conn,
814         const char * authid,
815         ber_len_t ssf )
816 {
817         int sc;
818         sasl_conn_t *ctx;
819 #if SASL_VERSION_MAJOR < 2
820         sasl_external_properties_t extprops;
821 #endif
822
823         ctx = conn->lconn_sasl_ctx;
824
825         if ( ctx == NULL ) {
826                 return LDAP_LOCAL_ERROR;
827         }
828    
829 #if SASL_VERSION_MAJOR >= 2
830         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
831         if ( sc == SASL_OK )
832                 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
833 #else
834         memset( &extprops, '\0', sizeof(extprops) );
835         extprops.ssf = ssf;
836         extprops.auth_id = (char *) authid;
837
838         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
839                 (void *) &extprops );
840 #endif
841
842         if ( sc != SASL_OK ) {
843                 return LDAP_LOCAL_ERROR;
844         }
845
846         return LDAP_SUCCESS;
847 }
848
849
850 int ldap_pvt_sasl_secprops(
851         const char *in,
852         sasl_security_properties_t *secprops )
853 {
854         int i;
855         char **props = ldap_str2charray( in, "," );
856         unsigned sflags = 0;
857         int got_sflags = 0;
858         sasl_ssf_t max_ssf = 0;
859         int got_max_ssf = 0;
860         sasl_ssf_t min_ssf = 0;
861         int got_min_ssf = 0;
862         unsigned maxbufsize = 0;
863         int got_maxbufsize = 0;
864
865         if( props == NULL || secprops == NULL ) {
866                 return LDAP_PARAM_ERROR;
867         }
868
869         for( i=0; props[i]; i++ ) {
870                 if( !strcasecmp(props[i], "none") ) {
871                         got_sflags++;
872
873                 } else if( !strcasecmp(props[i], "noplain") ) {
874                         got_sflags++;
875                         sflags |= SASL_SEC_NOPLAINTEXT;
876
877                 } else if( !strcasecmp(props[i], "noactive") ) {
878                         got_sflags++;
879                         sflags |= SASL_SEC_NOACTIVE;
880
881                 } else if( !strcasecmp(props[i], "nodict") ) {
882                         got_sflags++;
883                         sflags |= SASL_SEC_NODICTIONARY;
884
885                 } else if( !strcasecmp(props[i], "forwardsec") ) {
886                         got_sflags++;
887                         sflags |= SASL_SEC_FORWARD_SECRECY;
888
889                 } else if( !strcasecmp(props[i], "noanonymous")) {
890                         got_sflags++;
891                         sflags |= SASL_SEC_NOANONYMOUS;
892
893                 } else if( !strcasecmp(props[i], "passcred") ) {
894                         got_sflags++;
895                         sflags |= SASL_SEC_PASS_CREDENTIALS;
896
897                 } else if( !strncasecmp(props[i],
898                         "minssf=", sizeof("minssf")) )
899                 {
900                         if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
901                                 got_min_ssf++;
902                                 min_ssf = atoi( &props[i][sizeof("minssf")] );
903                         } else {
904                                 return LDAP_NOT_SUPPORTED;
905                         }
906
907                 } else if( !strncasecmp(props[i],
908                         "maxssf=", sizeof("maxssf")) )
909                 {
910                         if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
911                                 got_max_ssf++;
912                                 max_ssf = atoi( &props[i][sizeof("maxssf")] );
913                         } else {
914                                 return LDAP_NOT_SUPPORTED;
915                         }
916
917                 } else if( !strncasecmp(props[i],
918                         "maxbufsize=", sizeof("maxbufsize")) )
919                 {
920                         if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
921                                 got_maxbufsize++;
922                                 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
923                         } else {
924                                 return LDAP_NOT_SUPPORTED;
925                         }
926
927                         if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
928                                 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
929                         {
930                                 /* bad maxbufsize */
931                                 return LDAP_PARAM_ERROR;
932                         }
933
934                 } else {
935                         return LDAP_NOT_SUPPORTED;
936                 }
937         }
938
939         if(got_sflags) {
940                 secprops->security_flags = sflags;
941         }
942         if(got_min_ssf) {
943                 secprops->min_ssf = min_ssf;
944         }
945         if(got_max_ssf) {
946                 secprops->max_ssf = max_ssf;
947         }
948         if(got_maxbufsize) {
949                 secprops->maxbufsize = maxbufsize;
950         }
951
952         ldap_charray_free( props );
953         return LDAP_SUCCESS;
954 }
955
956 int
957 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
958 {
959         int rc;
960
961         switch( option ) {
962         case LDAP_OPT_X_SASL_SECPROPS:
963                 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
964                 if( rc == LDAP_SUCCESS ) return 0;
965         }
966
967         return -1;
968 }
969
970 int
971 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
972 {
973         if ( ld == NULL )
974                 return -1;
975
976         switch ( option ) {
977                 case LDAP_OPT_X_SASL_MECH: {
978                         *(char **)arg = ld->ld_options.ldo_def_sasl_mech
979                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
980                 } break;
981                 case LDAP_OPT_X_SASL_REALM: {
982                         *(char **)arg = ld->ld_options.ldo_def_sasl_realm
983                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
984                 } break;
985                 case LDAP_OPT_X_SASL_AUTHCID: {
986                         *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
987                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
988                 } break;
989                 case LDAP_OPT_X_SASL_AUTHZID: {
990                         *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
991                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
992                 } break;
993
994                 case LDAP_OPT_X_SASL_SSF: {
995                         int sc;
996                         sasl_ssf_t      *ssf;
997                         sasl_conn_t *ctx;
998
999                         if( ld->ld_defconn == NULL ) {
1000                                 return -1;
1001                         }
1002
1003                         ctx = ld->ld_defconn->lconn_sasl_ctx;
1004
1005                         if ( ctx == NULL ) {
1006                                 return -1;
1007                         }
1008
1009                         sc = sasl_getprop( ctx, SASL_SSF,
1010                                 (SASL_CONST void **) &ssf );
1011
1012                         if ( sc != SASL_OK ) {
1013                                 return -1;
1014                         }
1015
1016                         *(ber_len_t *)arg = *ssf;
1017                 } break;
1018
1019                 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1020                         /* this option is write only */
1021                         return -1;
1022
1023                 case LDAP_OPT_X_SASL_SSF_MIN:
1024                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1025                         break;
1026                 case LDAP_OPT_X_SASL_SSF_MAX:
1027                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1028                         break;
1029                 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1030                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1031                         break;
1032
1033                 case LDAP_OPT_X_SASL_SECPROPS:
1034                         /* this option is write only */
1035                         return -1;
1036
1037                 default:
1038                         return -1;
1039         }
1040         return 0;
1041 }
1042
1043 int
1044 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1045 {
1046         if ( ld == NULL )
1047                 return -1;
1048
1049         switch ( option ) {
1050         case LDAP_OPT_X_SASL_SSF:
1051                 /* This option is read-only */
1052                 return -1;
1053
1054         case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1055                 int sc;
1056 #if SASL_VERSION_MAJOR < 2
1057                 sasl_external_properties_t extprops;
1058 #endif
1059                 sasl_conn_t *ctx;
1060
1061                 if( ld->ld_defconn == NULL ) {
1062                         return -1;
1063                 }
1064
1065                 ctx = ld->ld_defconn->lconn_sasl_ctx;
1066
1067                 if ( ctx == NULL ) {
1068                         return -1;
1069                 }
1070
1071 #if SASL_VERSION_MAJOR >= 2
1072                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1073 #else
1074                 memset(&extprops, 0L, sizeof(extprops));
1075
1076                 extprops.ssf = * (ber_len_t *) arg;
1077
1078                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1079                         (void *) &extprops );
1080 #endif
1081
1082                 if ( sc != SASL_OK ) {
1083                         return -1;
1084                 }
1085                 } break;
1086
1087         case LDAP_OPT_X_SASL_SSF_MIN:
1088                 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1089                 break;
1090         case LDAP_OPT_X_SASL_SSF_MAX:
1091                 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1092                 break;
1093         case LDAP_OPT_X_SASL_MAXBUFSIZE:
1094                 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1095                 break;
1096
1097         case LDAP_OPT_X_SASL_SECPROPS: {
1098                 int sc;
1099                 sc = ldap_pvt_sasl_secprops( (char *) arg,
1100                         &ld->ld_options.ldo_sasl_secprops );
1101
1102                 return sc == LDAP_SUCCESS ? 0 : -1;
1103                 }
1104
1105         default:
1106                 return -1;
1107         }
1108         return 0;
1109 }
1110
1111 #ifdef LDAP_R_COMPILE
1112 #define LDAP_DEBUG_R_SASL
1113 void *ldap_pvt_sasl_mutex_new(void)
1114 {
1115         ldap_pvt_thread_mutex_t *mutex;
1116
1117         mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1118                 sizeof(ldap_pvt_thread_mutex_t) );
1119
1120         if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1121                 return mutex;
1122         }
1123 #ifndef LDAP_DEBUG_R_SASL
1124         assert( 0 );
1125 #endif /* !LDAP_DEBUG_R_SASL */
1126         return NULL;
1127 }
1128
1129 int ldap_pvt_sasl_mutex_lock(void *mutex)
1130 {
1131 #ifdef LDAP_DEBUG_R_SASL
1132         if ( mutex == NULL ) {
1133                 return SASL_OK;
1134         }
1135 #else /* !LDAP_DEBUG_R_SASL */
1136         assert( mutex );
1137 #endif /* !LDAP_DEBUG_R_SASL */
1138         return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1139                 ? SASL_FAIL : SASL_OK;
1140 }
1141
1142 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1143 {
1144 #ifdef LDAP_DEBUG_R_SASL
1145         if ( mutex == NULL ) {
1146                 return SASL_OK;
1147         }
1148 #else /* !LDAP_DEBUG_R_SASL */
1149         assert( mutex );
1150 #endif /* !LDAP_DEBUG_R_SASL */
1151         return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1152                 ? SASL_FAIL : SASL_OK;
1153 }
1154
1155 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1156 {
1157 #ifdef LDAP_DEBUG_R_SASL
1158         if ( mutex == NULL ) {
1159                 return;
1160         }
1161 #else /* !LDAP_DEBUG_R_SASL */
1162         assert( mutex );
1163 #endif /* !LDAP_DEBUG_R_SASL */
1164         (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1165         LDAP_FREE( mutex );
1166 }
1167 #endif
1168
1169 #else
1170 int ldap_int_sasl_init( void )
1171 { return LDAP_SUCCESS; }
1172
1173 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1174 { return LDAP_SUCCESS; }
1175
1176 int
1177 ldap_int_sasl_bind(
1178         LDAP                    *ld,
1179         const char              *dn,
1180         const char              *mechs,
1181         LDAPControl             **sctrls,
1182         LDAPControl             **cctrls,
1183         unsigned                flags,
1184         LDAP_SASL_INTERACT_PROC *interact,
1185         void * defaults )
1186 { return LDAP_NOT_SUPPORTED; }
1187
1188 int
1189 ldap_int_sasl_external(
1190         LDAP *ld,
1191         LDAPConn *conn,
1192         const char * authid,
1193         ber_len_t ssf )
1194 { return LDAP_SUCCESS; }
1195
1196 #endif /* HAVE_CYRUS_SASL */