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 ];
188 * Rewrite the bind dn if needed
190 dc.rwmap = &mi->mi_targets[ candidate ]->mt_rwmap;
191 dc.conn = op->o_conn;
195 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
196 send_ldap_result( op, rs );
200 /* FIXME: this fixes the bind problem right now; we need
201 * to use the asynchronous version to get the "matched"
202 * and more in case of failure ... */
203 /* FIXME: should be check if at least some of the op->o_ctrls
204 * can/should be passed? */
206 rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
207 LDAP_SASL_SIMPLE, &op->orb_cred,
208 op->o_ctrls, NULL, &msgid );
209 if ( rs->sr_err == LDAP_SUCCESS ) {
213 int nretries = mt->mt_nretries;
220 tv.tv_usec = META_BIND_TIMEOUT;
221 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
223 Debug( LDAP_DEBUG_ANY, "%s meta_back_single_bind: ldap_result=%d nretries=%d\n",
224 op->o_log_prefix, 0, nretries );
226 if ( nretries != META_RETRY_NEVER ) {
227 ldap_pvt_thread_yield();
228 if ( nretries > 0 ) {
233 rs->sr_err = LDAP_BUSY;
235 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
239 /* FIXME: some times the request times out
240 * while the other party is not willing to
241 * send a response any more. Give it a second
242 * chance with a freshly bound connection */
244 nretries = mt->mt_nretries;
248 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
252 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
255 Debug( LDAP_DEBUG_ANY, "### %s meta_back_single_bind: err=%d nretries=%d\n",
256 op->o_log_prefix, rs->sr_err, nretries );
258 rc = slap_map_api2result( rs );
259 if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
260 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
264 /* mc here must be the regular mc, reset and ready for init */
265 rc = meta_back_init_one_conn( op, rs, mt, msc,
266 LDAP_BACK_DONTSEND );
268 if ( nretries > 0 ) {
271 ldap_pvt_thread_yield();
278 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
279 NULL, NULL, NULL, NULL, 1 );
280 if ( rc != LDAP_SUCCESS ) {
287 if ( rs->sr_err != LDAP_SUCCESS ) {
288 rs->sr_err = slap_map_api2result( rs );
292 ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
293 msc->msc_bound = META_BOUND;
294 mc->mc_auth_target = candidate;
296 if ( LDAP_BACK_SAVECRED( mi ) ) {
297 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
298 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind, msc );
301 if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
302 && op->o_req_ndn.bv_len != 0 )
304 ( void )meta_dncache_update_entry( &mi->mi_cache,
305 &op->o_req_ndn, candidate );
309 if ( mdn.bv_val != op->o_req_dn.bv_val ) {
317 * meta_back_single_dobind
320 meta_back_single_dobind(
325 ldap_back_send_t sendok,
328 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
329 metatarget_t *mt = mi->mi_targets[ candidate ];
330 metasingleconn_t *msc = &mc->mc_conns[ candidate ];
332 struct berval cred = BER_BVC( "" );
335 save_nretries = nretries;
338 * Otherwise an anonymous bind is performed
339 * (note: if the target was already bound, the anonymous
340 * bind clears the previous bind).
342 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
343 ber_memfree( msc->msc_bound_ndn.bv_val );
344 BER_BVZERO( &msc->msc_bound_ndn );
347 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
348 /* destroy sensitive data */
349 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
350 ber_memfree( msc->msc_cred.bv_val );
351 BER_BVZERO( &msc->msc_cred );
354 /* FIXME: should we check if at least some of the op->o_ctrls
355 * can/should be passed? */
357 rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
358 NULL, NULL, &msgid );
359 if ( rc == LDAP_SUCCESS ) {
368 tv.tv_usec = META_BIND_TIMEOUT;
369 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
371 Debug( LDAP_DEBUG_ANY, "%s meta_back_single_dobind: ldap_result=%d nretries=%d\n",
372 op->o_log_prefix, 0, nretries );
374 if ( nretries != META_RETRY_NEVER ) {
375 ldap_pvt_thread_yield();
376 if ( nretries > 0 ) {
384 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
388 /* FIXME: some times the request times out
389 * while the other party is not willing to
390 * send a response any more. Give it a second
391 * chance with a freshly bound connection */
393 nretries = save_nretries;
397 ldap_get_option( msc->msc_ld,
398 LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
401 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
404 Debug( LDAP_DEBUG_ANY, "### %s meta_back_single_dobind: err=%d nretries=%d\n",
405 op->o_log_prefix, rs->sr_err, nretries );
407 rc = slap_map_api2result( rs );
408 if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
409 ldap_unbind_ext_s( msc->msc_ld, NULL, NULL );
413 /* mc here must be the regular mc, reset and ready for init */
414 rc = meta_back_init_one_conn( op, rs, mt, msc, LDAP_BACK_DONTSEND );
416 if ( rc == LDAP_SUCCESS ) {
417 ldap_pvt_thread_yield();
418 if ( nretries > 0 ) {
427 rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
428 NULL, NULL, NULL, NULL, 1 );
429 if ( rc == LDAP_SUCCESS ) {
430 rc = slap_map_api2result( rs );
437 if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
438 send_ldap_result( op, rs );
452 ldap_back_send_t sendok )
454 metainfo_t *mi = ( metainfo_t * )op->o_bd->be_private;
456 metasingleconn_t *msc;
459 SlapReply *candidates = meta_back_candidates_get( op );
461 ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
464 * all the targets are bound as pseudoroot
466 if ( mc->mc_auth_target == META_BOUND_ALL ) {
471 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
472 metatarget_t *mt = mi->mi_targets[ i ];
476 * Not a candidate or something wrong with this target ...
478 if ( msc->msc_ld == NULL ) {
483 * If the target is already bound it is skipped
485 if ( msc->msc_bound == META_BOUND && mc->mc_auth_target == i ) {
490 rc = meta_back_single_dobind( op, rs, mc, i,
491 LDAP_BACK_DONTSEND, mt->mt_nretries );
492 if ( rc != LDAP_SUCCESS ) {
493 rs->sr_err = slap_map_api2result( rs );
495 Debug( LDAP_DEBUG_ANY, "%s meta_back_dobind[%d]: "
496 "(anonymous) err=%d\n",
497 op->o_log_prefix, i, rc );
500 * null cred bind should always succeed
501 * as anonymous, so a failure means
502 * the target is no longer candidate possibly
503 * due to technical reasons (remote host down?)
504 * so better clear the handle
506 candidates[ i ].sr_tag = META_NOT_CANDIDATE;
508 ( void )meta_clear_one_candidate( msc );
513 candidates[ i ].sr_tag = META_CANDIDATE;
514 msc->msc_bound = META_ANONYMOUS;
519 ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
521 if ( bound == 0 && sendok & LDAP_BACK_SENDERR ) {
522 if ( rs->sr_err == LDAP_SUCCESS ) {
523 rs->sr_err = LDAP_BUSY;
525 send_ldap_result( op, rs );
534 * This is a callback used for chasing referrals using the same
535 * credentials as the original user on this session.
540 LDAP_CONST char *url,
545 metasingleconn_t *msc = ( metasingleconn_t * )params;
547 return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
548 LDAP_SASL_SIMPLE, &msc->msc_cred,
553 * FIXME: error return must be handled in a cleaner way ...
564 metasingleconn_t *msc;
570 if ( candidate != META_TARGET_NONE ) {
571 msc = &mc->mc_conns[ candidate ];
573 rs->sr_err = LDAP_SUCCESS;
575 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
576 if ( rs->sr_err != LDAP_SUCCESS ) {
578 * better check the type of error. In some cases
579 * (search ?) it might be better to return a
580 * success if at least one of the targets gave
581 * positive result ...
583 ldap_get_option( msc->msc_ld,
584 LDAP_OPT_ERROR_STRING, &rmsg );
585 ldap_get_option( msc->msc_ld,
586 LDAP_OPT_MATCHED_DN, &rmatch );
587 rerr = rs->sr_err = slap_map_api2result( rs );
596 Debug(LDAP_DEBUG_ANY,
597 "==> meta_back_op_result: target"
598 " <%d> sending msg \"%s\""
599 " (matched \"%s\")\n",
600 candidate, ( rmsg ? rmsg : "" ),
601 ( rmatch ? rmatch : "" ) );
605 for ( i = 0, msc = &mc->mc_conns[ 0 ]; !META_LAST( msc ); ++i, ++msc ) {
609 rs->sr_err = LDAP_SUCCESS;
611 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
612 if ( rs->sr_err != LDAP_SUCCESS ) {
614 * better check the type of error. In some cases
615 * (search ?) it might be better to return a
616 * success if at least one of the targets gave
617 * positive result ...
619 ldap_get_option( msc->msc_ld,
620 LDAP_OPT_ERROR_STRING, &msg );
621 ldap_get_option( msc->msc_ld,
622 LDAP_OPT_MATCHED_DN, &match );
623 rs->sr_err = slap_map_api2result( rs );
625 Debug(LDAP_DEBUG_ANY,
626 "==> meta_back_op_result: target"
627 " <%d> sending msg \"%s\""
628 " (matched \"%s\")\n",
629 i, ( msg ? msg : "" ),
630 ( match ? match : "" ) );
633 * FIXME: need to rewrite "match" (need rwinfo)
635 switch ( rs->sr_err ) {
645 ber_memfree( rmatch );
653 /* better test the pointers before freeing? */
666 rs->sr_matched = rmatch;
667 send_ldap_result( op, rs );
672 ber_memfree( rmatch );
675 rs->sr_matched = NULL;
677 return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );