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 };
300 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
306 typedef struct lookup_info {
309 const struct propval *list;
310 sasl_server_params_t *sparams;
313 static slap_response sasl_ap_lookup, sasl_cb_checkpass;
316 sasl_ap_lookup( Operation *op, SlapReply *rs )
319 AttributeDescription *ad;
323 slap_callback *tmp = op->o_callback;
324 lookup_info *sl = tmp->sc_private;
326 if (rs->sr_type != REP_SEARCH) return 0;
328 for( i = 0; i < sl->last; i++ ) {
329 const char *name = sl->list[i].name;
331 if ( name[0] == '*' ) {
332 if ( sl->flags & SASL_AUXPROP_AUTHZID ) continue;
334 } else if ( !(sl->flags & SASL_AUXPROP_AUTHZID ) )
337 if ( sl->list[i].values ) {
338 if ( !(sl->flags & SASL_AUXPROP_OVERRIDE) ) continue;
341 rc = slap_str2ad( name, &ad, &text );
342 if ( rc != LDAP_SUCCESS ) {
344 LDAP_LOG( TRANSPORT, DETAIL1,
345 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
347 Debug( LDAP_DEBUG_TRACE,
348 "slap_auxprop: str2ad(%s): %s\n", name, text, 0 );
352 a = attr_find( rs->sr_entry->e_attrs, ad );
354 if ( ! access_allowed( op, rs->sr_entry, ad, NULL, ACL_AUTH, NULL ) ) {
357 if ( sl->list[i].values && ( sl->flags & SASL_AUXPROP_OVERRIDE ) ) {
358 sl->sparams->utils->prop_erase( sl->sparams->propctx,
361 for ( bv = a->a_vals; bv->bv_val; bv++ ) {
362 sl->sparams->utils->prop_set( sl->sparams->propctx,
363 sl->list[i].name, bv->bv_val, bv->bv_len );
372 sasl_server_params_t *sparams,
379 Connection *conn = NULL;
382 sl.list = sparams->utils->prop_get( sparams->propctx );
383 sl.sparams = sparams;
386 /* Find our DN and conn first */
387 for( i = 0, sl.last = 0; sl.list[i].name; i++ ) {
388 if ( sl.list[i].name[0] == '*' ) {
389 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_CONN] ) ) {
390 if ( sl.list[i].values && sl.list[i].values[0] )
391 AC_MEMCPY( &conn, sl.list[i].values[0], sizeof( conn ) );
392 if ( !sl.last ) sl.last = i;
394 if ( (flags & SASL_AUXPROP_AUTHZID) &&
395 !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHZ] ) ) {
397 if ( sl.list[i].values && sl.list[i].values[0] )
398 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
399 if ( !sl.last ) sl.last = i;
402 if ( !strcmp( sl.list[i].name, slap_propnames[PROP_AUTHC] ) ) {
403 if ( !sl.last ) sl.last = i;
404 if ( sl.list[i].values && sl.list[i].values[0] ) {
405 AC_MEMCPY( &op.o_req_ndn, sl.list[i].values[0], sizeof( struct berval ) );
406 if ( !(flags & SASL_AUXPROP_AUTHZID) )
413 /* Now see what else needs to be fetched */
414 for( i = 0; i < sl.last; i++ ) {
415 const char *name = sl.list[i].name;
417 if ( name[0] == '*' ) {
418 if ( flags & SASL_AUXPROP_AUTHZID ) continue;
420 } else if ( !(flags & SASL_AUXPROP_AUTHZID ) )
423 if ( sl.list[i].values ) {
424 if ( !(flags & SASL_AUXPROP_OVERRIDE) ) continue;
430 slap_callback cb = { sasl_ap_lookup, NULL };
434 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
436 if ( op.o_bd && op.o_bd->be_search ) {
437 SlapReply rs = {REP_RESULT};
438 op.o_tag = LDAP_REQ_SEARCH;
439 op.o_protocol = LDAP_VERSION3;
440 op.o_ndn = conn->c_ndn;
442 op.o_time = slap_get_time();
443 op.o_do_not_cache = 1;
444 op.o_is_auth_check = 1;
445 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
446 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
447 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
449 op.o_pb = conn->c_sasl_bindop->o_pb;
452 op.o_connid = conn->c_connid;
453 op.o_req_dn = op.o_req_ndn;
454 op.ors_scope = LDAP_SCOPE_BASE;
455 op.ors_deref = LDAP_DEREF_NEVER;
457 op.ors_filter = &generic_filter;
458 op.ors_filterstr = generic_filterstr;
460 op.o_bd->be_search( &op, &rs );
465 static sasl_auxprop_plug_t slap_auxprop_plugin = {
468 NULL, /* glob_context */
469 NULL, /* auxprop_free */
477 const sasl_utils_t *utils,
480 sasl_auxprop_plug_t **plug,
481 const char *plugname)
483 if ( !out_version | !plug ) return SASL_BADPARAM;
485 if ( max_version < SASL_AUXPROP_PLUG_VERSION ) return SASL_BADVERS;
487 *out_version = SASL_AUXPROP_PLUG_VERSION;
488 *plug = &slap_auxprop_plugin;
492 typedef struct checkpass_info {
498 sasl_cb_checkpass( Operation *op, SlapReply *rs )
500 slap_callback *tmp = op->o_callback;
501 checkpass_info *ci = tmp->sc_private;
505 if (rs->sr_type != REP_SEARCH) return 0;
507 ci->rc = SASL_NOVERIFY;
509 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_userPassword );
511 if ( ! access_allowed( op, rs->sr_entry, slap_schema.si_ad_userPassword,
512 NULL, ACL_AUTH, NULL ) ) return 0;
514 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
515 if ( !lutil_passwd( bv, &ci->cred, NULL, &rs->sr_text ) ) {
527 const char *username,
530 struct propctx *propctx)
532 Connection *conn = (Connection *)context;
539 /* SASL will fallback to its own mechanisms if we don't
540 * find an answer here.
543 rc = slap_sasl_getdn( conn, NULL, (char *)username, 0, NULL, &op.o_req_ndn,
544 SLAP_GETDN_AUTHCID );
545 if ( rc != LDAP_SUCCESS ) {
546 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
550 if ( op.o_req_ndn.bv_len == 0 ) {
551 sasl_seterror( sconn, 0,
552 "No password is associated with the Root DSE" );
553 if ( op.o_req_ndn.bv_val != NULL ) {
554 ch_free( op.o_req_ndn.bv_val );
559 op.o_bd = select_backend( &op.o_req_ndn, 0, 1 );
560 if ( op.o_bd && op.o_bd->be_search ) {
561 slap_callback cb = { sasl_cb_checkpass, NULL };
562 SlapReply rs = {REP_RESULT};
564 ci.cred.bv_val = (char *)pass;
565 ci.cred.bv_len = passlen;
568 op.o_tag = LDAP_REQ_SEARCH;
569 op.o_protocol = LDAP_VERSION3;
570 op.o_ndn = conn->c_ndn;
572 op.o_time = slap_get_time();
573 op.o_do_not_cache = 1;
574 op.o_is_auth_check = 1;
575 op.o_threadctx = conn->c_sasl_bindop->o_threadctx;
576 op.o_tmpmemctx = conn->c_sasl_bindop->o_tmpmemctx;
577 op.o_tmpmfuncs = conn->c_sasl_bindop->o_tmpmfuncs;
579 op.o_pb = conn->c_sasl_bindop->o_pb;
582 op.o_connid = conn->c_connid;
583 op.o_req_dn = op.o_req_ndn;
584 op.ors_scope = LDAP_SCOPE_BASE;
585 op.ors_deref = LDAP_DEREF_NEVER;
587 op.ors_filter = &generic_filter;
588 op.ors_filterstr = generic_filterstr;
590 op.o_bd->be_search( &op, &rs );
592 if ( ci.rc != SASL_OK ) {
593 sasl_seterror( sconn, 0,
594 ldap_err2string( LDAP_INVALID_CREDENTIALS ) );
597 ch_free( op.o_req_ndn.bv_val );
602 /* Convert a SASL authcid or authzid into a DN. Store the DN in an
603 * auxiliary property, so that we can refer to it in sasl_authorize
604 * without interfering with anything else. Also, the SASL username
605 * buffer is constrained to 256 characters, and our DNs could be
606 * much longer (totally arbitrary length)...
609 slap_sasl_canonicalize(
615 const char *user_realm,
620 Connection *conn = (Connection *)context;
621 struct propctx *props = sasl_auxprop_getctx( sconn );
622 struct propval auxvals[3];
625 const char *names[2];
630 LDAP_LOG( TRANSPORT, ENTRY,
631 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
632 conn ? conn->c_connid : -1,
633 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
634 in ? in : "<empty>");
636 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
637 conn ? conn->c_connid : -1,
638 (flags & SASL_CU_AUTHID) ? "authcid" : "authzid",
639 in ? in : "<empty>");
642 /* If name is too big, just truncate. We don't care, we're
643 * using DNs, not the usernames.
645 if ( inlen > out_max )
648 /* See if we need to add request, can only do it once */
649 prop_getnames( props, slap_propnames, auxvals );
650 if ( !auxvals[0].name )
651 prop_request( props, slap_propnames );
653 if ( flags & SASL_CU_AUTHID )
658 /* Need to store the Connection for auxprop_lookup */
659 if ( !auxvals[PROP_CONN].values ) {
660 names[0] = slap_propnames[PROP_CONN];
662 prop_set( props, names[0], (char *)&conn, sizeof( conn ) );
665 /* Already been here? */
666 if ( auxvals[which].values )
669 /* Normally we require an authzID to have a u: or dn: prefix.
670 * However, SASL frequently gives us an authzID that is just
671 * an exact copy of the authcID, without a prefix. We need to
672 * detect and allow this condition. If SASL calls canonicalize
673 * with SASL_CU_AUTHID|SASL_CU_AUTHZID this is a no-brainer.
674 * But if it's broken into two calls, we need to remember the
675 * authcID so that we can compare the authzID later. We store
676 * the authcID temporarily in conn->c_sasl_dn. We necessarily
677 * finish Canonicalizing before Authorizing, so there is no
678 * conflict with slap_sasl_authorize's use of this temp var.
680 * The SASL EXTERNAL mech is backwards from all the other mechs,
681 * it does authzID before the authcID. If we see that authzID
682 * has already been done, don't do anything special with authcID.
684 if ( flags == SASL_CU_AUTHID && !auxvals[PROP_AUTHZ].values ) {
685 conn->c_sasl_dn.bv_val = (char *) in;
686 } else if ( flags == SASL_CU_AUTHZID && conn->c_sasl_dn.bv_val ) {
687 rc = strcmp( in, conn->c_sasl_dn.bv_val );
688 conn->c_sasl_dn.bv_val = NULL;
689 /* They were equal, no work needed */
690 if ( !rc ) goto done;
693 rc = slap_sasl_getdn( conn, NULL, (char *)in, inlen, (char *)user_realm, &dn,
694 (flags & SASL_CU_AUTHID) ? SLAP_GETDN_AUTHCID : SLAP_GETDN_AUTHZID );
695 if ( rc != LDAP_SUCCESS ) {
696 sasl_seterror( sconn, 0, ldap_err2string( rc ) );
700 names[0] = slap_propnames[which];
703 prop_set( props, names[0], (char *)&dn, sizeof( dn ) );
706 LDAP_LOG( TRANSPORT, ENTRY,
707 "slap_sasl_canonicalize: conn %d %s=\"%s\"\n",
708 conn ? conn->c_connid : -1, names[0]+1,
709 dn.bv_val ? dn.bv_val : "<EMPTY>" );
711 Debug( LDAP_DEBUG_ARGS, "SASL Canonicalize [conn=%ld]: %s=\"%s\"\n",
712 conn ? conn->c_connid : -1, names[0]+1,
713 dn.bv_val ? dn.bv_val : "<EMPTY>" );
717 AC_MEMCPY( out, in, inlen );
729 char *requested_user,
733 const char *def_realm,
735 struct propctx *props)
737 Connection *conn = (Connection *)context;
738 struct propval auxvals[3];
739 struct berval authcDN, authzDN;
743 LDAP_LOG( TRANSPORT, ENTRY,
744 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
745 conn ? conn->c_connid : -1, auth_identity, requested_user);
747 Debug( LDAP_DEBUG_ARGS, "SASL proxy authorize [conn=%ld]: "
748 "authcid=\"%s\" authzid=\"%s\"\n",
749 conn ? conn->c_connid : -1, auth_identity, requested_user );
751 if ( conn->c_sasl_dn.bv_val ) {
752 ch_free( conn->c_sasl_dn.bv_val );
753 conn->c_sasl_dn.bv_val = NULL;
754 conn->c_sasl_dn.bv_len = 0;
758 prop_getnames( props, slap_propnames+1, auxvals );
760 AC_MEMCPY( &authcDN, auxvals[0].values[0], sizeof(authcDN) );
762 /* Nothing to do if no authzID was given */
763 if ( !auxvals[1].name || !auxvals[1].values ) {
764 conn->c_sasl_dn = authcDN;
768 AC_MEMCPY( &authzDN, auxvals[1].values[0], sizeof(authzDN) );
770 rc = slap_sasl_authorized( conn->c_sasl_bindop, &authcDN, &authzDN );
771 ch_free( authcDN.bv_val );
772 if ( rc != LDAP_SUCCESS ) {
774 LDAP_LOG( TRANSPORT, INFO,
775 "slap_sasl_authorize: conn %ld "
776 "proxy authorization disallowed (%d)\n",
777 (long)(conn ? conn->c_connid : -1), rc, 0 );
779 Debug( LDAP_DEBUG_TRACE, "SASL Proxy Authorize [conn=%ld]: "
780 "proxy authorization disallowed (%d)\n",
781 (long) (conn ? conn->c_connid : -1), rc, 0 );
784 sasl_seterror( sconn, 0, "not authorized" );
785 ch_free( authzDN.bv_val );
789 conn->c_sasl_dn = authzDN;
791 if (conn->c_sasl_bindop) {
792 Statslog( LDAP_DEBUG_STATS,
793 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
794 conn->c_connid, conn->c_sasl_bindop->o_opid,
795 auth_identity, 0, 0);
799 LDAP_LOG( TRANSPORT, ENTRY,
800 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
801 (long)(conn ? conn->c_connid : -1), 0, 0 );
803 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
804 " proxy authorization allowed\n",
805 (long) (conn ? conn->c_connid : -1), 0, 0 );
818 struct berval authcDN, authzDN;
820 Connection *conn = context;
824 if ( conn->c_sasl_dn.bv_val ) {
825 ch_free( conn->c_sasl_dn.bv_val );
826 conn->c_sasl_dn.bv_val = NULL;
827 conn->c_sasl_dn.bv_len = 0;
831 LDAP_LOG( TRANSPORT, ENTRY,
832 "slap_sasl_authorize: conn %d authcid=\"%s\" authzid=\"%s\"\n",
833 conn ? conn->c_connid : -1, authcid ? authcid : "<empty>",
834 authzid ? authzid : "<empty>" );
836 Debug( LDAP_DEBUG_ARGS, "SASL Authorize [conn=%ld]: "
837 "authcid=\"%s\" authzid=\"%s\"\n",
838 (long) (conn ? conn->c_connid : -1),
839 authcid ? authcid : "<empty>",
840 authzid ? authzid : "<empty>" );
843 /* Figure out how much data we have for the dn */
844 rc = sasl_getprop( conn->c_sasl_authctx, SASL_REALM, (void **)&realm );
845 if( rc != SASL_OK && rc != SASL_NOTDONE ) {
847 LDAP_LOG( TRANSPORT, ERR,
848 "slap_sasl_authorize: getprop(REALM) failed.\n", 0, 0, 0 );
850 Debug(LDAP_DEBUG_TRACE,
851 "authorize: getprop(REALM) failed!\n", 0,0,0);
853 *errstr = "Could not extract realm";
857 /* Convert the identities to DN's. If no authzid was given, client will
858 be bound as the DN matching their username */
859 rc = slap_sasl_getdn( conn, NULL, (char *)authcid, 0, realm,
860 &authcDN, SLAP_GETDN_AUTHCID );
861 if( rc != LDAP_SUCCESS ) {
862 *errstr = ldap_err2string( rc );
865 if( ( authzid == NULL ) || !strcmp( authcid,authzid ) ) {
867 LDAP_LOG( TRANSPORT, ENTRY,
868 "slap_sasl_authorize: conn %d Using authcDN=%s\n",
869 conn ? conn->c_connid : -1, authcDN.bv_val, 0 );
871 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
872 "Using authcDN=%s\n", (long) (conn ? conn->c_connid : -1), authcDN.bv_val,0 );
875 conn->c_sasl_dn = authcDN;
878 rc = slap_sasl_getdn( conn, NULL, (char *)authzid, 0, realm,
879 &authzDN, SLAP_GETDN_AUTHZID );
880 if( rc != LDAP_SUCCESS ) {
881 ch_free( authcDN.bv_val );
882 *errstr = ldap_err2string( rc );
886 rc = slap_sasl_authorized(conn->c_sasl_bindop, &authcDN, &authzDN );
887 ch_free( authcDN.bv_val );
890 LDAP_LOG( TRANSPORT, INFO,
891 "slap_sasl_authorize: conn %ld "
892 "proxy authorization disallowed (%d)\n",
893 (long)(conn ? conn->c_connid : -1), rc, 0 );
895 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
896 "proxy authorization disallowed (%d)\n",
897 (long) (conn ? conn->c_connid : -1), rc, 0 );
900 *errstr = "not authorized";
901 ch_free( authzDN.bv_val );
904 conn->c_sasl_dn = authzDN;
908 LDAP_LOG( TRANSPORT, RESULTS,
909 "slap_sasl_authorize: conn %d proxy authorization allowed\n",
910 (long)(conn ? conn->c_connid : -1 ), 0, 0 );
912 Debug( LDAP_DEBUG_TRACE, "SASL Authorize [conn=%ld]: "
913 " authorization allowed\n",
914 (long) (conn ? conn->c_connid : -1), 0, 0 );
917 if (conn->c_sasl_bindop) {
918 Statslog( LDAP_DEBUG_STATS,
919 "conn=%lu op=%lu BIND authcid=\"%s\"\n",
920 conn->c_connid, conn->c_sasl_bindop->o_opid,
927 #endif /* SASL_VERSION_MAJOR >= 2 */
930 slap_sasl_err2ldap( int saslerr )
939 rc = LDAP_SASL_BIND_IN_PROGRESS;
948 rc = LDAP_AUTH_METHOD_NOT_SUPPORTED;
951 rc = LDAP_INVALID_CREDENTIALS;
954 rc = LDAP_INSUFFICIENT_ACCESS;
958 rc = LDAP_INAPPROPRIATE_AUTH;
969 int slap_sasl_init( void )
971 #ifdef HAVE_CYRUS_SASL
973 static sasl_callback_t server_callbacks[] = {
974 { SASL_CB_LOG, &slap_sasl_log, NULL },
975 { SASL_CB_LIST_END, NULL, NULL }
978 #ifdef HAVE_SASL_VERSION
979 /* stringify the version number, sasl.h doesn't do it for us */
980 #define VSTR0(maj, min, pat) #maj "." #min "." #pat
981 #define VSTR(maj, min, pat) VSTR0(maj, min, pat)
982 #define SASL_VERSION_STRING VSTR(SASL_VERSION_MAJOR, SASL_VERSION_MINOR, \
985 sasl_version( NULL, &rc );
986 if ( ((rc >> 16) != ((SASL_VERSION_MAJOR << 8)|SASL_VERSION_MINOR)) ||
987 (rc & 0xffff) < SASL_VERSION_STEP) {
988 char version[sizeof("xxx.xxx.xxxxx")];
989 sprintf( version, "%u.%d.%d", (unsigned)rc >> 24, (rc >> 16) & 0xff,
992 LDAP_LOG( TRANSPORT, INFO,
993 "slap_sasl_init: SASL library version mismatch:"
994 " expected " SASL_VERSION_STRING ","
995 " got %s\n", version, 0, 0 );
997 Debug( LDAP_DEBUG_ANY,
998 "slap_sasl_init: SASL library version mismatch:"
999 " expected " SASL_VERSION_STRING ","
1000 " got %s\n", version, 0, 0 );
1006 /* SASL 2 does its own memory management internally */
1007 #if SASL_VERSION_MAJOR < 2
1016 ldap_pvt_sasl_mutex_new,
1017 ldap_pvt_sasl_mutex_lock,
1018 ldap_pvt_sasl_mutex_unlock,
1019 ldap_pvt_sasl_mutex_dispose );
1021 #if SASL_VERSION_MAJOR >= 2
1022 generic_filter.f_desc = slap_schema.si_ad_objectClass;
1024 sasl_auxprop_add_plugin( "slapd", slap_auxprop_init );
1026 /* should provide callbacks for logging */
1027 /* server name should be configurable */
1028 rc = sasl_server_init( server_callbacks, "slapd" );
1030 if( rc != SASL_OK ) {
1032 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: init failed.\n", 0, 0, 0 );
1034 Debug( LDAP_DEBUG_ANY, "sasl_server_init failed\n",
1037 #if SASL_VERSION_MAJOR < 2
1038 /* A no-op used to make sure we linked with Cyrus 1.5 */
1039 sasl_client_auth( NULL, NULL, NULL, 0, NULL, NULL );
1046 LDAP_LOG( TRANSPORT, INFO, "slap_sasl_init: initialized!\n", 0, 0, 0 );
1048 Debug( LDAP_DEBUG_TRACE, "slap_sasl_init: initialized!\n",
1053 /* default security properties */
1054 memset( &sasl_secprops, '\0', sizeof(sasl_secprops) );
1055 sasl_secprops.max_ssf = INT_MAX;
1056 sasl_secprops.maxbufsize = 65536;
1057 sasl_secprops.security_flags = SASL_SEC_NOPLAINTEXT|SASL_SEC_NOANONYMOUS;
1063 int slap_sasl_destroy( void )
1065 #ifdef HAVE_CYRUS_SASL
1068 free( global_host );
1074 int slap_sasl_open( Connection *conn, int reopen )
1076 int cb, sc = LDAP_SUCCESS;
1077 #if SASL_VERSION_MAJOR >= 2
1078 char *ipremoteport = NULL, *iplocalport = NULL;
1081 #ifdef HAVE_CYRUS_SASL
1082 sasl_conn_t *ctx = NULL;
1083 sasl_callback_t *session_callbacks;
1085 assert( conn->c_sasl_authctx == NULL );
1088 assert( conn->c_sasl_extra == NULL );
1091 #if SASL_VERSION_MAJOR >= 2
1092 SLAP_CALLOC( 5, sizeof(sasl_callback_t));
1094 SLAP_CALLOC( 3, sizeof(sasl_callback_t));
1096 if( session_callbacks == NULL ) {
1098 LDAP_LOG( TRANSPORT, ERR,
1099 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1101 Debug( LDAP_DEBUG_ANY,
1102 "slap_sasl_open: SLAP_MALLOC failed", 0, 0, 0 );
1106 conn->c_sasl_extra = session_callbacks;
1108 session_callbacks[cb=0].id = SASL_CB_LOG;
1109 session_callbacks[cb].proc = &slap_sasl_log;
1110 session_callbacks[cb++].context = conn;
1112 session_callbacks[cb].id = SASL_CB_PROXY_POLICY;
1113 session_callbacks[cb].proc = &slap_sasl_authorize;
1114 session_callbacks[cb++].context = conn;
1116 #if SASL_VERSION_MAJOR >= 2
1117 session_callbacks[cb].id = SASL_CB_CANON_USER;
1118 session_callbacks[cb].proc = &slap_sasl_canonicalize;
1119 session_callbacks[cb++].context = conn;
1121 /* XXXX: this should be conditional */
1122 session_callbacks[cb].id = SASL_CB_SERVER_USERDB_CHECKPASS;
1123 session_callbacks[cb].proc = &slap_sasl_checkpass;
1124 session_callbacks[cb++].context = conn;
1127 session_callbacks[cb].id = SASL_CB_LIST_END;
1128 session_callbacks[cb].proc = NULL;
1129 session_callbacks[cb++].context = NULL;
1131 session_callbacks = conn->c_sasl_extra;
1134 conn->c_sasl_layers = 0;
1136 if( global_host == NULL ) {
1137 global_host = ldap_pvt_get_fqdn( NULL );
1140 /* create new SASL context */
1141 #if SASL_VERSION_MAJOR >= 2
1142 if ( conn->c_sock_name.bv_len != 0 &&
1143 strncmp( conn->c_sock_name.bv_val, "IP=", 3 ) == 0) {
1146 iplocalport = ch_strdup( conn->c_sock_name.bv_val + 3 );
1147 /* Convert IPv6 addresses to address;port syntax. */
1148 p = strrchr( iplocalport, ' ' );
1149 /* Convert IPv4 addresses to address;port syntax. */
1150 if ( p == NULL ) p = strchr( iplocalport, ':' );
1155 if ( conn->c_peer_name.bv_len != 0 &&
1156 strncmp( conn->c_peer_name.bv_val, "IP=", 3 ) == 0) {
1159 ipremoteport = ch_strdup( conn->c_peer_name.bv_val + 3 );
1160 /* Convert IPv6 addresses to address;port syntax. */
1161 p = strrchr( ipremoteport, ' ' );
1162 /* Convert IPv4 addresses to address;port syntax. */
1163 if ( p == NULL ) p = strchr( ipremoteport, ':' );
1168 sc = sasl_server_new( "ldap", global_host, global_realm,
1169 iplocalport, ipremoteport, session_callbacks, SASL_SUCCESS_DATA, &ctx );
1170 if ( iplocalport != NULL ) {
1171 ch_free( iplocalport );
1173 if ( ipremoteport != NULL ) {
1174 ch_free( ipremoteport );
1177 sc = sasl_server_new( "ldap", global_host, global_realm,
1178 session_callbacks, SASL_SECURITY_LAYER, &ctx );
1181 if( sc != SASL_OK ) {
1183 LDAP_LOG( TRANSPORT, ERR,
1184 "slap_sasl_open: sasl_server_new failed: %d\n", sc, 0, 0 );
1186 Debug( LDAP_DEBUG_ANY, "sasl_server_new failed: %d\n",
1193 conn->c_sasl_authctx = ctx;
1195 if( sc == SASL_OK ) {
1196 sc = sasl_setprop( ctx,
1197 SASL_SEC_PROPS, &sasl_secprops );
1199 if( sc != SASL_OK ) {
1201 LDAP_LOG( TRANSPORT, ERR,
1202 "slap_sasl_open: sasl_setprop failed: %d \n", sc, 0, 0 );
1204 Debug( LDAP_DEBUG_ANY, "sasl_setprop failed: %d\n",
1208 slap_sasl_close( conn );
1213 sc = slap_sasl_err2ldap( sc );
1218 int slap_sasl_external(
1221 const char *auth_id )
1223 #if SASL_VERSION_MAJOR >= 2
1225 sasl_conn_t *ctx = conn->c_sasl_authctx;
1227 if ( ctx == NULL ) {
1228 return LDAP_UNAVAILABLE;
1231 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1233 if ( sc != SASL_OK ) {
1237 sc = sasl_setprop( ctx, SASL_AUTH_EXTERNAL, auth_id );
1239 if ( sc != SASL_OK ) {
1243 #elif defined(HAVE_CYRUS_SASL)
1245 sasl_conn_t *ctx = conn->c_sasl_authctx;
1246 sasl_external_properties_t extprops;
1248 if ( ctx == NULL ) {
1249 return LDAP_UNAVAILABLE;
1252 memset( &extprops, '\0', sizeof(extprops) );
1254 extprops.auth_id = (char *) auth_id;
1256 sc = sasl_setprop( ctx, SASL_SSF_EXTERNAL,
1257 (void *) &extprops );
1259 if ( sc != SASL_OK ) {
1264 return LDAP_SUCCESS;
1267 int slap_sasl_reset( Connection *conn )
1269 return LDAP_SUCCESS;
1272 char ** slap_sasl_mechs( Connection *conn )
1274 char **mechs = NULL;
1276 #ifdef HAVE_CYRUS_SASL
1277 sasl_conn_t *ctx = conn->c_sasl_authctx;
1279 if( ctx == NULL ) ctx = conn->c_sasl_sockctx;
1283 SASL_CONST char *mechstr;
1285 sc = sasl_listmech( ctx,
1286 NULL, NULL, ",", NULL,
1287 &mechstr, NULL, NULL );
1289 if( sc != SASL_OK ) {
1291 LDAP_LOG( TRANSPORT, ERR,
1292 "slap_sasl_mechs: sasl_listmech failed: %d\n", sc, 0, 0 );
1294 Debug( LDAP_DEBUG_ANY, "slap_sasl_listmech failed: %d\n",
1301 mechs = ldap_str2charray( mechstr, "," );
1303 #if SASL_VERSION_MAJOR < 2
1312 int slap_sasl_close( Connection *conn )
1314 #ifdef HAVE_CYRUS_SASL
1315 sasl_conn_t *ctx = conn->c_sasl_authctx;
1318 sasl_dispose( &ctx );
1320 if ( conn->c_sasl_sockctx && conn->c_sasl_authctx != conn->c_sasl_sockctx ) {
1321 ctx = conn->c_sasl_sockctx;
1322 sasl_dispose( &ctx );
1325 conn->c_sasl_authctx = NULL;
1326 conn->c_sasl_sockctx = NULL;
1327 conn->c_sasl_done = 0;
1329 free( conn->c_sasl_extra );
1330 conn->c_sasl_extra = NULL;
1333 return LDAP_SUCCESS;
1336 int slap_sasl_bind( Operation *op, SlapReply *rs )
1338 #ifdef HAVE_CYRUS_SASL
1339 sasl_conn_t *ctx = op->o_conn->c_sasl_authctx;
1340 struct berval response;
1341 unsigned reslen = 0;
1345 LDAP_LOG( TRANSPORT, ENTRY,
1346 "sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1347 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1348 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1349 op->o_conn->c_sasl_bind_mech.bv_val,
1350 op->orb_cred.bv_len );
1352 Debug(LDAP_DEBUG_ARGS,
1353 "==> sasl_bind: dn=\"%s\" mech=%s datalen=%ld\n",
1354 op->o_req_dn.bv_len ? op->o_req_dn.bv_val : "",
1355 op->o_conn->c_sasl_bind_in_progress ? "<continuing>" :
1356 op->o_conn->c_sasl_bind_mech.bv_val,
1357 op->orb_cred.bv_len );
1362 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1363 "SASL unavailable on this session" );
1367 #if SASL_VERSION_MAJOR >= 2
1368 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1369 sasl_server_start( ctx, mech, cred, clen, resp, rlen )
1370 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1371 sasl_server_step( ctx, cred, clen, resp, rlen )
1373 #define START( ctx, mech, cred, clen, resp, rlen, err ) \
1374 sasl_server_start( ctx, mech, cred, clen, resp, rlen, err )
1375 #define STEP( ctx, cred, clen, resp, rlen, err ) \
1376 sasl_server_step( ctx, cred, clen, resp, rlen, err )
1379 if ( !op->o_conn->c_sasl_bind_in_progress ) {
1380 /* If we already authenticated once, must use a new context */
1381 if ( op->o_conn->c_sasl_done ) {
1383 const char *authid = NULL;
1384 #if SASL_VERSION_MAJOR >= 2
1385 sasl_getprop( ctx, SASL_SSF_EXTERNAL, (void *)&ssf );
1386 sasl_getprop( ctx, SASL_AUTH_EXTERNAL, (void *)&authid );
1387 if ( authid ) authid = ch_strdup( authid );
1389 if ( ctx != op->o_conn->c_sasl_sockctx ) {
1390 sasl_dispose( &ctx );
1392 op->o_conn->c_sasl_authctx = NULL;
1394 slap_sasl_open( op->o_conn, 1 );
1395 ctx = op->o_conn->c_sasl_authctx;
1396 #if SASL_VERSION_MAJOR >= 2
1398 sasl_setprop( ctx, SASL_SSF_EXTERNAL, &ssf );
1399 sasl_setprop( ctx, SASL_AUTH_EXTERNAL, authid );
1400 ch_free( (char *)authid );
1405 op->o_conn->c_sasl_bind_mech.bv_val,
1406 op->orb_cred.bv_val, op->orb_cred.bv_len,
1407 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1411 op->orb_cred.bv_val, op->orb_cred.bv_len,
1412 (SASL_CONST char **)&response.bv_val, &reslen, &rs->sr_text );
1415 response.bv_len = reslen;
1417 if ( sc == SASL_OK ) {
1418 sasl_ssf_t *ssf = NULL;
1420 op->orb_edn = op->o_conn->c_sasl_dn;
1421 op->o_conn->c_sasl_dn.bv_val = NULL;
1422 op->o_conn->c_sasl_dn.bv_len = 0;
1423 op->o_conn->c_sasl_done = 1;
1425 rs->sr_err = LDAP_SUCCESS;
1427 (void) sasl_getprop( ctx, SASL_SSF, (void *)&ssf );
1428 op->orb_ssf = ssf ? *ssf : 0;
1432 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1433 op->o_conn->c_sasl_layers++;
1435 /* If there's an old layer, set sockctx to NULL to
1436 * tell connection_read() to wait for us to finish.
1437 * Otherwise there is a race condition: we have to
1438 * send the Bind response using the old security
1439 * context and then remove it before reading any
1442 if ( op->o_conn->c_sasl_sockctx ) {
1443 ctx = op->o_conn->c_sasl_sockctx;
1444 op->o_conn->c_sasl_sockctx = NULL;
1446 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1448 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1451 /* Must send response using old security layer */
1452 if (response.bv_len) rs->sr_sasldata = &response;
1453 send_ldap_sasl( op, rs );
1455 /* Now dispose of the old security layer.
1458 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1459 ldap_pvt_sasl_remove( op->o_conn->c_sb );
1460 op->o_conn->c_sasl_sockctx = op->o_conn->c_sasl_authctx;
1461 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1462 sasl_dispose( &ctx );
1464 } else if ( sc == SASL_CONTINUE ) {
1465 rs->sr_err = LDAP_SASL_BIND_IN_PROGRESS,
1466 rs->sr_sasldata = &response;
1467 send_ldap_sasl( op, rs );
1470 #if SASL_VERSION_MAJOR >= 2
1471 rs->sr_text = sasl_errdetail( ctx );
1473 rs->sr_err = slap_sasl_err2ldap( sc ),
1474 send_ldap_result( op, rs );
1477 #if SASL_VERSION_MAJOR < 2
1478 if( response.bv_len ) {
1479 ch_free( response.bv_val );
1484 LDAP_LOG( TRANSPORT, RESULTS, "slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0 );
1486 Debug(LDAP_DEBUG_TRACE, "<== slap_sasl_bind: rc=%d\n", rs->sr_err, 0, 0);
1491 send_ldap_error( op, rs, LDAP_UNAVAILABLE,
1492 "SASL not supported" );
1498 char* slap_sasl_secprops( const char *in )
1500 #ifdef HAVE_CYRUS_SASL
1501 int rc = ldap_pvt_sasl_secprops( in, &sasl_secprops );
1503 return rc == LDAP_SUCCESS ? NULL : "Invalid security properties";
1505 return "SASL not supported";
1509 #ifdef HAVE_CYRUS_SASL
1511 slap_sasl_setpass( Operation *op, SlapReply *rs )
1513 struct berval id = { 0, NULL }; /* needs to come from connection */
1514 struct berval new = { 0, NULL };
1515 struct berval old = { 0, NULL };
1517 assert( ber_bvcmp( &slap_EXOP_MODIFY_PASSWD, &op->ore_reqoid ) == 0 );
1519 rs->sr_err = sasl_getprop( op->o_conn->c_sasl_authctx, SASL_USERNAME,
1520 (SASL_CONST void **)&id.bv_val );
1522 if( rs->sr_err != SASL_OK ) {
1523 rs->sr_text = "unable to retrieve SASL username";
1524 rs->sr_err = LDAP_OTHER;
1529 LDAP_LOG( BACKEND, ENTRY,
1530 "slap_sasl_setpass: \"%s\"\n",
1531 id.bv_val ? id.bv_val : "", 0, 0);
1533 Debug( LDAP_DEBUG_ARGS, "==> slap_sasl_setpass: \"%s\"\n",
1534 id.bv_val ? id.bv_val : "", 0, 0 );
1537 rs->sr_err = slap_passwd_parse( op->ore_reqdata,
1538 NULL, &old, &new, &rs->sr_text );
1540 if( rs->sr_err != LDAP_SUCCESS ) {
1544 if( new.bv_len == 0 ) {
1545 slap_passwd_generate(&new);
1547 if( new.bv_len == 0 ) {
1548 rs->sr_text = "password generation failed.";
1549 rs->sr_err = LDAP_OTHER;
1553 rs->sr_rspdata = slap_passwd_return( &new );
1556 #if SASL_VERSION_MAJOR < 2
1557 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx,
1558 id.bv_val, new.bv_val, new.bv_len, 0, &rs->sr_text );
1560 rs->sr_err = sasl_setpass( op->o_conn->c_sasl_authctx, id.bv_val,
1561 new.bv_val, new.bv_len, old.bv_val, old.bv_len, 0 );
1562 if( rs->sr_err != SASL_OK ) {
1563 rs->sr_text = sasl_errdetail( op->o_conn->c_sasl_authctx );
1566 switch(rs->sr_err) {
1568 rs->sr_err = LDAP_SUCCESS;
1578 rs->sr_err = LDAP_OTHER;
1584 #endif /* HAVE_CYRUS_SASL */
1586 /* Take any sort of identity string and return a DN with the "dn:" prefix. The
1587 string returned in *dn is in its own allocated memory, and must be free'd
1588 by the calling process.
1589 -Mark Adamson, Carnegie Mellon
1591 The "dn:" prefix is no longer used anywhere inside slapd. It is only used
1592 on strings passed in directly from SASL.
1593 -Howard Chu, Symas Corp.
1600 static struct berval ext_bv = BER_BVC( "EXTERNAL" );
1602 int slap_sasl_getdn( Connection *conn, Operation *op, char *id, int len,
1603 char *user_realm, struct berval *dn, int flags )
1606 int rc, is_dn = SET_NONE, do_norm = 1;
1610 LDAP_LOG( TRANSPORT, ENTRY,
1611 "slap_sasl_getdn: conn %d id=%s [len=%d]\n",
1612 conn ? conn->c_connid : -1, id ? (*id ? id : "<empty>") : "NULL", len );
1614 Debug( LDAP_DEBUG_ARGS, "slap_sasl_getdn: id=%s [len=%d]\n",
1615 id ? ( *id ? id : "<empty>" ) : "NULL", len, 0 );
1619 op = conn->c_sasl_bindop;
1626 if ( len == 0 ) len = strlen( id );
1628 /* Blatantly anonymous ID */
1629 if ( len == sizeof("anonymous") - 1 &&
1630 !strcasecmp( id, "anonymous" ) ) {
1631 return( LDAP_SUCCESS );
1637 /* An authcID needs to be converted to authzID form. Set the
1638 * values directly into *dn; they will be normalized later. (and
1639 * normalizing always makes a new copy.) An ID from a TLS certificate
1640 * is already normalized, so copy it and skip normalization.
1642 if( flags & SLAP_GETDN_AUTHCID ) {
1643 if( conn->c_sasl_bind_mech.bv_len == ext_bv.bv_len &&
1644 strcasecmp( ext_bv.bv_val, conn->c_sasl_bind_mech.bv_val ) == 0 )
1646 /* EXTERNAL DNs are already normalized */
1649 ber_str2bv_x( id, len, 1, dn, op->o_tmpmemctx );
1652 /* convert to u:<username> form */
1658 if( is_dn == SET_NONE ) {
1659 if( !strncasecmp( id, "u:", sizeof("u:")-1 )) {
1663 } else if ( !strncasecmp( id, "dn:", sizeof("dn:")-1) ) {
1670 /* No other possibilities from here */
1671 if( is_dn == SET_NONE ) {
1674 return( LDAP_INAPPROPRIATE_AUTH );
1677 /* Username strings */
1678 if( is_dn == SET_U ) {
1680 len = dn->bv_len + sizeof("uid=")-1 + sizeof(",cn=auth")-1;
1682 /* username may have embedded realm name */
1683 if( ( realm = strchr( dn->bv_val, '@') ) ) {
1685 len += sizeof(",cn=")-2;
1686 } else if( user_realm && *user_realm ) {
1687 len += strlen( user_realm ) + sizeof(",cn=")-1;
1690 if( conn->c_sasl_bind_mech.bv_len ) {
1691 len += conn->c_sasl_bind_mech.bv_len + sizeof(",cn=")-1;
1694 /* Build the new dn */
1696 dn->bv_val = sl_malloc( len+1, op->o_tmpmemctx );
1697 if( dn->bv_val == NULL ) {
1699 LDAP_LOG( TRANSPORT, ERR,
1700 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1702 Debug( LDAP_DEBUG_ANY,
1703 "slap_sasl_getdn: SLAP_MALLOC failed", 0, 0, 0 );
1707 p = lutil_strcopy( dn->bv_val, "uid=" );
1708 p = lutil_strncopy( p, c1, dn->bv_len );
1711 int rlen = dn->bv_len - ( realm - c1 );
1712 p = lutil_strcopy( p, ",cn=" );
1713 p = lutil_strncopy( p, realm, rlen );
1715 } else if( user_realm && *user_realm ) {
1716 p = lutil_strcopy( p, ",cn=" );
1717 p = lutil_strcopy( p, user_realm );
1720 if( conn->c_sasl_bind_mech.bv_len ) {
1721 p = lutil_strcopy( p, ",cn=" );
1722 p = lutil_strcopy( p, conn->c_sasl_bind_mech.bv_val );
1724 p = lutil_strcopy( p, ",cn=auth" );
1725 dn->bv_len = p - dn->bv_val;
1728 LDAP_LOG( TRANSPORT, ENTRY,
1729 "slap_sasl_getdn: u:id converted to %s.\n", dn->bv_val, 0, 0 );
1731 Debug( LDAP_DEBUG_TRACE, "getdn: u:id converted to %s\n", dn->bv_val,0,0 );
1735 /* Dup the DN in any case, so we don't risk
1736 * leaks or dangling pointers later,
1737 * and the DN value is '\0' terminated */
1738 ber_dupbv_x( &dn2, dn, op->o_tmpmemctx );
1739 dn->bv_val = dn2.bv_val;
1742 /* All strings are in DN form now. Normalize if needed. */
1744 rc = dnNormalize( 0, NULL, NULL, dn, &dn2, op->o_tmpmemctx );
1746 /* User DNs were constructed above and must be freed now */
1747 sl_free( dn->bv_val, op->o_tmpmemctx );
1749 if ( rc != LDAP_SUCCESS ) {
1757 /* Run thru regexp */
1758 slap_sasl2dn( op, dn, &dn2 );
1760 sl_free( dn->bv_val, op->o_tmpmemctx );
1763 LDAP_LOG( TRANSPORT, ENTRY,
1764 "slap_sasl_getdn: dn:id converted to %s.\n", dn->bv_val, 0, 0 );
1766 Debug( LDAP_DEBUG_TRACE, "getdn: dn:id converted to %s\n",
1771 return( LDAP_SUCCESS );