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;
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_tag = LDAP_REQ_SEARCH;
402 op.o_protocol = LDAP_VERSION3;
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_threadctx = conn->c_sasl_bindop->o_threadctx;
409 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
410 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
412 op.o_connid = conn->c_connid;
413 op.o_req_dn = op.o_req_ndn;
414 op.ors_scope = LDAP_SCOPE_BASE;
415 op.ors_deref = LDAP_DEREF_NEVER;
416 op.ors_tlimit = SLAP_NO_LIMIT;
418 op.ors_filter = &generic_filter;
419 op.ors_filterstr = generic_filterstr;
420 /* FIXME: we want all attributes, right? */
423 op.o_bd->be_search( &op, &rs );
428 #if SASL_VERSION_FULL >= 0x020110
432 sasl_server_params_t *sparams,
433 struct propctx *prctx,
438 SlapReply rs = {REP_RESULT};
440 Connection *conn = NULL;
441 const struct propval *pr;
442 Modifications *modlist = NULL, **modtail = &modlist, *mod;
443 slap_callback cb = { NULL, slap_null_cb, NULL, NULL };
444 char textbuf[SLAP_TEXT_BUFLEN];
446 size_t textlen = sizeof(textbuf);
448 /* just checking if we are enabled */
449 if (!prctx) return SASL_OK;
451 if (!sparams || !user) return SASL_BADPARAM;
453 pr = sparams->utils->prop_get( sparams->propctx );
455 /* Find our DN and conn first */
456 for( i = 0; pr[i].name; i++ ) {
457 if ( pr[i].name[0] == '*' ) {
458 if ( !strcmp( pr[i].name, slap_propnames[PROP_CONN] ) ) {
459 if ( pr[i].values && pr[i].values[0] )
460 AC_MEMCPY( &conn, pr[i].values[0], sizeof( conn ) );
463 if ( !strcmp( pr[i].name, slap_propnames[PROP_AUTHC] ) ) {
464 if ( pr[i].values && pr[i].values[0] ) {
465 AC_MEMCPY( &op.o_req_ndn, pr[i].values[0], sizeof( struct berval ) );
470 if (!conn || !op.o_req_ndn.bv_val) return SASL_BADPARAM;
472 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
474 if ( !op.o_bd || !op.o_bd->be_modify ) return SASL_FAIL;
476 pr = sparams->utils->prop_get( prctx );
477 if (!pr) return SASL_BADPARAM;
479 for (i=0; pr[i].name; i++);
480 if (!i) return SASL_BADPARAM;
482 for (i=0; pr[i].name; i++) {
483 mod = (Modifications *)ch_malloc( sizeof(Modifications) );
484 mod->sml_op = LDAP_MOD_REPLACE;
485 ber_str2bv( pr[i].name, 0, 0, &mod->sml_type );
486 mod->sml_values = (struct berval *)ch_malloc( (pr[i].nvalues + 1) *
487 sizeof(struct berval));
488 for (j=0; j<pr[i].nvalues; j++) {
489 ber_str2bv( pr[i].values[j], 0, 1, &mod->sml_values[j]);
491 BER_BVZERO( &mod->sml_values[j] );
492 mod->sml_nvalues = NULL;
493 mod->sml_desc = NULL;
495 modtail = &mod->sml_next;
499 rc = slap_mods_check( modlist, 0, &text, textbuf, textlen, NULL );
501 if ( rc == LDAP_SUCCESS ) {
502 rc = slap_mods_opattrs( &op, modlist, modtail, &text, textbuf,
506 if ( rc == LDAP_SUCCESS ) {
507 op.o_tag = LDAP_REQ_MODIFY;
508 op.o_protocol = LDAP_VERSION3;
509 op.o_ndn = op.o_req_ndn;
511 op.o_time = slap_get_time();
512 op.o_do_not_cache = 1;
513 op.o_is_auth_check = 1;
514 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
515 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
516 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
518 op.o_connid = conn->c_connid;
519 op.o_req_dn = op.o_req_ndn;
520 op.orm_modlist = modlist;
522 rc = op.o_bd->be_modify( &op, &rs );
524 slap_mods_free( modlist );
525 return rc ? SASL_FAIL : SASL_OK;
527 #endif /* SASL_VERSION_FULL >= 2.1.16 */
529 static sasl_auxprop_plug_t slap_auxprop_plugin = {
532 NULL, /* glob_context */
533 NULL, /* auxprop_free */
536 #if SASL_VERSION_FULL >= 0x020110
537 slap_auxprop_store /* the declaration of this member changed
538 * in cyrus SASL from 2.1.15 to 2.1.16 */
546 const sasl_utils_t *utils,
549 sasl_auxprop_plug_t **plug,
550 const char *plugname)
552 if ( !out_version || !plug ) return SASL_BADPARAM;
554 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
556 *out_version = SASL_AUXPROP_PLUG_VERSION;
557 *plug = &slap_auxprop_plugin;
561 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
562 * auxiliary property, so that we can refer to it in sasl_authorize
563 * without interfering with anything else. Also, the SASL username
564 * buffer is constrained to 256 characters, and our DNs could be
565 * much longer (SLAP_LDAPDN_MAXLEN, currently set to 8192)
568 slap_sasl_canonicalize(
574 const char *user_realm,
579 Connection *conn = (Connection *)context;
580 struct propctx *props = sasl_auxprop_getctx( sconn );
581 struct propval auxvals[3];
584 const char *names[2];
589 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
590 conn ? conn->c_connid : -1,
591 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
592 in ? in : "<empty>");
594 /* If name is too big, just truncate. We don't care, we're
595 * using DNs, not the usernames.
597 if ( inlen > out_max )
600 /* This is a Simple Bind using SPASSWD. That means the in-directory
601 * userPassword of the Binding user already points at SASL, so it
602 * cannot be used to actually satisfy a password comparison. Just
603 * ignore it, some other mech will process it.
605 if ( !conn->c_sasl_bindop ||
606 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) goto done;
608 /* See if we need to add request, can only do it once */
609 prop_getnames( props, slap_propnames, auxvals );
610 if ( !auxvals[0].name )
611 prop_request( props, slap_propnames );
613 if ( flags & SASL_CU_AUTHID )
618 /* Need to store the Connection for auxprop_lookup */
619 if ( !auxvals[PROP_CONN].values ) {
620 names[0] = slap_propnames[PROP_CONN];
622 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
625 /* Already been here? */
626 if ( auxvals[which].values )
629 /* Normally we require an authzID to have a u: or dn: prefix.
630 * However, SASL frequently gives us an authzID that is just
631 * an exact copy of the authcID, without a prefix. We need to
632 * detect and allow this condition. If SASL calls canonicalize
633 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
634 * But if it's broken into two calls, we need to remember the
635 * authcID so that we can compare the authzID later. We store
636 * the authcID temporarily in conn->c_sasl_dn. We necessarily
637 * finish Canonicalizing before Authorizing, so there is no
638 * conflict with slap_sasl_authorize's use of this temp var.
640 * The SASL EXTERNAL mech is backwards from all the other mechs,
641 * it does authzID before the authcID. If we see that authzID
642 * has already been done, don't do anything special with authcID.
644 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
645 conn->c_sasl_dn.bv_val = (char *) in;
646 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
647 rc = strcmp( in, conn->c_sasl_dn.bv_val );
648 conn->c_sasl_dn.bv_val = NULL;
649 /* They were equal, no work needed */
650 if ( !rc ) goto done;
653 bvin.bv_val = (char *)in;
655 rc = slap_sasl_getdn( conn, NULL, &bvin, (char *)user_realm, &dn,
656 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
657 if ( rc != LDAP_SUCCESS ) {
658 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
662 names[0] = slap_propnames[which];
665 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
667 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
668 conn ? conn->c_connid : -1, names[0]+1,
669 dn.bv_val ? dn.bv_val : "<EMPTY>" );
672 AC_MEMCPY( out, in, inlen );
684 char *requested_user,
688 const char *def_realm,
690 struct propctx *props)
692 Connection *conn = (Connection *)context;
693 struct propval auxvals[3];
694 struct berval authcDN, authzDN;
697 /* Simple Binds don't support proxy authorization, ignore it */
698 if ( !conn->c_sasl_bindop ||
699 conn->c_sasl_bindop->orb_method != LDAP_AUTH_SASL ) return SASL_OK;
701 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
702 "authcid=\"%s\" authzid=\"%s\"\n",
703 conn ? conn->c_connid : -1, auth_identity, requested_user );
704 if ( conn->c_sasl_dn.bv_val ) {
705 ch_free( conn->c_sasl_dn.bv_val );
706 BER_BVZERO( &conn->c_sasl_dn );
710 prop_getnames( props, slap_propnames+1, auxvals );
712 /* Should not happen */
713 if ( !auxvals[0].values ) {
714 sasl_seterror( sconn, 0, "invalid authcid" );
718 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
720 /* Nothing to do if no authzID was given */
721 if ( !auxvals[1].name || !auxvals[1].values ) {
722 conn->c_sasl_dn = authcDN;
726 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
728 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
729 ch_free( authcDN.bv_val );
730 if ( rc != LDAP_SUCCESS ) {
731 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
732 "proxy authorization disallowed (%d)\n",
733 (long) (conn ? conn->c_connid : -1), rc, 0 );
735 sasl_seterror( sconn, 0, "not authorized" );
736 ch_free( authzDN.bv_val );
740 conn->c_sasl_dn = authzDN;
742 if (conn->c_sasl_bindop) {
743 Statslog( LDAP_DEBUG_STATS,
744 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
745 conn->c_connid, conn->c_sasl_bindop->o_opid,
746 auth_identity, 0, 0);
749 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
750 " proxy authorization allowed\n",
751 (long) (conn ? conn->c_connid : -1), 0, 0 );
763 struct berval authcDN, authzDN;
765 Connection *conn = context;
767 struct berval bvauthcid, bvauthzid;
770 if ( conn->c_sasl_dn.bv_val ) {
771 ch_free( conn->c_sasl_dn.bv_val );
772 BER_BVZERO( &conn->c_sasl_dn );
775 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
776 "authcid=\"%s\" authzid=\"%s\"\n",
777 (long) (conn ? conn->c_connid : -1),
778 authcid ? authcid : "<empty>",
779 authzid ? authzid : "<empty>" );
781 /* Figure out how much data we have for the dn */
782 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
783 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
784 Debug(LDAP_DEBUG_TRACE,
785 "authorize: getprop(REALM) failed!\n", 0,0,0);
786 *errstr = "Could not extract realm";
790 /* Convert the identities to DN's. If no authzid was given, client will
791 be bound as the DN matching their username */
792 bvauthcid.bv_val = authcid;
793 bvauthcid.bv_len = authcid ? strlen( authcid ) : 0;
794 rc = slap_sasl_getdn( conn, NULL, &bvauthcid, realm,
795 &authcDN, SLAP_GETDN_AUTHCID );
796 if( rc != LDAP_SUCCESS ) {
797 *errstr = ldap_err2string( rc );
800 if( ( authzid == NULL ) || !strcmp( authcid, authzid ) ) {
801 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
802 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
804 conn->c_sasl_dn = authcDN;
808 bvauthzid.bv_val = authzid;
809 bvauthzid.bv_len = authzid ? strlen( authzid ) : 0;
810 rc = slap_sasl_getdn( conn, NULL, &bvauthzid, realm,
811 &authzDN, SLAP_GETDN_AUTHZID );
812 if( rc != LDAP_SUCCESS ) {
813 ch_free( authcDN.bv_val );
814 *errstr = ldap_err2string( rc );
818 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
819 ch_free( authcDN.bv_val );
821 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
822 "proxy authorization disallowed (%d)\n",
823 (long) (conn ? conn->c_connid : -1), rc, 0 );
825 *errstr = "not authorized";
826 ch_free( authzDN.bv_val );
829 conn->c_sasl_dn = authzDN;
832 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
833 " authorization allowed\n",
834 (long) (conn ? conn->c_connid : -1), 0, 0 );
836 if (conn->c_sasl_bindop) {
837 Statslog( LDAP_DEBUG_STATS,
838 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
839 conn->c_connid, conn->c_sasl_bindop->o_opid,
846 #endif /* SASL_VERSION_MAJOR >= 2 */
849 slap_sasl_err2ldap( int saslerr )
858 rc = LDAP_SASL_BIND_IN_PROGRESS;
867 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
870 rc = LDAP_INVALID_CREDENTIALS;
873 rc = LDAP_INSUFFICIENT_ACCESS;
877 rc = LDAP_INAPPROPRIATE_AUTH;
888 int slap_sasl_init( void )
890 #ifdef HAVE_CYRUS_SASL
892 static sasl_callback_t server_callbacks[] = {
893 { SASL_CB_LOG, &slap_sasl_log, NULL },
894 { SASL_CB_LIST_END, NULL, NULL }
897 #ifdef HAVE_SASL_VERSION
898 /* stringify the version number, sasl.h doesn't do it for us */
899 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
900 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
901 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
904 sasl_version( NULL, &rc );
905 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
906 (rc & 0xffff) < SASL_VERSION_STEP)
908 char version[sizeof("xxx.xxx.xxxxx")];
909 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
911 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: SASL library version mismatch:"
912 " expected " SASL_VERSION_STRING ","
913 " got %s\n", version, 0, 0 );
918 /* SASL 2 does its own memory management internally */
919 #if SASL_VERSION_MAJOR < 2
928 ldap_pvt_sasl_mutex_new,
929 ldap_pvt_sasl_mutex_lock,
930 ldap_pvt_sasl_mutex_unlock,
931 ldap_pvt_sasl_mutex_dispose );
933 #if SASL_VERSION_MAJOR >= 2
934 generic_filter.f_desc = slap_schema.si_ad_objectClass;
936 rc = sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
937 if( rc != SASL_OK ) {
938 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: auxprop add plugin failed\n",
943 /* should provide callbacks for logging */
944 /* server name should be configurable */
945 rc = sasl_server_init( server_callbacks, "slapd" );
947 if( rc != SASL_OK ) {
948 Debug( LDAP_DEBUG_ANY, "slap_sasl_init: server init failed\n",
950 #if SASL_VERSION_MAJOR < 2
951 /* A no-op used to make sure we linked with Cyrus 1.5 */
952 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
958 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
961 /* default security properties */
962 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
963 sasl_secprops.max_ssf = INT_MAX;
964 sasl_secprops.maxbufsize = 65536;
965 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
971 int slap_sasl_destroy( void )
973 #ifdef HAVE_CYRUS_SASL
982 int slap_sasl_open( Connection *conn, int reopen )
984 int sc = LDAP_SUCCESS;
985 #ifdef HAVE_CYRUS_SASL
988 sasl_conn_t *ctx = NULL;
989 sasl_callback_t *session_callbacks;
991 #if SASL_VERSION_MAJOR >= 2
992 char *ipremoteport = NULL, *iplocalport = NULL;
995 assert( conn->c_sasl_authctx == NULL );
998 assert( conn->c_sasl_extra == NULL );
1001 #if SASL_VERSION_MAJOR >= 2
1002 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1004 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1006 if( session_callbacks == NULL ) {
1007 Debug( LDAP_DEBUG_ANY,
1008 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1011 conn->c_sasl_extra = session_callbacks;
1013 session_callbacks[cb=0].id = SASL_CB_LOG;
1014 session_callbacks[cb].proc = &slap_sasl_log;
1015 session_callbacks[cb++].context = conn;
1017 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1018 session_callbacks[cb].proc = &slap_sasl_authorize;
1019 session_callbacks[cb++].context = conn;
1021 #if SASL_VERSION_MAJOR >= 2
1022 session_callbacks[cb].id = SASL_CB_CANON_USER;
1023 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1024 session_callbacks[cb++].context = conn;
1027 session_callbacks[cb].id = SASL_CB_LIST_END;
1028 session_callbacks[cb].proc = NULL;
1029 session_callbacks[cb++].context = NULL;
1031 session_callbacks = conn->c_sasl_extra;
1034 conn->c_sasl_layers = 0;
1036 if( global_host == NULL ) {
1037 global_host = ldap_pvt_get_fqdn( NULL );
1040 /* create new SASL context */
1041 #if SASL_VERSION_MAJOR >= 2
1042 if ( conn->c_sock_name.bv_len != 0 &&
1043 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1046 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1047 /* Convert IPv6 addresses to address;port syntax. */
1048 p = strrchr( iplocalport, ' ' );
1049 /* Convert IPv4 addresses to address;port syntax. */
1050 if ( p == NULL ) p = strchr( iplocalport, ':' );
1055 if ( conn->c_peer_name.bv_len != 0 &&
1056 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1059 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1060 /* Convert IPv6 addresses to address;port syntax. */
1061 p = strrchr( ipremoteport, ' ' );
1062 /* Convert IPv4 addresses to address;port syntax. */
1063 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1068 sc = sasl_server_new( "ldap", global_host, global_realm,
1069 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1070 if ( iplocalport != NULL ) {
1071 ch_free( iplocalport );
1073 if ( ipremoteport != NULL ) {
1074 ch_free( ipremoteport );
1077 sc = sasl_server_new( "ldap", global_host, global_realm,
1078 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1081 if( sc != SASL_OK ) {
1082 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1088 conn->c_sasl_authctx = ctx;
1090 if( sc == SASL_OK ) {
1091 sc = sasl_setprop( ctx,
1092 SASL_SEC_PROPS, &sasl_secprops );
1094 if( sc != SASL_OK ) {
1095 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1098 slap_sasl_close( conn );
1103 sc = slap_sasl_err2ldap( sc );
1105 #elif defined(SLAP_BUILTIN_SASL)
1106 /* built-in SASL implementation */
1107 SASL_CTX *ctx = (SASL_CTX *) SLAP_MALLOC(sizeof(SASL_CTX));
1108 if( ctx == NULL ) return -1;
1110 ctx->sc_external_ssf = 0;
1111 BER_BVZERO( &ctx->sc_external_id );
1113 conn->c_sasl_authctx = ctx;
1119 int slap_sasl_external(
1122 struct berval *auth_id )
1124 #if SASL_VERSION_MAJOR >= 2
1126 sasl_conn_t *ctx = conn->c_sasl_authctx;
1128 if ( ctx == NULL ) {
1129 return LDAP_UNAVAILABLE;
1132 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1134 if ( sc != SASL_OK ) {
1138 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL,
1139 auth_id ? auth_id->bv_val : NULL );
1141 if ( sc != SASL_OK ) {
1145 #elif defined(HAVE_CYRUS_SASL)
1147 sasl_conn_t *ctx = conn->c_sasl_authctx;
1148 sasl_external_properties_t extprops;
1150 if ( ctx == NULL ) {
1151 return LDAP_UNAVAILABLE;
1154 memset( &extprops, '\0', sizeof(extprops) );
1156 extprops.auth_id = auth_id ? auth_id->bv_val : NULL;
1158 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1159 (void *) &extprops );
1161 if ( sc != SASL_OK ) {
1164 #elif defined(SLAP_BUILTIN_SASL)
1165 /* built-in SASL implementation */
1166 SASL_CTX *ctx = conn->c_sasl_authctx;
1167 if ( ctx == NULL ) return LDAP_UNAVAILABLE;
1169 ctx->sc_external_ssf = ssf;
1171 ctx->sc_external_id = *auth_id;
1172 BER_BVZERO( auth_id );
1174 BER_BVZERO( &ctx->sc_external_id );
1178 return LDAP_SUCCESS;
1181 int slap_sasl_reset( Connection *conn )
1183 return LDAP_SUCCESS;
1186 char ** slap_sasl_mechs( Connection *conn )
1188 char **mechs = NULL;
1190 #ifdef HAVE_CYRUS_SASL
1191 sasl_conn_t *ctx = conn->c_sasl_authctx;
1193 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1197 SASL_CONST char *mechstr;
1199 sc = sasl_listmech( ctx,
1200 NULL, NULL, ",", NULL,
1201 &mechstr, NULL, NULL );
1203 if( sc != SASL_OK ) {
1204 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1210 mechs = ldap_str2charray( mechstr, "," );
1212 #if SASL_VERSION_MAJOR < 2
1216 #elif defined(SLAP_BUILTIN_SASL)
1217 /* builtin SASL implementation */
1218 SASL_CTX *ctx = conn->c_sasl_authctx;
1219 if ( ctx != NULL && ctx->sc_external_id.bv_val ) {
1220 /* should check ssf */
1221 mechs = ldap_str2charray( "EXTERNAL", "," );
1228 int slap_sasl_close( Connection *conn )
1230 #ifdef HAVE_CYRUS_SASL
1231 sasl_conn_t *ctx = conn->c_sasl_authctx;
1234 sasl_dispose( &ctx );
1236 if ( conn->c_sasl_sockctx &&
1237 conn->c_sasl_authctx != conn->c_sasl_sockctx )
1239 ctx = conn->c_sasl_sockctx;
1240 sasl_dispose( &ctx );
1243 conn->c_sasl_authctx = NULL;
1244 conn->c_sasl_sockctx = NULL;
1245 conn->c_sasl_done = 0;
1247 free( conn->c_sasl_extra );
1248 conn->c_sasl_extra = NULL;
1250 #elif defined(SLAP_BUILTIN_SASL)
1251 SASL_CTX *ctx = conn->c_sasl_authctx;
1253 if( ctx->sc_external_id.bv_val ) {
1254 free( ctx->sc_external_id.bv_val );
1255 BER_BVZERO( &ctx->sc_external_id );
1258 conn->c_sasl_authctx = NULL;
1262 return LDAP_SUCCESS;
1265 int slap_sasl_bind( Operation *op, SlapReply *rs )
1267 #ifdef HAVE_CYRUS_SASL
1268 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1269 struct berval response;
1270 unsigned reslen = 0;
1273 Debug(LDAP_DEBUG_ARGS,
1274 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1275 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1276 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1277 op->o_conn->c_sasl_bind_mech.bv_val,
1278 op->orb_cred.bv_len );
1281 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1282 "SASL unavailable on this session" );
1286 #if SASL_VERSION_MAJOR >= 2
1287 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1288 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1289 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1290 sasl_server_step( ctx, cred, clen, resp, rlen )
1292 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1293 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1294 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1295 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1298 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1299 /* If we already authenticated once, must use a new context */
1300 if ( op->o_conn->c_sasl_done ) {
1302 const char *authid = NULL;
1303 #if SASL_VERSION_MAJOR >= 2
1304 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1305 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1306 if ( authid ) authid = ch_strdup( authid );
1308 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1309 sasl_dispose( &ctx );
1311 op->o_conn->c_sasl_authctx = NULL;
1313 slap_sasl_open( op->o_conn, 1 );
1314 ctx = op->o_conn->c_sasl_authctx;
1315 #if SASL_VERSION_MAJOR >= 2
1317 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1318 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1319 ch_free( (char *)authid );
1324 op->o_conn->c_sasl_bind_mech.bv_val,
1325 op->orb_cred.bv_val, op->orb_cred.bv_len,
1326 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1330 op->orb_cred.bv_val, op->orb_cred.bv_len,
1331 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1334 response.bv_len = reslen;
1336 if ( sc == SASL_OK ) {
1337 sasl_ssf_t *ssf = NULL;
1339 op->orb_edn = op->o_conn->c_sasl_dn;
1340 BER_BVZERO( &op->o_conn->c_sasl_dn );
1341 op->o_conn->c_sasl_done = 1;
1343 rs->sr_err = LDAP_SUCCESS;
1345 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1346 op->orb_ssf = ssf ? *ssf : 0;
1350 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1351 op->o_conn->c_sasl_layers++;
1353 /* If there's an old layer, set sockctx to NULL to
1354 * tell connection_read() to wait for us to finish.
1355 * Otherwise there is a race condition: we have to
1356 * send the Bind response using the old security
1357 * context and then remove it before reading any
1360 if ( op->o_conn->c_sasl_sockctx ) {
1361 ctx = op->o_conn->c_sasl_sockctx;
1362 op->o_conn->c_sasl_sockctx = NULL;
1364 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1366 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1369 /* Must send response using old security layer */
1370 if (response.bv_len) rs->sr_sasldata = &response;
1371 send_ldap_sasl( op, rs );
1373 /* Now dispose of the old security layer.
1376 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1377 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1378 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1379 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1380 sasl_dispose( &ctx );
1382 } else if ( sc == SASL_CONTINUE ) {
1383 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1384 rs->sr_sasldata = &response;
1385 send_ldap_sasl( op, rs );
1388 #if SASL_VERSION_MAJOR >= 2
1389 rs->sr_text = sasl_errdetail( ctx );
1391 rs->sr_err = slap_sasl_err2ldap( sc ),
1392 send_ldap_result( op, rs );
1395 #if SASL_VERSION_MAJOR < 2
1396 if( response.bv_len ) {
1397 ch_free( response.bv_val );
1401 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1403 #elif defined(SLAP_BUILTIN_SASL)
1404 /* built-in SASL implementation */
1405 SASL_CTX *ctx = op->o_conn->c_sasl_authctx;
1407 if ( ctx == NULL ) {
1408 send_ldap_error( op, rs, LDAP_OTHER,
1409 "Internal SASL Error" );
1411 } else if ( bvmatch( &ext_bv, &op->o_conn->c_sasl_bind_mech ) ) {
1414 if( op->orb_cred.bv_len ) {
1415 rs->sr_text = "proxy authorization not support";
1416 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
1417 send_ldap_result( op, rs );
1420 op->orb_edn = ctx->sc_external_id;
1421 rs->sr_err = LDAP_SUCCESS;
1422 rs->sr_sasldata = NULL;
1423 send_ldap_sasl( op, rs );
1427 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1428 "requested SASL mechanism not supported" );
1431 send_ldap_error( op, rs, LDAP_AUTH_METHOD_NOT_SUPPORTED,
1432 "SASL not supported" );
1438 char* slap_sasl_secprops( const char *in )
1440 #ifdef HAVE_CYRUS_SASL
1441 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1443 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1445 return "SASL not supported";
1449 #ifdef HAVE_CYRUS_SASL
1451 slap_sasl_setpass( Operation *op, SlapReply *rs )
1453 struct berval id = BER_BVNULL; /* needs to come from connection */
1454 struct berval new = BER_BVNULL;
1455 struct berval old = BER_BVNULL;
1457 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1459 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1460 (SASL_CONST void **)&id.bv_val );
1462 if( rs->sr_err != SASL_OK ) {
1463 rs->sr_text = "unable to retrieve SASL username";
1464 rs->sr_err = LDAP_OTHER;
1468 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1469 id.bv_val ? id.bv_val : "", 0, 0 );
1471 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1472 NULL, &old, &new, &rs->sr_text );
1474 if( rs->sr_err != LDAP_SUCCESS ) {
1478 if( new.bv_len == 0 ) {
1479 slap_passwd_generate(&new);
1481 if( new.bv_len == 0 ) {
1482 rs->sr_text = "password generation failed.";
1483 rs->sr_err = LDAP_OTHER;
1487 rs->sr_rspdata = slap_passwd_return( &new );
1490 #if SASL_VERSION_MAJOR < 2
1491 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1492 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1494 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1495 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1496 if( rs->sr_err != SASL_OK ) {
1497 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1500 switch(rs->sr_err) {
1502 rs->sr_err = LDAP_SUCCESS;
1512 rs->sr_err = LDAP_OTHER;
1518 #endif /* HAVE_CYRUS_SASL */
1520 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1521 * string returned in *dn is in its own allocated memory, and must be free'd
1522 * by the calling process. -Mark Adamson, Carnegie Mellon
1524 * The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1525 * on strings passed in directly from SASL. -Howard Chu, Symas Corp.
1532 int slap_sasl_getdn( Connection *conn, Operation *op, struct berval *id,
1533 char *user_realm, struct berval *dn, int flags )
1535 int rc, is_dn = SET_NONE, do_norm = 1;
1536 struct berval dn2, *mech;
1541 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1543 BER_BVISNULL( id ) ? "NULL" : ( BER_BVISEMPTY( id ) ? "<empty>" : id->bv_val ),
1544 BER_BVISNULL( id ) ? 0 : ( BER_BVISEMPTY( id ) ? 0 : id->bv_len ) );
1547 op = conn->c_sasl_bindop;
1552 if ( !BER_BVISNULL( id ) ) {
1553 /* Blatantly anonymous ID */
1554 static struct berval bv_anonymous = BER_BVC( "anonymous" );
1556 if ( ber_bvstrcasecmp( id, &bv_anonymous ) == 0 ) {
1557 return( LDAP_SUCCESS );
1561 /* FIXME: if empty, should we stop? */
1562 BER_BVSTR( id, "" );
1565 if ( !BER_BVISEMPTY( &conn->c_sasl_bind_mech ) ) {
1566 mech = &conn->c_sasl_bind_mech;
1568 mech = &conn->c_authmech;
1571 /* An authcID needs to be converted to authzID form. Set the
1572 * values directly into *dn; they will be normalized later. (and
1573 * normalizing always makes a new copy.) An ID from a TLS certificate
1574 * is already normalized, so copy it and skip normalization.
1576 if( flags & SLAP_GETDN_AUTHCID ) {
1577 if( bvmatch( mech, &ext_bv )) {
1578 /* EXTERNAL DNs are already normalized */
1579 assert( !BER_BVISNULL( id ) );
1583 ber_dupbv_x( dn, id, op->o_tmpmemctx );
1586 /* convert to u:<username> form */
1592 if( is_dn == SET_NONE ) {
1593 if( !strncasecmp( id->bv_val, "u:", STRLENOF( "u:" ) ) ) {
1595 dn->bv_val = id->bv_val + STRLENOF( "u:" );
1596 dn->bv_len = id->bv_len - STRLENOF( "u:" );
1598 } else if ( !strncasecmp( id->bv_val, "dn:", STRLENOF( "dn:" ) ) ) {
1600 dn->bv_val = id->bv_val + STRLENOF( "dn:" );
1601 dn->bv_len = id->bv_len - STRLENOF( "dn:" );
1605 /* No other possibilities from here */
1606 if( is_dn == SET_NONE ) {
1608 return( LDAP_INAPPROPRIATE_AUTH );
1611 /* Username strings */
1612 if( is_dn == SET_U ) {
1614 struct berval realm = BER_BVNULL, c1 = *dn;
1617 len = dn->bv_len + STRLENOF( "uid=" ) + STRLENOF( ",cn=auth" );
1619 if( user_realm && *user_realm ) {
1620 ber_str2bv( user_realm, 0, 0, &realm );
1621 len += realm.bv_len + STRLENOF( ",cn=" );
1624 if( mech->bv_len ) {
1625 len += mech->bv_len + STRLENOF( ",cn=" );
1628 /* Build the new dn */
1629 dn->bv_val = slap_sl_malloc( len + 1, op->o_tmpmemctx );
1630 if( dn->bv_val == NULL ) {
1631 Debug( LDAP_DEBUG_ANY,
1632 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1635 p = lutil_strcopy( dn->bv_val, "uid=" );
1636 p = lutil_strncopy( p, c1.bv_val, c1.bv_len );
1638 if( realm.bv_len ) {
1639 p = lutil_strcopy( p, ",cn=" );
1640 p = lutil_strncopy( p, realm.bv_val, realm.bv_len );
1643 if( mech->bv_len ) {
1644 p = lutil_strcopy( p, ",cn=" );
1645 p = lutil_strcopy( p, mech->bv_val );
1647 p = lutil_strcopy( p, ",cn=auth" );
1648 dn->bv_len = p - dn->bv_val;
1650 Debug( LDAP_DEBUG_TRACE, "slap_sasl_getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1653 /* Dup the DN in any case, so we don't risk
1654 * leaks or dangling pointers later,
1655 * and the DN value is '\0' terminated */
1656 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1657 dn->bv_val = dn2.bv_val;
1660 /* All strings are in DN form now. Normalize if needed. */
1662 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1664 /* User DNs were constructed above and must be freed now */
1665 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1667 if ( rc != LDAP_SUCCESS ) {
1674 /* Run thru regexp */
1675 slap_sasl2dn( op, dn, &dn2, flags );
1676 if( !BER_BVISNULL( &dn2 ) ) {
1677 slap_sl_free( dn->bv_val, op->o_tmpmemctx );
1679 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1683 return( LDAP_SUCCESS );