2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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
37 #define INT_MAX 2147483647 /* 32 bit signed max */
41 ldap_pvt_thread_mutex_t ldap_int_sasl_mutex;
44 #ifdef HAVE_SASL_SASL_H
45 #include <sasl/sasl.h>
50 #if SASL_VERSION_MAJOR >= 2
51 #define SASL_CONST const
57 * Various Cyrus SASL related stuff.
60 static const sasl_callback_t client_callbacks[] = {
61 #ifdef SASL_CB_GETREALM
62 { SASL_CB_GETREALM, NULL, NULL },
64 { SASL_CB_USER, NULL, NULL },
65 { SASL_CB_AUTHNAME, NULL, NULL },
66 { SASL_CB_PASS, NULL, NULL },
67 { SASL_CB_ECHOPROMPT, NULL, NULL },
68 { SASL_CB_NOECHOPROMPT, NULL, NULL },
69 { SASL_CB_LIST_END, NULL, NULL }
72 int ldap_int_sasl_init( void )
74 /* XXX not threadsafe */
75 static int sasl_initialized = 0;
77 #ifdef HAVE_SASL_VERSION
78 /* stringify the version number, sasl.h doesn't do it for us */
79 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
80 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
81 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
84 sasl_version( NULL, &rc );
85 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
86 (rc & 0xffff) < SASL_VERSION_STEP) {
87 char version[sizeof("xxx.xxx.xxxxx")];
88 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
91 Debug( LDAP_DEBUG_ANY,
92 "ldap_int_sasl_init: SASL library version mismatch:"
93 " expected " SASL_VERSION_STRING ","
94 " got %s\n", version, 0, 0 );
99 if ( sasl_initialized ) {
103 /* SASL 2 takes care of its own memory completely internally */
104 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
110 #endif /* CSRIMALLOC */
112 #ifdef LDAP_R_COMPILE
114 ldap_pvt_sasl_mutex_new,
115 ldap_pvt_sasl_mutex_lock,
116 ldap_pvt_sasl_mutex_unlock,
117 ldap_pvt_sasl_mutex_dispose );
120 if ( sasl_client_init( NULL ) == SASL_OK ) {
121 sasl_initialized = 1;
125 #if SASL_VERSION_MAJOR < 2
126 /* A no-op to make sure we link with Cyrus 1.5 */
127 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
133 * SASL encryption support for LBER Sockbufs
136 struct sb_sasl_data {
137 sasl_conn_t *sasl_context;
138 unsigned *sasl_maxbuf;
139 Sockbuf_Buf sec_buf_in;
145 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
147 struct sb_sasl_data *p;
149 assert( sbiod != NULL );
151 p = LBER_MALLOC( sizeof( *p ) );
154 p->sasl_context = (sasl_conn_t *)arg;
155 ber_pvt_sb_buf_init( &p->sec_buf_in );
156 ber_pvt_sb_buf_init( &p->buf_in );
157 ber_pvt_sb_buf_init( &p->buf_out );
158 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
163 sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
164 (SASL_CONST void **) &p->sasl_maxbuf );
166 sbiod->sbiod_pvt = p;
172 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
174 struct sb_sasl_data *p;
176 assert( sbiod != NULL );
178 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
179 #if SASL_VERSION_MAJOR >= 2
181 * SASLv2 encode/decode buffers are managed by
182 * libsasl2. Ensure they are not freed by liblber.
184 p->buf_in.buf_base = NULL;
185 p->buf_out.buf_base = NULL;
187 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
188 ber_pvt_sb_buf_destroy( &p->buf_in );
189 ber_pvt_sb_buf_destroy( &p->buf_out );
191 sbiod->sbiod_pvt = NULL;
196 sb_sasl_pkt_length( const unsigned char *buf, int debuglevel )
200 assert( buf != NULL );
207 if ( size > SASL_MAX_BUFF_SIZE ) {
208 /* somebody is trying to mess me up. */
209 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
210 "sb_sasl_pkt_length: received illegal packet length "
211 "of %lu bytes\n", (unsigned long)size );
212 size = 16; /* this should lead to an error. */
215 return size + 4; /* include the size !!! */
218 /* Drop a processed packet from the input buffer */
220 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel )
224 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
226 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
227 sec_buf_in->buf_end, len );
230 sec_buf_in->buf_end = sb_sasl_pkt_length(
231 (unsigned char *) sec_buf_in->buf_base, debuglevel);
234 sec_buf_in->buf_end = 0;
236 sec_buf_in->buf_ptr = len;
240 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
242 struct sb_sasl_data *p;
243 ber_slen_t ret, bufptr;
245 assert( sbiod != NULL );
246 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
248 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
250 /* Are there anything left in the buffer? */
251 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
258 #if SASL_VERSION_MAJOR >= 2
259 ber_pvt_sb_buf_init( &p->buf_in );
261 ber_pvt_sb_buf_destroy( &p->buf_in );
264 /* Read the length of the packet */
265 while ( p->sec_buf_in.buf_ptr < 4 ) {
266 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
267 p->sec_buf_in.buf_ptr,
268 4 - p->sec_buf_in.buf_ptr );
270 if ( ( ret < 0 ) && ( errno == EINTR ) )
274 return bufptr ? bufptr : ret;
276 p->sec_buf_in.buf_ptr += ret;
279 /* The new packet always starts at p->sec_buf_in.buf_base */
280 ret = sb_sasl_pkt_length( (unsigned char *) p->sec_buf_in.buf_base,
281 sbiod->sbiod_sb->sb_debug );
283 /* Grow the packet buffer if neccessary */
284 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
285 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
290 p->sec_buf_in.buf_end = ret;
292 /* Did we read the whole encrypted packet? */
293 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
294 /* No, we have got only a part of it */
295 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
297 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
298 p->sec_buf_in.buf_ptr, ret );
300 if ( ( ret < 0 ) && ( errno == EINTR ) )
304 return bufptr ? bufptr : ret;
306 p->sec_buf_in.buf_ptr += ret;
309 /* Decode the packet */
311 unsigned tmpsize = p->buf_in.buf_end;
312 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
313 p->sec_buf_in.buf_end,
314 (SASL_CONST char **)&p->buf_in.buf_base,
315 (unsigned *)&tmpsize );
316 p->buf_in.buf_end = tmpsize;
319 /* Drop the packet from the input buffer */
320 sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
322 if ( ret != SASL_OK ) {
323 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
324 "sb_sasl_read: failed to decode packet: %s\n",
325 sasl_errstring( ret, NULL, NULL ) );
330 p->buf_in.buf_size = p->buf_in.buf_end;
332 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
338 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
340 struct sb_sasl_data *p;
343 assert( sbiod != NULL );
344 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
346 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
348 /* Are there anything left in the buffer? */
349 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
350 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
351 if ( ret < 0 ) return ret;
353 /* Still have something left?? */
354 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
360 /* now encode the next packet. */
361 #if SASL_VERSION_MAJOR >= 2
362 ber_pvt_sb_buf_init( &p->buf_out );
364 ber_pvt_sb_buf_destroy( &p->buf_out );
366 if ( len > *p->sasl_maxbuf - 100 ) {
367 len = *p->sasl_maxbuf - 100; /* For safety margin */
371 unsigned tmpsize = p->buf_out.buf_size;
372 ret = sasl_encode( p->sasl_context, buf, len,
373 (SASL_CONST char **)&p->buf_out.buf_base,
375 p->buf_out.buf_size = tmpsize;
378 if ( ret != SASL_OK ) {
379 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
380 "sb_sasl_write: failed to encode packet: %s\n",
381 sasl_errstring( ret, NULL, NULL ) );
385 p->buf_out.buf_end = p->buf_out.buf_size;
387 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
389 /* return number of bytes encoded, not written, to ensure
390 * no byte is encoded twice (even if only sent once).
396 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
398 struct sb_sasl_data *p;
400 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
402 if ( opt == LBER_SB_OPT_DATA_READY ) {
403 if ( p->buf_in.buf_ptr != p->buf_in.buf_end ) return 1;
406 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
409 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
410 sb_sasl_setup, /* sbi_setup */
411 sb_sasl_remove, /* sbi_remove */
412 sb_sasl_ctrl, /* sbi_ctrl */
413 sb_sasl_read, /* sbi_read */
414 sb_sasl_write, /* sbi_write */
418 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
420 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
423 /* don't install the stuff unless security has been negotiated */
425 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
426 &ldap_pvt_sockbuf_io_sasl ) )
429 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
430 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
432 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
433 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
439 void ldap_pvt_sasl_remove( Sockbuf *sb )
441 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
442 LBER_SBIOD_LEVEL_APPLICATION );
444 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
445 LBER_SBIOD_LEVEL_APPLICATION );
450 sasl_err2ldap( int saslerr )
456 rc = LDAP_MORE_RESULTS_TO_RETURN;
459 rc = LDAP_LOCAL_ERROR;
465 rc = LDAP_LOCAL_ERROR;
471 rc = LDAP_AUTH_UNKNOWN;
474 rc = LDAP_AUTH_UNKNOWN;
477 rc = LDAP_PARAM_ERROR;
481 rc = LDAP_AUTH_UNKNOWN;
484 rc = LDAP_LOCAL_ERROR;
488 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
501 assert( lc->lconn_sasl_authctx == NULL );
503 if ( host == NULL ) {
504 ld->ld_errno = LDAP_LOCAL_ERROR;
508 if ( ldap_int_sasl_init() ) {
509 ld->ld_errno = LDAP_LOCAL_ERROR;
513 #if SASL_VERSION_MAJOR >= 2
514 rc = sasl_client_new( "ldap", host, NULL, NULL,
515 client_callbacks, 0, &ctx );
517 rc = sasl_client_new( "ldap", host, client_callbacks,
518 SASL_SECURITY_LAYER, &ctx );
521 if ( rc != SASL_OK ) {
522 ld->ld_errno = sasl_err2ldap( rc );
526 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;
575 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
576 mechs ? mechs : "<null>", 0, 0 );
578 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
579 if (ld->ld_version < LDAP_VERSION3) {
580 ld->ld_errno = LDAP_NOT_SUPPORTED;
584 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
586 if ( sd == AC_SOCKET_INVALID ) {
587 /* not connected yet */
590 rc = ldap_open_defconn( ld );
591 if( rc < 0 ) return ld->ld_errno;
593 ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb, LBER_SB_OPT_GET_FD, &sd );
595 if( sd == AC_SOCKET_INVALID ) {
596 ld->ld_errno = LDAP_LOCAL_ERROR;
601 oldctx = ld->ld_defconn->lconn_sasl_authctx;
603 /* If we already have an authentication context, clear it out */
605 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
606 sasl_dispose( &oldctx );
608 ld->ld_defconn->lconn_sasl_authctx = NULL;
611 { char *saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb, "localhost" );
612 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
613 LDAP_FREE( saslhost );
616 if ( rc != LDAP_SUCCESS ) return rc;
618 ctx = ld->ld_defconn->lconn_sasl_authctx;
621 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
623 struct berval authid = BER_BVNULL;
626 fac = ldap_pvt_tls_get_strength( ssl );
627 /* failure is OK, we just can't use SASL EXTERNAL */
628 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
630 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
631 LDAP_FREE( authid.bv_val );
635 /* Check for local */
636 if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
637 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
638 "cn=peercred,cn=external,cn=auth")];
639 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
640 "cn=peercred,cn=external,cn=auth",
641 (int) geteuid(), (int) getegid() );
642 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
646 /* (re)set security properties */
647 sasl_setprop( ctx, SASL_SEC_PROPS,
648 &ld->ld_options.ldo_sasl_secprops );
654 saslrc = sasl_client_start( ctx,
656 #if SASL_VERSION_MAJOR < 2
660 (SASL_CONST char **)&ccred.bv_val,
664 if( pmech == NULL && mech != NULL ) {
667 if( flags != LDAP_SASL_QUIET ) {
669 "SASL/%s authentication started\n",
674 if( saslrc == SASL_INTERACT ) {
676 if( !interact ) break;
677 res = (interact)( ld, flags, defaults, prompts );
679 if( res != LDAP_SUCCESS ) break;
681 } while ( saslrc == SASL_INTERACT );
683 ccred.bv_len = credlen;
685 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
686 rc = ld->ld_errno = sasl_err2ldap( saslrc );
687 #if SASL_VERSION_MAJOR >= 2
688 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
694 struct berval *scred;
699 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
701 if ( ccred.bv_val != NULL ) {
702 #if SASL_VERSION_MAJOR < 2
703 LDAP_FREE( ccred.bv_val );
708 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
709 if( scred && scred->bv_len ) {
710 /* and server provided us with data? */
711 Debug( LDAP_DEBUG_TRACE,
712 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
713 rc, saslrc, scred->bv_len );
720 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
721 /* we're done, no need to step */
722 if( scred && scred->bv_len ) {
723 /* but server provided us with data! */
724 Debug( LDAP_DEBUG_TRACE,
725 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
726 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,
742 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
745 if( saslrc == SASL_INTERACT ) {
747 if( !interact ) break;
748 res = (interact)( ld, flags, defaults, prompts );
749 if( res != LDAP_SUCCESS ) break;
751 } while ( saslrc == SASL_INTERACT );
753 ccred.bv_len = credlen;
756 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
757 ld->ld_errno = sasl_err2ldap( saslrc );
758 #if SASL_VERSION_MAJOR >= 2
759 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
764 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
766 if ( rc != LDAP_SUCCESS ) goto done;
768 if ( saslrc != SASL_OK ) {
769 #if SASL_VERSION_MAJOR >= 2
770 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
772 rc = ld->ld_errno = sasl_err2ldap( saslrc );
776 if( flags != LDAP_SASL_QUIET ) {
777 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
778 if( saslrc == SASL_OK && data && *data ) {
779 fprintf( stderr, "SASL username: %s\n", data );
782 #if SASL_VERSION_MAJOR < 2
783 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
784 if( saslrc == SASL_OK && data && *data ) {
785 fprintf( stderr, "SASL realm: %s\n", data );
790 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
791 if( saslrc == SASL_OK ) {
792 if( flags != LDAP_SASL_QUIET ) {
793 fprintf( stderr, "SASL SSF: %lu\n",
794 (unsigned long) *ssf );
798 if( flags != LDAP_SASL_QUIET ) {
799 fprintf( stderr, "SASL installing layers\n" );
801 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
802 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
803 sasl_dispose( &oldctx );
804 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
806 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
807 ld->ld_defconn->lconn_sasl_sockctx = ctx;
810 ld->ld_defconn->lconn_sasl_authctx = ctx;
817 ldap_int_sasl_external(
825 #if SASL_VERSION_MAJOR < 2
826 sasl_external_properties_t extprops;
829 ctx = conn->lconn_sasl_authctx;
832 return LDAP_LOCAL_ERROR;
835 #if SASL_VERSION_MAJOR >= 2
836 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
838 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
840 memset( &extprops, '\0', sizeof(extprops) );
842 extprops.auth_id = (char *) authid;
844 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
845 (void *) &extprops );
848 if ( sc != SASL_OK ) {
849 return LDAP_LOCAL_ERROR;
866 { BER_BVC("none"), 0, 0, 0 },
867 { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
868 { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
869 { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
870 { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
871 { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
872 { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
873 { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
874 { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
875 { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
879 void ldap_pvt_sasl_secprops_unparse(
880 sasl_security_properties_t *secprops,
887 if ( secprops == NULL || out == NULL ) {
892 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
893 if ( sprops[i].ival ) {
896 switch( sprops[i].ival ) {
897 case GOT_MINSSF: v = secprops->min_ssf; break;
898 case GOT_MAXSSF: v = secprops->max_ssf; break;
899 case GOT_MAXBUF: v = secprops->maxbufsize; break;
901 /* It is the default, ignore it */
902 if ( v == sprops[i].idef ) continue;
904 l += sprops[i].key.bv_len + 24;
905 } else if ( sprops[i].sflag ) {
906 if ( sprops[i].sflag & secprops->security_flags ) {
907 l += sprops[i].key.bv_len;
909 } else if ( secprops->security_flags == 0 ) {
910 l += sprops[i].key.bv_len;
917 out->bv_val = LDAP_MALLOC( l );
918 if ( out->bv_val == NULL ) {
925 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
926 if ( sprops[i].ival ) {
929 switch( sprops[i].ival ) {
930 case GOT_MINSSF: v = secprops->min_ssf; break;
931 case GOT_MAXSSF: v = secprops->max_ssf; break;
932 case GOT_MAXBUF: v = secprops->maxbufsize; break;
934 /* It is the default, ignore it */
935 if ( v == sprops[i].idef ) continue;
937 if ( comma ) *ptr++ = ',';
938 ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
940 } else if ( sprops[i].sflag ) {
941 if ( sprops[i].sflag & secprops->security_flags ) {
942 if ( comma ) *ptr++ = ',';
943 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
946 } else if ( secprops->security_flags == 0 ) {
947 if ( comma ) *ptr++ = ',';
948 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
952 out->bv_len = ptr - out->bv_val;
955 int ldap_pvt_sasl_secprops(
957 sasl_security_properties_t *secprops )
960 char **props = ldap_str2charray( in, "," );
963 sasl_ssf_t max_ssf = 0;
965 sasl_ssf_t min_ssf = 0;
967 unsigned maxbufsize = 0;
968 int got_maxbufsize = 0;
970 if( props == NULL || secprops == NULL ) {
971 return LDAP_PARAM_ERROR;
974 for( i=0; props[i]; i++ ) {
975 l = strlen( props[i] );
976 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
977 if ( l < sprops[j].key.bv_len ) continue;
978 if ( strncasecmp( props[i], sprops[j].key.bv_val,
979 sprops[j].key.bv_len )) continue;
980 if ( sprops[j].ival ) {
982 if ( props[i][sprops[j].key.bv_len] != '=' ) continue;
983 if ( !isdigit( props[i][sprops[j].key.bv_len+1] )) continue;
984 v = atoi( props[i]+sprops[j].key.bv_len+1 );
985 switch( sprops[j].ival ) {
987 min_ssf = v; got_min_ssf++; break;
989 max_ssf = v; got_max_ssf++; break;
991 maxbufsize = v; got_maxbufsize++; break;
994 if ( props[i][sprops[j].key.bv_len] ) continue;
995 if ( sprops[j].sflag )
996 sflags |= sprops[j].sflag;
1003 if ( BER_BVISNULL( &sprops[j].key )) {
1004 return LDAP_NOT_SUPPORTED;
1009 secprops->security_flags = sflags;
1012 secprops->min_ssf = min_ssf;
1015 secprops->max_ssf = max_ssf;
1017 if(got_maxbufsize) {
1018 secprops->maxbufsize = maxbufsize;
1021 ldap_charray_free( props );
1022 return LDAP_SUCCESS;
1026 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
1031 case LDAP_OPT_X_SASL_SECPROPS:
1032 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
1033 if( rc == LDAP_SUCCESS ) return 0;
1040 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
1046 case LDAP_OPT_X_SASL_MECH: {
1047 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
1048 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
1050 case LDAP_OPT_X_SASL_REALM: {
1051 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
1052 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
1054 case LDAP_OPT_X_SASL_AUTHCID: {
1055 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
1056 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
1058 case LDAP_OPT_X_SASL_AUTHZID: {
1059 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
1060 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
1063 case LDAP_OPT_X_SASL_SSF: {
1068 if( ld->ld_defconn == NULL ) {
1072 ctx = ld->ld_defconn->lconn_sasl_sockctx;
1074 if ( ctx == NULL ) {
1078 sc = sasl_getprop( ctx, SASL_SSF,
1079 (SASL_CONST void **) &ssf );
1081 if ( sc != SASL_OK ) {
1085 *(ber_len_t *)arg = *ssf;
1088 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1089 /* this option is write only */
1092 case LDAP_OPT_X_SASL_SSF_MIN:
1093 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1095 case LDAP_OPT_X_SASL_SSF_MAX:
1096 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1098 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1099 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1102 case LDAP_OPT_X_SASL_SECPROPS:
1103 /* this option is write only */
1113 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1119 case LDAP_OPT_X_SASL_SSF:
1120 /* This option is read-only */
1123 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1125 #if SASL_VERSION_MAJOR < 2
1126 sasl_external_properties_t extprops;
1130 if( ld->ld_defconn == NULL ) {
1134 ctx = ld->ld_defconn->lconn_sasl_authctx;
1136 if ( ctx == NULL ) {
1140 #if SASL_VERSION_MAJOR >= 2
1141 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1143 memset(&extprops, 0L, sizeof(extprops));
1145 extprops.ssf = * (ber_len_t *) arg;
1147 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1148 (void *) &extprops );
1151 if ( sc != SASL_OK ) {
1156 case LDAP_OPT_X_SASL_SSF_MIN:
1157 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1159 case LDAP_OPT_X_SASL_SSF_MAX:
1160 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1162 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1163 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1166 case LDAP_OPT_X_SASL_SECPROPS: {
1168 sc = ldap_pvt_sasl_secprops( (char *) arg,
1169 &ld->ld_options.ldo_sasl_secprops );
1171 return sc == LDAP_SUCCESS ? 0 : -1;
1180 #ifdef LDAP_R_COMPILE
1181 #define LDAP_DEBUG_R_SASL
1182 void *ldap_pvt_sasl_mutex_new(void)
1184 ldap_pvt_thread_mutex_t *mutex;
1186 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1187 sizeof(ldap_pvt_thread_mutex_t) );
1189 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1192 #ifndef LDAP_DEBUG_R_SASL
1194 #endif /* !LDAP_DEBUG_R_SASL */
1198 int ldap_pvt_sasl_mutex_lock(void *mutex)
1200 #ifdef LDAP_DEBUG_R_SASL
1201 if ( mutex == NULL ) {
1204 #else /* !LDAP_DEBUG_R_SASL */
1206 #endif /* !LDAP_DEBUG_R_SASL */
1207 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1208 ? SASL_FAIL : SASL_OK;
1211 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1213 #ifdef LDAP_DEBUG_R_SASL
1214 if ( mutex == NULL ) {
1217 #else /* !LDAP_DEBUG_R_SASL */
1219 #endif /* !LDAP_DEBUG_R_SASL */
1220 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1221 ? SASL_FAIL : SASL_OK;
1224 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1226 #ifdef LDAP_DEBUG_R_SASL
1227 if ( mutex == NULL ) {
1230 #else /* !LDAP_DEBUG_R_SASL */
1232 #endif /* !LDAP_DEBUG_R_SASL */
1233 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1239 int ldap_int_sasl_init( void )
1240 { return LDAP_SUCCESS; }
1242 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1243 { return LDAP_SUCCESS; }
1250 LDAPControl **sctrls,
1251 LDAPControl **cctrls,
1253 LDAP_SASL_INTERACT_PROC *interact,
1255 { return LDAP_NOT_SUPPORTED; }
1258 ldap_int_sasl_external(
1261 const char * authid,
1263 { return LDAP_SUCCESS; }
1265 #endif /* HAVE_CYRUS_SASL */