2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2010 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 LDAP_MUTEX_LOCK( &ld->ld_req_mutex );
414 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
416 if ( sd == AC_SOCKET_INVALID ) {
417 /* not connected yet */
419 rc = ldap_open_defconn( ld );
422 ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb,
423 LBER_SB_OPT_GET_FD, &sd );
425 if( sd == AC_SOCKET_INVALID ) {
426 ld->ld_errno = LDAP_LOCAL_ERROR;
431 LDAP_MUTEX_UNLOCK( &ld->ld_req_mutex );
432 if( rc != 0 ) return ld->ld_errno;
434 oldctx = ld->ld_defconn->lconn_sasl_authctx;
436 /* If we already have an authentication context, clear it out */
438 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
439 sasl_dispose( &oldctx );
441 ld->ld_defconn->lconn_sasl_authctx = NULL;
446 int nocanon = (int)LDAP_BOOL_GET( &ld->ld_options,
447 LDAP_BOOL_SASL_NOCANON );
449 /* If we don't need to canonicalize just use the host
453 saslhost = ld->ld_defconn->lconn_server->lud_host;
455 saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb,
457 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
459 LDAP_FREE( saslhost );
462 if ( rc != LDAP_SUCCESS ) return rc;
464 ctx = ld->ld_defconn->lconn_sasl_authctx;
468 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
470 struct berval authid = BER_BVNULL;
473 fac = ldap_pvt_tls_get_strength( ssl );
474 /* failure is OK, we just can't use SASL EXTERNAL */
475 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
477 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
478 LDAP_FREE( authid.bv_val );
483 /* Check for local */
484 if ( ldap_pvt_url_scheme2proto(
485 ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC )
487 char authid[sizeof("gidNumber=4294967295+uidNumber=4294967295,"
488 "cn=peercred,cn=external,cn=auth")];
489 sprintf( authid, "gidNumber=%u+uidNumber=%u,"
490 "cn=peercred,cn=external,cn=auth",
491 getegid(), geteuid() );
492 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid,
493 LDAP_PVT_SASL_LOCAL_SSF );
497 /* (re)set security properties */
498 sasl_setprop( ctx, SASL_SEC_PROPS,
499 &ld->ld_options.ldo_sasl_secprops );
505 saslrc = sasl_client_start( ctx,
507 #if SASL_VERSION_MAJOR < 2
511 (SASL_CONST char **)&ccred.bv_val,
515 if( pmech == NULL && mech != NULL ) {
518 if( flags != LDAP_SASL_QUIET ) {
520 "SASL/%s authentication started\n",
525 if( saslrc == SASL_INTERACT ) {
527 if( !interact ) break;
528 res = (interact)( ld, flags, defaults, prompts );
530 if( res != LDAP_SUCCESS ) break;
532 } while ( saslrc == SASL_INTERACT );
534 ccred.bv_len = credlen;
536 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
537 rc = ld->ld_errno = sasl_err2ldap( saslrc );
538 #if SASL_VERSION_MAJOR >= 2
539 if ( ld->ld_error ) {
540 LDAP_FREE( ld->ld_error );
542 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
548 struct berval *scred;
553 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls,
556 if ( ccred.bv_val != NULL ) {
557 #if SASL_VERSION_MAJOR < 2
558 LDAP_FREE( ccred.bv_val );
563 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
565 /* and server provided us with data? */
566 Debug( LDAP_DEBUG_TRACE,
567 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
568 rc, saslrc, scred ? (long) scred->bv_len : -1L );
576 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
577 /* we're done, no need to step */
579 /* but we got additional data? */
580 #define KLUDGE_FOR_MSAD
581 #ifdef KLUDGE_FOR_MSAD
583 * MSAD provides empty additional data in violation of LDAP
584 * technical specifications. As no existing SASL mechanism
585 * allows empty data with an outcome message, just ignore it
586 * for now. Hopefully MS will fix their bug before someone
587 * defines a mechanism with possibly empty additional data.
589 if( scred->bv_len == 0 ) {
590 Debug( LDAP_DEBUG_ANY,
591 "ldap_int_sasl_bind: ignoring "
592 " bogus empty data provided with SASL outcome message.\n",
593 rc, saslrc, scred->bv_len );
598 Debug( LDAP_DEBUG_TRACE,
599 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
600 rc, saslrc, scred->bv_len );
601 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
612 Debug( LDAP_DEBUG_TRACE,
613 "ldap_int_sasl_bind: no data in step!\n",
617 saslrc = sasl_client_step( ctx,
618 (scred == NULL) ? NULL : scred->bv_val,
619 (scred == NULL) ? 0 : scred->bv_len,
621 (SASL_CONST char **)&ccred.bv_val,
624 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
627 if( saslrc == SASL_INTERACT ) {
629 if( !interact ) break;
630 res = (interact)( ld, flags, defaults, prompts );
631 if( res != LDAP_SUCCESS ) break;
633 } while ( saslrc == SASL_INTERACT );
635 ccred.bv_len = credlen;
638 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
639 ld->ld_errno = sasl_err2ldap( saslrc );
640 #if SASL_VERSION_MAJOR >= 2
641 if ( ld->ld_error ) {
642 LDAP_FREE( ld->ld_error );
644 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
649 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
651 if ( rc != LDAP_SUCCESS ) goto done;
653 if ( saslrc != SASL_OK ) {
654 #if SASL_VERSION_MAJOR >= 2
655 if ( ld->ld_error ) {
656 LDAP_FREE( ld->ld_error );
658 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
660 rc = ld->ld_errno = sasl_err2ldap( saslrc );
664 if( flags != LDAP_SASL_QUIET ) {
665 saslrc = sasl_getprop( ctx, SASL_USERNAME,
666 (SASL_CONST void **)(char *) &data );
667 if( saslrc == SASL_OK && data && *data ) {
668 fprintf( stderr, "SASL username: %s\n", data );
671 #if SASL_VERSION_MAJOR < 2
672 saslrc = sasl_getprop( ctx, SASL_REALM,
673 (SASL_CONST void **) &data );
674 if( saslrc == SASL_OK && data && *data ) {
675 fprintf( stderr, "SASL realm: %s\n", data );
680 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **)(char *) &ssf );
681 if( saslrc == SASL_OK ) {
682 if( flags != LDAP_SASL_QUIET ) {
683 fprintf( stderr, "SASL SSF: %lu\n",
684 (unsigned long) *ssf );
688 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
689 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
690 sasl_dispose( &oldctx );
691 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
693 ldap_pvt_sasl_install( ld->ld_defconn->lconn_sb, ctx );
694 ld->ld_defconn->lconn_sasl_sockctx = ctx;
696 if( flags != LDAP_SASL_QUIET ) {
697 fprintf( stderr, "SASL data security layer installed.\n" );
701 ld->ld_defconn->lconn_sasl_authctx = ctx;
708 ldap_int_sasl_external(
716 #if SASL_VERSION_MAJOR < 2
717 sasl_external_properties_t extprops;
719 sasl_ssf_t sasl_ssf = ssf;
722 ctx = conn->lconn_sasl_authctx;
725 return LDAP_LOCAL_ERROR;
728 #if SASL_VERSION_MAJOR >= 2
729 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
731 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
733 memset( &extprops, '\0', sizeof(extprops) );
735 extprops.auth_id = (char *) authid;
737 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
738 (void *) &extprops );
741 if ( sc != SASL_OK ) {
742 return LDAP_LOCAL_ERROR;
759 { BER_BVC("none"), 0, 0, 0 },
760 { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
761 { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
762 { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
763 { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
764 { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
765 { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
766 { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
767 { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
768 { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
769 { BER_BVNULL, 0, 0, 0 }
772 void ldap_pvt_sasl_secprops_unparse(
773 sasl_security_properties_t *secprops,
780 if ( secprops == NULL || out == NULL ) {
785 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
786 if ( sprops[i].ival ) {
789 switch( sprops[i].ival ) {
790 case GOT_MINSSF: v = secprops->min_ssf; break;
791 case GOT_MAXSSF: v = secprops->max_ssf; break;
792 case GOT_MAXBUF: v = secprops->maxbufsize; break;
794 /* It is the default, ignore it */
795 if ( v == sprops[i].idef ) continue;
797 l += sprops[i].key.bv_len + 24;
798 } else if ( sprops[i].sflag ) {
799 if ( sprops[i].sflag & secprops->security_flags ) {
800 l += sprops[i].key.bv_len;
802 } else if ( secprops->security_flags == 0 ) {
803 l += sprops[i].key.bv_len;
810 out->bv_val = LDAP_MALLOC( l );
811 if ( out->bv_val == NULL ) {
818 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
819 if ( sprops[i].ival ) {
822 switch( sprops[i].ival ) {
823 case GOT_MINSSF: v = secprops->min_ssf; break;
824 case GOT_MAXSSF: v = secprops->max_ssf; break;
825 case GOT_MAXBUF: v = secprops->maxbufsize; break;
827 /* It is the default, ignore it */
828 if ( v == sprops[i].idef ) continue;
830 if ( comma ) *ptr++ = ',';
831 ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
833 } else if ( sprops[i].sflag ) {
834 if ( sprops[i].sflag & secprops->security_flags ) {
835 if ( comma ) *ptr++ = ',';
836 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
839 } else if ( secprops->security_flags == 0 ) {
840 if ( comma ) *ptr++ = ',';
841 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
845 out->bv_len = ptr - out->bv_val;
848 int ldap_pvt_sasl_secprops(
850 sasl_security_properties_t *secprops )
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( secprops == NULL ) {
864 return LDAP_PARAM_ERROR;
866 props = ldap_str2charray( in, "," );
867 if( props == NULL ) {
868 return LDAP_PARAM_ERROR;
871 for( i=0; props[i]; i++ ) {
872 l = strlen( props[i] );
873 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
874 if ( l < sprops[j].key.bv_len ) continue;
875 if ( strncasecmp( props[i], sprops[j].key.bv_val,
876 sprops[j].key.bv_len )) continue;
877 if ( sprops[j].ival ) {
880 if ( !isdigit( (unsigned char)props[i][sprops[j].key.bv_len] ))
882 v = strtoul( &props[i][sprops[j].key.bv_len], &next, 10 );
883 if ( next == &props[i][sprops[j].key.bv_len] || next[0] != '\0' ) continue;
884 switch( sprops[j].ival ) {
886 min_ssf = v; got_min_ssf++; break;
888 max_ssf = v; got_max_ssf++; break;
890 maxbufsize = v; got_maxbufsize++; break;
893 if ( props[i][sprops[j].key.bv_len] ) continue;
894 if ( sprops[j].sflag )
895 sflags |= sprops[j].sflag;
902 if ( BER_BVISNULL( &sprops[j].key )) {
903 ldap_charray_free( props );
904 return LDAP_NOT_SUPPORTED;
909 secprops->security_flags = sflags;
912 secprops->min_ssf = min_ssf;
915 secprops->max_ssf = max_ssf;
918 secprops->maxbufsize = maxbufsize;
921 ldap_charray_free( props );
926 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
931 case LDAP_OPT_X_SASL_SECPROPS:
932 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
933 if( rc == LDAP_SUCCESS ) return 0;
940 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
942 if ( option == LDAP_OPT_X_SASL_MECHLIST ) {
943 if ( ldap_int_sasl_init() )
945 *(char ***)arg = (char **)sasl_global_listmech();
953 case LDAP_OPT_X_SASL_MECH: {
954 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
955 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
957 case LDAP_OPT_X_SASL_REALM: {
958 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
959 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
961 case LDAP_OPT_X_SASL_AUTHCID: {
962 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
963 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
965 case LDAP_OPT_X_SASL_AUTHZID: {
966 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
967 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
970 case LDAP_OPT_X_SASL_SSF: {
975 if( ld->ld_defconn == NULL ) {
979 ctx = ld->ld_defconn->lconn_sasl_sockctx;
985 sc = sasl_getprop( ctx, SASL_SSF,
986 (SASL_CONST void **)(char *) &ssf );
988 if ( sc != SASL_OK ) {
992 *(ber_len_t *)arg = *ssf;
995 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
996 /* this option is write only */
999 case LDAP_OPT_X_SASL_SSF_MIN:
1000 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1002 case LDAP_OPT_X_SASL_SSF_MAX:
1003 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1005 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1006 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1008 case LDAP_OPT_X_SASL_NOCANON:
1009 *(int *)arg = (int) LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1012 case LDAP_OPT_X_SASL_USERNAME: {
1017 if( ld->ld_defconn == NULL ) {
1021 ctx = ld->ld_defconn->lconn_sasl_authctx;
1023 if ( ctx == NULL ) {
1027 sc = sasl_getprop( ctx, SASL_USERNAME,
1028 (SASL_CONST void **)(char **) &username );
1030 if ( sc != SASL_OK ) {
1034 *(char **)arg = username ? LDAP_STRDUP( username ) : NULL;
1037 case LDAP_OPT_X_SASL_SECPROPS:
1038 /* this option is write only */
1041 #ifdef SASL_GSS_CREDS
1042 case LDAP_OPT_X_SASL_GSS_CREDS: {
1046 if ( ld->ld_defconn == NULL )
1049 ctx = ld->ld_defconn->lconn_sasl_authctx;
1053 sc = sasl_getprop( ctx, SASL_GSS_CREDS, arg );
1054 if ( sc != SASL_OK )
1067 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1072 if ( arg == NULL && option != LDAP_OPT_X_SASL_NOCANON )
1076 case LDAP_OPT_X_SASL_SSF:
1077 case LDAP_OPT_X_SASL_USERNAME:
1078 /* This option is read-only */
1081 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1083 #if SASL_VERSION_MAJOR < 2
1084 sasl_external_properties_t extprops;
1086 sasl_ssf_t sasl_ssf;
1090 if( ld->ld_defconn == NULL ) {
1094 ctx = ld->ld_defconn->lconn_sasl_authctx;
1096 if ( ctx == NULL ) {
1100 #if SASL_VERSION_MAJOR >= 2
1101 sasl_ssf = * (ber_len_t *)arg;
1102 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf);
1104 memset(&extprops, 0L, sizeof(extprops));
1106 extprops.ssf = * (ber_len_t *) arg;
1108 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1109 (void *) &extprops );
1112 if ( sc != SASL_OK ) {
1117 case LDAP_OPT_X_SASL_SSF_MIN:
1118 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1120 case LDAP_OPT_X_SASL_SSF_MAX:
1121 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1123 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1124 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1126 case LDAP_OPT_X_SASL_NOCANON:
1127 if ( arg == LDAP_OPT_OFF ) {
1128 LDAP_BOOL_CLR(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1130 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1134 case LDAP_OPT_X_SASL_SECPROPS: {
1136 sc = ldap_pvt_sasl_secprops( (char *) arg,
1137 &ld->ld_options.ldo_sasl_secprops );
1139 return sc == LDAP_SUCCESS ? 0 : -1;
1142 #ifdef SASL_GSS_CREDS
1143 case LDAP_OPT_X_SASL_GSS_CREDS: {
1147 if ( ld->ld_defconn == NULL )
1150 ctx = ld->ld_defconn->lconn_sasl_authctx;
1154 sc = sasl_setprop( ctx, SASL_GSS_CREDS, arg );
1155 if ( sc != SASL_OK )
1167 #ifdef LDAP_R_COMPILE
1168 #define LDAP_DEBUG_R_SASL
1169 void *ldap_pvt_sasl_mutex_new(void)
1171 ldap_pvt_thread_mutex_t *mutex;
1173 mutex = (ldap_pvt_thread_mutex_t *) LDAP_CALLOC( 1,
1174 sizeof(ldap_pvt_thread_mutex_t) );
1176 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1179 #ifndef LDAP_DEBUG_R_SASL
1181 #endif /* !LDAP_DEBUG_R_SASL */
1185 int ldap_pvt_sasl_mutex_lock(void *mutex)
1187 #ifdef LDAP_DEBUG_R_SASL
1188 if ( mutex == NULL ) {
1191 #else /* !LDAP_DEBUG_R_SASL */
1192 assert( mutex != NULL );
1193 #endif /* !LDAP_DEBUG_R_SASL */
1194 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1195 ? SASL_FAIL : SASL_OK;
1198 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1200 #ifdef LDAP_DEBUG_R_SASL
1201 if ( mutex == NULL ) {
1204 #else /* !LDAP_DEBUG_R_SASL */
1205 assert( mutex != NULL );
1206 #endif /* !LDAP_DEBUG_R_SASL */
1207 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1208 ? SASL_FAIL : SASL_OK;
1211 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1213 #ifdef LDAP_DEBUG_R_SASL
1214 if ( mutex == NULL ) {
1217 #else /* !LDAP_DEBUG_R_SASL */
1218 assert( mutex != NULL );
1219 #endif /* !LDAP_DEBUG_R_SASL */
1220 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1226 int ldap_int_sasl_init( void )
1227 { return LDAP_SUCCESS; }
1229 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1230 { return LDAP_SUCCESS; }
1237 LDAPControl **sctrls,
1238 LDAPControl **cctrls,
1240 LDAP_SASL_INTERACT_PROC *interact,
1242 { return LDAP_NOT_SUPPORTED; }
1245 ldap_int_sasl_external(
1248 const char * authid,
1250 { return LDAP_SUCCESS; }
1252 #endif /* HAVE_CYRUS_SASL */