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=*)");
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 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] )) {
188 i += SLAP_SASL_PROP_COUNT - 1;
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[SLAP_SASL_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[SLAP_SASL_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[SLAP_SASL_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] )) {
308 i += SLAP_SASL_PROP_COUNT - 1;
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[SLAP_SASL_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[SLAP_SASL_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[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
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 )
578 which = SLAP_SASL_PROP_AUTHC;
580 which = SLAP_SASL_PROP_AUTHZ;
582 /* Need to store the Connection for auxprop_lookup */
583 if ( !auxvals[SLAP_SASL_PROP_CONN].values ) {
584 names[0] = slap_propnames[SLAP_SASL_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[SLAP_SASL_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;
658 * (SLAP_SASL_PROP_COUNT - 1) because we skip "conn",
659 * + 1 for NULL termination?
661 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
662 struct berval authcDN, authzDN = BER_BVNULL;
665 /* Simple Binds don't support proxy authorization, ignore it */
666 if ( !conn->c_sasl_bindop ||
667 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
669 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
670 "authcid=\"%s\" authzid=\"%s\"\n",
671 conn ? conn->c_connid : -1, auth_identity, requested_user );
672 if ( conn->c_sasl_dn.bv_val ) {
673 ch_free( conn->c_sasl_dn.bv_val );
674 BER_BVZERO( &conn->c_sasl_dn );
677 /* Skip SLAP_SASL_PROP_CONN */
678 prop_getnames( props, slap_propnames+1, auxvals );
680 /* Should not happen */
681 if ( !auxvals[0].values ) {
682 sasl_seterror( sconn, 0, "invalid authcid" );
686 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
687 conn->c_sasl_dn = authcDN;
689 /* Nothing to do if no authzID was given */
690 if ( !auxvals[1].name || !auxvals[1].values ) {
694 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
696 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
697 if ( rc != LDAP_SUCCESS ) {
698 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
699 "proxy authorization disallowed (%d)\n",
700 (long) (conn ? conn->c_connid : -1), rc, 0 );
702 sasl_seterror( sconn, 0, "not authorized" );
703 ch_free( authzDN.bv_val );
707 /* FIXME: we need yet another dup because slap_sasl_getdn()
708 * is using the bind operation slab */
709 if ( conn->c_sasl_bindop ) {
710 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
711 slap_sl_free( authzDN.bv_val,
712 conn->c_sasl_bindop->o_tmpmemctx );
715 conn->c_sasl_authz_dn = authzDN;
719 if (conn->c_sasl_bindop) {
720 Statslog( LDAP_DEBUG_STATS,
721 "conn=%lu op=%lu BIND authcid=\"%s\" authzid=\"%s\"\n",
722 conn->c_connid, conn->c_sasl_bindop->o_opid,
723 auth_identity, requested_user, 0);
726 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
727 " proxy authorization allowed authzDN=\"%s\"\n",
728 (long) (conn ? conn->c_connid : -1),
729 authzDN.bv_val ? authzDN.bv_val : "", 0 );
741 struct berval authcDN, authzDN = BER_BVNULL;
743 Connection *conn = context;
745 struct berval bvauthcid, bvauthzid;
748 if ( conn->c_sasl_dn.bv_val ) {
749 ch_free( conn->c_sasl_dn.bv_val );
750 BER_BVZERO( &conn->c_sasl_dn );
753 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
754 "authcid=\"%s\" authzid=\"%s\"\n",
755 (long) (conn ? conn->c_connid : -1),
756 authcid ? authcid : "<empty>",
757 authzid ? authzid : "<empty>" );
759 /* Figure out how much data we have for the dn */
760 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
761 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
762 Debug(LDAP_DEBUG_TRACE,
763 "authorize: getprop(REALM) failed!\n", 0,0,0);
764 *errstr = "Could not extract realm";
768 /* Convert the identities to DN's. If no authzid was given, client will
769 be bound as the DN matching their username */
770 bvauthcid.bv_val = authcid;
771 bvauthcid.bv_len = authcid ? strlen( authcid ) : 0;
772 rc = slap_sasl_getdn( conn, NULL, &bvauthcid, realm,
773 &authcDN, SLAP_GETDN_AUTHCID );
774 if( rc != LDAP_SUCCESS ) {
775 *errstr = ldap_err2string( rc );
778 conn->c_sasl_dn = authcDN;
779 if( ( authzid == NULL ) || !strcmp( authcid, authzid ) ) {
780 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
781 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
786 bvauthzid.bv_val = authzid;
787 bvauthzid.bv_len = authzid ? strlen( authzid ) : 0;
788 rc = slap_sasl_getdn( conn, NULL, &bvauthzid, realm,
789 &authzDN, SLAP_GETDN_AUTHZID );
790 if( rc != LDAP_SUCCESS ) {
791 *errstr = ldap_err2string( rc );
795 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
797 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
798 "proxy authorization disallowed (%d)\n",
799 (long) (conn ? conn->c_connid : -1), rc, 0 );
801 *errstr = "not authorized";
802 ch_free( authzDN.bv_val );
806 /* FIXME: we need yet another dup because slap_sasl_getdn()
807 * is using the bind operation slab */
808 if ( conn->c_sasl_bindop ) {
809 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
810 slap_sl_free( authzDN.bv_val,
811 conn->c_sasl_bindop->o_tmpmemctx );
814 conn->c_sasl_authz_dn = authzDN;
818 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
819 " authorization allowed authzDN=\"%s\"\n",
820 (long) (conn ? conn->c_connid : -1),
821 authzDN.bv_val ? authzDN.bv_val : "", 0 );
823 if ( conn->c_sasl_bindop ) {
824 Statslog( LDAP_DEBUG_STATS,
825 "conn=%lu op=%lu BIND authcid=\"%s\" authzid=\"%s\"\n",
826 conn->c_connid, conn->c_sasl_bindop->o_opid,
827 authcid, authzid ? authzid : "", 0);
833 #endif /* SASL_VERSION_MAJOR >= 2 */
836 slap_sasl_err2ldap( int saslerr )
840 /* map SASL errors to LDAP resultCode returned by:
842 * SASL_OK, SASL_NOMEM
844 * SASL_OK, SASL_CONTINUE, SASL_TRANS, SASL_BADPARAM, SASL_BADPROT,
846 * sasl_server_start()
849 * SASL_OK, SASL_BADPARAM
857 rc = LDAP_SASL_BIND_IN_PROGRESS;
864 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
870 rc = LDAP_INVALID_CREDENTIALS;
873 rc = LDAP_INSUFFICIENT_ACCESS;
877 rc = LDAP_INAPPROPRIATE_AUTH;
881 rc = LDAP_UNAVAILABLE;
884 rc = LDAP_UNWILLING_TO_PERFORM;
896 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
899 const struct berval *sc,
900 const struct berval * passwd,
901 const struct berval * cred,
906 void *ctx, *sconn = NULL;
908 for( i=0; i<cred->bv_len; i++) {
909 if(cred->bv_val[i] == '\0') {
910 return LUTIL_PASSWD_ERR; /* NUL character in password */
914 if( cred->bv_val[i] != '\0' ) {
915 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
918 for( i=0; i<passwd->bv_len; i++) {
919 if(passwd->bv_val[i] == '\0') {
920 return LUTIL_PASSWD_ERR; /* NUL character in password */
924 if( passwd->bv_val[i] != '\0' ) {
925 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
928 rtn = LUTIL_PASSWD_ERR;
930 ctx = ldap_pvt_thread_pool_context();
931 ldap_pvt_thread_pool_getkey( ctx, slap_sasl_bind, &sconn, NULL );
933 if( sconn != NULL ) {
935 # if SASL_VERSION_MAJOR < 2
936 sc = sasl_checkpass( sconn,
937 passwd->bv_val, passwd->bv_len,
938 cred->bv_val, cred->bv_len,
941 sc = sasl_checkpass( sconn,
942 passwd->bv_val, passwd->bv_len,
943 cred->bv_val, cred->bv_len );
945 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
950 #endif /* SLAPD_SPASSWD */
952 #endif /* HAVE_CYRUS_SASL */
954 int slap_sasl_init( void )
956 #ifdef HAVE_CYRUS_SASL
958 static sasl_callback_t server_callbacks[] = {
959 { SASL_CB_LOG, &slap_sasl_log, NULL },
960 { SASL_CB_LIST_END, NULL, NULL }
963 #ifdef HAVE_SASL_VERSION
964 /* stringify the version number, sasl.h doesn't do it for us */
965 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
966 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
967 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
970 sasl_version( NULL, &rc );
971 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
972 (rc & 0xffff) < SASL_VERSION_STEP)
974 char version[sizeof("xxx.xxx.xxxxx")];
975 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
977 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
978 " expected " SASL_VERSION_STRING ","
979 " got %s\n", version, 0, 0 );
984 /* SASL 2 does its own memory management internally */
985 #if SASL_VERSION_MAJOR < 2
994 ldap_pvt_sasl_mutex_new,
995 ldap_pvt_sasl_mutex_lock,
996 ldap_pvt_sasl_mutex_unlock,
997 ldap_pvt_sasl_mutex_dispose );
999 #if SASL_VERSION_MAJOR >= 2
1000 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1002 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1003 if( rc != SASL_OK ) {
1004 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
1009 /* should provide callbacks for logging */
1010 /* server name should be configurable */
1011 rc = sasl_server_init( server_callbacks, "slapd" );
1013 if( rc != SASL_OK ) {
1014 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
1016 #if SASL_VERSION_MAJOR < 2
1017 /* A no-op used to make sure we linked with Cyrus 1.5 */
1018 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1024 #ifdef SLAPD_SPASSWD
1025 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
1028 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1031 /* default security properties */
1032 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1033 sasl_secprops.max_ssf = INT_MAX;
1034 sasl_secprops.maxbufsize = 65536;
1035 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1041 int slap_sasl_destroy( void )
1043 #ifdef HAVE_CYRUS_SASL
1046 free( global_host );
1052 int slap_sasl_open( Connection *conn, int reopen )
1054 int sc = LDAP_SUCCESS;
1055 #ifdef HAVE_CYRUS_SASL
1058 sasl_conn_t *ctx = NULL;
1059 sasl_callback_t *session_callbacks;
1061 #if SASL_VERSION_MAJOR >= 2
1062 char *ipremoteport = NULL, *iplocalport = NULL;
1065 assert( conn->c_sasl_authctx == NULL );
1068 assert( conn->c_sasl_extra == NULL );
1071 #if SASL_VERSION_MAJOR >= 2
1072 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1074 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1076 if( session_callbacks == NULL ) {
1077 Debug( LDAP_DEBUG_ANY,
1078 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1081 conn->c_sasl_extra = session_callbacks;
1083 session_callbacks[cb=0].id = SASL_CB_LOG;
1084 session_callbacks[cb].proc = &slap_sasl_log;
1085 session_callbacks[cb++].context = conn;
1087 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1088 session_callbacks[cb].proc = &slap_sasl_authorize;
1089 session_callbacks[cb++].context = conn;
1091 #if SASL_VERSION_MAJOR >= 2
1092 session_callbacks[cb].id = SASL_CB_CANON_USER;
1093 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1094 session_callbacks[cb++].context = conn;
1097 session_callbacks[cb].id = SASL_CB_LIST_END;
1098 session_callbacks[cb].proc = NULL;
1099 session_callbacks[cb++].context = NULL;
1101 session_callbacks = conn->c_sasl_extra;
1104 conn->c_sasl_layers = 0;
1106 if( global_host == NULL ) {
1107 global_host = ldap_pvt_get_fqdn( NULL );
1110 /* create new SASL context */
1111 #if SASL_VERSION_MAJOR >= 2
1112 if ( conn->c_sock_name.bv_len != 0 &&
1113 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1116 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1117 /* Convert IPv6 addresses to address;port syntax. */
1118 p = strrchr( iplocalport, ' ' );
1119 /* Convert IPv4 addresses to address;port syntax. */
1120 if ( p == NULL ) p = strchr( iplocalport, ':' );
1125 if ( conn->c_peer_name.bv_len != 0 &&
1126 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1129 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1130 /* Convert IPv6 addresses to address;port syntax. */
1131 p = strrchr( ipremoteport, ' ' );
1132 /* Convert IPv4 addresses to address;port syntax. */
1133 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1138 sc = sasl_server_new( "ldap", global_host, global_realm,
1139 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1140 if ( iplocalport != NULL ) {
1141 ch_free( iplocalport );
1143 if ( ipremoteport != NULL ) {
1144 ch_free( ipremoteport );
1147 sc = sasl_server_new( "ldap", global_host, global_realm,
1148 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1151 if( sc != SASL_OK ) {
1152 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1158 conn->c_sasl_authctx = ctx;
1160 if( sc == SASL_OK ) {
1161 sc = sasl_setprop( ctx,
1162 SASL_SEC_PROPS, &sasl_secprops );
1164 if( sc != SASL_OK ) {
1165 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1168 slap_sasl_close( conn );
1173 sc = slap_sasl_err2ldap( sc );
1175 #elif defined(SLAP_BUILTIN_SASL)
1176 /* built-in SASL implementation */
1177 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1178 if( ctx == NULL ) return -1;
1180 ctx->sc_external_ssf = 0;
1181 BER_BVZERO( &ctx->sc_external_id );
1183 conn->c_sasl_authctx = ctx;
1189 int slap_sasl_external(
1192 struct berval *auth_id )
1194 #if SASL_VERSION_MAJOR >= 2
1196 sasl_conn_t *ctx = conn->c_sasl_authctx;
1198 if ( ctx == NULL ) {
1199 return LDAP_UNAVAILABLE;
1202 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1204 if ( sc != SASL_OK ) {
1208 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1209 auth_id ? auth_id->bv_val : NULL );
1211 if ( sc != SASL_OK ) {
1215 #elif defined(HAVE_CYRUS_SASL)
1217 sasl_conn_t *ctx = conn->c_sasl_authctx;
1218 sasl_external_properties_t extprops;
1220 if ( ctx == NULL ) {
1221 return LDAP_UNAVAILABLE;
1224 memset( &extprops, '\0', sizeof(extprops) );
1226 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1228 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1229 (void *) &extprops );
1231 if ( sc != SASL_OK ) {
1234 #elif defined(SLAP_BUILTIN_SASL)
1235 /* built-in SASL implementation */
1236 SASL_CTX *ctx = conn->c_sasl_authctx;
1237 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1239 ctx->sc_external_ssf = ssf;
1241 ctx->sc_external_id = *auth_id;
1242 BER_BVZERO( auth_id );
1244 BER_BVZERO( &ctx->sc_external_id );
1248 return LDAP_SUCCESS;
1251 int slap_sasl_reset( Connection *conn )
1253 return LDAP_SUCCESS;
1256 char ** slap_sasl_mechs( Connection *conn )
1258 char **mechs = NULL;
1260 #ifdef HAVE_CYRUS_SASL
1261 sasl_conn_t *ctx = conn->c_sasl_authctx;
1263 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1267 SASL_CONST char *mechstr;
1269 sc = sasl_listmech( ctx,
1270 NULL, NULL, ",", NULL,
1271 &mechstr, NULL, NULL );
1273 if( sc != SASL_OK ) {
1274 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1280 mechs = ldap_str2charray( mechstr, "," );
1282 #if SASL_VERSION_MAJOR < 2
1286 #elif defined(SLAP_BUILTIN_SASL)
1287 /* builtin SASL implementation */
1288 SASL_CTX *ctx = conn->c_sasl_authctx;
1289 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1290 /* should check ssf */
1291 mechs = ldap_str2charray( "EXTERNAL", "," );
1298 int slap_sasl_close( Connection *conn )
1300 #ifdef HAVE_CYRUS_SASL
1301 sasl_conn_t *ctx = conn->c_sasl_authctx;
1304 sasl_dispose( &ctx );
1306 if ( conn->c_sasl_sockctx &&
1307 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1309 ctx = conn->c_sasl_sockctx;
1310 sasl_dispose( &ctx );
1313 conn->c_sasl_authctx = NULL;
1314 conn->c_sasl_sockctx = NULL;
1315 conn->c_sasl_done = 0;
1317 free( conn->c_sasl_extra );
1318 conn->c_sasl_extra = NULL;
1320 #elif defined(SLAP_BUILTIN_SASL)
1321 SASL_CTX *ctx = conn->c_sasl_authctx;
1323 if( ctx->sc_external_id.bv_val ) {
1324 free( ctx->sc_external_id.bv_val );
1325 BER_BVZERO( &ctx->sc_external_id );
1328 conn->c_sasl_authctx = NULL;
1332 return LDAP_SUCCESS;
1335 int slap_sasl_bind( Operation *op, SlapReply *rs )
1337 #ifdef HAVE_CYRUS_SASL
1338 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1339 struct berval response;
1340 unsigned reslen = 0;
1343 Debug(LDAP_DEBUG_ARGS,
1344 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1345 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1346 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1347 op->o_conn->c_sasl_bind_mech.bv_val,
1348 op->orb_cred.bv_len );
1351 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1352 "SASL unavailable on this session" );
1356 #if SASL_VERSION_MAJOR >= 2
1357 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1358 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1359 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1360 sasl_server_step( ctx, cred, clen, resp, rlen )
1362 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1363 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1364 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1365 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1368 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1369 /* If we already authenticated once, must use a new context */
1370 if ( op->o_conn->c_sasl_done ) {
1372 const char *authid = NULL;
1373 #if SASL_VERSION_MAJOR >= 2
1374 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1375 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1376 if ( authid ) authid = ch_strdup( authid );
1378 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1379 sasl_dispose( &ctx );
1381 op->o_conn->c_sasl_authctx = NULL;
1383 slap_sasl_open( op->o_conn, 1 );
1384 ctx = op->o_conn->c_sasl_authctx;
1385 #if SASL_VERSION_MAJOR >= 2
1387 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1388 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1389 ch_free( (char *)authid );
1394 op->o_conn->c_sasl_bind_mech.bv_val,
1395 op->orb_cred.bv_val, op->orb_cred.bv_len,
1396 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1400 op->orb_cred.bv_val, op->orb_cred.bv_len,
1401 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1404 response.bv_len = reslen;
1406 if ( sc == SASL_OK ) {
1407 sasl_ssf_t *ssf = NULL;
1409 op->orb_edn = op->o_conn->c_sasl_dn;
1410 BER_BVZERO( &op->o_conn->c_sasl_dn );
1411 op->o_conn->c_sasl_done = 1;
1413 rs->sr_err = LDAP_SUCCESS;
1415 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1416 op->orb_ssf = ssf ? *ssf : 0;
1420 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1421 op->o_conn->c_sasl_layers++;
1423 /* If there's an old layer, set sockctx to NULL to
1424 * tell connection_read() to wait for us to finish.
1425 * Otherwise there is a race condition: we have to
1426 * send the Bind response using the old security
1427 * context and then remove it before reading any
1430 if ( op->o_conn->c_sasl_sockctx ) {
1431 ctx = op->o_conn->c_sasl_sockctx;
1432 op->o_conn->c_sasl_sockctx = NULL;
1434 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1436 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1439 /* Must send response using old security layer */
1440 if (response.bv_len) rs->sr_sasldata = &response;
1441 send_ldap_sasl( op, rs );
1443 /* Now dispose of the old security layer.
1446 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1447 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1448 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1449 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1450 sasl_dispose( &ctx );
1452 } else if ( sc == SASL_CONTINUE ) {
1453 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1454 #if SASL_VERSION_MAJOR >= 2
1455 rs->sr_text = sasl_errdetail( ctx );
1457 rs->sr_sasldata = &response;
1458 send_ldap_sasl( op, rs );
1461 #if SASL_VERSION_MAJOR >= 2
1462 rs->sr_text = sasl_errdetail( ctx );
1464 rs->sr_err = slap_sasl_err2ldap( sc ),
1465 send_ldap_result( op, rs );
1468 #if SASL_VERSION_MAJOR < 2
1469 if( response.bv_len ) ch_free( response.bv_val );
1472 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1474 #elif defined(SLAP_BUILTIN_SASL)
1475 /* built-in SASL implementation */
1476 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1478 if ( ctx == NULL ) {
1479 send_ldap_error( op, rs, LDAP_OTHER,
1480 "Internal SASL Error" );
1482 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1485 if( op->orb_cred.bv_len ) {
1486 rs->sr_text = "proxy authorization not support";
1487 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1488 send_ldap_result( op, rs );
1491 op->orb_edn = ctx->sc_external_id;
1492 rs->sr_err = LDAP_SUCCESS;
1493 rs->sr_sasldata = NULL;
1494 send_ldap_sasl( op, rs );
1498 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1499 "requested SASL mechanism not supported" );
1502 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1503 "SASL not supported" );
1509 char* slap_sasl_secprops( const char *in )
1511 #ifdef HAVE_CYRUS_SASL
1512 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1514 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1516 return "SASL not supported";
1520 void slap_sasl_secprops_unparse( struct berval *bv )
1522 #ifdef HAVE_CYRUS_SASL
1523 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1527 #ifdef HAVE_CYRUS_SASL
1529 slap_sasl_setpass( Operation *op, SlapReply *rs )
1531 struct berval id = BER_BVNULL; /* needs to come from connection */
1532 struct berval new = BER_BVNULL;
1533 struct berval old = BER_BVNULL;
1535 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1537 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1538 (SASL_CONST void **)&id.bv_val );
1540 if( rs->sr_err != SASL_OK ) {
1541 rs->sr_text = "unable to retrieve SASL username";
1542 rs->sr_err = LDAP_OTHER;
1546 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1547 id.bv_val ? id.bv_val : "", 0, 0 );
1549 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1550 NULL, &old, &new, &rs->sr_text );
1552 if( rs->sr_err != LDAP_SUCCESS ) {
1556 if( new.bv_len == 0 ) {
1557 slap_passwd_generate(&new);
1559 if( new.bv_len == 0 ) {
1560 rs->sr_text = "password generation failed.";
1561 rs->sr_err = LDAP_OTHER;
1565 rs->sr_rspdata = slap_passwd_return( &new );
1568 #if SASL_VERSION_MAJOR < 2
1569 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1570 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1572 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1573 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1574 if( rs->sr_err != SASL_OK ) {
1575 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1578 switch(rs->sr_err) {
1580 rs->sr_err = LDAP_SUCCESS;
1590 rs->sr_err = LDAP_OTHER;
1596 #endif /* HAVE_CYRUS_SASL */
1598 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1599 * string returned in *dn is in its own allocated memory, and must be free'd
1600 * by the calling process. -Mark Adamson, Carnegie Mellon
1602 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1603 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1610 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1611 char *user_realm, struct berval *dn, int flags )
1613 int rc, is_dn = SET_NONE, do_norm = 1;
1614 struct berval dn2, *mech;
1616 assert( conn != NULL );
1617 assert( id != NULL );
1619 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1621 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1622 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1623 (unsigned long) id->bv_len ) );
1626 op = conn->c_sasl_bindop;
1628 assert( op != NULL );
1632 if ( !BER_BVISNULL( id ) ) {
1633 /* Blatantly anonymous ID */
1634 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1636 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1637 return( LDAP_SUCCESS );
1641 /* FIXME: if empty, should we stop? */
1642 BER_BVSTR( id, "" );
1645 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1646 mech = &conn->c_sasl_bind_mech;
1648 mech = &conn->c_authmech;
1651 /* An authcID needs to be converted to authzID form. Set the
1652 * values directly into *dn; they will be normalized later. (and
1653 * normalizing always makes a new copy.) An ID from a TLS certificate
1654 * is already normalized, so copy it and skip normalization.
1656 if( flags & SLAP_GETDN_AUTHCID ) {
1657 if( bvmatch( mech, &ext_bv )) {
1658 /* EXTERNAL DNs are already normalized */
1659 assert( !BER_BVISNULL( id ) );
1663 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1666 /* convert to u:<username> form */
1672 if( is_dn == SET_NONE ) {
1673 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1675 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1676 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1678 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1680 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1681 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1685 /* No other possibilities from here */
1686 if( is_dn == SET_NONE ) {
1688 return( LDAP_INAPPROPRIATE_AUTH );
1691 /* Username strings */
1692 if( is_dn == SET_U ) {
1693 /* ITS#3419: values may need escape */
1695 LDAPAVA *RDNs[ 4 ][ 2 ];
1700 DN[ irdn ] = RDNs[ irdn ];
1701 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1702 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1703 AVAs[ irdn ].la_value = *dn;
1704 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1705 AVAs[ irdn ].la_private = NULL;
1706 RDNs[ irdn ][ 1 ] = NULL;
1708 if ( user_realm && *user_realm ) {
1710 DN[ irdn ] = RDNs[ irdn ];
1711 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1712 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1713 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1714 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1715 AVAs[ irdn ].la_private = NULL;
1716 RDNs[ irdn ][ 1 ] = NULL;
1719 if ( !BER_BVISNULL( mech ) ) {
1721 DN[ irdn ] = RDNs[ irdn ];
1722 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1723 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1724 AVAs[ irdn ].la_value = *mech;
1725 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1726 AVAs[ irdn ].la_private = NULL;
1727 RDNs[ irdn ][ 1 ] = NULL;
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_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1735 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1736 AVAs[ irdn ].la_private = NULL;
1737 RDNs[ irdn ][ 1 ] = NULL;
1742 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1744 if ( rc != LDAP_SUCCESS ) {
1749 Debug( LDAP_DEBUG_TRACE,
1750 "slap_sasl_getdn: u:id converted to %s\n",
1755 /* Dup the DN in any case, so we don't risk
1756 * leaks or dangling pointers later,
1757 * and the DN value is '\0' terminated */
1758 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1759 dn->bv_val = dn2.bv_val;
1762 /* All strings are in DN form now. Normalize if needed. */
1764 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1766 /* User DNs were constructed above and must be freed now */
1767 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1769 if ( rc != LDAP_SUCCESS ) {
1776 /* Run thru regexp */
1777 slap_sasl2dn( op, dn, &dn2, flags );
1778 if( !BER_BVISNULL( &dn2 ) ) {
1779 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1781 Debug( LDAP_DEBUG_TRACE,
1782 "slap_sasl_getdn: dn:id converted to %s\n",
1786 return( LDAP_SUCCESS );