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;
58 LDAP_LOG( BACK_META, ENTRY, "meta_back_bind: dn: %s.\n",
59 op->o_req_dn.bv_val, 0, 0 );
60 #else /* !NEW_LOGGING */
61 Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
62 op->o_req_dn.bv_val, "", "" );
63 #endif /* !NEW_LOGGING */
65 if ( op->oq_bind.rb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
67 ber_dupbv( &op->oq_bind.rb_edn, be_root_dn( op->o_bd ) );
68 op_type = META_OP_REQUIRE_ALL;
70 lc = meta_back_getconn( op, rs, op_type,
71 &op->o_req_ndn, NULL );
74 LDAP_LOG( BACK_META, NOTICE,
75 "meta_back_bind: no target for dn %s.\n",
76 op->o_req_dn.bv_val, 0, 0 );
77 #else /* !NEW_LOGGING */
78 Debug( LDAP_DEBUG_ANY,
79 "meta_back_bind: no target for dn %s.\n%s%s",
80 op->o_req_dn.bv_val, "", "");
81 #endif /* !NEW_LOGGING */
83 send_ldap_result( op, rs );
88 * Each target is scanned ...
90 lc->bound_target = META_BOUND_NONE;
91 ndnlen = op->o_req_ndn.bv_len;
92 for ( i = 0; i < li->ntargets; i++ ) {
94 struct berval orig_dn = op->o_req_dn;
95 struct berval orig_ndn = op->o_req_ndn;
96 struct berval orig_cred = op->oq_bind.rb_cred;
97 int orig_method = op->oq_bind.rb_method;
101 * Skip non-candidates
103 if ( lc->conns[ i ].candidate != META_CANDIDATE ) {
111 * A bind operation is expected to have
112 * ONE CANDIDATE ONLY!
115 LDAP_LOG( BACK_META, WARNING,
116 "==>meta_back_bind: more than one"
117 " candidate is attempting to bind"
118 " ...\n" , 0, 0, 0 );
119 #else /* !NEW_LOGGING */
120 Debug( LDAP_DEBUG_ANY,
121 "==>meta_back_bind: more than one"
122 " candidate is attempting to bind"
125 #endif /* !NEW_LOGGING */
128 if ( isroot && li->targets[ i ]->pseudorootdn.bv_val != NULL ) {
129 op->o_req_dn = li->targets[ i ]->pseudorootdn;
130 op->o_req_ndn = li->targets[ i ]->pseudorootdn;
131 op->oq_bind.rb_cred = li->targets[ i ]->pseudorootpw;
132 op->oq_bind.rb_method = LDAP_AUTH_SIMPLE;
135 lerr = meta_back_do_single_bind( lc, op, rs, i );
136 if ( lerr != LDAP_SUCCESS ) {
138 ( void )meta_clear_one_candidate( &lc->conns[ i ], 1 );
143 op->o_req_dn = orig_dn;
144 op->o_req_ndn = orig_ndn;
145 op->oq_bind.rb_cred = orig_cred;
146 op->oq_bind.rb_method = orig_method;
150 lc->bound_target = META_BOUND_ALL;
154 * rc is LDAP_SUCCESS if at least one bind succeeded,
155 * err is the last error that occurred during a bind;
156 * if at least (and at most?) one bind succeedes, fine.
158 if ( rc != LDAP_SUCCESS /* && rs->sr_err != LDAP_SUCCESS */ ) {
161 * deal with bind failure ...
165 * no target was found within the naming context,
166 * so bind must fail with invalid credentials
168 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
169 rs->sr_err = LDAP_INVALID_CREDENTIALS;
172 rs->sr_err = ldap_back_map_result( rs );
173 send_ldap_result( op, rs );
181 * meta_back_do_single_bind
183 * attempts to perform a bind with creds
186 meta_back_do_single_bind(
193 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
194 struct berval mdn = { 0, NULL };
197 struct metasingleconn *lsc = &lc->conns[ candidate ];
201 * Rewrite the bind dn if needed
203 dc.rwmap = &li->targets[ candidate ]->rwmap;
204 dc.conn = op->o_conn;
208 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
209 send_ldap_result( op, rs );
214 rs->sr_err = ldap_set_option( lsc->ld,
215 LDAP_OPT_SERVER_CONTROLS, op->o_ctrls );
216 if ( rs->sr_err != LDAP_SUCCESS ) {
217 rs->sr_err = ldap_back_map_result( rs );
222 /* FIXME: this fixes the bind problem right now; we need
223 * to use the asynchronous version to get the "matched"
224 * and more in case of failure ... */
225 rs->sr_err = ldap_sasl_bind_s(lsc->ld, mdn.bv_val,
226 LDAP_SASL_SIMPLE, &op->oq_bind.rb_cred,
227 op->o_ctrls, NULL, NULL);
228 if ( rs->sr_err != LDAP_SUCCESS ) {
229 rs->sr_err = ldap_back_map_result( rs );
234 * FIXME: handle response!!!
236 if ( lsc->bound_dn.bv_val != NULL ) {
237 ber_memfree( lsc->bound_dn.bv_val );
239 ber_dupbv( &lsc->bound_dn, &op->o_req_dn );
240 lsc->bound = META_BOUND;
241 lc->bound_target = candidate;
243 if ( li->savecred ) {
244 if ( lsc->cred.bv_val ) {
245 memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
246 ber_memfree( lsc->cred.bv_val );
248 ber_dupbv( &lsc->cred, &op->oq_bind.rb_cred );
249 ldap_set_rebind_proc( lsc->ld, meta_back_rebind, lsc );
252 if ( li->cache.ttl != META_DNCACHE_DISABLED
253 && op->o_req_ndn.bv_len != 0 ) {
254 ( void )meta_dncache_update_entry( &li->cache,
255 &op->o_req_ndn, candidate );
260 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
271 meta_back_dobind( struct metaconn *lc, Operation *op )
273 struct metasingleconn *lsc;
277 * all the targets are bound as pseudoroot
279 if ( lc->bound_target == META_BOUND_ALL ) {
283 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
285 struct berval cred = BER_BVC("");
288 * Not a candidate or something wrong with this target ...
290 if ( lsc->ld == NULL ) {
295 * If required, set controls
298 if ( ldap_set_option( lsc->ld, LDAP_OPT_SERVER_CONTROLS,
299 op->o_ctrls ) != LDAP_SUCCESS ) {
300 ( void )meta_clear_one_candidate( lsc, 1 );
306 * If the target is already bound it is skipped
308 if ( lsc->bound == META_BOUND && lc->bound_target == i ) {
314 * Otherwise an anonymous bind is performed
315 * (note: if the target was already bound, the anonymous
316 * bind clears the previous bind).
318 if ( lsc->bound_dn.bv_val ) {
319 ber_memfree( lsc->bound_dn.bv_val );
320 lsc->bound_dn.bv_val = NULL;
321 lsc->bound_dn.bv_len = 0;
324 if ( /* FIXME: need li ... li->savecred && */
326 memset( lsc->cred.bv_val, 0, lsc->cred.bv_len );
327 ber_memfree( lsc->cred.bv_val );
328 lsc->cred.bv_val = NULL;
329 lsc->cred.bv_len = 0;
332 rc = ldap_sasl_bind_s(lsc->ld, "", LDAP_SASL_SIMPLE, &cred,
333 op->o_ctrls, NULL, NULL);
334 if ( rc != LDAP_SUCCESS ) {
337 LDAP_LOG( BACK_META, WARNING,
338 "meta_back_dobind: (anonymous)"
340 " with error %d (%s)\n",
341 rc, ldap_err2string( rc ), 0 );
342 #else /* !NEW_LOGGING */
343 Debug( LDAP_DEBUG_ANY,
344 "==>meta_back_dobind: (anonymous)"
346 " with error %d (%s)\n",
347 rc, ldap_err2string( rc ), 0 );
348 #endif /* !NEW_LOGGING */
351 * null cred bind should always succeed
352 * as anonymous, so a failure means
353 * the target is no longer candidate possibly
354 * due to technical reasons (remote host down?)
355 * so better clear the handle
357 ( void )meta_clear_one_candidate( lsc, 1 );
361 lsc->bound = META_ANONYMOUS;
372 meta_back_is_valid( struct metaconn *lc, int candidate )
374 struct metasingleconn *lsc;
379 if ( candidate < 0 ) {
383 for ( i = 0, lsc = lc->conns; !META_LAST(lsc) && i < candidate;
386 if ( !META_LAST(lsc) ) {
387 return( lsc->ld != NULL );
396 * This is a callback used for chasing referrals using the same
397 * credentials as the original user on this session.
400 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
401 ber_int_t msgid, void *params )
403 struct metasingleconn *lc = params;
405 return ldap_bind_s( ld, lc->bound_dn.bv_val, lc->cred.bv_val,
410 * FIXME: error return must be handled in a cleaner way ...
413 meta_back_op_result( struct metaconn *lc, Operation *op, SlapReply *rs )
415 int i, rerr = LDAP_SUCCESS;
416 struct metasingleconn *lsc;
419 int free_rmsg = 0, free_rmatch = 0;
421 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
425 rs->sr_err = LDAP_SUCCESS;
427 ldap_get_option( lsc->ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
428 if ( rs->sr_err != LDAP_SUCCESS ) {
430 * better check the type of error. In some cases
431 * (search ?) it might be better to return a
432 * success if at least one of the targets gave
433 * positive result ...
435 ldap_get_option( lsc->ld,
436 LDAP_OPT_ERROR_STRING, &msg );
437 ldap_get_option( lsc->ld,
438 LDAP_OPT_MATCHED_DN, &match );
439 rs->sr_err = ldap_back_map_result( rs );
442 LDAP_LOG( BACK_META, RESULTS,
443 "meta_back_op_result: target"
444 " <%d> sending msg \"%s\""
445 " (matched \"%s\")\n",
446 i, ( msg ? msg : "" ),
447 ( match ? match : "" ) );
448 #else /* !NEW_LOGGING */
449 Debug(LDAP_DEBUG_ANY,
450 "==> meta_back_op_result: target"
451 " <%d> sending msg \"%s\""
452 " (matched \"%s\")\n",
453 i, ( msg ? msg : "" ),
454 ( match ? match : "" ) );
455 #endif /* !NEW_LOGGING */
458 * FIXME: need to rewrite "match" (need rwinfo)
460 switch ( rs->sr_err ) {
470 ber_memfree( rmatch );
478 /* better test the pointers before freeing? */
490 rs->sr_matched = rmatch;
491 send_ldap_result( op, rs );
496 ber_memfree( rmatch );
499 rs->sr_matched = NULL;
501 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );