2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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 if( global_host == NULL ) {
1142 global_host = ldap_pvt_get_fqdn( NULL );
1145 /* create new SASL context */
1146 #if SASL_VERSION_MAJOR >= 2
1147 if ( conn->c_sock_name.bv_len != 0 &&
1148 strncmp( conn->c_sock_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1150 iplocalport = slap_sasl_peer2ipport( &conn->c_sock_name );
1153 if ( conn->c_peer_name.bv_len != 0 &&
1154 strncmp( conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1156 ipremoteport = slap_sasl_peer2ipport( &conn->c_peer_name );
1159 sc = sasl_server_new( "ldap", global_host, global_realm,
1160 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1161 if ( iplocalport != NULL ) {
1162 ch_free( iplocalport );
1164 if ( ipremoteport != NULL ) {
1165 ch_free( ipremoteport );
1168 sc = sasl_server_new( "ldap", global_host, global_realm,
1169 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1172 if( sc != SASL_OK ) {
1173 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1179 conn->c_sasl_authctx = ctx;
1181 if( sc == SASL_OK ) {
1182 sc = sasl_setprop( ctx,
1183 SASL_SEC_PROPS, &sasl_secprops );
1185 if( sc != SASL_OK ) {
1186 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1189 slap_sasl_close( conn );
1194 sc = slap_sasl_err2ldap( sc );
1196 #elif defined(SLAP_BUILTIN_SASL)
1197 /* built-in SASL implementation */
1198 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1199 if( ctx == NULL ) return -1;
1201 ctx->sc_external_ssf = 0;
1202 BER_BVZERO( &ctx->sc_external_id );
1204 conn->c_sasl_authctx = ctx;
1210 int slap_sasl_external(
1213 struct berval *auth_id )
1215 #if SASL_VERSION_MAJOR >= 2
1217 sasl_conn_t *ctx = conn->c_sasl_authctx;
1219 if ( ctx == NULL ) {
1220 return LDAP_UNAVAILABLE;
1223 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1225 if ( sc != SASL_OK ) {
1229 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1230 auth_id ? auth_id->bv_val : NULL );
1232 if ( sc != SASL_OK ) {
1236 #elif defined(HAVE_CYRUS_SASL)
1238 sasl_conn_t *ctx = conn->c_sasl_authctx;
1239 sasl_external_properties_t extprops;
1241 if ( ctx == NULL ) {
1242 return LDAP_UNAVAILABLE;
1245 memset( &extprops, '\0', sizeof(extprops) );
1247 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1249 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1250 (void *) &extprops );
1252 if ( sc != SASL_OK ) {
1255 #elif defined(SLAP_BUILTIN_SASL)
1256 /* built-in SASL implementation */
1257 SASL_CTX *ctx = conn->c_sasl_authctx;
1258 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1260 ctx->sc_external_ssf = ssf;
1262 ctx->sc_external_id = *auth_id;
1263 BER_BVZERO( auth_id );
1265 BER_BVZERO( &ctx->sc_external_id );
1269 return LDAP_SUCCESS;
1272 int slap_sasl_reset( Connection *conn )
1274 return LDAP_SUCCESS;
1277 char ** slap_sasl_mechs( Connection *conn )
1279 char **mechs = NULL;
1281 #ifdef HAVE_CYRUS_SASL
1282 sasl_conn_t *ctx = conn->c_sasl_authctx;
1284 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1288 SASL_CONST char *mechstr;
1290 sc = sasl_listmech( ctx,
1291 NULL, NULL, ",", NULL,
1292 &mechstr, NULL, NULL );
1294 if( sc != SASL_OK ) {
1295 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1301 mechs = ldap_str2charray( mechstr, "," );
1303 #if SASL_VERSION_MAJOR < 2
1307 #elif defined(SLAP_BUILTIN_SASL)
1308 /* builtin SASL implementation */
1309 SASL_CTX *ctx = conn->c_sasl_authctx;
1310 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1311 /* should check ssf */
1312 mechs = ldap_str2charray( "EXTERNAL", "," );
1319 int slap_sasl_close( Connection *conn )
1321 #ifdef HAVE_CYRUS_SASL
1322 sasl_conn_t *ctx = conn->c_sasl_authctx;
1325 sasl_dispose( &ctx );
1327 if ( conn->c_sasl_sockctx &&
1328 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1330 ctx = conn->c_sasl_sockctx;
1331 sasl_dispose( &ctx );
1334 conn->c_sasl_authctx = NULL;
1335 conn->c_sasl_sockctx = NULL;
1336 conn->c_sasl_done = 0;
1338 free( conn->c_sasl_extra );
1339 conn->c_sasl_extra = NULL;
1341 #elif defined(SLAP_BUILTIN_SASL)
1342 SASL_CTX *ctx = conn->c_sasl_authctx;
1344 if( ctx->sc_external_id.bv_val ) {
1345 free( ctx->sc_external_id.bv_val );
1346 BER_BVZERO( &ctx->sc_external_id );
1349 conn->c_sasl_authctx = NULL;
1353 return LDAP_SUCCESS;
1356 int slap_sasl_bind( Operation *op, SlapReply *rs )
1358 #ifdef HAVE_CYRUS_SASL
1359 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1360 struct berval response;
1361 unsigned reslen = 0;
1364 Debug(LDAP_DEBUG_ARGS,
1365 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1366 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1367 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1368 op->o_conn->c_sasl_bind_mech.bv_val,
1369 op->orb_cred.bv_len );
1372 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1373 "SASL unavailable on this session" );
1377 #if SASL_VERSION_MAJOR >= 2
1378 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1379 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1380 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1381 sasl_server_step( ctx, cred, clen, resp, rlen )
1383 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1384 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1385 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1386 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1389 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1390 /* If we already authenticated once, must use a new context */
1391 if ( op->o_conn->c_sasl_done ) {
1393 const char *authid = NULL;
1394 #if SASL_VERSION_MAJOR >= 2
1395 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1396 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1397 if ( authid ) authid = ch_strdup( authid );
1399 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1400 sasl_dispose( &ctx );
1402 op->o_conn->c_sasl_authctx = NULL;
1404 slap_sasl_open( op->o_conn, 1 );
1405 ctx = op->o_conn->c_sasl_authctx;
1406 #if SASL_VERSION_MAJOR >= 2
1408 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1409 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1410 ch_free( (char *)authid );
1415 op->o_conn->c_sasl_bind_mech.bv_val,
1416 op->orb_cred.bv_val, op->orb_cred.bv_len,
1417 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1421 op->orb_cred.bv_val, op->orb_cred.bv_len,
1422 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1425 response.bv_len = reslen;
1427 if ( sc == SASL_OK ) {
1428 sasl_ssf_t *ssf = NULL;
1430 op->orb_edn = op->o_conn->c_sasl_dn;
1431 BER_BVZERO( &op->o_conn->c_sasl_dn );
1432 op->o_conn->c_sasl_done = 1;
1434 rs->sr_err = LDAP_SUCCESS;
1436 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1437 op->orb_ssf = ssf ? *ssf : 0;
1441 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1442 op->o_conn->c_sasl_layers++;
1444 /* If there's an old layer, set sockctx to NULL to
1445 * tell connection_read() to wait for us to finish.
1446 * Otherwise there is a race condition: we have to
1447 * send the Bind response using the old security
1448 * context and then remove it before reading any
1451 if ( op->o_conn->c_sasl_sockctx ) {
1452 ctx = op->o_conn->c_sasl_sockctx;
1453 op->o_conn->c_sasl_sockctx = NULL;
1455 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1457 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1460 /* Must send response using old security layer */
1461 if (response.bv_len) rs->sr_sasldata = &response;
1462 send_ldap_sasl( op, rs );
1464 /* Now dispose of the old security layer.
1467 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1468 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1469 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1470 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1471 sasl_dispose( &ctx );
1473 } else if ( sc == SASL_CONTINUE ) {
1474 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1475 #if SASL_VERSION_MAJOR >= 2
1476 rs->sr_text = sasl_errdetail( ctx );
1478 rs->sr_sasldata = &response;
1479 send_ldap_sasl( op, rs );
1482 #if SASL_VERSION_MAJOR >= 2
1483 rs->sr_text = sasl_errdetail( ctx );
1485 rs->sr_err = slap_sasl_err2ldap( sc ),
1486 send_ldap_result( op, rs );
1489 #if SASL_VERSION_MAJOR < 2
1490 if( response.bv_len ) ch_free( response.bv_val );
1493 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1495 #elif defined(SLAP_BUILTIN_SASL)
1496 /* built-in SASL implementation */
1497 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1499 if ( ctx == NULL ) {
1500 send_ldap_error( op, rs, LDAP_OTHER,
1501 "Internal SASL Error" );
1503 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1506 if( op->orb_cred.bv_len ) {
1507 rs->sr_text = "proxy authorization not support";
1508 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1509 send_ldap_result( op, rs );
1512 op->orb_edn = ctx->sc_external_id;
1513 rs->sr_err = LDAP_SUCCESS;
1514 rs->sr_sasldata = NULL;
1515 send_ldap_sasl( op, rs );
1519 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1520 "requested SASL mechanism not supported" );
1523 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1524 "SASL not supported" );
1530 char* slap_sasl_secprops( const char *in )
1532 #ifdef HAVE_CYRUS_SASL
1533 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1535 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1537 return "SASL not supported";
1541 void slap_sasl_secprops_unparse( struct berval *bv )
1543 #ifdef HAVE_CYRUS_SASL
1544 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1548 #ifdef HAVE_CYRUS_SASL
1550 slap_sasl_setpass( Operation *op, SlapReply *rs )
1552 struct berval id = BER_BVNULL; /* needs to come from connection */
1553 struct berval new = BER_BVNULL;
1554 struct berval old = BER_BVNULL;
1556 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1558 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1559 (SASL_CONST void **)(char *)&id.bv_val );
1561 if( rs->sr_err != SASL_OK ) {
1562 rs->sr_text = "unable to retrieve SASL username";
1563 rs->sr_err = LDAP_OTHER;
1567 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1568 id.bv_val ? id.bv_val : "", 0, 0 );
1570 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1571 NULL, &old, &new, &rs->sr_text );
1573 if( rs->sr_err != LDAP_SUCCESS ) {
1577 if( new.bv_len == 0 ) {
1578 slap_passwd_generate(&new);
1580 if( new.bv_len == 0 ) {
1581 rs->sr_text = "password generation failed.";
1582 rs->sr_err = LDAP_OTHER;
1586 rs->sr_rspdata = slap_passwd_return( &new );
1589 #if SASL_VERSION_MAJOR < 2
1590 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1591 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1593 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1594 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1595 if( rs->sr_err != SASL_OK ) {
1596 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1599 switch(rs->sr_err) {
1601 rs->sr_err = LDAP_SUCCESS;
1611 rs->sr_err = LDAP_OTHER;
1617 #endif /* HAVE_CYRUS_SASL */
1619 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1620 * string returned in *dn is in its own allocated memory, and must be free'd
1621 * by the calling process. -Mark Adamson, Carnegie Mellon
1623 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1624 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1631 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1632 char *user_realm, struct berval *dn, int flags )
1634 int rc, is_dn = SET_NONE, do_norm = 1;
1635 struct berval dn2, *mech;
1637 assert( conn != NULL );
1638 assert( id != NULL );
1640 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1642 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1643 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1644 (unsigned long) id->bv_len ) );
1647 op = conn->c_sasl_bindop;
1649 assert( op != NULL );
1653 if ( !BER_BVISNULL( id ) ) {
1654 /* Blatantly anonymous ID */
1655 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1657 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1658 return( LDAP_SUCCESS );
1662 /* FIXME: if empty, should we stop? */
1663 BER_BVSTR( id, "" );
1666 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1667 mech = &conn->c_sasl_bind_mech;
1669 mech = &conn->c_authmech;
1672 /* An authcID needs to be converted to authzID form. Set the
1673 * values directly into *dn; they will be normalized later. (and
1674 * normalizing always makes a new copy.) An ID from a TLS certificate
1675 * is already normalized, so copy it and skip normalization.
1677 if( flags & SLAP_GETDN_AUTHCID ) {
1678 if( bvmatch( mech, &ext_bv )) {
1679 /* EXTERNAL DNs are already normalized */
1680 assert( !BER_BVISNULL( id ) );
1684 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1687 /* convert to u:<username> form */
1693 if( is_dn == SET_NONE ) {
1694 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1696 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1697 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1699 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1701 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1702 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1706 /* No other possibilities from here */
1707 if( is_dn == SET_NONE ) {
1709 return( LDAP_INAPPROPRIATE_AUTH );
1712 /* Username strings */
1713 if( is_dn == SET_U ) {
1714 /* ITS#3419: values may need escape */
1716 LDAPAVA *RDNs[ 4 ][ 2 ];
1721 DN[ irdn ] = RDNs[ irdn ];
1722 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1723 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1724 AVAs[ irdn ].la_value = *dn;
1725 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1726 AVAs[ irdn ].la_private = NULL;
1727 RDNs[ irdn ][ 1 ] = NULL;
1729 if ( user_realm && *user_realm ) {
1731 DN[ irdn ] = RDNs[ irdn ];
1732 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1733 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1734 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1735 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1736 AVAs[ irdn ].la_private = NULL;
1737 RDNs[ irdn ][ 1 ] = NULL;
1740 if ( !BER_BVISNULL( mech ) ) {
1742 DN[ irdn ] = RDNs[ irdn ];
1743 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1744 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1745 AVAs[ irdn ].la_value = *mech;
1746 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1747 AVAs[ irdn ].la_private = NULL;
1748 RDNs[ irdn ][ 1 ] = NULL;
1752 DN[ irdn ] = RDNs[ irdn ];
1753 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1754 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1755 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1756 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1757 AVAs[ irdn ].la_private = NULL;
1758 RDNs[ irdn ][ 1 ] = NULL;
1763 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1765 if ( rc != LDAP_SUCCESS ) {
1770 Debug( LDAP_DEBUG_TRACE,
1771 "slap_sasl_getdn: u:id converted to %s\n",
1776 /* Dup the DN in any case, so we don't risk
1777 * leaks or dangling pointers later,
1778 * and the DN value is '\0' terminated */
1779 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1780 dn->bv_val = dn2.bv_val;
1783 /* All strings are in DN form now. Normalize if needed. */
1785 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1787 /* User DNs were constructed above and must be freed now */
1788 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1790 if ( rc != LDAP_SUCCESS ) {
1797 /* Run thru regexp */
1798 slap_sasl2dn( op, dn, &dn2, flags );
1799 if( !BER_BVISNULL( &dn2 ) ) {
1800 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1802 Debug( LDAP_DEBUG_TRACE,
1803 "slap_sasl_getdn: dn:id converted to %s\n",
1807 return( LDAP_SUCCESS );