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;
65 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
67 int slap_sasl_config( int cargc, char **cargv, char *line,
68 const char *fname, int lineno )
70 /* set SASL proxy authorization policy */
71 if ( !strcasecmp( cargv[0], "authz-policy" ) ||
72 !strcasecmp( cargv[0], "sasl-authz-policy" ))
75 Debug( LDAP_DEBUG_ANY,
76 "%s: line %d: missing policy in"
77 " \"%s <policy>\" line\n",
78 cargv[0], fname, lineno );
82 if ( slap_sasl_setpolicy( cargv[1] ) ) {
83 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
84 "unable to parse value \"%s\" in \"authz-policy "
86 fname, lineno, cargv[1] );
90 } else if ( !strcasecmp( cargv[0], "authz-regexp" ) ||
91 !strcasecmp( cargv[0], "sasl-regexp" ) ||
92 !strcasecmp( cargv[0], "saslregexp" ) )
96 Debug( LDAP_DEBUG_ANY,
97 "%s: line %d: need 2 args in "
98 "\"authz-regexp <match> <replace>\"\n",
103 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
108 #ifdef HAVE_CYRUS_SASL
110 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
112 Debug( LDAP_DEBUG_ANY,
113 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
119 if ( global_host != NULL ) {
120 Debug( LDAP_DEBUG_ANY,
121 "%s: line %d: already set sasl-host!\n",
127 global_host = ch_strdup( cargv[1] );
131 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
133 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
134 "missing realm in \"sasl-realm <realm>\" line.\n",
140 if ( global_realm != NULL ) {
141 Debug( LDAP_DEBUG_ANY,
142 "%s: line %d: already set sasl-realm!\n",
148 global_realm = ch_strdup( cargv[1] );
151 /* SASL security properties */
152 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
156 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
157 "missing flags in \"sasl-secprops <properties>\" line\n",
163 txt = slap_sasl_secprops( cargv[1] );
165 Debug( LDAP_DEBUG_ANY,
166 "%s: line %d: sasl-secprops: %s\n",
167 fname, lineno, txt );
171 #endif /* HAVE_CYRUS_SASL */
177 #ifdef HAVE_CYRUS_SASL
185 Connection *conn = context;
189 if ( message == NULL ) {
190 return SASL_BADPARAM;
194 #if SASL_VERSION_MAJOR >= 2
196 level = LDAP_DEBUG_NONE;
200 level = LDAP_DEBUG_ANY;
204 level = LDAP_DEBUG_ANY;
208 level = LDAP_DEBUG_TRACE;
212 level = LDAP_DEBUG_TRACE;
216 level = LDAP_DEBUG_TRACE;
220 level = LDAP_DEBUG_TRACE;
224 level = LDAP_DEBUG_TRACE;
225 label = "Password Trace";
229 level = LDAP_DEBUG_ANY;
232 case SASL_LOG_WARNING:
233 level = LDAP_DEBUG_TRACE;
237 level = LDAP_DEBUG_TRACE;
242 return SASL_BADPARAM;
245 Debug( level, "SASL [conn=%ld] %s: %s\n",
246 conn ? conn->c_connid: -1,
254 #if SASL_VERSION_MAJOR >= 2
255 static const char *slap_propnames[] = {
256 "*slapConn", "*slapAuthcDN", "*slapAuthzDN", NULL };
258 static Filter generic_filter = { LDAP_FILTER_PRESENT };
259 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
264 #define PROP_COUNT 3 /* Number of properties we used */
266 typedef struct lookup_info {
268 const struct propval *list;
269 sasl_server_params_t *sparams;
272 static slap_response sasl_ap_lookup;
275 sasl_ap_lookup( Operation *op, SlapReply *rs )
278 AttributeDescription *ad;
282 slap_callback *tmp = op->o_callback;
283 lookup_info *sl = tmp->sc_private;
285 if (rs->sr_type != REP_SEARCH) return 0;
287 for( i = 0; sl->list[i].name; i++ ) {
288 const char *name = sl->list[i].name;
290 if ( name[0] == '*' ) {
291 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
292 /* Skip our private properties */
293 if ( !strcmp( name, slap_propnames[0] )) {
298 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
301 if ( sl->list[i].values ) {
302 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
305 rc = slap_str2ad( name, &ad, &text );
306 if ( rc != LDAP_SUCCESS ) {
307 Debug( LDAP_DEBUG_TRACE,
308 "slap_ap_lookup: str2ad(%s): %s\n", name, text, 0 );
311 a = attr_find( rs->sr_entry->e_attrs, ad );
313 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
316 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
317 sl->sparams->utils->prop_erase( sl->sparams->propctx,
320 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
321 sl->sparams->utils->prop_set( sl->sparams->propctx,
322 sl->list[i].name, bv->bv_val, bv->bv_len );
331 sasl_server_params_t *sparams,
338 Connection *conn = NULL;
341 sl.list = sparams->utils->prop_get( sparams->propctx );
342 sl.sparams = sparams;
345 /* Find our DN and conn first */
346 for( i = 0; sl.list[i].name; i++ ) {
347 if ( sl.list[i].name[0] == '*' ) {
348 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
349 if ( sl.list[i].values && sl.list[i].values[0] )
350 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
353 if ( (flags & SASL_AUXPROP_AUTHZID) &&
354 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
356 if ( sl.list[i].values && sl.list[i].values[0] )
357 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
360 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
361 if ( sl.list[i].values && sl.list[i].values[0] ) {
362 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
363 if ( !(flags & SASL_AUXPROP_AUTHZID) )
370 /* Now see what else needs to be fetched */
371 for( i = 0; sl.list[i].name; i++ ) {
372 const char *name = sl.list[i].name;
374 if ( name[0] == '*' ) {
375 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
376 /* Skip our private properties */
377 if ( !strcmp( name, slap_propnames[0] )) {
382 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
385 if ( sl.list[i].values ) {
386 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
393 slap_callback cb = { NULL, sasl_ap_lookup, NULL, NULL };
397 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
399 if ( op.o_bd && op.o_bd->be_search ) {
400 SlapReply rs = {REP_RESULT};
401 op.o_hdr = conn->c_sasl_bindop->o_hdr;
402 op.o_tag = LDAP_REQ_SEARCH;
403 op.o_ndn = conn->c_ndn;
405 op.o_time = slap_get_time();
406 op.o_do_not_cache = 1;
407 op.o_is_auth_check = 1;
408 op.o_req_dn = op.o_req_ndn;
409 op.ors_scope = LDAP_SCOPE_BASE;
410 op.ors_deref = LDAP_DEREF_NEVER;
411 op.ors_tlimit = SLAP_NO_LIMIT;
413 op.ors_filter = &generic_filter;
414 op.ors_filterstr = generic_filterstr;
415 /* FIXME: we want all attributes, right? */
418 op.o_bd->be_search( &op, &rs );
423 #if SASL_VERSION_FULL >= 0x020110
427 sasl_server_params_t *sparams,
428 struct propctx *prctx,
433 SlapReply rs = {REP_RESULT};
435 Connection *conn = NULL;
436 const struct propval *pr;
437 Modifications *modlist = NULL, **modtail = &modlist, *mod;
438 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
439 char textbuf[SLAP_TEXT_BUFLEN];
441 size_t textlen = sizeof(textbuf);
443 /* just checking if we are enabled */
444 if (!prctx) return SASL_OK;
446 if (!sparams || !user) return SASL_BADPARAM;
448 pr = sparams->utils->prop_get( sparams->propctx );
450 /* Find our DN and conn first */
451 for( i = 0; pr[i].name; i++ ) {
452 if ( pr[i].name[0] == '*' ) {
453 if ( !strcmp( pr[i].name, slap_propnames[PROP_CONN] ) ) {
454 if ( pr[i].values && pr[i].values[0] )
455 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
458 if ( !strcmp( pr[i].name, slap_propnames[PROP_AUTHC] ) ) {
459 if ( pr[i].values && pr[i].values[0] ) {
460 AC_MEMCPY( &op.o_req_ndn, pr[i].values[0], sizeof( struct berval ) );
465 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
467 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
469 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
471 pr = sparams->utils->prop_get( prctx );
472 if (!pr) return SASL_BADPARAM;
474 for (i=0; pr[i].name; i++);
475 if (!i) return SASL_BADPARAM;
477 for (i=0; pr[i].name; i++) {
478 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
479 mod->sml_op = LDAP_MOD_REPLACE;
480 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
481 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
482 sizeof(struct berval));
483 for (j=0; j<pr[i].nvalues; j++) {
484 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
486 BER_BVZERO( &mod->sml_values[j] );
487 mod->sml_nvalues = NULL;
488 mod->sml_desc = NULL;
490 modtail = &mod->sml_next;
494 rc = slap_mods_check( modlist, &text, textbuf, textlen, NULL );
496 if ( rc == LDAP_SUCCESS ) {
497 rc = slap_mods_no_update_check( modlist, &text,
500 if ( rc == LDAP_SUCCESS ) {
501 rc = slap_mods_opattrs( &op, modlist, modtail,
502 &text, textbuf, textlen, 1 );
504 if ( rc == LDAP_SUCCESS ) {
505 op.o_hdr = conn->c_sasl_bindop->o_hdr;
506 op.o_tag = LDAP_REQ_MODIFY;
507 op.o_ndn = op.o_req_ndn;
509 op.o_time = slap_get_time();
510 op.o_do_not_cache = 1;
511 op.o_is_auth_check = 1;
512 op.o_req_dn = op.o_req_ndn;
513 op.orm_modlist = modlist;
515 rc = op.o_bd->be_modify( &op, &rs );
519 slap_mods_free( modlist );
520 return rc != LDAP_SUCCESS ? SASL_FAIL : SASL_OK;
522 #endif /* SASL_VERSION_FULL >= 2.1.16 */
524 static sasl_auxprop_plug_t slap_auxprop_plugin = {
527 NULL, /* glob_context */
528 NULL, /* auxprop_free */
531 #if SASL_VERSION_FULL >= 0x020110
532 slap_auxprop_store /* the declaration of this member changed
533 * in cyrus SASL from 2.1.15 to 2.1.16 */
541 const sasl_utils_t *utils,
544 sasl_auxprop_plug_t **plug,
545 const char *plugname)
547 if ( !out_version || !plug ) return SASL_BADPARAM;
549 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
551 *out_version = SASL_AUXPROP_PLUG_VERSION;
552 *plug = &slap_auxprop_plugin;
556 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
557 * auxiliary property, so that we can refer to it in sasl_authorize
558 * without interfering with anything else. Also, the SASL username
559 * buffer is constrained to 256 characters, and our DNs could be
560 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
563 slap_sasl_canonicalize(
569 const char *user_realm,
574 Connection *conn = (Connection *)context;
575 struct propctx *props = sasl_auxprop_getctx( sconn );
576 struct propval auxvals[3];
579 const char *names[2];
584 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
585 conn ? conn->c_connid : -1,
586 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
587 in ? in : "<empty>");
589 /* If name is too big, just truncate. We don't care, we're
590 * using DNs, not the usernames.
592 if ( inlen > out_max )
595 /* This is a Simple Bind using SPASSWD. That means the in-directory
596 * userPassword of the Binding user already points at SASL, so it
597 * cannot be used to actually satisfy a password comparison. Just
598 * ignore it, some other mech will process it.
600 if ( !conn->c_sasl_bindop ||
601 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
603 /* See if we need to add request, can only do it once */
604 prop_getnames( props, slap_propnames, auxvals );
605 if ( !auxvals[0].name )
606 prop_request( props, slap_propnames );
608 if ( flags & SASL_CU_AUTHID )
613 /* Need to store the Connection for auxprop_lookup */
614 if ( !auxvals[PROP_CONN].values ) {
615 names[0] = slap_propnames[PROP_CONN];
617 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
620 /* Already been here? */
621 if ( auxvals[which].values )
624 /* Normally we require an authzID to have a u: or dn: prefix.
625 * However, SASL frequently gives us an authzID that is just
626 * an exact copy of the authcID, without a prefix. We need to
627 * detect and allow this condition. If SASL calls canonicalize
628 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
629 * But if it's broken into two calls, we need to remember the
630 * authcID so that we can compare the authzID later. We store
631 * the authcID temporarily in conn->c_sasl_dn. We necessarily
632 * finish Canonicalizing before Authorizing, so there is no
633 * conflict with slap_sasl_authorize's use of this temp var.
635 * The SASL EXTERNAL mech is backwards from all the other mechs,
636 * it does authzID before the authcID. If we see that authzID
637 * has already been done, don't do anything special with authcID.
639 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
640 conn->c_sasl_dn.bv_val = (char *) in;
641 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
642 rc = strcmp( in, conn->c_sasl_dn.bv_val );
643 conn->c_sasl_dn.bv_val = NULL;
644 /* They were equal, no work needed */
645 if ( !rc ) goto done;
648 bvin.bv_val = (char *)in;
650 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
651 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
652 if ( rc != LDAP_SUCCESS ) {
653 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
657 names[0] = slap_propnames[which];
660 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
662 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
663 conn ? conn->c_connid : -1, names[0]+1,
664 dn.bv_val ? dn.bv_val : "<EMPTY>" );
667 AC_MEMCPY( out, in, inlen );
679 char *requested_user,
683 const char *def_realm,
685 struct propctx *props)
687 Connection *conn = (Connection *)context;
688 struct propval auxvals[3];
689 struct berval authcDN, authzDN;
692 /* Simple Binds don't support proxy authorization, ignore it */
693 if ( !conn->c_sasl_bindop ||
694 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
696 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
697 "authcid=\"%s\" authzid=\"%s\"\n",
698 conn ? conn->c_connid : -1, auth_identity, requested_user );
699 if ( conn->c_sasl_dn.bv_val ) {
700 ch_free( conn->c_sasl_dn.bv_val );
701 BER_BVZERO( &conn->c_sasl_dn );
705 prop_getnames( props, slap_propnames+1, auxvals );
707 /* Should not happen */
708 if ( !auxvals[0].values ) {
709 sasl_seterror( sconn, 0, "invalid authcid" );
713 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
715 /* Nothing to do if no authzID was given */
716 if ( !auxvals[1].name || !auxvals[1].values ) {
717 conn->c_sasl_dn = authcDN;
721 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
723 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
724 ch_free( authcDN.bv_val );
725 if ( rc != LDAP_SUCCESS ) {
726 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
727 "proxy authorization disallowed (%d)\n",
728 (long) (conn ? conn->c_connid : -1), rc, 0 );
730 sasl_seterror( sconn, 0, "not authorized" );
731 ch_free( authzDN.bv_val );
735 conn->c_sasl_dn = authzDN;
737 if (conn->c_sasl_bindop) {
738 Statslog( LDAP_DEBUG_STATS,
739 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
740 conn->c_connid, conn->c_sasl_bindop->o_opid,
741 auth_identity, 0, 0);
744 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
745 " proxy authorization allowed\n",
746 (long) (conn ? conn->c_connid : -1), 0, 0 );
758 struct berval authcDN, authzDN;
760 Connection *conn = context;
762 struct berval bvauthcid, bvauthzid;
765 if ( conn->c_sasl_dn.bv_val ) {
766 ch_free( conn->c_sasl_dn.bv_val );
767 BER_BVZERO( &conn->c_sasl_dn );
770 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
771 "authcid=\"%s\" authzid=\"%s\"\n",
772 (long) (conn ? conn->c_connid : -1),
773 authcid ? authcid : "<empty>",
774 authzid ? authzid : "<empty>" );
776 /* Figure out how much data we have for the dn */
777 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
778 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
779 Debug(LDAP_DEBUG_TRACE,
780 "authorize: getprop(REALM) failed!\n", 0,0,0);
781 *errstr = "Could not extract realm";
785 /* Convert the identities to DN's. If no authzid was given, client will
786 be bound as the DN matching their username */
787 bvauthcid.bv_val = authcid;
788 bvauthcid.bv_len = authcid ? strlen( authcid ) : 0;
789 rc = slap_sasl_getdn( conn, NULL, &bvauthcid, realm,
790 &authcDN, SLAP_GETDN_AUTHCID );
791 if( rc != LDAP_SUCCESS ) {
792 *errstr = ldap_err2string( rc );
795 if( ( authzid == NULL ) || !strcmp( authcid, authzid ) ) {
796 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
797 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
799 conn->c_sasl_dn = authcDN;
803 bvauthzid.bv_val = authzid;
804 bvauthzid.bv_len = authzid ? strlen( authzid ) : 0;
805 rc = slap_sasl_getdn( conn, NULL, &bvauthzid, realm,
806 &authzDN, SLAP_GETDN_AUTHZID );
807 if( rc != LDAP_SUCCESS ) {
808 ch_free( authcDN.bv_val );
809 *errstr = ldap_err2string( rc );
813 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
814 ch_free( authcDN.bv_val );
816 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
817 "proxy authorization disallowed (%d)\n",
818 (long) (conn ? conn->c_connid : -1), rc, 0 );
820 *errstr = "not authorized";
821 ch_free( authzDN.bv_val );
824 conn->c_sasl_dn = authzDN;
827 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
828 " authorization allowed\n",
829 (long) (conn ? conn->c_connid : -1), 0, 0 );
831 if (conn->c_sasl_bindop) {
832 Statslog( LDAP_DEBUG_STATS,
833 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
834 conn->c_connid, conn->c_sasl_bindop->o_opid,
841 #endif /* SASL_VERSION_MAJOR >= 2 */
844 slap_sasl_err2ldap( int saslerr )
853 rc = LDAP_SASL_BIND_IN_PROGRESS;
862 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
865 rc = LDAP_INVALID_CREDENTIALS;
868 rc = LDAP_INSUFFICIENT_ACCESS;
872 rc = LDAP_INAPPROPRIATE_AUTH;
883 int slap_sasl_init( void )
885 #ifdef HAVE_CYRUS_SASL
887 static sasl_callback_t server_callbacks[] = {
888 { SASL_CB_LOG, &slap_sasl_log, NULL },
889 { SASL_CB_LIST_END, NULL, NULL }
892 #ifdef HAVE_SASL_VERSION
893 /* stringify the version number, sasl.h doesn't do it for us */
894 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
895 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
896 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
899 sasl_version( NULL, &rc );
900 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
901 (rc & 0xffff) < SASL_VERSION_STEP)
903 char version[sizeof("xxx.xxx.xxxxx")];
904 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
906 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
907 " expected " SASL_VERSION_STRING ","
908 " got %s\n", version, 0, 0 );
913 /* SASL 2 does its own memory management internally */
914 #if SASL_VERSION_MAJOR < 2
923 ldap_pvt_sasl_mutex_new,
924 ldap_pvt_sasl_mutex_lock,
925 ldap_pvt_sasl_mutex_unlock,
926 ldap_pvt_sasl_mutex_dispose );
928 #if SASL_VERSION_MAJOR >= 2
929 generic_filter.f_desc = slap_schema.si_ad_objectClass;
931 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
932 if( rc != SASL_OK ) {
933 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
938 /* should provide callbacks for logging */
939 /* server name should be configurable */
940 rc = sasl_server_init( server_callbacks, "slapd" );
942 if( rc != SASL_OK ) {
943 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
945 #if SASL_VERSION_MAJOR < 2
946 /* A no-op used to make sure we linked with Cyrus 1.5 */
947 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
953 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
956 /* default security properties */
957 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
958 sasl_secprops.max_ssf = INT_MAX;
959 sasl_secprops.maxbufsize = 65536;
960 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
966 int slap_sasl_destroy( void )
968 #ifdef HAVE_CYRUS_SASL
977 int slap_sasl_open( Connection *conn, int reopen )
979 int sc = LDAP_SUCCESS;
980 #ifdef HAVE_CYRUS_SASL
983 sasl_conn_t *ctx = NULL;
984 sasl_callback_t *session_callbacks;
986 #if SASL_VERSION_MAJOR >= 2
987 char *ipremoteport = NULL, *iplocalport = NULL;
990 assert( conn->c_sasl_authctx == NULL );
993 assert( conn->c_sasl_extra == NULL );
996 #if SASL_VERSION_MAJOR >= 2
997 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
999 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1001 if( session_callbacks == NULL ) {
1002 Debug( LDAP_DEBUG_ANY,
1003 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1006 conn->c_sasl_extra = session_callbacks;
1008 session_callbacks[cb=0].id = SASL_CB_LOG;
1009 session_callbacks[cb].proc = &slap_sasl_log;
1010 session_callbacks[cb++].context = conn;
1012 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1013 session_callbacks[cb].proc = &slap_sasl_authorize;
1014 session_callbacks[cb++].context = conn;
1016 #if SASL_VERSION_MAJOR >= 2
1017 session_callbacks[cb].id = SASL_CB_CANON_USER;
1018 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1019 session_callbacks[cb++].context = conn;
1022 session_callbacks[cb].id = SASL_CB_LIST_END;
1023 session_callbacks[cb].proc = NULL;
1024 session_callbacks[cb++].context = NULL;
1026 session_callbacks = conn->c_sasl_extra;
1029 conn->c_sasl_layers = 0;
1031 if( global_host == NULL ) {
1032 global_host = ldap_pvt_get_fqdn( NULL );
1035 /* create new SASL context */
1036 #if SASL_VERSION_MAJOR >= 2
1037 if ( conn->c_sock_name.bv_len != 0 &&
1038 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1041 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1042 /* Convert IPv6 addresses to address;port syntax. */
1043 p = strrchr( iplocalport, ' ' );
1044 /* Convert IPv4 addresses to address;port syntax. */
1045 if ( p == NULL ) p = strchr( iplocalport, ':' );
1050 if ( conn->c_peer_name.bv_len != 0 &&
1051 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1054 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1055 /* Convert IPv6 addresses to address;port syntax. */
1056 p = strrchr( ipremoteport, ' ' );
1057 /* Convert IPv4 addresses to address;port syntax. */
1058 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1063 sc = sasl_server_new( "ldap", global_host, global_realm,
1064 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1065 if ( iplocalport != NULL ) {
1066 ch_free( iplocalport );
1068 if ( ipremoteport != NULL ) {
1069 ch_free( ipremoteport );
1072 sc = sasl_server_new( "ldap", global_host, global_realm,
1073 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1076 if( sc != SASL_OK ) {
1077 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1083 conn->c_sasl_authctx = ctx;
1085 if( sc == SASL_OK ) {
1086 sc = sasl_setprop( ctx,
1087 SASL_SEC_PROPS, &sasl_secprops );
1089 if( sc != SASL_OK ) {
1090 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1093 slap_sasl_close( conn );
1098 sc = slap_sasl_err2ldap( sc );
1100 #elif defined(SLAP_BUILTIN_SASL)
1101 /* built-in SASL implementation */
1102 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1103 if( ctx == NULL ) return -1;
1105 ctx->sc_external_ssf = 0;
1106 BER_BVZERO( &ctx->sc_external_id );
1108 conn->c_sasl_authctx = ctx;
1114 int slap_sasl_external(
1117 struct berval *auth_id )
1119 #if SASL_VERSION_MAJOR >= 2
1121 sasl_conn_t *ctx = conn->c_sasl_authctx;
1123 if ( ctx == NULL ) {
1124 return LDAP_UNAVAILABLE;
1127 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1129 if ( sc != SASL_OK ) {
1133 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1134 auth_id ? auth_id->bv_val : NULL );
1136 if ( sc != SASL_OK ) {
1140 #elif defined(HAVE_CYRUS_SASL)
1142 sasl_conn_t *ctx = conn->c_sasl_authctx;
1143 sasl_external_properties_t extprops;
1145 if ( ctx == NULL ) {
1146 return LDAP_UNAVAILABLE;
1149 memset( &extprops, '\0', sizeof(extprops) );
1151 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1153 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1154 (void *) &extprops );
1156 if ( sc != SASL_OK ) {
1159 #elif defined(SLAP_BUILTIN_SASL)
1160 /* built-in SASL implementation */
1161 SASL_CTX *ctx = conn->c_sasl_authctx;
1162 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1164 ctx->sc_external_ssf = ssf;
1166 ctx->sc_external_id = *auth_id;
1167 BER_BVZERO( auth_id );
1169 BER_BVZERO( &ctx->sc_external_id );
1173 return LDAP_SUCCESS;
1176 int slap_sasl_reset( Connection *conn )
1178 return LDAP_SUCCESS;
1181 char ** slap_sasl_mechs( Connection *conn )
1183 char **mechs = NULL;
1185 #ifdef HAVE_CYRUS_SASL
1186 sasl_conn_t *ctx = conn->c_sasl_authctx;
1188 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1192 SASL_CONST char *mechstr;
1194 sc = sasl_listmech( ctx,
1195 NULL, NULL, ",", NULL,
1196 &mechstr, NULL, NULL );
1198 if( sc != SASL_OK ) {
1199 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1205 mechs = ldap_str2charray( mechstr, "," );
1207 #if SASL_VERSION_MAJOR < 2
1211 #elif defined(SLAP_BUILTIN_SASL)
1212 /* builtin SASL implementation */
1213 SASL_CTX *ctx = conn->c_sasl_authctx;
1214 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1215 /* should check ssf */
1216 mechs = ldap_str2charray( "EXTERNAL", "," );
1223 int slap_sasl_close( Connection *conn )
1225 #ifdef HAVE_CYRUS_SASL
1226 sasl_conn_t *ctx = conn->c_sasl_authctx;
1229 sasl_dispose( &ctx );
1231 if ( conn->c_sasl_sockctx &&
1232 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1234 ctx = conn->c_sasl_sockctx;
1235 sasl_dispose( &ctx );
1238 conn->c_sasl_authctx = NULL;
1239 conn->c_sasl_sockctx = NULL;
1240 conn->c_sasl_done = 0;
1242 free( conn->c_sasl_extra );
1243 conn->c_sasl_extra = NULL;
1245 #elif defined(SLAP_BUILTIN_SASL)
1246 SASL_CTX *ctx = conn->c_sasl_authctx;
1248 if( ctx->sc_external_id.bv_val ) {
1249 free( ctx->sc_external_id.bv_val );
1250 BER_BVZERO( &ctx->sc_external_id );
1253 conn->c_sasl_authctx = NULL;
1257 return LDAP_SUCCESS;
1260 int slap_sasl_bind( Operation *op, SlapReply *rs )
1262 #ifdef HAVE_CYRUS_SASL
1263 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1264 struct berval response;
1265 unsigned reslen = 0;
1268 Debug(LDAP_DEBUG_ARGS,
1269 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1270 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1271 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1272 op->o_conn->c_sasl_bind_mech.bv_val,
1273 op->orb_cred.bv_len );
1276 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1277 "SASL unavailable on this session" );
1281 #if SASL_VERSION_MAJOR >= 2
1282 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1283 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1284 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1285 sasl_server_step( ctx, cred, clen, resp, rlen )
1287 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1288 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1289 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1290 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1293 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1294 /* If we already authenticated once, must use a new context */
1295 if ( op->o_conn->c_sasl_done ) {
1297 const char *authid = NULL;
1298 #if SASL_VERSION_MAJOR >= 2
1299 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1300 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1301 if ( authid ) authid = ch_strdup( authid );
1303 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1304 sasl_dispose( &ctx );
1306 op->o_conn->c_sasl_authctx = NULL;
1308 slap_sasl_open( op->o_conn, 1 );
1309 ctx = op->o_conn->c_sasl_authctx;
1310 #if SASL_VERSION_MAJOR >= 2
1312 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1313 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1314 ch_free( (char *)authid );
1319 op->o_conn->c_sasl_bind_mech.bv_val,
1320 op->orb_cred.bv_val, op->orb_cred.bv_len,
1321 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1325 op->orb_cred.bv_val, op->orb_cred.bv_len,
1326 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1329 response.bv_len = reslen;
1331 if ( sc == SASL_OK ) {
1332 sasl_ssf_t *ssf = NULL;
1334 op->orb_edn = op->o_conn->c_sasl_dn;
1335 BER_BVZERO( &op->o_conn->c_sasl_dn );
1336 op->o_conn->c_sasl_done = 1;
1338 rs->sr_err = LDAP_SUCCESS;
1340 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1341 op->orb_ssf = ssf ? *ssf : 0;
1345 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1346 op->o_conn->c_sasl_layers++;
1348 /* If there's an old layer, set sockctx to NULL to
1349 * tell connection_read() to wait for us to finish.
1350 * Otherwise there is a race condition: we have to
1351 * send the Bind response using the old security
1352 * context and then remove it before reading any
1355 if ( op->o_conn->c_sasl_sockctx ) {
1356 ctx = op->o_conn->c_sasl_sockctx;
1357 op->o_conn->c_sasl_sockctx = NULL;
1359 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1361 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1364 /* Must send response using old security layer */
1365 if (response.bv_len) rs->sr_sasldata = &response;
1366 send_ldap_sasl( op, rs );
1368 /* Now dispose of the old security layer.
1371 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1372 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1373 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1374 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1375 sasl_dispose( &ctx );
1377 } else if ( sc == SASL_CONTINUE ) {
1378 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1379 rs->sr_sasldata = &response;
1380 send_ldap_sasl( op, rs );
1383 #if SASL_VERSION_MAJOR >= 2
1384 rs->sr_text = sasl_errdetail( ctx );
1386 rs->sr_err = slap_sasl_err2ldap( sc ),
1387 send_ldap_result( op, rs );
1390 #if SASL_VERSION_MAJOR < 2
1391 if( response.bv_len ) {
1392 ch_free( response.bv_val );
1396 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1398 #elif defined(SLAP_BUILTIN_SASL)
1399 /* built-in SASL implementation */
1400 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1402 if ( ctx == NULL ) {
1403 send_ldap_error( op, rs, LDAP_OTHER,
1404 "Internal SASL Error" );
1406 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1409 if( op->orb_cred.bv_len ) {
1410 rs->sr_text = "proxy authorization not support";
1411 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1412 send_ldap_result( op, rs );
1415 op->orb_edn = ctx->sc_external_id;
1416 rs->sr_err = LDAP_SUCCESS;
1417 rs->sr_sasldata = NULL;
1418 send_ldap_sasl( op, rs );
1422 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1423 "requested SASL mechanism not supported" );
1426 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1427 "SASL not supported" );
1433 char* slap_sasl_secprops( const char *in )
1435 #ifdef HAVE_CYRUS_SASL
1436 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1438 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1440 return "SASL not supported";
1444 #ifdef HAVE_CYRUS_SASL
1446 slap_sasl_setpass( Operation *op, SlapReply *rs )
1448 struct berval id = BER_BVNULL; /* needs to come from connection */
1449 struct berval new = BER_BVNULL;
1450 struct berval old = BER_BVNULL;
1452 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1454 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1455 (SASL_CONST void **)&id.bv_val );
1457 if( rs->sr_err != SASL_OK ) {
1458 rs->sr_text = "unable to retrieve SASL username";
1459 rs->sr_err = LDAP_OTHER;
1463 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1464 id.bv_val ? id.bv_val : "", 0, 0 );
1466 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1467 NULL, &old, &new, &rs->sr_text );
1469 if( rs->sr_err != LDAP_SUCCESS ) {
1473 if( new.bv_len == 0 ) {
1474 slap_passwd_generate(&new);
1476 if( new.bv_len == 0 ) {
1477 rs->sr_text = "password generation failed.";
1478 rs->sr_err = LDAP_OTHER;
1482 rs->sr_rspdata = slap_passwd_return( &new );
1485 #if SASL_VERSION_MAJOR < 2
1486 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1487 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1489 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1490 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1491 if( rs->sr_err != SASL_OK ) {
1492 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1495 switch(rs->sr_err) {
1497 rs->sr_err = LDAP_SUCCESS;
1507 rs->sr_err = LDAP_OTHER;
1513 #endif /* HAVE_CYRUS_SASL */
1515 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1516 * string returned in *dn is in its own allocated memory, and must be free'd
1517 * by the calling process. -Mark Adamson, Carnegie Mellon
1519 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1520 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1527 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1528 char *user_realm, struct berval *dn, int flags )
1530 int rc, is_dn = SET_NONE, do_norm = 1;
1531 struct berval dn2, *mech;
1536 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1538 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1539 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 : id->bv_len ) );
1542 op = conn->c_sasl_bindop;
1547 if ( !BER_BVISNULL( id ) ) {
1548 /* Blatantly anonymous ID */
1549 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1551 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1552 return( LDAP_SUCCESS );
1556 /* FIXME: if empty, should we stop? */
1557 BER_BVSTR( id, "" );
1560 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1561 mech = &conn->c_sasl_bind_mech;
1563 mech = &conn->c_authmech;
1566 /* An authcID needs to be converted to authzID form. Set the
1567 * values directly into *dn; they will be normalized later. (and
1568 * normalizing always makes a new copy.) An ID from a TLS certificate
1569 * is already normalized, so copy it and skip normalization.
1571 if( flags & SLAP_GETDN_AUTHCID ) {
1572 if( bvmatch( mech, &ext_bv )) {
1573 /* EXTERNAL DNs are already normalized */
1574 assert( !BER_BVISNULL( id ) );
1578 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1581 /* convert to u:<username> form */
1587 if( is_dn == SET_NONE ) {
1588 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1590 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1591 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1593 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1595 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1596 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1600 /* No other possibilities from here */
1601 if( is_dn == SET_NONE ) {
1603 return( LDAP_INAPPROPRIATE_AUTH );
1606 /* Username strings */
1607 if( is_dn == SET_U ) {
1608 /* ITS#3419: values may need escape */
1610 LDAPAVA *RDNs[ 4 ][ 2 ];
1615 DN[ irdn ] = RDNs[ irdn ];
1616 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1617 AVAs[ irdn ].la_attr = slap_schema.si_ad_uid->ad_cname;
1618 AVAs[ irdn ].la_value = *dn;
1619 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1620 AVAs[ irdn ].la_private = NULL;
1621 RDNs[ irdn ][ 1 ] = NULL;
1623 if ( user_realm && *user_realm ) {
1625 DN[ irdn ] = RDNs[ irdn ];
1626 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1627 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1628 ber_str2bv( user_realm, 0, 0, &AVAs[ irdn ].la_value );
1629 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1630 AVAs[ irdn ].la_private = NULL;
1631 RDNs[ irdn ][ 1 ] = NULL;
1634 if ( !BER_BVISNULL( mech ) ) {
1636 DN[ irdn ] = RDNs[ irdn ];
1637 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1638 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1639 AVAs[ irdn ].la_value = *mech;
1640 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1641 AVAs[ irdn ].la_private = NULL;
1642 RDNs[ irdn ][ 1 ] = NULL;
1646 DN[ irdn ] = RDNs[ irdn ];
1647 RDNs[ irdn ][ 0 ] = &AVAs[ irdn ];
1648 AVAs[ irdn ].la_attr = slap_schema.si_ad_cn->ad_cname;
1649 BER_BVSTR( &AVAs[ irdn ].la_value, "auth" );
1650 AVAs[ irdn ].la_flags = LDAP_AVA_NULL;
1651 AVAs[ irdn ].la_private = NULL;
1652 RDNs[ irdn ][ 1 ] = NULL;
1657 rc = ldap_dn2bv_x( DN, dn, LDAP_DN_FORMAT_LDAPV3, op->o_tmpmemctx );
1658 if ( rc != LDAP_SUCCESS ) {
1663 Debug( LDAP_DEBUG_TRACE, "slap_sasl_getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1667 /* Dup the DN in any case, so we don't risk
1668 * leaks or dangling pointers later,
1669 * and the DN value is '\0' terminated */
1670 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1671 dn->bv_val = dn2.bv_val;
1674 /* All strings are in DN form now. Normalize if needed. */
1676 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1678 /* User DNs were constructed above and must be freed now */
1679 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1681 if ( rc != LDAP_SUCCESS ) {
1688 /* Run thru regexp */
1689 slap_sasl2dn( op, dn, &dn2, flags );
1690 if( !BER_BVISNULL( &dn2 ) ) {
1691 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1693 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1697 return( LDAP_SUCCESS );