3 * Copyright 1999-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
12 #include <ac/stdlib.h>
13 #include <ac/string.h>
20 #ifdef HAVE_CYRUS_SASL
23 ldap_pvt_thread_mutex_t ldap_int_sasl_mutex;
26 #ifdef HAVE_SASL_SASL_H
27 #include <sasl/sasl.h>
32 #if SASL_VERSION_MAJOR >= 2
33 #define SASL_CONST const
39 * Various Cyrus SASL related stuff.
42 int ldap_int_sasl_init( void )
44 /* XXX not threadsafe */
45 static int sasl_initialized = 0;
47 static sasl_callback_t client_callbacks[] = {
48 #ifdef SASL_CB_GETREALM
49 { SASL_CB_GETREALM, NULL, NULL },
51 { SASL_CB_USER, NULL, NULL },
52 { SASL_CB_AUTHNAME, NULL, NULL },
53 { SASL_CB_PASS, NULL, NULL },
54 { SASL_CB_ECHOPROMPT, NULL, NULL },
55 { SASL_CB_NOECHOPROMPT, NULL, NULL },
56 { SASL_CB_LIST_END, NULL, NULL }
59 #ifdef HAVE_SASL_VERSION
60 #define SASL_BUILD_VERSION ((SASL_VERSION_MAJOR << 24) |\
61 (SASL_VERSION_MINOR << 16) | SASL_VERSION_STEP)
64 sasl_version( NULL, &rc );
65 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
66 (rc & 0xffff) < SASL_VERSION_STEP) {
69 LDAP_LOG( TRANSPORT, INFO,
70 "ldap_int_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
71 rc, SASL_BUILD_VERSION, 0 );
73 Debug( LDAP_DEBUG_ANY,
74 "ldap_int_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
75 rc, SASL_BUILD_VERSION, 0 );
81 if ( sasl_initialized ) {
85 /* SASL 2 takes care of its own memory completely internally */
86 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
92 #endif /* CSRIMALLOC */
96 ldap_pvt_sasl_mutex_new,
97 ldap_pvt_sasl_mutex_lock,
98 ldap_pvt_sasl_mutex_unlock,
99 ldap_pvt_sasl_mutex_dispose );
101 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
104 if ( sasl_client_init( client_callbacks ) == SASL_OK ) {
105 sasl_initialized = 1;
109 #if SASL_VERSION_MAJOR < 2
110 /* A no-op to make sure we link with Cyrus 1.5 */
111 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
117 * SASL encryption support for LBER Sockbufs
120 struct sb_sasl_data {
121 sasl_conn_t *sasl_context;
122 unsigned *sasl_maxbuf;
123 Sockbuf_Buf sec_buf_in;
129 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
131 struct sb_sasl_data *p;
133 assert( sbiod != NULL );
135 p = LBER_MALLOC( sizeof( *p ) );
138 p->sasl_context = (sasl_conn_t *)arg;
139 ber_pvt_sb_buf_init( &p->sec_buf_in );
140 ber_pvt_sb_buf_init( &p->buf_in );
141 ber_pvt_sb_buf_init( &p->buf_out );
142 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
147 sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
148 (SASL_CONST void **) &p->sasl_maxbuf );
150 sbiod->sbiod_pvt = p;
156 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
158 struct sb_sasl_data *p;
160 assert( sbiod != NULL );
162 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
163 #if SASL_VERSION_MAJOR >= 2
165 * SASLv2 encode/decode buffers are managed by
166 * libsasl2. Ensure they are not freed by liblber.
168 p->buf_in.buf_base = NULL;
169 p->buf_out.buf_base = NULL;
171 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
172 ber_pvt_sb_buf_destroy( &p->buf_in );
173 ber_pvt_sb_buf_destroy( &p->buf_out );
175 sbiod->sbiod_pvt = NULL;
180 sb_sasl_pkt_length( const unsigned char *buf, unsigned max, int debuglevel )
184 assert( buf != NULL );
191 if ( size > SASL_MAX_BUFF_SIZE ) {
192 /* somebody is trying to mess me up. */
193 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
194 "sb_sasl_pkt_length: received illegal packet length "
195 "of %lu bytes\n", (unsigned long)size );
196 size = 16; /* this should lead to an error. */
197 } else if ( size > max ) {
198 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
199 "sb_sasl_pkt_length: received packet length "
200 "of %lu exceeds negotiated max of %lu bytes\n", (unsigned long)size, (unsigned long)max );
203 return size + 4; /* include the size !!! */
206 /* Drop a processed packet from the input buffer */
208 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, unsigned max, int debuglevel )
212 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
214 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
215 sec_buf_in->buf_end, len );
218 sec_buf_in->buf_end = sb_sasl_pkt_length( sec_buf_in->buf_base,
222 sec_buf_in->buf_end = 0;
224 sec_buf_in->buf_ptr = len;
228 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
230 struct sb_sasl_data *p;
231 ber_slen_t ret, bufptr;
233 assert( sbiod != NULL );
234 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
236 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
238 /* Are there anything left in the buffer? */
239 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
246 #if SASL_VERSION_MAJOR >= 2
247 ber_pvt_sb_buf_init( &p->buf_in );
249 ber_pvt_sb_buf_destroy( &p->buf_in );
252 /* Read the length of the packet */
253 while ( p->sec_buf_in.buf_ptr < 4 ) {
254 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
255 4 - p->sec_buf_in.buf_ptr );
257 if ( ( ret < 0 ) && ( errno == EINTR ) )
261 return bufptr ? bufptr : ret;
263 p->sec_buf_in.buf_ptr += ret;
266 /* The new packet always starts at p->sec_buf_in.buf_base */
267 ret = sb_sasl_pkt_length( p->sec_buf_in.buf_base,
268 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
270 /* Grow the packet buffer if neccessary */
271 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
272 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
277 p->sec_buf_in.buf_end = ret;
279 /* Did we read the whole encrypted packet? */
280 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
281 /* No, we have got only a part of it */
282 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
284 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
285 p->sec_buf_in.buf_ptr, ret );
287 if ( ( ret < 0 ) && ( errno == EINTR ) )
291 return bufptr ? bufptr : ret;
293 p->sec_buf_in.buf_ptr += ret;
296 /* Decode the packet */
297 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
298 p->sec_buf_in.buf_end,
299 (SASL_CONST char **)&p->buf_in.buf_base,
300 (unsigned *)&p->buf_in.buf_end );
302 /* Drop the packet from the input buffer */
303 sb_sasl_drop_packet( &p->sec_buf_in,
304 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
306 if ( ret != SASL_OK ) {
307 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
308 "sb_sasl_read: failed to decode packet: %s\n",
309 sasl_errstring( ret, NULL, NULL ) );
314 p->buf_in.buf_size = p->buf_in.buf_end;
316 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
322 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
324 struct sb_sasl_data *p;
327 assert( sbiod != NULL );
328 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
330 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
332 /* Are there anything left in the buffer? */
333 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
334 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
337 /* Still have something left?? */
338 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
344 /* now encode the next packet. */
345 #if SASL_VERSION_MAJOR >= 2
346 ber_pvt_sb_buf_init( &p->buf_out );
348 ber_pvt_sb_buf_destroy( &p->buf_out );
350 if ( len > *p->sasl_maxbuf - 100 )
351 len = *p->sasl_maxbuf - 100; /* For safety margin */
352 ret = sasl_encode( p->sasl_context, buf, len,
353 (SASL_CONST char **)&p->buf_out.buf_base,
354 (unsigned *)&p->buf_out.buf_size );
355 if ( ret != SASL_OK ) {
356 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
357 "sb_sasl_write: failed to encode packet: %s\n",
358 sasl_errstring( ret, NULL, NULL ) );
361 p->buf_out.buf_end = p->buf_out.buf_size;
363 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
370 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
372 struct sb_sasl_data *p;
374 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
376 if ( opt == LBER_SB_OPT_DATA_READY ) {
377 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
381 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
384 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
385 sb_sasl_setup, /* sbi_setup */
386 sb_sasl_remove, /* sbi_remove */
387 sb_sasl_ctrl, /* sbi_ctrl */
388 sb_sasl_read, /* sbi_read */
389 sb_sasl_write, /* sbi_write */
393 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
396 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_install\n", 0, 0, 0 );
398 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
402 /* don't install the stuff unless security has been negotiated */
404 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
405 &ldap_pvt_sockbuf_io_sasl ) )
408 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
409 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
411 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
412 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
419 sasl_err2ldap( int saslerr )
425 rc = LDAP_MORE_RESULTS_TO_RETURN;
428 rc = LDAP_LOCAL_ERROR;
434 rc = LDAP_LOCAL_ERROR;
440 rc = LDAP_AUTH_UNKNOWN;
443 rc = LDAP_AUTH_UNKNOWN;
446 rc = LDAP_PARAM_ERROR;
450 rc = LDAP_AUTH_UNKNOWN;
453 rc = LDAP_LOCAL_ERROR;
457 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
471 assert( lc->lconn_sasl_ctx == NULL );
473 if ( host == NULL ) {
474 ld->ld_errno = LDAP_LOCAL_ERROR;
478 #if SASL_VERSION_MAJOR >= 2
479 rc = sasl_client_new( "ldap", host, NULL, NULL,
482 rc = sasl_client_new( "ldap", host, NULL,
483 SASL_SECURITY_LAYER, &ctx );
486 if ( rc != SASL_OK ) {
487 ld->ld_errno = sasl_err2ldap( rc );
492 LDAP_LOG ( TRANSPORT, DETAIL1, "ldap_int_sasl_open: host=%s\n",
495 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
499 lc->lconn_sasl_ctx = ctx;
502 #if SASL_VERSION_MAJOR >= 2
503 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
506 sasl_external_properties_t extprops;
507 memset(&extprops, 0L, sizeof(extprops));
510 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
511 (void *) &extprops );
514 LDAP_LOG ( TRANSPORT, DETAIL1,
515 "ldap_int_sasl_open: ssf=%ld\n", (long) ssf, 0, 0 );
517 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: ssf=%ld\n",
525 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
527 sasl_conn_t *ctx = lc->lconn_sasl_ctx;
530 sasl_dispose( &ctx );
531 lc->lconn_sasl_ctx = NULL;
542 LDAPControl **sctrls,
543 LDAPControl **cctrls,
545 LDAP_SASL_INTERACT_PROC *interact,
549 const char *mech = NULL;
550 const char *pmech = NULL;
552 sasl_ssf_t *ssf = NULL;
554 sasl_interact_t *prompts = NULL;
560 LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n",
561 mechs ? mechs : "<null>", 0, 0 );
563 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
564 mechs ? mechs : "<null>", 0, 0 );
567 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
568 if (ld->ld_version < LDAP_VERSION3) {
569 ld->ld_errno = LDAP_NOT_SUPPORTED;
573 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
575 if ( sd == AC_SOCKET_INVALID ) {
576 /* not connected yet */
579 rc = ldap_open_defconn( ld );
580 if( rc < 0 ) return ld->ld_errno;
582 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
584 if( sd == AC_SOCKET_INVALID ) {
585 ld->ld_errno = LDAP_LOCAL_ERROR;
590 ctx = ld->ld_defconn->lconn_sasl_ctx;
593 ld->ld_errno = LDAP_LOCAL_ERROR;
597 /* (re)set security properties */
598 sasl_setprop( ctx, SASL_SEC_PROPS,
599 &ld->ld_options.ldo_sasl_secprops );
605 saslrc = sasl_client_start( ctx,
607 #if SASL_VERSION_MAJOR < 2
611 (SASL_CONST char **)&ccred.bv_val,
615 if( pmech == NULL && mech != NULL ) {
618 if( flags != LDAP_SASL_QUIET ) {
620 "SASL/%s authentication started\n",
625 #if SASL_VERSION_MAJOR >= 2
626 /* XXX the application should free interact results. */
627 if ( prompts != NULL && prompts->result != NULL ) {
628 LDAP_FREE( (void *)prompts->result );
629 prompts->result = NULL;
633 if( saslrc == SASL_INTERACT ) {
635 if( !interact ) break;
636 res = (interact)( ld, flags, defaults, prompts );
637 if( res != LDAP_SUCCESS ) {
641 } while ( saslrc == SASL_INTERACT );
643 ccred.bv_len = credlen;
645 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
646 ld->ld_errno = sasl_err2ldap( saslrc );
647 #if SASL_VERSION_MAJOR >= 2
648 ld->ld_error = (char *)sasl_errdetail( ctx );
654 struct berval *scred;
659 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
661 if ( ccred.bv_val != NULL ) {
662 #if SASL_VERSION_MAJOR < 2
663 LDAP_FREE( ccred.bv_val );
668 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
669 if( scred && scred->bv_len ) {
670 /* and server provided us with data? */
672 LDAP_LOG ( TRANSPORT, DETAIL1,
673 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
674 rc, saslrc, scred->bv_len );
676 Debug( LDAP_DEBUG_TRACE,
677 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
678 rc, saslrc, scred->bv_len );
685 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
686 /* we're done, no need to step */
687 if( scred && scred->bv_len ) {
688 /* but server provided us with data! */
690 LDAP_LOG ( TRANSPORT, DETAIL1,
691 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
692 rc, saslrc, scred->bv_len );
694 Debug( LDAP_DEBUG_TRACE,
695 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
696 rc, saslrc, scred->bv_len );
699 return ld->ld_errno = LDAP_LOCAL_ERROR;
705 saslrc = sasl_client_step( ctx,
706 (scred == NULL) ? NULL : scred->bv_val,
707 (scred == NULL) ? 0 : scred->bv_len,
709 (SASL_CONST char **)&ccred.bv_val,
713 LDAP_LOG ( TRANSPORT, DETAIL1,
714 "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
716 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
720 #if SASL_VERSION_MAJOR >= 2
721 /* XXX the application should free interact results. */
722 if ( prompts != NULL && prompts->result != NULL ) {
723 LDAP_FREE( (void *)prompts->result );
724 prompts->result = NULL;
728 if( saslrc == SASL_INTERACT ) {
730 if( !interact ) break;
731 res = (interact)( ld, flags, defaults, prompts );
732 if( res != LDAP_SUCCESS ) {
736 } while ( saslrc == SASL_INTERACT );
738 ccred.bv_len = credlen;
741 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
742 ld->ld_errno = sasl_err2ldap( saslrc );
743 #if SASL_VERSION_MAJOR >= 2
744 ld->ld_error = (char *)sasl_errdetail( ctx );
748 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
750 if ( rc != LDAP_SUCCESS ) {
754 if ( saslrc != SASL_OK ) {
755 #if SASL_VERSION_MAJOR >= 2
756 ld->ld_error = (char *)sasl_errdetail( ctx );
758 return ld->ld_errno = sasl_err2ldap( saslrc );
761 if( flags != LDAP_SASL_QUIET ) {
762 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
763 if( saslrc == SASL_OK && data && *data ) {
764 fprintf( stderr, "SASL username: %s\n", data );
767 #if SASL_VERSION_MAJOR >= 2
768 saslrc = sasl_getprop( ctx, SASL_DEFUSERREALM, (SASL_CONST void **) &data );
770 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
772 if( saslrc == SASL_OK && data && *data ) {
773 fprintf( stderr, "SASL realm: %s\n", data );
777 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
778 if( saslrc == SASL_OK ) {
779 if( flags != LDAP_SASL_QUIET ) {
780 fprintf( stderr, "SASL SSF: %lu\n",
781 (unsigned long) *ssf );
785 if( flags != LDAP_SASL_QUIET ) {
786 fprintf( stderr, "SASL installing layers\n" );
788 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
796 ldap_int_sasl_external(
804 #if SASL_VERSION_MAJOR < 2
805 sasl_external_properties_t extprops;
808 ctx = conn->lconn_sasl_ctx;
811 return LDAP_LOCAL_ERROR;
814 #if SASL_VERSION_MAJOR >= 2
815 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
817 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
819 memset( &extprops, '\0', sizeof(extprops) );
821 extprops.auth_id = (char *) authid;
823 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
824 (void *) &extprops );
827 if ( sc != SASL_OK ) {
828 return LDAP_LOCAL_ERROR;
835 int ldap_pvt_sasl_secprops(
837 sasl_security_properties_t *secprops )
840 char **props = ldap_str2charray( in, "," );
843 sasl_ssf_t max_ssf = 0;
845 sasl_ssf_t min_ssf = 0;
847 unsigned maxbufsize = 0;
848 int got_maxbufsize = 0;
850 if( props == NULL || secprops == NULL ) {
851 return LDAP_PARAM_ERROR;
854 for( i=0; props[i]; i++ ) {
855 if( !strcasecmp(props[i], "none") ) {
858 } else if( !strcasecmp(props[i], "noplain") ) {
860 sflags |= SASL_SEC_NOPLAINTEXT;
862 } else if( !strcasecmp(props[i], "noactive") ) {
864 sflags |= SASL_SEC_NOACTIVE;
866 } else if( !strcasecmp(props[i], "nodict") ) {
868 sflags |= SASL_SEC_NODICTIONARY;
870 } else if( !strcasecmp(props[i], "forwardsec") ) {
872 sflags |= SASL_SEC_FORWARD_SECRECY;
874 } else if( !strcasecmp(props[i], "noanonymous")) {
876 sflags |= SASL_SEC_NOANONYMOUS;
878 } else if( !strcasecmp(props[i], "passcred") ) {
880 sflags |= SASL_SEC_PASS_CREDENTIALS;
882 } else if( !strncasecmp(props[i],
883 "minssf=", sizeof("minssf")) )
885 if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
887 min_ssf = atoi( &props[i][sizeof("minssf")] );
889 return LDAP_NOT_SUPPORTED;
892 } else if( !strncasecmp(props[i],
893 "maxssf=", sizeof("maxssf")) )
895 if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
897 max_ssf = atoi( &props[i][sizeof("maxssf")] );
899 return LDAP_NOT_SUPPORTED;
902 } else if( !strncasecmp(props[i],
903 "maxbufsize=", sizeof("maxbufsize")) )
905 if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
907 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
909 return LDAP_NOT_SUPPORTED;
912 if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
913 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
916 return LDAP_PARAM_ERROR;
920 return LDAP_NOT_SUPPORTED;
925 secprops->security_flags = sflags;
928 secprops->min_ssf = min_ssf;
931 secprops->max_ssf = max_ssf;
934 secprops->maxbufsize = maxbufsize;
937 ldap_charray_free( props );
942 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
947 case LDAP_OPT_X_SASL_SECPROPS:
948 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
949 if( rc == LDAP_SUCCESS ) return 0;
956 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
962 case LDAP_OPT_X_SASL_MECH: {
963 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
964 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
966 case LDAP_OPT_X_SASL_REALM: {
967 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
968 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
970 case LDAP_OPT_X_SASL_AUTHCID: {
971 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
972 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
974 case LDAP_OPT_X_SASL_AUTHZID: {
975 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
976 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
979 case LDAP_OPT_X_SASL_SSF: {
984 if( ld->ld_defconn == NULL ) {
988 ctx = ld->ld_defconn->lconn_sasl_ctx;
994 sc = sasl_getprop( ctx, SASL_SSF,
995 (SASL_CONST void **) &ssf );
997 if ( sc != SASL_OK ) {
1001 *(ber_len_t *)arg = *ssf;
1004 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1005 /* this option is write only */
1008 case LDAP_OPT_X_SASL_SSF_MIN:
1009 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1011 case LDAP_OPT_X_SASL_SSF_MAX:
1012 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1014 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1015 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1018 case LDAP_OPT_X_SASL_SECPROPS:
1019 /* this option is write only */
1029 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1035 case LDAP_OPT_X_SASL_SSF:
1036 /* This option is read-only */
1039 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1041 #if SASL_VERSION_MAJOR < 2
1042 sasl_external_properties_t extprops;
1046 if( ld->ld_defconn == NULL ) {
1050 ctx = ld->ld_defconn->lconn_sasl_ctx;
1052 if ( ctx == NULL ) {
1056 #if SASL_VERSION_MAJOR >= 2
1057 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1059 memset(&extprops, 0L, sizeof(extprops));
1061 extprops.ssf = * (ber_len_t *) arg;
1063 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1064 (void *) &extprops );
1067 if ( sc != SASL_OK ) {
1072 case LDAP_OPT_X_SASL_SSF_MIN:
1073 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1075 case LDAP_OPT_X_SASL_SSF_MAX:
1076 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1078 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1079 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1082 case LDAP_OPT_X_SASL_SECPROPS: {
1084 sc = ldap_pvt_sasl_secprops( (char *) arg,
1085 &ld->ld_options.ldo_sasl_secprops );
1087 return sc == LDAP_SUCCESS ? 0 : -1;
1096 #ifdef LDAP_R_COMPILE
1097 #define LDAP_DEBUG_R_SASL
1098 void *ldap_pvt_sasl_mutex_new(void)
1100 ldap_pvt_thread_mutex_t *mutex;
1102 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1103 sizeof(ldap_pvt_thread_mutex_t) );
1105 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1108 #ifndef LDAP_DEBUG_R_SASL
1110 #endif /* !LDAP_DEBUG_R_SASL */
1114 int ldap_pvt_sasl_mutex_lock(void *mutex)
1116 #ifdef LDAP_DEBUG_R_SASL
1117 if ( mutex == NULL ) {
1120 #else /* !LDAP_DEBUG_R_SASL */
1122 #endif /* !LDAP_DEBUG_R_SASL */
1123 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1124 ? SASL_FAIL : SASL_OK;
1127 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1129 #ifdef LDAP_DEBUG_R_SASL
1130 if ( mutex == NULL ) {
1133 #else /* !LDAP_DEBUG_R_SASL */
1135 #endif /* !LDAP_DEBUG_R_SASL */
1136 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1137 ? SASL_FAIL : SASL_OK;
1140 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1142 #ifdef LDAP_DEBUG_R_SASL
1143 if ( mutex == NULL ) {
1146 #else /* !LDAP_DEBUG_R_SASL */
1148 #endif /* !LDAP_DEBUG_R_SASL */
1149 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1155 int ldap_int_sasl_init( void )
1156 { return LDAP_SUCCESS; }
1158 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1159 { return LDAP_SUCCESS; }
1166 LDAPControl **sctrls,
1167 LDAPControl **cctrls,
1169 LDAP_SASL_INTERACT_PROC *interact,
1171 { return LDAP_NOT_SUPPORTED; }
1174 ldap_int_sasl_external(
1177 const char * authid,
1179 { return LDAP_SUCCESS; }
1181 #endif /* HAVE_CYRUS_SASL */