2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 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>
31 #ifdef HAVE_CYRUS_SASL
32 # ifdef HAVE_SASL_SASL_H
33 # include <sasl/sasl.h>
38 # if SASL_VERSION_MAJOR >= 2
39 # ifdef HAVE_SASL_SASL_H
40 # include <sasl/saslplug.h>
42 # include <saslplug.h>
44 # define SASL_CONST const
49 #define SASL_VERSION_FULL ((SASL_VERSION_MAJOR << 16) |\
50 (SASL_VERSION_MINOR << 8) | SASL_VERSION_STEP)
52 static sasl_security_properties_t sasl_secprops;
53 #elif defined( SLAP_BUILTIN_SASL )
55 * built-in SASL implementation
56 * only supports EXTERNAL
58 typedef struct sasl_ctx {
59 slap_ssf_t sc_external_ssf;
60 struct berval sc_external_id;
67 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
69 #ifdef HAVE_CYRUS_SASL
77 Connection *conn = context;
81 if ( message == NULL ) {
86 #if SASL_VERSION_MAJOR >= 2
88 level = LDAP_DEBUG_NONE;
92 level = LDAP_DEBUG_ANY;
96 level = LDAP_DEBUG_ANY;
100 level = LDAP_DEBUG_TRACE;
104 level = LDAP_DEBUG_TRACE;
108 level = LDAP_DEBUG_TRACE;
112 level = LDAP_DEBUG_TRACE;
116 level = LDAP_DEBUG_TRACE;
117 label = "Password Trace";
121 level = LDAP_DEBUG_ANY;
124 case SASL_LOG_WARNING:
125 level = LDAP_DEBUG_TRACE;
129 level = LDAP_DEBUG_TRACE;
134 return SASL_BADPARAM;
137 Debug( level, "SASL [conn=%ld] %s: %s\n",
138 conn ? conn->c_connid: -1,
146 #if SASL_VERSION_MAJOR >= 2
147 static const char *slap_propnames[] = {
148 "*slapConn", "*slapAuthcDN", "*slapAuthzDN", NULL };
150 static Filter generic_filter = { LDAP_FILTER_PRESENT, { 0 }, NULL };
151 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
153 #define SLAP_SASL_PROP_CONN 0
154 #define SLAP_SASL_PROP_AUTHC 1
155 #define SLAP_SASL_PROP_AUTHZ 2
156 #define SLAP_SASL_PROP_COUNT 3 /* Number of properties we used */
158 typedef struct lookup_info {
160 const struct propval *list;
161 sasl_server_params_t *sparams;
164 static slap_response sasl_ap_lookup;
166 static struct berval sc_cleartext = BER_BVC("{CLEARTEXT}");
169 sasl_ap_lookup( Operation *op, SlapReply *rs )
172 AttributeDescription *ad;
176 lookup_info *sl = (lookup_info *)op->o_callback->sc_private;
178 if (rs->sr_type != REP_SEARCH) return 0;
180 for( i = 0; sl->list[i].name; i++ ) {
181 const char *name = sl->list[i].name;
183 if ( name[0] == '*' ) {
184 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
185 /* Skip our private properties */
186 if ( !strcmp( name, slap_propnames[0] )) {
187 i += SLAP_SASL_PROP_COUNT - 1;
191 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
194 if ( sl->list[i].values ) {
195 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
198 rc = slap_str2ad( name, &ad, &text );
199 if ( rc != LDAP_SUCCESS ) {
200 Debug( LDAP_DEBUG_TRACE,
201 "slap_ap_lookup: str2ad(%s): %s\n", name, text, 0 );
205 /* If it's the rootdn and a rootpw was present, we already set
206 * it so don't override it here.
208 if ( ad == slap_schema.si_ad_userPassword && sl->list[i].values &&
209 be_isroot_dn( op->o_bd, &op->o_req_ndn ))
212 a = attr_find( rs->sr_entry->e_attrs, ad );
214 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
217 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
218 sl->sparams->utils->prop_erase( sl->sparams->propctx,
221 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
222 /* ITS#3846 don't give hashed passwords to SASL */
223 if ( ad == slap_schema.si_ad_userPassword &&
224 bv->bv_val[0] == '{' /*}*/ )
226 if ( lutil_passwd_scheme( bv->bv_val ) ) {
227 /* If it's not a recognized scheme, just assume it's
228 * a cleartext password that happened to include brackets.
230 * If it's a recognized scheme, skip this value, unless the
231 * scheme is {CLEARTEXT}. In that case, skip over the
232 * scheme name and use the remainder. If there is nothing
233 * past the scheme name, skip this value.
235 #ifdef SLAPD_CLEARTEXT
236 if ( !ber_bvstrcasecmp( bv, &sc_cleartext ) ) {
238 cbv.bv_len = bv->bv_len - sc_cleartext.bv_len;
239 if ( cbv.bv_len > 0 ) {
240 cbv.bv_val = bv->bv_val + sc_cleartext.bv_len;
241 sl->sparams->utils->prop_set( sl->sparams->propctx,
242 sl->list[i].name, cbv.bv_val, cbv.bv_len );
249 sl->sparams->utils->prop_set( sl->sparams->propctx,
250 sl->list[i].name, bv->bv_val, bv->bv_len );
259 sasl_server_params_t *sparams,
266 Connection *conn = NULL;
269 sl.list = sparams->utils->prop_get( sparams->propctx );
270 sl.sparams = sparams;
273 /* Find our DN and conn first */
274 for( i = 0; sl.list[i].name; i++ ) {
275 if ( sl.list[i].name[0] == '*' ) {
276 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
277 if ( sl.list[i].values && sl.list[i].values[0] )
278 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
281 if ( (flags & SASL_AUXPROP_AUTHZID) &&
282 !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZ] ) ) {
284 if ( sl.list[i].values && sl.list[i].values[0] )
285 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
288 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
289 if ( sl.list[i].values && sl.list[i].values[0] ) {
290 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
291 if ( !(flags & SASL_AUXPROP_AUTHZID) )
298 /* Now see what else needs to be fetched */
299 for( i = 0; sl.list[i].name; i++ ) {
300 const char *name = sl.list[i].name;
302 if ( name[0] == '*' ) {
303 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
304 /* Skip our private properties */
305 if ( !strcmp( name, slap_propnames[0] )) {
306 i += SLAP_SASL_PROP_COUNT - 1;
310 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
313 if ( sl.list[i].values ) {
314 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
321 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
325 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
328 /* For rootdn, see if we can use the rootpw */
329 if ( be_isroot_dn( op.o_bd, &op.o_req_ndn ) &&
330 !BER_BVISEMPTY( &op.o_bd->be_rootpw )) {
331 struct berval cbv = BER_BVNULL;
333 /* If there's a recognized scheme, see if it's CLEARTEXT */
334 if ( lutil_passwd_scheme( op.o_bd->be_rootpw.bv_val )) {
335 if ( !strncasecmp( op.o_bd->be_rootpw.bv_val,
336 sc_cleartext.bv_val, sc_cleartext.bv_len )) {
338 /* If it's CLEARTEXT, skip past scheme spec */
339 cbv.bv_len = op.o_bd->be_rootpw.bv_len -
342 cbv.bv_val = op.o_bd->be_rootpw.bv_val +
346 /* No scheme, use the whole value */
348 cbv = op.o_bd->be_rootpw;
350 if ( !BER_BVISEMPTY( &cbv )) {
351 for( i = 0; sl.list[i].name; i++ ) {
352 const char *name = sl.list[i].name;
354 if ( name[0] == '*' ) {
355 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
357 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
360 if ( !strcasecmp(name,"userPassword") ) {
361 sl.sparams->utils->prop_set( sl.sparams->propctx,
362 sl.list[i].name, cbv.bv_val, cbv.bv_len );
369 if ( op.o_bd->be_search ) {
370 SlapReply rs = {REP_RESULT};
371 op.o_hdr = conn->c_sasl_bindop->o_hdr;
372 op.o_tag = LDAP_REQ_SEARCH;
373 op.o_dn = conn->c_ndn;
374 op.o_ndn = conn->c_ndn;
376 slap_op_time( &op.o_time, &op.o_tincr );
377 op.o_do_not_cache = 1;
378 op.o_is_auth_check = 1;
379 op.o_req_dn = op.o_req_ndn;
380 op.ors_scope = LDAP_SCOPE_BASE;
381 op.ors_deref = LDAP_DEREF_NEVER;
382 op.ors_tlimit = SLAP_NO_LIMIT;
384 op.ors_filter = &generic_filter;
385 op.ors_filterstr = generic_filterstr;
386 /* FIXME: we want all attributes, right? */
389 op.o_bd->be_search( &op, &rs );
395 #if SASL_VERSION_FULL >= 0x020110
399 sasl_server_params_t *sparams,
400 struct propctx *prctx,
405 SlapReply rs = {REP_RESULT};
407 Connection *conn = NULL;
408 const struct propval *pr;
409 Modifications *modlist = NULL, **modtail = &modlist, *mod;
410 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
411 char textbuf[SLAP_TEXT_BUFLEN];
413 size_t textlen = sizeof(textbuf);
415 /* just checking if we are enabled */
416 if (!prctx) return SASL_OK;
418 if (!sparams || !user) return SASL_BADPARAM;
420 pr = sparams->utils->prop_get( sparams->propctx );
422 /* Find our DN and conn first */
423 for( i = 0; pr[i].name; i++ ) {
424 if ( pr[i].name[0] == '*' ) {
425 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
426 if ( pr[i].values && pr[i].values[0] )
427 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
430 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
431 if ( pr[i].values && pr[i].values[0] ) {
432 AC_MEMCPY( &op.o_req_ndn, pr[i].values[0], sizeof( struct berval ) );
437 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
439 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
441 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
443 pr = sparams->utils->prop_get( prctx );
444 if (!pr) return SASL_BADPARAM;
446 for (i=0; pr[i].name; i++);
447 if (!i) return SASL_BADPARAM;
449 for (i=0; pr[i].name; i++) {
450 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
451 mod->sml_op = LDAP_MOD_REPLACE;
453 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
454 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
455 sizeof(struct berval));
456 for (j=0; j<pr[i].nvalues; j++) {
457 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
459 BER_BVZERO( &mod->sml_values[j] );
460 mod->sml_nvalues = NULL;
461 mod->sml_desc = NULL;
463 modtail = &mod->sml_next;
467 rc = slap_mods_check( &op, modlist, &text, textbuf, textlen, NULL );
469 if ( rc == LDAP_SUCCESS ) {
470 rc = slap_mods_no_user_mod_check( &op, modlist,
471 &text, textbuf, textlen );
473 if ( rc == LDAP_SUCCESS ) {
474 op.o_hdr = conn->c_sasl_bindop->o_hdr;
475 op.o_tag = LDAP_REQ_MODIFY;
476 op.o_ndn = op.o_req_ndn;
478 slap_op_time( &op.o_time, &op.o_tincr );
479 op.o_do_not_cache = 1;
480 op.o_is_auth_check = 1;
481 op.o_req_dn = op.o_req_ndn;
482 op.orm_modlist = modlist;
484 rc = op.o_bd->be_modify( &op, &rs );
487 slap_mods_free( modlist, 1 );
488 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
490 #endif /* SASL_VERSION_FULL >= 2.1.16 */
492 static sasl_auxprop_plug_t slap_auxprop_plugin = {
495 NULL, /* glob_context */
496 NULL, /* auxprop_free */
499 #if SASL_VERSION_FULL >= 0x020110
500 slap_auxprop_store /* the declaration of this member changed
501 * in cyrus SASL from 2.1.15 to 2.1.16 */
509 const sasl_utils_t *utils,
512 sasl_auxprop_plug_t **plug,
513 const char *plugname)
515 if ( !out_version || !plug ) return SASL_BADPARAM;
517 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
519 *out_version = SASL_AUXPROP_PLUG_VERSION;
520 *plug = &slap_auxprop_plugin;
524 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
525 * auxiliary property, so that we can refer to it in sasl_authorize
526 * without interfering with anything else. Also, the SASL username
527 * buffer is constrained to 256 characters, and our DNs could be
528 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
531 slap_sasl_canonicalize(
537 const char *user_realm,
542 Connection *conn = (Connection *)context;
543 struct propctx *props = sasl_auxprop_getctx( sconn );
544 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
547 const char *names[2];
552 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
553 conn ? conn->c_connid : -1,
554 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
555 in ? in : "<empty>");
557 /* If name is too big, just truncate. We don't care, we're
558 * using DNs, not the usernames.
560 if ( inlen > out_max )
563 /* This is a Simple Bind using SPASSWD. That means the in-directory
564 * userPassword of the Binding user already points at SASL, so it
565 * cannot be used to actually satisfy a password comparison. Just
566 * ignore it, some other mech will process it.
568 if ( !conn->c_sasl_bindop ||
569 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
571 /* See if we need to add request, can only do it once */
572 prop_getnames( props, slap_propnames, auxvals );
573 if ( !auxvals[0].name )
574 prop_request( props, slap_propnames );
576 if ( flags & SASL_CU_AUTHID )
577 which = SLAP_SASL_PROP_AUTHC;
579 which = SLAP_SASL_PROP_AUTHZ;
581 /* Need to store the Connection for auxprop_lookup */
582 if ( !auxvals[SLAP_SASL_PROP_CONN].values ) {
583 names[0] = slap_propnames[SLAP_SASL_PROP_CONN];
585 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
588 /* Already been here? */
589 if ( auxvals[which].values )
592 /* Normally we require an authzID to have a u: or dn: prefix.
593 * However, SASL frequently gives us an authzID that is just
594 * an exact copy of the authcID, without a prefix. We need to
595 * detect and allow this condition. If SASL calls canonicalize
596 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
597 * But if it's broken into two calls, we need to remember the
598 * authcID so that we can compare the authzID later. We store
599 * the authcID temporarily in conn->c_sasl_dn. We necessarily
600 * finish Canonicalizing before Authorizing, so there is no
601 * conflict with slap_sasl_authorize's use of this temp var.
603 * The SASL EXTERNAL mech is backwards from all the other mechs,
604 * it does authzID before the authcID. If we see that authzID
605 * has already been done, don't do anything special with authcID.
607 if ( flags == SASL_CU_AUTHID && !auxvals[SLAP_SASL_PROP_AUTHZ].values ) {
608 conn->c_sasl_dn.bv_val = (char *) in;
609 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
610 rc = strcmp( in, conn->c_sasl_dn.bv_val );
611 conn->c_sasl_dn.bv_val = NULL;
612 /* They were equal, no work needed */
613 if ( !rc ) goto done;
616 bvin.bv_val = (char *)in;
618 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
619 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
620 if ( rc != LDAP_SUCCESS ) {
621 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
625 names[0] = slap_propnames[which];
628 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
630 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
631 conn ? conn->c_connid : -1, names[0]+1,
632 dn.bv_val ? dn.bv_val : "<EMPTY>" );
635 AC_MEMCPY( out, in, inlen );
647 char *requested_user,
651 const char *def_realm,
653 struct propctx *props)
655 Connection *conn = (Connection *)context;
657 * (SLAP_SASL_PROP_COUNT - 1) because we skip "conn",
658 * + 1 for NULL termination?
660 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
661 struct berval authcDN, authzDN = BER_BVNULL;
664 /* Simple Binds don't support proxy authorization, ignore it */
665 if ( !conn->c_sasl_bindop ||
666 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
668 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
669 "authcid=\"%s\" authzid=\"%s\"\n",
670 conn ? conn->c_connid : -1, auth_identity, requested_user );
671 if ( conn->c_sasl_dn.bv_val ) {
672 ch_free( conn->c_sasl_dn.bv_val );
673 BER_BVZERO( &conn->c_sasl_dn );
676 /* Skip SLAP_SASL_PROP_CONN */
677 prop_getnames( props, slap_propnames+1, auxvals );
679 /* Should not happen */
680 if ( !auxvals[0].values ) {
681 sasl_seterror( sconn, 0, "invalid authcid" );
685 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
686 conn->c_sasl_dn = authcDN;
688 /* Nothing to do if no authzID was given */
689 if ( !auxvals[1].name || !auxvals[1].values ) {
693 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
695 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
696 if ( rc != LDAP_SUCCESS ) {
697 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
698 "proxy authorization disallowed (%d)\n",
699 (long) (conn ? conn->c_connid : -1), rc, 0 );
701 sasl_seterror( sconn, 0, "not authorized" );
702 ch_free( authzDN.bv_val );
706 /* FIXME: we need yet another dup because slap_sasl_getdn()
707 * is using the bind operation slab */
708 if ( conn->c_sasl_bindop ) {
709 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
710 slap_sl_free( authzDN.bv_val,
711 conn->c_sasl_bindop->o_tmpmemctx );
714 conn->c_sasl_authz_dn = authzDN;
718 if (conn->c_sasl_bindop) {
719 Statslog( LDAP_DEBUG_STATS,
720 "conn=%lu op=%lu BIND authcid=\"%s\" authzid=\"%s\"\n",
721 conn->c_connid, conn->c_sasl_bindop->o_opid,
722 auth_identity, requested_user, 0);
725 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
726 " proxy authorization allowed authzDN=\"%s\"\n",
727 (long) (conn ? conn->c_connid : -1),
728 authzDN.bv_val ? authzDN.bv_val : "", 0 );
740 struct berval authcDN, authzDN = BER_BVNULL;
742 Connection *conn = context;
744 struct berval bvauthcid, bvauthzid;
747 if ( conn->c_sasl_dn.bv_val ) {
748 ch_free( conn->c_sasl_dn.bv_val );
749 BER_BVZERO( &conn->c_sasl_dn );
752 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
753 "authcid=\"%s\" authzid=\"%s\"\n",
754 (long) (conn ? conn->c_connid : -1),
755 authcid ? authcid : "<empty>",
756 authzid ? authzid : "<empty>" );
758 /* Figure out how much data we have for the dn */
759 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
760 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
761 Debug(LDAP_DEBUG_TRACE,
762 "authorize: getprop(REALM) failed!\n", 0,0,0);
763 *errstr = "Could not extract realm";
767 /* Convert the identities to DN's. If no authzid was given, client will
768 be bound as the DN matching their username */
769 bvauthcid.bv_val = authcid;
770 bvauthcid.bv_len = authcid ? strlen( authcid ) : 0;
771 rc = slap_sasl_getdn( conn, NULL, &bvauthcid, realm,
772 &authcDN, SLAP_GETDN_AUTHCID );
773 if( rc != LDAP_SUCCESS ) {
774 *errstr = ldap_err2string( rc );
777 conn->c_sasl_dn = authcDN;
778 if( ( authzid == NULL ) || !strcmp( authcid, authzid ) ) {
779 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
780 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
785 bvauthzid.bv_val = authzid;
786 bvauthzid.bv_len = authzid ? strlen( authzid ) : 0;
787 rc = slap_sasl_getdn( conn, NULL, &bvauthzid, realm,
788 &authzDN, SLAP_GETDN_AUTHZID );
789 if( rc != LDAP_SUCCESS ) {
790 *errstr = ldap_err2string( rc );
794 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
796 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
797 "proxy authorization disallowed (%d)\n",
798 (long) (conn ? conn->c_connid : -1), rc, 0 );
800 *errstr = "not authorized";
801 ch_free( authzDN.bv_val );
805 /* FIXME: we need yet another dup because slap_sasl_getdn()
806 * is using the bind operation slab */
807 if ( conn->c_sasl_bindop ) {
808 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
809 slap_sl_free( authzDN.bv_val,
810 conn->c_sasl_bindop->o_tmpmemctx );
813 conn->c_sasl_authz_dn = authzDN;
817 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
818 " authorization allowed authzDN=\"%s\"\n",
819 (long) (conn ? conn->c_connid : -1),
820 authzDN.bv_val ? authzDN.bv_val : "", 0 );
822 if ( conn->c_sasl_bindop ) {
823 Statslog( LDAP_DEBUG_STATS,
824 "conn=%lu op=%lu BIND authcid=\"%s\" authzid=\"%s\"\n",
825 conn->c_connid, conn->c_sasl_bindop->o_opid,
826 authcid, authzid ? authzid : "", 0);
832 #endif /* SASL_VERSION_MAJOR >= 2 */
835 slap_sasl_err2ldap( int saslerr )
839 /* map SASL errors to LDAP resultCode returned by:
841 * SASL_OK, SASL_NOMEM
843 * SASL_OK, SASL_CONTINUE, SASL_TRANS, SASL_BADPARAM, SASL_BADPROT,
845 * sasl_server_start()
848 * SASL_OK, SASL_BADPARAM
856 rc = LDAP_SASL_BIND_IN_PROGRESS;
863 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
869 rc = LDAP_INVALID_CREDENTIALS;
872 rc = LDAP_INSUFFICIENT_ACCESS;
876 rc = LDAP_INAPPROPRIATE_AUTH;
880 rc = LDAP_UNAVAILABLE;
883 rc = LDAP_UNWILLING_TO_PERFORM;
895 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
898 const struct berval *sc,
899 const struct berval * passwd,
900 const struct berval * cred,
905 void *ctx, *sconn = NULL;
907 for( i=0; i<cred->bv_len; i++) {
908 if(cred->bv_val[i] == '\0') {
909 return LUTIL_PASSWD_ERR; /* NUL character in password */
913 if( cred->bv_val[i] != '\0' ) {
914 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
917 for( i=0; i<passwd->bv_len; i++) {
918 if(passwd->bv_val[i] == '\0') {
919 return LUTIL_PASSWD_ERR; /* NUL character in password */
923 if( passwd->bv_val[i] != '\0' ) {
924 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
927 rtn = LUTIL_PASSWD_ERR;
929 ctx = ldap_pvt_thread_pool_context();
930 ldap_pvt_thread_pool_getkey( ctx, slap_sasl_bind, &sconn, NULL );
932 if( sconn != NULL ) {
934 # if SASL_VERSION_MAJOR < 2
935 sc = sasl_checkpass( sconn,
936 passwd->bv_val, passwd->bv_len,
937 cred->bv_val, cred->bv_len,
940 sc = sasl_checkpass( sconn,
941 passwd->bv_val, passwd->bv_len,
942 cred->bv_val, cred->bv_len );
944 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
949 #endif /* SLAPD_SPASSWD */
951 #endif /* HAVE_CYRUS_SASL */
953 int slap_sasl_init( void )
955 #ifdef HAVE_CYRUS_SASL
957 static sasl_callback_t server_callbacks[] = {
958 { SASL_CB_LOG, &slap_sasl_log, NULL },
959 { SASL_CB_LIST_END, NULL, NULL }
962 #ifdef HAVE_SASL_VERSION
963 /* stringify the version number, sasl.h doesn't do it for us */
964 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
965 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
966 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
969 sasl_version( NULL, &rc );
970 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
971 (rc & 0xffff) < SASL_VERSION_STEP)
973 char version[sizeof("xxx.xxx.xxxxx")];
974 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
976 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
977 " expected " SASL_VERSION_STRING ","
978 " got %s\n", version, 0, 0 );
983 /* SASL 2 does its own memory management internally */
984 #if SASL_VERSION_MAJOR < 2
993 ldap_pvt_sasl_mutex_new,
994 ldap_pvt_sasl_mutex_lock,
995 ldap_pvt_sasl_mutex_unlock,
996 ldap_pvt_sasl_mutex_dispose );
998 #if SASL_VERSION_MAJOR >= 2
999 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1001 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1002 if( rc != SASL_OK ) {
1003 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
1008 /* should provide callbacks for logging */
1009 /* server name should be configurable */
1010 rc = sasl_server_init( server_callbacks, "slapd" );
1012 if( rc != SASL_OK ) {
1013 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
1015 #if SASL_VERSION_MAJOR < 2
1016 /* A no-op used to make sure we linked with Cyrus 1.5 */
1017 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1023 #ifdef SLAPD_SPASSWD
1024 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
1027 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1030 /* default security properties */
1031 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1032 sasl_secprops.max_ssf = INT_MAX;
1033 sasl_secprops.maxbufsize = 65536;
1034 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1040 int slap_sasl_destroy( void )
1042 #ifdef HAVE_CYRUS_SASL
1045 free( global_host );
1051 #if SASL_VERSION_MAJOR >= 2
1053 slap_sasl_peer2ipport( struct berval *peer )
1057 *p = &peer->bv_val[ STRLENOF( "IP=" ) ];
1058 ber_len_t plen = peer->bv_len - STRLENOF( "IP=" );
1061 if ( p[0] == '[' ) {
1065 ipport = ch_strdup( &p[isv6] );
1067 /* Convert IPv6/IPv4 addresses to address;port syntax. */
1068 p = strrchr( ipport, ':' );
1072 assert( p[-1] == ']' );
1073 AC_MEMCPY( &p[-1], p, plen - ( p - ipport ) + 1 );
1076 } else if ( isv6 ) {
1079 assert( p[plen] == ']' );
1087 int slap_sasl_open( Connection *conn, int reopen )
1089 int sc = LDAP_SUCCESS;
1090 #ifdef HAVE_CYRUS_SASL
1093 sasl_conn_t *ctx = NULL;
1094 sasl_callback_t *session_callbacks;
1096 #if SASL_VERSION_MAJOR >= 2
1097 char *ipremoteport = NULL, *iplocalport = NULL;
1100 assert( conn->c_sasl_authctx == NULL );
1103 assert( conn->c_sasl_extra == NULL );
1106 #if SASL_VERSION_MAJOR >= 2
1107 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1109 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1111 if( session_callbacks == NULL ) {
1112 Debug( LDAP_DEBUG_ANY,
1113 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1116 conn->c_sasl_extra = session_callbacks;
1118 session_callbacks[cb=0].id = SASL_CB_LOG;
1119 session_callbacks[cb].proc = &slap_sasl_log;
1120 session_callbacks[cb++].context = conn;
1122 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1123 session_callbacks[cb].proc = &slap_sasl_authorize;
1124 session_callbacks[cb++].context = conn;
1126 #if SASL_VERSION_MAJOR >= 2
1127 session_callbacks[cb].id = SASL_CB_CANON_USER;
1128 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1129 session_callbacks[cb++].context = conn;
1132 session_callbacks[cb].id = SASL_CB_LIST_END;
1133 session_callbacks[cb].proc = NULL;
1134 session_callbacks[cb++].context = NULL;
1136 session_callbacks = conn->c_sasl_extra;
1139 conn->c_sasl_layers = 0;
1141 /* create new SASL context */
1142 #if SASL_VERSION_MAJOR >= 2
1143 if ( conn->c_sock_name.bv_len != 0 &&
1144 strncmp( conn->c_sock_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1146 iplocalport = slap_sasl_peer2ipport( &conn->c_sock_name );
1149 if ( conn->c_peer_name.bv_len != 0 &&
1150 strncmp( conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1152 ipremoteport = slap_sasl_peer2ipport( &conn->c_peer_name );
1155 sc = sasl_server_new( "ldap", global_host, global_realm,
1156 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1157 if ( iplocalport != NULL ) {
1158 ch_free( iplocalport );
1160 if ( ipremoteport != NULL ) {
1161 ch_free( ipremoteport );
1164 sc = sasl_server_new( "ldap", global_host, global_realm,
1165 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1168 if( sc != SASL_OK ) {
1169 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1175 conn->c_sasl_authctx = ctx;
1177 if( sc == SASL_OK ) {
1178 sc = sasl_setprop( ctx,
1179 SASL_SEC_PROPS, &sasl_secprops );
1181 if( sc != SASL_OK ) {
1182 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1185 slap_sasl_close( conn );
1190 sc = slap_sasl_err2ldap( sc );
1192 #elif defined(SLAP_BUILTIN_SASL)
1193 /* built-in SASL implementation */
1194 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1195 if( ctx == NULL ) return -1;
1197 ctx->sc_external_ssf = 0;
1198 BER_BVZERO( &ctx->sc_external_id );
1200 conn->c_sasl_authctx = ctx;
1206 int slap_sasl_external(
1209 struct berval *auth_id )
1211 #if SASL_VERSION_MAJOR >= 2
1213 sasl_conn_t *ctx = conn->c_sasl_authctx;
1215 if ( ctx == NULL ) {
1216 return LDAP_UNAVAILABLE;
1219 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1221 if ( sc != SASL_OK ) {
1225 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1226 auth_id ? auth_id->bv_val : NULL );
1228 if ( sc != SASL_OK ) {
1232 #elif defined(HAVE_CYRUS_SASL)
1234 sasl_conn_t *ctx = conn->c_sasl_authctx;
1235 sasl_external_properties_t extprops;
1237 if ( ctx == NULL ) {
1238 return LDAP_UNAVAILABLE;
1241 memset( &extprops, '\0', sizeof(extprops) );
1243 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1245 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1246 (void *) &extprops );
1248 if ( sc != SASL_OK ) {
1251 #elif defined(SLAP_BUILTIN_SASL)
1252 /* built-in SASL implementation */
1253 SASL_CTX *ctx = conn->c_sasl_authctx;
1254 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1256 ctx->sc_external_ssf = ssf;
1258 ctx->sc_external_id = *auth_id;
1259 BER_BVZERO( auth_id );
1261 BER_BVZERO( &ctx->sc_external_id );
1265 return LDAP_SUCCESS;
1268 int slap_sasl_reset( Connection *conn )
1270 return LDAP_SUCCESS;
1273 char ** slap_sasl_mechs( Connection *conn )
1275 char **mechs = NULL;
1277 #ifdef HAVE_CYRUS_SASL
1278 sasl_conn_t *ctx = conn->c_sasl_authctx;
1280 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1284 SASL_CONST char *mechstr;
1286 sc = sasl_listmech( ctx,
1287 NULL, NULL, ",", NULL,
1288 &mechstr, NULL, NULL );
1290 if( sc != SASL_OK ) {
1291 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1297 mechs = ldap_str2charray( mechstr, "," );
1299 #if SASL_VERSION_MAJOR < 2
1303 #elif defined(SLAP_BUILTIN_SASL)
1304 /* builtin SASL implementation */
1305 SASL_CTX *ctx = conn->c_sasl_authctx;
1306 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1307 /* should check ssf */
1308 mechs = ldap_str2charray( "EXTERNAL", "," );
1315 int slap_sasl_close( Connection *conn )
1317 #ifdef HAVE_CYRUS_SASL
1318 sasl_conn_t *ctx = conn->c_sasl_authctx;
1321 sasl_dispose( &ctx );
1323 if ( conn->c_sasl_sockctx &&
1324 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1326 ctx = conn->c_sasl_sockctx;
1327 sasl_dispose( &ctx );
1330 conn->c_sasl_authctx = NULL;
1331 conn->c_sasl_sockctx = NULL;
1332 conn->c_sasl_done = 0;
1334 free( conn->c_sasl_extra );
1335 conn->c_sasl_extra = NULL;
1337 #elif defined(SLAP_BUILTIN_SASL)
1338 SASL_CTX *ctx = conn->c_sasl_authctx;
1340 if( ctx->sc_external_id.bv_val ) {
1341 free( ctx->sc_external_id.bv_val );
1342 BER_BVZERO( &ctx->sc_external_id );
1345 conn->c_sasl_authctx = NULL;
1349 return LDAP_SUCCESS;
1352 int slap_sasl_bind( Operation *op, SlapReply *rs )
1354 #ifdef HAVE_CYRUS_SASL
1355 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1356 struct berval response;
1357 unsigned reslen = 0;
1360 Debug(LDAP_DEBUG_ARGS,
1361 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1362 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1363 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1364 op->o_conn->c_sasl_bind_mech.bv_val,
1365 op->orb_cred.bv_len );
1368 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1369 "SASL unavailable on this session" );
1373 #if SASL_VERSION_MAJOR >= 2
1374 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1375 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1376 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1377 sasl_server_step( ctx, cred, clen, resp, rlen )
1379 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1380 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1381 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1382 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1385 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1386 /* If we already authenticated once, must use a new context */
1387 if ( op->o_conn->c_sasl_done ) {
1389 const char *authid = NULL;
1390 #if SASL_VERSION_MAJOR >= 2
1391 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1392 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1393 if ( authid ) authid = ch_strdup( authid );
1395 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1396 sasl_dispose( &ctx );
1398 op->o_conn->c_sasl_authctx = NULL;
1400 slap_sasl_open( op->o_conn, 1 );
1401 ctx = op->o_conn->c_sasl_authctx;
1402 #if SASL_VERSION_MAJOR >= 2
1404 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1405 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1406 ch_free( (char *)authid );
1411 op->o_conn->c_sasl_bind_mech.bv_val,
1412 op->orb_cred.bv_val, op->orb_cred.bv_len,
1413 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1417 op->orb_cred.bv_val, op->orb_cred.bv_len,
1418 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1421 response.bv_len = reslen;
1423 if ( sc == SASL_OK ) {
1424 sasl_ssf_t *ssf = NULL;
1426 op->orb_edn = op->o_conn->c_sasl_dn;
1427 BER_BVZERO( &op->o_conn->c_sasl_dn );
1428 op->o_conn->c_sasl_done = 1;
1430 rs->sr_err = LDAP_SUCCESS;
1432 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1433 op->orb_ssf = ssf ? *ssf : 0;
1437 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1438 op->o_conn->c_sasl_layers++;
1440 /* If there's an old layer, set sockctx to NULL to
1441 * tell connection_read() to wait for us to finish.
1442 * Otherwise there is a race condition: we have to
1443 * send the Bind response using the old security
1444 * context and then remove it before reading any
1447 if ( op->o_conn->c_sasl_sockctx ) {
1448 ctx = op->o_conn->c_sasl_sockctx;
1449 op->o_conn->c_sasl_sockctx = NULL;
1451 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1453 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1456 /* Must send response using old security layer */
1457 if (response.bv_len) rs->sr_sasldata = &response;
1458 send_ldap_sasl( op, rs );
1460 /* Now dispose of the old security layer.
1463 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1464 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1465 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1466 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1467 sasl_dispose( &ctx );
1469 } else if ( sc == SASL_CONTINUE ) {
1470 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1471 #if SASL_VERSION_MAJOR >= 2
1472 rs->sr_text = sasl_errdetail( ctx );
1474 rs->sr_sasldata = &response;
1475 send_ldap_sasl( op, rs );
1478 #if SASL_VERSION_MAJOR >= 2
1479 rs->sr_text = sasl_errdetail( ctx );
1481 rs->sr_err = slap_sasl_err2ldap( sc ),
1482 send_ldap_result( op, rs );
1485 #if SASL_VERSION_MAJOR < 2
1486 if( response.bv_len ) ch_free( response.bv_val );
1489 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1491 #elif defined(SLAP_BUILTIN_SASL)
1492 /* built-in SASL implementation */
1493 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1495 if ( ctx == NULL ) {
1496 send_ldap_error( op, rs, LDAP_OTHER,
1497 "Internal SASL Error" );
1499 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1502 if( op->orb_cred.bv_len ) {
1503 rs->sr_text = "proxy authorization not support";
1504 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1505 send_ldap_result( op, rs );
1508 op->orb_edn = ctx->sc_external_id;
1509 rs->sr_err = LDAP_SUCCESS;
1510 rs->sr_sasldata = NULL;
1511 send_ldap_sasl( op, rs );
1515 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1516 "requested SASL mechanism not supported" );
1519 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1520 "SASL not supported" );
1526 char* slap_sasl_secprops( const char *in )
1528 #ifdef HAVE_CYRUS_SASL
1529 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1531 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1533 return "SASL not supported";
1537 void slap_sasl_secprops_unparse( struct berval *bv )
1539 #ifdef HAVE_CYRUS_SASL
1540 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1544 #ifdef HAVE_CYRUS_SASL
1546 slap_sasl_setpass( Operation *op, SlapReply *rs )
1548 struct berval id = BER_BVNULL; /* needs to come from connection */
1549 struct berval new = BER_BVNULL;
1550 struct berval old = BER_BVNULL;
1552 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1554 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1555 (SASL_CONST void **)(char *)&id.bv_val );
1557 if( rs->sr_err != SASL_OK ) {
1558 rs->sr_text = "unable to retrieve SASL username";
1559 rs->sr_err = LDAP_OTHER;
1563 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1564 id.bv_val ? id.bv_val : "", 0, 0 );
1566 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1567 NULL, &old, &new, &rs->sr_text );
1569 if( rs->sr_err != LDAP_SUCCESS ) {
1573 if( new.bv_len == 0 ) {
1574 slap_passwd_generate(&new);
1576 if( new.bv_len == 0 ) {
1577 rs->sr_text = "password generation failed.";
1578 rs->sr_err = LDAP_OTHER;
1582 rs->sr_rspdata = slap_passwd_return( &new );
1585 #if SASL_VERSION_MAJOR < 2
1586 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1587 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1589 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1590 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1591 if( rs->sr_err != SASL_OK ) {
1592 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1595 switch(rs->sr_err) {
1597 rs->sr_err = LDAP_SUCCESS;
1607 rs->sr_err = LDAP_OTHER;
1613 #endif /* HAVE_CYRUS_SASL */
1615 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1616 * string returned in *dn is in its own allocated memory, and must be free'd
1617 * by the calling process. -Mark Adamson, Carnegie Mellon
1619 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1620 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1627 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1628 char *user_realm, struct berval *dn, int flags )
1630 int rc, is_dn = SET_NONE, do_norm = 1;
1631 struct berval dn2, *mech;
1633 assert( conn != NULL );
1634 assert( id != NULL );
1636 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1638 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1639 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1640 (unsigned long) id->bv_len ) );
1643 op = conn->c_sasl_bindop;
1645 assert( op != NULL );
1649 if ( !BER_BVISNULL( id ) ) {
1650 /* Blatantly anonymous ID */
1651 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1653 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1654 return( LDAP_SUCCESS );
1658 /* FIXME: if empty, should we stop? */
1659 BER_BVSTR( id, "" );
1662 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1663 mech = &conn->c_sasl_bind_mech;
1665 mech = &conn->c_authmech;
1668 /* An authcID needs to be converted to authzID form. Set the
1669 * values directly into *dn; they will be normalized later. (and
1670 * normalizing always makes a new copy.) An ID from a TLS certificate
1671 * is already normalized, so copy it and skip normalization.
1673 if( flags & SLAP_GETDN_AUTHCID ) {
1674 if( bvmatch( mech, &ext_bv )) {
1675 /* EXTERNAL DNs are already normalized */
1676 assert( !BER_BVISNULL( id ) );
1680 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1683 /* convert to u:<username> form */
1689 if( is_dn == SET_NONE ) {
1690 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1692 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1693 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1695 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1697 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1698 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1702 /* No other possibilities from here */
1703 if( is_dn == SET_NONE ) {
1705 return( LDAP_INAPPROPRIATE_AUTH );
1708 /* Username strings */
1709 if( is_dn == SET_U ) {
1710 /* ITS#3419: values may need escape */
1712 LDAPAVA *RDNs[ 4 ][ 2 ];
1717 DN[ irdn ] = RDNs[ irdn ];
1718 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1719 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1720 AVAs[ irdn ].la_value = *dn;
1721 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1722 AVAs[ irdn ].la_private = NULL;
1723 RDNs[ irdn ][ 1 ] = NULL;
1725 if ( user_realm && *user_realm ) {
1727 DN[ irdn ] = RDNs[ irdn ];
1728 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1729 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1730 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1731 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1732 AVAs[ irdn ].la_private = NULL;
1733 RDNs[ irdn ][ 1 ] = NULL;
1736 if ( !BER_BVISNULL( mech ) ) {
1738 DN[ irdn ] = RDNs[ irdn ];
1739 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1740 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1741 AVAs[ irdn ].la_value = *mech;
1742 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1743 AVAs[ irdn ].la_private = NULL;
1744 RDNs[ irdn ][ 1 ] = NULL;
1748 DN[ irdn ] = RDNs[ irdn ];
1749 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1750 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1751 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1752 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1753 AVAs[ irdn ].la_private = NULL;
1754 RDNs[ irdn ][ 1 ] = NULL;
1759 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1761 if ( rc != LDAP_SUCCESS ) {
1766 Debug( LDAP_DEBUG_TRACE,
1767 "slap_sasl_getdn: u:id converted to %s\n",
1772 /* Dup the DN in any case, so we don't risk
1773 * leaks or dangling pointers later,
1774 * and the DN value is '\0' terminated */
1775 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1776 dn->bv_val = dn2.bv_val;
1779 /* All strings are in DN form now. Normalize if needed. */
1781 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1783 /* User DNs were constructed above and must be freed now */
1784 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1786 if ( rc != LDAP_SUCCESS ) {
1793 /* Run thru regexp */
1794 slap_sasl2dn( op, dn, &dn2, flags );
1795 if( !BER_BVISNULL( &dn2 ) ) {
1796 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1798 Debug( LDAP_DEBUG_TRACE,
1799 "slap_sasl_getdn: dn:id converted to %s\n",
1803 return( LDAP_SUCCESS );