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 );
119 if ( sasl_client_init( NULL ) == SASL_OK ) {
120 sasl_initialized = 1;
124 #if SASL_VERSION_MAJOR < 2
125 /* A no-op to make sure we link with Cyrus 1.5 */
126 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
132 * SASL encryption support for LBER Sockbufs
135 struct sb_sasl_data {
136 sasl_conn_t *sasl_context;
137 unsigned *sasl_maxbuf;
138 Sockbuf_Buf sec_buf_in;
144 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
146 struct sb_sasl_data *p;
148 assert( sbiod != NULL );
150 p = LBER_MALLOC( sizeof( *p ) );
153 p->sasl_context = (sasl_conn_t *)arg;
154 ber_pvt_sb_buf_init( &p->sec_buf_in );
155 ber_pvt_sb_buf_init( &p->buf_in );
156 ber_pvt_sb_buf_init( &p->buf_out );
157 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
162 sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
163 (SASL_CONST void **) &p->sasl_maxbuf );
165 sbiod->sbiod_pvt = p;
171 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
173 struct sb_sasl_data *p;
175 assert( sbiod != NULL );
177 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
178 #if SASL_VERSION_MAJOR >= 2
180 * SASLv2 encode/decode buffers are managed by
181 * libsasl2. Ensure they are not freed by liblber.
183 p->buf_in.buf_base = NULL;
184 p->buf_out.buf_base = NULL;
186 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
187 ber_pvt_sb_buf_destroy( &p->buf_in );
188 ber_pvt_sb_buf_destroy( &p->buf_out );
190 sbiod->sbiod_pvt = NULL;
195 sb_sasl_pkt_length( const unsigned char *buf, int debuglevel )
199 assert( buf != NULL );
206 if ( size > SASL_MAX_BUFF_SIZE ) {
207 /* somebody is trying to mess me up. */
208 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
209 "sb_sasl_pkt_length: received illegal packet length "
210 "of %lu bytes\n", (unsigned long)size );
211 size = 16; /* this should lead to an error. */
214 return size + 4; /* include the size !!! */
217 /* Drop a processed packet from the input buffer */
219 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel )
223 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
225 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
226 sec_buf_in->buf_end, len );
229 sec_buf_in->buf_end = sb_sasl_pkt_length(
230 (unsigned char *) sec_buf_in->buf_base, debuglevel);
233 sec_buf_in->buf_end = 0;
235 sec_buf_in->buf_ptr = len;
239 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
241 struct sb_sasl_data *p;
242 ber_slen_t ret, bufptr;
244 assert( sbiod != NULL );
245 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
247 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
249 /* Are there anything left in the buffer? */
250 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
257 #if SASL_VERSION_MAJOR >= 2
258 ber_pvt_sb_buf_init( &p->buf_in );
260 ber_pvt_sb_buf_destroy( &p->buf_in );
263 /* Read the length of the packet */
264 while ( p->sec_buf_in.buf_ptr < 4 ) {
265 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
266 p->sec_buf_in.buf_ptr,
267 4 - p->sec_buf_in.buf_ptr );
269 if ( ( ret < 0 ) && ( errno == EINTR ) )
273 return bufptr ? bufptr : ret;
275 p->sec_buf_in.buf_ptr += ret;
278 /* The new packet always starts at p->sec_buf_in.buf_base */
279 ret = sb_sasl_pkt_length( (unsigned char *) p->sec_buf_in.buf_base,
280 sbiod->sbiod_sb->sb_debug );
282 /* Grow the packet buffer if neccessary */
283 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
284 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
289 p->sec_buf_in.buf_end = ret;
291 /* Did we read the whole encrypted packet? */
292 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
293 /* No, we have got only a part of it */
294 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
296 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
297 p->sec_buf_in.buf_ptr, ret );
299 if ( ( ret < 0 ) && ( errno == EINTR ) )
303 return bufptr ? bufptr : ret;
305 p->sec_buf_in.buf_ptr += ret;
308 /* Decode the packet */
310 unsigned tmpsize = p->buf_in.buf_end;
311 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
312 p->sec_buf_in.buf_end,
313 (SASL_CONST char **)&p->buf_in.buf_base,
314 (unsigned *)&tmpsize );
315 p->buf_in.buf_end = tmpsize;
318 /* Drop the packet from the input buffer */
319 sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
321 if ( ret != SASL_OK ) {
322 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
323 "sb_sasl_read: failed to decode packet: %s\n",
324 sasl_errstring( ret, NULL, NULL ) );
329 p->buf_in.buf_size = p->buf_in.buf_end;
331 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
337 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
339 struct sb_sasl_data *p;
342 assert( sbiod != NULL );
343 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
345 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
347 /* Are there anything left in the buffer? */
348 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
349 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
352 /* Still have something left?? */
353 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
359 /* now encode the next packet. */
360 #if SASL_VERSION_MAJOR >= 2
361 ber_pvt_sb_buf_init( &p->buf_out );
363 ber_pvt_sb_buf_destroy( &p->buf_out );
365 if ( len > *p->sasl_maxbuf - 100 ) {
366 len = *p->sasl_maxbuf - 100; /* For safety margin */
370 unsigned tmpsize = p->buf_out.buf_size;
371 ret = sasl_encode( p->sasl_context, buf, len,
372 (SASL_CONST char **)&p->buf_out.buf_base,
374 p->buf_out.buf_size = tmpsize;
377 if ( ret != SASL_OK ) {
378 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
379 "sb_sasl_write: failed to encode packet: %s\n",
380 sasl_errstring( ret, NULL, NULL ) );
383 p->buf_out.buf_end = p->buf_out.buf_size;
385 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
393 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
395 struct sb_sasl_data *p;
397 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
399 if ( opt == LBER_SB_OPT_DATA_READY ) {
400 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
404 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
407 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
408 sb_sasl_setup, /* sbi_setup */
409 sb_sasl_remove, /* sbi_remove */
410 sb_sasl_ctrl, /* sbi_ctrl */
411 sb_sasl_read, /* sbi_read */
412 sb_sasl_write, /* sbi_write */
416 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
419 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_install\n", 0, 0, 0 );
421 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
425 /* don't install the stuff unless security has been negotiated */
427 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
428 &ldap_pvt_sockbuf_io_sasl ) )
431 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
432 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
434 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
435 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
441 void ldap_pvt_sasl_remove( Sockbuf *sb )
443 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
444 LBER_SBIOD_LEVEL_APPLICATION );
446 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
447 LBER_SBIOD_LEVEL_APPLICATION );
452 sasl_err2ldap( int saslerr )
458 rc = LDAP_MORE_RESULTS_TO_RETURN;
461 rc = LDAP_LOCAL_ERROR;
467 rc = LDAP_LOCAL_ERROR;
473 rc = LDAP_AUTH_UNKNOWN;
476 rc = LDAP_AUTH_UNKNOWN;
479 rc = LDAP_PARAM_ERROR;
483 rc = LDAP_AUTH_UNKNOWN;
486 rc = LDAP_LOCAL_ERROR;
490 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
503 assert( lc->lconn_sasl_authctx == NULL );
505 if ( host == NULL ) {
506 ld->ld_errno = LDAP_LOCAL_ERROR;
510 if ( ldap_int_sasl_init() ) {
511 ld->ld_errno = LDAP_LOCAL_ERROR;
515 #if SASL_VERSION_MAJOR >= 2
516 rc = sasl_client_new( "ldap", host, NULL, NULL,
517 client_callbacks, 0, &ctx );
519 rc = sasl_client_new( "ldap", host, client_callbacks,
520 SASL_SECURITY_LAYER, &ctx );
523 if ( rc != SASL_OK ) {
524 ld->ld_errno = sasl_err2ldap( rc );
529 LDAP_LOG ( TRANSPORT, DETAIL1, "ldap_int_sasl_open: host=%s\n",
532 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
536 lc->lconn_sasl_authctx = ctx;
541 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
543 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
546 sasl_dispose( &ctx );
547 if ( lc->lconn_sasl_sockctx &&
548 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
549 ctx = lc->lconn_sasl_sockctx;
550 sasl_dispose( &ctx );
552 lc->lconn_sasl_sockctx = NULL;
553 lc->lconn_sasl_authctx = NULL;
564 LDAPControl **sctrls,
565 LDAPControl **cctrls,
567 LDAP_SASL_INTERACT_PROC *interact,
571 const char *mech = NULL;
572 const char *pmech = NULL;
574 sasl_ssf_t *ssf = NULL;
575 sasl_conn_t *ctx, *oldctx = NULL;
576 sasl_interact_t *prompts = NULL;
583 LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n",
584 mechs ? mechs : "<null>", 0, 0 );
586 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
587 mechs ? mechs : "<null>", 0, 0 );
590 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
591 if (ld->ld_version < LDAP_VERSION3) {
592 ld->ld_errno = LDAP_NOT_SUPPORTED;
596 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
598 if ( sd == AC_SOCKET_INVALID ) {
599 /* not connected yet */
602 rc = ldap_open_defconn( ld );
603 if( rc < 0 ) return ld->ld_errno;
605 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
607 if( sd == AC_SOCKET_INVALID ) {
608 ld->ld_errno = LDAP_LOCAL_ERROR;
613 oldctx = ld->ld_defconn->lconn_sasl_authctx;
615 /* If we already have an authentication context, clear it out */
617 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
618 sasl_dispose( &oldctx );
620 ld->ld_defconn->lconn_sasl_authctx = NULL;
623 { char *saslhost = ldap_host_connected_to( ld->ld_sb, "localhost" );
624 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
625 LDAP_FREE( saslhost );
628 if ( rc != LDAP_SUCCESS ) return rc;
630 ctx = ld->ld_defconn->lconn_sasl_authctx;
633 ssl = ldap_pvt_tls_sb_ctx( ld->ld_sb );
635 struct berval authid = BER_BVNULL;
638 fac = ldap_pvt_tls_get_strength( ssl );
639 /* failure is OK, we just can't use SASL EXTERNAL */
640 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
642 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
643 LDAP_FREE( authid.bv_val );
647 /* Check for local */
648 if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
649 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
650 "cn=peercred,cn=external,cn=auth")];
651 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
652 "cn=peercred,cn=external,cn=auth",
653 (int) geteuid(), (int) getegid() );
654 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
658 /* (re)set security properties */
659 sasl_setprop( ctx, SASL_SEC_PROPS,
660 &ld->ld_options.ldo_sasl_secprops );
666 saslrc = sasl_client_start( ctx,
668 #if SASL_VERSION_MAJOR < 2
672 (SASL_CONST char **)&ccred.bv_val,
676 if( pmech == NULL && mech != NULL ) {
679 if( flags != LDAP_SASL_QUIET ) {
681 "SASL/%s authentication started\n",
686 if( saslrc == SASL_INTERACT ) {
688 if( !interact ) break;
689 res = (interact)( ld, flags, defaults, prompts );
691 if( res != LDAP_SUCCESS ) break;
693 } while ( saslrc == SASL_INTERACT );
695 ccred.bv_len = credlen;
697 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
698 rc = ld->ld_errno = sasl_err2ldap( saslrc );
699 #if SASL_VERSION_MAJOR >= 2
700 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
706 struct berval *scred;
711 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
713 if ( ccred.bv_val != NULL ) {
714 #if SASL_VERSION_MAJOR < 2
715 LDAP_FREE( ccred.bv_val );
720 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
721 if( scred && scred->bv_len ) {
722 /* and server provided us with data? */
724 LDAP_LOG ( TRANSPORT, DETAIL1,
725 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
726 rc, saslrc, scred->bv_len );
728 Debug( LDAP_DEBUG_TRACE,
729 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
730 rc, saslrc, scred->bv_len );
738 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
739 /* we're done, no need to step */
740 if( scred && scred->bv_len ) {
741 /* but server provided us with data! */
743 LDAP_LOG ( TRANSPORT, DETAIL1,
744 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
745 rc, saslrc, scred->bv_len );
747 Debug( LDAP_DEBUG_TRACE,
748 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
749 rc, saslrc, scred->bv_len );
752 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
759 saslrc = sasl_client_step( ctx,
760 (scred == NULL) ? NULL : scred->bv_val,
761 (scred == NULL) ? 0 : scred->bv_len,
763 (SASL_CONST char **)&ccred.bv_val,
767 LDAP_LOG ( TRANSPORT, DETAIL1,
768 "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
770 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
774 if( saslrc == SASL_INTERACT ) {
776 if( !interact ) break;
777 res = (interact)( ld, flags, defaults, prompts );
778 if( res != LDAP_SUCCESS ) break;
780 } while ( saslrc == SASL_INTERACT );
782 ccred.bv_len = credlen;
785 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
786 ld->ld_errno = sasl_err2ldap( saslrc );
787 #if SASL_VERSION_MAJOR >= 2
788 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
793 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
795 if ( rc != LDAP_SUCCESS ) goto done;
797 if ( saslrc != SASL_OK ) {
798 #if SASL_VERSION_MAJOR >= 2
799 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
801 rc = ld->ld_errno = sasl_err2ldap( saslrc );
805 if( flags != LDAP_SASL_QUIET ) {
806 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
807 if( saslrc == SASL_OK && data && *data ) {
808 fprintf( stderr, "SASL username: %s\n", data );
811 #if SASL_VERSION_MAJOR < 2
812 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
813 if( saslrc == SASL_OK && data && *data ) {
814 fprintf( stderr, "SASL realm: %s\n", data );
819 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
820 if( saslrc == SASL_OK ) {
821 if( flags != LDAP_SASL_QUIET ) {
822 fprintf( stderr, "SASL SSF: %lu\n",
823 (unsigned long) *ssf );
827 if( flags != LDAP_SASL_QUIET ) {
828 fprintf( stderr, "SASL installing layers\n" );
830 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
831 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
832 sasl_dispose( &oldctx );
833 ldap_pvt_sasl_remove( ld->ld_sb );
835 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
836 ld->ld_defconn->lconn_sasl_sockctx = ctx;
839 ld->ld_defconn->lconn_sasl_authctx = ctx;
846 ldap_int_sasl_external(
854 #if SASL_VERSION_MAJOR < 2
855 sasl_external_properties_t extprops;
858 ctx = conn->lconn_sasl_authctx;
861 return LDAP_LOCAL_ERROR;
864 #if SASL_VERSION_MAJOR >= 2
865 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
867 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
869 memset( &extprops, '\0', sizeof(extprops) );
871 extprops.auth_id = (char *) authid;
873 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
874 (void *) &extprops );
877 if ( sc != SASL_OK ) {
878 return LDAP_LOCAL_ERROR;
885 int ldap_pvt_sasl_secprops(
887 sasl_security_properties_t *secprops )
890 char **props = ldap_str2charray( in, "," );
893 sasl_ssf_t max_ssf = 0;
895 sasl_ssf_t min_ssf = 0;
897 unsigned maxbufsize = 0;
898 int got_maxbufsize = 0;
900 if( props == NULL || secprops == NULL ) {
901 return LDAP_PARAM_ERROR;
904 for( i=0; props[i]; i++ ) {
905 if( !strcasecmp(props[i], "none") ) {
908 } else if( !strcasecmp(props[i], "noplain") ) {
910 sflags |= SASL_SEC_NOPLAINTEXT;
912 } else if( !strcasecmp(props[i], "noactive") ) {
914 sflags |= SASL_SEC_NOACTIVE;
916 } else if( !strcasecmp(props[i], "nodict") ) {
918 sflags |= SASL_SEC_NODICTIONARY;
920 } else if( !strcasecmp(props[i], "forwardsec") ) {
922 sflags |= SASL_SEC_FORWARD_SECRECY;
924 } else if( !strcasecmp(props[i], "noanonymous")) {
926 sflags |= SASL_SEC_NOANONYMOUS;
928 } else if( !strcasecmp(props[i], "passcred") ) {
930 sflags |= SASL_SEC_PASS_CREDENTIALS;
932 } else if( !strncasecmp(props[i],
933 "minssf=", sizeof("minssf")) )
935 if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
937 min_ssf = atoi( &props[i][sizeof("minssf")] );
939 return LDAP_NOT_SUPPORTED;
942 } else if( !strncasecmp(props[i],
943 "maxssf=", sizeof("maxssf")) )
945 if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
947 max_ssf = atoi( &props[i][sizeof("maxssf")] );
949 return LDAP_NOT_SUPPORTED;
952 } else if( !strncasecmp(props[i],
953 "maxbufsize=", sizeof("maxbufsize")) )
955 if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
957 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
959 return LDAP_NOT_SUPPORTED;
962 if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
963 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
966 return LDAP_PARAM_ERROR;
970 return LDAP_NOT_SUPPORTED;
975 secprops->security_flags = sflags;
978 secprops->min_ssf = min_ssf;
981 secprops->max_ssf = max_ssf;
984 secprops->maxbufsize = maxbufsize;
987 ldap_charray_free( props );
992 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
997 case LDAP_OPT_X_SASL_SECPROPS:
998 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
999 if( rc == LDAP_SUCCESS ) return 0;
1006 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
1012 case LDAP_OPT_X_SASL_MECH: {
1013 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
1014 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
1016 case LDAP_OPT_X_SASL_REALM: {
1017 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
1018 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
1020 case LDAP_OPT_X_SASL_AUTHCID: {
1021 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
1022 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
1024 case LDAP_OPT_X_SASL_AUTHZID: {
1025 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
1026 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
1029 case LDAP_OPT_X_SASL_SSF: {
1034 if( ld->ld_defconn == NULL ) {
1038 ctx = ld->ld_defconn->lconn_sasl_sockctx;
1040 if ( ctx == NULL ) {
1044 sc = sasl_getprop( ctx, SASL_SSF,
1045 (SASL_CONST void **) &ssf );
1047 if ( sc != SASL_OK ) {
1051 *(ber_len_t *)arg = *ssf;
1054 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1055 /* this option is write only */
1058 case LDAP_OPT_X_SASL_SSF_MIN:
1059 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1061 case LDAP_OPT_X_SASL_SSF_MAX:
1062 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1064 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1065 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1068 case LDAP_OPT_X_SASL_SECPROPS:
1069 /* this option is write only */
1079 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1085 case LDAP_OPT_X_SASL_SSF:
1086 /* This option is read-only */
1089 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1091 #if SASL_VERSION_MAJOR < 2
1092 sasl_external_properties_t extprops;
1096 if( ld->ld_defconn == NULL ) {
1100 ctx = ld->ld_defconn->lconn_sasl_authctx;
1102 if ( ctx == NULL ) {
1106 #if SASL_VERSION_MAJOR >= 2
1107 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1109 memset(&extprops, 0L, sizeof(extprops));
1111 extprops.ssf = * (ber_len_t *) arg;
1113 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1114 (void *) &extprops );
1117 if ( sc != SASL_OK ) {
1122 case LDAP_OPT_X_SASL_SSF_MIN:
1123 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1125 case LDAP_OPT_X_SASL_SSF_MAX:
1126 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1128 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1129 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1132 case LDAP_OPT_X_SASL_SECPROPS: {
1134 sc = ldap_pvt_sasl_secprops( (char *) arg,
1135 &ld->ld_options.ldo_sasl_secprops );
1137 return sc == LDAP_SUCCESS ? 0 : -1;
1146 #ifdef LDAP_R_COMPILE
1147 #define LDAP_DEBUG_R_SASL
1148 void *ldap_pvt_sasl_mutex_new(void)
1150 ldap_pvt_thread_mutex_t *mutex;
1152 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1153 sizeof(ldap_pvt_thread_mutex_t) );
1155 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1158 #ifndef LDAP_DEBUG_R_SASL
1160 #endif /* !LDAP_DEBUG_R_SASL */
1164 int ldap_pvt_sasl_mutex_lock(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_lock( (ldap_pvt_thread_mutex_t *)mutex )
1174 ? SASL_FAIL : SASL_OK;
1177 int ldap_pvt_sasl_mutex_unlock(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 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1187 ? SASL_FAIL : SASL_OK;
1190 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1192 #ifdef LDAP_DEBUG_R_SASL
1193 if ( mutex == NULL ) {
1196 #else /* !LDAP_DEBUG_R_SASL */
1198 #endif /* !LDAP_DEBUG_R_SASL */
1199 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1205 int ldap_int_sasl_init( void )
1206 { return LDAP_SUCCESS; }
1208 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1209 { return LDAP_SUCCESS; }
1216 LDAPControl **sctrls,
1217 LDAPControl **cctrls,
1219 LDAP_SASL_INTERACT_PROC *interact,
1221 { return LDAP_NOT_SUPPORTED; }
1224 ldap_int_sasl_external(
1227 const char * authid,
1229 { return LDAP_SUCCESS; }
1231 #endif /* HAVE_CYRUS_SASL */