2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2009 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 char *slap_sasl_auxprops;
68 #ifdef HAVE_CYRUS_SASL
70 /* Just use our internal auxprop by default */
74 const char *plugin_name,
79 if ( strcmp( option, "auxprop_plugin" )) {
82 if ( slap_sasl_auxprops )
83 *result = slap_sasl_auxprops;
95 Connection *conn = context;
99 if ( message == NULL ) {
100 return SASL_BADPARAM;
105 level = LDAP_DEBUG_NONE;
109 level = LDAP_DEBUG_ANY;
113 level = LDAP_DEBUG_ANY;
117 level = LDAP_DEBUG_TRACE;
121 level = LDAP_DEBUG_TRACE;
125 level = LDAP_DEBUG_TRACE;
129 level = LDAP_DEBUG_TRACE;
133 level = LDAP_DEBUG_TRACE;
134 label = "Password Trace";
137 return SASL_BADPARAM;
140 Debug( level, "SASL [conn=%ld] %s: %s\n",
141 conn ? (long) conn->c_connid: -1L,
148 static const char *slap_propnames[] = {
149 "*slapConn", "*slapAuthcDNlen", "*slapAuthcDN",
150 "*slapAuthzDNlen", "*slapAuthzDN", NULL };
152 static Filter generic_filter = { LDAP_FILTER_PRESENT, { 0 }, NULL };
153 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
155 #define SLAP_SASL_PROP_CONN 0
156 #define SLAP_SASL_PROP_AUTHCLEN 1
157 #define SLAP_SASL_PROP_AUTHC 2
158 #define SLAP_SASL_PROP_AUTHZLEN 3
159 #define SLAP_SASL_PROP_AUTHZ 4
160 #define SLAP_SASL_PROP_COUNT 5 /* Number of properties we used */
162 typedef struct lookup_info {
164 const struct propval *list;
165 sasl_server_params_t *sparams;
168 static slap_response sasl_ap_lookup;
170 static struct berval sc_cleartext = BER_BVC("{CLEARTEXT}");
173 sasl_ap_lookup( Operation *op, SlapReply *rs )
176 AttributeDescription *ad;
180 lookup_info *sl = (lookup_info *)op->o_callback->sc_private;
182 if (rs->sr_type != REP_SEARCH) return 0;
184 for( i = 0; sl->list[i].name; i++ ) {
185 const char *name = sl->list[i].name;
187 if ( name[0] == '*' ) {
188 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
189 /* Skip our private properties */
190 if ( !strcmp( name, slap_propnames[0] )) {
191 i += SLAP_SASL_PROP_COUNT - 1;
195 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
198 if ( sl->list[i].values ) {
199 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
202 rc = slap_str2ad( name, &ad, &text );
203 if ( rc != LDAP_SUCCESS ) {
204 Debug( LDAP_DEBUG_TRACE,
205 "slap_ap_lookup: str2ad(%s): %s\n", name, text, 0 );
209 /* If it's the rootdn and a rootpw was present, we already set
210 * it so don't override it here.
212 if ( ad == slap_schema.si_ad_userPassword && sl->list[i].values &&
213 be_isroot_dn( op->o_bd, &op->o_req_ndn ))
216 a = attr_find( rs->sr_entry->e_attrs, ad );
218 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
221 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
222 sl->sparams->utils->prop_erase( sl->sparams->propctx,
225 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
226 /* ITS#3846 don't give hashed passwords to SASL */
227 if ( ad == slap_schema.si_ad_userPassword &&
228 bv->bv_val[0] == '{' /*}*/ )
230 if ( lutil_passwd_scheme( bv->bv_val ) ) {
231 /* If it's not a recognized scheme, just assume it's
232 * a cleartext password that happened to include brackets.
234 * If it's a recognized scheme, skip this value, unless the
235 * scheme is {CLEARTEXT}. In that case, skip over the
236 * scheme name and use the remainder. If there is nothing
237 * past the scheme name, skip this value.
239 #ifdef SLAPD_CLEARTEXT
240 if ( !strncasecmp( bv->bv_val, sc_cleartext.bv_val,
241 sc_cleartext.bv_len )) {
243 cbv.bv_len = bv->bv_len - sc_cleartext.bv_len;
244 if ( cbv.bv_len > 0 ) {
245 cbv.bv_val = bv->bv_val + sc_cleartext.bv_len;
246 sl->sparams->utils->prop_set( sl->sparams->propctx,
247 sl->list[i].name, cbv.bv_val, cbv.bv_len );
254 sl->sparams->utils->prop_set( sl->sparams->propctx,
255 sl->list[i].name, bv->bv_val, bv->bv_len );
261 #if SASL_VERSION_FULL >= 0x020118
268 sasl_server_params_t *sparams,
273 OperationBuffer opbuf = {{ NULL }};
274 Operation *op = (Operation *)&opbuf;
276 Connection *conn = NULL;
278 int rc = LDAP_SUCCESS;
280 sl.list = sparams->utils->prop_get( sparams->propctx );
281 sl.sparams = sparams;
284 /* Find our DN and conn first */
285 for( i = 0; sl.list[i].name; i++ ) {
286 if ( sl.list[i].name[0] == '*' ) {
287 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
288 if ( sl.list[i].values && sl.list[i].values[0] )
289 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
292 if ( flags & SASL_AUXPROP_AUTHZID ) {
293 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZLEN] )) {
294 if ( sl.list[i].values && sl.list[i].values[0] )
295 AC_MEMCPY( &op->o_req_ndn.bv_len, sl.list[i].values[0],
296 sizeof( op->o_req_ndn.bv_len ) );
297 } else if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZ] )) {
298 if ( sl.list[i].values )
299 op->o_req_ndn.bv_val = (char *)sl.list[i].values[0];
304 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
305 if ( sl.list[i].values && sl.list[i].values[0] )
306 AC_MEMCPY( &op->o_req_ndn.bv_len, sl.list[i].values[0],
307 sizeof( op->o_req_ndn.bv_len ) );
308 } else if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
309 if ( sl.list[i].values ) {
310 op->o_req_ndn.bv_val = (char *)sl.list[i].values[0];
311 if ( !(flags & SASL_AUXPROP_AUTHZID) )
318 /* Now see what else needs to be fetched */
319 for( i = 0; sl.list[i].name; i++ ) {
320 const char *name = sl.list[i].name;
322 if ( name[0] == '*' ) {
323 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
324 /* Skip our private properties */
325 if ( !strcmp( name, slap_propnames[0] )) {
326 i += SLAP_SASL_PROP_COUNT - 1;
330 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
333 if ( sl.list[i].values ) {
334 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
341 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
345 op->o_bd = select_backend( &op->o_req_ndn, 1 );
348 /* For rootdn, see if we can use the rootpw */
349 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) &&
350 !BER_BVISEMPTY( &op->o_bd->be_rootpw )) {
351 struct berval cbv = BER_BVNULL;
353 /* If there's a recognized scheme, see if it's CLEARTEXT */
354 if ( lutil_passwd_scheme( op->o_bd->be_rootpw.bv_val )) {
355 if ( !strncasecmp( op->o_bd->be_rootpw.bv_val,
356 sc_cleartext.bv_val, sc_cleartext.bv_len )) {
358 /* If it's CLEARTEXT, skip past scheme spec */
359 cbv.bv_len = op->o_bd->be_rootpw.bv_len -
362 cbv.bv_val = op->o_bd->be_rootpw.bv_val +
366 /* No scheme, use the whole value */
368 cbv = op->o_bd->be_rootpw;
370 if ( !BER_BVISEMPTY( &cbv )) {
371 for( i = 0; sl.list[i].name; i++ ) {
372 const char *name = sl.list[i].name;
374 if ( name[0] == '*' ) {
375 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
377 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
380 if ( !strcasecmp(name,"userPassword") ) {
381 sl.sparams->utils->prop_set( sl.sparams->propctx,
382 sl.list[i].name, cbv.bv_val, cbv.bv_len );
389 if ( op->o_bd->be_search ) {
390 SlapReply rs = {REP_RESULT};
391 op->o_hdr = conn->c_sasl_bindop->o_hdr;
392 op->o_controls = opbuf.ob_controls;
393 op->o_tag = LDAP_REQ_SEARCH;
394 op->o_dn = conn->c_ndn;
395 op->o_ndn = conn->c_ndn;
396 op->o_callback = &cb;
397 slap_op_time( &op->o_time, &op->o_tincr );
398 op->o_do_not_cache = 1;
399 op->o_is_auth_check = 1;
400 op->o_req_dn = op->o_req_ndn;
401 op->ors_scope = LDAP_SCOPE_BASE;
402 op->ors_deref = LDAP_DEREF_NEVER;
403 op->ors_tlimit = SLAP_NO_LIMIT;
405 op->ors_filter = &generic_filter;
406 op->ors_filterstr = generic_filterstr;
407 /* FIXME: we want all attributes, right? */
408 op->ors_attrs = NULL;
410 rc = op->o_bd->be_search( op, &rs );
414 #if SASL_VERSION_FULL >= 0x020118
415 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
419 #if SASL_VERSION_FULL >= 0x020110
423 sasl_server_params_t *sparams,
424 struct propctx *prctx,
430 SlapReply rs = {REP_RESULT};
433 Connection *conn = NULL;
434 const struct propval *pr;
435 Modifications *modlist = NULL, **modtail = &modlist, *mod;
436 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
437 char textbuf[SLAP_TEXT_BUFLEN];
439 size_t textlen = sizeof(textbuf);
441 /* just checking if we are enabled */
442 if (!prctx) return SASL_OK;
444 if (!sparams || !user) return SASL_BADPARAM;
446 pr = sparams->utils->prop_get( sparams->propctx );
448 /* Find our DN and conn first */
449 for( i = 0; pr[i].name; i++ ) {
450 if ( pr[i].name[0] == '*' ) {
451 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
452 if ( pr[i].values && pr[i].values[0] )
453 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
456 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
457 if ( pr[i].values && pr[i].values[0] )
458 AC_MEMCPY( &op.o_req_ndn.bv_len, pr[i].values[0],
459 sizeof( op.o_req_ndn.bv_len ) );
460 } else if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
462 op.o_req_ndn.bv_val = (char *)pr[i].values[0];
466 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
468 op.o_bd = select_backend( &op.o_req_ndn, 1 );
470 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
472 pr = sparams->utils->prop_get( prctx );
473 if (!pr) return SASL_BADPARAM;
475 for (i=0; pr[i].name; i++);
476 if (!i) return SASL_BADPARAM;
478 for (i=0; pr[i].name; i++) {
479 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
480 mod->sml_op = LDAP_MOD_REPLACE;
482 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
483 mod->sml_numvals = pr[i].nvalues;
484 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
485 sizeof(struct berval));
486 for (j=0; j<pr[i].nvalues; j++) {
487 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
489 BER_BVZERO( &mod->sml_values[j] );
490 mod->sml_nvalues = NULL;
491 mod->sml_desc = NULL;
493 modtail = &mod->sml_next;
497 rc = slap_mods_check( &op, modlist, &text, textbuf, textlen, NULL );
499 if ( rc == LDAP_SUCCESS ) {
500 rc = slap_mods_no_user_mod_check( &op, modlist,
501 &text, textbuf, textlen );
503 if ( rc == LDAP_SUCCESS ) {
504 if ( conn->c_sasl_bindop ) {
505 op.o_hdr = conn->c_sasl_bindop->o_hdr;
508 memset( &oph, 0, sizeof(oph) );
509 operation_fake_init( conn, &op, ldap_pvt_thread_pool_context(), 0 );
511 op.o_tag = LDAP_REQ_MODIFY;
512 op.o_ndn = op.o_req_ndn;
514 slap_op_time( &op.o_time, &op.o_tincr );
515 op.o_do_not_cache = 1;
516 op.o_is_auth_check = 1;
517 op.o_req_dn = op.o_req_ndn;
518 op.orm_modlist = modlist;
520 rc = op.o_bd->be_modify( &op, &rs );
523 slap_mods_free( modlist, 1 );
524 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
526 #endif /* SASL_VERSION_FULL >= 2.1.16 */
528 static sasl_auxprop_plug_t slap_auxprop_plugin = {
531 NULL, /* glob_context */
532 NULL, /* auxprop_free */
535 #if SASL_VERSION_FULL >= 0x020110
536 slap_auxprop_store /* the declaration of this member changed
537 * in cyrus SASL from 2.1.15 to 2.1.16 */
545 const sasl_utils_t *utils,
548 sasl_auxprop_plug_t **plug,
549 const char *plugname)
551 if ( !out_version || !plug ) return SASL_BADPARAM;
553 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
555 *out_version = SASL_AUXPROP_PLUG_VERSION;
556 *plug = &slap_auxprop_plugin;
560 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
561 * auxiliary property, so that we can refer to it in sasl_authorize
562 * without interfering with anything else. Also, the SASL username
563 * buffer is constrained to 256 characters, and our DNs could be
564 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
567 slap_sasl_canonicalize(
573 const char *user_realm,
578 Connection *conn = (Connection *)context;
579 struct propctx *props = sasl_auxprop_getctx( sconn );
580 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
583 const char *names[2];
588 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
589 conn ? (long) conn->c_connid : -1L,
590 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
591 in ? in : "<empty>");
593 /* If name is too big, just truncate. We don't care, we're
594 * using DNs, not the usernames.
596 if ( inlen > out_max )
599 /* This is a Simple Bind using SPASSWD. That means the in-directory
600 * userPassword of the Binding user already points at SASL, so it
601 * cannot be used to actually satisfy a password comparison. Just
602 * ignore it, some other mech will process it.
604 if ( !conn->c_sasl_bindop ||
605 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
607 /* See if we need to add request, can only do it once */
608 prop_getnames( props, slap_propnames, auxvals );
609 if ( !auxvals[0].name )
610 prop_request( props, slap_propnames );
612 if ( flags & SASL_CU_AUTHID )
613 which = SLAP_SASL_PROP_AUTHCLEN;
615 which = SLAP_SASL_PROP_AUTHZLEN;
617 /* Need to store the Connection for auxprop_lookup */
618 if ( !auxvals[SLAP_SASL_PROP_CONN].values ) {
619 names[0] = slap_propnames[SLAP_SASL_PROP_CONN];
621 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
624 /* Already been here? */
625 if ( auxvals[which].values )
628 /* Normally we require an authzID to have a u: or dn: prefix.
629 * However, SASL frequently gives us an authzID that is just
630 * an exact copy of the authcID, without a prefix. We need to
631 * detect and allow this condition. If SASL calls canonicalize
632 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
633 * But if it's broken into two calls, we need to remember the
634 * authcID so that we can compare the authzID later. We store
635 * the authcID temporarily in conn->c_sasl_dn. We necessarily
636 * finish Canonicalizing before Authorizing, so there is no
637 * conflict with slap_sasl_authorize's use of this temp var.
639 * The SASL EXTERNAL mech is backwards from all the other mechs,
640 * it does authzID before the authcID. If we see that authzID
641 * has already been done, don't do anything special with authcID.
643 if ( flags == SASL_CU_AUTHID && !auxvals[SLAP_SASL_PROP_AUTHZ].values ) {
644 conn->c_sasl_dn.bv_val = (char *) in;
645 conn->c_sasl_dn.bv_len = 0;
646 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
647 rc = strcmp( in, conn->c_sasl_dn.bv_val );
648 conn->c_sasl_dn.bv_val = NULL;
649 /* They were equal, no work needed */
650 if ( !rc ) goto done;
653 bvin.bv_val = (char *)in;
655 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
656 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
657 if ( rc != LDAP_SUCCESS ) {
658 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
662 names[0] = slap_propnames[which];
664 prop_set( props, names[0], (char *)&dn.bv_len, sizeof( dn.bv_len ) );
667 names[0] = slap_propnames[which];
668 prop_set( props, names[0], dn.bv_val, dn.bv_len );
670 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
671 conn ? (long) conn->c_connid : -1L, names[0]+1,
672 dn.bv_val ? dn.bv_val : "<EMPTY>" );
674 /* Not needed any more, SASL has copied it */
675 if ( conn && conn->c_sasl_bindop )
676 conn->c_sasl_bindop->o_tmpfree( dn.bv_val, conn->c_sasl_bindop->o_tmpmemctx );
679 AC_MEMCPY( out, in, inlen );
691 char *requested_user,
695 const char *def_realm,
697 struct propctx *props)
699 Connection *conn = (Connection *)context;
701 * (SLAP_SASL_PROP_COUNT - 1) because we skip "conn",
702 * + 1 for NULL termination?
704 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
705 struct berval authcDN, authzDN = BER_BVNULL;
708 /* Simple Binds don't support proxy authorization, ignore it */
709 if ( !conn->c_sasl_bindop ||
710 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
712 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
713 "authcid=\"%s\" authzid=\"%s\"\n",
714 conn ? (long) conn->c_connid : -1L, auth_identity, requested_user );
715 if ( conn->c_sasl_dn.bv_val ) {
716 BER_BVZERO( &conn->c_sasl_dn );
719 /* Skip SLAP_SASL_PROP_CONN */
720 prop_getnames( props, slap_propnames+1, auxvals );
722 /* Should not happen */
723 if ( !auxvals[0].values ) {
724 sasl_seterror( sconn, 0, "invalid authcid" );
728 AC_MEMCPY( &authcDN.bv_len, auxvals[0].values[0], sizeof(authcDN.bv_len) );
729 authcDN.bv_val = auxvals[1].values ? (char *)auxvals[1].values[0] : NULL;
730 conn->c_sasl_dn = authcDN;
732 /* Nothing to do if no authzID was given */
733 if ( !auxvals[2].name || !auxvals[2].values ) {
737 AC_MEMCPY( &authzDN.bv_len, auxvals[2].values[0], sizeof(authzDN.bv_len) );
738 authzDN.bv_val = auxvals[3].values ? (char *)auxvals[3].values[0] : NULL;
740 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
741 if ( rc != LDAP_SUCCESS ) {
742 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
743 "proxy authorization disallowed (%d)\n",
744 conn ? (long) conn->c_connid : -1L, rc, 0 );
746 sasl_seterror( sconn, 0, "not authorized" );
750 /* FIXME: we need yet another dup because slap_sasl_getdn()
751 * is using the bind operation slab */
752 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
755 if (conn->c_sasl_bindop) {
756 Statslog( LDAP_DEBUG_STATS,
757 "%s BIND authcid=\"%s\" authzid=\"%s\"\n",
758 conn->c_sasl_bindop->o_log_prefix,
759 auth_identity, requested_user, 0, 0 );
762 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
763 " proxy authorization allowed authzDN=\"%s\"\n",
764 conn ? (long) conn->c_connid : -1L,
765 authzDN.bv_val ? authzDN.bv_val : "", 0 );
770 slap_sasl_err2ldap( int saslerr )
774 /* map SASL errors to LDAP resultCode returned by:
776 * SASL_OK, SASL_NOMEM
778 * SASL_OK, SASL_CONTINUE, SASL_TRANS, SASL_BADPARAM, SASL_BADPROT,
780 * sasl_server_start()
783 * SASL_OK, SASL_BADPARAM
791 rc = LDAP_SASL_BIND_IN_PROGRESS;
798 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
804 rc = LDAP_INVALID_CREDENTIALS;
807 rc = LDAP_INSUFFICIENT_ACCESS;
811 rc = LDAP_INAPPROPRIATE_AUTH;
815 rc = LDAP_UNAVAILABLE;
818 rc = LDAP_UNWILLING_TO_PERFORM;
830 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
833 const struct berval *sc,
834 const struct berval * passwd,
835 const struct berval * cred,
840 void *ctx, *sconn = NULL;
842 for( i=0; i<cred->bv_len; i++) {
843 if(cred->bv_val[i] == '\0') {
844 return LUTIL_PASSWD_ERR; /* NUL character in password */
848 if( cred->bv_val[i] != '\0' ) {
849 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
852 for( i=0; i<passwd->bv_len; i++) {
853 if(passwd->bv_val[i] == '\0') {
854 return LUTIL_PASSWD_ERR; /* NUL character in password */
858 if( passwd->bv_val[i] != '\0' ) {
859 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
862 rtn = LUTIL_PASSWD_ERR;
864 ctx = ldap_pvt_thread_pool_context();
865 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_sasl_bind, &sconn, NULL );
867 if( sconn != NULL ) {
869 sc = sasl_checkpass( sconn,
870 passwd->bv_val, passwd->bv_len,
871 cred->bv_val, cred->bv_len );
872 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
877 #endif /* SLAPD_SPASSWD */
879 #endif /* HAVE_CYRUS_SASL */
881 #ifdef ENABLE_REWRITE
883 typedef struct slapd_map_data {
885 struct berval filter;
886 AttributeName attrs[2];
891 slapd_rw_config( const char *fname, int lineno, int argc, char **argv )
893 slapd_map_data *ret = NULL;
894 LDAPURLDesc *lud = NULL;
896 AttributeDescription *ad = NULL;
898 struct berval dn, ndn;
901 Debug( LDAP_DEBUG_ANY,
902 "[%s:%d] slapd map needs URI\n",
908 if ( strncasecmp( uri, "uri=", STRLENOF( "uri=" ) ) == 0 ) {
909 uri += STRLENOF( "uri=" );
912 if ( ldap_url_parse( uri, &lud ) != LDAP_URL_SUCCESS ) {
913 Debug( LDAP_DEBUG_ANY,
914 "[%s:%d] illegal URI '%s'\n",
915 fname, lineno, uri );
919 if ( strcasecmp( lud->lud_scheme, "ldap" )) {
920 Debug( LDAP_DEBUG_ANY,
921 "[%s:%d] illegal URI scheme '%s'\n",
922 fname, lineno, lud->lud_scheme );
926 if (( lud->lud_host && lud->lud_host[0] ) || lud->lud_exts
928 Debug( LDAP_DEBUG_ANY,
929 "[%s:%d] illegal URI '%s'\n",
930 fname, lineno, uri );
934 if ( lud->lud_attrs ) {
935 if ( lud->lud_attrs[1] ) {
936 Debug( LDAP_DEBUG_ANY,
937 "[%s:%d] only one attribute allowed in URI\n",
941 if ( strcasecmp( lud->lud_attrs[0], "dn" ) &&
942 strcasecmp( lud->lud_attrs[0], "entryDN" )) {
944 rc = slap_str2ad( lud->lud_attrs[0], &ad, &text );
949 ber_str2bv( lud->lud_dn, 0, 0, &dn );
950 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL ))
953 if ( lud->lud_filter ) {
954 flen = strlen( lud->lud_filter ) + 1;
956 ret = ch_malloc( sizeof( slapd_map_data ) + flen );
959 ret->filter.bv_val = (char *)(ret+1);
960 ret->filter.bv_len = flen - 1;
961 strcpy( ret->filter.bv_val, lud->lud_filter );
963 BER_BVZERO( &ret->filter );
965 ret->scope = lud->lud_scope;
967 ret->attrs[0].an_name = ad->ad_cname;
969 BER_BVZERO( &ret->attrs[0].an_name );
971 ret->attrs[0].an_desc = ad;
972 BER_BVZERO( &ret->attrs[1].an_name );
974 ldap_free_urldesc( lud );
978 struct slapd_rw_info {
979 slapd_map_data *si_data;
980 struct berval si_val;
984 slapd_rw_cb( Operation *op, SlapReply *rs )
986 if ( rs->sr_type == REP_SEARCH ) {
987 struct slapd_rw_info *si = op->o_callback->sc_private;
989 if ( si->si_data->attrs[0].an_desc ) {
992 a = attr_find( rs->sr_entry->e_attrs,
993 si->si_data->attrs[0].an_desc );
995 ber_dupbv( &si->si_val, a->a_vals );
998 ber_dupbv( &si->si_val, &rs->sr_entry->e_name );
1001 return LDAP_SUCCESS;
1005 slapd_rw_apply( void *private, const char *filter, struct berval *val )
1007 slapd_map_data *sl = private;
1008 slap_callback cb = { NULL };
1009 Connection conn = {0};
1010 OperationBuffer opbuf;
1013 SlapReply rs = {REP_RESULT};
1014 struct slapd_rw_info si;
1018 thrctx = ldap_pvt_thread_pool_context();
1019 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1022 op->o_tag = LDAP_REQ_SEARCH;
1023 op->o_req_dn = op->o_req_ndn = sl->base;
1024 op->o_bd = select_backend( &op->o_req_ndn, 1 );
1029 BER_BVZERO( &si.si_val );
1030 op->ors_scope = sl->scope;
1031 op->ors_deref = LDAP_DEREF_NEVER;
1033 op->ors_tlimit = SLAP_NO_LIMIT;
1034 if ( sl->attrs[0].an_desc ) {
1035 op->ors_attrs = sl->attrs;
1037 op->ors_attrs = slap_anlist_no_attrs;
1040 rc = strlen( filter );
1044 rc += sl->filter.bv_len;
1045 ptr = op->ors_filterstr.bv_val = op->o_tmpalloc( rc + 1, op->o_tmpmemctx );
1046 if ( sl->filter.bv_len ) {
1047 ptr = lutil_strcopy( ptr, sl->filter.bv_val );
1052 strcpy( ptr, filter );
1054 op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
1055 if ( !op->ors_filter ) {
1056 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1060 op->ors_attrsonly = 0;
1061 op->o_dn = op->o_bd->be_rootdn;
1062 op->o_ndn = op->o_bd->be_rootndn;
1063 op->o_do_not_cache = 1;
1065 cb.sc_response = slapd_rw_cb;
1066 cb.sc_private = &si;
1067 op->o_callback = &cb;
1069 rc = op->o_bd->be_search( op, &rs );
1070 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &si.si_val )) {
1072 rc = REWRITE_SUCCESS;
1074 if ( !BER_BVISNULL( &si.si_val )) {
1075 ch_free( si.si_val.bv_val );
1079 filter_free_x( op, op->ors_filter, 1 );
1080 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1085 slapd_rw_destroy( void *private )
1087 slapd_map_data *md = private;
1089 assert( private != NULL );
1091 ch_free( md->base.bv_val );
1092 ch_free( md->filter.bv_val );
1098 static const rewrite_mapper slapd_mapper = {
1106 int slap_sasl_init( void )
1108 #ifdef HAVE_CYRUS_SASL
1110 static sasl_callback_t server_callbacks[] = {
1111 { SASL_CB_LOG, &slap_sasl_log, NULL },
1112 { SASL_CB_GETOPT, &slap_sasl_getopt, NULL },
1113 { SASL_CB_LIST_END, NULL, NULL }
1117 #ifdef ENABLE_REWRITE
1118 rewrite_mapper_register( &slapd_mapper );
1121 #ifdef HAVE_CYRUS_SASL
1122 #ifdef HAVE_SASL_VERSION
1123 /* stringify the version number, sasl.h doesn't do it for us */
1124 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1125 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1126 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1129 sasl_version( NULL, &rc );
1130 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1131 (rc & 0xffff) < SASL_VERSION_STEP)
1133 char version[sizeof("xxx.xxx.xxxxx")];
1134 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1136 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
1137 " expected %s, got %s\n",
1138 SASL_VERSION_STRING, version, 0 );
1144 ldap_pvt_sasl_mutex_new,
1145 ldap_pvt_sasl_mutex_lock,
1146 ldap_pvt_sasl_mutex_unlock,
1147 ldap_pvt_sasl_mutex_dispose );
1149 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1151 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1152 if( rc != SASL_OK ) {
1153 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
1158 /* should provide callbacks for logging */
1159 /* server name should be configurable */
1160 rc = sasl_server_init( server_callbacks, "slapd" );
1162 if( rc != SASL_OK ) {
1163 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
1169 #ifdef SLAPD_SPASSWD
1170 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
1173 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1176 /* default security properties */
1177 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1178 sasl_secprops.max_ssf = INT_MAX;
1179 sasl_secprops.maxbufsize = 65536;
1180 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1186 int slap_sasl_destroy( void )
1188 #ifdef HAVE_CYRUS_SASL
1198 slap_sasl_peer2ipport( struct berval *peer )
1202 *addr = &peer->bv_val[ STRLENOF( "IP=" ) ];
1203 ber_len_t plen = peer->bv_len - STRLENOF( "IP=" );
1206 if ( addr[0] == '[' ) {
1210 ipport = ch_strdup( &addr[isv6] );
1212 /* Convert IPv6/IPv4 addresses to address;port syntax. */
1213 p = strrchr( ipport, ':' );
1217 assert( p[-1] == ']' );
1218 AC_MEMCPY( &p[-1], p, plen - ( p - ipport ) + 1 );
1221 } else if ( isv6 ) {
1224 assert( addr[plen] == ']' );
1231 int slap_sasl_open( Connection *conn, int reopen )
1233 int sc = LDAP_SUCCESS;
1234 #ifdef HAVE_CYRUS_SASL
1237 sasl_conn_t *ctx = NULL;
1238 sasl_callback_t *session_callbacks;
1239 char *ipremoteport = NULL, *iplocalport = NULL;
1241 assert( conn->c_sasl_authctx == NULL );
1244 assert( conn->c_sasl_extra == NULL );
1247 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1248 if( session_callbacks == NULL ) {
1249 Debug( LDAP_DEBUG_ANY,
1250 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1253 conn->c_sasl_extra = session_callbacks;
1255 session_callbacks[cb=0].id = SASL_CB_LOG;
1256 session_callbacks[cb].proc = &slap_sasl_log;
1257 session_callbacks[cb++].context = conn;
1259 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1260 session_callbacks[cb].proc = &slap_sasl_authorize;
1261 session_callbacks[cb++].context = conn;
1263 session_callbacks[cb].id = SASL_CB_CANON_USER;
1264 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1265 session_callbacks[cb++].context = conn;
1267 session_callbacks[cb].id = SASL_CB_LIST_END;
1268 session_callbacks[cb].proc = NULL;
1269 session_callbacks[cb++].context = NULL;
1271 session_callbacks = conn->c_sasl_extra;
1274 conn->c_sasl_layers = 0;
1276 /* create new SASL context */
1277 if ( conn->c_sock_name.bv_len != 0 &&
1278 strncmp( conn->c_sock_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1280 iplocalport = slap_sasl_peer2ipport( &conn->c_sock_name );
1283 if ( conn->c_peer_name.bv_len != 0 &&
1284 strncmp( conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1286 ipremoteport = slap_sasl_peer2ipport( &conn->c_peer_name );
1289 sc = sasl_server_new( "ldap", sasl_host, global_realm,
1290 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1291 if ( iplocalport != NULL ) {
1292 ch_free( iplocalport );
1294 if ( ipremoteport != NULL ) {
1295 ch_free( ipremoteport );
1298 if( sc != SASL_OK ) {
1299 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1305 conn->c_sasl_authctx = ctx;
1307 if( sc == SASL_OK ) {
1308 sc = sasl_setprop( ctx,
1309 SASL_SEC_PROPS, &sasl_secprops );
1311 if( sc != SASL_OK ) {
1312 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1315 slap_sasl_close( conn );
1320 sc = slap_sasl_err2ldap( sc );
1322 #elif defined(SLAP_BUILTIN_SASL)
1323 /* built-in SASL implementation */
1324 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1325 if( ctx == NULL ) return -1;
1327 ctx->sc_external_ssf = 0;
1328 BER_BVZERO( &ctx->sc_external_id );
1330 conn->c_sasl_authctx = ctx;
1336 int slap_sasl_external(
1339 struct berval *auth_id )
1341 #ifdef HAVE_CYRUS_SASL
1343 sasl_conn_t *ctx = conn->c_sasl_authctx;
1344 sasl_ssf_t sasl_ssf = ssf;
1346 if ( ctx == NULL ) {
1347 return LDAP_UNAVAILABLE;
1350 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
1352 if ( sc != SASL_OK ) {
1356 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1357 auth_id ? auth_id->bv_val : NULL );
1359 if ( sc != SASL_OK ) {
1362 #elif defined(SLAP_BUILTIN_SASL)
1363 /* built-in SASL implementation */
1364 SASL_CTX *ctx = conn->c_sasl_authctx;
1365 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1367 ctx->sc_external_ssf = ssf;
1369 ctx->sc_external_id = *auth_id;
1370 BER_BVZERO( auth_id );
1372 BER_BVZERO( &ctx->sc_external_id );
1376 return LDAP_SUCCESS;
1379 int slap_sasl_reset( Connection *conn )
1381 return LDAP_SUCCESS;
1384 char ** slap_sasl_mechs( Connection *conn )
1386 char **mechs = NULL;
1388 #ifdef HAVE_CYRUS_SASL
1389 sasl_conn_t *ctx = conn->c_sasl_authctx;
1391 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1395 SASL_CONST char *mechstr;
1397 sc = sasl_listmech( ctx,
1398 NULL, NULL, ",", NULL,
1399 &mechstr, NULL, NULL );
1401 if( sc != SASL_OK ) {
1402 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1408 mechs = ldap_str2charray( mechstr, "," );
1410 #elif defined(SLAP_BUILTIN_SASL)
1411 /* builtin SASL implementation */
1412 SASL_CTX *ctx = conn->c_sasl_authctx;
1413 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1414 /* should check ssf */
1415 mechs = ldap_str2charray( "EXTERNAL", "," );
1422 int slap_sasl_close( Connection *conn )
1424 #ifdef HAVE_CYRUS_SASL
1425 sasl_conn_t *ctx = conn->c_sasl_authctx;
1428 sasl_dispose( &ctx );
1430 if ( conn->c_sasl_sockctx &&
1431 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1433 ctx = conn->c_sasl_sockctx;
1434 sasl_dispose( &ctx );
1437 conn->c_sasl_authctx = NULL;
1438 conn->c_sasl_sockctx = NULL;
1439 conn->c_sasl_done = 0;
1441 free( conn->c_sasl_extra );
1442 conn->c_sasl_extra = NULL;
1444 #elif defined(SLAP_BUILTIN_SASL)
1445 SASL_CTX *ctx = conn->c_sasl_authctx;
1447 if( ctx->sc_external_id.bv_val ) {
1448 free( ctx->sc_external_id.bv_val );
1449 BER_BVZERO( &ctx->sc_external_id );
1452 conn->c_sasl_authctx = NULL;
1456 return LDAP_SUCCESS;
1459 int slap_sasl_bind( Operation *op, SlapReply *rs )
1461 #ifdef HAVE_CYRUS_SASL
1462 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1463 struct berval response;
1464 unsigned reslen = 0;
1467 Debug(LDAP_DEBUG_ARGS,
1468 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1469 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1470 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1471 op->o_conn->c_sasl_bind_mech.bv_val,
1472 op->orb_cred.bv_len );
1475 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1476 "SASL unavailable on this session" );
1480 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1481 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1482 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1483 sasl_server_step( ctx, cred, clen, resp, rlen )
1485 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1486 /* If we already authenticated once, must use a new context */
1487 if ( op->o_conn->c_sasl_done ) {
1489 const char *authid = NULL;
1490 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1491 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1492 if ( authid ) authid = ch_strdup( authid );
1493 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1494 sasl_dispose( &ctx );
1496 op->o_conn->c_sasl_authctx = NULL;
1498 slap_sasl_open( op->o_conn, 1 );
1499 ctx = op->o_conn->c_sasl_authctx;
1501 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1502 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1503 ch_free( (char *)authid );
1507 op->o_conn->c_sasl_bind_mech.bv_val,
1508 op->orb_cred.bv_val, op->orb_cred.bv_len,
1509 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1513 op->orb_cred.bv_val, op->orb_cred.bv_len,
1514 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1517 response.bv_len = reslen;
1519 if ( sc == SASL_OK ) {
1520 sasl_ssf_t *ssf = NULL;
1522 ber_dupbv_x( &op->orb_edn, &op->o_conn->c_sasl_dn, op->o_tmpmemctx );
1523 BER_BVZERO( &op->o_conn->c_sasl_dn );
1524 op->o_conn->c_sasl_done = 1;
1526 rs->sr_err = LDAP_SUCCESS;
1528 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1529 op->orb_ssf = ssf ? *ssf : 0;
1533 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1534 op->o_conn->c_sasl_layers++;
1536 /* If there's an old layer, set sockctx to NULL to
1537 * tell connection_read() to wait for us to finish.
1538 * Otherwise there is a race condition: we have to
1539 * send the Bind response using the old security
1540 * context and then remove it before reading any
1543 if ( op->o_conn->c_sasl_sockctx ) {
1544 ctx = op->o_conn->c_sasl_sockctx;
1545 op->o_conn->c_sasl_sockctx = NULL;
1547 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1549 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1552 /* Must send response using old security layer */
1553 rs->sr_sasldata = (response.bv_len ? &response : NULL);
1554 send_ldap_sasl( op, rs );
1556 /* Now dispose of the old security layer.
1559 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1560 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1561 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1562 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1563 sasl_dispose( &ctx );
1565 } else if ( sc == SASL_CONTINUE ) {
1566 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1567 rs->sr_text = sasl_errdetail( ctx );
1568 rs->sr_sasldata = &response;
1569 send_ldap_sasl( op, rs );
1572 BER_BVZERO( &op->o_conn->c_sasl_dn );
1573 rs->sr_text = sasl_errdetail( ctx );
1574 rs->sr_err = slap_sasl_err2ldap( sc ),
1575 send_ldap_result( op, rs );
1578 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1580 #elif defined(SLAP_BUILTIN_SASL)
1581 /* built-in SASL implementation */
1582 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1584 if ( ctx == NULL ) {
1585 send_ldap_error( op, rs, LDAP_OTHER,
1586 "Internal SASL Error" );
1588 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1591 if( op->orb_cred.bv_len ) {
1592 rs->sr_text = "proxy authorization not support";
1593 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1594 send_ldap_result( op, rs );
1597 op->orb_edn = ctx->sc_external_id;
1598 rs->sr_err = LDAP_SUCCESS;
1599 rs->sr_sasldata = NULL;
1600 send_ldap_sasl( op, rs );
1604 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1605 "requested SASL mechanism not supported" );
1608 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1609 "SASL not supported" );
1615 char* slap_sasl_secprops( const char *in )
1617 #ifdef HAVE_CYRUS_SASL
1618 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1620 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1622 return "SASL not supported";
1626 void slap_sasl_secprops_unparse( struct berval *bv )
1628 #ifdef HAVE_CYRUS_SASL
1629 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1633 #ifdef HAVE_CYRUS_SASL
1635 slap_sasl_setpass( Operation *op, SlapReply *rs )
1637 struct berval id = BER_BVNULL; /* needs to come from connection */
1638 struct berval new = BER_BVNULL;
1639 struct berval old = BER_BVNULL;
1641 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1643 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1644 (SASL_CONST void **)(char *)&id.bv_val );
1646 if( rs->sr_err != SASL_OK ) {
1647 rs->sr_text = "unable to retrieve SASL username";
1648 rs->sr_err = LDAP_OTHER;
1652 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1653 id.bv_val ? id.bv_val : "", 0, 0 );
1655 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1656 NULL, &old, &new, &rs->sr_text );
1658 if( rs->sr_err != LDAP_SUCCESS ) {
1662 if( new.bv_len == 0 ) {
1663 slap_passwd_generate(&new);
1665 if( new.bv_len == 0 ) {
1666 rs->sr_text = "password generation failed.";
1667 rs->sr_err = LDAP_OTHER;
1671 rs->sr_rspdata = slap_passwd_return( &new );
1674 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1675 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1676 if( rs->sr_err != SASL_OK ) {
1677 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1679 switch(rs->sr_err) {
1681 rs->sr_err = LDAP_SUCCESS;
1691 rs->sr_err = LDAP_OTHER;
1697 #endif /* HAVE_CYRUS_SASL */
1699 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1700 * string returned in *dn is in its own allocated memory, and must be free'd
1701 * by the calling process. -Mark Adamson, Carnegie Mellon
1703 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1704 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1711 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1712 char *user_realm, struct berval *dn, int flags )
1714 int rc, is_dn = SET_NONE, do_norm = 1;
1715 struct berval dn2, *mech;
1717 assert( conn != NULL );
1718 assert( id != NULL );
1720 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1722 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1723 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1724 (unsigned long) id->bv_len ) );
1727 op = conn->c_sasl_bindop;
1729 assert( op != NULL );
1733 if ( !BER_BVISNULL( id ) ) {
1734 /* Blatantly anonymous ID */
1735 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1737 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1738 return( LDAP_SUCCESS );
1742 /* FIXME: if empty, should we stop? */
1743 BER_BVSTR( id, "" );
1746 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1747 mech = &conn->c_sasl_bind_mech;
1749 mech = &conn->c_authmech;
1752 /* An authcID needs to be converted to authzID form. Set the
1753 * values directly into *dn; they will be normalized later. (and
1754 * normalizing always makes a new copy.) An ID from a TLS certificate
1755 * is already normalized, so copy it and skip normalization.
1757 if( flags & SLAP_GETDN_AUTHCID ) {
1758 if( bvmatch( mech, &ext_bv )) {
1759 /* EXTERNAL DNs are already normalized */
1760 assert( !BER_BVISNULL( id ) );
1764 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1767 /* convert to u:<username> form */
1773 if( is_dn == SET_NONE ) {
1774 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1776 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1777 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1779 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1781 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1782 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1786 /* No other possibilities from here */
1787 if( is_dn == SET_NONE ) {
1789 return( LDAP_INAPPROPRIATE_AUTH );
1792 /* Username strings */
1793 if( is_dn == SET_U ) {
1794 /* ITS#3419: values may need escape */
1796 LDAPAVA *RDNs[ 4 ][ 2 ];
1801 DN[ irdn ] = RDNs[ irdn ];
1802 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1803 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1804 AVAs[ irdn ].la_value = *dn;
1805 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1806 AVAs[ irdn ].la_private = NULL;
1807 RDNs[ irdn ][ 1 ] = NULL;
1809 if ( user_realm && *user_realm ) {
1811 DN[ irdn ] = RDNs[ irdn ];
1812 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1813 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1814 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1815 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1816 AVAs[ irdn ].la_private = NULL;
1817 RDNs[ irdn ][ 1 ] = NULL;
1820 if ( !BER_BVISNULL( mech ) ) {
1822 DN[ irdn ] = RDNs[ irdn ];
1823 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1824 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1825 AVAs[ irdn ].la_value = *mech;
1826 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1827 AVAs[ irdn ].la_private = NULL;
1828 RDNs[ irdn ][ 1 ] = NULL;
1832 DN[ irdn ] = RDNs[ irdn ];
1833 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1834 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1835 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1836 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1837 AVAs[ irdn ].la_private = NULL;
1838 RDNs[ irdn ][ 1 ] = NULL;
1843 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1845 if ( rc != LDAP_SUCCESS ) {
1850 Debug( LDAP_DEBUG_TRACE,
1851 "slap_sasl_getdn: u:id converted to %s\n",
1856 /* Dup the DN in any case, so we don't risk
1857 * leaks or dangling pointers later,
1858 * and the DN value is '\0' terminated */
1859 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1860 dn->bv_val = dn2.bv_val;
1863 /* All strings are in DN form now. Normalize if needed. */
1865 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1867 /* User DNs were constructed above and must be freed now */
1868 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1870 if ( rc != LDAP_SUCCESS ) {
1877 /* Run thru regexp */
1878 slap_sasl2dn( op, dn, &dn2, flags );
1879 if( !BER_BVISNULL( &dn2 ) ) {
1880 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1882 Debug( LDAP_DEBUG_TRACE,
1883 "slap_sasl_getdn: dn:id converted to %s\n",
1887 return( LDAP_SUCCESS );