2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2017 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 #if SASL_VERSION_MINOR >= 0x020119 /* 2.1.25 */
50 typedef sasl_callback_ft slap_sasl_cb_ft;
52 typedef int (*slap_sasl_cb_ft)();
55 static sasl_security_properties_t sasl_secprops;
56 #elif defined( SLAP_BUILTIN_SASL )
58 * built-in SASL implementation
59 * only supports EXTERNAL
61 typedef struct sasl_ctx {
62 slap_ssf_t sc_external_ssf;
63 struct berval sc_external_id;
70 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
72 char *slap_sasl_auxprops;
74 #ifdef HAVE_CYRUS_SASL
76 /* Just use our internal auxprop by default */
80 const char *plugin_name,
85 if ( strcmp( option, "auxprop_plugin" )) {
88 if ( slap_sasl_auxprops )
89 *result = slap_sasl_auxprops;
101 Connection *conn = context;
105 if ( message == NULL ) {
106 return SASL_BADPARAM;
111 level = LDAP_DEBUG_NONE;
115 level = LDAP_DEBUG_ANY;
119 level = LDAP_DEBUG_ANY;
123 level = LDAP_DEBUG_TRACE;
127 level = LDAP_DEBUG_TRACE;
131 level = LDAP_DEBUG_TRACE;
135 level = LDAP_DEBUG_TRACE;
139 level = LDAP_DEBUG_TRACE;
140 label = "Password Trace";
143 return SASL_BADPARAM;
146 Debug( level, "SASL [conn=%ld] %s: %s\n",
147 conn ? (long) conn->c_connid: -1L,
154 static const char *slap_propnames[] = {
155 "*slapConn", "*slapAuthcDNlen", "*slapAuthcDN",
156 "*slapAuthzDNlen", "*slapAuthzDN", NULL };
158 static Filter generic_filter = { LDAP_FILTER_PRESENT, { 0 }, NULL };
159 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
161 #define SLAP_SASL_PROP_CONN 0
162 #define SLAP_SASL_PROP_AUTHCLEN 1
163 #define SLAP_SASL_PROP_AUTHC 2
164 #define SLAP_SASL_PROP_AUTHZLEN 3
165 #define SLAP_SASL_PROP_AUTHZ 4
166 #define SLAP_SASL_PROP_COUNT 5 /* Number of properties we used */
168 typedef struct lookup_info {
170 const struct propval *list;
171 sasl_server_params_t *sparams;
174 static slap_response sasl_ap_lookup;
176 static struct berval sc_cleartext = BER_BVC("{CLEARTEXT}");
179 sasl_ap_lookup( Operation *op, SlapReply *rs )
182 AttributeDescription *ad;
186 lookup_info *sl = (lookup_info *)op->o_callback->sc_private;
188 if (rs->sr_type != REP_SEARCH) return 0;
190 for( i = 0; sl->list[i].name; i++ ) {
191 const char *name = sl->list[i].name;
193 if ( name[0] == '*' ) {
194 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
195 /* Skip our private properties */
196 if ( !strcmp( name, slap_propnames[0] )) {
197 i += SLAP_SASL_PROP_COUNT - 1;
201 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
204 if ( sl->list[i].values ) {
205 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
208 rc = slap_str2ad( name, &ad, &text );
209 if ( rc != LDAP_SUCCESS ) {
210 Debug( LDAP_DEBUG_TRACE,
211 "slap_ap_lookup: str2ad(%s): %s\n", name, text, 0 );
215 /* If it's the rootdn and a rootpw was present, we already set
216 * it so don't override it here.
218 if ( ad == slap_schema.si_ad_userPassword && sl->list[i].values &&
219 be_isroot_dn( op->o_bd, &op->o_req_ndn ))
222 a = attr_find( rs->sr_entry->e_attrs, ad );
224 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
227 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
228 sl->sparams->utils->prop_erase( sl->sparams->propctx,
231 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
232 /* ITS#3846 don't give hashed passwords to SASL */
233 if ( ad == slap_schema.si_ad_userPassword &&
234 bv->bv_val[0] == '{' /*}*/ )
236 if ( lutil_passwd_scheme( bv->bv_val ) ) {
237 /* If it's not a recognized scheme, just assume it's
238 * a cleartext password that happened to include brackets.
240 * If it's a recognized scheme, skip this value, unless the
241 * scheme is {CLEARTEXT}. In that case, skip over the
242 * scheme name and use the remainder. If there is nothing
243 * past the scheme name, skip this value.
245 #ifdef SLAPD_CLEARTEXT
246 if ( !strncasecmp( bv->bv_val, sc_cleartext.bv_val,
247 sc_cleartext.bv_len )) {
249 cbv.bv_len = bv->bv_len - sc_cleartext.bv_len;
250 if ( cbv.bv_len > 0 ) {
251 cbv.bv_val = bv->bv_val + sc_cleartext.bv_len;
252 sl->sparams->utils->prop_set( sl->sparams->propctx,
253 sl->list[i].name, cbv.bv_val, cbv.bv_len );
260 sl->sparams->utils->prop_set( sl->sparams->propctx,
261 sl->list[i].name, bv->bv_val, bv->bv_len );
267 #if SASL_VERSION_FULL >= 0x020118
274 sasl_server_params_t *sparams,
279 OperationBuffer opbuf = {{ NULL }};
280 Operation *op = (Operation *)&opbuf;
282 Connection *conn = NULL;
284 int rc = LDAP_SUCCESS;
286 sl.list = sparams->utils->prop_get( sparams->propctx );
287 sl.sparams = sparams;
290 /* Find our DN and conn first */
291 for( i = 0; sl.list[i].name; i++ ) {
292 if ( sl.list[i].name[0] == '*' ) {
293 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
294 if ( sl.list[i].values && sl.list[i].values[0] )
295 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
298 if ( flags & SASL_AUXPROP_AUTHZID ) {
299 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZLEN] )) {
300 if ( sl.list[i].values && sl.list[i].values[0] )
301 AC_MEMCPY( &op->o_req_ndn.bv_len, sl.list[i].values[0],
302 sizeof( op->o_req_ndn.bv_len ) );
303 } else if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZ] )) {
304 if ( sl.list[i].values )
305 op->o_req_ndn.bv_val = (char *)sl.list[i].values[0];
310 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
311 if ( sl.list[i].values && sl.list[i].values[0] )
312 AC_MEMCPY( &op->o_req_ndn.bv_len, sl.list[i].values[0],
313 sizeof( op->o_req_ndn.bv_len ) );
314 } else if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
315 if ( sl.list[i].values ) {
316 op->o_req_ndn.bv_val = (char *)sl.list[i].values[0];
317 if ( !(flags & SASL_AUXPROP_AUTHZID) )
324 /* we don't know anything about this, ignore it */
330 /* Now see what else needs to be fetched */
331 for( i = 0; sl.list[i].name; i++ ) {
332 const char *name = sl.list[i].name;
334 if ( name[0] == '*' ) {
335 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
336 /* Skip our private properties */
337 if ( !strcmp( name, slap_propnames[0] )) {
338 i += SLAP_SASL_PROP_COUNT - 1;
342 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
345 if ( sl.list[i].values ) {
346 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
353 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
357 op->o_bd = select_backend( &op->o_req_ndn, 1 );
360 /* For rootdn, see if we can use the rootpw */
361 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) &&
362 !BER_BVISEMPTY( &op->o_bd->be_rootpw )) {
363 struct berval cbv = BER_BVNULL;
365 /* If there's a recognized scheme, see if it's CLEARTEXT */
366 if ( lutil_passwd_scheme( op->o_bd->be_rootpw.bv_val )) {
367 if ( !strncasecmp( op->o_bd->be_rootpw.bv_val,
368 sc_cleartext.bv_val, sc_cleartext.bv_len )) {
370 /* If it's CLEARTEXT, skip past scheme spec */
371 cbv.bv_len = op->o_bd->be_rootpw.bv_len -
374 cbv.bv_val = op->o_bd->be_rootpw.bv_val +
378 /* No scheme, use the whole value */
380 cbv = op->o_bd->be_rootpw;
382 if ( !BER_BVISEMPTY( &cbv )) {
383 for( i = 0; sl.list[i].name; i++ ) {
384 const char *name = sl.list[i].name;
386 if ( name[0] == '*' ) {
387 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
389 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
392 if ( !strcasecmp(name,"userPassword") ) {
393 sl.sparams->utils->prop_set( sl.sparams->propctx,
394 sl.list[i].name, cbv.bv_val, cbv.bv_len );
401 if ( op->o_bd->be_search ) {
402 SlapReply rs = {REP_RESULT};
403 op->o_hdr = conn->c_sasl_bindop->o_hdr;
404 op->o_controls = opbuf.ob_controls;
405 op->o_tag = LDAP_REQ_SEARCH;
406 op->o_dn = conn->c_ndn;
407 op->o_ndn = conn->c_ndn;
408 op->o_callback = &cb;
409 slap_op_time( &op->o_time, &op->o_tincr );
410 op->o_do_not_cache = 1;
411 op->o_is_auth_check = 1;
412 op->o_req_dn = op->o_req_ndn;
413 op->ors_scope = LDAP_SCOPE_BASE;
414 op->ors_deref = LDAP_DEREF_NEVER;
415 op->ors_tlimit = SLAP_NO_LIMIT;
417 op->ors_filter = &generic_filter;
418 op->ors_filterstr = generic_filterstr;
419 op->o_authz = conn->c_authz;
420 /* FIXME: we want all attributes, right? */
421 op->ors_attrs = NULL;
423 rc = op->o_bd->be_search( op, &rs );
428 #if SASL_VERSION_FULL >= 0x020118
429 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
433 #if SASL_VERSION_FULL >= 0x020110
437 sasl_server_params_t *sparams,
438 struct propctx *prctx,
444 SlapReply rs = {REP_RESULT};
447 Connection *conn = NULL;
448 const struct propval *pr;
449 Modifications *modlist = NULL, **modtail = &modlist, *mod;
450 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
451 char textbuf[SLAP_TEXT_BUFLEN];
453 size_t textlen = sizeof(textbuf);
455 /* just checking if we are enabled */
456 if (!prctx) return SASL_OK;
458 if (!sparams || !user) return SASL_BADPARAM;
460 pr = sparams->utils->prop_get( sparams->propctx );
462 /* Find our DN and conn first */
463 for( i = 0; pr[i].name; i++ ) {
464 if ( pr[i].name[0] == '*' ) {
465 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
466 if ( pr[i].values && pr[i].values[0] )
467 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
470 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
471 if ( pr[i].values && pr[i].values[0] )
472 AC_MEMCPY( &op.o_req_ndn.bv_len, pr[i].values[0],
473 sizeof( op.o_req_ndn.bv_len ) );
474 } else if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
476 op.o_req_ndn.bv_val = (char *)pr[i].values[0];
480 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
482 op.o_bd = select_backend( &op.o_req_ndn, 1 );
484 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
486 pr = sparams->utils->prop_get( prctx );
487 if (!pr) return SASL_BADPARAM;
489 for (i=0; pr[i].name; i++);
490 if (!i) return SASL_BADPARAM;
492 for (i=0; pr[i].name; i++) {
493 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
494 mod->sml_op = LDAP_MOD_REPLACE;
496 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
497 mod->sml_numvals = pr[i].nvalues;
498 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
499 sizeof(struct berval));
500 for (j=0; j<pr[i].nvalues; j++) {
501 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
503 BER_BVZERO( &mod->sml_values[j] );
504 mod->sml_nvalues = NULL;
505 mod->sml_desc = NULL;
507 modtail = &mod->sml_next;
511 rc = slap_mods_check( &op, modlist, &text, textbuf, textlen, NULL );
513 if ( rc == LDAP_SUCCESS ) {
514 rc = slap_mods_no_user_mod_check( &op, modlist,
515 &text, textbuf, textlen );
517 if ( rc == LDAP_SUCCESS ) {
518 if ( conn->c_sasl_bindop ) {
519 op.o_hdr = conn->c_sasl_bindop->o_hdr;
522 memset( &oph, 0, sizeof(oph) );
523 operation_fake_init( conn, &op, ldap_pvt_thread_pool_context(), 0 );
525 op.o_tag = LDAP_REQ_MODIFY;
526 op.o_ndn = op.o_req_ndn;
528 slap_op_time( &op.o_time, &op.o_tincr );
529 op.o_do_not_cache = 1;
530 op.o_is_auth_check = 1;
531 op.o_req_dn = op.o_req_ndn;
532 op.orm_modlist = modlist;
534 rc = op.o_bd->be_modify( &op, &rs );
537 slap_mods_free( modlist, 1 );
538 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
540 #endif /* SASL_VERSION_FULL >= 2.1.16 */
542 static sasl_auxprop_plug_t slap_auxprop_plugin = {
545 NULL, /* glob_context */
546 NULL, /* auxprop_free */
549 #if SASL_VERSION_FULL >= 0x020110
550 slap_auxprop_store /* the declaration of this member changed
551 * in cyrus SASL from 2.1.15 to 2.1.16 */
559 const sasl_utils_t *utils,
562 sasl_auxprop_plug_t **plug,
563 const char *plugname)
565 if ( !out_version || !plug ) return SASL_BADPARAM;
567 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
569 *out_version = SASL_AUXPROP_PLUG_VERSION;
570 *plug = &slap_auxprop_plugin;
574 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
575 * auxiliary property, so that we can refer to it in sasl_authorize
576 * without interfering with anything else. Also, the SASL username
577 * buffer is constrained to 256 characters, and our DNs could be
578 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
581 slap_sasl_canonicalize(
587 const char *user_realm,
592 Connection *conn = (Connection *)context;
593 struct propctx *props = sasl_auxprop_getctx( sconn );
594 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
597 const char *names[2];
602 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
603 conn ? (long) conn->c_connid : -1L,
604 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
605 in ? in : "<empty>");
607 /* If name is too big, just truncate. We don't care, we're
608 * using DNs, not the usernames.
610 if ( inlen > out_max )
613 /* This is a Simple Bind using SPASSWD. That means the in-directory
614 * userPassword of the Binding user already points at SASL, so it
615 * cannot be used to actually satisfy a password comparison. Just
616 * ignore it, some other mech will process it.
618 if ( !conn->c_sasl_bindop ||
619 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
621 /* See if we need to add request, can only do it once */
622 prop_getnames( props, slap_propnames, auxvals );
623 if ( !auxvals[0].name )
624 prop_request( props, slap_propnames );
626 if ( flags & SASL_CU_AUTHID )
627 which = SLAP_SASL_PROP_AUTHCLEN;
629 which = SLAP_SASL_PROP_AUTHZLEN;
631 /* Need to store the Connection for auxprop_lookup */
632 if ( !auxvals[SLAP_SASL_PROP_CONN].values ) {
633 names[0] = slap_propnames[SLAP_SASL_PROP_CONN];
635 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
638 /* Already been here? */
639 if ( auxvals[which].values )
642 /* Normally we require an authzID to have a u: or dn: prefix.
643 * However, SASL frequently gives us an authzID that is just
644 * an exact copy of the authcID, without a prefix. We need to
645 * detect and allow this condition. If SASL calls canonicalize
646 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
647 * But if it's broken into two calls, we need to remember the
648 * authcID so that we can compare the authzID later. We store
649 * the authcID temporarily in conn->c_sasl_dn. We necessarily
650 * finish Canonicalizing before Authorizing, so there is no
651 * conflict with slap_sasl_authorize's use of this temp var.
653 * The SASL EXTERNAL mech is backwards from all the other mechs,
654 * it does authzID before the authcID. If we see that authzID
655 * has already been done, don't do anything special with authcID.
657 if ( flags == SASL_CU_AUTHID && !auxvals[SLAP_SASL_PROP_AUTHZ].values ) {
658 conn->c_sasl_dn.bv_val = (char *) in;
659 conn->c_sasl_dn.bv_len = 0;
660 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
661 rc = strcmp( in, conn->c_sasl_dn.bv_val );
662 conn->c_sasl_dn.bv_val = NULL;
663 /* They were equal, no work needed */
664 if ( !rc ) goto done;
667 bvin.bv_val = (char *)in;
669 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
670 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
671 if ( rc != LDAP_SUCCESS ) {
672 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
676 names[0] = slap_propnames[which];
678 prop_set( props, names[0], (char *)&dn.bv_len, sizeof( dn.bv_len ) );
681 names[0] = slap_propnames[which];
682 prop_set( props, names[0], dn.bv_val, dn.bv_len );
684 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
685 conn ? (long) conn->c_connid : -1L, names[0]+1,
686 dn.bv_val ? dn.bv_val : "<EMPTY>" );
688 /* Not needed any more, SASL has copied it */
689 if ( conn && conn->c_sasl_bindop )
690 conn->c_sasl_bindop->o_tmpfree( dn.bv_val, conn->c_sasl_bindop->o_tmpmemctx );
693 AC_MEMCPY( out, in, inlen );
705 char *requested_user,
709 const char *def_realm,
711 struct propctx *props)
713 Connection *conn = (Connection *)context;
715 * (SLAP_SASL_PROP_COUNT - 1) because we skip "conn",
716 * + 1 for NULL termination?
718 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
719 struct berval authcDN, authzDN = BER_BVNULL;
722 /* Simple Binds don't support proxy authorization, ignore it */
723 if ( !conn->c_sasl_bindop ||
724 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
726 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
727 "authcid=\"%s\" authzid=\"%s\"\n",
728 conn ? (long) conn->c_connid : -1L, auth_identity, requested_user );
729 if ( conn->c_sasl_dn.bv_val ) {
730 BER_BVZERO( &conn->c_sasl_dn );
733 /* Skip SLAP_SASL_PROP_CONN */
734 prop_getnames( props, slap_propnames+1, auxvals );
736 /* Should not happen */
737 if ( !auxvals[0].values ) {
738 sasl_seterror( sconn, 0, "invalid authcid" );
742 AC_MEMCPY( &authcDN.bv_len, auxvals[0].values[0], sizeof(authcDN.bv_len) );
743 authcDN.bv_val = auxvals[1].values ? (char *)auxvals[1].values[0] : NULL;
744 conn->c_sasl_dn = authcDN;
746 /* Nothing to do if no authzID was given */
747 if ( !auxvals[2].name || !auxvals[2].values ) {
751 AC_MEMCPY( &authzDN.bv_len, auxvals[2].values[0], sizeof(authzDN.bv_len) );
752 authzDN.bv_val = auxvals[3].values ? (char *)auxvals[3].values[0] : NULL;
754 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
755 if ( rc != LDAP_SUCCESS ) {
756 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
757 "proxy authorization disallowed (%d)\n",
758 conn ? (long) conn->c_connid : -1L, rc, 0 );
760 sasl_seterror( sconn, 0, "not authorized" );
764 /* FIXME: we need yet another dup because slap_sasl_getdn()
765 * is using the bind operation slab */
766 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
769 if (conn->c_sasl_bindop) {
770 Statslog( LDAP_DEBUG_STATS,
771 "%s BIND authcid=\"%s\" authzid=\"%s\"\n",
772 conn->c_sasl_bindop->o_log_prefix,
773 auth_identity, requested_user, 0, 0 );
776 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
777 " proxy authorization allowed authzDN=\"%s\"\n",
778 conn ? (long) conn->c_connid : -1L,
779 authzDN.bv_val ? authzDN.bv_val : "", 0 );
784 slap_sasl_err2ldap( int saslerr )
788 /* map SASL errors to LDAP resultCode returned by:
790 * SASL_OK, SASL_NOMEM
792 * SASL_OK, SASL_CONTINUE, SASL_TRANS, SASL_BADPARAM, SASL_BADPROT,
794 * sasl_server_start()
797 * SASL_OK, SASL_BADPARAM
805 rc = LDAP_SASL_BIND_IN_PROGRESS;
812 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
818 rc = LDAP_INVALID_CREDENTIALS;
821 rc = LDAP_INSUFFICIENT_ACCESS;
825 rc = LDAP_INAPPROPRIATE_AUTH;
829 rc = LDAP_UNAVAILABLE;
832 rc = LDAP_UNWILLING_TO_PERFORM;
844 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
847 const struct berval *sc,
848 const struct berval * passwd,
849 const struct berval * cred,
854 void *ctx, *sconn = NULL;
856 for( i=0; i<cred->bv_len; i++) {
857 if(cred->bv_val[i] == '\0') {
858 return LUTIL_PASSWD_ERR; /* NUL character in password */
862 if( cred->bv_val[i] != '\0' ) {
863 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
866 for( i=0; i<passwd->bv_len; i++) {
867 if(passwd->bv_val[i] == '\0') {
868 return LUTIL_PASSWD_ERR; /* NUL character in password */
872 if( passwd->bv_val[i] != '\0' ) {
873 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
876 rtn = LUTIL_PASSWD_ERR;
878 ctx = ldap_pvt_thread_pool_context();
879 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_sasl_bind, &sconn, NULL );
881 if( sconn != NULL ) {
883 sc = sasl_checkpass( sconn,
884 passwd->bv_val, passwd->bv_len,
885 cred->bv_val, cred->bv_len );
886 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
891 #endif /* SLAPD_SPASSWD */
893 #endif /* HAVE_CYRUS_SASL */
895 #ifdef ENABLE_REWRITE
897 typedef struct slapd_map_data {
899 struct berval filter;
900 AttributeName attrs[2];
905 slapd_rw_config( const char *fname, int lineno, int argc, char **argv )
907 slapd_map_data *ret = NULL;
908 LDAPURLDesc *lud = NULL;
910 AttributeDescription *ad = NULL;
912 struct berval dn, ndn;
915 Debug( LDAP_DEBUG_ANY,
916 "[%s:%d] slapd map needs URI\n",
922 if ( strncasecmp( uri, "uri=", STRLENOF( "uri=" ) ) == 0 ) {
923 uri += STRLENOF( "uri=" );
926 if ( ldap_url_parse( uri, &lud ) != LDAP_URL_SUCCESS ) {
927 Debug( LDAP_DEBUG_ANY,
928 "[%s:%d] illegal URI '%s'\n",
929 fname, lineno, uri );
933 if ( strcasecmp( lud->lud_scheme, "ldap" )) {
934 Debug( LDAP_DEBUG_ANY,
935 "[%s:%d] illegal URI scheme '%s'\n",
936 fname, lineno, lud->lud_scheme );
940 if (( lud->lud_host && lud->lud_host[0] ) || lud->lud_exts
942 Debug( LDAP_DEBUG_ANY,
943 "[%s:%d] illegal URI '%s'\n",
944 fname, lineno, uri );
948 if ( lud->lud_attrs ) {
949 if ( lud->lud_attrs[1] ) {
950 Debug( LDAP_DEBUG_ANY,
951 "[%s:%d] only one attribute allowed in URI\n",
955 if ( strcasecmp( lud->lud_attrs[0], "dn" ) &&
956 strcasecmp( lud->lud_attrs[0], "entryDN" )) {
958 rc = slap_str2ad( lud->lud_attrs[0], &ad, &text );
963 ber_str2bv( lud->lud_dn, 0, 0, &dn );
964 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL ))
967 if ( lud->lud_filter ) {
968 flen = strlen( lud->lud_filter ) + 1;
970 ret = ch_malloc( sizeof( slapd_map_data ) + flen );
973 ret->filter.bv_val = (char *)(ret+1);
974 ret->filter.bv_len = flen - 1;
975 strcpy( ret->filter.bv_val, lud->lud_filter );
977 BER_BVZERO( &ret->filter );
979 ret->scope = lud->lud_scope;
981 ret->attrs[0].an_name = ad->ad_cname;
983 BER_BVZERO( &ret->attrs[0].an_name );
985 ret->attrs[0].an_desc = ad;
986 BER_BVZERO( &ret->attrs[1].an_name );
988 ldap_free_urldesc( lud );
992 struct slapd_rw_info {
993 slapd_map_data *si_data;
994 struct berval si_val;
998 slapd_rw_cb( Operation *op, SlapReply *rs )
1000 if ( rs->sr_type == REP_SEARCH ) {
1001 struct slapd_rw_info *si = op->o_callback->sc_private;
1003 if ( si->si_data->attrs[0].an_desc ) {
1006 a = attr_find( rs->sr_entry->e_attrs,
1007 si->si_data->attrs[0].an_desc );
1009 ber_dupbv( &si->si_val, a->a_vals );
1012 ber_dupbv( &si->si_val, &rs->sr_entry->e_name );
1015 return LDAP_SUCCESS;
1019 slapd_rw_apply( void *private, const char *filter, struct berval *val )
1021 slapd_map_data *sl = private;
1022 slap_callback cb = { NULL };
1023 Connection conn = {0};
1024 OperationBuffer opbuf;
1027 SlapReply rs = {REP_RESULT};
1028 struct slapd_rw_info si;
1032 thrctx = ldap_pvt_thread_pool_context();
1033 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1036 op->o_tag = LDAP_REQ_SEARCH;
1037 op->o_req_dn = op->o_req_ndn = sl->base;
1038 op->o_bd = select_backend( &op->o_req_ndn, 1 );
1043 BER_BVZERO( &si.si_val );
1044 op->ors_scope = sl->scope;
1045 op->ors_deref = LDAP_DEREF_NEVER;
1047 op->ors_tlimit = SLAP_NO_LIMIT;
1048 if ( sl->attrs[0].an_desc ) {
1049 op->ors_attrs = sl->attrs;
1051 op->ors_attrs = slap_anlist_no_attrs;
1054 rc = strlen( filter );
1058 rc += sl->filter.bv_len;
1059 ptr = op->ors_filterstr.bv_val = op->o_tmpalloc( rc + 1, op->o_tmpmemctx );
1060 if ( sl->filter.bv_len ) {
1061 ptr = lutil_strcopy( ptr, sl->filter.bv_val );
1066 strcpy( ptr, filter );
1068 op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
1069 if ( !op->ors_filter ) {
1070 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1074 op->ors_attrsonly = 0;
1075 op->o_dn = op->o_bd->be_rootdn;
1076 op->o_ndn = op->o_bd->be_rootndn;
1077 op->o_do_not_cache = 1;
1079 cb.sc_response = slapd_rw_cb;
1080 cb.sc_private = &si;
1081 op->o_callback = &cb;
1083 rc = op->o_bd->be_search( op, &rs );
1084 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &si.si_val )) {
1086 rc = REWRITE_SUCCESS;
1088 if ( !BER_BVISNULL( &si.si_val )) {
1089 ch_free( si.si_val.bv_val );
1093 filter_free_x( op, op->ors_filter, 1 );
1094 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1099 slapd_rw_destroy( void *private )
1101 slapd_map_data *md = private;
1103 assert( private != NULL );
1105 ch_free( md->base.bv_val );
1111 static const rewrite_mapper slapd_mapper = {
1119 int slap_sasl_init( void )
1121 #ifdef HAVE_CYRUS_SASL
1123 static sasl_callback_t server_callbacks[] = {
1124 { SASL_CB_LOG, (slap_sasl_cb_ft)&slap_sasl_log, NULL },
1125 { SASL_CB_GETOPT, (slap_sasl_cb_ft)&slap_sasl_getopt, NULL },
1126 { SASL_CB_LIST_END, NULL, NULL }
1130 #ifdef ENABLE_REWRITE
1131 rewrite_mapper_register( &slapd_mapper );
1134 #ifdef HAVE_CYRUS_SASL
1135 #ifdef HAVE_SASL_VERSION
1136 /* stringify the version number, sasl.h doesn't do it for us */
1137 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1138 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1139 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1142 sasl_version( NULL, &rc );
1143 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1144 (rc & 0xffff) < SASL_VERSION_STEP)
1146 char version[sizeof("xxx.xxx.xxxxx")];
1147 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1149 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
1150 " expected %s, got %s\n",
1151 SASL_VERSION_STRING, version, 0 );
1157 ldap_pvt_sasl_mutex_new,
1158 ldap_pvt_sasl_mutex_lock,
1159 ldap_pvt_sasl_mutex_unlock,
1160 ldap_pvt_sasl_mutex_dispose );
1162 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1164 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1165 if( rc != SASL_OK ) {
1166 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
1171 /* should provide callbacks for logging */
1172 /* server name should be configurable */
1173 rc = sasl_server_init( server_callbacks, "slapd" );
1175 if( rc != SASL_OK ) {
1176 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
1182 #ifdef SLAPD_SPASSWD
1183 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
1186 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1189 /* default security properties */
1190 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1191 sasl_secprops.max_ssf = INT_MAX;
1192 sasl_secprops.maxbufsize = 65536;
1193 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1199 int slap_sasl_destroy( void )
1201 #ifdef HAVE_CYRUS_SASL
1211 slap_sasl_peer2ipport( struct berval *peer )
1215 *addr = &peer->bv_val[ STRLENOF( "IP=" ) ];
1216 ber_len_t plen = peer->bv_len - STRLENOF( "IP=" );
1219 if ( addr[0] == '[' ) {
1223 ipport = ch_strdup( &addr[isv6] );
1225 /* Convert IPv6/IPv4 addresses to address;port syntax. */
1226 p = strrchr( ipport, ':' );
1230 assert( p[-1] == ']' );
1231 AC_MEMCPY( &p[-1], p, plen - ( p - ipport ) + 1 );
1234 } else if ( isv6 ) {
1237 assert( addr[plen] == ']' );
1244 int slap_sasl_open( Connection *conn, int reopen )
1246 int sc = LDAP_SUCCESS;
1247 #ifdef HAVE_CYRUS_SASL
1250 sasl_conn_t *ctx = NULL;
1251 sasl_callback_t *session_callbacks;
1252 char *ipremoteport = NULL, *iplocalport = NULL;
1254 assert( conn->c_sasl_authctx == NULL );
1257 assert( conn->c_sasl_extra == NULL );
1260 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1261 if( session_callbacks == NULL ) {
1262 Debug( LDAP_DEBUG_ANY,
1263 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1266 conn->c_sasl_extra = session_callbacks;
1268 session_callbacks[cb=0].id = SASL_CB_LOG;
1269 session_callbacks[cb].proc = (slap_sasl_cb_ft)&slap_sasl_log;
1270 session_callbacks[cb++].context = conn;
1272 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1273 session_callbacks[cb].proc = (slap_sasl_cb_ft)&slap_sasl_authorize;
1274 session_callbacks[cb++].context = conn;
1276 session_callbacks[cb].id = SASL_CB_CANON_USER;
1277 session_callbacks[cb].proc = (slap_sasl_cb_ft)&slap_sasl_canonicalize;
1278 session_callbacks[cb++].context = conn;
1280 session_callbacks[cb].id = SASL_CB_LIST_END;
1281 session_callbacks[cb].proc = NULL;
1282 session_callbacks[cb++].context = NULL;
1284 session_callbacks = conn->c_sasl_extra;
1287 conn->c_sasl_layers = 0;
1289 /* create new SASL context */
1290 if ( conn->c_sock_name.bv_len != 0 &&
1291 strncmp( conn->c_sock_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1293 iplocalport = slap_sasl_peer2ipport( &conn->c_sock_name );
1296 if ( conn->c_peer_name.bv_len != 0 &&
1297 strncmp( conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1299 ipremoteport = slap_sasl_peer2ipport( &conn->c_peer_name );
1302 sc = sasl_server_new( "ldap", sasl_host, global_realm,
1303 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1304 if ( iplocalport != NULL ) {
1305 ch_free( iplocalport );
1307 if ( ipremoteport != NULL ) {
1308 ch_free( ipremoteport );
1311 if( sc != SASL_OK ) {
1312 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1318 conn->c_sasl_authctx = ctx;
1320 if( sc == SASL_OK ) {
1321 sc = sasl_setprop( ctx,
1322 SASL_SEC_PROPS, &sasl_secprops );
1324 if( sc != SASL_OK ) {
1325 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1328 slap_sasl_close( conn );
1333 sc = slap_sasl_err2ldap( sc );
1335 #elif defined(SLAP_BUILTIN_SASL)
1336 /* built-in SASL implementation */
1337 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1338 if( ctx == NULL ) return -1;
1340 ctx->sc_external_ssf = 0;
1341 BER_BVZERO( &ctx->sc_external_id );
1343 conn->c_sasl_authctx = ctx;
1349 int slap_sasl_external(
1352 struct berval *auth_id )
1354 #ifdef HAVE_CYRUS_SASL
1356 sasl_conn_t *ctx = conn->c_sasl_authctx;
1357 sasl_ssf_t sasl_ssf = ssf;
1359 if ( ctx == NULL ) {
1360 return LDAP_UNAVAILABLE;
1363 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
1365 if ( sc != SASL_OK ) {
1369 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1370 auth_id ? auth_id->bv_val : NULL );
1372 if ( sc != SASL_OK ) {
1375 #elif defined(SLAP_BUILTIN_SASL)
1376 /* built-in SASL implementation */
1377 SASL_CTX *ctx = conn->c_sasl_authctx;
1378 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1380 ctx->sc_external_ssf = ssf;
1382 ctx->sc_external_id = *auth_id;
1383 BER_BVZERO( auth_id );
1385 BER_BVZERO( &ctx->sc_external_id );
1389 return LDAP_SUCCESS;
1392 int slap_sasl_reset( Connection *conn )
1394 return LDAP_SUCCESS;
1397 char ** slap_sasl_mechs( Connection *conn )
1399 char **mechs = NULL;
1401 #ifdef HAVE_CYRUS_SASL
1402 sasl_conn_t *ctx = conn->c_sasl_authctx;
1404 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1408 SASL_CONST char *mechstr;
1410 sc = sasl_listmech( ctx,
1411 NULL, NULL, ",", NULL,
1412 &mechstr, NULL, NULL );
1414 if( sc != SASL_OK ) {
1415 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1421 mechs = ldap_str2charray( mechstr, "," );
1423 #elif defined(SLAP_BUILTIN_SASL)
1424 /* builtin SASL implementation */
1425 SASL_CTX *ctx = conn->c_sasl_authctx;
1426 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1427 /* should check ssf */
1428 mechs = ldap_str2charray( "EXTERNAL", "," );
1435 int slap_sasl_close( Connection *conn )
1437 #ifdef HAVE_CYRUS_SASL
1438 sasl_conn_t *ctx = conn->c_sasl_authctx;
1441 sasl_dispose( &ctx );
1443 if ( conn->c_sasl_sockctx &&
1444 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1446 ctx = conn->c_sasl_sockctx;
1447 sasl_dispose( &ctx );
1450 conn->c_sasl_authctx = NULL;
1451 conn->c_sasl_sockctx = NULL;
1452 conn->c_sasl_done = 0;
1454 free( conn->c_sasl_extra );
1455 conn->c_sasl_extra = NULL;
1457 #elif defined(SLAP_BUILTIN_SASL)
1458 SASL_CTX *ctx = conn->c_sasl_authctx;
1460 if( ctx->sc_external_id.bv_val ) {
1461 free( ctx->sc_external_id.bv_val );
1462 BER_BVZERO( &ctx->sc_external_id );
1465 conn->c_sasl_authctx = NULL;
1469 return LDAP_SUCCESS;
1472 int slap_sasl_bind( Operation *op, SlapReply *rs )
1474 #ifdef HAVE_CYRUS_SASL
1475 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1476 struct berval response;
1477 unsigned reslen = 0;
1480 Debug(LDAP_DEBUG_ARGS,
1481 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1482 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1483 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1484 op->o_conn->c_sasl_bind_mech.bv_val,
1485 op->orb_cred.bv_len );
1488 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1489 "SASL unavailable on this session" );
1493 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1494 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1495 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1496 sasl_server_step( ctx, cred, clen, resp, rlen )
1498 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1499 /* If we already authenticated once, must use a new context */
1500 if ( op->o_conn->c_sasl_done ) {
1502 const char *authid = NULL;
1503 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1504 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1505 if ( authid ) authid = ch_strdup( authid );
1506 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1507 sasl_dispose( &ctx );
1509 op->o_conn->c_sasl_authctx = NULL;
1511 slap_sasl_open( op->o_conn, 1 );
1512 ctx = op->o_conn->c_sasl_authctx;
1514 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1515 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1516 ch_free( (char *)authid );
1520 op->o_conn->c_sasl_bind_mech.bv_val,
1521 op->orb_cred.bv_val, op->orb_cred.bv_len,
1522 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1526 op->orb_cred.bv_val, op->orb_cred.bv_len,
1527 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1530 response.bv_len = reslen;
1532 if ( sc == SASL_OK ) {
1533 sasl_ssf_t *ssf = NULL;
1535 ber_dupbv_x( &op->orb_edn, &op->o_conn->c_sasl_dn, op->o_tmpmemctx );
1536 BER_BVZERO( &op->o_conn->c_sasl_dn );
1537 op->o_conn->c_sasl_done = 1;
1539 rs->sr_err = LDAP_SUCCESS;
1541 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1542 op->orb_ssf = ssf ? *ssf : 0;
1546 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1547 op->o_conn->c_sasl_layers++;
1549 /* If there's an old layer, set sockctx to NULL to
1550 * tell connection_read() to wait for us to finish.
1551 * Otherwise there is a race condition: we have to
1552 * send the Bind response using the old security
1553 * context and then remove it before reading any
1556 if ( op->o_conn->c_sasl_sockctx ) {
1557 ctx = op->o_conn->c_sasl_sockctx;
1558 op->o_conn->c_sasl_sockctx = NULL;
1560 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1562 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1565 /* Must send response using old security layer */
1566 rs->sr_sasldata = (response.bv_len ? &response : NULL);
1567 send_ldap_sasl( op, rs );
1569 /* Now dispose of the old security layer.
1572 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1573 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1574 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1575 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1576 sasl_dispose( &ctx );
1578 } else if ( sc == SASL_CONTINUE ) {
1579 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1580 rs->sr_text = sasl_errdetail( ctx );
1581 rs->sr_sasldata = &response;
1582 send_ldap_sasl( op, rs );
1585 BER_BVZERO( &op->o_conn->c_sasl_dn );
1586 rs->sr_text = sasl_errdetail( ctx );
1587 rs->sr_err = slap_sasl_err2ldap( sc ),
1588 send_ldap_result( op, rs );
1591 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1593 #elif defined(SLAP_BUILTIN_SASL)
1594 /* built-in SASL implementation */
1595 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1597 if ( ctx == NULL ) {
1598 send_ldap_error( op, rs, LDAP_OTHER,
1599 "Internal SASL Error" );
1601 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1604 if( op->orb_cred.bv_len ) {
1605 rs->sr_text = "proxy authorization not supported";
1606 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1607 send_ldap_result( op, rs );
1610 op->orb_edn = ctx->sc_external_id;
1611 rs->sr_err = LDAP_SUCCESS;
1612 rs->sr_sasldata = NULL;
1613 send_ldap_sasl( op, rs );
1617 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1618 "requested SASL mechanism not supported" );
1621 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1622 "SASL not supported" );
1628 char* slap_sasl_secprops( const char *in )
1630 #ifdef HAVE_CYRUS_SASL
1631 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1633 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1635 return "SASL not supported";
1639 void slap_sasl_secprops_unparse( struct berval *bv )
1641 #ifdef HAVE_CYRUS_SASL
1642 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1646 #ifdef HAVE_CYRUS_SASL
1648 slap_sasl_setpass( Operation *op, SlapReply *rs )
1650 struct berval id = BER_BVNULL; /* needs to come from connection */
1651 struct berval new = BER_BVNULL;
1652 struct berval old = BER_BVNULL;
1654 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1656 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1657 (SASL_CONST void **)(char *)&id.bv_val );
1659 if( rs->sr_err != SASL_OK ) {
1660 rs->sr_text = "unable to retrieve SASL username";
1661 rs->sr_err = LDAP_OTHER;
1665 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1666 id.bv_val ? id.bv_val : "", 0, 0 );
1668 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1669 NULL, &old, &new, &rs->sr_text );
1671 if( rs->sr_err != LDAP_SUCCESS ) {
1675 if( new.bv_len == 0 ) {
1676 slap_passwd_generate(&new);
1678 if( new.bv_len == 0 ) {
1679 rs->sr_text = "password generation failed.";
1680 rs->sr_err = LDAP_OTHER;
1684 rs->sr_rspdata = slap_passwd_return( &new );
1687 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1688 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1689 if( rs->sr_err != SASL_OK ) {
1690 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1692 switch(rs->sr_err) {
1694 rs->sr_err = LDAP_SUCCESS;
1704 rs->sr_err = LDAP_OTHER;
1710 #endif /* HAVE_CYRUS_SASL */
1712 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1713 * string returned in *dn is in its own allocated memory, and must be free'd
1714 * by the calling process. -Mark Adamson, Carnegie Mellon
1716 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1717 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1724 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1725 char *user_realm, struct berval *dn, int flags )
1727 int rc, is_dn = SET_NONE, do_norm = 1;
1728 struct berval dn2, *mech;
1730 assert( conn != NULL );
1731 assert( id != NULL );
1733 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1735 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1736 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1737 (unsigned long) id->bv_len ) );
1740 op = conn->c_sasl_bindop;
1742 assert( op != NULL );
1746 if ( !BER_BVISNULL( id ) ) {
1747 /* Blatantly anonymous ID */
1748 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1750 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1751 return( LDAP_SUCCESS );
1755 /* FIXME: if empty, should we stop? */
1756 BER_BVSTR( id, "" );
1759 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1760 mech = &conn->c_sasl_bind_mech;
1762 mech = &conn->c_authmech;
1765 /* An authcID needs to be converted to authzID form. Set the
1766 * values directly into *dn; they will be normalized later. (and
1767 * normalizing always makes a new copy.) An ID from a TLS certificate
1768 * is already normalized, so copy it and skip normalization.
1770 if( flags & SLAP_GETDN_AUTHCID ) {
1771 if( bvmatch( mech, &ext_bv )) {
1772 /* EXTERNAL DNs are already normalized */
1773 assert( !BER_BVISNULL( id ) );
1777 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1780 /* convert to u:<username> form */
1786 if( is_dn == SET_NONE ) {
1787 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1789 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1790 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1792 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1794 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1795 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1799 /* No other possibilities from here */
1800 if( is_dn == SET_NONE ) {
1802 return( LDAP_INAPPROPRIATE_AUTH );
1805 /* Username strings */
1806 if( is_dn == SET_U ) {
1807 /* ITS#3419: values may need escape */
1809 LDAPAVA *RDNs[ 4 ][ 2 ];
1814 DN[ irdn ] = RDNs[ irdn ];
1815 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1816 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1817 AVAs[ irdn ].la_value = *dn;
1818 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1819 AVAs[ irdn ].la_private = NULL;
1820 RDNs[ irdn ][ 1 ] = NULL;
1822 if ( user_realm && *user_realm ) {
1824 DN[ irdn ] = RDNs[ irdn ];
1825 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1826 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1827 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1828 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1829 AVAs[ irdn ].la_private = NULL;
1830 RDNs[ irdn ][ 1 ] = NULL;
1833 if ( !BER_BVISNULL( mech ) ) {
1835 DN[ irdn ] = RDNs[ irdn ];
1836 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1837 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1838 AVAs[ irdn ].la_value = *mech;
1839 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1840 AVAs[ irdn ].la_private = NULL;
1841 RDNs[ irdn ][ 1 ] = NULL;
1845 DN[ irdn ] = RDNs[ irdn ];
1846 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1847 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1848 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1849 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1850 AVAs[ irdn ].la_private = NULL;
1851 RDNs[ irdn ][ 1 ] = NULL;
1856 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1858 if ( rc != LDAP_SUCCESS ) {
1863 Debug( LDAP_DEBUG_TRACE,
1864 "slap_sasl_getdn: u:id converted to %s\n",
1869 /* Dup the DN in any case, so we don't risk
1870 * leaks or dangling pointers later,
1871 * and the DN value is '\0' terminated */
1872 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1873 dn->bv_val = dn2.bv_val;
1876 /* All strings are in DN form now. Normalize if needed. */
1878 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1880 /* User DNs were constructed above and must be freed now */
1881 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1883 if ( rc != LDAP_SUCCESS ) {
1890 /* Run thru regexp */
1891 slap_sasl2dn( op, dn, &dn2, flags );
1892 if( !BER_BVISNULL( &dn2 ) ) {
1893 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1895 Debug( LDAP_DEBUG_TRACE,
1896 "slap_sasl_getdn: dn:id converted to %s\n",
1900 return( LDAP_SUCCESS );