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( 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 int slap_sasl_open( Connection *conn, int reopen )
1053 int sc = LDAP_SUCCESS;
1054 #ifdef HAVE_CYRUS_SASL
1057 sasl_conn_t *ctx = NULL;
1058 sasl_callback_t *session_callbacks;
1060 #if SASL_VERSION_MAJOR >= 2
1061 char *ipremoteport = NULL, *iplocalport = NULL;
1064 assert( conn->c_sasl_authctx == NULL );
1067 assert( conn->c_sasl_extra == NULL );
1070 #if SASL_VERSION_MAJOR >= 2
1071 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1073 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1075 if( session_callbacks == NULL ) {
1076 Debug( LDAP_DEBUG_ANY,
1077 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1080 conn->c_sasl_extra = session_callbacks;
1082 session_callbacks[cb=0].id = SASL_CB_LOG;
1083 session_callbacks[cb].proc = &slap_sasl_log;
1084 session_callbacks[cb++].context = conn;
1086 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1087 session_callbacks[cb].proc = &slap_sasl_authorize;
1088 session_callbacks[cb++].context = conn;
1090 #if SASL_VERSION_MAJOR >= 2
1091 session_callbacks[cb].id = SASL_CB_CANON_USER;
1092 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1093 session_callbacks[cb++].context = conn;
1096 session_callbacks[cb].id = SASL_CB_LIST_END;
1097 session_callbacks[cb].proc = NULL;
1098 session_callbacks[cb++].context = NULL;
1100 session_callbacks = conn->c_sasl_extra;
1103 conn->c_sasl_layers = 0;
1105 if( global_host == NULL ) {
1106 global_host = ldap_pvt_get_fqdn( NULL );
1109 /* create new SASL context */
1110 #if SASL_VERSION_MAJOR >= 2
1111 if ( conn->c_sock_name.bv_len != 0 &&
1112 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1115 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1116 /* Convert IPv6 addresses to address;port syntax. */
1117 p = strrchr( iplocalport, ' ' );
1118 /* Convert IPv4 addresses to address;port syntax. */
1119 if ( p == NULL ) p = strchr( iplocalport, ':' );
1124 if ( conn->c_peer_name.bv_len != 0 &&
1125 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1128 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1129 /* Convert IPv6 addresses to address;port syntax. */
1130 p = strrchr( ipremoteport, ' ' );
1131 /* Convert IPv4 addresses to address;port syntax. */
1132 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1137 sc = sasl_server_new( "ldap", global_host, global_realm,
1138 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1139 if ( iplocalport != NULL ) {
1140 ch_free( iplocalport );
1142 if ( ipremoteport != NULL ) {
1143 ch_free( ipremoteport );
1146 sc = sasl_server_new( "ldap", global_host, global_realm,
1147 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1150 if( sc != SASL_OK ) {
1151 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1157 conn->c_sasl_authctx = ctx;
1159 if( sc == SASL_OK ) {
1160 sc = sasl_setprop( ctx,
1161 SASL_SEC_PROPS, &sasl_secprops );
1163 if( sc != SASL_OK ) {
1164 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1167 slap_sasl_close( conn );
1172 sc = slap_sasl_err2ldap( sc );
1174 #elif defined(SLAP_BUILTIN_SASL)
1175 /* built-in SASL implementation */
1176 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1177 if( ctx == NULL ) return -1;
1179 ctx->sc_external_ssf = 0;
1180 BER_BVZERO( &ctx->sc_external_id );
1182 conn->c_sasl_authctx = ctx;
1188 int slap_sasl_external(
1191 struct berval *auth_id )
1193 #if SASL_VERSION_MAJOR >= 2
1195 sasl_conn_t *ctx = conn->c_sasl_authctx;
1197 if ( ctx == NULL ) {
1198 return LDAP_UNAVAILABLE;
1201 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1203 if ( sc != SASL_OK ) {
1207 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1208 auth_id ? auth_id->bv_val : NULL );
1210 if ( sc != SASL_OK ) {
1214 #elif defined(HAVE_CYRUS_SASL)
1216 sasl_conn_t *ctx = conn->c_sasl_authctx;
1217 sasl_external_properties_t extprops;
1219 if ( ctx == NULL ) {
1220 return LDAP_UNAVAILABLE;
1223 memset( &extprops, '\0', sizeof(extprops) );
1225 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1227 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1228 (void *) &extprops );
1230 if ( sc != SASL_OK ) {
1233 #elif defined(SLAP_BUILTIN_SASL)
1234 /* built-in SASL implementation */
1235 SASL_CTX *ctx = conn->c_sasl_authctx;
1236 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1238 ctx->sc_external_ssf = ssf;
1240 ctx->sc_external_id = *auth_id;
1241 BER_BVZERO( auth_id );
1243 BER_BVZERO( &ctx->sc_external_id );
1247 return LDAP_SUCCESS;
1250 int slap_sasl_reset( Connection *conn )
1252 return LDAP_SUCCESS;
1255 char ** slap_sasl_mechs( Connection *conn )
1257 char **mechs = NULL;
1259 #ifdef HAVE_CYRUS_SASL
1260 sasl_conn_t *ctx = conn->c_sasl_authctx;
1262 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1266 SASL_CONST char *mechstr;
1268 sc = sasl_listmech( ctx,
1269 NULL, NULL, ",", NULL,
1270 &mechstr, NULL, NULL );
1272 if( sc != SASL_OK ) {
1273 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1279 mechs = ldap_str2charray( mechstr, "," );
1281 #if SASL_VERSION_MAJOR < 2
1285 #elif defined(SLAP_BUILTIN_SASL)
1286 /* builtin SASL implementation */
1287 SASL_CTX *ctx = conn->c_sasl_authctx;
1288 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1289 /* should check ssf */
1290 mechs = ldap_str2charray( "EXTERNAL", "," );
1297 int slap_sasl_close( Connection *conn )
1299 #ifdef HAVE_CYRUS_SASL
1300 sasl_conn_t *ctx = conn->c_sasl_authctx;
1303 sasl_dispose( &ctx );
1305 if ( conn->c_sasl_sockctx &&
1306 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1308 ctx = conn->c_sasl_sockctx;
1309 sasl_dispose( &ctx );
1312 conn->c_sasl_authctx = NULL;
1313 conn->c_sasl_sockctx = NULL;
1314 conn->c_sasl_done = 0;
1316 free( conn->c_sasl_extra );
1317 conn->c_sasl_extra = NULL;
1319 #elif defined(SLAP_BUILTIN_SASL)
1320 SASL_CTX *ctx = conn->c_sasl_authctx;
1322 if( ctx->sc_external_id.bv_val ) {
1323 free( ctx->sc_external_id.bv_val );
1324 BER_BVZERO( &ctx->sc_external_id );
1327 conn->c_sasl_authctx = NULL;
1331 return LDAP_SUCCESS;
1334 int slap_sasl_bind( Operation *op, SlapReply *rs )
1336 #ifdef HAVE_CYRUS_SASL
1337 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1338 struct berval response;
1339 unsigned reslen = 0;
1342 Debug(LDAP_DEBUG_ARGS,
1343 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1344 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1345 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1346 op->o_conn->c_sasl_bind_mech.bv_val,
1347 op->orb_cred.bv_len );
1350 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1351 "SASL unavailable on this session" );
1355 #if SASL_VERSION_MAJOR >= 2
1356 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1357 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1358 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1359 sasl_server_step( ctx, cred, clen, resp, rlen )
1361 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1362 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1363 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1364 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1367 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1368 /* If we already authenticated once, must use a new context */
1369 if ( op->o_conn->c_sasl_done ) {
1371 const char *authid = NULL;
1372 #if SASL_VERSION_MAJOR >= 2
1373 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1374 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1375 if ( authid ) authid = ch_strdup( authid );
1377 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1378 sasl_dispose( &ctx );
1380 op->o_conn->c_sasl_authctx = NULL;
1382 slap_sasl_open( op->o_conn, 1 );
1383 ctx = op->o_conn->c_sasl_authctx;
1384 #if SASL_VERSION_MAJOR >= 2
1386 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1387 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1388 ch_free( (char *)authid );
1393 op->o_conn->c_sasl_bind_mech.bv_val,
1394 op->orb_cred.bv_val, op->orb_cred.bv_len,
1395 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1399 op->orb_cred.bv_val, op->orb_cred.bv_len,
1400 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1403 response.bv_len = reslen;
1405 if ( sc == SASL_OK ) {
1406 sasl_ssf_t *ssf = NULL;
1408 op->orb_edn = op->o_conn->c_sasl_dn;
1409 BER_BVZERO( &op->o_conn->c_sasl_dn );
1410 op->o_conn->c_sasl_done = 1;
1412 rs->sr_err = LDAP_SUCCESS;
1414 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1415 op->orb_ssf = ssf ? *ssf : 0;
1419 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1420 op->o_conn->c_sasl_layers++;
1422 /* If there's an old layer, set sockctx to NULL to
1423 * tell connection_read() to wait for us to finish.
1424 * Otherwise there is a race condition: we have to
1425 * send the Bind response using the old security
1426 * context and then remove it before reading any
1429 if ( op->o_conn->c_sasl_sockctx ) {
1430 ctx = op->o_conn->c_sasl_sockctx;
1431 op->o_conn->c_sasl_sockctx = NULL;
1433 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1435 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1438 /* Must send response using old security layer */
1439 if (response.bv_len) rs->sr_sasldata = &response;
1440 send_ldap_sasl( op, rs );
1442 /* Now dispose of the old security layer.
1445 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1446 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1447 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1448 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1449 sasl_dispose( &ctx );
1451 } else if ( sc == SASL_CONTINUE ) {
1452 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1453 #if SASL_VERSION_MAJOR >= 2
1454 rs->sr_text = sasl_errdetail( ctx );
1456 rs->sr_sasldata = &response;
1457 send_ldap_sasl( op, rs );
1460 #if SASL_VERSION_MAJOR >= 2
1461 rs->sr_text = sasl_errdetail( ctx );
1463 rs->sr_err = slap_sasl_err2ldap( sc ),
1464 send_ldap_result( op, rs );
1467 #if SASL_VERSION_MAJOR < 2
1468 if( response.bv_len ) ch_free( response.bv_val );
1471 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1473 #elif defined(SLAP_BUILTIN_SASL)
1474 /* built-in SASL implementation */
1475 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1477 if ( ctx == NULL ) {
1478 send_ldap_error( op, rs, LDAP_OTHER,
1479 "Internal SASL Error" );
1481 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1484 if( op->orb_cred.bv_len ) {
1485 rs->sr_text = "proxy authorization not support";
1486 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1487 send_ldap_result( op, rs );
1490 op->orb_edn = ctx->sc_external_id;
1491 rs->sr_err = LDAP_SUCCESS;
1492 rs->sr_sasldata = NULL;
1493 send_ldap_sasl( op, rs );
1497 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1498 "requested SASL mechanism not supported" );
1501 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1502 "SASL not supported" );
1508 char* slap_sasl_secprops( const char *in )
1510 #ifdef HAVE_CYRUS_SASL
1511 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1513 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1515 return "SASL not supported";
1519 void slap_sasl_secprops_unparse( struct berval *bv )
1521 #ifdef HAVE_CYRUS_SASL
1522 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1526 #ifdef HAVE_CYRUS_SASL
1528 slap_sasl_setpass( Operation *op, SlapReply *rs )
1530 struct berval id = BER_BVNULL; /* needs to come from connection */
1531 struct berval new = BER_BVNULL;
1532 struct berval old = BER_BVNULL;
1534 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1536 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1537 (SASL_CONST void **)(char *)&id.bv_val );
1539 if( rs->sr_err != SASL_OK ) {
1540 rs->sr_text = "unable to retrieve SASL username";
1541 rs->sr_err = LDAP_OTHER;
1545 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1546 id.bv_val ? id.bv_val : "", 0, 0 );
1548 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1549 NULL, &old, &new, &rs->sr_text );
1551 if( rs->sr_err != LDAP_SUCCESS ) {
1555 if( new.bv_len == 0 ) {
1556 slap_passwd_generate(&new);
1558 if( new.bv_len == 0 ) {
1559 rs->sr_text = "password generation failed.";
1560 rs->sr_err = LDAP_OTHER;
1564 rs->sr_rspdata = slap_passwd_return( &new );
1567 #if SASL_VERSION_MAJOR < 2
1568 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1569 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1571 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1572 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1573 if( rs->sr_err != SASL_OK ) {
1574 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1577 switch(rs->sr_err) {
1579 rs->sr_err = LDAP_SUCCESS;
1589 rs->sr_err = LDAP_OTHER;
1595 #endif /* HAVE_CYRUS_SASL */
1597 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1598 * string returned in *dn is in its own allocated memory, and must be free'd
1599 * by the calling process. -Mark Adamson, Carnegie Mellon
1601 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1602 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1609 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1610 char *user_realm, struct berval *dn, int flags )
1612 int rc, is_dn = SET_NONE, do_norm = 1;
1613 struct berval dn2, *mech;
1615 assert( conn != NULL );
1616 assert( id != NULL );
1618 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1620 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1621 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1622 (unsigned long) id->bv_len ) );
1625 op = conn->c_sasl_bindop;
1627 assert( op != NULL );
1631 if ( !BER_BVISNULL( id ) ) {
1632 /* Blatantly anonymous ID */
1633 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1635 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1636 return( LDAP_SUCCESS );
1640 /* FIXME: if empty, should we stop? */
1641 BER_BVSTR( id, "" );
1644 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1645 mech = &conn->c_sasl_bind_mech;
1647 mech = &conn->c_authmech;
1650 /* An authcID needs to be converted to authzID form. Set the
1651 * values directly into *dn; they will be normalized later. (and
1652 * normalizing always makes a new copy.) An ID from a TLS certificate
1653 * is already normalized, so copy it and skip normalization.
1655 if( flags & SLAP_GETDN_AUTHCID ) {
1656 if( bvmatch( mech, &ext_bv )) {
1657 /* EXTERNAL DNs are already normalized */
1658 assert( !BER_BVISNULL( id ) );
1662 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1665 /* convert to u:<username> form */
1671 if( is_dn == SET_NONE ) {
1672 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1674 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1675 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1677 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1679 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1680 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1684 /* No other possibilities from here */
1685 if( is_dn == SET_NONE ) {
1687 return( LDAP_INAPPROPRIATE_AUTH );
1690 /* Username strings */
1691 if( is_dn == SET_U ) {
1692 /* ITS#3419: values may need escape */
1694 LDAPAVA *RDNs[ 4 ][ 2 ];
1699 DN[ irdn ] = RDNs[ irdn ];
1700 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1701 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1702 AVAs[ irdn ].la_value = *dn;
1703 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1704 AVAs[ irdn ].la_private = NULL;
1705 RDNs[ irdn ][ 1 ] = NULL;
1707 if ( user_realm && *user_realm ) {
1709 DN[ irdn ] = RDNs[ irdn ];
1710 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1711 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1712 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1713 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1714 AVAs[ irdn ].la_private = NULL;
1715 RDNs[ irdn ][ 1 ] = NULL;
1718 if ( !BER_BVISNULL( mech ) ) {
1720 DN[ irdn ] = RDNs[ irdn ];
1721 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1722 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1723 AVAs[ irdn ].la_value = *mech;
1724 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1725 AVAs[ irdn ].la_private = NULL;
1726 RDNs[ irdn ][ 1 ] = NULL;
1730 DN[ irdn ] = RDNs[ irdn ];
1731 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1732 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1733 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1734 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1735 AVAs[ irdn ].la_private = NULL;
1736 RDNs[ irdn ][ 1 ] = NULL;
1741 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1743 if ( rc != LDAP_SUCCESS ) {
1748 Debug( LDAP_DEBUG_TRACE,
1749 "slap_sasl_getdn: u:id converted to %s\n",
1754 /* Dup the DN in any case, so we don't risk
1755 * leaks or dangling pointers later,
1756 * and the DN value is '\0' terminated */
1757 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1758 dn->bv_val = dn2.bv_val;
1761 /* All strings are in DN form now. Normalize if needed. */
1763 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1765 /* User DNs were constructed above and must be freed now */
1766 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1768 if ( rc != LDAP_SUCCESS ) {
1775 /* Run thru regexp */
1776 slap_sasl2dn( op, dn, &dn2, flags );
1777 if( !BER_BVISNULL( &dn2 ) ) {
1778 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1780 Debug( LDAP_DEBUG_TRACE,
1781 "slap_sasl_getdn: dn:id converted to %s\n",
1785 return( LDAP_SUCCESS );