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