2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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=*)");
156 #define 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 slap_callback *tmp = op->o_callback;
177 lookup_info *sl = tmp->sc_private;
179 if (rs->sr_type != REP_SEARCH) return 0;
181 for( i = 0; sl->list[i].name; i++ ) {
182 const char *name = sl->list[i].name;
184 if ( name[0] == '*' ) {
185 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
186 /* Skip our private properties */
187 if ( !strcmp( name, slap_propnames[0] )) {
192 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
195 if ( sl->list[i].values ) {
196 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
199 rc = slap_str2ad( name, &ad, &text );
200 if ( rc != LDAP_SUCCESS ) {
201 Debug( LDAP_DEBUG_TRACE,
202 "slap_ap_lookup: str2ad(%s): %s\n", name, text, 0 );
206 /* If it's the rootdn and a rootpw was present, we already set
207 * it so don't override it here.
209 if ( ad == slap_schema.si_ad_userPassword && sl->list[i].values &&
210 be_isroot_dn( op->o_bd, &op->o_req_ndn ))
213 a = attr_find( rs->sr_entry->e_attrs, ad );
215 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
218 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
219 sl->sparams->utils->prop_erase( sl->sparams->propctx,
222 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
223 /* ITS#3846 don't give hashed passwords to SASL */
224 if ( ad == slap_schema.si_ad_userPassword &&
225 bv->bv_val[0] == '{' ) {
226 rc = lutil_passwd_scheme( bv->bv_val );
228 /* If it's not a recognized scheme, just assume it's
229 * a cleartext password that happened to include brackets.
231 * If it's a recognized scheme, skip this value, unless the
232 * scheme is {CLEARTEXT}. In that case, skip over the
233 * scheme name and use the remainder. If there is nothing
234 * past the scheme name, skip this value.
236 #ifdef SLAPD_CLEARTEXT
237 if ( !strncasecmp( bv->bv_val, sc_cleartext.bv_val,
238 sc_cleartext.bv_len )) {
240 cbv.bv_len = bv->bv_len - sc_cleartext.bv_len;
242 cbv.bv_val = bv->bv_val + sc_cleartext.bv_len;
243 sl->sparams->utils->prop_set( sl->sparams->propctx,
244 sl->list[i].name, cbv.bv_val, cbv.bv_len );
251 sl->sparams->utils->prop_set( sl->sparams->propctx,
252 sl->list[i].name, bv->bv_val, bv->bv_len );
261 sasl_server_params_t *sparams,
268 Connection *conn = NULL;
271 sl.list = sparams->utils->prop_get( sparams->propctx );
272 sl.sparams = sparams;
275 /* Find our DN and conn first */
276 for( i = 0; sl.list[i].name; i++ ) {
277 if ( sl.list[i].name[0] == '*' ) {
278 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
279 if ( sl.list[i].values && sl.list[i].values[0] )
280 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
283 if ( (flags & SASL_AUXPROP_AUTHZID) &&
284 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
286 if ( sl.list[i].values && sl.list[i].values[0] )
287 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
290 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
291 if ( sl.list[i].values && sl.list[i].values[0] ) {
292 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
293 if ( !(flags & SASL_AUXPROP_AUTHZID) )
300 /* Now see what else needs to be fetched */
301 for( i = 0; sl.list[i].name; i++ ) {
302 const char *name = sl.list[i].name;
304 if ( name[0] == '*' ) {
305 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
306 /* Skip our private properties */
307 if ( !strcmp( name, slap_propnames[0] )) {
312 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
315 if ( sl.list[i].values ) {
316 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
323 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
327 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
330 /* For rootdn, see if we can use the rootpw */
331 if ( be_isroot_dn( op.o_bd, &op.o_req_ndn ) &&
332 !BER_BVISEMPTY( &op.o_bd->be_rootpw )) {
333 struct berval cbv = BER_BVNULL;
335 /* If there's a recognized scheme, see if it's CLEARTEXT */
336 if ( lutil_passwd_scheme( op.o_bd->be_rootpw.bv_val )) {
337 if ( !strncasecmp( op.o_bd->be_rootpw.bv_val,
338 sc_cleartext.bv_val, sc_cleartext.bv_len )) {
340 /* If it's CLEARTEXT, skip past scheme spec */
341 cbv.bv_len = op.o_bd->be_rootpw.bv_len -
344 cbv.bv_val = op.o_bd->be_rootpw.bv_val +
348 /* No scheme, use the whole value */
350 cbv = op.o_bd->be_rootpw;
352 if ( !BER_BVISEMPTY( &cbv )) {
353 for( i = 0; sl.list[i].name; i++ ) {
354 const char *name = sl.list[i].name;
356 if ( name[0] == '*' ) {
357 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
359 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
362 if ( !strcasecmp(name,"userPassword") ) {
363 sl.sparams->utils->prop_set( sl.sparams->propctx,
364 sl.list[i].name, cbv.bv_val, cbv.bv_len );
371 if ( op.o_bd->be_search ) {
372 SlapReply rs = {REP_RESULT};
373 op.o_hdr = conn->c_sasl_bindop->o_hdr;
374 op.o_tag = LDAP_REQ_SEARCH;
375 op.o_ndn = conn->c_ndn;
377 slap_op_time( &op.o_time, &op.o_tincr );
378 op.o_do_not_cache = 1;
379 op.o_is_auth_check = 1;
380 op.o_req_dn = op.o_req_ndn;
381 op.ors_scope = LDAP_SCOPE_BASE;
382 op.ors_deref = LDAP_DEREF_NEVER;
383 op.ors_tlimit = SLAP_NO_LIMIT;
385 op.ors_filter = &generic_filter;
386 op.ors_filterstr = generic_filterstr;
387 /* FIXME: we want all attributes, right? */
390 op.o_bd->be_search( &op, &rs );
396 #if SASL_VERSION_FULL >= 0x020110
400 sasl_server_params_t *sparams,
401 struct propctx *prctx,
406 SlapReply rs = {REP_RESULT};
408 Connection *conn = NULL;
409 const struct propval *pr;
410 Modifications *modlist = NULL, **modtail = &modlist, *mod;
411 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
412 char textbuf[SLAP_TEXT_BUFLEN];
414 size_t textlen = sizeof(textbuf);
416 /* just checking if we are enabled */
417 if (!prctx) return SASL_OK;
419 if (!sparams || !user) return SASL_BADPARAM;
421 pr = sparams->utils->prop_get( sparams->propctx );
423 /* Find our DN and conn first */
424 for( i = 0; pr[i].name; i++ ) {
425 if ( pr[i].name[0] == '*' ) {
426 if ( !strcmp( pr[i].name, slap_propnames[PROP_CONN] ) ) {
427 if ( pr[i].values && pr[i].values[0] )
428 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
431 if ( !strcmp( pr[i].name, slap_propnames[PROP_AUTHC] ) ) {
432 if ( pr[i].values && pr[i].values[0] ) {
433 AC_MEMCPY( &op.o_req_ndn, pr[i].values[0], sizeof( struct berval ) );
438 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
440 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
442 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
444 pr = sparams->utils->prop_get( prctx );
445 if (!pr) return SASL_BADPARAM;
447 for (i=0; pr[i].name; i++);
448 if (!i) return SASL_BADPARAM;
450 for (i=0; pr[i].name; i++) {
451 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
452 mod->sml_op = LDAP_MOD_REPLACE;
454 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
455 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
456 sizeof(struct berval));
457 for (j=0; j<pr[i].nvalues; j++) {
458 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
460 BER_BVZERO( &mod->sml_values[j] );
461 mod->sml_nvalues = NULL;
462 mod->sml_desc = NULL;
464 modtail = &mod->sml_next;
468 rc = slap_mods_check( modlist, &text, textbuf, textlen, NULL );
470 if ( rc == LDAP_SUCCESS ) {
471 rc = slap_mods_no_user_mod_check( &op, modlist,
472 &text, textbuf, textlen );
474 if ( rc == LDAP_SUCCESS ) {
475 op.o_hdr = conn->c_sasl_bindop->o_hdr;
476 op.o_tag = LDAP_REQ_MODIFY;
477 op.o_ndn = op.o_req_ndn;
479 slap_op_time( &op.o_time, &op.o_tincr );
480 op.o_do_not_cache = 1;
481 op.o_is_auth_check = 1;
482 op.o_req_dn = op.o_req_ndn;
483 op.orm_modlist = modlist;
485 rc = op.o_bd->be_modify( &op, &rs );
488 slap_mods_free( modlist, 1 );
489 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
491 #endif /* SASL_VERSION_FULL >= 2.1.16 */
493 static sasl_auxprop_plug_t slap_auxprop_plugin = {
496 NULL, /* glob_context */
497 NULL, /* auxprop_free */
500 #if SASL_VERSION_FULL >= 0x020110
501 slap_auxprop_store /* the declaration of this member changed
502 * in cyrus SASL from 2.1.15 to 2.1.16 */
510 const sasl_utils_t *utils,
513 sasl_auxprop_plug_t **plug,
514 const char *plugname)
516 if ( !out_version || !plug ) return SASL_BADPARAM;
518 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
520 *out_version = SASL_AUXPROP_PLUG_VERSION;
521 *plug = &slap_auxprop_plugin;
525 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
526 * auxiliary property, so that we can refer to it in sasl_authorize
527 * without interfering with anything else. Also, the SASL username
528 * buffer is constrained to 256 characters, and our DNs could be
529 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
532 slap_sasl_canonicalize(
538 const char *user_realm,
543 Connection *conn = (Connection *)context;
544 struct propctx *props = sasl_auxprop_getctx( sconn );
545 struct propval auxvals[3];
548 const char *names[2];
553 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
554 conn ? conn->c_connid : -1,
555 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
556 in ? in : "<empty>");
558 /* If name is too big, just truncate. We don't care, we're
559 * using DNs, not the usernames.
561 if ( inlen > out_max )
564 /* This is a Simple Bind using SPASSWD. That means the in-directory
565 * userPassword of the Binding user already points at SASL, so it
566 * cannot be used to actually satisfy a password comparison. Just
567 * ignore it, some other mech will process it.
569 if ( !conn->c_sasl_bindop ||
570 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
572 /* See if we need to add request, can only do it once */
573 prop_getnames( props, slap_propnames, auxvals );
574 if ( !auxvals[0].name )
575 prop_request( props, slap_propnames );
577 if ( flags & SASL_CU_AUTHID )
582 /* Need to store the Connection for auxprop_lookup */
583 if ( !auxvals[PROP_CONN].values ) {
584 names[0] = slap_propnames[PROP_CONN];
586 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
589 /* Already been here? */
590 if ( auxvals[which].values )
593 /* Normally we require an authzID to have a u: or dn: prefix.
594 * However, SASL frequently gives us an authzID that is just
595 * an exact copy of the authcID, without a prefix. We need to
596 * detect and allow this condition. If SASL calls canonicalize
597 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
598 * But if it's broken into two calls, we need to remember the
599 * authcID so that we can compare the authzID later. We store
600 * the authcID temporarily in conn->c_sasl_dn. We necessarily
601 * finish Canonicalizing before Authorizing, so there is no
602 * conflict with slap_sasl_authorize's use of this temp var.
604 * The SASL EXTERNAL mech is backwards from all the other mechs,
605 * it does authzID before the authcID. If we see that authzID
606 * has already been done, don't do anything special with authcID.
608 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
609 conn->c_sasl_dn.bv_val = (char *) in;
610 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
611 rc = strcmp( in, conn->c_sasl_dn.bv_val );
612 conn->c_sasl_dn.bv_val = NULL;
613 /* They were equal, no work needed */
614 if ( !rc ) goto done;
617 bvin.bv_val = (char *)in;
619 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
620 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
621 if ( rc != LDAP_SUCCESS ) {
622 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
626 names[0] = slap_propnames[which];
629 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
631 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
632 conn ? conn->c_connid : -1, names[0]+1,
633 dn.bv_val ? dn.bv_val : "<EMPTY>" );
636 AC_MEMCPY( out, in, inlen );
648 char *requested_user,
652 const char *def_realm,
654 struct propctx *props)
656 Connection *conn = (Connection *)context;
657 struct propval auxvals[3];
658 struct berval authcDN, authzDN = BER_BVNULL;
661 /* Simple Binds don't support proxy authorization, ignore it */
662 if ( !conn->c_sasl_bindop ||
663 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
665 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
666 "authcid=\"%s\" authzid=\"%s\"\n",
667 conn ? conn->c_connid : -1, auth_identity, requested_user );
668 if ( conn->c_sasl_dn.bv_val ) {
669 ch_free( conn->c_sasl_dn.bv_val );
670 BER_BVZERO( &conn->c_sasl_dn );
674 prop_getnames( props, slap_propnames+1, auxvals );
676 /* Should not happen */
677 if ( !auxvals[0].values ) {
678 sasl_seterror( sconn, 0, "invalid authcid" );
682 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
683 conn->c_sasl_dn = authcDN;
685 /* Nothing to do if no authzID was given */
686 if ( !auxvals[1].name || !auxvals[1].values ) {
690 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
692 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
693 if ( rc != LDAP_SUCCESS ) {
694 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
695 "proxy authorization disallowed (%d)\n",
696 (long) (conn ? conn->c_connid : -1), rc, 0 );
698 sasl_seterror( sconn, 0, "not authorized" );
699 ch_free( authzDN.bv_val );
703 /* FIXME: we need yet another dup because slap_sasl_getdn()
704 * is using the bind operation slab */
705 if ( conn->c_sasl_bindop ) {
706 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
707 slap_sl_free( authzDN.bv_val,
708 conn->c_sasl_bindop->o_tmpmemctx );
711 conn->c_sasl_authz_dn = authzDN;
715 if (conn->c_sasl_bindop) {
716 Statslog( LDAP_DEBUG_STATS,
717 "conn=%lu op=%lu BIND authcid=\"%s\" authzid=\"%s\"\n",
718 conn->c_connid, conn->c_sasl_bindop->o_opid,
719 auth_identity, requested_user, 0);
722 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
723 " proxy authorization allowed authzDN=\"%s\"\n",
724 (long) (conn ? conn->c_connid : -1),
725 authzDN.bv_val ? authzDN.bv_val : "", 0 );
737 struct berval authcDN, authzDN = BER_BVNULL;
739 Connection *conn = context;
741 struct berval bvauthcid, bvauthzid;
744 if ( conn->c_sasl_dn.bv_val ) {
745 ch_free( conn->c_sasl_dn.bv_val );
746 BER_BVZERO( &conn->c_sasl_dn );
749 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
750 "authcid=\"%s\" authzid=\"%s\"\n",
751 (long) (conn ? conn->c_connid : -1),
752 authcid ? authcid : "<empty>",
753 authzid ? authzid : "<empty>" );
755 /* Figure out how much data we have for the dn */
756 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
757 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
758 Debug(LDAP_DEBUG_TRACE,
759 "authorize: getprop(REALM) failed!\n", 0,0,0);
760 *errstr = "Could not extract realm";
764 /* Convert the identities to DN's. If no authzid was given, client will
765 be bound as the DN matching their username */
766 bvauthcid.bv_val = authcid;
767 bvauthcid.bv_len = authcid ? strlen( authcid ) : 0;
768 rc = slap_sasl_getdn( conn, NULL, &bvauthcid, realm,
769 &authcDN, SLAP_GETDN_AUTHCID );
770 if( rc != LDAP_SUCCESS ) {
771 *errstr = ldap_err2string( rc );
774 conn->c_sasl_dn = authcDN;
775 if( ( authzid == NULL ) || !strcmp( authcid, authzid ) ) {
776 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
777 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
782 bvauthzid.bv_val = authzid;
783 bvauthzid.bv_len = authzid ? strlen( authzid ) : 0;
784 rc = slap_sasl_getdn( conn, NULL, &bvauthzid, realm,
785 &authzDN, SLAP_GETDN_AUTHZID );
786 if( rc != LDAP_SUCCESS ) {
787 *errstr = ldap_err2string( rc );
791 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
793 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
794 "proxy authorization disallowed (%d)\n",
795 (long) (conn ? conn->c_connid : -1), rc, 0 );
797 *errstr = "not authorized";
798 ch_free( authzDN.bv_val );
802 /* FIXME: we need yet another dup because slap_sasl_getdn()
803 * is using the bind operation slab */
804 if ( conn->c_sasl_bindop ) {
805 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
806 slap_sl_free( authzDN.bv_val,
807 conn->c_sasl_bindop->o_tmpmemctx );
810 conn->c_sasl_authz_dn = authzDN;
814 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
815 " authorization allowed authzDN=\"%s\"\n",
816 (long) (conn ? conn->c_connid : -1),
817 authzDN.bv_val ? authzDN.bv_val : "", 0 );
819 if ( conn->c_sasl_bindop ) {
820 Statslog( LDAP_DEBUG_STATS,
821 "conn=%lu op=%lu BIND authcid=\"%s\" authzid=\"%s\"\n",
822 conn->c_connid, conn->c_sasl_bindop->o_opid,
823 authcid, authzid ? authzid : "", 0);
829 #endif /* SASL_VERSION_MAJOR >= 2 */
832 slap_sasl_err2ldap( int saslerr )
841 rc = LDAP_SASL_BIND_IN_PROGRESS;
850 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
853 rc = LDAP_INVALID_CREDENTIALS;
856 rc = LDAP_INSUFFICIENT_ACCESS;
860 rc = LDAP_INAPPROPRIATE_AUTH;
872 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
875 const struct berval *sc,
876 const struct berval * passwd,
877 const struct berval * cred,
882 void *ctx, *sconn = NULL;
884 for( i=0; i<cred->bv_len; i++) {
885 if(cred->bv_val[i] == '\0') {
886 return LUTIL_PASSWD_ERR; /* NUL character in password */
890 if( cred->bv_val[i] != '\0' ) {
891 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
894 for( i=0; i<passwd->bv_len; i++) {
895 if(passwd->bv_val[i] == '\0') {
896 return LUTIL_PASSWD_ERR; /* NUL character in password */
900 if( passwd->bv_val[i] != '\0' ) {
901 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
904 rtn = LUTIL_PASSWD_ERR;
906 ctx = ldap_pvt_thread_pool_context();
907 ldap_pvt_thread_pool_getkey( ctx, slap_sasl_bind, &sconn, NULL );
909 if( sconn != NULL ) {
911 # if SASL_VERSION_MAJOR < 2
912 sc = sasl_checkpass( sconn,
913 passwd->bv_val, passwd->bv_len,
914 cred->bv_val, cred->bv_len,
917 sc = sasl_checkpass( sconn,
918 passwd->bv_val, passwd->bv_len,
919 cred->bv_val, cred->bv_len );
921 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
926 #endif /* SLAPD_SPASSWD */
928 #endif /* HAVE_CYRUS_SASL */
930 int slap_sasl_init( void )
932 #ifdef HAVE_CYRUS_SASL
934 static sasl_callback_t server_callbacks[] = {
935 { SASL_CB_LOG, &slap_sasl_log, NULL },
936 { SASL_CB_LIST_END, NULL, NULL }
939 #ifdef HAVE_SASL_VERSION
940 /* stringify the version number, sasl.h doesn't do it for us */
941 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
942 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
943 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
946 sasl_version( NULL, &rc );
947 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
948 (rc & 0xffff) < SASL_VERSION_STEP)
950 char version[sizeof("xxx.xxx.xxxxx")];
951 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
953 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
954 " expected " SASL_VERSION_STRING ","
955 " got %s\n", version, 0, 0 );
960 /* SASL 2 does its own memory management internally */
961 #if SASL_VERSION_MAJOR < 2
970 ldap_pvt_sasl_mutex_new,
971 ldap_pvt_sasl_mutex_lock,
972 ldap_pvt_sasl_mutex_unlock,
973 ldap_pvt_sasl_mutex_dispose );
975 #if SASL_VERSION_MAJOR >= 2
976 generic_filter.f_desc = slap_schema.si_ad_objectClass;
978 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
979 if( rc != SASL_OK ) {
980 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
985 /* should provide callbacks for logging */
986 /* server name should be configurable */
987 rc = sasl_server_init( server_callbacks, "slapd" );
989 if( rc != SASL_OK ) {
990 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
992 #if SASL_VERSION_MAJOR < 2
993 /* A no-op used to make sure we linked with Cyrus 1.5 */
994 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1000 #ifdef SLAPD_SPASSWD
1001 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
1004 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1007 /* default security properties */
1008 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1009 sasl_secprops.max_ssf = INT_MAX;
1010 sasl_secprops.maxbufsize = 65536;
1011 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1017 int slap_sasl_destroy( void )
1019 #ifdef HAVE_CYRUS_SASL
1022 free( global_host );
1028 int slap_sasl_open( Connection *conn, int reopen )
1030 int sc = LDAP_SUCCESS;
1031 #ifdef HAVE_CYRUS_SASL
1034 sasl_conn_t *ctx = NULL;
1035 sasl_callback_t *session_callbacks;
1037 #if SASL_VERSION_MAJOR >= 2
1038 char *ipremoteport = NULL, *iplocalport = NULL;
1041 assert( conn->c_sasl_authctx == NULL );
1044 assert( conn->c_sasl_extra == NULL );
1047 #if SASL_VERSION_MAJOR >= 2
1048 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1050 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1052 if( session_callbacks == NULL ) {
1053 Debug( LDAP_DEBUG_ANY,
1054 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1057 conn->c_sasl_extra = session_callbacks;
1059 session_callbacks[cb=0].id = SASL_CB_LOG;
1060 session_callbacks[cb].proc = &slap_sasl_log;
1061 session_callbacks[cb++].context = conn;
1063 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1064 session_callbacks[cb].proc = &slap_sasl_authorize;
1065 session_callbacks[cb++].context = conn;
1067 #if SASL_VERSION_MAJOR >= 2
1068 session_callbacks[cb].id = SASL_CB_CANON_USER;
1069 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1070 session_callbacks[cb++].context = conn;
1073 session_callbacks[cb].id = SASL_CB_LIST_END;
1074 session_callbacks[cb].proc = NULL;
1075 session_callbacks[cb++].context = NULL;
1077 session_callbacks = conn->c_sasl_extra;
1080 conn->c_sasl_layers = 0;
1082 if( global_host == NULL ) {
1083 global_host = ldap_pvt_get_fqdn( NULL );
1086 /* create new SASL context */
1087 #if SASL_VERSION_MAJOR >= 2
1088 if ( conn->c_sock_name.bv_len != 0 &&
1089 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1092 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1093 /* Convert IPv6 addresses to address;port syntax. */
1094 p = strrchr( iplocalport, ' ' );
1095 /* Convert IPv4 addresses to address;port syntax. */
1096 if ( p == NULL ) p = strchr( iplocalport, ':' );
1101 if ( conn->c_peer_name.bv_len != 0 &&
1102 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1105 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1106 /* Convert IPv6 addresses to address;port syntax. */
1107 p = strrchr( ipremoteport, ' ' );
1108 /* Convert IPv4 addresses to address;port syntax. */
1109 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1114 sc = sasl_server_new( "ldap", global_host, global_realm,
1115 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1116 if ( iplocalport != NULL ) {
1117 ch_free( iplocalport );
1119 if ( ipremoteport != NULL ) {
1120 ch_free( ipremoteport );
1123 sc = sasl_server_new( "ldap", global_host, global_realm,
1124 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1127 if( sc != SASL_OK ) {
1128 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1134 conn->c_sasl_authctx = ctx;
1136 if( sc == SASL_OK ) {
1137 sc = sasl_setprop( ctx,
1138 SASL_SEC_PROPS, &sasl_secprops );
1140 if( sc != SASL_OK ) {
1141 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1144 slap_sasl_close( conn );
1149 sc = slap_sasl_err2ldap( sc );
1151 #elif defined(SLAP_BUILTIN_SASL)
1152 /* built-in SASL implementation */
1153 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1154 if( ctx == NULL ) return -1;
1156 ctx->sc_external_ssf = 0;
1157 BER_BVZERO( &ctx->sc_external_id );
1159 conn->c_sasl_authctx = ctx;
1165 int slap_sasl_external(
1168 struct berval *auth_id )
1170 #if SASL_VERSION_MAJOR >= 2
1172 sasl_conn_t *ctx = conn->c_sasl_authctx;
1174 if ( ctx == NULL ) {
1175 return LDAP_UNAVAILABLE;
1178 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1180 if ( sc != SASL_OK ) {
1184 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1185 auth_id ? auth_id->bv_val : NULL );
1187 if ( sc != SASL_OK ) {
1191 #elif defined(HAVE_CYRUS_SASL)
1193 sasl_conn_t *ctx = conn->c_sasl_authctx;
1194 sasl_external_properties_t extprops;
1196 if ( ctx == NULL ) {
1197 return LDAP_UNAVAILABLE;
1200 memset( &extprops, '\0', sizeof(extprops) );
1202 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1204 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1205 (void *) &extprops );
1207 if ( sc != SASL_OK ) {
1210 #elif defined(SLAP_BUILTIN_SASL)
1211 /* built-in SASL implementation */
1212 SASL_CTX *ctx = conn->c_sasl_authctx;
1213 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1215 ctx->sc_external_ssf = ssf;
1217 ctx->sc_external_id = *auth_id;
1218 BER_BVZERO( auth_id );
1220 BER_BVZERO( &ctx->sc_external_id );
1224 return LDAP_SUCCESS;
1227 int slap_sasl_reset( Connection *conn )
1229 return LDAP_SUCCESS;
1232 char ** slap_sasl_mechs( Connection *conn )
1234 char **mechs = NULL;
1236 #ifdef HAVE_CYRUS_SASL
1237 sasl_conn_t *ctx = conn->c_sasl_authctx;
1239 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1243 SASL_CONST char *mechstr;
1245 sc = sasl_listmech( ctx,
1246 NULL, NULL, ",", NULL,
1247 &mechstr, NULL, NULL );
1249 if( sc != SASL_OK ) {
1250 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1256 mechs = ldap_str2charray( mechstr, "," );
1258 #if SASL_VERSION_MAJOR < 2
1262 #elif defined(SLAP_BUILTIN_SASL)
1263 /* builtin SASL implementation */
1264 SASL_CTX *ctx = conn->c_sasl_authctx;
1265 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1266 /* should check ssf */
1267 mechs = ldap_str2charray( "EXTERNAL", "," );
1274 int slap_sasl_close( Connection *conn )
1276 #ifdef HAVE_CYRUS_SASL
1277 sasl_conn_t *ctx = conn->c_sasl_authctx;
1280 sasl_dispose( &ctx );
1282 if ( conn->c_sasl_sockctx &&
1283 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1285 ctx = conn->c_sasl_sockctx;
1286 sasl_dispose( &ctx );
1289 conn->c_sasl_authctx = NULL;
1290 conn->c_sasl_sockctx = NULL;
1291 conn->c_sasl_done = 0;
1293 free( conn->c_sasl_extra );
1294 conn->c_sasl_extra = NULL;
1296 #elif defined(SLAP_BUILTIN_SASL)
1297 SASL_CTX *ctx = conn->c_sasl_authctx;
1299 if( ctx->sc_external_id.bv_val ) {
1300 free( ctx->sc_external_id.bv_val );
1301 BER_BVZERO( &ctx->sc_external_id );
1304 conn->c_sasl_authctx = NULL;
1308 return LDAP_SUCCESS;
1311 int slap_sasl_bind( Operation *op, SlapReply *rs )
1313 #ifdef HAVE_CYRUS_SASL
1314 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1315 struct berval response;
1316 unsigned reslen = 0;
1319 Debug(LDAP_DEBUG_ARGS,
1320 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1321 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1322 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1323 op->o_conn->c_sasl_bind_mech.bv_val,
1324 op->orb_cred.bv_len );
1327 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1328 "SASL unavailable on this session" );
1332 #if SASL_VERSION_MAJOR >= 2
1333 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1334 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1335 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1336 sasl_server_step( ctx, cred, clen, resp, rlen )
1338 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1339 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1340 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1341 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1344 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1345 /* If we already authenticated once, must use a new context */
1346 if ( op->o_conn->c_sasl_done ) {
1348 const char *authid = NULL;
1349 #if SASL_VERSION_MAJOR >= 2
1350 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1351 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1352 if ( authid ) authid = ch_strdup( authid );
1354 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1355 sasl_dispose( &ctx );
1357 op->o_conn->c_sasl_authctx = NULL;
1359 slap_sasl_open( op->o_conn, 1 );
1360 ctx = op->o_conn->c_sasl_authctx;
1361 #if SASL_VERSION_MAJOR >= 2
1363 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1364 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1365 ch_free( (char *)authid );
1370 op->o_conn->c_sasl_bind_mech.bv_val,
1371 op->orb_cred.bv_val, op->orb_cred.bv_len,
1372 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1376 op->orb_cred.bv_val, op->orb_cred.bv_len,
1377 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1380 response.bv_len = reslen;
1382 if ( sc == SASL_OK ) {
1383 sasl_ssf_t *ssf = NULL;
1385 op->orb_edn = op->o_conn->c_sasl_dn;
1386 BER_BVZERO( &op->o_conn->c_sasl_dn );
1387 op->o_conn->c_sasl_done = 1;
1389 rs->sr_err = LDAP_SUCCESS;
1391 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1392 op->orb_ssf = ssf ? *ssf : 0;
1396 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1397 op->o_conn->c_sasl_layers++;
1399 /* If there's an old layer, set sockctx to NULL to
1400 * tell connection_read() to wait for us to finish.
1401 * Otherwise there is a race condition: we have to
1402 * send the Bind response using the old security
1403 * context and then remove it before reading any
1406 if ( op->o_conn->c_sasl_sockctx ) {
1407 ctx = op->o_conn->c_sasl_sockctx;
1408 op->o_conn->c_sasl_sockctx = NULL;
1410 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1412 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1415 /* Must send response using old security layer */
1416 if (response.bv_len) rs->sr_sasldata = &response;
1417 send_ldap_sasl( op, rs );
1419 /* Now dispose of the old security layer.
1422 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1423 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1424 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1425 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1426 sasl_dispose( &ctx );
1428 } else if ( sc == SASL_CONTINUE ) {
1429 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1430 rs->sr_sasldata = &response;
1431 send_ldap_sasl( op, rs );
1434 #if SASL_VERSION_MAJOR >= 2
1435 rs->sr_text = sasl_errdetail( ctx );
1437 rs->sr_err = slap_sasl_err2ldap( sc ),
1438 send_ldap_result( op, rs );
1441 #if SASL_VERSION_MAJOR < 2
1442 if( response.bv_len ) {
1443 ch_free( response.bv_val );
1447 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1449 #elif defined(SLAP_BUILTIN_SASL)
1450 /* built-in SASL implementation */
1451 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1453 if ( ctx == NULL ) {
1454 send_ldap_error( op, rs, LDAP_OTHER,
1455 "Internal SASL Error" );
1457 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1460 if( op->orb_cred.bv_len ) {
1461 rs->sr_text = "proxy authorization not support";
1462 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1463 send_ldap_result( op, rs );
1466 op->orb_edn = ctx->sc_external_id;
1467 rs->sr_err = LDAP_SUCCESS;
1468 rs->sr_sasldata = NULL;
1469 send_ldap_sasl( op, rs );
1473 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1474 "requested SASL mechanism not supported" );
1477 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1478 "SASL not supported" );
1484 char* slap_sasl_secprops( const char *in )
1486 #ifdef HAVE_CYRUS_SASL
1487 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1489 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1491 return "SASL not supported";
1495 void slap_sasl_secprops_unparse( struct berval *bv )
1497 #ifdef HAVE_CYRUS_SASL
1498 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1502 #ifdef HAVE_CYRUS_SASL
1504 slap_sasl_setpass( Operation *op, SlapReply *rs )
1506 struct berval id = BER_BVNULL; /* needs to come from connection */
1507 struct berval new = BER_BVNULL;
1508 struct berval old = BER_BVNULL;
1510 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1512 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1513 (SASL_CONST void **)&id.bv_val );
1515 if( rs->sr_err != SASL_OK ) {
1516 rs->sr_text = "unable to retrieve SASL username";
1517 rs->sr_err = LDAP_OTHER;
1521 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1522 id.bv_val ? id.bv_val : "", 0, 0 );
1524 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1525 NULL, &old, &new, &rs->sr_text );
1527 if( rs->sr_err != LDAP_SUCCESS ) {
1531 if( new.bv_len == 0 ) {
1532 slap_passwd_generate(&new);
1534 if( new.bv_len == 0 ) {
1535 rs->sr_text = "password generation failed.";
1536 rs->sr_err = LDAP_OTHER;
1540 rs->sr_rspdata = slap_passwd_return( &new );
1543 #if SASL_VERSION_MAJOR < 2
1544 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1545 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1547 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1548 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1549 if( rs->sr_err != SASL_OK ) {
1550 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1553 switch(rs->sr_err) {
1555 rs->sr_err = LDAP_SUCCESS;
1565 rs->sr_err = LDAP_OTHER;
1571 #endif /* HAVE_CYRUS_SASL */
1573 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1574 * string returned in *dn is in its own allocated memory, and must be free'd
1575 * by the calling process. -Mark Adamson, Carnegie Mellon
1577 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1578 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1585 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1586 char *user_realm, struct berval *dn, int flags )
1588 int rc, is_dn = SET_NONE, do_norm = 1;
1589 struct berval dn2, *mech;
1591 assert( conn != NULL );
1592 assert( id != NULL );
1594 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1596 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1597 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1598 (unsigned long) id->bv_len ) );
1601 op = conn->c_sasl_bindop;
1603 assert( op != NULL );
1607 if ( !BER_BVISNULL( id ) ) {
1608 /* Blatantly anonymous ID */
1609 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1611 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1612 return( LDAP_SUCCESS );
1616 /* FIXME: if empty, should we stop? */
1617 BER_BVSTR( id, "" );
1620 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1621 mech = &conn->c_sasl_bind_mech;
1623 mech = &conn->c_authmech;
1626 /* An authcID needs to be converted to authzID form. Set the
1627 * values directly into *dn; they will be normalized later. (and
1628 * normalizing always makes a new copy.) An ID from a TLS certificate
1629 * is already normalized, so copy it and skip normalization.
1631 if( flags & SLAP_GETDN_AUTHCID ) {
1632 if( bvmatch( mech, &ext_bv )) {
1633 /* EXTERNAL DNs are already normalized */
1634 assert( !BER_BVISNULL( id ) );
1638 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1641 /* convert to u:<username> form */
1647 if( is_dn == SET_NONE ) {
1648 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1650 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1651 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1653 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1655 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1656 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1660 /* No other possibilities from here */
1661 if( is_dn == SET_NONE ) {
1663 return( LDAP_INAPPROPRIATE_AUTH );
1666 /* Username strings */
1667 if( is_dn == SET_U ) {
1668 /* ITS#3419: values may need escape */
1670 LDAPAVA *RDNs[ 4 ][ 2 ];
1675 DN[ irdn ] = RDNs[ irdn ];
1676 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1677 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1678 AVAs[ irdn ].la_value = *dn;
1679 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1680 AVAs[ irdn ].la_private = NULL;
1681 RDNs[ irdn ][ 1 ] = NULL;
1683 if ( user_realm && *user_realm ) {
1685 DN[ irdn ] = RDNs[ irdn ];
1686 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1687 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1688 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1689 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1690 AVAs[ irdn ].la_private = NULL;
1691 RDNs[ irdn ][ 1 ] = NULL;
1694 if ( !BER_BVISNULL( mech ) ) {
1696 DN[ irdn ] = RDNs[ irdn ];
1697 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1698 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1699 AVAs[ irdn ].la_value = *mech;
1700 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1701 AVAs[ irdn ].la_private = NULL;
1702 RDNs[ irdn ][ 1 ] = NULL;
1706 DN[ irdn ] = RDNs[ irdn ];
1707 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1708 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1709 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1710 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1711 AVAs[ irdn ].la_private = NULL;
1712 RDNs[ irdn ][ 1 ] = NULL;
1717 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1719 if ( rc != LDAP_SUCCESS ) {
1724 Debug( LDAP_DEBUG_TRACE,
1725 "slap_sasl_getdn: u:id converted to %s\n",
1730 /* Dup the DN in any case, so we don't risk
1731 * leaks or dangling pointers later,
1732 * and the DN value is '\0' terminated */
1733 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1734 dn->bv_val = dn2.bv_val;
1737 /* All strings are in DN form now. Normalize if needed. */
1739 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1741 /* User DNs were constructed above and must be freed now */
1742 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1744 if ( rc != LDAP_SUCCESS ) {
1751 /* Run thru regexp */
1752 slap_sasl2dn( op, dn, &dn2, flags );
1753 if( !BER_BVISNULL( &dn2 ) ) {
1754 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1756 Debug( LDAP_DEBUG_TRACE,
1757 "slap_sasl_getdn: dn:id converted to %s\n",
1761 return( LDAP_SUCCESS );