2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2013 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 */
44 #ifdef HAVE_SASL_SASL_H
45 #include <sasl/sasl.h>
50 #if SASL_VERSION_MAJOR >= 2
51 #define SASL_CONST const
57 * Various Cyrus SASL related stuff.
60 static const sasl_callback_t client_callbacks[] = {
61 #ifdef SASL_CB_GETREALM
62 { SASL_CB_GETREALM, NULL, NULL },
64 { SASL_CB_USER, NULL, NULL },
65 { SASL_CB_AUTHNAME, NULL, NULL },
66 { SASL_CB_PASS, NULL, NULL },
67 { SASL_CB_ECHOPROMPT, NULL, NULL },
68 { SASL_CB_NOECHOPROMPT, NULL, NULL },
69 { SASL_CB_LIST_END, NULL, NULL }
72 int ldap_int_sasl_init( void )
74 /* XXX not threadsafe */
75 static int sasl_initialized = 0;
77 #ifdef HAVE_SASL_VERSION
78 /* stringify the version number, sasl.h doesn't do it for us */
79 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
80 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
81 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
84 sasl_version( NULL, &rc );
85 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
86 (rc & 0xffff) < SASL_VERSION_STEP) {
87 char version[sizeof("xxx.xxx.xxxxx")];
88 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
91 Debug( LDAP_DEBUG_ANY,
92 "ldap_int_sasl_init: SASL library version mismatch:"
93 " expected " SASL_VERSION_STRING ","
94 " got %s\n", version, 0, 0 );
99 if ( sasl_initialized ) {
103 /* SASL 2 takes care of its own memory completely internally */
104 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
110 #endif /* CSRIMALLOC */
112 #ifdef LDAP_R_COMPILE
114 ldap_pvt_sasl_mutex_new,
115 ldap_pvt_sasl_mutex_lock,
116 ldap_pvt_sasl_mutex_unlock,
117 ldap_pvt_sasl_mutex_dispose );
120 if ( sasl_client_init( NULL ) == SASL_OK ) {
121 sasl_initialized = 1;
125 #if SASL_VERSION_MAJOR < 2
126 /* A no-op to make sure we link with Cyrus 1.5 */
127 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
134 struct sb_sasl_generic_data *p,
139 sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
142 sasl_getprop( sasl_context, SASL_MAXOUTBUF,
143 (SASL_CONST void **)(char *) &maxbuf );
145 *min_send = SASL_MIN_BUFF_SIZE;
147 *max_recv = SASL_MAX_BUFF_SIZE;
151 sb_sasl_cyrus_encode(
152 struct sb_sasl_generic_data *p,
157 sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
159 unsigned tmpsize = dst->buf_size;
161 ret = sasl_encode( sasl_context, (char *)buf, len,
162 (SASL_CONST char **)&dst->buf_base,
165 dst->buf_size = tmpsize;
166 dst->buf_end = dst->buf_size;
168 if ( ret != SASL_OK ) {
169 ber_log_printf( LDAP_DEBUG_ANY, p->sbiod->sbiod_sb->sb_debug,
170 "sb_sasl_cyrus_encode: failed to encode packet: %s\n",
171 sasl_errstring( ret, NULL, NULL ) );
179 sb_sasl_cyrus_decode(
180 struct sb_sasl_generic_data *p,
181 const Sockbuf_Buf *src,
184 sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
186 unsigned tmpsize = dst->buf_size;
188 ret = sasl_decode( sasl_context,
189 src->buf_base, src->buf_end,
190 (SASL_CONST char **)&dst->buf_base,
191 (unsigned *)&tmpsize );
194 dst->buf_size = tmpsize;
195 dst->buf_end = dst->buf_size;
197 if ( ret != SASL_OK ) {
198 ber_log_printf( LDAP_DEBUG_ANY, p->sbiod->sbiod_sb->sb_debug,
199 "sb_sasl_cyrus_decode: failed to decode packet: %s\n",
200 sasl_errstring( ret, NULL, NULL ) );
208 sb_sasl_cyrus_reset_buf(
209 struct sb_sasl_generic_data *p,
212 #if SASL_VERSION_MAJOR >= 2
213 ber_pvt_sb_buf_init( buf );
215 ber_pvt_sb_buf_destroy( buf );
221 struct sb_sasl_generic_data *p)
223 #if SASL_VERSION_MAJOR >= 2
225 * SASLv2 encode/decode buffers are managed by
226 * libsasl2. Ensure they are not freed by liblber.
228 p->buf_in.buf_base = NULL;
229 p->buf_out.buf_base = NULL;
233 static const struct sb_sasl_generic_ops sb_sasl_cyrus_ops = {
235 sb_sasl_cyrus_encode,
236 sb_sasl_cyrus_decode,
237 sb_sasl_cyrus_reset_buf,
241 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
243 struct sb_sasl_generic_install install_arg;
245 install_arg.ops = &sb_sasl_cyrus_ops;
246 install_arg.ops_private = ctx_arg;
248 return ldap_pvt_sasl_generic_install( sb, &install_arg );
251 void ldap_pvt_sasl_remove( Sockbuf *sb )
253 ldap_pvt_sasl_generic_remove( sb );
257 sasl_err2ldap( int saslerr )
261 /* map SASL errors to LDAP API errors returned by:
263 * SASL_OK, SASL_NOMECH, SASL_NOMEM
264 * sasl_client_start()
265 * SASL_OK, SASL_NOMECH, SASL_NOMEM, SASL_INTERACT
267 * SASL_OK, SASL_INTERACT, SASL_BADPROT, SASL_BADSERV
272 rc = LDAP_MORE_RESULTS_TO_RETURN;
275 rc = LDAP_LOCAL_ERROR;
284 rc = LDAP_AUTH_UNKNOWN;
287 rc = LDAP_DECODING_ERROR;
290 rc = LDAP_AUTH_UNKNOWN;
295 rc = LDAP_AUTH_UNKNOWN;
298 rc = LDAP_PARAM_ERROR;
301 rc = LDAP_LOCAL_ERROR;
305 rc = LDAP_AUTH_UNKNOWN;
308 rc = LDAP_LOCAL_ERROR;
312 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
325 assert( lc->lconn_sasl_authctx == NULL );
327 if ( host == NULL ) {
328 ld->ld_errno = LDAP_LOCAL_ERROR;
332 if ( ldap_int_sasl_init() ) {
333 ld->ld_errno = LDAP_LOCAL_ERROR;
337 #if SASL_VERSION_MAJOR >= 2
338 rc = sasl_client_new( "ldap", host, NULL, NULL,
339 client_callbacks, 0, &ctx );
341 rc = sasl_client_new( "ldap", host, client_callbacks,
342 SASL_SECURITY_LAYER, &ctx );
345 if ( rc != SASL_OK ) {
346 ld->ld_errno = sasl_err2ldap( rc );
350 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
353 lc->lconn_sasl_authctx = ctx;
358 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
360 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
363 sasl_dispose( &ctx );
364 if ( lc->lconn_sasl_sockctx &&
365 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
366 ctx = lc->lconn_sasl_sockctx;
367 sasl_dispose( &ctx );
369 lc->lconn_sasl_sockctx = NULL;
370 lc->lconn_sasl_authctx = NULL;
372 if( lc->lconn_sasl_cbind ) {
373 ldap_memfree( lc->lconn_sasl_cbind );
374 lc->lconn_sasl_cbind = 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 );
489 #ifdef SASL_CHANNEL_BINDING /* 2.1.25+ */
492 struct berval cbv = { sizeof(cbinding), cbinding };
493 if ( ldap_pvt_tls_get_unique( ssl, &cbv, 0 )) {
494 sasl_channel_binding_t *cb = ldap_memalloc( sizeof(*cb) +
498 cb->data = (char *)(cb+1);
499 cb->len = cbv.bv_len;
500 memcpy( cb->data, cbv.bv_val, cbv.bv_len );
501 sasl_setprop( ld->ld_defconn->lconn_sasl_authctx,
502 SASL_CHANNEL_BINDING, cb );
503 ld->ld_defconn->lconn_sasl_cbind = cb;
511 /* Check for local */
512 if ( ldap_pvt_url_scheme2proto(
513 ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC )
515 char authid[sizeof("gidNumber=4294967295+uidNumber=4294967295,"
516 "cn=peercred,cn=external,cn=auth")];
517 sprintf( authid, "gidNumber=%u+uidNumber=%u,"
518 "cn=peercred,cn=external,cn=auth",
519 getegid(), geteuid() );
520 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid,
521 LDAP_PVT_SASL_LOCAL_SSF );
525 /* (re)set security properties */
526 sasl_setprop( ctx, SASL_SEC_PROPS,
527 &ld->ld_options.ldo_sasl_secprops );
532 saslrc = sasl_client_start( ctx,
534 #if SASL_VERSION_MAJOR < 2
538 (SASL_CONST char **)&ccred.bv_val,
542 if( pmech == NULL && mech != NULL ) {
546 if( flags != LDAP_SASL_QUIET ) {
548 "SASL/%s authentication started\n",
553 if( saslrc == SASL_INTERACT ) {
555 if( !interact ) break;
556 res = (interact)( ld, flags, defaults, prompts );
558 if( res != LDAP_SUCCESS ) break;
560 } while ( saslrc == SASL_INTERACT );
561 rc = LDAP_SASL_BIND_IN_PROGRESS;
564 /* continuing an in-progress Bind */
565 struct berval *scred = NULL;
567 ctx = ld->ld_defconn->lconn_sasl_authctx;
569 rc = ldap_parse_sasl_bind_result( ld, result, &scred, 0 );
570 if ( rc != LDAP_SUCCESS )
573 rc = ldap_result2error( ld, result, 0 );
574 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
576 /* and server provided us with data? */
577 Debug( LDAP_DEBUG_TRACE,
578 "ldap_int_sasl_bind: rc=%d len=%ld\n",
579 rc, scred ? (long) scred->bv_len : -1L, 0 );
587 if ( rc == LDAP_SUCCESS && mech == NULL )
593 Debug( LDAP_DEBUG_TRACE,
594 "ldap_int_sasl_bind: no data in step!\n",
598 saslrc = sasl_client_step( ctx,
599 (scred == NULL) ? NULL : scred->bv_val,
600 (scred == NULL) ? 0 : scred->bv_len,
602 (SASL_CONST char **)&ccred.bv_val,
605 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
608 if( saslrc == SASL_INTERACT ) {
610 if( !interact ) break;
611 res = (interact)( ld, flags, defaults, prompts );
612 if( res != LDAP_SUCCESS ) break;
614 } while ( saslrc == SASL_INTERACT );
619 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
620 rc = ld->ld_errno = sasl_err2ldap( saslrc );
621 #if SASL_VERSION_MAJOR >= 2
622 if ( ld->ld_error ) {
623 LDAP_FREE( ld->ld_error );
625 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
630 if ( saslrc == SASL_OK )
633 ccred.bv_len = credlen;
635 if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
636 rc = ldap_sasl_bind( ld, dn, mech, &ccred, sctrls, cctrls, msgid );
638 if ( ccred.bv_val != NULL ) {
639 #if SASL_VERSION_MAJOR < 2
640 LDAP_FREE( ccred.bv_val );
644 if ( rc == LDAP_SUCCESS )
645 rc = LDAP_SASL_BIND_IN_PROGRESS;
650 /* Conversation was completed successfully by now */
651 if( flags != LDAP_SASL_QUIET ) {
653 saslrc = sasl_getprop( ctx, SASL_USERNAME,
654 (SASL_CONST void **)(char *) &data );
655 if( saslrc == SASL_OK && data && *data ) {
656 fprintf( stderr, "SASL username: %s\n", data );
659 #if SASL_VERSION_MAJOR < 2
660 saslrc = sasl_getprop( ctx, SASL_REALM,
661 (SASL_CONST void **) &data );
662 if( saslrc == SASL_OK && data && *data ) {
663 fprintf( stderr, "SASL realm: %s\n", data );
669 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **)(char *) &ssf );
670 if( saslrc == SASL_OK ) {
671 if( flags != LDAP_SASL_QUIET ) {
672 fprintf( stderr, "SASL SSF: %lu\n",
673 (unsigned long) *ssf );
677 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
678 sasl_conn_t *oldctx = ld->ld_defconn->lconn_sasl_sockctx;
679 sasl_dispose( &oldctx );
680 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
682 ldap_pvt_sasl_install( ld->ld_defconn->lconn_sb, ctx );
683 ld->ld_defconn->lconn_sasl_sockctx = ctx;
685 if( flags != LDAP_SASL_QUIET ) {
686 fprintf( stderr, "SASL data security layer installed.\n" );
690 ld->ld_defconn->lconn_sasl_authctx = ctx;
697 ldap_int_sasl_external(
705 #if SASL_VERSION_MAJOR < 2
706 sasl_external_properties_t extprops;
708 sasl_ssf_t sasl_ssf = ssf;
711 ctx = conn->lconn_sasl_authctx;
714 return LDAP_LOCAL_ERROR;
717 #if SASL_VERSION_MAJOR >= 2
718 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
720 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
722 memset( &extprops, '\0', sizeof(extprops) );
724 extprops.auth_id = (char *) authid;
726 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
727 (void *) &extprops );
730 if ( sc != SASL_OK ) {
731 return LDAP_LOCAL_ERROR;
748 { BER_BVC("none"), 0, 0, 0 },
749 { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
750 { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
751 { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
752 { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
753 { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
754 { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
755 { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
756 { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
757 { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
758 { BER_BVNULL, 0, 0, 0 }
761 void ldap_pvt_sasl_secprops_unparse(
762 sasl_security_properties_t *secprops,
769 if ( secprops == NULL || out == NULL ) {
774 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
775 if ( sprops[i].ival ) {
778 switch( sprops[i].ival ) {
779 case GOT_MINSSF: v = secprops->min_ssf; break;
780 case GOT_MAXSSF: v = secprops->max_ssf; break;
781 case GOT_MAXBUF: v = secprops->maxbufsize; break;
783 /* It is the default, ignore it */
784 if ( v == sprops[i].idef ) continue;
786 l += sprops[i].key.bv_len + 24;
787 } else if ( sprops[i].sflag ) {
788 if ( sprops[i].sflag & secprops->security_flags ) {
789 l += sprops[i].key.bv_len;
791 } else if ( secprops->security_flags == 0 ) {
792 l += sprops[i].key.bv_len;
799 out->bv_val = LDAP_MALLOC( l );
800 if ( out->bv_val == NULL ) {
807 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
808 if ( sprops[i].ival ) {
811 switch( sprops[i].ival ) {
812 case GOT_MINSSF: v = secprops->min_ssf; break;
813 case GOT_MAXSSF: v = secprops->max_ssf; break;
814 case GOT_MAXBUF: v = secprops->maxbufsize; break;
816 /* It is the default, ignore it */
817 if ( v == sprops[i].idef ) continue;
819 if ( comma ) *ptr++ = ',';
820 ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
822 } else if ( sprops[i].sflag ) {
823 if ( sprops[i].sflag & secprops->security_flags ) {
824 if ( comma ) *ptr++ = ',';
825 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
828 } else if ( secprops->security_flags == 0 ) {
829 if ( comma ) *ptr++ = ',';
830 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
834 out->bv_len = ptr - out->bv_val;
837 int ldap_pvt_sasl_secprops(
839 sasl_security_properties_t *secprops )
845 sasl_ssf_t max_ssf = 0;
847 sasl_ssf_t min_ssf = 0;
849 unsigned maxbufsize = 0;
850 int got_maxbufsize = 0;
852 if( secprops == NULL ) {
853 return LDAP_PARAM_ERROR;
855 props = ldap_str2charray( in, "," );
856 if( props == NULL ) {
857 return LDAP_PARAM_ERROR;
860 for( i=0; props[i]; i++ ) {
861 l = strlen( props[i] );
862 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
863 if ( l < sprops[j].key.bv_len ) continue;
864 if ( strncasecmp( props[i], sprops[j].key.bv_val,
865 sprops[j].key.bv_len )) continue;
866 if ( sprops[j].ival ) {
869 if ( !isdigit( (unsigned char)props[i][sprops[j].key.bv_len] ))
871 v = strtoul( &props[i][sprops[j].key.bv_len], &next, 10 );
872 if ( next == &props[i][sprops[j].key.bv_len] || next[0] != '\0' ) continue;
873 switch( sprops[j].ival ) {
875 min_ssf = v; got_min_ssf++; break;
877 max_ssf = v; got_max_ssf++; break;
879 maxbufsize = v; got_maxbufsize++; break;
882 if ( props[i][sprops[j].key.bv_len] ) continue;
883 if ( sprops[j].sflag )
884 sflags |= sprops[j].sflag;
891 if ( BER_BVISNULL( &sprops[j].key )) {
892 ldap_charray_free( props );
893 return LDAP_NOT_SUPPORTED;
898 secprops->security_flags = sflags;
901 secprops->min_ssf = min_ssf;
904 secprops->max_ssf = max_ssf;
907 secprops->maxbufsize = maxbufsize;
910 ldap_charray_free( props );
915 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
920 case LDAP_OPT_X_SASL_SECPROPS:
921 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
922 if( rc == LDAP_SUCCESS ) return 0;
929 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
931 if ( option == LDAP_OPT_X_SASL_MECHLIST ) {
932 if ( ldap_int_sasl_init() )
934 *(char ***)arg = (char **)sasl_global_listmech();
942 case LDAP_OPT_X_SASL_MECH: {
943 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
944 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
946 case LDAP_OPT_X_SASL_REALM: {
947 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
948 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
950 case LDAP_OPT_X_SASL_AUTHCID: {
951 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
952 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
954 case LDAP_OPT_X_SASL_AUTHZID: {
955 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
956 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
959 case LDAP_OPT_X_SASL_SSF: {
964 if( ld->ld_defconn == NULL ) {
968 ctx = ld->ld_defconn->lconn_sasl_sockctx;
974 sc = sasl_getprop( ctx, SASL_SSF,
975 (SASL_CONST void **)(char *) &ssf );
977 if ( sc != SASL_OK ) {
981 *(ber_len_t *)arg = *ssf;
984 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
985 /* this option is write only */
988 case LDAP_OPT_X_SASL_SSF_MIN:
989 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
991 case LDAP_OPT_X_SASL_SSF_MAX:
992 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
994 case LDAP_OPT_X_SASL_MAXBUFSIZE:
995 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
997 case LDAP_OPT_X_SASL_NOCANON:
998 *(int *)arg = (int) LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1001 case LDAP_OPT_X_SASL_USERNAME: {
1006 if( ld->ld_defconn == NULL ) {
1010 ctx = ld->ld_defconn->lconn_sasl_authctx;
1012 if ( ctx == NULL ) {
1016 sc = sasl_getprop( ctx, SASL_USERNAME,
1017 (SASL_CONST void **)(char **) &username );
1019 if ( sc != SASL_OK ) {
1023 *(char **)arg = username ? LDAP_STRDUP( username ) : NULL;
1026 case LDAP_OPT_X_SASL_SECPROPS:
1027 /* this option is write only */
1030 #ifdef SASL_GSS_CREDS
1031 case LDAP_OPT_X_SASL_GSS_CREDS: {
1035 if ( ld->ld_defconn == NULL )
1038 ctx = ld->ld_defconn->lconn_sasl_authctx;
1042 sc = sasl_getprop( ctx, SASL_GSS_CREDS, arg );
1043 if ( sc != SASL_OK )
1056 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1061 if ( arg == NULL && option != LDAP_OPT_X_SASL_NOCANON )
1065 case LDAP_OPT_X_SASL_SSF:
1066 case LDAP_OPT_X_SASL_USERNAME:
1067 /* This option is read-only */
1070 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1072 #if SASL_VERSION_MAJOR < 2
1073 sasl_external_properties_t extprops;
1075 sasl_ssf_t sasl_ssf;
1079 if( ld->ld_defconn == NULL ) {
1083 ctx = ld->ld_defconn->lconn_sasl_authctx;
1085 if ( ctx == NULL ) {
1089 #if SASL_VERSION_MAJOR >= 2
1090 sasl_ssf = * (ber_len_t *)arg;
1091 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf);
1093 memset(&extprops, 0L, sizeof(extprops));
1095 extprops.ssf = * (ber_len_t *) arg;
1097 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1098 (void *) &extprops );
1101 if ( sc != SASL_OK ) {
1106 case LDAP_OPT_X_SASL_SSF_MIN:
1107 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1109 case LDAP_OPT_X_SASL_SSF_MAX:
1110 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1112 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1113 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1115 case LDAP_OPT_X_SASL_NOCANON:
1116 if ( arg == LDAP_OPT_OFF ) {
1117 LDAP_BOOL_CLR(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1119 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1123 case LDAP_OPT_X_SASL_SECPROPS: {
1125 sc = ldap_pvt_sasl_secprops( (char *) arg,
1126 &ld->ld_options.ldo_sasl_secprops );
1128 return sc == LDAP_SUCCESS ? 0 : -1;
1131 #ifdef SASL_GSS_CREDS
1132 case LDAP_OPT_X_SASL_GSS_CREDS: {
1136 if ( ld->ld_defconn == NULL )
1139 ctx = ld->ld_defconn->lconn_sasl_authctx;
1143 sc = sasl_setprop( ctx, SASL_GSS_CREDS, arg );
1144 if ( sc != SASL_OK )
1156 #ifdef LDAP_R_COMPILE
1157 #define LDAP_DEBUG_R_SASL
1158 void *ldap_pvt_sasl_mutex_new(void)
1160 ldap_pvt_thread_mutex_t *mutex;
1162 mutex = (ldap_pvt_thread_mutex_t *) LDAP_CALLOC( 1,
1163 sizeof(ldap_pvt_thread_mutex_t) );
1165 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1168 #ifndef LDAP_DEBUG_R_SASL
1170 #endif /* !LDAP_DEBUG_R_SASL */
1174 int ldap_pvt_sasl_mutex_lock(void *mutex)
1176 #ifdef LDAP_DEBUG_R_SASL
1177 if ( mutex == NULL ) {
1180 #else /* !LDAP_DEBUG_R_SASL */
1181 assert( mutex != NULL );
1182 #endif /* !LDAP_DEBUG_R_SASL */
1183 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1184 ? SASL_FAIL : SASL_OK;
1187 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1189 #ifdef LDAP_DEBUG_R_SASL
1190 if ( mutex == NULL ) {
1193 #else /* !LDAP_DEBUG_R_SASL */
1194 assert( mutex != NULL );
1195 #endif /* !LDAP_DEBUG_R_SASL */
1196 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1197 ? SASL_FAIL : SASL_OK;
1200 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1202 #ifdef LDAP_DEBUG_R_SASL
1203 if ( mutex == NULL ) {
1206 #else /* !LDAP_DEBUG_R_SASL */
1207 assert( mutex != NULL );
1208 #endif /* !LDAP_DEBUG_R_SASL */
1209 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1215 int ldap_int_sasl_init( void )
1216 { return LDAP_SUCCESS; }
1218 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1219 { return LDAP_SUCCESS; }
1226 LDAPControl **sctrls,
1227 LDAPControl **cctrls,
1229 LDAP_SASL_INTERACT_PROC *interact,
1231 LDAPMessage *result,
1234 { return LDAP_NOT_SUPPORTED; }
1237 ldap_int_sasl_external(
1240 const char * authid,
1242 { return LDAP_SUCCESS; }
1244 #endif /* HAVE_CYRUS_SASL */