2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 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 }
73 * ldap_int_initialize is responsible for calling this only once.
75 int ldap_int_sasl_init( void )
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 );
100 /* SASL 2 takes care of its own memory completely internally */
101 #if SASL_VERSION_MAJOR < 2 && !defined(CSRIMALLOC)
107 #endif /* CSRIMALLOC */
109 #ifdef LDAP_R_COMPILE
111 ldap_pvt_sasl_mutex_new,
112 ldap_pvt_sasl_mutex_lock,
113 ldap_pvt_sasl_mutex_unlock,
114 ldap_pvt_sasl_mutex_dispose );
117 if ( sasl_client_init( NULL ) == SASL_OK ) {
121 #if SASL_VERSION_MAJOR < 2
122 /* A no-op to make sure we link with Cyrus 1.5 */
123 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
130 struct sb_sasl_generic_data *p,
135 sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
138 sasl_getprop( sasl_context, SASL_MAXOUTBUF,
139 (SASL_CONST void **)(char *) &maxbuf );
141 *min_send = SASL_MIN_BUFF_SIZE;
143 *max_recv = SASL_MAX_BUFF_SIZE;
147 sb_sasl_cyrus_encode(
148 struct sb_sasl_generic_data *p,
153 sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
155 unsigned tmpsize = dst->buf_size;
157 ret = sasl_encode( sasl_context, (char *)buf, len,
158 (SASL_CONST char **)&dst->buf_base,
161 dst->buf_size = tmpsize;
162 dst->buf_end = dst->buf_size;
164 if ( ret != SASL_OK ) {
165 ber_log_printf( LDAP_DEBUG_ANY, p->sbiod->sbiod_sb->sb_debug,
166 "sb_sasl_cyrus_encode: failed to encode packet: %s\n",
167 sasl_errstring( ret, NULL, NULL ) );
175 sb_sasl_cyrus_decode(
176 struct sb_sasl_generic_data *p,
177 const Sockbuf_Buf *src,
180 sasl_conn_t *sasl_context = (sasl_conn_t *)p->ops_private;
182 unsigned tmpsize = dst->buf_size;
184 ret = sasl_decode( sasl_context,
185 src->buf_base, src->buf_end,
186 (SASL_CONST char **)&dst->buf_base,
187 (unsigned *)&tmpsize );
190 dst->buf_size = tmpsize;
191 dst->buf_end = dst->buf_size;
193 if ( ret != SASL_OK ) {
194 ber_log_printf( LDAP_DEBUG_ANY, p->sbiod->sbiod_sb->sb_debug,
195 "sb_sasl_cyrus_decode: failed to decode packet: %s\n",
196 sasl_errstring( ret, NULL, NULL ) );
204 sb_sasl_cyrus_reset_buf(
205 struct sb_sasl_generic_data *p,
208 #if SASL_VERSION_MAJOR >= 2
209 ber_pvt_sb_buf_init( buf );
211 ber_pvt_sb_buf_destroy( buf );
217 struct sb_sasl_generic_data *p)
219 #if SASL_VERSION_MAJOR >= 2
221 * SASLv2 encode/decode buffers are managed by
222 * libsasl2. Ensure they are not freed by liblber.
224 p->buf_in.buf_base = NULL;
225 p->buf_out.buf_base = NULL;
229 static const struct sb_sasl_generic_ops sb_sasl_cyrus_ops = {
231 sb_sasl_cyrus_encode,
232 sb_sasl_cyrus_decode,
233 sb_sasl_cyrus_reset_buf,
237 int ldap_pvt_sasl_install( Sockbuf *sb, void *ctx_arg )
239 struct sb_sasl_generic_install install_arg;
241 install_arg.ops = &sb_sasl_cyrus_ops;
242 install_arg.ops_private = ctx_arg;
244 return ldap_pvt_sasl_generic_install( sb, &install_arg );
247 void ldap_pvt_sasl_remove( Sockbuf *sb )
249 ldap_pvt_sasl_generic_remove( sb );
253 sasl_err2ldap( int saslerr )
257 /* map SASL errors to LDAP API errors returned by:
259 * SASL_OK, SASL_NOMECH, SASL_NOMEM
260 * sasl_client_start()
261 * SASL_OK, SASL_NOMECH, SASL_NOMEM, SASL_INTERACT
263 * SASL_OK, SASL_INTERACT, SASL_BADPROT, SASL_BADSERV
268 rc = LDAP_MORE_RESULTS_TO_RETURN;
271 rc = LDAP_LOCAL_ERROR;
280 rc = LDAP_AUTH_UNKNOWN;
283 rc = LDAP_DECODING_ERROR;
286 rc = LDAP_AUTH_UNKNOWN;
291 rc = LDAP_AUTH_UNKNOWN;
294 rc = LDAP_PARAM_ERROR;
297 rc = LDAP_LOCAL_ERROR;
301 rc = LDAP_AUTH_UNKNOWN;
304 rc = LDAP_LOCAL_ERROR;
308 assert( rc == LDAP_SUCCESS || LDAP_API_ERROR( rc ) );
321 assert( lc->lconn_sasl_authctx == NULL );
323 if ( host == NULL ) {
324 ld->ld_errno = LDAP_LOCAL_ERROR;
328 #if SASL_VERSION_MAJOR >= 2
329 rc = sasl_client_new( "ldap", host, NULL, NULL,
330 client_callbacks, 0, &ctx );
332 rc = sasl_client_new( "ldap", host, client_callbacks,
333 SASL_SECURITY_LAYER, &ctx );
336 if ( rc != SASL_OK ) {
337 ld->ld_errno = sasl_err2ldap( rc );
341 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_open: host=%s\n",
344 lc->lconn_sasl_authctx = ctx;
349 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
351 sasl_conn_t *ctx = lc->lconn_sasl_authctx;
354 sasl_dispose( &ctx );
355 if ( lc->lconn_sasl_sockctx &&
356 lc->lconn_sasl_authctx != lc->lconn_sasl_sockctx ) {
357 ctx = lc->lconn_sasl_sockctx;
358 sasl_dispose( &ctx );
360 lc->lconn_sasl_sockctx = NULL;
361 lc->lconn_sasl_authctx = NULL;
363 if( lc->lconn_sasl_cbind ) {
364 ldap_memfree( lc->lconn_sasl_cbind );
365 lc->lconn_sasl_cbind = NULL;
376 LDAPControl **sctrls,
377 LDAPControl **cctrls,
379 LDAP_SASL_INTERACT_PROC *interact,
388 sasl_interact_t *prompts = NULL;
389 struct berval ccred = BER_BVNULL;
393 Debug( LDAP_DEBUG_TRACE, "ldap_int_sasl_bind: %s\n",
394 mechs ? mechs : "<null>", 0, 0 );
396 /* do a quick !LDAPv3 check... ldap_sasl_bind will do the rest. */
397 if (ld->ld_version < LDAP_VERSION3) {
398 ld->ld_errno = LDAP_NOT_SUPPORTED;
402 /* Starting a Bind */
404 const char *pmech = NULL;
410 LDAP_MUTEX_LOCK( &ld->ld_conn_mutex );
411 ber_sockbuf_ctrl( ld->ld_sb, LBER_SB_OPT_GET_FD, &sd );
413 if ( sd == AC_SOCKET_INVALID || !ld->ld_defconn ) {
414 /* not connected yet */
416 rc = ldap_open_defconn( ld );
419 ber_sockbuf_ctrl( ld->ld_defconn->lconn_sb,
420 LBER_SB_OPT_GET_FD, &sd );
422 if( sd == AC_SOCKET_INVALID ) {
423 ld->ld_errno = LDAP_LOCAL_ERROR;
428 if ( rc == 0 && ld->ld_defconn &&
429 ld->ld_defconn->lconn_status == LDAP_CONNST_CONNECTING ) {
430 rc = ldap_int_check_async_open( ld, sd );
432 LDAP_MUTEX_UNLOCK( &ld->ld_conn_mutex );
433 if( rc != 0 ) return ld->ld_errno;
435 oldctx = ld->ld_defconn->lconn_sasl_authctx;
437 /* If we already have an authentication context, clear it out */
439 if ( oldctx != ld->ld_defconn->lconn_sasl_sockctx ) {
440 sasl_dispose( &oldctx );
442 ld->ld_defconn->lconn_sasl_authctx = NULL;
447 int nocanon = (int)LDAP_BOOL_GET( &ld->ld_options,
448 LDAP_BOOL_SASL_NOCANON );
450 /* If we don't need to canonicalize just use the host
454 saslhost = ld->ld_defconn->lconn_server->lud_host;
456 saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb,
458 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
460 LDAP_FREE( saslhost );
463 if ( rc != LDAP_SUCCESS ) return rc;
465 ctx = ld->ld_defconn->lconn_sasl_authctx;
469 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
471 struct berval authid = BER_BVNULL;
474 fac = ldap_pvt_tls_get_strength( ssl );
475 /* failure is OK, we just can't use SASL EXTERNAL */
476 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
478 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
479 LDAP_FREE( authid.bv_val );
480 #ifdef SASL_CHANNEL_BINDING /* 2.1.25+ */
483 struct berval cbv = { sizeof(cbinding), cbinding };
484 if ( ldap_pvt_tls_get_unique( ssl, &cbv, 0 )) {
485 sasl_channel_binding_t *cb = ldap_memalloc( sizeof(*cb) +
487 void *cb_data; /* used since cb->data is const* */
490 cb->len = cbv.bv_len;
491 cb->data = cb_data = cb+1;
492 memcpy( cb_data, cbv.bv_val, cbv.bv_len );
493 sasl_setprop( ld->ld_defconn->lconn_sasl_authctx,
494 SASL_CHANNEL_BINDING, cb );
495 ld->ld_defconn->lconn_sasl_cbind = cb;
503 /* Check for local */
504 if ( ldap_pvt_url_scheme2proto(
505 ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC )
507 char authid[sizeof("gidNumber=4294967295+uidNumber=4294967295,"
508 "cn=peercred,cn=external,cn=auth")];
509 sprintf( authid, "gidNumber=%u+uidNumber=%u,"
510 "cn=peercred,cn=external,cn=auth",
511 getegid(), geteuid() );
512 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid,
513 LDAP_PVT_SASL_LOCAL_SSF );
517 /* (re)set security properties */
518 sasl_setprop( ctx, SASL_SEC_PROPS,
519 &ld->ld_options.ldo_sasl_secprops );
524 saslrc = sasl_client_start( ctx,
526 #if SASL_VERSION_MAJOR < 2
530 (SASL_CONST char **)&ccred.bv_val,
534 if( pmech == NULL && mech != NULL ) {
538 if( flags != LDAP_SASL_QUIET ) {
540 "SASL/%s authentication started\n",
545 if( saslrc == SASL_INTERACT ) {
547 if( !interact ) break;
548 res = (interact)( ld, flags, defaults, prompts );
550 if( res != LDAP_SUCCESS ) break;
552 } while ( saslrc == SASL_INTERACT );
553 rc = LDAP_SASL_BIND_IN_PROGRESS;
556 /* continuing an in-progress Bind */
557 struct berval *scred = NULL;
559 ctx = ld->ld_defconn->lconn_sasl_authctx;
561 rc = ldap_parse_sasl_bind_result( ld, result, &scred, 0 );
562 if ( rc != LDAP_SUCCESS ) {
568 rc = ldap_result2error( ld, result, 0 );
569 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
571 /* and server provided us with data? */
572 Debug( LDAP_DEBUG_TRACE,
573 "ldap_int_sasl_bind: rc=%d len=%ld\n",
574 rc, scred ? (long) scred->bv_len : -1L, 0 );
582 if ( rc == LDAP_SUCCESS && mech == NULL ) {
591 Debug( LDAP_DEBUG_TRACE,
592 "ldap_int_sasl_bind: no data in step!\n",
596 saslrc = sasl_client_step( ctx,
597 (scred == NULL) ? NULL : scred->bv_val,
598 (scred == NULL) ? 0 : scred->bv_len,
600 (SASL_CONST char **)&ccred.bv_val,
603 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
606 if( saslrc == SASL_INTERACT ) {
608 if( !interact ) break;
609 res = (interact)( ld, flags, defaults, prompts );
610 if( res != LDAP_SUCCESS ) break;
612 } while ( saslrc == SASL_INTERACT );
617 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
618 rc = ld->ld_errno = sasl_err2ldap( saslrc );
619 #if SASL_VERSION_MAJOR >= 2
620 if ( ld->ld_error ) {
621 LDAP_FREE( ld->ld_error );
623 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
628 if ( saslrc == SASL_OK )
631 ccred.bv_len = credlen;
633 if ( rc == LDAP_SASL_BIND_IN_PROGRESS ) {
634 rc = ldap_sasl_bind( ld, dn, mech, &ccred, sctrls, cctrls, msgid );
636 if ( ccred.bv_val != NULL ) {
637 #if SASL_VERSION_MAJOR < 2
638 LDAP_FREE( ccred.bv_val );
642 if ( rc == LDAP_SUCCESS )
643 rc = LDAP_SASL_BIND_IN_PROGRESS;
648 /* Conversation was completed successfully by now */
649 if( flags != LDAP_SASL_QUIET ) {
651 saslrc = sasl_getprop( ctx, SASL_USERNAME,
652 (SASL_CONST void **)(char *) &data );
653 if( saslrc == SASL_OK && data && *data ) {
654 fprintf( stderr, "SASL username: %s\n", data );
657 #if SASL_VERSION_MAJOR < 2
658 saslrc = sasl_getprop( ctx, SASL_REALM,
659 (SASL_CONST void **) &data );
660 if( saslrc == SASL_OK && data && *data ) {
661 fprintf( stderr, "SASL realm: %s\n", data );
667 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **)(char *) &ssf );
668 if( saslrc == SASL_OK ) {
669 if( flags != LDAP_SASL_QUIET ) {
670 fprintf( stderr, "SASL SSF: %lu\n",
671 (unsigned long) *ssf );
675 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
676 sasl_conn_t *oldctx = ld->ld_defconn->lconn_sasl_sockctx;
677 sasl_dispose( &oldctx );
678 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
680 ldap_pvt_sasl_install( ld->ld_defconn->lconn_sb, ctx );
681 ld->ld_defconn->lconn_sasl_sockctx = ctx;
683 if( flags != LDAP_SASL_QUIET ) {
684 fprintf( stderr, "SASL data security layer installed.\n" );
688 ld->ld_defconn->lconn_sasl_authctx = ctx;
695 ldap_int_sasl_external(
703 #if SASL_VERSION_MAJOR < 2
704 sasl_external_properties_t extprops;
706 sasl_ssf_t sasl_ssf = ssf;
709 ctx = conn->lconn_sasl_authctx;
712 return LDAP_LOCAL_ERROR;
715 #if SASL_VERSION_MAJOR >= 2
716 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
718 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
720 memset( &extprops, '\0', sizeof(extprops) );
722 extprops.auth_id = (char *) authid;
724 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
725 (void *) &extprops );
728 if ( sc != SASL_OK ) {
729 return LDAP_LOCAL_ERROR;
746 { BER_BVC("none"), 0, 0, 0 },
747 { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
748 { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
749 { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
750 { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
751 { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
752 { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
753 { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
754 { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
755 { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
756 { BER_BVNULL, 0, 0, 0 }
759 void ldap_pvt_sasl_secprops_unparse(
760 sasl_security_properties_t *secprops,
767 if ( secprops == NULL || out == NULL ) {
772 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
773 if ( sprops[i].ival ) {
776 switch( sprops[i].ival ) {
777 case GOT_MINSSF: v = secprops->min_ssf; break;
778 case GOT_MAXSSF: v = secprops->max_ssf; break;
779 case GOT_MAXBUF: v = secprops->maxbufsize; break;
781 /* It is the default, ignore it */
782 if ( v == sprops[i].idef ) continue;
784 l += sprops[i].key.bv_len + 24;
785 } else if ( sprops[i].sflag ) {
786 if ( sprops[i].sflag & secprops->security_flags ) {
787 l += sprops[i].key.bv_len;
789 } else if ( secprops->security_flags == 0 ) {
790 l += sprops[i].key.bv_len;
797 out->bv_val = LDAP_MALLOC( l );
798 if ( out->bv_val == NULL ) {
805 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
806 if ( sprops[i].ival ) {
809 switch( sprops[i].ival ) {
810 case GOT_MINSSF: v = secprops->min_ssf; break;
811 case GOT_MAXSSF: v = secprops->max_ssf; break;
812 case GOT_MAXBUF: v = secprops->maxbufsize; break;
814 /* It is the default, ignore it */
815 if ( v == sprops[i].idef ) continue;
817 if ( comma ) *ptr++ = ',';
818 ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
820 } else if ( sprops[i].sflag ) {
821 if ( sprops[i].sflag & secprops->security_flags ) {
822 if ( comma ) *ptr++ = ',';
823 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
826 } else if ( secprops->security_flags == 0 ) {
827 if ( comma ) *ptr++ = ',';
828 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
832 out->bv_len = ptr - out->bv_val;
835 int ldap_pvt_sasl_secprops(
837 sasl_security_properties_t *secprops )
843 sasl_ssf_t max_ssf = 0;
845 sasl_ssf_t min_ssf = 0;
847 unsigned maxbufsize = 0;
848 int got_maxbufsize = 0;
850 if( secprops == NULL ) {
851 return LDAP_PARAM_ERROR;
853 props = ldap_str2charray( in, "," );
854 if( props == NULL ) {
855 return LDAP_PARAM_ERROR;
858 for( i=0; props[i]; i++ ) {
859 l = strlen( props[i] );
860 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
861 if ( l < sprops[j].key.bv_len ) continue;
862 if ( strncasecmp( props[i], sprops[j].key.bv_val,
863 sprops[j].key.bv_len )) continue;
864 if ( sprops[j].ival ) {
867 if ( !isdigit( (unsigned char)props[i][sprops[j].key.bv_len] ))
869 v = strtoul( &props[i][sprops[j].key.bv_len], &next, 10 );
870 if ( next == &props[i][sprops[j].key.bv_len] || next[0] != '\0' ) continue;
871 switch( sprops[j].ival ) {
873 min_ssf = v; got_min_ssf++; break;
875 max_ssf = v; got_max_ssf++; break;
877 maxbufsize = v; got_maxbufsize++; break;
880 if ( props[i][sprops[j].key.bv_len] ) continue;
881 if ( sprops[j].sflag )
882 sflags |= sprops[j].sflag;
889 if ( BER_BVISNULL( &sprops[j].key )) {
890 ldap_charray_free( props );
891 return LDAP_NOT_SUPPORTED;
896 secprops->security_flags = sflags;
899 secprops->min_ssf = min_ssf;
902 secprops->max_ssf = max_ssf;
905 secprops->maxbufsize = maxbufsize;
908 ldap_charray_free( props );
913 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
918 case LDAP_OPT_X_SASL_SECPROPS:
919 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
920 if( rc == LDAP_SUCCESS ) return 0;
927 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
929 if ( option == LDAP_OPT_X_SASL_MECHLIST ) {
930 *(char ***)arg = (char **)sasl_global_listmech();
938 case LDAP_OPT_X_SASL_MECH: {
939 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
940 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
942 case LDAP_OPT_X_SASL_REALM: {
943 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
944 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
946 case LDAP_OPT_X_SASL_AUTHCID: {
947 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
948 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
950 case LDAP_OPT_X_SASL_AUTHZID: {
951 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
952 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
955 case LDAP_OPT_X_SASL_SSF: {
960 if( ld->ld_defconn == NULL ) {
964 ctx = ld->ld_defconn->lconn_sasl_sockctx;
970 sc = sasl_getprop( ctx, SASL_SSF,
971 (SASL_CONST void **)(char *) &ssf );
973 if ( sc != SASL_OK ) {
977 *(ber_len_t *)arg = *ssf;
980 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
981 /* this option is write only */
984 case LDAP_OPT_X_SASL_SSF_MIN:
985 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
987 case LDAP_OPT_X_SASL_SSF_MAX:
988 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
990 case LDAP_OPT_X_SASL_MAXBUFSIZE:
991 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
993 case LDAP_OPT_X_SASL_NOCANON:
994 *(int *)arg = (int) LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
997 case LDAP_OPT_X_SASL_USERNAME: {
1002 if( ld->ld_defconn == NULL ) {
1006 ctx = ld->ld_defconn->lconn_sasl_authctx;
1008 if ( ctx == NULL ) {
1012 sc = sasl_getprop( ctx, SASL_USERNAME,
1013 (SASL_CONST void **)(char **) &username );
1015 if ( sc != SASL_OK ) {
1019 *(char **)arg = username ? LDAP_STRDUP( username ) : NULL;
1022 case LDAP_OPT_X_SASL_SECPROPS:
1023 /* this option is write only */
1026 #ifdef SASL_GSS_CREDS
1027 case LDAP_OPT_X_SASL_GSS_CREDS: {
1031 if ( ld->ld_defconn == NULL )
1034 ctx = ld->ld_defconn->lconn_sasl_authctx;
1038 sc = sasl_getprop( ctx, SASL_GSS_CREDS, arg );
1039 if ( sc != SASL_OK )
1052 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1057 if ( arg == NULL && option != LDAP_OPT_X_SASL_NOCANON )
1061 case LDAP_OPT_X_SASL_SSF:
1062 case LDAP_OPT_X_SASL_USERNAME:
1063 /* This option is read-only */
1066 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1068 #if SASL_VERSION_MAJOR < 2
1069 sasl_external_properties_t extprops;
1071 sasl_ssf_t sasl_ssf;
1075 if( ld->ld_defconn == NULL ) {
1079 ctx = ld->ld_defconn->lconn_sasl_authctx;
1081 if ( ctx == NULL ) {
1085 #if SASL_VERSION_MAJOR >= 2
1086 sasl_ssf = * (ber_len_t *)arg;
1087 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf);
1089 memset(&extprops, 0L, sizeof(extprops));
1091 extprops.ssf = * (ber_len_t *) arg;
1093 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1094 (void *) &extprops );
1097 if ( sc != SASL_OK ) {
1102 case LDAP_OPT_X_SASL_SSF_MIN:
1103 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1105 case LDAP_OPT_X_SASL_SSF_MAX:
1106 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1108 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1109 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1111 case LDAP_OPT_X_SASL_NOCANON:
1112 if ( arg == LDAP_OPT_OFF ) {
1113 LDAP_BOOL_CLR(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1115 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1119 case LDAP_OPT_X_SASL_SECPROPS: {
1121 sc = ldap_pvt_sasl_secprops( (char *) arg,
1122 &ld->ld_options.ldo_sasl_secprops );
1124 return sc == LDAP_SUCCESS ? 0 : -1;
1127 #ifdef SASL_GSS_CREDS
1128 case LDAP_OPT_X_SASL_GSS_CREDS: {
1132 if ( ld->ld_defconn == NULL )
1135 ctx = ld->ld_defconn->lconn_sasl_authctx;
1139 sc = sasl_setprop( ctx, SASL_GSS_CREDS, arg );
1140 if ( sc != SASL_OK )
1152 #ifdef LDAP_R_COMPILE
1153 #define LDAP_DEBUG_R_SASL
1154 void *ldap_pvt_sasl_mutex_new(void)
1156 ldap_pvt_thread_mutex_t *mutex;
1158 mutex = (ldap_pvt_thread_mutex_t *) LDAP_CALLOC( 1,
1159 sizeof(ldap_pvt_thread_mutex_t) );
1161 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1165 #ifndef LDAP_DEBUG_R_SASL
1167 #endif /* !LDAP_DEBUG_R_SASL */
1171 int ldap_pvt_sasl_mutex_lock(void *mutex)
1173 #ifdef LDAP_DEBUG_R_SASL
1174 if ( mutex == NULL ) {
1177 #else /* !LDAP_DEBUG_R_SASL */
1178 assert( mutex != NULL );
1179 #endif /* !LDAP_DEBUG_R_SASL */
1180 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1181 ? SASL_FAIL : SASL_OK;
1184 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1186 #ifdef LDAP_DEBUG_R_SASL
1187 if ( mutex == NULL ) {
1190 #else /* !LDAP_DEBUG_R_SASL */
1191 assert( mutex != NULL );
1192 #endif /* !LDAP_DEBUG_R_SASL */
1193 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1194 ? SASL_FAIL : SASL_OK;
1197 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1199 #ifdef LDAP_DEBUG_R_SASL
1200 if ( mutex == NULL ) {
1203 #else /* !LDAP_DEBUG_R_SASL */
1204 assert( mutex != NULL );
1205 #endif /* !LDAP_DEBUG_R_SASL */
1206 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1212 int ldap_int_sasl_init( void )
1213 { return LDAP_SUCCESS; }
1215 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1216 { return LDAP_SUCCESS; }
1223 LDAPControl **sctrls,
1224 LDAPControl **cctrls,
1226 LDAP_SASL_INTERACT_PROC *interact,
1228 LDAPMessage *result,
1231 { return LDAP_NOT_SUPPORTED; }
1234 ldap_int_sasl_external(
1237 const char * authid,
1239 { return LDAP_SUCCESS; }
1241 #endif /* HAVE_CYRUS_SASL */