3 * Copyright 1999-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
12 #include <ac/stdlib.h>
13 #include <ac/string.h>
20 #ifdef HAVE_CYRUS_SASL
23 ldap_pvt_thread_mutex_t ldap_int_sasl_mutex;
26 #ifdef HAVE_SASL_SASL_H
27 #include <sasl/sasl.h>
32 #if SASL_VERSION_MAJOR >= 2
33 #define SASL_CONST const
39 * Various Cyrus SASL related stuff.
42 int ldap_int_sasl_init( void )
44 /* XXX not threadsafe */
45 static int sasl_initialized = 0;
47 static sasl_callback_t client_callbacks[] = {
48 #ifdef SASL_CB_GETREALM
49 { SASL_CB_GETREALM, NULL, NULL },
51 { SASL_CB_USER, NULL, NULL },
52 { SASL_CB_AUTHNAME, NULL, NULL },
53 { SASL_CB_PASS, NULL, NULL },
54 { SASL_CB_ECHOPROMPT, NULL, NULL },
55 { SASL_CB_NOECHOPROMPT, NULL, NULL },
56 { SASL_CB_LIST_END, NULL, NULL }
59 #ifdef HAVE_SASL_VERSION
60 #define SASL_BUILD_VERSION ((SASL_VERSION_MAJOR << 24) |\
61 (SASL_VERSION_MINOR << 16) | SASL_VERSION_STEP)
64 sasl_version( NULL, &rc );
65 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
66 (rc & 0xffff) < SASL_VERSION_STEP) {
69 LDAP_LOG( TRANSPORT, INFO,
70 "ldap_int_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
71 rc, SASL_BUILD_VERSION, 0 );
73 Debug( LDAP_DEBUG_ANY,
74 "ldap_int_sasl_init: SASL version mismatch, got %x, wanted %x.\n",
75 rc, SASL_BUILD_VERSION, 0 );
81 if ( sasl_initialized ) {
91 #endif /* CSRIMALLOC */
95 ldap_pvt_sasl_mutex_new,
96 ldap_pvt_sasl_mutex_lock,
97 ldap_pvt_sasl_mutex_unlock,
98 ldap_pvt_sasl_mutex_dispose );
100 ldap_pvt_thread_mutex_init( &ldap_int_sasl_mutex );
103 if ( sasl_client_init( client_callbacks ) == SASL_OK ) {
104 sasl_initialized = 1;
112 * SASL encryption support for LBER Sockbufs
115 struct sb_sasl_data {
116 sasl_conn_t *sasl_context;
117 unsigned *sasl_maxbuf;
118 Sockbuf_Buf sec_buf_in;
124 sb_sasl_setup( Sockbuf_IO_Desc *sbiod, void *arg )
126 struct sb_sasl_data *p;
128 assert( sbiod != NULL );
130 p = LBER_MALLOC( sizeof( *p ) );
133 p->sasl_context = (sasl_conn_t *)arg;
134 ber_pvt_sb_buf_init( &p->sec_buf_in );
135 ber_pvt_sb_buf_init( &p->buf_in );
136 ber_pvt_sb_buf_init( &p->buf_out );
137 if ( ber_pvt_sb_grow_buffer( &p->sec_buf_in, SASL_MIN_BUFF_SIZE ) < 0 ) {
142 sasl_getprop( p->sasl_context, SASL_MAXOUTBUF,
143 (SASL_CONST void **) &p->sasl_maxbuf );
145 sbiod->sbiod_pvt = p;
151 sb_sasl_remove( Sockbuf_IO_Desc *sbiod )
153 struct sb_sasl_data *p;
155 assert( sbiod != NULL );
157 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
158 #if SASL_VERSION_MAJOR >= 2
160 * SASLv2 encode/decode buffers are managed by
161 * libsasl2. Ensure they are not freed by liblber.
163 p->buf_in.buf_base = NULL;
164 p->buf_out.buf_base = NULL;
166 ber_pvt_sb_buf_destroy( &p->sec_buf_in );
167 ber_pvt_sb_buf_destroy( &p->buf_in );
168 ber_pvt_sb_buf_destroy( &p->buf_out );
170 sbiod->sbiod_pvt = NULL;
175 sb_sasl_pkt_length( const unsigned char *buf, unsigned max, int debuglevel )
179 assert( buf != NULL );
186 if ( size > SASL_MAX_BUFF_SIZE ) {
187 /* somebody is trying to mess me up. */
188 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
189 "sb_sasl_pkt_length: received illegal packet length "
190 "of %lu bytes\n", (unsigned long)size );
191 size = 16; /* this should lead to an error. */
192 } else if ( size > max ) {
193 ber_log_printf( LDAP_DEBUG_ANY, debuglevel,
194 "sb_sasl_pkt_length: received packet length "
195 "of %lu exceeds negotiated max of %lu bytes\n", (unsigned long)size, (unsigned long)max );
198 return size + 4; /* include the size !!! */
201 /* Drop a processed packet from the input buffer */
203 sb_sasl_drop_packet ( Sockbuf_Buf *sec_buf_in, unsigned max, int debuglevel )
207 len = sec_buf_in->buf_ptr - sec_buf_in->buf_end;
209 AC_MEMCPY( sec_buf_in->buf_base, sec_buf_in->buf_base +
210 sec_buf_in->buf_end, len );
213 sec_buf_in->buf_end = sb_sasl_pkt_length( sec_buf_in->buf_base,
217 sec_buf_in->buf_end = 0;
219 sec_buf_in->buf_ptr = len;
223 sb_sasl_read( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
225 struct sb_sasl_data *p;
226 ber_slen_t ret, bufptr;
228 assert( sbiod != NULL );
229 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
231 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
233 /* Are there anything left in the buffer? */
234 ret = ber_pvt_sb_copy_out( &p->buf_in, buf, len );
241 #if SASL_VERSION_MAJOR >= 2
242 ber_pvt_sb_buf_init( &p->buf_in );
244 ber_pvt_sb_buf_destroy( &p->buf_in );
247 /* Read the length of the packet */
248 while ( p->sec_buf_in.buf_ptr < 4 ) {
249 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base,
250 4 - p->sec_buf_in.buf_ptr );
252 if ( ( ret < 0 ) && ( errno == EINTR ) )
258 p->sec_buf_in.buf_ptr += ret;
261 /* The new packet always starts at p->sec_buf_in.buf_base */
262 ret = sb_sasl_pkt_length( p->sec_buf_in.buf_base,
263 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
265 /* Grow the packet buffer if neccessary */
266 if ( ( p->sec_buf_in.buf_size < (ber_len_t) ret ) &&
267 ber_pvt_sb_grow_buffer( &p->sec_buf_in, ret ) < 0 )
272 p->sec_buf_in.buf_end = ret;
274 /* Did we read the whole encrypted packet? */
275 while ( p->sec_buf_in.buf_ptr < p->sec_buf_in.buf_end ) {
276 /* No, we have got only a part of it */
277 ret = p->sec_buf_in.buf_end - p->sec_buf_in.buf_ptr;
279 ret = LBER_SBIOD_READ_NEXT( sbiod, p->sec_buf_in.buf_base +
280 p->sec_buf_in.buf_ptr, ret );
282 if ( ( ret < 0 ) && ( errno == EINTR ) )
288 p->sec_buf_in.buf_ptr += ret;
291 /* Decode the packet */
292 ret = sasl_decode( p->sasl_context, p->sec_buf_in.buf_base,
293 p->sec_buf_in.buf_end,
294 (SASL_CONST char **)&p->buf_in.buf_base,
295 (unsigned *)&p->buf_in.buf_end );
297 /* Drop the packet from the input buffer */
298 sb_sasl_drop_packet( &p->sec_buf_in,
299 *p->sasl_maxbuf, sbiod->sbiod_sb->sb_debug );
301 if ( ret != SASL_OK ) {
302 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
303 "sb_sasl_read: failed to decode packet: %s\n",
304 sasl_errstring( ret, NULL, NULL ) );
309 p->buf_in.buf_size = p->buf_in.buf_end;
311 bufptr += ber_pvt_sb_copy_out( &p->buf_in, (char*) buf + bufptr, len );
317 sb_sasl_write( Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
319 struct sb_sasl_data *p;
322 assert( sbiod != NULL );
323 assert( SOCKBUF_VALID( sbiod->sbiod_sb ) );
325 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
327 /* Are there anything left in the buffer? */
328 if ( p->buf_out.buf_ptr != p->buf_out.buf_end ) {
329 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
334 /* now encode the next packet. */
335 #if SASL_VERSION_MAJOR >= 2
336 ber_pvt_sb_buf_init( &p->buf_out );
338 ber_pvt_sb_buf_destroy( &p->buf_out );
340 if ( len > *p->sasl_maxbuf - 100 )
341 len = *p->sasl_maxbuf - 100; /* For safety margin */
342 ret = sasl_encode( p->sasl_context, buf, len,
343 (SASL_CONST char **)&p->buf_out.buf_base,
344 (unsigned *)&p->buf_out.buf_size );
345 if ( ret != SASL_OK ) {
346 ber_log_printf( LDAP_DEBUG_ANY, sbiod->sbiod_sb->sb_debug,
347 "sb_sasl_write: failed to encode packet: %s\n",
348 sasl_errstring( ret, NULL, NULL ) );
351 p->buf_out.buf_end = p->buf_out.buf_size;
353 ret = ber_pvt_sb_do_write( sbiod, &p->buf_out );
360 sb_sasl_ctrl( Sockbuf_IO_Desc *sbiod, int opt, void *arg )
362 struct sb_sasl_data *p;
364 p = (struct sb_sasl_data *)sbiod->sbiod_pvt;
366 if ( opt == LBER_SB_OPT_DATA_READY ) {
367 if ( p->buf_in.buf_ptr != p->buf_in.buf_end )
371 return LBER_SBIOD_CTRL_NEXT( sbiod, opt, arg );
374 Sockbuf_IO ldap_pvt_sockbuf_io_sasl = {
375 sb_sasl_setup, /* sbi_setup */
376 sb_sasl_remove, /* sbi_remove */
377 sb_sasl_ctrl, /* sbi_ctrl */
378 sb_sasl_read, /* sbi_read */
379 sb_sasl_write, /* sbi_write */
383 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
386 LDAP_LOG ( TRANSPORT, ENTRY, "ldap_pvt_sasl_install\n", 0, 0, 0 );
388 Debug( LDAP_DEBUG_TRACE, "ldap_pvt_sasl_install\n",
392 /* don't install the stuff unless security has been negotiated */
394 if ( !ber_sockbuf_ctrl( sb, LBER_SB_OPT_HAS_IO,
395 &ldap_pvt_sockbuf_io_sasl ) )
398 ber_sockbuf_add_io( sb, &ber_sockbuf_io_debug,
399 LBER_SBIOD_LEVEL_APPLICATION, (void *)"sasl_" );
401 ber_sockbuf_add_io( sb, &ldap_pvt_sockbuf_io_sasl,
402 LBER_SBIOD_LEVEL_APPLICATION, ctx_arg );
409 sasl_err2ldap( int saslerr )
415 rc = LDAP_MORE_RESULTS_TO_RETURN;
418 rc = LDAP_LOCAL_ERROR;
424 rc = LDAP_LOCAL_ERROR;
430 rc = LDAP_AUTH_UNKNOWN;
433 rc = LDAP_AUTH_UNKNOWN;
436 rc = LDAP_PARAM_ERROR;
440 rc = LDAP_AUTH_UNKNOWN;
443 rc = LDAP_LOCAL_ERROR;
447 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
461 assert( lc->lconn_sasl_ctx == NULL );
463 if ( host == NULL ) {
464 ld->ld_errno = LDAP_LOCAL_ERROR;
468 #if SASL_VERSION_MAJOR >= 2
469 rc = sasl_client_new( "ldap", host, NULL, NULL,
472 rc = sasl_client_new( "ldap", host, NULL,
473 SASL_SECURITY_LAYER, &ctx );
476 if ( rc != SASL_OK ) {
477 ld->ld_errno = sasl_err2ldap( rc );
482 LDAP_LOG ( TRANSPORT, DETAIL1, "ldap_int_sasl_open: host=%s\n",
485 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
489 lc->lconn_sasl_ctx = ctx;
492 #if SASL_VERSION_MAJOR >= 2
493 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
496 sasl_external_properties_t extprops;
497 memset(&extprops, 0L, sizeof(extprops));
500 (void) sasl_setprop( ctx, SASL_SSF_EXTERNAL,
501 (void *) &extprops );
504 LDAP_LOG ( TRANSPORT, DETAIL1,
505 "ldap_int_sasl_open: ssf=%ld\n", (long) ssf, 0, 0 );
507 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: ssf=%ld\n",
515 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
517 sasl_conn_t *ctx = lc->lconn_sasl_ctx;
520 sasl_dispose( &ctx );
521 lc->lconn_sasl_ctx = NULL;
532 LDAPControl **sctrls,
533 LDAPControl **cctrls,
535 LDAP_SASL_INTERACT_PROC *interact,
539 const char *mech = NULL;
540 const char *pmech = NULL;
542 sasl_ssf_t *ssf = NULL;
544 sasl_interact_t *prompts = NULL;
550 LDAP_LOG ( TRANSPORT, ARGS, "ldap_int_sasl_bind: %s\n",
551 mechs ? mechs : "<null>", 0, 0 );
553 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
554 mechs ? mechs : "<null>", 0, 0 );
557 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
558 if (ld->ld_version < LDAP_VERSION3) {
559 ld->ld_errno = LDAP_NOT_SUPPORTED;
563 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
565 if ( sd == AC_SOCKET_INVALID ) {
566 /* not connected yet */
569 rc = ldap_open_defconn( ld );
570 if( rc < 0 ) return ld->ld_errno;
572 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
574 if( sd == AC_SOCKET_INVALID ) {
575 ld->ld_errno = LDAP_LOCAL_ERROR;
580 ctx = ld->ld_defconn->lconn_sasl_ctx;
583 ld->ld_errno = LDAP_LOCAL_ERROR;
587 /* (re)set security properties */
588 sasl_setprop( ctx, SASL_SEC_PROPS,
589 &ld->ld_options.ldo_sasl_secprops );
595 saslrc = sasl_client_start( ctx,
597 #if SASL_VERSION_MAJOR < 2
601 (SASL_CONST char **)&ccred.bv_val,
605 if( pmech == NULL && mech != NULL ) {
608 if( flags != LDAP_SASL_QUIET ) {
610 "SASL/%s authentication started\n",
615 #if SASL_VERSION_MAJOR >= 2
616 /* XXX the application should free interact results. */
617 if ( prompts != NULL && prompts->result != NULL ) {
618 LDAP_FREE( (void *)prompts->result );
619 prompts->result = NULL;
623 if( saslrc == SASL_INTERACT ) {
625 if( !interact ) break;
626 res = (interact)( ld, flags, defaults, prompts );
627 if( res != LDAP_SUCCESS ) {
631 } while ( saslrc == SASL_INTERACT );
633 ccred.bv_len = credlen;
635 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
636 ld->ld_errno = sasl_err2ldap( saslrc );
637 #if SASL_VERSION_MAJOR >= 2
638 ld->ld_error = (char *)sasl_errdetail( ctx );
644 struct berval *scred;
649 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls, &scred );
651 if ( ccred.bv_val != NULL ) {
652 #if SASL_VERSION_MAJOR < 2
653 LDAP_FREE( ccred.bv_val );
658 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
659 if( scred && scred->bv_len ) {
660 /* and server provided us with data? */
662 LDAP_LOG ( TRANSPORT, DETAIL1,
663 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
664 rc, saslrc, scred->bv_len );
666 Debug( LDAP_DEBUG_TRACE,
667 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
668 rc, saslrc, scred->bv_len );
675 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
676 /* we're done, no need to step */
677 if( scred && scred->bv_len ) {
678 /* but server provided us with data! */
680 LDAP_LOG ( TRANSPORT, DETAIL1,
681 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
682 rc, saslrc, scred->bv_len );
684 Debug( LDAP_DEBUG_TRACE,
685 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
686 rc, saslrc, scred->bv_len );
689 return ld->ld_errno = LDAP_LOCAL_ERROR;
695 saslrc = sasl_client_step( ctx,
696 (scred == NULL) ? NULL : scred->bv_val,
697 (scred == NULL) ? 0 : scred->bv_len,
699 (SASL_CONST char **)&ccred.bv_val,
703 LDAP_LOG ( TRANSPORT, DETAIL1,
704 "ldap_int_sasl_bind: sasl_client_step: %d\n", saslrc,0,0 );
706 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
710 #if SASL_VERSION_MAJOR >= 2
711 /* XXX the application should free interact results. */
712 if ( prompts != NULL && prompts->result != NULL ) {
713 LDAP_FREE( (void *)prompts->result );
714 prompts->result = NULL;
718 if( saslrc == SASL_INTERACT ) {
720 if( !interact ) break;
721 res = (interact)( ld, flags, defaults, prompts );
722 if( res != LDAP_SUCCESS ) {
726 } while ( saslrc == SASL_INTERACT );
728 ccred.bv_len = credlen;
731 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
732 ld->ld_errno = sasl_err2ldap( saslrc );
733 #if SASL_VERSION_MAJOR >= 2
734 ld->ld_error = (char *)sasl_errdetail( ctx );
738 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
740 if ( rc != LDAP_SUCCESS ) {
744 if ( saslrc != SASL_OK ) {
745 #if SASL_VERSION_MAJOR >= 2
746 ld->ld_error = (char *)sasl_errdetail( ctx );
748 return ld->ld_errno = sasl_err2ldap( saslrc );
751 if( flags != LDAP_SASL_QUIET ) {
752 saslrc = sasl_getprop( ctx, SASL_USERNAME, (SASL_CONST void **) &data );
753 if( saslrc == SASL_OK && data && *data ) {
754 fprintf( stderr, "SASL username: %s\n", data );
757 #if SASL_VERSION_MAJOR >= 2
758 saslrc = sasl_getprop( ctx, SASL_DEFUSERREALM, (SASL_CONST void **) &data );
760 saslrc = sasl_getprop( ctx, SASL_REALM, (SASL_CONST void **) &data );
762 if( saslrc == SASL_OK && data && *data ) {
763 fprintf( stderr, "SASL realm: %s\n", data );
767 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **) &ssf );
768 if( saslrc == SASL_OK ) {
769 if( flags != LDAP_SASL_QUIET ) {
770 fprintf( stderr, "SASL SSF: %lu\n",
771 (unsigned long) *ssf );
775 if( flags != LDAP_SASL_QUIET ) {
776 fprintf( stderr, "SASL installing layers\n" );
778 ldap_pvt_sasl_install( ld->ld_conns->lconn_sb, ctx );
786 ldap_int_sasl_external(
794 #if SASL_VERSION_MAJOR < 2
795 sasl_external_properties_t extprops;
798 ctx = conn->lconn_sasl_ctx;
801 return LDAP_LOCAL_ERROR;
804 #if SASL_VERSION_MAJOR >= 2
805 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
807 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
809 memset( &extprops, '\0', sizeof(extprops) );
811 extprops.auth_id = (char *) authid;
813 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
814 (void *) &extprops );
817 if ( sc != SASL_OK ) {
818 return LDAP_LOCAL_ERROR;
825 int ldap_pvt_sasl_secprops(
827 sasl_security_properties_t *secprops )
830 char **props = ldap_str2charray( in, "," );
833 sasl_ssf_t max_ssf = 0;
835 sasl_ssf_t min_ssf = 0;
837 unsigned maxbufsize = 0;
838 int got_maxbufsize = 0;
840 if( props == NULL || secprops == NULL ) {
841 return LDAP_PARAM_ERROR;
844 for( i=0; props[i]; i++ ) {
845 if( !strcasecmp(props[i], "none") ) {
848 } else if( !strcasecmp(props[i], "noplain") ) {
850 sflags |= SASL_SEC_NOPLAINTEXT;
852 } else if( !strcasecmp(props[i], "noactive") ) {
854 sflags |= SASL_SEC_NOACTIVE;
856 } else if( !strcasecmp(props[i], "nodict") ) {
858 sflags |= SASL_SEC_NODICTIONARY;
860 } else if( !strcasecmp(props[i], "forwardsec") ) {
862 sflags |= SASL_SEC_FORWARD_SECRECY;
864 } else if( !strcasecmp(props[i], "noanonymous")) {
866 sflags |= SASL_SEC_NOANONYMOUS;
868 } else if( !strcasecmp(props[i], "passcred") ) {
870 sflags |= SASL_SEC_PASS_CREDENTIALS;
872 } else if( !strncasecmp(props[i],
873 "minssf=", sizeof("minssf")) )
875 if( isdigit( (unsigned char) props[i][sizeof("minssf")] ) ) {
877 min_ssf = atoi( &props[i][sizeof("minssf")] );
879 return LDAP_NOT_SUPPORTED;
882 } else if( !strncasecmp(props[i],
883 "maxssf=", sizeof("maxssf")) )
885 if( isdigit( (unsigned char) props[i][sizeof("maxssf")] ) ) {
887 max_ssf = atoi( &props[i][sizeof("maxssf")] );
889 return LDAP_NOT_SUPPORTED;
892 } else if( !strncasecmp(props[i],
893 "maxbufsize=", sizeof("maxbufsize")) )
895 if( isdigit( (unsigned char) props[i][sizeof("maxbufsize")] ) ) {
897 maxbufsize = atoi( &props[i][sizeof("maxbufsize")] );
899 return LDAP_NOT_SUPPORTED;
902 if( maxbufsize && (( maxbufsize < SASL_MIN_BUFF_SIZE )
903 || (maxbufsize > SASL_MAX_BUFF_SIZE )))
906 return LDAP_PARAM_ERROR;
910 return LDAP_NOT_SUPPORTED;
915 secprops->security_flags = sflags;
918 secprops->min_ssf = min_ssf;
921 secprops->max_ssf = max_ssf;
924 secprops->maxbufsize = maxbufsize;
927 ldap_charray_free( props );
932 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
937 case LDAP_OPT_X_SASL_SECPROPS:
938 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
939 if( rc == LDAP_SUCCESS ) return 0;
946 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
952 case LDAP_OPT_X_SASL_MECH: {
953 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
954 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
956 case LDAP_OPT_X_SASL_REALM: {
957 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
958 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
960 case LDAP_OPT_X_SASL_AUTHCID: {
961 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
962 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
964 case LDAP_OPT_X_SASL_AUTHZID: {
965 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
966 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
969 case LDAP_OPT_X_SASL_SSF: {
974 if( ld->ld_defconn == NULL ) {
978 ctx = ld->ld_defconn->lconn_sasl_ctx;
984 sc = sasl_getprop( ctx, SASL_SSF,
985 (SASL_CONST void **) &ssf );
987 if ( sc != SASL_OK ) {
991 *(ber_len_t *)arg = *ssf;
994 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
995 /* this option is write only */
998 case LDAP_OPT_X_SASL_SSF_MIN:
999 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1001 case LDAP_OPT_X_SASL_SSF_MAX:
1002 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1004 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1005 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1008 case LDAP_OPT_X_SASL_SECPROPS:
1009 /* this option is write only */
1019 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1025 case LDAP_OPT_X_SASL_SSF:
1026 /* This option is read-only */
1029 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1031 #if SASL_VERSION_MAJOR < 2
1032 sasl_external_properties_t extprops;
1036 if( ld->ld_defconn == NULL ) {
1040 ctx = ld->ld_defconn->lconn_sasl_ctx;
1042 if ( ctx == NULL ) {
1046 #if SASL_VERSION_MAJOR >= 2
1047 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, arg);
1049 memset(&extprops, 0L, sizeof(extprops));
1051 extprops.ssf = * (ber_len_t *) arg;
1053 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1054 (void *) &extprops );
1057 if ( sc != SASL_OK ) {
1062 case LDAP_OPT_X_SASL_SSF_MIN:
1063 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1065 case LDAP_OPT_X_SASL_SSF_MAX:
1066 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1068 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1069 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1072 case LDAP_OPT_X_SASL_SECPROPS: {
1074 sc = ldap_pvt_sasl_secprops( (char *) arg,
1075 &ld->ld_options.ldo_sasl_secprops );
1077 return sc == LDAP_SUCCESS ? 0 : -1;
1086 #ifdef LDAP_R_COMPILE
1087 void *ldap_pvt_sasl_mutex_new(void)
1089 ldap_pvt_thread_mutex_t *mutex;
1091 mutex = (ldap_pvt_thread_mutex_t *) LDAP_MALLOC(
1092 sizeof(ldap_pvt_thread_mutex_t) );
1094 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1100 int ldap_pvt_sasl_mutex_lock(void *mutex)
1102 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1103 ? SASL_FAIL : SASL_OK;
1106 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1108 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1109 ? SASL_FAIL : SASL_OK;
1112 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1114 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1120 int ldap_int_sasl_init( void )
1121 { return LDAP_SUCCESS; }
1123 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1124 { return LDAP_SUCCESS; }
1131 LDAPControl **sctrls,
1132 LDAPControl **cctrls,
1134 LDAP_SASL_INTERACT_PROC *interact,
1136 { return LDAP_NOT_SUPPORTED; }
1139 ldap_int_sasl_external(
1142 const char * authid,
1144 { return LDAP_SUCCESS; }
1146 #endif /* HAVE_CYRUS_SASL */