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 metainfo_t *mi = ( metainfo_t * )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 < mi->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( &mi->mi_targets[ i ]->mt_pseudorootdn ) )
120 op2.o_req_dn = mi->mi_targets[ i ]->mt_pseudorootdn;
121 op2.o_req_ndn = mi->mi_targets[ i ]->mt_pseudorootdn;
122 op2.orb_cred = mi->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(
179 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
180 metatarget_t *mt = mi->mi_targets[ candidate ];
181 struct berval mdn = BER_BVNULL;
183 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
187 * Rewrite the bind dn if needed
189 dc.rwmap = &mi->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? */
205 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
206 LDAP_SASL_SIMPLE, &op->orb_cred,
207 op->o_ctrls, NULL, &msgid );
208 if ( rs->sr_err == LDAP_SUCCESS ) {
210 struct timeval tv = { 0, 0 };
212 int nretries = META_BIND_NRETRIES;
218 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
220 if ( nretries > 0 ) {
221 ldap_pvt_thread_yield();
223 tv.tv_usec = META_BIND_TIMEOUT;
227 rs->sr_err = LDAP_BUSY;
231 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
234 Debug( LDAP_DEBUG_ANY, "### %s meta_back_single_bind(%s) err=%d\n",
235 op->o_log_prefix, mdn.bv_val, rs->sr_err );
237 if ( rs->sr_err == LDAP_UNAVAILABLE && nretries > 0 ) {
238 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
242 /* mc here must be the regular mc, reset and ready for init */
243 rc = meta_back_init_one_conn( op, rs, mt, msc,
244 LDAP_BACK_DONTSEND );
247 ldap_pvt_thread_yield();
254 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
255 NULL, NULL, NULL, NULL, 1 );
256 if ( rc != LDAP_SUCCESS ) {
263 if ( rs->sr_err != LDAP_SUCCESS ) {
264 rs->sr_err = slap_map_api2result( rs );
268 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
269 msc->msc_bound = META_BOUND;
270 mc->mc_auth_target = candidate;
272 if ( LDAP_BACK_SAVECRED( mi ) ) {
273 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
274 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind, msc );
277 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
278 && op->o_req_ndn.bv_len != 0 )
280 ( void )meta_dncache_update_entry( &mi->mi_cache,
281 &op->o_req_ndn, candidate );
285 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
293 * meta_back_single_dobind
296 meta_back_single_dobind(
301 ldap_back_send_t sendok,
304 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
305 metatarget_t *mt = mi->mi_targets[ candidate ];
306 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
308 struct berval cred = BER_BVC( "" );
312 * Otherwise an anonymous bind is performed
313 * (note: if the target was already bound, the anonymous
314 * bind clears the previous bind).
316 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
317 ber_memfree( msc->msc_bound_ndn.bv_val );
318 BER_BVZERO( &msc->msc_bound_ndn );
321 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
322 /* destroy sensitive data */
323 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
324 ber_memfree( msc->msc_cred.bv_val );
325 BER_BVZERO( &msc->msc_cred );
328 /* FIXME: should we check if at least some of the op->o_ctrls
329 * can/should be passed? */
331 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
332 NULL, NULL, &msgid );
333 if ( rc == LDAP_SUCCESS ) {
335 struct timeval tv = { 0, 0 };
342 tv.tv_usec = META_BIND_TIMEOUT;
343 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
346 ldap_pvt_thread_yield();
355 ldap_get_option( msc->msc_ld,
356 LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
358 Debug( LDAP_DEBUG_ANY, "### %s meta_back_single_dobind(\"\") err=%d\n",
359 op->o_log_prefix, rs->sr_err, 0 );
361 rc = slap_map_api2result( rs );
362 if ( rc == LDAP_UNAVAILABLE && retries > 0 ) {
363 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
367 /* mc here must be the regular mc, reset and ready for init */
368 rc = meta_back_init_one_conn( op, rs, mt, msc, LDAP_BACK_DONTSEND );
371 ldap_pvt_thread_yield();
379 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
380 NULL, NULL, NULL, NULL, 1 );
381 if ( rc == LDAP_SUCCESS ) {
382 rc = slap_map_api2result( rs );
388 if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
390 send_ldap_result( op, rs );
404 ldap_back_send_t sendok )
406 metasingleconn_t *msc;
409 SlapReply *candidates = meta_back_candidates_get( op );
411 ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
414 * all the targets are bound as pseudoroot
416 if ( mc->mc_auth_target == META_BOUND_ALL ) {
421 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
425 * Not a candidate or something wrong with this target ...
427 if ( msc->msc_ld == NULL ) {
432 * If the target is already bound it is skipped
434 if ( msc->msc_bound == META_BOUND && mc->mc_auth_target == i ) {
439 rc = meta_back_single_dobind( op, rs, mc, i,
440 LDAP_BACK_DONTSEND, META_BIND_NRETRIES );
441 if ( rc != LDAP_SUCCESS ) {
442 Debug( LDAP_DEBUG_ANY, "%s meta_back_dobind[%d]: "
443 "(anonymous) err=%d\n",
444 op->o_log_prefix, i, rc );
447 * null cred bind should always succeed
448 * as anonymous, so a failure means
449 * the target is no longer candidate possibly
450 * due to technical reasons (remote host down?)
451 * so better clear the handle
453 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
455 ( void )meta_clear_one_candidate( msc );
460 candidates[ i ].sr_tag = META_CANDIDATE;
461 msc->msc_bound = META_ANONYMOUS;
466 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
468 if ( bound == 0 && sendok & LDAP_BACK_SENDERR ) {
469 if ( rs->sr_err == LDAP_SUCCESS ) {
470 rs->sr_err = LDAP_BUSY;
472 send_ldap_result( op, rs );
481 * This is a callback used for chasing referrals using the same
482 * credentials as the original user on this session.
487 LDAP_CONST char *url,
492 metasingleconn_t *msc = ( metasingleconn_t * )params;
494 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
495 LDAP_SASL_SIMPLE, &msc->msc_cred,
500 * FIXME: error return must be handled in a cleaner way ...
511 metasingleconn_t *msc;
517 if ( candidate != META_TARGET_NONE ) {
518 msc = &mc->mc_conns[ candidate ];
520 rs->sr_err = LDAP_SUCCESS;
522 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
523 if ( rs->sr_err != LDAP_SUCCESS ) {
525 * better check the type of error. In some cases
526 * (search ?) it might be better to return a
527 * success if at least one of the targets gave
528 * positive result ...
530 ldap_get_option( msc->msc_ld,
531 LDAP_OPT_ERROR_STRING, &rmsg );
532 ldap_get_option( msc->msc_ld,
533 LDAP_OPT_MATCHED_DN, &rmatch );
534 rerr = rs->sr_err = slap_map_api2result( rs );
543 Debug(LDAP_DEBUG_ANY,
544 "==> meta_back_op_result: target"
545 " <%d> sending msg \"%s\""
546 " (matched \"%s\")\n",
547 candidate, ( rmsg ? rmsg : "" ),
548 ( rmatch ? rmatch : "" ) );
552 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
556 rs->sr_err = LDAP_SUCCESS;
558 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
559 if ( rs->sr_err != LDAP_SUCCESS ) {
561 * better check the type of error. In some cases
562 * (search ?) it might be better to return a
563 * success if at least one of the targets gave
564 * positive result ...
566 ldap_get_option( msc->msc_ld,
567 LDAP_OPT_ERROR_STRING, &msg );
568 ldap_get_option( msc->msc_ld,
569 LDAP_OPT_MATCHED_DN, &match );
570 rs->sr_err = slap_map_api2result( rs );
572 Debug(LDAP_DEBUG_ANY,
573 "==> meta_back_op_result: target"
574 " <%d> sending msg \"%s\""
575 " (matched \"%s\")\n",
576 i, ( msg ? msg : "" ),
577 ( match ? match : "" ) );
580 * FIXME: need to rewrite "match" (need rwinfo)
582 switch ( rs->sr_err ) {
592 ber_memfree( rmatch );
600 /* better test the pointers before freeing? */
613 rs->sr_matched = rmatch;
614 send_ldap_result( op, rs );
619 ber_memfree( rmatch );
622 rs->sr_matched = NULL;
624 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );