2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
20 #include <ac/socket.h>
21 #include <ac/stdlib.h>
22 #include <ac/string.h>
26 #include <ac/unistd.h>
30 #ifdef HAVE_CYRUS_SASL
33 ldap_pvt_thread_mutex_t ldap_int_sasl_mutex;
36 #ifdef HAVE_SASL_SASL_H
37 #include <sasl/sasl.h>
42 #if SASL_VERSION_MAJOR >= 2
43 #define SASL_CONST const
49 * Various Cyrus SASL related stuff.
52 static const sasl_callback_t client_callbacks[] = {
53 #ifdef SASL_CB_GETREALM
54 { SASL_CB_GETREALM, NULL, NULL },
56 { SASL_CB_USER, NULL, NULL },
57 { SASL_CB_AUTHNAME, NULL, NULL },
58 { SASL_CB_PASS, NULL, NULL },
59 { SASL_CB_ECHOPROMPT, NULL, NULL },
60 { SASL_CB_NOECHOPROMPT, NULL, NULL },
61 { SASL_CB_LIST_END, NULL, NULL }
64 int ldap_int_sasl_init( void )
66 /* XXX not threadsafe */
67 static int sasl_initialized = 0;
69 #ifdef HAVE_SASL_VERSION
70 /* stringify the version number, sasl.h doesn't do it for us */
71 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
72 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
73 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
76 sasl_version( NULL, &rc );
77 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
78 (rc & 0xffff) < SASL_VERSION_STEP) {
79 char version[sizeof("xxx.xxx.xxxxx")];
80 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
84 LDAP_LOG( TRANSPORT, INFO,
85 "ldap_int_sasl_init: SASL library version mismatch:"
86 " expected " SASL_VERSION_STRING ","
87 " got %s\n", version, 0, 0 );
89 Debug( LDAP_DEBUG_ANY,
90 "ldap_int_sasl_init: SASL library version mismatch:"
91 " expected " SASL_VERSION_STRING ","
92 " got %s\n", version, 0, 0 );
98 if ( sasl_initialized ) {
102 /* SASL 2 takes care of its own memory completely internally */
103 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
109 #endif /* CSRIMALLOC */
111 #ifdef LDAP_R_COMPILE
113 ldap_pvt_sasl_mutex_new,
114 ldap_pvt_sasl_mutex_lock,
115 ldap_pvt_sasl_mutex_unlock,
116 ldap_pvt_sasl_mutex_dispose );
118 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
121 if ( sasl_client_init( NULL ) == SASL_OK ) {
122 sasl_initialized = 1;
126 #if SASL_VERSION_MAJOR < 2
127 /* A no-op to make sure we link with Cyrus 1.5 */
128 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
134 * SASL encryption support for LBER Sockbufs
137 struct sb_sasl_data {
138 sasl_conn_t *sasl_context;
139 unsigned *sasl_maxbuf;
140 Sockbuf_Buf sec_buf_in;
146 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
148 struct sb_sasl_data *p;
150 assert( sbiod != NULL );
152 p = LBER_MALLOC( sizeof( *p ) );
155 p->sasl_context = (sasl_conn_t *)arg;
156 ber_pvt_sb_buf_init( &p->sec_buf_in );
157 ber_pvt_sb_buf_init( &p->buf_in );
158 ber_pvt_sb_buf_init( &p->buf_out );
159 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
164 sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
165 (SASL_CONST void **) &p->sasl_maxbuf );
167 sbiod->sbiod_pvt = p;
173 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
175 struct sb_sasl_data *p;
177 assert( sbiod != NULL );
179 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
180 #if SASL_VERSION_MAJOR >= 2
182 * SASLv2 encode/decode buffers are managed by
183 * libsasl2. Ensure they are not freed by liblber.
185 p->buf_in.buf_base = NULL;
186 p->buf_out.buf_base = NULL;
188 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
189 ber_pvt_sb_buf_destroy( &p->buf_in );
190 ber_pvt_sb_buf_destroy( &p->buf_out );
192 sbiod->sbiod_pvt = NULL;
197 sb_sasl_pkt_length( const unsigned char *buf, int debuglevel )
201 assert( buf != NULL );
208 if ( size > SASL_MAX_BUFF_SIZE ) {
209 /* somebody is trying to mess me up. */
210 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
211 "sb_sasl_pkt_length: received illegal packet length "
212 "of %lu bytes\n", (unsigned long)size );
213 size = 16; /* this should lead to an error. */
216 return size + 4; /* include the size !!! */
219 /* Drop a processed packet from the input buffer */
221 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel )
225 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
227 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
228 sec_buf_in->buf_end, len );
231 sec_buf_in->buf_end = sb_sasl_pkt_length(
232 (unsigned char *) sec_buf_in->buf_base, debuglevel);
235 sec_buf_in->buf_end = 0;
237 sec_buf_in->buf_ptr = len;
241 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
243 struct sb_sasl_data *p;
244 ber_slen_t ret, bufptr;
246 assert( sbiod != NULL );
247 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
249 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
251 /* Are there anything left in the buffer? */
252 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
259 #if SASL_VERSION_MAJOR >= 2
260 ber_pvt_sb_buf_init( &p->buf_in );
262 ber_pvt_sb_buf_destroy( &p->buf_in );
265 /* Read the length of the packet */
266 while ( p->sec_buf_in.buf_ptr < 4 ) {
267 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
268 4 - p->sec_buf_in.buf_ptr );
270 if ( ( ret < 0 ) && ( errno == EINTR ) )
274 return bufptr ? bufptr : ret;
276 p->sec_buf_in.buf_ptr += ret;
279 /* The new packet always starts at p->sec_buf_in.buf_base */
280 ret = sb_sasl_pkt_length( (unsigned char *) p->sec_buf_in.buf_base,
281 sbiod->sbiod_sb->sb_debug );
283 /* Grow the packet buffer if neccessary */
284 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
285 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
290 p->sec_buf_in.buf_end = ret;
292 /* Did we read the whole encrypted packet? */
293 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
294 /* No, we have got only a part of it */
295 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
297 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
298 p->sec_buf_in.buf_ptr, ret );
300 if ( ( ret < 0 ) && ( errno == EINTR ) )
304 return bufptr ? bufptr : ret;
306 p->sec_buf_in.buf_ptr += ret;
309 /* Decode the packet */
310 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
311 p->sec_buf_in.buf_end,
312 (SASL_CONST char **)&p->buf_in.buf_base,
313 (unsigned *)&p->buf_in.buf_end );
315 /* Drop the packet from the input buffer */
316 sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
318 if ( ret != SASL_OK ) {
319 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
320 "sb_sasl_read: failed to decode packet: %s\n",
321 sasl_errstring( ret, NULL, NULL ) );
326 p->buf_in.buf_size = p->buf_in.buf_end;
328 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
334 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
336 struct sb_sasl_data *p;
339 assert( sbiod != NULL );
340 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
342 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
344 /* Are there anything left in the buffer? */
345 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
346 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
349 /* Still have something left?? */
350 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
356 /* now encode the next packet. */
357 #if SASL_VERSION_MAJOR >= 2
358 ber_pvt_sb_buf_init( &p->buf_out );
360 ber_pvt_sb_buf_destroy( &p->buf_out );
362 if ( len > *p->sasl_maxbuf - 100 )
363 len = *p->sasl_maxbuf - 100; /* For safety margin */
364 ret = sasl_encode( p->sasl_context, buf, len,
365 (SASL_CONST char **)&p->buf_out.buf_base,
366 (unsigned *)&p->buf_out.buf_size );
367 if ( ret != SASL_OK ) {
368 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
369 "sb_sasl_write: failed to encode packet: %s\n",
370 sasl_errstring( ret, NULL, NULL ) );
373 p->buf_out.buf_end = p->buf_out.buf_size;
375 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
382 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
384 struct sb_sasl_data *p;
386 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
388 if ( opt == LBER_SB_OPT_DATA_READY ) {
389 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
393 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
396 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
397 sb_sasl_setup, /* sbi_setup */
398 sb_sasl_remove, /* sbi_remove */
399 sb_sasl_ctrl, /* sbi_ctrl */
400 sb_sasl_read, /* sbi_read */
401 sb_sasl_write, /* sbi_write */
405 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
408 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_install\n", 0, 0, 0 );
410 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
414 /* don't install the stuff unless security has been negotiated */
416 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
417 &ldap_pvt_sockbuf_io_sasl ) )
420 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
421 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
423 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
424 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
430 void ldap_pvt_sasl_remove( Sockbuf *sb )
432 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
433 LBER_SBIOD_LEVEL_APPLICATION );
435 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
436 LBER_SBIOD_LEVEL_APPLICATION );
441 sasl_err2ldap( int saslerr )
447 rc = LDAP_MORE_RESULTS_TO_RETURN;
450 rc = LDAP_LOCAL_ERROR;
456 rc = LDAP_LOCAL_ERROR;
462 rc = LDAP_AUTH_UNKNOWN;
465 rc = LDAP_AUTH_UNKNOWN;
468 rc = LDAP_PARAM_ERROR;
472 rc = LDAP_AUTH_UNKNOWN;
475 rc = LDAP_LOCAL_ERROR;
479 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
492 assert( lc->lconn_sasl_authctx == NULL );
494 if ( host == NULL ) {
495 ld->ld_errno = LDAP_LOCAL_ERROR;
499 if ( ldap_int_sasl_init() ) {
500 ld->ld_errno = LDAP_LOCAL_ERROR;
504 #if SASL_VERSION_MAJOR >= 2
505 rc = sasl_client_new( "ldap", host, NULL, NULL,
506 client_callbacks, 0, &ctx );
508 rc = sasl_client_new( "ldap", host, client_callbacks,
509 SASL_SECURITY_LAYER, &ctx );
512 if ( rc != SASL_OK ) {
513 ld->ld_errno = sasl_err2ldap( rc );
518 LDAP_LOG ( TRANSPORT, DETAIL1, "ldap_int_sasl_open: host=%s\n",
521 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
525 lc->lconn_sasl_authctx = ctx;
530 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
532 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
535 sasl_dispose( &ctx );
536 if ( lc->lconn_sasl_sockctx &&
537 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
538 ctx = lc->lconn_sasl_sockctx;
539 sasl_dispose( &ctx );
541 lc->lconn_sasl_sockctx = NULL;
542 lc->lconn_sasl_authctx = NULL;
553 LDAPControl **sctrls,
554 LDAPControl **cctrls,
556 LDAP_SASL_INTERACT_PROC *interact,
560 const char *mech = NULL;
561 const char *pmech = NULL;
563 sasl_ssf_t *ssf = NULL;
564 sasl_conn_t *ctx, *oldctx = NULL;
565 sasl_interact_t *prompts = NULL;
572 LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n",
573 mechs ? mechs : "<null>", 0, 0 );
575 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
576 mechs ? mechs : "<null>", 0, 0 );
579 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
580 if (ld->ld_version < LDAP_VERSION3) {
581 ld->ld_errno = LDAP_NOT_SUPPORTED;
585 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
587 if ( sd == AC_SOCKET_INVALID ) {
588 /* not connected yet */
591 rc = ldap_open_defconn( ld );
592 if( rc < 0 ) return ld->ld_errno;
594 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
596 if( sd == AC_SOCKET_INVALID ) {
597 ld->ld_errno = LDAP_LOCAL_ERROR;
602 oldctx = ld->ld_defconn->lconn_sasl_authctx;
604 /* If we already have an authentication context, clear it out */
606 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
607 sasl_dispose( &oldctx );
609 ld->ld_defconn->lconn_sasl_authctx = NULL;
612 { char *saslhost = ldap_host_connected_to( ld->ld_sb, "localhost" );
613 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
614 LDAP_FREE( saslhost );
617 if ( rc != LDAP_SUCCESS ) return rc;
619 ctx = ld->ld_defconn->lconn_sasl_authctx;
622 ssl = ldap_pvt_tls_sb_ctx( ld->ld_sb );
624 struct berval authid = { 0, NULL };
627 fac = ldap_pvt_tls_get_strength( ssl );
628 /* failure is OK, we just can't use SASL EXTERNAL */
629 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
631 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
632 LDAP_FREE( authid.bv_val );
635 /* Check for local */
636 if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
637 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
638 "cn=peercred,cn=external,cn=auth")];
639 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
640 "cn=peercred,cn=external,cn=auth",
641 (int) geteuid(), (int) getegid() );
642 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
645 /* (re)set security properties */
646 sasl_setprop( ctx, SASL_SEC_PROPS,
647 &ld->ld_options.ldo_sasl_secprops );
653 saslrc = sasl_client_start( ctx,
655 #if SASL_VERSION_MAJOR < 2
659 (SASL_CONST char **)&ccred.bv_val,
663 if( pmech == NULL && mech != NULL ) {
666 if( flags != LDAP_SASL_QUIET ) {
668 "SASL/%s authentication started\n",
673 if( saslrc == SASL_INTERACT ) {
675 if( !interact ) break;
676 res = (interact)( ld, flags, defaults, prompts );
678 if( res != LDAP_SUCCESS ) break;
680 } while ( saslrc == SASL_INTERACT );
682 ccred.bv_len = credlen;
684 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
685 rc = ld->ld_errno = sasl_err2ldap( saslrc );
686 #if SASL_VERSION_MAJOR >= 2
687 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
693 struct berval *scred;
698 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
700 if ( ccred.bv_val != NULL ) {
701 #if SASL_VERSION_MAJOR < 2
702 LDAP_FREE( ccred.bv_val );
707 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
708 if( scred && scred->bv_len ) {
709 /* and 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 );
725 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
726 /* we're done, no need to step */
727 if( scred && scred->bv_len ) {
728 /* but server provided us with data! */
730 LDAP_LOG ( TRANSPORT, DETAIL1,
731 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
732 rc, saslrc, scred->bv_len );
734 Debug( LDAP_DEBUG_TRACE,
735 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
736 rc, saslrc, scred->bv_len );
739 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
746 saslrc = sasl_client_step( ctx,
747 (scred == NULL) ? NULL : scred->bv_val,
748 (scred == NULL) ? 0 : scred->bv_len,
750 (SASL_CONST char **)&ccred.bv_val,
754 LDAP_LOG ( TRANSPORT, DETAIL1,
755 "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
757 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
761 if( saslrc == SASL_INTERACT ) {
763 if( !interact ) break;
764 res = (interact)( ld, flags, defaults, prompts );
765 if( res != LDAP_SUCCESS ) break;
767 } while ( saslrc == SASL_INTERACT );
769 ccred.bv_len = credlen;
772 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
773 ld->ld_errno = sasl_err2ldap( saslrc );
774 #if SASL_VERSION_MAJOR >= 2
775 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
780 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
782 if ( rc != LDAP_SUCCESS ) goto done;
784 if ( saslrc != SASL_OK ) {
785 #if SASL_VERSION_MAJOR >= 2
786 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
788 rc = ld->ld_errno = sasl_err2ldap( saslrc );
792 if( flags != LDAP_SASL_QUIET ) {
793 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
794 if( saslrc == SASL_OK && data && *data ) {
795 fprintf( stderr, "SASL username: %s\n", data );
798 #if SASL_VERSION_MAJOR < 2
799 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
800 if( saslrc == SASL_OK && data && *data ) {
801 fprintf( stderr, "SASL realm: %s\n", data );
806 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
807 if( saslrc == SASL_OK ) {
808 if( flags != LDAP_SASL_QUIET ) {
809 fprintf( stderr, "SASL SSF: %lu\n",
810 (unsigned long) *ssf );
814 if( flags != LDAP_SASL_QUIET ) {
815 fprintf( stderr, "SASL installing layers\n" );
817 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
818 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
819 sasl_dispose( &oldctx );
820 ldap_pvt_sasl_remove( ld->ld_sb );
822 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
823 ld->ld_defconn->lconn_sasl_sockctx = ctx;
826 ld->ld_defconn->lconn_sasl_authctx = ctx;
833 ldap_int_sasl_external(
841 #if SASL_VERSION_MAJOR < 2
842 sasl_external_properties_t extprops;
845 ctx = conn->lconn_sasl_authctx;
848 return LDAP_LOCAL_ERROR;
851 #if SASL_VERSION_MAJOR >= 2
852 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
854 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
856 memset( &extprops, '\0', sizeof(extprops) );
858 extprops.auth_id = (char *) authid;
860 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
861 (void *) &extprops );
864 if ( sc != SASL_OK ) {
865 return LDAP_LOCAL_ERROR;
872 int ldap_pvt_sasl_secprops(
874 sasl_security_properties_t *secprops )
877 char **props = ldap_str2charray( in, "," );
880 sasl_ssf_t max_ssf = 0;
882 sasl_ssf_t min_ssf = 0;
884 unsigned maxbufsize = 0;
885 int got_maxbufsize = 0;
887 if( props == NULL || secprops == NULL ) {
888 return LDAP_PARAM_ERROR;
891 for( i=0; props[i]; i++ ) {
892 if( !strcasecmp(props[i], "none") ) {
895 } else if( !strcasecmp(props[i], "noplain") ) {
897 sflags |= SASL_SEC_NOPLAINTEXT;
899 } else if( !strcasecmp(props[i], "noactive") ) {
901 sflags |= SASL_SEC_NOACTIVE;
903 } else if( !strcasecmp(props[i], "nodict") ) {
905 sflags |= SASL_SEC_NODICTIONARY;
907 } else if( !strcasecmp(props[i], "forwardsec") ) {
909 sflags |= SASL_SEC_FORWARD_SECRECY;
911 } else if( !strcasecmp(props[i], "noanonymous")) {
913 sflags |= SASL_SEC_NOANONYMOUS;
915 } else if( !strcasecmp(props[i], "passcred") ) {
917 sflags |= SASL_SEC_PASS_CREDENTIALS;
919 } else if( !strncasecmp(props[i],
920 "minssf=", sizeof("minssf")) )
922 if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
924 min_ssf = atoi( &props[i][sizeof("minssf")] );
926 return LDAP_NOT_SUPPORTED;
929 } else if( !strncasecmp(props[i],
930 "maxssf=", sizeof("maxssf")) )
932 if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
934 max_ssf = atoi( &props[i][sizeof("maxssf")] );
936 return LDAP_NOT_SUPPORTED;
939 } else if( !strncasecmp(props[i],
940 "maxbufsize=", sizeof("maxbufsize")) )
942 if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
944 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
946 return LDAP_NOT_SUPPORTED;
949 if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
950 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
953 return LDAP_PARAM_ERROR;
957 return LDAP_NOT_SUPPORTED;
962 secprops->security_flags = sflags;
965 secprops->min_ssf = min_ssf;
968 secprops->max_ssf = max_ssf;
971 secprops->maxbufsize = maxbufsize;
974 ldap_charray_free( props );
979 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
984 case LDAP_OPT_X_SASL_SECPROPS:
985 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
986 if( rc == LDAP_SUCCESS ) return 0;
993 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
999 case LDAP_OPT_X_SASL_MECH: {
1000 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
1001 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
1003 case LDAP_OPT_X_SASL_REALM: {
1004 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
1005 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
1007 case LDAP_OPT_X_SASL_AUTHCID: {
1008 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
1009 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
1011 case LDAP_OPT_X_SASL_AUTHZID: {
1012 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
1013 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
1016 case LDAP_OPT_X_SASL_SSF: {
1021 if( ld->ld_defconn == NULL ) {
1025 ctx = ld->ld_defconn->lconn_sasl_sockctx;
1027 if ( ctx == NULL ) {
1031 sc = sasl_getprop( ctx, SASL_SSF,
1032 (SASL_CONST void **) &ssf );
1034 if ( sc != SASL_OK ) {
1038 *(ber_len_t *)arg = *ssf;
1041 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1042 /* this option is write only */
1045 case LDAP_OPT_X_SASL_SSF_MIN:
1046 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1048 case LDAP_OPT_X_SASL_SSF_MAX:
1049 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1051 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1052 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1055 case LDAP_OPT_X_SASL_SECPROPS:
1056 /* this option is write only */
1066 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1072 case LDAP_OPT_X_SASL_SSF:
1073 /* This option is read-only */
1076 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1078 #if SASL_VERSION_MAJOR < 2
1079 sasl_external_properties_t extprops;
1083 if( ld->ld_defconn == NULL ) {
1087 ctx = ld->ld_defconn->lconn_sasl_authctx;
1089 if ( ctx == NULL ) {
1093 #if SASL_VERSION_MAJOR >= 2
1094 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1096 memset(&extprops, 0L, sizeof(extprops));
1098 extprops.ssf = * (ber_len_t *) arg;
1100 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1101 (void *) &extprops );
1104 if ( sc != SASL_OK ) {
1109 case LDAP_OPT_X_SASL_SSF_MIN:
1110 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1112 case LDAP_OPT_X_SASL_SSF_MAX:
1113 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1115 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1116 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1119 case LDAP_OPT_X_SASL_SECPROPS: {
1121 sc = ldap_pvt_sasl_secprops( (char *) arg,
1122 &ld->ld_options.ldo_sasl_secprops );
1124 return sc == LDAP_SUCCESS ? 0 : -1;
1133 #ifdef LDAP_R_COMPILE
1134 #define LDAP_DEBUG_R_SASL
1135 void *ldap_pvt_sasl_mutex_new(void)
1137 ldap_pvt_thread_mutex_t *mutex;
1139 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1140 sizeof(ldap_pvt_thread_mutex_t) );
1142 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1145 #ifndef LDAP_DEBUG_R_SASL
1147 #endif /* !LDAP_DEBUG_R_SASL */
1151 int ldap_pvt_sasl_mutex_lock(void *mutex)
1153 #ifdef LDAP_DEBUG_R_SASL
1154 if ( mutex == NULL ) {
1157 #else /* !LDAP_DEBUG_R_SASL */
1159 #endif /* !LDAP_DEBUG_R_SASL */
1160 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1161 ? SASL_FAIL : SASL_OK;
1164 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1166 #ifdef LDAP_DEBUG_R_SASL
1167 if ( mutex == NULL ) {
1170 #else /* !LDAP_DEBUG_R_SASL */
1172 #endif /* !LDAP_DEBUG_R_SASL */
1173 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1174 ? SASL_FAIL : SASL_OK;
1177 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1179 #ifdef LDAP_DEBUG_R_SASL
1180 if ( mutex == NULL ) {
1183 #else /* !LDAP_DEBUG_R_SASL */
1185 #endif /* !LDAP_DEBUG_R_SASL */
1186 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1192 int ldap_int_sasl_init( void )
1193 { return LDAP_SUCCESS; }
1195 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1196 { return LDAP_SUCCESS; }
1203 LDAPControl **sctrls,
1204 LDAPControl **cctrls,
1206 LDAP_SASL_INTERACT_PROC *interact,
1208 { return LDAP_NOT_SUPPORTED; }
1211 ldap_int_sasl_external(
1214 const char * authid,
1216 { return LDAP_SUCCESS; }
1218 #endif /* HAVE_CYRUS_SASL */