2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 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], "authz-policy" ) ||
74 !strcasecmp( cargv[0], "sasl-authz-policy" ))
78 LDAP_LOG( CONFIG, CRIT,
79 "%s: line %d: missing policy in"
80 " \"%s <policy>\" line\n",
81 cargv[0], fname, lineno );
83 Debug( LDAP_DEBUG_ANY,
84 "%s: line %d: missing policy in"
85 " \"%s <policy>\" line\n",
86 cargv[0], fname, lineno );
91 if ( slap_sasl_setpolicy( cargv[1] ) ) {
93 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
94 "unable to parse value \"%s\" in \"authz-policy "
96 fname, lineno, cargv[1] );
98 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
99 "unable to parse value \"%s\" in \"authz-policy "
100 "<policy>\" line.\n",
101 fname, lineno, cargv[1] );
106 } else if ( !strcasecmp( cargv[0], "authz-regexp" ) ||
107 !strcasecmp( cargv[0], "sasl-regexp" ) ||
108 !strcasecmp( cargv[0], "saslregexp" ) )
113 LDAP_LOG( CONFIG, CRIT,
114 "%s: line %d: need 2 args in "
115 "\"authz-regexp <match> <replace>\"\n",
118 Debug( LDAP_DEBUG_ANY,
119 "%s: line %d: need 2 args in "
120 "\"authz-regexp <match> <replace>\"\n",
126 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
131 #ifdef HAVE_CYRUS_SASL
133 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
136 LDAP_LOG( CONFIG, CRIT,
137 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
140 Debug( LDAP_DEBUG_ANY,
141 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
148 if ( global_host != NULL ) {
150 LDAP_LOG( CONFIG, CRIT,
151 "%s: line %d: already set sasl-host!\n",
154 Debug( LDAP_DEBUG_ANY,
155 "%s: line %d: already set sasl-host!\n",
162 global_host = ch_strdup( cargv[1] );
166 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
169 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
170 "missing realm in \"sasl-realm <realm>\" line.\n",
173 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
174 "missing realm in \"sasl-realm <realm>\" line.\n",
181 if ( global_realm != NULL ) {
183 LDAP_LOG( CONFIG, CRIT,
184 "%s: line %d: already set sasl-realm!\n",
187 Debug( LDAP_DEBUG_ANY,
188 "%s: line %d: already set sasl-realm!\n",
195 global_realm = ch_strdup( cargv[1] );
198 /* SASL security properties */
199 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
204 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
205 "missing flags in \"sasl-secprops <properties>\" line\n",
208 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
209 "missing flags in \"sasl-secprops <properties>\" line\n",
216 txt = slap_sasl_secprops( cargv[1] );
219 LDAP_LOG( CONFIG, CRIT,
220 "%s: line %d sasl-secprops: %s\n",
221 fname, lineno, txt );
223 Debug( LDAP_DEBUG_ANY,
224 "%s: line %d: sasl-secprops: %s\n",
225 fname, lineno, txt );
230 #endif /* HAVE_CYRUS_SASL */
236 #ifdef HAVE_CYRUS_SASL
244 Connection *conn = context;
248 if ( message == NULL ) {
249 return SASL_BADPARAM;
253 #if SASL_VERSION_MAJOR >= 2
255 level = LDAP_DEBUG_NONE;
259 level = LDAP_DEBUG_ANY;
263 level = LDAP_DEBUG_ANY;
267 level = LDAP_DEBUG_TRACE;
271 level = LDAP_DEBUG_TRACE;
275 level = LDAP_DEBUG_TRACE;
279 level = LDAP_DEBUG_TRACE;
283 level = LDAP_DEBUG_TRACE;
284 label = "Password Trace";
288 level = LDAP_DEBUG_ANY;
291 case SASL_LOG_WARNING:
292 level = LDAP_DEBUG_TRACE;
296 level = LDAP_DEBUG_TRACE;
301 return SASL_BADPARAM;
305 LDAP_LOG( TRANSPORT, ENTRY,
306 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
308 Debug( level, "SASL [conn=%ld] %s: %s\n",
309 conn ? conn->c_connid: -1,
318 #if SASL_VERSION_MAJOR >= 2
319 static const char *slap_propnames[] = {
320 "*slapConn", "*slapAuthcDN", "*slapAuthzDN", NULL };
322 static Filter generic_filter = { LDAP_FILTER_PRESENT };
323 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
328 #define PROP_COUNT 3 /* Number of properties we used */
330 typedef struct lookup_info {
332 const struct propval *list;
333 sasl_server_params_t *sparams;
336 static slap_response sasl_ap_lookup;
339 sasl_ap_lookup( Operation *op, SlapReply *rs )
342 AttributeDescription *ad;
346 slap_callback *tmp = op->o_callback;
347 lookup_info *sl = tmp->sc_private;
349 if (rs->sr_type != REP_SEARCH) return 0;
351 for( i = 0; sl->list[i].name; i++ ) {
352 const char *name = sl->list[i].name;
354 if ( name[0] == '*' ) {
355 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
356 /* Skip our private properties */
357 if ( !strcmp( name, slap_propnames[0] )) {
362 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
365 if ( sl->list[i].values ) {
366 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
369 rc = slap_str2ad( name, &ad, &text );
370 if ( rc != LDAP_SUCCESS ) {
372 LDAP_LOG( TRANSPORT, DETAIL1,
373 "slap_ap_lookup: str2ad(%s): %s\n", name, text, 0 );
375 Debug( LDAP_DEBUG_TRACE,
376 "slap_ap_lookup: str2ad(%s): %s\n", name, text, 0 );
380 a = attr_find( rs->sr_entry->e_attrs, ad );
382 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
385 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
386 sl->sparams->utils->prop_erase( sl->sparams->propctx,
389 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
390 sl->sparams->utils->prop_set( sl->sparams->propctx,
391 sl->list[i].name, bv->bv_val, bv->bv_len );
400 sasl_server_params_t *sparams,
407 Connection *conn = NULL;
410 sl.list = sparams->utils->prop_get( sparams->propctx );
411 sl.sparams = sparams;
414 /* Find our DN and conn first */
415 for( i = 0; sl.list[i].name; i++ ) {
416 if ( sl.list[i].name[0] == '*' ) {
417 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
418 if ( sl.list[i].values && sl.list[i].values[0] )
419 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
422 if ( (flags & SASL_AUXPROP_AUTHZID) &&
423 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
425 if ( sl.list[i].values && sl.list[i].values[0] )
426 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
429 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
430 if ( sl.list[i].values && sl.list[i].values[0] ) {
431 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
432 if ( !(flags & SASL_AUXPROP_AUTHZID) )
439 /* Now see what else needs to be fetched */
440 for( i = 0; sl.list[i].name; i++ ) {
441 const char *name = sl.list[i].name;
443 if ( name[0] == '*' ) {
444 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
445 /* Skip our private properties */
446 if ( !strcmp( name, slap_propnames[0] )) {
451 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
454 if ( sl.list[i].values ) {
455 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
462 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
466 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
468 if ( op.o_bd && op.o_bd->be_search ) {
469 SlapReply rs = {REP_RESULT};
470 op.o_tag = LDAP_REQ_SEARCH;
471 op.o_protocol = LDAP_VERSION3;
472 op.o_ndn = conn->c_ndn;
474 op.o_time = slap_get_time();
475 op.o_do_not_cache = 1;
476 op.o_is_auth_check = 1;
477 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
478 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
479 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
481 op.o_connid = conn->c_connid;
482 op.o_req_dn = op.o_req_ndn;
483 op.ors_scope = LDAP_SCOPE_BASE;
484 op.ors_deref = LDAP_DEREF_NEVER;
485 op.ors_tlimit = SLAP_NO_LIMIT;
487 op.ors_filter = &generic_filter;
488 op.ors_filterstr = generic_filterstr;
489 /* FIXME: we want all attributes, right? */
492 op.o_bd->be_search( &op, &rs );
497 #if SASL_VERSION_FULL >= 0x020110
501 sasl_server_params_t *sparams,
502 struct propctx *prctx,
507 SlapReply rs = {REP_RESULT};
509 Connection *conn = NULL;
510 const struct propval *pr;
511 Modifications *modlist = NULL, **modtail = &modlist, *mod;
512 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
513 char textbuf[SLAP_TEXT_BUFLEN];
515 size_t textlen = sizeof(textbuf);
517 /* just checking if we are enabled */
518 if (!prctx) return SASL_OK;
520 if (!sparams || !user) return SASL_BADPARAM;
522 pr = sparams->utils->prop_get( sparams->propctx );
524 /* Find our DN and conn first */
525 for( i = 0; pr[i].name; i++ ) {
526 if ( pr[i].name[0] == '*' ) {
527 if ( !strcmp( pr[i].name, slap_propnames[PROP_CONN] ) ) {
528 if ( pr[i].values && pr[i].values[0] )
529 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
532 if ( !strcmp( pr[i].name, slap_propnames[PROP_AUTHC] ) ) {
533 if ( pr[i].values && pr[i].values[0] ) {
534 AC_MEMCPY( &op.o_req_ndn, pr[i].values[0], sizeof( struct berval ) );
539 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
541 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
543 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
545 pr = sparams->utils->prop_get( prctx );
546 if (!pr) return SASL_BADPARAM;
548 for (i=0; pr[i].name; i++);
549 if (!i) return SASL_BADPARAM;
551 for (i=0; pr[i].name; i++) {
552 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
553 mod->sml_op = LDAP_MOD_REPLACE;
554 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
555 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
556 sizeof(struct berval));
557 for (j=0; j<pr[i].nvalues; j++) {
558 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
560 BER_BVZERO( &mod->sml_values[j] );
561 mod->sml_nvalues = NULL;
562 mod->sml_desc = NULL;
564 modtail = &mod->sml_next;
568 rc = slap_mods_check( modlist, 0, &text, textbuf, textlen, NULL );
570 if ( rc == LDAP_SUCCESS ) {
571 rc = slap_mods_opattrs( &op, modlist, modtail, &text, textbuf,
575 if ( rc == LDAP_SUCCESS ) {
576 op.o_tag = LDAP_REQ_MODIFY;
577 op.o_protocol = LDAP_VERSION3;
578 op.o_ndn = op.o_req_ndn;
580 op.o_time = slap_get_time();
581 op.o_do_not_cache = 1;
582 op.o_is_auth_check = 1;
583 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
584 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
585 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
587 op.o_connid = conn->c_connid;
588 op.o_req_dn = op.o_req_ndn;
589 op.orm_modlist = modlist;
591 rc = op.o_bd->be_modify( &op, &rs );
593 slap_mods_free( modlist );
594 return rc ? SASL_FAIL : SASL_OK;
596 #endif /* SASL_VERSION_FULL >= 2.1.16 */
598 static sasl_auxprop_plug_t slap_auxprop_plugin = {
601 NULL, /* glob_context */
602 NULL, /* auxprop_free */
605 #if SASL_VERSION_FULL >= 0x020110
606 slap_auxprop_store /* the declaration of this member changed
607 * in cyrus SASL from 2.1.15 to 2.1.16 */
615 const sasl_utils_t *utils,
618 sasl_auxprop_plug_t **plug,
619 const char *plugname)
621 if ( !out_version || !plug ) return SASL_BADPARAM;
623 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
625 *out_version = SASL_AUXPROP_PLUG_VERSION;
626 *plug = &slap_auxprop_plugin;
630 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
631 * auxiliary property, so that we can refer to it in sasl_authorize
632 * without interfering with anything else. Also, the SASL username
633 * buffer is constrained to 256 characters, and our DNs could be
634 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
637 slap_sasl_canonicalize(
643 const char *user_realm,
648 Connection *conn = (Connection *)context;
649 struct propctx *props = sasl_auxprop_getctx( sconn );
650 struct propval auxvals[3];
653 const char *names[2];
659 LDAP_LOG( TRANSPORT, ENTRY,
660 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
661 conn ? conn->c_connid : -1,
662 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
663 in ? in : "<empty>");
665 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
666 conn ? conn->c_connid : -1,
667 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
668 in ? in : "<empty>");
671 /* If name is too big, just truncate. We don't care, we're
672 * using DNs, not the usernames.
674 if ( inlen > out_max )
677 /* This is a Simple Bind using SPASSWD. That means the in-directory
678 * userPassword of the Binding user already points at SASL, so it
679 * cannot be used to actually satisfy a password comparison. Just
680 * ignore it, some other mech will process it.
682 if ( !conn->c_sasl_bindop ||
683 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
685 /* See if we need to add request, can only do it once */
686 prop_getnames( props, slap_propnames, auxvals );
687 if ( !auxvals[0].name )
688 prop_request( props, slap_propnames );
690 if ( flags & SASL_CU_AUTHID )
695 /* Need to store the Connection for auxprop_lookup */
696 if ( !auxvals[PROP_CONN].values ) {
697 names[0] = slap_propnames[PROP_CONN];
699 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
702 /* Already been here? */
703 if ( auxvals[which].values )
706 /* Normally we require an authzID to have a u: or dn: prefix.
707 * However, SASL frequently gives us an authzID that is just
708 * an exact copy of the authcID, without a prefix. We need to
709 * detect and allow this condition. If SASL calls canonicalize
710 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
711 * But if it's broken into two calls, we need to remember the
712 * authcID so that we can compare the authzID later. We store
713 * the authcID temporarily in conn->c_sasl_dn. We necessarily
714 * finish Canonicalizing before Authorizing, so there is no
715 * conflict with slap_sasl_authorize's use of this temp var.
717 * The SASL EXTERNAL mech is backwards from all the other mechs,
718 * it does authzID before the authcID. If we see that authzID
719 * has already been done, don't do anything special with authcID.
721 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
722 conn->c_sasl_dn.bv_val = (char *) in;
723 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
724 rc = strcmp( in, conn->c_sasl_dn.bv_val );
725 conn->c_sasl_dn.bv_val = NULL;
726 /* They were equal, no work needed */
727 if ( !rc ) goto done;
730 bvin.bv_val = (char *)in;
732 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
733 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
734 if ( rc != LDAP_SUCCESS ) {
735 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
739 names[0] = slap_propnames[which];
742 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
745 LDAP_LOG( TRANSPORT, ENTRY,
746 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
747 conn ? conn->c_connid : -1, names[0]+1,
748 dn.bv_val ? dn.bv_val : "<EMPTY>" );
750 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
751 conn ? conn->c_connid : -1, names[0]+1,
752 dn.bv_val ? dn.bv_val : "<EMPTY>" );
756 AC_MEMCPY( out, in, inlen );
768 char *requested_user,
772 const char *def_realm,
774 struct propctx *props)
776 Connection *conn = (Connection *)context;
777 struct propval auxvals[3];
778 struct berval authcDN, authzDN;
781 /* Simple Binds don't support proxy authorization, ignore it */
782 if ( !conn->c_sasl_bindop ||
783 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
786 LDAP_LOG( TRANSPORT, ENTRY,
787 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
788 conn ? conn->c_connid : -1, auth_identity, requested_user);
790 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
791 "authcid=\"%s\" authzid=\"%s\"\n",
792 conn ? conn->c_connid : -1, auth_identity, requested_user );
794 if ( conn->c_sasl_dn.bv_val ) {
795 ch_free( conn->c_sasl_dn.bv_val );
796 BER_BVZERO( &conn->c_sasl_dn );
800 prop_getnames( props, slap_propnames+1, auxvals );
802 /* Should not happen */
803 if ( !auxvals[0].values ) {
804 sasl_seterror( sconn, 0, "invalid authcid" );
808 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
810 /* Nothing to do if no authzID was given */
811 if ( !auxvals[1].name || !auxvals[1].values ) {
812 conn->c_sasl_dn = authcDN;
816 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
818 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
819 ch_free( authcDN.bv_val );
820 if ( rc != LDAP_SUCCESS ) {
822 LDAP_LOG( TRANSPORT, INFO,
823 "slap_sasl_authorize: conn %ld "
824 "proxy authorization disallowed (%d)\n",
825 (long)(conn ? conn->c_connid : -1), rc, 0 );
827 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
828 "proxy authorization disallowed (%d)\n",
829 (long) (conn ? conn->c_connid : -1), rc, 0 );
832 sasl_seterror( sconn, 0, "not authorized" );
833 ch_free( authzDN.bv_val );
837 conn->c_sasl_dn = authzDN;
839 if (conn->c_sasl_bindop) {
840 Statslog( LDAP_DEBUG_STATS,
841 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
842 conn->c_connid, conn->c_sasl_bindop->o_opid,
843 auth_identity, 0, 0);
847 LDAP_LOG( TRANSPORT, ENTRY,
848 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
849 (long)(conn ? conn->c_connid : -1), 0, 0 );
851 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
852 " proxy authorization allowed\n",
853 (long) (conn ? conn->c_connid : -1), 0, 0 );
866 struct berval authcDN, authzDN;
868 Connection *conn = context;
870 struct berval bvauthcid, bvauthzid;
873 if ( conn->c_sasl_dn.bv_val ) {
874 ch_free( conn->c_sasl_dn.bv_val );
875 BER_BVZERO( &conn->c_sasl_dn );
879 LDAP_LOG( TRANSPORT, ENTRY,
880 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
881 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
882 authzid ? authzid : "<empty>" );
884 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
885 "authcid=\"%s\" authzid=\"%s\"\n",
886 (long) (conn ? conn->c_connid : -1),
887 authcid ? authcid : "<empty>",
888 authzid ? authzid : "<empty>" );
891 /* Figure out how much data we have for the dn */
892 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
893 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
895 LDAP_LOG( TRANSPORT, ERR,
896 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
898 Debug(LDAP_DEBUG_TRACE,
899 "authorize: getprop(REALM) failed!\n", 0,0,0);
901 *errstr = "Could not extract realm";
905 /* Convert the identities to DN's. If no authzid was given, client will
906 be bound as the DN matching their username */
907 bvauthcid.bv_val = authcid;
908 bvauthcid.bv_len = authcid ? strlen( authcid ) : 0;
909 rc = slap_sasl_getdn( conn, NULL, &bvauthcid, realm,
910 &authcDN, SLAP_GETDN_AUTHCID );
911 if( rc != LDAP_SUCCESS ) {
912 *errstr = ldap_err2string( rc );
915 if( ( authzid == NULL ) || !strcmp( authcid, authzid ) ) {
917 LDAP_LOG( TRANSPORT, ENTRY,
918 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
919 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
921 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
922 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
925 conn->c_sasl_dn = authcDN;
929 bvauthzid.bv_val = authzid;
930 bvauthzid.bv_len = authzid ? strlen( authzid ) : 0;
931 rc = slap_sasl_getdn( conn, NULL, &bvauthzid, realm,
932 &authzDN, SLAP_GETDN_AUTHZID );
933 if( rc != LDAP_SUCCESS ) {
934 ch_free( authcDN.bv_val );
935 *errstr = ldap_err2string( rc );
939 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
940 ch_free( authcDN.bv_val );
943 LDAP_LOG( TRANSPORT, INFO,
944 "slap_sasl_authorize: conn %ld "
945 "proxy authorization disallowed (%d)\n",
946 (long)(conn ? conn->c_connid : -1), rc, 0 );
948 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
949 "proxy authorization disallowed (%d)\n",
950 (long) (conn ? conn->c_connid : -1), rc, 0 );
953 *errstr = "not authorized";
954 ch_free( authzDN.bv_val );
957 conn->c_sasl_dn = authzDN;
961 LDAP_LOG( TRANSPORT, RESULTS,
962 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
963 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
965 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
966 " authorization allowed\n",
967 (long) (conn ? conn->c_connid : -1), 0, 0 );
970 if (conn->c_sasl_bindop) {
971 Statslog( LDAP_DEBUG_STATS,
972 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
973 conn->c_connid, conn->c_sasl_bindop->o_opid,
980 #endif /* SASL_VERSION_MAJOR >= 2 */
983 slap_sasl_err2ldap( int saslerr )
992 rc = LDAP_SASL_BIND_IN_PROGRESS;
1001 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1004 rc = LDAP_INVALID_CREDENTIALS;
1007 rc = LDAP_INSUFFICIENT_ACCESS;
1011 rc = LDAP_INAPPROPRIATE_AUTH;
1022 int slap_sasl_init( void )
1024 #ifdef HAVE_CYRUS_SASL
1026 static sasl_callback_t server_callbacks[] = {
1027 { SASL_CB_LOG, &slap_sasl_log, NULL },
1028 { SASL_CB_LIST_END, NULL, NULL }
1031 #ifdef HAVE_SASL_VERSION
1032 /* stringify the version number, sasl.h doesn't do it for us */
1033 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1034 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1035 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1038 sasl_version( NULL, &rc );
1039 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1040 (rc & 0xffff) < SASL_VERSION_STEP)
1042 char version[sizeof("xxx.xxx.xxxxx")];
1043 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1046 LDAP_LOG( TRANSPORT, INFO,
1047 "slap_sasl_init: SASL library version mismatch:"
1048 " expected " SASL_VERSION_STRING ","
1049 " got %s\n", version, 0, 0 );
1051 Debug( LDAP_DEBUG_ANY,
1052 "slap_sasl_init: SASL library version mismatch:"
1053 " expected " SASL_VERSION_STRING ","
1054 " got %s\n", version, 0, 0 );
1060 /* SASL 2 does its own memory management internally */
1061 #if SASL_VERSION_MAJOR < 2
1070 ldap_pvt_sasl_mutex_new,
1071 ldap_pvt_sasl_mutex_lock,
1072 ldap_pvt_sasl_mutex_unlock,
1073 ldap_pvt_sasl_mutex_dispose );
1075 #if SASL_VERSION_MAJOR >= 2
1076 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1078 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1080 /* should provide callbacks for logging */
1081 /* server name should be configurable */
1082 rc = sasl_server_init( server_callbacks, "slapd" );
1084 if( rc != SASL_OK ) {
1086 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1088 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1091 #if SASL_VERSION_MAJOR < 2
1092 /* A no-op used to make sure we linked with Cyrus 1.5 */
1093 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1100 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1102 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1106 /* default security properties */
1107 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1108 sasl_secprops.max_ssf = INT_MAX;
1109 sasl_secprops.maxbufsize = 65536;
1110 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1116 int slap_sasl_destroy( void )
1118 #ifdef HAVE_CYRUS_SASL
1121 free( global_host );
1127 int slap_sasl_open( Connection *conn, int reopen )
1129 int sc = LDAP_SUCCESS;
1130 #ifdef HAVE_CYRUS_SASL
1133 sasl_conn_t *ctx = NULL;
1134 sasl_callback_t *session_callbacks;
1136 #if SASL_VERSION_MAJOR >= 2
1137 char *ipremoteport = NULL, *iplocalport = NULL;
1140 assert( conn->c_sasl_authctx == NULL );
1143 assert( conn->c_sasl_extra == NULL );
1146 #if SASL_VERSION_MAJOR >= 2
1147 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1149 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1151 if( session_callbacks == NULL ) {
1153 LDAP_LOG( TRANSPORT, ERR,
1154 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1156 Debug( LDAP_DEBUG_ANY,
1157 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1161 conn->c_sasl_extra = session_callbacks;
1163 session_callbacks[cb=0].id = SASL_CB_LOG;
1164 session_callbacks[cb].proc = &slap_sasl_log;
1165 session_callbacks[cb++].context = conn;
1167 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1168 session_callbacks[cb].proc = &slap_sasl_authorize;
1169 session_callbacks[cb++].context = conn;
1171 #if SASL_VERSION_MAJOR >= 2
1172 session_callbacks[cb].id = SASL_CB_CANON_USER;
1173 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1174 session_callbacks[cb++].context = conn;
1177 session_callbacks[cb].id = SASL_CB_LIST_END;
1178 session_callbacks[cb].proc = NULL;
1179 session_callbacks[cb++].context = NULL;
1181 session_callbacks = conn->c_sasl_extra;
1184 conn->c_sasl_layers = 0;
1186 if( global_host == NULL ) {
1187 global_host = ldap_pvt_get_fqdn( NULL );
1190 /* create new SASL context */
1191 #if SASL_VERSION_MAJOR >= 2
1192 if ( conn->c_sock_name.bv_len != 0 &&
1193 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1196 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1197 /* Convert IPv6 addresses to address;port syntax. */
1198 p = strrchr( iplocalport, ' ' );
1199 /* Convert IPv4 addresses to address;port syntax. */
1200 if ( p == NULL ) p = strchr( iplocalport, ':' );
1205 if ( conn->c_peer_name.bv_len != 0 &&
1206 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1209 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1210 /* Convert IPv6 addresses to address;port syntax. */
1211 p = strrchr( ipremoteport, ' ' );
1212 /* Convert IPv4 addresses to address;port syntax. */
1213 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1218 sc = sasl_server_new( "ldap", global_host, global_realm,
1219 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1220 if ( iplocalport != NULL ) {
1221 ch_free( iplocalport );
1223 if ( ipremoteport != NULL ) {
1224 ch_free( ipremoteport );
1227 sc = sasl_server_new( "ldap", global_host, global_realm,
1228 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1231 if( sc != SASL_OK ) {
1233 LDAP_LOG( TRANSPORT, ERR,
1234 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1236 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1243 conn->c_sasl_authctx = ctx;
1245 if( sc == SASL_OK ) {
1246 sc = sasl_setprop( ctx,
1247 SASL_SEC_PROPS, &sasl_secprops );
1249 if( sc != SASL_OK ) {
1251 LDAP_LOG( TRANSPORT, ERR,
1252 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1254 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1258 slap_sasl_close( conn );
1263 sc = slap_sasl_err2ldap( sc );
1265 #elif defined(SLAP_BUILTIN_SASL)
1266 /* built-in SASL implementation */
1267 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1268 if( ctx == NULL ) return -1;
1270 ctx->sc_external_ssf = 0;
1271 BER_BVZERO( &ctx->sc_external_id );
1273 conn->c_sasl_authctx = ctx;
1279 int slap_sasl_external(
1282 struct berval *auth_id )
1284 #if SASL_VERSION_MAJOR >= 2
1286 sasl_conn_t *ctx = conn->c_sasl_authctx;
1288 if ( ctx == NULL ) {
1289 return LDAP_UNAVAILABLE;
1292 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1294 if ( sc != SASL_OK ) {
1298 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1299 auth_id ? auth_id->bv_val : NULL );
1301 if ( sc != SASL_OK ) {
1305 #elif defined(HAVE_CYRUS_SASL)
1307 sasl_conn_t *ctx = conn->c_sasl_authctx;
1308 sasl_external_properties_t extprops;
1310 if ( ctx == NULL ) {
1311 return LDAP_UNAVAILABLE;
1314 memset( &extprops, '\0', sizeof(extprops) );
1316 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1318 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1319 (void *) &extprops );
1321 if ( sc != SASL_OK ) {
1324 #elif defined(SLAP_BUILTIN_SASL)
1325 /* built-in SASL implementation */
1326 SASL_CTX *ctx = conn->c_sasl_authctx;
1327 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1329 ctx->sc_external_ssf = ssf;
1331 ctx->sc_external_id = *auth_id;
1332 BER_BVZERO( auth_id );
1334 BER_BVZERO( &ctx->sc_external_id );
1338 return LDAP_SUCCESS;
1341 int slap_sasl_reset( Connection *conn )
1343 return LDAP_SUCCESS;
1346 char ** slap_sasl_mechs( Connection *conn )
1348 char **mechs = NULL;
1350 #ifdef HAVE_CYRUS_SASL
1351 sasl_conn_t *ctx = conn->c_sasl_authctx;
1353 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1357 SASL_CONST char *mechstr;
1359 sc = sasl_listmech( ctx,
1360 NULL, NULL, ",", NULL,
1361 &mechstr, NULL, NULL );
1363 if( sc != SASL_OK ) {
1365 LDAP_LOG( TRANSPORT, ERR,
1366 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1368 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1375 mechs = ldap_str2charray( mechstr, "," );
1377 #if SASL_VERSION_MAJOR < 2
1381 #elif defined(SLAP_BUILTIN_SASL)
1382 /* builtin SASL implementation */
1383 SASL_CTX *ctx = conn->c_sasl_authctx;
1384 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1385 /* should check ssf */
1386 mechs = ldap_str2charray( "EXTERNAL", "," );
1393 int slap_sasl_close( Connection *conn )
1395 #ifdef HAVE_CYRUS_SASL
1396 sasl_conn_t *ctx = conn->c_sasl_authctx;
1399 sasl_dispose( &ctx );
1401 if ( conn->c_sasl_sockctx &&
1402 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1404 ctx = conn->c_sasl_sockctx;
1405 sasl_dispose( &ctx );
1408 conn->c_sasl_authctx = NULL;
1409 conn->c_sasl_sockctx = NULL;
1410 conn->c_sasl_done = 0;
1412 free( conn->c_sasl_extra );
1413 conn->c_sasl_extra = NULL;
1415 #elif defined(SLAP_BUILTIN_SASL)
1416 SASL_CTX *ctx = conn->c_sasl_authctx;
1418 if( ctx->sc_external_id.bv_val ) {
1419 free( ctx->sc_external_id.bv_val );
1420 BER_BVZERO( &ctx->sc_external_id );
1423 conn->c_sasl_authctx = NULL;
1427 return LDAP_SUCCESS;
1430 int slap_sasl_bind( Operation *op, SlapReply *rs )
1432 #ifdef HAVE_CYRUS_SASL
1433 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1434 struct berval response;
1435 unsigned reslen = 0;
1439 LDAP_LOG( TRANSPORT, ENTRY,
1440 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1441 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1442 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1443 op->o_conn->c_sasl_bind_mech.bv_val,
1444 op->orb_cred.bv_len );
1446 Debug(LDAP_DEBUG_ARGS,
1447 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1448 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1449 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1450 op->o_conn->c_sasl_bind_mech.bv_val,
1451 op->orb_cred.bv_len );
1455 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1456 "SASL unavailable on this session" );
1460 #if SASL_VERSION_MAJOR >= 2
1461 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1462 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1463 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1464 sasl_server_step( ctx, cred, clen, resp, rlen )
1466 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1467 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1468 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1469 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1472 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1473 /* If we already authenticated once, must use a new context */
1474 if ( op->o_conn->c_sasl_done ) {
1476 const char *authid = NULL;
1477 #if SASL_VERSION_MAJOR >= 2
1478 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1479 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1480 if ( authid ) authid = ch_strdup( authid );
1482 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1483 sasl_dispose( &ctx );
1485 op->o_conn->c_sasl_authctx = NULL;
1487 slap_sasl_open( op->o_conn, 1 );
1488 ctx = op->o_conn->c_sasl_authctx;
1489 #if SASL_VERSION_MAJOR >= 2
1491 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1492 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1493 ch_free( (char *)authid );
1498 op->o_conn->c_sasl_bind_mech.bv_val,
1499 op->orb_cred.bv_val, op->orb_cred.bv_len,
1500 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1504 op->orb_cred.bv_val, op->orb_cred.bv_len,
1505 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1508 response.bv_len = reslen;
1510 if ( sc == SASL_OK ) {
1511 sasl_ssf_t *ssf = NULL;
1513 op->orb_edn = op->o_conn->c_sasl_dn;
1514 BER_BVZERO( &op->o_conn->c_sasl_dn );
1515 op->o_conn->c_sasl_done = 1;
1517 rs->sr_err = LDAP_SUCCESS;
1519 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1520 op->orb_ssf = ssf ? *ssf : 0;
1524 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1525 op->o_conn->c_sasl_layers++;
1527 /* If there's an old layer, set sockctx to NULL to
1528 * tell connection_read() to wait for us to finish.
1529 * Otherwise there is a race condition: we have to
1530 * send the Bind response using the old security
1531 * context and then remove it before reading any
1534 if ( op->o_conn->c_sasl_sockctx ) {
1535 ctx = op->o_conn->c_sasl_sockctx;
1536 op->o_conn->c_sasl_sockctx = NULL;
1538 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1540 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1543 /* Must send response using old security layer */
1544 if (response.bv_len) rs->sr_sasldata = &response;
1545 send_ldap_sasl( op, rs );
1547 /* Now dispose of the old security layer.
1550 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1551 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1552 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1553 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1554 sasl_dispose( &ctx );
1556 } else if ( sc == SASL_CONTINUE ) {
1557 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1558 rs->sr_sasldata = &response;
1559 send_ldap_sasl( op, rs );
1562 #if SASL_VERSION_MAJOR >= 2
1563 rs->sr_text = sasl_errdetail( ctx );
1565 rs->sr_err = slap_sasl_err2ldap( sc ),
1566 send_ldap_result( op, rs );
1569 #if SASL_VERSION_MAJOR < 2
1570 if( response.bv_len ) {
1571 ch_free( response.bv_val );
1576 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0 );
1578 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1581 #elif defined(SLAP_BUILTIN_SASL)
1582 /* built-in SASL implementation */
1583 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1585 if ( ctx == NULL ) {
1586 send_ldap_error( op, rs, LDAP_OTHER,
1587 "Internal SASL Error" );
1589 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1592 if( op->orb_cred.bv_len ) {
1593 rs->sr_text = "proxy authorization not support";
1594 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1595 send_ldap_result( op, rs );
1598 op->orb_edn = ctx->sc_external_id;
1599 rs->sr_err = LDAP_SUCCESS;
1600 rs->sr_sasldata = NULL;
1601 send_ldap_sasl( op, rs );
1605 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1606 "requested SASL mechanism not supported" );
1609 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1610 "SASL not supported" );
1616 char* slap_sasl_secprops( const char *in )
1618 #ifdef HAVE_CYRUS_SASL
1619 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1621 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1623 return "SASL not supported";
1627 #ifdef HAVE_CYRUS_SASL
1629 slap_sasl_setpass( Operation *op, SlapReply *rs )
1631 struct berval id = BER_BVNULL; /* needs to come from connection */
1632 struct berval new = BER_BVNULL;
1633 struct berval old = BER_BVNULL;
1635 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1637 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1638 (SASL_CONST void **)&id.bv_val );
1640 if( rs->sr_err != SASL_OK ) {
1641 rs->sr_text = "unable to retrieve SASL username";
1642 rs->sr_err = LDAP_OTHER;
1647 LDAP_LOG( BACKEND, ENTRY,
1648 "slap_sasl_setpass: \"%s\"\n",
1649 id.bv_val ? id.bv_val : "", 0, 0);
1651 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1652 id.bv_val ? id.bv_val : "", 0, 0 );
1655 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1656 NULL, &old, &new, &rs->sr_text );
1658 if( rs->sr_err != LDAP_SUCCESS ) {
1662 if( new.bv_len == 0 ) {
1663 slap_passwd_generate(&new);
1665 if( new.bv_len == 0 ) {
1666 rs->sr_text = "password generation failed.";
1667 rs->sr_err = LDAP_OTHER;
1671 rs->sr_rspdata = slap_passwd_return( &new );
1674 #if SASL_VERSION_MAJOR < 2
1675 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1676 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
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 );
1684 switch(rs->sr_err) {
1686 rs->sr_err = LDAP_SUCCESS;
1696 rs->sr_err = LDAP_OTHER;
1702 #endif /* HAVE_CYRUS_SASL */
1704 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1705 * string returned in *dn is in its own allocated memory, and must be free'd
1706 * by the calling process. -Mark Adamson, Carnegie Mellon
1708 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1709 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1716 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1717 char *user_realm, struct berval *dn, int flags )
1719 int rc, is_dn = SET_NONE, do_norm = 1;
1720 struct berval dn2, *mech;
1726 LDAP_LOG( TRANSPORT, ENTRY,
1727 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1729 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1730 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 : id->bv_len ) );
1732 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1734 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1735 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 : id->bv_len ) );
1739 op = conn->c_sasl_bindop;
1744 if ( !BER_BVISNULL( id ) ) {
1745 /* Blatantly anonymous ID */
1746 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1748 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1749 return( LDAP_SUCCESS );
1753 /* FIXME: if empty, should we stop? */
1754 BER_BVSTR( id, "" );
1757 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1758 mech = &conn->c_sasl_bind_mech;
1760 mech = &conn->c_authmech;
1763 /* An authcID needs to be converted to authzID form. Set the
1764 * values directly into *dn; they will be normalized later. (and
1765 * normalizing always makes a new copy.) An ID from a TLS certificate
1766 * is already normalized, so copy it and skip normalization.
1768 if( flags & SLAP_GETDN_AUTHCID ) {
1769 if( bvmatch( mech, &ext_bv )) {
1770 /* EXTERNAL DNs are already normalized */
1771 assert( !BER_BVISNULL( id ) );
1775 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1778 /* convert to u:<username> form */
1784 if( is_dn == SET_NONE ) {
1785 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1787 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1788 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1790 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1792 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1793 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1797 /* No other possibilities from here */
1798 if( is_dn == SET_NONE ) {
1800 return( LDAP_INAPPROPRIATE_AUTH );
1803 /* Username strings */
1804 if( is_dn == SET_U ) {
1806 struct berval realm = BER_BVNULL, c1 = *dn;
1809 len = dn->bv_len + STRLENOF( "uid=" ) + STRLENOF( ",cn=auth" );
1811 if( user_realm && *user_realm ) {
1812 ber_str2bv( user_realm, 0, 0, &realm );
1813 len += realm.bv_len + STRLENOF( ",cn=" );
1816 if( mech->bv_len ) {
1817 len += mech->bv_len + STRLENOF( ",cn=" );
1820 /* Build the new dn */
1821 dn->bv_val = slap_sl_malloc( len + 1, op->o_tmpmemctx );
1822 if( dn->bv_val == NULL ) {
1824 LDAP_LOG( TRANSPORT, ERR,
1825 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1827 Debug( LDAP_DEBUG_ANY,
1828 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1832 p = lutil_strcopy( dn->bv_val, "uid=" );
1833 p = lutil_strncopy( p, c1.bv_val, c1.bv_len );
1835 if( realm.bv_len ) {
1836 p = lutil_strcopy( p, ",cn=" );
1837 p = lutil_strncopy( p, realm.bv_val, realm.bv_len );
1840 if( mech->bv_len ) {
1841 p = lutil_strcopy( p, ",cn=" );
1842 p = lutil_strcopy( p, mech->bv_val );
1844 p = lutil_strcopy( p, ",cn=auth" );
1845 dn->bv_len = p - dn->bv_val;
1848 LDAP_LOG( TRANSPORT, ENTRY,
1849 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1851 Debug( LDAP_DEBUG_TRACE, "slap_sasl_getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1855 /* Dup the DN in any case, so we don't risk
1856 * leaks or dangling pointers later,
1857 * and the DN value is '\0' terminated */
1858 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1859 dn->bv_val = dn2.bv_val;
1862 /* All strings are in DN form now. Normalize if needed. */
1864 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1866 /* User DNs were constructed above and must be freed now */
1867 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1869 if ( rc != LDAP_SUCCESS ) {
1876 /* Run thru regexp */
1877 slap_sasl2dn( op, dn, &dn2, flags );
1878 if( !BER_BVISNULL( &dn2 ) ) {
1879 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1882 LDAP_LOG( TRANSPORT, ENTRY,
1883 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1885 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1890 return( LDAP_SUCCESS );