2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/socket.h>
28 #include <ac/string.h>
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
36 static LDAP_REBIND_PROC meta_back_rebind;
39 meta_back_do_single_bind(
47 meta_back_bind( Operation *op, SlapReply *rs )
49 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
52 int rc = -1, i, gotit = 0, ndnlen, isroot = 0;
53 int op_type = META_OP_ALLOW_MULTIPLE;
55 rs->sr_err = LDAP_SUCCESS;
57 Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
58 op->o_req_dn.bv_val, "", "" );
60 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
62 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
63 op_type = META_OP_REQUIRE_ALL;
65 lc = meta_back_getconn( op, rs, op_type,
66 &op->o_req_ndn, NULL, LDAP_BACK_SENDERR );
68 Debug( LDAP_DEBUG_ANY,
69 "meta_back_bind: no target for dn %s.\n%s%s",
70 op->o_req_dn.bv_val, "", "");
72 send_ldap_result( op, rs );
77 * Each target is scanned ...
79 lc->mc_bound_target = META_BOUND_NONE;
80 ndnlen = op->o_req_ndn.bv_len;
81 for ( i = 0; i < li->ntargets; i++ ) {
83 struct berval orig_dn = op->o_req_dn;
84 struct berval orig_ndn = op->o_req_ndn;
85 struct berval orig_cred = op->orb_cred;
86 int orig_method = op->orb_method;
92 if ( lc->mc_conns[ i ].msc_candidate != META_CANDIDATE ) {
100 * A bind operation is expected to have
101 * ONE CANDIDATE ONLY!
103 Debug( LDAP_DEBUG_ANY,
104 "==>meta_back_bind: more than one"
105 " candidate is attempting to bind"
110 if ( isroot && li->targets[ i ]->mt_pseudorootdn.bv_val != NULL ) {
111 op->o_req_dn = li->targets[ i ]->mt_pseudorootdn;
112 op->o_req_ndn = li->targets[ i ]->mt_pseudorootdn;
113 op->orb_cred = li->targets[ i ]->mt_pseudorootpw;
114 op->orb_method = LDAP_AUTH_SIMPLE;
117 lerr = meta_back_do_single_bind( lc, op, rs, i );
118 if ( lerr != LDAP_SUCCESS ) {
120 ( void )meta_clear_one_candidate( &lc->mc_conns[ i ], 1 );
126 op->o_req_dn = orig_dn;
127 op->o_req_ndn = orig_ndn;
128 op->orb_cred = orig_cred;
129 op->orb_method = orig_method;
133 lc->mc_bound_target = META_BOUND_ALL;
137 * rc is LDAP_SUCCESS if at least one bind succeeded,
138 * err is the last error that occurred during a bind;
139 * if at least (and at most?) one bind succeedes, fine.
141 if ( rc != LDAP_SUCCESS /* && rs->sr_err != LDAP_SUCCESS */ ) {
144 * deal with bind failure ...
148 * no target was found within the naming context,
149 * so bind must fail with invalid credentials
151 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
152 rs->sr_err = LDAP_INVALID_CREDENTIALS;
155 rs->sr_err = slap_map_api2result( rs );
156 send_ldap_result( op, rs );
164 * meta_back_do_single_bind
166 * attempts to perform a bind with creds
169 meta_back_do_single_bind(
176 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
177 struct berval mdn = BER_BVNULL;
179 struct metasingleconn *lsc = &lc->mc_conns[ candidate ];
183 * Rewrite the bind dn if needed
185 dc.rwmap = &li->targets[ candidate ]->mt_rwmap;
186 dc.conn = op->o_conn;
190 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
191 send_ldap_result( op, rs );
195 /* FIXME: this fixes the bind problem right now; we need
196 * to use the asynchronous version to get the "matched"
197 * and more in case of failure ... */
198 /* FIXME: should be check if at least some of the op->o_ctrls
199 * can/should be passed? */
200 rs->sr_err = ldap_sasl_bind( lsc->msc_ld, mdn.bv_val,
201 LDAP_SASL_SIMPLE, &op->orb_cred,
202 op->o_ctrls, NULL, &msgid );
203 if ( rs->sr_err == LDAP_SUCCESS ) {
205 struct timeval tv = { 0, 0 };
213 switch ( ldap_result( lsc->msc_ld, msgid, 0, &tv, &res ) ) {
215 if ( ++nretries <= META_BIND_NRETRIES ) {
216 ldap_pvt_thread_yield();
218 tv.tv_usec = META_BIND_TIMEOUT;
221 rs->sr_err = LDAP_BUSY;
225 ldap_get_option( lsc->msc_ld, LDAP_OPT_ERROR_NUMBER,
230 rc = ldap_parse_result( lsc->msc_ld, res, &rs->sr_err,
231 NULL, NULL, NULL, NULL, 1 );
232 if ( rc != LDAP_SUCCESS ) {
239 if ( rs->sr_err != LDAP_SUCCESS ) {
240 rs->sr_err = slap_map_api2result( rs );
244 if ( !BER_BVISNULL( &lsc->msc_bound_ndn ) ) {
245 ber_memfree( lsc->msc_bound_ndn.bv_val );
247 ber_dupbv( &lsc->msc_bound_ndn, &op->o_req_dn );
248 lsc->msc_bound = META_BOUND;
249 lc->mc_bound_target = candidate;
251 if ( LDAP_BACK_SAVECRED( li ) ) {
252 if ( !BER_BVISNULL( &lsc->msc_cred ) ) {
253 /* destroy sensitive data */
254 memset( lsc->msc_cred.bv_val, 0, lsc->msc_cred.bv_len );
255 ber_memfree( lsc->msc_cred.bv_val );
257 ber_dupbv( &lsc->msc_cred, &op->orb_cred );
258 ldap_set_rebind_proc( lsc->msc_ld, meta_back_rebind, lsc );
261 if ( li->cache.ttl != META_DNCACHE_DISABLED
262 && op->o_req_ndn.bv_len != 0 ) {
263 ( void )meta_dncache_update_entry( &li->cache,
264 &op->o_req_ndn, candidate );
269 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
280 meta_back_dobind( struct metaconn *lc, Operation *op, ldap_back_send_t sendok )
282 struct metasingleconn *lsc;
286 * all the targets are bound as pseudoroot
288 if ( lc->mc_bound_target == META_BOUND_ALL ) {
292 for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); ++i, ++lsc ) {
294 struct berval cred = BER_BVC("");
298 * Not a candidate or something wrong with this target ...
300 if ( lsc->msc_ld == NULL ) {
305 * If the target is already bound it is skipped
307 if ( lsc->msc_bound == META_BOUND && lc->mc_bound_target == i ) {
313 * Otherwise an anonymous bind is performed
314 * (note: if the target was already bound, the anonymous
315 * bind clears the previous bind).
317 if ( !BER_BVISNULL( &lsc->msc_bound_ndn ) ) {
318 ber_memfree( lsc->msc_bound_ndn.bv_val );
319 BER_BVZERO( &lsc->msc_bound_ndn );
322 if ( /* FIXME: need li ... li->savecred && */
323 !BER_BVISNULL( &lsc->msc_cred ) )
325 /* destroy sensitive data */
326 memset( lsc->msc_cred.bv_val, 0, lsc->msc_cred.bv_len );
327 ber_memfree( lsc->msc_cred.bv_val );
328 BER_BVZERO( &lsc->msc_cred );
331 /* FIXME: should we check if at least some of the op->o_ctrls
332 * can/should be passed? */
333 rc = ldap_sasl_bind( lsc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
334 NULL, NULL, &msgid );
335 if ( rc == LDAP_SUCCESS ) {
337 struct timeval tv = { 0, 0 };
345 switch ( ldap_result( lsc->msc_ld, msgid, 0, &tv, &res ) ) {
347 if ( ++nretries <= META_BIND_NRETRIES ) {
348 ldap_pvt_thread_yield();
350 tv.tv_usec = META_BIND_TIMEOUT;
358 ldap_get_option( lsc->msc_ld, LDAP_OPT_ERROR_NUMBER,
363 rc = ldap_parse_result( lsc->msc_ld, res, &err,
364 NULL, NULL, NULL, NULL, 1 );
365 if ( rc == LDAP_SUCCESS ) {
372 if ( rc != LDAP_SUCCESS ) {
373 Debug( LDAP_DEBUG_ANY,
374 "==>meta_back_dobind: (anonymous)"
376 " with error %d (%s)\n",
377 rc, ldap_err2string( rc ), 0 );
380 * null cred bind should always succeed
381 * as anonymous, so a failure means
382 * the target is no longer candidate possibly
383 * due to technical reasons (remote host down?)
384 * so better clear the handle
386 ( void )meta_clear_one_candidate( lsc, 1 );
390 lsc->msc_bound = META_ANONYMOUS;
401 meta_back_is_valid( struct metaconn *lc, int candidate )
403 struct metasingleconn *lsc;
408 if ( candidate < 0 ) {
412 for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ) && i < candidate;
415 if ( !META_LAST( lsc ) ) {
416 return ( lsc->msc_ld != NULL );
425 * This is a callback used for chasing referrals using the same
426 * credentials as the original user on this session.
429 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
430 ber_int_t msgid, void *params )
432 struct metasingleconn *lsc = params;
434 return ldap_sasl_bind_s( ld, lsc->msc_bound_ndn.bv_val,
435 LDAP_SASL_SIMPLE, &lsc->msc_cred,
440 * FIXME: error return must be handled in a cleaner way ...
443 meta_back_op_result( struct metaconn *lc, Operation *op, SlapReply *rs )
447 struct metasingleconn *lsc;
453 for ( i = 0, lsc = lc->mc_conns; !META_LAST( lsc ); ++i, ++lsc ) {
457 rs->sr_err = LDAP_SUCCESS;
459 ldap_get_option( lsc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
460 if ( rs->sr_err != LDAP_SUCCESS ) {
462 * better check the type of error. In some cases
463 * (search ?) it might be better to return a
464 * success if at least one of the targets gave
465 * positive result ...
467 ldap_get_option( lsc->msc_ld,
468 LDAP_OPT_ERROR_STRING, &msg );
469 ldap_get_option( lsc->msc_ld,
470 LDAP_OPT_MATCHED_DN, &match );
471 rs->sr_err = slap_map_api2result( rs );
473 Debug(LDAP_DEBUG_ANY,
474 "==> meta_back_op_result: target"
475 " <%d> sending msg \"%s\""
476 " (matched \"%s\")\n",
477 i, ( msg ? msg : "" ),
478 ( match ? match : "" ) );
481 * FIXME: need to rewrite "match" (need rwinfo)
483 switch ( rs->sr_err ) {
493 ber_memfree( rmatch );
501 /* better test the pointers before freeing? */
513 rs->sr_matched = rmatch;
514 send_ldap_result( op, rs );
519 ber_memfree( rmatch );
522 rs->sr_matched = NULL;
524 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );