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,
83 Debug( LDAP_DEBUG_ANY,
84 "ldap_int_sasl_init: SASL library version mismatch:"
85 " expected " SASL_VERSION_STRING ","
86 " got %s\n", version, 0, 0 );
91 if ( sasl_initialized ) {
95 /* SASL 2 takes care of its own memory completely internally */
96 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
102 #endif /* CSRIMALLOC */
104 #ifdef LDAP_R_COMPILE
106 ldap_pvt_sasl_mutex_new,
107 ldap_pvt_sasl_mutex_lock,
108 ldap_pvt_sasl_mutex_unlock,
109 ldap_pvt_sasl_mutex_dispose );
112 if ( sasl_client_init( NULL ) == SASL_OK ) {
113 sasl_initialized = 1;
117 #if SASL_VERSION_MAJOR < 2
118 /* A no-op to make sure we link with Cyrus 1.5 */
119 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
125 * SASL encryption support for LBER Sockbufs
128 struct sb_sasl_data {
129 sasl_conn_t *sasl_context;
130 unsigned *sasl_maxbuf;
131 Sockbuf_Buf sec_buf_in;
137 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
139 struct sb_sasl_data *p;
141 assert( sbiod != NULL );
143 p = LBER_MALLOC( sizeof( *p ) );
146 p->sasl_context = (sasl_conn_t *)arg;
147 ber_pvt_sb_buf_init( &p->sec_buf_in );
148 ber_pvt_sb_buf_init( &p->buf_in );
149 ber_pvt_sb_buf_init( &p->buf_out );
150 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
155 sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
156 (SASL_CONST void **) &p->sasl_maxbuf );
158 sbiod->sbiod_pvt = p;
164 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
166 struct sb_sasl_data *p;
168 assert( sbiod != NULL );
170 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
171 #if SASL_VERSION_MAJOR >= 2
173 * SASLv2 encode/decode buffers are managed by
174 * libsasl2. Ensure they are not freed by liblber.
176 p->buf_in.buf_base = NULL;
177 p->buf_out.buf_base = NULL;
179 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
180 ber_pvt_sb_buf_destroy( &p->buf_in );
181 ber_pvt_sb_buf_destroy( &p->buf_out );
183 sbiod->sbiod_pvt = NULL;
188 sb_sasl_pkt_length( const unsigned char *buf, int debuglevel )
192 assert( buf != NULL );
199 if ( size > SASL_MAX_BUFF_SIZE ) {
200 /* somebody is trying to mess me up. */
201 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
202 "sb_sasl_pkt_length: received illegal packet length "
203 "of %lu bytes\n", (unsigned long)size );
204 size = 16; /* this should lead to an error. */
207 return size + 4; /* include the size !!! */
210 /* Drop a processed packet from the input buffer */
212 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, int debuglevel )
216 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
218 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
219 sec_buf_in->buf_end, len );
222 sec_buf_in->buf_end = sb_sasl_pkt_length(
223 (unsigned char *) sec_buf_in->buf_base, debuglevel);
226 sec_buf_in->buf_end = 0;
228 sec_buf_in->buf_ptr = len;
232 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
234 struct sb_sasl_data *p;
235 ber_slen_t ret, bufptr;
237 assert( sbiod != NULL );
238 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
240 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
242 /* Are there anything left in the buffer? */
243 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
250 #if SASL_VERSION_MAJOR >= 2
251 ber_pvt_sb_buf_init( &p->buf_in );
253 ber_pvt_sb_buf_destroy( &p->buf_in );
256 /* Read the length of the packet */
257 while ( p->sec_buf_in.buf_ptr < 4 ) {
258 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
259 p->sec_buf_in.buf_ptr,
260 4 - p->sec_buf_in.buf_ptr );
262 if ( ( ret < 0 ) && ( errno == EINTR ) )
266 return bufptr ? bufptr : ret;
268 p->sec_buf_in.buf_ptr += ret;
271 /* The new packet always starts at p->sec_buf_in.buf_base */
272 ret = sb_sasl_pkt_length( (unsigned char *) p->sec_buf_in.buf_base,
273 sbiod->sbiod_sb->sb_debug );
275 /* Grow the packet buffer if neccessary */
276 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
277 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
282 p->sec_buf_in.buf_end = ret;
284 /* Did we read the whole encrypted packet? */
285 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
286 /* No, we have got only a part of it */
287 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
289 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
290 p->sec_buf_in.buf_ptr, ret );
292 if ( ( ret < 0 ) && ( errno == EINTR ) )
296 return bufptr ? bufptr : ret;
298 p->sec_buf_in.buf_ptr += ret;
301 /* Decode the packet */
303 unsigned tmpsize = p->buf_in.buf_end;
304 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
305 p->sec_buf_in.buf_end,
306 (SASL_CONST char **)&p->buf_in.buf_base,
307 (unsigned *)&tmpsize );
308 p->buf_in.buf_end = tmpsize;
311 /* Drop the packet from the input buffer */
312 sb_sasl_drop_packet( &p->sec_buf_in, sbiod->sbiod_sb->sb_debug );
314 if ( ret != SASL_OK ) {
315 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
316 "sb_sasl_read: failed to decode packet: %s\n",
317 sasl_errstring( ret, NULL, NULL ) );
322 p->buf_in.buf_size = p->buf_in.buf_end;
324 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
330 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
332 struct sb_sasl_data *p;
335 assert( sbiod != NULL );
336 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
338 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
340 /* Are there anything left in the buffer? */
341 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
342 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
343 if ( ret < 0 ) return ret;
345 /* Still have something left?? */
346 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
352 /* now encode the next packet. */
353 #if SASL_VERSION_MAJOR >= 2
354 ber_pvt_sb_buf_init( &p->buf_out );
356 ber_pvt_sb_buf_destroy( &p->buf_out );
358 if ( len > *p->sasl_maxbuf - 100 ) {
359 len = *p->sasl_maxbuf - 100; /* For safety margin */
363 unsigned tmpsize = p->buf_out.buf_size;
364 ret = sasl_encode( p->sasl_context, buf, len,
365 (SASL_CONST char **)&p->buf_out.buf_base,
367 p->buf_out.buf_size = tmpsize;
370 if ( ret != SASL_OK ) {
371 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
372 "sb_sasl_write: failed to encode packet: %s\n",
373 sasl_errstring( ret, NULL, NULL ) );
377 p->buf_out.buf_end = p->buf_out.buf_size;
379 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
381 /* return number of bytes encoded, not written, to ensure
382 * no byte is encoded twice (even if only sent once).
388 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
390 struct sb_sasl_data *p;
392 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
394 if ( opt == LBER_SB_OPT_DATA_READY ) {
395 if ( p->buf_in.buf_ptr != p->buf_in.buf_end ) return 1;
398 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
401 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
402 sb_sasl_setup, /* sbi_setup */
403 sb_sasl_remove, /* sbi_remove */
404 sb_sasl_ctrl, /* sbi_ctrl */
405 sb_sasl_read, /* sbi_read */
406 sb_sasl_write, /* sbi_write */
410 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
412 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
415 /* don't install the stuff unless security has been negotiated */
417 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
418 &ldap_pvt_sockbuf_io_sasl ) )
421 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
422 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
424 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
425 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
431 void ldap_pvt_sasl_remove( Sockbuf *sb )
433 ber_sockbuf_remove_io( sb, &ldap_pvt_sockbuf_io_sasl,
434 LBER_SBIOD_LEVEL_APPLICATION );
436 ber_sockbuf_remove_io( sb, &ber_sockbuf_io_debug,
437 LBER_SBIOD_LEVEL_APPLICATION );
442 sasl_err2ldap( int saslerr )
448 rc = LDAP_MORE_RESULTS_TO_RETURN;
451 rc = LDAP_LOCAL_ERROR;
457 rc = LDAP_LOCAL_ERROR;
463 rc = LDAP_AUTH_UNKNOWN;
466 rc = LDAP_AUTH_UNKNOWN;
469 rc = LDAP_PARAM_ERROR;
473 rc = LDAP_AUTH_UNKNOWN;
476 rc = LDAP_LOCAL_ERROR;
480 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
493 assert( lc->lconn_sasl_authctx == NULL );
495 if ( host == NULL ) {
496 ld->ld_errno = LDAP_LOCAL_ERROR;
500 if ( ldap_int_sasl_init() ) {
501 ld->ld_errno = LDAP_LOCAL_ERROR;
505 #if SASL_VERSION_MAJOR >= 2
506 rc = sasl_client_new( "ldap", host, NULL, NULL,
507 client_callbacks, 0, &ctx );
509 rc = sasl_client_new( "ldap", host, client_callbacks,
510 SASL_SECURITY_LAYER, &ctx );
513 if ( rc != SASL_OK ) {
514 ld->ld_errno = sasl_err2ldap( rc );
518 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
521 lc->lconn_sasl_authctx = ctx;
526 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
528 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
531 sasl_dispose( &ctx );
532 if ( lc->lconn_sasl_sockctx &&
533 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
534 ctx = lc->lconn_sasl_sockctx;
535 sasl_dispose( &ctx );
537 lc->lconn_sasl_sockctx = NULL;
538 lc->lconn_sasl_authctx = NULL;
549 LDAPControl **sctrls,
550 LDAPControl **cctrls,
552 LDAP_SASL_INTERACT_PROC *interact,
556 const char *mech = NULL;
557 const char *pmech = NULL;
559 sasl_ssf_t *ssf = NULL;
560 sasl_conn_t *ctx, *oldctx = NULL;
561 sasl_interact_t *prompts = NULL;
567 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
568 mechs ? mechs : "<null>", 0, 0 );
570 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
571 if (ld->ld_version < LDAP_VERSION3) {
572 ld->ld_errno = LDAP_NOT_SUPPORTED;
576 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
578 if ( sd == AC_SOCKET_INVALID ) {
579 /* not connected yet */
582 rc = ldap_open_defconn( ld );
583 if( rc < 0 ) return ld->ld_errno;
585 ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb, LBER_SB_OPT_GET_FD, &sd );
587 if( sd == AC_SOCKET_INVALID ) {
588 ld->ld_errno = LDAP_LOCAL_ERROR;
593 oldctx = ld->ld_defconn->lconn_sasl_authctx;
595 /* If we already have an authentication context, clear it out */
597 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
598 sasl_dispose( &oldctx );
600 ld->ld_defconn->lconn_sasl_authctx = NULL;
603 { char *saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb, "localhost" );
604 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
605 LDAP_FREE( saslhost );
608 if ( rc != LDAP_SUCCESS ) return rc;
610 ctx = ld->ld_defconn->lconn_sasl_authctx;
613 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
615 struct berval authid = BER_BVNULL;
618 fac = ldap_pvt_tls_get_strength( ssl );
619 /* failure is OK, we just can't use SASL EXTERNAL */
620 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
622 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
623 LDAP_FREE( authid.bv_val );
627 /* Check for local */
628 if ( ldap_pvt_url_scheme2proto( ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC ) {
629 char authid[sizeof("uidNumber=4294967295+gidNumber=4294967295,"
630 "cn=peercred,cn=external,cn=auth")];
631 sprintf( authid, "uidNumber=%d+gidNumber=%d,"
632 "cn=peercred,cn=external,cn=auth",
633 (int) geteuid(), (int) getegid() );
634 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid, LDAP_PVT_SASL_LOCAL_SSF );
638 /* (re)set security properties */
639 sasl_setprop( ctx, SASL_SEC_PROPS,
640 &ld->ld_options.ldo_sasl_secprops );
646 saslrc = sasl_client_start( ctx,
648 #if SASL_VERSION_MAJOR < 2
652 (SASL_CONST char **)&ccred.bv_val,
656 if( pmech == NULL && mech != NULL ) {
659 if( flags != LDAP_SASL_QUIET ) {
661 "SASL/%s authentication started\n",
666 if( saslrc == SASL_INTERACT ) {
668 if( !interact ) break;
669 res = (interact)( ld, flags, defaults, prompts );
671 if( res != LDAP_SUCCESS ) break;
673 } while ( saslrc == SASL_INTERACT );
675 ccred.bv_len = credlen;
677 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
678 rc = ld->ld_errno = sasl_err2ldap( saslrc );
679 #if SASL_VERSION_MAJOR >= 2
680 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
686 struct berval *scred;
691 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
693 if ( ccred.bv_val != NULL ) {
694 #if SASL_VERSION_MAJOR < 2
695 LDAP_FREE( ccred.bv_val );
700 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
701 if( scred && scred->bv_len ) {
702 /* and server provided us with data? */
703 Debug( LDAP_DEBUG_TRACE,
704 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
705 rc, saslrc, scred->bv_len );
712 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
713 /* we're done, no need to step */
714 if( scred && scred->bv_len ) {
715 /* but server provided us with data! */
716 Debug( LDAP_DEBUG_TRACE,
717 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
718 rc, saslrc, scred->bv_len );
720 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
727 saslrc = sasl_client_step( ctx,
728 (scred == NULL) ? NULL : scred->bv_val,
729 (scred == NULL) ? 0 : scred->bv_len,
731 (SASL_CONST char **)&ccred.bv_val,
734 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
737 if( saslrc == SASL_INTERACT ) {
739 if( !interact ) break;
740 res = (interact)( ld, flags, defaults, prompts );
741 if( res != LDAP_SUCCESS ) break;
743 } while ( saslrc == SASL_INTERACT );
745 ccred.bv_len = credlen;
748 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
749 ld->ld_errno = sasl_err2ldap( saslrc );
750 #if SASL_VERSION_MAJOR >= 2
751 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
756 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
758 if ( rc != LDAP_SUCCESS ) goto done;
760 if ( saslrc != SASL_OK ) {
761 #if SASL_VERSION_MAJOR >= 2
762 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
764 rc = ld->ld_errno = sasl_err2ldap( saslrc );
768 if( flags != LDAP_SASL_QUIET ) {
769 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
770 if( saslrc == SASL_OK && data && *data ) {
771 fprintf( stderr, "SASL username: %s\n", data );
774 #if SASL_VERSION_MAJOR < 2
775 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
776 if( saslrc == SASL_OK && data && *data ) {
777 fprintf( stderr, "SASL realm: %s\n", data );
782 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
783 if( saslrc == SASL_OK ) {
784 if( flags != LDAP_SASL_QUIET ) {
785 fprintf( stderr, "SASL SSF: %lu\n",
786 (unsigned long) *ssf );
790 if( flags != LDAP_SASL_QUIET ) {
791 fprintf( stderr, "SASL installing layers\n" );
793 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
794 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
795 sasl_dispose( &oldctx );
796 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
798 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
799 ld->ld_defconn->lconn_sasl_sockctx = ctx;
802 ld->ld_defconn->lconn_sasl_authctx = ctx;
809 ldap_int_sasl_external(
817 #if SASL_VERSION_MAJOR < 2
818 sasl_external_properties_t extprops;
821 ctx = conn->lconn_sasl_authctx;
824 return LDAP_LOCAL_ERROR;
827 #if SASL_VERSION_MAJOR >= 2
828 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
830 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
832 memset( &extprops, '\0', sizeof(extprops) );
834 extprops.auth_id = (char *) authid;
836 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
837 (void *) &extprops );
840 if ( sc != SASL_OK ) {
841 return LDAP_LOCAL_ERROR;
848 int ldap_pvt_sasl_secprops(
850 sasl_security_properties_t *secprops )
853 char **props = ldap_str2charray( in, "," );
856 sasl_ssf_t max_ssf = 0;
858 sasl_ssf_t min_ssf = 0;
860 unsigned maxbufsize = 0;
861 int got_maxbufsize = 0;
863 if( props == NULL || secprops == NULL ) {
864 return LDAP_PARAM_ERROR;
867 for( i=0; props[i]; i++ ) {
868 if( !strcasecmp(props[i], "none") ) {
871 } else if( !strcasecmp(props[i], "noplain") ) {
873 sflags |= SASL_SEC_NOPLAINTEXT;
875 } else if( !strcasecmp(props[i], "noactive") ) {
877 sflags |= SASL_SEC_NOACTIVE;
879 } else if( !strcasecmp(props[i], "nodict") ) {
881 sflags |= SASL_SEC_NODICTIONARY;
883 } else if( !strcasecmp(props[i], "forwardsec") ) {
885 sflags |= SASL_SEC_FORWARD_SECRECY;
887 } else if( !strcasecmp(props[i], "noanonymous")) {
889 sflags |= SASL_SEC_NOANONYMOUS;
891 } else if( !strcasecmp(props[i], "passcred") ) {
893 sflags |= SASL_SEC_PASS_CREDENTIALS;
895 } else if( !strncasecmp(props[i],
896 "minssf=", sizeof("minssf")) )
898 if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
900 min_ssf = atoi( &props[i][sizeof("minssf")] );
902 return LDAP_NOT_SUPPORTED;
905 } else if( !strncasecmp(props[i],
906 "maxssf=", sizeof("maxssf")) )
908 if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
910 max_ssf = atoi( &props[i][sizeof("maxssf")] );
912 return LDAP_NOT_SUPPORTED;
915 } else if( !strncasecmp(props[i],
916 "maxbufsize=", sizeof("maxbufsize")) )
918 if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
920 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
922 return LDAP_NOT_SUPPORTED;
925 if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
926 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
929 return LDAP_PARAM_ERROR;
933 return LDAP_NOT_SUPPORTED;
938 secprops->security_flags = sflags;
941 secprops->min_ssf = min_ssf;
944 secprops->max_ssf = max_ssf;
947 secprops->maxbufsize = maxbufsize;
950 ldap_charray_free( props );
955 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
960 case LDAP_OPT_X_SASL_SECPROPS:
961 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
962 if( rc == LDAP_SUCCESS ) return 0;
969 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
975 case LDAP_OPT_X_SASL_MECH: {
976 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
977 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
979 case LDAP_OPT_X_SASL_REALM: {
980 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
981 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
983 case LDAP_OPT_X_SASL_AUTHCID: {
984 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
985 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
987 case LDAP_OPT_X_SASL_AUTHZID: {
988 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
989 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
992 case LDAP_OPT_X_SASL_SSF: {
997 if( ld->ld_defconn == NULL ) {
1001 ctx = ld->ld_defconn->lconn_sasl_sockctx;
1003 if ( ctx == NULL ) {
1007 sc = sasl_getprop( ctx, SASL_SSF,
1008 (SASL_CONST void **) &ssf );
1010 if ( sc != SASL_OK ) {
1014 *(ber_len_t *)arg = *ssf;
1017 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1018 /* this option is write only */
1021 case LDAP_OPT_X_SASL_SSF_MIN:
1022 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1024 case LDAP_OPT_X_SASL_SSF_MAX:
1025 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1027 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1028 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1031 case LDAP_OPT_X_SASL_SECPROPS:
1032 /* this option is write only */
1042 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1048 case LDAP_OPT_X_SASL_SSF:
1049 /* This option is read-only */
1052 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1054 #if SASL_VERSION_MAJOR < 2
1055 sasl_external_properties_t extprops;
1059 if( ld->ld_defconn == NULL ) {
1063 ctx = ld->ld_defconn->lconn_sasl_authctx;
1065 if ( ctx == NULL ) {
1069 #if SASL_VERSION_MAJOR >= 2
1070 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1072 memset(&extprops, 0L, sizeof(extprops));
1074 extprops.ssf = * (ber_len_t *) arg;
1076 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1077 (void *) &extprops );
1080 if ( sc != SASL_OK ) {
1085 case LDAP_OPT_X_SASL_SSF_MIN:
1086 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1088 case LDAP_OPT_X_SASL_SSF_MAX:
1089 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1091 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1092 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1095 case LDAP_OPT_X_SASL_SECPROPS: {
1097 sc = ldap_pvt_sasl_secprops( (char *) arg,
1098 &ld->ld_options.ldo_sasl_secprops );
1100 return sc == LDAP_SUCCESS ? 0 : -1;
1109 #ifdef LDAP_R_COMPILE
1110 #define LDAP_DEBUG_R_SASL
1111 void *ldap_pvt_sasl_mutex_new(void)
1113 ldap_pvt_thread_mutex_t *mutex;
1115 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1116 sizeof(ldap_pvt_thread_mutex_t) );
1118 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1121 #ifndef LDAP_DEBUG_R_SASL
1123 #endif /* !LDAP_DEBUG_R_SASL */
1127 int ldap_pvt_sasl_mutex_lock(void *mutex)
1129 #ifdef LDAP_DEBUG_R_SASL
1130 if ( mutex == NULL ) {
1133 #else /* !LDAP_DEBUG_R_SASL */
1135 #endif /* !LDAP_DEBUG_R_SASL */
1136 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1137 ? SASL_FAIL : SASL_OK;
1140 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1142 #ifdef LDAP_DEBUG_R_SASL
1143 if ( mutex == NULL ) {
1146 #else /* !LDAP_DEBUG_R_SASL */
1148 #endif /* !LDAP_DEBUG_R_SASL */
1149 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1150 ? SASL_FAIL : SASL_OK;
1153 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1155 #ifdef LDAP_DEBUG_R_SASL
1156 if ( mutex == NULL ) {
1159 #else /* !LDAP_DEBUG_R_SASL */
1161 #endif /* !LDAP_DEBUG_R_SASL */
1162 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1168 int ldap_int_sasl_init( void )
1169 { return LDAP_SUCCESS; }
1171 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1172 { return LDAP_SUCCESS; }
1179 LDAPControl **sctrls,
1180 LDAPControl **cctrls,
1182 LDAP_SASL_INTERACT_PROC *interact,
1184 { return LDAP_NOT_SUPPORTED; }
1187 ldap_int_sasl_external(
1190 const char * authid,
1192 { return LDAP_SUCCESS; }
1194 #endif /* HAVE_CYRUS_SASL */