2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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>.
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
29 #ifdef HAVE_CYRUS_SASL
30 # ifdef HAVE_SASL_SASL_H
31 # include <sasl/sasl.h>
36 # if SASL_VERSION_MAJOR >= 2
37 # ifdef HAVE_SASL_SASL_H
38 # include <sasl/saslplug.h>
40 # include <saslplug.h>
42 # define SASL_CONST const
47 #define SASL_VERSION_FULL ((SASL_VERSION_MAJOR << 16) |\
48 (SASL_VERSION_MINOR << 8) | SASL_VERSION_STEP)
50 static sasl_security_properties_t sasl_secprops;
51 #elif defined( SLAP_BUILTIN_SASL )
53 * built-in SASL implementation
54 * only supports EXTERNAL
56 typedef struct sasl_ctx {
57 slap_ssf_t sc_external_ssf;
58 struct berval sc_external_id;
67 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
69 int slap_sasl_config( int cargc, char **cargv, char *line,
70 const char *fname, int lineno )
72 /* set SASL proxy authorization policy */
73 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
76 LDAP_LOG( CONFIG, CRIT,
77 "%s: line %d: missing policy in"
78 " \"sasl-authz-policy <policy>\" line\n",
81 Debug( LDAP_DEBUG_ANY,
82 "%s: line %d: missing policy in"
83 " \"sasl-authz-policy <policy>\" line\n",
89 if ( slap_sasl_setpolicy( cargv[1] ) ) {
91 LDAP_LOG( CONFIG, CRIT,
92 "%s: line %d: unable "
93 "to parse value \"%s\" "
94 "in \"sasl-authz-policy "
96 fname, lineno, cargv[1] );
98 Debug( LDAP_DEBUG_ANY,
99 "%s: line %d: unable "
100 "to parse value \"%s\" "
101 "in \"sasl-authz-policy "
103 fname, lineno, cargv[1] );
108 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
109 || !strcasecmp( cargv[0], "saslregexp" ) )
114 LDAP_LOG( CONFIG, CRIT,
115 "%s: line %d: need 2 args in "
116 "\"saslregexp <match> <replace>\"\n",
119 Debug( LDAP_DEBUG_ANY,
120 "%s: line %d: need 2 args in "
121 "\"saslregexp <match> <replace>\"\n",
127 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
132 #ifdef HAVE_CYRUS_SASL
134 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
137 LDAP_LOG( CONFIG, CRIT,
138 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
141 Debug( LDAP_DEBUG_ANY,
142 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
149 if ( global_host != NULL ) {
151 LDAP_LOG( CONFIG, CRIT,
152 "%s: line %d: already set sasl-host!\n",
155 Debug( LDAP_DEBUG_ANY,
156 "%s: line %d: already set sasl-host!\n",
163 global_host = ch_strdup( cargv[1] );
167 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
170 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
171 "missing realm in \"sasl-realm <realm>\" line.\n",
174 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
175 "missing realm in \"sasl-realm <realm>\" line.\n",
182 if ( global_realm != NULL ) {
184 LDAP_LOG( CONFIG, CRIT,
185 "%s: line %d: already set sasl-realm!\n",
188 Debug( LDAP_DEBUG_ANY,
189 "%s: line %d: already set sasl-realm!\n",
196 global_realm = ch_strdup( cargv[1] );
199 /* SASL security properties */
200 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
205 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
206 "missing flags in \"sasl-secprops <properties>\" line\n",
209 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
210 "missing flags in \"sasl-secprops <properties>\" line\n",
217 txt = slap_sasl_secprops( cargv[1] );
220 LDAP_LOG( CONFIG, CRIT,
221 "%s: line %d sasl-secprops: %s\n",
222 fname, lineno, txt );
224 Debug( LDAP_DEBUG_ANY,
225 "%s: line %d: sasl-secprops: %s\n",
226 fname, lineno, txt );
231 #endif /* HAVE_CYRUS_SASL */
237 #ifdef HAVE_CYRUS_SASL
245 Connection *conn = context;
249 if ( message == NULL ) {
250 return SASL_BADPARAM;
254 #if SASL_VERSION_MAJOR >= 2
256 level = LDAP_DEBUG_NONE;
260 level = LDAP_DEBUG_ANY;
264 level = LDAP_DEBUG_ANY;
268 level = LDAP_DEBUG_TRACE;
272 level = LDAP_DEBUG_TRACE;
276 level = LDAP_DEBUG_TRACE;
280 level = LDAP_DEBUG_TRACE;
284 level = LDAP_DEBUG_TRACE;
285 label = "Password Trace";
289 level = LDAP_DEBUG_ANY;
292 case SASL_LOG_WARNING:
293 level = LDAP_DEBUG_TRACE;
297 level = LDAP_DEBUG_TRACE;
302 return SASL_BADPARAM;
306 LDAP_LOG( TRANSPORT, ENTRY,
307 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
309 Debug( level, "SASL [conn=%ld] %s: %s\n",
310 conn ? conn->c_connid: -1,
319 #if SASL_VERSION_MAJOR >= 2
320 static const char *slap_propnames[] = {
321 "*slapConn", "*slapAuthcDN", "*slapAuthzDN", NULL };
323 static Filter generic_filter = { LDAP_FILTER_PRESENT };
324 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
329 #define PROP_COUNT 3 /* Number of properties we used */
331 typedef struct lookup_info {
333 const struct propval *list;
334 sasl_server_params_t *sparams;
337 static slap_response sasl_ap_lookup;
339 static struct berval sc_cleartext = BER_BVC("{CLEARTEXT}");
342 sasl_ap_lookup( Operation *op, SlapReply *rs )
345 AttributeDescription *ad;
349 slap_callback *tmp = op->o_callback;
350 lookup_info *sl = tmp->sc_private;
352 if (rs->sr_type != REP_SEARCH) return 0;
354 for( i = 0; sl->list[i].name; i++ ) {
355 const char *name = sl->list[i].name;
357 if ( name[0] == '*' ) {
358 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
359 /* Skip our private properties */
360 if ( !strcmp( name, slap_propnames[0] )) {
365 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
368 if ( sl->list[i].values ) {
369 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
372 rc = slap_str2ad( name, &ad, &text );
373 if ( rc != LDAP_SUCCESS ) {
375 LDAP_LOG( TRANSPORT, DETAIL1,
376 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
378 Debug( LDAP_DEBUG_TRACE,
379 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
383 a = attr_find( rs->sr_entry->e_attrs, ad );
385 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
388 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
389 sl->sparams->utils->prop_erase( sl->sparams->propctx,
392 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
393 /* ITS#3846 don't give hashed passwords to SASL */
394 if ( ad == slap_schema.si_ad_userPassword &&
395 bv->bv_val[0] == '{' ) {
396 rc = lutil_passwd_scheme( bv->bv_val );
398 /* If it's not a recognized scheme, just assume it's
399 * a cleartext password that happened to include brackets.
401 * If it's a recognized scheme, skip this value, unless the
402 * scheme is {CLEARTEXT}. In that case, skip over the
403 * scheme name and use the remainder. If there is nothing
404 * past the scheme name, skip this value.
406 #ifdef SLAPD_CLEARTEXT
407 if ( !strncasecmp( bv->bv_val, sc_cleartext.bv_val,
408 sc_cleartext.bv_len )) {
410 cbv.bv_len = bv->bv_len - sc_cleartext.bv_len;
412 cbv.bv_val = bv->bv_val + sc_cleartext.bv_len;
413 sl->sparams->utils->prop_set( sl->sparams->propctx,
414 sl->list[i].name, cbv.bv_val, cbv.bv_len );
421 sl->sparams->utils->prop_set( sl->sparams->propctx,
422 sl->list[i].name, bv->bv_val, bv->bv_len );
431 sasl_server_params_t *sparams,
438 Connection *conn = NULL;
441 sl.list = sparams->utils->prop_get( sparams->propctx );
442 sl.sparams = sparams;
445 /* Find our DN and conn first */
446 for( i = 0; sl.list[i].name; i++ ) {
447 if ( sl.list[i].name[0] == '*' ) {
448 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
449 if ( sl.list[i].values && sl.list[i].values[0] )
450 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
453 if ( (flags & SASL_AUXPROP_AUTHZID) &&
454 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
456 if ( sl.list[i].values && sl.list[i].values[0] )
457 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
460 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
461 if ( sl.list[i].values && sl.list[i].values[0] ) {
462 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
463 if ( !(flags & SASL_AUXPROP_AUTHZID) )
470 /* Now see what else needs to be fetched */
471 for( i = 0; sl.list[i].name; i++ ) {
472 const char *name = sl.list[i].name;
474 if ( name[0] == '*' ) {
475 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
476 /* Skip our private properties */
477 if ( !strcmp( name, slap_propnames[0] )) {
482 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
485 if ( sl.list[i].values ) {
486 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
493 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
497 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
499 if ( op.o_bd && op.o_bd->be_search ) {
500 SlapReply rs = {REP_RESULT};
501 op.o_tag = LDAP_REQ_SEARCH;
502 op.o_protocol = LDAP_VERSION3;
503 op.o_ndn = conn->c_ndn;
505 op.o_time = slap_get_time();
506 op.o_do_not_cache = 1;
507 op.o_is_auth_check = 1;
508 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
509 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
510 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
512 op.o_connid = conn->c_connid;
513 op.o_req_dn = op.o_req_ndn;
514 op.ors_scope = LDAP_SCOPE_BASE;
515 op.ors_deref = LDAP_DEREF_NEVER;
516 op.ors_tlimit = SLAP_NO_LIMIT;
518 op.ors_filter = &generic_filter;
519 op.ors_filterstr = generic_filterstr;
521 op.o_bd->be_search( &op, &rs );
526 #if SASL_VERSION_FULL >= 0x020110
530 sasl_server_params_t *sparams,
531 struct propctx *prctx,
536 SlapReply rs = {REP_RESULT};
538 Connection *conn = NULL;
539 const struct propval *pr;
540 Modifications *modlist = NULL, **modtail = &modlist, *mod;
541 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
542 char textbuf[SLAP_TEXT_BUFLEN];
544 size_t textlen = sizeof(textbuf);
546 /* just checking if we are enabled */
547 if (!prctx) return SASL_OK;
549 if (!sparams || !user) return SASL_BADPARAM;
551 pr = sparams->utils->prop_get( sparams->propctx );
553 /* Find our DN and conn first */
554 for( i = 0; pr[i].name; i++ ) {
555 if ( pr[i].name[0] == '*' ) {
556 if ( !strcmp( pr[i].name, slap_propnames[PROP_CONN] ) ) {
557 if ( pr[i].values && pr[i].values[0] )
558 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
561 if ( !strcmp( pr[i].name, slap_propnames[PROP_AUTHC] ) ) {
562 if ( pr[i].values && pr[i].values[0] ) {
563 AC_MEMCPY( &op.o_req_ndn, pr[i].values[0], sizeof( struct berval ) );
568 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
570 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
572 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
574 pr = sparams->utils->prop_get( prctx );
575 if (!pr) return SASL_BADPARAM;
577 for (i=0; pr[i].name; i++);
578 if (!i) return SASL_BADPARAM;
580 for (i=0; pr[i].name; i++) {
581 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
582 mod->sml_op = LDAP_MOD_REPLACE;
583 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
584 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
585 sizeof(struct berval));
586 for (j=0; j<pr[i].nvalues; j++) {
587 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
589 mod->sml_values[j].bv_val = NULL;
590 mod->sml_values[j].bv_len = 0;
591 mod->sml_nvalues = NULL;
592 mod->sml_desc = NULL;
594 modtail = &mod->sml_next;
598 rc = slap_mods_check( modlist, 0, &text, textbuf, textlen, NULL );
600 if ( rc == LDAP_SUCCESS ) {
601 rc = slap_mods_opattrs( &op, modlist, modtail, &text, textbuf,
605 if ( rc == LDAP_SUCCESS ) {
606 op.o_tag = LDAP_REQ_MODIFY;
607 op.o_protocol = LDAP_VERSION3;
608 op.o_ndn = op.o_req_ndn;
610 op.o_time = slap_get_time();
611 op.o_do_not_cache = 1;
612 op.o_is_auth_check = 1;
613 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
614 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
615 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
617 op.o_connid = conn->c_connid;
618 op.o_req_dn = op.o_req_ndn;
619 op.orm_modlist = modlist;
621 rc = op.o_bd->be_modify( &op, &rs );
623 slap_mods_free( modlist );
624 return rc ? SASL_FAIL : SASL_OK;
626 #endif /* SASL_VERSION_FULL >= 2.1.16 */
628 static sasl_auxprop_plug_t slap_auxprop_plugin = {
631 NULL, /* glob_context */
632 NULL, /* auxprop_free */
635 #if SASL_VERSION_FULL >= 0x020110
636 slap_auxprop_store /* the declaration of this member changed
637 * in cyrus SASL from 2.1.15 to 2.1.16 */
645 const sasl_utils_t *utils,
648 sasl_auxprop_plug_t **plug,
649 const char *plugname)
651 if ( !out_version || !plug ) return SASL_BADPARAM;
653 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
655 *out_version = SASL_AUXPROP_PLUG_VERSION;
656 *plug = &slap_auxprop_plugin;
660 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
661 * auxiliary property, so that we can refer to it in sasl_authorize
662 * without interfering with anything else. Also, the SASL username
663 * buffer is constrained to 256 characters, and our DNs could be
664 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
667 slap_sasl_canonicalize(
673 const char *user_realm,
678 Connection *conn = (Connection *)context;
679 struct propctx *props = sasl_auxprop_getctx( sconn );
680 struct propval auxvals[3];
683 const char *names[2];
688 LDAP_LOG( TRANSPORT, ENTRY,
689 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
690 conn ? conn->c_connid : -1,
691 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
692 in ? in : "<empty>");
694 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
695 conn ? conn->c_connid : -1,
696 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
697 in ? in : "<empty>");
700 /* If name is too big, just truncate. We don't care, we're
701 * using DNs, not the usernames.
703 if ( inlen > out_max )
706 /* This is a Simple Bind using SPASSWD. That means the in-directory
707 * userPassword of the Binding user already points at SASL, so it
708 * cannot be used to actually satisfy a password comparison. Just
709 * ignore it, some other mech will process it.
711 if ( !conn->c_sasl_bindop ||
712 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
714 /* See if we need to add request, can only do it once */
715 prop_getnames( props, slap_propnames, auxvals );
716 if ( !auxvals[0].name )
717 prop_request( props, slap_propnames );
719 if ( flags & SASL_CU_AUTHID )
724 /* Need to store the Connection for auxprop_lookup */
725 if ( !auxvals[PROP_CONN].values ) {
726 names[0] = slap_propnames[PROP_CONN];
728 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
731 /* Already been here? */
732 if ( auxvals[which].values )
735 /* Normally we require an authzID to have a u: or dn: prefix.
736 * However, SASL frequently gives us an authzID that is just
737 * an exact copy of the authcID, without a prefix. We need to
738 * detect and allow this condition. If SASL calls canonicalize
739 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
740 * But if it's broken into two calls, we need to remember the
741 * authcID so that we can compare the authzID later. We store
742 * the authcID temporarily in conn->c_sasl_dn. We necessarily
743 * finish Canonicalizing before Authorizing, so there is no
744 * conflict with slap_sasl_authorize's use of this temp var.
746 * The SASL EXTERNAL mech is backwards from all the other mechs,
747 * it does authzID before the authcID. If we see that authzID
748 * has already been done, don't do anything special with authcID.
750 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
751 conn->c_sasl_dn.bv_val = (char *) in;
752 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
753 rc = strcmp( in, conn->c_sasl_dn.bv_val );
754 conn->c_sasl_dn.bv_val = NULL;
755 /* They were equal, no work needed */
756 if ( !rc ) goto done;
759 rc = slap_sasl_getdn( conn, NULL, (char *)in, inlen, (char *)user_realm, &dn,
760 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
761 if ( rc != LDAP_SUCCESS ) {
762 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
766 names[0] = slap_propnames[which];
769 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
772 LDAP_LOG( TRANSPORT, ENTRY,
773 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
774 conn ? conn->c_connid : -1, names[0]+1,
775 dn.bv_val ? dn.bv_val : "<EMPTY>" );
777 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
778 conn ? conn->c_connid : -1, names[0]+1,
779 dn.bv_val ? dn.bv_val : "<EMPTY>" );
783 AC_MEMCPY( out, in, inlen );
795 char *requested_user,
799 const char *def_realm,
801 struct propctx *props)
803 Connection *conn = (Connection *)context;
804 struct propval auxvals[3];
805 struct berval authcDN, authzDN;
808 /* Simple Binds don't support proxy authorization, ignore it */
809 if ( !conn->c_sasl_bindop ||
810 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
813 LDAP_LOG( TRANSPORT, ENTRY,
814 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
815 conn ? conn->c_connid : -1, auth_identity, requested_user);
817 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
818 "authcid=\"%s\" authzid=\"%s\"\n",
819 conn ? conn->c_connid : -1, auth_identity, requested_user );
821 if ( conn->c_sasl_dn.bv_val ) {
822 ch_free( conn->c_sasl_dn.bv_val );
823 conn->c_sasl_dn.bv_val = NULL;
824 conn->c_sasl_dn.bv_len = 0;
828 prop_getnames( props, slap_propnames+1, auxvals );
830 /* Should not happen */
831 if ( !auxvals[0].values ) {
832 sasl_seterror( sconn, 0, "invalid authcid" );
836 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
838 /* Nothing to do if no authzID was given */
839 if ( !auxvals[1].name || !auxvals[1].values ) {
840 conn->c_sasl_dn = authcDN;
844 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
846 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
847 ch_free( authcDN.bv_val );
848 if ( rc != LDAP_SUCCESS ) {
850 LDAP_LOG( TRANSPORT, INFO,
851 "slap_sasl_authorize: conn %ld "
852 "proxy authorization disallowed (%d)\n",
853 (long)(conn ? conn->c_connid : -1), rc, 0 );
855 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
856 "proxy authorization disallowed (%d)\n",
857 (long) (conn ? conn->c_connid : -1), rc, 0 );
860 sasl_seterror( sconn, 0, "not authorized" );
861 ch_free( authzDN.bv_val );
865 conn->c_sasl_dn = authzDN;
867 if (conn->c_sasl_bindop) {
868 Statslog( LDAP_DEBUG_STATS,
869 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
870 conn->c_connid, conn->c_sasl_bindop->o_opid,
871 auth_identity, 0, 0);
875 LDAP_LOG( TRANSPORT, ENTRY,
876 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
877 (long)(conn ? conn->c_connid : -1), 0, 0 );
879 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
880 " proxy authorization allowed\n",
881 (long) (conn ? conn->c_connid : -1), 0, 0 );
894 struct berval authcDN, authzDN;
896 Connection *conn = context;
900 if ( conn->c_sasl_dn.bv_val ) {
901 ch_free( conn->c_sasl_dn.bv_val );
902 conn->c_sasl_dn.bv_val = NULL;
903 conn->c_sasl_dn.bv_len = 0;
907 LDAP_LOG( TRANSPORT, ENTRY,
908 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
909 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
910 authzid ? authzid : "<empty>" );
912 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
913 "authcid=\"%s\" authzid=\"%s\"\n",
914 (long) (conn ? conn->c_connid : -1),
915 authcid ? authcid : "<empty>",
916 authzid ? authzid : "<empty>" );
919 /* Figure out how much data we have for the dn */
920 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
921 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
923 LDAP_LOG( TRANSPORT, ERR,
924 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
926 Debug(LDAP_DEBUG_TRACE,
927 "authorize: getprop(REALM) failed!\n", 0,0,0);
929 *errstr = "Could not extract realm";
933 /* Convert the identities to DN's. If no authzid was given, client will
934 be bound as the DN matching their username */
935 rc = slap_sasl_getdn( conn, NULL, (char *)authcid, 0, realm,
936 &authcDN, SLAP_GETDN_AUTHCID );
937 if( rc != LDAP_SUCCESS ) {
938 *errstr = ldap_err2string( rc );
941 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
943 LDAP_LOG( TRANSPORT, ENTRY,
944 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
945 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
947 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
948 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
951 conn->c_sasl_dn = authcDN;
954 rc = slap_sasl_getdn( conn, NULL, (char *)authzid, 0, realm,
955 &authzDN, SLAP_GETDN_AUTHZID );
956 if( rc != LDAP_SUCCESS ) {
957 ch_free( authcDN.bv_val );
958 *errstr = ldap_err2string( rc );
962 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
963 ch_free( authcDN.bv_val );
966 LDAP_LOG( TRANSPORT, INFO,
967 "slap_sasl_authorize: conn %ld "
968 "proxy authorization disallowed (%d)\n",
969 (long)(conn ? conn->c_connid : -1), rc, 0 );
971 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
972 "proxy authorization disallowed (%d)\n",
973 (long) (conn ? conn->c_connid : -1), rc, 0 );
976 *errstr = "not authorized";
977 ch_free( authzDN.bv_val );
980 conn->c_sasl_dn = authzDN;
984 LDAP_LOG( TRANSPORT, RESULTS,
985 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
986 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
988 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
989 " authorization allowed\n",
990 (long) (conn ? conn->c_connid : -1), 0, 0 );
993 if (conn->c_sasl_bindop) {
994 Statslog( LDAP_DEBUG_STATS,
995 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
996 conn->c_connid, conn->c_sasl_bindop->o_opid,
1003 #endif /* SASL_VERSION_MAJOR >= 2 */
1006 slap_sasl_err2ldap( int saslerr )
1015 rc = LDAP_SASL_BIND_IN_PROGRESS;
1024 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1027 rc = LDAP_INVALID_CREDENTIALS;
1030 rc = LDAP_INSUFFICIENT_ACCESS;
1034 rc = LDAP_INAPPROPRIATE_AUTH;
1045 int slap_sasl_init( void )
1047 #ifdef HAVE_CYRUS_SASL
1049 static sasl_callback_t server_callbacks[] = {
1050 { SASL_CB_LOG, &slap_sasl_log, NULL },
1051 { SASL_CB_LIST_END, NULL, NULL }
1054 #ifdef HAVE_SASL_VERSION
1055 /* stringify the version number, sasl.h doesn't do it for us */
1056 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1057 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1058 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1061 sasl_version( NULL, &rc );
1062 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1063 (rc & 0xffff) < SASL_VERSION_STEP)
1065 char version[sizeof("xxx.xxx.xxxxx")];
1066 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1069 LDAP_LOG( TRANSPORT, INFO,
1070 "slap_sasl_init: SASL library version mismatch:"
1071 " expected " SASL_VERSION_STRING ","
1072 " got %s\n", version, 0, 0 );
1074 Debug( LDAP_DEBUG_ANY,
1075 "slap_sasl_init: SASL library version mismatch:"
1076 " expected " SASL_VERSION_STRING ","
1077 " got %s\n", version, 0, 0 );
1083 /* SASL 2 does its own memory management internally */
1084 #if SASL_VERSION_MAJOR < 2
1093 ldap_pvt_sasl_mutex_new,
1094 ldap_pvt_sasl_mutex_lock,
1095 ldap_pvt_sasl_mutex_unlock,
1096 ldap_pvt_sasl_mutex_dispose );
1098 #if SASL_VERSION_MAJOR >= 2
1099 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1101 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1103 /* should provide callbacks for logging */
1104 /* server name should be configurable */
1105 rc = sasl_server_init( server_callbacks, "slapd" );
1107 if( rc != SASL_OK ) {
1109 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1111 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1114 #if SASL_VERSION_MAJOR < 2
1115 /* A no-op used to make sure we linked with Cyrus 1.5 */
1116 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1123 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1125 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1129 /* default security properties */
1130 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1131 sasl_secprops.max_ssf = INT_MAX;
1132 sasl_secprops.maxbufsize = 65536;
1133 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1139 int slap_sasl_destroy( void )
1141 #ifdef HAVE_CYRUS_SASL
1144 free( global_host );
1150 int slap_sasl_open( Connection *conn, int reopen )
1152 int sc = LDAP_SUCCESS;
1153 #ifdef HAVE_CYRUS_SASL
1156 sasl_conn_t *ctx = NULL;
1157 sasl_callback_t *session_callbacks;
1159 #if SASL_VERSION_MAJOR >= 2
1160 char *ipremoteport = NULL, *iplocalport = NULL;
1163 assert( conn->c_sasl_authctx == NULL );
1166 assert( conn->c_sasl_extra == NULL );
1169 #if SASL_VERSION_MAJOR >= 2
1170 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1172 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1174 if( session_callbacks == NULL ) {
1176 LDAP_LOG( TRANSPORT, ERR,
1177 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1179 Debug( LDAP_DEBUG_ANY,
1180 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1184 conn->c_sasl_extra = session_callbacks;
1186 session_callbacks[cb=0].id = SASL_CB_LOG;
1187 session_callbacks[cb].proc = &slap_sasl_log;
1188 session_callbacks[cb++].context = conn;
1190 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1191 session_callbacks[cb].proc = &slap_sasl_authorize;
1192 session_callbacks[cb++].context = conn;
1194 #if SASL_VERSION_MAJOR >= 2
1195 session_callbacks[cb].id = SASL_CB_CANON_USER;
1196 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1197 session_callbacks[cb++].context = conn;
1200 session_callbacks[cb].id = SASL_CB_LIST_END;
1201 session_callbacks[cb].proc = NULL;
1202 session_callbacks[cb++].context = NULL;
1204 session_callbacks = conn->c_sasl_extra;
1207 conn->c_sasl_layers = 0;
1209 if( global_host == NULL ) {
1210 global_host = ldap_pvt_get_fqdn( NULL );
1213 /* create new SASL context */
1214 #if SASL_VERSION_MAJOR >= 2
1215 if ( conn->c_sock_name.bv_len != 0 &&
1216 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1219 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1220 /* Convert IPv6 addresses to address;port syntax. */
1221 p = strrchr( iplocalport, ' ' );
1222 /* Convert IPv4 addresses to address;port syntax. */
1223 if ( p == NULL ) p = strchr( iplocalport, ':' );
1228 if ( conn->c_peer_name.bv_len != 0 &&
1229 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1232 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1233 /* Convert IPv6 addresses to address;port syntax. */
1234 p = strrchr( ipremoteport, ' ' );
1235 /* Convert IPv4 addresses to address;port syntax. */
1236 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1241 sc = sasl_server_new( "ldap", global_host, global_realm,
1242 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1243 if ( iplocalport != NULL ) {
1244 ch_free( iplocalport );
1246 if ( ipremoteport != NULL ) {
1247 ch_free( ipremoteport );
1250 sc = sasl_server_new( "ldap", global_host, global_realm,
1251 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1254 if( sc != SASL_OK ) {
1256 LDAP_LOG( TRANSPORT, ERR,
1257 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1259 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1266 conn->c_sasl_authctx = ctx;
1268 if( sc == SASL_OK ) {
1269 sc = sasl_setprop( ctx,
1270 SASL_SEC_PROPS, &sasl_secprops );
1272 if( sc != SASL_OK ) {
1274 LDAP_LOG( TRANSPORT, ERR,
1275 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1277 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1281 slap_sasl_close( conn );
1286 sc = slap_sasl_err2ldap( sc );
1288 #elif defined(SLAP_BUILTIN_SASL)
1289 /* built-in SASL implementation */
1290 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1291 if( ctx == NULL ) return -1;
1293 ctx->sc_external_ssf = 0;
1294 ctx->sc_external_id.bv_len = 0;
1295 ctx->sc_external_id.bv_val = NULL;
1297 conn->c_sasl_authctx = ctx;
1303 int slap_sasl_external(
1306 struct berval *auth_id )
1308 #if SASL_VERSION_MAJOR >= 2
1310 sasl_conn_t *ctx = conn->c_sasl_authctx;
1312 if ( ctx == NULL ) {
1313 return LDAP_UNAVAILABLE;
1316 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1318 if ( sc != SASL_OK ) {
1322 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1323 auth_id ? auth_id->bv_val : NULL );
1325 if ( sc != SASL_OK ) {
1329 #elif defined(HAVE_CYRUS_SASL)
1331 sasl_conn_t *ctx = conn->c_sasl_authctx;
1332 sasl_external_properties_t extprops;
1334 if ( ctx == NULL ) {
1335 return LDAP_UNAVAILABLE;
1338 memset( &extprops, '\0', sizeof(extprops) );
1340 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1342 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1343 (void *) &extprops );
1345 if ( sc != SASL_OK ) {
1348 #elif defined(SLAP_BUILTIN_SASL)
1349 /* built-in SASL implementation */
1350 SASL_CTX *ctx = conn->c_sasl_authctx;
1351 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1353 ctx->sc_external_ssf = ssf;
1355 ctx->sc_external_id = *auth_id;
1356 auth_id->bv_len = 0;
1357 auth_id->bv_val = NULL;
1359 ctx->sc_external_id.bv_len = 0;
1360 ctx->sc_external_id.bv_val = NULL;
1364 return LDAP_SUCCESS;
1367 int slap_sasl_reset( Connection *conn )
1369 return LDAP_SUCCESS;
1372 char ** slap_sasl_mechs( Connection *conn )
1374 char **mechs = NULL;
1376 #ifdef HAVE_CYRUS_SASL
1377 sasl_conn_t *ctx = conn->c_sasl_authctx;
1379 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1383 SASL_CONST char *mechstr;
1385 sc = sasl_listmech( ctx,
1386 NULL, NULL, ",", NULL,
1387 &mechstr, NULL, NULL );
1389 if( sc != SASL_OK ) {
1391 LDAP_LOG( TRANSPORT, ERR,
1392 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1394 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1401 mechs = ldap_str2charray( mechstr, "," );
1403 #if SASL_VERSION_MAJOR < 2
1407 #elif defined(SLAP_BUILTIN_SASL)
1408 /* builtin SASL implementation */
1409 SASL_CTX *ctx = conn->c_sasl_authctx;
1410 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1411 /* should check ssf */
1412 mechs = ldap_str2charray( "EXTERNAL", "," );
1419 int slap_sasl_close( Connection *conn )
1421 #ifdef HAVE_CYRUS_SASL
1422 sasl_conn_t *ctx = conn->c_sasl_authctx;
1425 sasl_dispose( &ctx );
1427 if ( conn->c_sasl_sockctx &&
1428 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1430 ctx = conn->c_sasl_sockctx;
1431 sasl_dispose( &ctx );
1434 conn->c_sasl_authctx = NULL;
1435 conn->c_sasl_sockctx = NULL;
1436 conn->c_sasl_done = 0;
1438 free( conn->c_sasl_extra );
1439 conn->c_sasl_extra = NULL;
1441 #elif defined(SLAP_BUILTIN_SASL)
1442 SASL_CTX *ctx = conn->c_sasl_authctx;
1444 if( ctx->sc_external_id.bv_val ) {
1445 free( ctx->sc_external_id.bv_val );
1446 ctx->sc_external_id.bv_val = NULL;
1449 conn->c_sasl_authctx = NULL;
1453 return LDAP_SUCCESS;
1456 int slap_sasl_bind( Operation *op, SlapReply *rs )
1458 #ifdef HAVE_CYRUS_SASL
1459 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1460 struct berval response;
1461 unsigned reslen = 0;
1465 LDAP_LOG( TRANSPORT, ENTRY,
1466 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1467 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1468 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1469 op->o_conn->c_sasl_bind_mech.bv_val,
1470 op->orb_cred.bv_len );
1472 Debug(LDAP_DEBUG_ARGS,
1473 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1474 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1475 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1476 op->o_conn->c_sasl_bind_mech.bv_val,
1477 op->orb_cred.bv_len );
1481 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1482 "SASL unavailable on this session" );
1486 #if SASL_VERSION_MAJOR >= 2
1487 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1488 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1489 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1490 sasl_server_step( ctx, cred, clen, resp, rlen )
1492 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1493 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1494 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1495 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1498 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1499 /* If we already authenticated once, must use a new context */
1500 if ( op->o_conn->c_sasl_done ) {
1502 const char *authid = NULL;
1503 #if SASL_VERSION_MAJOR >= 2
1504 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1505 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1506 if ( authid ) authid = ch_strdup( authid );
1508 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1509 sasl_dispose( &ctx );
1511 op->o_conn->c_sasl_authctx = NULL;
1513 slap_sasl_open( op->o_conn, 1 );
1514 ctx = op->o_conn->c_sasl_authctx;
1515 #if SASL_VERSION_MAJOR >= 2
1517 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1518 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1519 ch_free( (char *)authid );
1524 op->o_conn->c_sasl_bind_mech.bv_val,
1525 op->orb_cred.bv_val, op->orb_cred.bv_len,
1526 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1530 op->orb_cred.bv_val, op->orb_cred.bv_len,
1531 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1534 response.bv_len = reslen;
1536 if ( sc == SASL_OK ) {
1537 sasl_ssf_t *ssf = NULL;
1539 op->orb_edn = op->o_conn->c_sasl_dn;
1540 op->o_conn->c_sasl_dn.bv_val = NULL;
1541 op->o_conn->c_sasl_dn.bv_len = 0;
1542 op->o_conn->c_sasl_done = 1;
1544 rs->sr_err = LDAP_SUCCESS;
1546 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1547 op->orb_ssf = ssf ? *ssf : 0;
1551 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1552 op->o_conn->c_sasl_layers++;
1554 /* If there's an old layer, set sockctx to NULL to
1555 * tell connection_read() to wait for us to finish.
1556 * Otherwise there is a race condition: we have to
1557 * send the Bind response using the old security
1558 * context and then remove it before reading any
1561 if ( op->o_conn->c_sasl_sockctx ) {
1562 ctx = op->o_conn->c_sasl_sockctx;
1563 op->o_conn->c_sasl_sockctx = NULL;
1565 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1567 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1570 /* Must send response using old security layer */
1571 if (response.bv_len) rs->sr_sasldata = &response;
1572 send_ldap_sasl( op, rs );
1574 /* Now dispose of the old security layer.
1577 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1578 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1579 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1580 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1581 sasl_dispose( &ctx );
1583 } else if ( sc == SASL_CONTINUE ) {
1584 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1585 rs->sr_sasldata = &response;
1586 send_ldap_sasl( op, rs );
1589 #if SASL_VERSION_MAJOR >= 2
1590 rs->sr_text = sasl_errdetail( ctx );
1592 rs->sr_err = slap_sasl_err2ldap( sc ),
1593 send_ldap_result( op, rs );
1596 #if SASL_VERSION_MAJOR < 2
1597 if( response.bv_len ) {
1598 ch_free( response.bv_val );
1603 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0 );
1605 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1608 #elif defined(SLAP_BUILTIN_SASL)
1609 /* built-in SASL implementation */
1610 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1612 if ( ctx == NULL ) {
1613 send_ldap_error( op, rs, LDAP_OTHER,
1614 "Internal SASL Error" );
1616 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1619 if( op->orb_cred.bv_len ) {
1620 rs->sr_text = "proxy authorization not support";
1621 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1622 send_ldap_result( op, rs );
1625 op->orb_edn = ctx->sc_external_id;
1626 rs->sr_err = LDAP_SUCCESS;
1627 rs->sr_sasldata = NULL;
1628 send_ldap_sasl( op, rs );
1632 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1633 "requested SASL mechanism not supported" );
1636 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1637 "SASL not supported" );
1643 char* slap_sasl_secprops( const char *in )
1645 #ifdef HAVE_CYRUS_SASL
1646 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1648 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1650 return "SASL not supported";
1654 #ifdef HAVE_CYRUS_SASL
1656 slap_sasl_setpass( Operation *op, SlapReply *rs )
1658 struct berval id = BER_BVNULL; /* needs to come from connection */
1659 struct berval new = BER_BVNULL;
1660 struct berval old = BER_BVNULL;
1662 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1664 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1665 (SASL_CONST void **)&id.bv_val );
1667 if( rs->sr_err != SASL_OK ) {
1668 rs->sr_text = "unable to retrieve SASL username";
1669 rs->sr_err = LDAP_OTHER;
1674 LDAP_LOG( BACKEND, ENTRY,
1675 "slap_sasl_setpass: \"%s\"\n",
1676 id.bv_val ? id.bv_val : "", 0, 0);
1678 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1679 id.bv_val ? id.bv_val : "", 0, 0 );
1682 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1683 NULL, &old, &new, &rs->sr_text );
1685 if( rs->sr_err != LDAP_SUCCESS ) {
1689 if( new.bv_len == 0 ) {
1690 slap_passwd_generate(&new);
1692 if( new.bv_len == 0 ) {
1693 rs->sr_text = "password generation failed.";
1694 rs->sr_err = LDAP_OTHER;
1698 rs->sr_rspdata = slap_passwd_return( &new );
1701 #if SASL_VERSION_MAJOR < 2
1702 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1703 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1705 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1706 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1707 if( rs->sr_err != SASL_OK ) {
1708 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1711 switch(rs->sr_err) {
1713 rs->sr_err = LDAP_SUCCESS;
1723 rs->sr_err = LDAP_OTHER;
1729 #endif /* HAVE_CYRUS_SASL */
1731 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1732 * string returned in *dn is in its own allocated memory, and must be free'd
1733 * by the calling process. -Mark Adamson, Carnegie Mellon
1735 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1736 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1743 int slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len,
1744 char *user_realm, struct berval *dn, int flags )
1746 int rc, is_dn = SET_NONE, do_norm = 1;
1747 struct berval dn2, *mech;
1752 LDAP_LOG( TRANSPORT, ENTRY,
1753 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1754 conn->c_connid, id ? (*id ? id : "<empty>") : "NULL", len );
1756 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s [len=%d]\n",
1757 id ? ( *id ? id : "<empty>" ) : "NULL", len, 0 );
1761 op = conn->c_sasl_bindop;
1768 if ( len == 0 ) len = strlen( id );
1770 /* Blatantly anonymous ID */
1771 if ( len == sizeof("anonymous") - 1 &&
1772 !strcasecmp( id, "anonymous" ) ) {
1773 return( LDAP_SUCCESS );
1779 if ( conn->c_sasl_bind_mech.bv_len ) {
1780 mech = &conn->c_sasl_bind_mech;
1782 mech = &conn->c_authmech;
1785 /* An authcID needs to be converted to authzID form. Set the
1786 * values directly into *dn; they will be normalized later. (and
1787 * normalizing always makes a new copy.) An ID from a TLS certificate
1788 * is already normalized, so copy it and skip normalization.
1790 if( flags & SLAP_GETDN_AUTHCID ) {
1791 if( bvmatch( mech, &ext_bv )) {
1792 /* EXTERNAL DNs are already normalized */
1795 ber_str2bv_x( id, len, 1, dn, op->o_tmpmemctx );
1798 /* convert to u:<username> form */
1804 if( is_dn == SET_NONE ) {
1805 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1809 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1816 /* No other possibilities from here */
1817 if( is_dn == SET_NONE ) {
1820 return( LDAP_INAPPROPRIATE_AUTH );
1823 /* Username strings */
1824 if( is_dn == SET_U ) {
1825 /* ITS#3419: values may need escape */
1827 LDAPAVA *RDNs[ 4 ][ 2 ];
1832 DN[ irdn ] = RDNs[ irdn ];
1833 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1834 BER_BVSTR( &AVAs[ irdn ].la_attr, "uid" );
1835 AVAs[ irdn ].la_value = *dn;
1836 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1837 AVAs[ irdn ].la_private = NULL;
1838 RDNs[ irdn ][ 1 ] = NULL;
1840 if ( user_realm && *user_realm ) {
1842 DN[ irdn ] = RDNs[ irdn ];
1843 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1844 BER_BVSTR( &AVAs[ irdn ].la_attr, "cn" );
1845 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1846 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1847 AVAs[ irdn ].la_private = NULL;
1848 RDNs[ irdn ][ 1 ] = NULL;
1851 if ( !BER_BVISNULL( mech ) ) {
1853 DN[ irdn ] = RDNs[ irdn ];
1854 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1855 BER_BVSTR( &AVAs[ irdn ].la_attr, "cn" );
1856 AVAs[ irdn ].la_value = *mech;
1857 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1858 AVAs[ irdn ].la_private = NULL;
1859 RDNs[ irdn ][ 1 ] = NULL;
1863 DN[ irdn ] = RDNs[ irdn ];
1864 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1865 BER_BVSTR( &AVAs[ irdn ].la_attr, "cn" );
1866 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1867 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1868 AVAs[ irdn ].la_private = NULL;
1869 RDNs[ irdn ][ 1 ] = NULL;
1874 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3, op->o_tmpmemctx );
1875 if ( rc != LDAP_SUCCESS ) {
1881 LDAP_LOG( TRANSPORT, ENTRY,
1882 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1884 Debug( LDAP_DEBUG_TRACE, "slap_sasl_getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1888 /* Dup the DN in any case, so we don't risk
1889 * leaks or dangling pointers later,
1890 * and the DN value is '\0' terminated */
1891 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1892 dn->bv_val = dn2.bv_val;
1895 /* All strings are in DN form now. Normalize if needed. */
1897 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1899 /* User DNs were constructed above and must be freed now */
1900 sl_free( dn->bv_val, op->o_tmpmemctx );
1902 if ( rc != LDAP_SUCCESS ) {
1910 /* Run thru regexp */
1911 slap_sasl2dn( op, dn, &dn2, flags );
1913 sl_free( dn->bv_val, op->o_tmpmemctx );
1916 LDAP_LOG( TRANSPORT, ENTRY,
1917 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1919 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1924 return( LDAP_SUCCESS );