2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2015 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 */
328 /* Now see what else needs to be fetched */
329 for( i = 0; sl.list[i].name; i++ ) {
330 const char *name = sl.list[i].name;
332 if ( name[0] == '*' ) {
333 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
334 /* Skip our private properties */
335 if ( !strcmp( name, slap_propnames[0] )) {
336 i += SLAP_SASL_PROP_COUNT - 1;
340 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
343 if ( sl.list[i].values ) {
344 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
351 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
355 op->o_bd = select_backend( &op->o_req_ndn, 1 );
358 /* For rootdn, see if we can use the rootpw */
359 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) &&
360 !BER_BVISEMPTY( &op->o_bd->be_rootpw )) {
361 struct berval cbv = BER_BVNULL;
363 /* If there's a recognized scheme, see if it's CLEARTEXT */
364 if ( lutil_passwd_scheme( op->o_bd->be_rootpw.bv_val )) {
365 if ( !strncasecmp( op->o_bd->be_rootpw.bv_val,
366 sc_cleartext.bv_val, sc_cleartext.bv_len )) {
368 /* If it's CLEARTEXT, skip past scheme spec */
369 cbv.bv_len = op->o_bd->be_rootpw.bv_len -
372 cbv.bv_val = op->o_bd->be_rootpw.bv_val +
376 /* No scheme, use the whole value */
378 cbv = op->o_bd->be_rootpw;
380 if ( !BER_BVISEMPTY( &cbv )) {
381 for( i = 0; sl.list[i].name; i++ ) {
382 const char *name = sl.list[i].name;
384 if ( name[0] == '*' ) {
385 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
387 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
390 if ( !strcasecmp(name,"userPassword") ) {
391 sl.sparams->utils->prop_set( sl.sparams->propctx,
392 sl.list[i].name, cbv.bv_val, cbv.bv_len );
399 if ( op->o_bd->be_search ) {
400 SlapReply rs = {REP_RESULT};
401 op->o_hdr = conn->c_sasl_bindop->o_hdr;
402 op->o_controls = opbuf.ob_controls;
403 op->o_tag = LDAP_REQ_SEARCH;
404 op->o_dn = conn->c_ndn;
405 op->o_ndn = conn->c_ndn;
406 op->o_callback = &cb;
407 slap_op_time( &op->o_time, &op->o_tincr );
408 op->o_do_not_cache = 1;
409 op->o_is_auth_check = 1;
410 op->o_req_dn = op->o_req_ndn;
411 op->ors_scope = LDAP_SCOPE_BASE;
412 op->ors_deref = LDAP_DEREF_NEVER;
413 op->ors_tlimit = SLAP_NO_LIMIT;
415 op->ors_filter = &generic_filter;
416 op->ors_filterstr = generic_filterstr;
417 op->o_authz = conn->c_authz;
418 /* FIXME: we want all attributes, right? */
419 op->ors_attrs = NULL;
421 rc = op->o_bd->be_search( op, &rs );
425 #if SASL_VERSION_FULL >= 0x020118
426 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
430 #if SASL_VERSION_FULL >= 0x020110
434 sasl_server_params_t *sparams,
435 struct propctx *prctx,
441 SlapReply rs = {REP_RESULT};
444 Connection *conn = NULL;
445 const struct propval *pr;
446 Modifications *modlist = NULL, **modtail = &modlist, *mod;
447 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
448 char textbuf[SLAP_TEXT_BUFLEN];
450 size_t textlen = sizeof(textbuf);
452 /* just checking if we are enabled */
453 if (!prctx) return SASL_OK;
455 if (!sparams || !user) return SASL_BADPARAM;
457 pr = sparams->utils->prop_get( sparams->propctx );
459 /* Find our DN and conn first */
460 for( i = 0; pr[i].name; i++ ) {
461 if ( pr[i].name[0] == '*' ) {
462 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
463 if ( pr[i].values && pr[i].values[0] )
464 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
467 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
468 if ( pr[i].values && pr[i].values[0] )
469 AC_MEMCPY( &op.o_req_ndn.bv_len, pr[i].values[0],
470 sizeof( op.o_req_ndn.bv_len ) );
471 } else if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
473 op.o_req_ndn.bv_val = (char *)pr[i].values[0];
477 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
479 op.o_bd = select_backend( &op.o_req_ndn, 1 );
481 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
483 pr = sparams->utils->prop_get( prctx );
484 if (!pr) return SASL_BADPARAM;
486 for (i=0; pr[i].name; i++);
487 if (!i) return SASL_BADPARAM;
489 for (i=0; pr[i].name; i++) {
490 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
491 mod->sml_op = LDAP_MOD_REPLACE;
493 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
494 mod->sml_numvals = pr[i].nvalues;
495 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
496 sizeof(struct berval));
497 for (j=0; j<pr[i].nvalues; j++) {
498 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
500 BER_BVZERO( &mod->sml_values[j] );
501 mod->sml_nvalues = NULL;
502 mod->sml_desc = NULL;
504 modtail = &mod->sml_next;
508 rc = slap_mods_check( &op, modlist, &text, textbuf, textlen, NULL );
510 if ( rc == LDAP_SUCCESS ) {
511 rc = slap_mods_no_user_mod_check( &op, modlist,
512 &text, textbuf, textlen );
514 if ( rc == LDAP_SUCCESS ) {
515 if ( conn->c_sasl_bindop ) {
516 op.o_hdr = conn->c_sasl_bindop->o_hdr;
519 memset( &oph, 0, sizeof(oph) );
520 operation_fake_init( conn, &op, ldap_pvt_thread_pool_context(), 0 );
522 op.o_tag = LDAP_REQ_MODIFY;
523 op.o_ndn = op.o_req_ndn;
525 slap_op_time( &op.o_time, &op.o_tincr );
526 op.o_do_not_cache = 1;
527 op.o_is_auth_check = 1;
528 op.o_req_dn = op.o_req_ndn;
529 op.orm_modlist = modlist;
531 rc = op.o_bd->be_modify( &op, &rs );
534 slap_mods_free( modlist, 1 );
535 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
537 #endif /* SASL_VERSION_FULL >= 2.1.16 */
539 static sasl_auxprop_plug_t slap_auxprop_plugin = {
542 NULL, /* glob_context */
543 NULL, /* auxprop_free */
546 #if SASL_VERSION_FULL >= 0x020110
547 slap_auxprop_store /* the declaration of this member changed
548 * in cyrus SASL from 2.1.15 to 2.1.16 */
556 const sasl_utils_t *utils,
559 sasl_auxprop_plug_t **plug,
560 const char *plugname)
562 if ( !out_version || !plug ) return SASL_BADPARAM;
564 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
566 *out_version = SASL_AUXPROP_PLUG_VERSION;
567 *plug = &slap_auxprop_plugin;
571 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
572 * auxiliary property, so that we can refer to it in sasl_authorize
573 * without interfering with anything else. Also, the SASL username
574 * buffer is constrained to 256 characters, and our DNs could be
575 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
578 slap_sasl_canonicalize(
584 const char *user_realm,
589 Connection *conn = (Connection *)context;
590 struct propctx *props = sasl_auxprop_getctx( sconn );
591 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
594 const char *names[2];
599 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
600 conn ? (long) conn->c_connid : -1L,
601 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
602 in ? in : "<empty>");
604 /* If name is too big, just truncate. We don't care, we're
605 * using DNs, not the usernames.
607 if ( inlen > out_max )
610 /* This is a Simple Bind using SPASSWD. That means the in-directory
611 * userPassword of the Binding user already points at SASL, so it
612 * cannot be used to actually satisfy a password comparison. Just
613 * ignore it, some other mech will process it.
615 if ( !conn->c_sasl_bindop ||
616 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
618 /* See if we need to add request, can only do it once */
619 prop_getnames( props, slap_propnames, auxvals );
620 if ( !auxvals[0].name )
621 prop_request( props, slap_propnames );
623 if ( flags & SASL_CU_AUTHID )
624 which = SLAP_SASL_PROP_AUTHCLEN;
626 which = SLAP_SASL_PROP_AUTHZLEN;
628 /* Need to store the Connection for auxprop_lookup */
629 if ( !auxvals[SLAP_SASL_PROP_CONN].values ) {
630 names[0] = slap_propnames[SLAP_SASL_PROP_CONN];
632 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
635 /* Already been here? */
636 if ( auxvals[which].values )
639 /* Normally we require an authzID to have a u: or dn: prefix.
640 * However, SASL frequently gives us an authzID that is just
641 * an exact copy of the authcID, without a prefix. We need to
642 * detect and allow this condition. If SASL calls canonicalize
643 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
644 * But if it's broken into two calls, we need to remember the
645 * authcID so that we can compare the authzID later. We store
646 * the authcID temporarily in conn->c_sasl_dn. We necessarily
647 * finish Canonicalizing before Authorizing, so there is no
648 * conflict with slap_sasl_authorize's use of this temp var.
650 * The SASL EXTERNAL mech is backwards from all the other mechs,
651 * it does authzID before the authcID. If we see that authzID
652 * has already been done, don't do anything special with authcID.
654 if ( flags == SASL_CU_AUTHID && !auxvals[SLAP_SASL_PROP_AUTHZ].values ) {
655 conn->c_sasl_dn.bv_val = (char *) in;
656 conn->c_sasl_dn.bv_len = 0;
657 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
658 rc = strcmp( in, conn->c_sasl_dn.bv_val );
659 conn->c_sasl_dn.bv_val = NULL;
660 /* They were equal, no work needed */
661 if ( !rc ) goto done;
664 bvin.bv_val = (char *)in;
666 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
667 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
668 if ( rc != LDAP_SUCCESS ) {
669 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
673 names[0] = slap_propnames[which];
675 prop_set( props, names[0], (char *)&dn.bv_len, sizeof( dn.bv_len ) );
678 names[0] = slap_propnames[which];
679 prop_set( props, names[0], dn.bv_val, dn.bv_len );
681 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
682 conn ? (long) conn->c_connid : -1L, names[0]+1,
683 dn.bv_val ? dn.bv_val : "<EMPTY>" );
685 /* Not needed any more, SASL has copied it */
686 if ( conn && conn->c_sasl_bindop )
687 conn->c_sasl_bindop->o_tmpfree( dn.bv_val, conn->c_sasl_bindop->o_tmpmemctx );
690 AC_MEMCPY( out, in, inlen );
702 char *requested_user,
706 const char *def_realm,
708 struct propctx *props)
710 Connection *conn = (Connection *)context;
712 * (SLAP_SASL_PROP_COUNT - 1) because we skip "conn",
713 * + 1 for NULL termination?
715 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
716 struct berval authcDN, authzDN = BER_BVNULL;
719 /* Simple Binds don't support proxy authorization, ignore it */
720 if ( !conn->c_sasl_bindop ||
721 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
723 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
724 "authcid=\"%s\" authzid=\"%s\"\n",
725 conn ? (long) conn->c_connid : -1L, auth_identity, requested_user );
726 if ( conn->c_sasl_dn.bv_val ) {
727 BER_BVZERO( &conn->c_sasl_dn );
730 /* Skip SLAP_SASL_PROP_CONN */
731 prop_getnames( props, slap_propnames+1, auxvals );
733 /* Should not happen */
734 if ( !auxvals[0].values ) {
735 sasl_seterror( sconn, 0, "invalid authcid" );
739 AC_MEMCPY( &authcDN.bv_len, auxvals[0].values[0], sizeof(authcDN.bv_len) );
740 authcDN.bv_val = auxvals[1].values ? (char *)auxvals[1].values[0] : NULL;
741 conn->c_sasl_dn = authcDN;
743 /* Nothing to do if no authzID was given */
744 if ( !auxvals[2].name || !auxvals[2].values ) {
748 AC_MEMCPY( &authzDN.bv_len, auxvals[2].values[0], sizeof(authzDN.bv_len) );
749 authzDN.bv_val = auxvals[3].values ? (char *)auxvals[3].values[0] : NULL;
751 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
752 if ( rc != LDAP_SUCCESS ) {
753 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
754 "proxy authorization disallowed (%d)\n",
755 conn ? (long) conn->c_connid : -1L, rc, 0 );
757 sasl_seterror( sconn, 0, "not authorized" );
761 /* FIXME: we need yet another dup because slap_sasl_getdn()
762 * is using the bind operation slab */
763 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
766 if (conn->c_sasl_bindop) {
767 Statslog( LDAP_DEBUG_STATS,
768 "%s BIND authcid=\"%s\" authzid=\"%s\"\n",
769 conn->c_sasl_bindop->o_log_prefix,
770 auth_identity, requested_user, 0, 0 );
773 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
774 " proxy authorization allowed authzDN=\"%s\"\n",
775 conn ? (long) conn->c_connid : -1L,
776 authzDN.bv_val ? authzDN.bv_val : "", 0 );
781 slap_sasl_err2ldap( int saslerr )
785 /* map SASL errors to LDAP resultCode returned by:
787 * SASL_OK, SASL_NOMEM
789 * SASL_OK, SASL_CONTINUE, SASL_TRANS, SASL_BADPARAM, SASL_BADPROT,
791 * sasl_server_start()
794 * SASL_OK, SASL_BADPARAM
802 rc = LDAP_SASL_BIND_IN_PROGRESS;
809 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
815 rc = LDAP_INVALID_CREDENTIALS;
818 rc = LDAP_INSUFFICIENT_ACCESS;
822 rc = LDAP_INAPPROPRIATE_AUTH;
826 rc = LDAP_UNAVAILABLE;
829 rc = LDAP_UNWILLING_TO_PERFORM;
841 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
844 const struct berval *sc,
845 const struct berval * passwd,
846 const struct berval * cred,
851 void *ctx, *sconn = NULL;
853 for( i=0; i<cred->bv_len; i++) {
854 if(cred->bv_val[i] == '\0') {
855 return LUTIL_PASSWD_ERR; /* NUL character in password */
859 if( cred->bv_val[i] != '\0' ) {
860 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
863 for( i=0; i<passwd->bv_len; i++) {
864 if(passwd->bv_val[i] == '\0') {
865 return LUTIL_PASSWD_ERR; /* NUL character in password */
869 if( passwd->bv_val[i] != '\0' ) {
870 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
873 rtn = LUTIL_PASSWD_ERR;
875 ctx = ldap_pvt_thread_pool_context();
876 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_sasl_bind, &sconn, NULL );
878 if( sconn != NULL ) {
880 sc = sasl_checkpass( sconn,
881 passwd->bv_val, passwd->bv_len,
882 cred->bv_val, cred->bv_len );
883 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
888 #endif /* SLAPD_SPASSWD */
890 #endif /* HAVE_CYRUS_SASL */
892 #ifdef ENABLE_REWRITE
894 typedef struct slapd_map_data {
896 struct berval filter;
897 AttributeName attrs[2];
902 slapd_rw_config( const char *fname, int lineno, int argc, char **argv )
904 slapd_map_data *ret = NULL;
905 LDAPURLDesc *lud = NULL;
907 AttributeDescription *ad = NULL;
909 struct berval dn, ndn;
912 Debug( LDAP_DEBUG_ANY,
913 "[%s:%d] slapd map needs URI\n",
919 if ( strncasecmp( uri, "uri=", STRLENOF( "uri=" ) ) == 0 ) {
920 uri += STRLENOF( "uri=" );
923 if ( ldap_url_parse( uri, &lud ) != LDAP_URL_SUCCESS ) {
924 Debug( LDAP_DEBUG_ANY,
925 "[%s:%d] illegal URI '%s'\n",
926 fname, lineno, uri );
930 if ( strcasecmp( lud->lud_scheme, "ldap" )) {
931 Debug( LDAP_DEBUG_ANY,
932 "[%s:%d] illegal URI scheme '%s'\n",
933 fname, lineno, lud->lud_scheme );
937 if (( lud->lud_host && lud->lud_host[0] ) || lud->lud_exts
939 Debug( LDAP_DEBUG_ANY,
940 "[%s:%d] illegal URI '%s'\n",
941 fname, lineno, uri );
945 if ( lud->lud_attrs ) {
946 if ( lud->lud_attrs[1] ) {
947 Debug( LDAP_DEBUG_ANY,
948 "[%s:%d] only one attribute allowed in URI\n",
952 if ( strcasecmp( lud->lud_attrs[0], "dn" ) &&
953 strcasecmp( lud->lud_attrs[0], "entryDN" )) {
955 rc = slap_str2ad( lud->lud_attrs[0], &ad, &text );
960 ber_str2bv( lud->lud_dn, 0, 0, &dn );
961 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL ))
964 if ( lud->lud_filter ) {
965 flen = strlen( lud->lud_filter ) + 1;
967 ret = ch_malloc( sizeof( slapd_map_data ) + flen );
970 ret->filter.bv_val = (char *)(ret+1);
971 ret->filter.bv_len = flen - 1;
972 strcpy( ret->filter.bv_val, lud->lud_filter );
974 BER_BVZERO( &ret->filter );
976 ret->scope = lud->lud_scope;
978 ret->attrs[0].an_name = ad->ad_cname;
980 BER_BVZERO( &ret->attrs[0].an_name );
982 ret->attrs[0].an_desc = ad;
983 BER_BVZERO( &ret->attrs[1].an_name );
985 ldap_free_urldesc( lud );
989 struct slapd_rw_info {
990 slapd_map_data *si_data;
991 struct berval si_val;
995 slapd_rw_cb( Operation *op, SlapReply *rs )
997 if ( rs->sr_type == REP_SEARCH ) {
998 struct slapd_rw_info *si = op->o_callback->sc_private;
1000 if ( si->si_data->attrs[0].an_desc ) {
1003 a = attr_find( rs->sr_entry->e_attrs,
1004 si->si_data->attrs[0].an_desc );
1006 ber_dupbv( &si->si_val, a->a_vals );
1009 ber_dupbv( &si->si_val, &rs->sr_entry->e_name );
1012 return LDAP_SUCCESS;
1016 slapd_rw_apply( void *private, const char *filter, struct berval *val )
1018 slapd_map_data *sl = private;
1019 slap_callback cb = { NULL };
1020 Connection conn = {0};
1021 OperationBuffer opbuf;
1024 SlapReply rs = {REP_RESULT};
1025 struct slapd_rw_info si;
1029 thrctx = ldap_pvt_thread_pool_context();
1030 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1033 op->o_tag = LDAP_REQ_SEARCH;
1034 op->o_req_dn = op->o_req_ndn = sl->base;
1035 op->o_bd = select_backend( &op->o_req_ndn, 1 );
1040 BER_BVZERO( &si.si_val );
1041 op->ors_scope = sl->scope;
1042 op->ors_deref = LDAP_DEREF_NEVER;
1044 op->ors_tlimit = SLAP_NO_LIMIT;
1045 if ( sl->attrs[0].an_desc ) {
1046 op->ors_attrs = sl->attrs;
1048 op->ors_attrs = slap_anlist_no_attrs;
1051 rc = strlen( filter );
1055 rc += sl->filter.bv_len;
1056 ptr = op->ors_filterstr.bv_val = op->o_tmpalloc( rc + 1, op->o_tmpmemctx );
1057 if ( sl->filter.bv_len ) {
1058 ptr = lutil_strcopy( ptr, sl->filter.bv_val );
1063 strcpy( ptr, filter );
1065 op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
1066 if ( !op->ors_filter ) {
1067 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1071 op->ors_attrsonly = 0;
1072 op->o_dn = op->o_bd->be_rootdn;
1073 op->o_ndn = op->o_bd->be_rootndn;
1074 op->o_do_not_cache = 1;
1076 cb.sc_response = slapd_rw_cb;
1077 cb.sc_private = &si;
1078 op->o_callback = &cb;
1080 rc = op->o_bd->be_search( op, &rs );
1081 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &si.si_val )) {
1083 rc = REWRITE_SUCCESS;
1085 if ( !BER_BVISNULL( &si.si_val )) {
1086 ch_free( si.si_val.bv_val );
1090 filter_free_x( op, op->ors_filter, 1 );
1091 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1096 slapd_rw_destroy( void *private )
1098 slapd_map_data *md = private;
1100 assert( private != NULL );
1102 ch_free( md->base.bv_val );
1108 static const rewrite_mapper slapd_mapper = {
1116 int slap_sasl_init( void )
1118 #ifdef HAVE_CYRUS_SASL
1120 static sasl_callback_t server_callbacks[] = {
1121 { SASL_CB_LOG, (slap_sasl_cb_ft)&slap_sasl_log, NULL },
1122 { SASL_CB_GETOPT, (slap_sasl_cb_ft)&slap_sasl_getopt, NULL },
1123 { SASL_CB_LIST_END, NULL, NULL }
1127 #ifdef ENABLE_REWRITE
1128 rewrite_mapper_register( &slapd_mapper );
1131 #ifdef HAVE_CYRUS_SASL
1132 #ifdef HAVE_SASL_VERSION
1133 /* stringify the version number, sasl.h doesn't do it for us */
1134 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1135 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1136 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1139 sasl_version( NULL, &rc );
1140 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1141 (rc & 0xffff) < SASL_VERSION_STEP)
1143 char version[sizeof("xxx.xxx.xxxxx")];
1144 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1146 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
1147 " expected %s, got %s\n",
1148 SASL_VERSION_STRING, version, 0 );
1154 ldap_pvt_sasl_mutex_new,
1155 ldap_pvt_sasl_mutex_lock,
1156 ldap_pvt_sasl_mutex_unlock,
1157 ldap_pvt_sasl_mutex_dispose );
1159 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1161 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1162 if( rc != SASL_OK ) {
1163 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
1168 /* should provide callbacks for logging */
1169 /* server name should be configurable */
1170 rc = sasl_server_init( server_callbacks, "slapd" );
1172 if( rc != SASL_OK ) {
1173 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
1179 #ifdef SLAPD_SPASSWD
1180 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
1183 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1186 /* default security properties */
1187 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1188 sasl_secprops.max_ssf = INT_MAX;
1189 sasl_secprops.maxbufsize = 65536;
1190 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1196 int slap_sasl_destroy( void )
1198 #ifdef HAVE_CYRUS_SASL
1208 slap_sasl_peer2ipport( struct berval *peer )
1212 *addr = &peer->bv_val[ STRLENOF( "IP=" ) ];
1213 ber_len_t plen = peer->bv_len - STRLENOF( "IP=" );
1216 if ( addr[0] == '[' ) {
1220 ipport = ch_strdup( &addr[isv6] );
1222 /* Convert IPv6/IPv4 addresses to address;port syntax. */
1223 p = strrchr( ipport, ':' );
1227 assert( p[-1] == ']' );
1228 AC_MEMCPY( &p[-1], p, plen - ( p - ipport ) + 1 );
1231 } else if ( isv6 ) {
1234 assert( addr[plen] == ']' );
1241 int slap_sasl_open( Connection *conn, int reopen )
1243 int sc = LDAP_SUCCESS;
1244 #ifdef HAVE_CYRUS_SASL
1247 sasl_conn_t *ctx = NULL;
1248 sasl_callback_t *session_callbacks;
1249 char *ipremoteport = NULL, *iplocalport = NULL;
1251 assert( conn->c_sasl_authctx == NULL );
1254 assert( conn->c_sasl_extra == NULL );
1257 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1258 if( session_callbacks == NULL ) {
1259 Debug( LDAP_DEBUG_ANY,
1260 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1263 conn->c_sasl_extra = session_callbacks;
1265 session_callbacks[cb=0].id = SASL_CB_LOG;
1266 session_callbacks[cb].proc = (slap_sasl_cb_ft)&slap_sasl_log;
1267 session_callbacks[cb++].context = conn;
1269 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1270 session_callbacks[cb].proc = (slap_sasl_cb_ft)&slap_sasl_authorize;
1271 session_callbacks[cb++].context = conn;
1273 session_callbacks[cb].id = SASL_CB_CANON_USER;
1274 session_callbacks[cb].proc = (slap_sasl_cb_ft)&slap_sasl_canonicalize;
1275 session_callbacks[cb++].context = conn;
1277 session_callbacks[cb].id = SASL_CB_LIST_END;
1278 session_callbacks[cb].proc = NULL;
1279 session_callbacks[cb++].context = NULL;
1281 session_callbacks = conn->c_sasl_extra;
1284 conn->c_sasl_layers = 0;
1286 /* create new SASL context */
1287 if ( conn->c_sock_name.bv_len != 0 &&
1288 strncmp( conn->c_sock_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1290 iplocalport = slap_sasl_peer2ipport( &conn->c_sock_name );
1293 if ( conn->c_peer_name.bv_len != 0 &&
1294 strncmp( conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1296 ipremoteport = slap_sasl_peer2ipport( &conn->c_peer_name );
1299 sc = sasl_server_new( "ldap", sasl_host, global_realm,
1300 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1301 if ( iplocalport != NULL ) {
1302 ch_free( iplocalport );
1304 if ( ipremoteport != NULL ) {
1305 ch_free( ipremoteport );
1308 if( sc != SASL_OK ) {
1309 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1315 conn->c_sasl_authctx = ctx;
1317 if( sc == SASL_OK ) {
1318 sc = sasl_setprop( ctx,
1319 SASL_SEC_PROPS, &sasl_secprops );
1321 if( sc != SASL_OK ) {
1322 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1325 slap_sasl_close( conn );
1330 sc = slap_sasl_err2ldap( sc );
1332 #elif defined(SLAP_BUILTIN_SASL)
1333 /* built-in SASL implementation */
1334 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1335 if( ctx == NULL ) return -1;
1337 ctx->sc_external_ssf = 0;
1338 BER_BVZERO( &ctx->sc_external_id );
1340 conn->c_sasl_authctx = ctx;
1346 int slap_sasl_external(
1349 struct berval *auth_id )
1351 #ifdef HAVE_CYRUS_SASL
1353 sasl_conn_t *ctx = conn->c_sasl_authctx;
1354 sasl_ssf_t sasl_ssf = ssf;
1356 if ( ctx == NULL ) {
1357 return LDAP_UNAVAILABLE;
1360 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
1362 if ( sc != SASL_OK ) {
1366 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1367 auth_id ? auth_id->bv_val : NULL );
1369 if ( sc != SASL_OK ) {
1372 #elif defined(SLAP_BUILTIN_SASL)
1373 /* built-in SASL implementation */
1374 SASL_CTX *ctx = conn->c_sasl_authctx;
1375 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1377 ctx->sc_external_ssf = ssf;
1379 ctx->sc_external_id = *auth_id;
1380 BER_BVZERO( auth_id );
1382 BER_BVZERO( &ctx->sc_external_id );
1386 return LDAP_SUCCESS;
1389 int slap_sasl_reset( Connection *conn )
1391 return LDAP_SUCCESS;
1394 char ** slap_sasl_mechs( Connection *conn )
1396 char **mechs = NULL;
1398 #ifdef HAVE_CYRUS_SASL
1399 sasl_conn_t *ctx = conn->c_sasl_authctx;
1401 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1405 SASL_CONST char *mechstr;
1407 sc = sasl_listmech( ctx,
1408 NULL, NULL, ",", NULL,
1409 &mechstr, NULL, NULL );
1411 if( sc != SASL_OK ) {
1412 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1418 mechs = ldap_str2charray( mechstr, "," );
1420 #elif defined(SLAP_BUILTIN_SASL)
1421 /* builtin SASL implementation */
1422 SASL_CTX *ctx = conn->c_sasl_authctx;
1423 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1424 /* should check ssf */
1425 mechs = ldap_str2charray( "EXTERNAL", "," );
1432 int slap_sasl_close( Connection *conn )
1434 #ifdef HAVE_CYRUS_SASL
1435 sasl_conn_t *ctx = conn->c_sasl_authctx;
1438 sasl_dispose( &ctx );
1440 if ( conn->c_sasl_sockctx &&
1441 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1443 ctx = conn->c_sasl_sockctx;
1444 sasl_dispose( &ctx );
1447 conn->c_sasl_authctx = NULL;
1448 conn->c_sasl_sockctx = NULL;
1449 conn->c_sasl_done = 0;
1451 free( conn->c_sasl_extra );
1452 conn->c_sasl_extra = NULL;
1454 #elif defined(SLAP_BUILTIN_SASL)
1455 SASL_CTX *ctx = conn->c_sasl_authctx;
1457 if( ctx->sc_external_id.bv_val ) {
1458 free( ctx->sc_external_id.bv_val );
1459 BER_BVZERO( &ctx->sc_external_id );
1462 conn->c_sasl_authctx = NULL;
1466 return LDAP_SUCCESS;
1469 int slap_sasl_bind( Operation *op, SlapReply *rs )
1471 #ifdef HAVE_CYRUS_SASL
1472 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1473 struct berval response;
1474 unsigned reslen = 0;
1477 Debug(LDAP_DEBUG_ARGS,
1478 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1479 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1480 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1481 op->o_conn->c_sasl_bind_mech.bv_val,
1482 op->orb_cred.bv_len );
1485 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1486 "SASL unavailable on this session" );
1490 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1491 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1492 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1493 sasl_server_step( ctx, cred, clen, resp, rlen )
1495 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1496 /* If we already authenticated once, must use a new context */
1497 if ( op->o_conn->c_sasl_done ) {
1499 const char *authid = NULL;
1500 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1501 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1502 if ( authid ) authid = ch_strdup( authid );
1503 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1504 sasl_dispose( &ctx );
1506 op->o_conn->c_sasl_authctx = NULL;
1508 slap_sasl_open( op->o_conn, 1 );
1509 ctx = op->o_conn->c_sasl_authctx;
1511 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1512 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1513 ch_free( (char *)authid );
1517 op->o_conn->c_sasl_bind_mech.bv_val,
1518 op->orb_cred.bv_val, op->orb_cred.bv_len,
1519 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1523 op->orb_cred.bv_val, op->orb_cred.bv_len,
1524 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1527 response.bv_len = reslen;
1529 if ( sc == SASL_OK ) {
1530 sasl_ssf_t *ssf = NULL;
1532 ber_dupbv_x( &op->orb_edn, &op->o_conn->c_sasl_dn, op->o_tmpmemctx );
1533 BER_BVZERO( &op->o_conn->c_sasl_dn );
1534 op->o_conn->c_sasl_done = 1;
1536 rs->sr_err = LDAP_SUCCESS;
1538 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1539 op->orb_ssf = ssf ? *ssf : 0;
1543 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1544 op->o_conn->c_sasl_layers++;
1546 /* If there's an old layer, set sockctx to NULL to
1547 * tell connection_read() to wait for us to finish.
1548 * Otherwise there is a race condition: we have to
1549 * send the Bind response using the old security
1550 * context and then remove it before reading any
1553 if ( op->o_conn->c_sasl_sockctx ) {
1554 ctx = op->o_conn->c_sasl_sockctx;
1555 op->o_conn->c_sasl_sockctx = NULL;
1557 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1559 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1562 /* Must send response using old security layer */
1563 rs->sr_sasldata = (response.bv_len ? &response : NULL);
1564 send_ldap_sasl( op, rs );
1566 /* Now dispose of the old security layer.
1569 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1570 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1571 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1572 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1573 sasl_dispose( &ctx );
1575 } else if ( sc == SASL_CONTINUE ) {
1576 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1577 rs->sr_text = sasl_errdetail( ctx );
1578 rs->sr_sasldata = &response;
1579 send_ldap_sasl( op, rs );
1582 BER_BVZERO( &op->o_conn->c_sasl_dn );
1583 rs->sr_text = sasl_errdetail( ctx );
1584 rs->sr_err = slap_sasl_err2ldap( sc ),
1585 send_ldap_result( op, rs );
1588 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1590 #elif defined(SLAP_BUILTIN_SASL)
1591 /* built-in SASL implementation */
1592 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1594 if ( ctx == NULL ) {
1595 send_ldap_error( op, rs, LDAP_OTHER,
1596 "Internal SASL Error" );
1598 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1601 if( op->orb_cred.bv_len ) {
1602 rs->sr_text = "proxy authorization not supported";
1603 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1604 send_ldap_result( op, rs );
1607 op->orb_edn = ctx->sc_external_id;
1608 rs->sr_err = LDAP_SUCCESS;
1609 rs->sr_sasldata = NULL;
1610 send_ldap_sasl( op, rs );
1614 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1615 "requested SASL mechanism not supported" );
1618 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1619 "SASL not supported" );
1625 char* slap_sasl_secprops( const char *in )
1627 #ifdef HAVE_CYRUS_SASL
1628 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1630 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1632 return "SASL not supported";
1636 void slap_sasl_secprops_unparse( struct berval *bv )
1638 #ifdef HAVE_CYRUS_SASL
1639 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1643 #ifdef HAVE_CYRUS_SASL
1645 slap_sasl_setpass( Operation *op, SlapReply *rs )
1647 struct berval id = BER_BVNULL; /* needs to come from connection */
1648 struct berval new = BER_BVNULL;
1649 struct berval old = BER_BVNULL;
1651 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1653 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1654 (SASL_CONST void **)(char *)&id.bv_val );
1656 if( rs->sr_err != SASL_OK ) {
1657 rs->sr_text = "unable to retrieve SASL username";
1658 rs->sr_err = LDAP_OTHER;
1662 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1663 id.bv_val ? id.bv_val : "", 0, 0 );
1665 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1666 NULL, &old, &new, &rs->sr_text );
1668 if( rs->sr_err != LDAP_SUCCESS ) {
1672 if( new.bv_len == 0 ) {
1673 slap_passwd_generate(&new);
1675 if( new.bv_len == 0 ) {
1676 rs->sr_text = "password generation failed.";
1677 rs->sr_err = LDAP_OTHER;
1681 rs->sr_rspdata = slap_passwd_return( &new );
1684 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1685 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1686 if( rs->sr_err != SASL_OK ) {
1687 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1689 switch(rs->sr_err) {
1691 rs->sr_err = LDAP_SUCCESS;
1701 rs->sr_err = LDAP_OTHER;
1707 #endif /* HAVE_CYRUS_SASL */
1709 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1710 * string returned in *dn is in its own allocated memory, and must be free'd
1711 * by the calling process. -Mark Adamson, Carnegie Mellon
1713 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1714 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1721 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1722 char *user_realm, struct berval *dn, int flags )
1724 int rc, is_dn = SET_NONE, do_norm = 1;
1725 struct berval dn2, *mech;
1727 assert( conn != NULL );
1728 assert( id != NULL );
1730 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1732 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1733 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1734 (unsigned long) id->bv_len ) );
1737 op = conn->c_sasl_bindop;
1739 assert( op != NULL );
1743 if ( !BER_BVISNULL( id ) ) {
1744 /* Blatantly anonymous ID */
1745 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1747 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1748 return( LDAP_SUCCESS );
1752 /* FIXME: if empty, should we stop? */
1753 BER_BVSTR( id, "" );
1756 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1757 mech = &conn->c_sasl_bind_mech;
1759 mech = &conn->c_authmech;
1762 /* An authcID needs to be converted to authzID form. Set the
1763 * values directly into *dn; they will be normalized later. (and
1764 * normalizing always makes a new copy.) An ID from a TLS certificate
1765 * is already normalized, so copy it and skip normalization.
1767 if( flags & SLAP_GETDN_AUTHCID ) {
1768 if( bvmatch( mech, &ext_bv )) {
1769 /* EXTERNAL DNs are already normalized */
1770 assert( !BER_BVISNULL( id ) );
1774 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1777 /* convert to u:<username> form */
1783 if( is_dn == SET_NONE ) {
1784 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1786 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1787 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1789 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1791 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1792 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1796 /* No other possibilities from here */
1797 if( is_dn == SET_NONE ) {
1799 return( LDAP_INAPPROPRIATE_AUTH );
1802 /* Username strings */
1803 if( is_dn == SET_U ) {
1804 /* ITS#3419: values may need escape */
1806 LDAPAVA *RDNs[ 4 ][ 2 ];
1811 DN[ irdn ] = RDNs[ irdn ];
1812 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1813 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1814 AVAs[ irdn ].la_value = *dn;
1815 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1816 AVAs[ irdn ].la_private = NULL;
1817 RDNs[ irdn ][ 1 ] = NULL;
1819 if ( user_realm && *user_realm ) {
1821 DN[ irdn ] = RDNs[ irdn ];
1822 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1823 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1824 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1825 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1826 AVAs[ irdn ].la_private = NULL;
1827 RDNs[ irdn ][ 1 ] = NULL;
1830 if ( !BER_BVISNULL( mech ) ) {
1832 DN[ irdn ] = RDNs[ irdn ];
1833 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1834 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1835 AVAs[ irdn ].la_value = *mech;
1836 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1837 AVAs[ irdn ].la_private = NULL;
1838 RDNs[ irdn ][ 1 ] = NULL;
1842 DN[ irdn ] = RDNs[ irdn ];
1843 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1844 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1845 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1846 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1847 AVAs[ irdn ].la_private = NULL;
1848 RDNs[ irdn ][ 1 ] = NULL;
1853 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1855 if ( rc != LDAP_SUCCESS ) {
1860 Debug( LDAP_DEBUG_TRACE,
1861 "slap_sasl_getdn: u:id converted to %s\n",
1866 /* Dup the DN in any case, so we don't risk
1867 * leaks or dangling pointers later,
1868 * and the DN value is '\0' terminated */
1869 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1870 dn->bv_val = dn2.bv_val;
1873 /* All strings are in DN form now. Normalize if needed. */
1875 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1877 /* User DNs were constructed above and must be freed now */
1878 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1880 if ( rc != LDAP_SUCCESS ) {
1887 /* Run thru regexp */
1888 slap_sasl2dn( op, dn, &dn2, flags );
1889 if( !BER_BVISNULL( &dn2 ) ) {
1890 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1892 Debug( LDAP_DEBUG_TRACE,
1893 "slap_sasl_getdn: dn:id converted to %s\n",
1897 return( LDAP_SUCCESS );