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 };
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 );
205 a = attr_find( rs->sr_entry->e_attrs, ad );
207 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
210 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
211 sl->sparams->utils->prop_erase( sl->sparams->propctx,
214 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
215 /* ITS#3846 don't give hashed passwords to SASL */
216 if ( ad == slap_schema.si_ad_userPassword &&
217 bv->bv_val[0] == '{' ) {
218 rc = lutil_passwd_scheme( bv->bv_val );
220 /* If it's not a recognized scheme, just assume it's
221 * a cleartext password that happened to include brackets.
223 * If it's a recognized scheme, skip this value, unless the
224 * scheme is {CLEARTEXT}. In that case, skip over the
225 * scheme name and use the remainder. If there is nothing
226 * past the scheme name, skip this value.
228 #ifdef SLAPD_CLEARTEXT
229 if ( !strncasecmp( bv->bv_val, sc_cleartext.bv_val,
230 sc_cleartext.bv_len )) {
232 cbv.bv_len = bv->bv_len - sc_cleartext.bv_len;
234 cbv.bv_val = bv->bv_val + sc_cleartext.bv_len;
235 sl->sparams->utils->prop_set( sl->sparams->propctx,
236 sl->list[i].name, cbv.bv_val, cbv.bv_len );
243 sl->sparams->utils->prop_set( sl->sparams->propctx,
244 sl->list[i].name, bv->bv_val, bv->bv_len );
253 sasl_server_params_t *sparams,
260 Connection *conn = NULL;
263 sl.list = sparams->utils->prop_get( sparams->propctx );
264 sl.sparams = sparams;
267 /* Find our DN and conn first */
268 for( i = 0; sl.list[i].name; i++ ) {
269 if ( sl.list[i].name[0] == '*' ) {
270 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
271 if ( sl.list[i].values && sl.list[i].values[0] )
272 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
275 if ( (flags & SASL_AUXPROP_AUTHZID) &&
276 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
278 if ( sl.list[i].values && sl.list[i].values[0] )
279 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
282 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
283 if ( sl.list[i].values && sl.list[i].values[0] ) {
284 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
285 if ( !(flags & SASL_AUXPROP_AUTHZID) )
292 /* Now see what else needs to be fetched */
293 for( i = 0; sl.list[i].name; i++ ) {
294 const char *name = sl.list[i].name;
296 if ( name[0] == '*' ) {
297 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
298 /* Skip our private properties */
299 if ( !strcmp( name, slap_propnames[0] )) {
304 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
307 if ( sl.list[i].values ) {
308 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
315 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
319 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
321 if ( op.o_bd && op.o_bd->be_search ) {
322 SlapReply rs = {REP_RESULT};
323 op.o_hdr = conn->c_sasl_bindop->o_hdr;
324 op.o_tag = LDAP_REQ_SEARCH;
325 op.o_ndn = conn->c_ndn;
327 op.o_time = slap_get_time();
328 op.o_do_not_cache = 1;
329 op.o_is_auth_check = 1;
330 op.o_req_dn = op.o_req_ndn;
331 op.ors_scope = LDAP_SCOPE_BASE;
332 op.ors_deref = LDAP_DEREF_NEVER;
333 op.ors_tlimit = SLAP_NO_LIMIT;
335 op.ors_filter = &generic_filter;
336 op.ors_filterstr = generic_filterstr;
337 /* FIXME: we want all attributes, right? */
340 op.o_bd->be_search( &op, &rs );
345 #if SASL_VERSION_FULL >= 0x020110
349 sasl_server_params_t *sparams,
350 struct propctx *prctx,
355 SlapReply rs = {REP_RESULT};
357 Connection *conn = NULL;
358 const struct propval *pr;
359 Modifications *modlist = NULL, **modtail = &modlist, *mod;
360 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
361 char textbuf[SLAP_TEXT_BUFLEN];
363 size_t textlen = sizeof(textbuf);
365 /* just checking if we are enabled */
366 if (!prctx) return SASL_OK;
368 if (!sparams || !user) return SASL_BADPARAM;
370 pr = sparams->utils->prop_get( sparams->propctx );
372 /* Find our DN and conn first */
373 for( i = 0; pr[i].name; i++ ) {
374 if ( pr[i].name[0] == '*' ) {
375 if ( !strcmp( pr[i].name, slap_propnames[PROP_CONN] ) ) {
376 if ( pr[i].values && pr[i].values[0] )
377 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
380 if ( !strcmp( pr[i].name, slap_propnames[PROP_AUTHC] ) ) {
381 if ( pr[i].values && pr[i].values[0] ) {
382 AC_MEMCPY( &op.o_req_ndn, pr[i].values[0], sizeof( struct berval ) );
387 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
389 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
391 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
393 pr = sparams->utils->prop_get( prctx );
394 if (!pr) return SASL_BADPARAM;
396 for (i=0; pr[i].name; i++);
397 if (!i) return SASL_BADPARAM;
399 for (i=0; pr[i].name; i++) {
400 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
401 mod->sml_op = LDAP_MOD_REPLACE;
403 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
404 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
405 sizeof(struct berval));
406 for (j=0; j<pr[i].nvalues; j++) {
407 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
409 BER_BVZERO( &mod->sml_values[j] );
410 mod->sml_nvalues = NULL;
411 mod->sml_desc = NULL;
413 modtail = &mod->sml_next;
417 rc = slap_mods_check( modlist, &text, textbuf, textlen, NULL );
419 if ( rc == LDAP_SUCCESS ) {
420 rc = slap_mods_no_user_mod_check( &op, modlist,
421 &text, textbuf, textlen );
423 if ( rc == LDAP_SUCCESS ) {
424 rc = slap_mods_opattrs( &op, modlist, modtail,
425 &text, textbuf, textlen, 1 );
427 if ( rc == LDAP_SUCCESS ) {
428 op.o_hdr = conn->c_sasl_bindop->o_hdr;
429 op.o_tag = LDAP_REQ_MODIFY;
430 op.o_ndn = op.o_req_ndn;
432 op.o_time = slap_get_time();
433 op.o_do_not_cache = 1;
434 op.o_is_auth_check = 1;
435 op.o_req_dn = op.o_req_ndn;
436 op.orm_modlist = modlist;
438 rc = op.o_bd->be_modify( &op, &rs );
442 slap_mods_free( modlist );
443 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
445 #endif /* SASL_VERSION_FULL >= 2.1.16 */
447 static sasl_auxprop_plug_t slap_auxprop_plugin = {
450 NULL, /* glob_context */
451 NULL, /* auxprop_free */
454 #if SASL_VERSION_FULL >= 0x020110
455 slap_auxprop_store /* the declaration of this member changed
456 * in cyrus SASL from 2.1.15 to 2.1.16 */
464 const sasl_utils_t *utils,
467 sasl_auxprop_plug_t **plug,
468 const char *plugname)
470 if ( !out_version || !plug ) return SASL_BADPARAM;
472 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
474 *out_version = SASL_AUXPROP_PLUG_VERSION;
475 *plug = &slap_auxprop_plugin;
479 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
480 * auxiliary property, so that we can refer to it in sasl_authorize
481 * without interfering with anything else. Also, the SASL username
482 * buffer is constrained to 256 characters, and our DNs could be
483 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
486 slap_sasl_canonicalize(
492 const char *user_realm,
497 Connection *conn = (Connection *)context;
498 struct propctx *props = sasl_auxprop_getctx( sconn );
499 struct propval auxvals[3];
502 const char *names[2];
507 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
508 conn ? conn->c_connid : -1,
509 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
510 in ? in : "<empty>");
512 /* If name is too big, just truncate. We don't care, we're
513 * using DNs, not the usernames.
515 if ( inlen > out_max )
518 /* This is a Simple Bind using SPASSWD. That means the in-directory
519 * userPassword of the Binding user already points at SASL, so it
520 * cannot be used to actually satisfy a password comparison. Just
521 * ignore it, some other mech will process it.
523 if ( !conn->c_sasl_bindop ||
524 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
526 /* See if we need to add request, can only do it once */
527 prop_getnames( props, slap_propnames, auxvals );
528 if ( !auxvals[0].name )
529 prop_request( props, slap_propnames );
531 if ( flags & SASL_CU_AUTHID )
536 /* Need to store the Connection for auxprop_lookup */
537 if ( !auxvals[PROP_CONN].values ) {
538 names[0] = slap_propnames[PROP_CONN];
540 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
543 /* Already been here? */
544 if ( auxvals[which].values )
547 /* Normally we require an authzID to have a u: or dn: prefix.
548 * However, SASL frequently gives us an authzID that is just
549 * an exact copy of the authcID, without a prefix. We need to
550 * detect and allow this condition. If SASL calls canonicalize
551 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
552 * But if it's broken into two calls, we need to remember the
553 * authcID so that we can compare the authzID later. We store
554 * the authcID temporarily in conn->c_sasl_dn. We necessarily
555 * finish Canonicalizing before Authorizing, so there is no
556 * conflict with slap_sasl_authorize's use of this temp var.
558 * The SASL EXTERNAL mech is backwards from all the other mechs,
559 * it does authzID before the authcID. If we see that authzID
560 * has already been done, don't do anything special with authcID.
562 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
563 conn->c_sasl_dn.bv_val = (char *) in;
564 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
565 rc = strcmp( in, conn->c_sasl_dn.bv_val );
566 conn->c_sasl_dn.bv_val = NULL;
567 /* They were equal, no work needed */
568 if ( !rc ) goto done;
571 bvin.bv_val = (char *)in;
573 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
574 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
575 if ( rc != LDAP_SUCCESS ) {
576 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
580 names[0] = slap_propnames[which];
583 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
585 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
586 conn ? conn->c_connid : -1, names[0]+1,
587 dn.bv_val ? dn.bv_val : "<EMPTY>" );
590 AC_MEMCPY( out, in, inlen );
602 char *requested_user,
606 const char *def_realm,
608 struct propctx *props)
610 Connection *conn = (Connection *)context;
611 struct propval auxvals[3];
612 struct berval authcDN, authzDN = BER_BVNULL;
615 /* Simple Binds don't support proxy authorization, ignore it */
616 if ( !conn->c_sasl_bindop ||
617 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
619 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
620 "authcid=\"%s\" authzid=\"%s\"\n",
621 conn ? conn->c_connid : -1, auth_identity, requested_user );
622 if ( conn->c_sasl_dn.bv_val ) {
623 ch_free( conn->c_sasl_dn.bv_val );
624 BER_BVZERO( &conn->c_sasl_dn );
628 prop_getnames( props, slap_propnames+1, auxvals );
630 /* Should not happen */
631 if ( !auxvals[0].values ) {
632 sasl_seterror( sconn, 0, "invalid authcid" );
636 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
637 conn->c_sasl_dn = authcDN;
639 /* Nothing to do if no authzID was given */
640 if ( !auxvals[1].name || !auxvals[1].values ) {
644 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
646 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
647 if ( rc != LDAP_SUCCESS ) {
648 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
649 "proxy authorization disallowed (%d)\n",
650 (long) (conn ? conn->c_connid : -1), rc, 0 );
652 sasl_seterror( sconn, 0, "not authorized" );
653 ch_free( authzDN.bv_val );
657 /* FIXME: we need yet another dup because slap_sasl_getdn()
658 * is using the bind operation slab */
659 if ( conn->c_sasl_bindop ) {
660 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
661 slap_sl_free( authzDN.bv_val,
662 conn->c_sasl_bindop->o_tmpmemctx );
665 conn->c_sasl_authz_dn = authzDN;
669 if (conn->c_sasl_bindop) {
670 Statslog( LDAP_DEBUG_STATS,
671 "conn=%lu op=%lu BIND authcid=\"%s\" authzid=\"%s\"\n",
672 conn->c_connid, conn->c_sasl_bindop->o_opid,
673 auth_identity, requested_user, 0);
676 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
677 " proxy authorization allowed authzDN=\"%s\"\n",
678 (long) (conn ? conn->c_connid : -1),
679 authzDN.bv_val ? authzDN.bv_val : "", 0 );
691 struct berval authcDN, authzDN = BER_BVNULL;
693 Connection *conn = context;
695 struct berval bvauthcid, bvauthzid;
698 if ( conn->c_sasl_dn.bv_val ) {
699 ch_free( conn->c_sasl_dn.bv_val );
700 BER_BVZERO( &conn->c_sasl_dn );
703 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
704 "authcid=\"%s\" authzid=\"%s\"\n",
705 (long) (conn ? conn->c_connid : -1),
706 authcid ? authcid : "<empty>",
707 authzid ? authzid : "<empty>" );
709 /* Figure out how much data we have for the dn */
710 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
711 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
712 Debug(LDAP_DEBUG_TRACE,
713 "authorize: getprop(REALM) failed!\n", 0,0,0);
714 *errstr = "Could not extract realm";
718 /* Convert the identities to DN's. If no authzid was given, client will
719 be bound as the DN matching their username */
720 bvauthcid.bv_val = authcid;
721 bvauthcid.bv_len = authcid ? strlen( authcid ) : 0;
722 rc = slap_sasl_getdn( conn, NULL, &bvauthcid, realm,
723 &authcDN, SLAP_GETDN_AUTHCID );
724 if( rc != LDAP_SUCCESS ) {
725 *errstr = ldap_err2string( rc );
728 conn->c_sasl_dn = authcDN;
729 if( ( authzid == NULL ) || !strcmp( authcid, authzid ) ) {
730 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
731 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
736 bvauthzid.bv_val = authzid;
737 bvauthzid.bv_len = authzid ? strlen( authzid ) : 0;
738 rc = slap_sasl_getdn( conn, NULL, &bvauthzid, realm,
739 &authzDN, SLAP_GETDN_AUTHZID );
740 if( rc != LDAP_SUCCESS ) {
741 *errstr = ldap_err2string( rc );
745 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
747 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
748 "proxy authorization disallowed (%d)\n",
749 (long) (conn ? conn->c_connid : -1), rc, 0 );
751 *errstr = "not authorized";
752 ch_free( authzDN.bv_val );
756 /* FIXME: we need yet another dup because slap_sasl_getdn()
757 * is using the bind operation slab */
758 if ( conn->c_sasl_bindop ) {
759 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
760 slap_sl_free( authzDN.bv_val,
761 conn->c_sasl_bindop->o_tmpmemctx );
764 conn->c_sasl_authz_dn = authzDN;
768 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
769 " authorization allowed authzDN=\"%s\"\n",
770 (long) (conn ? conn->c_connid : -1),
771 authzDN.bv_val ? authzDN.bv_val : "", 0 );
773 if ( conn->c_sasl_bindop ) {
774 Statslog( LDAP_DEBUG_STATS,
775 "conn=%lu op=%lu BIND authcid=\"%s\" authzid=\"%s\"\n",
776 conn->c_connid, conn->c_sasl_bindop->o_opid,
777 authcid, authzid ? authzid : "", 0);
783 #endif /* SASL_VERSION_MAJOR >= 2 */
786 slap_sasl_err2ldap( int saslerr )
795 rc = LDAP_SASL_BIND_IN_PROGRESS;
804 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
807 rc = LDAP_INVALID_CREDENTIALS;
810 rc = LDAP_INSUFFICIENT_ACCESS;
814 rc = LDAP_INAPPROPRIATE_AUTH;
826 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
829 const struct berval *sc,
830 const struct berval * passwd,
831 const struct berval * cred,
836 void *ctx, *sconn = NULL;
838 for( i=0; i<cred->bv_len; i++) {
839 if(cred->bv_val[i] == '\0') {
840 return LUTIL_PASSWD_ERR; /* NUL character in password */
844 if( cred->bv_val[i] != '\0' ) {
845 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
848 for( i=0; i<passwd->bv_len; i++) {
849 if(passwd->bv_val[i] == '\0') {
850 return LUTIL_PASSWD_ERR; /* NUL character in password */
854 if( passwd->bv_val[i] != '\0' ) {
855 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
858 rtn = LUTIL_PASSWD_ERR;
860 ctx = ldap_pvt_thread_pool_context();
861 ldap_pvt_thread_pool_getkey( ctx, slap_sasl_bind, &sconn, NULL );
863 if( sconn != NULL ) {
865 # if SASL_VERSION_MAJOR < 2
866 sc = sasl_checkpass( sconn,
867 passwd->bv_val, passwd->bv_len,
868 cred->bv_val, cred->bv_len,
871 sc = sasl_checkpass( sconn,
872 passwd->bv_val, passwd->bv_len,
873 cred->bv_val, cred->bv_len );
875 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
880 #endif /* SLAPD_SPASSWD */
882 #endif /* HAVE_CYRUS_SASL */
884 int slap_sasl_init( void )
886 #ifdef HAVE_CYRUS_SASL
888 static sasl_callback_t server_callbacks[] = {
889 { SASL_CB_LOG, &slap_sasl_log, NULL },
890 { SASL_CB_LIST_END, NULL, NULL }
893 #ifdef HAVE_SASL_VERSION
894 /* stringify the version number, sasl.h doesn't do it for us */
895 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
896 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
897 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
900 sasl_version( NULL, &rc );
901 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
902 (rc & 0xffff) < SASL_VERSION_STEP)
904 char version[sizeof("xxx.xxx.xxxxx")];
905 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
907 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
908 " expected " SASL_VERSION_STRING ","
909 " got %s\n", version, 0, 0 );
914 /* SASL 2 does its own memory management internally */
915 #if SASL_VERSION_MAJOR < 2
924 ldap_pvt_sasl_mutex_new,
925 ldap_pvt_sasl_mutex_lock,
926 ldap_pvt_sasl_mutex_unlock,
927 ldap_pvt_sasl_mutex_dispose );
929 #if SASL_VERSION_MAJOR >= 2
930 generic_filter.f_desc = slap_schema.si_ad_objectClass;
932 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
933 if( rc != SASL_OK ) {
934 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
939 /* should provide callbacks for logging */
940 /* server name should be configurable */
941 rc = sasl_server_init( server_callbacks, "slapd" );
943 if( rc != SASL_OK ) {
944 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
946 #if SASL_VERSION_MAJOR < 2
947 /* A no-op used to make sure we linked with Cyrus 1.5 */
948 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
955 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
958 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
961 /* default security properties */
962 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
963 sasl_secprops.max_ssf = INT_MAX;
964 sasl_secprops.maxbufsize = 65536;
965 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
971 int slap_sasl_destroy( void )
973 #ifdef HAVE_CYRUS_SASL
982 int slap_sasl_open( Connection *conn, int reopen )
984 int sc = LDAP_SUCCESS;
985 #ifdef HAVE_CYRUS_SASL
988 sasl_conn_t *ctx = NULL;
989 sasl_callback_t *session_callbacks;
991 #if SASL_VERSION_MAJOR >= 2
992 char *ipremoteport = NULL, *iplocalport = NULL;
995 assert( conn->c_sasl_authctx == NULL );
998 assert( conn->c_sasl_extra == NULL );
1001 #if SASL_VERSION_MAJOR >= 2
1002 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1004 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1006 if( session_callbacks == NULL ) {
1007 Debug( LDAP_DEBUG_ANY,
1008 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1011 conn->c_sasl_extra = session_callbacks;
1013 session_callbacks[cb=0].id = SASL_CB_LOG;
1014 session_callbacks[cb].proc = &slap_sasl_log;
1015 session_callbacks[cb++].context = conn;
1017 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1018 session_callbacks[cb].proc = &slap_sasl_authorize;
1019 session_callbacks[cb++].context = conn;
1021 #if SASL_VERSION_MAJOR >= 2
1022 session_callbacks[cb].id = SASL_CB_CANON_USER;
1023 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1024 session_callbacks[cb++].context = conn;
1027 session_callbacks[cb].id = SASL_CB_LIST_END;
1028 session_callbacks[cb].proc = NULL;
1029 session_callbacks[cb++].context = NULL;
1031 session_callbacks = conn->c_sasl_extra;
1034 conn->c_sasl_layers = 0;
1036 if( global_host == NULL ) {
1037 global_host = ldap_pvt_get_fqdn( NULL );
1040 /* create new SASL context */
1041 #if SASL_VERSION_MAJOR >= 2
1042 if ( conn->c_sock_name.bv_len != 0 &&
1043 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1046 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1047 /* Convert IPv6 addresses to address;port syntax. */
1048 p = strrchr( iplocalport, ' ' );
1049 /* Convert IPv4 addresses to address;port syntax. */
1050 if ( p == NULL ) p = strchr( iplocalport, ':' );
1055 if ( conn->c_peer_name.bv_len != 0 &&
1056 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1059 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1060 /* Convert IPv6 addresses to address;port syntax. */
1061 p = strrchr( ipremoteport, ' ' );
1062 /* Convert IPv4 addresses to address;port syntax. */
1063 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1068 sc = sasl_server_new( "ldap", global_host, global_realm,
1069 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1070 if ( iplocalport != NULL ) {
1071 ch_free( iplocalport );
1073 if ( ipremoteport != NULL ) {
1074 ch_free( ipremoteport );
1077 sc = sasl_server_new( "ldap", global_host, global_realm,
1078 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1081 if( sc != SASL_OK ) {
1082 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1088 conn->c_sasl_authctx = ctx;
1090 if( sc == SASL_OK ) {
1091 sc = sasl_setprop( ctx,
1092 SASL_SEC_PROPS, &sasl_secprops );
1094 if( sc != SASL_OK ) {
1095 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1098 slap_sasl_close( conn );
1103 sc = slap_sasl_err2ldap( sc );
1105 #elif defined(SLAP_BUILTIN_SASL)
1106 /* built-in SASL implementation */
1107 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1108 if( ctx == NULL ) return -1;
1110 ctx->sc_external_ssf = 0;
1111 BER_BVZERO( &ctx->sc_external_id );
1113 conn->c_sasl_authctx = ctx;
1119 int slap_sasl_external(
1122 struct berval *auth_id )
1124 #if SASL_VERSION_MAJOR >= 2
1126 sasl_conn_t *ctx = conn->c_sasl_authctx;
1128 if ( ctx == NULL ) {
1129 return LDAP_UNAVAILABLE;
1132 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1134 if ( sc != SASL_OK ) {
1138 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1139 auth_id ? auth_id->bv_val : NULL );
1141 if ( sc != SASL_OK ) {
1145 #elif defined(HAVE_CYRUS_SASL)
1147 sasl_conn_t *ctx = conn->c_sasl_authctx;
1148 sasl_external_properties_t extprops;
1150 if ( ctx == NULL ) {
1151 return LDAP_UNAVAILABLE;
1154 memset( &extprops, '\0', sizeof(extprops) );
1156 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1158 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1159 (void *) &extprops );
1161 if ( sc != SASL_OK ) {
1164 #elif defined(SLAP_BUILTIN_SASL)
1165 /* built-in SASL implementation */
1166 SASL_CTX *ctx = conn->c_sasl_authctx;
1167 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1169 ctx->sc_external_ssf = ssf;
1171 ctx->sc_external_id = *auth_id;
1172 BER_BVZERO( auth_id );
1174 BER_BVZERO( &ctx->sc_external_id );
1178 return LDAP_SUCCESS;
1181 int slap_sasl_reset( Connection *conn )
1183 return LDAP_SUCCESS;
1186 char ** slap_sasl_mechs( Connection *conn )
1188 char **mechs = NULL;
1190 #ifdef HAVE_CYRUS_SASL
1191 sasl_conn_t *ctx = conn->c_sasl_authctx;
1193 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1197 SASL_CONST char *mechstr;
1199 sc = sasl_listmech( ctx,
1200 NULL, NULL, ",", NULL,
1201 &mechstr, NULL, NULL );
1203 if( sc != SASL_OK ) {
1204 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1210 mechs = ldap_str2charray( mechstr, "," );
1212 #if SASL_VERSION_MAJOR < 2
1216 #elif defined(SLAP_BUILTIN_SASL)
1217 /* builtin SASL implementation */
1218 SASL_CTX *ctx = conn->c_sasl_authctx;
1219 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1220 /* should check ssf */
1221 mechs = ldap_str2charray( "EXTERNAL", "," );
1228 int slap_sasl_close( Connection *conn )
1230 #ifdef HAVE_CYRUS_SASL
1231 sasl_conn_t *ctx = conn->c_sasl_authctx;
1234 sasl_dispose( &ctx );
1236 if ( conn->c_sasl_sockctx &&
1237 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1239 ctx = conn->c_sasl_sockctx;
1240 sasl_dispose( &ctx );
1243 conn->c_sasl_authctx = NULL;
1244 conn->c_sasl_sockctx = NULL;
1245 conn->c_sasl_done = 0;
1247 free( conn->c_sasl_extra );
1248 conn->c_sasl_extra = NULL;
1250 #elif defined(SLAP_BUILTIN_SASL)
1251 SASL_CTX *ctx = conn->c_sasl_authctx;
1253 if( ctx->sc_external_id.bv_val ) {
1254 free( ctx->sc_external_id.bv_val );
1255 BER_BVZERO( &ctx->sc_external_id );
1258 conn->c_sasl_authctx = NULL;
1262 return LDAP_SUCCESS;
1265 int slap_sasl_bind( Operation *op, SlapReply *rs )
1267 #ifdef HAVE_CYRUS_SASL
1268 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1269 struct berval response;
1270 unsigned reslen = 0;
1273 Debug(LDAP_DEBUG_ARGS,
1274 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1275 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1276 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1277 op->o_conn->c_sasl_bind_mech.bv_val,
1278 op->orb_cred.bv_len );
1281 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1282 "SASL unavailable on this session" );
1286 #if SASL_VERSION_MAJOR >= 2
1287 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1288 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1289 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1290 sasl_server_step( ctx, cred, clen, resp, rlen )
1292 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1293 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1294 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1295 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1298 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1299 /* If we already authenticated once, must use a new context */
1300 if ( op->o_conn->c_sasl_done ) {
1302 const char *authid = NULL;
1303 #if SASL_VERSION_MAJOR >= 2
1304 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1305 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1306 if ( authid ) authid = ch_strdup( authid );
1308 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1309 sasl_dispose( &ctx );
1311 op->o_conn->c_sasl_authctx = NULL;
1313 slap_sasl_open( op->o_conn, 1 );
1314 ctx = op->o_conn->c_sasl_authctx;
1315 #if SASL_VERSION_MAJOR >= 2
1317 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1318 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1319 ch_free( (char *)authid );
1324 op->o_conn->c_sasl_bind_mech.bv_val,
1325 op->orb_cred.bv_val, op->orb_cred.bv_len,
1326 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1330 op->orb_cred.bv_val, op->orb_cred.bv_len,
1331 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1334 response.bv_len = reslen;
1336 if ( sc == SASL_OK ) {
1337 sasl_ssf_t *ssf = NULL;
1339 op->orb_edn = op->o_conn->c_sasl_dn;
1340 BER_BVZERO( &op->o_conn->c_sasl_dn );
1341 op->o_conn->c_sasl_done = 1;
1343 rs->sr_err = LDAP_SUCCESS;
1345 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1346 op->orb_ssf = ssf ? *ssf : 0;
1350 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1351 op->o_conn->c_sasl_layers++;
1353 /* If there's an old layer, set sockctx to NULL to
1354 * tell connection_read() to wait for us to finish.
1355 * Otherwise there is a race condition: we have to
1356 * send the Bind response using the old security
1357 * context and then remove it before reading any
1360 if ( op->o_conn->c_sasl_sockctx ) {
1361 ctx = op->o_conn->c_sasl_sockctx;
1362 op->o_conn->c_sasl_sockctx = NULL;
1364 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1366 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1369 /* Must send response using old security layer */
1370 if (response.bv_len) rs->sr_sasldata = &response;
1371 send_ldap_sasl( op, rs );
1373 /* Now dispose of the old security layer.
1376 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1377 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1378 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1379 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1380 sasl_dispose( &ctx );
1382 } else if ( sc == SASL_CONTINUE ) {
1383 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1384 rs->sr_sasldata = &response;
1385 send_ldap_sasl( op, rs );
1388 #if SASL_VERSION_MAJOR >= 2
1389 rs->sr_text = sasl_errdetail( ctx );
1391 rs->sr_err = slap_sasl_err2ldap( sc ),
1392 send_ldap_result( op, rs );
1395 #if SASL_VERSION_MAJOR < 2
1396 if( response.bv_len ) {
1397 ch_free( response.bv_val );
1401 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1403 #elif defined(SLAP_BUILTIN_SASL)
1404 /* built-in SASL implementation */
1405 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1407 if ( ctx == NULL ) {
1408 send_ldap_error( op, rs, LDAP_OTHER,
1409 "Internal SASL Error" );
1411 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1414 if( op->orb_cred.bv_len ) {
1415 rs->sr_text = "proxy authorization not support";
1416 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1417 send_ldap_result( op, rs );
1420 op->orb_edn = ctx->sc_external_id;
1421 rs->sr_err = LDAP_SUCCESS;
1422 rs->sr_sasldata = NULL;
1423 send_ldap_sasl( op, rs );
1427 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1428 "requested SASL mechanism not supported" );
1431 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1432 "SASL not supported" );
1438 char* slap_sasl_secprops( const char *in )
1440 #ifdef HAVE_CYRUS_SASL
1441 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1443 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1445 return "SASL not supported";
1449 void slap_sasl_secprops_unparse( struct berval *bv )
1451 #ifdef HAVE_CYRUS_SASL
1452 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1456 #ifdef HAVE_CYRUS_SASL
1458 slap_sasl_setpass( Operation *op, SlapReply *rs )
1460 struct berval id = BER_BVNULL; /* needs to come from connection */
1461 struct berval new = BER_BVNULL;
1462 struct berval old = BER_BVNULL;
1464 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1466 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1467 (SASL_CONST void **)&id.bv_val );
1469 if( rs->sr_err != SASL_OK ) {
1470 rs->sr_text = "unable to retrieve SASL username";
1471 rs->sr_err = LDAP_OTHER;
1475 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1476 id.bv_val ? id.bv_val : "", 0, 0 );
1478 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1479 NULL, &old, &new, &rs->sr_text );
1481 if( rs->sr_err != LDAP_SUCCESS ) {
1485 if( new.bv_len == 0 ) {
1486 slap_passwd_generate(&new);
1488 if( new.bv_len == 0 ) {
1489 rs->sr_text = "password generation failed.";
1490 rs->sr_err = LDAP_OTHER;
1494 rs->sr_rspdata = slap_passwd_return( &new );
1497 #if SASL_VERSION_MAJOR < 2
1498 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1499 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1501 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1502 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1503 if( rs->sr_err != SASL_OK ) {
1504 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1507 switch(rs->sr_err) {
1509 rs->sr_err = LDAP_SUCCESS;
1519 rs->sr_err = LDAP_OTHER;
1525 #endif /* HAVE_CYRUS_SASL */
1527 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1528 * string returned in *dn is in its own allocated memory, and must be free'd
1529 * by the calling process. -Mark Adamson, Carnegie Mellon
1531 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1532 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1539 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1540 char *user_realm, struct berval *dn, int flags )
1542 int rc, is_dn = SET_NONE, do_norm = 1;
1543 struct berval dn2, *mech;
1545 assert( conn != NULL );
1546 assert( id != NULL );
1548 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1550 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1551 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1552 (unsigned long) id->bv_len ) );
1555 op = conn->c_sasl_bindop;
1557 assert( op != NULL );
1561 if ( !BER_BVISNULL( id ) ) {
1562 /* Blatantly anonymous ID */
1563 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1565 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1566 return( LDAP_SUCCESS );
1570 /* FIXME: if empty, should we stop? */
1571 BER_BVSTR( id, "" );
1574 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1575 mech = &conn->c_sasl_bind_mech;
1577 mech = &conn->c_authmech;
1580 /* An authcID needs to be converted to authzID form. Set the
1581 * values directly into *dn; they will be normalized later. (and
1582 * normalizing always makes a new copy.) An ID from a TLS certificate
1583 * is already normalized, so copy it and skip normalization.
1585 if( flags & SLAP_GETDN_AUTHCID ) {
1586 if( bvmatch( mech, &ext_bv )) {
1587 /* EXTERNAL DNs are already normalized */
1588 assert( !BER_BVISNULL( id ) );
1592 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1595 /* convert to u:<username> form */
1601 if( is_dn == SET_NONE ) {
1602 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1604 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1605 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1607 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1609 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1610 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1614 /* No other possibilities from here */
1615 if( is_dn == SET_NONE ) {
1617 return( LDAP_INAPPROPRIATE_AUTH );
1620 /* Username strings */
1621 if( is_dn == SET_U ) {
1622 /* ITS#3419: values may need escape */
1624 LDAPAVA *RDNs[ 4 ][ 2 ];
1629 DN[ irdn ] = RDNs[ irdn ];
1630 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1631 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1632 AVAs[ irdn ].la_value = *dn;
1633 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1634 AVAs[ irdn ].la_private = NULL;
1635 RDNs[ irdn ][ 1 ] = NULL;
1637 if ( user_realm && *user_realm ) {
1639 DN[ irdn ] = RDNs[ irdn ];
1640 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1641 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1642 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1643 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1644 AVAs[ irdn ].la_private = NULL;
1645 RDNs[ irdn ][ 1 ] = NULL;
1648 if ( !BER_BVISNULL( mech ) ) {
1650 DN[ irdn ] = RDNs[ irdn ];
1651 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1652 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1653 AVAs[ irdn ].la_value = *mech;
1654 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1655 AVAs[ irdn ].la_private = NULL;
1656 RDNs[ irdn ][ 1 ] = NULL;
1660 DN[ irdn ] = RDNs[ irdn ];
1661 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1662 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1663 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1664 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1665 AVAs[ irdn ].la_private = NULL;
1666 RDNs[ irdn ][ 1 ] = NULL;
1671 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1673 if ( rc != LDAP_SUCCESS ) {
1678 Debug( LDAP_DEBUG_TRACE,
1679 "slap_sasl_getdn: u:id converted to %s\n",
1684 /* Dup the DN in any case, so we don't risk
1685 * leaks or dangling pointers later,
1686 * and the DN value is '\0' terminated */
1687 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1688 dn->bv_val = dn2.bv_val;
1691 /* All strings are in DN form now. Normalize if needed. */
1693 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1695 /* User DNs were constructed above and must be freed now */
1696 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1698 if ( rc != LDAP_SUCCESS ) {
1705 /* Run thru regexp */
1706 slap_sasl2dn( op, dn, &dn2, flags );
1707 if( !BER_BVISNULL( &dn2 ) ) {
1708 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1710 Debug( LDAP_DEBUG_TRACE,
1711 "slap_sasl_getdn: dn:id converted to %s\n",
1715 return( LDAP_SUCCESS );