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;
449 char *saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb,
451 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
452 LDAP_FREE( saslhost );
455 if ( rc != LDAP_SUCCESS ) return rc;
457 ctx = ld->ld_defconn->lconn_sasl_authctx;
460 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
462 struct berval authid = BER_BVNULL;
465 fac = ldap_pvt_tls_get_strength( ssl );
466 /* failure is OK, we just can't use SASL EXTERNAL */
467 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
469 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
470 LDAP_FREE( authid.bv_val );
474 /* Check for local */
475 if ( ldap_pvt_url_scheme2proto(
476 ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC )
478 char authid[sizeof("gidNumber=4294967295+uidNumber=4294967295,"
479 "cn=peercred,cn=external,cn=auth")];
480 sprintf( authid, "gidNumber=%u+uidNumber=%u,"
481 "cn=peercred,cn=external,cn=auth",
482 getegid(), geteuid() );
483 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid,
484 LDAP_PVT_SASL_LOCAL_SSF );
488 /* (re)set security properties */
489 sasl_setprop( ctx, SASL_SEC_PROPS,
490 &ld->ld_options.ldo_sasl_secprops );
496 saslrc = sasl_client_start( ctx,
498 #if SASL_VERSION_MAJOR < 2
502 (SASL_CONST char **)&ccred.bv_val,
506 if( pmech == NULL && mech != NULL ) {
509 if( flags != LDAP_SASL_QUIET ) {
511 "SASL/%s authentication started\n",
516 if( saslrc == SASL_INTERACT ) {
518 if( !interact ) break;
519 res = (interact)( ld, flags, defaults, prompts );
521 if( res != LDAP_SUCCESS ) break;
523 } while ( saslrc == SASL_INTERACT );
525 ccred.bv_len = credlen;
527 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
528 rc = ld->ld_errno = sasl_err2ldap( saslrc );
529 #if SASL_VERSION_MAJOR >= 2
530 if ( ld->ld_error ) {
531 LDAP_FREE( ld->ld_error );
533 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
539 struct berval *scred;
544 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls,
547 if ( ccred.bv_val != NULL ) {
548 #if SASL_VERSION_MAJOR < 2
549 LDAP_FREE( ccred.bv_val );
554 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
556 /* and server provided us with data? */
557 Debug( LDAP_DEBUG_TRACE,
558 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
559 rc, saslrc, scred ? (long) scred->bv_len : -1L );
567 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
568 /* we're done, no need to step */
570 /* but we got additional data? */
571 #define KLUDGE_FOR_MSAD
572 #ifdef KLUDGE_FOR_MSAD
574 * MSAD provides empty additional data in violation of LDAP
575 * technical specifications. As no existing SASL mechanism
576 * allows empty data with an outcome message, just ignore it
577 * for now. Hopefully MS will fix their bug before someone
578 * defines a mechanism with possibly empty additional data.
580 if( scred->bv_len == 0 ) {
581 Debug( LDAP_DEBUG_ANY,
582 "ldap_int_sasl_bind: ignoring "
583 " bogus empty data provided with SASL outcome message.\n",
584 rc, saslrc, scred->bv_len );
589 Debug( LDAP_DEBUG_TRACE,
590 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
591 rc, saslrc, scred->bv_len );
592 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
603 Debug( LDAP_DEBUG_TRACE,
604 "ldap_int_sasl_bind: no data in step!\n",
608 saslrc = sasl_client_step( ctx,
609 (scred == NULL) ? NULL : scred->bv_val,
610 (scred == NULL) ? 0 : scred->bv_len,
612 (SASL_CONST char **)&ccred.bv_val,
615 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
618 if( saslrc == SASL_INTERACT ) {
620 if( !interact ) break;
621 res = (interact)( ld, flags, defaults, prompts );
622 if( res != LDAP_SUCCESS ) break;
624 } while ( saslrc == SASL_INTERACT );
626 ccred.bv_len = credlen;
629 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
630 ld->ld_errno = sasl_err2ldap( saslrc );
631 #if SASL_VERSION_MAJOR >= 2
632 if ( ld->ld_error ) {
633 LDAP_FREE( ld->ld_error );
635 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
640 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
642 if ( rc != LDAP_SUCCESS ) goto done;
644 if ( saslrc != SASL_OK ) {
645 #if SASL_VERSION_MAJOR >= 2
646 if ( ld->ld_error ) {
647 LDAP_FREE( ld->ld_error );
649 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
651 rc = ld->ld_errno = sasl_err2ldap( saslrc );
655 if( flags != LDAP_SASL_QUIET ) {
656 saslrc = sasl_getprop( ctx, SASL_USERNAME,
657 (SASL_CONST void **)(char *) &data );
658 if( saslrc == SASL_OK && data && *data ) {
659 fprintf( stderr, "SASL username: %s\n", data );
662 #if SASL_VERSION_MAJOR < 2
663 saslrc = sasl_getprop( ctx, SASL_REALM,
664 (SASL_CONST void **) &data );
665 if( saslrc == SASL_OK && data && *data ) {
666 fprintf( stderr, "SASL realm: %s\n", data );
671 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **)(char *) &ssf );
672 if( saslrc == SASL_OK ) {
673 if( flags != LDAP_SASL_QUIET ) {
674 fprintf( stderr, "SASL SSF: %lu\n",
675 (unsigned long) *ssf );
679 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
680 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
681 sasl_dispose( &oldctx );
682 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
684 ldap_pvt_sasl_install( ld->ld_defconn->lconn_sb, ctx );
685 ld->ld_defconn->lconn_sasl_sockctx = ctx;
687 if( flags != LDAP_SASL_QUIET ) {
688 fprintf( stderr, "SASL data security layer installed.\n" );
692 ld->ld_defconn->lconn_sasl_authctx = ctx;
699 ldap_int_sasl_external(
707 #if SASL_VERSION_MAJOR < 2
708 sasl_external_properties_t extprops;
710 sasl_ssf_t sasl_ssf = ssf;
713 ctx = conn->lconn_sasl_authctx;
716 return LDAP_LOCAL_ERROR;
719 #if SASL_VERSION_MAJOR >= 2
720 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
722 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
724 memset( &extprops, '\0', sizeof(extprops) );
726 extprops.auth_id = (char *) authid;
728 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
729 (void *) &extprops );
732 if ( sc != SASL_OK ) {
733 return LDAP_LOCAL_ERROR;
750 { BER_BVC("none"), 0, 0, 0 },
751 { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
752 { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
753 { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
754 { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
755 { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
756 { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
757 { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
758 { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
759 { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
760 { BER_BVNULL, 0, 0, 0 }
763 void ldap_pvt_sasl_secprops_unparse(
764 sasl_security_properties_t *secprops,
771 if ( secprops == NULL || out == NULL ) {
776 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
777 if ( sprops[i].ival ) {
780 switch( sprops[i].ival ) {
781 case GOT_MINSSF: v = secprops->min_ssf; break;
782 case GOT_MAXSSF: v = secprops->max_ssf; break;
783 case GOT_MAXBUF: v = secprops->maxbufsize; break;
785 /* It is the default, ignore it */
786 if ( v == sprops[i].idef ) continue;
788 l += sprops[i].key.bv_len + 24;
789 } else if ( sprops[i].sflag ) {
790 if ( sprops[i].sflag & secprops->security_flags ) {
791 l += sprops[i].key.bv_len;
793 } else if ( secprops->security_flags == 0 ) {
794 l += sprops[i].key.bv_len;
801 out->bv_val = LDAP_MALLOC( l );
802 if ( out->bv_val == NULL ) {
809 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
810 if ( sprops[i].ival ) {
813 switch( sprops[i].ival ) {
814 case GOT_MINSSF: v = secprops->min_ssf; break;
815 case GOT_MAXSSF: v = secprops->max_ssf; break;
816 case GOT_MAXBUF: v = secprops->maxbufsize; break;
818 /* It is the default, ignore it */
819 if ( v == sprops[i].idef ) continue;
821 if ( comma ) *ptr++ = ',';
822 ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
824 } else if ( sprops[i].sflag ) {
825 if ( sprops[i].sflag & secprops->security_flags ) {
826 if ( comma ) *ptr++ = ',';
827 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
830 } else if ( secprops->security_flags == 0 ) {
831 if ( comma ) *ptr++ = ',';
832 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
836 out->bv_len = ptr - out->bv_val;
839 int ldap_pvt_sasl_secprops(
841 sasl_security_properties_t *secprops )
847 sasl_ssf_t max_ssf = 0;
849 sasl_ssf_t min_ssf = 0;
851 unsigned maxbufsize = 0;
852 int got_maxbufsize = 0;
854 if( secprops == NULL ) {
855 return LDAP_PARAM_ERROR;
857 props = ldap_str2charray( in, "," );
858 if( props == NULL ) {
859 return LDAP_PARAM_ERROR;
862 for( i=0; props[i]; i++ ) {
863 l = strlen( props[i] );
864 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
865 if ( l < sprops[j].key.bv_len ) continue;
866 if ( strncasecmp( props[i], sprops[j].key.bv_val,
867 sprops[j].key.bv_len )) continue;
868 if ( sprops[j].ival ) {
871 if ( !isdigit( (unsigned char)props[i][sprops[j].key.bv_len] ))
873 v = strtoul( &props[i][sprops[j].key.bv_len], &next, 10 );
874 if ( next == &props[i][sprops[j].key.bv_len] || next[0] != '\0' ) continue;
875 switch( sprops[j].ival ) {
877 min_ssf = v; got_min_ssf++; break;
879 max_ssf = v; got_max_ssf++; break;
881 maxbufsize = v; got_maxbufsize++; break;
884 if ( props[i][sprops[j].key.bv_len] ) continue;
885 if ( sprops[j].sflag )
886 sflags |= sprops[j].sflag;
893 if ( BER_BVISNULL( &sprops[j].key )) {
894 ldap_charray_free( props );
895 return LDAP_NOT_SUPPORTED;
900 secprops->security_flags = sflags;
903 secprops->min_ssf = min_ssf;
906 secprops->max_ssf = max_ssf;
909 secprops->maxbufsize = maxbufsize;
912 ldap_charray_free( props );
917 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
922 case LDAP_OPT_X_SASL_SECPROPS:
923 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
924 if( rc == LDAP_SUCCESS ) return 0;
931 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
933 if ( option == LDAP_OPT_X_SASL_MECHLIST ) {
934 if ( ldap_int_sasl_init() )
936 *(char ***)arg = (char **)sasl_global_listmech();
944 case LDAP_OPT_X_SASL_MECH: {
945 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
946 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
948 case LDAP_OPT_X_SASL_REALM: {
949 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
950 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
952 case LDAP_OPT_X_SASL_AUTHCID: {
953 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
954 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
956 case LDAP_OPT_X_SASL_AUTHZID: {
957 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
958 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
961 case LDAP_OPT_X_SASL_SSF: {
966 if( ld->ld_defconn == NULL ) {
970 ctx = ld->ld_defconn->lconn_sasl_sockctx;
976 sc = sasl_getprop( ctx, SASL_SSF,
977 (SASL_CONST void **)(char *) &ssf );
979 if ( sc != SASL_OK ) {
983 *(ber_len_t *)arg = *ssf;
986 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
987 /* this option is write only */
990 case LDAP_OPT_X_SASL_SSF_MIN:
991 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
993 case LDAP_OPT_X_SASL_SSF_MAX:
994 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
996 case LDAP_OPT_X_SASL_MAXBUFSIZE:
997 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1000 case LDAP_OPT_X_SASL_SECPROPS:
1001 /* this option is write only */
1011 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1013 if ( ld == NULL || arg == NULL )
1017 case LDAP_OPT_X_SASL_SSF:
1018 /* This option is read-only */
1021 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1023 #if SASL_VERSION_MAJOR < 2
1024 sasl_external_properties_t extprops;
1026 sasl_ssf_t sasl_ssf;
1030 if( ld->ld_defconn == NULL ) {
1034 ctx = ld->ld_defconn->lconn_sasl_authctx;
1036 if ( ctx == NULL ) {
1040 #if SASL_VERSION_MAJOR >= 2
1041 sasl_ssf = * (ber_len_t *)arg;
1042 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf);
1044 memset(&extprops, 0L, sizeof(extprops));
1046 extprops.ssf = * (ber_len_t *) arg;
1048 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1049 (void *) &extprops );
1052 if ( sc != SASL_OK ) {
1057 case LDAP_OPT_X_SASL_SSF_MIN:
1058 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1060 case LDAP_OPT_X_SASL_SSF_MAX:
1061 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1063 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1064 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1067 case LDAP_OPT_X_SASL_SECPROPS: {
1069 sc = ldap_pvt_sasl_secprops( (char *) arg,
1070 &ld->ld_options.ldo_sasl_secprops );
1072 return sc == LDAP_SUCCESS ? 0 : -1;
1081 #ifdef LDAP_R_COMPILE
1082 #define LDAP_DEBUG_R_SASL
1083 void *ldap_pvt_sasl_mutex_new(void)
1085 ldap_pvt_thread_mutex_t *mutex;
1087 mutex = (ldap_pvt_thread_mutex_t *) LDAP_CALLOC( 1,
1088 sizeof(ldap_pvt_thread_mutex_t) );
1090 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1093 #ifndef LDAP_DEBUG_R_SASL
1095 #endif /* !LDAP_DEBUG_R_SASL */
1099 int ldap_pvt_sasl_mutex_lock(void *mutex)
1101 #ifdef LDAP_DEBUG_R_SASL
1102 if ( mutex == NULL ) {
1105 #else /* !LDAP_DEBUG_R_SASL */
1106 assert( mutex != NULL );
1107 #endif /* !LDAP_DEBUG_R_SASL */
1108 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1109 ? SASL_FAIL : SASL_OK;
1112 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1114 #ifdef LDAP_DEBUG_R_SASL
1115 if ( mutex == NULL ) {
1118 #else /* !LDAP_DEBUG_R_SASL */
1119 assert( mutex != NULL );
1120 #endif /* !LDAP_DEBUG_R_SASL */
1121 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1122 ? SASL_FAIL : SASL_OK;
1125 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1127 #ifdef LDAP_DEBUG_R_SASL
1128 if ( mutex == NULL ) {
1131 #else /* !LDAP_DEBUG_R_SASL */
1132 assert( mutex != NULL );
1133 #endif /* !LDAP_DEBUG_R_SASL */
1134 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1140 int ldap_int_sasl_init( void )
1141 { return LDAP_SUCCESS; }
1143 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1144 { return LDAP_SUCCESS; }
1151 LDAPControl **sctrls,
1152 LDAPControl **cctrls,
1154 LDAP_SASL_INTERACT_PROC *interact,
1156 { return LDAP_NOT_SUPPORTED; }
1159 ldap_int_sasl_external(
1162 const char * authid,
1164 { return LDAP_SUCCESS; }
1166 #endif /* HAVE_CYRUS_SASL */