2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 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->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
62 ber_dupbv( &op->oq_bind.rb_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 );
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->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->oq_bind.rb_cred;
86 int orig_method = op->oq_bind.rb_method;
92 if ( lc->conns[ i ].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 ]->pseudorootdn.bv_val != NULL ) {
111 op->o_req_dn = li->targets[ i ]->pseudorootdn;
112 op->o_req_ndn = li->targets[ i ]->pseudorootdn;
113 op->oq_bind.rb_cred = li->targets[ i ]->pseudorootpw;
114 op->oq_bind.rb_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->conns[ i ], 1 );
125 op->o_req_dn = orig_dn;
126 op->o_req_ndn = orig_ndn;
127 op->oq_bind.rb_cred = orig_cred;
128 op->oq_bind.rb_method = orig_method;
132 lc->bound_target = META_BOUND_ALL;
136 * rc is LDAP_SUCCESS if at least one bind succeeded,
137 * err is the last error that occurred during a bind;
138 * if at least (and at most?) one bind succeedes, fine.
140 if ( rc != LDAP_SUCCESS /* && rs->sr_err != LDAP_SUCCESS */ ) {
143 * deal with bind failure ...
147 * no target was found within the naming context,
148 * so bind must fail with invalid credentials
150 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
151 rs->sr_err = LDAP_INVALID_CREDENTIALS;
154 rs->sr_err = slap_map_api2result( rs );
155 send_ldap_result( op, rs );
163 * meta_back_do_single_bind
165 * attempts to perform a bind with creds
168 meta_back_do_single_bind(
175 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
176 struct berval mdn = BER_BVNULL;
179 struct metasingleconn *lsc = &lc->conns[ candidate ];
183 * Rewrite the bind dn if needed
185 dc.rwmap = &li->targets[ candidate ]->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 );
196 rs->sr_err = ldap_set_option( lsc->ld,
197 LDAP_OPT_SERVER_CONTROLS, op->o_ctrls );
198 if ( rs->sr_err != LDAP_SUCCESS ) {
199 rs->sr_err = slap_map_api2result( rs );
204 /* FIXME: this fixes the bind problem right now; we need
205 * to use the asynchronous version to get the "matched"
206 * and more in case of failure ... */
207 rs->sr_err = ldap_sasl_bind_s(lsc->ld, mdn.bv_val,
208 LDAP_SASL_SIMPLE, &op->oq_bind.rb_cred,
209 op->o_ctrls, NULL, NULL);
210 if ( rs->sr_err != LDAP_SUCCESS ) {
211 rs->sr_err = slap_map_api2result( rs );
216 * FIXME: handle response!!!
218 if ( lsc->bound_dn.bv_val != NULL ) {
219 ber_memfree( lsc->bound_dn.bv_val );
221 ber_dupbv( &lsc->bound_dn, &op->o_req_dn );
222 lsc->bound = META_BOUND;
223 lc->bound_target = candidate;
225 if ( li->savecred ) {
226 if ( lsc->cred.bv_val ) {
227 memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
228 ber_memfree( lsc->cred.bv_val );
230 ber_dupbv( &lsc->cred, &op->oq_bind.rb_cred );
231 ldap_set_rebind_proc( lsc->ld, meta_back_rebind, lsc );
234 if ( li->cache.ttl != META_DNCACHE_DISABLED
235 && op->o_req_ndn.bv_len != 0 ) {
236 ( void )meta_dncache_update_entry( &li->cache,
237 &op->o_req_ndn, candidate );
242 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
253 meta_back_dobind( struct metaconn *lc, Operation *op )
255 struct metasingleconn *lsc;
259 * all the targets are bound as pseudoroot
261 if ( lc->bound_target == META_BOUND_ALL ) {
265 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
267 struct berval cred = BER_BVC("");
270 * Not a candidate or something wrong with this target ...
272 if ( lsc->ld == NULL ) {
277 * If required, set controls
280 if ( ldap_set_option( lsc->ld, LDAP_OPT_SERVER_CONTROLS,
281 op->o_ctrls ) != LDAP_SUCCESS ) {
282 ( void )meta_clear_one_candidate( lsc, 1 );
288 * If the target is already bound it is skipped
290 if ( lsc->bound == META_BOUND && lc->bound_target == i ) {
296 * Otherwise an anonymous bind is performed
297 * (note: if the target was already bound, the anonymous
298 * bind clears the previous bind).
300 if ( lsc->bound_dn.bv_val ) {
301 ber_memfree( lsc->bound_dn.bv_val );
302 lsc->bound_dn.bv_val = NULL;
303 lsc->bound_dn.bv_len = 0;
306 if ( /* FIXME: need li ... li->savecred && */
308 memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
309 ber_memfree( lsc->cred.bv_val );
310 lsc->cred.bv_val = NULL;
311 lsc->cred.bv_len = 0;
314 rc = ldap_sasl_bind_s(lsc->ld, "", LDAP_SASL_SIMPLE, &cred,
315 op->o_ctrls, NULL, NULL);
316 if ( rc != LDAP_SUCCESS ) {
318 Debug( LDAP_DEBUG_ANY,
319 "==>meta_back_dobind: (anonymous)"
321 " with error %d (%s)\n",
322 rc, ldap_err2string( rc ), 0 );
325 * null cred bind should always succeed
326 * as anonymous, so a failure means
327 * the target is no longer candidate possibly
328 * due to technical reasons (remote host down?)
329 * so better clear the handle
331 ( void )meta_clear_one_candidate( lsc, 1 );
335 lsc->bound = META_ANONYMOUS;
346 meta_back_is_valid( struct metaconn *lc, int candidate )
348 struct metasingleconn *lsc;
353 if ( candidate < 0 ) {
357 for ( i = 0, lsc = lc->conns; !META_LAST(lsc) && i < candidate;
360 if ( !META_LAST(lsc) ) {
361 return( lsc->ld != NULL );
370 * This is a callback used for chasing referrals using the same
371 * credentials as the original user on this session.
374 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
375 ber_int_t msgid, void *params )
377 struct metasingleconn *lc = params;
379 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val,
384 * FIXME: error return must be handled in a cleaner way ...
387 meta_back_op_result( struct metaconn *lc, Operation *op, SlapReply *rs )
389 int i, rerr = LDAP_SUCCESS;
390 struct metasingleconn *lsc;
393 int free_rmsg = 0, free_rmatch = 0;
395 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
399 rs->sr_err = LDAP_SUCCESS;
401 ldap_get_option( lsc->ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
402 if ( rs->sr_err != LDAP_SUCCESS ) {
404 * better check the type of error. In some cases
405 * (search ?) it might be better to return a
406 * success if at least one of the targets gave
407 * positive result ...
409 ldap_get_option( lsc->ld,
410 LDAP_OPT_ERROR_STRING, &msg );
411 ldap_get_option( lsc->ld,
412 LDAP_OPT_MATCHED_DN, &match );
413 rs->sr_err = slap_map_api2result( rs );
415 Debug(LDAP_DEBUG_ANY,
416 "==> meta_back_op_result: target"
417 " <%d> sending msg \"%s\""
418 " (matched \"%s\")\n",
419 i, ( msg ? msg : "" ),
420 ( match ? match : "" ) );
423 * FIXME: need to rewrite "match" (need rwinfo)
425 switch ( rs->sr_err ) {
435 ber_memfree( rmatch );
443 /* better test the pointers before freeing? */
455 rs->sr_matched = rmatch;
456 send_ldap_result( op, rs );
461 ber_memfree( rmatch );
464 rs->sr_matched = NULL;
466 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );