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