2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 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;
450 int nocanon = (int)LDAP_BOOL_GET( &ld->ld_options,
451 LDAP_BOOL_SASL_NOCANON );
453 /* If we don't need to canonicalize just use the host
457 saslhost = ld->ld_defconn->lconn_server->lud_host;
459 saslhost = ldap_host_connected_to( ld->ld_defconn->lconn_sb,
461 rc = ldap_int_sasl_open( ld, ld->ld_defconn, saslhost );
463 LDAP_FREE( saslhost );
466 if ( rc != LDAP_SUCCESS ) return rc;
468 ctx = ld->ld_defconn->lconn_sasl_authctx;
472 ssl = ldap_pvt_tls_sb_ctx( ld->ld_defconn->lconn_sb );
474 struct berval authid = BER_BVNULL;
477 fac = ldap_pvt_tls_get_strength( ssl );
478 /* failure is OK, we just can't use SASL EXTERNAL */
479 (void) ldap_pvt_tls_get_my_dn( ssl, &authid, NULL, 0 );
481 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid.bv_val, fac );
482 LDAP_FREE( authid.bv_val );
487 /* Check for local */
488 if ( ldap_pvt_url_scheme2proto(
489 ld->ld_defconn->lconn_server->lud_scheme ) == LDAP_PROTO_IPC )
491 char authid[sizeof("gidNumber=4294967295+uidNumber=4294967295,"
492 "cn=peercred,cn=external,cn=auth")];
493 sprintf( authid, "gidNumber=%u+uidNumber=%u,"
494 "cn=peercred,cn=external,cn=auth",
495 getegid(), geteuid() );
496 (void) ldap_int_sasl_external( ld, ld->ld_defconn, authid,
497 LDAP_PVT_SASL_LOCAL_SSF );
501 /* (re)set security properties */
502 sasl_setprop( ctx, SASL_SEC_PROPS,
503 &ld->ld_options.ldo_sasl_secprops );
509 saslrc = sasl_client_start( ctx,
511 #if SASL_VERSION_MAJOR < 2
515 (SASL_CONST char **)&ccred.bv_val,
519 if( pmech == NULL && mech != NULL ) {
522 if( flags != LDAP_SASL_QUIET ) {
524 "SASL/%s authentication started\n",
529 if( saslrc == SASL_INTERACT ) {
531 if( !interact ) break;
532 res = (interact)( ld, flags, defaults, prompts );
534 if( res != LDAP_SUCCESS ) break;
536 } while ( saslrc == SASL_INTERACT );
538 ccred.bv_len = credlen;
540 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
541 rc = ld->ld_errno = sasl_err2ldap( saslrc );
542 #if SASL_VERSION_MAJOR >= 2
543 if ( ld->ld_error ) {
544 LDAP_FREE( ld->ld_error );
546 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
552 struct berval *scred;
557 rc = ldap_sasl_bind_s( ld, dn, mech, &ccred, sctrls, cctrls,
560 if ( ccred.bv_val != NULL ) {
561 #if SASL_VERSION_MAJOR < 2
562 LDAP_FREE( ccred.bv_val );
567 if ( rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS ) {
569 /* and server provided us with data? */
570 Debug( LDAP_DEBUG_TRACE,
571 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
572 rc, saslrc, scred ? (long) scred->bv_len : -1L );
580 if( rc == LDAP_SUCCESS && saslrc == SASL_OK ) {
581 /* we're done, no need to step */
583 /* but we got additional data? */
584 #define KLUDGE_FOR_MSAD
585 #ifdef KLUDGE_FOR_MSAD
587 * MSAD provides empty additional data in violation of LDAP
588 * technical specifications. As no existing SASL mechanism
589 * allows empty data with an outcome message, just ignore it
590 * for now. Hopefully MS will fix their bug before someone
591 * defines a mechanism with possibly empty additional data.
593 if( scred->bv_len == 0 ) {
594 Debug( LDAP_DEBUG_ANY,
595 "ldap_int_sasl_bind: ignoring "
596 " bogus empty data provided with SASL outcome message.\n",
597 rc, saslrc, scred->bv_len );
602 Debug( LDAP_DEBUG_TRACE,
603 "ldap_int_sasl_bind: rc=%d sasl=%d len=%ld\n",
604 rc, saslrc, scred->bv_len );
605 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
616 Debug( LDAP_DEBUG_TRACE,
617 "ldap_int_sasl_bind: no data in step!\n",
621 saslrc = sasl_client_step( ctx,
622 (scred == NULL) ? NULL : scred->bv_val,
623 (scred == NULL) ? 0 : scred->bv_len,
625 (SASL_CONST char **)&ccred.bv_val,
628 Debug( LDAP_DEBUG_TRACE, "sasl_client_step: %d\n",
631 if( saslrc == SASL_INTERACT ) {
633 if( !interact ) break;
634 res = (interact)( ld, flags, defaults, prompts );
635 if( res != LDAP_SUCCESS ) break;
637 } while ( saslrc == SASL_INTERACT );
639 ccred.bv_len = credlen;
642 if ( (saslrc != SASL_OK) && (saslrc != SASL_CONTINUE) ) {
643 ld->ld_errno = sasl_err2ldap( saslrc );
644 #if SASL_VERSION_MAJOR >= 2
645 if ( ld->ld_error ) {
646 LDAP_FREE( ld->ld_error );
648 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
653 } while ( rc == LDAP_SASL_BIND_IN_PROGRESS );
655 if ( rc != LDAP_SUCCESS ) goto done;
657 if ( saslrc != SASL_OK ) {
658 #if SASL_VERSION_MAJOR >= 2
659 if ( ld->ld_error ) {
660 LDAP_FREE( ld->ld_error );
662 ld->ld_error = LDAP_STRDUP( sasl_errdetail( ctx ) );
664 rc = ld->ld_errno = sasl_err2ldap( saslrc );
668 if( flags != LDAP_SASL_QUIET ) {
669 saslrc = sasl_getprop( ctx, SASL_USERNAME,
670 (SASL_CONST void **)(char *) &data );
671 if( saslrc == SASL_OK && data && *data ) {
672 fprintf( stderr, "SASL username: %s\n", data );
675 #if SASL_VERSION_MAJOR < 2
676 saslrc = sasl_getprop( ctx, SASL_REALM,
677 (SASL_CONST void **) &data );
678 if( saslrc == SASL_OK && data && *data ) {
679 fprintf( stderr, "SASL realm: %s\n", data );
684 saslrc = sasl_getprop( ctx, SASL_SSF, (SASL_CONST void **)(char *) &ssf );
685 if( saslrc == SASL_OK ) {
686 if( flags != LDAP_SASL_QUIET ) {
687 fprintf( stderr, "SASL SSF: %lu\n",
688 (unsigned long) *ssf );
692 if ( ld->ld_defconn->lconn_sasl_sockctx ) {
693 oldctx = ld->ld_defconn->lconn_sasl_sockctx;
694 sasl_dispose( &oldctx );
695 ldap_pvt_sasl_remove( ld->ld_defconn->lconn_sb );
697 ldap_pvt_sasl_install( ld->ld_defconn->lconn_sb, ctx );
698 ld->ld_defconn->lconn_sasl_sockctx = ctx;
700 if( flags != LDAP_SASL_QUIET ) {
701 fprintf( stderr, "SASL data security layer installed.\n" );
705 ld->ld_defconn->lconn_sasl_authctx = ctx;
712 ldap_int_sasl_external(
720 #if SASL_VERSION_MAJOR < 2
721 sasl_external_properties_t extprops;
723 sasl_ssf_t sasl_ssf = ssf;
726 ctx = conn->lconn_sasl_authctx;
729 return LDAP_LOCAL_ERROR;
732 #if SASL_VERSION_MAJOR >= 2
733 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
735 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
737 memset( &extprops, '\0', sizeof(extprops) );
739 extprops.auth_id = (char *) authid;
741 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
742 (void *) &extprops );
745 if ( sc != SASL_OK ) {
746 return LDAP_LOCAL_ERROR;
763 { BER_BVC("none"), 0, 0, 0 },
764 { BER_BVC("nodict"), SASL_SEC_NODICTIONARY, 0, 0 },
765 { BER_BVC("noplain"), SASL_SEC_NOPLAINTEXT, 0, 0 },
766 { BER_BVC("noactive"), SASL_SEC_NOACTIVE, 0, 0 },
767 { BER_BVC("passcred"), SASL_SEC_PASS_CREDENTIALS, 0, 0 },
768 { BER_BVC("forwardsec"), SASL_SEC_FORWARD_SECRECY, 0, 0 },
769 { BER_BVC("noanonymous"), SASL_SEC_NOANONYMOUS, 0, 0 },
770 { BER_BVC("minssf="), 0, GOT_MINSSF, 0 },
771 { BER_BVC("maxssf="), 0, GOT_MAXSSF, INT_MAX },
772 { BER_BVC("maxbufsize="), 0, GOT_MAXBUF, 65536 },
773 { BER_BVNULL, 0, 0, 0 }
776 void ldap_pvt_sasl_secprops_unparse(
777 sasl_security_properties_t *secprops,
784 if ( secprops == NULL || out == 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 l += sprops[i].key.bv_len + 24;
802 } else if ( sprops[i].sflag ) {
803 if ( sprops[i].sflag & secprops->security_flags ) {
804 l += sprops[i].key.bv_len;
806 } else if ( secprops->security_flags == 0 ) {
807 l += sprops[i].key.bv_len;
814 out->bv_val = LDAP_MALLOC( l );
815 if ( out->bv_val == NULL ) {
822 for ( i=0; !BER_BVISNULL( &sprops[i].key ); i++ ) {
823 if ( sprops[i].ival ) {
826 switch( sprops[i].ival ) {
827 case GOT_MINSSF: v = secprops->min_ssf; break;
828 case GOT_MAXSSF: v = secprops->max_ssf; break;
829 case GOT_MAXBUF: v = secprops->maxbufsize; break;
831 /* It is the default, ignore it */
832 if ( v == sprops[i].idef ) continue;
834 if ( comma ) *ptr++ = ',';
835 ptr += sprintf(ptr, "%s%d", sprops[i].key.bv_val, v );
837 } else if ( sprops[i].sflag ) {
838 if ( sprops[i].sflag & secprops->security_flags ) {
839 if ( comma ) *ptr++ = ',';
840 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
843 } else if ( secprops->security_flags == 0 ) {
844 if ( comma ) *ptr++ = ',';
845 ptr += sprintf(ptr, "%s", sprops[i].key.bv_val );
849 out->bv_len = ptr - out->bv_val;
852 int ldap_pvt_sasl_secprops(
854 sasl_security_properties_t *secprops )
860 sasl_ssf_t max_ssf = 0;
862 sasl_ssf_t min_ssf = 0;
864 unsigned maxbufsize = 0;
865 int got_maxbufsize = 0;
867 if( secprops == NULL ) {
868 return LDAP_PARAM_ERROR;
870 props = ldap_str2charray( in, "," );
871 if( props == NULL ) {
872 return LDAP_PARAM_ERROR;
875 for( i=0; props[i]; i++ ) {
876 l = strlen( props[i] );
877 for ( j=0; !BER_BVISNULL( &sprops[j].key ); j++ ) {
878 if ( l < sprops[j].key.bv_len ) continue;
879 if ( strncasecmp( props[i], sprops[j].key.bv_val,
880 sprops[j].key.bv_len )) continue;
881 if ( sprops[j].ival ) {
884 if ( !isdigit( (unsigned char)props[i][sprops[j].key.bv_len] ))
886 v = strtoul( &props[i][sprops[j].key.bv_len], &next, 10 );
887 if ( next == &props[i][sprops[j].key.bv_len] || next[0] != '\0' ) continue;
888 switch( sprops[j].ival ) {
890 min_ssf = v; got_min_ssf++; break;
892 max_ssf = v; got_max_ssf++; break;
894 maxbufsize = v; got_maxbufsize++; break;
897 if ( props[i][sprops[j].key.bv_len] ) continue;
898 if ( sprops[j].sflag )
899 sflags |= sprops[j].sflag;
906 if ( BER_BVISNULL( &sprops[j].key )) {
907 ldap_charray_free( props );
908 return LDAP_NOT_SUPPORTED;
913 secprops->security_flags = sflags;
916 secprops->min_ssf = min_ssf;
919 secprops->max_ssf = max_ssf;
922 secprops->maxbufsize = maxbufsize;
925 ldap_charray_free( props );
930 ldap_int_sasl_config( struct ldapoptions *lo, int option, const char *arg )
935 case LDAP_OPT_X_SASL_SECPROPS:
936 rc = ldap_pvt_sasl_secprops( arg, &lo->ldo_sasl_secprops );
937 if( rc == LDAP_SUCCESS ) return 0;
944 ldap_int_sasl_get_option( LDAP *ld, int option, void *arg )
946 if ( option == LDAP_OPT_X_SASL_MECHLIST ) {
947 if ( ldap_int_sasl_init() )
949 *(char ***)arg = (char **)sasl_global_listmech();
957 case LDAP_OPT_X_SASL_MECH: {
958 *(char **)arg = ld->ld_options.ldo_def_sasl_mech
959 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_mech ) : NULL;
961 case LDAP_OPT_X_SASL_REALM: {
962 *(char **)arg = ld->ld_options.ldo_def_sasl_realm
963 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_realm ) : NULL;
965 case LDAP_OPT_X_SASL_AUTHCID: {
966 *(char **)arg = ld->ld_options.ldo_def_sasl_authcid
967 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authcid ) : NULL;
969 case LDAP_OPT_X_SASL_AUTHZID: {
970 *(char **)arg = ld->ld_options.ldo_def_sasl_authzid
971 ? LDAP_STRDUP( ld->ld_options.ldo_def_sasl_authzid ) : NULL;
974 case LDAP_OPT_X_SASL_SSF: {
979 if( ld->ld_defconn == NULL ) {
983 ctx = ld->ld_defconn->lconn_sasl_sockctx;
989 sc = sasl_getprop( ctx, SASL_SSF,
990 (SASL_CONST void **)(char *) &ssf );
992 if ( sc != SASL_OK ) {
996 *(ber_len_t *)arg = *ssf;
999 case LDAP_OPT_X_SASL_SSF_EXTERNAL:
1000 /* this option is write only */
1003 case LDAP_OPT_X_SASL_SSF_MIN:
1004 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.min_ssf;
1006 case LDAP_OPT_X_SASL_SSF_MAX:
1007 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.max_ssf;
1009 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1010 *(ber_len_t *)arg = ld->ld_options.ldo_sasl_secprops.maxbufsize;
1012 case LDAP_OPT_X_SASL_NOCANON:
1013 *(int *)arg = (int) LDAP_BOOL_GET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1016 case LDAP_OPT_X_SASL_SECPROPS:
1017 /* this option is write only */
1027 ldap_int_sasl_set_option( LDAP *ld, int option, void *arg )
1032 if ( arg == NULL && option != LDAP_OPT_X_SASL_NOCANON )
1036 case LDAP_OPT_X_SASL_SSF:
1037 /* This option is read-only */
1040 case LDAP_OPT_X_SASL_SSF_EXTERNAL: {
1042 #if SASL_VERSION_MAJOR < 2
1043 sasl_external_properties_t extprops;
1045 sasl_ssf_t sasl_ssf;
1049 if( ld->ld_defconn == NULL ) {
1053 ctx = ld->ld_defconn->lconn_sasl_authctx;
1055 if ( ctx == NULL ) {
1059 #if SASL_VERSION_MAJOR >= 2
1060 sasl_ssf = * (ber_len_t *)arg;
1061 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf);
1063 memset(&extprops, 0L, sizeof(extprops));
1065 extprops.ssf = * (ber_len_t *) arg;
1067 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1068 (void *) &extprops );
1071 if ( sc != SASL_OK ) {
1076 case LDAP_OPT_X_SASL_SSF_MIN:
1077 ld->ld_options.ldo_sasl_secprops.min_ssf = *(ber_len_t *)arg;
1079 case LDAP_OPT_X_SASL_SSF_MAX:
1080 ld->ld_options.ldo_sasl_secprops.max_ssf = *(ber_len_t *)arg;
1082 case LDAP_OPT_X_SASL_MAXBUFSIZE:
1083 ld->ld_options.ldo_sasl_secprops.maxbufsize = *(ber_len_t *)arg;
1085 case LDAP_OPT_X_SASL_NOCANON:
1086 if ( arg == LDAP_OPT_OFF ) {
1087 LDAP_BOOL_CLR(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1089 LDAP_BOOL_SET(&ld->ld_options, LDAP_BOOL_SASL_NOCANON );
1093 case LDAP_OPT_X_SASL_SECPROPS: {
1095 sc = ldap_pvt_sasl_secprops( (char *) arg,
1096 &ld->ld_options.ldo_sasl_secprops );
1098 return sc == LDAP_SUCCESS ? 0 : -1;
1107 #ifdef LDAP_R_COMPILE
1108 #define LDAP_DEBUG_R_SASL
1109 void *ldap_pvt_sasl_mutex_new(void)
1111 ldap_pvt_thread_mutex_t *mutex;
1113 mutex = (ldap_pvt_thread_mutex_t *) LDAP_CALLOC( 1,
1114 sizeof(ldap_pvt_thread_mutex_t) );
1116 if ( ldap_pvt_thread_mutex_init( mutex ) == 0 ) {
1119 #ifndef LDAP_DEBUG_R_SASL
1121 #endif /* !LDAP_DEBUG_R_SASL */
1125 int ldap_pvt_sasl_mutex_lock(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 return ldap_pvt_thread_mutex_lock( (ldap_pvt_thread_mutex_t *)mutex )
1135 ? SASL_FAIL : SASL_OK;
1138 int ldap_pvt_sasl_mutex_unlock(void *mutex)
1140 #ifdef LDAP_DEBUG_R_SASL
1141 if ( mutex == NULL ) {
1144 #else /* !LDAP_DEBUG_R_SASL */
1145 assert( mutex != NULL );
1146 #endif /* !LDAP_DEBUG_R_SASL */
1147 return ldap_pvt_thread_mutex_unlock( (ldap_pvt_thread_mutex_t *)mutex )
1148 ? SASL_FAIL : SASL_OK;
1151 void ldap_pvt_sasl_mutex_dispose(void *mutex)
1153 #ifdef LDAP_DEBUG_R_SASL
1154 if ( mutex == NULL ) {
1157 #else /* !LDAP_DEBUG_R_SASL */
1158 assert( mutex != NULL );
1159 #endif /* !LDAP_DEBUG_R_SASL */
1160 (void) ldap_pvt_thread_mutex_destroy( (ldap_pvt_thread_mutex_t *)mutex );
1166 int ldap_int_sasl_init( void )
1167 { return LDAP_SUCCESS; }
1169 int ldap_int_sasl_close( LDAP *ld, LDAPConn *lc )
1170 { return LDAP_SUCCESS; }
1177 LDAPControl **sctrls,
1178 LDAPControl **cctrls,
1180 LDAP_SASL_INTERACT_PROC *interact,
1182 { return LDAP_NOT_SUPPORTED; }
1185 ldap_int_sasl_external(
1188 const char * authid,
1190 { return LDAP_SUCCESS; }
1192 #endif /* HAVE_CYRUS_SASL */