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