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 #ifdef SLAP_AUXPROP_DONTUSECOPY
159 int slap_dontUseCopy_ignore;
160 BerVarray slap_dontUseCopy_propnames;
161 #endif /* SLAP_AUXPROP_DONTUSECOPY */
163 static Filter generic_filter = { LDAP_FILTER_PRESENT, { 0 }, NULL };
164 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
166 #define SLAP_SASL_PROP_CONN 0
167 #define SLAP_SASL_PROP_AUTHCLEN 1
168 #define SLAP_SASL_PROP_AUTHC 2
169 #define SLAP_SASL_PROP_AUTHZLEN 3
170 #define SLAP_SASL_PROP_AUTHZ 4
171 #define SLAP_SASL_PROP_COUNT 5 /* Number of properties we used */
173 typedef struct lookup_info {
175 const struct propval *list;
176 sasl_server_params_t *sparams;
179 static slap_response sasl_ap_lookup;
181 static struct berval sc_cleartext = BER_BVC("{CLEARTEXT}");
184 sasl_ap_lookup( Operation *op, SlapReply *rs )
187 AttributeDescription *ad;
191 lookup_info *sl = (lookup_info *)op->o_callback->sc_private;
193 /* return the actual error code,
194 * to allow caller to handle specific errors
196 if (rs->sr_type != REP_SEARCH) return rs->sr_err;
198 for( i = 0; sl->list[i].name; i++ ) {
199 const char *name = sl->list[i].name;
201 if ( name[0] == '*' ) {
202 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
203 /* Skip our private properties */
204 if ( !strcmp( name, slap_propnames[0] )) {
205 i += SLAP_SASL_PROP_COUNT - 1;
209 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
212 if ( sl->list[i].values ) {
213 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
216 rc = slap_str2ad( name, &ad, &text );
217 if ( rc != LDAP_SUCCESS ) {
218 Debug( LDAP_DEBUG_TRACE,
219 "slap_ap_lookup: str2ad(%s): %s\n", name, text, 0 );
223 /* If it's the rootdn and a rootpw was present, we already set
224 * it so don't override it here.
226 if ( ad == slap_schema.si_ad_userPassword && sl->list[i].values &&
227 be_isroot_dn( op->o_bd, &op->o_req_ndn ))
230 a = attr_find( rs->sr_entry->e_attrs, ad );
232 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
235 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
236 sl->sparams->utils->prop_erase( sl->sparams->propctx,
239 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
240 /* ITS#3846 don't give hashed passwords to SASL */
241 if ( ad == slap_schema.si_ad_userPassword &&
242 bv->bv_val[0] == '{' /*}*/ )
244 if ( lutil_passwd_scheme( bv->bv_val ) ) {
245 /* If it's not a recognized scheme, just assume it's
246 * a cleartext password that happened to include brackets.
248 * If it's a recognized scheme, skip this value, unless the
249 * scheme is {CLEARTEXT}. In that case, skip over the
250 * scheme name and use the remainder. If there is nothing
251 * past the scheme name, skip this value.
253 #ifdef SLAPD_CLEARTEXT
254 if ( !strncasecmp( bv->bv_val, sc_cleartext.bv_val,
255 sc_cleartext.bv_len )) {
257 cbv.bv_len = bv->bv_len - sc_cleartext.bv_len;
258 if ( cbv.bv_len > 0 ) {
259 cbv.bv_val = bv->bv_val + sc_cleartext.bv_len;
260 sl->sparams->utils->prop_set( sl->sparams->propctx,
261 sl->list[i].name, cbv.bv_val, cbv.bv_len );
268 sl->sparams->utils->prop_set( sl->sparams->propctx,
269 sl->list[i].name, bv->bv_val, bv->bv_len );
275 #if SASL_VERSION_FULL >= 0x020118
282 sasl_server_params_t *sparams,
287 OperationBuffer opbuf = {{ NULL }};
288 Operation *op = (Operation *)&opbuf;
290 Connection *conn = NULL;
292 int rc = LDAP_SUCCESS;
293 #ifdef SLAP_AUXPROP_DONTUSECOPY
295 BackendDB *dontUseCopy_bd = NULL;
296 #endif /* SLAP_AUXPROP_DONTUSECOPY */
298 sl.list = sparams->utils->prop_get( sparams->propctx );
299 sl.sparams = sparams;
302 /* Find our DN and conn first */
303 for( i = 0; sl.list[i].name; i++ ) {
304 if ( sl.list[i].name[0] == '*' ) {
305 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
306 if ( sl.list[i].values && sl.list[i].values[0] )
307 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
310 if ( flags & SASL_AUXPROP_AUTHZID ) {
311 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZLEN] )) {
312 if ( sl.list[i].values && sl.list[i].values[0] )
313 AC_MEMCPY( &op->o_req_ndn.bv_len, sl.list[i].values[0],
314 sizeof( op->o_req_ndn.bv_len ) );
315 } else if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHZ] )) {
316 if ( sl.list[i].values )
317 op->o_req_ndn.bv_val = (char *)sl.list[i].values[0];
322 if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
323 if ( sl.list[i].values && sl.list[i].values[0] )
324 AC_MEMCPY( &op->o_req_ndn.bv_len, sl.list[i].values[0],
325 sizeof( op->o_req_ndn.bv_len ) );
326 } else if ( !strcmp( sl.list[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
327 if ( sl.list[i].values ) {
328 op->o_req_ndn.bv_val = (char *)sl.list[i].values[0];
329 if ( !(flags & SASL_AUXPROP_AUTHZID) )
333 #ifdef SLAP_AUXPROP_DONTUSECOPY
334 if ( slap_dontUseCopy_propnames != NULL ) {
337 ber_str2bv( &sl.list[i].name[1], 0, 1, &bv );
338 for ( j = 0; !BER_BVISNULL( &slap_dontUseCopy_propnames[ j ]); j++ ) {
339 if ( bvmatch( &bv, &slap_dontUseCopy_propnames[ j ] ) ) {
345 #endif /* SLAP_AUXPROP_DONTUSECOPY */
349 /* we don't know anything about this, ignore it */
353 /* Now see what else needs to be fetched */
354 for( i = 0; sl.list[i].name; i++ ) {
355 const char *name = sl.list[i].name;
357 if ( name[0] == '*' ) {
358 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
359 /* Skip our private properties */
360 if ( !strcmp( name, slap_propnames[0] )) {
361 i += SLAP_SASL_PROP_COUNT - 1;
365 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
368 if ( sl.list[i].values ) {
369 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
376 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
380 op->o_bd = select_backend( &op->o_req_ndn, 1 );
383 /* For rootdn, see if we can use the rootpw */
384 if ( be_isroot_dn( op->o_bd, &op->o_req_ndn ) &&
385 !BER_BVISEMPTY( &op->o_bd->be_rootpw )) {
386 struct berval cbv = BER_BVNULL;
388 /* If there's a recognized scheme, see if it's CLEARTEXT */
389 if ( lutil_passwd_scheme( op->o_bd->be_rootpw.bv_val )) {
390 if ( !strncasecmp( op->o_bd->be_rootpw.bv_val,
391 sc_cleartext.bv_val, sc_cleartext.bv_len )) {
393 /* If it's CLEARTEXT, skip past scheme spec */
394 cbv.bv_len = op->o_bd->be_rootpw.bv_len -
397 cbv.bv_val = op->o_bd->be_rootpw.bv_val +
401 /* No scheme, use the whole value */
403 cbv = op->o_bd->be_rootpw;
405 if ( !BER_BVISEMPTY( &cbv )) {
406 for( i = 0; sl.list[i].name; i++ ) {
407 const char *name = sl.list[i].name;
409 if ( name[0] == '*' ) {
410 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
412 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
415 if ( !strcasecmp(name,"userPassword") ) {
416 sl.sparams->utils->prop_set( sl.sparams->propctx,
417 sl.list[i].name, cbv.bv_val, cbv.bv_len );
424 #ifdef SLAP_AUXPROP_DONTUSECOPY
425 if ( SLAP_SHADOW( op->o_bd ) && dontUseCopy ) {
426 dontUseCopy_bd = op->o_bd;
427 op->o_bd = frontendDB;
431 #endif /* SLAP_AUXPROP_DONTUSECOPY */
433 if ( op->o_bd->be_search ) {
434 SlapReply rs = {REP_RESULT};
435 #ifdef SLAP_AUXPROP_DONTUSECOPY
436 LDAPControl **save_ctrls = NULL, c;
437 int save_dontUseCopy;
438 #endif /* SLAP_AUXPROP_DONTUSECOPY */
440 op->o_hdr = conn->c_sasl_bindop->o_hdr;
441 op->o_controls = opbuf.ob_controls;
442 op->o_tag = LDAP_REQ_SEARCH;
443 op->o_dn = conn->c_ndn;
444 op->o_ndn = conn->c_ndn;
445 op->o_callback = &cb;
446 slap_op_time( &op->o_time, &op->o_tincr );
447 op->o_do_not_cache = 1;
448 op->o_is_auth_check = 1;
449 op->o_req_dn = op->o_req_ndn;
450 op->ors_scope = LDAP_SCOPE_BASE;
451 op->ors_deref = LDAP_DEREF_NEVER;
452 op->ors_tlimit = SLAP_NO_LIMIT;
454 op->ors_filter = &generic_filter;
455 op->ors_filterstr = generic_filterstr;
456 op->o_authz = conn->c_authz;
457 /* FIXME: we want all attributes, right? */
458 op->ors_attrs = NULL;
460 #ifdef SLAP_AUXPROP_DONTUSECOPY
462 save_dontUseCopy = op->o_dontUseCopy;
463 if ( !op->o_dontUseCopy ) {
465 save_ctrls = op->o_ctrls;
467 for ( ; op->o_ctrls[ cnt ]; cnt++ )
470 op->o_ctrls = op->o_tmpcalloc( sizeof(LDAPControl *), cnt + 2, op->o_tmpmemctx );
472 for ( cnt = 0; save_ctrls[ cnt ]; cnt++ ) {
473 op->o_ctrls[ cnt ] = save_ctrls[ cnt ];
476 c.ldctl_oid = LDAP_CONTROL_DONTUSECOPY;
477 c.ldctl_iscritical = 1;
478 BER_BVZERO( &c.ldctl_value );
479 op->o_ctrls[ cnt ] = &c;
481 op->o_dontUseCopy = SLAP_CONTROL_CRITICAL;
483 #endif /* SLAP_AUXPROP_DONTUSECOPY */
485 rc = op->o_bd->be_search( op, &rs );
487 #ifdef SLAP_AUXPROP_DONTUSECOPY
489 if ( save_ctrls != op->o_ctrls ) {
490 op->o_tmpfree( op->o_ctrls, op->o_tmpmemctx );
491 op->o_ctrls = save_ctrls;
492 op->o_dontUseCopy = save_dontUseCopy;
495 if ( rs.sr_err == LDAP_UNAVAILABLE && slap_dontUseCopy_ignore )
497 op->o_bd = dontUseCopy_bd;
499 goto retry_dontUseCopy;
502 #endif /* SLAP_AUXPROP_DONTUSECOPY */
506 #if SASL_VERSION_FULL >= 0x020118
507 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
511 #if SASL_VERSION_FULL >= 0x020110
515 sasl_server_params_t *sparams,
516 struct propctx *prctx,
524 Connection *conn = NULL;
525 const struct propval *pr;
526 Modifications *modlist = NULL, **modtail = &modlist, *mod;
527 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
528 char textbuf[SLAP_TEXT_BUFLEN];
530 size_t textlen = sizeof(textbuf);
531 #ifdef SLAP_AUXPROP_DONTUSECOPY
533 BackendDB *dontUseCopy_bd = NULL;
534 #endif /* SLAP_AUXPROP_DONTUSECOPY */
536 /* just checking if we are enabled */
537 if (!prctx) return SASL_OK;
539 if (!sparams || !user) return SASL_BADPARAM;
541 pr = sparams->utils->prop_get( sparams->propctx );
543 /* Find our DN and conn first */
544 for( i = 0; pr[i].name; i++ ) {
545 if ( pr[i].name[0] == '*' ) {
546 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_CONN] ) ) {
547 if ( pr[i].values && pr[i].values[0] )
548 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
551 if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHCLEN] )) {
552 if ( pr[i].values && pr[i].values[0] )
553 AC_MEMCPY( &op.o_req_ndn.bv_len, pr[i].values[0],
554 sizeof( op.o_req_ndn.bv_len ) );
555 } else if ( !strcmp( pr[i].name, slap_propnames[SLAP_SASL_PROP_AUTHC] ) ) {
557 op.o_req_ndn.bv_val = (char *)pr[i].values[0];
559 #ifdef SLAP_AUXPROP_DONTUSECOPY
560 if ( slap_dontUseCopy_propnames != NULL ) {
562 ber_str2bv( &pr[i].name[1], 0, 1, &bv );
563 for ( j = 0; !BER_BVISNULL( &slap_dontUseCopy_propnames[ j ] ); j++ ) {
564 if ( bvmatch( &bv, &slap_dontUseCopy_propnames[ j ] ) ) {
570 #endif /* SLAP_AUXPROP_DONTUSECOPY */
573 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
575 op.o_bd = select_backend( &op.o_req_ndn, 1 );
577 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
579 #ifdef SLAP_AUXPROP_DONTUSECOPY
580 if ( SLAP_SHADOW( op.o_bd ) && dontUseCopy ) {
581 dontUseCopy_bd = op.o_bd;
582 op.o_bd = frontendDB;
583 op.o_dontUseCopy = SLAP_CONTROL_CRITICAL;
585 #endif /* SLAP_AUXPROP_DONTUSECOPY */
587 pr = sparams->utils->prop_get( prctx );
588 if (!pr) return SASL_BADPARAM;
590 for (i=0; pr[i].name; i++);
591 if (!i) return SASL_BADPARAM;
593 for (i=0; pr[i].name; i++) {
594 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
595 mod->sml_op = LDAP_MOD_REPLACE;
597 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
598 mod->sml_numvals = pr[i].nvalues;
599 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
600 sizeof(struct berval));
601 for (j=0; j<pr[i].nvalues; j++) {
602 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
604 BER_BVZERO( &mod->sml_values[j] );
605 mod->sml_nvalues = NULL;
606 mod->sml_desc = NULL;
608 modtail = &mod->sml_next;
612 rc = slap_mods_check( &op, modlist, &text, textbuf, textlen, NULL );
614 if ( rc == LDAP_SUCCESS ) {
615 rc = slap_mods_no_user_mod_check( &op, modlist,
616 &text, textbuf, textlen );
618 if ( rc == LDAP_SUCCESS ) {
619 if ( conn->c_sasl_bindop ) {
620 op.o_hdr = conn->c_sasl_bindop->o_hdr;
623 memset( &oph, 0, sizeof(oph) );
624 operation_fake_init( conn, &op, ldap_pvt_thread_pool_context(), 0 );
626 op.o_tag = LDAP_REQ_MODIFY;
627 op.o_ndn = op.o_req_ndn;
629 slap_op_time( &op.o_time, &op.o_tincr );
630 op.o_do_not_cache = 1;
631 op.o_is_auth_check = 1;
632 op.o_req_dn = op.o_req_ndn;
633 op.orm_modlist = modlist;
636 SlapReply rs = {REP_RESULT};
637 rc = op.o_bd->be_modify( &op, &rs );
639 #ifdef SLAP_AUXPROP_DONTUSECOPY
641 rs.sr_err == LDAP_UNAVAILABLE &&
642 slap_dontUseCopy_ignore )
644 op.o_bd = dontUseCopy_bd;
645 op.o_dontUseCopy = SLAP_CONTROL_NONE;
649 #endif /* SLAP_AUXPROP_DONTUSECOPY */
654 slap_mods_free( modlist, 1 );
655 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
657 #endif /* SASL_VERSION_FULL >= 2.1.16 */
659 static sasl_auxprop_plug_t slap_auxprop_plugin = {
662 NULL, /* glob_context */
663 NULL, /* auxprop_free */
666 #if SASL_VERSION_FULL >= 0x020110
667 slap_auxprop_store /* the declaration of this member changed
668 * in cyrus SASL from 2.1.15 to 2.1.16 */
676 const sasl_utils_t *utils,
679 sasl_auxprop_plug_t **plug,
680 const char *plugname)
682 if ( !out_version || !plug ) return SASL_BADPARAM;
684 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
686 *out_version = SASL_AUXPROP_PLUG_VERSION;
687 *plug = &slap_auxprop_plugin;
691 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
692 * auxiliary property, so that we can refer to it in sasl_authorize
693 * without interfering with anything else. Also, the SASL username
694 * buffer is constrained to 256 characters, and our DNs could be
695 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
698 slap_sasl_canonicalize(
704 const char *user_realm,
709 Connection *conn = (Connection *)context;
710 struct propctx *props = sasl_auxprop_getctx( sconn );
711 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
714 const char *names[2];
719 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
720 conn ? (long) conn->c_connid : -1L,
721 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
722 in ? in : "<empty>");
724 /* If name is too big, just truncate. We don't care, we're
725 * using DNs, not the usernames.
727 if ( inlen > out_max )
730 /* This is a Simple Bind using SPASSWD. That means the in-directory
731 * userPassword of the Binding user already points at SASL, so it
732 * cannot be used to actually satisfy a password comparison. Just
733 * ignore it, some other mech will process it.
735 if ( !conn->c_sasl_bindop ||
736 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
738 /* See if we need to add request, can only do it once */
739 prop_getnames( props, slap_propnames, auxvals );
740 if ( !auxvals[0].name )
741 prop_request( props, slap_propnames );
743 if ( flags & SASL_CU_AUTHID )
744 which = SLAP_SASL_PROP_AUTHCLEN;
746 which = SLAP_SASL_PROP_AUTHZLEN;
748 /* Need to store the Connection for auxprop_lookup */
749 if ( !auxvals[SLAP_SASL_PROP_CONN].values ) {
750 names[0] = slap_propnames[SLAP_SASL_PROP_CONN];
752 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
755 /* Already been here? */
756 if ( auxvals[which].values )
759 /* Normally we require an authzID to have a u: or dn: prefix.
760 * However, SASL frequently gives us an authzID that is just
761 * an exact copy of the authcID, without a prefix. We need to
762 * detect and allow this condition. If SASL calls canonicalize
763 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
764 * But if it's broken into two calls, we need to remember the
765 * authcID so that we can compare the authzID later. We store
766 * the authcID temporarily in conn->c_sasl_dn. We necessarily
767 * finish Canonicalizing before Authorizing, so there is no
768 * conflict with slap_sasl_authorize's use of this temp var.
770 * The SASL EXTERNAL mech is backwards from all the other mechs,
771 * it does authzID before the authcID. If we see that authzID
772 * has already been done, don't do anything special with authcID.
774 if ( flags == SASL_CU_AUTHID && !auxvals[SLAP_SASL_PROP_AUTHZ].values ) {
775 conn->c_sasl_dn.bv_val = (char *) in;
776 conn->c_sasl_dn.bv_len = 0;
777 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
778 rc = strcmp( in, conn->c_sasl_dn.bv_val );
779 conn->c_sasl_dn.bv_val = NULL;
780 /* They were equal, no work needed */
781 if ( !rc ) goto done;
784 bvin.bv_val = (char *)in;
786 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
787 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
788 if ( rc != LDAP_SUCCESS ) {
789 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
793 names[0] = slap_propnames[which];
795 prop_set( props, names[0], (char *)&dn.bv_len, sizeof( dn.bv_len ) );
798 names[0] = slap_propnames[which];
799 prop_set( props, names[0], dn.bv_val, dn.bv_len );
801 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
802 conn ? (long) conn->c_connid : -1L, names[0]+1,
803 dn.bv_val ? dn.bv_val : "<EMPTY>" );
805 /* Not needed any more, SASL has copied it */
806 if ( conn && conn->c_sasl_bindop )
807 conn->c_sasl_bindop->o_tmpfree( dn.bv_val, conn->c_sasl_bindop->o_tmpmemctx );
810 AC_MEMCPY( out, in, inlen );
822 char *requested_user,
826 const char *def_realm,
828 struct propctx *props)
830 Connection *conn = (Connection *)context;
832 * (SLAP_SASL_PROP_COUNT - 1) because we skip "conn",
833 * + 1 for NULL termination?
835 struct propval auxvals[ SLAP_SASL_PROP_COUNT ] = { { 0 } };
836 struct berval authcDN, authzDN = BER_BVNULL;
839 /* Simple Binds don't support proxy authorization, ignore it */
840 if ( !conn->c_sasl_bindop ||
841 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
843 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
844 "authcid=\"%s\" authzid=\"%s\"\n",
845 conn ? (long) conn->c_connid : -1L, auth_identity, requested_user );
846 if ( conn->c_sasl_dn.bv_val ) {
847 BER_BVZERO( &conn->c_sasl_dn );
850 /* Skip SLAP_SASL_PROP_CONN */
851 prop_getnames( props, slap_propnames+1, auxvals );
853 /* Should not happen */
854 if ( !auxvals[0].values ) {
855 sasl_seterror( sconn, 0, "invalid authcid" );
859 AC_MEMCPY( &authcDN.bv_len, auxvals[0].values[0], sizeof(authcDN.bv_len) );
860 authcDN.bv_val = auxvals[1].values ? (char *)auxvals[1].values[0] : NULL;
861 conn->c_sasl_dn = authcDN;
863 /* Nothing to do if no authzID was given */
864 if ( !auxvals[2].name || !auxvals[2].values ) {
868 AC_MEMCPY( &authzDN.bv_len, auxvals[2].values[0], sizeof(authzDN.bv_len) );
869 authzDN.bv_val = auxvals[3].values ? (char *)auxvals[3].values[0] : NULL;
871 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
872 if ( rc != LDAP_SUCCESS ) {
873 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
874 "proxy authorization disallowed (%d)\n",
875 conn ? (long) conn->c_connid : -1L, rc, 0 );
877 sasl_seterror( sconn, 0, "not authorized" );
881 /* FIXME: we need yet another dup because slap_sasl_getdn()
882 * is using the bind operation slab */
883 ber_dupbv( &conn->c_sasl_authz_dn, &authzDN );
886 if (conn->c_sasl_bindop) {
887 Statslog( LDAP_DEBUG_STATS,
888 "%s BIND authcid=\"%s\" authzid=\"%s\"\n",
889 conn->c_sasl_bindop->o_log_prefix,
890 auth_identity, requested_user, 0, 0 );
893 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
894 " proxy authorization allowed authzDN=\"%s\"\n",
895 conn ? (long) conn->c_connid : -1L,
896 authzDN.bv_val ? authzDN.bv_val : "", 0 );
901 slap_sasl_err2ldap( int saslerr )
905 /* map SASL errors to LDAP resultCode returned by:
907 * SASL_OK, SASL_NOMEM
909 * SASL_OK, SASL_CONTINUE, SASL_TRANS, SASL_BADPARAM, SASL_BADPROT,
911 * sasl_server_start()
914 * SASL_OK, SASL_BADPARAM
922 rc = LDAP_SASL_BIND_IN_PROGRESS;
929 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
935 rc = LDAP_INVALID_CREDENTIALS;
938 rc = LDAP_INSUFFICIENT_ACCESS;
942 rc = LDAP_INAPPROPRIATE_AUTH;
946 rc = LDAP_UNAVAILABLE;
949 rc = LDAP_UNWILLING_TO_PERFORM;
961 static struct berval sasl_pwscheme = BER_BVC("{SASL}");
964 const struct berval *sc,
965 const struct berval * passwd,
966 const struct berval * cred,
971 void *ctx, *sconn = NULL;
973 for( i=0; i<cred->bv_len; i++) {
974 if(cred->bv_val[i] == '\0') {
975 return LUTIL_PASSWD_ERR; /* NUL character in password */
979 if( cred->bv_val[i] != '\0' ) {
980 return LUTIL_PASSWD_ERR; /* cred must behave like a string */
983 for( i=0; i<passwd->bv_len; i++) {
984 if(passwd->bv_val[i] == '\0') {
985 return LUTIL_PASSWD_ERR; /* NUL character in password */
989 if( passwd->bv_val[i] != '\0' ) {
990 return LUTIL_PASSWD_ERR; /* passwd must behave like a string */
993 rtn = LUTIL_PASSWD_ERR;
995 ctx = ldap_pvt_thread_pool_context();
996 ldap_pvt_thread_pool_getkey( ctx, (void *)slap_sasl_bind, &sconn, NULL );
998 if( sconn != NULL ) {
1000 sc = sasl_checkpass( sconn,
1001 passwd->bv_val, passwd->bv_len,
1002 cred->bv_val, cred->bv_len );
1003 rtn = ( sc != SASL_OK ) ? LUTIL_PASSWD_ERR : LUTIL_PASSWD_OK;
1008 #endif /* SLAPD_SPASSWD */
1010 #endif /* HAVE_CYRUS_SASL */
1012 #ifdef ENABLE_REWRITE
1014 typedef struct slapd_map_data {
1016 struct berval filter;
1017 AttributeName attrs[2];
1022 slapd_rw_config( const char *fname, int lineno, int argc, char **argv )
1024 slapd_map_data *ret = NULL;
1025 LDAPURLDesc *lud = NULL;
1027 AttributeDescription *ad = NULL;
1029 struct berval dn, ndn;
1032 Debug( LDAP_DEBUG_ANY,
1033 "[%s:%d] slapd map needs URI\n",
1039 if ( strncasecmp( uri, "uri=", STRLENOF( "uri=" ) ) == 0 ) {
1040 uri += STRLENOF( "uri=" );
1043 if ( ldap_url_parse( uri, &lud ) != LDAP_URL_SUCCESS ) {
1044 Debug( LDAP_DEBUG_ANY,
1045 "[%s:%d] illegal URI '%s'\n",
1046 fname, lineno, uri );
1050 if ( strcasecmp( lud->lud_scheme, "ldap" )) {
1051 Debug( LDAP_DEBUG_ANY,
1052 "[%s:%d] illegal URI scheme '%s'\n",
1053 fname, lineno, lud->lud_scheme );
1057 if (( lud->lud_host && lud->lud_host[0] ) || lud->lud_exts
1059 Debug( LDAP_DEBUG_ANY,
1060 "[%s:%d] illegal URI '%s'\n",
1061 fname, lineno, uri );
1065 if ( lud->lud_attrs ) {
1066 if ( lud->lud_attrs[1] ) {
1067 Debug( LDAP_DEBUG_ANY,
1068 "[%s:%d] only one attribute allowed in URI\n",
1072 if ( strcasecmp( lud->lud_attrs[0], "dn" ) &&
1073 strcasecmp( lud->lud_attrs[0], "entryDN" )) {
1075 rc = slap_str2ad( lud->lud_attrs[0], &ad, &text );
1080 ber_str2bv( lud->lud_dn, 0, 0, &dn );
1081 if ( dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL ))
1084 if ( lud->lud_filter ) {
1085 flen = strlen( lud->lud_filter ) + 1;
1087 ret = ch_malloc( sizeof( slapd_map_data ) + flen );
1090 ret->filter.bv_val = (char *)(ret+1);
1091 ret->filter.bv_len = flen - 1;
1092 strcpy( ret->filter.bv_val, lud->lud_filter );
1094 BER_BVZERO( &ret->filter );
1096 ret->scope = lud->lud_scope;
1098 ret->attrs[0].an_name = ad->ad_cname;
1100 BER_BVZERO( &ret->attrs[0].an_name );
1102 ret->attrs[0].an_desc = ad;
1103 BER_BVZERO( &ret->attrs[1].an_name );
1105 ldap_free_urldesc( lud );
1109 struct slapd_rw_info {
1110 slapd_map_data *si_data;
1111 struct berval si_val;
1115 slapd_rw_cb( Operation *op, SlapReply *rs )
1117 if ( rs->sr_type == REP_SEARCH ) {
1118 struct slapd_rw_info *si = op->o_callback->sc_private;
1120 if ( si->si_data->attrs[0].an_desc ) {
1123 a = attr_find( rs->sr_entry->e_attrs,
1124 si->si_data->attrs[0].an_desc );
1126 ber_dupbv( &si->si_val, a->a_vals );
1129 ber_dupbv( &si->si_val, &rs->sr_entry->e_name );
1132 return LDAP_SUCCESS;
1136 slapd_rw_apply( void *private, const char *filter, struct berval *val )
1138 slapd_map_data *sl = private;
1139 slap_callback cb = { NULL };
1140 Connection conn = {0};
1141 OperationBuffer opbuf;
1144 SlapReply rs = {REP_RESULT};
1145 struct slapd_rw_info si;
1149 thrctx = ldap_pvt_thread_pool_context();
1150 connection_fake_init2( &conn, &opbuf, thrctx, 0 );
1153 op->o_tag = LDAP_REQ_SEARCH;
1154 op->o_req_dn = op->o_req_ndn = sl->base;
1155 op->o_bd = select_backend( &op->o_req_ndn, 1 );
1160 BER_BVZERO( &si.si_val );
1161 op->ors_scope = sl->scope;
1162 op->ors_deref = LDAP_DEREF_NEVER;
1164 op->ors_tlimit = SLAP_NO_LIMIT;
1165 if ( sl->attrs[0].an_desc ) {
1166 op->ors_attrs = sl->attrs;
1168 op->ors_attrs = slap_anlist_no_attrs;
1171 rc = strlen( filter );
1175 rc += sl->filter.bv_len;
1176 ptr = op->ors_filterstr.bv_val = op->o_tmpalloc( rc + 1, op->o_tmpmemctx );
1177 if ( sl->filter.bv_len ) {
1178 ptr = lutil_strcopy( ptr, sl->filter.bv_val );
1183 strcpy( ptr, filter );
1185 op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
1186 if ( !op->ors_filter ) {
1187 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1191 op->ors_attrsonly = 0;
1192 op->o_dn = op->o_bd->be_rootdn;
1193 op->o_ndn = op->o_bd->be_rootndn;
1194 op->o_do_not_cache = 1;
1196 cb.sc_response = slapd_rw_cb;
1197 cb.sc_private = &si;
1198 op->o_callback = &cb;
1200 rc = op->o_bd->be_search( op, &rs );
1201 if ( rc == LDAP_SUCCESS && !BER_BVISNULL( &si.si_val )) {
1203 rc = REWRITE_SUCCESS;
1205 if ( !BER_BVISNULL( &si.si_val )) {
1206 ch_free( si.si_val.bv_val );
1210 filter_free_x( op, op->ors_filter, 1 );
1211 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1216 slapd_rw_destroy( void *private )
1218 slapd_map_data *md = private;
1220 assert( private != NULL );
1222 ch_free( md->base.bv_val );
1228 static const rewrite_mapper slapd_mapper = {
1236 int slap_sasl_init( void )
1238 #ifdef HAVE_CYRUS_SASL
1240 static sasl_callback_t server_callbacks[] = {
1241 { SASL_CB_LOG, (slap_sasl_cb_ft)&slap_sasl_log, NULL },
1242 { SASL_CB_GETOPT, (slap_sasl_cb_ft)&slap_sasl_getopt, NULL },
1243 { SASL_CB_LIST_END, NULL, NULL }
1247 #ifdef ENABLE_REWRITE
1248 rewrite_mapper_register( &slapd_mapper );
1251 #ifdef HAVE_CYRUS_SASL
1252 #ifdef HAVE_SASL_VERSION
1253 /* stringify the version number, sasl.h doesn't do it for us */
1254 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1255 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1256 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1259 sasl_version( NULL, &rc );
1260 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1261 (rc & 0xffff) < SASL_VERSION_STEP)
1263 char version[sizeof("xxx.xxx.xxxxx")];
1264 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1266 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
1267 " expected %s, got %s\n",
1268 SASL_VERSION_STRING, version, 0 );
1274 ldap_pvt_sasl_mutex_new,
1275 ldap_pvt_sasl_mutex_lock,
1276 ldap_pvt_sasl_mutex_unlock,
1277 ldap_pvt_sasl_mutex_dispose );
1279 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1281 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1282 if( rc != SASL_OK ) {
1283 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
1288 /* should provide callbacks for logging */
1289 /* server name should be configurable */
1290 rc = sasl_server_init( server_callbacks, "slapd" );
1292 if( rc != SASL_OK ) {
1293 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
1299 #ifdef SLAPD_SPASSWD
1300 lutil_passwd_add( &sasl_pwscheme, chk_sasl, NULL );
1303 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1306 /* default security properties */
1307 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1308 sasl_secprops.max_ssf = INT_MAX;
1309 sasl_secprops.maxbufsize = 65536;
1310 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1316 int slap_sasl_destroy( void )
1318 #ifdef HAVE_CYRUS_SASL
1321 #ifdef SLAP_AUXPROP_DONTUSECOPY
1322 if ( slap_dontUseCopy_propnames ) {
1323 ber_bvarray_free( slap_dontUseCopy_propnames );
1324 slap_dontUseCopy_propnames = NULL;
1326 #endif /* SLAP_AUXPROP_DONTUSECOPY */
1335 slap_sasl_peer2ipport( struct berval *peer )
1339 *addr = &peer->bv_val[ STRLENOF( "IP=" ) ];
1340 ber_len_t plen = peer->bv_len - STRLENOF( "IP=" );
1343 if ( addr[0] == '[' ) {
1347 ipport = ch_strdup( &addr[isv6] );
1349 /* Convert IPv6/IPv4 addresses to address;port syntax. */
1350 p = strrchr( ipport, ':' );
1354 assert( p[-1] == ']' );
1355 AC_MEMCPY( &p[-1], p, plen - ( p - ipport ) + 1 );
1358 } else if ( isv6 ) {
1361 assert( addr[plen] == ']' );
1368 int slap_sasl_open( Connection *conn, int reopen )
1370 int sc = LDAP_SUCCESS;
1371 #ifdef HAVE_CYRUS_SASL
1374 sasl_conn_t *ctx = NULL;
1375 sasl_callback_t *session_callbacks;
1376 char *ipremoteport = NULL, *iplocalport = NULL;
1378 assert( conn->c_sasl_authctx == NULL );
1381 assert( conn->c_sasl_extra == NULL );
1384 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1385 if( session_callbacks == NULL ) {
1386 Debug( LDAP_DEBUG_ANY,
1387 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1390 conn->c_sasl_extra = session_callbacks;
1392 session_callbacks[cb=0].id = SASL_CB_LOG;
1393 session_callbacks[cb].proc = (slap_sasl_cb_ft)&slap_sasl_log;
1394 session_callbacks[cb++].context = conn;
1396 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1397 session_callbacks[cb].proc = (slap_sasl_cb_ft)&slap_sasl_authorize;
1398 session_callbacks[cb++].context = conn;
1400 session_callbacks[cb].id = SASL_CB_CANON_USER;
1401 session_callbacks[cb].proc = (slap_sasl_cb_ft)&slap_sasl_canonicalize;
1402 session_callbacks[cb++].context = conn;
1404 session_callbacks[cb].id = SASL_CB_LIST_END;
1405 session_callbacks[cb].proc = NULL;
1406 session_callbacks[cb++].context = NULL;
1408 session_callbacks = conn->c_sasl_extra;
1411 conn->c_sasl_layers = 0;
1413 /* create new SASL context */
1414 if ( conn->c_sock_name.bv_len != 0 &&
1415 strncmp( conn->c_sock_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1417 iplocalport = slap_sasl_peer2ipport( &conn->c_sock_name );
1420 if ( conn->c_peer_name.bv_len != 0 &&
1421 strncmp( conn->c_peer_name.bv_val, "IP=", STRLENOF( "IP=" ) ) == 0 )
1423 ipremoteport = slap_sasl_peer2ipport( &conn->c_peer_name );
1426 sc = sasl_server_new( "ldap", sasl_host, global_realm,
1427 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1428 if ( iplocalport != NULL ) {
1429 ch_free( iplocalport );
1431 if ( ipremoteport != NULL ) {
1432 ch_free( ipremoteport );
1435 if( sc != SASL_OK ) {
1436 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1442 conn->c_sasl_authctx = ctx;
1444 if( sc == SASL_OK ) {
1445 sc = sasl_setprop( ctx,
1446 SASL_SEC_PROPS, &sasl_secprops );
1448 if( sc != SASL_OK ) {
1449 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1452 slap_sasl_close( conn );
1457 sc = slap_sasl_err2ldap( sc );
1459 #elif defined(SLAP_BUILTIN_SASL)
1460 /* built-in SASL implementation */
1461 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1462 if( ctx == NULL ) return -1;
1464 ctx->sc_external_ssf = 0;
1465 BER_BVZERO( &ctx->sc_external_id );
1467 conn->c_sasl_authctx = ctx;
1473 int slap_sasl_external(
1476 struct berval *auth_id )
1478 #ifdef HAVE_CYRUS_SASL
1480 sasl_conn_t *ctx = conn->c_sasl_authctx;
1481 sasl_ssf_t sasl_ssf = ssf;
1483 if ( ctx == NULL ) {
1484 return LDAP_UNAVAILABLE;
1487 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &sasl_ssf );
1489 if ( sc != SASL_OK ) {
1493 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1494 auth_id ? auth_id->bv_val : NULL );
1496 if ( sc != SASL_OK ) {
1499 #elif defined(SLAP_BUILTIN_SASL)
1500 /* built-in SASL implementation */
1501 SASL_CTX *ctx = conn->c_sasl_authctx;
1502 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1504 ctx->sc_external_ssf = ssf;
1506 ctx->sc_external_id = *auth_id;
1507 BER_BVZERO( auth_id );
1509 BER_BVZERO( &ctx->sc_external_id );
1513 return LDAP_SUCCESS;
1516 int slap_sasl_cbinding( Connection *conn, struct berval *cbv )
1518 #ifdef SASL_CHANNEL_BINDING
1519 sasl_channel_binding_t *cb = ch_malloc( sizeof(*cb) + cbv->bv_len );;
1522 cb->data = (char *)(cb+1);
1523 cb->len = cbv->bv_len;
1524 memcpy( (void *)cb->data, cbv->bv_val, cbv->bv_len );
1525 sasl_setprop( conn->c_sasl_authctx, SASL_CHANNEL_BINDING, cb );
1526 conn->c_sasl_cbind = cb;
1528 return LDAP_SUCCESS;
1531 int slap_sasl_reset( Connection *conn )
1533 return LDAP_SUCCESS;
1536 char ** slap_sasl_mechs( Connection *conn )
1538 char **mechs = NULL;
1540 #ifdef HAVE_CYRUS_SASL
1541 sasl_conn_t *ctx = conn->c_sasl_authctx;
1543 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1547 SASL_CONST char *mechstr;
1549 sc = sasl_listmech( ctx,
1550 NULL, NULL, ",", NULL,
1551 &mechstr, NULL, NULL );
1553 if( sc != SASL_OK ) {
1554 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1560 mechs = ldap_str2charray( mechstr, "," );
1562 #elif defined(SLAP_BUILTIN_SASL)
1563 /* builtin SASL implementation */
1564 SASL_CTX *ctx = conn->c_sasl_authctx;
1565 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1566 /* should check ssf */
1567 mechs = ldap_str2charray( "EXTERNAL", "," );
1574 int slap_sasl_close( Connection *conn )
1576 #ifdef HAVE_CYRUS_SASL
1577 sasl_conn_t *ctx = conn->c_sasl_authctx;
1580 sasl_dispose( &ctx );
1582 if ( conn->c_sasl_sockctx &&
1583 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1585 ctx = conn->c_sasl_sockctx;
1586 sasl_dispose( &ctx );
1589 conn->c_sasl_authctx = NULL;
1590 conn->c_sasl_sockctx = NULL;
1591 conn->c_sasl_done = 0;
1593 free( conn->c_sasl_extra );
1594 conn->c_sasl_extra = NULL;
1596 free( conn->c_sasl_cbind );
1597 conn->c_sasl_cbind = NULL;
1599 #elif defined(SLAP_BUILTIN_SASL)
1600 SASL_CTX *ctx = conn->c_sasl_authctx;
1602 if( ctx->sc_external_id.bv_val ) {
1603 free( ctx->sc_external_id.bv_val );
1604 BER_BVZERO( &ctx->sc_external_id );
1607 conn->c_sasl_authctx = NULL;
1611 return LDAP_SUCCESS;
1614 int slap_sasl_bind( Operation *op, SlapReply *rs )
1616 #ifdef HAVE_CYRUS_SASL
1617 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1618 struct berval response;
1619 unsigned reslen = 0;
1622 Debug(LDAP_DEBUG_ARGS,
1623 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1624 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1625 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1626 op->o_conn->c_sasl_bind_mech.bv_val,
1627 op->orb_cred.bv_len );
1630 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1631 "SASL unavailable on this session" );
1635 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1636 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1637 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1638 sasl_server_step( ctx, cred, clen, resp, rlen )
1640 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1641 /* If we already authenticated once, must use a new context */
1642 if ( op->o_conn->c_sasl_done ) {
1644 const char *authid = NULL;
1645 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1646 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1647 if ( authid ) authid = ch_strdup( authid );
1648 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1649 sasl_dispose( &ctx );
1651 op->o_conn->c_sasl_authctx = NULL;
1653 slap_sasl_open( op->o_conn, 1 );
1654 ctx = op->o_conn->c_sasl_authctx;
1656 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1657 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1658 ch_free( (char *)authid );
1662 op->o_conn->c_sasl_bind_mech.bv_val,
1663 op->orb_cred.bv_val, op->orb_cred.bv_len,
1664 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1668 op->orb_cred.bv_val, op->orb_cred.bv_len,
1669 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1672 response.bv_len = reslen;
1674 if ( sc == SASL_OK ) {
1675 sasl_ssf_t *ssf = NULL;
1677 ber_dupbv_x( &op->orb_edn, &op->o_conn->c_sasl_dn, op->o_tmpmemctx );
1678 BER_BVZERO( &op->o_conn->c_sasl_dn );
1679 op->o_conn->c_sasl_done = 1;
1681 rs->sr_err = LDAP_SUCCESS;
1683 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1684 op->orb_ssf = ssf ? *ssf : 0;
1688 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1689 op->o_conn->c_sasl_layers++;
1691 /* If there's an old layer, set sockctx to NULL to
1692 * tell connection_read() to wait for us to finish.
1693 * Otherwise there is a race condition: we have to
1694 * send the Bind response using the old security
1695 * context and then remove it before reading any
1698 if ( op->o_conn->c_sasl_sockctx ) {
1699 ctx = op->o_conn->c_sasl_sockctx;
1700 op->o_conn->c_sasl_sockctx = NULL;
1702 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1704 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1707 /* Must send response using old security layer */
1708 rs->sr_sasldata = (response.bv_len ? &response : NULL);
1709 send_ldap_sasl( op, rs );
1711 /* Now dispose of the old security layer.
1714 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1715 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1716 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1717 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1718 sasl_dispose( &ctx );
1720 } else if ( sc == SASL_CONTINUE ) {
1721 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1722 rs->sr_text = sasl_errdetail( ctx );
1723 rs->sr_sasldata = &response;
1724 send_ldap_sasl( op, rs );
1727 BER_BVZERO( &op->o_conn->c_sasl_dn );
1728 rs->sr_text = sasl_errdetail( ctx );
1729 rs->sr_err = slap_sasl_err2ldap( sc ),
1730 send_ldap_result( op, rs );
1733 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1735 #elif defined(SLAP_BUILTIN_SASL)
1736 /* built-in SASL implementation */
1737 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1739 if ( ctx == NULL ) {
1740 send_ldap_error( op, rs, LDAP_OTHER,
1741 "Internal SASL Error" );
1743 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1746 if( op->orb_cred.bv_len ) {
1747 rs->sr_text = "proxy authorization not supported";
1748 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1749 send_ldap_result( op, rs );
1752 op->orb_edn = ctx->sc_external_id;
1753 rs->sr_err = LDAP_SUCCESS;
1754 rs->sr_sasldata = NULL;
1755 send_ldap_sasl( op, rs );
1759 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1760 "requested SASL mechanism not supported" );
1763 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1764 "SASL not supported" );
1770 char* slap_sasl_secprops( const char *in )
1772 #ifdef HAVE_CYRUS_SASL
1773 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1775 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1777 return "SASL not supported";
1781 void slap_sasl_secprops_unparse( struct berval *bv )
1783 #ifdef HAVE_CYRUS_SASL
1784 ldap_pvt_sasl_secprops_unparse( &sasl_secprops, bv );
1788 #ifdef HAVE_CYRUS_SASL
1790 slap_sasl_setpass( Operation *op, SlapReply *rs )
1792 struct berval id = BER_BVNULL; /* needs to come from connection */
1793 struct berval new = BER_BVNULL;
1794 struct berval old = BER_BVNULL;
1796 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1798 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1799 (SASL_CONST void **)(char *)&id.bv_val );
1801 if( rs->sr_err != SASL_OK ) {
1802 rs->sr_text = "unable to retrieve SASL username";
1803 rs->sr_err = LDAP_OTHER;
1807 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1808 id.bv_val ? id.bv_val : "", 0, 0 );
1810 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1811 NULL, &old, &new, &rs->sr_text );
1813 if( rs->sr_err != LDAP_SUCCESS ) {
1817 if( new.bv_len == 0 ) {
1818 slap_passwd_generate(&new);
1820 if( new.bv_len == 0 ) {
1821 rs->sr_text = "password generation failed.";
1822 rs->sr_err = LDAP_OTHER;
1826 rs->sr_rspdata = slap_passwd_return( &new );
1829 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1830 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1831 if( rs->sr_err != SASL_OK ) {
1832 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1834 switch(rs->sr_err) {
1836 rs->sr_err = LDAP_SUCCESS;
1846 rs->sr_err = LDAP_OTHER;
1852 #endif /* HAVE_CYRUS_SASL */
1854 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1855 * string returned in *dn is in its own allocated memory, and must be free'd
1856 * by the calling process. -Mark Adamson, Carnegie Mellon
1858 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1859 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1866 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1867 char *user_realm, struct berval *dn, int flags )
1869 int rc, is_dn = SET_NONE, do_norm = 1;
1870 struct berval dn2, *mech;
1872 assert( conn != NULL );
1873 assert( id != NULL );
1875 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %lu id=%s [len=%lu]\n",
1877 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1878 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 :
1879 (unsigned long) id->bv_len ) );
1882 op = conn->c_sasl_bindop;
1884 assert( op != NULL );
1888 if ( !BER_BVISNULL( id ) ) {
1889 /* Blatantly anonymous ID */
1890 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1892 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1893 return( LDAP_SUCCESS );
1897 /* FIXME: if empty, should we stop? */
1898 BER_BVSTR( id, "" );
1901 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1902 mech = &conn->c_sasl_bind_mech;
1904 mech = &conn->c_authmech;
1907 /* An authcID needs to be converted to authzID form. Set the
1908 * values directly into *dn; they will be normalized later. (and
1909 * normalizing always makes a new copy.) An ID from a TLS certificate
1910 * is already normalized, so copy it and skip normalization.
1912 if( flags & SLAP_GETDN_AUTHCID ) {
1913 if( bvmatch( mech, &ext_bv )) {
1914 /* EXTERNAL DNs are already normalized */
1915 assert( !BER_BVISNULL( id ) );
1919 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1922 /* convert to u:<username> form */
1928 if( is_dn == SET_NONE ) {
1929 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1931 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1932 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1934 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1936 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1937 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1941 /* No other possibilities from here */
1942 if( is_dn == SET_NONE ) {
1944 return( LDAP_INAPPROPRIATE_AUTH );
1947 /* Username strings */
1948 if( is_dn == SET_U ) {
1949 /* ITS#3419: values may need escape */
1951 LDAPAVA *RDNs[ 4 ][ 2 ];
1956 DN[ irdn ] = RDNs[ irdn ];
1957 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1958 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1959 AVAs[ irdn ].la_value = *dn;
1960 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1961 AVAs[ irdn ].la_private = NULL;
1962 RDNs[ irdn ][ 1 ] = NULL;
1964 if ( user_realm && *user_realm ) {
1966 DN[ irdn ] = RDNs[ irdn ];
1967 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1968 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1969 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1970 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1971 AVAs[ irdn ].la_private = NULL;
1972 RDNs[ irdn ][ 1 ] = NULL;
1975 if ( !BER_BVISNULL( mech ) ) {
1977 DN[ irdn ] = RDNs[ irdn ];
1978 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1979 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1980 AVAs[ irdn ].la_value = *mech;
1981 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1982 AVAs[ irdn ].la_private = NULL;
1983 RDNs[ irdn ][ 1 ] = NULL;
1987 DN[ irdn ] = RDNs[ irdn ];
1988 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1989 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1990 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1991 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1992 AVAs[ irdn ].la_private = NULL;
1993 RDNs[ irdn ][ 1 ] = NULL;
1998 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3,
2000 if ( rc != LDAP_SUCCESS ) {
2005 Debug( LDAP_DEBUG_TRACE,
2006 "slap_sasl_getdn: u:id converted to %s\n",
2011 /* Dup the DN in any case, so we don't risk
2012 * leaks or dangling pointers later,
2013 * and the DN value is '\0' terminated */
2014 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
2015 dn->bv_val = dn2.bv_val;
2018 /* All strings are in DN form now. Normalize if needed. */
2020 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
2022 /* User DNs were constructed above and must be freed now */
2023 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
2025 if ( rc != LDAP_SUCCESS ) {
2032 /* Run thru regexp */
2033 slap_sasl2dn( op, dn, &dn2, flags );
2034 if( !BER_BVISNULL( &dn2 ) ) {
2035 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
2037 Debug( LDAP_DEBUG_TRACE,
2038 "slap_sasl_getdn: dn:id converted to %s\n",
2042 return( LDAP_SUCCESS );