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 p->sec_buf_in.buf_ptr,
269 4 - p->sec_buf_in.buf_ptr );
271 if ( ( ret < 0 ) && ( errno == EINTR ) )
275 return bufptr ? bufptr : ret;
277 p->sec_buf_in.buf_ptr += ret;
280 /* The new packet always starts at p->sec_buf_in.buf_base */
281 ret = sb_sasl_pkt_length( (unsigned char *) p->sec_buf_in.buf_base,
282 sbiod->sbiod_sb->sb_debug );
284 /* Grow the packet buffer if neccessary */
285 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
286 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
291 p->sec_buf_in.buf_end = ret;
293 /* Did we read the whole encrypted packet? */
294 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
295 /* No, we have got only a part of it */
296 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
298 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
299 p->sec_buf_in.buf_ptr, ret );
301 if ( ( ret < 0 ) && ( errno == EINTR ) )
305 return bufptr ? bufptr : ret;
307 p->sec_buf_in.buf_ptr += ret;
310 /* Decode the packet */
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 *)&p->buf_in.buf_end );
316 /* Drop the packet from the input buffer */
317 sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
319 if ( ret != SASL_OK ) {
320 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
321 "sb_sasl_read: failed to decode packet: %s\n",
322 sasl_errstring( ret, NULL, NULL ) );
327 p->buf_in.buf_size = p->buf_in.buf_end;
329 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
335 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
337 struct sb_sasl_data *p;
340 assert( sbiod != NULL );
341 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
343 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
345 /* Are there anything left in the buffer? */
346 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
347 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
350 /* Still have something left?? */
351 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
357 /* now encode the next packet. */
358 #if SASL_VERSION_MAJOR >= 2
359 ber_pvt_sb_buf_init( &p->buf_out );
361 ber_pvt_sb_buf_destroy( &p->buf_out );
363 if ( len > *p->sasl_maxbuf - 100 )
364 len = *p->sasl_maxbuf - 100; /* For safety margin */
365 ret = sasl_encode( p->sasl_context, buf, len,
366 (SASL_CONST char **)&p->buf_out.buf_base,
367 (unsigned *)&p->buf_out.buf_size );
368 if ( ret != SASL_OK ) {
369 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
370 "sb_sasl_write: failed to encode packet: %s\n",
371 sasl_errstring( ret, NULL, NULL ) );
374 p->buf_out.buf_end = p->buf_out.buf_size;
376 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
378 /* caller will retry, so clear this buffer out */
379 p->buf_out.buf_ptr = p->buf_out.buf_end;
386 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
388 struct sb_sasl_data *p;
390 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
392 if ( opt == LBER_SB_OPT_DATA_READY ) {
393 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
397 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
400 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
401 sb_sasl_setup, /* sbi_setup */
402 sb_sasl_remove, /* sbi_remove */
403 sb_sasl_ctrl, /* sbi_ctrl */
404 sb_sasl_read, /* sbi_read */
405 sb_sasl_write, /* sbi_write */
409 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
412 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_install\n", 0, 0, 0 );
414 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
418 /* don't install the stuff unless security has been negotiated */
420 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
421 &ldap_pvt_sockbuf_io_sasl ) )
424 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
425 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
427 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
428 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
434 void ldap_pvt_sasl_remove( Sockbuf *sb )
436 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
437 LBER_SBIOD_LEVEL_APPLICATION );
439 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
440 LBER_SBIOD_LEVEL_APPLICATION );
445 sasl_err2ldap( int saslerr )
451 rc = LDAP_MORE_RESULTS_TO_RETURN;
454 rc = LDAP_LOCAL_ERROR;
460 rc = LDAP_LOCAL_ERROR;
466 rc = LDAP_AUTH_UNKNOWN;
469 rc = LDAP_AUTH_UNKNOWN;
472 rc = LDAP_PARAM_ERROR;
476 rc = LDAP_AUTH_UNKNOWN;
479 rc = LDAP_LOCAL_ERROR;
483 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
496 assert( lc->lconn_sasl_authctx == NULL );
498 if ( host == NULL ) {
499 ld->ld_errno = LDAP_LOCAL_ERROR;
503 if ( ldap_int_sasl_init() ) {
504 ld->ld_errno = LDAP_LOCAL_ERROR;
508 #if SASL_VERSION_MAJOR >= 2
509 rc = sasl_client_new( "ldap", host, NULL, NULL,
510 client_callbacks, 0, &ctx );
512 rc = sasl_client_new( "ldap", host, client_callbacks,
513 SASL_SECURITY_LAYER, &ctx );
516 if ( rc != SASL_OK ) {
517 ld->ld_errno = sasl_err2ldap( rc );
522 LDAP_LOG ( TRANSPORT, DETAIL1, "ldap_int_sasl_open: host=%s\n",
525 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
529 lc->lconn_sasl_authctx = ctx;
534 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
536 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
539 sasl_dispose( &ctx );
540 if ( lc->lconn_sasl_sockctx &&
541 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
542 ctx = lc->lconn_sasl_sockctx;
543 sasl_dispose( &ctx );
545 lc->lconn_sasl_sockctx = NULL;
546 lc->lconn_sasl_authctx = NULL;
557 LDAPControl **sctrls,
558 LDAPControl **cctrls,
560 LDAP_SASL_INTERACT_PROC *interact,
564 const char *mech = NULL;
565 const char *pmech = NULL;
567 sasl_ssf_t *ssf = NULL;
568 sasl_conn_t *ctx, *oldctx = NULL;
569 sasl_interact_t *prompts = NULL;
576 LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n",
577 mechs ? mechs : "<null>", 0, 0 );
579 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
580 mechs ? mechs : "<null>", 0, 0 );
583 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
584 if (ld->ld_version < LDAP_VERSION3) {
585 ld->ld_errno = LDAP_NOT_SUPPORTED;
589 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
591 if ( sd == AC_SOCKET_INVALID ) {
592 /* not connected yet */
595 rc = ldap_open_defconn( ld );
596 if( rc < 0 ) return ld->ld_errno;
598 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
600 if( sd == AC_SOCKET_INVALID ) {
601 ld->ld_errno = LDAP_LOCAL_ERROR;
606 oldctx = ld->ld_defconn->lconn_sasl_authctx;
608 /* If we already have an authentication context, clear it out */
610 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
611 sasl_dispose( &oldctx );
613 ld->ld_defconn->lconn_sasl_authctx = NULL;
616 { char *saslhost = ldap_host_connected_to( ld->ld_sb, "localhost" );
617 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
618 LDAP_FREE( saslhost );
621 if ( rc != LDAP_SUCCESS ) return rc;
623 ctx = ld->ld_defconn->lconn_sasl_authctx;
626 ssl = ldap_pvt_tls_sb_ctx( ld->ld_sb );
628 struct berval authid = BER_BVNULL;
631 fac = ldap_pvt_tls_get_strength( ssl );
632 /* failure is OK, we just can't use SASL EXTERNAL */
633 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
635 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
636 LDAP_FREE( authid.bv_val );
640 /* Check for local */
641 if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
642 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
643 "cn=peercred,cn=external,cn=auth")];
644 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
645 "cn=peercred,cn=external,cn=auth",
646 (int) geteuid(), (int) getegid() );
647 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
651 /* (re)set security properties */
652 sasl_setprop( ctx, SASL_SEC_PROPS,
653 &ld->ld_options.ldo_sasl_secprops );
659 saslrc = sasl_client_start( ctx,
661 #if SASL_VERSION_MAJOR < 2
665 (SASL_CONST char **)&ccred.bv_val,
669 if( pmech == NULL && mech != NULL ) {
672 if( flags != LDAP_SASL_QUIET ) {
674 "SASL/%s authentication started\n",
679 if( saslrc == SASL_INTERACT ) {
681 if( !interact ) break;
682 res = (interact)( ld, flags, defaults, prompts );
684 if( res != LDAP_SUCCESS ) break;
686 } while ( saslrc == SASL_INTERACT );
688 ccred.bv_len = credlen;
690 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
691 rc = ld->ld_errno = sasl_err2ldap( saslrc );
692 #if SASL_VERSION_MAJOR >= 2
693 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
699 struct berval *scred;
704 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
706 if ( ccred.bv_val != NULL ) {
707 #if SASL_VERSION_MAJOR < 2
708 LDAP_FREE( ccred.bv_val );
713 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
714 if( scred && scred->bv_len ) {
715 /* and server provided us with data? */
717 LDAP_LOG ( TRANSPORT, DETAIL1,
718 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
719 rc, saslrc, scred->bv_len );
721 Debug( LDAP_DEBUG_TRACE,
722 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
723 rc, saslrc, scred->bv_len );
731 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
732 /* we're done, no need to step */
733 if( scred && scred->bv_len ) {
734 /* but server provided us with data! */
736 LDAP_LOG ( TRANSPORT, DETAIL1,
737 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
738 rc, saslrc, scred->bv_len );
740 Debug( LDAP_DEBUG_TRACE,
741 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
742 rc, saslrc, scred->bv_len );
745 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
752 saslrc = sasl_client_step( ctx,
753 (scred == NULL) ? NULL : scred->bv_val,
754 (scred == NULL) ? 0 : scred->bv_len,
756 (SASL_CONST char **)&ccred.bv_val,
760 LDAP_LOG ( TRANSPORT, DETAIL1,
761 "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
763 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
767 if( saslrc == SASL_INTERACT ) {
769 if( !interact ) break;
770 res = (interact)( ld, flags, defaults, prompts );
771 if( res != LDAP_SUCCESS ) break;
773 } while ( saslrc == SASL_INTERACT );
775 ccred.bv_len = credlen;
778 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
779 ld->ld_errno = sasl_err2ldap( saslrc );
780 #if SASL_VERSION_MAJOR >= 2
781 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
786 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
788 if ( rc != LDAP_SUCCESS ) goto done;
790 if ( saslrc != SASL_OK ) {
791 #if SASL_VERSION_MAJOR >= 2
792 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
794 rc = ld->ld_errno = sasl_err2ldap( saslrc );
798 if( flags != LDAP_SASL_QUIET ) {
799 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
800 if( saslrc == SASL_OK && data && *data ) {
801 fprintf( stderr, "SASL username: %s\n", data );
804 #if SASL_VERSION_MAJOR < 2
805 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
806 if( saslrc == SASL_OK && data && *data ) {
807 fprintf( stderr, "SASL realm: %s\n", data );
812 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
813 if( saslrc == SASL_OK ) {
814 if( flags != LDAP_SASL_QUIET ) {
815 fprintf( stderr, "SASL SSF: %lu\n",
816 (unsigned long) *ssf );
820 if( flags != LDAP_SASL_QUIET ) {
821 fprintf( stderr, "SASL installing layers\n" );
823 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
824 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
825 sasl_dispose( &oldctx );
826 ldap_pvt_sasl_remove( ld->ld_sb );
828 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
829 ld->ld_defconn->lconn_sasl_sockctx = ctx;
832 ld->ld_defconn->lconn_sasl_authctx = ctx;
839 ldap_int_sasl_external(
847 #if SASL_VERSION_MAJOR < 2
848 sasl_external_properties_t extprops;
851 ctx = conn->lconn_sasl_authctx;
854 return LDAP_LOCAL_ERROR;
857 #if SASL_VERSION_MAJOR >= 2
858 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
860 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
862 memset( &extprops, '\0', sizeof(extprops) );
864 extprops.auth_id = (char *) authid;
866 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
867 (void *) &extprops );
870 if ( sc != SASL_OK ) {
871 return LDAP_LOCAL_ERROR;
878 int ldap_pvt_sasl_secprops(
880 sasl_security_properties_t *secprops )
883 char **props = ldap_str2charray( in, "," );
886 sasl_ssf_t max_ssf = 0;
888 sasl_ssf_t min_ssf = 0;
890 unsigned maxbufsize = 0;
891 int got_maxbufsize = 0;
893 if( props == NULL || secprops == NULL ) {
894 return LDAP_PARAM_ERROR;
897 for( i=0; props[i]; i++ ) {
898 if( !strcasecmp(props[i], "none") ) {
901 } else if( !strcasecmp(props[i], "noplain") ) {
903 sflags |= SASL_SEC_NOPLAINTEXT;
905 } else if( !strcasecmp(props[i], "noactive") ) {
907 sflags |= SASL_SEC_NOACTIVE;
909 } else if( !strcasecmp(props[i], "nodict") ) {
911 sflags |= SASL_SEC_NODICTIONARY;
913 } else if( !strcasecmp(props[i], "forwardsec") ) {
915 sflags |= SASL_SEC_FORWARD_SECRECY;
917 } else if( !strcasecmp(props[i], "noanonymous")) {
919 sflags |= SASL_SEC_NOANONYMOUS;
921 } else if( !strcasecmp(props[i], "passcred") ) {
923 sflags |= SASL_SEC_PASS_CREDENTIALS;
925 } else if( !strncasecmp(props[i],
926 "minssf=", sizeof("minssf")) )
928 if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
930 min_ssf = atoi( &props[i][sizeof("minssf")] );
932 return LDAP_NOT_SUPPORTED;
935 } else if( !strncasecmp(props[i],
936 "maxssf=", sizeof("maxssf")) )
938 if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
940 max_ssf = atoi( &props[i][sizeof("maxssf")] );
942 return LDAP_NOT_SUPPORTED;
945 } else if( !strncasecmp(props[i],
946 "maxbufsize=", sizeof("maxbufsize")) )
948 if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
950 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
952 return LDAP_NOT_SUPPORTED;
955 if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
956 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
959 return LDAP_PARAM_ERROR;
963 return LDAP_NOT_SUPPORTED;
968 secprops->security_flags = sflags;
971 secprops->min_ssf = min_ssf;
974 secprops->max_ssf = max_ssf;
977 secprops->maxbufsize = maxbufsize;
980 ldap_charray_free( props );
985 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
990 case LDAP_OPT_X_SASL_SECPROPS:
991 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
992 if( rc == LDAP_SUCCESS ) return 0;
999 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
1005 case LDAP_OPT_X_SASL_MECH: {
1006 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
1007 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
1009 case LDAP_OPT_X_SASL_REALM: {
1010 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
1011 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
1013 case LDAP_OPT_X_SASL_AUTHCID: {
1014 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
1015 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
1017 case LDAP_OPT_X_SASL_AUTHZID: {
1018 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
1019 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
1022 case LDAP_OPT_X_SASL_SSF: {
1027 if( ld->ld_defconn == NULL ) {
1031 ctx = ld->ld_defconn->lconn_sasl_sockctx;
1033 if ( ctx == NULL ) {
1037 sc = sasl_getprop( ctx, SASL_SSF,
1038 (SASL_CONST void **) &ssf );
1040 if ( sc != SASL_OK ) {
1044 *(ber_len_t *)arg = *ssf;
1047 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1048 /* this option is write only */
1051 case LDAP_OPT_X_SASL_SSF_MIN:
1052 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1054 case LDAP_OPT_X_SASL_SSF_MAX:
1055 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1057 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1058 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1061 case LDAP_OPT_X_SASL_SECPROPS:
1062 /* this option is write only */
1072 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1078 case LDAP_OPT_X_SASL_SSF:
1079 /* This option is read-only */
1082 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1084 #if SASL_VERSION_MAJOR < 2
1085 sasl_external_properties_t extprops;
1089 if( ld->ld_defconn == NULL ) {
1093 ctx = ld->ld_defconn->lconn_sasl_authctx;
1095 if ( ctx == NULL ) {
1099 #if SASL_VERSION_MAJOR >= 2
1100 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1102 memset(&extprops, 0L, sizeof(extprops));
1104 extprops.ssf = * (ber_len_t *) arg;
1106 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1107 (void *) &extprops );
1110 if ( sc != SASL_OK ) {
1115 case LDAP_OPT_X_SASL_SSF_MIN:
1116 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1118 case LDAP_OPT_X_SASL_SSF_MAX:
1119 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1121 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1122 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1125 case LDAP_OPT_X_SASL_SECPROPS: {
1127 sc = ldap_pvt_sasl_secprops( (char *) arg,
1128 &ld->ld_options.ldo_sasl_secprops );
1130 return sc == LDAP_SUCCESS ? 0 : -1;
1139 #ifdef LDAP_R_COMPILE
1140 #define LDAP_DEBUG_R_SASL
1141 void *ldap_pvt_sasl_mutex_new(void)
1143 ldap_pvt_thread_mutex_t *mutex;
1145 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1146 sizeof(ldap_pvt_thread_mutex_t) );
1148 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1151 #ifndef LDAP_DEBUG_R_SASL
1153 #endif /* !LDAP_DEBUG_R_SASL */
1157 int ldap_pvt_sasl_mutex_lock(void *mutex)
1159 #ifdef LDAP_DEBUG_R_SASL
1160 if ( mutex == NULL ) {
1163 #else /* !LDAP_DEBUG_R_SASL */
1165 #endif /* !LDAP_DEBUG_R_SASL */
1166 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1167 ? SASL_FAIL : SASL_OK;
1170 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1172 #ifdef LDAP_DEBUG_R_SASL
1173 if ( mutex == NULL ) {
1176 #else /* !LDAP_DEBUG_R_SASL */
1178 #endif /* !LDAP_DEBUG_R_SASL */
1179 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1180 ? SASL_FAIL : SASL_OK;
1183 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1185 #ifdef LDAP_DEBUG_R_SASL
1186 if ( mutex == NULL ) {
1189 #else /* !LDAP_DEBUG_R_SASL */
1191 #endif /* !LDAP_DEBUG_R_SASL */
1192 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1198 int ldap_int_sasl_init( void )
1199 { return LDAP_SUCCESS; }
1201 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1202 { return LDAP_SUCCESS; }
1209 LDAPControl **sctrls,
1210 LDAPControl **cctrls,
1212 LDAP_SASL_INTERACT_PROC *interact,
1214 { return LDAP_NOT_SUPPORTED; }
1217 ldap_int_sasl_external(
1220 const char * authid,
1222 { return LDAP_SUCCESS; }
1224 #endif /* HAVE_CYRUS_SASL */