2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 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>
34 #ifdef HAVE_CYRUS_SASL
41 #define INT_MAX 2147483647 /* 32 bit signed max */
45 ldap_pvt_thread_mutex_t ldap_int_sasl_mutex;
48 #ifdef HAVE_SASL_SASL_H
49 #include <sasl/sasl.h>
54 #if SASL_VERSION_MAJOR >= 2
55 #define SASL_CONST const
61 * Various Cyrus SASL related stuff.
64 static const sasl_callback_t client_callbacks[] = {
65 #ifdef SASL_CB_GETREALM
66 { SASL_CB_GETREALM, NULL, NULL },
68 { SASL_CB_USER, NULL, NULL },
69 { SASL_CB_AUTHNAME, NULL, NULL },
70 { SASL_CB_PASS, NULL, NULL },
71 { SASL_CB_ECHOPROMPT, NULL, NULL },
72 { SASL_CB_NOECHOPROMPT, NULL, NULL },
73 { SASL_CB_LIST_END, NULL, NULL }
76 int ldap_int_sasl_init( void )
78 /* XXX not threadsafe */
79 static int sasl_initialized = 0;
81 #ifdef HAVE_SASL_VERSION
82 /* stringify the version number, sasl.h doesn't do it for us */
83 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
84 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
85 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
88 sasl_version( NULL, &rc );
89 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
90 (rc & 0xffff) < SASL_VERSION_STEP) {
91 char version[sizeof("xxx.xxx.xxxxx")];
92 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
95 Debug( LDAP_DEBUG_ANY,
96 "ldap_int_sasl_init: SASL library version mismatch:"
97 " expected " SASL_VERSION_STRING ","
98 " got %s\n", version, 0, 0 );
103 if ( sasl_initialized ) {
107 /* SASL 2 takes care of its own memory completely internally */
108 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
114 #endif /* CSRIMALLOC */
116 #ifdef LDAP_R_COMPILE
118 ldap_pvt_sasl_mutex_new,
119 ldap_pvt_sasl_mutex_lock,
120 ldap_pvt_sasl_mutex_unlock,
121 ldap_pvt_sasl_mutex_dispose );
124 if ( sasl_client_init( NULL ) == SASL_OK ) {
125 sasl_initialized = 1;
129 #if SASL_VERSION_MAJOR < 2
130 /* A no-op to make sure we link with Cyrus 1.5 */
131 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
138 struct sb_sasl_generic_data *p,
143 sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
146 sasl_getprop( sasl_context, SASL_MAXOUTBUF,
147 (SASL_CONST void **)(char *) &maxbuf );
149 *min_send = SASL_MIN_BUFF_SIZE;
151 *max_recv = SASL_MAX_BUFF_SIZE;
155 sb_sasl_cyrus_encode(
156 struct sb_sasl_generic_data *p,
161 sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
163 unsigned tmpsize = dst->buf_size;
165 ret = sasl_encode( sasl_context, (char *)buf, len,
166 (SASL_CONST char **)&dst->buf_base,
169 dst->buf_size = tmpsize;
170 dst->buf_end = dst->buf_size;
172 if ( ret != SASL_OK ) {
173 ber_log_printf( LDAP_DEBUG_ANY, p->sbiod->sbiod_sb->sb_debug,
174 "sb_sasl_cyrus_encode: failed to encode packet: %s\n",
175 sasl_errstring( ret, NULL, NULL ) );
183 sb_sasl_cyrus_decode(
184 struct sb_sasl_generic_data *p,
185 const Sockbuf_Buf *src,
188 sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
190 unsigned tmpsize = dst->buf_size;
192 ret = sasl_decode( sasl_context,
193 src->buf_base, src->buf_end,
194 (SASL_CONST char **)&dst->buf_base,
195 (unsigned *)&tmpsize );
198 dst->buf_size = tmpsize;
199 dst->buf_end = dst->buf_size;
201 if ( ret != SASL_OK ) {
202 ber_log_printf( LDAP_DEBUG_ANY, p->sbiod->sbiod_sb->sb_debug,
203 "sb_sasl_cyrus_decode: failed to decode packet: %s\n",
204 sasl_errstring( ret, NULL, NULL ) );
212 sb_sasl_cyrus_reset_buf(
213 struct sb_sasl_generic_data *p,
216 #if SASL_VERSION_MAJOR >= 2
217 ber_pvt_sb_buf_init( buf );
219 ber_pvt_sb_buf_destroy( buf );
225 struct sb_sasl_generic_data *p)
227 #if SASL_VERSION_MAJOR >= 2
229 * SASLv2 encode/decode buffers are managed by
230 * libsasl2. Ensure they are not freed by liblber.
232 p->buf_in.buf_base = NULL;
233 p->buf_out.buf_base = NULL;
237 static const struct sb_sasl_generic_ops sb_sasl_cyrus_ops = {
239 sb_sasl_cyrus_encode,
240 sb_sasl_cyrus_decode,
241 sb_sasl_cyrus_reset_buf,
245 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
247 struct sb_sasl_generic_install install_arg;
249 install_arg.ops = &sb_sasl_cyrus_ops;
250 install_arg.ops_private = ctx_arg;
252 return ldap_pvt_sasl_generic_install( sb, &install_arg );
255 void ldap_pvt_sasl_remove( Sockbuf *sb )
257 ldap_pvt_sasl_generic_remove( sb );
261 sasl_err2ldap( int saslerr )
265 /* map SASL errors to LDAP API errors returned by:
267 * SASL_OK, SASL_NOMECH, SASL_NOMEM
268 * sasl_client_start()
269 * SASL_OK, SASL_NOMECH, SASL_NOMEM, SASL_INTERACT
271 * SASL_OK, SASL_INTERACT, SASL_BADPROT, SASL_BADSERV
276 rc = LDAP_MORE_RESULTS_TO_RETURN;
279 rc = LDAP_LOCAL_ERROR;
288 rc = LDAP_AUTH_UNKNOWN;
291 rc = LDAP_DECODING_ERROR;
294 rc = LDAP_AUTH_UNKNOWN;
299 rc = LDAP_AUTH_UNKNOWN;
302 rc = LDAP_PARAM_ERROR;
305 rc = LDAP_LOCAL_ERROR;
309 rc = LDAP_AUTH_UNKNOWN;
312 rc = LDAP_LOCAL_ERROR;
316 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
329 assert( lc->lconn_sasl_authctx == NULL );
331 if ( host == NULL ) {
332 ld->ld_errno = LDAP_LOCAL_ERROR;
336 if ( ldap_int_sasl_init() ) {
337 ld->ld_errno = LDAP_LOCAL_ERROR;
341 #if SASL_VERSION_MAJOR >= 2
342 rc = sasl_client_new( "ldap", host, NULL, NULL,
343 client_callbacks, 0, &ctx );
345 rc = sasl_client_new( "ldap", host, client_callbacks,
346 SASL_SECURITY_LAYER, &ctx );
349 if ( rc != SASL_OK ) {
350 ld->ld_errno = sasl_err2ldap( rc );
354 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
357 lc->lconn_sasl_authctx = ctx;
362 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
364 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
367 sasl_dispose( &ctx );
368 if ( lc->lconn_sasl_sockctx &&
369 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
370 ctx = lc->lconn_sasl_sockctx;
371 sasl_dispose( &ctx );
373 lc->lconn_sasl_sockctx = NULL;
374 lc->lconn_sasl_authctx = NULL;
385 LDAPControl **sctrls,
386 LDAPControl **cctrls,
388 LDAP_SASL_INTERACT_PROC *interact,
392 const char *mech = NULL;
393 const char *pmech = NULL;
395 sasl_ssf_t *ssf = NULL;
396 sasl_conn_t *ctx, *oldctx = NULL;
397 sasl_interact_t *prompts = NULL;
403 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
404 mechs ? mechs : "<null>", 0, 0 );
406 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
407 if (ld->ld_version < LDAP_VERSION3) {
408 ld->ld_errno = LDAP_NOT_SUPPORTED;
413 #ifdef LDAP_R_COMPILE
414 ldap_pvt_thread_mutex_lock( &ld->ld_req_mutex );
416 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
418 if ( sd == AC_SOCKET_INVALID ) {
419 /* not connected yet */
421 rc = ldap_open_defconn( ld );
424 ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb,
425 LBER_SB_OPT_GET_FD, &sd );
427 if( sd == AC_SOCKET_INVALID ) {
428 ld->ld_errno = LDAP_LOCAL_ERROR;
433 #ifdef LDAP_R_COMPILE
434 ldap_pvt_thread_mutex_unlock( &ld->ld_req_mutex );
436 if( rc != 0 ) return ld->ld_errno;
438 oldctx = ld->ld_defconn->lconn_sasl_authctx;
440 /* If we already have an authentication context, clear it out */
442 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
443 sasl_dispose( &oldctx );
445 ld->ld_defconn->lconn_sasl_authctx = NULL;
450 int nocanon = (int)LDAP_BOOL_GET( &ld->ld_options,
451 LDAP_BOOL_SASL_NOCANON );
453 /* If we don't need to canonicalize just use the host
457 saslhost = ld->ld_defconn->lconn_server->lud_host;
459 saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb,
461 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
463 LDAP_FREE( saslhost );
466 if ( rc != LDAP_SUCCESS ) return rc;
468 ctx = ld->ld_defconn->lconn_sasl_authctx;
471 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
473 struct berval authid = BER_BVNULL;
476 fac = ldap_pvt_tls_get_strength( ssl );
477 /* failure is OK, we just can't use SASL EXTERNAL */
478 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
480 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
481 LDAP_FREE( authid.bv_val );
485 /* Check for local */
486 if ( ldap_pvt_url_scheme2proto(
487 ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC )
489 char authid[sizeof("gidNumber=4294967295+uidNumber=4294967295,"
490 "cn=peercred,cn=external,cn=auth")];
491 sprintf( authid, "gidNumber=%u+uidNumber=%u,"
492 "cn=peercred,cn=external,cn=auth",
493 getegid(), geteuid() );
494 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid,
495 LDAP_PVT_SASL_LOCAL_SSF );
499 /* (re)set security properties */
500 sasl_setprop( ctx, SASL_SEC_PROPS,
501 &ld->ld_options.ldo_sasl_secprops );
507 saslrc = sasl_client_start( ctx,
509 #if SASL_VERSION_MAJOR < 2
513 (SASL_CONST char **)&ccred.bv_val,
517 if( pmech == NULL && mech != NULL ) {
520 if( flags != LDAP_SASL_QUIET ) {
522 "SASL/%s authentication started\n",
527 if( saslrc == SASL_INTERACT ) {
529 if( !interact ) break;
530 res = (interact)( ld, flags, defaults, prompts );
532 if( res != LDAP_SUCCESS ) break;
534 } while ( saslrc == SASL_INTERACT );
536 ccred.bv_len = credlen;
538 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
539 rc = ld->ld_errno = sasl_err2ldap( saslrc );
540 #if SASL_VERSION_MAJOR >= 2
541 if ( ld->ld_error ) {
542 LDAP_FREE( ld->ld_error );
544 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
550 struct berval *scred;
555 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls,
558 if ( ccred.bv_val != NULL ) {
559 #if SASL_VERSION_MAJOR < 2
560 LDAP_FREE( ccred.bv_val );
565 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
567 /* and server provided us with data? */
568 Debug( LDAP_DEBUG_TRACE,
569 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
570 rc, saslrc, scred ? (long) scred->bv_len : -1L );
578 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
579 /* we're done, no need to step */
581 /* but we got additional data? */
582 #define KLUDGE_FOR_MSAD
583 #ifdef KLUDGE_FOR_MSAD
585 * MSAD provides empty additional data in violation of LDAP
586 * technical specifications. As no existing SASL mechanism
587 * allows empty data with an outcome message, just ignore it
588 * for now. Hopefully MS will fix their bug before someone
589 * defines a mechanism with possibly empty additional data.
591 if( scred->bv_len == 0 ) {
592 Debug( LDAP_DEBUG_ANY,
593 "ldap_int_sasl_bind: ignoring "
594 " bogus empty data provided with SASL outcome message.\n",
595 rc, saslrc, scred->bv_len );
600 Debug( LDAP_DEBUG_TRACE,
601 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
602 rc, saslrc, scred->bv_len );
603 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
614 Debug( LDAP_DEBUG_TRACE,
615 "ldap_int_sasl_bind: no data in step!\n",
619 saslrc = sasl_client_step( ctx,
620 (scred == NULL) ? NULL : scred->bv_val,
621 (scred == NULL) ? 0 : scred->bv_len,
623 (SASL_CONST char **)&ccred.bv_val,
626 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
629 if( saslrc == SASL_INTERACT ) {
631 if( !interact ) break;
632 res = (interact)( ld, flags, defaults, prompts );
633 if( res != LDAP_SUCCESS ) break;
635 } while ( saslrc == SASL_INTERACT );
637 ccred.bv_len = credlen;
640 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
641 ld->ld_errno = sasl_err2ldap( saslrc );
642 #if SASL_VERSION_MAJOR >= 2
643 if ( ld->ld_error ) {
644 LDAP_FREE( ld->ld_error );
646 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
651 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
653 if ( rc != LDAP_SUCCESS ) goto done;
655 if ( saslrc != SASL_OK ) {
656 #if SASL_VERSION_MAJOR >= 2
657 if ( ld->ld_error ) {
658 LDAP_FREE( ld->ld_error );
660 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
662 rc = ld->ld_errno = sasl_err2ldap( saslrc );
666 if( flags != LDAP_SASL_QUIET ) {
667 saslrc = sasl_getprop( ctx, SASL_USERNAME,
668 (SASL_CONST void **)(char *) &data );
669 if( saslrc == SASL_OK && data && *data ) {
670 fprintf( stderr, "SASL username: %s\n", data );
673 #if SASL_VERSION_MAJOR < 2
674 saslrc = sasl_getprop( ctx, SASL_REALM,
675 (SASL_CONST void **) &data );
676 if( saslrc == SASL_OK && data && *data ) {
677 fprintf( stderr, "SASL realm: %s\n", data );
682 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **)(char *) &ssf );
683 if( saslrc == SASL_OK ) {
684 if( flags != LDAP_SASL_QUIET ) {
685 fprintf( stderr, "SASL SSF: %lu\n",
686 (unsigned long) *ssf );
690 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
691 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
692 sasl_dispose( &oldctx );
693 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
695 ldap_pvt_sasl_install( ld->ld_defconn->lconn_sb, ctx );
696 ld->ld_defconn->lconn_sasl_sockctx = ctx;
698 if( flags != LDAP_SASL_QUIET ) {
699 fprintf( stderr, "SASL data security layer installed.\n" );
703 ld->ld_defconn->lconn_sasl_authctx = ctx;
710 ldap_int_sasl_external(
718 #if SASL_VERSION_MAJOR < 2
719 sasl_external_properties_t extprops;
721 sasl_ssf_t sasl_ssf = ssf;
724 ctx = conn->lconn_sasl_authctx;
727 return LDAP_LOCAL_ERROR;
730 #if SASL_VERSION_MAJOR >= 2
731 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
733 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
735 memset( &extprops, '\0', sizeof(extprops) );
737 extprops.auth_id = (char *) authid;
739 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
740 (void *) &extprops );
743 if ( sc != SASL_OK ) {
744 return LDAP_LOCAL_ERROR;
761 { BER_BVC("none"), 0, 0, 0 },
762 { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
763 { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
764 { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
765 { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
766 { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
767 { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
768 { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
769 { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
770 { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
771 { BER_BVNULL, 0, 0, 0 }
774 void ldap_pvt_sasl_secprops_unparse(
775 sasl_security_properties_t *secprops,
782 if ( secprops == NULL || out == NULL ) {
787 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
788 if ( sprops[i].ival ) {
791 switch( sprops[i].ival ) {
792 case GOT_MINSSF: v = secprops->min_ssf; break;
793 case GOT_MAXSSF: v = secprops->max_ssf; break;
794 case GOT_MAXBUF: v = secprops->maxbufsize; break;
796 /* It is the default, ignore it */
797 if ( v == sprops[i].idef ) continue;
799 l += sprops[i].key.bv_len + 24;
800 } else if ( sprops[i].sflag ) {
801 if ( sprops[i].sflag & secprops->security_flags ) {
802 l += sprops[i].key.bv_len;
804 } else if ( secprops->security_flags == 0 ) {
805 l += sprops[i].key.bv_len;
812 out->bv_val = LDAP_MALLOC( l );
813 if ( out->bv_val == NULL ) {
820 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
821 if ( sprops[i].ival ) {
824 switch( sprops[i].ival ) {
825 case GOT_MINSSF: v = secprops->min_ssf; break;
826 case GOT_MAXSSF: v = secprops->max_ssf; break;
827 case GOT_MAXBUF: v = secprops->maxbufsize; break;
829 /* It is the default, ignore it */
830 if ( v == sprops[i].idef ) continue;
832 if ( comma ) *ptr++ = ',';
833 ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
835 } else if ( sprops[i].sflag ) {
836 if ( sprops[i].sflag & secprops->security_flags ) {
837 if ( comma ) *ptr++ = ',';
838 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
841 } else if ( secprops->security_flags == 0 ) {
842 if ( comma ) *ptr++ = ',';
843 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
847 out->bv_len = ptr - out->bv_val;
850 int ldap_pvt_sasl_secprops(
852 sasl_security_properties_t *secprops )
858 sasl_ssf_t max_ssf = 0;
860 sasl_ssf_t min_ssf = 0;
862 unsigned maxbufsize = 0;
863 int got_maxbufsize = 0;
865 if( secprops == NULL ) {
866 return LDAP_PARAM_ERROR;
868 props = ldap_str2charray( in, "," );
869 if( props == NULL ) {
870 return LDAP_PARAM_ERROR;
873 for( i=0; props[i]; i++ ) {
874 l = strlen( props[i] );
875 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
876 if ( l < sprops[j].key.bv_len ) continue;
877 if ( strncasecmp( props[i], sprops[j].key.bv_val,
878 sprops[j].key.bv_len )) continue;
879 if ( sprops[j].ival ) {
882 if ( !isdigit( (unsigned char)props[i][sprops[j].key.bv_len] ))
884 v = strtoul( &props[i][sprops[j].key.bv_len], &next, 10 );
885 if ( next == &props[i][sprops[j].key.bv_len] || next[0] != '\0' ) continue;
886 switch( sprops[j].ival ) {
888 min_ssf = v; got_min_ssf++; break;
890 max_ssf = v; got_max_ssf++; break;
892 maxbufsize = v; got_maxbufsize++; break;
895 if ( props[i][sprops[j].key.bv_len] ) continue;
896 if ( sprops[j].sflag )
897 sflags |= sprops[j].sflag;
904 if ( BER_BVISNULL( &sprops[j].key )) {
905 ldap_charray_free( props );
906 return LDAP_NOT_SUPPORTED;
911 secprops->security_flags = sflags;
914 secprops->min_ssf = min_ssf;
917 secprops->max_ssf = max_ssf;
920 secprops->maxbufsize = maxbufsize;
923 ldap_charray_free( props );
928 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
933 case LDAP_OPT_X_SASL_SECPROPS:
934 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
935 if( rc == LDAP_SUCCESS ) return 0;
942 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
944 if ( option == LDAP_OPT_X_SASL_MECHLIST ) {
945 if ( ldap_int_sasl_init() )
947 *(char ***)arg = (char **)sasl_global_listmech();
955 case LDAP_OPT_X_SASL_MECH: {
956 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
957 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
959 case LDAP_OPT_X_SASL_REALM: {
960 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
961 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
963 case LDAP_OPT_X_SASL_AUTHCID: {
964 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
965 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
967 case LDAP_OPT_X_SASL_AUTHZID: {
968 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
969 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
972 case LDAP_OPT_X_SASL_SSF: {
977 if( ld->ld_defconn == NULL ) {
981 ctx = ld->ld_defconn->lconn_sasl_sockctx;
987 sc = sasl_getprop( ctx, SASL_SSF,
988 (SASL_CONST void **)(char *) &ssf );
990 if ( sc != SASL_OK ) {
994 *(ber_len_t *)arg = *ssf;
997 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
998 /* this option is write only */
1001 case LDAP_OPT_X_SASL_SSF_MIN:
1002 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1004 case LDAP_OPT_X_SASL_SSF_MAX:
1005 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1007 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1008 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1010 case LDAP_OPT_X_SASL_NOCANON:
1011 *(int *)arg = (int) LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1014 case LDAP_OPT_X_SASL_SECPROPS:
1015 /* this option is write only */
1025 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1030 if ( arg == NULL && option != LDAP_OPT_X_SASL_NOCANON )
1034 case LDAP_OPT_X_SASL_SSF:
1035 /* This option is read-only */
1038 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1040 #if SASL_VERSION_MAJOR < 2
1041 sasl_external_properties_t extprops;
1043 sasl_ssf_t sasl_ssf;
1047 if( ld->ld_defconn == NULL ) {
1051 ctx = ld->ld_defconn->lconn_sasl_authctx;
1053 if ( ctx == NULL ) {
1057 #if SASL_VERSION_MAJOR >= 2
1058 sasl_ssf = * (ber_len_t *)arg;
1059 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf);
1061 memset(&extprops, 0L, sizeof(extprops));
1063 extprops.ssf = * (ber_len_t *) arg;
1065 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1066 (void *) &extprops );
1069 if ( sc != SASL_OK ) {
1074 case LDAP_OPT_X_SASL_SSF_MIN:
1075 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1077 case LDAP_OPT_X_SASL_SSF_MAX:
1078 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1080 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1081 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1083 case LDAP_OPT_X_SASL_NOCANON:
1084 if ( arg == LDAP_OPT_OFF ) {
1085 LDAP_BOOL_CLR(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1087 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1091 case LDAP_OPT_X_SASL_SECPROPS: {
1093 sc = ldap_pvt_sasl_secprops( (char *) arg,
1094 &ld->ld_options.ldo_sasl_secprops );
1096 return sc == LDAP_SUCCESS ? 0 : -1;
1105 #ifdef LDAP_R_COMPILE
1106 #define LDAP_DEBUG_R_SASL
1107 void *ldap_pvt_sasl_mutex_new(void)
1109 ldap_pvt_thread_mutex_t *mutex;
1111 mutex = (ldap_pvt_thread_mutex_t *) LDAP_CALLOC( 1,
1112 sizeof(ldap_pvt_thread_mutex_t) );
1114 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1117 #ifndef LDAP_DEBUG_R_SASL
1119 #endif /* !LDAP_DEBUG_R_SASL */
1123 int ldap_pvt_sasl_mutex_lock(void *mutex)
1125 #ifdef LDAP_DEBUG_R_SASL
1126 if ( mutex == NULL ) {
1129 #else /* !LDAP_DEBUG_R_SASL */
1130 assert( mutex != NULL );
1131 #endif /* !LDAP_DEBUG_R_SASL */
1132 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1133 ? SASL_FAIL : SASL_OK;
1136 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1138 #ifdef LDAP_DEBUG_R_SASL
1139 if ( mutex == NULL ) {
1142 #else /* !LDAP_DEBUG_R_SASL */
1143 assert( mutex != NULL );
1144 #endif /* !LDAP_DEBUG_R_SASL */
1145 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1146 ? SASL_FAIL : SASL_OK;
1149 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1151 #ifdef LDAP_DEBUG_R_SASL
1152 if ( mutex == NULL ) {
1155 #else /* !LDAP_DEBUG_R_SASL */
1156 assert( mutex != NULL );
1157 #endif /* !LDAP_DEBUG_R_SASL */
1158 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1164 int ldap_int_sasl_init( void )
1165 { return LDAP_SUCCESS; }
1167 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1168 { return LDAP_SUCCESS; }
1175 LDAPControl **sctrls,
1176 LDAPControl **cctrls,
1178 LDAP_SASL_INTERACT_PROC *interact,
1180 { return LDAP_NOT_SUPPORTED; }
1183 ldap_int_sasl_external(
1186 const char * authid,
1188 { return LDAP_SUCCESS; }
1190 #endif /* HAVE_CYRUS_SASL */