3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/stdlib.h>
11 #include <ac/string.h>
20 #ifdef HAVE_CYRUS_SASL
21 # ifdef HAVE_SASL_SASL_H
22 # include <sasl/sasl.h>
27 # if SASL_VERSION_MAJOR >= 2
28 # include <sasl/saslplug.h>
29 # define SASL_CONST const
34 static sasl_security_properties_t sasl_secprops;
35 #endif /* HAVE_CYRUS_SASL */
41 int slap_sasl_config( int cargc, char **cargv, char *line,
42 const char *fname, int lineno )
44 /* set SASL proxy authorization policy */
45 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
48 LDAP_LOG( CONFIG, CRIT,
49 "%s: line %d: missing policy in"
50 " \"sasl-authz-policy <policy>\" line\n",
53 Debug( LDAP_DEBUG_ANY,
54 "%s: line %d: missing policy in"
55 " \"sasl-authz-policy <policy>\" line\n",
61 if ( slap_sasl_setpolicy( cargv[1] ) ) {
63 LDAP_LOG( CONFIG, CRIT,
64 "%s: line %d: unable "
65 "to parse value \"%s\" "
66 "in \"sasl-authz-policy "
68 fname, lineno, cargv[1] );
70 Debug( LDAP_DEBUG_ANY,
71 "%s: line %d: unable "
72 "to parse value \"%s\" "
73 "in \"sasl-authz-policy "
75 fname, lineno, cargv[1] );
80 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
81 || !strcasecmp( cargv[0], "saslregexp" ) )
86 LDAP_LOG( CONFIG, CRIT,
87 "%s: line %d: need 2 args in "
88 "\"saslregexp <match> <replace>\"\n",
91 Debug( LDAP_DEBUG_ANY,
92 "%s: line %d: need 2 args in "
93 "\"saslregexp <match> <replace>\"\n",
99 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
104 #ifdef HAVE_CYRUS_SASL
106 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
109 LDAP_LOG( CONFIG, CRIT,
110 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
113 Debug( LDAP_DEBUG_ANY,
114 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
121 if ( global_host != NULL ) {
123 LDAP_LOG( CONFIG, CRIT,
124 "%s: line %d: already set sasl-host!\n",
127 Debug( LDAP_DEBUG_ANY,
128 "%s: line %d: already set sasl-host!\n",
135 global_host = ch_strdup( cargv[1] );
139 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
142 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
143 "missing realm in \"sasl-realm <realm>\" line.\n",
146 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
147 "missing realm in \"sasl-realm <realm>\" line.\n",
154 if ( global_realm != NULL ) {
156 LDAP_LOG( CONFIG, CRIT,
157 "%s: line %d: already set sasl-realm!\n",
160 Debug( LDAP_DEBUG_ANY,
161 "%s: line %d: already set sasl-realm!\n",
168 global_realm = ch_strdup( cargv[1] );
171 /* SASL security properties */
172 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
177 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
178 "missing flags in \"sasl-secprops <properties>\" line\n",
181 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
182 "missing flags in \"sasl-secprops <properties>\" line\n",
189 txt = slap_sasl_secprops( cargv[1] );
192 LDAP_LOG( CONFIG, CRIT,
193 "%s: line %d sasl-secprops: %s\n",
194 fname, lineno, txt );
196 Debug( LDAP_DEBUG_ANY,
197 "%s: line %d: sasl-secprops: %s\n",
198 fname, lineno, txt );
203 #endif /* HAVE_CYRUS_SASL */
209 #ifdef HAVE_CYRUS_SASL
217 Connection *conn = context;
221 if ( message == NULL ) {
222 return SASL_BADPARAM;
226 #if SASL_VERSION_MAJOR >= 2
228 level = LDAP_DEBUG_NONE;
232 level = LDAP_DEBUG_ANY;
236 level = LDAP_DEBUG_ANY;
240 level = LDAP_DEBUG_TRACE;
244 level = LDAP_DEBUG_TRACE;
248 level = LDAP_DEBUG_TRACE;
252 level = LDAP_DEBUG_TRACE;
256 level = LDAP_DEBUG_TRACE;
257 label = "Password Trace";
261 level = LDAP_DEBUG_ANY;
264 case SASL_LOG_WARNING:
265 level = LDAP_DEBUG_TRACE;
269 level = LDAP_DEBUG_TRACE;
274 return SASL_BADPARAM;
278 LDAP_LOG( TRANSPORT, ENTRY,
279 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
281 Debug( level, "SASL [conn=%ld] %s: %s\n",
282 conn ? conn->c_connid: -1,
291 #if SASL_VERSION_MAJOR >= 2
292 static const char *slap_propnames[] = {
293 "*slapConn", "*authcDN", "*authzDN", NULL };
295 static Filter generic_filter = { LDAP_FILTER_PRESENT };
301 typedef struct lookup_info {
304 const struct propval *list;
305 sasl_server_params_t *sparams;
308 static slap_response sasl_ap_lookup, sasl_cb_checkpass;
311 sasl_ap_lookup( Operation *op, SlapReply *rs )
314 AttributeDescription *ad;
318 slap_callback *tmp = op->o_callback;
319 lookup_info *sl = tmp->sc_private;
321 if (rs->sr_type != REP_SEARCH) return 0;
323 for( i = 0; i < sl->last; i++ ) {
324 const char *name = sl->list[i].name;
326 if ( name[0] == '*' ) {
327 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
329 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
332 if ( sl->list[i].values ) {
333 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
336 rc = slap_str2ad( name, &ad, &text );
337 if ( rc != LDAP_SUCCESS ) {
339 LDAP_LOG( TRANSPORT, DETAIL1,
340 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
342 Debug( LDAP_DEBUG_TRACE,
343 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
347 a = attr_find( rs->sr_entry->e_attrs, ad );
349 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
352 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
353 sl->sparams->utils->prop_erase( sl->sparams->propctx,
356 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
357 sl->sparams->utils->prop_set( sl->sparams->propctx,
358 sl->list[i].name, bv->bv_val, bv->bv_len );
367 sasl_server_params_t *sparams,
374 Connection *conn = NULL;
377 sl.list = sparams->utils->prop_get( sparams->propctx );
378 sl.sparams = sparams;
381 /* Find our DN and conn first */
382 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
383 if ( sl.list[i].name[0] == '*' ) {
384 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
385 if ( sl.list[i].values && sl.list[i].values[0] )
386 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
387 if ( !sl.last ) sl.last = i;
389 if ( (flags & SASL_AUXPROP_AUTHZID) &&
390 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
392 if ( sl.list[i].values && sl.list[i].values[0] )
393 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
394 if ( !sl.last ) sl.last = i;
397 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
398 if ( !sl.last ) sl.last = i;
399 if ( sl.list[i].values && sl.list[i].values[0] ) {
400 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
401 if ( !(flags & SASL_AUXPROP_AUTHZID) )
408 /* Now see what else needs to be fetched */
409 for( i = 0; i < sl.last; i++ ) {
410 const char *name = sl.list[i].name;
412 if ( name[0] == '*' ) {
413 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
415 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
418 if ( sl.list[i].values ) {
419 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
425 slap_callback cb = { sasl_ap_lookup, NULL };
429 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
431 if ( op.o_bd && op.o_bd->be_search ) {
432 SlapReply rs = {REP_RESULT};
433 op.o_tag = LDAP_REQ_SEARCH;
434 op.o_protocol = LDAP_VERSION3;
435 op.o_ndn = conn->c_ndn;
437 op.o_time = slap_get_time();
438 op.o_do_not_cache = 1;
439 op.o_is_auth_check = 1;
440 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
441 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
442 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
444 op.o_pb = conn->c_sasl_bindop->o_pb;
447 op.o_connid = conn->c_connid;
448 op.ors_scope = LDAP_SCOPE_BASE;
449 op.ors_deref = LDAP_DEREF_NEVER;
451 op.ors_filter = &generic_filter;
453 op.o_bd->be_search( &op, &rs );
458 static sasl_auxprop_plug_t slap_auxprop_plugin = {
461 NULL, /* glob_context */
462 NULL, /* auxprop_free */
470 const sasl_utils_t *utils,
473 sasl_auxprop_plug_t **plug,
474 const char *plugname)
476 if ( !out_version | !plug ) return SASL_BADPARAM;
478 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
480 *out_version = SASL_AUXPROP_PLUG_VERSION;
481 *plug = &slap_auxprop_plugin;
485 typedef struct checkpass_info {
491 sasl_cb_checkpass( Operation *op, SlapReply *rs )
493 slap_callback *tmp = op->o_callback;
494 checkpass_info *ci = tmp->sc_private;
498 if (rs->sr_type != REP_SEARCH) return 0;
500 ci->rc = SASL_NOVERIFY;
502 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_userPassword );
504 if ( ! access_allowed( op, rs->sr_entry, slap_schema.si_ad_userPassword,
505 NULL, ACL_AUTH, NULL ) ) return 0;
507 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
508 if ( !lutil_passwd( bv, &ci->cred, NULL, &rs->sr_text ) ) {
520 const char *username,
523 struct propctx *propctx)
525 Connection *conn = (Connection *)context;
532 /* SASL will fallback to its own mechanisms if we don't
533 * find an answer here.
536 rc = slap_sasl_getdn( conn, NULL, (char *)username, 0, NULL, &op.o_req_ndn,
537 SLAP_GETDN_AUTHCID );
538 if ( rc != LDAP_SUCCESS ) {
539 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
543 if ( op.o_req_ndn.bv_len == 0 ) {
544 sasl_seterror( sconn, 0,
545 "No password is associated with the Root DSE" );
546 if ( op.o_req_ndn.bv_val != NULL ) {
547 ch_free( op.o_req_ndn.bv_val );
552 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
553 if ( op.o_bd && op.o_bd->be_search ) {
554 slap_callback cb = { sasl_cb_checkpass, NULL };
555 SlapReply rs = {REP_RESULT};
557 ci.cred.bv_val = (char *)pass;
558 ci.cred.bv_len = passlen;
561 op.o_tag = LDAP_REQ_SEARCH;
562 op.o_protocol = LDAP_VERSION3;
563 op.o_ndn = conn->c_ndn;
565 op.o_time = slap_get_time();
566 op.o_do_not_cache = 1;
567 op.o_is_auth_check = 1;
568 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
569 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
570 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
572 op.o_pb = conn->c_sasl_bindop->o_pb;
575 op.o_connid = conn->c_connid;
576 op.ors_scope = LDAP_SCOPE_BASE;
577 op.ors_deref = LDAP_DEREF_NEVER;
579 op.ors_filter = &generic_filter;
581 op.o_bd->be_search( &op, &rs );
583 if ( ci.rc != SASL_OK ) {
584 sasl_seterror( sconn, 0,
585 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
588 ch_free( op.o_req_ndn.bv_val );
593 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
594 * auxiliary property, so that we can refer to it in sasl_authorize
595 * without interfering with anything else. Also, the SASL username
596 * buffer is constrained to 256 characters, and our DNs could be
597 * much longer (totally arbitrary length)...
600 slap_sasl_canonicalize(
606 const char *user_realm,
611 Connection *conn = (Connection *)context;
612 struct propctx *props = sasl_auxprop_getctx( sconn );
613 struct propval auxvals[3];
616 const char *names[2];
621 LDAP_LOG( TRANSPORT, ENTRY,
622 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
623 conn ? conn->c_connid : -1,
624 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
625 in ? in : "<empty>");
627 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
628 conn ? conn->c_connid : -1,
629 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
630 in ? in : "<empty>");
633 /* If name is too big, just truncate. We don't care, we're
634 * using DNs, not the usernames.
636 if ( inlen > out_max )
639 /* See if we need to add request, can only do it once */
640 prop_getnames( props, slap_propnames, auxvals );
641 if ( !auxvals[0].name )
642 prop_request( props, slap_propnames );
644 if ( flags & SASL_CU_AUTHID )
649 /* Need to store the Connection for auxprop_lookup */
650 if ( !auxvals[PROP_CONN].values ) {
651 names[0] = slap_propnames[PROP_CONN];
653 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
656 /* Already been here? */
657 if ( auxvals[which].values )
660 /* Normally we require an authzID to have a u: or dn: prefix.
661 * However, SASL frequently gives us an authzID that is just
662 * an exact copy of the authcID, without a prefix. We need to
663 * detect and allow this condition. If SASL calls canonicalize
664 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
665 * But if it's broken into two calls, we need to remember the
666 * authcID so that we can compare the authzID later. We store
667 * the authcID temporarily in conn->c_sasl_dn. We necessarily
668 * finish Canonicalizing before Authorizing, so there is no
669 * conflict with slap_sasl_authorize's use of this temp var.
671 * The SASL EXTERNAL mech is backwards from all the other mechs,
672 * it does authzID before the authcID. If we see that authzID
673 * has already been done, don't do anything special with authcID.
675 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
676 conn->c_sasl_dn.bv_val = (char *) in;
677 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
678 rc = strcmp( in, conn->c_sasl_dn.bv_val );
679 conn->c_sasl_dn.bv_val = NULL;
680 /* They were equal, no work needed */
681 if ( !rc ) goto done;
684 rc = slap_sasl_getdn( conn, NULL, (char *)in, inlen, (char *)user_realm, &dn,
685 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
686 if ( rc != LDAP_SUCCESS ) {
687 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
691 names[0] = slap_propnames[which];
694 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
697 LDAP_LOG( TRANSPORT, ENTRY,
698 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
699 conn ? conn->c_connid : -1, names[0]+1,
700 dn.bv_val ? dn.bv_val : "<EMPTY>" );
702 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
703 conn ? conn->c_connid : -1, names[0]+1,
704 dn.bv_val ? dn.bv_val : "<EMPTY>" );
708 AC_MEMCPY( out, in, inlen );
720 char *requested_user,
724 const char *def_realm,
726 struct propctx *props)
728 Connection *conn = (Connection *)context;
729 struct propval auxvals[3];
730 struct berval authcDN, authzDN;
734 LDAP_LOG( TRANSPORT, ENTRY,
735 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
736 conn ? conn->c_connid : -1, auth_identity, requested_user);
738 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
739 "authcid=\"%s\" authzid=\"%s\"\n",
740 conn ? conn->c_connid : -1, auth_identity, requested_user );
742 if ( conn->c_sasl_dn.bv_val ) {
743 ch_free( conn->c_sasl_dn.bv_val );
744 conn->c_sasl_dn.bv_val = NULL;
745 conn->c_sasl_dn.bv_len = 0;
749 prop_getnames( props, slap_propnames+1, auxvals );
751 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
753 /* Nothing to do if no authzID was given */
754 if ( !auxvals[1].name || !auxvals[1].values ) {
755 conn->c_sasl_dn = authcDN;
759 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
761 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
762 ch_free( authcDN.bv_val );
763 if ( rc != LDAP_SUCCESS ) {
765 LDAP_LOG( TRANSPORT, INFO,
766 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
767 (long)(conn ? conn->c_connid : -1), rc, 0 );
769 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
770 " authorization disallowed (%d)\n",
771 (long) (conn ? conn->c_connid : -1), rc, 0 );
774 sasl_seterror( sconn, 0, "not authorized" );
775 ch_free( authzDN.bv_val );
779 conn->c_sasl_dn = authzDN;
781 if (conn->c_sasl_bindop) {
782 Statslog( LDAP_DEBUG_STATS,
783 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
784 conn->c_connid, conn->c_sasl_bindop->o_opid,
785 auth_identity, 0, 0);
789 LDAP_LOG( TRANSPORT, ENTRY,
790 "slap_sasl_authorize: conn %d authorization allowed\n",
791 (long)(conn ? conn->c_connid : -1), 0, 0 );
793 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
794 " authorization allowed\n",
795 (long) (conn ? conn->c_connid : -1), 0, 0 );
808 struct berval authcDN, authzDN;
810 Connection *conn = context;
814 if ( conn->c_sasl_dn.bv_val ) {
815 ch_free( conn->c_sasl_dn.bv_val );
816 conn->c_sasl_dn.bv_val = NULL;
817 conn->c_sasl_dn.bv_len = 0;
821 LDAP_LOG( TRANSPORT, ENTRY,
822 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
823 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
824 authzid ? authzid : "<empty>" );
826 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
827 "authcid=\"%s\" authzid=\"%s\"\n",
828 (long) (conn ? conn->c_connid : -1),
829 authcid ? authcid : "<empty>",
830 authzid ? authzid : "<empty>" );
833 /* Figure out how much data we have for the dn */
834 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
835 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
837 LDAP_LOG( TRANSPORT, ERR,
838 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
840 Debug(LDAP_DEBUG_TRACE,
841 "authorize: getprop(REALM) failed!\n", 0,0,0);
843 *errstr = "Could not extract realm";
847 /* Convert the identities to DN's. If no authzid was given, client will
848 be bound as the DN matching their username */
849 rc = slap_sasl_getdn( conn, NULL, (char *)authcid, 0, realm,
850 &authcDN, SLAP_GETDN_AUTHCID );
851 if( rc != LDAP_SUCCESS ) {
852 *errstr = ldap_err2string( rc );
855 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
857 LDAP_LOG( TRANSPORT, ENTRY,
858 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
859 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
861 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
862 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
865 conn->c_sasl_dn = authcDN;
868 rc = slap_sasl_getdn( conn, NULL, (char *)authzid, 0, realm,
869 &authzDN, SLAP_GETDN_AUTHZID );
870 if( rc != LDAP_SUCCESS ) {
871 ch_free( authcDN.bv_val );
872 *errstr = ldap_err2string( rc );
876 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
877 ch_free( authcDN.bv_val );
880 LDAP_LOG( TRANSPORT, INFO,
881 "slap_sasl_authorize: conn %ld authorization disallowed (%d)\n",
882 (long)(conn ? conn->c_connid : -1), rc, 0 );
884 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
885 " authorization disallowed (%d)\n",
886 (long) (conn ? conn->c_connid : -1), rc, 0 );
889 *errstr = "not authorized";
890 ch_free( authzDN.bv_val );
893 conn->c_sasl_dn = authzDN;
897 LDAP_LOG( TRANSPORT, RESULTS,
898 "slap_sasl_authorize: conn %d authorization allowed\n",
899 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
901 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
902 " authorization allowed\n",
903 (long) (conn ? conn->c_connid : -1), 0, 0 );
906 if (conn->c_sasl_bindop) {
907 Statslog( LDAP_DEBUG_STATS,
908 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
909 conn->c_connid, conn->c_sasl_bindop->o_opid,
916 #endif /* SASL_VERSION_MAJOR >= 2 */
919 slap_sasl_err2ldap( int saslerr )
928 rc = LDAP_SASL_BIND_IN_PROGRESS;
937 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
940 rc = LDAP_INVALID_CREDENTIALS;
943 rc = LDAP_INSUFFICIENT_ACCESS;
947 rc = LDAP_INAPPROPRIATE_AUTH;
958 int slap_sasl_init( void )
960 #ifdef HAVE_CYRUS_SASL
962 static sasl_callback_t server_callbacks[] = {
963 { SASL_CB_LOG, &slap_sasl_log, NULL },
964 { SASL_CB_LIST_END, NULL, NULL }
967 #ifdef HAVE_SASL_VERSION
968 /* stringify the version number, sasl.h doesn't do it for us */
969 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
970 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
971 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
974 sasl_version( NULL, &rc );
975 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
976 (rc & 0xffff) < SASL_VERSION_STEP) {
977 char version[sizeof("xxx.xxx.xxxxx")];
978 sprintf( version, "%d.%d.%d", rc >> 24, rc >> 16 & 0xff,
981 LDAP_LOG( TRANSPORT, INFO,
982 "slap_sasl_init: SASL library version mismatch:"
983 " expected " SASL_VERSION_STRING ","
984 " got %s\n", version, 0, 0 );
986 Debug( LDAP_DEBUG_ANY,
987 "slap_sasl_init: SASL library version mismatch:"
988 " expected " SASL_VERSION_STRING ","
989 " got %s\n", version, 0, 0 );
995 /* SASL 2 does its own memory management internally */
996 #if SASL_VERSION_MAJOR < 2
1005 ldap_pvt_sasl_mutex_new,
1006 ldap_pvt_sasl_mutex_lock,
1007 ldap_pvt_sasl_mutex_unlock,
1008 ldap_pvt_sasl_mutex_dispose );
1010 #if SASL_VERSION_MAJOR >= 2
1011 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1013 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1015 /* should provide callbacks for logging */
1016 /* server name should be configurable */
1017 rc = sasl_server_init( server_callbacks, "slapd" );
1019 if( rc != SASL_OK ) {
1021 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1023 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1026 #if SASL_VERSION_MAJOR < 2
1027 /* A no-op used to make sure we linked with Cyrus 1.5 */
1028 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1035 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1037 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1042 /* default security properties */
1043 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1044 sasl_secprops.max_ssf = INT_MAX;
1045 sasl_secprops.maxbufsize = 65536;
1046 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1052 int slap_sasl_destroy( void )
1054 #ifdef HAVE_CYRUS_SASL
1057 free( global_host );
1063 int slap_sasl_open( Connection *conn, int reopen )
1065 int cb, sc = LDAP_SUCCESS;
1066 #if SASL_VERSION_MAJOR >= 2
1067 char *ipremoteport = NULL, *iplocalport = NULL;
1070 #ifdef HAVE_CYRUS_SASL
1071 sasl_conn_t *ctx = NULL;
1072 sasl_callback_t *session_callbacks;
1074 assert( conn->c_sasl_authctx == NULL );
1077 assert( conn->c_sasl_extra == NULL );
1080 #if SASL_VERSION_MAJOR >= 2
1081 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1083 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1085 if( session_callbacks == NULL ) {
1087 LDAP_LOG( TRANSPORT, ERR,
1088 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1090 Debug( LDAP_DEBUG_ANY,
1091 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1095 conn->c_sasl_extra = session_callbacks;
1097 session_callbacks[cb=0].id = SASL_CB_LOG;
1098 session_callbacks[cb].proc = &slap_sasl_log;
1099 session_callbacks[cb++].context = conn;
1101 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1102 session_callbacks[cb].proc = &slap_sasl_authorize;
1103 session_callbacks[cb++].context = conn;
1105 #if SASL_VERSION_MAJOR >= 2
1106 session_callbacks[cb].id = SASL_CB_CANON_USER;
1107 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1108 session_callbacks[cb++].context = conn;
1110 /* XXXX: this should be conditional */
1111 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1112 session_callbacks[cb].proc = &slap_sasl_checkpass;
1113 session_callbacks[cb++].context = conn;
1116 session_callbacks[cb].id = SASL_CB_LIST_END;
1117 session_callbacks[cb].proc = NULL;
1118 session_callbacks[cb++].context = NULL;
1120 session_callbacks = conn->c_sasl_extra;
1123 conn->c_sasl_layers = 0;
1125 if( global_host == NULL ) {
1126 global_host = ldap_pvt_get_fqdn( NULL );
1129 /* create new SASL context */
1130 #if SASL_VERSION_MAJOR >= 2
1131 if ( conn->c_sock_name.bv_len != 0 &&
1132 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1135 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1136 /* Convert IPv6 addresses to address;port syntax. */
1137 p = strrchr( iplocalport, ' ' );
1138 /* Convert IPv4 addresses to address;port syntax. */
1139 if ( p == NULL ) p = strchr( iplocalport, ':' );
1144 if ( conn->c_peer_name.bv_len != 0 &&
1145 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1148 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1149 /* Convert IPv6 addresses to address;port syntax. */
1150 p = strrchr( ipremoteport, ' ' );
1151 /* Convert IPv4 addresses to address;port syntax. */
1152 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1157 sc = sasl_server_new( "ldap", global_host, global_realm,
1158 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1159 if ( iplocalport != NULL ) {
1160 ch_free( iplocalport );
1162 if ( ipremoteport != NULL ) {
1163 ch_free( ipremoteport );
1166 sc = sasl_server_new( "ldap", global_host, global_realm,
1167 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1170 if( sc != SASL_OK ) {
1172 LDAP_LOG( TRANSPORT, ERR,
1173 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1175 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1182 conn->c_sasl_authctx = ctx;
1184 if( sc == SASL_OK ) {
1185 sc = sasl_setprop( ctx,
1186 SASL_SEC_PROPS, &sasl_secprops );
1188 if( sc != SASL_OK ) {
1190 LDAP_LOG( TRANSPORT, ERR,
1191 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1193 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1197 slap_sasl_close( conn );
1202 sc = slap_sasl_err2ldap( sc );
1207 int slap_sasl_external(
1210 const char *auth_id )
1212 #if SASL_VERSION_MAJOR >= 2
1214 sasl_conn_t *ctx = conn->c_sasl_authctx;
1216 if ( ctx == NULL ) {
1217 return LDAP_UNAVAILABLE;
1220 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1222 if ( sc != SASL_OK ) {
1226 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1228 if ( sc != SASL_OK ) {
1232 #elif defined(HAVE_CYRUS_SASL)
1234 sasl_conn_t *ctx = conn->c_sasl_authctx;
1235 sasl_external_properties_t extprops;
1237 if ( ctx == NULL ) {
1238 return LDAP_UNAVAILABLE;
1241 memset( &extprops, '\0', sizeof(extprops) );
1243 extprops.auth_id = (char *) auth_id;
1245 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1246 (void *) &extprops );
1248 if ( sc != SASL_OK ) {
1253 return LDAP_SUCCESS;
1256 int slap_sasl_reset( Connection *conn )
1258 return LDAP_SUCCESS;
1261 char ** slap_sasl_mechs( Connection *conn )
1263 char **mechs = NULL;
1265 #ifdef HAVE_CYRUS_SASL
1266 sasl_conn_t *ctx = conn->c_sasl_authctx;
1268 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1272 SASL_CONST char *mechstr;
1274 sc = sasl_listmech( ctx,
1275 NULL, NULL, ",", NULL,
1276 &mechstr, NULL, NULL );
1278 if( sc != SASL_OK ) {
1280 LDAP_LOG( TRANSPORT, ERR,
1281 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1283 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1290 mechs = ldap_str2charray( mechstr, "," );
1292 #if SASL_VERSION_MAJOR < 2
1301 int slap_sasl_close( Connection *conn )
1303 #ifdef HAVE_CYRUS_SASL
1304 sasl_conn_t *ctx = conn->c_sasl_authctx;
1307 sasl_dispose( &ctx );
1309 if ( conn->c_sasl_sockctx && conn->c_sasl_authctx != conn->c_sasl_sockctx ) {
1310 ctx = conn->c_sasl_sockctx;
1311 sasl_dispose( &ctx );
1314 conn->c_sasl_authctx = NULL;
1315 conn->c_sasl_sockctx = NULL;
1316 conn->c_sasl_done = 0;
1318 free( conn->c_sasl_extra );
1319 conn->c_sasl_extra = NULL;
1322 return LDAP_SUCCESS;
1325 int slap_sasl_bind( Operation *op, SlapReply *rs )
1327 #ifdef HAVE_CYRUS_SASL
1328 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1329 struct berval response;
1330 unsigned reslen = 0;
1334 LDAP_LOG( TRANSPORT, ENTRY,
1335 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1336 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1337 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1338 op->o_conn->c_sasl_bind_mech.bv_val,
1339 op->orb_cred.bv_len );
1341 Debug(LDAP_DEBUG_ARGS,
1342 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1343 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1344 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1345 op->o_conn->c_sasl_bind_mech.bv_val,
1346 op->orb_cred.bv_len );
1351 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1352 "SASL unavailable on this session" );
1356 #if SASL_VERSION_MAJOR >= 2
1357 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1358 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1359 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1360 sasl_server_step( ctx, cred, clen, resp, rlen )
1362 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1363 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1364 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1365 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1368 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1369 /* If we already authenticated once, must use a new context */
1370 if ( op->o_conn->c_sasl_done ) {
1372 const char *authid = NULL;
1373 #if SASL_VERSION_MAJOR >= 2
1374 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1375 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1376 if ( authid ) authid = ch_strdup( authid );
1378 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1379 sasl_dispose( &ctx );
1381 op->o_conn->c_sasl_authctx = NULL;
1383 slap_sasl_open( op->o_conn, 1 );
1384 ctx = op->o_conn->c_sasl_authctx;
1385 #if SASL_VERSION_MAJOR >= 2
1387 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1388 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1389 ch_free( (char *)authid );
1394 op->o_conn->c_sasl_bind_mech.bv_val,
1395 op->orb_cred.bv_val, op->orb_cred.bv_len,
1396 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1400 op->orb_cred.bv_val, op->orb_cred.bv_len,
1401 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1404 response.bv_len = reslen;
1406 if ( sc == SASL_OK ) {
1407 sasl_ssf_t *ssf = NULL;
1409 op->orb_edn = op->o_conn->c_sasl_dn;
1410 op->o_conn->c_sasl_dn.bv_val = NULL;
1411 op->o_conn->c_sasl_dn.bv_len = 0;
1412 op->o_conn->c_sasl_done = 1;
1414 rs->sr_err = LDAP_SUCCESS;
1416 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1417 op->orb_ssf = ssf ? *ssf : 0;
1421 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1422 op->o_conn->c_sasl_layers++;
1424 /* If there's an old layer, set sockctx to NULL to
1425 * tell connection_read() to wait for us to finish.
1426 * Otherwise there is a race condition: we have to
1427 * send the Bind response using the old security
1428 * context and then remove it before reading any
1431 if ( op->o_conn->c_sasl_sockctx ) {
1432 ctx = op->o_conn->c_sasl_sockctx;
1433 op->o_conn->c_sasl_sockctx = NULL;
1435 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1437 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1440 /* Must send response using old security layer */
1441 if (response.bv_len) rs->sr_sasldata = &response;
1442 send_ldap_sasl( op, rs );
1444 /* Now dispose of the old security layer.
1447 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1448 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1449 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1450 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1451 sasl_dispose( &ctx );
1453 } else if ( sc == SASL_CONTINUE ) {
1454 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1455 rs->sr_sasldata = &response;
1456 send_ldap_sasl( op, rs );
1459 #if SASL_VERSION_MAJOR >= 2
1460 rs->sr_text = sasl_errdetail( ctx );
1462 rs->sr_err = slap_sasl_err2ldap( sc ),
1463 send_ldap_result( op, rs );
1466 #if SASL_VERSION_MAJOR < 2
1467 if( response.bv_len ) {
1468 ch_free( response.bv_val );
1473 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0 );
1475 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1480 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1481 "SASL not supported" );
1487 char* slap_sasl_secprops( const char *in )
1489 #ifdef HAVE_CYRUS_SASL
1490 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1492 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1494 return "SASL not supported";
1498 #ifdef HAVE_CYRUS_SASL
1500 slap_sasl_setpass( Operation *op, SlapReply *rs )
1502 struct berval id = { 0, NULL }; /* needs to come from connection */
1503 struct berval new = { 0, NULL };
1504 struct berval old = { 0, NULL };
1506 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1508 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1509 (SASL_CONST void **)&id.bv_val );
1511 if( rs->sr_err != SASL_OK ) {
1512 rs->sr_text = "unable to retrieve SASL username";
1513 rs->sr_err = LDAP_OTHER;
1518 LDAP_LOG( BACKEND, ENTRY,
1519 "slap_sasl_setpass: \"%s\"\n",
1520 id.bv_val ? id.bv_val : "", 0, 0);
1522 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1523 id.bv_val ? id.bv_val : "", 0, 0 );
1526 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1527 NULL, &old, &new, &rs->sr_text );
1529 if( rs->sr_err != LDAP_SUCCESS ) {
1533 if( new.bv_len == 0 ) {
1534 slap_passwd_generate(&new);
1536 if( new.bv_len == 0 ) {
1537 rs->sr_text = "password generation failed.";
1538 rs->sr_err = LDAP_OTHER;
1542 rs->sr_rspdata = slap_passwd_return( &new );
1545 #if SASL_VERSION_MAJOR < 2
1546 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1547 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1549 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1550 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1551 if( rs->sr_err != SASL_OK ) {
1552 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1555 switch(rs->sr_err) {
1557 rs->sr_err = LDAP_SUCCESS;
1567 rs->sr_err = LDAP_OTHER;
1573 #endif /* HAVE_CYRUS_SASL */
1575 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1576 string returned in *dn is in its own allocated memory, and must be free'd
1577 by the calling process.
1578 -Mark Adamson, Carnegie Mellon
1580 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1581 on strings passed in directly from SASL.
1582 -Howard Chu, Symas Corp.
1589 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
1591 int slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len,
1592 char *user_realm, struct berval *dn, int flags )
1595 int rc, is_dn = SET_NONE, do_norm = 1;
1599 LDAP_LOG( TRANSPORT, ENTRY,
1600 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1601 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", len );
1603 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s [len=%d]\n",
1604 id ? ( *id ? id : "<empty>" ) : "NULL", len, 0 );
1608 op = conn->c_sasl_bindop;
1615 if ( len == 0 ) len = strlen( id );
1617 /* Blatantly anonymous ID */
1618 if ( len == sizeof("anonymous") - 1 &&
1619 !strcasecmp( id, "anonymous" ) ) {
1620 return( LDAP_SUCCESS );
1626 /* An authcID needs to be converted to authzID form. Set the
1627 * values directly into *dn; they will be normalized later. (and
1628 * normalizing always makes a new copy.) An ID from a TLS certificate
1629 * is already normalized, so copy it and skip normalization.
1631 if( flags & SLAP_GETDN_AUTHCID ) {
1632 if( conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len &&
1633 strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
1635 /* EXTERNAL DNs are already normalized */
1638 ber_str2bv_x( id, len, 1, dn, op->o_tmpmemctx );
1641 /* convert to u:<username> form */
1647 if( is_dn == SET_NONE ) {
1648 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1652 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1659 /* No other possibilities from here */
1660 if( is_dn == SET_NONE ) {
1663 return( LDAP_INAPPROPRIATE_AUTH );
1666 /* Username strings */
1667 if( is_dn == SET_U ) {
1669 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
1671 /* username may have embedded realm name */
1672 if( ( realm = strchr( dn->bv_val, '@') ) ) {
1674 len += sizeof(",cn=")-2;
1675 } else if( user_realm && *user_realm ) {
1676 len += strlen( user_realm ) + sizeof(",cn=")-1;
1679 if( conn->c_sasl_bind_mech.bv_len ) {
1680 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
1683 /* Build the new dn */
1685 dn->bv_val = sl_malloc( len+1, op->o_tmpmemctx );
1686 if( dn->bv_val == NULL ) {
1688 LDAP_LOG( TRANSPORT, ERR,
1689 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1691 Debug( LDAP_DEBUG_ANY,
1692 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1696 p = lutil_strcopy( dn->bv_val, "uid=" );
1697 p = lutil_strncopy( p, c1, dn->bv_len );
1700 int rlen = dn->bv_len - ( realm - c1 );
1701 p = lutil_strcopy( p, ",cn=" );
1702 p = lutil_strncopy( p, realm, rlen );
1704 } else if( user_realm && *user_realm ) {
1705 p = lutil_strcopy( p, ",cn=" );
1706 p = lutil_strcopy( p, user_realm );
1709 if( conn->c_sasl_bind_mech.bv_len ) {
1710 p = lutil_strcopy( p, ",cn=" );
1711 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
1713 p = lutil_strcopy( p, ",cn=auth" );
1714 dn->bv_len = p - dn->bv_val;
1717 LDAP_LOG( TRANSPORT, ENTRY,
1718 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1720 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1724 /* Dup the DN in any case, so we don't risk
1725 * leaks or dangling pointers later,
1726 * and the DN value is '\0' terminated */
1727 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1728 dn->bv_val = dn2.bv_val;
1731 /* All strings are in DN form now. Normalize if needed. */
1733 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1735 /* User DNs were constructed above and must be freed now */
1736 sl_free( dn->bv_val, op->o_tmpmemctx );
1738 if ( rc != LDAP_SUCCESS ) {
1746 /* Run thru regexp */
1747 slap_sasl2dn( op, dn, &dn2 );
1749 sl_free( dn->bv_val, op->o_tmpmemctx );
1752 LDAP_LOG( TRANSPORT, ENTRY,
1753 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1755 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1760 return( LDAP_SUCCESS );