3 * Copyright 1999-2003 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. */
199 return size + 4; /* include the size !!! */
202 /* Drop a processed packet from the input buffer */
204 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, unsigned max, int debuglevel )
208 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
210 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
211 sec_buf_in->buf_end, len );
214 sec_buf_in->buf_end = sb_sasl_pkt_length(
215 (unsigned char *) sec_buf_in->buf_base, max, debuglevel);
218 sec_buf_in->buf_end = 0;
220 sec_buf_in->buf_ptr = len;
224 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
226 struct sb_sasl_data *p;
227 ber_slen_t ret, bufptr;
229 assert( sbiod != NULL );
230 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
232 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
234 /* Are there anything left in the buffer? */
235 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
242 #if SASL_VERSION_MAJOR >= 2
243 ber_pvt_sb_buf_init( &p->buf_in );
245 ber_pvt_sb_buf_destroy( &p->buf_in );
248 /* Read the length of the packet */
249 while ( p->sec_buf_in.buf_ptr < 4 ) {
250 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
251 4 - p->sec_buf_in.buf_ptr );
253 if ( ( ret < 0 ) && ( errno == EINTR ) )
257 return bufptr ? bufptr : ret;
259 p->sec_buf_in.buf_ptr += ret;
262 /* The new packet always starts at p->sec_buf_in.buf_base */
263 ret = sb_sasl_pkt_length( (unsigned char *) p->sec_buf_in.buf_base,
264 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
266 /* Grow the packet buffer if neccessary */
267 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
268 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
273 p->sec_buf_in.buf_end = ret;
275 /* Did we read the whole encrypted packet? */
276 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
277 /* No, we have got only a part of it */
278 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
280 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
281 p->sec_buf_in.buf_ptr, ret );
283 if ( ( ret < 0 ) && ( errno == EINTR ) )
287 return bufptr ? bufptr : ret;
289 p->sec_buf_in.buf_ptr += ret;
292 /* Decode the packet */
293 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
294 p->sec_buf_in.buf_end,
295 (SASL_CONST char **)&p->buf_in.buf_base,
296 (unsigned *)&p->buf_in.buf_end );
298 /* Drop the packet from the input buffer */
299 sb_sasl_drop_packet( &p->sec_buf_in,
300 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
302 if ( ret != SASL_OK ) {
303 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
304 "sb_sasl_read: failed to decode packet: %s\n",
305 sasl_errstring( ret, NULL, NULL ) );
310 p->buf_in.buf_size = p->buf_in.buf_end;
312 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
318 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
320 struct sb_sasl_data *p;
323 assert( sbiod != NULL );
324 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
326 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
328 /* Are there anything left in the buffer? */
329 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
330 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
333 /* Still have something left?? */
334 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
340 /* now encode the next packet. */
341 #if SASL_VERSION_MAJOR >= 2
342 ber_pvt_sb_buf_init( &p->buf_out );
343 /* sasl v2 makes sure this number is correct */
344 if ( len > *p->sasl_maxbuf )
345 len = *p->sasl_maxbuf;
347 ber_pvt_sb_buf_destroy( &p->buf_out );
348 if ( len > *p->sasl_maxbuf - 100 )
349 len = *p->sasl_maxbuf - 100; /* For safety margin */
351 ret = sasl_encode( p->sasl_context, buf, len,
352 (SASL_CONST char **)&p->buf_out.buf_base,
353 (unsigned *)&p->buf_out.buf_size );
354 if ( ret != SASL_OK ) {
355 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
356 "sb_sasl_write: failed to encode packet: %s\n",
357 sasl_errstring( ret, NULL, NULL ) );
360 p->buf_out.buf_end = p->buf_out.buf_size;
362 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
369 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
371 struct sb_sasl_data *p;
373 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
375 if ( opt == LBER_SB_OPT_DATA_READY ) {
376 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
380 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
383 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
384 sb_sasl_setup, /* sbi_setup */
385 sb_sasl_remove, /* sbi_remove */
386 sb_sasl_ctrl, /* sbi_ctrl */
387 sb_sasl_read, /* sbi_read */
388 sb_sasl_write, /* sbi_write */
392 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
395 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_install\n", 0, 0, 0 );
397 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
401 /* don't install the stuff unless security has been negotiated */
403 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
404 &ldap_pvt_sockbuf_io_sasl ) )
407 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
408 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
410 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
411 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
417 void ldap_pvt_sasl_remove( Sockbuf *sb )
419 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
420 LBER_SBIOD_LEVEL_APPLICATION );
422 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
423 LBER_SBIOD_LEVEL_APPLICATION );
428 sasl_err2ldap( int saslerr )
434 rc = LDAP_MORE_RESULTS_TO_RETURN;
437 rc = LDAP_LOCAL_ERROR;
443 rc = LDAP_LOCAL_ERROR;
449 rc = LDAP_AUTH_UNKNOWN;
452 rc = LDAP_AUTH_UNKNOWN;
455 rc = LDAP_PARAM_ERROR;
459 rc = LDAP_AUTH_UNKNOWN;
462 rc = LDAP_LOCAL_ERROR;
466 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
479 assert( lc->lconn_sasl_authctx == NULL );
481 if ( host == NULL ) {
482 ld->ld_errno = LDAP_LOCAL_ERROR;
486 #if SASL_VERSION_MAJOR >= 2
487 rc = sasl_client_new( "ldap", host, NULL, NULL,
490 rc = sasl_client_new( "ldap", host, NULL,
491 SASL_SECURITY_LAYER, &ctx );
494 if ( rc != SASL_OK ) {
495 ld->ld_errno = sasl_err2ldap( rc );
500 LDAP_LOG ( TRANSPORT, DETAIL1, "ldap_int_sasl_open: host=%s\n",
503 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
507 lc->lconn_sasl_authctx = ctx;
512 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
514 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
517 sasl_dispose( &ctx );
518 if ( lc->lconn_sasl_sockctx &&
519 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
520 ctx = lc->lconn_sasl_sockctx;
521 sasl_dispose( &ctx );
523 lc->lconn_sasl_sockctx = NULL;
524 lc->lconn_sasl_authctx = NULL;
535 LDAPControl **sctrls,
536 LDAPControl **cctrls,
538 LDAP_SASL_INTERACT_PROC *interact,
542 const char *mech = NULL;
543 const char *pmech = NULL;
545 sasl_ssf_t *ssf = NULL;
546 sasl_conn_t *ctx, *oldctx = NULL;
547 sasl_interact_t *prompts = NULL;
554 LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n",
555 mechs ? mechs : "<null>", 0, 0 );
557 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
558 mechs ? mechs : "<null>", 0, 0 );
561 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
562 if (ld->ld_version < LDAP_VERSION3) {
563 ld->ld_errno = LDAP_NOT_SUPPORTED;
567 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
569 if ( sd == AC_SOCKET_INVALID ) {
570 /* not connected yet */
573 rc = ldap_open_defconn( ld );
574 if( rc < 0 ) return ld->ld_errno;
576 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
578 if( sd == AC_SOCKET_INVALID ) {
579 ld->ld_errno = LDAP_LOCAL_ERROR;
584 oldctx = ld->ld_defconn->lconn_sasl_authctx;
586 /* If we already have an authentication context, clear it out */
588 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
589 sasl_dispose( &oldctx );
591 ld->ld_defconn->lconn_sasl_authctx = NULL;
594 rc = ldap_int_sasl_open( ld, ld->ld_defconn,
595 ld->ld_defconn->lconn_server->lud_host ?
596 ld->ld_defconn->lconn_server->lud_host : "localhost" );
598 if ( rc != LDAP_SUCCESS ) return rc;
600 ctx = ld->ld_defconn->lconn_sasl_authctx;
603 ssl = ldap_pvt_tls_sb_ctx( ld->ld_sb );
605 struct berval authid = { 0, NULL };
608 fac = ldap_pvt_tls_get_strength( ssl );
609 /* failure is OK, we just can't use SASL EXTERNAL */
610 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
612 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
613 LDAP_FREE( authid.bv_val );
616 /* Check for local */
617 if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
618 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
619 "cn=peercred,cn=external,cn=auth")];
620 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
621 "cn=peercred,cn=external,cn=auth",
622 (int) geteuid(), (int) getegid() );
623 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
626 /* (re)set security properties */
627 sasl_setprop( ctx, SASL_SEC_PROPS,
628 &ld->ld_options.ldo_sasl_secprops );
634 saslrc = sasl_client_start( ctx,
636 #if SASL_VERSION_MAJOR < 2
640 (SASL_CONST char **)&ccred.bv_val,
644 if( pmech == NULL && mech != NULL ) {
647 if( flags != LDAP_SASL_QUIET ) {
649 "SASL/%s authentication started\n",
654 if( saslrc == SASL_INTERACT ) {
656 if( !interact ) break;
657 res = (interact)( ld, flags, defaults, prompts );
659 if( res != LDAP_SUCCESS ) break;
661 } while ( saslrc == SASL_INTERACT );
663 ccred.bv_len = credlen;
665 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
666 rc = ld->ld_errno = sasl_err2ldap( saslrc );
667 #if SASL_VERSION_MAJOR >= 2
668 ld->ld_error = (char *)sasl_errdetail( ctx );
674 struct berval *scred;
679 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
681 if ( ccred.bv_val != NULL ) {
682 #if SASL_VERSION_MAJOR < 2
683 LDAP_FREE( ccred.bv_val );
688 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
689 if( scred && scred->bv_len ) {
690 /* and server provided us with data? */
692 LDAP_LOG ( TRANSPORT, DETAIL1,
693 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
694 rc, saslrc, scred->bv_len );
696 Debug( LDAP_DEBUG_TRACE,
697 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
698 rc, saslrc, scred->bv_len );
706 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
707 /* we're done, no need to step */
708 if( scred && scred->bv_len ) {
709 /* but server provided us with data! */
711 LDAP_LOG ( TRANSPORT, DETAIL1,
712 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
713 rc, saslrc, scred->bv_len );
715 Debug( LDAP_DEBUG_TRACE,
716 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
717 rc, saslrc, scred->bv_len );
720 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
727 saslrc = sasl_client_step( ctx,
728 (scred == NULL) ? NULL : scred->bv_val,
729 (scred == NULL) ? 0 : scred->bv_len,
731 (SASL_CONST char **)&ccred.bv_val,
735 LDAP_LOG ( TRANSPORT, DETAIL1,
736 "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
738 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
742 if( saslrc == SASL_INTERACT ) {
744 if( !interact ) break;
745 res = (interact)( ld, flags, defaults, prompts );
746 if( res != LDAP_SUCCESS ) break;
748 } while ( saslrc == SASL_INTERACT );
750 ccred.bv_len = credlen;
753 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
754 ld->ld_errno = sasl_err2ldap( saslrc );
755 #if SASL_VERSION_MAJOR >= 2
756 ld->ld_error = (char *)sasl_errdetail( ctx );
761 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
763 if ( rc != LDAP_SUCCESS ) goto done;
765 if ( saslrc != SASL_OK ) {
766 #if SASL_VERSION_MAJOR >= 2
767 ld->ld_error = (char *)sasl_errdetail( ctx );
769 rc = ld->ld_errno = sasl_err2ldap( saslrc );
773 if( flags != LDAP_SASL_QUIET ) {
774 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
775 if( saslrc == SASL_OK && data && *data ) {
776 fprintf( stderr, "SASL username: %s\n", data );
779 #if SASL_VERSION_MAJOR < 2
780 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
781 if( saslrc == SASL_OK && data && *data ) {
782 fprintf( stderr, "SASL realm: %s\n", data );
787 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
788 if( saslrc == SASL_OK ) {
789 if( flags != LDAP_SASL_QUIET ) {
790 fprintf( stderr, "SASL SSF: %lu\n",
791 (unsigned long) *ssf );
795 if( flags != LDAP_SASL_QUIET ) {
796 fprintf( stderr, "SASL installing layers\n" );
798 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
799 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
800 sasl_dispose( &oldctx );
801 ldap_pvt_sasl_remove( ld->ld_sb );
803 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
804 ld->ld_defconn->lconn_sasl_sockctx = ctx;
807 ld->ld_defconn->lconn_sasl_authctx = ctx;
814 ldap_int_sasl_external(
822 #if SASL_VERSION_MAJOR < 2
823 sasl_external_properties_t extprops;
826 ctx = conn->lconn_sasl_authctx;
829 return LDAP_LOCAL_ERROR;
832 #if SASL_VERSION_MAJOR >= 2
833 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
835 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
837 memset( &extprops, '\0', sizeof(extprops) );
839 extprops.auth_id = (char *) authid;
841 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
842 (void *) &extprops );
845 if ( sc != SASL_OK ) {
846 return LDAP_LOCAL_ERROR;
853 int ldap_pvt_sasl_secprops(
855 sasl_security_properties_t *secprops )
858 char **props = ldap_str2charray( in, "," );
861 sasl_ssf_t max_ssf = 0;
863 sasl_ssf_t min_ssf = 0;
865 unsigned maxbufsize = 0;
866 int got_maxbufsize = 0;
868 if( props == NULL || secprops == NULL ) {
869 return LDAP_PARAM_ERROR;
872 for( i=0; props[i]; i++ ) {
873 if( !strcasecmp(props[i], "none") ) {
876 } else if( !strcasecmp(props[i], "noplain") ) {
878 sflags |= SASL_SEC_NOPLAINTEXT;
880 } else if( !strcasecmp(props[i], "noactive") ) {
882 sflags |= SASL_SEC_NOACTIVE;
884 } else if( !strcasecmp(props[i], "nodict") ) {
886 sflags |= SASL_SEC_NODICTIONARY;
888 } else if( !strcasecmp(props[i], "forwardsec") ) {
890 sflags |= SASL_SEC_FORWARD_SECRECY;
892 } else if( !strcasecmp(props[i], "noanonymous")) {
894 sflags |= SASL_SEC_NOANONYMOUS;
896 } else if( !strcasecmp(props[i], "passcred") ) {
898 sflags |= SASL_SEC_PASS_CREDENTIALS;
900 } else if( !strncasecmp(props[i],
901 "minssf=", sizeof("minssf")) )
903 if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
905 min_ssf = atoi( &props[i][sizeof("minssf")] );
907 return LDAP_NOT_SUPPORTED;
910 } else if( !strncasecmp(props[i],
911 "maxssf=", sizeof("maxssf")) )
913 if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
915 max_ssf = atoi( &props[i][sizeof("maxssf")] );
917 return LDAP_NOT_SUPPORTED;
920 } else if( !strncasecmp(props[i],
921 "maxbufsize=", sizeof("maxbufsize")) )
923 if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
925 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
927 return LDAP_NOT_SUPPORTED;
930 if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
931 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
934 return LDAP_PARAM_ERROR;
938 return LDAP_NOT_SUPPORTED;
943 secprops->security_flags = sflags;
946 secprops->min_ssf = min_ssf;
949 secprops->max_ssf = max_ssf;
952 secprops->maxbufsize = maxbufsize;
955 ldap_charray_free( props );
960 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
965 case LDAP_OPT_X_SASL_SECPROPS:
966 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
967 if( rc == LDAP_SUCCESS ) return 0;
974 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
980 case LDAP_OPT_X_SASL_MECH: {
981 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
982 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
984 case LDAP_OPT_X_SASL_REALM: {
985 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
986 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
988 case LDAP_OPT_X_SASL_AUTHCID: {
989 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
990 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
992 case LDAP_OPT_X_SASL_AUTHZID: {
993 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
994 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
997 case LDAP_OPT_X_SASL_SSF: {
1002 if( ld->ld_defconn == NULL ) {
1006 ctx = ld->ld_defconn->lconn_sasl_sockctx;
1008 if ( ctx == NULL ) {
1012 sc = sasl_getprop( ctx, SASL_SSF,
1013 (SASL_CONST void **) &ssf );
1015 if ( sc != SASL_OK ) {
1019 *(ber_len_t *)arg = *ssf;
1022 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1023 /* this option is write only */
1026 case LDAP_OPT_X_SASL_SSF_MIN:
1027 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1029 case LDAP_OPT_X_SASL_SSF_MAX:
1030 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1032 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1033 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1036 case LDAP_OPT_X_SASL_SECPROPS:
1037 /* this option is write only */
1047 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1053 case LDAP_OPT_X_SASL_SSF:
1054 /* This option is read-only */
1057 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1059 #if SASL_VERSION_MAJOR < 2
1060 sasl_external_properties_t extprops;
1064 if( ld->ld_defconn == NULL ) {
1068 ctx = ld->ld_defconn->lconn_sasl_authctx;
1070 if ( ctx == NULL ) {
1074 #if SASL_VERSION_MAJOR >= 2
1075 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1077 memset(&extprops, 0L, sizeof(extprops));
1079 extprops.ssf = * (ber_len_t *) arg;
1081 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1082 (void *) &extprops );
1085 if ( sc != SASL_OK ) {
1090 case LDAP_OPT_X_SASL_SSF_MIN:
1091 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1093 case LDAP_OPT_X_SASL_SSF_MAX:
1094 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1096 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1097 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1100 case LDAP_OPT_X_SASL_SECPROPS: {
1102 sc = ldap_pvt_sasl_secprops( (char *) arg,
1103 &ld->ld_options.ldo_sasl_secprops );
1105 return sc == LDAP_SUCCESS ? 0 : -1;
1114 #ifdef LDAP_R_COMPILE
1115 #define LDAP_DEBUG_R_SASL
1116 void *ldap_pvt_sasl_mutex_new(void)
1118 ldap_pvt_thread_mutex_t *mutex;
1120 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1121 sizeof(ldap_pvt_thread_mutex_t) );
1123 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1126 #ifndef LDAP_DEBUG_R_SASL
1128 #endif /* !LDAP_DEBUG_R_SASL */
1132 int ldap_pvt_sasl_mutex_lock(void *mutex)
1134 #ifdef LDAP_DEBUG_R_SASL
1135 if ( mutex == NULL ) {
1138 #else /* !LDAP_DEBUG_R_SASL */
1140 #endif /* !LDAP_DEBUG_R_SASL */
1141 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1142 ? SASL_FAIL : SASL_OK;
1145 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1147 #ifdef LDAP_DEBUG_R_SASL
1148 if ( mutex == NULL ) {
1151 #else /* !LDAP_DEBUG_R_SASL */
1153 #endif /* !LDAP_DEBUG_R_SASL */
1154 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1155 ? SASL_FAIL : SASL_OK;
1158 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1160 #ifdef LDAP_DEBUG_R_SASL
1161 if ( mutex == NULL ) {
1164 #else /* !LDAP_DEBUG_R_SASL */
1166 #endif /* !LDAP_DEBUG_R_SASL */
1167 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1173 int ldap_int_sasl_init( void )
1174 { return LDAP_SUCCESS; }
1176 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1177 { return LDAP_SUCCESS; }
1184 LDAPControl **sctrls,
1185 LDAPControl **cctrls,
1187 LDAP_SASL_INTERACT_PROC *interact,
1189 { return LDAP_NOT_SUPPORTED; }
1192 ldap_int_sasl_external(
1195 const char * authid,
1197 { return LDAP_SUCCESS; }
1199 #endif /* HAVE_CYRUS_SASL */