2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2012 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,
397 sasl_interact_t *prompts = NULL;
398 struct berval ccred = BER_BVNULL;
402 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
403 mechs ? mechs : "<null>", 0, 0 );
405 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
406 if (ld->ld_version < LDAP_VERSION3) {
407 ld->ld_errno = LDAP_NOT_SUPPORTED;
411 /* Starting a Bind */
413 const char *pmech = NULL;
419 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
420 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
422 if ( sd == AC_SOCKET_INVALID || !ld->ld_defconn ) {
423 /* not connected yet */
425 rc = ldap_open_defconn( ld );
428 ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb,
429 LBER_SB_OPT_GET_FD, &sd );
431 if( sd == AC_SOCKET_INVALID ) {
432 ld->ld_errno = LDAP_LOCAL_ERROR;
437 if ( rc == 0 && ld->ld_defconn &&
438 ld->ld_defconn->lconn_status == LDAP_CONNST_CONNECTING ) {
439 rc = ldap_int_check_async_open( ld, sd );
441 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
442 if( rc != 0 ) return ld->ld_errno;
444 oldctx = ld->ld_defconn->lconn_sasl_authctx;
446 /* If we already have an authentication context, clear it out */
448 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
449 sasl_dispose( &oldctx );
451 ld->ld_defconn->lconn_sasl_authctx = NULL;
456 int nocanon = (int)LDAP_BOOL_GET( &ld->ld_options,
457 LDAP_BOOL_SASL_NOCANON );
459 /* If we don't need to canonicalize just use the host
463 saslhost = ld->ld_defconn->lconn_server->lud_host;
465 saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb,
467 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
469 LDAP_FREE( saslhost );
472 if ( rc != LDAP_SUCCESS ) return rc;
474 ctx = ld->ld_defconn->lconn_sasl_authctx;
478 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
480 struct berval authid = BER_BVNULL;
483 fac = ldap_pvt_tls_get_strength( ssl );
484 /* failure is OK, we just can't use SASL EXTERNAL */
485 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
487 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
488 LDAP_FREE( authid.bv_val );
493 /* Check for local */
494 if ( ldap_pvt_url_scheme2proto(
495 ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC )
497 char authid[sizeof("gidNumber=4294967295+uidNumber=4294967295,"
498 "cn=peercred,cn=external,cn=auth")];
499 sprintf( authid, "gidNumber=%u+uidNumber=%u,"
500 "cn=peercred,cn=external,cn=auth",
501 getegid(), geteuid() );
502 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid,
503 LDAP_PVT_SASL_LOCAL_SSF );
507 /* (re)set security properties */
508 sasl_setprop( ctx, SASL_SEC_PROPS,
509 &ld->ld_options.ldo_sasl_secprops );
514 saslrc = sasl_client_start( ctx,
516 #if SASL_VERSION_MAJOR < 2
520 (SASL_CONST char **)&ccred.bv_val,
524 if( pmech == NULL && mech != NULL ) {
528 if( flags != LDAP_SASL_QUIET ) {
530 "SASL/%s authentication started\n",
535 if( saslrc == SASL_INTERACT ) {
537 if( !interact ) break;
538 res = (interact)( ld, flags, defaults, prompts );
540 if( res != LDAP_SUCCESS ) break;
542 } while ( saslrc == SASL_INTERACT );
543 rc = LDAP_SASL_BIND_IN_PROGRESS;
546 /* continuing an in-progress Bind */
547 struct berval *scred = NULL;
549 ctx = ld->ld_defconn->lconn_sasl_authctx;
551 rc = ldap_parse_sasl_bind_result( ld, result, &scred, 0 );
552 if ( rc != LDAP_SUCCESS )
555 rc = ldap_result2error( ld, result, 0 );
556 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
558 /* and server provided us with data? */
559 Debug( LDAP_DEBUG_TRACE,
560 "ldap_int_sasl_bind: rc=%d len=%ld\n",
561 rc, scred ? (long) scred->bv_len : -1L, 0 );
569 if ( rc == LDAP_SUCCESS && mech == NULL )
575 Debug( LDAP_DEBUG_TRACE,
576 "ldap_int_sasl_bind: no data in step!\n",
580 saslrc = sasl_client_step( ctx,
581 (scred == NULL) ? NULL : scred->bv_val,
582 (scred == NULL) ? 0 : scred->bv_len,
584 (SASL_CONST char **)&ccred.bv_val,
587 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
590 if( saslrc == SASL_INTERACT ) {
592 if( !interact ) break;
593 res = (interact)( ld, flags, defaults, prompts );
594 if( res != LDAP_SUCCESS ) break;
596 } while ( saslrc == SASL_INTERACT );
601 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
602 rc = ld->ld_errno = sasl_err2ldap( saslrc );
603 #if SASL_VERSION_MAJOR >= 2
604 if ( ld->ld_error ) {
605 LDAP_FREE( ld->ld_error );
607 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
612 if ( saslrc == SASL_OK )
615 ccred.bv_len = credlen;
617 if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
618 rc = ldap_sasl_bind( ld, dn, mech, &ccred, sctrls, cctrls, msgid );
620 if ( ccred.bv_val != NULL ) {
621 #if SASL_VERSION_MAJOR < 2
622 LDAP_FREE( ccred.bv_val );
626 if ( rc == LDAP_SUCCESS )
627 rc = LDAP_SASL_BIND_IN_PROGRESS;
632 /* Conversation was completed successfully by now */
633 if( flags != LDAP_SASL_QUIET ) {
635 saslrc = sasl_getprop( ctx, SASL_USERNAME,
636 (SASL_CONST void **)(char *) &data );
637 if( saslrc == SASL_OK && data && *data ) {
638 fprintf( stderr, "SASL username: %s\n", data );
641 #if SASL_VERSION_MAJOR < 2
642 saslrc = sasl_getprop( ctx, SASL_REALM,
643 (SASL_CONST void **) &data );
644 if( saslrc == SASL_OK && data && *data ) {
645 fprintf( stderr, "SASL realm: %s\n", data );
651 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **)(char *) &ssf );
652 if( saslrc == SASL_OK ) {
653 if( flags != LDAP_SASL_QUIET ) {
654 fprintf( stderr, "SASL SSF: %lu\n",
655 (unsigned long) *ssf );
659 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
660 sasl_conn_t *oldctx = ld->ld_defconn->lconn_sasl_sockctx;
661 sasl_dispose( &oldctx );
662 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
664 ldap_pvt_sasl_install( ld->ld_defconn->lconn_sb, ctx );
665 ld->ld_defconn->lconn_sasl_sockctx = ctx;
667 if( flags != LDAP_SASL_QUIET ) {
668 fprintf( stderr, "SASL data security layer installed.\n" );
672 ld->ld_defconn->lconn_sasl_authctx = ctx;
679 ldap_int_sasl_external(
687 #if SASL_VERSION_MAJOR < 2
688 sasl_external_properties_t extprops;
690 sasl_ssf_t sasl_ssf = ssf;
693 ctx = conn->lconn_sasl_authctx;
696 return LDAP_LOCAL_ERROR;
699 #if SASL_VERSION_MAJOR >= 2
700 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
702 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
704 memset( &extprops, '\0', sizeof(extprops) );
706 extprops.auth_id = (char *) authid;
708 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
709 (void *) &extprops );
712 if ( sc != SASL_OK ) {
713 return LDAP_LOCAL_ERROR;
730 { BER_BVC("none"), 0, 0, 0 },
731 { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
732 { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
733 { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
734 { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
735 { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
736 { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
737 { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
738 { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
739 { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
740 { BER_BVNULL, 0, 0, 0 }
743 void ldap_pvt_sasl_secprops_unparse(
744 sasl_security_properties_t *secprops,
751 if ( secprops == NULL || out == NULL ) {
756 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
757 if ( sprops[i].ival ) {
760 switch( sprops[i].ival ) {
761 case GOT_MINSSF: v = secprops->min_ssf; break;
762 case GOT_MAXSSF: v = secprops->max_ssf; break;
763 case GOT_MAXBUF: v = secprops->maxbufsize; break;
765 /* It is the default, ignore it */
766 if ( v == sprops[i].idef ) continue;
768 l += sprops[i].key.bv_len + 24;
769 } else if ( sprops[i].sflag ) {
770 if ( sprops[i].sflag & secprops->security_flags ) {
771 l += sprops[i].key.bv_len;
773 } else if ( secprops->security_flags == 0 ) {
774 l += sprops[i].key.bv_len;
781 out->bv_val = LDAP_MALLOC( l );
782 if ( out->bv_val == NULL ) {
789 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
790 if ( sprops[i].ival ) {
793 switch( sprops[i].ival ) {
794 case GOT_MINSSF: v = secprops->min_ssf; break;
795 case GOT_MAXSSF: v = secprops->max_ssf; break;
796 case GOT_MAXBUF: v = secprops->maxbufsize; break;
798 /* It is the default, ignore it */
799 if ( v == sprops[i].idef ) continue;
801 if ( comma ) *ptr++ = ',';
802 ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
804 } else if ( sprops[i].sflag ) {
805 if ( sprops[i].sflag & secprops->security_flags ) {
806 if ( comma ) *ptr++ = ',';
807 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
810 } else if ( secprops->security_flags == 0 ) {
811 if ( comma ) *ptr++ = ',';
812 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
816 out->bv_len = ptr - out->bv_val;
819 int ldap_pvt_sasl_secprops(
821 sasl_security_properties_t *secprops )
827 sasl_ssf_t max_ssf = 0;
829 sasl_ssf_t min_ssf = 0;
831 unsigned maxbufsize = 0;
832 int got_maxbufsize = 0;
834 if( secprops == NULL ) {
835 return LDAP_PARAM_ERROR;
837 props = ldap_str2charray( in, "," );
838 if( props == NULL ) {
839 return LDAP_PARAM_ERROR;
842 for( i=0; props[i]; i++ ) {
843 l = strlen( props[i] );
844 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
845 if ( l < sprops[j].key.bv_len ) continue;
846 if ( strncasecmp( props[i], sprops[j].key.bv_val,
847 sprops[j].key.bv_len )) continue;
848 if ( sprops[j].ival ) {
851 if ( !isdigit( (unsigned char)props[i][sprops[j].key.bv_len] ))
853 v = strtoul( &props[i][sprops[j].key.bv_len], &next, 10 );
854 if ( next == &props[i][sprops[j].key.bv_len] || next[0] != '\0' ) continue;
855 switch( sprops[j].ival ) {
857 min_ssf = v; got_min_ssf++; break;
859 max_ssf = v; got_max_ssf++; break;
861 maxbufsize = v; got_maxbufsize++; break;
864 if ( props[i][sprops[j].key.bv_len] ) continue;
865 if ( sprops[j].sflag )
866 sflags |= sprops[j].sflag;
873 if ( BER_BVISNULL( &sprops[j].key )) {
874 ldap_charray_free( props );
875 return LDAP_NOT_SUPPORTED;
880 secprops->security_flags = sflags;
883 secprops->min_ssf = min_ssf;
886 secprops->max_ssf = max_ssf;
889 secprops->maxbufsize = maxbufsize;
892 ldap_charray_free( props );
897 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
902 case LDAP_OPT_X_SASL_SECPROPS:
903 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
904 if( rc == LDAP_SUCCESS ) return 0;
911 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
913 if ( option == LDAP_OPT_X_SASL_MECHLIST ) {
914 if ( ldap_int_sasl_init() )
916 *(char ***)arg = (char **)sasl_global_listmech();
924 case LDAP_OPT_X_SASL_MECH: {
925 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
926 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
928 case LDAP_OPT_X_SASL_REALM: {
929 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
930 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
932 case LDAP_OPT_X_SASL_AUTHCID: {
933 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
934 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
936 case LDAP_OPT_X_SASL_AUTHZID: {
937 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
938 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
941 case LDAP_OPT_X_SASL_SSF: {
946 if( ld->ld_defconn == NULL ) {
950 ctx = ld->ld_defconn->lconn_sasl_sockctx;
956 sc = sasl_getprop( ctx, SASL_SSF,
957 (SASL_CONST void **)(char *) &ssf );
959 if ( sc != SASL_OK ) {
963 *(ber_len_t *)arg = *ssf;
966 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
967 /* this option is write only */
970 case LDAP_OPT_X_SASL_SSF_MIN:
971 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
973 case LDAP_OPT_X_SASL_SSF_MAX:
974 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
976 case LDAP_OPT_X_SASL_MAXBUFSIZE:
977 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
979 case LDAP_OPT_X_SASL_NOCANON:
980 *(int *)arg = (int) LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
983 case LDAP_OPT_X_SASL_USERNAME: {
988 if( ld->ld_defconn == NULL ) {
992 ctx = ld->ld_defconn->lconn_sasl_authctx;
998 sc = sasl_getprop( ctx, SASL_USERNAME,
999 (SASL_CONST void **)(char **) &username );
1001 if ( sc != SASL_OK ) {
1005 *(char **)arg = username ? LDAP_STRDUP( username ) : NULL;
1008 case LDAP_OPT_X_SASL_SECPROPS:
1009 /* this option is write only */
1012 #ifdef SASL_GSS_CREDS
1013 case LDAP_OPT_X_SASL_GSS_CREDS: {
1017 if ( ld->ld_defconn == NULL )
1020 ctx = ld->ld_defconn->lconn_sasl_authctx;
1024 sc = sasl_getprop( ctx, SASL_GSS_CREDS, arg );
1025 if ( sc != SASL_OK )
1038 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1043 if ( arg == NULL && option != LDAP_OPT_X_SASL_NOCANON )
1047 case LDAP_OPT_X_SASL_SSF:
1048 case LDAP_OPT_X_SASL_USERNAME:
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;
1057 sasl_ssf_t sasl_ssf;
1061 if( ld->ld_defconn == NULL ) {
1065 ctx = ld->ld_defconn->lconn_sasl_authctx;
1067 if ( ctx == NULL ) {
1071 #if SASL_VERSION_MAJOR >= 2
1072 sasl_ssf = * (ber_len_t *)arg;
1073 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf);
1075 memset(&extprops, 0L, sizeof(extprops));
1077 extprops.ssf = * (ber_len_t *) arg;
1079 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1080 (void *) &extprops );
1083 if ( sc != SASL_OK ) {
1088 case LDAP_OPT_X_SASL_SSF_MIN:
1089 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1091 case LDAP_OPT_X_SASL_SSF_MAX:
1092 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1094 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1095 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1097 case LDAP_OPT_X_SASL_NOCANON:
1098 if ( arg == LDAP_OPT_OFF ) {
1099 LDAP_BOOL_CLR(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1101 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1105 case LDAP_OPT_X_SASL_SECPROPS: {
1107 sc = ldap_pvt_sasl_secprops( (char *) arg,
1108 &ld->ld_options.ldo_sasl_secprops );
1110 return sc == LDAP_SUCCESS ? 0 : -1;
1113 #ifdef SASL_GSS_CREDS
1114 case LDAP_OPT_X_SASL_GSS_CREDS: {
1118 if ( ld->ld_defconn == NULL )
1121 ctx = ld->ld_defconn->lconn_sasl_authctx;
1125 sc = sasl_setprop( ctx, SASL_GSS_CREDS, arg );
1126 if ( sc != SASL_OK )
1138 #ifdef LDAP_R_COMPILE
1139 #define LDAP_DEBUG_R_SASL
1140 void *ldap_pvt_sasl_mutex_new(void)
1142 ldap_pvt_thread_mutex_t *mutex;
1144 mutex = (ldap_pvt_thread_mutex_t *) LDAP_CALLOC( 1,
1145 sizeof(ldap_pvt_thread_mutex_t) );
1147 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1150 #ifndef LDAP_DEBUG_R_SASL
1152 #endif /* !LDAP_DEBUG_R_SASL */
1156 int ldap_pvt_sasl_mutex_lock(void *mutex)
1158 #ifdef LDAP_DEBUG_R_SASL
1159 if ( mutex == NULL ) {
1162 #else /* !LDAP_DEBUG_R_SASL */
1163 assert( mutex != NULL );
1164 #endif /* !LDAP_DEBUG_R_SASL */
1165 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1166 ? SASL_FAIL : SASL_OK;
1169 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1171 #ifdef LDAP_DEBUG_R_SASL
1172 if ( mutex == NULL ) {
1175 #else /* !LDAP_DEBUG_R_SASL */
1176 assert( mutex != NULL );
1177 #endif /* !LDAP_DEBUG_R_SASL */
1178 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1179 ? SASL_FAIL : SASL_OK;
1182 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1184 #ifdef LDAP_DEBUG_R_SASL
1185 if ( mutex == NULL ) {
1188 #else /* !LDAP_DEBUG_R_SASL */
1189 assert( mutex != NULL );
1190 #endif /* !LDAP_DEBUG_R_SASL */
1191 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1197 int ldap_int_sasl_init( void )
1198 { return LDAP_SUCCESS; }
1200 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1201 { return LDAP_SUCCESS; }
1208 LDAPControl **sctrls,
1209 LDAPControl **cctrls,
1211 LDAP_SASL_INTERACT_PROC *interact,
1213 LDAPMessage *result,
1216 { return LDAP_NOT_SUPPORTED; }
1219 ldap_int_sasl_external(
1222 const char * authid,
1224 { return LDAP_SUCCESS; }
1226 #endif /* HAVE_CYRUS_SASL */