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 /* stringify the version number, sasl.h doesn't do it for us */
61 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
62 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
63 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
66 sasl_version( NULL, &rc );
67 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
68 (rc & 0xffff) < SASL_VERSION_STEP) {
69 char version[sizeof("xxx.xxx.xxxxx")];
70 sprintf( version, "%d.%d.%d", rc >> 24, rc >> 16 * 0xff,
74 LDAP_LOG( TRANSPORT, INFO,
75 "ldap_int_sasl_init: SASL library version mismatch:"
76 " expected " SASL_VERSION_STRING ","
77 " got %s\n", version, 0, 0 );
79 Debug( LDAP_DEBUG_ANY,
80 "ldap_int_sasl_init: SASL library version mismatch:"
81 " expected " SASL_VERSION_STRING ","
82 " got %s\n", version, 0, 0 );
88 if ( sasl_initialized ) {
92 /* SASL 2 takes care of its own memory completely internally */
93 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
99 #endif /* CSRIMALLOC */
101 #ifdef LDAP_R_COMPILE
103 ldap_pvt_sasl_mutex_new,
104 ldap_pvt_sasl_mutex_lock,
105 ldap_pvt_sasl_mutex_unlock,
106 ldap_pvt_sasl_mutex_dispose );
108 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
111 if ( sasl_client_init( client_callbacks ) == SASL_OK ) {
112 sasl_initialized = 1;
116 #if SASL_VERSION_MAJOR < 2
117 /* A no-op to make sure we link with Cyrus 1.5 */
118 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
124 * SASL encryption support for LBER Sockbufs
127 struct sb_sasl_data {
128 sasl_conn_t *sasl_context;
129 unsigned *sasl_maxbuf;
130 Sockbuf_Buf sec_buf_in;
136 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
138 struct sb_sasl_data *p;
140 assert( sbiod != NULL );
142 p = LBER_MALLOC( sizeof( *p ) );
145 p->sasl_context = (sasl_conn_t *)arg;
146 ber_pvt_sb_buf_init( &p->sec_buf_in );
147 ber_pvt_sb_buf_init( &p->buf_in );
148 ber_pvt_sb_buf_init( &p->buf_out );
149 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
154 sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
155 (SASL_CONST void **) &p->sasl_maxbuf );
157 if ( p->sasl_maxbuf == 0 )
158 p->sasl_maxbuf = SASL_MAX_BUFF_SIZE;
160 sbiod->sbiod_pvt = p;
166 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
168 struct sb_sasl_data *p;
170 assert( sbiod != NULL );
172 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
173 #if SASL_VERSION_MAJOR >= 2
175 * SASLv2 encode/decode buffers are managed by
176 * libsasl2. Ensure they are not freed by liblber.
178 p->buf_in.buf_base = NULL;
179 p->buf_out.buf_base = NULL;
181 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
182 ber_pvt_sb_buf_destroy( &p->buf_in );
183 ber_pvt_sb_buf_destroy( &p->buf_out );
185 sbiod->sbiod_pvt = NULL;
190 sb_sasl_pkt_length( const unsigned char *buf, unsigned max, int debuglevel )
194 assert( buf != NULL );
201 if ( size > SASL_MAX_BUFF_SIZE ) {
202 /* somebody is trying to mess me up. */
203 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
204 "sb_sasl_pkt_length: received illegal packet length "
205 "of %lu bytes\n", (unsigned long)size );
206 size = 16; /* this should lead to an error. */
209 return size + 4; /* include the size !!! */
212 /* Drop a processed packet from the input buffer */
214 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, unsigned max, int debuglevel )
218 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
220 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
221 sec_buf_in->buf_end, len );
224 sec_buf_in->buf_end = sb_sasl_pkt_length(
225 (unsigned char *) sec_buf_in->buf_base, max, debuglevel);
228 sec_buf_in->buf_end = 0;
230 sec_buf_in->buf_ptr = len;
234 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
236 struct sb_sasl_data *p;
237 ber_slen_t ret, bufptr;
239 assert( sbiod != NULL );
240 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
242 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
244 /* Are there anything left in the buffer? */
245 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
252 #if SASL_VERSION_MAJOR >= 2
253 ber_pvt_sb_buf_init( &p->buf_in );
255 ber_pvt_sb_buf_destroy( &p->buf_in );
258 /* Read the length of the packet */
259 while ( p->sec_buf_in.buf_ptr < 4 ) {
260 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
261 4 - p->sec_buf_in.buf_ptr );
263 if ( ( ret < 0 ) && ( errno == EINTR ) )
267 return bufptr ? bufptr : ret;
269 p->sec_buf_in.buf_ptr += ret;
272 /* The new packet always starts at p->sec_buf_in.buf_base */
273 ret = sb_sasl_pkt_length( (unsigned char *) p->sec_buf_in.buf_base,
274 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
276 /* Grow the packet buffer if neccessary */
277 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
278 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
283 p->sec_buf_in.buf_end = ret;
285 /* Did we read the whole encrypted packet? */
286 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
287 /* No, we have got only a part of it */
288 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
290 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
291 p->sec_buf_in.buf_ptr, ret );
293 if ( ( ret < 0 ) && ( errno == EINTR ) )
297 return bufptr ? bufptr : ret;
299 p->sec_buf_in.buf_ptr += ret;
302 /* Decode the packet */
303 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
304 p->sec_buf_in.buf_end,
305 (SASL_CONST char **)&p->buf_in.buf_base,
306 (unsigned *)&p->buf_in.buf_end );
308 /* Drop the packet from the input buffer */
309 sb_sasl_drop_packet( &p->sec_buf_in,
310 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
312 if ( ret != SASL_OK ) {
313 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
314 "sb_sasl_read: failed to decode packet: %s\n",
315 sasl_errstring( ret, NULL, NULL ) );
320 p->buf_in.buf_size = p->buf_in.buf_end;
322 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
328 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
330 struct sb_sasl_data *p;
333 assert( sbiod != NULL );
334 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
336 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
338 /* Are there anything left in the buffer? */
339 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
340 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
343 /* Still have something left?? */
344 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
350 /* now encode the next packet. */
351 #if SASL_VERSION_MAJOR >= 2
352 ber_pvt_sb_buf_init( &p->buf_out );
353 /* sasl v2 makes sure this number is correct */
354 if ( len > *p->sasl_maxbuf )
355 len = *p->sasl_maxbuf;
357 ber_pvt_sb_buf_destroy( &p->buf_out );
358 if ( len > *p->sasl_maxbuf - 100 )
359 len = *p->sasl_maxbuf - 100; /* For safety margin */
361 ret = sasl_encode( p->sasl_context, buf, len,
362 (SASL_CONST char **)&p->buf_out.buf_base,
363 (unsigned *)&p->buf_out.buf_size );
364 if ( ret != SASL_OK ) {
365 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
366 "sb_sasl_write: failed to encode packet: %s\n",
367 sasl_errstring( ret, NULL, NULL ) );
370 p->buf_out.buf_end = p->buf_out.buf_size;
372 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
379 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
381 struct sb_sasl_data *p;
383 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
385 if ( opt == LBER_SB_OPT_DATA_READY ) {
386 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
390 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
393 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
394 sb_sasl_setup, /* sbi_setup */
395 sb_sasl_remove, /* sbi_remove */
396 sb_sasl_ctrl, /* sbi_ctrl */
397 sb_sasl_read, /* sbi_read */
398 sb_sasl_write, /* sbi_write */
402 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
405 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_install\n", 0, 0, 0 );
407 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
411 /* don't install the stuff unless security has been negotiated */
413 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
414 &ldap_pvt_sockbuf_io_sasl ) )
417 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
418 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
420 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
421 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
427 void ldap_pvt_sasl_remove( Sockbuf *sb )
429 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
430 LBER_SBIOD_LEVEL_APPLICATION );
432 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
433 LBER_SBIOD_LEVEL_APPLICATION );
438 sasl_err2ldap( int saslerr )
444 rc = LDAP_MORE_RESULTS_TO_RETURN;
447 rc = LDAP_LOCAL_ERROR;
453 rc = LDAP_LOCAL_ERROR;
459 rc = LDAP_AUTH_UNKNOWN;
462 rc = LDAP_AUTH_UNKNOWN;
465 rc = LDAP_PARAM_ERROR;
469 rc = LDAP_AUTH_UNKNOWN;
472 rc = LDAP_LOCAL_ERROR;
476 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
489 assert( lc->lconn_sasl_authctx == NULL );
491 if ( host == NULL ) {
492 ld->ld_errno = LDAP_LOCAL_ERROR;
496 #if SASL_VERSION_MAJOR >= 2
497 rc = sasl_client_new( "ldap", host, NULL, NULL,
500 rc = sasl_client_new( "ldap", host, NULL,
501 SASL_SECURITY_LAYER, &ctx );
504 if ( rc != SASL_OK ) {
505 ld->ld_errno = sasl_err2ldap( rc );
510 LDAP_LOG ( TRANSPORT, DETAIL1, "ldap_int_sasl_open: host=%s\n",
513 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
517 lc->lconn_sasl_authctx = ctx;
522 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
524 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
527 sasl_dispose( &ctx );
528 if ( lc->lconn_sasl_sockctx &&
529 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
530 ctx = lc->lconn_sasl_sockctx;
531 sasl_dispose( &ctx );
533 lc->lconn_sasl_sockctx = NULL;
534 lc->lconn_sasl_authctx = NULL;
545 LDAPControl **sctrls,
546 LDAPControl **cctrls,
548 LDAP_SASL_INTERACT_PROC *interact,
552 const char *mech = NULL;
553 const char *pmech = NULL;
555 sasl_ssf_t *ssf = NULL;
556 sasl_conn_t *ctx, *oldctx = NULL;
557 sasl_interact_t *prompts = NULL;
564 LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n",
565 mechs ? mechs : "<null>", 0, 0 );
567 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
568 mechs ? mechs : "<null>", 0, 0 );
571 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
572 if (ld->ld_version < LDAP_VERSION3) {
573 ld->ld_errno = LDAP_NOT_SUPPORTED;
577 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
579 if ( sd == AC_SOCKET_INVALID ) {
580 /* not connected yet */
583 rc = ldap_open_defconn( ld );
584 if( rc < 0 ) return ld->ld_errno;
586 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
588 if( sd == AC_SOCKET_INVALID ) {
589 ld->ld_errno = LDAP_LOCAL_ERROR;
594 oldctx = ld->ld_defconn->lconn_sasl_authctx;
596 /* If we already have an authentication context, clear it out */
598 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
599 sasl_dispose( &oldctx );
601 ld->ld_defconn->lconn_sasl_authctx = NULL;
604 rc = ldap_int_sasl_open( ld, ld->ld_defconn,
605 ld->ld_defconn->lconn_server->lud_host ?
606 ld->ld_defconn->lconn_server->lud_host : "localhost" );
608 if ( rc != LDAP_SUCCESS ) return rc;
610 ctx = ld->ld_defconn->lconn_sasl_authctx;
613 ssl = ldap_pvt_tls_sb_ctx( ld->ld_sb );
615 struct berval authid = { 0, NULL };
618 fac = ldap_pvt_tls_get_strength( ssl );
619 /* failure is OK, we just can't use SASL EXTERNAL */
620 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
622 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
623 LDAP_FREE( authid.bv_val );
626 /* Check for local */
627 if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
628 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
629 "cn=peercred,cn=external,cn=auth")];
630 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
631 "cn=peercred,cn=external,cn=auth",
632 (int) geteuid(), (int) getegid() );
633 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
636 /* (re)set security properties */
637 sasl_setprop( ctx, SASL_SEC_PROPS,
638 &ld->ld_options.ldo_sasl_secprops );
644 saslrc = sasl_client_start( ctx,
646 #if SASL_VERSION_MAJOR < 2
650 (SASL_CONST char **)&ccred.bv_val,
654 if( pmech == NULL && mech != NULL ) {
657 if( flags != LDAP_SASL_QUIET ) {
659 "SASL/%s authentication started\n",
664 if( saslrc == SASL_INTERACT ) {
666 if( !interact ) break;
667 res = (interact)( ld, flags, defaults, prompts );
669 if( res != LDAP_SUCCESS ) break;
671 } while ( saslrc == SASL_INTERACT );
673 ccred.bv_len = credlen;
675 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
676 rc = ld->ld_errno = sasl_err2ldap( saslrc );
677 #if SASL_VERSION_MAJOR >= 2
678 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
684 struct berval *scred;
689 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
691 if ( ccred.bv_val != NULL ) {
692 #if SASL_VERSION_MAJOR < 2
693 LDAP_FREE( ccred.bv_val );
698 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
699 if( scred && scred->bv_len ) {
700 /* and server provided us with data? */
702 LDAP_LOG ( TRANSPORT, DETAIL1,
703 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
704 rc, saslrc, scred->bv_len );
706 Debug( LDAP_DEBUG_TRACE,
707 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
708 rc, saslrc, scred->bv_len );
716 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
717 /* we're done, no need to step */
718 if( scred && scred->bv_len ) {
719 /* but server provided us with data! */
721 LDAP_LOG ( TRANSPORT, DETAIL1,
722 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
723 rc, saslrc, scred->bv_len );
725 Debug( LDAP_DEBUG_TRACE,
726 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
727 rc, saslrc, scred->bv_len );
730 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
737 saslrc = sasl_client_step( ctx,
738 (scred == NULL) ? NULL : scred->bv_val,
739 (scred == NULL) ? 0 : scred->bv_len,
741 (SASL_CONST char **)&ccred.bv_val,
745 LDAP_LOG ( TRANSPORT, DETAIL1,
746 "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
748 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
752 if( saslrc == SASL_INTERACT ) {
754 if( !interact ) break;
755 res = (interact)( ld, flags, defaults, prompts );
756 if( res != LDAP_SUCCESS ) break;
758 } while ( saslrc == SASL_INTERACT );
760 ccred.bv_len = credlen;
763 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
764 ld->ld_errno = sasl_err2ldap( saslrc );
765 #if SASL_VERSION_MAJOR >= 2
766 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
771 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
773 if ( rc != LDAP_SUCCESS ) goto done;
775 if ( saslrc != SASL_OK ) {
776 #if SASL_VERSION_MAJOR >= 2
777 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
779 rc = ld->ld_errno = sasl_err2ldap( saslrc );
783 if( flags != LDAP_SASL_QUIET ) {
784 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
785 if( saslrc == SASL_OK && data && *data ) {
786 fprintf( stderr, "SASL username: %s\n", data );
789 #if SASL_VERSION_MAJOR < 2
790 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
791 if( saslrc == SASL_OK && data && *data ) {
792 fprintf( stderr, "SASL realm: %s\n", data );
797 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
798 if( saslrc == SASL_OK ) {
799 if( flags != LDAP_SASL_QUIET ) {
800 fprintf( stderr, "SASL SSF: %lu\n",
801 (unsigned long) *ssf );
805 if( flags != LDAP_SASL_QUIET ) {
806 fprintf( stderr, "SASL installing layers\n" );
808 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
809 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
810 sasl_dispose( &oldctx );
811 ldap_pvt_sasl_remove( ld->ld_sb );
813 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
814 ld->ld_defconn->lconn_sasl_sockctx = ctx;
817 ld->ld_defconn->lconn_sasl_authctx = ctx;
824 ldap_int_sasl_external(
832 #if SASL_VERSION_MAJOR < 2
833 sasl_external_properties_t extprops;
836 ctx = conn->lconn_sasl_authctx;
839 return LDAP_LOCAL_ERROR;
842 #if SASL_VERSION_MAJOR >= 2
843 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
845 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
847 memset( &extprops, '\0', sizeof(extprops) );
849 extprops.auth_id = (char *) authid;
851 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
852 (void *) &extprops );
855 if ( sc != SASL_OK ) {
856 return LDAP_LOCAL_ERROR;
863 int ldap_pvt_sasl_secprops(
865 sasl_security_properties_t *secprops )
868 char **props = ldap_str2charray( in, "," );
871 sasl_ssf_t max_ssf = 0;
873 sasl_ssf_t min_ssf = 0;
875 unsigned maxbufsize = 0;
876 int got_maxbufsize = 0;
878 if( props == NULL || secprops == NULL ) {
879 return LDAP_PARAM_ERROR;
882 for( i=0; props[i]; i++ ) {
883 if( !strcasecmp(props[i], "none") ) {
886 } else if( !strcasecmp(props[i], "noplain") ) {
888 sflags |= SASL_SEC_NOPLAINTEXT;
890 } else if( !strcasecmp(props[i], "noactive") ) {
892 sflags |= SASL_SEC_NOACTIVE;
894 } else if( !strcasecmp(props[i], "nodict") ) {
896 sflags |= SASL_SEC_NODICTIONARY;
898 } else if( !strcasecmp(props[i], "forwardsec") ) {
900 sflags |= SASL_SEC_FORWARD_SECRECY;
902 } else if( !strcasecmp(props[i], "noanonymous")) {
904 sflags |= SASL_SEC_NOANONYMOUS;
906 } else if( !strcasecmp(props[i], "passcred") ) {
908 sflags |= SASL_SEC_PASS_CREDENTIALS;
910 } else if( !strncasecmp(props[i],
911 "minssf=", sizeof("minssf")) )
913 if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
915 min_ssf = atoi( &props[i][sizeof("minssf")] );
917 return LDAP_NOT_SUPPORTED;
920 } else if( !strncasecmp(props[i],
921 "maxssf=", sizeof("maxssf")) )
923 if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
925 max_ssf = atoi( &props[i][sizeof("maxssf")] );
927 return LDAP_NOT_SUPPORTED;
930 } else if( !strncasecmp(props[i],
931 "maxbufsize=", sizeof("maxbufsize")) )
933 if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
935 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
937 return LDAP_NOT_SUPPORTED;
940 if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
941 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
944 return LDAP_PARAM_ERROR;
948 return LDAP_NOT_SUPPORTED;
953 secprops->security_flags = sflags;
956 secprops->min_ssf = min_ssf;
959 secprops->max_ssf = max_ssf;
962 secprops->maxbufsize = maxbufsize;
965 ldap_charray_free( props );
970 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
975 case LDAP_OPT_X_SASL_SECPROPS:
976 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
977 if( rc == LDAP_SUCCESS ) return 0;
984 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
990 case LDAP_OPT_X_SASL_MECH: {
991 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
992 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
994 case LDAP_OPT_X_SASL_REALM: {
995 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
996 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
998 case LDAP_OPT_X_SASL_AUTHCID: {
999 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
1000 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
1002 case LDAP_OPT_X_SASL_AUTHZID: {
1003 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
1004 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
1007 case LDAP_OPT_X_SASL_SSF: {
1012 if( ld->ld_defconn == NULL ) {
1016 ctx = ld->ld_defconn->lconn_sasl_sockctx;
1018 if ( ctx == NULL ) {
1022 sc = sasl_getprop( ctx, SASL_SSF,
1023 (SASL_CONST void **) &ssf );
1025 if ( sc != SASL_OK ) {
1029 *(ber_len_t *)arg = *ssf;
1032 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1033 /* this option is write only */
1036 case LDAP_OPT_X_SASL_SSF_MIN:
1037 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1039 case LDAP_OPT_X_SASL_SSF_MAX:
1040 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1042 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1043 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1046 case LDAP_OPT_X_SASL_SECPROPS:
1047 /* this option is write only */
1057 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1063 case LDAP_OPT_X_SASL_SSF:
1064 /* This option is read-only */
1067 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1069 #if SASL_VERSION_MAJOR < 2
1070 sasl_external_properties_t extprops;
1074 if( ld->ld_defconn == NULL ) {
1078 ctx = ld->ld_defconn->lconn_sasl_authctx;
1080 if ( ctx == NULL ) {
1084 #if SASL_VERSION_MAJOR >= 2
1085 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1087 memset(&extprops, 0L, sizeof(extprops));
1089 extprops.ssf = * (ber_len_t *) arg;
1091 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1092 (void *) &extprops );
1095 if ( sc != SASL_OK ) {
1100 case LDAP_OPT_X_SASL_SSF_MIN:
1101 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1103 case LDAP_OPT_X_SASL_SSF_MAX:
1104 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1106 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1107 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1110 case LDAP_OPT_X_SASL_SECPROPS: {
1112 sc = ldap_pvt_sasl_secprops( (char *) arg,
1113 &ld->ld_options.ldo_sasl_secprops );
1115 return sc == LDAP_SUCCESS ? 0 : -1;
1124 #ifdef LDAP_R_COMPILE
1125 #define LDAP_DEBUG_R_SASL
1126 void *ldap_pvt_sasl_mutex_new(void)
1128 ldap_pvt_thread_mutex_t *mutex;
1130 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1131 sizeof(ldap_pvt_thread_mutex_t) );
1133 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1136 #ifndef LDAP_DEBUG_R_SASL
1138 #endif /* !LDAP_DEBUG_R_SASL */
1142 int ldap_pvt_sasl_mutex_lock(void *mutex)
1144 #ifdef LDAP_DEBUG_R_SASL
1145 if ( mutex == NULL ) {
1148 #else /* !LDAP_DEBUG_R_SASL */
1150 #endif /* !LDAP_DEBUG_R_SASL */
1151 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1152 ? SASL_FAIL : SASL_OK;
1155 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1157 #ifdef LDAP_DEBUG_R_SASL
1158 if ( mutex == NULL ) {
1161 #else /* !LDAP_DEBUG_R_SASL */
1163 #endif /* !LDAP_DEBUG_R_SASL */
1164 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1165 ? SASL_FAIL : SASL_OK;
1168 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1170 #ifdef LDAP_DEBUG_R_SASL
1171 if ( mutex == NULL ) {
1174 #else /* !LDAP_DEBUG_R_SASL */
1176 #endif /* !LDAP_DEBUG_R_SASL */
1177 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1183 int ldap_int_sasl_init( void )
1184 { return LDAP_SUCCESS; }
1186 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1187 { return LDAP_SUCCESS; }
1194 LDAPControl **sctrls,
1195 LDAPControl **cctrls,
1197 LDAP_SASL_INTERACT_PROC *interact,
1199 { return LDAP_NOT_SUPPORTED; }
1202 ldap_int_sasl_external(
1205 const char * authid,
1207 { return LDAP_SUCCESS; }
1209 #endif /* HAVE_CYRUS_SASL */