]> git.sur5r.net Git - openldap/blob - libraries/libldap/cyrus.c
da4bc62dabd7a48a8c534c9d33d722c2bd6ff7ed
[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         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
384         sasl_callback_t session_callbacks[] = {
385 #ifdef SASL_CB_GETREALM
386                 { SASL_CB_GETREALM, NULL, NULL },
387 #endif
388                 { SASL_CB_USER, NULL, NULL },
389                 { SASL_CB_AUTHNAME, NULL, NULL },
390                 { SASL_CB_PASS, NULL, NULL },
391                 { SASL_CB_ECHOPROMPT, NULL, NULL },
392                 { SASL_CB_NOECHOPROMPT, NULL, NULL },
393                 { SASL_CB_LIST_END, NULL, NULL }
394         };
395
396         assert( lc->lconn_sasl_ctx == NULL );
397
398         if ( host == NULL ) {
399                 ld->ld_errno = LDAP_UNAVAILABLE;
400                 return ld->ld_errno;
401         }
402
403         rc = sasl_client_new( "ldap", host,
404                 session_callbacks,
405 #ifdef LDAP_SASL_SECURITY_LAYER
406                 SASL_SECURITY_LAYER,
407 #else
408                 0,
409 #endif
410                 &ctx );
411
412         if ( rc != SASL_OK ) {
413                 ld->ld_errno = sasl_err2ldap( rc );
414                 return ld->ld_errno;
415         }
416
417         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: %s\n",
418                 host, 0, 0 );
419
420         lc->lconn_sasl_ctx = ctx;
421
422         if( ssf ) {
423                 sasl_external_properties_t extprops;
424                 memset(&extprops, 0L, sizeof(extprops));
425                 extprops.ssf = ssf;
426
427                 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
428                         (void *) &extprops );
429
430                 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: ssf=%ld\n",
431                         (long) ssf, 0, 0 );
432         }
433
434         return LDAP_SUCCESS;
435 }
436
437 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
438 {
439         sasl_conn_t *ctx = lc->lconn_sasl_ctx;
440         assert( ctx != NULL );
441
442         if( ctx ) {
443                 sasl_dispose( &ctx );
444                 lc->lconn_sasl_ctx = NULL;
445         }
446
447         return LDAP_SUCCESS;
448 }
449
450 int
451 ldap_int_sasl_bind(
452         LDAP                    *ld,
453         const char              *dn,
454         const char              *mechs,
455         LDAPControl             **sctrls,
456         LDAPControl             **cctrls )
457 {
458         char *data;
459         const char *mech = NULL;
460         const char *pmech = NULL;
461         int                     saslrc, rc;
462         sasl_ssf_t              *ssf = NULL;
463         sasl_conn_t     *ctx;
464         sasl_interact_t *prompts = NULL;
465         unsigned credlen;
466         struct berval ccred, *scred;
467         ber_socket_t            sd;
468
469         Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
470                 mechs ? mechs : "<null>", 0, 0 );
471
472         /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
473         if (ld->ld_version < LDAP_VERSION3) {
474                 ld->ld_errno = LDAP_NOT_SUPPORTED;
475                 return ld->ld_errno;
476         }
477
478         ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
479
480         if ( sd == AC_SOCKET_INVALID ) {
481                 /* not connected yet */
482                 int rc = ldap_open_defconn( ld );
483   
484                 if( rc < 0 ) return ld->ld_errno;
485                 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
486
487                 if( sd == AC_SOCKET_INVALID ) {
488                         ld->ld_errno = LDAP_UNAVAILABLE;
489                         return ld->ld_errno;
490                 }
491         }   
492
493         ctx = ld->ld_defconn->lconn_sasl_ctx;
494
495         if( ctx == NULL ) {
496                 ld->ld_errno = LDAP_UNAVAILABLE;
497                 return ld->ld_errno;
498         }
499
500         /* (re)set security properties */
501         sasl_setprop( ctx, SASL_SEC_PROPS,
502                 &ld->ld_options.ldo_sasl_secprops );
503
504         ccred.bv_val = NULL;
505         ccred.bv_len = 0;
506
507         do {
508                 saslrc = sasl_client_start( ctx,
509                         mechs,
510                         NULL,
511                         &prompts,
512                         &ccred.bv_val,
513                         &credlen,
514                         &mech );
515
516                 if( pmech == NULL && mech != NULL ) {
517                         pmech = mech;
518
519                         fprintf(stderr,
520                                 "SASL/%s authentication started\n",
521                                 pmech );
522                 }
523
524                 if( saslrc == SASL_INTERACT ) {
525                         fprintf(stderr, "SASL Interacting\n");
526                         if( !ld->ld_options.ldo_sasl_interact ) break;
527
528                         rc = (ld->ld_options.ldo_sasl_interact)( ld, prompts );
529                         if( rc != LDAP_SUCCESS ) {
530                                 break;
531                         }
532                 }
533         } while ( saslrc == SASL_INTERACT );
534
535         ccred.bv_len = credlen;
536
537         if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
538                 ld->ld_errno = sasl_err2ldap( saslrc );
539                 return ld->ld_errno;
540         }
541
542         scred = NULL;
543
544         do {
545                 unsigned credlen;
546
547                 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
548
549                 if ( rc == LDAP_SUCCESS ) {
550                         break;
551                 } else if ( rc != LDAP_SASL_BIND_IN_PROGRESS ) {
552                         if ( ccred.bv_val != NULL ) {
553                                 LDAP_FREE( ccred.bv_val );
554                         }
555                         return ld->ld_errno;
556                 }
557
558                 if ( ccred.bv_val != NULL ) {
559                         LDAP_FREE( ccred.bv_val );
560                         ccred.bv_val = NULL;
561                 }
562
563                 do {
564                         saslrc = sasl_client_step( ctx,
565                                 (scred == NULL) ? NULL : scred->bv_val,
566                                 (scred == NULL) ? 0 : scred->bv_len,
567                                 &prompts,
568                                 &ccred.bv_val,
569                                 &credlen );
570
571                         Debug( LDAP_DEBUG_TRACE, "sasl_client_start: %d\n",
572                                 saslrc, 0, 0 );
573
574                         if( saslrc == SASL_INTERACT ) {
575                                 int res;
576                                 fprintf(stderr, "SASL Interacting\n");
577                                 if( !ld->ld_options.ldo_sasl_interact ) break;
578
579                                 res = (ld->ld_options.ldo_sasl_interact)( ld, prompts );
580                                 if( res != LDAP_SUCCESS ) {
581                                         break;
582                                 }
583                         }
584                 } while ( saslrc == SASL_INTERACT );
585
586                 ccred.bv_len = credlen;
587                 ber_bvfree( scred );
588
589                 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
590                         ld->ld_errno = sasl_err2ldap( saslrc );
591                         return ld->ld_errno;
592                 }
593         } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
594
595         assert ( rc == LDAP_SUCCESS );
596
597         /* likely should add a quiet option */
598
599         saslrc = sasl_getprop( ctx, SASL_USERNAME, (void **) &data );
600         if( saslrc == SASL_OK ) {
601                 fprintf( stderr, "SASL username: %s\n", data );
602         }
603
604         saslrc = sasl_getprop( ctx, SASL_REALM, (void **) &data );
605         if( saslrc == SASL_OK ) {
606                 fprintf( stderr, "SASL realm: %s\n", data );
607         }
608
609         saslrc = sasl_getprop( ctx, SASL_SSF, (void **) &ssf );
610         if( saslrc == SASL_OK ) {
611                 fprintf( stderr, "SASL SSF: %lu\n",
612                         (unsigned long) *ssf );
613
614 #ifdef LDAP_SASL_SECURITY_LAYER
615                 if( ssf && *ssf ) {
616                         fprintf( stderr, "SASL installing layers\n" );
617                         ldap_pvt_sasl_install( ld->ld_sb, ctx );
618                 }
619 #endif
620         }
621
622         return rc;
623 }
624
625 int ldap_pvt_sasl_secprops(
626         const char *in,
627         sasl_security_properties_t *secprops )
628 {
629         int i;
630         char **props = ldap_str2charray( in, "," );
631         unsigned sflags = 0;
632         int got_sflags = 0;
633         sasl_ssf_t max_ssf;
634         int got_max_ssf = 0;
635         sasl_ssf_t min_ssf;
636         int got_min_ssf = 0;
637         unsigned maxbufsize;
638         int got_maxbufsize = 0;
639
640         if( props == NULL || secprops == NULL ) {
641                 return LDAP_PARAM_ERROR;
642         }
643
644         for( i=0; props[i]; i++ ) {
645                 if( !strcasecmp(props[i], "none") ) {
646                         got_sflags++;
647
648                 } else if( !strcasecmp(props[i], "noplain") ) {
649                         got_sflags++;
650                         sflags |= SASL_SEC_NOPLAINTEXT;
651
652                 } else if( !strcasecmp(props[i], "noactive") ) {
653                         got_sflags++;
654                         sflags |= SASL_SEC_NOACTIVE;
655
656                 } else if( !strcasecmp(props[i], "nodict") ) {
657                         got_sflags++;
658                         sflags |= SASL_SEC_NODICTIONARY;
659
660                 } else if( !strcasecmp(props[i], "forwardsec") ) {
661                         got_sflags++;
662                         sflags |= SASL_SEC_FORWARD_SECRECY;
663
664                 } else if( !strcasecmp(props[i], "noanonymous")) {
665                         got_sflags++;
666                         sflags |= SASL_SEC_NOANONYMOUS;
667
668                 } else if( !strcasecmp(props[i], "passcred") ) {
669                         got_sflags++;
670                         sflags |= SASL_SEC_PASS_CREDENTIALS;
671
672                 } else if( !strncasecmp(props[i],
673                         "minssf=", sizeof("minssf")) )
674                 {
675                         if( isdigit( props[i][sizeof("minssf")] ) ) {
676                                 got_max_ssf++;
677                                 min_ssf = atoi( &props[i][sizeof("minssf")] );
678                         } else {
679                                 return LDAP_NOT_SUPPORTED;
680                         }
681
682                 } else if( !strncasecmp(props[i],
683                         "maxssf=", sizeof("maxssf")) )
684                 {
685                         if( isdigit( props[i][sizeof("maxssf")] ) ) {
686                                 got_max_ssf++;
687                                 max_ssf = atoi( &props[i][sizeof("maxssf")] );
688                         } else {
689                                 return LDAP_NOT_SUPPORTED;
690                         }
691
692                 } else if( !strncasecmp(props[i],
693                         "maxbufsize=", sizeof("maxbufsize")) )
694                 {
695                         if( isdigit( props[i][sizeof("maxbufsize")] ) ) {
696                                 got_maxbufsize++;
697                                 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
698                         } else {
699                                 return LDAP_NOT_SUPPORTED;
700                         }
701
702                 } else {
703                         return LDAP_NOT_SUPPORTED;
704                 }
705         }
706
707         if(got_sflags) {
708                 secprops->security_flags = sflags;
709         }
710         if(got_min_ssf) {
711                 secprops->min_ssf = min_ssf;
712         }
713         if(got_max_ssf) {
714                 secprops->max_ssf = max_ssf;
715         }
716         if(got_maxbufsize) {
717                 secprops->maxbufsize = maxbufsize;
718         }
719
720         ldap_charray_free( props );
721         return LDAP_SUCCESS;
722 }
723
724 int
725 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
726 {
727         int rc;
728
729         switch( option ) {
730         case LDAP_OPT_X_SASL_SECPROPS:
731                 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
732                 if( rc == LDAP_SUCCESS ) return 0;
733         }
734
735         return -1;
736 }
737
738 int
739 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
740 {
741         if ( ld == NULL )
742                 return -1;
743
744         switch ( option ) {
745                 case LDAP_OPT_X_SASL_SSF: {
746                         int sc;
747                         sasl_ssf_t      *ssf;
748                         sasl_conn_t *ctx;
749
750                         if( ld->ld_defconn == NULL ) {
751                                 return -1;
752                         }
753
754                         ctx = ld->ld_defconn->lconn_sasl_ctx;
755
756                         if ( ctx == NULL ) {
757                                 return -1;
758                         }
759
760                         sc = sasl_getprop( ctx, SASL_SSF,
761                                 (void **) &ssf );
762
763                         if ( sc != SASL_OK ) {
764                                 return -1;
765                         }
766
767                         *(ber_len_t *)arg = *ssf;
768                 } break;
769
770                 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
771                         /* this option is write only */
772                         return -1;
773
774                 case LDAP_OPT_X_SASL_SSF_MIN:
775                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
776                         break;
777                 case LDAP_OPT_X_SASL_SSF_MAX:
778                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
779                         break;
780                 case LDAP_OPT_X_SASL_MAXBUFSIZE:
781                         *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
782                         break;
783
784                 case LDAP_OPT_X_SASL_SECPROPS:
785                         /* this option is write only */
786                         return -1;
787
788                 default:
789                         return -1;
790         }
791         return 0;
792 }
793
794 int
795 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
796 {
797         if ( ld == NULL )
798                 return -1;
799
800         switch ( option ) {
801         case LDAP_OPT_X_SASL_SSF:
802                 /* This option is read-only */
803                 return -1;
804
805         case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
806                 int sc;
807                 sasl_external_properties_t extprops;
808                 sasl_conn_t *ctx;
809
810                 if( ld->ld_defconn == NULL ) {
811                         return -1;
812                 }
813
814                 ctx = ld->ld_defconn->lconn_sasl_ctx;
815
816                 if ( ctx == NULL ) {
817                         return -1;
818                 }
819
820                 memset(&extprops, 0L, sizeof(extprops));
821
822                 extprops.ssf = * (ber_len_t *) arg;
823
824                 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
825                         (void *) &extprops );
826
827                 if ( sc != SASL_OK ) {
828                         return -1;
829                 }
830                 } break;
831
832         case LDAP_OPT_X_SASL_SSF_MIN:
833                 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
834                 break;
835         case LDAP_OPT_X_SASL_SSF_MAX:
836                 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
837                 break;
838         case LDAP_OPT_X_SASL_MAXBUFSIZE:
839                 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
840                 break;
841
842         case LDAP_OPT_X_SASL_SECPROPS: {
843                 int sc;
844                 sc = ldap_pvt_sasl_secprops( (char *) arg,
845                         &ld->ld_options.ldo_sasl_secprops );
846
847                 return sc == LDAP_SUCCESS ? 0 : -1;
848                 }
849
850         default:
851                 return -1;
852         }
853         return 0;
854 }
855
856 #ifdef LDAP_R_COMPILE
857 void *ldap_pvt_sasl_mutex_new(void)
858 {
859         ldap_pvt_thread_mutex_t *mutex;
860
861         mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
862                 sizeof(ldap_pvt_thread_mutex_t) );
863
864         if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
865                 return mutex;
866         }
867         return NULL;
868 }
869
870 int ldap_pvt_sasl_mutex_lock(void *mutex)
871 {
872         return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
873                 ? SASL_FAIL : SASL_OK;
874 }
875
876 int ldap_pvt_sasl_mutex_unlock(void *mutex)
877 {
878         return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
879                 ? SASL_FAIL : SASL_OK;
880 }
881
882 void ldap_pvt_sasl_mutex_dispose(void *mutex)
883 {
884         (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
885         LDAP_FREE( mutex );
886 }
887 #endif
888
889 #else
890 int ldap_int_sasl_init( void )
891 { return LDAP_SUCCESS; }
892
893 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
894 { return LDAP_SUCCESS; }
895
896 int
897 ldap_int_sasl_bind(
898         LDAP                    *ld,
899         const char              *dn,
900         const char              *mechs,
901         LDAPControl             **sctrls,
902         LDAPControl             **cctrls )
903 { return LDAP_NOT_SUPPORTED; }
904 #endif /* HAVE_CYRUS_SASL */