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 # ifdef HAVE_SASL_SASL_H
29 # include <sasl/saslplug.h>
31 # include <saslplug.h>
33 # define SASL_CONST const
38 static sasl_security_properties_t sasl_secprops;
39 #endif /* HAVE_CYRUS_SASL */
45 int slap_sasl_config( int cargc, char **cargv, char *line,
46 const char *fname, int lineno )
48 /* set SASL proxy authorization policy */
49 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
52 LDAP_LOG( CONFIG, CRIT,
53 "%s: line %d: missing policy in"
54 " \"sasl-authz-policy <policy>\" line\n",
57 Debug( LDAP_DEBUG_ANY,
58 "%s: line %d: missing policy in"
59 " \"sasl-authz-policy <policy>\" line\n",
65 if ( slap_sasl_setpolicy( cargv[1] ) ) {
67 LDAP_LOG( CONFIG, CRIT,
68 "%s: line %d: unable "
69 "to parse value \"%s\" "
70 "in \"sasl-authz-policy "
72 fname, lineno, cargv[1] );
74 Debug( LDAP_DEBUG_ANY,
75 "%s: line %d: unable "
76 "to parse value \"%s\" "
77 "in \"sasl-authz-policy "
79 fname, lineno, cargv[1] );
84 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
85 || !strcasecmp( cargv[0], "saslregexp" ) )
90 LDAP_LOG( CONFIG, CRIT,
91 "%s: line %d: need 2 args in "
92 "\"saslregexp <match> <replace>\"\n",
95 Debug( LDAP_DEBUG_ANY,
96 "%s: line %d: need 2 args in "
97 "\"saslregexp <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 ) {
113 LDAP_LOG( CONFIG, CRIT,
114 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
117 Debug( LDAP_DEBUG_ANY,
118 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
125 if ( global_host != NULL ) {
127 LDAP_LOG( CONFIG, CRIT,
128 "%s: line %d: already set sasl-host!\n",
131 Debug( LDAP_DEBUG_ANY,
132 "%s: line %d: already set sasl-host!\n",
139 global_host = ch_strdup( cargv[1] );
143 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
146 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
147 "missing realm in \"sasl-realm <realm>\" line.\n",
150 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
151 "missing realm in \"sasl-realm <realm>\" line.\n",
158 if ( global_realm != NULL ) {
160 LDAP_LOG( CONFIG, CRIT,
161 "%s: line %d: already set sasl-realm!\n",
164 Debug( LDAP_DEBUG_ANY,
165 "%s: line %d: already set sasl-realm!\n",
172 global_realm = ch_strdup( cargv[1] );
175 /* SASL security properties */
176 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
181 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
182 "missing flags in \"sasl-secprops <properties>\" line\n",
185 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
186 "missing flags in \"sasl-secprops <properties>\" line\n",
193 txt = slap_sasl_secprops( cargv[1] );
196 LDAP_LOG( CONFIG, CRIT,
197 "%s: line %d sasl-secprops: %s\n",
198 fname, lineno, txt );
200 Debug( LDAP_DEBUG_ANY,
201 "%s: line %d: sasl-secprops: %s\n",
202 fname, lineno, txt );
207 #endif /* HAVE_CYRUS_SASL */
213 #ifdef HAVE_CYRUS_SASL
221 Connection *conn = context;
225 if ( message == NULL ) {
226 return SASL_BADPARAM;
230 #if SASL_VERSION_MAJOR >= 2
232 level = LDAP_DEBUG_NONE;
236 level = LDAP_DEBUG_ANY;
240 level = LDAP_DEBUG_ANY;
244 level = LDAP_DEBUG_TRACE;
248 level = LDAP_DEBUG_TRACE;
252 level = LDAP_DEBUG_TRACE;
256 level = LDAP_DEBUG_TRACE;
260 level = LDAP_DEBUG_TRACE;
261 label = "Password Trace";
265 level = LDAP_DEBUG_ANY;
268 case SASL_LOG_WARNING:
269 level = LDAP_DEBUG_TRACE;
273 level = LDAP_DEBUG_TRACE;
278 return SASL_BADPARAM;
282 LDAP_LOG( TRANSPORT, ENTRY,
283 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
285 Debug( level, "SASL [conn=%ld] %s: %s\n",
286 conn ? conn->c_connid: -1,
295 #if SASL_VERSION_MAJOR >= 2
296 static const char *slap_propnames[] = {
297 "*slapConn", "*authcDN", "*authzDN", NULL };
299 static Filter generic_filter = { LDAP_FILTER_PRESENT };
305 typedef struct lookup_info {
308 const struct propval *list;
309 sasl_server_params_t *sparams;
312 static slap_response sasl_ap_lookup, sasl_cb_checkpass;
315 sasl_ap_lookup( Operation *op, SlapReply *rs )
318 AttributeDescription *ad;
322 slap_callback *tmp = op->o_callback;
323 lookup_info *sl = tmp->sc_private;
325 if (rs->sr_type != REP_SEARCH) return 0;
327 for( i = 0; i < sl->last; i++ ) {
328 const char *name = sl->list[i].name;
330 if ( name[0] == '*' ) {
331 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
333 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
336 if ( sl->list[i].values ) {
337 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
340 rc = slap_str2ad( name, &ad, &text );
341 if ( rc != LDAP_SUCCESS ) {
343 LDAP_LOG( TRANSPORT, DETAIL1,
344 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
346 Debug( LDAP_DEBUG_TRACE,
347 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
351 a = attr_find( rs->sr_entry->e_attrs, ad );
353 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
356 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
357 sl->sparams->utils->prop_erase( sl->sparams->propctx,
360 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
361 sl->sparams->utils->prop_set( sl->sparams->propctx,
362 sl->list[i].name, bv->bv_val, bv->bv_len );
371 sasl_server_params_t *sparams,
378 Connection *conn = NULL;
381 sl.list = sparams->utils->prop_get( sparams->propctx );
382 sl.sparams = sparams;
385 /* Find our DN and conn first */
386 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
387 if ( sl.list[i].name[0] == '*' ) {
388 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
389 if ( sl.list[i].values && sl.list[i].values[0] )
390 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
391 if ( !sl.last ) sl.last = i;
393 if ( (flags & SASL_AUXPROP_AUTHZID) &&
394 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
396 if ( sl.list[i].values && sl.list[i].values[0] )
397 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
398 if ( !sl.last ) sl.last = i;
401 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
402 if ( !sl.last ) sl.last = i;
403 if ( sl.list[i].values && sl.list[i].values[0] ) {
404 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
405 if ( !(flags & SASL_AUXPROP_AUTHZID) )
412 /* Now see what else needs to be fetched */
413 for( i = 0; i < sl.last; i++ ) {
414 const char *name = sl.list[i].name;
416 if ( name[0] == '*' ) {
417 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
419 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
422 if ( sl.list[i].values ) {
423 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
429 slap_callback cb = { sasl_ap_lookup, NULL };
433 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
435 if ( op.o_bd && op.o_bd->be_search ) {
436 SlapReply rs = {REP_RESULT};
437 op.o_tag = LDAP_REQ_SEARCH;
438 op.o_protocol = LDAP_VERSION3;
439 op.o_ndn = conn->c_ndn;
441 op.o_time = slap_get_time();
442 op.o_do_not_cache = 1;
443 op.o_is_auth_check = 1;
444 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
445 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
446 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
448 op.o_pb = conn->c_sasl_bindop->o_pb;
451 op.o_connid = conn->c_connid;
452 op.ors_scope = LDAP_SCOPE_BASE;
453 op.ors_deref = LDAP_DEREF_NEVER;
455 op.ors_filter = &generic_filter;
457 op.o_bd->be_search( &op, &rs );
462 static sasl_auxprop_plug_t slap_auxprop_plugin = {
465 NULL, /* glob_context */
466 NULL, /* auxprop_free */
474 const sasl_utils_t *utils,
477 sasl_auxprop_plug_t **plug,
478 const char *plugname)
480 if ( !out_version | !plug ) return SASL_BADPARAM;
482 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
484 *out_version = SASL_AUXPROP_PLUG_VERSION;
485 *plug = &slap_auxprop_plugin;
489 typedef struct checkpass_info {
495 sasl_cb_checkpass( Operation *op, SlapReply *rs )
497 slap_callback *tmp = op->o_callback;
498 checkpass_info *ci = tmp->sc_private;
502 if (rs->sr_type != REP_SEARCH) return 0;
504 ci->rc = SASL_NOVERIFY;
506 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_userPassword );
508 if ( ! access_allowed( op, rs->sr_entry, slap_schema.si_ad_userPassword,
509 NULL, ACL_AUTH, NULL ) ) return 0;
511 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
512 if ( !lutil_passwd( bv, &ci->cred, NULL, &rs->sr_text ) ) {
524 const char *username,
527 struct propctx *propctx)
529 Connection *conn = (Connection *)context;
536 /* SASL will fallback to its own mechanisms if we don't
537 * find an answer here.
540 rc = slap_sasl_getdn( conn, NULL, (char *)username, 0, NULL, &op.o_req_ndn,
541 SLAP_GETDN_AUTHCID );
542 if ( rc != LDAP_SUCCESS ) {
543 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
547 if ( op.o_req_ndn.bv_len == 0 ) {
548 sasl_seterror( sconn, 0,
549 "No password is associated with the Root DSE" );
550 if ( op.o_req_ndn.bv_val != NULL ) {
551 ch_free( op.o_req_ndn.bv_val );
556 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
557 if ( op.o_bd && op.o_bd->be_search ) {
558 slap_callback cb = { sasl_cb_checkpass, NULL };
559 SlapReply rs = {REP_RESULT};
561 ci.cred.bv_val = (char *)pass;
562 ci.cred.bv_len = passlen;
565 op.o_tag = LDAP_REQ_SEARCH;
566 op.o_protocol = LDAP_VERSION3;
567 op.o_ndn = conn->c_ndn;
569 op.o_time = slap_get_time();
570 op.o_do_not_cache = 1;
571 op.o_is_auth_check = 1;
572 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
573 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
574 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
576 op.o_pb = conn->c_sasl_bindop->o_pb;
579 op.o_connid = conn->c_connid;
580 op.ors_scope = LDAP_SCOPE_BASE;
581 op.ors_deref = LDAP_DEREF_NEVER;
583 op.ors_filter = &generic_filter;
585 op.o_bd->be_search( &op, &rs );
587 if ( ci.rc != SASL_OK ) {
588 sasl_seterror( sconn, 0,
589 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
592 ch_free( op.o_req_ndn.bv_val );
597 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
598 * auxiliary property, so that we can refer to it in sasl_authorize
599 * without interfering with anything else. Also, the SASL username
600 * buffer is constrained to 256 characters, and our DNs could be
601 * much longer (totally arbitrary length)...
604 slap_sasl_canonicalize(
610 const char *user_realm,
615 Connection *conn = (Connection *)context;
616 struct propctx *props = sasl_auxprop_getctx( sconn );
617 struct propval auxvals[3];
620 const char *names[2];
625 LDAP_LOG( TRANSPORT, ENTRY,
626 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
627 conn ? conn->c_connid : -1,
628 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
629 in ? in : "<empty>");
631 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
632 conn ? conn->c_connid : -1,
633 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
634 in ? in : "<empty>");
637 /* If name is too big, just truncate. We don't care, we're
638 * using DNs, not the usernames.
640 if ( inlen > out_max )
643 /* See if we need to add request, can only do it once */
644 prop_getnames( props, slap_propnames, auxvals );
645 if ( !auxvals[0].name )
646 prop_request( props, slap_propnames );
648 if ( flags & SASL_CU_AUTHID )
653 /* Need to store the Connection for auxprop_lookup */
654 if ( !auxvals[PROP_CONN].values ) {
655 names[0] = slap_propnames[PROP_CONN];
657 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
660 /* Already been here? */
661 if ( auxvals[which].values )
664 /* Normally we require an authzID to have a u: or dn: prefix.
665 * However, SASL frequently gives us an authzID that is just
666 * an exact copy of the authcID, without a prefix. We need to
667 * detect and allow this condition. If SASL calls canonicalize
668 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
669 * But if it's broken into two calls, we need to remember the
670 * authcID so that we can compare the authzID later. We store
671 * the authcID temporarily in conn->c_sasl_dn. We necessarily
672 * finish Canonicalizing before Authorizing, so there is no
673 * conflict with slap_sasl_authorize's use of this temp var.
675 * The SASL EXTERNAL mech is backwards from all the other mechs,
676 * it does authzID before the authcID. If we see that authzID
677 * has already been done, don't do anything special with authcID.
679 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
680 conn->c_sasl_dn.bv_val = (char *) in;
681 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
682 rc = strcmp( in, conn->c_sasl_dn.bv_val );
683 conn->c_sasl_dn.bv_val = NULL;
684 /* They were equal, no work needed */
685 if ( !rc ) goto done;
688 rc = slap_sasl_getdn( conn, NULL, (char *)in, inlen, (char *)user_realm, &dn,
689 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
690 if ( rc != LDAP_SUCCESS ) {
691 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
695 names[0] = slap_propnames[which];
698 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
701 LDAP_LOG( TRANSPORT, ENTRY,
702 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
703 conn ? conn->c_connid : -1, names[0]+1,
704 dn.bv_val ? dn.bv_val : "<EMPTY>" );
706 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
707 conn ? conn->c_connid : -1, names[0]+1,
708 dn.bv_val ? dn.bv_val : "<EMPTY>" );
712 AC_MEMCPY( out, in, inlen );
724 char *requested_user,
728 const char *def_realm,
730 struct propctx *props)
732 Connection *conn = (Connection *)context;
733 struct propval auxvals[3];
734 struct berval authcDN, authzDN;
738 LDAP_LOG( TRANSPORT, ENTRY,
739 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
740 conn ? conn->c_connid : -1, auth_identity, requested_user);
742 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
743 "authcid=\"%s\" authzid=\"%s\"\n",
744 conn ? conn->c_connid : -1, auth_identity, requested_user );
746 if ( conn->c_sasl_dn.bv_val ) {
747 ch_free( conn->c_sasl_dn.bv_val );
748 conn->c_sasl_dn.bv_val = NULL;
749 conn->c_sasl_dn.bv_len = 0;
753 prop_getnames( props, slap_propnames+1, auxvals );
755 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
757 /* Nothing to do if no authzID was given */
758 if ( !auxvals[1].name || !auxvals[1].values ) {
759 conn->c_sasl_dn = authcDN;
763 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
765 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
766 ch_free( authcDN.bv_val );
767 if ( rc != LDAP_SUCCESS ) {
769 LDAP_LOG( TRANSPORT, INFO,
770 "slap_sasl_authorize: conn %ld "
771 "proxy authorization disallowed (%d)\n",
772 (long)(conn ? conn->c_connid : -1), rc, 0 );
774 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
775 "proxy authorization disallowed (%d)\n",
776 (long) (conn ? conn->c_connid : -1), rc, 0 );
779 sasl_seterror( sconn, 0, "not authorized" );
780 ch_free( authzDN.bv_val );
784 conn->c_sasl_dn = authzDN;
786 if (conn->c_sasl_bindop) {
787 Statslog( LDAP_DEBUG_STATS,
788 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
789 conn->c_connid, conn->c_sasl_bindop->o_opid,
790 auth_identity, 0, 0);
794 LDAP_LOG( TRANSPORT, ENTRY,
795 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
796 (long)(conn ? conn->c_connid : -1), 0, 0 );
798 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
799 " proxy authorization allowed\n",
800 (long) (conn ? conn->c_connid : -1), 0, 0 );
813 struct berval authcDN, authzDN;
815 Connection *conn = context;
819 if ( conn->c_sasl_dn.bv_val ) {
820 ch_free( conn->c_sasl_dn.bv_val );
821 conn->c_sasl_dn.bv_val = NULL;
822 conn->c_sasl_dn.bv_len = 0;
826 LDAP_LOG( TRANSPORT, ENTRY,
827 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
828 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
829 authzid ? authzid : "<empty>" );
831 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
832 "authcid=\"%s\" authzid=\"%s\"\n",
833 (long) (conn ? conn->c_connid : -1),
834 authcid ? authcid : "<empty>",
835 authzid ? authzid : "<empty>" );
838 /* Figure out how much data we have for the dn */
839 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
840 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
842 LDAP_LOG( TRANSPORT, ERR,
843 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
845 Debug(LDAP_DEBUG_TRACE,
846 "authorize: getprop(REALM) failed!\n", 0,0,0);
848 *errstr = "Could not extract realm";
852 /* Convert the identities to DN's. If no authzid was given, client will
853 be bound as the DN matching their username */
854 rc = slap_sasl_getdn( conn, NULL, (char *)authcid, 0, realm,
855 &authcDN, SLAP_GETDN_AUTHCID );
856 if( rc != LDAP_SUCCESS ) {
857 *errstr = ldap_err2string( rc );
860 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
862 LDAP_LOG( TRANSPORT, ENTRY,
863 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
864 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
866 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
867 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
870 conn->c_sasl_dn = authcDN;
873 rc = slap_sasl_getdn( conn, NULL, (char *)authzid, 0, realm,
874 &authzDN, SLAP_GETDN_AUTHZID );
875 if( rc != LDAP_SUCCESS ) {
876 ch_free( authcDN.bv_val );
877 *errstr = ldap_err2string( rc );
881 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
882 ch_free( authcDN.bv_val );
885 LDAP_LOG( TRANSPORT, INFO,
886 "slap_sasl_authorize: conn %ld "
887 "proxy authorization disallowed (%d)\n",
888 (long)(conn ? conn->c_connid : -1), rc, 0 );
890 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
891 "proxy authorization disallowed (%d)\n",
892 (long) (conn ? conn->c_connid : -1), rc, 0 );
895 *errstr = "not authorized";
896 ch_free( authzDN.bv_val );
899 conn->c_sasl_dn = authzDN;
903 LDAP_LOG( TRANSPORT, RESULTS,
904 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
905 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
907 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
908 " authorization allowed\n",
909 (long) (conn ? conn->c_connid : -1), 0, 0 );
912 if (conn->c_sasl_bindop) {
913 Statslog( LDAP_DEBUG_STATS,
914 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
915 conn->c_connid, conn->c_sasl_bindop->o_opid,
922 #endif /* SASL_VERSION_MAJOR >= 2 */
925 slap_sasl_err2ldap( int saslerr )
934 rc = LDAP_SASL_BIND_IN_PROGRESS;
943 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
946 rc = LDAP_INVALID_CREDENTIALS;
949 rc = LDAP_INSUFFICIENT_ACCESS;
953 rc = LDAP_INAPPROPRIATE_AUTH;
964 int slap_sasl_init( void )
966 #ifdef HAVE_CYRUS_SASL
968 static sasl_callback_t server_callbacks[] = {
969 { SASL_CB_LOG, &slap_sasl_log, NULL },
970 { SASL_CB_LIST_END, NULL, NULL }
973 #ifdef HAVE_SASL_VERSION
974 /* stringify the version number, sasl.h doesn't do it for us */
975 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
976 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
977 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
980 sasl_version( NULL, &rc );
981 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
982 (rc & 0xffff) < SASL_VERSION_STEP) {
983 char version[sizeof("xxx.xxx.xxxxx")];
984 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
987 LDAP_LOG( TRANSPORT, INFO,
988 "slap_sasl_init: SASL library version mismatch:"
989 " expected " SASL_VERSION_STRING ","
990 " got %s\n", version, 0, 0 );
992 Debug( LDAP_DEBUG_ANY,
993 "slap_sasl_init: SASL library version mismatch:"
994 " expected " SASL_VERSION_STRING ","
995 " got %s\n", version, 0, 0 );
1001 /* SASL 2 does its own memory management internally */
1002 #if SASL_VERSION_MAJOR < 2
1011 ldap_pvt_sasl_mutex_new,
1012 ldap_pvt_sasl_mutex_lock,
1013 ldap_pvt_sasl_mutex_unlock,
1014 ldap_pvt_sasl_mutex_dispose );
1016 #if SASL_VERSION_MAJOR >= 2
1017 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1019 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1021 /* should provide callbacks for logging */
1022 /* server name should be configurable */
1023 rc = sasl_server_init( server_callbacks, "slapd" );
1025 if( rc != SASL_OK ) {
1027 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1029 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1032 #if SASL_VERSION_MAJOR < 2
1033 /* A no-op used to make sure we linked with Cyrus 1.5 */
1034 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1041 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1043 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1048 /* default security properties */
1049 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1050 sasl_secprops.max_ssf = INT_MAX;
1051 sasl_secprops.maxbufsize = 65536;
1052 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1058 int slap_sasl_destroy( void )
1060 #ifdef HAVE_CYRUS_SASL
1063 free( global_host );
1069 int slap_sasl_open( Connection *conn, int reopen )
1071 int cb, sc = LDAP_SUCCESS;
1072 #if SASL_VERSION_MAJOR >= 2
1073 char *ipremoteport = NULL, *iplocalport = NULL;
1076 #ifdef HAVE_CYRUS_SASL
1077 sasl_conn_t *ctx = NULL;
1078 sasl_callback_t *session_callbacks;
1080 assert( conn->c_sasl_authctx == NULL );
1083 assert( conn->c_sasl_extra == NULL );
1086 #if SASL_VERSION_MAJOR >= 2
1087 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1089 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1091 if( session_callbacks == NULL ) {
1093 LDAP_LOG( TRANSPORT, ERR,
1094 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1096 Debug( LDAP_DEBUG_ANY,
1097 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1101 conn->c_sasl_extra = session_callbacks;
1103 session_callbacks[cb=0].id = SASL_CB_LOG;
1104 session_callbacks[cb].proc = &slap_sasl_log;
1105 session_callbacks[cb++].context = conn;
1107 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1108 session_callbacks[cb].proc = &slap_sasl_authorize;
1109 session_callbacks[cb++].context = conn;
1111 #if SASL_VERSION_MAJOR >= 2
1112 session_callbacks[cb].id = SASL_CB_CANON_USER;
1113 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1114 session_callbacks[cb++].context = conn;
1116 /* XXXX: this should be conditional */
1117 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1118 session_callbacks[cb].proc = &slap_sasl_checkpass;
1119 session_callbacks[cb++].context = conn;
1122 session_callbacks[cb].id = SASL_CB_LIST_END;
1123 session_callbacks[cb].proc = NULL;
1124 session_callbacks[cb++].context = NULL;
1126 session_callbacks = conn->c_sasl_extra;
1129 conn->c_sasl_layers = 0;
1131 if( global_host == NULL ) {
1132 global_host = ldap_pvt_get_fqdn( NULL );
1135 /* create new SASL context */
1136 #if SASL_VERSION_MAJOR >= 2
1137 if ( conn->c_sock_name.bv_len != 0 &&
1138 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1141 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1142 /* Convert IPv6 addresses to address;port syntax. */
1143 p = strrchr( iplocalport, ' ' );
1144 /* Convert IPv4 addresses to address;port syntax. */
1145 if ( p == NULL ) p = strchr( iplocalport, ':' );
1150 if ( conn->c_peer_name.bv_len != 0 &&
1151 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1154 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1155 /* Convert IPv6 addresses to address;port syntax. */
1156 p = strrchr( ipremoteport, ' ' );
1157 /* Convert IPv4 addresses to address;port syntax. */
1158 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1163 sc = sasl_server_new( "ldap", global_host, global_realm,
1164 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1165 if ( iplocalport != NULL ) {
1166 ch_free( iplocalport );
1168 if ( ipremoteport != NULL ) {
1169 ch_free( ipremoteport );
1172 sc = sasl_server_new( "ldap", global_host, global_realm,
1173 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1176 if( sc != SASL_OK ) {
1178 LDAP_LOG( TRANSPORT, ERR,
1179 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1181 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1188 conn->c_sasl_authctx = ctx;
1190 if( sc == SASL_OK ) {
1191 sc = sasl_setprop( ctx,
1192 SASL_SEC_PROPS, &sasl_secprops );
1194 if( sc != SASL_OK ) {
1196 LDAP_LOG( TRANSPORT, ERR,
1197 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1199 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1203 slap_sasl_close( conn );
1208 sc = slap_sasl_err2ldap( sc );
1213 int slap_sasl_external(
1216 const char *auth_id )
1218 #if SASL_VERSION_MAJOR >= 2
1220 sasl_conn_t *ctx = conn->c_sasl_authctx;
1222 if ( ctx == NULL ) {
1223 return LDAP_UNAVAILABLE;
1226 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1228 if ( sc != SASL_OK ) {
1232 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1234 if ( sc != SASL_OK ) {
1238 #elif defined(HAVE_CYRUS_SASL)
1240 sasl_conn_t *ctx = conn->c_sasl_authctx;
1241 sasl_external_properties_t extprops;
1243 if ( ctx == NULL ) {
1244 return LDAP_UNAVAILABLE;
1247 memset( &extprops, '\0', sizeof(extprops) );
1249 extprops.auth_id = (char *) auth_id;
1251 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1252 (void *) &extprops );
1254 if ( sc != SASL_OK ) {
1259 return LDAP_SUCCESS;
1262 int slap_sasl_reset( Connection *conn )
1264 return LDAP_SUCCESS;
1267 char ** slap_sasl_mechs( Connection *conn )
1269 char **mechs = NULL;
1271 #ifdef HAVE_CYRUS_SASL
1272 sasl_conn_t *ctx = conn->c_sasl_authctx;
1274 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1278 SASL_CONST char *mechstr;
1280 sc = sasl_listmech( ctx,
1281 NULL, NULL, ",", NULL,
1282 &mechstr, NULL, NULL );
1284 if( sc != SASL_OK ) {
1286 LDAP_LOG( TRANSPORT, ERR,
1287 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1289 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1296 mechs = ldap_str2charray( mechstr, "," );
1298 #if SASL_VERSION_MAJOR < 2
1307 int slap_sasl_close( Connection *conn )
1309 #ifdef HAVE_CYRUS_SASL
1310 sasl_conn_t *ctx = conn->c_sasl_authctx;
1313 sasl_dispose( &ctx );
1315 if ( conn->c_sasl_sockctx && conn->c_sasl_authctx != conn->c_sasl_sockctx ) {
1316 ctx = conn->c_sasl_sockctx;
1317 sasl_dispose( &ctx );
1320 conn->c_sasl_authctx = NULL;
1321 conn->c_sasl_sockctx = NULL;
1322 conn->c_sasl_done = 0;
1324 free( conn->c_sasl_extra );
1325 conn->c_sasl_extra = NULL;
1328 return LDAP_SUCCESS;
1331 int slap_sasl_bind( Operation *op, SlapReply *rs )
1333 #ifdef HAVE_CYRUS_SASL
1334 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1335 struct berval response;
1336 unsigned reslen = 0;
1340 LDAP_LOG( TRANSPORT, ENTRY,
1341 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1342 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1343 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1344 op->o_conn->c_sasl_bind_mech.bv_val,
1345 op->orb_cred.bv_len );
1347 Debug(LDAP_DEBUG_ARGS,
1348 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1349 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1350 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1351 op->o_conn->c_sasl_bind_mech.bv_val,
1352 op->orb_cred.bv_len );
1357 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1358 "SASL unavailable on this session" );
1362 #if SASL_VERSION_MAJOR >= 2
1363 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1364 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1365 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1366 sasl_server_step( ctx, cred, clen, resp, rlen )
1368 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1369 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1370 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1371 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1374 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1375 /* If we already authenticated once, must use a new context */
1376 if ( op->o_conn->c_sasl_done ) {
1378 const char *authid = NULL;
1379 #if SASL_VERSION_MAJOR >= 2
1380 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1381 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1382 if ( authid ) authid = ch_strdup( authid );
1384 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1385 sasl_dispose( &ctx );
1387 op->o_conn->c_sasl_authctx = NULL;
1389 slap_sasl_open( op->o_conn, 1 );
1390 ctx = op->o_conn->c_sasl_authctx;
1391 #if SASL_VERSION_MAJOR >= 2
1393 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1394 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1395 ch_free( (char *)authid );
1400 op->o_conn->c_sasl_bind_mech.bv_val,
1401 op->orb_cred.bv_val, op->orb_cred.bv_len,
1402 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1406 op->orb_cred.bv_val, op->orb_cred.bv_len,
1407 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1410 response.bv_len = reslen;
1412 if ( sc == SASL_OK ) {
1413 sasl_ssf_t *ssf = NULL;
1415 op->orb_edn = op->o_conn->c_sasl_dn;
1416 op->o_conn->c_sasl_dn.bv_val = NULL;
1417 op->o_conn->c_sasl_dn.bv_len = 0;
1418 op->o_conn->c_sasl_done = 1;
1420 rs->sr_err = LDAP_SUCCESS;
1422 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1423 op->orb_ssf = ssf ? *ssf : 0;
1427 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1428 op->o_conn->c_sasl_layers++;
1430 /* If there's an old layer, set sockctx to NULL to
1431 * tell connection_read() to wait for us to finish.
1432 * Otherwise there is a race condition: we have to
1433 * send the Bind response using the old security
1434 * context and then remove it before reading any
1437 if ( op->o_conn->c_sasl_sockctx ) {
1438 ctx = op->o_conn->c_sasl_sockctx;
1439 op->o_conn->c_sasl_sockctx = NULL;
1441 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1443 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1446 /* Must send response using old security layer */
1447 if (response.bv_len) rs->sr_sasldata = &response;
1448 send_ldap_sasl( op, rs );
1450 /* Now dispose of the old security layer.
1453 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1454 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1455 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1456 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1457 sasl_dispose( &ctx );
1459 } else if ( sc == SASL_CONTINUE ) {
1460 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1461 rs->sr_sasldata = &response;
1462 send_ldap_sasl( op, rs );
1465 #if SASL_VERSION_MAJOR >= 2
1466 rs->sr_text = sasl_errdetail( ctx );
1468 rs->sr_err = slap_sasl_err2ldap( sc ),
1469 send_ldap_result( op, rs );
1472 #if SASL_VERSION_MAJOR < 2
1473 if( response.bv_len ) {
1474 ch_free( response.bv_val );
1479 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0 );
1481 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1486 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1487 "SASL not supported" );
1493 char* slap_sasl_secprops( const char *in )
1495 #ifdef HAVE_CYRUS_SASL
1496 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1498 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1500 return "SASL not supported";
1504 #ifdef HAVE_CYRUS_SASL
1506 slap_sasl_setpass( Operation *op, SlapReply *rs )
1508 struct berval id = { 0, NULL }; /* needs to come from connection */
1509 struct berval new = { 0, NULL };
1510 struct berval old = { 0, NULL };
1512 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1514 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1515 (SASL_CONST void **)&id.bv_val );
1517 if( rs->sr_err != SASL_OK ) {
1518 rs->sr_text = "unable to retrieve SASL username";
1519 rs->sr_err = LDAP_OTHER;
1524 LDAP_LOG( BACKEND, ENTRY,
1525 "slap_sasl_setpass: \"%s\"\n",
1526 id.bv_val ? id.bv_val : "", 0, 0);
1528 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1529 id.bv_val ? id.bv_val : "", 0, 0 );
1532 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1533 NULL, &old, &new, &rs->sr_text );
1535 if( rs->sr_err != LDAP_SUCCESS ) {
1539 if( new.bv_len == 0 ) {
1540 slap_passwd_generate(&new);
1542 if( new.bv_len == 0 ) {
1543 rs->sr_text = "password generation failed.";
1544 rs->sr_err = LDAP_OTHER;
1548 rs->sr_rspdata = slap_passwd_return( &new );
1551 #if SASL_VERSION_MAJOR < 2
1552 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1553 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1555 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1556 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1557 if( rs->sr_err != SASL_OK ) {
1558 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1561 switch(rs->sr_err) {
1563 rs->sr_err = LDAP_SUCCESS;
1573 rs->sr_err = LDAP_OTHER;
1579 #endif /* HAVE_CYRUS_SASL */
1581 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1582 string returned in *dn is in its own allocated memory, and must be free'd
1583 by the calling process.
1584 -Mark Adamson, Carnegie Mellon
1586 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1587 on strings passed in directly from SASL.
1588 -Howard Chu, Symas Corp.
1595 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
1597 int slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len,
1598 char *user_realm, struct berval *dn, int flags )
1601 int rc, is_dn = SET_NONE, do_norm = 1;
1605 LDAP_LOG( TRANSPORT, ENTRY,
1606 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1607 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", len );
1609 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s [len=%d]\n",
1610 id ? ( *id ? id : "<empty>" ) : "NULL", len, 0 );
1614 op = conn->c_sasl_bindop;
1621 if ( len == 0 ) len = strlen( id );
1623 /* Blatantly anonymous ID */
1624 if ( len == sizeof("anonymous") - 1 &&
1625 !strcasecmp( id, "anonymous" ) ) {
1626 return( LDAP_SUCCESS );
1632 /* An authcID needs to be converted to authzID form. Set the
1633 * values directly into *dn; they will be normalized later. (and
1634 * normalizing always makes a new copy.) An ID from a TLS certificate
1635 * is already normalized, so copy it and skip normalization.
1637 if( flags & SLAP_GETDN_AUTHCID ) {
1638 if( conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len &&
1639 strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
1641 /* EXTERNAL DNs are already normalized */
1644 ber_str2bv_x( id, len, 1, dn, op->o_tmpmemctx );
1647 /* convert to u:<username> form */
1653 if( is_dn == SET_NONE ) {
1654 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1658 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1665 /* No other possibilities from here */
1666 if( is_dn == SET_NONE ) {
1669 return( LDAP_INAPPROPRIATE_AUTH );
1672 /* Username strings */
1673 if( is_dn == SET_U ) {
1675 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
1677 /* username may have embedded realm name */
1678 if( ( realm = strchr( dn->bv_val, '@') ) ) {
1680 len += sizeof(",cn=")-2;
1681 } else if( user_realm && *user_realm ) {
1682 len += strlen( user_realm ) + sizeof(",cn=")-1;
1685 if( conn->c_sasl_bind_mech.bv_len ) {
1686 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
1689 /* Build the new dn */
1691 dn->bv_val = sl_malloc( len+1, op->o_tmpmemctx );
1692 if( dn->bv_val == NULL ) {
1694 LDAP_LOG( TRANSPORT, ERR,
1695 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1697 Debug( LDAP_DEBUG_ANY,
1698 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1702 p = lutil_strcopy( dn->bv_val, "uid=" );
1703 p = lutil_strncopy( p, c1, dn->bv_len );
1706 int rlen = dn->bv_len - ( realm - c1 );
1707 p = lutil_strcopy( p, ",cn=" );
1708 p = lutil_strncopy( p, realm, rlen );
1710 } else if( user_realm && *user_realm ) {
1711 p = lutil_strcopy( p, ",cn=" );
1712 p = lutil_strcopy( p, user_realm );
1715 if( conn->c_sasl_bind_mech.bv_len ) {
1716 p = lutil_strcopy( p, ",cn=" );
1717 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
1719 p = lutil_strcopy( p, ",cn=auth" );
1720 dn->bv_len = p - dn->bv_val;
1723 LDAP_LOG( TRANSPORT, ENTRY,
1724 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1726 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1730 /* Dup the DN in any case, so we don't risk
1731 * leaks or dangling pointers later,
1732 * and the DN value is '\0' terminated */
1733 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1734 dn->bv_val = dn2.bv_val;
1737 /* All strings are in DN form now. Normalize if needed. */
1739 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1741 /* User DNs were constructed above and must be freed now */
1742 sl_free( dn->bv_val, op->o_tmpmemctx );
1744 if ( rc != LDAP_SUCCESS ) {
1752 /* Run thru regexp */
1753 slap_sasl2dn( op, dn, &dn2 );
1755 sl_free( dn->bv_val, op->o_tmpmemctx );
1758 LDAP_LOG( TRANSPORT, ENTRY,
1759 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1761 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1766 return( LDAP_SUCCESS );