]> git.sur5r.net Git - openldap/blob - libraries/libldap/cyrus.c
ITS#2424 use two SASL contexts per session to conform to RFC 2222
[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_authctx == 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_authctx = 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_authctx;
515
516         if( ctx != NULL ) {
517                 sasl_dispose( &ctx );
518                 if ( lc->lconn_sasl_sockctx && ctx != lc->lconn_sasl_sockctx ) {
519                         ctx = lc->lconn_sasl_sockctx;
520                         sasl_dispose( &ctx );
521                 }
522                 lc->lconn_sasl_sockctx = NULL;
523                 lc->lconn_sasl_authctx = NULL;
524         }
525
526         return LDAP_SUCCESS;
527 }
528
529 int
530 ldap_int_sasl_bind(
531         LDAP                    *ld,
532         const char              *dn,
533         const char              *mechs,
534         LDAPControl             **sctrls,
535         LDAPControl             **cctrls,
536         unsigned                flags,
537         LDAP_SASL_INTERACT_PROC *interact,
538         void * defaults )
539 {
540         char *data;
541         const char *mech = NULL;
542         const char *pmech = NULL;
543         int                     saslrc, rc;
544         sasl_ssf_t              *ssf = NULL;
545         sasl_conn_t     *ctx, *oldctx = NULL;
546         sasl_interact_t *prompts = NULL;
547         unsigned credlen;
548         struct berval ccred;
549         ber_socket_t            sd;
550         void    *ssl;
551
552 #ifdef NEW_LOGGING
553         LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n", 
554                 mechs ? mechs : "<null>", 0, 0 );
555 #else
556         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
557                 mechs ? mechs : "<null>", 0, 0 );
558 #endif
559
560         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
561         if (ld->ld_version < LDAP_VERSION3) {
562                 ld->ld_errno = LDAP_NOT_SUPPORTED;
563                 return ld->ld_errno;
564         }
565
566         ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
567
568         if ( sd == AC_SOCKET_INVALID ) {
569                 /* not connected yet */
570                 int rc;
571
572                 rc = ldap_open_defconn( ld );
573                 if( rc < 0 ) return ld->ld_errno;
574
575                 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
576
577                 if( sd == AC_SOCKET_INVALID ) {
578                         ld->ld_errno = LDAP_LOCAL_ERROR;
579                         return ld->ld_errno;
580                 }
581         }   
582
583         oldctx = ld->ld_defconn->lconn_sasl_authctx;
584
585         /* If we already have an authentication context, clear it out */
586         if( oldctx ) {
587                 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
588                         sasl_dispose( &oldctx );
589                 }
590                 ld->ld_defconn->lconn_sasl_authctx = NULL;
591         }
592
593         rc = ldap_int_sasl_open( ld, ld->ld_defconn,
594                 ld->ld_defconn->lconn_server->lud_host ?
595                 ld->ld_defconn->lconn_server->lud_host : "localhost" );
596
597         if ( rc != LDAP_SUCCESS ) return rc;
598
599         ctx = ld->ld_defconn->lconn_sasl_authctx;
600
601         /* Check for TLS */
602         ssl = ldap_pvt_tls_sb_ctx( ld->ld_sb );
603         if ( ssl ) {
604                 struct berval authid = { 0, NULL };
605                 ber_len_t fac;
606
607                 fac = ldap_pvt_tls_get_strength( ssl );
608                 /* failure is OK, we just can't use SASL EXTERNAL */
609                 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
610
611                 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
612                 LDAP_FREE( authid.bv_val );
613         }
614
615         /* Check for local */
616         if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
617                 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
618                         "cn=peercred,cn=external,cn=auth")];
619                 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
620                         "cn=peercred,cn=external,cn=auth",
621                         (int) geteuid(), (int) getegid() );
622                 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
623         }
624
625         /* (re)set security properties */
626         sasl_setprop( ctx, SASL_SEC_PROPS,
627                 &ld->ld_options.ldo_sasl_secprops );
628
629         ccred.bv_val = NULL;
630         ccred.bv_len = 0;
631
632         do {
633                 saslrc = sasl_client_start( ctx,
634                         mechs,
635 #if SASL_VERSION_MAJOR < 2
636                         NULL,
637 #endif
638                         &prompts,
639                         (SASL_CONST char **)&ccred.bv_val,
640                         &credlen,
641                         &mech );
642
643                 if( pmech == NULL && mech != NULL ) {
644                         pmech = mech;
645
646                         if( flags != LDAP_SASL_QUIET ) {
647                                 fprintf(stderr,
648                                         "SASL/%s authentication started\n",
649                                         pmech );
650                         }
651                 }
652
653                 if( saslrc == SASL_INTERACT ) {
654                         int res;
655                         if( !interact ) break;
656                         res = (interact)( ld, flags, defaults, prompts );
657
658                         if( res != LDAP_SUCCESS ) break;
659                 }
660         } while ( saslrc == SASL_INTERACT );
661
662         ccred.bv_len = credlen;
663
664         if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
665                 rc = ld->ld_errno = sasl_err2ldap( saslrc );
666 #if SASL_VERSION_MAJOR >= 2
667                 ld->ld_error = (char *)sasl_errdetail( ctx );
668 #endif
669                 goto done;
670         }
671
672         do {
673                 struct berval *scred;
674                 unsigned credlen;
675
676                 scred = NULL;
677
678                 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
679
680                 if ( ccred.bv_val != NULL ) {
681 #if SASL_VERSION_MAJOR < 2
682                         LDAP_FREE( ccred.bv_val );
683 #endif
684                         ccred.bv_val = NULL;
685                 }
686
687                 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
688                         if( scred && scred->bv_len ) {
689                                 /* and server provided us with data? */
690 #ifdef NEW_LOGGING
691                                 LDAP_LOG ( TRANSPORT, DETAIL1, 
692                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n", 
693                                         rc, saslrc, scred->bv_len );
694 #else
695                                 Debug( LDAP_DEBUG_TRACE,
696                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
697                                         rc, saslrc, scred->bv_len );
698 #endif
699                                 ber_bvfree( scred );
700                         }
701                         rc = ld->ld_errno;
702                         goto done;
703                 }
704
705                 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
706                         /* we're done, no need to step */
707                         if( scred && scred->bv_len ) {
708                                 /* but server provided us with data! */
709 #ifdef NEW_LOGGING
710                                 LDAP_LOG ( TRANSPORT, DETAIL1, 
711                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n", 
712                                         rc, saslrc, scred->bv_len );
713 #else
714                                 Debug( LDAP_DEBUG_TRACE,
715                                         "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
716                                         rc, saslrc, scred->bv_len );
717 #endif
718                                 ber_bvfree( scred );
719                                 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
720                                 goto done;
721                         }
722                         break;
723                 }
724
725                 do {
726                         saslrc = sasl_client_step( ctx,
727                                 (scred == NULL) ? NULL : scred->bv_val,
728                                 (scred == NULL) ? 0 : scred->bv_len,
729                                 &prompts,
730                                 (SASL_CONST char **)&ccred.bv_val,
731                                 &credlen );
732
733 #ifdef NEW_LOGGING
734                                 LDAP_LOG ( TRANSPORT, DETAIL1, 
735                                         "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
736 #else
737                         Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
738                                 saslrc, 0, 0 );
739 #endif
740
741                         if( saslrc == SASL_INTERACT ) {
742                                 int res;
743                                 if( !interact ) break;
744                                 res = (interact)( ld, flags, defaults, prompts );
745                                 if( res != LDAP_SUCCESS ) break;
746                         }
747                 } while ( saslrc == SASL_INTERACT );
748
749                 ccred.bv_len = credlen;
750                 ber_bvfree( scred );
751
752                 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
753                         ld->ld_errno = sasl_err2ldap( saslrc );
754 #if SASL_VERSION_MAJOR >= 2
755                         ld->ld_error = (char *)sasl_errdetail( ctx );
756 #endif
757                         rc = ld->ld_errno;
758                         goto done;
759                 }
760         } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
761
762         if ( rc != LDAP_SUCCESS ) goto done;
763
764         if ( saslrc != SASL_OK ) {
765 #if SASL_VERSION_MAJOR >= 2
766                 ld->ld_error = (char *)sasl_errdetail( ctx );
767 #endif
768                 rc = ld->ld_errno = sasl_err2ldap( saslrc );
769                 goto done;
770         }
771
772         if( flags != LDAP_SASL_QUIET ) {
773                 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
774                 if( saslrc == SASL_OK && data && *data ) {
775                         fprintf( stderr, "SASL username: %s\n", data );
776                 }
777
778 #if SASL_VERSION_MAJOR < 2
779                 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
780                 if( saslrc == SASL_OK && data && *data ) {
781                         fprintf( stderr, "SASL realm: %s\n", data );
782                 }
783 #endif
784         }
785
786         saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
787         if( saslrc == SASL_OK ) {
788                 if( flags != LDAP_SASL_QUIET ) {
789                         fprintf( stderr, "SASL SSF: %lu\n",
790                                 (unsigned long) *ssf );
791                 }
792
793                 if( ssf && *ssf ) {
794                         if( flags != LDAP_SASL_QUIET ) {
795                                 fprintf( stderr, "SASL installing layers\n" );
796                         }
797                         if ( ld->ld_defconn->lconn_sasl_sockctx ) {
798                                 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
799                                 sasl_dispose( &oldctx );
800                                 ldap_pvt_sasl_remove( ld->ld_sb );
801                         }
802                         ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
803                         ld->ld_defconn->lconn_sasl_sockctx = ctx;
804                 }
805         }
806         ld->ld_defconn->lconn_sasl_authctx = ctx;
807
808 done:
809         return rc;
810 }
811
812 int
813 ldap_int_sasl_external(
814         LDAP *ld,
815         LDAPConn *conn,
816         const char * authid,
817         ber_len_t ssf )
818 {
819         int sc;
820         sasl_conn_t *ctx;
821 #if SASL_VERSION_MAJOR < 2
822         sasl_external_properties_t extprops;
823 #endif
824
825         ctx = conn->lconn_sasl_authctx;
826
827         if ( ctx == NULL ) {
828                 return LDAP_LOCAL_ERROR;
829         }
830    
831 #if SASL_VERSION_MAJOR >= 2
832         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
833         if ( sc == SASL_OK )
834                 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
835 #else
836         memset( &extprops, '\0', sizeof(extprops) );
837         extprops.ssf = ssf;
838         extprops.auth_id = (char *) authid;
839
840         sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
841                 (void *) &extprops );
842 #endif
843
844         if ( sc != SASL_OK ) {
845                 return LDAP_LOCAL_ERROR;
846         }
847
848         return LDAP_SUCCESS;
849 }
850
851
852 int ldap_pvt_sasl_secprops(
853         const char *in,
854         sasl_security_properties_t *secprops )
855 {
856         int i;
857         char **props = ldap_str2charray( in, "," );
858         unsigned sflags = 0;
859         int got_sflags = 0;
860         sasl_ssf_t max_ssf = 0;
861         int got_max_ssf = 0;
862         sasl_ssf_t min_ssf = 0;
863         int got_min_ssf = 0;
864         unsigned maxbufsize = 0;
865         int got_maxbufsize = 0;
866
867         if( props == NULL || secprops == NULL ) {
868                 return LDAP_PARAM_ERROR;
869         }
870
871         for( i=0; props[i]; i++ ) {
872                 if( !strcasecmp(props[i], "none") ) {
873                         got_sflags++;
874
875                 } else if( !strcasecmp(props[i], "noplain") ) {
876                         got_sflags++;
877                         sflags |= SASL_SEC_NOPLAINTEXT;
878
879                 } else if( !strcasecmp(props[i], "noactive") ) {
880                         got_sflags++;
881                         sflags |= SASL_SEC_NOACTIVE;
882
883                 } else if( !strcasecmp(props[i], "nodict") ) {
884                         got_sflags++;
885                         sflags |= SASL_SEC_NODICTIONARY;
886
887                 } else if( !strcasecmp(props[i], "forwardsec") ) {
888                         got_sflags++;
889                         sflags |= SASL_SEC_FORWARD_SECRECY;
890
891                 } else if( !strcasecmp(props[i], "noanonymous")) {
892                         got_sflags++;
893                         sflags |= SASL_SEC_NOANONYMOUS;
894
895                 } else if( !strcasecmp(props[i], "passcred") ) {
896                         got_sflags++;
897                         sflags |= SASL_SEC_PASS_CREDENTIALS;
898
899                 } else if( !strncasecmp(props[i],
900                         "minssf=", sizeof("minssf")) )
901                 {
902                         if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
903                                 got_min_ssf++;
904                                 min_ssf = atoi( &props[i][sizeof("minssf")] );
905                         } else {
906                                 return LDAP_NOT_SUPPORTED;
907                         }
908
909                 } else if( !strncasecmp(props[i],
910                         "maxssf=", sizeof("maxssf")) )
911                 {
912                         if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
913                                 got_max_ssf++;
914                                 max_ssf = atoi( &props[i][sizeof("maxssf")] );
915                         } else {
916                                 return LDAP_NOT_SUPPORTED;
917                         }
918
919                 } else if( !strncasecmp(props[i],
920                         "maxbufsize=", sizeof("maxbufsize")) )
921                 {
922                         if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
923                                 got_maxbufsize++;
924                                 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
925                         } else {
926                                 return LDAP_NOT_SUPPORTED;
927                         }
928
929                         if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
930                                 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
931                         {
932                                 /* bad maxbufsize */
933                                 return LDAP_PARAM_ERROR;
934                         }
935
936                 } else {
937                         return LDAP_NOT_SUPPORTED;
938                 }
939         }
940
941         if(got_sflags) {
942                 secprops->security_flags = sflags;
943         }
944         if(got_min_ssf) {
945                 secprops->min_ssf = min_ssf;
946         }
947         if(got_max_ssf) {
948                 secprops->max_ssf = max_ssf;
949         }
950         if(got_maxbufsize) {
951                 secprops->maxbufsize = maxbufsize;
952         }
953
954         ldap_charray_free( props );
955         return LDAP_SUCCESS;
956 }
957
958 int
959 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
960 {
961         int rc;
962
963         switch( option ) {
964         case LDAP_OPT_X_SASL_SECPROPS:
965                 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
966                 if( rc == LDAP_SUCCESS ) return 0;
967         }
968
969         return -1;
970 }
971
972 int
973 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
974 {
975         if ( ld == NULL )
976                 return -1;
977
978         switch ( option ) {
979                 case LDAP_OPT_X_SASL_MECH: {
980                         *(char **)arg = ld->ld_options.ldo_def_sasl_mech
981                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
982                 } break;
983                 case LDAP_OPT_X_SASL_REALM: {
984                         *(char **)arg = ld->ld_options.ldo_def_sasl_realm
985                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
986                 } break;
987                 case LDAP_OPT_X_SASL_AUTHCID: {
988                         *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
989                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
990                 } break;
991                 case LDAP_OPT_X_SASL_AUTHZID: {
992                         *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
993                                 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
994                 } break;
995
996                 case LDAP_OPT_X_SASL_SSF: {
997                         int sc;
998                         sasl_ssf_t      *ssf;
999                         sasl_conn_t *ctx;
1000
1001                         if( ld->ld_defconn == NULL ) {
1002                                 return -1;
1003                         }
1004
1005                         ctx = ld->ld_defconn->lconn_sasl_sockctx;
1006
1007                         if ( ctx == NULL ) {
1008                                 return -1;
1009                         }
1010
1011                         sc = sasl_getprop( ctx, SASL_SSF,
1012                                 (SASL_CONST void **) &ssf );
1013
1014                         if ( sc != SASL_OK ) {
1015                                 return -1;
1016                         }
1017
1018                         *(ber_len_t *)arg = *ssf;
1019                 } break;
1020
1021                 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1022                         /* this option is write only */
1023                         return -1;
1024
1025                 case LDAP_OPT_X_SASL_SSF_MIN:
1026                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1027                         break;
1028                 case LDAP_OPT_X_SASL_SSF_MAX:
1029                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1030                         break;
1031                 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1032                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1033                         break;
1034
1035                 case LDAP_OPT_X_SASL_SECPROPS:
1036                         /* this option is write only */
1037                         return -1;
1038
1039                 default:
1040                         return -1;
1041         }
1042         return 0;
1043 }
1044
1045 int
1046 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1047 {
1048         if ( ld == NULL )
1049                 return -1;
1050
1051         switch ( option ) {
1052         case LDAP_OPT_X_SASL_SSF:
1053                 /* This option is read-only */
1054                 return -1;
1055
1056         case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1057                 int sc;
1058 #if SASL_VERSION_MAJOR < 2
1059                 sasl_external_properties_t extprops;
1060 #endif
1061                 sasl_conn_t *ctx;
1062
1063                 if( ld->ld_defconn == NULL ) {
1064                         return -1;
1065                 }
1066
1067                 ctx = ld->ld_defconn->lconn_sasl_authctx;
1068
1069                 if ( ctx == NULL ) {
1070                         return -1;
1071                 }
1072
1073 #if SASL_VERSION_MAJOR >= 2
1074                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1075 #else
1076                 memset(&extprops, 0L, sizeof(extprops));
1077
1078                 extprops.ssf = * (ber_len_t *) arg;
1079
1080                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1081                         (void *) &extprops );
1082 #endif
1083
1084                 if ( sc != SASL_OK ) {
1085                         return -1;
1086                 }
1087                 } break;
1088
1089         case LDAP_OPT_X_SASL_SSF_MIN:
1090                 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1091                 break;
1092         case LDAP_OPT_X_SASL_SSF_MAX:
1093                 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1094                 break;
1095         case LDAP_OPT_X_SASL_MAXBUFSIZE:
1096                 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1097                 break;
1098
1099         case LDAP_OPT_X_SASL_SECPROPS: {
1100                 int sc;
1101                 sc = ldap_pvt_sasl_secprops( (char *) arg,
1102                         &ld->ld_options.ldo_sasl_secprops );
1103
1104                 return sc == LDAP_SUCCESS ? 0 : -1;
1105                 }
1106
1107         default:
1108                 return -1;
1109         }
1110         return 0;
1111 }
1112
1113 #ifdef LDAP_R_COMPILE
1114 #define LDAP_DEBUG_R_SASL
1115 void *ldap_pvt_sasl_mutex_new(void)
1116 {
1117         ldap_pvt_thread_mutex_t *mutex;
1118
1119         mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1120                 sizeof(ldap_pvt_thread_mutex_t) );
1121
1122         if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1123                 return mutex;
1124         }
1125 #ifndef LDAP_DEBUG_R_SASL
1126         assert( 0 );
1127 #endif /* !LDAP_DEBUG_R_SASL */
1128         return NULL;
1129 }
1130
1131 int ldap_pvt_sasl_mutex_lock(void *mutex)
1132 {
1133 #ifdef LDAP_DEBUG_R_SASL
1134         if ( mutex == NULL ) {
1135                 return SASL_OK;
1136         }
1137 #else /* !LDAP_DEBUG_R_SASL */
1138         assert( mutex );
1139 #endif /* !LDAP_DEBUG_R_SASL */
1140         return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1141                 ? SASL_FAIL : SASL_OK;
1142 }
1143
1144 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1145 {
1146 #ifdef LDAP_DEBUG_R_SASL
1147         if ( mutex == NULL ) {
1148                 return SASL_OK;
1149         }
1150 #else /* !LDAP_DEBUG_R_SASL */
1151         assert( mutex );
1152 #endif /* !LDAP_DEBUG_R_SASL */
1153         return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1154                 ? SASL_FAIL : SASL_OK;
1155 }
1156
1157 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1158 {
1159 #ifdef LDAP_DEBUG_R_SASL
1160         if ( mutex == NULL ) {
1161                 return;
1162         }
1163 #else /* !LDAP_DEBUG_R_SASL */
1164         assert( mutex );
1165 #endif /* !LDAP_DEBUG_R_SASL */
1166         (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1167         LDAP_FREE( mutex );
1168 }
1169 #endif
1170
1171 #else
1172 int ldap_int_sasl_init( void )
1173 { return LDAP_SUCCESS; }
1174
1175 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1176 { return LDAP_SUCCESS; }
1177
1178 int
1179 ldap_int_sasl_bind(
1180         LDAP                    *ld,
1181         const char              *dn,
1182         const char              *mechs,
1183         LDAPControl             **sctrls,
1184         LDAPControl             **cctrls,
1185         unsigned                flags,
1186         LDAP_SASL_INTERACT_PROC *interact,
1187         void * defaults )
1188 { return LDAP_NOT_SUPPORTED; }
1189
1190 int
1191 ldap_int_sasl_external(
1192         LDAP *ld,
1193         LDAPConn *conn,
1194         const char * authid,
1195         ber_len_t ssf )
1196 { return LDAP_SUCCESS; }
1197
1198 #endif /* HAVE_CYRUS_SASL */