2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 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 static sasl_security_properties_t sasl_secprops;
49 #define SASL_VERSION_FULL ((SASL_VERSION_MAJOR << 16) |\
50 (SASL_VERSION_MINOR << 8) | SASL_VERSION_STEP)
52 #endif /* HAVE_CYRUS_SASL */
58 int slap_sasl_config( int cargc, char **cargv, char *line,
59 const char *fname, int lineno )
61 /* set SASL proxy authorization policy */
62 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
65 LDAP_LOG( CONFIG, CRIT,
66 "%s: line %d: missing policy in"
67 " \"sasl-authz-policy <policy>\" line\n",
70 Debug( LDAP_DEBUG_ANY,
71 "%s: line %d: missing policy in"
72 " \"sasl-authz-policy <policy>\" line\n",
78 if ( slap_sasl_setpolicy( cargv[1] ) ) {
80 LDAP_LOG( CONFIG, CRIT,
81 "%s: line %d: unable "
82 "to parse value \"%s\" "
83 "in \"sasl-authz-policy "
85 fname, lineno, cargv[1] );
87 Debug( LDAP_DEBUG_ANY,
88 "%s: line %d: unable "
89 "to parse value \"%s\" "
90 "in \"sasl-authz-policy "
92 fname, lineno, cargv[1] );
97 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
98 || !strcasecmp( cargv[0], "saslregexp" ) )
103 LDAP_LOG( CONFIG, CRIT,
104 "%s: line %d: need 2 args in "
105 "\"saslregexp <match> <replace>\"\n",
108 Debug( LDAP_DEBUG_ANY,
109 "%s: line %d: need 2 args in "
110 "\"saslregexp <match> <replace>\"\n",
116 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
121 #ifdef HAVE_CYRUS_SASL
123 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
126 LDAP_LOG( CONFIG, CRIT,
127 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
130 Debug( LDAP_DEBUG_ANY,
131 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
138 if ( global_host != NULL ) {
140 LDAP_LOG( CONFIG, CRIT,
141 "%s: line %d: already set sasl-host!\n",
144 Debug( LDAP_DEBUG_ANY,
145 "%s: line %d: already set sasl-host!\n",
152 global_host = ch_strdup( cargv[1] );
156 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
159 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
160 "missing realm in \"sasl-realm <realm>\" line.\n",
163 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
164 "missing realm in \"sasl-realm <realm>\" line.\n",
171 if ( global_realm != NULL ) {
173 LDAP_LOG( CONFIG, CRIT,
174 "%s: line %d: already set sasl-realm!\n",
177 Debug( LDAP_DEBUG_ANY,
178 "%s: line %d: already set sasl-realm!\n",
185 global_realm = ch_strdup( cargv[1] );
188 /* SASL security properties */
189 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
194 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
195 "missing flags in \"sasl-secprops <properties>\" line\n",
198 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
199 "missing flags in \"sasl-secprops <properties>\" line\n",
206 txt = slap_sasl_secprops( cargv[1] );
209 LDAP_LOG( CONFIG, CRIT,
210 "%s: line %d sasl-secprops: %s\n",
211 fname, lineno, txt );
213 Debug( LDAP_DEBUG_ANY,
214 "%s: line %d: sasl-secprops: %s\n",
215 fname, lineno, txt );
220 #endif /* HAVE_CYRUS_SASL */
226 #ifdef HAVE_CYRUS_SASL
234 Connection *conn = context;
238 if ( message == NULL ) {
239 return SASL_BADPARAM;
243 #if SASL_VERSION_MAJOR >= 2
245 level = LDAP_DEBUG_NONE;
249 level = LDAP_DEBUG_ANY;
253 level = LDAP_DEBUG_ANY;
257 level = LDAP_DEBUG_TRACE;
261 level = LDAP_DEBUG_TRACE;
265 level = LDAP_DEBUG_TRACE;
269 level = LDAP_DEBUG_TRACE;
273 level = LDAP_DEBUG_TRACE;
274 label = "Password Trace";
278 level = LDAP_DEBUG_ANY;
281 case SASL_LOG_WARNING:
282 level = LDAP_DEBUG_TRACE;
286 level = LDAP_DEBUG_TRACE;
291 return SASL_BADPARAM;
295 LDAP_LOG( TRANSPORT, ENTRY,
296 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
298 Debug( level, "SASL [conn=%ld] %s: %s\n",
299 conn ? conn->c_connid: -1,
308 #if SASL_VERSION_MAJOR >= 2
309 static const char *slap_propnames[] = {
310 "*slapConn", "*authcDN", "*authzDN", NULL };
312 static Filter generic_filter = { LDAP_FILTER_PRESENT };
313 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
319 typedef struct lookup_info {
322 const struct propval *list;
323 sasl_server_params_t *sparams;
326 static slap_response sasl_ap_lookup, sasl_cb_checkpass;
329 sasl_ap_lookup( Operation *op, SlapReply *rs )
332 AttributeDescription *ad;
336 slap_callback *tmp = op->o_callback;
337 lookup_info *sl = tmp->sc_private;
339 if (rs->sr_type != REP_SEARCH) return 0;
341 for( i = 0; i < sl->last; i++ ) {
342 const char *name = sl->list[i].name;
344 if ( name[0] == '*' ) {
345 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
347 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
350 if ( sl->list[i].values ) {
351 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
354 rc = slap_str2ad( name, &ad, &text );
355 if ( rc != LDAP_SUCCESS ) {
357 LDAP_LOG( TRANSPORT, DETAIL1,
358 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
360 Debug( LDAP_DEBUG_TRACE,
361 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
365 a = attr_find( rs->sr_entry->e_attrs, ad );
367 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
370 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
371 sl->sparams->utils->prop_erase( sl->sparams->propctx,
374 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
375 sl->sparams->utils->prop_set( sl->sparams->propctx,
376 sl->list[i].name, bv->bv_val, bv->bv_len );
385 sasl_server_params_t *sparams,
392 Connection *conn = NULL;
395 sl.list = sparams->utils->prop_get( sparams->propctx );
396 sl.sparams = sparams;
399 /* Find our DN and conn first */
400 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
401 if ( sl.list[i].name[0] == '*' ) {
402 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
403 if ( sl.list[i].values && sl.list[i].values[0] )
404 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
405 if ( !sl.last ) sl.last = i;
407 if ( (flags & SASL_AUXPROP_AUTHZID) &&
408 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
410 if ( sl.list[i].values && sl.list[i].values[0] )
411 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
412 if ( !sl.last ) sl.last = i;
415 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
416 if ( !sl.last ) sl.last = i;
417 if ( sl.list[i].values && sl.list[i].values[0] ) {
418 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
419 if ( !(flags & SASL_AUXPROP_AUTHZID) )
426 /* Now see what else needs to be fetched */
427 for( i = 0; i < sl.last; i++ ) {
428 const char *name = sl.list[i].name;
430 if ( name[0] == '*' ) {
431 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
433 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
436 if ( sl.list[i].values ) {
437 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
443 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
447 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
449 if ( op.o_bd && op.o_bd->be_search ) {
450 SlapReply rs = {REP_RESULT};
451 op.o_tag = LDAP_REQ_SEARCH;
452 op.o_protocol = LDAP_VERSION3;
453 op.o_ndn = conn->c_ndn;
455 op.o_time = slap_get_time();
456 op.o_do_not_cache = 1;
457 op.o_is_auth_check = 1;
458 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
459 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
460 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
462 op.o_connid = conn->c_connid;
463 op.o_req_dn = op.o_req_ndn;
464 op.ors_scope = LDAP_SCOPE_BASE;
465 op.ors_deref = LDAP_DEREF_NEVER;
467 op.ors_filter = &generic_filter;
468 op.ors_filterstr = generic_filterstr;
470 op.o_bd->be_search( &op, &rs );
475 #if SASL_VERSION_FULL >= 0x020110
479 sasl_server_params_t *sparams,
480 struct propctx *prctx,
485 SlapReply rs = {REP_RESULT};
487 Connection *conn = NULL;
488 const struct propval *pr;
489 Modifications *modlist = NULL, **modtail = &modlist, *mod;
490 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
491 char textbuf[SLAP_TEXT_BUFLEN];
493 size_t textlen = sizeof(textbuf);
495 /* just checking if we are enabled */
496 if (!prctx) return SASL_OK;
498 if (!sparams || !user) return SASL_BADPARAM;
500 pr = sparams->utils->prop_get( sparams->propctx );
502 /* Find our DN and conn first */
503 for( i = 0; pr[i].name; i++ ) {
504 if ( pr[i].name[0] == '*' ) {
505 if ( !strcmp( pr[i].name, slap_propnames[PROP_CONN] ) ) {
506 if ( pr[i].values && pr[i].values[0] )
507 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
510 if ( !strcmp( pr[i].name, slap_propnames[PROP_AUTHC] ) ) {
511 if ( pr[i].values && pr[i].values[0] ) {
512 AC_MEMCPY( &op.o_req_ndn, pr[i].values[0], sizeof( struct berval ) );
517 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
519 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
521 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
523 pr = sparams->utils->prop_get( prctx );
524 if (!pr) return SASL_BADPARAM;
526 for (i=0; pr[i].name; i++);
527 if (!i) return SASL_BADPARAM;
529 for (i=0; pr[i].name; i++) {
530 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
531 mod->sml_op = LDAP_MOD_REPLACE;
532 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
533 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
534 sizeof(struct berval));
535 for (j=0; j<pr[i].nvalues; j++) {
536 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
538 mod->sml_values[j].bv_val = NULL;
539 mod->sml_values[j].bv_len = 0;
540 mod->sml_nvalues = NULL;
541 mod->sml_desc = NULL;
543 modtail = &mod->sml_next;
547 rc = slap_mods_check( modlist, 0, &text, textbuf, textlen, NULL );
549 if ( rc == LDAP_SUCCESS ) {
550 rc = slap_mods_opattrs( &op, modlist, modtail, &text, textbuf,
554 if ( rc == LDAP_SUCCESS ) {
555 op.o_tag = LDAP_REQ_MODIFY;
556 op.o_protocol = LDAP_VERSION3;
557 op.o_ndn = op.o_req_ndn;
559 op.o_time = slap_get_time();
560 op.o_do_not_cache = 1;
561 op.o_is_auth_check = 1;
562 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
563 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
564 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
566 op.o_connid = conn->c_connid;
567 op.o_req_dn = op.o_req_ndn;
568 op.orm_modlist = modlist;
570 rc = op.o_bd->be_modify( &op, &rs );
572 slap_mods_free( modlist );
573 return rc ? SASL_FAIL : SASL_OK;
575 #endif /* SASL_VERSION_FULL >= 2.1.16 */
577 static sasl_auxprop_plug_t slap_auxprop_plugin = {
580 NULL, /* glob_context */
581 NULL, /* auxprop_free */
584 #if SASL_VERSION_FULL >= 0x020110
585 slap_auxprop_store /* the declaration of this member changed
586 * in cyrus SASL from 2.1.15 to 2.1.16 */
594 const sasl_utils_t *utils,
597 sasl_auxprop_plug_t **plug,
598 const char *plugname)
600 if ( !out_version | !plug ) return SASL_BADPARAM;
602 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
604 *out_version = SASL_AUXPROP_PLUG_VERSION;
605 *plug = &slap_auxprop_plugin;
609 typedef struct checkpass_info {
615 sasl_cb_checkpass( Operation *op, SlapReply *rs )
617 slap_callback *tmp = op->o_callback;
618 checkpass_info *ci = tmp->sc_private;
622 if (rs->sr_type != REP_SEARCH) return 0;
624 ci->rc = SASL_NOVERIFY;
626 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_userPassword );
628 if ( ! access_allowed( op, rs->sr_entry, slap_schema.si_ad_userPassword,
629 NULL, ACL_AUTH, NULL ) ) return 0;
631 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
632 if ( !lutil_passwd( bv, &ci->cred, NULL, &rs->sr_text ) ) {
644 const char *username,
647 struct propctx *propctx)
649 Connection *conn = (Connection *)context;
656 /* SASL will fallback to its own mechanisms if we don't
657 * find an answer here.
660 rc = slap_sasl_getdn( conn, NULL, (char *)username, 0, NULL, &op.o_req_ndn,
661 SLAP_GETDN_AUTHCID );
662 if ( rc != LDAP_SUCCESS ) {
663 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
667 if ( op.o_req_ndn.bv_len == 0 ) {
668 sasl_seterror( sconn, 0,
669 "No password is associated with the Root DSE" );
670 if ( op.o_req_ndn.bv_val != NULL ) {
671 ch_free( op.o_req_ndn.bv_val );
676 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
677 if ( op.o_bd && op.o_bd->be_search ) {
678 slap_callback cb = { NULL, sasl_cb_checkpass, NULL, NULL };
679 SlapReply rs = {REP_RESULT};
681 ci.cred.bv_val = (char *)pass;
682 ci.cred.bv_len = passlen;
685 op.o_tag = LDAP_REQ_SEARCH;
686 op.o_protocol = LDAP_VERSION3;
687 op.o_ndn = conn->c_ndn;
689 op.o_time = slap_get_time();
690 op.o_do_not_cache = 1;
691 op.o_is_auth_check = 1;
692 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
693 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
694 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
696 op.o_connid = conn->c_connid;
697 op.o_req_dn = op.o_req_ndn;
698 op.ors_scope = LDAP_SCOPE_BASE;
699 op.ors_deref = LDAP_DEREF_NEVER;
701 op.ors_filter = &generic_filter;
702 op.ors_filterstr = generic_filterstr;
704 op.o_bd->be_search( &op, &rs );
706 if ( ci.rc != SASL_OK ) {
707 sasl_seterror( sconn, 0,
708 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
711 ch_free( op.o_req_ndn.bv_val );
716 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
717 * auxiliary property, so that we can refer to it in sasl_authorize
718 * without interfering with anything else. Also, the SASL username
719 * buffer is constrained to 256 characters, and our DNs could be
720 * much longer (totally arbitrary length)...
723 slap_sasl_canonicalize(
729 const char *user_realm,
734 Connection *conn = (Connection *)context;
735 struct propctx *props = sasl_auxprop_getctx( sconn );
736 struct propval auxvals[3];
739 const char *names[2];
744 LDAP_LOG( TRANSPORT, ENTRY,
745 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
746 conn ? conn->c_connid : -1,
747 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
748 in ? in : "<empty>");
750 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
751 conn ? conn->c_connid : -1,
752 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
753 in ? in : "<empty>");
756 /* If name is too big, just truncate. We don't care, we're
757 * using DNs, not the usernames.
759 if ( inlen > out_max )
762 /* See if we need to add request, can only do it once */
763 prop_getnames( props, slap_propnames, auxvals );
764 if ( !auxvals[0].name )
765 prop_request( props, slap_propnames );
767 if ( flags & SASL_CU_AUTHID )
772 /* Need to store the Connection for auxprop_lookup */
773 if ( !auxvals[PROP_CONN].values ) {
774 names[0] = slap_propnames[PROP_CONN];
776 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
779 /* Already been here? */
780 if ( auxvals[which].values )
783 /* Normally we require an authzID to have a u: or dn: prefix.
784 * However, SASL frequently gives us an authzID that is just
785 * an exact copy of the authcID, without a prefix. We need to
786 * detect and allow this condition. If SASL calls canonicalize
787 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
788 * But if it's broken into two calls, we need to remember the
789 * authcID so that we can compare the authzID later. We store
790 * the authcID temporarily in conn->c_sasl_dn. We necessarily
791 * finish Canonicalizing before Authorizing, so there is no
792 * conflict with slap_sasl_authorize's use of this temp var.
794 * The SASL EXTERNAL mech is backwards from all the other mechs,
795 * it does authzID before the authcID. If we see that authzID
796 * has already been done, don't do anything special with authcID.
798 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
799 conn->c_sasl_dn.bv_val = (char *) in;
800 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
801 rc = strcmp( in, conn->c_sasl_dn.bv_val );
802 conn->c_sasl_dn.bv_val = NULL;
803 /* They were equal, no work needed */
804 if ( !rc ) goto done;
807 rc = slap_sasl_getdn( conn, NULL, (char *)in, inlen, (char *)user_realm, &dn,
808 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
809 if ( rc != LDAP_SUCCESS ) {
810 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
814 names[0] = slap_propnames[which];
817 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
820 LDAP_LOG( TRANSPORT, ENTRY,
821 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
822 conn ? conn->c_connid : -1, names[0]+1,
823 dn.bv_val ? dn.bv_val : "<EMPTY>" );
825 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
826 conn ? conn->c_connid : -1, names[0]+1,
827 dn.bv_val ? dn.bv_val : "<EMPTY>" );
831 AC_MEMCPY( out, in, inlen );
843 char *requested_user,
847 const char *def_realm,
849 struct propctx *props)
851 Connection *conn = (Connection *)context;
852 struct propval auxvals[3];
853 struct berval authcDN, authzDN;
857 LDAP_LOG( TRANSPORT, ENTRY,
858 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
859 conn ? conn->c_connid : -1, auth_identity, requested_user);
861 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
862 "authcid=\"%s\" authzid=\"%s\"\n",
863 conn ? conn->c_connid : -1, auth_identity, requested_user );
865 if ( conn->c_sasl_dn.bv_val ) {
866 ch_free( conn->c_sasl_dn.bv_val );
867 conn->c_sasl_dn.bv_val = NULL;
868 conn->c_sasl_dn.bv_len = 0;
872 prop_getnames( props, slap_propnames+1, auxvals );
874 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
876 /* Nothing to do if no authzID was given */
877 if ( !auxvals[1].name || !auxvals[1].values ) {
878 conn->c_sasl_dn = authcDN;
882 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
884 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
885 ch_free( authcDN.bv_val );
886 if ( rc != LDAP_SUCCESS ) {
888 LDAP_LOG( TRANSPORT, INFO,
889 "slap_sasl_authorize: conn %ld "
890 "proxy authorization disallowed (%d)\n",
891 (long)(conn ? conn->c_connid : -1), rc, 0 );
893 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
894 "proxy authorization disallowed (%d)\n",
895 (long) (conn ? conn->c_connid : -1), rc, 0 );
898 sasl_seterror( sconn, 0, "not authorized" );
899 ch_free( authzDN.bv_val );
903 conn->c_sasl_dn = authzDN;
905 if (conn->c_sasl_bindop) {
906 Statslog( LDAP_DEBUG_STATS,
907 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
908 conn->c_connid, conn->c_sasl_bindop->o_opid,
909 auth_identity, 0, 0);
913 LDAP_LOG( TRANSPORT, ENTRY,
914 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
915 (long)(conn ? conn->c_connid : -1), 0, 0 );
917 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
918 " proxy authorization allowed\n",
919 (long) (conn ? conn->c_connid : -1), 0, 0 );
932 struct berval authcDN, authzDN;
934 Connection *conn = context;
938 if ( conn->c_sasl_dn.bv_val ) {
939 ch_free( conn->c_sasl_dn.bv_val );
940 conn->c_sasl_dn.bv_val = NULL;
941 conn->c_sasl_dn.bv_len = 0;
945 LDAP_LOG( TRANSPORT, ENTRY,
946 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
947 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
948 authzid ? authzid : "<empty>" );
950 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
951 "authcid=\"%s\" authzid=\"%s\"\n",
952 (long) (conn ? conn->c_connid : -1),
953 authcid ? authcid : "<empty>",
954 authzid ? authzid : "<empty>" );
957 /* Figure out how much data we have for the dn */
958 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
959 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
961 LDAP_LOG( TRANSPORT, ERR,
962 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
964 Debug(LDAP_DEBUG_TRACE,
965 "authorize: getprop(REALM) failed!\n", 0,0,0);
967 *errstr = "Could not extract realm";
971 /* Convert the identities to DN's. If no authzid was given, client will
972 be bound as the DN matching their username */
973 rc = slap_sasl_getdn( conn, NULL, (char *)authcid, 0, realm,
974 &authcDN, SLAP_GETDN_AUTHCID );
975 if( rc != LDAP_SUCCESS ) {
976 *errstr = ldap_err2string( rc );
979 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
981 LDAP_LOG( TRANSPORT, ENTRY,
982 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
983 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
985 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
986 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
989 conn->c_sasl_dn = authcDN;
992 rc = slap_sasl_getdn( conn, NULL, (char *)authzid, 0, realm,
993 &authzDN, SLAP_GETDN_AUTHZID );
994 if( rc != LDAP_SUCCESS ) {
995 ch_free( authcDN.bv_val );
996 *errstr = ldap_err2string( rc );
1000 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
1001 ch_free( authcDN.bv_val );
1004 LDAP_LOG( TRANSPORT, INFO,
1005 "slap_sasl_authorize: conn %ld "
1006 "proxy authorization disallowed (%d)\n",
1007 (long)(conn ? conn->c_connid : -1), rc, 0 );
1009 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1010 "proxy authorization disallowed (%d)\n",
1011 (long) (conn ? conn->c_connid : -1), rc, 0 );
1014 *errstr = "not authorized";
1015 ch_free( authzDN.bv_val );
1016 return SASL_NOAUTHZ;
1018 conn->c_sasl_dn = authzDN;
1022 LDAP_LOG( TRANSPORT, RESULTS,
1023 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
1024 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
1026 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
1027 " authorization allowed\n",
1028 (long) (conn ? conn->c_connid : -1), 0, 0 );
1031 if (conn->c_sasl_bindop) {
1032 Statslog( LDAP_DEBUG_STATS,
1033 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
1034 conn->c_connid, conn->c_sasl_bindop->o_opid,
1041 #endif /* SASL_VERSION_MAJOR >= 2 */
1044 slap_sasl_err2ldap( int saslerr )
1053 rc = LDAP_SASL_BIND_IN_PROGRESS;
1062 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
1065 rc = LDAP_INVALID_CREDENTIALS;
1068 rc = LDAP_INSUFFICIENT_ACCESS;
1072 rc = LDAP_INAPPROPRIATE_AUTH;
1083 int slap_sasl_init( void )
1085 #ifdef HAVE_CYRUS_SASL
1087 static sasl_callback_t server_callbacks[] = {
1088 { SASL_CB_LOG, &slap_sasl_log, NULL },
1089 { SASL_CB_LIST_END, NULL, NULL }
1092 #ifdef HAVE_SASL_VERSION
1093 /* stringify the version number, sasl.h doesn't do it for us */
1094 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
1095 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
1096 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
1099 sasl_version( NULL, &rc );
1100 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
1101 (rc & 0xffff) < SASL_VERSION_STEP) {
1102 char version[sizeof("xxx.xxx.xxxxx")];
1103 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1106 LDAP_LOG( TRANSPORT, INFO,
1107 "slap_sasl_init: SASL library version mismatch:"
1108 " expected " SASL_VERSION_STRING ","
1109 " got %s\n", version, 0, 0 );
1111 Debug( LDAP_DEBUG_ANY,
1112 "slap_sasl_init: SASL library version mismatch:"
1113 " expected " SASL_VERSION_STRING ","
1114 " got %s\n", version, 0, 0 );
1120 /* SASL 2 does its own memory management internally */
1121 #if SASL_VERSION_MAJOR < 2
1130 ldap_pvt_sasl_mutex_new,
1131 ldap_pvt_sasl_mutex_lock,
1132 ldap_pvt_sasl_mutex_unlock,
1133 ldap_pvt_sasl_mutex_dispose );
1135 #if SASL_VERSION_MAJOR >= 2
1136 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1138 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1140 /* should provide callbacks for logging */
1141 /* server name should be configurable */
1142 rc = sasl_server_init( server_callbacks, "slapd" );
1144 if( rc != SASL_OK ) {
1146 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1148 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1151 #if SASL_VERSION_MAJOR < 2
1152 /* A no-op used to make sure we linked with Cyrus 1.5 */
1153 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1160 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1162 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1167 /* default security properties */
1168 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1169 sasl_secprops.max_ssf = INT_MAX;
1170 sasl_secprops.maxbufsize = 65536;
1171 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1177 int slap_sasl_destroy( void )
1179 #ifdef HAVE_CYRUS_SASL
1182 free( global_host );
1188 int slap_sasl_open( Connection *conn, int reopen )
1190 int cb, sc = LDAP_SUCCESS;
1191 #if SASL_VERSION_MAJOR >= 2
1192 char *ipremoteport = NULL, *iplocalport = NULL;
1195 #ifdef HAVE_CYRUS_SASL
1196 sasl_conn_t *ctx = NULL;
1197 sasl_callback_t *session_callbacks;
1199 assert( conn->c_sasl_authctx == NULL );
1202 assert( conn->c_sasl_extra == NULL );
1205 #if SASL_VERSION_MAJOR >= 2
1206 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1208 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1210 if( session_callbacks == NULL ) {
1212 LDAP_LOG( TRANSPORT, ERR,
1213 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1215 Debug( LDAP_DEBUG_ANY,
1216 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1220 conn->c_sasl_extra = session_callbacks;
1222 session_callbacks[cb=0].id = SASL_CB_LOG;
1223 session_callbacks[cb].proc = &slap_sasl_log;
1224 session_callbacks[cb++].context = conn;
1226 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1227 session_callbacks[cb].proc = &slap_sasl_authorize;
1228 session_callbacks[cb++].context = conn;
1230 #if SASL_VERSION_MAJOR >= 2
1231 session_callbacks[cb].id = SASL_CB_CANON_USER;
1232 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1233 session_callbacks[cb++].context = conn;
1235 /* XXXX: this should be conditional */
1236 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1237 session_callbacks[cb].proc = &slap_sasl_checkpass;
1238 session_callbacks[cb++].context = conn;
1241 session_callbacks[cb].id = SASL_CB_LIST_END;
1242 session_callbacks[cb].proc = NULL;
1243 session_callbacks[cb++].context = NULL;
1245 session_callbacks = conn->c_sasl_extra;
1248 conn->c_sasl_layers = 0;
1250 if( global_host == NULL ) {
1251 global_host = ldap_pvt_get_fqdn( NULL );
1254 /* create new SASL context */
1255 #if SASL_VERSION_MAJOR >= 2
1256 if ( conn->c_sock_name.bv_len != 0 &&
1257 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1260 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1261 /* Convert IPv6 addresses to address;port syntax. */
1262 p = strrchr( iplocalport, ' ' );
1263 /* Convert IPv4 addresses to address;port syntax. */
1264 if ( p == NULL ) p = strchr( iplocalport, ':' );
1269 if ( conn->c_peer_name.bv_len != 0 &&
1270 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1273 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1274 /* Convert IPv6 addresses to address;port syntax. */
1275 p = strrchr( ipremoteport, ' ' );
1276 /* Convert IPv4 addresses to address;port syntax. */
1277 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1282 sc = sasl_server_new( "ldap", global_host, global_realm,
1283 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1284 if ( iplocalport != NULL ) {
1285 ch_free( iplocalport );
1287 if ( ipremoteport != NULL ) {
1288 ch_free( ipremoteport );
1291 sc = sasl_server_new( "ldap", global_host, global_realm,
1292 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1295 if( sc != SASL_OK ) {
1297 LDAP_LOG( TRANSPORT, ERR,
1298 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1300 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1307 conn->c_sasl_authctx = ctx;
1309 if( sc == SASL_OK ) {
1310 sc = sasl_setprop( ctx,
1311 SASL_SEC_PROPS, &sasl_secprops );
1313 if( sc != SASL_OK ) {
1315 LDAP_LOG( TRANSPORT, ERR,
1316 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1318 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1322 slap_sasl_close( conn );
1327 sc = slap_sasl_err2ldap( sc );
1332 int slap_sasl_external(
1335 const char *auth_id )
1337 #if SASL_VERSION_MAJOR >= 2
1339 sasl_conn_t *ctx = conn->c_sasl_authctx;
1341 if ( ctx == NULL ) {
1342 return LDAP_UNAVAILABLE;
1345 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1347 if ( sc != SASL_OK ) {
1351 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1353 if ( sc != SASL_OK ) {
1357 #elif defined(HAVE_CYRUS_SASL)
1359 sasl_conn_t *ctx = conn->c_sasl_authctx;
1360 sasl_external_properties_t extprops;
1362 if ( ctx == NULL ) {
1363 return LDAP_UNAVAILABLE;
1366 memset( &extprops, '\0', sizeof(extprops) );
1368 extprops.auth_id = (char *) auth_id;
1370 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1371 (void *) &extprops );
1373 if ( sc != SASL_OK ) {
1378 return LDAP_SUCCESS;
1381 int slap_sasl_reset( Connection *conn )
1383 return LDAP_SUCCESS;
1386 char ** slap_sasl_mechs( Connection *conn )
1388 char **mechs = NULL;
1390 #ifdef HAVE_CYRUS_SASL
1391 sasl_conn_t *ctx = conn->c_sasl_authctx;
1393 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1397 SASL_CONST char *mechstr;
1399 sc = sasl_listmech( ctx,
1400 NULL, NULL, ",", NULL,
1401 &mechstr, NULL, NULL );
1403 if( sc != SASL_OK ) {
1405 LDAP_LOG( TRANSPORT, ERR,
1406 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1408 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1415 mechs = ldap_str2charray( mechstr, "," );
1417 #if SASL_VERSION_MAJOR < 2
1426 int slap_sasl_close( Connection *conn )
1428 #ifdef HAVE_CYRUS_SASL
1429 sasl_conn_t *ctx = conn->c_sasl_authctx;
1432 sasl_dispose( &ctx );
1434 if ( conn->c_sasl_sockctx && conn->c_sasl_authctx != conn->c_sasl_sockctx ) {
1435 ctx = conn->c_sasl_sockctx;
1436 sasl_dispose( &ctx );
1439 conn->c_sasl_authctx = NULL;
1440 conn->c_sasl_sockctx = NULL;
1441 conn->c_sasl_done = 0;
1443 free( conn->c_sasl_extra );
1444 conn->c_sasl_extra = NULL;
1447 return LDAP_SUCCESS;
1450 int slap_sasl_bind( Operation *op, SlapReply *rs )
1452 #ifdef HAVE_CYRUS_SASL
1453 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1454 struct berval response;
1455 unsigned reslen = 0;
1459 LDAP_LOG( TRANSPORT, ENTRY,
1460 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1461 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1462 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1463 op->o_conn->c_sasl_bind_mech.bv_val,
1464 op->orb_cred.bv_len );
1466 Debug(LDAP_DEBUG_ARGS,
1467 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1468 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1469 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1470 op->o_conn->c_sasl_bind_mech.bv_val,
1471 op->orb_cred.bv_len );
1476 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1477 "SASL unavailable on this session" );
1481 #if SASL_VERSION_MAJOR >= 2
1482 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1483 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1484 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1485 sasl_server_step( ctx, cred, clen, resp, rlen )
1487 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1488 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1489 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1490 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1493 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1494 /* If we already authenticated once, must use a new context */
1495 if ( op->o_conn->c_sasl_done ) {
1497 const char *authid = NULL;
1498 #if SASL_VERSION_MAJOR >= 2
1499 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1500 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1501 if ( authid ) authid = ch_strdup( authid );
1503 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1504 sasl_dispose( &ctx );
1506 op->o_conn->c_sasl_authctx = NULL;
1508 slap_sasl_open( op->o_conn, 1 );
1509 ctx = op->o_conn->c_sasl_authctx;
1510 #if SASL_VERSION_MAJOR >= 2
1512 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1513 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1514 ch_free( (char *)authid );
1519 op->o_conn->c_sasl_bind_mech.bv_val,
1520 op->orb_cred.bv_val, op->orb_cred.bv_len,
1521 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1525 op->orb_cred.bv_val, op->orb_cred.bv_len,
1526 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1529 response.bv_len = reslen;
1531 if ( sc == SASL_OK ) {
1532 sasl_ssf_t *ssf = NULL;
1534 op->orb_edn = op->o_conn->c_sasl_dn;
1535 op->o_conn->c_sasl_dn.bv_val = NULL;
1536 op->o_conn->c_sasl_dn.bv_len = 0;
1537 op->o_conn->c_sasl_done = 1;
1539 rs->sr_err = LDAP_SUCCESS;
1541 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1542 op->orb_ssf = ssf ? *ssf : 0;
1546 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1547 op->o_conn->c_sasl_layers++;
1549 /* If there's an old layer, set sockctx to NULL to
1550 * tell connection_read() to wait for us to finish.
1551 * Otherwise there is a race condition: we have to
1552 * send the Bind response using the old security
1553 * context and then remove it before reading any
1556 if ( op->o_conn->c_sasl_sockctx ) {
1557 ctx = op->o_conn->c_sasl_sockctx;
1558 op->o_conn->c_sasl_sockctx = NULL;
1560 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1562 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1565 /* Must send response using old security layer */
1566 if (response.bv_len) rs->sr_sasldata = &response;
1567 send_ldap_sasl( op, rs );
1569 /* Now dispose of the old security layer.
1572 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1573 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1574 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1575 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1576 sasl_dispose( &ctx );
1578 } else if ( sc == SASL_CONTINUE ) {
1579 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1580 rs->sr_sasldata = &response;
1581 send_ldap_sasl( op, rs );
1584 #if SASL_VERSION_MAJOR >= 2
1585 rs->sr_text = sasl_errdetail( ctx );
1587 rs->sr_err = slap_sasl_err2ldap( sc ),
1588 send_ldap_result( op, rs );
1591 #if SASL_VERSION_MAJOR < 2
1592 if( response.bv_len ) {
1593 ch_free( response.bv_val );
1598 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0 );
1600 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1605 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1606 "SASL not supported" );
1612 char* slap_sasl_secprops( const char *in )
1614 #ifdef HAVE_CYRUS_SASL
1615 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1617 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1619 return "SASL not supported";
1623 #ifdef HAVE_CYRUS_SASL
1625 slap_sasl_setpass( Operation *op, SlapReply *rs )
1627 struct berval id = { 0, NULL }; /* needs to come from connection */
1628 struct berval new = { 0, NULL };
1629 struct berval old = { 0, NULL };
1631 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1633 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1634 (SASL_CONST void **)&id.bv_val );
1636 if( rs->sr_err != SASL_OK ) {
1637 rs->sr_text = "unable to retrieve SASL username";
1638 rs->sr_err = LDAP_OTHER;
1643 LDAP_LOG( BACKEND, ENTRY,
1644 "slap_sasl_setpass: \"%s\"\n",
1645 id.bv_val ? id.bv_val : "", 0, 0);
1647 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1648 id.bv_val ? id.bv_val : "", 0, 0 );
1651 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1652 NULL, &old, &new, &rs->sr_text );
1654 if( rs->sr_err != LDAP_SUCCESS ) {
1658 if( new.bv_len == 0 ) {
1659 slap_passwd_generate(&new);
1661 if( new.bv_len == 0 ) {
1662 rs->sr_text = "password generation failed.";
1663 rs->sr_err = LDAP_OTHER;
1667 rs->sr_rspdata = slap_passwd_return( &new );
1670 #if SASL_VERSION_MAJOR < 2
1671 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1672 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1674 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1675 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1676 if( rs->sr_err != SASL_OK ) {
1677 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1680 switch(rs->sr_err) {
1682 rs->sr_err = LDAP_SUCCESS;
1692 rs->sr_err = LDAP_OTHER;
1698 #endif /* HAVE_CYRUS_SASL */
1700 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1701 * string returned in *dn is in its own allocated memory, and must be free'd
1702 * by the calling process. -Mark Adamson, Carnegie Mellon
1704 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1705 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1712 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
1714 int slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len,
1715 char *user_realm, struct berval *dn, int flags )
1718 int rc, is_dn = SET_NONE, do_norm = 1;
1724 LDAP_LOG( TRANSPORT, ENTRY,
1725 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1726 conn->c_connid, id ? (*id ? id : "<empty>") : "NULL", len );
1728 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s [len=%d]\n",
1729 id ? ( *id ? id : "<empty>" ) : "NULL", len, 0 );
1733 op = conn->c_sasl_bindop;
1740 if ( len == 0 ) len = strlen( id );
1742 /* Blatantly anonymous ID */
1743 if ( len == sizeof("anonymous") - 1 &&
1744 !strcasecmp( id, "anonymous" ) ) {
1745 return( LDAP_SUCCESS );
1751 /* An authcID needs to be converted to authzID form. Set the
1752 * values directly into *dn; they will be normalized later. (and
1753 * normalizing always makes a new copy.) An ID from a TLS certificate
1754 * is already normalized, so copy it and skip normalization.
1756 if( flags & SLAP_GETDN_AUTHCID ) {
1757 if( conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len &&
1758 strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
1760 /* EXTERNAL DNs are already normalized */
1763 ber_str2bv_x( id, len, 1, dn, op->o_tmpmemctx );
1766 /* convert to u:<username> form */
1772 if( is_dn == SET_NONE ) {
1773 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1777 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1784 /* No other possibilities from here */
1785 if( is_dn == SET_NONE ) {
1788 return( LDAP_INAPPROPRIATE_AUTH );
1791 /* Username strings */
1792 if( is_dn == SET_U ) {
1794 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
1796 /* username may have embedded realm name */
1797 if( ( realm = strchr( dn->bv_val, '@') ) ) {
1799 len += sizeof(",cn=")-2;
1800 } else if( user_realm && *user_realm ) {
1801 len += strlen( user_realm ) + sizeof(",cn=")-1;
1804 if( conn->c_sasl_bind_mech.bv_len ) {
1805 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
1808 /* Build the new dn */
1810 dn->bv_val = sl_malloc( len+1, op->o_tmpmemctx );
1811 if( dn->bv_val == NULL ) {
1813 LDAP_LOG( TRANSPORT, ERR,
1814 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1816 Debug( LDAP_DEBUG_ANY,
1817 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1821 p = lutil_strcopy( dn->bv_val, "uid=" );
1822 p = lutil_strncopy( p, c1, dn->bv_len );
1825 int rlen = dn->bv_len - ( realm - c1 );
1826 p = lutil_strcopy( p, ",cn=" );
1827 p = lutil_strncopy( p, realm, rlen );
1829 } else if( user_realm && *user_realm ) {
1830 p = lutil_strcopy( p, ",cn=" );
1831 p = lutil_strcopy( p, user_realm );
1834 if( conn->c_sasl_bind_mech.bv_len ) {
1835 p = lutil_strcopy( p, ",cn=" );
1836 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
1838 p = lutil_strcopy( p, ",cn=auth" );
1839 dn->bv_len = p - dn->bv_val;
1842 LDAP_LOG( TRANSPORT, ENTRY,
1843 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1845 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1849 /* Dup the DN in any case, so we don't risk
1850 * leaks or dangling pointers later,
1851 * and the DN value is '\0' terminated */
1852 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1853 dn->bv_val = dn2.bv_val;
1856 /* All strings are in DN form now. Normalize if needed. */
1858 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1860 /* User DNs were constructed above and must be freed now */
1861 sl_free( dn->bv_val, op->o_tmpmemctx );
1863 if ( rc != LDAP_SUCCESS ) {
1871 /* Run thru regexp */
1872 slap_sasl2dn( op, dn, &dn2 );
1874 sl_free( dn->bv_val, op->o_tmpmemctx );
1877 LDAP_LOG( TRANSPORT, ENTRY,
1878 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1880 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1885 return( LDAP_SUCCESS );