2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
29 #ifdef HAVE_CYRUS_SASL
30 # ifdef HAVE_SASL_SASL_H
31 # include <sasl/sasl.h>
36 # if SASL_VERSION_MAJOR >= 2
37 # ifdef HAVE_SASL_SASL_H
38 # include <sasl/saslplug.h>
40 # include <saslplug.h>
42 # define SASL_CONST const
47 static sasl_security_properties_t sasl_secprops;
48 #endif /* HAVE_CYRUS_SASL */
54 int slap_sasl_config( int cargc, char **cargv, char *line,
55 const char *fname, int lineno )
57 /* set SASL proxy authorization policy */
58 if ( strcasecmp( cargv[0], "sasl-authz-policy" ) == 0 ) {
61 LDAP_LOG( CONFIG, CRIT,
62 "%s: line %d: missing policy in"
63 " \"sasl-authz-policy <policy>\" line\n",
66 Debug( LDAP_DEBUG_ANY,
67 "%s: line %d: missing policy in"
68 " \"sasl-authz-policy <policy>\" line\n",
74 if ( slap_sasl_setpolicy( cargv[1] ) ) {
76 LDAP_LOG( CONFIG, CRIT,
77 "%s: line %d: unable "
78 "to parse value \"%s\" "
79 "in \"sasl-authz-policy "
81 fname, lineno, cargv[1] );
83 Debug( LDAP_DEBUG_ANY,
84 "%s: line %d: unable "
85 "to parse value \"%s\" "
86 "in \"sasl-authz-policy "
88 fname, lineno, cargv[1] );
93 } else if ( !strcasecmp( cargv[0], "sasl-regexp" )
94 || !strcasecmp( cargv[0], "saslregexp" ) )
99 LDAP_LOG( CONFIG, CRIT,
100 "%s: line %d: need 2 args in "
101 "\"saslregexp <match> <replace>\"\n",
104 Debug( LDAP_DEBUG_ANY,
105 "%s: line %d: need 2 args in "
106 "\"saslregexp <match> <replace>\"\n",
112 rc = slap_sasl_regexp_config( cargv[1], cargv[2] );
117 #ifdef HAVE_CYRUS_SASL
119 } else if ( strcasecmp( cargv[0], "sasl-host" ) == 0 ) {
122 LDAP_LOG( CONFIG, CRIT,
123 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
126 Debug( LDAP_DEBUG_ANY,
127 "%s: line %d: missing host in \"sasl-host <host>\" line\n",
134 if ( global_host != NULL ) {
136 LDAP_LOG( CONFIG, CRIT,
137 "%s: line %d: already set sasl-host!\n",
140 Debug( LDAP_DEBUG_ANY,
141 "%s: line %d: already set sasl-host!\n",
148 global_host = ch_strdup( cargv[1] );
152 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
155 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
156 "missing realm in \"sasl-realm <realm>\" line.\n",
159 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
160 "missing realm in \"sasl-realm <realm>\" line.\n",
167 if ( global_realm != NULL ) {
169 LDAP_LOG( CONFIG, CRIT,
170 "%s: line %d: already set sasl-realm!\n",
173 Debug( LDAP_DEBUG_ANY,
174 "%s: line %d: already set sasl-realm!\n",
181 global_realm = ch_strdup( cargv[1] );
184 /* SASL security properties */
185 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
190 LDAP_LOG( CONFIG, CRIT, "%s: line %d: "
191 "missing flags in \"sasl-secprops <properties>\" line\n",
194 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
195 "missing flags in \"sasl-secprops <properties>\" line\n",
202 txt = slap_sasl_secprops( cargv[1] );
205 LDAP_LOG( CONFIG, CRIT,
206 "%s: line %d sasl-secprops: %s\n",
207 fname, lineno, txt );
209 Debug( LDAP_DEBUG_ANY,
210 "%s: line %d: sasl-secprops: %s\n",
211 fname, lineno, txt );
216 #endif /* HAVE_CYRUS_SASL */
222 #ifdef HAVE_CYRUS_SASL
230 Connection *conn = context;
234 if ( message == NULL ) {
235 return SASL_BADPARAM;
239 #if SASL_VERSION_MAJOR >= 2
241 level = LDAP_DEBUG_NONE;
245 level = LDAP_DEBUG_ANY;
249 level = LDAP_DEBUG_ANY;
253 level = LDAP_DEBUG_TRACE;
257 level = LDAP_DEBUG_TRACE;
261 level = LDAP_DEBUG_TRACE;
265 level = LDAP_DEBUG_TRACE;
269 level = LDAP_DEBUG_TRACE;
270 label = "Password Trace";
274 level = LDAP_DEBUG_ANY;
277 case SASL_LOG_WARNING:
278 level = LDAP_DEBUG_TRACE;
282 level = LDAP_DEBUG_TRACE;
287 return SASL_BADPARAM;
291 LDAP_LOG( TRANSPORT, ENTRY,
292 "SASL [conn=%ld] %s: %s\n", conn ? conn->c_connid : -1, label, message);
294 Debug( level, "SASL [conn=%ld] %s: %s\n",
295 conn ? conn->c_connid: -1,
304 #if SASL_VERSION_MAJOR >= 2
305 static const char *slap_propnames[] = {
306 "*slapConn", "*authcDN", "*authzDN", NULL };
308 static Filter generic_filter = { LDAP_FILTER_PRESENT };
309 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
315 typedef struct lookup_info {
318 const struct propval *list;
319 sasl_server_params_t *sparams;
322 static slap_response sasl_ap_lookup, sasl_cb_checkpass;
325 sasl_ap_lookup( Operation *op, SlapReply *rs )
328 AttributeDescription *ad;
332 slap_callback *tmp = op->o_callback;
333 lookup_info *sl = tmp->sc_private;
335 if (rs->sr_type != REP_SEARCH) return 0;
337 for( i = 0; i < sl->last; i++ ) {
338 const char *name = sl->list[i].name;
340 if ( name[0] == '*' ) {
341 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
343 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
346 if ( sl->list[i].values ) {
347 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
350 rc = slap_str2ad( name, &ad, &text );
351 if ( rc != LDAP_SUCCESS ) {
353 LDAP_LOG( TRANSPORT, DETAIL1,
354 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
356 Debug( LDAP_DEBUG_TRACE,
357 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
361 a = attr_find( rs->sr_entry->e_attrs, ad );
363 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
366 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
367 sl->sparams->utils->prop_erase( sl->sparams->propctx,
370 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
371 sl->sparams->utils->prop_set( sl->sparams->propctx,
372 sl->list[i].name, bv->bv_val, bv->bv_len );
381 sasl_server_params_t *sparams,
388 Connection *conn = NULL;
391 sl.list = sparams->utils->prop_get( sparams->propctx );
392 sl.sparams = sparams;
395 /* Find our DN and conn first */
396 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
397 if ( sl.list[i].name[0] == '*' ) {
398 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
399 if ( sl.list[i].values && sl.list[i].values[0] )
400 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
401 if ( !sl.last ) sl.last = i;
403 if ( (flags & SASL_AUXPROP_AUTHZID) &&
404 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
406 if ( sl.list[i].values && sl.list[i].values[0] )
407 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
408 if ( !sl.last ) sl.last = i;
411 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
412 if ( !sl.last ) sl.last = i;
413 if ( sl.list[i].values && sl.list[i].values[0] ) {
414 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
415 if ( !(flags & SASL_AUXPROP_AUTHZID) )
422 /* Now see what else needs to be fetched */
423 for( i = 0; i < sl.last; i++ ) {
424 const char *name = sl.list[i].name;
426 if ( name[0] == '*' ) {
427 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
429 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
432 if ( sl.list[i].values ) {
433 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
439 slap_callback cb = { sasl_ap_lookup, NULL };
443 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
445 if ( op.o_bd && op.o_bd->be_search ) {
446 SlapReply rs = {REP_RESULT};
447 op.o_tag = LDAP_REQ_SEARCH;
448 op.o_protocol = LDAP_VERSION3;
449 op.o_ndn = conn->c_ndn;
451 op.o_time = slap_get_time();
452 op.o_do_not_cache = 1;
453 op.o_is_auth_check = 1;
454 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
455 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
456 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
458 op.o_pb = conn->c_sasl_bindop->o_pb;
461 op.o_connid = conn->c_connid;
462 op.o_req_dn = op.o_req_ndn;
463 op.ors_scope = LDAP_SCOPE_BASE;
464 op.ors_deref = LDAP_DEREF_NEVER;
466 op.ors_filter = &generic_filter;
467 op.ors_filterstr = generic_filterstr;
469 op.o_bd->be_search( &op, &rs );
474 static sasl_auxprop_plug_t slap_auxprop_plugin = {
477 NULL, /* glob_context */
478 NULL, /* auxprop_free */
486 const sasl_utils_t *utils,
489 sasl_auxprop_plug_t **plug,
490 const char *plugname)
492 if ( !out_version | !plug ) return SASL_BADPARAM;
494 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
496 *out_version = SASL_AUXPROP_PLUG_VERSION;
497 *plug = &slap_auxprop_plugin;
501 typedef struct checkpass_info {
507 sasl_cb_checkpass( Operation *op, SlapReply *rs )
509 slap_callback *tmp = op->o_callback;
510 checkpass_info *ci = tmp->sc_private;
514 if (rs->sr_type != REP_SEARCH) return 0;
516 ci->rc = SASL_NOVERIFY;
518 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_userPassword );
520 if ( ! access_allowed( op, rs->sr_entry, slap_schema.si_ad_userPassword,
521 NULL, ACL_AUTH, NULL ) ) return 0;
523 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
524 if ( !lutil_passwd( bv, &ci->cred, NULL, &rs->sr_text ) ) {
536 const char *username,
539 struct propctx *propctx)
541 Connection *conn = (Connection *)context;
548 /* SASL will fallback to its own mechanisms if we don't
549 * find an answer here.
552 rc = slap_sasl_getdn( conn, NULL, (char *)username, 0, NULL, &op.o_req_ndn,
553 SLAP_GETDN_AUTHCID );
554 if ( rc != LDAP_SUCCESS ) {
555 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
559 if ( op.o_req_ndn.bv_len == 0 ) {
560 sasl_seterror( sconn, 0,
561 "No password is associated with the Root DSE" );
562 if ( op.o_req_ndn.bv_val != NULL ) {
563 ch_free( op.o_req_ndn.bv_val );
568 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
569 if ( op.o_bd && op.o_bd->be_search ) {
570 slap_callback cb = { sasl_cb_checkpass, NULL };
571 SlapReply rs = {REP_RESULT};
573 ci.cred.bv_val = (char *)pass;
574 ci.cred.bv_len = passlen;
577 op.o_tag = LDAP_REQ_SEARCH;
578 op.o_protocol = LDAP_VERSION3;
579 op.o_ndn = conn->c_ndn;
581 op.o_time = slap_get_time();
582 op.o_do_not_cache = 1;
583 op.o_is_auth_check = 1;
584 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
585 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
586 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
588 op.o_pb = conn->c_sasl_bindop->o_pb;
591 op.o_connid = conn->c_connid;
592 op.o_req_dn = op.o_req_ndn;
593 op.ors_scope = LDAP_SCOPE_BASE;
594 op.ors_deref = LDAP_DEREF_NEVER;
596 op.ors_filter = &generic_filter;
597 op.ors_filterstr = generic_filterstr;
599 op.o_bd->be_search( &op, &rs );
601 if ( ci.rc != SASL_OK ) {
602 sasl_seterror( sconn, 0,
603 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
606 ch_free( op.o_req_ndn.bv_val );
611 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
612 * auxiliary property, so that we can refer to it in sasl_authorize
613 * without interfering with anything else. Also, the SASL username
614 * buffer is constrained to 256 characters, and our DNs could be
615 * much longer (totally arbitrary length)...
618 slap_sasl_canonicalize(
624 const char *user_realm,
629 Connection *conn = (Connection *)context;
630 struct propctx *props = sasl_auxprop_getctx( sconn );
631 struct propval auxvals[3];
634 const char *names[2];
639 LDAP_LOG( TRANSPORT, ENTRY,
640 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
641 conn ? conn->c_connid : -1,
642 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
643 in ? in : "<empty>");
645 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
646 conn ? conn->c_connid : -1,
647 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
648 in ? in : "<empty>");
651 /* If name is too big, just truncate. We don't care, we're
652 * using DNs, not the usernames.
654 if ( inlen > out_max )
657 /* See if we need to add request, can only do it once */
658 prop_getnames( props, slap_propnames, auxvals );
659 if ( !auxvals[0].name )
660 prop_request( props, slap_propnames );
662 if ( flags & SASL_CU_AUTHID )
667 /* Need to store the Connection for auxprop_lookup */
668 if ( !auxvals[PROP_CONN].values ) {
669 names[0] = slap_propnames[PROP_CONN];
671 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
674 /* Already been here? */
675 if ( auxvals[which].values )
678 /* Normally we require an authzID to have a u: or dn: prefix.
679 * However, SASL frequently gives us an authzID that is just
680 * an exact copy of the authcID, without a prefix. We need to
681 * detect and allow this condition. If SASL calls canonicalize
682 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
683 * But if it's broken into two calls, we need to remember the
684 * authcID so that we can compare the authzID later. We store
685 * the authcID temporarily in conn->c_sasl_dn. We necessarily
686 * finish Canonicalizing before Authorizing, so there is no
687 * conflict with slap_sasl_authorize's use of this temp var.
689 * The SASL EXTERNAL mech is backwards from all the other mechs,
690 * it does authzID before the authcID. If we see that authzID
691 * has already been done, don't do anything special with authcID.
693 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
694 conn->c_sasl_dn.bv_val = (char *) in;
695 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
696 rc = strcmp( in, conn->c_sasl_dn.bv_val );
697 conn->c_sasl_dn.bv_val = NULL;
698 /* They were equal, no work needed */
699 if ( !rc ) goto done;
702 rc = slap_sasl_getdn( conn, NULL, (char *)in, inlen, (char *)user_realm, &dn,
703 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
704 if ( rc != LDAP_SUCCESS ) {
705 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
709 names[0] = slap_propnames[which];
712 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
715 LDAP_LOG( TRANSPORT, ENTRY,
716 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
717 conn ? conn->c_connid : -1, names[0]+1,
718 dn.bv_val ? dn.bv_val : "<EMPTY>" );
720 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
721 conn ? conn->c_connid : -1, names[0]+1,
722 dn.bv_val ? dn.bv_val : "<EMPTY>" );
726 AC_MEMCPY( out, in, inlen );
738 char *requested_user,
742 const char *def_realm,
744 struct propctx *props)
746 Connection *conn = (Connection *)context;
747 struct propval auxvals[3];
748 struct berval authcDN, authzDN;
752 LDAP_LOG( TRANSPORT, ENTRY,
753 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
754 conn ? conn->c_connid : -1, auth_identity, requested_user);
756 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
757 "authcid=\"%s\" authzid=\"%s\"\n",
758 conn ? conn->c_connid : -1, auth_identity, requested_user );
760 if ( conn->c_sasl_dn.bv_val ) {
761 ch_free( conn->c_sasl_dn.bv_val );
762 conn->c_sasl_dn.bv_val = NULL;
763 conn->c_sasl_dn.bv_len = 0;
767 prop_getnames( props, slap_propnames+1, auxvals );
769 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
771 /* Nothing to do if no authzID was given */
772 if ( !auxvals[1].name || !auxvals[1].values ) {
773 conn->c_sasl_dn = authcDN;
777 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
779 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
780 ch_free( authcDN.bv_val );
781 if ( rc != LDAP_SUCCESS ) {
783 LDAP_LOG( TRANSPORT, INFO,
784 "slap_sasl_authorize: conn %ld "
785 "proxy authorization disallowed (%d)\n",
786 (long)(conn ? conn->c_connid : -1), rc, 0 );
788 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
789 "proxy authorization disallowed (%d)\n",
790 (long) (conn ? conn->c_connid : -1), rc, 0 );
793 sasl_seterror( sconn, 0, "not authorized" );
794 ch_free( authzDN.bv_val );
798 conn->c_sasl_dn = authzDN;
800 if (conn->c_sasl_bindop) {
801 Statslog( LDAP_DEBUG_STATS,
802 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
803 conn->c_connid, conn->c_sasl_bindop->o_opid,
804 auth_identity, 0, 0);
808 LDAP_LOG( TRANSPORT, ENTRY,
809 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
810 (long)(conn ? conn->c_connid : -1), 0, 0 );
812 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
813 " proxy authorization allowed\n",
814 (long) (conn ? conn->c_connid : -1), 0, 0 );
827 struct berval authcDN, authzDN;
829 Connection *conn = context;
833 if ( conn->c_sasl_dn.bv_val ) {
834 ch_free( conn->c_sasl_dn.bv_val );
835 conn->c_sasl_dn.bv_val = NULL;
836 conn->c_sasl_dn.bv_len = 0;
840 LDAP_LOG( TRANSPORT, ENTRY,
841 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
842 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
843 authzid ? authzid : "<empty>" );
845 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
846 "authcid=\"%s\" authzid=\"%s\"\n",
847 (long) (conn ? conn->c_connid : -1),
848 authcid ? authcid : "<empty>",
849 authzid ? authzid : "<empty>" );
852 /* Figure out how much data we have for the dn */
853 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
854 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
856 LDAP_LOG( TRANSPORT, ERR,
857 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
859 Debug(LDAP_DEBUG_TRACE,
860 "authorize: getprop(REALM) failed!\n", 0,0,0);
862 *errstr = "Could not extract realm";
866 /* Convert the identities to DN's. If no authzid was given, client will
867 be bound as the DN matching their username */
868 rc = slap_sasl_getdn( conn, NULL, (char *)authcid, 0, realm,
869 &authcDN, SLAP_GETDN_AUTHCID );
870 if( rc != LDAP_SUCCESS ) {
871 *errstr = ldap_err2string( rc );
874 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
876 LDAP_LOG( TRANSPORT, ENTRY,
877 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
878 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
880 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
881 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
884 conn->c_sasl_dn = authcDN;
887 rc = slap_sasl_getdn( conn, NULL, (char *)authzid, 0, realm,
888 &authzDN, SLAP_GETDN_AUTHZID );
889 if( rc != LDAP_SUCCESS ) {
890 ch_free( authcDN.bv_val );
891 *errstr = ldap_err2string( rc );
895 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
896 ch_free( authcDN.bv_val );
899 LDAP_LOG( TRANSPORT, INFO,
900 "slap_sasl_authorize: conn %ld "
901 "proxy authorization disallowed (%d)\n",
902 (long)(conn ? conn->c_connid : -1), rc, 0 );
904 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
905 "proxy authorization disallowed (%d)\n",
906 (long) (conn ? conn->c_connid : -1), rc, 0 );
909 *errstr = "not authorized";
910 ch_free( authzDN.bv_val );
913 conn->c_sasl_dn = authzDN;
917 LDAP_LOG( TRANSPORT, RESULTS,
918 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
919 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
921 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
922 " authorization allowed\n",
923 (long) (conn ? conn->c_connid : -1), 0, 0 );
926 if (conn->c_sasl_bindop) {
927 Statslog( LDAP_DEBUG_STATS,
928 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
929 conn->c_connid, conn->c_sasl_bindop->o_opid,
936 #endif /* SASL_VERSION_MAJOR >= 2 */
939 slap_sasl_err2ldap( int saslerr )
948 rc = LDAP_SASL_BIND_IN_PROGRESS;
957 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
960 rc = LDAP_INVALID_CREDENTIALS;
963 rc = LDAP_INSUFFICIENT_ACCESS;
967 rc = LDAP_INAPPROPRIATE_AUTH;
978 int slap_sasl_init( void )
980 #ifdef HAVE_CYRUS_SASL
982 static sasl_callback_t server_callbacks[] = {
983 { SASL_CB_LOG, &slap_sasl_log, NULL },
984 { SASL_CB_LIST_END, NULL, NULL }
987 #ifdef HAVE_SASL_VERSION
988 /* stringify the version number, sasl.h doesn't do it for us */
989 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
990 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
991 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
994 sasl_version( NULL, &rc );
995 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
996 (rc & 0xffff) < SASL_VERSION_STEP) {
997 char version[sizeof("xxx.xxx.xxxxx")];
998 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
1001 LDAP_LOG( TRANSPORT, INFO,
1002 "slap_sasl_init: SASL library version mismatch:"
1003 " expected " SASL_VERSION_STRING ","
1004 " got %s\n", version, 0, 0 );
1006 Debug( LDAP_DEBUG_ANY,
1007 "slap_sasl_init: SASL library version mismatch:"
1008 " expected " SASL_VERSION_STRING ","
1009 " got %s\n", version, 0, 0 );
1015 /* SASL 2 does its own memory management internally */
1016 #if SASL_VERSION_MAJOR < 2
1025 ldap_pvt_sasl_mutex_new,
1026 ldap_pvt_sasl_mutex_lock,
1027 ldap_pvt_sasl_mutex_unlock,
1028 ldap_pvt_sasl_mutex_dispose );
1030 #if SASL_VERSION_MAJOR >= 2
1031 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1033 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1035 /* should provide callbacks for logging */
1036 /* server name should be configurable */
1037 rc = sasl_server_init( server_callbacks, "slapd" );
1039 if( rc != SASL_OK ) {
1041 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1043 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1046 #if SASL_VERSION_MAJOR < 2
1047 /* A no-op used to make sure we linked with Cyrus 1.5 */
1048 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1055 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1057 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1062 /* default security properties */
1063 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1064 sasl_secprops.max_ssf = INT_MAX;
1065 sasl_secprops.maxbufsize = 65536;
1066 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1072 int slap_sasl_destroy( void )
1074 #ifdef HAVE_CYRUS_SASL
1077 free( global_host );
1083 int slap_sasl_open( Connection *conn, int reopen )
1085 int cb, sc = LDAP_SUCCESS;
1086 #if SASL_VERSION_MAJOR >= 2
1087 char *ipremoteport = NULL, *iplocalport = NULL;
1090 #ifdef HAVE_CYRUS_SASL
1091 sasl_conn_t *ctx = NULL;
1092 sasl_callback_t *session_callbacks;
1094 assert( conn->c_sasl_authctx == NULL );
1097 assert( conn->c_sasl_extra == NULL );
1100 #if SASL_VERSION_MAJOR >= 2
1101 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1103 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1105 if( session_callbacks == NULL ) {
1107 LDAP_LOG( TRANSPORT, ERR,
1108 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1110 Debug( LDAP_DEBUG_ANY,
1111 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1115 conn->c_sasl_extra = session_callbacks;
1117 session_callbacks[cb=0].id = SASL_CB_LOG;
1118 session_callbacks[cb].proc = &slap_sasl_log;
1119 session_callbacks[cb++].context = conn;
1121 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1122 session_callbacks[cb].proc = &slap_sasl_authorize;
1123 session_callbacks[cb++].context = conn;
1125 #if SASL_VERSION_MAJOR >= 2
1126 session_callbacks[cb].id = SASL_CB_CANON_USER;
1127 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1128 session_callbacks[cb++].context = conn;
1130 /* XXXX: this should be conditional */
1131 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1132 session_callbacks[cb].proc = &slap_sasl_checkpass;
1133 session_callbacks[cb++].context = conn;
1136 session_callbacks[cb].id = SASL_CB_LIST_END;
1137 session_callbacks[cb].proc = NULL;
1138 session_callbacks[cb++].context = NULL;
1140 session_callbacks = conn->c_sasl_extra;
1143 conn->c_sasl_layers = 0;
1145 if( global_host == NULL ) {
1146 global_host = ldap_pvt_get_fqdn( NULL );
1149 /* create new SASL context */
1150 #if SASL_VERSION_MAJOR >= 2
1151 if ( conn->c_sock_name.bv_len != 0 &&
1152 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1155 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1156 /* Convert IPv6 addresses to address;port syntax. */
1157 p = strrchr( iplocalport, ' ' );
1158 /* Convert IPv4 addresses to address;port syntax. */
1159 if ( p == NULL ) p = strchr( iplocalport, ':' );
1164 if ( conn->c_peer_name.bv_len != 0 &&
1165 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1168 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1169 /* Convert IPv6 addresses to address;port syntax. */
1170 p = strrchr( ipremoteport, ' ' );
1171 /* Convert IPv4 addresses to address;port syntax. */
1172 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1177 sc = sasl_server_new( "ldap", global_host, global_realm,
1178 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1179 if ( iplocalport != NULL ) {
1180 ch_free( iplocalport );
1182 if ( ipremoteport != NULL ) {
1183 ch_free( ipremoteport );
1186 sc = sasl_server_new( "ldap", global_host, global_realm,
1187 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1190 if( sc != SASL_OK ) {
1192 LDAP_LOG( TRANSPORT, ERR,
1193 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1195 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1202 conn->c_sasl_authctx = ctx;
1204 if( sc == SASL_OK ) {
1205 sc = sasl_setprop( ctx,
1206 SASL_SEC_PROPS, &sasl_secprops );
1208 if( sc != SASL_OK ) {
1210 LDAP_LOG( TRANSPORT, ERR,
1211 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1213 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1217 slap_sasl_close( conn );
1222 sc = slap_sasl_err2ldap( sc );
1227 int slap_sasl_external(
1230 const char *auth_id )
1232 #if SASL_VERSION_MAJOR >= 2
1234 sasl_conn_t *ctx = conn->c_sasl_authctx;
1236 if ( ctx == NULL ) {
1237 return LDAP_UNAVAILABLE;
1240 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1242 if ( sc != SASL_OK ) {
1246 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1248 if ( sc != SASL_OK ) {
1252 #elif defined(HAVE_CYRUS_SASL)
1254 sasl_conn_t *ctx = conn->c_sasl_authctx;
1255 sasl_external_properties_t extprops;
1257 if ( ctx == NULL ) {
1258 return LDAP_UNAVAILABLE;
1261 memset( &extprops, '\0', sizeof(extprops) );
1263 extprops.auth_id = (char *) auth_id;
1265 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1266 (void *) &extprops );
1268 if ( sc != SASL_OK ) {
1273 return LDAP_SUCCESS;
1276 int slap_sasl_reset( Connection *conn )
1278 return LDAP_SUCCESS;
1281 char ** slap_sasl_mechs( Connection *conn )
1283 char **mechs = NULL;
1285 #ifdef HAVE_CYRUS_SASL
1286 sasl_conn_t *ctx = conn->c_sasl_authctx;
1288 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1292 SASL_CONST char *mechstr;
1294 sc = sasl_listmech( ctx,
1295 NULL, NULL, ",", NULL,
1296 &mechstr, NULL, NULL );
1298 if( sc != SASL_OK ) {
1300 LDAP_LOG( TRANSPORT, ERR,
1301 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1303 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1310 mechs = ldap_str2charray( mechstr, "," );
1312 #if SASL_VERSION_MAJOR < 2
1321 int slap_sasl_close( Connection *conn )
1323 #ifdef HAVE_CYRUS_SASL
1324 sasl_conn_t *ctx = conn->c_sasl_authctx;
1327 sasl_dispose( &ctx );
1329 if ( conn->c_sasl_sockctx && conn->c_sasl_authctx != conn->c_sasl_sockctx ) {
1330 ctx = conn->c_sasl_sockctx;
1331 sasl_dispose( &ctx );
1334 conn->c_sasl_authctx = NULL;
1335 conn->c_sasl_sockctx = NULL;
1336 conn->c_sasl_done = 0;
1338 free( conn->c_sasl_extra );
1339 conn->c_sasl_extra = NULL;
1342 return LDAP_SUCCESS;
1345 int slap_sasl_bind( Operation *op, SlapReply *rs )
1347 #ifdef HAVE_CYRUS_SASL
1348 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1349 struct berval response;
1350 unsigned reslen = 0;
1354 LDAP_LOG( TRANSPORT, ENTRY,
1355 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1356 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1357 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1358 op->o_conn->c_sasl_bind_mech.bv_val,
1359 op->orb_cred.bv_len );
1361 Debug(LDAP_DEBUG_ARGS,
1362 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1363 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1364 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1365 op->o_conn->c_sasl_bind_mech.bv_val,
1366 op->orb_cred.bv_len );
1371 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1372 "SASL unavailable on this session" );
1376 #if SASL_VERSION_MAJOR >= 2
1377 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1378 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1379 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1380 sasl_server_step( ctx, cred, clen, resp, rlen )
1382 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1383 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1384 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1385 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1388 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1389 /* If we already authenticated once, must use a new context */
1390 if ( op->o_conn->c_sasl_done ) {
1392 const char *authid = NULL;
1393 #if SASL_VERSION_MAJOR >= 2
1394 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1395 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1396 if ( authid ) authid = ch_strdup( authid );
1398 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1399 sasl_dispose( &ctx );
1401 op->o_conn->c_sasl_authctx = NULL;
1403 slap_sasl_open( op->o_conn, 1 );
1404 ctx = op->o_conn->c_sasl_authctx;
1405 #if SASL_VERSION_MAJOR >= 2
1407 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1408 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1409 ch_free( (char *)authid );
1414 op->o_conn->c_sasl_bind_mech.bv_val,
1415 op->orb_cred.bv_val, op->orb_cred.bv_len,
1416 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1420 op->orb_cred.bv_val, op->orb_cred.bv_len,
1421 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1424 response.bv_len = reslen;
1426 if ( sc == SASL_OK ) {
1427 sasl_ssf_t *ssf = NULL;
1429 op->orb_edn = op->o_conn->c_sasl_dn;
1430 op->o_conn->c_sasl_dn.bv_val = NULL;
1431 op->o_conn->c_sasl_dn.bv_len = 0;
1432 op->o_conn->c_sasl_done = 1;
1434 rs->sr_err = LDAP_SUCCESS;
1436 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1437 op->orb_ssf = ssf ? *ssf : 0;
1441 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1442 op->o_conn->c_sasl_layers++;
1444 /* If there's an old layer, set sockctx to NULL to
1445 * tell connection_read() to wait for us to finish.
1446 * Otherwise there is a race condition: we have to
1447 * send the Bind response using the old security
1448 * context and then remove it before reading any
1451 if ( op->o_conn->c_sasl_sockctx ) {
1452 ctx = op->o_conn->c_sasl_sockctx;
1453 op->o_conn->c_sasl_sockctx = NULL;
1455 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1457 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1460 /* Must send response using old security layer */
1461 if (response.bv_len) rs->sr_sasldata = &response;
1462 send_ldap_sasl( op, rs );
1464 /* Now dispose of the old security layer.
1467 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1468 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1469 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1470 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1471 sasl_dispose( &ctx );
1473 } else if ( sc == SASL_CONTINUE ) {
1474 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1475 rs->sr_sasldata = &response;
1476 send_ldap_sasl( op, rs );
1479 #if SASL_VERSION_MAJOR >= 2
1480 rs->sr_text = sasl_errdetail( ctx );
1482 rs->sr_err = slap_sasl_err2ldap( sc ),
1483 send_ldap_result( op, rs );
1486 #if SASL_VERSION_MAJOR < 2
1487 if( response.bv_len ) {
1488 ch_free( response.bv_val );
1493 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0 );
1495 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1500 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1501 "SASL not supported" );
1507 char* slap_sasl_secprops( const char *in )
1509 #ifdef HAVE_CYRUS_SASL
1510 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1512 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1514 return "SASL not supported";
1518 #ifdef HAVE_CYRUS_SASL
1520 slap_sasl_setpass( Operation *op, SlapReply *rs )
1522 struct berval id = { 0, NULL }; /* needs to come from connection */
1523 struct berval new = { 0, NULL };
1524 struct berval old = { 0, NULL };
1526 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1528 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1529 (SASL_CONST void **)&id.bv_val );
1531 if( rs->sr_err != SASL_OK ) {
1532 rs->sr_text = "unable to retrieve SASL username";
1533 rs->sr_err = LDAP_OTHER;
1538 LDAP_LOG( BACKEND, ENTRY,
1539 "slap_sasl_setpass: \"%s\"\n",
1540 id.bv_val ? id.bv_val : "", 0, 0);
1542 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1543 id.bv_val ? id.bv_val : "", 0, 0 );
1546 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1547 NULL, &old, &new, &rs->sr_text );
1549 if( rs->sr_err != LDAP_SUCCESS ) {
1553 if( new.bv_len == 0 ) {
1554 slap_passwd_generate(&new);
1556 if( new.bv_len == 0 ) {
1557 rs->sr_text = "password generation failed.";
1558 rs->sr_err = LDAP_OTHER;
1562 rs->sr_rspdata = slap_passwd_return( &new );
1565 #if SASL_VERSION_MAJOR < 2
1566 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1567 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1569 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1570 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1571 if( rs->sr_err != SASL_OK ) {
1572 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1575 switch(rs->sr_err) {
1577 rs->sr_err = LDAP_SUCCESS;
1587 rs->sr_err = LDAP_OTHER;
1593 #endif /* HAVE_CYRUS_SASL */
1595 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1596 string returned in *dn is in its own allocated memory, and must be free'd
1597 by the calling process.
1598 -Mark Adamson, Carnegie Mellon
1600 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1601 on strings passed in directly from SASL.
1602 -Howard Chu, Symas Corp.
1609 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
1611 int slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len,
1612 char *user_realm, struct berval *dn, int flags )
1615 int rc, is_dn = SET_NONE, do_norm = 1;
1619 LDAP_LOG( TRANSPORT, ENTRY,
1620 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1621 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", len );
1623 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s [len=%d]\n",
1624 id ? ( *id ? id : "<empty>" ) : "NULL", len, 0 );
1628 op = conn->c_sasl_bindop;
1635 if ( len == 0 ) len = strlen( id );
1637 /* Blatantly anonymous ID */
1638 if ( len == sizeof("anonymous") - 1 &&
1639 !strcasecmp( id, "anonymous" ) ) {
1640 return( LDAP_SUCCESS );
1646 /* An authcID needs to be converted to authzID form. Set the
1647 * values directly into *dn; they will be normalized later. (and
1648 * normalizing always makes a new copy.) An ID from a TLS certificate
1649 * is already normalized, so copy it and skip normalization.
1651 if( flags & SLAP_GETDN_AUTHCID ) {
1652 if( conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len &&
1653 strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
1655 /* EXTERNAL DNs are already normalized */
1658 ber_str2bv_x( id, len, 1, dn, op->o_tmpmemctx );
1661 /* convert to u:<username> form */
1667 if( is_dn == SET_NONE ) {
1668 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1672 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1679 /* No other possibilities from here */
1680 if( is_dn == SET_NONE ) {
1683 return( LDAP_INAPPROPRIATE_AUTH );
1686 /* Username strings */
1687 if( is_dn == SET_U ) {
1689 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
1691 /* username may have embedded realm name */
1692 if( ( realm = strchr( dn->bv_val, '@') ) ) {
1694 len += sizeof(",cn=")-2;
1695 } else if( user_realm && *user_realm ) {
1696 len += strlen( user_realm ) + sizeof(",cn=")-1;
1699 if( conn->c_sasl_bind_mech.bv_len ) {
1700 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
1703 /* Build the new dn */
1705 dn->bv_val = sl_malloc( len+1, op->o_tmpmemctx );
1706 if( dn->bv_val == NULL ) {
1708 LDAP_LOG( TRANSPORT, ERR,
1709 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1711 Debug( LDAP_DEBUG_ANY,
1712 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1716 p = lutil_strcopy( dn->bv_val, "uid=" );
1717 p = lutil_strncopy( p, c1, dn->bv_len );
1720 int rlen = dn->bv_len - ( realm - c1 );
1721 p = lutil_strcopy( p, ",cn=" );
1722 p = lutil_strncopy( p, realm, rlen );
1724 } else if( user_realm && *user_realm ) {
1725 p = lutil_strcopy( p, ",cn=" );
1726 p = lutil_strcopy( p, user_realm );
1729 if( conn->c_sasl_bind_mech.bv_len ) {
1730 p = lutil_strcopy( p, ",cn=" );
1731 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
1733 p = lutil_strcopy( p, ",cn=auth" );
1734 dn->bv_len = p - dn->bv_val;
1737 LDAP_LOG( TRANSPORT, ENTRY,
1738 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1740 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1744 /* Dup the DN in any case, so we don't risk
1745 * leaks or dangling pointers later,
1746 * and the DN value is '\0' terminated */
1747 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1748 dn->bv_val = dn2.bv_val;
1751 /* All strings are in DN form now. Normalize if needed. */
1753 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1755 /* User DNs were constructed above and must be freed now */
1756 sl_free( dn->bv_val, op->o_tmpmemctx );
1758 if ( rc != LDAP_SUCCESS ) {
1766 /* Run thru regexp */
1767 slap_sasl2dn( op, dn, &dn2 );
1769 sl_free( dn->bv_val, op->o_tmpmemctx );
1772 LDAP_LOG( TRANSPORT, ENTRY,
1773 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1775 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1780 return( LDAP_SUCCESS );