2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 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>
35 #ifdef HAVE_CYRUS_SASL
36 # ifdef HAVE_SASL_SASL_H
37 # include <sasl/sasl.h>
38 # include <sasl/saslplug.h>
41 # include <saslplug.h>
44 # define SASL_CONST const
46 #define SASL_VERSION_FULL ((SASL_VERSION_MAJOR << 16) |\
47 (SASL_VERSION_MINOR << 8) | SASL_VERSION_STEP)
49 static sasl_security_properties_t sasl_secprops;
50 #elif defined( SLAP_BUILTIN_SASL )
52 * built-in SASL implementation
53 * only supports EXTERNAL
55 typedef struct sasl_ctx {
56 slap_ssf_t sc_external_ssf;
57 struct berval sc_external_id;
64 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
66 #ifdef HAVE_CYRUS_SASL
74 Connection *conn = context;
78 if ( message == NULL ) {
84 level = LDAP_DEBUG_NONE;
88 level = LDAP_DEBUG_ANY;
92 level = LDAP_DEBUG_ANY;
96 level = LDAP_DEBUG_TRACE;
100 level = LDAP_DEBUG_TRACE;
104 level = LDAP_DEBUG_TRACE;
108 level = LDAP_DEBUG_TRACE;
112 level = LDAP_DEBUG_TRACE;
113 label = "Password Trace";
116 return SASL_BADPARAM;
119 Debug( level, "SASL [conn=%ld] %s: %s\n",
120 conn ? conn->c_connid: -1,
127 static const char *slap_propnames[] = {
128 "*slapConn", "*slapAuthcDNlen", "*slapAuthcDN",
129 "*slapAuthzDNlen", "*slapAuthzDN", NULL };
131 static Filter generic_filter = { LDAP_FILTER_PRESENT, { 0 }, NULL };
132 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
134 #define SLAP_SASL_PROP_CONN 0
135 #define SLAP_SASL_PROP_AUTHCLEN 1
136 #define SLAP_SASL_PROP_AUTHC 2
137 #define SLAP_SASL_PROP_AUTHZLEN 3
138 #define SLAP_SASL_PROP_AUTHZ 4
139 #define SLAP_SASL_PROP_COUNT 5 /* Number of properties we used */
141 typedef struct lookup_info {
143 const struct propval *list;
144 sasl_server_params_t *sparams;
147 static slap_response sasl_ap_lookup;
149 static struct berval sc_cleartext = BER_BVC("{CLEARTEXT}");
152 sasl_ap_lookup( Operation *op, SlapReply *rs )
155 AttributeDescription *ad;
159 lookup_info *sl = (lookup_info *)op->o_callback->sc_private;
161 if (rs->sr_type != REP_SEARCH) return 0;
163 for( i = 0; sl->list[i].name; i++ ) {
164 const char *name = sl->list[i].name;
166 if ( name[0] == '*' ) {
167 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
168 /* Skip our private properties */
169 if ( !strcmp( name, slap_propnames[0] )) {
170 i += SLAP_SASL_PROP_COUNT - 1;
174 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
177 if ( sl->list[i].values ) {
178 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
181 rc = slap_str2ad( name, &ad, &text );
182 if ( rc != LDAP_SUCCESS ) {
183 Debug( LDAP_DEBUG_TRACE,
184 "slap_ap_lookup: str2ad(%s): %s\n", name, text, 0 );
188 /* If it's the rootdn and a rootpw was present, we already set
189 * it so don't override it here.
191 if ( ad == slap_schema.si_ad_userPassword && sl->list[i].values &&
192 be_isroot_dn( op->o_bd, &op->o_req_ndn ))
195 a = attr_find( rs->sr_entry->e_attrs, ad );
197 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
200 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
201 sl->sparams->utils->prop_erase( sl->sparams->propctx,
204 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
205 /* ITS#3846 don't give hashed passwords to SASL */
206 if ( ad == slap_schema.si_ad_userPassword &&
207 bv->bv_val[0] == '{' /*}*/ )
209 if ( lutil_passwd_scheme( bv->bv_val ) ) {
210 /* If it's not a recognized scheme, just assume it's
211 * a cleartext password that happened to include brackets.
213 * If it's a recognized scheme, skip this value, unless the
214 * scheme is {CLEARTEXT}. In that case, skip over the
215 * scheme name and use the remainder. If there is nothing
216 * past the scheme name, skip this value.
218 #ifdef SLAPD_CLEARTEXT
219 if ( !ber_bvstrcasecmp( bv, &sc_cleartext ) ) {
221 cbv.bv_len = bv->bv_len - sc_cleartext.bv_len;
222 if ( cbv.bv_len > 0 ) {
223 cbv.bv_val = bv->bv_val + sc_cleartext.bv_len;
224 sl->sparams->utils->prop_set( sl->sparams->propctx,
225 sl->list[i].name, cbv.bv_val, cbv.bv_len );
232 sl->sparams->utils->prop_set( sl->sparams->propctx,
233 sl->list[i].name, bv->bv_val, bv->bv_len );
242 sasl_server_params_t *sparams,
249 Connection *conn = NULL;
252 sl.list = sparams->utils->prop_get( sparams->propctx );
253 sl.sparams = sparams;
256 /* Find our DN and conn first */
257 for( i = 0; sl.list[i].name; i++ ) {
258 if ( sl.list[i].name[0] == '*' ) {
259 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
260 if ( sl.list[i].values && sl.list[i].values[0] )
261 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
264 if ( flags & SASL_AUXPROP_AUTHZID ) {
265 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZLEN] )) {
266 if ( sl.list[i].values && sl.list[i].values[0] )
267 AC_MEMCPY( &op.o_req_ndn.bv_len, sl.list[i].values[0],
268 sizeof( op.o_req_ndn.bv_len ) );
269 } else if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZ] )) {
270 if ( sl.list[i].values )
271 op.o_req_ndn.bv_val = (char *)sl.list[i].values[0];
276 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
277 if ( sl.list[i].values && sl.list[i].values[0] )
278 AC_MEMCPY( &op.o_req_ndn.bv_len, sl.list[i].values[0],
279 sizeof( op.o_req_ndn.bv_len ) );
280 } else if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
281 if ( sl.list[i].values ) {
282 op.o_req_ndn.bv_val = (char *)sl.list[i].values[0];
283 if ( !(flags & SASL_AUXPROP_AUTHZID) )
290 /* Now see what else needs to be fetched */
291 for( i = 0; sl.list[i].name; i++ ) {
292 const char *name = sl.list[i].name;
294 if ( name[0] == '*' ) {
295 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
296 /* Skip our private properties */
297 if ( !strcmp( name, slap_propnames[0] )) {
298 i += SLAP_SASL_PROP_COUNT - 1;
302 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
305 if ( sl.list[i].values ) {
306 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
313 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
317 op.o_bd = select_backend( &op.o_req_ndn, 1 );
320 /* For rootdn, see if we can use the rootpw */
321 if ( be_isroot_dn( op.o_bd, &op.o_req_ndn ) &&
322 !BER_BVISEMPTY( &op.o_bd->be_rootpw )) {
323 struct berval cbv = BER_BVNULL;
325 /* If there's a recognized scheme, see if it's CLEARTEXT */
326 if ( lutil_passwd_scheme( op.o_bd->be_rootpw.bv_val )) {
327 if ( !strncasecmp( op.o_bd->be_rootpw.bv_val,
328 sc_cleartext.bv_val, sc_cleartext.bv_len )) {
330 /* If it's CLEARTEXT, skip past scheme spec */
331 cbv.bv_len = op.o_bd->be_rootpw.bv_len -
334 cbv.bv_val = op.o_bd->be_rootpw.bv_val +
338 /* No scheme, use the whole value */
340 cbv = op.o_bd->be_rootpw;
342 if ( !BER_BVISEMPTY( &cbv )) {
343 for( i = 0; sl.list[i].name; i++ ) {
344 const char *name = sl.list[i].name;
346 if ( name[0] == '*' ) {
347 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
349 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
352 if ( !strcasecmp(name,"userPassword") ) {
353 sl.sparams->utils->prop_set( sl.sparams->propctx,
354 sl.list[i].name, cbv.bv_val, cbv.bv_len );
361 if ( op.o_bd->be_search ) {
362 SlapReply rs = {REP_RESULT};
363 op.o_hdr = conn->c_sasl_bindop->o_hdr;
364 op.o_tag = LDAP_REQ_SEARCH;
365 op.o_dn = conn->c_ndn;
366 op.o_ndn = conn->c_ndn;
368 slap_op_time( &op.o_time, &op.o_tincr );
369 op.o_do_not_cache = 1;
370 op.o_is_auth_check = 1;
371 op.o_req_dn = op.o_req_ndn;
372 op.ors_scope = LDAP_SCOPE_BASE;
373 op.ors_deref = LDAP_DEREF_NEVER;
374 op.ors_tlimit = SLAP_NO_LIMIT;
376 op.ors_filter = &generic_filter;
377 op.ors_filterstr = generic_filterstr;
378 /* FIXME: we want all attributes, right? */
381 op.o_bd->be_search( &op, &rs );
387 #if SASL_VERSION_FULL >= 0x020110
391 sasl_server_params_t *sparams,
392 struct propctx *prctx,
398 SlapReply rs = {REP_RESULT};
400 Connection *conn = NULL;
401 const struct propval *pr;
402 Modifications *modlist = NULL, **modtail = &modlist, *mod;
403 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
404 char textbuf[SLAP_TEXT_BUFLEN];
406 size_t textlen = sizeof(textbuf);
408 /* just checking if we are enabled */
409 if (!prctx) return SASL_OK;
411 if (!sparams || !user) return SASL_BADPARAM;
413 pr = sparams->utils->prop_get( sparams->propctx );
415 /* Find our DN and conn first */
416 for( i = 0; pr[i].name; i++ ) {
417 if ( pr[i].name[0] == '*' ) {
418 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
419 if ( pr[i].values && pr[i].values[0] )
420 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
423 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
424 if ( pr[i].values && pr[i].values[0] )
425 AC_MEMCPY( &op.o_req_ndn.bv_len, pr[i].values[0],
426 sizeof( op.o_req_ndn.bv_len ) );
427 } else if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
429 op.o_req_ndn.bv_val = (char *)pr[i].values[0];
433 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
435 op.o_bd = select_backend( &op.o_req_ndn, 1 );
437 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
439 pr = sparams->utils->prop_get( prctx );
440 if (!pr) return SASL_BADPARAM;
442 for (i=0; pr[i].name; i++);
443 if (!i) return SASL_BADPARAM;
445 for (i=0; pr[i].name; i++) {
446 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
447 mod->sml_op = LDAP_MOD_REPLACE;
449 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
450 mod->sml_numvals = pr[i].nvalues;
451 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
452 sizeof(struct berval));
453 for (j=0; j<pr[i].nvalues; j++) {
454 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
456 BER_BVZERO( &mod->sml_values[j] );
457 mod->sml_nvalues = NULL;
458 mod->sml_desc = NULL;
460 modtail = &mod->sml_next;
464 rc = slap_mods_check( &op, modlist, &text, textbuf, textlen, NULL );
466 if ( rc == LDAP_SUCCESS ) {
467 rc = slap_mods_no_user_mod_check( &op, modlist,
468 &text, textbuf, textlen );
470 if ( rc == LDAP_SUCCESS ) {
471 if ( conn->c_sasl_bindop ) {
472 op.o_hdr = conn->c_sasl_bindop->o_hdr;
475 memset( &oph, 0, sizeof(oph) );
476 operation_fake_init( conn, &op, ldap_pvt_thread_pool_context(), 0 );
478 op.o_tag = LDAP_REQ_MODIFY;
479 op.o_ndn = op.o_req_ndn;
481 slap_op_time( &op.o_time, &op.o_tincr );
482 op.o_do_not_cache = 1;
483 op.o_is_auth_check = 1;
484 op.o_req_dn = op.o_req_ndn;
485 op.orm_modlist = modlist;
487 rc = op.o_bd->be_modify( &op, &rs );
490 slap_mods_free( modlist, 1 );
491 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
493 #endif /* SASL_VERSION_FULL >= 2.1.16 */
495 static sasl_auxprop_plug_t slap_auxprop_plugin = {
498 NULL, /* glob_context */
499 NULL, /* auxprop_free */
502 #if SASL_VERSION_FULL >= 0x020110
503 slap_auxprop_store /* the declaration of this member changed
504 * in cyrus SASL from 2.1.15 to 2.1.16 */
512 const sasl_utils_t *utils,
515 sasl_auxprop_plug_t **plug,
516 const char *plugname)
518 if ( !out_version || !plug ) return SASL_BADPARAM;
520 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
522 *out_version = SASL_AUXPROP_PLUG_VERSION;
523 *plug = &slap_auxprop_plugin;
527 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
528 * auxiliary property, so that we can refer to it in sasl_authorize
529 * without interfering with anything else. Also, the SASL username
530 * buffer is constrained to 256 characters, and our DNs could be
531 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
534 slap_sasl_canonicalize(
540 const char *user_realm,
545 Connection *conn = (Connection *)context;
546 struct propctx *props = sasl_auxprop_getctx( sconn );
547 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
550 const char *names[2];
555 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
556 conn ? conn->c_connid : -1,
557 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
558 in ? in : "<empty>");
560 /* If name is too big, just truncate. We don't care, we're
561 * using DNs, not the usernames.
563 if ( inlen > out_max )
566 /* This is a Simple Bind using SPASSWD. That means the in-directory
567 * userPassword of the Binding user already points at SASL, so it
568 * cannot be used to actually satisfy a password comparison. Just
569 * ignore it, some other mech will process it.
571 if ( !conn->c_sasl_bindop ||
572 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
574 /* See if we need to add request, can only do it once */
575 prop_getnames( props, slap_propnames, auxvals );
576 if ( !auxvals[0].name )
577 prop_request( props, slap_propnames );
579 if ( flags & SASL_CU_AUTHID )
580 which = SLAP_SASL_PROP_AUTHCLEN;
582 which = SLAP_SASL_PROP_AUTHZLEN;
584 /* Need to store the Connection for auxprop_lookup */
585 if ( !auxvals[SLAP_SASL_PROP_CONN].values ) {
586 names[0] = slap_propnames[SLAP_SASL_PROP_CONN];
588 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
591 /* Already been here? */
592 if ( auxvals[which].values )
595 /* Normally we require an authzID to have a u: or dn: prefix.
596 * However, SASL frequently gives us an authzID that is just
597 * an exact copy of the authcID, without a prefix. We need to
598 * detect and allow this condition. If SASL calls canonicalize
599 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
600 * But if it's broken into two calls, we need to remember the
601 * authcID so that we can compare the authzID later. We store
602 * the authcID temporarily in conn->c_sasl_dn. We necessarily
603 * finish Canonicalizing before Authorizing, so there is no
604 * conflict with slap_sasl_authorize's use of this temp var.
606 * The SASL EXTERNAL mech is backwards from all the other mechs,
607 * it does authzID before the authcID. If we see that authzID
608 * has already been done, don't do anything special with authcID.
610 if ( flags == SASL_CU_AUTHID && !auxvals[SLAP_SASL_PROP_AUTHZ].values ) {
611 conn->c_sasl_dn.bv_val = (char *) in;
612 conn->c_sasl_dn.bv_len = 0;
613 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
614 rc = strcmp( in, conn->c_sasl_dn.bv_val );
615 conn->c_sasl_dn.bv_val = NULL;
616 /* They were equal, no work needed */
617 if ( !rc ) goto done;
620 bvin.bv_val = (char *)in;
622 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
623 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
624 if ( rc != LDAP_SUCCESS ) {
625 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
629 names[0] = slap_propnames[which];
631 prop_set( props, names[0], (char *)&dn.bv_len, sizeof( dn.bv_len ) );
634 names[0] = slap_propnames[which];
635 prop_set( props, names[0], dn.bv_val, dn.bv_len );
637 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
638 conn ? conn->c_connid : -1, names[0]+1,
639 dn.bv_val ? dn.bv_val : "<EMPTY>" );
641 /* Not needed any more, SASL has copied it */
642 if ( conn && conn->c_sasl_bindop )
643 conn->c_sasl_bindop->o_tmpfree( dn.bv_val, conn->c_sasl_bindop->o_tmpmemctx );
646 AC_MEMCPY( out, in, inlen );
658 char *requested_user,
662 const char *def_realm,
664 struct propctx *props)
666 Connection *conn = (Connection *)context;
668 * (SLAP_SASL_PROP_COUNT - 1) because we skip "conn",
669 * + 1 for NULL termination?
671 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
672 struct berval authcDN, authzDN = BER_BVNULL;
675 /* Simple Binds don't support proxy authorization, ignore it */
676 if ( !conn->c_sasl_bindop ||
677 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
679 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
680 "authcid=\"%s\" authzid=\"%s\"\n",
681 conn ? conn->c_connid : -1, auth_identity, requested_user );
682 if ( conn->c_sasl_dn.bv_val ) {
683 BER_BVZERO( &conn->c_sasl_dn );
686 /* Skip SLAP_SASL_PROP_CONN */
687 prop_getnames( props, slap_propnames+1, auxvals );
689 /* Should not happen */
690 if ( !auxvals[0].values ) {
691 sasl_seterror( sconn, 0, "invalid authcid" );
695 AC_MEMCPY( &authcDN.bv_len, auxvals[0].values[0], sizeof(authcDN.bv_len) );
696 authcDN.bv_val = auxvals[1].values ? (char *)auxvals[1].values[0] : NULL;
697 conn->c_sasl_dn = authcDN;
699 /* Nothing to do if no authzID was given */
700 if ( !auxvals[2].name || !auxvals[2].values ) {
704 AC_MEMCPY( &authzDN.bv_len, auxvals[2].values[0], sizeof(authzDN.bv_len) );
705 authzDN.bv_val = auxvals[3].values ? (char *)auxvals[3].values[0] : NULL;
707 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
708 if ( rc != LDAP_SUCCESS ) {
709 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
710 "proxy authorization disallowed (%d)\n",
711 (long) (conn ? conn->c_connid : -1), rc, 0 );
713 sasl_seterror( sconn, 0, "not authorized" );
717 /* FIXME: we need yet another dup because slap_sasl_getdn()
718 * is using the bind operation slab */
719 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
722 if (conn->c_sasl_bindop) {
723 Statslog( LDAP_DEBUG_STATS,
724 "%s BIND authcid=\"%s\" authzid=\"%s\"\n",
725 conn->c_sasl_bindop->o_log_prefix,
726 auth_identity, requested_user, 0, 0 );
729 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
730 " proxy authorization allowed authzDN=\"%s\"\n",
731 (long) (conn ? conn->c_connid : -1),
732 authzDN.bv_val ? authzDN.bv_val : "", 0 );
737 slap_sasl_err2ldap( int saslerr )
741 /* map SASL errors to LDAP resultCode returned by:
743 * SASL_OK, SASL_NOMEM
745 * SASL_OK, SASL_CONTINUE, SASL_TRANS, SASL_BADPARAM, SASL_BADPROT,
747 * sasl_server_start()
750 * SASL_OK, SASL_BADPARAM
758 rc = LDAP_SASL_BIND_IN_PROGRESS;
765 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
771 rc = LDAP_INVALID_CREDENTIALS;
774 rc = LDAP_INSUFFICIENT_ACCESS;
778 rc = LDAP_INAPPROPRIATE_AUTH;
782 rc = LDAP_UNAVAILABLE;
785 rc = LDAP_UNWILLING_TO_PERFORM;
797 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
800 const struct berval *sc,
801 const struct berval * passwd,
802 const struct berval * cred,
807 void *ctx, *sconn = NULL;
809 for( i=0; i<cred->bv_len; i++) {
810 if(cred->bv_val[i] == '\0') {
811 return LUTIL_PASSWD_ERR; /* NUL character in password */
815 if( cred->bv_val[i] != '\0' ) {
816 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
819 for( i=0; i<passwd->bv_len; i++) {
820 if(passwd->bv_val[i] == '\0') {
821 return LUTIL_PASSWD_ERR; /* NUL character in password */
825 if( passwd->bv_val[i] != '\0' ) {
826 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
829 rtn = LUTIL_PASSWD_ERR;
831 ctx = ldap_pvt_thread_pool_context();
832 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_sasl_bind, &sconn, NULL );
834 if( sconn != NULL ) {
836 sc = sasl_checkpass( sconn,
837 passwd->bv_val, passwd->bv_len,
838 cred->bv_val, cred->bv_len );
839 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
844 #endif /* SLAPD_SPASSWD */
846 #endif /* HAVE_CYRUS_SASL */
848 #ifdef ENABLE_REWRITE
850 typedef struct slapd_map_data {
852 struct berval filter;
853 AttributeName attrs[2];
858 slapd_rw_config( const char *fname, int lineno, int argc, char **argv )
860 slapd_map_data *ret = NULL;
861 LDAPURLDesc *lud = NULL;
863 AttributeDescription *ad = NULL;
865 struct berval dn, ndn;
868 Debug( LDAP_DEBUG_ANY,
869 "[%s:%d] slapd map needs URI\n",
875 if ( strncasecmp( uri, "uri=", STRLENOF( "uri=" ) ) == 0 ) {
876 uri += STRLENOF( "uri=" );
879 if ( ldap_url_parse( uri, &lud ) != LDAP_URL_SUCCESS ) {
880 Debug( LDAP_DEBUG_ANY,
881 "[%s:%d] illegal URI '%s'\n",
882 fname, lineno, uri );
886 if ( strcasecmp( lud->lud_scheme, "ldap" )) {
887 Debug( LDAP_DEBUG_ANY,
888 "[%s:%d] illegal URI scheme '%s'\n",
889 fname, lineno, lud->lud_scheme );
893 if (( lud->lud_host && lud->lud_host[0] ) || lud->lud_exts
895 Debug( LDAP_DEBUG_ANY,
896 "[%s:%d] illegal URI '%s'\n",
897 fname, lineno, uri );
901 if ( lud->lud_attrs ) {
902 if ( lud->lud_attrs[1] ) {
903 Debug( LDAP_DEBUG_ANY,
904 "[%s:%d] only one attribute allowed in URI\n",
908 if ( strcasecmp( lud->lud_attrs[0], "dn" ) &&
909 strcasecmp( lud->lud_attrs[0], "entryDN" )) {
911 rc = slap_str2ad( lud->lud_attrs[0], &ad, &text );
916 ber_str2bv( lud->lud_dn, 0, 0, &dn );
917 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL ))
920 if ( lud->lud_filter ) {
921 flen = strlen( lud->lud_filter ) + 1;
923 ret = ch_malloc( sizeof( slapd_map_data ) + flen );
926 ret->filter.bv_val = (char *)(ret+1);
927 ret->filter.bv_len = flen - 1;
928 strcpy( ret->filter.bv_val, lud->lud_filter );
930 BER_BVZERO( &ret->filter );
932 ret->scope = lud->lud_scope;
934 ret->attrs[0].an_name = ad->ad_cname;
936 BER_BVZERO( &ret->attrs[0].an_name );
938 ret->attrs[0].an_desc = ad;
939 BER_BVZERO( &ret->attrs[1].an_name );
941 ldap_free_urldesc( lud );
945 struct slapd_rw_info {
946 slapd_map_data *si_data;
947 struct berval si_val;
951 slapd_rw_cb( Operation *op, SlapReply *rs )
953 if ( rs->sr_type == REP_SEARCH ) {
954 struct slapd_rw_info *si = op->o_callback->sc_private;
956 if ( si->si_data->attrs[0].an_desc ) {
959 a = attr_find( rs->sr_entry->e_attrs,
960 si->si_data->attrs[0].an_desc );
962 ber_dupbv( &si->si_val, a->a_vals );
965 ber_dupbv( &si->si_val, &rs->sr_entry->e_name );
972 slapd_rw_apply( void *private, const char *filter, struct berval *val )
974 slapd_map_data *sl = private;
975 slap_callback cb = { NULL };
976 Connection conn = {0};
977 OperationBuffer opbuf;
980 SlapReply rs = {REP_RESULT};
981 struct slapd_rw_info si;
985 thrctx = ldap_pvt_thread_pool_context();
986 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
989 op->o_tag = LDAP_REQ_SEARCH;
990 op->o_req_dn = op->o_req_ndn = sl->base;
991 op->o_bd = select_backend( &op->o_req_ndn, 1 );
996 BER_BVZERO( &si.si_val );
997 op->ors_scope = sl->scope;
998 op->ors_deref = LDAP_DEREF_NEVER;
1000 op->ors_tlimit = SLAP_NO_LIMIT;
1001 if ( sl->attrs[0].an_desc ) {
1002 op->ors_attrs = sl->attrs;
1004 op->ors_attrs = slap_anlist_no_attrs;
1007 rc = strlen( filter );
1011 rc += sl->filter.bv_len;
1012 ptr = op->ors_filterstr.bv_val = op->o_tmpalloc( rc + 1, op->o_tmpmemctx );
1013 if ( sl->filter.bv_len ) {
1014 ptr = lutil_strcopy( ptr, sl->filter.bv_val );
1019 strcpy( ptr, filter );
1021 op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
1022 if ( !op->ors_filter ) {
1023 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1027 op->ors_attrsonly = 0;
1028 op->o_dn = op->o_bd->be_rootdn;
1029 op->o_ndn = op->o_bd->be_rootndn;
1030 op->o_do_not_cache = 1;
1032 cb.sc_response = slapd_rw_cb;
1033 cb.sc_private = &si;
1034 op->o_callback = &cb;
1036 rc = op->o_bd->be_search( op, &rs );
1037 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &si.si_val )) {
1039 rc = REWRITE_SUCCESS;
1041 if ( !BER_BVISNULL( &si.si_val )) {
1042 ch_free( si.si_val.bv_val );
1046 filter_free_x( op, op->ors_filter );
1047 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1052 slapd_rw_destroy( void *private )
1054 slapd_map_data *md = private;
1056 assert( private != NULL );
1058 ch_free( md->base.bv_val );
1059 ch_free( md->filter.bv_val );
1065 static const rewrite_mapper slapd_mapper = {
1073 int slap_sasl_init( void )
1075 #ifdef HAVE_CYRUS_SASL
1077 static sasl_callback_t server_callbacks[] = {
1078 { SASL_CB_LOG, &slap_sasl_log, NULL },
1079 { SASL_CB_LIST_END, NULL, NULL }
1083 #ifdef ENABLE_REWRITE
1084 rewrite_mapper_register( &slapd_mapper );
1087 #ifdef HAVE_CYRUS_SASL
1088 #ifdef HAVE_SASL_VERSION
1089 /* stringify the version number, sasl.h doesn't do it for us */
1090 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1091 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1092 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1095 sasl_version( NULL, &rc );
1096 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1097 (rc & 0xffff) < SASL_VERSION_STEP)
1099 char version[sizeof("xxx.xxx.xxxxx")];
1100 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1102 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
1103 " expected %s, got %s\n",
1104 SASL_VERSION_STRING, version, 0 );
1110 ldap_pvt_sasl_mutex_new,
1111 ldap_pvt_sasl_mutex_lock,
1112 ldap_pvt_sasl_mutex_unlock,
1113 ldap_pvt_sasl_mutex_dispose );
1115 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1117 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1118 if( rc != SASL_OK ) {
1119 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
1124 /* should provide callbacks for logging */
1125 /* server name should be configurable */
1126 rc = sasl_server_init( server_callbacks, "slapd" );
1128 if( rc != SASL_OK ) {
1129 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
1135 #ifdef SLAPD_SPASSWD
1136 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
1139 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1142 /* default security properties */
1143 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1144 sasl_secprops.max_ssf = INT_MAX;
1145 sasl_secprops.maxbufsize = 65536;
1146 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1152 int slap_sasl_destroy( void )
1154 #ifdef HAVE_CYRUS_SASL
1164 slap_sasl_peer2ipport( struct berval *peer )
1168 *addr = &peer->bv_val[ STRLENOF( "IP=" ) ];
1169 ber_len_t plen = peer->bv_len - STRLENOF( "IP=" );
1172 if ( addr[0] == '[' ) {
1176 ipport = ch_strdup( &addr[isv6] );
1178 /* Convert IPv6/IPv4 addresses to address;port syntax. */
1179 p = strrchr( ipport, ':' );
1183 assert( p[-1] == ']' );
1184 AC_MEMCPY( &p[-1], p, plen - ( p - ipport ) + 1 );
1187 } else if ( isv6 ) {
1190 assert( addr[plen] == ']' );
1197 int slap_sasl_open( Connection *conn, int reopen )
1199 int sc = LDAP_SUCCESS;
1200 #ifdef HAVE_CYRUS_SASL
1203 sasl_conn_t *ctx = NULL;
1204 sasl_callback_t *session_callbacks;
1205 char *ipremoteport = NULL, *iplocalport = NULL;
1207 assert( conn->c_sasl_authctx == NULL );
1210 assert( conn->c_sasl_extra == NULL );
1213 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1214 if( session_callbacks == NULL ) {
1215 Debug( LDAP_DEBUG_ANY,
1216 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1219 conn->c_sasl_extra = session_callbacks;
1221 session_callbacks[cb=0].id = SASL_CB_LOG;
1222 session_callbacks[cb].proc = &slap_sasl_log;
1223 session_callbacks[cb++].context = conn;
1225 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1226 session_callbacks[cb].proc = &slap_sasl_authorize;
1227 session_callbacks[cb++].context = conn;
1229 session_callbacks[cb].id = SASL_CB_CANON_USER;
1230 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1231 session_callbacks[cb++].context = conn;
1233 session_callbacks[cb].id = SASL_CB_LIST_END;
1234 session_callbacks[cb].proc = NULL;
1235 session_callbacks[cb++].context = NULL;
1237 session_callbacks = conn->c_sasl_extra;
1240 conn->c_sasl_layers = 0;
1242 /* create new SASL context */
1243 if ( conn->c_sock_name.bv_len != 0 &&
1244 strncmp( conn->c_sock_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1246 iplocalport = slap_sasl_peer2ipport( &conn->c_sock_name );
1249 if ( conn->c_peer_name.bv_len != 0 &&
1250 strncmp( conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1252 ipremoteport = slap_sasl_peer2ipport( &conn->c_peer_name );
1255 sc = sasl_server_new( "ldap", sasl_host, global_realm,
1256 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1257 if ( iplocalport != NULL ) {
1258 ch_free( iplocalport );
1260 if ( ipremoteport != NULL ) {
1261 ch_free( ipremoteport );
1264 if( sc != SASL_OK ) {
1265 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1271 conn->c_sasl_authctx = ctx;
1273 if( sc == SASL_OK ) {
1274 sc = sasl_setprop( ctx,
1275 SASL_SEC_PROPS, &sasl_secprops );
1277 if( sc != SASL_OK ) {
1278 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1281 slap_sasl_close( conn );
1286 sc = slap_sasl_err2ldap( sc );
1288 #elif defined(SLAP_BUILTIN_SASL)
1289 /* built-in SASL implementation */
1290 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1291 if( ctx == NULL ) return -1;
1293 ctx->sc_external_ssf = 0;
1294 BER_BVZERO( &ctx->sc_external_id );
1296 conn->c_sasl_authctx = ctx;
1302 int slap_sasl_external(
1305 struct berval *auth_id )
1307 #ifdef HAVE_CYRUS_SASL
1309 sasl_conn_t *ctx = conn->c_sasl_authctx;
1310 sasl_ssf_t sasl_ssf = ssf;
1312 if ( ctx == NULL ) {
1313 return LDAP_UNAVAILABLE;
1316 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
1318 if ( sc != SASL_OK ) {
1322 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1323 auth_id ? auth_id->bv_val : NULL );
1325 if ( sc != SASL_OK ) {
1328 #elif defined(SLAP_BUILTIN_SASL)
1329 /* built-in SASL implementation */
1330 SASL_CTX *ctx = conn->c_sasl_authctx;
1331 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1333 ctx->sc_external_ssf = ssf;
1335 ctx->sc_external_id = *auth_id;
1336 BER_BVZERO( auth_id );
1338 BER_BVZERO( &ctx->sc_external_id );
1342 return LDAP_SUCCESS;
1345 int slap_sasl_reset( Connection *conn )
1347 return LDAP_SUCCESS;
1350 char ** slap_sasl_mechs( Connection *conn )
1352 char **mechs = NULL;
1354 #ifdef HAVE_CYRUS_SASL
1355 sasl_conn_t *ctx = conn->c_sasl_authctx;
1357 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1361 SASL_CONST char *mechstr;
1363 sc = sasl_listmech( ctx,
1364 NULL, NULL, ",", NULL,
1365 &mechstr, NULL, NULL );
1367 if( sc != SASL_OK ) {
1368 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1374 mechs = ldap_str2charray( mechstr, "," );
1376 #elif defined(SLAP_BUILTIN_SASL)
1377 /* builtin SASL implementation */
1378 SASL_CTX *ctx = conn->c_sasl_authctx;
1379 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1380 /* should check ssf */
1381 mechs = ldap_str2charray( "EXTERNAL", "," );
1388 int slap_sasl_close( Connection *conn )
1390 #ifdef HAVE_CYRUS_SASL
1391 sasl_conn_t *ctx = conn->c_sasl_authctx;
1394 sasl_dispose( &ctx );
1396 if ( conn->c_sasl_sockctx &&
1397 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1399 ctx = conn->c_sasl_sockctx;
1400 sasl_dispose( &ctx );
1403 conn->c_sasl_authctx = NULL;
1404 conn->c_sasl_sockctx = NULL;
1405 conn->c_sasl_done = 0;
1407 free( conn->c_sasl_extra );
1408 conn->c_sasl_extra = NULL;
1410 #elif defined(SLAP_BUILTIN_SASL)
1411 SASL_CTX *ctx = conn->c_sasl_authctx;
1413 if( ctx->sc_external_id.bv_val ) {
1414 free( ctx->sc_external_id.bv_val );
1415 BER_BVZERO( &ctx->sc_external_id );
1418 conn->c_sasl_authctx = NULL;
1422 return LDAP_SUCCESS;
1425 int slap_sasl_bind( Operation *op, SlapReply *rs )
1427 #ifdef HAVE_CYRUS_SASL
1428 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1429 struct berval response;
1430 unsigned reslen = 0;
1433 Debug(LDAP_DEBUG_ARGS,
1434 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1435 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1436 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1437 op->o_conn->c_sasl_bind_mech.bv_val,
1438 op->orb_cred.bv_len );
1441 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1442 "SASL unavailable on this session" );
1446 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1447 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1448 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1449 sasl_server_step( ctx, cred, clen, resp, rlen )
1451 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1452 /* If we already authenticated once, must use a new context */
1453 if ( op->o_conn->c_sasl_done ) {
1455 const char *authid = NULL;
1456 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1457 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1458 if ( authid ) authid = ch_strdup( authid );
1459 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1460 sasl_dispose( &ctx );
1462 op->o_conn->c_sasl_authctx = NULL;
1464 slap_sasl_open( op->o_conn, 1 );
1465 ctx = op->o_conn->c_sasl_authctx;
1467 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1468 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1469 ch_free( (char *)authid );
1473 op->o_conn->c_sasl_bind_mech.bv_val,
1474 op->orb_cred.bv_val, op->orb_cred.bv_len,
1475 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1479 op->orb_cred.bv_val, op->orb_cred.bv_len,
1480 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1483 response.bv_len = reslen;
1485 if ( sc == SASL_OK ) {
1486 sasl_ssf_t *ssf = NULL;
1488 ber_dupbv_x( &op->orb_edn, &op->o_conn->c_sasl_dn, op->o_tmpmemctx );
1489 BER_BVZERO( &op->o_conn->c_sasl_dn );
1490 op->o_conn->c_sasl_done = 1;
1492 rs->sr_err = LDAP_SUCCESS;
1494 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1495 op->orb_ssf = ssf ? *ssf : 0;
1499 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1500 op->o_conn->c_sasl_layers++;
1502 /* If there's an old layer, set sockctx to NULL to
1503 * tell connection_read() to wait for us to finish.
1504 * Otherwise there is a race condition: we have to
1505 * send the Bind response using the old security
1506 * context and then remove it before reading any
1509 if ( op->o_conn->c_sasl_sockctx ) {
1510 ctx = op->o_conn->c_sasl_sockctx;
1511 op->o_conn->c_sasl_sockctx = NULL;
1513 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1515 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1518 /* Must send response using old security layer */
1519 if (response.bv_len) rs->sr_sasldata = &response;
1520 send_ldap_sasl( op, rs );
1522 /* Now dispose of the old security layer.
1525 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1526 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1527 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1528 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1529 sasl_dispose( &ctx );
1531 } else if ( sc == SASL_CONTINUE ) {
1532 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1533 rs->sr_text = sasl_errdetail( ctx );
1534 rs->sr_sasldata = &response;
1535 send_ldap_sasl( op, rs );
1538 BER_BVZERO( &op->o_conn->c_sasl_dn );
1539 rs->sr_text = sasl_errdetail( ctx );
1540 rs->sr_err = slap_sasl_err2ldap( sc ),
1541 send_ldap_result( op, rs );
1544 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1546 #elif defined(SLAP_BUILTIN_SASL)
1547 /* built-in SASL implementation */
1548 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1550 if ( ctx == NULL ) {
1551 send_ldap_error( op, rs, LDAP_OTHER,
1552 "Internal SASL Error" );
1554 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1557 if( op->orb_cred.bv_len ) {
1558 rs->sr_text = "proxy authorization not support";
1559 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1560 send_ldap_result( op, rs );
1563 op->orb_edn = ctx->sc_external_id;
1564 rs->sr_err = LDAP_SUCCESS;
1565 rs->sr_sasldata = NULL;
1566 send_ldap_sasl( op, rs );
1570 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1571 "requested SASL mechanism not supported" );
1574 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1575 "SASL not supported" );
1581 char* slap_sasl_secprops( const char *in )
1583 #ifdef HAVE_CYRUS_SASL
1584 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1586 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1588 return "SASL not supported";
1592 void slap_sasl_secprops_unparse( struct berval *bv )
1594 #ifdef HAVE_CYRUS_SASL
1595 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1599 #ifdef HAVE_CYRUS_SASL
1601 slap_sasl_setpass( Operation *op, SlapReply *rs )
1603 struct berval id = BER_BVNULL; /* needs to come from connection */
1604 struct berval new = BER_BVNULL;
1605 struct berval old = BER_BVNULL;
1607 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1609 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1610 (SASL_CONST void **)(char *)&id.bv_val );
1612 if( rs->sr_err != SASL_OK ) {
1613 rs->sr_text = "unable to retrieve SASL username";
1614 rs->sr_err = LDAP_OTHER;
1618 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1619 id.bv_val ? id.bv_val : "", 0, 0 );
1621 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1622 NULL, &old, &new, &rs->sr_text );
1624 if( rs->sr_err != LDAP_SUCCESS ) {
1628 if( new.bv_len == 0 ) {
1629 slap_passwd_generate(&new);
1631 if( new.bv_len == 0 ) {
1632 rs->sr_text = "password generation failed.";
1633 rs->sr_err = LDAP_OTHER;
1637 rs->sr_rspdata = slap_passwd_return( &new );
1640 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1641 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1642 if( rs->sr_err != SASL_OK ) {
1643 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1645 switch(rs->sr_err) {
1647 rs->sr_err = LDAP_SUCCESS;
1657 rs->sr_err = LDAP_OTHER;
1663 #endif /* HAVE_CYRUS_SASL */
1665 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1666 * string returned in *dn is in its own allocated memory, and must be free'd
1667 * by the calling process. -Mark Adamson, Carnegie Mellon
1669 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1670 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1677 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1678 char *user_realm, struct berval *dn, int flags )
1680 int rc, is_dn = SET_NONE, do_norm = 1;
1681 struct berval dn2, *mech;
1683 assert( conn != NULL );
1684 assert( id != NULL );
1686 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1688 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1689 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1690 (unsigned long) id->bv_len ) );
1693 op = conn->c_sasl_bindop;
1695 assert( op != NULL );
1699 if ( !BER_BVISNULL( id ) ) {
1700 /* Blatantly anonymous ID */
1701 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1703 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1704 return( LDAP_SUCCESS );
1708 /* FIXME: if empty, should we stop? */
1709 BER_BVSTR( id, "" );
1712 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1713 mech = &conn->c_sasl_bind_mech;
1715 mech = &conn->c_authmech;
1718 /* An authcID needs to be converted to authzID form. Set the
1719 * values directly into *dn; they will be normalized later. (and
1720 * normalizing always makes a new copy.) An ID from a TLS certificate
1721 * is already normalized, so copy it and skip normalization.
1723 if( flags & SLAP_GETDN_AUTHCID ) {
1724 if( bvmatch( mech, &ext_bv )) {
1725 /* EXTERNAL DNs are already normalized */
1726 assert( !BER_BVISNULL( id ) );
1730 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1733 /* convert to u:<username> form */
1739 if( is_dn == SET_NONE ) {
1740 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1742 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1743 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1745 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1747 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1748 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1752 /* No other possibilities from here */
1753 if( is_dn == SET_NONE ) {
1755 return( LDAP_INAPPROPRIATE_AUTH );
1758 /* Username strings */
1759 if( is_dn == SET_U ) {
1760 /* ITS#3419: values may need escape */
1762 LDAPAVA *RDNs[ 4 ][ 2 ];
1767 DN[ irdn ] = RDNs[ irdn ];
1768 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1769 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1770 AVAs[ irdn ].la_value = *dn;
1771 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1772 AVAs[ irdn ].la_private = NULL;
1773 RDNs[ irdn ][ 1 ] = NULL;
1775 if ( user_realm && *user_realm ) {
1777 DN[ irdn ] = RDNs[ irdn ];
1778 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1779 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1780 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1781 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1782 AVAs[ irdn ].la_private = NULL;
1783 RDNs[ irdn ][ 1 ] = NULL;
1786 if ( !BER_BVISNULL( mech ) ) {
1788 DN[ irdn ] = RDNs[ irdn ];
1789 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1790 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1791 AVAs[ irdn ].la_value = *mech;
1792 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1793 AVAs[ irdn ].la_private = NULL;
1794 RDNs[ irdn ][ 1 ] = NULL;
1798 DN[ irdn ] = RDNs[ irdn ];
1799 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1800 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1801 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1802 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1803 AVAs[ irdn ].la_private = NULL;
1804 RDNs[ irdn ][ 1 ] = NULL;
1809 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1811 if ( rc != LDAP_SUCCESS ) {
1816 Debug( LDAP_DEBUG_TRACE,
1817 "slap_sasl_getdn: u:id converted to %s\n",
1822 /* Dup the DN in any case, so we don't risk
1823 * leaks or dangling pointers later,
1824 * and the DN value is '\0' terminated */
1825 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1826 dn->bv_val = dn2.bv_val;
1829 /* All strings are in DN form now. Normalize if needed. */
1831 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1833 /* User DNs were constructed above and must be freed now */
1834 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1836 if ( rc != LDAP_SUCCESS ) {
1843 /* Run thru regexp */
1844 slap_sasl2dn( op, dn, &dn2, flags );
1845 if( !BER_BVISNULL( &dn2 ) ) {
1846 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1848 Debug( LDAP_DEBUG_TRACE,
1849 "slap_sasl_getdn: dn:id converted to %s\n",
1853 return( LDAP_SUCCESS );