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>
17 #include <ac/unistd.h>
21 #ifdef HAVE_CYRUS_SASL
24 ldap_pvt_thread_mutex_t ldap_int_sasl_mutex;
27 #ifdef HAVE_SASL_SASL_H
28 #include <sasl/sasl.h>
33 #if SASL_VERSION_MAJOR >= 2
34 #define SASL_CONST const
40 * Various Cyrus SASL related stuff.
43 int ldap_int_sasl_init( void )
45 /* XXX not threadsafe */
46 static int sasl_initialized = 0;
48 static sasl_callback_t client_callbacks[] = {
49 #ifdef SASL_CB_GETREALM
50 { SASL_CB_GETREALM, NULL, NULL },
52 { SASL_CB_USER, NULL, NULL },
53 { SASL_CB_AUTHNAME, NULL, NULL },
54 { SASL_CB_PASS, NULL, NULL },
55 { SASL_CB_ECHOPROMPT, NULL, NULL },
56 { SASL_CB_NOECHOPROMPT, NULL, NULL },
57 { SASL_CB_LIST_END, NULL, NULL }
60 #ifdef HAVE_SASL_VERSION
61 /* stringify the version number, sasl.h doesn't do it for us */
62 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
63 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
64 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
67 sasl_version( NULL, &rc );
68 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
69 (rc & 0xffff) < SASL_VERSION_STEP) {
70 char version[sizeof("xxx.xxx.xxxxx")];
71 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
75 LDAP_LOG( TRANSPORT, INFO,
76 "ldap_int_sasl_init: SASL library version mismatch:"
77 " expected " SASL_VERSION_STRING ","
78 " got %s\n", version, 0, 0 );
80 Debug( LDAP_DEBUG_ANY,
81 "ldap_int_sasl_init: SASL library version mismatch:"
82 " expected " SASL_VERSION_STRING ","
83 " got %s\n", version, 0, 0 );
89 if ( sasl_initialized ) {
93 /* SASL 2 takes care of its own memory completely internally */
94 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
100 #endif /* CSRIMALLOC */
102 #ifdef LDAP_R_COMPILE
104 ldap_pvt_sasl_mutex_new,
105 ldap_pvt_sasl_mutex_lock,
106 ldap_pvt_sasl_mutex_unlock,
107 ldap_pvt_sasl_mutex_dispose );
109 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
112 if ( sasl_client_init( client_callbacks ) == SASL_OK ) {
113 sasl_initialized = 1;
117 #if SASL_VERSION_MAJOR < 2
118 /* A no-op to make sure we link with Cyrus 1.5 */
119 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
125 * SASL encryption support for LBER Sockbufs
128 struct sb_sasl_data {
129 sasl_conn_t *sasl_context;
130 unsigned *sasl_maxbuf;
131 Sockbuf_Buf sec_buf_in;
137 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
139 struct sb_sasl_data *p;
141 assert( sbiod != NULL );
143 p = LBER_MALLOC( sizeof( *p ) );
146 p->sasl_context = (sasl_conn_t *)arg;
147 ber_pvt_sb_buf_init( &p->sec_buf_in );
148 ber_pvt_sb_buf_init( &p->buf_in );
149 ber_pvt_sb_buf_init( &p->buf_out );
150 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
155 sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
156 (SASL_CONST void **) &p->sasl_maxbuf );
158 sbiod->sbiod_pvt = p;
164 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
166 struct sb_sasl_data *p;
168 assert( sbiod != NULL );
170 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
171 #if SASL_VERSION_MAJOR >= 2
173 * SASLv2 encode/decode buffers are managed by
174 * libsasl2. Ensure they are not freed by liblber.
176 p->buf_in.buf_base = NULL;
177 p->buf_out.buf_base = NULL;
179 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
180 ber_pvt_sb_buf_destroy( &p->buf_in );
181 ber_pvt_sb_buf_destroy( &p->buf_out );
183 sbiod->sbiod_pvt = NULL;
188 sb_sasl_pkt_length( const unsigned char *buf, unsigned max, int debuglevel )
192 assert( buf != NULL );
199 if ( size > SASL_MAX_BUFF_SIZE ) {
200 /* somebody is trying to mess me up. */
201 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
202 "sb_sasl_pkt_length: received illegal packet length "
203 "of %lu bytes\n", (unsigned long)size );
204 size = 16; /* this should lead to an error. */
207 return size + 4; /* include the size !!! */
210 /* Drop a processed packet from the input buffer */
212 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, unsigned max, int debuglevel )
216 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
218 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
219 sec_buf_in->buf_end, len );
222 sec_buf_in->buf_end = sb_sasl_pkt_length(
223 (unsigned char *) sec_buf_in->buf_base, max, debuglevel);
226 sec_buf_in->buf_end = 0;
228 sec_buf_in->buf_ptr = len;
232 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
234 struct sb_sasl_data *p;
235 ber_slen_t ret, bufptr;
237 assert( sbiod != NULL );
238 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
240 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
242 /* Are there anything left in the buffer? */
243 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
250 #if SASL_VERSION_MAJOR >= 2
251 ber_pvt_sb_buf_init( &p->buf_in );
253 ber_pvt_sb_buf_destroy( &p->buf_in );
256 /* Read the length of the packet */
257 while ( p->sec_buf_in.buf_ptr < 4 ) {
258 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
259 4 - p->sec_buf_in.buf_ptr );
261 if ( ( ret < 0 ) && ( errno == EINTR ) )
265 return bufptr ? bufptr : ret;
267 p->sec_buf_in.buf_ptr += ret;
270 /* The new packet always starts at p->sec_buf_in.buf_base */
271 ret = sb_sasl_pkt_length( (unsigned char *) p->sec_buf_in.buf_base,
272 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
274 /* Grow the packet buffer if neccessary */
275 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
276 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
281 p->sec_buf_in.buf_end = ret;
283 /* Did we read the whole encrypted packet? */
284 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
285 /* No, we have got only a part of it */
286 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
288 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
289 p->sec_buf_in.buf_ptr, ret );
291 if ( ( ret < 0 ) && ( errno == EINTR ) )
295 return bufptr ? bufptr : ret;
297 p->sec_buf_in.buf_ptr += ret;
300 /* Decode the packet */
301 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
302 p->sec_buf_in.buf_end,
303 (SASL_CONST char **)&p->buf_in.buf_base,
304 (unsigned *)&p->buf_in.buf_end );
306 /* Drop the packet from the input buffer */
307 sb_sasl_drop_packet( &p->sec_buf_in,
308 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
310 if ( ret != SASL_OK ) {
311 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
312 "sb_sasl_read: failed to decode packet: %s\n",
313 sasl_errstring( ret, NULL, NULL ) );
318 p->buf_in.buf_size = p->buf_in.buf_end;
320 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
326 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
328 struct sb_sasl_data *p;
331 assert( sbiod != NULL );
332 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
334 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
336 /* Are there anything left in the buffer? */
337 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
338 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
341 /* Still have something left?? */
342 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
348 /* now encode the next packet. */
349 #if SASL_VERSION_MAJOR >= 2
350 ber_pvt_sb_buf_init( &p->buf_out );
351 /* sasl v2 makes sure this number is correct */
352 if ( len > *p->sasl_maxbuf )
353 len = *p->sasl_maxbuf;
355 ber_pvt_sb_buf_destroy( &p->buf_out );
356 if ( len > *p->sasl_maxbuf - 100 )
357 len = *p->sasl_maxbuf - 100; /* For safety margin */
359 ret = sasl_encode( p->sasl_context, buf, len,
360 (SASL_CONST char **)&p->buf_out.buf_base,
361 (unsigned *)&p->buf_out.buf_size );
362 if ( ret != SASL_OK ) {
363 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
364 "sb_sasl_write: failed to encode packet: %s\n",
365 sasl_errstring( ret, NULL, NULL ) );
368 p->buf_out.buf_end = p->buf_out.buf_size;
370 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
377 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
379 struct sb_sasl_data *p;
381 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
383 if ( opt == LBER_SB_OPT_DATA_READY ) {
384 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
388 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
391 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
392 sb_sasl_setup, /* sbi_setup */
393 sb_sasl_remove, /* sbi_remove */
394 sb_sasl_ctrl, /* sbi_ctrl */
395 sb_sasl_read, /* sbi_read */
396 sb_sasl_write, /* sbi_write */
400 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
403 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_install\n", 0, 0, 0 );
405 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
409 /* don't install the stuff unless security has been negotiated */
411 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
412 &ldap_pvt_sockbuf_io_sasl ) )
415 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
416 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
418 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
419 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
425 void ldap_pvt_sasl_remove( Sockbuf *sb )
427 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
428 LBER_SBIOD_LEVEL_APPLICATION );
430 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
431 LBER_SBIOD_LEVEL_APPLICATION );
436 sasl_err2ldap( int saslerr )
442 rc = LDAP_MORE_RESULTS_TO_RETURN;
445 rc = LDAP_LOCAL_ERROR;
451 rc = LDAP_LOCAL_ERROR;
457 rc = LDAP_AUTH_UNKNOWN;
460 rc = LDAP_AUTH_UNKNOWN;
463 rc = LDAP_PARAM_ERROR;
467 rc = LDAP_AUTH_UNKNOWN;
470 rc = LDAP_LOCAL_ERROR;
474 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
487 assert( lc->lconn_sasl_authctx == NULL );
489 if ( host == NULL ) {
490 ld->ld_errno = LDAP_LOCAL_ERROR;
494 #if SASL_VERSION_MAJOR >= 2
495 rc = sasl_client_new( "ldap", host, NULL, NULL,
498 rc = sasl_client_new( "ldap", host, NULL,
499 SASL_SECURITY_LAYER, &ctx );
502 if ( rc != SASL_OK ) {
503 ld->ld_errno = sasl_err2ldap( rc );
508 LDAP_LOG ( TRANSPORT, DETAIL1, "ldap_int_sasl_open: host=%s\n",
511 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
515 lc->lconn_sasl_authctx = ctx;
520 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
522 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
525 sasl_dispose( &ctx );
526 if ( lc->lconn_sasl_sockctx &&
527 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
528 ctx = lc->lconn_sasl_sockctx;
529 sasl_dispose( &ctx );
531 lc->lconn_sasl_sockctx = NULL;
532 lc->lconn_sasl_authctx = NULL;
543 LDAPControl **sctrls,
544 LDAPControl **cctrls,
546 LDAP_SASL_INTERACT_PROC *interact,
550 const char *mech = NULL;
551 const char *pmech = NULL;
553 sasl_ssf_t *ssf = NULL;
554 sasl_conn_t *ctx, *oldctx = NULL;
555 sasl_interact_t *prompts = NULL;
562 LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n",
563 mechs ? mechs : "<null>", 0, 0 );
565 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
566 mechs ? mechs : "<null>", 0, 0 );
569 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
570 if (ld->ld_version < LDAP_VERSION3) {
571 ld->ld_errno = LDAP_NOT_SUPPORTED;
575 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
577 if ( sd == AC_SOCKET_INVALID ) {
578 /* not connected yet */
581 rc = ldap_open_defconn( ld );
582 if( rc < 0 ) return ld->ld_errno;
584 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
586 if( sd == AC_SOCKET_INVALID ) {
587 ld->ld_errno = LDAP_LOCAL_ERROR;
592 oldctx = ld->ld_defconn->lconn_sasl_authctx;
594 /* If we already have an authentication context, clear it out */
596 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
597 sasl_dispose( &oldctx );
599 ld->ld_defconn->lconn_sasl_authctx = NULL;
602 rc = ldap_int_sasl_open( ld, ld->ld_defconn,
603 ld->ld_defconn->lconn_server->lud_host ?
604 ld->ld_defconn->lconn_server->lud_host : "localhost" );
606 if ( rc != LDAP_SUCCESS ) return rc;
608 ctx = ld->ld_defconn->lconn_sasl_authctx;
611 ssl = ldap_pvt_tls_sb_ctx( ld->ld_sb );
613 struct berval authid = { 0, NULL };
616 fac = ldap_pvt_tls_get_strength( ssl );
617 /* failure is OK, we just can't use SASL EXTERNAL */
618 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
620 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
621 LDAP_FREE( authid.bv_val );
624 /* Check for local */
625 if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
626 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
627 "cn=peercred,cn=external,cn=auth")];
628 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
629 "cn=peercred,cn=external,cn=auth",
630 (int) geteuid(), (int) getegid() );
631 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
634 /* (re)set security properties */
635 sasl_setprop( ctx, SASL_SEC_PROPS,
636 &ld->ld_options.ldo_sasl_secprops );
642 saslrc = sasl_client_start( ctx,
644 #if SASL_VERSION_MAJOR < 2
648 (SASL_CONST char **)&ccred.bv_val,
652 if( pmech == NULL && mech != NULL ) {
655 if( flags != LDAP_SASL_QUIET ) {
657 "SASL/%s authentication started\n",
662 if( saslrc == SASL_INTERACT ) {
664 if( !interact ) break;
665 res = (interact)( ld, flags, defaults, prompts );
667 if( res != LDAP_SUCCESS ) break;
669 } while ( saslrc == SASL_INTERACT );
671 ccred.bv_len = credlen;
673 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
674 rc = ld->ld_errno = sasl_err2ldap( saslrc );
675 #if SASL_VERSION_MAJOR >= 2
676 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
682 struct berval *scred;
687 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
689 if ( ccred.bv_val != NULL ) {
690 #if SASL_VERSION_MAJOR < 2
691 LDAP_FREE( ccred.bv_val );
696 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
697 if( scred && scred->bv_len ) {
698 /* and server provided us with data? */
700 LDAP_LOG ( TRANSPORT, DETAIL1,
701 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
702 rc, saslrc, scred->bv_len );
704 Debug( LDAP_DEBUG_TRACE,
705 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
706 rc, saslrc, scred->bv_len );
714 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
715 /* we're done, no need to step */
716 if( scred && scred->bv_len ) {
717 /* but server provided us with data! */
719 LDAP_LOG ( TRANSPORT, DETAIL1,
720 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
721 rc, saslrc, scred->bv_len );
723 Debug( LDAP_DEBUG_TRACE,
724 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
725 rc, saslrc, scred->bv_len );
728 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
735 saslrc = sasl_client_step( ctx,
736 (scred == NULL) ? NULL : scred->bv_val,
737 (scred == NULL) ? 0 : scred->bv_len,
739 (SASL_CONST char **)&ccred.bv_val,
743 LDAP_LOG ( TRANSPORT, DETAIL1,
744 "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
746 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
750 if( saslrc == SASL_INTERACT ) {
752 if( !interact ) break;
753 res = (interact)( ld, flags, defaults, prompts );
754 if( res != LDAP_SUCCESS ) break;
756 } while ( saslrc == SASL_INTERACT );
758 ccred.bv_len = credlen;
761 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
762 ld->ld_errno = sasl_err2ldap( saslrc );
763 #if SASL_VERSION_MAJOR >= 2
764 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
769 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
771 if ( rc != LDAP_SUCCESS ) goto done;
773 if ( saslrc != SASL_OK ) {
774 #if SASL_VERSION_MAJOR >= 2
775 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
777 rc = ld->ld_errno = sasl_err2ldap( saslrc );
781 if( flags != LDAP_SASL_QUIET ) {
782 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
783 if( saslrc == SASL_OK && data && *data ) {
784 fprintf( stderr, "SASL username: %s\n", data );
787 #if SASL_VERSION_MAJOR < 2
788 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
789 if( saslrc == SASL_OK && data && *data ) {
790 fprintf( stderr, "SASL realm: %s\n", data );
795 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
796 if( saslrc == SASL_OK ) {
797 if( flags != LDAP_SASL_QUIET ) {
798 fprintf( stderr, "SASL SSF: %lu\n",
799 (unsigned long) *ssf );
803 if( flags != LDAP_SASL_QUIET ) {
804 fprintf( stderr, "SASL installing layers\n" );
806 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
807 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
808 sasl_dispose( &oldctx );
809 ldap_pvt_sasl_remove( ld->ld_sb );
811 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
812 ld->ld_defconn->lconn_sasl_sockctx = ctx;
815 ld->ld_defconn->lconn_sasl_authctx = ctx;
822 ldap_int_sasl_external(
830 #if SASL_VERSION_MAJOR < 2
831 sasl_external_properties_t extprops;
834 ctx = conn->lconn_sasl_authctx;
837 return LDAP_LOCAL_ERROR;
840 #if SASL_VERSION_MAJOR >= 2
841 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
843 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
845 memset( &extprops, '\0', sizeof(extprops) );
847 extprops.auth_id = (char *) authid;
849 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
850 (void *) &extprops );
853 if ( sc != SASL_OK ) {
854 return LDAP_LOCAL_ERROR;
861 int ldap_pvt_sasl_secprops(
863 sasl_security_properties_t *secprops )
866 char **props = ldap_str2charray( in, "," );
869 sasl_ssf_t max_ssf = 0;
871 sasl_ssf_t min_ssf = 0;
873 unsigned maxbufsize = 0;
874 int got_maxbufsize = 0;
876 if( props == NULL || secprops == NULL ) {
877 return LDAP_PARAM_ERROR;
880 for( i=0; props[i]; i++ ) {
881 if( !strcasecmp(props[i], "none") ) {
884 } else if( !strcasecmp(props[i], "noplain") ) {
886 sflags |= SASL_SEC_NOPLAINTEXT;
888 } else if( !strcasecmp(props[i], "noactive") ) {
890 sflags |= SASL_SEC_NOACTIVE;
892 } else if( !strcasecmp(props[i], "nodict") ) {
894 sflags |= SASL_SEC_NODICTIONARY;
896 } else if( !strcasecmp(props[i], "forwardsec") ) {
898 sflags |= SASL_SEC_FORWARD_SECRECY;
900 } else if( !strcasecmp(props[i], "noanonymous")) {
902 sflags |= SASL_SEC_NOANONYMOUS;
904 } else if( !strcasecmp(props[i], "passcred") ) {
906 sflags |= SASL_SEC_PASS_CREDENTIALS;
908 } else if( !strncasecmp(props[i],
909 "minssf=", sizeof("minssf")) )
911 if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
913 min_ssf = atoi( &props[i][sizeof("minssf")] );
915 return LDAP_NOT_SUPPORTED;
918 } else if( !strncasecmp(props[i],
919 "maxssf=", sizeof("maxssf")) )
921 if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
923 max_ssf = atoi( &props[i][sizeof("maxssf")] );
925 return LDAP_NOT_SUPPORTED;
928 } else if( !strncasecmp(props[i],
929 "maxbufsize=", sizeof("maxbufsize")) )
931 if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
933 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
935 return LDAP_NOT_SUPPORTED;
938 if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
939 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
942 return LDAP_PARAM_ERROR;
946 return LDAP_NOT_SUPPORTED;
951 secprops->security_flags = sflags;
954 secprops->min_ssf = min_ssf;
957 secprops->max_ssf = max_ssf;
960 secprops->maxbufsize = maxbufsize;
963 ldap_charray_free( props );
968 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
973 case LDAP_OPT_X_SASL_SECPROPS:
974 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
975 if( rc == LDAP_SUCCESS ) return 0;
982 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
988 case LDAP_OPT_X_SASL_MECH: {
989 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
990 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
992 case LDAP_OPT_X_SASL_REALM: {
993 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
994 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
996 case LDAP_OPT_X_SASL_AUTHCID: {
997 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
998 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
1000 case LDAP_OPT_X_SASL_AUTHZID: {
1001 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
1002 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
1005 case LDAP_OPT_X_SASL_SSF: {
1010 if( ld->ld_defconn == NULL ) {
1014 ctx = ld->ld_defconn->lconn_sasl_sockctx;
1016 if ( ctx == NULL ) {
1020 sc = sasl_getprop( ctx, SASL_SSF,
1021 (SASL_CONST void **) &ssf );
1023 if ( sc != SASL_OK ) {
1027 *(ber_len_t *)arg = *ssf;
1030 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1031 /* this option is write only */
1034 case LDAP_OPT_X_SASL_SSF_MIN:
1035 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1037 case LDAP_OPT_X_SASL_SSF_MAX:
1038 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1040 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1041 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1044 case LDAP_OPT_X_SASL_SECPROPS:
1045 /* this option is write only */
1055 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1061 case LDAP_OPT_X_SASL_SSF:
1062 /* This option is read-only */
1065 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1067 #if SASL_VERSION_MAJOR < 2
1068 sasl_external_properties_t extprops;
1072 if( ld->ld_defconn == NULL ) {
1076 ctx = ld->ld_defconn->lconn_sasl_authctx;
1078 if ( ctx == NULL ) {
1082 #if SASL_VERSION_MAJOR >= 2
1083 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1085 memset(&extprops, 0L, sizeof(extprops));
1087 extprops.ssf = * (ber_len_t *) arg;
1089 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1090 (void *) &extprops );
1093 if ( sc != SASL_OK ) {
1098 case LDAP_OPT_X_SASL_SSF_MIN:
1099 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1101 case LDAP_OPT_X_SASL_SSF_MAX:
1102 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1104 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1105 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1108 case LDAP_OPT_X_SASL_SECPROPS: {
1110 sc = ldap_pvt_sasl_secprops( (char *) arg,
1111 &ld->ld_options.ldo_sasl_secprops );
1113 return sc == LDAP_SUCCESS ? 0 : -1;
1122 #ifdef LDAP_R_COMPILE
1123 #define LDAP_DEBUG_R_SASL
1124 void *ldap_pvt_sasl_mutex_new(void)
1126 ldap_pvt_thread_mutex_t *mutex;
1128 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1129 sizeof(ldap_pvt_thread_mutex_t) );
1131 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1134 #ifndef LDAP_DEBUG_R_SASL
1136 #endif /* !LDAP_DEBUG_R_SASL */
1140 int ldap_pvt_sasl_mutex_lock(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 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1150 ? SASL_FAIL : SASL_OK;
1153 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1155 #ifdef LDAP_DEBUG_R_SASL
1156 if ( mutex == NULL ) {
1159 #else /* !LDAP_DEBUG_R_SASL */
1161 #endif /* !LDAP_DEBUG_R_SASL */
1162 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1163 ? SASL_FAIL : SASL_OK;
1166 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1168 #ifdef LDAP_DEBUG_R_SASL
1169 if ( mutex == NULL ) {
1172 #else /* !LDAP_DEBUG_R_SASL */
1174 #endif /* !LDAP_DEBUG_R_SASL */
1175 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1181 int ldap_int_sasl_init( void )
1182 { return LDAP_SUCCESS; }
1184 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1185 { return LDAP_SUCCESS; }
1192 LDAPControl **sctrls,
1193 LDAPControl **cctrls,
1195 LDAP_SASL_INTERACT_PROC *interact,
1197 { return LDAP_NOT_SUPPORTED; }
1200 ldap_int_sasl_external(
1203 const char * authid,
1205 { return LDAP_SUCCESS; }
1207 #endif /* HAVE_CYRUS_SASL */