2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2011 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>.
23 #include <ac/stdlib.h>
24 #include <ac/string.h>
35 #ifdef HAVE_CYRUS_SASL
36 # ifdef HAVE_SASL_SASL_H
37 # include <sasl/sasl.h>
38 # include <sasl/saslplug.h>
41 # include <saslplug.h>
44 # define SASL_CONST const
46 #define SASL_VERSION_FULL ((SASL_VERSION_MAJOR << 16) |\
47 (SASL_VERSION_MINOR << 8) | SASL_VERSION_STEP)
49 static sasl_security_properties_t sasl_secprops;
50 #elif defined( SLAP_BUILTIN_SASL )
52 * built-in SASL implementation
53 * only supports EXTERNAL
55 typedef struct sasl_ctx {
56 slap_ssf_t sc_external_ssf;
57 struct berval sc_external_id;
64 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
66 char *slap_sasl_auxprops;
68 #ifdef HAVE_CYRUS_SASL
70 /* Just use our internal auxprop by default */
74 const char *plugin_name,
79 if ( strcmp( option, "auxprop_plugin" )) {
82 if ( slap_sasl_auxprops )
83 *result = slap_sasl_auxprops;
95 Connection *conn = context;
99 if ( message == NULL ) {
100 return SASL_BADPARAM;
105 level = LDAP_DEBUG_NONE;
109 level = LDAP_DEBUG_ANY;
113 level = LDAP_DEBUG_ANY;
117 level = LDAP_DEBUG_TRACE;
121 level = LDAP_DEBUG_TRACE;
125 level = LDAP_DEBUG_TRACE;
129 level = LDAP_DEBUG_TRACE;
133 level = LDAP_DEBUG_TRACE;
134 label = "Password Trace";
137 return SASL_BADPARAM;
140 Debug( level, "SASL [conn=%ld] %s: %s\n",
141 conn ? (long) conn->c_connid: -1L,
148 static const char *slap_propnames[] = {
149 "*slapConn", "*slapAuthcDNlen", "*slapAuthcDN",
150 "*slapAuthzDNlen", "*slapAuthzDN", NULL };
152 #ifdef SLAP_AUXPROP_DONTUSECOPY
153 int slap_dontUseCopy_ignore;
154 BerVarray slap_dontUseCopy_propnames;
155 #endif /* SLAP_AUXPROP_DONTUSECOPY */
157 static Filter generic_filter = { LDAP_FILTER_PRESENT, { 0 }, NULL };
158 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
160 #define SLAP_SASL_PROP_CONN 0
161 #define SLAP_SASL_PROP_AUTHCLEN 1
162 #define SLAP_SASL_PROP_AUTHC 2
163 #define SLAP_SASL_PROP_AUTHZLEN 3
164 #define SLAP_SASL_PROP_AUTHZ 4
165 #define SLAP_SASL_PROP_COUNT 5 /* Number of properties we used */
167 typedef struct lookup_info {
169 const struct propval *list;
170 sasl_server_params_t *sparams;
173 static slap_response sasl_ap_lookup;
175 static struct berval sc_cleartext = BER_BVC("{CLEARTEXT}");
178 sasl_ap_lookup( Operation *op, SlapReply *rs )
181 AttributeDescription *ad;
185 lookup_info *sl = (lookup_info *)op->o_callback->sc_private;
187 /* return the actual error code,
188 * to allow caller to handle specific errors
190 if (rs->sr_type != REP_SEARCH) return rs->sr_err;
192 for( i = 0; sl->list[i].name; i++ ) {
193 const char *name = sl->list[i].name;
195 if ( name[0] == '*' ) {
196 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
197 /* Skip our private properties */
198 if ( !strcmp( name, slap_propnames[0] )) {
199 i += SLAP_SASL_PROP_COUNT - 1;
203 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
206 if ( sl->list[i].values ) {
207 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
210 rc = slap_str2ad( name, &ad, &text );
211 if ( rc != LDAP_SUCCESS ) {
212 Debug( LDAP_DEBUG_TRACE,
213 "slap_ap_lookup: str2ad(%s): %s\n", name, text, 0 );
217 /* If it's the rootdn and a rootpw was present, we already set
218 * it so don't override it here.
220 if ( ad == slap_schema.si_ad_userPassword && sl->list[i].values &&
221 be_isroot_dn( op->o_bd, &op->o_req_ndn ))
224 a = attr_find( rs->sr_entry->e_attrs, ad );
226 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
229 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
230 sl->sparams->utils->prop_erase( sl->sparams->propctx,
233 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
234 /* ITS#3846 don't give hashed passwords to SASL */
235 if ( ad == slap_schema.si_ad_userPassword &&
236 bv->bv_val[0] == '{' /*}*/ )
238 if ( lutil_passwd_scheme( bv->bv_val ) ) {
239 /* If it's not a recognized scheme, just assume it's
240 * a cleartext password that happened to include brackets.
242 * If it's a recognized scheme, skip this value, unless the
243 * scheme is {CLEARTEXT}. In that case, skip over the
244 * scheme name and use the remainder. If there is nothing
245 * past the scheme name, skip this value.
247 #ifdef SLAPD_CLEARTEXT
248 if ( !strncasecmp( bv->bv_val, sc_cleartext.bv_val,
249 sc_cleartext.bv_len )) {
251 cbv.bv_len = bv->bv_len - sc_cleartext.bv_len;
252 if ( cbv.bv_len > 0 ) {
253 cbv.bv_val = bv->bv_val + sc_cleartext.bv_len;
254 sl->sparams->utils->prop_set( sl->sparams->propctx,
255 sl->list[i].name, cbv.bv_val, cbv.bv_len );
262 sl->sparams->utils->prop_set( sl->sparams->propctx,
263 sl->list[i].name, bv->bv_val, bv->bv_len );
269 #if SASL_VERSION_FULL >= 0x020118
276 sasl_server_params_t *sparams,
281 OperationBuffer opbuf = {{ NULL }};
282 Operation *op = (Operation *)&opbuf;
284 Connection *conn = NULL;
286 int rc = LDAP_SUCCESS;
287 #ifdef SLAP_AUXPROP_DONTUSECOPY
289 BackendDB *dontUseCopy_bd = NULL;
290 #endif /* SLAP_AUXPROP_DONTUSECOPY */
292 sl.list = sparams->utils->prop_get( sparams->propctx );
293 sl.sparams = sparams;
296 /* Find our DN and conn first */
297 for( i = 0; sl.list[i].name; i++ ) {
298 if ( sl.list[i].name[0] == '*' ) {
299 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
300 if ( sl.list[i].values && sl.list[i].values[0] )
301 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
304 if ( flags & SASL_AUXPROP_AUTHZID ) {
305 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZLEN] )) {
306 if ( sl.list[i].values && sl.list[i].values[0] )
307 AC_MEMCPY( &op->o_req_ndn.bv_len, sl.list[i].values[0],
308 sizeof( op->o_req_ndn.bv_len ) );
309 } else if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZ] )) {
310 if ( sl.list[i].values )
311 op->o_req_ndn.bv_val = (char *)sl.list[i].values[0];
316 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
317 if ( sl.list[i].values && sl.list[i].values[0] )
318 AC_MEMCPY( &op->o_req_ndn.bv_len, sl.list[i].values[0],
319 sizeof( op->o_req_ndn.bv_len ) );
320 } else if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
321 if ( sl.list[i].values ) {
322 op->o_req_ndn.bv_val = (char *)sl.list[i].values[0];
323 if ( !(flags & SASL_AUXPROP_AUTHZID) )
327 #ifdef SLAP_AUXPROP_DONTUSECOPY
328 if ( slap_dontUseCopy_propnames != NULL ) {
331 ber_str2bv( &sl.list[i].name[1], 0, 1, &bv );
332 for ( j = 0; !BER_BVISNULL( &slap_dontUseCopy_propnames[ j ]); j++ ) {
333 if ( bvmatch( &bv, &slap_dontUseCopy_propnames[ j ] ) ) {
339 #endif /* SLAP_AUXPROP_DONTUSECOPY */
343 /* Now see what else needs to be fetched */
344 for( i = 0; sl.list[i].name; i++ ) {
345 const char *name = sl.list[i].name;
347 if ( name[0] == '*' ) {
348 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
349 /* Skip our private properties */
350 if ( !strcmp( name, slap_propnames[0] )) {
351 i += SLAP_SASL_PROP_COUNT - 1;
355 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
358 if ( sl.list[i].values ) {
359 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
366 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
370 op->o_bd = select_backend( &op->o_req_ndn, 1 );
373 /* For rootdn, see if we can use the rootpw */
374 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) &&
375 !BER_BVISEMPTY( &op->o_bd->be_rootpw )) {
376 struct berval cbv = BER_BVNULL;
378 /* If there's a recognized scheme, see if it's CLEARTEXT */
379 if ( lutil_passwd_scheme( op->o_bd->be_rootpw.bv_val )) {
380 if ( !strncasecmp( op->o_bd->be_rootpw.bv_val,
381 sc_cleartext.bv_val, sc_cleartext.bv_len )) {
383 /* If it's CLEARTEXT, skip past scheme spec */
384 cbv.bv_len = op->o_bd->be_rootpw.bv_len -
387 cbv.bv_val = op->o_bd->be_rootpw.bv_val +
391 /* No scheme, use the whole value */
393 cbv = op->o_bd->be_rootpw;
395 if ( !BER_BVISEMPTY( &cbv )) {
396 for( i = 0; sl.list[i].name; i++ ) {
397 const char *name = sl.list[i].name;
399 if ( name[0] == '*' ) {
400 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
402 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
405 if ( !strcasecmp(name,"userPassword") ) {
406 sl.sparams->utils->prop_set( sl.sparams->propctx,
407 sl.list[i].name, cbv.bv_val, cbv.bv_len );
414 #ifdef SLAP_AUXPROP_DONTUSECOPY
415 if ( SLAP_SHADOW( op->o_bd ) && dontUseCopy ) {
416 dontUseCopy_bd = op->o_bd;
417 op->o_bd = frontendDB;
421 #endif /* SLAP_AUXPROP_DONTUSECOPY */
423 if ( op->o_bd->be_search ) {
424 SlapReply rs = {REP_RESULT};
425 #ifdef SLAP_AUXPROP_DONTUSECOPY
426 LDAPControl **save_ctrls = NULL, c;
427 int save_dontUseCopy;
428 #endif /* SLAP_AUXPROP_DONTUSECOPY */
430 op->o_hdr = conn->c_sasl_bindop->o_hdr;
431 op->o_controls = opbuf.ob_controls;
432 op->o_tag = LDAP_REQ_SEARCH;
433 op->o_dn = conn->c_ndn;
434 op->o_ndn = conn->c_ndn;
435 op->o_callback = &cb;
436 slap_op_time( &op->o_time, &op->o_tincr );
437 op->o_do_not_cache = 1;
438 op->o_is_auth_check = 1;
439 op->o_req_dn = op->o_req_ndn;
440 op->ors_scope = LDAP_SCOPE_BASE;
441 op->ors_deref = LDAP_DEREF_NEVER;
442 op->ors_tlimit = SLAP_NO_LIMIT;
444 op->ors_filter = &generic_filter;
445 op->ors_filterstr = generic_filterstr;
446 op->o_authz = conn->c_authz;
447 /* FIXME: we want all attributes, right? */
448 op->ors_attrs = NULL;
450 #ifdef SLAP_AUXPROP_DONTUSECOPY
452 save_dontUseCopy = op->o_dontUseCopy;
453 if ( !op->o_dontUseCopy ) {
455 save_ctrls = op->o_ctrls;
457 for ( ; op->o_ctrls[ cnt ]; cnt++ )
460 op->o_ctrls = op->o_tmpcalloc( sizeof(LDAPControl *), cnt + 2, op->o_tmpmemctx );
462 for ( cnt = 0; save_ctrls[ cnt ]; cnt++ ) {
463 op->o_ctrls[ cnt ] = save_ctrls[ cnt ];
466 c.ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
467 c.ldctl_iscritical = 1;
468 BER_BVZERO( &c.ldctl_value );
469 op->o_ctrls[ cnt ] = &c;
471 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
473 #endif /* SLAP_AUXPROP_DONTUSECOPY */
475 rc = op->o_bd->be_search( op, &rs );
477 #ifdef SLAP_AUXPROP_DONTUSECOPY
479 if ( save_ctrls != op->o_ctrls ) {
480 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
481 op->o_ctrls = save_ctrls;
482 op->o_dontUseCopy = save_dontUseCopy;
485 if ( rs.sr_err == LDAP_UNAVAILABLE && slap_dontUseCopy_ignore )
487 op->o_bd = dontUseCopy_bd;
489 goto retry_dontUseCopy;
492 #endif /* SLAP_AUXPROP_DONTUSECOPY */
496 #if SASL_VERSION_FULL >= 0x020118
497 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
501 #if SASL_VERSION_FULL >= 0x020110
505 sasl_server_params_t *sparams,
506 struct propctx *prctx,
514 Connection *conn = NULL;
515 const struct propval *pr;
516 Modifications *modlist = NULL, **modtail = &modlist, *mod;
517 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
518 char textbuf[SLAP_TEXT_BUFLEN];
520 size_t textlen = sizeof(textbuf);
521 #ifdef SLAP_AUXPROP_DONTUSECOPY
523 BackendDB *dontUseCopy_bd = NULL;
524 #endif /* SLAP_AUXPROP_DONTUSECOPY */
526 /* just checking if we are enabled */
527 if (!prctx) return SASL_OK;
529 if (!sparams || !user) return SASL_BADPARAM;
531 pr = sparams->utils->prop_get( sparams->propctx );
533 /* Find our DN and conn first */
534 for( i = 0; pr[i].name; i++ ) {
535 if ( pr[i].name[0] == '*' ) {
536 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
537 if ( pr[i].values && pr[i].values[0] )
538 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
541 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
542 if ( pr[i].values && pr[i].values[0] )
543 AC_MEMCPY( &op.o_req_ndn.bv_len, pr[i].values[0],
544 sizeof( op.o_req_ndn.bv_len ) );
545 } else if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
547 op.o_req_ndn.bv_val = (char *)pr[i].values[0];
549 #ifdef SLAP_AUXPROP_DONTUSECOPY
550 if ( slap_dontUseCopy_propnames != NULL ) {
552 ber_str2bv( &pr[i].name[1], 0, 1, &bv );
553 for ( j = 0; !BER_BVISNULL( &slap_dontUseCopy_propnames[ j ] ); j++ ) {
554 if ( bvmatch( &bv, &slap_dontUseCopy_propnames[ j ] ) ) {
560 #endif /* SLAP_AUXPROP_DONTUSECOPY */
563 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
565 op.o_bd = select_backend( &op.o_req_ndn, 1 );
567 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
569 #ifdef SLAP_AUXPROP_DONTUSECOPY
570 if ( SLAP_SHADOW( op.o_bd ) && dontUseCopy ) {
571 dontUseCopy_bd = op.o_bd;
572 op.o_bd = frontendDB;
573 op.o_dontUseCopy = SLAP_CONTROL_CRITICAL;
575 #endif /* SLAP_AUXPROP_DONTUSECOPY */
577 pr = sparams->utils->prop_get( prctx );
578 if (!pr) return SASL_BADPARAM;
580 for (i=0; pr[i].name; i++);
581 if (!i) return SASL_BADPARAM;
583 for (i=0; pr[i].name; i++) {
584 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
585 mod->sml_op = LDAP_MOD_REPLACE;
587 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
588 mod->sml_numvals = pr[i].nvalues;
589 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
590 sizeof(struct berval));
591 for (j=0; j<pr[i].nvalues; j++) {
592 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
594 BER_BVZERO( &mod->sml_values[j] );
595 mod->sml_nvalues = NULL;
596 mod->sml_desc = NULL;
598 modtail = &mod->sml_next;
602 rc = slap_mods_check( &op, modlist, &text, textbuf, textlen, NULL );
604 if ( rc == LDAP_SUCCESS ) {
605 rc = slap_mods_no_user_mod_check( &op, modlist,
606 &text, textbuf, textlen );
608 if ( rc == LDAP_SUCCESS ) {
609 if ( conn->c_sasl_bindop ) {
610 op.o_hdr = conn->c_sasl_bindop->o_hdr;
613 memset( &oph, 0, sizeof(oph) );
614 operation_fake_init( conn, &op, ldap_pvt_thread_pool_context(), 0 );
616 op.o_tag = LDAP_REQ_MODIFY;
617 op.o_ndn = op.o_req_ndn;
619 slap_op_time( &op.o_time, &op.o_tincr );
620 op.o_do_not_cache = 1;
621 op.o_is_auth_check = 1;
622 op.o_req_dn = op.o_req_ndn;
623 op.orm_modlist = modlist;
626 SlapReply rs = {REP_RESULT};
627 rc = op.o_bd->be_modify( &op, &rs );
629 #ifdef SLAP_AUXPROP_DONTUSECOPY
631 rs.sr_err == LDAP_UNAVAILABLE &&
632 slap_dontUseCopy_ignore )
634 op.o_bd = dontUseCopy_bd;
635 op.o_dontUseCopy = SLAP_CONTROL_NONE;
639 #endif /* SLAP_AUXPROP_DONTUSECOPY */
644 slap_mods_free( modlist, 1 );
645 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
647 #endif /* SASL_VERSION_FULL >= 2.1.16 */
649 static sasl_auxprop_plug_t slap_auxprop_plugin = {
652 NULL, /* glob_context */
653 NULL, /* auxprop_free */
656 #if SASL_VERSION_FULL >= 0x020110
657 slap_auxprop_store /* the declaration of this member changed
658 * in cyrus SASL from 2.1.15 to 2.1.16 */
666 const sasl_utils_t *utils,
669 sasl_auxprop_plug_t **plug,
670 const char *plugname)
672 if ( !out_version || !plug ) return SASL_BADPARAM;
674 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
676 *out_version = SASL_AUXPROP_PLUG_VERSION;
677 *plug = &slap_auxprop_plugin;
681 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
682 * auxiliary property, so that we can refer to it in sasl_authorize
683 * without interfering with anything else. Also, the SASL username
684 * buffer is constrained to 256 characters, and our DNs could be
685 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
688 slap_sasl_canonicalize(
694 const char *user_realm,
699 Connection *conn = (Connection *)context;
700 struct propctx *props = sasl_auxprop_getctx( sconn );
701 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
704 const char *names[2];
709 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
710 conn ? (long) conn->c_connid : -1L,
711 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
712 in ? in : "<empty>");
714 /* If name is too big, just truncate. We don't care, we're
715 * using DNs, not the usernames.
717 if ( inlen > out_max )
720 /* This is a Simple Bind using SPASSWD. That means the in-directory
721 * userPassword of the Binding user already points at SASL, so it
722 * cannot be used to actually satisfy a password comparison. Just
723 * ignore it, some other mech will process it.
725 if ( !conn->c_sasl_bindop ||
726 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
728 /* See if we need to add request, can only do it once */
729 prop_getnames( props, slap_propnames, auxvals );
730 if ( !auxvals[0].name )
731 prop_request( props, slap_propnames );
733 if ( flags & SASL_CU_AUTHID )
734 which = SLAP_SASL_PROP_AUTHCLEN;
736 which = SLAP_SASL_PROP_AUTHZLEN;
738 /* Need to store the Connection for auxprop_lookup */
739 if ( !auxvals[SLAP_SASL_PROP_CONN].values ) {
740 names[0] = slap_propnames[SLAP_SASL_PROP_CONN];
742 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
745 /* Already been here? */
746 if ( auxvals[which].values )
749 /* Normally we require an authzID to have a u: or dn: prefix.
750 * However, SASL frequently gives us an authzID that is just
751 * an exact copy of the authcID, without a prefix. We need to
752 * detect and allow this condition. If SASL calls canonicalize
753 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
754 * But if it's broken into two calls, we need to remember the
755 * authcID so that we can compare the authzID later. We store
756 * the authcID temporarily in conn->c_sasl_dn. We necessarily
757 * finish Canonicalizing before Authorizing, so there is no
758 * conflict with slap_sasl_authorize's use of this temp var.
760 * The SASL EXTERNAL mech is backwards from all the other mechs,
761 * it does authzID before the authcID. If we see that authzID
762 * has already been done, don't do anything special with authcID.
764 if ( flags == SASL_CU_AUTHID && !auxvals[SLAP_SASL_PROP_AUTHZ].values ) {
765 conn->c_sasl_dn.bv_val = (char *) in;
766 conn->c_sasl_dn.bv_len = 0;
767 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
768 rc = strcmp( in, conn->c_sasl_dn.bv_val );
769 conn->c_sasl_dn.bv_val = NULL;
770 /* They were equal, no work needed */
771 if ( !rc ) goto done;
774 bvin.bv_val = (char *)in;
776 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
777 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
778 if ( rc != LDAP_SUCCESS ) {
779 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
783 names[0] = slap_propnames[which];
785 prop_set( props, names[0], (char *)&dn.bv_len, sizeof( dn.bv_len ) );
788 names[0] = slap_propnames[which];
789 prop_set( props, names[0], dn.bv_val, dn.bv_len );
791 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
792 conn ? (long) conn->c_connid : -1L, names[0]+1,
793 dn.bv_val ? dn.bv_val : "<EMPTY>" );
795 /* Not needed any more, SASL has copied it */
796 if ( conn && conn->c_sasl_bindop )
797 conn->c_sasl_bindop->o_tmpfree( dn.bv_val, conn->c_sasl_bindop->o_tmpmemctx );
800 AC_MEMCPY( out, in, inlen );
812 char *requested_user,
816 const char *def_realm,
818 struct propctx *props)
820 Connection *conn = (Connection *)context;
822 * (SLAP_SASL_PROP_COUNT - 1) because we skip "conn",
823 * + 1 for NULL termination?
825 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
826 struct berval authcDN, authzDN = BER_BVNULL;
829 /* Simple Binds don't support proxy authorization, ignore it */
830 if ( !conn->c_sasl_bindop ||
831 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
833 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
834 "authcid=\"%s\" authzid=\"%s\"\n",
835 conn ? (long) conn->c_connid : -1L, auth_identity, requested_user );
836 if ( conn->c_sasl_dn.bv_val ) {
837 BER_BVZERO( &conn->c_sasl_dn );
840 /* Skip SLAP_SASL_PROP_CONN */
841 prop_getnames( props, slap_propnames+1, auxvals );
843 /* Should not happen */
844 if ( !auxvals[0].values ) {
845 sasl_seterror( sconn, 0, "invalid authcid" );
849 AC_MEMCPY( &authcDN.bv_len, auxvals[0].values[0], sizeof(authcDN.bv_len) );
850 authcDN.bv_val = auxvals[1].values ? (char *)auxvals[1].values[0] : NULL;
851 conn->c_sasl_dn = authcDN;
853 /* Nothing to do if no authzID was given */
854 if ( !auxvals[2].name || !auxvals[2].values ) {
858 AC_MEMCPY( &authzDN.bv_len, auxvals[2].values[0], sizeof(authzDN.bv_len) );
859 authzDN.bv_val = auxvals[3].values ? (char *)auxvals[3].values[0] : NULL;
861 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
862 if ( rc != LDAP_SUCCESS ) {
863 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
864 "proxy authorization disallowed (%d)\n",
865 conn ? (long) conn->c_connid : -1L, rc, 0 );
867 sasl_seterror( sconn, 0, "not authorized" );
871 /* FIXME: we need yet another dup because slap_sasl_getdn()
872 * is using the bind operation slab */
873 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
876 if (conn->c_sasl_bindop) {
877 Statslog( LDAP_DEBUG_STATS,
878 "%s BIND authcid=\"%s\" authzid=\"%s\"\n",
879 conn->c_sasl_bindop->o_log_prefix,
880 auth_identity, requested_user, 0, 0 );
883 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
884 " proxy authorization allowed authzDN=\"%s\"\n",
885 conn ? (long) conn->c_connid : -1L,
886 authzDN.bv_val ? authzDN.bv_val : "", 0 );
891 slap_sasl_err2ldap( int saslerr )
895 /* map SASL errors to LDAP resultCode returned by:
897 * SASL_OK, SASL_NOMEM
899 * SASL_OK, SASL_CONTINUE, SASL_TRANS, SASL_BADPARAM, SASL_BADPROT,
901 * sasl_server_start()
904 * SASL_OK, SASL_BADPARAM
912 rc = LDAP_SASL_BIND_IN_PROGRESS;
919 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
925 rc = LDAP_INVALID_CREDENTIALS;
928 rc = LDAP_INSUFFICIENT_ACCESS;
932 rc = LDAP_INAPPROPRIATE_AUTH;
936 rc = LDAP_UNAVAILABLE;
939 rc = LDAP_UNWILLING_TO_PERFORM;
951 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
954 const struct berval *sc,
955 const struct berval * passwd,
956 const struct berval * cred,
961 void *ctx, *sconn = NULL;
963 for( i=0; i<cred->bv_len; i++) {
964 if(cred->bv_val[i] == '\0') {
965 return LUTIL_PASSWD_ERR; /* NUL character in password */
969 if( cred->bv_val[i] != '\0' ) {
970 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
973 for( i=0; i<passwd->bv_len; i++) {
974 if(passwd->bv_val[i] == '\0') {
975 return LUTIL_PASSWD_ERR; /* NUL character in password */
979 if( passwd->bv_val[i] != '\0' ) {
980 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
983 rtn = LUTIL_PASSWD_ERR;
985 ctx = ldap_pvt_thread_pool_context();
986 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_sasl_bind, &sconn, NULL );
988 if( sconn != NULL ) {
990 sc = sasl_checkpass( sconn,
991 passwd->bv_val, passwd->bv_len,
992 cred->bv_val, cred->bv_len );
993 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
998 #endif /* SLAPD_SPASSWD */
1000 #endif /* HAVE_CYRUS_SASL */
1002 #ifdef ENABLE_REWRITE
1004 typedef struct slapd_map_data {
1006 struct berval filter;
1007 AttributeName attrs[2];
1012 slapd_rw_config( const char *fname, int lineno, int argc, char **argv )
1014 slapd_map_data *ret = NULL;
1015 LDAPURLDesc *lud = NULL;
1017 AttributeDescription *ad = NULL;
1019 struct berval dn, ndn;
1022 Debug( LDAP_DEBUG_ANY,
1023 "[%s:%d] slapd map needs URI\n",
1029 if ( strncasecmp( uri, "uri=", STRLENOF( "uri=" ) ) == 0 ) {
1030 uri += STRLENOF( "uri=" );
1033 if ( ldap_url_parse( uri, &lud ) != LDAP_URL_SUCCESS ) {
1034 Debug( LDAP_DEBUG_ANY,
1035 "[%s:%d] illegal URI '%s'\n",
1036 fname, lineno, uri );
1040 if ( strcasecmp( lud->lud_scheme, "ldap" )) {
1041 Debug( LDAP_DEBUG_ANY,
1042 "[%s:%d] illegal URI scheme '%s'\n",
1043 fname, lineno, lud->lud_scheme );
1047 if (( lud->lud_host && lud->lud_host[0] ) || lud->lud_exts
1049 Debug( LDAP_DEBUG_ANY,
1050 "[%s:%d] illegal URI '%s'\n",
1051 fname, lineno, uri );
1055 if ( lud->lud_attrs ) {
1056 if ( lud->lud_attrs[1] ) {
1057 Debug( LDAP_DEBUG_ANY,
1058 "[%s:%d] only one attribute allowed in URI\n",
1062 if ( strcasecmp( lud->lud_attrs[0], "dn" ) &&
1063 strcasecmp( lud->lud_attrs[0], "entryDN" )) {
1065 rc = slap_str2ad( lud->lud_attrs[0], &ad, &text );
1070 ber_str2bv( lud->lud_dn, 0, 0, &dn );
1071 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL ))
1074 if ( lud->lud_filter ) {
1075 flen = strlen( lud->lud_filter ) + 1;
1077 ret = ch_malloc( sizeof( slapd_map_data ) + flen );
1080 ret->filter.bv_val = (char *)(ret+1);
1081 ret->filter.bv_len = flen - 1;
1082 strcpy( ret->filter.bv_val, lud->lud_filter );
1084 BER_BVZERO( &ret->filter );
1086 ret->scope = lud->lud_scope;
1088 ret->attrs[0].an_name = ad->ad_cname;
1090 BER_BVZERO( &ret->attrs[0].an_name );
1092 ret->attrs[0].an_desc = ad;
1093 BER_BVZERO( &ret->attrs[1].an_name );
1095 ldap_free_urldesc( lud );
1099 struct slapd_rw_info {
1100 slapd_map_data *si_data;
1101 struct berval si_val;
1105 slapd_rw_cb( Operation *op, SlapReply *rs )
1107 if ( rs->sr_type == REP_SEARCH ) {
1108 struct slapd_rw_info *si = op->o_callback->sc_private;
1110 if ( si->si_data->attrs[0].an_desc ) {
1113 a = attr_find( rs->sr_entry->e_attrs,
1114 si->si_data->attrs[0].an_desc );
1116 ber_dupbv( &si->si_val, a->a_vals );
1119 ber_dupbv( &si->si_val, &rs->sr_entry->e_name );
1122 return LDAP_SUCCESS;
1126 slapd_rw_apply( void *private, const char *filter, struct berval *val )
1128 slapd_map_data *sl = private;
1129 slap_callback cb = { NULL };
1130 Connection conn = {0};
1131 OperationBuffer opbuf;
1134 SlapReply rs = {REP_RESULT};
1135 struct slapd_rw_info si;
1139 thrctx = ldap_pvt_thread_pool_context();
1140 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1143 op->o_tag = LDAP_REQ_SEARCH;
1144 op->o_req_dn = op->o_req_ndn = sl->base;
1145 op->o_bd = select_backend( &op->o_req_ndn, 1 );
1150 BER_BVZERO( &si.si_val );
1151 op->ors_scope = sl->scope;
1152 op->ors_deref = LDAP_DEREF_NEVER;
1154 op->ors_tlimit = SLAP_NO_LIMIT;
1155 if ( sl->attrs[0].an_desc ) {
1156 op->ors_attrs = sl->attrs;
1158 op->ors_attrs = slap_anlist_no_attrs;
1161 rc = strlen( filter );
1165 rc += sl->filter.bv_len;
1166 ptr = op->ors_filterstr.bv_val = op->o_tmpalloc( rc + 1, op->o_tmpmemctx );
1167 if ( sl->filter.bv_len ) {
1168 ptr = lutil_strcopy( ptr, sl->filter.bv_val );
1173 strcpy( ptr, filter );
1175 op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
1176 if ( !op->ors_filter ) {
1177 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1181 op->ors_attrsonly = 0;
1182 op->o_dn = op->o_bd->be_rootdn;
1183 op->o_ndn = op->o_bd->be_rootndn;
1184 op->o_do_not_cache = 1;
1186 cb.sc_response = slapd_rw_cb;
1187 cb.sc_private = &si;
1188 op->o_callback = &cb;
1190 rc = op->o_bd->be_search( op, &rs );
1191 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &si.si_val )) {
1193 rc = REWRITE_SUCCESS;
1195 if ( !BER_BVISNULL( &si.si_val )) {
1196 ch_free( si.si_val.bv_val );
1200 filter_free_x( op, op->ors_filter, 1 );
1201 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1206 slapd_rw_destroy( void *private )
1208 slapd_map_data *md = private;
1210 assert( private != NULL );
1212 ch_free( md->base.bv_val );
1213 ch_free( md->filter.bv_val );
1219 static const rewrite_mapper slapd_mapper = {
1227 int slap_sasl_init( void )
1229 #ifdef HAVE_CYRUS_SASL
1231 static sasl_callback_t server_callbacks[] = {
1232 { SASL_CB_LOG, &slap_sasl_log, NULL },
1233 { SASL_CB_GETOPT, &slap_sasl_getopt, NULL },
1234 { SASL_CB_LIST_END, NULL, NULL }
1238 #ifdef ENABLE_REWRITE
1239 rewrite_mapper_register( &slapd_mapper );
1242 #ifdef HAVE_CYRUS_SASL
1243 #ifdef HAVE_SASL_VERSION
1244 /* stringify the version number, sasl.h doesn't do it for us */
1245 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1246 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1247 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1250 sasl_version( NULL, &rc );
1251 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1252 (rc & 0xffff) < SASL_VERSION_STEP)
1254 char version[sizeof("xxx.xxx.xxxxx")];
1255 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1257 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
1258 " expected %s, got %s\n",
1259 SASL_VERSION_STRING, version, 0 );
1265 ldap_pvt_sasl_mutex_new,
1266 ldap_pvt_sasl_mutex_lock,
1267 ldap_pvt_sasl_mutex_unlock,
1268 ldap_pvt_sasl_mutex_dispose );
1270 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1272 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1273 if( rc != SASL_OK ) {
1274 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
1279 /* should provide callbacks for logging */
1280 /* server name should be configurable */
1281 rc = sasl_server_init( server_callbacks, "slapd" );
1283 if( rc != SASL_OK ) {
1284 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
1290 #ifdef SLAPD_SPASSWD
1291 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
1294 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1297 /* default security properties */
1298 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1299 sasl_secprops.max_ssf = INT_MAX;
1300 sasl_secprops.maxbufsize = 65536;
1301 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1307 int slap_sasl_destroy( void )
1309 #ifdef HAVE_CYRUS_SASL
1312 #ifdef SLAP_AUXPROP_DONTUSECOPY
1313 if ( slap_dontUseCopy_propnames ) {
1314 ber_bvarray_free( slap_dontUseCopy_propnames );
1315 slap_dontUseCopy_propnames = NULL;
1317 #endif /* SLAP_AUXPROP_DONTUSECOPY */
1326 slap_sasl_peer2ipport( struct berval *peer )
1330 *addr = &peer->bv_val[ STRLENOF( "IP=" ) ];
1331 ber_len_t plen = peer->bv_len - STRLENOF( "IP=" );
1334 if ( addr[0] == '[' ) {
1338 ipport = ch_strdup( &addr[isv6] );
1340 /* Convert IPv6/IPv4 addresses to address;port syntax. */
1341 p = strrchr( ipport, ':' );
1345 assert( p[-1] == ']' );
1346 AC_MEMCPY( &p[-1], p, plen - ( p - ipport ) + 1 );
1349 } else if ( isv6 ) {
1352 assert( addr[plen] == ']' );
1359 int slap_sasl_open( Connection *conn, int reopen )
1361 int sc = LDAP_SUCCESS;
1362 #ifdef HAVE_CYRUS_SASL
1365 sasl_conn_t *ctx = NULL;
1366 sasl_callback_t *session_callbacks;
1367 char *ipremoteport = NULL, *iplocalport = NULL;
1369 assert( conn->c_sasl_authctx == NULL );
1372 assert( conn->c_sasl_extra == NULL );
1375 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1376 if( session_callbacks == NULL ) {
1377 Debug( LDAP_DEBUG_ANY,
1378 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1381 conn->c_sasl_extra = session_callbacks;
1383 session_callbacks[cb=0].id = SASL_CB_LOG;
1384 session_callbacks[cb].proc = &slap_sasl_log;
1385 session_callbacks[cb++].context = conn;
1387 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1388 session_callbacks[cb].proc = &slap_sasl_authorize;
1389 session_callbacks[cb++].context = conn;
1391 session_callbacks[cb].id = SASL_CB_CANON_USER;
1392 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1393 session_callbacks[cb++].context = conn;
1395 session_callbacks[cb].id = SASL_CB_LIST_END;
1396 session_callbacks[cb].proc = NULL;
1397 session_callbacks[cb++].context = NULL;
1399 session_callbacks = conn->c_sasl_extra;
1402 conn->c_sasl_layers = 0;
1404 /* create new SASL context */
1405 if ( conn->c_sock_name.bv_len != 0 &&
1406 strncmp( conn->c_sock_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1408 iplocalport = slap_sasl_peer2ipport( &conn->c_sock_name );
1411 if ( conn->c_peer_name.bv_len != 0 &&
1412 strncmp( conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1414 ipremoteport = slap_sasl_peer2ipport( &conn->c_peer_name );
1417 sc = sasl_server_new( "ldap", sasl_host, global_realm,
1418 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1419 if ( iplocalport != NULL ) {
1420 ch_free( iplocalport );
1422 if ( ipremoteport != NULL ) {
1423 ch_free( ipremoteport );
1426 if( sc != SASL_OK ) {
1427 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1433 conn->c_sasl_authctx = ctx;
1435 if( sc == SASL_OK ) {
1436 sc = sasl_setprop( ctx,
1437 SASL_SEC_PROPS, &sasl_secprops );
1439 if( sc != SASL_OK ) {
1440 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1443 slap_sasl_close( conn );
1448 sc = slap_sasl_err2ldap( sc );
1450 #elif defined(SLAP_BUILTIN_SASL)
1451 /* built-in SASL implementation */
1452 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1453 if( ctx == NULL ) return -1;
1455 ctx->sc_external_ssf = 0;
1456 BER_BVZERO( &ctx->sc_external_id );
1458 conn->c_sasl_authctx = ctx;
1464 int slap_sasl_external(
1467 struct berval *auth_id )
1469 #ifdef HAVE_CYRUS_SASL
1471 sasl_conn_t *ctx = conn->c_sasl_authctx;
1472 sasl_ssf_t sasl_ssf = ssf;
1474 if ( ctx == NULL ) {
1475 return LDAP_UNAVAILABLE;
1478 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
1480 if ( sc != SASL_OK ) {
1484 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1485 auth_id ? auth_id->bv_val : NULL );
1487 if ( sc != SASL_OK ) {
1490 #elif defined(SLAP_BUILTIN_SASL)
1491 /* built-in SASL implementation */
1492 SASL_CTX *ctx = conn->c_sasl_authctx;
1493 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1495 ctx->sc_external_ssf = ssf;
1497 ctx->sc_external_id = *auth_id;
1498 BER_BVZERO( auth_id );
1500 BER_BVZERO( &ctx->sc_external_id );
1504 return LDAP_SUCCESS;
1507 int slap_sasl_reset( Connection *conn )
1509 return LDAP_SUCCESS;
1512 char ** slap_sasl_mechs( Connection *conn )
1514 char **mechs = NULL;
1516 #ifdef HAVE_CYRUS_SASL
1517 sasl_conn_t *ctx = conn->c_sasl_authctx;
1519 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1523 SASL_CONST char *mechstr;
1525 sc = sasl_listmech( ctx,
1526 NULL, NULL, ",", NULL,
1527 &mechstr, NULL, NULL );
1529 if( sc != SASL_OK ) {
1530 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1536 mechs = ldap_str2charray( mechstr, "," );
1538 #elif defined(SLAP_BUILTIN_SASL)
1539 /* builtin SASL implementation */
1540 SASL_CTX *ctx = conn->c_sasl_authctx;
1541 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1542 /* should check ssf */
1543 mechs = ldap_str2charray( "EXTERNAL", "," );
1550 int slap_sasl_close( Connection *conn )
1552 #ifdef HAVE_CYRUS_SASL
1553 sasl_conn_t *ctx = conn->c_sasl_authctx;
1556 sasl_dispose( &ctx );
1558 if ( conn->c_sasl_sockctx &&
1559 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1561 ctx = conn->c_sasl_sockctx;
1562 sasl_dispose( &ctx );
1565 conn->c_sasl_authctx = NULL;
1566 conn->c_sasl_sockctx = NULL;
1567 conn->c_sasl_done = 0;
1569 free( conn->c_sasl_extra );
1570 conn->c_sasl_extra = NULL;
1572 #elif defined(SLAP_BUILTIN_SASL)
1573 SASL_CTX *ctx = conn->c_sasl_authctx;
1575 if( ctx->sc_external_id.bv_val ) {
1576 free( ctx->sc_external_id.bv_val );
1577 BER_BVZERO( &ctx->sc_external_id );
1580 conn->c_sasl_authctx = NULL;
1584 return LDAP_SUCCESS;
1587 int slap_sasl_bind( Operation *op, SlapReply *rs )
1589 #ifdef HAVE_CYRUS_SASL
1590 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1591 struct berval response;
1592 unsigned reslen = 0;
1595 Debug(LDAP_DEBUG_ARGS,
1596 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1597 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1598 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1599 op->o_conn->c_sasl_bind_mech.bv_val,
1600 op->orb_cred.bv_len );
1603 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1604 "SASL unavailable on this session" );
1608 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1609 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1610 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1611 sasl_server_step( ctx, cred, clen, resp, rlen )
1613 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1614 /* If we already authenticated once, must use a new context */
1615 if ( op->o_conn->c_sasl_done ) {
1617 const char *authid = NULL;
1618 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1619 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1620 if ( authid ) authid = ch_strdup( authid );
1621 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1622 sasl_dispose( &ctx );
1624 op->o_conn->c_sasl_authctx = NULL;
1626 slap_sasl_open( op->o_conn, 1 );
1627 ctx = op->o_conn->c_sasl_authctx;
1629 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1630 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1631 ch_free( (char *)authid );
1635 op->o_conn->c_sasl_bind_mech.bv_val,
1636 op->orb_cred.bv_val, op->orb_cred.bv_len,
1637 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1641 op->orb_cred.bv_val, op->orb_cred.bv_len,
1642 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1645 response.bv_len = reslen;
1647 if ( sc == SASL_OK ) {
1648 sasl_ssf_t *ssf = NULL;
1650 ber_dupbv_x( &op->orb_edn, &op->o_conn->c_sasl_dn, op->o_tmpmemctx );
1651 BER_BVZERO( &op->o_conn->c_sasl_dn );
1652 op->o_conn->c_sasl_done = 1;
1654 rs->sr_err = LDAP_SUCCESS;
1656 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1657 op->orb_ssf = ssf ? *ssf : 0;
1661 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1662 op->o_conn->c_sasl_layers++;
1664 /* If there's an old layer, set sockctx to NULL to
1665 * tell connection_read() to wait for us to finish.
1666 * Otherwise there is a race condition: we have to
1667 * send the Bind response using the old security
1668 * context and then remove it before reading any
1671 if ( op->o_conn->c_sasl_sockctx ) {
1672 ctx = op->o_conn->c_sasl_sockctx;
1673 op->o_conn->c_sasl_sockctx = NULL;
1675 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1677 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1680 /* Must send response using old security layer */
1681 rs->sr_sasldata = (response.bv_len ? &response : NULL);
1682 send_ldap_sasl( op, rs );
1684 /* Now dispose of the old security layer.
1687 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1688 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1689 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1690 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1691 sasl_dispose( &ctx );
1693 } else if ( sc == SASL_CONTINUE ) {
1694 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1695 rs->sr_text = sasl_errdetail( ctx );
1696 rs->sr_sasldata = &response;
1697 send_ldap_sasl( op, rs );
1700 BER_BVZERO( &op->o_conn->c_sasl_dn );
1701 rs->sr_text = sasl_errdetail( ctx );
1702 rs->sr_err = slap_sasl_err2ldap( sc ),
1703 send_ldap_result( op, rs );
1706 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1708 #elif defined(SLAP_BUILTIN_SASL)
1709 /* built-in SASL implementation */
1710 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1712 if ( ctx == NULL ) {
1713 send_ldap_error( op, rs, LDAP_OTHER,
1714 "Internal SASL Error" );
1716 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1719 if( op->orb_cred.bv_len ) {
1720 rs->sr_text = "proxy authorization not supported";
1721 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1722 send_ldap_result( op, rs );
1725 op->orb_edn = ctx->sc_external_id;
1726 rs->sr_err = LDAP_SUCCESS;
1727 rs->sr_sasldata = NULL;
1728 send_ldap_sasl( op, rs );
1732 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1733 "requested SASL mechanism not supported" );
1736 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1737 "SASL not supported" );
1743 char* slap_sasl_secprops( const char *in )
1745 #ifdef HAVE_CYRUS_SASL
1746 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1748 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1750 return "SASL not supported";
1754 void slap_sasl_secprops_unparse( struct berval *bv )
1756 #ifdef HAVE_CYRUS_SASL
1757 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1761 #ifdef HAVE_CYRUS_SASL
1763 slap_sasl_setpass( Operation *op, SlapReply *rs )
1765 struct berval id = BER_BVNULL; /* needs to come from connection */
1766 struct berval new = BER_BVNULL;
1767 struct berval old = BER_BVNULL;
1769 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1771 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1772 (SASL_CONST void **)(char *)&id.bv_val );
1774 if( rs->sr_err != SASL_OK ) {
1775 rs->sr_text = "unable to retrieve SASL username";
1776 rs->sr_err = LDAP_OTHER;
1780 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1781 id.bv_val ? id.bv_val : "", 0, 0 );
1783 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1784 NULL, &old, &new, &rs->sr_text );
1786 if( rs->sr_err != LDAP_SUCCESS ) {
1790 if( new.bv_len == 0 ) {
1791 slap_passwd_generate(&new);
1793 if( new.bv_len == 0 ) {
1794 rs->sr_text = "password generation failed.";
1795 rs->sr_err = LDAP_OTHER;
1799 rs->sr_rspdata = slap_passwd_return( &new );
1802 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1803 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1804 if( rs->sr_err != SASL_OK ) {
1805 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1807 switch(rs->sr_err) {
1809 rs->sr_err = LDAP_SUCCESS;
1819 rs->sr_err = LDAP_OTHER;
1825 #endif /* HAVE_CYRUS_SASL */
1827 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1828 * string returned in *dn is in its own allocated memory, and must be free'd
1829 * by the calling process. -Mark Adamson, Carnegie Mellon
1831 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1832 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1839 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1840 char *user_realm, struct berval *dn, int flags )
1842 int rc, is_dn = SET_NONE, do_norm = 1;
1843 struct berval dn2, *mech;
1845 assert( conn != NULL );
1846 assert( id != NULL );
1848 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1850 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1851 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1852 (unsigned long) id->bv_len ) );
1855 op = conn->c_sasl_bindop;
1857 assert( op != NULL );
1861 if ( !BER_BVISNULL( id ) ) {
1862 /* Blatantly anonymous ID */
1863 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1865 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1866 return( LDAP_SUCCESS );
1870 /* FIXME: if empty, should we stop? */
1871 BER_BVSTR( id, "" );
1874 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1875 mech = &conn->c_sasl_bind_mech;
1877 mech = &conn->c_authmech;
1880 /* An authcID needs to be converted to authzID form. Set the
1881 * values directly into *dn; they will be normalized later. (and
1882 * normalizing always makes a new copy.) An ID from a TLS certificate
1883 * is already normalized, so copy it and skip normalization.
1885 if( flags & SLAP_GETDN_AUTHCID ) {
1886 if( bvmatch( mech, &ext_bv )) {
1887 /* EXTERNAL DNs are already normalized */
1888 assert( !BER_BVISNULL( id ) );
1892 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1895 /* convert to u:<username> form */
1901 if( is_dn == SET_NONE ) {
1902 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1904 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1905 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1907 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1909 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1910 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1914 /* No other possibilities from here */
1915 if( is_dn == SET_NONE ) {
1917 return( LDAP_INAPPROPRIATE_AUTH );
1920 /* Username strings */
1921 if( is_dn == SET_U ) {
1922 /* ITS#3419: values may need escape */
1924 LDAPAVA *RDNs[ 4 ][ 2 ];
1929 DN[ irdn ] = RDNs[ irdn ];
1930 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1931 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1932 AVAs[ irdn ].la_value = *dn;
1933 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1934 AVAs[ irdn ].la_private = NULL;
1935 RDNs[ irdn ][ 1 ] = NULL;
1937 if ( user_realm && *user_realm ) {
1939 DN[ irdn ] = RDNs[ irdn ];
1940 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1941 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1942 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1943 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1944 AVAs[ irdn ].la_private = NULL;
1945 RDNs[ irdn ][ 1 ] = NULL;
1948 if ( !BER_BVISNULL( mech ) ) {
1950 DN[ irdn ] = RDNs[ irdn ];
1951 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1952 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1953 AVAs[ irdn ].la_value = *mech;
1954 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1955 AVAs[ irdn ].la_private = NULL;
1956 RDNs[ irdn ][ 1 ] = NULL;
1960 DN[ irdn ] = RDNs[ irdn ];
1961 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1962 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1963 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1964 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1965 AVAs[ irdn ].la_private = NULL;
1966 RDNs[ irdn ][ 1 ] = NULL;
1971 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1973 if ( rc != LDAP_SUCCESS ) {
1978 Debug( LDAP_DEBUG_TRACE,
1979 "slap_sasl_getdn: u:id converted to %s\n",
1984 /* Dup the DN in any case, so we don't risk
1985 * leaks or dangling pointers later,
1986 * and the DN value is '\0' terminated */
1987 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1988 dn->bv_val = dn2.bv_val;
1991 /* All strings are in DN form now. Normalize if needed. */
1993 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1995 /* User DNs were constructed above and must be freed now */
1996 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1998 if ( rc != LDAP_SUCCESS ) {
2005 /* Run thru regexp */
2006 slap_sasl2dn( op, dn, &dn2, flags );
2007 if( !BER_BVISNULL( &dn2 ) ) {
2008 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
2010 Debug( LDAP_DEBUG_TRACE,
2011 "slap_sasl_getdn: dn:id converted to %s\n",
2015 return( LDAP_SUCCESS );