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_single_bind(
46 meta_back_bind( Operation *op, SlapReply *rs )
48 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
52 i, gotit = 0, isroot = 0;
54 SlapReply *candidates = meta_back_candidates_get( op );
56 rs->sr_err = LDAP_SUCCESS;
58 Debug( LDAP_DEBUG_ARGS, "meta_back_bind: dn: %s.\n%s%s",
59 op->o_req_dn.bv_val, "", "" );
61 if ( op->orb_method == LDAP_AUTH_SIMPLE && be_isroot_pw( op ) ) {
63 ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
66 /* we need meta_back_getconn() not send result even on error,
67 * because we want to intercept the error and make it
68 * invalidCredentials */
69 mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_DONTSEND );
71 Debug( LDAP_DEBUG_ANY,
72 "meta_back_bind: no target "
73 "for dn \"%s\" (%d: %s).\n",
74 op->o_req_dn.bv_val, rs->sr_err,
75 rs->sr_text ? rs->sr_text : "" );
76 /* FIXME: there might be cases where we don't want
77 * to map the error onto invalidCredentials */
78 switch ( rs->sr_err ) {
79 case LDAP_NO_SUCH_OBJECT:
80 case LDAP_UNWILLING_TO_PERFORM:
81 rs->sr_err = LDAP_INVALID_CREDENTIALS;
85 send_ldap_result( op, rs );
90 * Each target is scanned ...
92 mc->mc_auth_target = META_BOUND_NONE;
93 for ( i = 0; i < li->mi_ntargets; i++ ) {
100 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
107 } else if ( isroot == 0 ) {
109 * A bind operation is expected to have
110 * ONE CANDIDATE ONLY!
112 Debug( LDAP_DEBUG_ANY,
113 "==>meta_back_bind: more than one"
114 " candidate is trying to bind...\n",
118 if ( isroot && !BER_BVISNULL( &li->mi_targets[ i ]->mt_pseudorootdn ) )
120 op2.o_req_dn = li->mi_targets[ i ]->mt_pseudorootdn;
121 op2.o_req_ndn = li->mi_targets[ i ]->mt_pseudorootdn;
122 op2.orb_cred = li->mi_targets[ i ]->mt_pseudorootpw;
123 op2.orb_method = LDAP_AUTH_SIMPLE;
126 lerr = meta_back_single_bind( &op2, rs, mc, i );
127 if ( lerr != LDAP_SUCCESS ) {
129 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
137 mc->mc_auth_target = META_BOUND_ALL;
141 * rc is LDAP_SUCCESS if at least one bind succeeded,
142 * err is the last error that occurred during a bind;
143 * if at least (and at most?) one bind succeedes, fine.
145 if ( rc != LDAP_SUCCESS ) {
148 * deal with bind failure ...
152 * no target was found within the naming context,
153 * so bind must fail with invalid credentials
155 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
156 rs->sr_err = LDAP_INVALID_CREDENTIALS;
159 rs->sr_err = slap_map_api2result( rs );
160 send_ldap_result( op, rs );
168 * meta_back_single_bind
170 * attempts to perform a bind with creds
173 meta_back_single_bind(
180 struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
181 struct berval mdn = BER_BVNULL;
183 struct metasingleconn *msc = &mc->mc_conns[ candidate ];
187 * Rewrite the bind dn if needed
189 dc.rwmap = &li->mi_targets[ candidate ]->mt_rwmap;
190 dc.conn = op->o_conn;
194 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
195 send_ldap_result( op, rs );
199 /* FIXME: this fixes the bind problem right now; we need
200 * to use the asynchronous version to get the "matched"
201 * and more in case of failure ... */
202 /* FIXME: should be check if at least some of the op->o_ctrls
203 * can/should be passed? */
204 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
205 LDAP_SASL_SIMPLE, &op->orb_cred,
206 op->o_ctrls, NULL, &msgid );
207 if ( rs->sr_err == LDAP_SUCCESS ) {
209 struct timeval tv = { 0, 0 };
211 int nretries = META_BIND_NRETRIES;
217 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
219 if ( nretries > 0 ) {
220 ldap_pvt_thread_yield();
222 tv.tv_usec = META_BIND_TIMEOUT;
226 rs->sr_err = LDAP_BUSY;
230 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
232 if ( rs->sr_err == LDAP_UNAVAILABLE && nretries > 0 ) {
234 if ( meta_back_retry( op, rs, mc, candidate, LDAP_BACK_SENDERR ) ) {
241 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
242 NULL, NULL, NULL, NULL, 1 );
243 if ( rc != LDAP_SUCCESS ) {
250 if ( rs->sr_err != LDAP_SUCCESS ) {
251 rs->sr_err = slap_map_api2result( rs );
255 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
256 msc->msc_bound = META_BOUND;
257 mc->mc_auth_target = candidate;
259 if ( LDAP_BACK_SAVECRED( li ) ) {
260 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
261 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind, msc );
264 if ( li->mi_cache.ttl != META_DNCACHE_DISABLED
265 && op->o_req_ndn.bv_len != 0 )
267 ( void )meta_dncache_update_entry( &li->mi_cache,
268 &op->o_req_ndn, candidate );
272 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
280 * meta_back_single_dobind
283 meta_back_single_dobind(
285 struct metasingleconn *msc,
286 ldap_back_send_t sendok,
290 struct berval cred = BER_BVC( "" );
294 * Otherwise an anonymous bind is performed
295 * (note: if the target was already bound, the anonymous
296 * bind clears the previous bind).
298 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
299 ber_memfree( msc->msc_bound_ndn.bv_val );
300 BER_BVZERO( &msc->msc_bound_ndn );
303 if ( /* FIXME: need li ... li->savecred && */
304 !BER_BVISNULL( &msc->msc_cred ) )
306 /* destroy sensitive data */
307 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
308 ber_memfree( msc->msc_cred.bv_val );
309 BER_BVZERO( &msc->msc_cred );
312 /* FIXME: should we check if at least some of the op->o_ctrls
313 * can/should be passed? */
314 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
315 NULL, NULL, &msgid );
316 if ( rc == LDAP_SUCCESS ) {
318 struct timeval tv = { 0, 0 };
325 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
328 ldap_pvt_thread_yield();
330 tv.tv_usec = META_BIND_TIMEOUT;
339 ldap_get_option( msc->msc_ld,
340 LDAP_OPT_ERROR_NUMBER, &rc );
344 rc = ldap_parse_result( msc->msc_ld, res, &err,
345 NULL, NULL, NULL, NULL, 1 );
346 if ( rc == LDAP_SUCCESS ) {
360 meta_back_dobind( struct metaconn *mc, Operation *op, ldap_back_send_t sendok )
362 struct metasingleconn *msc;
365 SlapReply *candidates = meta_back_candidates_get( op );
367 ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
370 * all the targets are bound as pseudoroot
372 if ( mc->mc_auth_target == META_BOUND_ALL ) {
377 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
381 * Not a candidate or something wrong with this target ...
383 if ( msc->msc_ld == NULL ) {
388 * If the target is already bound it is skipped
390 if ( msc->msc_bound == META_BOUND && mc->mc_auth_target == i ) {
395 rc = meta_back_single_dobind( op, msc, sendok, META_BIND_NRETRIES );
396 if ( rc != LDAP_SUCCESS ) {
397 Debug( LDAP_DEBUG_ANY, "%s meta_back_dobind[%d]: "
398 "(anonymous) err=%d\n",
399 op->o_log_prefix, i, rc );
402 * null cred bind should always succeed
403 * as anonymous, so a failure means
404 * the target is no longer candidate possibly
405 * due to technical reasons (remote host down?)
406 * so better clear the handle
408 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
410 ( void )meta_clear_one_candidate( msc );
415 candidates[ i ].sr_tag = META_CANDIDATE;
416 msc->msc_bound = META_ANONYMOUS;
421 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
429 * This is a callback used for chasing referrals using the same
430 * credentials as the original user on this session.
433 meta_back_rebind( LDAP *ld, LDAP_CONST char *url, ber_tag_t request,
434 ber_int_t msgid, void *params )
436 struct metasingleconn *msc = (struct metasingleconn *)params;
438 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
439 LDAP_SASL_SIMPLE, &msc->msc_cred,
444 * FIXME: error return must be handled in a cleaner way ...
455 struct metasingleconn *msc;
461 if ( candidate != META_TARGET_NONE ) {
462 msc = &mc->mc_conns[ candidate ];
464 rs->sr_err = LDAP_SUCCESS;
466 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
467 if ( rs->sr_err != LDAP_SUCCESS ) {
469 * better check the type of error. In some cases
470 * (search ?) it might be better to return a
471 * success if at least one of the targets gave
472 * positive result ...
474 ldap_get_option( msc->msc_ld,
475 LDAP_OPT_ERROR_STRING, &rmsg );
476 ldap_get_option( msc->msc_ld,
477 LDAP_OPT_MATCHED_DN, &rmatch );
478 rerr = rs->sr_err = slap_map_api2result( rs );
487 Debug(LDAP_DEBUG_ANY,
488 "==> meta_back_op_result: target"
489 " <%d> sending msg \"%s\""
490 " (matched \"%s\")\n",
491 candidate, ( rmsg ? rmsg : "" ),
492 ( rmatch ? rmatch : "" ) );
496 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
500 rs->sr_err = LDAP_SUCCESS;
502 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
503 if ( rs->sr_err != LDAP_SUCCESS ) {
505 * better check the type of error. In some cases
506 * (search ?) it might be better to return a
507 * success if at least one of the targets gave
508 * positive result ...
510 ldap_get_option( msc->msc_ld,
511 LDAP_OPT_ERROR_STRING, &msg );
512 ldap_get_option( msc->msc_ld,
513 LDAP_OPT_MATCHED_DN, &match );
514 rs->sr_err = slap_map_api2result( rs );
516 Debug(LDAP_DEBUG_ANY,
517 "==> meta_back_op_result: target"
518 " <%d> sending msg \"%s\""
519 " (matched \"%s\")\n",
520 i, ( msg ? msg : "" ),
521 ( match ? match : "" ) );
524 * FIXME: need to rewrite "match" (need rwinfo)
526 switch ( rs->sr_err ) {
536 ber_memfree( rmatch );
544 /* better test the pointers before freeing? */
557 rs->sr_matched = rmatch;
558 send_ldap_result( op, rs );
563 ber_memfree( rmatch );
566 rs->sr_matched = NULL;
568 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );