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