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 proxy 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 "
767 "proxy authorization disallowed (%d)\n",
768 (long)(conn ? conn->c_connid : -1), rc, 0 );
770 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
771 "proxy authorization disallowed (%d)\n",
772 (long) (conn ? conn->c_connid : -1), rc, 0 );
775 sasl_seterror( sconn, 0, "not authorized" );
776 ch_free( authzDN.bv_val );
780 conn->c_sasl_dn = authzDN;
782 if (conn->c_sasl_bindop) {
783 Statslog( LDAP_DEBUG_STATS,
784 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
785 conn->c_connid, conn->c_sasl_bindop->o_opid,
786 auth_identity, 0, 0);
790 LDAP_LOG( TRANSPORT, ENTRY,
791 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
792 (long)(conn ? conn->c_connid : -1), 0, 0 );
794 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
795 " proxy authorization allowed\n",
796 (long) (conn ? conn->c_connid : -1), 0, 0 );
809 struct berval authcDN, authzDN;
811 Connection *conn = context;
815 if ( conn->c_sasl_dn.bv_val ) {
816 ch_free( conn->c_sasl_dn.bv_val );
817 conn->c_sasl_dn.bv_val = NULL;
818 conn->c_sasl_dn.bv_len = 0;
822 LDAP_LOG( TRANSPORT, ENTRY,
823 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
824 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
825 authzid ? authzid : "<empty>" );
827 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
828 "authcid=\"%s\" authzid=\"%s\"\n",
829 (long) (conn ? conn->c_connid : -1),
830 authcid ? authcid : "<empty>",
831 authzid ? authzid : "<empty>" );
834 /* Figure out how much data we have for the dn */
835 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
836 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
838 LDAP_LOG( TRANSPORT, ERR,
839 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
841 Debug(LDAP_DEBUG_TRACE,
842 "authorize: getprop(REALM) failed!\n", 0,0,0);
844 *errstr = "Could not extract realm";
848 /* Convert the identities to DN's. If no authzid was given, client will
849 be bound as the DN matching their username */
850 rc = slap_sasl_getdn( conn, NULL, (char *)authcid, 0, realm,
851 &authcDN, SLAP_GETDN_AUTHCID );
852 if( rc != LDAP_SUCCESS ) {
853 *errstr = ldap_err2string( rc );
856 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
858 LDAP_LOG( TRANSPORT, ENTRY,
859 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
860 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
862 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
863 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
866 conn->c_sasl_dn = authcDN;
869 rc = slap_sasl_getdn( conn, NULL, (char *)authzid, 0, realm,
870 &authzDN, SLAP_GETDN_AUTHZID );
871 if( rc != LDAP_SUCCESS ) {
872 ch_free( authcDN.bv_val );
873 *errstr = ldap_err2string( rc );
877 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
878 ch_free( authcDN.bv_val );
881 LDAP_LOG( TRANSPORT, INFO,
882 "slap_sasl_authorize: conn %ld "
883 "proxy authorization disallowed (%d)\n",
884 (long)(conn ? conn->c_connid : -1), rc, 0 );
886 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
887 "proxy authorization disallowed (%d)\n",
888 (long) (conn ? conn->c_connid : -1), rc, 0 );
891 *errstr = "not authorized";
892 ch_free( authzDN.bv_val );
895 conn->c_sasl_dn = authzDN;
899 LDAP_LOG( TRANSPORT, RESULTS,
900 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
901 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
903 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
904 " authorization allowed\n",
905 (long) (conn ? conn->c_connid : -1), 0, 0 );
908 if (conn->c_sasl_bindop) {
909 Statslog( LDAP_DEBUG_STATS,
910 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
911 conn->c_connid, conn->c_sasl_bindop->o_opid,
918 #endif /* SASL_VERSION_MAJOR >= 2 */
921 slap_sasl_err2ldap( int saslerr )
930 rc = LDAP_SASL_BIND_IN_PROGRESS;
939 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
942 rc = LDAP_INVALID_CREDENTIALS;
945 rc = LDAP_INSUFFICIENT_ACCESS;
949 rc = LDAP_INAPPROPRIATE_AUTH;
960 int slap_sasl_init( void )
962 #ifdef HAVE_CYRUS_SASL
964 static sasl_callback_t server_callbacks[] = {
965 { SASL_CB_LOG, &slap_sasl_log, NULL },
966 { SASL_CB_LIST_END, NULL, NULL }
969 #ifdef HAVE_SASL_VERSION
970 /* stringify the version number, sasl.h doesn't do it for us */
971 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
972 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
973 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
976 sasl_version( NULL, &rc );
977 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
978 (rc & 0xffff) < SASL_VERSION_STEP) {
979 char version[sizeof("xxx.xxx.xxxxx")];
980 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
983 LDAP_LOG( TRANSPORT, INFO,
984 "slap_sasl_init: SASL library version mismatch:"
985 " expected " SASL_VERSION_STRING ","
986 " got %s\n", version, 0, 0 );
988 Debug( LDAP_DEBUG_ANY,
989 "slap_sasl_init: SASL library version mismatch:"
990 " expected " SASL_VERSION_STRING ","
991 " got %s\n", version, 0, 0 );
997 /* SASL 2 does its own memory management internally */
998 #if SASL_VERSION_MAJOR < 2
1007 ldap_pvt_sasl_mutex_new,
1008 ldap_pvt_sasl_mutex_lock,
1009 ldap_pvt_sasl_mutex_unlock,
1010 ldap_pvt_sasl_mutex_dispose );
1012 #if SASL_VERSION_MAJOR >= 2
1013 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1015 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1017 /* should provide callbacks for logging */
1018 /* server name should be configurable */
1019 rc = sasl_server_init( server_callbacks, "slapd" );
1021 if( rc != SASL_OK ) {
1023 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1025 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1028 #if SASL_VERSION_MAJOR < 2
1029 /* A no-op used to make sure we linked with Cyrus 1.5 */
1030 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1037 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1039 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1044 /* default security properties */
1045 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1046 sasl_secprops.max_ssf = INT_MAX;
1047 sasl_secprops.maxbufsize = 65536;
1048 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1054 int slap_sasl_destroy( void )
1056 #ifdef HAVE_CYRUS_SASL
1059 free( global_host );
1065 int slap_sasl_open( Connection *conn, int reopen )
1067 int cb, sc = LDAP_SUCCESS;
1068 #if SASL_VERSION_MAJOR >= 2
1069 char *ipremoteport = NULL, *iplocalport = NULL;
1072 #ifdef HAVE_CYRUS_SASL
1073 sasl_conn_t *ctx = NULL;
1074 sasl_callback_t *session_callbacks;
1076 assert( conn->c_sasl_authctx == NULL );
1079 assert( conn->c_sasl_extra == NULL );
1082 #if SASL_VERSION_MAJOR >= 2
1083 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1085 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1087 if( session_callbacks == NULL ) {
1089 LDAP_LOG( TRANSPORT, ERR,
1090 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1092 Debug( LDAP_DEBUG_ANY,
1093 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1097 conn->c_sasl_extra = session_callbacks;
1099 session_callbacks[cb=0].id = SASL_CB_LOG;
1100 session_callbacks[cb].proc = &slap_sasl_log;
1101 session_callbacks[cb++].context = conn;
1103 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1104 session_callbacks[cb].proc = &slap_sasl_authorize;
1105 session_callbacks[cb++].context = conn;
1107 #if SASL_VERSION_MAJOR >= 2
1108 session_callbacks[cb].id = SASL_CB_CANON_USER;
1109 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1110 session_callbacks[cb++].context = conn;
1112 /* XXXX: this should be conditional */
1113 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1114 session_callbacks[cb].proc = &slap_sasl_checkpass;
1115 session_callbacks[cb++].context = conn;
1118 session_callbacks[cb].id = SASL_CB_LIST_END;
1119 session_callbacks[cb].proc = NULL;
1120 session_callbacks[cb++].context = NULL;
1122 session_callbacks = conn->c_sasl_extra;
1125 conn->c_sasl_layers = 0;
1127 if( global_host == NULL ) {
1128 global_host = ldap_pvt_get_fqdn( NULL );
1131 /* create new SASL context */
1132 #if SASL_VERSION_MAJOR >= 2
1133 if ( conn->c_sock_name.bv_len != 0 &&
1134 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1137 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1138 /* Convert IPv6 addresses to address;port syntax. */
1139 p = strrchr( iplocalport, ' ' );
1140 /* Convert IPv4 addresses to address;port syntax. */
1141 if ( p == NULL ) p = strchr( iplocalport, ':' );
1146 if ( conn->c_peer_name.bv_len != 0 &&
1147 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1150 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1151 /* Convert IPv6 addresses to address;port syntax. */
1152 p = strrchr( ipremoteport, ' ' );
1153 /* Convert IPv4 addresses to address;port syntax. */
1154 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1159 sc = sasl_server_new( "ldap", global_host, global_realm,
1160 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1161 if ( iplocalport != NULL ) {
1162 ch_free( iplocalport );
1164 if ( ipremoteport != NULL ) {
1165 ch_free( ipremoteport );
1168 sc = sasl_server_new( "ldap", global_host, global_realm,
1169 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1172 if( sc != SASL_OK ) {
1174 LDAP_LOG( TRANSPORT, ERR,
1175 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1177 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1184 conn->c_sasl_authctx = ctx;
1186 if( sc == SASL_OK ) {
1187 sc = sasl_setprop( ctx,
1188 SASL_SEC_PROPS, &sasl_secprops );
1190 if( sc != SASL_OK ) {
1192 LDAP_LOG( TRANSPORT, ERR,
1193 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1195 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1199 slap_sasl_close( conn );
1204 sc = slap_sasl_err2ldap( sc );
1209 int slap_sasl_external(
1212 const char *auth_id )
1214 #if SASL_VERSION_MAJOR >= 2
1216 sasl_conn_t *ctx = conn->c_sasl_authctx;
1218 if ( ctx == NULL ) {
1219 return LDAP_UNAVAILABLE;
1222 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1224 if ( sc != SASL_OK ) {
1228 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1230 if ( sc != SASL_OK ) {
1234 #elif defined(HAVE_CYRUS_SASL)
1236 sasl_conn_t *ctx = conn->c_sasl_authctx;
1237 sasl_external_properties_t extprops;
1239 if ( ctx == NULL ) {
1240 return LDAP_UNAVAILABLE;
1243 memset( &extprops, '\0', sizeof(extprops) );
1245 extprops.auth_id = (char *) auth_id;
1247 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1248 (void *) &extprops );
1250 if ( sc != SASL_OK ) {
1255 return LDAP_SUCCESS;
1258 int slap_sasl_reset( Connection *conn )
1260 return LDAP_SUCCESS;
1263 char ** slap_sasl_mechs( Connection *conn )
1265 char **mechs = NULL;
1267 #ifdef HAVE_CYRUS_SASL
1268 sasl_conn_t *ctx = conn->c_sasl_authctx;
1270 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1274 SASL_CONST char *mechstr;
1276 sc = sasl_listmech( ctx,
1277 NULL, NULL, ",", NULL,
1278 &mechstr, NULL, NULL );
1280 if( sc != SASL_OK ) {
1282 LDAP_LOG( TRANSPORT, ERR,
1283 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1285 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1292 mechs = ldap_str2charray( mechstr, "," );
1294 #if SASL_VERSION_MAJOR < 2
1303 int slap_sasl_close( Connection *conn )
1305 #ifdef HAVE_CYRUS_SASL
1306 sasl_conn_t *ctx = conn->c_sasl_authctx;
1309 sasl_dispose( &ctx );
1311 if ( conn->c_sasl_sockctx && conn->c_sasl_authctx != conn->c_sasl_sockctx ) {
1312 ctx = conn->c_sasl_sockctx;
1313 sasl_dispose( &ctx );
1316 conn->c_sasl_authctx = NULL;
1317 conn->c_sasl_sockctx = NULL;
1318 conn->c_sasl_done = 0;
1320 free( conn->c_sasl_extra );
1321 conn->c_sasl_extra = NULL;
1324 return LDAP_SUCCESS;
1327 int slap_sasl_bind( Operation *op, SlapReply *rs )
1329 #ifdef HAVE_CYRUS_SASL
1330 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1331 struct berval response;
1332 unsigned reslen = 0;
1336 LDAP_LOG( TRANSPORT, ENTRY,
1337 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1338 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1339 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1340 op->o_conn->c_sasl_bind_mech.bv_val,
1341 op->orb_cred.bv_len );
1343 Debug(LDAP_DEBUG_ARGS,
1344 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1345 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1346 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1347 op->o_conn->c_sasl_bind_mech.bv_val,
1348 op->orb_cred.bv_len );
1353 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1354 "SASL unavailable on this session" );
1358 #if SASL_VERSION_MAJOR >= 2
1359 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1360 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1361 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1362 sasl_server_step( ctx, cred, clen, resp, rlen )
1364 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1365 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1366 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1367 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1370 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1371 /* If we already authenticated once, must use a new context */
1372 if ( op->o_conn->c_sasl_done ) {
1374 const char *authid = NULL;
1375 #if SASL_VERSION_MAJOR >= 2
1376 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1377 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1378 if ( authid ) authid = ch_strdup( authid );
1380 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1381 sasl_dispose( &ctx );
1383 op->o_conn->c_sasl_authctx = NULL;
1385 slap_sasl_open( op->o_conn, 1 );
1386 ctx = op->o_conn->c_sasl_authctx;
1387 #if SASL_VERSION_MAJOR >= 2
1389 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1390 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1391 ch_free( (char *)authid );
1396 op->o_conn->c_sasl_bind_mech.bv_val,
1397 op->orb_cred.bv_val, op->orb_cred.bv_len,
1398 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1402 op->orb_cred.bv_val, op->orb_cred.bv_len,
1403 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1406 response.bv_len = reslen;
1408 if ( sc == SASL_OK ) {
1409 sasl_ssf_t *ssf = NULL;
1411 op->orb_edn = op->o_conn->c_sasl_dn;
1412 op->o_conn->c_sasl_dn.bv_val = NULL;
1413 op->o_conn->c_sasl_dn.bv_len = 0;
1414 op->o_conn->c_sasl_done = 1;
1416 rs->sr_err = LDAP_SUCCESS;
1418 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1419 op->orb_ssf = ssf ? *ssf : 0;
1423 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1424 op->o_conn->c_sasl_layers++;
1426 /* If there's an old layer, set sockctx to NULL to
1427 * tell connection_read() to wait for us to finish.
1428 * Otherwise there is a race condition: we have to
1429 * send the Bind response using the old security
1430 * context and then remove it before reading any
1433 if ( op->o_conn->c_sasl_sockctx ) {
1434 ctx = op->o_conn->c_sasl_sockctx;
1435 op->o_conn->c_sasl_sockctx = NULL;
1437 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1439 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1442 /* Must send response using old security layer */
1443 if (response.bv_len) rs->sr_sasldata = &response;
1444 send_ldap_sasl( op, rs );
1446 /* Now dispose of the old security layer.
1449 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1450 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1451 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1452 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1453 sasl_dispose( &ctx );
1455 } else if ( sc == SASL_CONTINUE ) {
1456 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1457 rs->sr_sasldata = &response;
1458 send_ldap_sasl( op, rs );
1461 #if SASL_VERSION_MAJOR >= 2
1462 rs->sr_text = sasl_errdetail( ctx );
1464 rs->sr_err = slap_sasl_err2ldap( sc ),
1465 send_ldap_result( op, rs );
1468 #if SASL_VERSION_MAJOR < 2
1469 if( response.bv_len ) {
1470 ch_free( response.bv_val );
1475 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0 );
1477 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1482 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1483 "SASL not supported" );
1489 char* slap_sasl_secprops( const char *in )
1491 #ifdef HAVE_CYRUS_SASL
1492 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1494 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1496 return "SASL not supported";
1500 #ifdef HAVE_CYRUS_SASL
1502 slap_sasl_setpass( Operation *op, SlapReply *rs )
1504 struct berval id = { 0, NULL }; /* needs to come from connection */
1505 struct berval new = { 0, NULL };
1506 struct berval old = { 0, NULL };
1508 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1510 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1511 (SASL_CONST void **)&id.bv_val );
1513 if( rs->sr_err != SASL_OK ) {
1514 rs->sr_text = "unable to retrieve SASL username";
1515 rs->sr_err = LDAP_OTHER;
1520 LDAP_LOG( BACKEND, ENTRY,
1521 "slap_sasl_setpass: \"%s\"\n",
1522 id.bv_val ? id.bv_val : "", 0, 0);
1524 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1525 id.bv_val ? id.bv_val : "", 0, 0 );
1528 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1529 NULL, &old, &new, &rs->sr_text );
1531 if( rs->sr_err != LDAP_SUCCESS ) {
1535 if( new.bv_len == 0 ) {
1536 slap_passwd_generate(&new);
1538 if( new.bv_len == 0 ) {
1539 rs->sr_text = "password generation failed.";
1540 rs->sr_err = LDAP_OTHER;
1544 rs->sr_rspdata = slap_passwd_return( &new );
1547 #if SASL_VERSION_MAJOR < 2
1548 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1549 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1551 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1552 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1553 if( rs->sr_err != SASL_OK ) {
1554 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1557 switch(rs->sr_err) {
1559 rs->sr_err = LDAP_SUCCESS;
1569 rs->sr_err = LDAP_OTHER;
1575 #endif /* HAVE_CYRUS_SASL */
1577 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1578 string returned in *dn is in its own allocated memory, and must be free'd
1579 by the calling process.
1580 -Mark Adamson, Carnegie Mellon
1582 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1583 on strings passed in directly from SASL.
1584 -Howard Chu, Symas Corp.
1591 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
1593 int slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len,
1594 char *user_realm, struct berval *dn, int flags )
1597 int rc, is_dn = SET_NONE, do_norm = 1;
1601 LDAP_LOG( TRANSPORT, ENTRY,
1602 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1603 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", len );
1605 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s [len=%d]\n",
1606 id ? ( *id ? id : "<empty>" ) : "NULL", len, 0 );
1610 op = conn->c_sasl_bindop;
1617 if ( len == 0 ) len = strlen( id );
1619 /* Blatantly anonymous ID */
1620 if ( len == sizeof("anonymous") - 1 &&
1621 !strcasecmp( id, "anonymous" ) ) {
1622 return( LDAP_SUCCESS );
1628 /* An authcID needs to be converted to authzID form. Set the
1629 * values directly into *dn; they will be normalized later. (and
1630 * normalizing always makes a new copy.) An ID from a TLS certificate
1631 * is already normalized, so copy it and skip normalization.
1633 if( flags & SLAP_GETDN_AUTHCID ) {
1634 if( conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len &&
1635 strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
1637 /* EXTERNAL DNs are already normalized */
1640 ber_str2bv_x( id, len, 1, dn, op->o_tmpmemctx );
1643 /* convert to u:<username> form */
1649 if( is_dn == SET_NONE ) {
1650 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1654 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1661 /* No other possibilities from here */
1662 if( is_dn == SET_NONE ) {
1665 return( LDAP_INAPPROPRIATE_AUTH );
1668 /* Username strings */
1669 if( is_dn == SET_U ) {
1671 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
1673 /* username may have embedded realm name */
1674 if( ( realm = strchr( dn->bv_val, '@') ) ) {
1676 len += sizeof(",cn=")-2;
1677 } else if( user_realm && *user_realm ) {
1678 len += strlen( user_realm ) + sizeof(",cn=")-1;
1681 if( conn->c_sasl_bind_mech.bv_len ) {
1682 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
1685 /* Build the new dn */
1687 dn->bv_val = sl_malloc( len+1, op->o_tmpmemctx );
1688 if( dn->bv_val == NULL ) {
1690 LDAP_LOG( TRANSPORT, ERR,
1691 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1693 Debug( LDAP_DEBUG_ANY,
1694 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1698 p = lutil_strcopy( dn->bv_val, "uid=" );
1699 p = lutil_strncopy( p, c1, dn->bv_len );
1702 int rlen = dn->bv_len - ( realm - c1 );
1703 p = lutil_strcopy( p, ",cn=" );
1704 p = lutil_strncopy( p, realm, rlen );
1706 } else if( user_realm && *user_realm ) {
1707 p = lutil_strcopy( p, ",cn=" );
1708 p = lutil_strcopy( p, user_realm );
1711 if( conn->c_sasl_bind_mech.bv_len ) {
1712 p = lutil_strcopy( p, ",cn=" );
1713 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
1715 p = lutil_strcopy( p, ",cn=auth" );
1716 dn->bv_len = p - dn->bv_val;
1719 LDAP_LOG( TRANSPORT, ENTRY,
1720 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1722 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1726 /* Dup the DN in any case, so we don't risk
1727 * leaks or dangling pointers later,
1728 * and the DN value is '\0' terminated */
1729 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1730 dn->bv_val = dn2.bv_val;
1733 /* All strings are in DN form now. Normalize if needed. */
1735 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1737 /* User DNs were constructed above and must be freed now */
1738 sl_free( dn->bv_val, op->o_tmpmemctx );
1740 if ( rc != LDAP_SUCCESS ) {
1748 /* Run thru regexp */
1749 slap_sasl2dn( op, dn, &dn2 );
1751 sl_free( dn->bv_val, op->o_tmpmemctx );
1754 LDAP_LOG( TRANSPORT, ENTRY,
1755 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1757 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1762 return( LDAP_SUCCESS );