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 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 /* we don't know anything about this, ignore it */
322 /* Now see what else needs to be fetched */
323 for( i = 0; sl.list[i].name; i++ ) {
324 const char *name = sl.list[i].name;
326 if ( name[0] == '*' ) {
327 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
328 /* Skip our private properties */
329 if ( !strcmp( name, slap_propnames[0] )) {
330 i += SLAP_SASL_PROP_COUNT - 1;
334 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
337 if ( sl.list[i].values ) {
338 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
345 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
349 op->o_bd = select_backend( &op->o_req_ndn, 1 );
352 /* For rootdn, see if we can use the rootpw */
353 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) &&
354 !BER_BVISEMPTY( &op->o_bd->be_rootpw )) {
355 struct berval cbv = BER_BVNULL;
357 /* If there's a recognized scheme, see if it's CLEARTEXT */
358 if ( lutil_passwd_scheme( op->o_bd->be_rootpw.bv_val )) {
359 if ( !strncasecmp( op->o_bd->be_rootpw.bv_val,
360 sc_cleartext.bv_val, sc_cleartext.bv_len )) {
362 /* If it's CLEARTEXT, skip past scheme spec */
363 cbv.bv_len = op->o_bd->be_rootpw.bv_len -
366 cbv.bv_val = op->o_bd->be_rootpw.bv_val +
370 /* No scheme, use the whole value */
372 cbv = op->o_bd->be_rootpw;
374 if ( !BER_BVISEMPTY( &cbv )) {
375 for( i = 0; sl.list[i].name; i++ ) {
376 const char *name = sl.list[i].name;
378 if ( name[0] == '*' ) {
379 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
381 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
384 if ( !strcasecmp(name,"userPassword") ) {
385 sl.sparams->utils->prop_set( sl.sparams->propctx,
386 sl.list[i].name, cbv.bv_val, cbv.bv_len );
393 if ( op->o_bd->be_search ) {
394 SlapReply rs = {REP_RESULT};
395 op->o_hdr = conn->c_sasl_bindop->o_hdr;
396 op->o_controls = opbuf.ob_controls;
397 op->o_tag = LDAP_REQ_SEARCH;
398 op->o_dn = conn->c_ndn;
399 op->o_ndn = conn->c_ndn;
400 op->o_callback = &cb;
401 slap_op_time( &op->o_time, &op->o_tincr );
402 op->o_do_not_cache = 1;
403 op->o_is_auth_check = 1;
404 op->o_req_dn = op->o_req_ndn;
405 op->ors_scope = LDAP_SCOPE_BASE;
406 op->ors_deref = LDAP_DEREF_NEVER;
407 op->ors_tlimit = SLAP_NO_LIMIT;
409 op->ors_filter = &generic_filter;
410 op->ors_filterstr = generic_filterstr;
411 op->o_authz = conn->c_authz;
412 /* FIXME: we want all attributes, right? */
413 op->ors_attrs = NULL;
415 rc = op->o_bd->be_search( op, &rs );
419 #if SASL_VERSION_FULL >= 0x020118
420 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
424 #if SASL_VERSION_FULL >= 0x020110
428 sasl_server_params_t *sparams,
429 struct propctx *prctx,
435 SlapReply rs = {REP_RESULT};
438 Connection *conn = NULL;
439 const struct propval *pr;
440 Modifications *modlist = NULL, **modtail = &modlist, *mod;
441 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
442 char textbuf[SLAP_TEXT_BUFLEN];
444 size_t textlen = sizeof(textbuf);
446 /* just checking if we are enabled */
447 if (!prctx) return SASL_OK;
449 if (!sparams || !user) return SASL_BADPARAM;
451 pr = sparams->utils->prop_get( sparams->propctx );
453 /* Find our DN and conn first */
454 for( i = 0; pr[i].name; i++ ) {
455 if ( pr[i].name[0] == '*' ) {
456 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
457 if ( pr[i].values && pr[i].values[0] )
458 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
461 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
462 if ( pr[i].values && pr[i].values[0] )
463 AC_MEMCPY( &op.o_req_ndn.bv_len, pr[i].values[0],
464 sizeof( op.o_req_ndn.bv_len ) );
465 } else if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
467 op.o_req_ndn.bv_val = (char *)pr[i].values[0];
471 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
473 op.o_bd = select_backend( &op.o_req_ndn, 1 );
475 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
477 pr = sparams->utils->prop_get( prctx );
478 if (!pr) return SASL_BADPARAM;
480 for (i=0; pr[i].name; i++);
481 if (!i) return SASL_BADPARAM;
483 for (i=0; pr[i].name; i++) {
484 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
485 mod->sml_op = LDAP_MOD_REPLACE;
487 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
488 mod->sml_numvals = pr[i].nvalues;
489 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
490 sizeof(struct berval));
491 for (j=0; j<pr[i].nvalues; j++) {
492 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
494 BER_BVZERO( &mod->sml_values[j] );
495 mod->sml_nvalues = NULL;
496 mod->sml_desc = NULL;
498 modtail = &mod->sml_next;
502 rc = slap_mods_check( &op, modlist, &text, textbuf, textlen, NULL );
504 if ( rc == LDAP_SUCCESS ) {
505 rc = slap_mods_no_user_mod_check( &op, modlist,
506 &text, textbuf, textlen );
508 if ( rc == LDAP_SUCCESS ) {
509 if ( conn->c_sasl_bindop ) {
510 op.o_hdr = conn->c_sasl_bindop->o_hdr;
513 memset( &oph, 0, sizeof(oph) );
514 operation_fake_init( conn, &op, ldap_pvt_thread_pool_context(), 0 );
516 op.o_tag = LDAP_REQ_MODIFY;
517 op.o_ndn = op.o_req_ndn;
519 slap_op_time( &op.o_time, &op.o_tincr );
520 op.o_do_not_cache = 1;
521 op.o_is_auth_check = 1;
522 op.o_req_dn = op.o_req_ndn;
523 op.orm_modlist = modlist;
525 rc = op.o_bd->be_modify( &op, &rs );
528 slap_mods_free( modlist, 1 );
529 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
531 #endif /* SASL_VERSION_FULL >= 2.1.16 */
533 static sasl_auxprop_plug_t slap_auxprop_plugin = {
536 NULL, /* glob_context */
537 NULL, /* auxprop_free */
540 #if SASL_VERSION_FULL >= 0x020110
541 slap_auxprop_store /* the declaration of this member changed
542 * in cyrus SASL from 2.1.15 to 2.1.16 */
550 const sasl_utils_t *utils,
553 sasl_auxprop_plug_t **plug,
554 const char *plugname)
556 if ( !out_version || !plug ) return SASL_BADPARAM;
558 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
560 *out_version = SASL_AUXPROP_PLUG_VERSION;
561 *plug = &slap_auxprop_plugin;
565 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
566 * auxiliary property, so that we can refer to it in sasl_authorize
567 * without interfering with anything else. Also, the SASL username
568 * buffer is constrained to 256 characters, and our DNs could be
569 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
572 slap_sasl_canonicalize(
578 const char *user_realm,
583 Connection *conn = (Connection *)context;
584 struct propctx *props = sasl_auxprop_getctx( sconn );
585 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
588 const char *names[2];
593 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
594 conn ? (long) conn->c_connid : -1L,
595 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
596 in ? in : "<empty>");
598 /* If name is too big, just truncate. We don't care, we're
599 * using DNs, not the usernames.
601 if ( inlen > out_max )
604 /* This is a Simple Bind using SPASSWD. That means the in-directory
605 * userPassword of the Binding user already points at SASL, so it
606 * cannot be used to actually satisfy a password comparison. Just
607 * ignore it, some other mech will process it.
609 if ( !conn->c_sasl_bindop ||
610 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
612 /* See if we need to add request, can only do it once */
613 prop_getnames( props, slap_propnames, auxvals );
614 if ( !auxvals[0].name )
615 prop_request( props, slap_propnames );
617 if ( flags & SASL_CU_AUTHID )
618 which = SLAP_SASL_PROP_AUTHCLEN;
620 which = SLAP_SASL_PROP_AUTHZLEN;
622 /* Need to store the Connection for auxprop_lookup */
623 if ( !auxvals[SLAP_SASL_PROP_CONN].values ) {
624 names[0] = slap_propnames[SLAP_SASL_PROP_CONN];
626 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
629 /* Already been here? */
630 if ( auxvals[which].values )
633 /* Normally we require an authzID to have a u: or dn: prefix.
634 * However, SASL frequently gives us an authzID that is just
635 * an exact copy of the authcID, without a prefix. We need to
636 * detect and allow this condition. If SASL calls canonicalize
637 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
638 * But if it's broken into two calls, we need to remember the
639 * authcID so that we can compare the authzID later. We store
640 * the authcID temporarily in conn->c_sasl_dn. We necessarily
641 * finish Canonicalizing before Authorizing, so there is no
642 * conflict with slap_sasl_authorize's use of this temp var.
644 * The SASL EXTERNAL mech is backwards from all the other mechs,
645 * it does authzID before the authcID. If we see that authzID
646 * has already been done, don't do anything special with authcID.
648 if ( flags == SASL_CU_AUTHID && !auxvals[SLAP_SASL_PROP_AUTHZ].values ) {
649 conn->c_sasl_dn.bv_val = (char *) in;
650 conn->c_sasl_dn.bv_len = 0;
651 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
652 rc = strcmp( in, conn->c_sasl_dn.bv_val );
653 conn->c_sasl_dn.bv_val = NULL;
654 /* They were equal, no work needed */
655 if ( !rc ) goto done;
658 bvin.bv_val = (char *)in;
660 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
661 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
662 if ( rc != LDAP_SUCCESS ) {
663 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
667 names[0] = slap_propnames[which];
669 prop_set( props, names[0], (char *)&dn.bv_len, sizeof( dn.bv_len ) );
672 names[0] = slap_propnames[which];
673 prop_set( props, names[0], dn.bv_val, dn.bv_len );
675 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
676 conn ? (long) conn->c_connid : -1L, names[0]+1,
677 dn.bv_val ? dn.bv_val : "<EMPTY>" );
679 /* Not needed any more, SASL has copied it */
680 if ( conn && conn->c_sasl_bindop )
681 conn->c_sasl_bindop->o_tmpfree( dn.bv_val, conn->c_sasl_bindop->o_tmpmemctx );
684 AC_MEMCPY( out, in, inlen );
696 char *requested_user,
700 const char *def_realm,
702 struct propctx *props)
704 Connection *conn = (Connection *)context;
706 * (SLAP_SASL_PROP_COUNT - 1) because we skip "conn",
707 * + 1 for NULL termination?
709 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
710 struct berval authcDN, authzDN = BER_BVNULL;
713 /* Simple Binds don't support proxy authorization, ignore it */
714 if ( !conn->c_sasl_bindop ||
715 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
717 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
718 "authcid=\"%s\" authzid=\"%s\"\n",
719 conn ? (long) conn->c_connid : -1L, auth_identity, requested_user );
720 if ( conn->c_sasl_dn.bv_val ) {
721 BER_BVZERO( &conn->c_sasl_dn );
724 /* Skip SLAP_SASL_PROP_CONN */
725 prop_getnames( props, slap_propnames+1, auxvals );
727 /* Should not happen */
728 if ( !auxvals[0].values ) {
729 sasl_seterror( sconn, 0, "invalid authcid" );
733 AC_MEMCPY( &authcDN.bv_len, auxvals[0].values[0], sizeof(authcDN.bv_len) );
734 authcDN.bv_val = auxvals[1].values ? (char *)auxvals[1].values[0] : NULL;
735 conn->c_sasl_dn = authcDN;
737 /* Nothing to do if no authzID was given */
738 if ( !auxvals[2].name || !auxvals[2].values ) {
742 AC_MEMCPY( &authzDN.bv_len, auxvals[2].values[0], sizeof(authzDN.bv_len) );
743 authzDN.bv_val = auxvals[3].values ? (char *)auxvals[3].values[0] : NULL;
745 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
746 if ( rc != LDAP_SUCCESS ) {
747 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
748 "proxy authorization disallowed (%d)\n",
749 conn ? (long) conn->c_connid : -1L, rc, 0 );
751 sasl_seterror( sconn, 0, "not authorized" );
755 /* FIXME: we need yet another dup because slap_sasl_getdn()
756 * is using the bind operation slab */
757 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
760 if (conn->c_sasl_bindop) {
761 Statslog( LDAP_DEBUG_STATS,
762 "%s BIND authcid=\"%s\" authzid=\"%s\"\n",
763 conn->c_sasl_bindop->o_log_prefix,
764 auth_identity, requested_user, 0, 0 );
767 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
768 " proxy authorization allowed authzDN=\"%s\"\n",
769 conn ? (long) conn->c_connid : -1L,
770 authzDN.bv_val ? authzDN.bv_val : "", 0 );
775 slap_sasl_err2ldap( int saslerr )
779 /* map SASL errors to LDAP resultCode returned by:
781 * SASL_OK, SASL_NOMEM
783 * SASL_OK, SASL_CONTINUE, SASL_TRANS, SASL_BADPARAM, SASL_BADPROT,
785 * sasl_server_start()
788 * SASL_OK, SASL_BADPARAM
796 rc = LDAP_SASL_BIND_IN_PROGRESS;
803 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
809 rc = LDAP_INVALID_CREDENTIALS;
812 rc = LDAP_INSUFFICIENT_ACCESS;
816 rc = LDAP_INAPPROPRIATE_AUTH;
820 rc = LDAP_UNAVAILABLE;
823 rc = LDAP_UNWILLING_TO_PERFORM;
835 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
838 const struct berval *sc,
839 const struct berval * passwd,
840 const struct berval * cred,
845 void *ctx, *sconn = NULL;
847 for( i=0; i<cred->bv_len; i++) {
848 if(cred->bv_val[i] == '\0') {
849 return LUTIL_PASSWD_ERR; /* NUL character in password */
853 if( cred->bv_val[i] != '\0' ) {
854 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
857 for( i=0; i<passwd->bv_len; i++) {
858 if(passwd->bv_val[i] == '\0') {
859 return LUTIL_PASSWD_ERR; /* NUL character in password */
863 if( passwd->bv_val[i] != '\0' ) {
864 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
867 rtn = LUTIL_PASSWD_ERR;
869 ctx = ldap_pvt_thread_pool_context();
870 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_sasl_bind, &sconn, NULL );
872 if( sconn != NULL ) {
874 sc = sasl_checkpass( sconn,
875 passwd->bv_val, passwd->bv_len,
876 cred->bv_val, cred->bv_len );
877 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
882 #endif /* SLAPD_SPASSWD */
884 #endif /* HAVE_CYRUS_SASL */
886 #ifdef ENABLE_REWRITE
888 typedef struct slapd_map_data {
890 struct berval filter;
891 AttributeName attrs[2];
896 slapd_rw_config( const char *fname, int lineno, int argc, char **argv )
898 slapd_map_data *ret = NULL;
899 LDAPURLDesc *lud = NULL;
901 AttributeDescription *ad = NULL;
903 struct berval dn, ndn;
906 Debug( LDAP_DEBUG_ANY,
907 "[%s:%d] slapd map needs URI\n",
913 if ( strncasecmp( uri, "uri=", STRLENOF( "uri=" ) ) == 0 ) {
914 uri += STRLENOF( "uri=" );
917 if ( ldap_url_parse( uri, &lud ) != LDAP_URL_SUCCESS ) {
918 Debug( LDAP_DEBUG_ANY,
919 "[%s:%d] illegal URI '%s'\n",
920 fname, lineno, uri );
924 if ( strcasecmp( lud->lud_scheme, "ldap" )) {
925 Debug( LDAP_DEBUG_ANY,
926 "[%s:%d] illegal URI scheme '%s'\n",
927 fname, lineno, lud->lud_scheme );
931 if (( lud->lud_host && lud->lud_host[0] ) || lud->lud_exts
933 Debug( LDAP_DEBUG_ANY,
934 "[%s:%d] illegal URI '%s'\n",
935 fname, lineno, uri );
939 if ( lud->lud_attrs ) {
940 if ( lud->lud_attrs[1] ) {
941 Debug( LDAP_DEBUG_ANY,
942 "[%s:%d] only one attribute allowed in URI\n",
946 if ( strcasecmp( lud->lud_attrs[0], "dn" ) &&
947 strcasecmp( lud->lud_attrs[0], "entryDN" )) {
949 rc = slap_str2ad( lud->lud_attrs[0], &ad, &text );
954 ber_str2bv( lud->lud_dn, 0, 0, &dn );
955 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL ))
958 if ( lud->lud_filter ) {
959 flen = strlen( lud->lud_filter ) + 1;
961 ret = ch_malloc( sizeof( slapd_map_data ) + flen );
964 ret->filter.bv_val = (char *)(ret+1);
965 ret->filter.bv_len = flen - 1;
966 strcpy( ret->filter.bv_val, lud->lud_filter );
968 BER_BVZERO( &ret->filter );
970 ret->scope = lud->lud_scope;
972 ret->attrs[0].an_name = ad->ad_cname;
974 BER_BVZERO( &ret->attrs[0].an_name );
976 ret->attrs[0].an_desc = ad;
977 BER_BVZERO( &ret->attrs[1].an_name );
979 ldap_free_urldesc( lud );
983 struct slapd_rw_info {
984 slapd_map_data *si_data;
985 struct berval si_val;
989 slapd_rw_cb( Operation *op, SlapReply *rs )
991 if ( rs->sr_type == REP_SEARCH ) {
992 struct slapd_rw_info *si = op->o_callback->sc_private;
994 if ( si->si_data->attrs[0].an_desc ) {
997 a = attr_find( rs->sr_entry->e_attrs,
998 si->si_data->attrs[0].an_desc );
1000 ber_dupbv( &si->si_val, a->a_vals );
1003 ber_dupbv( &si->si_val, &rs->sr_entry->e_name );
1006 return LDAP_SUCCESS;
1010 slapd_rw_apply( void *private, const char *filter, struct berval *val )
1012 slapd_map_data *sl = private;
1013 slap_callback cb = { NULL };
1014 Connection conn = {0};
1015 OperationBuffer opbuf;
1018 SlapReply rs = {REP_RESULT};
1019 struct slapd_rw_info si;
1023 thrctx = ldap_pvt_thread_pool_context();
1024 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1027 op->o_tag = LDAP_REQ_SEARCH;
1028 op->o_req_dn = op->o_req_ndn = sl->base;
1029 op->o_bd = select_backend( &op->o_req_ndn, 1 );
1034 BER_BVZERO( &si.si_val );
1035 op->ors_scope = sl->scope;
1036 op->ors_deref = LDAP_DEREF_NEVER;
1038 op->ors_tlimit = SLAP_NO_LIMIT;
1039 if ( sl->attrs[0].an_desc ) {
1040 op->ors_attrs = sl->attrs;
1042 op->ors_attrs = slap_anlist_no_attrs;
1045 rc = strlen( filter );
1049 rc += sl->filter.bv_len;
1050 ptr = op->ors_filterstr.bv_val = op->o_tmpalloc( rc + 1, op->o_tmpmemctx );
1051 if ( sl->filter.bv_len ) {
1052 ptr = lutil_strcopy( ptr, sl->filter.bv_val );
1057 strcpy( ptr, filter );
1059 op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
1060 if ( !op->ors_filter ) {
1061 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1065 op->ors_attrsonly = 0;
1066 op->o_dn = op->o_bd->be_rootdn;
1067 op->o_ndn = op->o_bd->be_rootndn;
1068 op->o_do_not_cache = 1;
1070 cb.sc_response = slapd_rw_cb;
1071 cb.sc_private = &si;
1072 op->o_callback = &cb;
1074 rc = op->o_bd->be_search( op, &rs );
1075 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &si.si_val )) {
1077 rc = REWRITE_SUCCESS;
1079 if ( !BER_BVISNULL( &si.si_val )) {
1080 ch_free( si.si_val.bv_val );
1084 filter_free_x( op, op->ors_filter, 1 );
1085 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1090 slapd_rw_destroy( void *private )
1092 slapd_map_data *md = private;
1094 assert( private != NULL );
1096 ch_free( md->base.bv_val );
1102 static const rewrite_mapper slapd_mapper = {
1110 int slap_sasl_init( void )
1112 #ifdef HAVE_CYRUS_SASL
1114 static sasl_callback_t server_callbacks[] = {
1115 { SASL_CB_LOG, &slap_sasl_log, NULL },
1116 { SASL_CB_GETOPT, &slap_sasl_getopt, NULL },
1117 { SASL_CB_LIST_END, NULL, NULL }
1121 #ifdef ENABLE_REWRITE
1122 rewrite_mapper_register( &slapd_mapper );
1125 #ifdef HAVE_CYRUS_SASL
1126 #ifdef HAVE_SASL_VERSION
1127 /* stringify the version number, sasl.h doesn't do it for us */
1128 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1129 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1130 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1133 sasl_version( NULL, &rc );
1134 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1135 (rc & 0xffff) < SASL_VERSION_STEP)
1137 char version[sizeof("xxx.xxx.xxxxx")];
1138 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1140 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
1141 " expected %s, got %s\n",
1142 SASL_VERSION_STRING, version, 0 );
1148 ldap_pvt_sasl_mutex_new,
1149 ldap_pvt_sasl_mutex_lock,
1150 ldap_pvt_sasl_mutex_unlock,
1151 ldap_pvt_sasl_mutex_dispose );
1153 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1155 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1156 if( rc != SASL_OK ) {
1157 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
1162 /* should provide callbacks for logging */
1163 /* server name should be configurable */
1164 rc = sasl_server_init( server_callbacks, "slapd" );
1166 if( rc != SASL_OK ) {
1167 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
1173 #ifdef SLAPD_SPASSWD
1174 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
1177 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1180 /* default security properties */
1181 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1182 sasl_secprops.max_ssf = INT_MAX;
1183 sasl_secprops.maxbufsize = 65536;
1184 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1190 int slap_sasl_destroy( void )
1192 #ifdef HAVE_CYRUS_SASL
1202 slap_sasl_peer2ipport( struct berval *peer )
1206 *addr = &peer->bv_val[ STRLENOF( "IP=" ) ];
1207 ber_len_t plen = peer->bv_len - STRLENOF( "IP=" );
1210 if ( addr[0] == '[' ) {
1214 ipport = ch_strdup( &addr[isv6] );
1216 /* Convert IPv6/IPv4 addresses to address;port syntax. */
1217 p = strrchr( ipport, ':' );
1221 assert( p[-1] == ']' );
1222 AC_MEMCPY( &p[-1], p, plen - ( p - ipport ) + 1 );
1225 } else if ( isv6 ) {
1228 assert( addr[plen] == ']' );
1235 int slap_sasl_open( Connection *conn, int reopen )
1237 int sc = LDAP_SUCCESS;
1238 #ifdef HAVE_CYRUS_SASL
1241 sasl_conn_t *ctx = NULL;
1242 sasl_callback_t *session_callbacks;
1243 char *ipremoteport = NULL, *iplocalport = NULL;
1245 assert( conn->c_sasl_authctx == NULL );
1248 assert( conn->c_sasl_extra == NULL );
1251 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1252 if( session_callbacks == NULL ) {
1253 Debug( LDAP_DEBUG_ANY,
1254 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1257 conn->c_sasl_extra = session_callbacks;
1259 session_callbacks[cb=0].id = SASL_CB_LOG;
1260 session_callbacks[cb].proc = &slap_sasl_log;
1261 session_callbacks[cb++].context = conn;
1263 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1264 session_callbacks[cb].proc = &slap_sasl_authorize;
1265 session_callbacks[cb++].context = conn;
1267 session_callbacks[cb].id = SASL_CB_CANON_USER;
1268 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1269 session_callbacks[cb++].context = conn;
1271 session_callbacks[cb].id = SASL_CB_LIST_END;
1272 session_callbacks[cb].proc = NULL;
1273 session_callbacks[cb++].context = NULL;
1275 session_callbacks = conn->c_sasl_extra;
1278 conn->c_sasl_layers = 0;
1280 /* create new SASL context */
1281 if ( conn->c_sock_name.bv_len != 0 &&
1282 strncmp( conn->c_sock_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1284 iplocalport = slap_sasl_peer2ipport( &conn->c_sock_name );
1287 if ( conn->c_peer_name.bv_len != 0 &&
1288 strncmp( conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1290 ipremoteport = slap_sasl_peer2ipport( &conn->c_peer_name );
1293 sc = sasl_server_new( "ldap", sasl_host, global_realm,
1294 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1295 if ( iplocalport != NULL ) {
1296 ch_free( iplocalport );
1298 if ( ipremoteport != NULL ) {
1299 ch_free( ipremoteport );
1302 if( sc != SASL_OK ) {
1303 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1309 conn->c_sasl_authctx = ctx;
1311 if( sc == SASL_OK ) {
1312 sc = sasl_setprop( ctx,
1313 SASL_SEC_PROPS, &sasl_secprops );
1315 if( sc != SASL_OK ) {
1316 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1319 slap_sasl_close( conn );
1324 sc = slap_sasl_err2ldap( sc );
1326 #elif defined(SLAP_BUILTIN_SASL)
1327 /* built-in SASL implementation */
1328 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1329 if( ctx == NULL ) return -1;
1331 ctx->sc_external_ssf = 0;
1332 BER_BVZERO( &ctx->sc_external_id );
1334 conn->c_sasl_authctx = ctx;
1340 int slap_sasl_external(
1343 struct berval *auth_id )
1345 #ifdef HAVE_CYRUS_SASL
1347 sasl_conn_t *ctx = conn->c_sasl_authctx;
1348 sasl_ssf_t sasl_ssf = ssf;
1350 if ( ctx == NULL ) {
1351 return LDAP_UNAVAILABLE;
1354 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
1356 if ( sc != SASL_OK ) {
1360 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1361 auth_id ? auth_id->bv_val : NULL );
1363 if ( sc != SASL_OK ) {
1366 #elif defined(SLAP_BUILTIN_SASL)
1367 /* built-in SASL implementation */
1368 SASL_CTX *ctx = conn->c_sasl_authctx;
1369 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1371 ctx->sc_external_ssf = ssf;
1373 ctx->sc_external_id = *auth_id;
1374 BER_BVZERO( auth_id );
1376 BER_BVZERO( &ctx->sc_external_id );
1380 return LDAP_SUCCESS;
1383 int slap_sasl_reset( Connection *conn )
1385 return LDAP_SUCCESS;
1388 char ** slap_sasl_mechs( Connection *conn )
1390 char **mechs = NULL;
1392 #ifdef HAVE_CYRUS_SASL
1393 sasl_conn_t *ctx = conn->c_sasl_authctx;
1395 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1399 SASL_CONST char *mechstr;
1401 sc = sasl_listmech( ctx,
1402 NULL, NULL, ",", NULL,
1403 &mechstr, NULL, NULL );
1405 if( sc != SASL_OK ) {
1406 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1412 mechs = ldap_str2charray( mechstr, "," );
1414 #elif defined(SLAP_BUILTIN_SASL)
1415 /* builtin SASL implementation */
1416 SASL_CTX *ctx = conn->c_sasl_authctx;
1417 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1418 /* should check ssf */
1419 mechs = ldap_str2charray( "EXTERNAL", "," );
1426 int slap_sasl_close( Connection *conn )
1428 #ifdef HAVE_CYRUS_SASL
1429 sasl_conn_t *ctx = conn->c_sasl_authctx;
1432 sasl_dispose( &ctx );
1434 if ( conn->c_sasl_sockctx &&
1435 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1437 ctx = conn->c_sasl_sockctx;
1438 sasl_dispose( &ctx );
1441 conn->c_sasl_authctx = NULL;
1442 conn->c_sasl_sockctx = NULL;
1443 conn->c_sasl_done = 0;
1445 free( conn->c_sasl_extra );
1446 conn->c_sasl_extra = NULL;
1448 #elif defined(SLAP_BUILTIN_SASL)
1449 SASL_CTX *ctx = conn->c_sasl_authctx;
1451 if( ctx->sc_external_id.bv_val ) {
1452 free( ctx->sc_external_id.bv_val );
1453 BER_BVZERO( &ctx->sc_external_id );
1456 conn->c_sasl_authctx = NULL;
1460 return LDAP_SUCCESS;
1463 int slap_sasl_bind( Operation *op, SlapReply *rs )
1465 #ifdef HAVE_CYRUS_SASL
1466 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1467 struct berval response;
1468 unsigned reslen = 0;
1471 Debug(LDAP_DEBUG_ARGS,
1472 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1473 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1474 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1475 op->o_conn->c_sasl_bind_mech.bv_val,
1476 op->orb_cred.bv_len );
1479 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1480 "SASL unavailable on this session" );
1484 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1485 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1486 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1487 sasl_server_step( ctx, cred, clen, resp, rlen )
1489 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1490 /* If we already authenticated once, must use a new context */
1491 if ( op->o_conn->c_sasl_done ) {
1493 const char *authid = NULL;
1494 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1495 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1496 if ( authid ) authid = ch_strdup( authid );
1497 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1498 sasl_dispose( &ctx );
1500 op->o_conn->c_sasl_authctx = NULL;
1502 slap_sasl_open( op->o_conn, 1 );
1503 ctx = op->o_conn->c_sasl_authctx;
1505 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1506 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1507 ch_free( (char *)authid );
1511 op->o_conn->c_sasl_bind_mech.bv_val,
1512 op->orb_cred.bv_val, op->orb_cred.bv_len,
1513 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1517 op->orb_cred.bv_val, op->orb_cred.bv_len,
1518 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1521 response.bv_len = reslen;
1523 if ( sc == SASL_OK ) {
1524 sasl_ssf_t *ssf = NULL;
1526 ber_dupbv_x( &op->orb_edn, &op->o_conn->c_sasl_dn, op->o_tmpmemctx );
1527 BER_BVZERO( &op->o_conn->c_sasl_dn );
1528 op->o_conn->c_sasl_done = 1;
1530 rs->sr_err = LDAP_SUCCESS;
1532 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1533 op->orb_ssf = ssf ? *ssf : 0;
1537 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1538 op->o_conn->c_sasl_layers++;
1540 /* If there's an old layer, set sockctx to NULL to
1541 * tell connection_read() to wait for us to finish.
1542 * Otherwise there is a race condition: we have to
1543 * send the Bind response using the old security
1544 * context and then remove it before reading any
1547 if ( op->o_conn->c_sasl_sockctx ) {
1548 ctx = op->o_conn->c_sasl_sockctx;
1549 op->o_conn->c_sasl_sockctx = NULL;
1551 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1553 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1556 /* Must send response using old security layer */
1557 rs->sr_sasldata = (response.bv_len ? &response : NULL);
1558 send_ldap_sasl( op, rs );
1560 /* Now dispose of the old security layer.
1563 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1564 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1565 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1566 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1567 sasl_dispose( &ctx );
1569 } else if ( sc == SASL_CONTINUE ) {
1570 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1571 rs->sr_text = sasl_errdetail( ctx );
1572 rs->sr_sasldata = &response;
1573 send_ldap_sasl( op, rs );
1576 BER_BVZERO( &op->o_conn->c_sasl_dn );
1577 rs->sr_text = sasl_errdetail( ctx );
1578 rs->sr_err = slap_sasl_err2ldap( sc ),
1579 send_ldap_result( op, rs );
1582 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1584 #elif defined(SLAP_BUILTIN_SASL)
1585 /* built-in SASL implementation */
1586 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1588 if ( ctx == NULL ) {
1589 send_ldap_error( op, rs, LDAP_OTHER,
1590 "Internal SASL Error" );
1592 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1595 if( op->orb_cred.bv_len ) {
1596 rs->sr_text = "proxy authorization not supported";
1597 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1598 send_ldap_result( op, rs );
1601 op->orb_edn = ctx->sc_external_id;
1602 rs->sr_err = LDAP_SUCCESS;
1603 rs->sr_sasldata = NULL;
1604 send_ldap_sasl( op, rs );
1608 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1609 "requested SASL mechanism not supported" );
1612 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1613 "SASL not supported" );
1619 char* slap_sasl_secprops( const char *in )
1621 #ifdef HAVE_CYRUS_SASL
1622 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1624 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1626 return "SASL not supported";
1630 void slap_sasl_secprops_unparse( struct berval *bv )
1632 #ifdef HAVE_CYRUS_SASL
1633 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1637 #ifdef HAVE_CYRUS_SASL
1639 slap_sasl_setpass( Operation *op, SlapReply *rs )
1641 struct berval id = BER_BVNULL; /* needs to come from connection */
1642 struct berval new = BER_BVNULL;
1643 struct berval old = BER_BVNULL;
1645 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1647 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1648 (SASL_CONST void **)(char *)&id.bv_val );
1650 if( rs->sr_err != SASL_OK ) {
1651 rs->sr_text = "unable to retrieve SASL username";
1652 rs->sr_err = LDAP_OTHER;
1656 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1657 id.bv_val ? id.bv_val : "", 0, 0 );
1659 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1660 NULL, &old, &new, &rs->sr_text );
1662 if( rs->sr_err != LDAP_SUCCESS ) {
1666 if( new.bv_len == 0 ) {
1667 slap_passwd_generate(&new);
1669 if( new.bv_len == 0 ) {
1670 rs->sr_text = "password generation failed.";
1671 rs->sr_err = LDAP_OTHER;
1675 rs->sr_rspdata = slap_passwd_return( &new );
1678 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1679 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1680 if( rs->sr_err != SASL_OK ) {
1681 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1683 switch(rs->sr_err) {
1685 rs->sr_err = LDAP_SUCCESS;
1695 rs->sr_err = LDAP_OTHER;
1701 #endif /* HAVE_CYRUS_SASL */
1703 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1704 * string returned in *dn is in its own allocated memory, and must be free'd
1705 * by the calling process. -Mark Adamson, Carnegie Mellon
1707 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1708 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1715 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1716 char *user_realm, struct berval *dn, int flags )
1718 int rc, is_dn = SET_NONE, do_norm = 1;
1719 struct berval dn2, *mech;
1721 assert( conn != NULL );
1722 assert( id != NULL );
1724 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1726 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1727 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1728 (unsigned long) id->bv_len ) );
1731 op = conn->c_sasl_bindop;
1733 assert( op != NULL );
1737 if ( !BER_BVISNULL( id ) ) {
1738 /* Blatantly anonymous ID */
1739 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1741 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1742 return( LDAP_SUCCESS );
1746 /* FIXME: if empty, should we stop? */
1747 BER_BVSTR( id, "" );
1750 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1751 mech = &conn->c_sasl_bind_mech;
1753 mech = &conn->c_authmech;
1756 /* An authcID needs to be converted to authzID form. Set the
1757 * values directly into *dn; they will be normalized later. (and
1758 * normalizing always makes a new copy.) An ID from a TLS certificate
1759 * is already normalized, so copy it and skip normalization.
1761 if( flags & SLAP_GETDN_AUTHCID ) {
1762 if( bvmatch( mech, &ext_bv )) {
1763 /* EXTERNAL DNs are already normalized */
1764 assert( !BER_BVISNULL( id ) );
1768 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1771 /* convert to u:<username> form */
1777 if( is_dn == SET_NONE ) {
1778 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1780 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1781 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1783 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1785 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1786 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1790 /* No other possibilities from here */
1791 if( is_dn == SET_NONE ) {
1793 return( LDAP_INAPPROPRIATE_AUTH );
1796 /* Username strings */
1797 if( is_dn == SET_U ) {
1798 /* ITS#3419: values may need escape */
1800 LDAPAVA *RDNs[ 4 ][ 2 ];
1805 DN[ irdn ] = RDNs[ irdn ];
1806 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1807 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1808 AVAs[ irdn ].la_value = *dn;
1809 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1810 AVAs[ irdn ].la_private = NULL;
1811 RDNs[ irdn ][ 1 ] = NULL;
1813 if ( user_realm && *user_realm ) {
1815 DN[ irdn ] = RDNs[ irdn ];
1816 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1817 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1818 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1819 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1820 AVAs[ irdn ].la_private = NULL;
1821 RDNs[ irdn ][ 1 ] = NULL;
1824 if ( !BER_BVISNULL( mech ) ) {
1826 DN[ irdn ] = RDNs[ irdn ];
1827 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1828 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1829 AVAs[ irdn ].la_value = *mech;
1830 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1831 AVAs[ irdn ].la_private = NULL;
1832 RDNs[ irdn ][ 1 ] = NULL;
1836 DN[ irdn ] = RDNs[ irdn ];
1837 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1838 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1839 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1840 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1841 AVAs[ irdn ].la_private = NULL;
1842 RDNs[ irdn ][ 1 ] = NULL;
1847 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
1849 if ( rc != LDAP_SUCCESS ) {
1854 Debug( LDAP_DEBUG_TRACE,
1855 "slap_sasl_getdn: u:id converted to %s\n",
1860 /* Dup the DN in any case, so we don't risk
1861 * leaks or dangling pointers later,
1862 * and the DN value is '\0' terminated */
1863 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1864 dn->bv_val = dn2.bv_val;
1867 /* All strings are in DN form now. Normalize if needed. */
1869 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1871 /* User DNs were constructed above and must be freed now */
1872 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1874 if ( rc != LDAP_SUCCESS ) {
1881 /* Run thru regexp */
1882 slap_sasl2dn( op, dn, &dn2, flags );
1883 if( !BER_BVISNULL( &dn2 ) ) {
1884 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1886 Debug( LDAP_DEBUG_TRACE,
1887 "slap_sasl_getdn: dn:id converted to %s\n",
1891 return( LDAP_SUCCESS );