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 */
309 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
310 p->sec_buf_in.buf_end,
311 (SASL_CONST char **)&p->buf_in.buf_base,
312 (unsigned *)&p->buf_in.buf_end );
314 /* Drop the packet from the input buffer */
315 sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
317 if ( ret != SASL_OK ) {
318 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
319 "sb_sasl_read: failed to decode packet: %s\n",
320 sasl_errstring( ret, NULL, NULL ) );
325 p->buf_in.buf_size = p->buf_in.buf_end;
327 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
333 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
335 struct sb_sasl_data *p;
338 assert( sbiod != NULL );
339 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
341 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
343 /* Are there anything left in the buffer? */
344 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
345 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
348 /* Still have something left?? */
349 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
355 /* now encode the next packet. */
356 #if SASL_VERSION_MAJOR >= 2
357 ber_pvt_sb_buf_init( &p->buf_out );
359 ber_pvt_sb_buf_destroy( &p->buf_out );
361 if ( len > *p->sasl_maxbuf - 100 )
362 len = *p->sasl_maxbuf - 100; /* For safety margin */
363 ret = sasl_encode( p->sasl_context, buf, len,
364 (SASL_CONST char **)&p->buf_out.buf_base,
365 (unsigned *)&p->buf_out.buf_size );
366 if ( ret != SASL_OK ) {
367 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
368 "sb_sasl_write: failed to encode packet: %s\n",
369 sasl_errstring( ret, NULL, NULL ) );
372 p->buf_out.buf_end = p->buf_out.buf_size;
374 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
376 /* caller will retry, so clear this buffer out */
377 p->buf_out.buf_ptr = p->buf_out.buf_end;
384 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
386 struct sb_sasl_data *p;
388 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
390 if ( opt == LBER_SB_OPT_DATA_READY ) {
391 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
395 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
398 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
399 sb_sasl_setup, /* sbi_setup */
400 sb_sasl_remove, /* sbi_remove */
401 sb_sasl_ctrl, /* sbi_ctrl */
402 sb_sasl_read, /* sbi_read */
403 sb_sasl_write, /* sbi_write */
407 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
410 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_install\n", 0, 0, 0 );
412 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
416 /* don't install the stuff unless security has been negotiated */
418 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
419 &ldap_pvt_sockbuf_io_sasl ) )
422 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
423 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
425 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
426 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
432 void ldap_pvt_sasl_remove( Sockbuf *sb )
434 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
435 LBER_SBIOD_LEVEL_APPLICATION );
437 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
438 LBER_SBIOD_LEVEL_APPLICATION );
443 sasl_err2ldap( int saslerr )
449 rc = LDAP_MORE_RESULTS_TO_RETURN;
452 rc = LDAP_LOCAL_ERROR;
458 rc = LDAP_LOCAL_ERROR;
464 rc = LDAP_AUTH_UNKNOWN;
467 rc = LDAP_AUTH_UNKNOWN;
470 rc = LDAP_PARAM_ERROR;
474 rc = LDAP_AUTH_UNKNOWN;
477 rc = LDAP_LOCAL_ERROR;
481 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
494 assert( lc->lconn_sasl_authctx == NULL );
496 if ( host == NULL ) {
497 ld->ld_errno = LDAP_LOCAL_ERROR;
501 if ( ldap_int_sasl_init() ) {
502 ld->ld_errno = LDAP_LOCAL_ERROR;
506 #if SASL_VERSION_MAJOR >= 2
507 rc = sasl_client_new( "ldap", host, NULL, NULL,
508 client_callbacks, 0, &ctx );
510 rc = sasl_client_new( "ldap", host, client_callbacks,
511 SASL_SECURITY_LAYER, &ctx );
514 if ( rc != SASL_OK ) {
515 ld->ld_errno = sasl_err2ldap( rc );
520 LDAP_LOG ( TRANSPORT, DETAIL1, "ldap_int_sasl_open: host=%s\n",
523 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
527 lc->lconn_sasl_authctx = ctx;
532 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
534 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
537 sasl_dispose( &ctx );
538 if ( lc->lconn_sasl_sockctx &&
539 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
540 ctx = lc->lconn_sasl_sockctx;
541 sasl_dispose( &ctx );
543 lc->lconn_sasl_sockctx = NULL;
544 lc->lconn_sasl_authctx = NULL;
555 LDAPControl **sctrls,
556 LDAPControl **cctrls,
558 LDAP_SASL_INTERACT_PROC *interact,
562 const char *mech = NULL;
563 const char *pmech = NULL;
565 sasl_ssf_t *ssf = NULL;
566 sasl_conn_t *ctx, *oldctx = NULL;
567 sasl_interact_t *prompts = NULL;
574 LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n",
575 mechs ? mechs : "<null>", 0, 0 );
577 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
578 mechs ? mechs : "<null>", 0, 0 );
581 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
582 if (ld->ld_version < LDAP_VERSION3) {
583 ld->ld_errno = LDAP_NOT_SUPPORTED;
587 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
589 if ( sd == AC_SOCKET_INVALID ) {
590 /* not connected yet */
593 rc = ldap_open_defconn( ld );
594 if( rc < 0 ) return ld->ld_errno;
596 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
598 if( sd == AC_SOCKET_INVALID ) {
599 ld->ld_errno = LDAP_LOCAL_ERROR;
604 oldctx = ld->ld_defconn->lconn_sasl_authctx;
606 /* If we already have an authentication context, clear it out */
608 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
609 sasl_dispose( &oldctx );
611 ld->ld_defconn->lconn_sasl_authctx = NULL;
614 { char *saslhost = ldap_host_connected_to( ld->ld_sb, "localhost" );
615 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
616 LDAP_FREE( saslhost );
619 if ( rc != LDAP_SUCCESS ) return rc;
621 ctx = ld->ld_defconn->lconn_sasl_authctx;
624 ssl = ldap_pvt_tls_sb_ctx( ld->ld_sb );
626 struct berval authid = BER_BVNULL;
629 fac = ldap_pvt_tls_get_strength( ssl );
630 /* failure is OK, we just can't use SASL EXTERNAL */
631 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
633 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
634 LDAP_FREE( authid.bv_val );
638 /* Check for local */
639 if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
640 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
641 "cn=peercred,cn=external,cn=auth")];
642 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
643 "cn=peercred,cn=external,cn=auth",
644 (int) geteuid(), (int) getegid() );
645 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
649 /* (re)set security properties */
650 sasl_setprop( ctx, SASL_SEC_PROPS,
651 &ld->ld_options.ldo_sasl_secprops );
657 saslrc = sasl_client_start( ctx,
659 #if SASL_VERSION_MAJOR < 2
663 (SASL_CONST char **)&ccred.bv_val,
667 if( pmech == NULL && mech != NULL ) {
670 if( flags != LDAP_SASL_QUIET ) {
672 "SASL/%s authentication started\n",
677 if( saslrc == SASL_INTERACT ) {
679 if( !interact ) break;
680 res = (interact)( ld, flags, defaults, prompts );
682 if( res != LDAP_SUCCESS ) break;
684 } while ( saslrc == SASL_INTERACT );
686 ccred.bv_len = credlen;
688 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
689 rc = ld->ld_errno = sasl_err2ldap( saslrc );
690 #if SASL_VERSION_MAJOR >= 2
691 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
697 struct berval *scred;
702 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
704 if ( ccred.bv_val != NULL ) {
705 #if SASL_VERSION_MAJOR < 2
706 LDAP_FREE( ccred.bv_val );
711 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
712 if( scred && scred->bv_len ) {
713 /* and server provided us with data? */
715 LDAP_LOG ( TRANSPORT, DETAIL1,
716 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
717 rc, saslrc, scred->bv_len );
719 Debug( LDAP_DEBUG_TRACE,
720 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
721 rc, saslrc, scred->bv_len );
729 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
730 /* we're done, no need to step */
731 if( scred && scred->bv_len ) {
732 /* but server provided us with data! */
734 LDAP_LOG ( TRANSPORT, DETAIL1,
735 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
736 rc, saslrc, scred->bv_len );
738 Debug( LDAP_DEBUG_TRACE,
739 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
740 rc, saslrc, scred->bv_len );
743 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
750 saslrc = sasl_client_step( ctx,
751 (scred == NULL) ? NULL : scred->bv_val,
752 (scred == NULL) ? 0 : scred->bv_len,
754 (SASL_CONST char **)&ccred.bv_val,
758 LDAP_LOG ( TRANSPORT, DETAIL1,
759 "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
761 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
765 if( saslrc == SASL_INTERACT ) {
767 if( !interact ) break;
768 res = (interact)( ld, flags, defaults, prompts );
769 if( res != LDAP_SUCCESS ) break;
771 } while ( saslrc == SASL_INTERACT );
773 ccred.bv_len = credlen;
776 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
777 ld->ld_errno = sasl_err2ldap( saslrc );
778 #if SASL_VERSION_MAJOR >= 2
779 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
784 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
786 if ( rc != LDAP_SUCCESS ) goto done;
788 if ( saslrc != SASL_OK ) {
789 #if SASL_VERSION_MAJOR >= 2
790 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
792 rc = ld->ld_errno = sasl_err2ldap( saslrc );
796 if( flags != LDAP_SASL_QUIET ) {
797 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
798 if( saslrc == SASL_OK && data && *data ) {
799 fprintf( stderr, "SASL username: %s\n", data );
802 #if SASL_VERSION_MAJOR < 2
803 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
804 if( saslrc == SASL_OK && data && *data ) {
805 fprintf( stderr, "SASL realm: %s\n", data );
810 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
811 if( saslrc == SASL_OK ) {
812 if( flags != LDAP_SASL_QUIET ) {
813 fprintf( stderr, "SASL SSF: %lu\n",
814 (unsigned long) *ssf );
818 if( flags != LDAP_SASL_QUIET ) {
819 fprintf( stderr, "SASL installing layers\n" );
821 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
822 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
823 sasl_dispose( &oldctx );
824 ldap_pvt_sasl_remove( ld->ld_sb );
826 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
827 ld->ld_defconn->lconn_sasl_sockctx = ctx;
830 ld->ld_defconn->lconn_sasl_authctx = ctx;
837 ldap_int_sasl_external(
845 #if SASL_VERSION_MAJOR < 2
846 sasl_external_properties_t extprops;
849 ctx = conn->lconn_sasl_authctx;
852 return LDAP_LOCAL_ERROR;
855 #if SASL_VERSION_MAJOR >= 2
856 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
858 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
860 memset( &extprops, '\0', sizeof(extprops) );
862 extprops.auth_id = (char *) authid;
864 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
865 (void *) &extprops );
868 if ( sc != SASL_OK ) {
869 return LDAP_LOCAL_ERROR;
876 int ldap_pvt_sasl_secprops(
878 sasl_security_properties_t *secprops )
881 char **props = ldap_str2charray( in, "," );
884 sasl_ssf_t max_ssf = 0;
886 sasl_ssf_t min_ssf = 0;
888 unsigned maxbufsize = 0;
889 int got_maxbufsize = 0;
891 if( props == NULL || secprops == NULL ) {
892 return LDAP_PARAM_ERROR;
895 for( i=0; props[i]; i++ ) {
896 if( !strcasecmp(props[i], "none") ) {
899 } else if( !strcasecmp(props[i], "noplain") ) {
901 sflags |= SASL_SEC_NOPLAINTEXT;
903 } else if( !strcasecmp(props[i], "noactive") ) {
905 sflags |= SASL_SEC_NOACTIVE;
907 } else if( !strcasecmp(props[i], "nodict") ) {
909 sflags |= SASL_SEC_NODICTIONARY;
911 } else if( !strcasecmp(props[i], "forwardsec") ) {
913 sflags |= SASL_SEC_FORWARD_SECRECY;
915 } else if( !strcasecmp(props[i], "noanonymous")) {
917 sflags |= SASL_SEC_NOANONYMOUS;
919 } else if( !strcasecmp(props[i], "passcred") ) {
921 sflags |= SASL_SEC_PASS_CREDENTIALS;
923 } else if( !strncasecmp(props[i],
924 "minssf=", sizeof("minssf")) )
926 if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
928 min_ssf = atoi( &props[i][sizeof("minssf")] );
930 return LDAP_NOT_SUPPORTED;
933 } else if( !strncasecmp(props[i],
934 "maxssf=", sizeof("maxssf")) )
936 if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
938 max_ssf = atoi( &props[i][sizeof("maxssf")] );
940 return LDAP_NOT_SUPPORTED;
943 } else if( !strncasecmp(props[i],
944 "maxbufsize=", sizeof("maxbufsize")) )
946 if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
948 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
950 return LDAP_NOT_SUPPORTED;
953 if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
954 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
957 return LDAP_PARAM_ERROR;
961 return LDAP_NOT_SUPPORTED;
966 secprops->security_flags = sflags;
969 secprops->min_ssf = min_ssf;
972 secprops->max_ssf = max_ssf;
975 secprops->maxbufsize = maxbufsize;
978 ldap_charray_free( props );
983 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
988 case LDAP_OPT_X_SASL_SECPROPS:
989 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
990 if( rc == LDAP_SUCCESS ) return 0;
997 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
1003 case LDAP_OPT_X_SASL_MECH: {
1004 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
1005 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
1007 case LDAP_OPT_X_SASL_REALM: {
1008 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
1009 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
1011 case LDAP_OPT_X_SASL_AUTHCID: {
1012 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
1013 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
1015 case LDAP_OPT_X_SASL_AUTHZID: {
1016 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
1017 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
1020 case LDAP_OPT_X_SASL_SSF: {
1025 if( ld->ld_defconn == NULL ) {
1029 ctx = ld->ld_defconn->lconn_sasl_sockctx;
1031 if ( ctx == NULL ) {
1035 sc = sasl_getprop( ctx, SASL_SSF,
1036 (SASL_CONST void **) &ssf );
1038 if ( sc != SASL_OK ) {
1042 *(ber_len_t *)arg = *ssf;
1045 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1046 /* this option is write only */
1049 case LDAP_OPT_X_SASL_SSF_MIN:
1050 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1052 case LDAP_OPT_X_SASL_SSF_MAX:
1053 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1055 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1056 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1059 case LDAP_OPT_X_SASL_SECPROPS:
1060 /* this option is write only */
1070 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1076 case LDAP_OPT_X_SASL_SSF:
1077 /* This option is read-only */
1080 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1082 #if SASL_VERSION_MAJOR < 2
1083 sasl_external_properties_t extprops;
1087 if( ld->ld_defconn == NULL ) {
1091 ctx = ld->ld_defconn->lconn_sasl_authctx;
1093 if ( ctx == NULL ) {
1097 #if SASL_VERSION_MAJOR >= 2
1098 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1100 memset(&extprops, 0L, sizeof(extprops));
1102 extprops.ssf = * (ber_len_t *) arg;
1104 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1105 (void *) &extprops );
1108 if ( sc != SASL_OK ) {
1113 case LDAP_OPT_X_SASL_SSF_MIN:
1114 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1116 case LDAP_OPT_X_SASL_SSF_MAX:
1117 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1119 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1120 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1123 case LDAP_OPT_X_SASL_SECPROPS: {
1125 sc = ldap_pvt_sasl_secprops( (char *) arg,
1126 &ld->ld_options.ldo_sasl_secprops );
1128 return sc == LDAP_SUCCESS ? 0 : -1;
1137 #ifdef LDAP_R_COMPILE
1138 #define LDAP_DEBUG_R_SASL
1139 void *ldap_pvt_sasl_mutex_new(void)
1141 ldap_pvt_thread_mutex_t *mutex;
1143 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1144 sizeof(ldap_pvt_thread_mutex_t) );
1146 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1149 #ifndef LDAP_DEBUG_R_SASL
1151 #endif /* !LDAP_DEBUG_R_SASL */
1155 int ldap_pvt_sasl_mutex_lock(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_lock( (ldap_pvt_thread_mutex_t *)mutex )
1165 ? SASL_FAIL : SASL_OK;
1168 int ldap_pvt_sasl_mutex_unlock(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 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1178 ? SASL_FAIL : SASL_OK;
1181 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1183 #ifdef LDAP_DEBUG_R_SASL
1184 if ( mutex == NULL ) {
1187 #else /* !LDAP_DEBUG_R_SASL */
1189 #endif /* !LDAP_DEBUG_R_SASL */
1190 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1196 int ldap_int_sasl_init( void )
1197 { return LDAP_SUCCESS; }
1199 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1200 { return LDAP_SUCCESS; }
1207 LDAPControl **sctrls,
1208 LDAPControl **cctrls,
1210 LDAP_SASL_INTERACT_PROC *interact,
1212 { return LDAP_NOT_SUPPORTED; }
1215 ldap_int_sasl_external(
1218 const char * authid,
1220 { return LDAP_SUCCESS; }
1222 #endif /* HAVE_CYRUS_SASL */