]> git.sur5r.net Git - openldap/blob - libraries/libldap/cyrus.c
Fix an interaction error on multiple step binds.
[openldap] / libraries / libldap / cyrus.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1999-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8
9 #include <stdlib.h>
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/string.h>
14 #include <ac/time.h>
15 #include <ac/errno.h>
16
17 #include "ldap-int.h"
18 #ifdef LDAP_R_COMPILE
19 #include "ldap_pvt_thread.h"
20 #endif
21
22 #ifdef HAVE_CYRUS_SASL
23 #include <sasl.h>
24
25 /*
26 * Various Cyrus SASL related stuff.
27 */
28
29 #define SASL_MAX_BUFF_SIZE      65536
30 #define SASL_MIN_BUFF_SIZE      4096
31
32 int ldap_int_sasl_init( void )
33 {
34         /* XXX not threadsafe */
35         static int sasl_initialized = 0;
36
37         static sasl_callback_t client_callbacks[] = {
38 #ifdef SASL_CB_GETREALM
39                 { SASL_CB_GETREALM, NULL, NULL },
40 #endif
41                 { SASL_CB_USER, NULL, NULL },
42                 { SASL_CB_AUTHNAME, NULL, NULL },
43                 { SASL_CB_PASS, NULL, NULL },
44                 { SASL_CB_ECHOPROMPT, NULL, NULL },
45                 { SASL_CB_NOECHOPROMPT, NULL, NULL },
46                 { SASL_CB_LIST_END, NULL, NULL }
47         };
48
49         if ( sasl_initialized ) {
50                 return 0;
51         }
52
53 #ifndef CSRIMALLOC
54         sasl_set_alloc(
55                 ber_memalloc,
56                 ber_memcalloc,
57                 ber_memrealloc,
58                 ber_memfree );
59 #endif /* CSRIMALLOC */
60
61 #ifdef LDAP_R_COMPILE
62         sasl_set_mutex(
63                 ldap_pvt_sasl_mutex_new,
64                 ldap_pvt_sasl_mutex_lock,
65                 ldap_pvt_sasl_mutex_unlock,    
66                 ldap_pvt_sasl_mutex_dispose );    
67 #endif
68
69         if ( sasl_client_init( client_callbacks ) == SASL_OK ) {
70                 sasl_initialized = 1;
71                 return 0;
72         }
73
74         return -1;
75 }
76
77 /*
78  * SASL encryption support for LBER Sockbufs
79  */
80
81 struct sb_sasl_data {
82         sasl_conn_t             *sasl_context;
83         Sockbuf_Buf             sec_buf_in;
84         Sockbuf_Buf             buf_in;
85         Sockbuf_Buf             buf_out;
86 };
87
88 static int
89 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
90 {
91         struct sb_sasl_data     *p;
92
93         assert( sbiod != NULL );
94
95         p = LBER_MALLOC( sizeof( *p ) );
96         if ( p == NULL )
97                 return -1;
98         p->sasl_context = (sasl_conn_t *)arg;
99         ber_pvt_sb_buf_init( &p->sec_buf_in );
100         ber_pvt_sb_buf_init( &p->buf_in );
101         ber_pvt_sb_buf_init( &p->buf_out );
102         if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
103                 errno = ENOMEM;
104                 return -1;
105         }
106
107         sbiod->sbiod_pvt = p;
108
109         return 0;
110 }
111
112 static int
113 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
114 {
115         struct sb_sasl_data     *p;
116
117         assert( sbiod != NULL );
118         
119         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
120         ber_pvt_sb_buf_destroy( &p->sec_buf_in );
121         ber_pvt_sb_buf_destroy( &p->buf_in );
122         ber_pvt_sb_buf_destroy( &p->buf_out );
123         LBER_FREE( p );
124         sbiod->sbiod_pvt = NULL;
125         return 0;
126 }
127
128 static ber_len_t
129 sb_sasl_pkt_length( const char *buf, int debuglevel )
130 {
131         ber_len_t               size;
132         long                    tmp;
133
134         assert( buf != NULL );
135
136         tmp = *((long *)buf);
137         size = ntohl( tmp );
138    
139         if ( size > SASL_MAX_BUFF_SIZE ) {
140                 /* somebody is trying to mess me up. */
141                 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
142                         "sb_sasl_pkt_length: received illegal packet length "
143                         "of %lu bytes\n", (unsigned long)size );      
144                 size = 16; /* this should lead to an error. */
145 }
146
147         return size + 4; /* include the size !!! */
148 }
149
150 /* Drop a processed packet from the input buffer */
151 static void
152 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel )
153 {
154         ber_slen_t                      len;
155
156         len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
157         if ( len > 0 )
158                 memmove( sec_buf_in->buf_base, sec_buf_in->buf_base +
159                         sec_buf_in->buf_end, len );
160    
161         if ( len >= 4 ) {
162                 sec_buf_in->buf_end = sb_sasl_pkt_length( sec_buf_in->buf_base,
163                         debuglevel);
164         }
165         else {
166                 sec_buf_in->buf_end = 0;
167         }
168         sec_buf_in->buf_ptr = len;
169 }
170
171 static ber_slen_t
172 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
173 {
174         struct sb_sasl_data     *p;
175         ber_slen_t              ret, bufptr;
176    
177         assert( sbiod != NULL );
178         assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
179
180         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
181
182         /* Are there anything left in the buffer? */
183         ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
184         bufptr = ret;
185         len -= ret;
186
187         if ( len == 0 )
188                 return bufptr;
189
190         ber_pvt_sb_buf_destroy( &p->buf_in );
191
192         /* Read the length of the packet */
193         while ( p->sec_buf_in.buf_ptr < 4 ) {
194                 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
195                         4 - p->sec_buf_in.buf_ptr );
196 #ifdef EINTR
197                 if ( ( ret < 0 ) && ( errno == EINTR ) )
198                         continue;
199 #endif
200                 if ( ret <= 0 )
201                         return ret;
202
203                 p->sec_buf_in.buf_ptr += ret;
204         }
205
206         /* The new packet always starts at p->sec_buf_in.buf_base */
207         ret = sb_sasl_pkt_length( p->sec_buf_in.buf_base,
208                 sbiod->sbiod_sb->sb_debug );
209
210         /* Grow the packet buffer if neccessary */
211         if ( ( p->sec_buf_in.buf_size < ret ) && 
212                         ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 ) {
213                 errno = ENOMEM;
214                 return -1;
215         }
216         p->sec_buf_in.buf_end = ret;
217
218         /* Did we read the whole encrypted packet? */
219         while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
220                 /* No, we have got only a part of it */
221                 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
222
223                 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
224                         p->sec_buf_in.buf_ptr, ret );
225 #ifdef EINTR
226                 if ( ( ret < 0 ) && ( errno == EINTR ) )
227                         continue;
228 #endif
229                 if ( ret <= 0 )
230                         return ret;
231
232                 p->sec_buf_in.buf_ptr += ret;
233         }
234
235         /* Decode the packet */
236         ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
237                 p->sec_buf_in.buf_end, &p->buf_in.buf_base,
238                 (unsigned *)&p->buf_in.buf_end );
239         if ( ret != SASL_OK ) {
240                 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
241                         "sb_sasl_read: failed to decode packet: %s\n",
242                         sasl_errstring( ret, NULL, NULL ) );
243                 sb_sasl_drop_packet( &p->sec_buf_in,
244                         sbiod->sbiod_sb->sb_debug );
245                 errno = EIO;
246                 return -1;
247         }
248         
249         /* Drop the packet from the input buffer */
250         sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
251
252         p->buf_in.buf_size = p->buf_in.buf_end;
253
254         bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
255
256         return bufptr;
257 }
258
259 static ber_slen_t
260 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
261 {
262         struct sb_sasl_data     *p;
263         int                     ret;
264
265         assert( sbiod != NULL );
266         assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
267
268         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
269
270         /* Are there anything left in the buffer? */
271         if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
272                 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
273                 if ( ret <= 0 )
274                         return ret;
275         }
276
277         /* now encode the next packet. */
278         ber_pvt_sb_buf_destroy( &p->buf_out );
279         ret = sasl_encode( p->sasl_context, buf, len, &p->buf_out.buf_base,
280                 (unsigned *)&p->buf_out.buf_size );
281         if ( ret != SASL_OK ) {
282                 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
283                         "sb_sasl_write: failed to encode packet: %s\n",
284                         sasl_errstring( ret, NULL, NULL ) );
285                 return -1;
286         }
287         p->buf_out.buf_end = p->buf_out.buf_size;
288
289         ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
290         if ( ret <= 0 )
291                 return ret;
292         return len;
293 }
294
295 static int
296 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
297 {
298         struct sb_sasl_data     *p;
299
300         p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
301
302         if ( opt == LBER_SB_OPT_DATA_READY ) {
303                 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
304                         return 1;
305         }
306         
307         return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
308 }
309
310 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
311         sb_sasl_setup,          /* sbi_setup */
312         sb_sasl_remove,         /* sbi_remove */
313         sb_sasl_ctrl,           /* sbi_ctrl */
314         sb_sasl_read,           /* sbi_read */
315         sb_sasl_write,          /* sbi_write */
316         NULL                    /* sbi_close */
317 };
318
319 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
320 {
321         Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
322                 0, 0, 0 );
323
324         /* don't install the stuff unless security has been negotiated */
325
326         if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
327                         &ldap_pvt_sockbuf_io_sasl ) )
328                 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
329                         LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
330
331         return LDAP_SUCCESS;
332 }
333
334 static int
335 sasl_err2ldap( int saslerr )
336 {
337         int rc;
338
339         switch (saslerr) {
340                 case SASL_CONTINUE:
341                         rc = LDAP_MORE_RESULTS_TO_RETURN;
342                         break;
343                 case SASL_OK:
344                         rc = LDAP_SUCCESS;
345                         break;
346                 case SASL_FAIL:
347                         rc = LDAP_LOCAL_ERROR;
348                         break;
349                 case SASL_NOMEM:
350                         rc = LDAP_NO_MEMORY;
351                         break;
352                 case SASL_NOMECH:
353                         rc = LDAP_AUTH_UNKNOWN;
354                         break;
355                 case SASL_BADAUTH:
356                         rc = LDAP_AUTH_UNKNOWN;
357                         break;
358                 case SASL_NOAUTHZ:
359                         rc = LDAP_PARAM_ERROR;
360                         break;
361                 case SASL_TOOWEAK:
362                 case SASL_ENCRYPT:
363                         rc = LDAP_AUTH_UNKNOWN;
364                         break;
365                 default:
366                         rc = LDAP_LOCAL_ERROR;
367                         break;
368         }
369
370         assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
371         return rc;
372 }
373
374 int
375 ldap_int_sasl_open(
376         LDAP *ld, 
377         LDAPConn *lc,
378         const char * host,
379         ber_len_t ssf )
380 {
381         int rc;
382         sasl_conn_t *ctx;
383         assert( lc->lconn_sasl_ctx == NULL );
384
385         if ( host == NULL ) {
386                 ld->ld_errno = LDAP_UNAVAILABLE;
387                 return ld->ld_errno;
388         }
389
390         rc = sasl_client_new( "ldap", host,
391                 NULL,
392 #ifdef LDAP_SASL_SECURITY_LAYER
393                 SASL_SECURITY_LAYER,
394 #else
395                 0,
396 #endif
397                 &ctx );
398
399         if ( rc != SASL_OK ) {
400                 ld->ld_errno = sasl_err2ldap( rc );
401                 return ld->ld_errno;
402         }
403
404         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: %s\n",
405                 host, 0, 0 );
406
407         lc->lconn_sasl_ctx = ctx;
408
409         if( ssf ) {
410                 sasl_external_properties_t extprops;
411                 memset(&extprops, 0L, sizeof(extprops));
412                 extprops.ssf = ssf;
413
414                 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
415                         (void *) &extprops );
416
417                 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: ssf=%ld\n",
418                         (long) ssf, 0, 0 );
419         }
420
421         return LDAP_SUCCESS;
422 }
423
424 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
425 {
426         sasl_conn_t *ctx = lc->lconn_sasl_ctx;
427         assert( ctx != NULL );
428
429         if( ctx ) {
430                 sasl_dispose( &ctx );
431                 lc->lconn_sasl_ctx = NULL;
432         }
433
434         return LDAP_SUCCESS;
435 }
436
437 int
438 ldap_int_sasl_bind(
439         LDAP                    *ld,
440         const char              *dn,
441         const char              *mechs,
442         LDAPControl             **sctrls,
443         LDAPControl             **cctrls )
444 {
445         char *data;
446         const char *mech = NULL;
447         const char *pmech = NULL;
448         int                     saslrc, rc;
449         sasl_ssf_t              *ssf = NULL;
450         sasl_conn_t     *ctx;
451         sasl_interact_t *prompts = NULL;
452         unsigned credlen;
453         struct berval ccred, *scred;
454         ber_socket_t            sd;
455
456         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
457                 mechs ? mechs : "<null>", 0, 0 );
458
459         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
460         if (ld->ld_version < LDAP_VERSION3) {
461                 ld->ld_errno = LDAP_NOT_SUPPORTED;
462                 return ld->ld_errno;
463         }
464
465         ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
466
467         if ( sd == AC_SOCKET_INVALID ) {
468                 /* not connected yet */
469                 int rc = ldap_open_defconn( ld );
470   
471                 if( rc < 0 ) return ld->ld_errno;
472                 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
473
474                 if( sd == AC_SOCKET_INVALID ) {
475                         ld->ld_errno = LDAP_UNAVAILABLE;
476                         return ld->ld_errno;
477                 }
478         }   
479
480         ctx = ld->ld_defconn->lconn_sasl_ctx;
481
482         if( ctx == NULL ) {
483                 ld->ld_errno = LDAP_UNAVAILABLE;
484                 return ld->ld_errno;
485         }
486
487         /* (re)set security properties */
488         sasl_setprop( ctx, SASL_SEC_PROPS,
489                 &ld->ld_options.ldo_sasl_secprops );
490
491         ccred.bv_val = NULL;
492         ccred.bv_len = 0;
493
494         do {
495                 saslrc = sasl_client_start( ctx,
496                         mechs,
497                         NULL,
498                         &prompts,
499                         &ccred.bv_val,
500                         &credlen,
501                         &mech );
502
503                 if( pmech == NULL && mech != NULL ) {
504                         pmech = mech;
505
506                         fprintf(stderr,
507                                 "SASL/%s authentication started\n",
508                                 pmech );
509                 }
510
511                 if( saslrc == SASL_INTERACT ) {
512                         fprintf(stderr, "SASL Interacting\n");
513                         if( !ld->ld_options.ldo_sasl_interact ) break;
514
515                         rc = (ld->ld_options.ldo_sasl_interact)( ld, prompts );
516                         if( rc != LDAP_SUCCESS ) {
517                                 break;
518                         }
519                 }
520         } while ( saslrc == SASL_INTERACT );
521
522         ccred.bv_len = credlen;
523
524         if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
525                 ld->ld_errno = sasl_err2ldap( saslrc );
526                 return ld->ld_errno;
527         }
528
529         scred = NULL;
530
531         do {
532                 unsigned credlen;
533
534                 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
535
536                 if ( rc == LDAP_SUCCESS ) {
537                         break;
538                 } else if ( rc != LDAP_SASL_BIND_IN_PROGRESS ) {
539                         if ( ccred.bv_val != NULL ) {
540                                 LDAP_FREE( ccred.bv_val );
541                         }
542                         return ld->ld_errno;
543                 }
544
545                 if ( ccred.bv_val != NULL ) {
546                         LDAP_FREE( ccred.bv_val );
547                         ccred.bv_val = NULL;
548                 }
549
550                 do {
551                         saslrc = sasl_client_step( ctx,
552                                 (scred == NULL) ? NULL : scred->bv_val,
553                                 (scred == NULL) ? 0 : scred->bv_len,
554                                 &prompts,
555                                 &ccred.bv_val,
556                                 &credlen );
557
558                         Debug( LDAP_DEBUG_TRACE, "sasl_client_start: %d\n",
559                                 saslrc, 0, 0 );
560
561                         if( saslrc == SASL_INTERACT ) {
562                                 int res;
563                                 fprintf(stderr, "SASL Interacting\n");
564                                 if( !ld->ld_options.ldo_sasl_interact ) break;
565
566                                 res = (ld->ld_options.ldo_sasl_interact)( ld, prompts );
567                                 if( res != LDAP_SUCCESS ) {
568                                         break;
569                                 }
570                         }
571                 } while ( saslrc == SASL_INTERACT );
572
573                 ccred.bv_len = credlen;
574                 ber_bvfree( scred );
575
576                 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
577                         ld->ld_errno = sasl_err2ldap( saslrc );
578                         return ld->ld_errno;
579                 }
580         } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
581
582         assert ( rc == LDAP_SUCCESS );
583
584         /* likely should add a quiet option */
585
586         saslrc = sasl_getprop( ctx, SASL_USERNAME, (void **) &data );
587         if( saslrc == SASL_OK ) {
588                 fprintf( stderr, "SASL username: %s\n", data );
589         }
590
591         saslrc = sasl_getprop( ctx, SASL_REALM, (void **) &data );
592         if( saslrc == SASL_OK ) {
593                 fprintf( stderr, "SASL realm: %s\n", data );
594         }
595
596         saslrc = sasl_getprop( ctx, SASL_SSF, (void **) &ssf );
597         if( saslrc == SASL_OK ) {
598                 fprintf( stderr, "SASL SSF: %lu\n",
599                         (unsigned long) *ssf );
600
601 #ifdef LDAP_SASL_SECURITY_LAYER
602                 if( ssf && *ssf > 1 ) {
603                         fprintf( stderr, "SASL installing layers\n" );
604                         ldap_pvt_sasl_install( ld->ld_sb, ctx );
605                 }
606 #endif
607         }
608
609         return rc;
610 }
611
612 int ldap_pvt_sasl_secprops(
613         const char *in,
614         sasl_security_properties_t *secprops )
615 {
616         int i;
617         char **props = ldap_str2charray( in, "," );
618         unsigned sflags = 0;
619         int got_sflags = 0;
620         sasl_ssf_t max_ssf;
621         int got_max_ssf = 0;
622         sasl_ssf_t min_ssf;
623         int got_min_ssf = 0;
624         unsigned maxbufsize;
625         int got_maxbufsize = 0;
626
627         if( props == NULL || secprops == NULL ) {
628                 return LDAP_PARAM_ERROR;
629         }
630
631         for( i=0; props[i]; i++ ) {
632                 if( !strcasecmp(props[i], "none") ) {
633                         got_sflags++;
634
635                 } else if( !strcasecmp(props[i], "noplain") ) {
636                         got_sflags++;
637                         sflags |= SASL_SEC_NOPLAINTEXT;
638
639                 } else if( !strcasecmp(props[i], "noactive") ) {
640                         got_sflags++;
641                         sflags |= SASL_SEC_NOACTIVE;
642
643                 } else if( !strcasecmp(props[i], "nodict") ) {
644                         got_sflags++;
645                         sflags |= SASL_SEC_NODICTIONARY;
646
647                 } else if( !strcasecmp(props[i], "forwardsec") ) {
648                         got_sflags++;
649                         sflags |= SASL_SEC_FORWARD_SECRECY;
650
651                 } else if( !strcasecmp(props[i], "noanonymous")) {
652                         got_sflags++;
653                         sflags |= SASL_SEC_NOANONYMOUS;
654
655                 } else if( !strcasecmp(props[i], "passcred") ) {
656                         got_sflags++;
657                         sflags |= SASL_SEC_PASS_CREDENTIALS;
658
659                 } else if( !strncasecmp(props[i],
660                         "minssf=", sizeof("minssf")) )
661                 {
662                         if( isdigit( props[i][sizeof("minssf")] ) ) {
663                                 got_max_ssf++;
664                                 min_ssf = atoi( &props[i][sizeof("minssf")] );
665                         } else {
666                                 return LDAP_NOT_SUPPORTED;
667                         }
668
669                 } else if( !strncasecmp(props[i],
670                         "maxssf=", sizeof("maxssf")) )
671                 {
672                         if( isdigit( props[i][sizeof("maxssf")] ) ) {
673                                 got_max_ssf++;
674                                 max_ssf = atoi( &props[i][sizeof("maxssf")] );
675                         } else {
676                                 return LDAP_NOT_SUPPORTED;
677                         }
678
679                 } else if( !strncasecmp(props[i],
680                         "maxbufsize=", sizeof("maxbufsize")) )
681                 {
682                         if( isdigit( props[i][sizeof("maxbufsize")] ) ) {
683                                 got_maxbufsize++;
684                                 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
685                         } else {
686                                 return LDAP_NOT_SUPPORTED;
687                         }
688
689                 } else {
690                         return LDAP_NOT_SUPPORTED;
691                 }
692         }
693
694         if(got_sflags) {
695                 secprops->security_flags = sflags;
696         }
697         if(got_min_ssf) {
698                 secprops->min_ssf = min_ssf;
699         }
700         if(got_max_ssf) {
701                 secprops->max_ssf = max_ssf;
702         }
703         if(got_maxbufsize) {
704                 secprops->maxbufsize = maxbufsize;
705         }
706
707         ldap_charray_free( props );
708         return LDAP_SUCCESS;
709 }
710
711 int
712 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
713 {
714         int rc;
715
716         switch( option ) {
717         case LDAP_OPT_X_SASL_SECPROPS:
718                 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
719                 if( rc == LDAP_SUCCESS ) return 0;
720         }
721
722         return -1;
723 }
724
725 int
726 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
727 {
728         if ( ld == NULL )
729                 return -1;
730
731         switch ( option ) {
732                 case LDAP_OPT_X_SASL_SSF: {
733                         int sc;
734                         sasl_ssf_t      *ssf;
735                         sasl_conn_t *ctx;
736
737                         if( ld->ld_defconn == NULL ) {
738                                 return -1;
739                         }
740
741                         ctx = ld->ld_defconn->lconn_sasl_ctx;
742
743                         if ( ctx == NULL ) {
744                                 return -1;
745                         }
746
747                         sc = sasl_getprop( ctx, SASL_SSF,
748                                 (void **) &ssf );
749
750                         if ( sc != SASL_OK ) {
751                                 return -1;
752                         }
753
754                         *(ber_len_t *)arg = *ssf;
755                 } break;
756
757                 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
758                         /* this option is write only */
759                         return -1;
760
761                 case LDAP_OPT_X_SASL_SSF_MIN:
762                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
763                         break;
764                 case LDAP_OPT_X_SASL_SSF_MAX:
765                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
766                         break;
767                 case LDAP_OPT_X_SASL_MAXBUFSIZE:
768                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
769                         break;
770
771                 case LDAP_OPT_X_SASL_SECPROPS:
772                         /* this option is write only */
773                         return -1;
774
775                 default:
776                         return -1;
777         }
778         return 0;
779 }
780
781 int
782 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
783 {
784         if ( ld == NULL )
785                 return -1;
786
787         switch ( option ) {
788         case LDAP_OPT_X_SASL_SSF:
789                 /* This option is read-only */
790                 return -1;
791
792         case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
793                 int sc;
794                 sasl_external_properties_t extprops;
795                 sasl_conn_t *ctx;
796
797                 if( ld->ld_defconn == NULL ) {
798                         return -1;
799                 }
800
801                 ctx = ld->ld_defconn->lconn_sasl_ctx;
802
803                 if ( ctx == NULL ) {
804                         return -1;
805                 }
806
807                 memset(&extprops, 0L, sizeof(extprops));
808
809                 extprops.ssf = * (ber_len_t *) arg;
810
811                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
812                         (void *) &extprops );
813
814                 if ( sc != SASL_OK ) {
815                         return -1;
816                 }
817                 } break;
818
819         case LDAP_OPT_X_SASL_SSF_MIN:
820                 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
821                 break;
822         case LDAP_OPT_X_SASL_SSF_MAX:
823                 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
824                 break;
825         case LDAP_OPT_X_SASL_MAXBUFSIZE:
826                 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
827                 break;
828
829         case LDAP_OPT_X_SASL_SECPROPS: {
830                 int sc;
831                 sc = ldap_pvt_sasl_secprops( (char *) arg,
832                         &ld->ld_options.ldo_sasl_secprops );
833
834                 return sc == LDAP_SUCCESS ? 0 : -1;
835                 }
836
837         default:
838                 return -1;
839         }
840         return 0;
841 }
842
843 #ifdef LDAP_R_COMPILE
844 void *ldap_pvt_sasl_mutex_new(void)
845 {
846         ldap_pvt_thread_mutex_t *mutex;
847
848         mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
849                 sizeof(ldap_pvt_thread_mutex_t) );
850
851         if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
852                 return mutex;
853         }
854         return NULL;
855 }
856
857 int ldap_pvt_sasl_mutex_lock(void *mutex)
858 {
859         return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
860                 ? SASL_FAIL : SASL_OK;
861 }
862
863 int ldap_pvt_sasl_mutex_unlock(void *mutex)
864 {
865         return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
866                 ? SASL_FAIL : SASL_OK;
867 }
868
869 void ldap_pvt_sasl_mutex_dispose(void *mutex)
870 {
871         (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
872         LDAP_FREE( mutex );
873 }
874 #endif
875
876 #else
877 int ldap_int_sasl_init( void )
878 { return LDAP_SUCCESS; }
879
880 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
881 { return LDAP_SUCCESS; }
882
883 int
884 ldap_int_sasl_bind(
885         LDAP                    *ld,
886         const char              *dn,
887         const char              *mechs,
888         LDAPControl             **sctrls,
889         LDAPControl             **cctrls )
890 { return LDAP_NOT_SUPPORTED; }
891 #endif /* HAVE_CYRUS_SASL */