]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
minor cleanup
[openldap] / servers / slapd / back-meta / bind.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/errno.h>
28 #include <ac/socket.h>
29 #include <ac/string.h>
30
31
32 #define AVL_INTERNAL
33 #include "slap.h"
34 #include "../back-ldap/back-ldap.h"
35 #include "back-meta.h"
36
37 static LDAP_REBIND_PROC meta_back_default_rebind;
38
39 /*
40  * a module could register a replacement for this function
41  */
42 LDAP_REBIND_PROC        *meta_back_rebind_f = meta_back_default_rebind;
43
44 static int
45 meta_back_single_bind(
46         Operation               *op,
47         SlapReply               *rs,
48         metaconn_t              *mc,
49         int                     candidate,
50         int                     massage );
51
52 int
53 meta_back_bind( Operation *op, SlapReply *rs )
54 {
55         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
56         metaconn_t      *mc = NULL;
57
58         int             rc = LDAP_OTHER,
59                         i,
60                         gotit = 0,
61                         isroot = 0;
62
63         SlapReply       *candidates = meta_back_candidates_get( op );
64
65         rs->sr_err = LDAP_SUCCESS;
66
67         Debug( LDAP_DEBUG_ARGS, "%s meta_back_bind: dn=\"%s\".\n",
68                 op->o_log_prefix, op->o_req_dn.bv_val, 0 );
69
70         /* the test on the bind method should be superfluous */
71         if ( op->orb_method == LDAP_AUTH_SIMPLE
72                 && be_isroot_dn( op->o_bd, &op->o_req_ndn ) )
73         {
74                 if ( !be_isroot_pw( op ) ) {
75                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
76                         rs->sr_text = NULL;
77                         send_ldap_result( op, rs );
78                         return rs->sr_err;
79                 }
80
81                 if ( META_BACK_DEFER_ROOTDN_BIND( mi ) ) {
82                         rs->sr_err = LDAP_SUCCESS;
83                         rs->sr_text = NULL;
84                         /* frontend will return success */
85                         return rs->sr_err;
86                 }
87
88                 isroot = 1;
89         }
90
91         /* we need meta_back_getconn() not send result even on error,
92          * because we want to intercept the error and make it
93          * invalidCredentials */
94         mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_DONTSEND );
95         if ( !mc ) {
96                 char    buf[ SLAP_TEXT_BUFLEN ];
97
98                 snprintf( buf, sizeof( buf ),
99                         "meta_back_bind: no target "
100                         "for dn \"%s\" (%d%s%s).",
101                         op->o_req_dn.bv_val, rs->sr_err,
102                         rs->sr_text ? ". " : "",
103                         rs->sr_text ? rs->sr_text : "" );
104                 Debug( LDAP_DEBUG_ANY,
105                         "%s %s\n",
106                         op->o_log_prefix, buf, 0 );
107
108                 /* FIXME: there might be cases where we don't want
109                  * to map the error onto invalidCredentials */
110                 switch ( rs->sr_err ) {
111                 case LDAP_NO_SUCH_OBJECT:
112                 case LDAP_UNWILLING_TO_PERFORM:
113                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
114                         rs->sr_text = NULL;
115                         break;
116                 }
117                 send_ldap_result( op, rs );
118                 return rs->sr_err;
119         }
120
121         /*
122          * Each target is scanned ...
123          */
124         mc->mc_authz_target = META_BOUND_NONE;
125         for ( i = 0; i < mi->mi_ntargets; i++ ) {
126                 int             lerr;
127                 Operation       op2 = *op;
128                 int             massage = 1;
129
130                 /*
131                  * Skip non-candidates
132                  */
133                 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
134                         continue;
135                 }
136
137                 if ( gotit == 0 ) {
138                         /* set rc to LDAP_SUCCESS only if at least
139                          * one candidate has been tried */
140                         rc = LDAP_SUCCESS;
141                         gotit = 1;
142
143                 } else if ( isroot == 0 ) {
144                         /*
145                          * A bind operation is expected to have
146                          * ONE CANDIDATE ONLY!
147                          */
148                         Debug( LDAP_DEBUG_ANY,
149                                 "### %s meta_back_bind: more than one"
150                                 " candidate selected...\n",
151                                 op->o_log_prefix, 0, 0 );
152                 }
153
154                 if ( isroot ) {
155                         if ( BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
156                         {
157                                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
158
159                                 /* skip the target if no pseudorootdn is provided */
160                                 if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
161                                         ch_free( msc->msc_bound_ndn.bv_val );
162                                         BER_BVZERO( &msc->msc_bound_ndn );
163                                 }
164
165                                 if ( LDAP_BACK_SAVECRED( mi ) &&
166                                         !BER_BVISNULL( &msc->msc_cred ) )
167                                 {
168                                         /* destroy sensitive data */
169                                         memset( msc->msc_cred.bv_val, 0,
170                                                 msc->msc_cred.bv_len );
171                                         ch_free( msc->msc_cred.bv_val );
172                                         BER_BVZERO( &msc->msc_cred );
173                                 }
174
175                                 continue;
176                         }
177
178                         op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
179                         op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
180                         op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
181                         op2.orb_method = LDAP_AUTH_SIMPLE;
182
183                         massage = 0;
184                 }
185                 
186                 lerr = meta_back_single_bind( &op2, rs, mc, i, massage );
187
188                 if ( lerr != LDAP_SUCCESS ) {
189                         rc = rs->sr_err = lerr;
190                         candidates[ i ].sr_tag = META_NOT_CANDIDATE;
191                         break;
192                 }
193         }
194
195         /* must re-insert if local DN changed as result of bind */
196         if ( rc == LDAP_SUCCESS ) {
197                 if ( isroot ) {
198                         mc->mc_authz_target = META_BOUND_ALL;
199                         ber_dupbv( &op->orb_edn, be_root_dn( op->o_bd ) );
200                 }
201
202                 if ( !dn_match( &op->o_req_ndn, &mc->mc_local_ndn ) ) {
203                         int             lerr;
204
205                         /* wait for all other ops to release the connection */
206 retry_lock:;
207                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
208                         if ( mc->mc_refcnt > 1 ) {
209                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
210                                 ldap_pvt_thread_yield();
211                                 goto retry_lock;
212                         }
213
214                         assert( mc->mc_refcnt == 1 );
215                         mc = avl_delete( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
216                                 meta_back_conn_cmp );
217                         assert( mc != NULL );
218
219                         ber_bvreplace( &mc->mc_local_ndn, &op->o_req_ndn );
220                         lerr = avl_insert( &mi->mi_conninfo.lai_tree, (caddr_t)mc,
221                                 meta_back_conn_cmp, meta_back_conn_dup );
222                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
223                         if ( lerr == -1 ) {
224                                 for ( i = 0; i < mi->mi_ntargets; ++i ) {
225                                         if ( mc->mc_conns[ i ].msc_ld != NULL ) {
226                                                 meta_clear_one_candidate( &mc->mc_conns[ i ] );
227                                         }
228                                 }
229
230                                 /* we can do this because mc_refcnt == 1 */
231                                 mc->mc_refcnt = 0;
232                                 meta_back_conn_free( mc );
233                                 mc = NULL;
234                         }
235                 }
236         }
237
238         if ( mc != NULL ) {
239                 meta_back_release_conn( op, mc );
240         }
241
242         /*
243          * rc is LDAP_SUCCESS if at least one bind succeeded,
244          * err is the last error that occurred during a bind;
245          * if at least (and at most?) one bind succeeds, fine.
246          */
247         if ( rc != LDAP_SUCCESS ) {
248                 
249                 /*
250                  * deal with bind failure ...
251                  */
252
253                 /*
254                  * no target was found within the naming context, 
255                  * so bind must fail with invalid credentials
256                  */
257                 if ( rs->sr_err == LDAP_SUCCESS && gotit == 0 ) {
258                         rs->sr_err = LDAP_INVALID_CREDENTIALS;
259                 } else {
260                         rs->sr_err = slap_map_api2result( rs );
261                 }
262                 send_ldap_result( op, rs );
263                 return rs->sr_err;
264
265         }
266
267         return LDAP_SUCCESS;
268 }
269
270 /*
271  * meta_back_single_bind
272  *
273  * attempts to perform a bind with creds
274  */
275 static int
276 meta_back_single_bind(
277         Operation               *op,
278         SlapReply               *rs,
279         metaconn_t              *mc,
280         int                     candidate,
281         int                     massage )
282 {
283         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
284         metatarget_t            *mt = &mi->mi_targets[ candidate ];
285         struct berval           mdn = BER_BVNULL;
286         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
287         int                     msgid,
288                                 rebinding = 0;
289
290         
291         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
292                 ch_free( msc->msc_bound_ndn.bv_val );
293                 BER_BVZERO( &msc->msc_bound_ndn );
294         }
295
296         if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
297                 /* destroy sensitive data */
298                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
299                 ch_free( msc->msc_cred.bv_val );
300                 BER_BVZERO( &msc->msc_cred );
301         }
302
303         /*
304          * Rewrite the bind dn if needed
305          */
306         if ( massage ) {
307                 dncookie                dc;
308
309                 dc.target = mt;
310                 dc.conn = op->o_conn;
311                 dc.rs = rs;
312                 dc.ctx = "bindDN";
313
314                 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
315                         send_ldap_result( op, rs );
316                         return -1;
317                 }
318
319         } else {
320                 mdn = op->o_req_dn;
321         }
322
323         /* FIXME: this fixes the bind problem right now; we need
324          * to use the asynchronous version to get the "matched"
325          * and more in case of failure ... */
326         /* FIXME: should we check if at least some of the op->o_ctrls
327          * can/should be passed? */
328 rebind:;
329         rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
330                         LDAP_SASL_SIMPLE, &op->orb_cred,
331                         op->o_ctrls, NULL, &msgid );
332         if ( rs->sr_err == LDAP_SUCCESS ) {
333                 LDAPMessage     *res;
334                 struct timeval  tv;
335                 int             rc;
336                 int             nretries = mt->mt_nretries;
337                 char            buf[ SLAP_TEXT_BUFLEN ];
338
339                 LDAP_BACK_TV_SET( &tv );
340
341                 /*
342                  * handle response!!!
343                  */
344 retry:;
345                 tv.tv_sec = 0;
346                 tv.tv_usec = META_BIND_TIMEOUT;
347                 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
348                 case 0:
349                         snprintf( buf, sizeof( buf ),
350                                 "ldap_result=0 nretries=%d%s\n",
351                                 nretries, rebinding ? " rebinding" : "" );
352                         Debug( LDAP_DEBUG_ANY,
353                                 "%s meta_back_single_bind[%d]: %s.\n",
354                                 op->o_log_prefix, candidate, buf );
355
356                         if ( nretries != META_RETRY_NEVER ) {
357                                 ldap_pvt_thread_yield();
358                                 if ( nretries > 0 ) {
359                                         nretries--;
360                                 }
361                                 LDAP_BACK_TV_SET( &tv );
362                                 goto retry;
363                         }
364                         rs->sr_err = LDAP_BUSY;
365                         if ( rebinding ) {
366                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
367                                 break;
368                         }
369
370                         /* FIXME: some times the request times out
371                          * while the other party is not willing to
372                          * send a response any more.  Give it a second
373                          * chance with a freshly bound connection */
374                         rebinding = 1;
375                         nretries = mt->mt_nretries;
376                         /* fallthru */
377
378                 case -1:
379                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
380                                         &rs->sr_err );
381
382                         if ( rebinding ) {
383                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
384                         }
385
386                         snprintf( buf, sizeof( buf ),
387                                 "err=%d nretries=%d",
388                                 rs->sr_err, nretries );
389                         Debug( LDAP_DEBUG_ANY,
390                                 "### %s meta_back_single_bind[%d]: %s.\n",
391                                 op->o_log_prefix, candidate, buf );
392
393                         rc = slap_map_api2result( rs );
394                         if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
395                                 rc = meta_back_retry( op, rs, mc, candidate, LDAP_BACK_DONTSEND );
396                                 if ( rc ) {
397                                         if ( nretries > 0 ) {
398                                                 nretries--;
399                                         }
400                                         ldap_pvt_thread_yield();
401                                         goto rebind;
402                                 }
403                         }
404                         break;
405
406                 default:
407                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
408                                         NULL, NULL, NULL, NULL, 1 );
409                         if ( rc != LDAP_SUCCESS ) {
410                                 rs->sr_err = rc;
411                         }
412                         break;
413                 }
414         }
415
416         if ( rs->sr_err != LDAP_SUCCESS ) {
417                 rs->sr_err = slap_map_api2result( rs );
418                 goto return_results;
419         }
420
421         ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
422         LDAP_BACK_CONN_ISBOUND_SET( msc );
423         mc->mc_authz_target = candidate;
424
425         if ( LDAP_BACK_SAVECRED( mi ) ) {
426                 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
427                 ldap_set_rebind_proc( msc->msc_ld, meta_back_rebind_f, msc );
428         }
429
430         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
431                         && op->o_req_ndn.bv_len != 0 )
432         {
433                 ( void )meta_dncache_update_entry( &mi->mi_cache,
434                                 &op->o_req_ndn, candidate );
435         }
436
437 return_results:;
438         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
439                 free( mdn.bv_val );
440         }
441
442         return rs->sr_err;
443 }
444
445 /*
446  * meta_back_single_dobind
447  */
448 int
449 meta_back_single_dobind(
450         Operation               *op,
451         SlapReply               *rs,
452         metaconn_t              *mc,
453         int                     candidate,
454         ldap_back_send_t        sendok,
455         int                     nretries,
456         int                     dolock )
457 {
458         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
459         metatarget_t            *mt = &mi->mi_targets[ candidate ];
460         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
461         int                     rc;
462         static struct berval    cred = BER_BVC( "" );
463         int                     msgid,
464                                 rebinding = 0,
465                                 save_nretries = nretries;
466
467         assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
468
469         /*
470          * Otherwise an anonymous bind is performed
471          * (note: if the target was already bound, the anonymous
472          * bind clears the previous bind).
473          */
474         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
475                 ber_memfree( msc->msc_bound_ndn.bv_val );
476                 BER_BVZERO( &msc->msc_bound_ndn );
477         }
478                 
479         if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
480                 /* destroy sensitive data */
481                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
482                 ber_memfree( msc->msc_cred.bv_val );
483                 BER_BVZERO( &msc->msc_cred );
484         }
485
486         /* FIXME: should we check if at least some of the op->o_ctrls
487          * can/should be passed? */
488 rebind:;
489         rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
490                         NULL, NULL, &msgid );
491         if ( rc == LDAP_SUCCESS ) {
492                 LDAPMessage     *res;
493                 struct timeval  tv;
494                 char            buf[ SLAP_TEXT_BUFLEN ];
495
496                 LDAP_BACK_TV_SET( &tv );
497
498                 /*
499                  * handle response!!!
500                  */
501 retry:;
502                 tv.tv_sec = 0;
503                 tv.tv_usec = META_BIND_TIMEOUT;
504                 switch ( ldap_result( msc->msc_ld, msgid, 0, &tv, &res ) ) {
505                 case 0:
506                         snprintf( buf, sizeof( buf ),
507                                 "ldap_result=0 nretries=%d%s",
508                                 nretries, rebinding ? " rebinding" : "" );
509                         Debug( LDAP_DEBUG_ANY,
510                                 "%s meta_back_single_dobind[%d]: %s.\n",
511                                 op->o_log_prefix, candidate, buf );
512
513                         if ( nretries != META_RETRY_NEVER ) {
514                                 ldap_pvt_thread_yield();
515                                 if ( nretries > 0 ) {
516                                         nretries--;
517                                 }
518                                 LDAP_BACK_TV_SET( &tv );
519                                 goto retry;
520                         }
521
522                         rc = LDAP_BUSY;
523                         if ( rebinding ) {
524                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
525                                 break;
526                         }
527
528                         /* FIXME: some times the request times out
529                          * while the other party is not willing to
530                          * send a response any more.  Give it a second
531                          * chance with a freshly bound connection */
532                         rebinding = 1;
533                         nretries = save_nretries;
534                         /* fallthru */
535
536                 case -1:
537                         ldap_get_option( msc->msc_ld,
538                                         LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
539
540                         if ( rebinding ) {
541                                 ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
542                         }
543
544                         snprintf( buf, sizeof( buf ),
545                                 "err=%d nretries=%d",
546                                 rs->sr_err, nretries );
547                         Debug( LDAP_DEBUG_ANY,
548                                 "### %s meta_back_single_dobind[%d]: %s.\n",
549                                 op->o_log_prefix, candidate, buf );
550
551                         rc = slap_map_api2result( rs );
552                         if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
553                                 /* NOTE: we do not use meta_back_retry() here
554                                  * to avoid circular loops; mc_mutex is set
555                                  * by the caller */
556                                 if ( dolock ) {
557                                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
558                                 }
559
560                                 if ( mc->mc_refcnt == 1 ) {
561                                         meta_clear_one_candidate( msc );
562                                         LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
563
564                                         ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
565
566                                         /* mc here must be the regular mc,
567                                          * reset and ready for init */
568                                         rc = meta_back_init_one_conn( op, rs,
569                                                 mt, mc, candidate,
570                                                 LDAP_BACK_CONN_ISPRIV( mc ),
571                                                 LDAP_BACK_DONTSEND );
572
573                                 } else {
574                                         /* can't do anything about it */
575                                         rc = LDAP_UNAVAILABLE;
576                                 }
577
578                                 if ( dolock ) {
579                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
580                                 }
581
582                                 if ( rc == LDAP_SUCCESS ) {
583                                         ldap_pvt_thread_yield();
584                                         if ( nretries > 0 ) {
585                                                 nretries--;
586                                         }
587                                         goto rebind;
588                                 }
589                         }
590                         break;
591
592                 default:
593                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
594                                         NULL, NULL, NULL, NULL, 1 );
595                         if ( rc == LDAP_SUCCESS ) {
596                                 rc = slap_map_api2result( rs );
597                         }
598                         break;
599                 }
600
601         } else {
602                 rs->sr_err = rc;
603                 rc = slap_map_api2result( rs );
604         }
605
606         rs->sr_err = rc;
607         if ( rc != LDAP_SUCCESS && ( sendok & LDAP_BACK_SENDERR ) ) {
608                 send_ldap_result( op, rs );
609         }
610
611         return rc;
612 }
613
614 /*
615  * meta_back_dobind
616  */
617 int
618 meta_back_dobind(
619         Operation               *op,
620         SlapReply               *rs,
621         metaconn_t              *mc,
622         ldap_back_send_t        sendok )
623 {
624         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
625
626         int                     bound = 0,
627                                 i,
628                                 isroot = 0;
629
630         SlapReply               *candidates = meta_back_candidates_get( op );
631
632         if ( be_isroot( op ) ) {
633                 isroot = 1;
634         }
635
636         Debug( LDAP_DEBUG_TRACE,
637                 "%s meta_back_dobind: conn=%ld%s\n",
638                 op->o_log_prefix,
639                 LDAP_BACK_PCONN_ID( mc->mc_conn ),
640                 isroot ? " (isroot)" : "" );
641
642         ldap_pvt_thread_mutex_lock( &mc->mc_mutex );
643
644         /*
645          * all the targets are bound as pseudoroot
646          */
647         if ( mc->mc_authz_target == META_BOUND_ALL ) {
648                 bound = 1;
649                 goto done;
650         }
651
652         for ( i = 0; i < mi->mi_ntargets; i++ ) {
653                 metatarget_t            *mt = &mi->mi_targets[ i ];
654                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
655                 int                     rc;
656                 char                    *rootdn = NULL;
657
658                 /*
659                  * Not a candidate
660                  */
661                 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
662                         continue;
663                 }
664
665                 assert( msc->msc_ld != NULL );
666
667                 /*
668                  * If the target is already bound it is skipped
669                  */
670                 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
671                         ++bound;
672                         continue;
673                 }
674
675                 if ( isroot && !BER_BVISNULL( &mi->mi_targets[ i ].mt_pseudorootdn ) )
676                 {
677                         Operation       op2 = *op;
678
679                         op2.o_tag = LDAP_REQ_BIND;
680                         op2.o_req_dn = mi->mi_targets[ i ].mt_pseudorootdn;
681                         op2.o_req_ndn = mi->mi_targets[ i ].mt_pseudorootdn;
682                         op2.orb_cred = mi->mi_targets[ i ].mt_pseudorootpw;
683                         op2.orb_method = LDAP_AUTH_SIMPLE;
684
685                         rootdn = mi->mi_targets[ i ].mt_pseudorootdn.bv_val;
686
687                         rc = meta_back_single_bind( &op2, rs, mc, i, 0 );
688
689                 } else {
690                         rc = meta_back_single_dobind( op, rs, mc, i,
691                                 LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
692                 }
693
694                 if ( rc != LDAP_SUCCESS ) {
695                         char            buf[ SLAP_TEXT_BUFLEN ];
696
697                         snprintf( buf, sizeof( buf ),
698                                 "meta_back_dobind[%d]: (%s) err=%d.",
699                                 i, rootdn ? rootdn : "anonymous", rc );
700                         Debug( LDAP_DEBUG_ANY,
701                                 "%s %s\n",
702                                 op->o_log_prefix, buf, 0 );
703
704                         /*
705                          * null cred bind should always succeed
706                          * as anonymous, so a failure means
707                          * the target is no longer candidate possibly
708                          * due to technical reasons (remote host down?)
709                          * so better clear the handle
710                          */
711                         /* leave the target candidate, but record the error for later use */
712                         candidates[ i ].sr_err = rc;
713                         if ( META_BACK_ONERR_STOP( mi ) ) {
714                                 bound = 0;
715                                 goto done;
716                         }
717                         continue;
718                 } /* else */
719                 
720                 Debug( LDAP_DEBUG_TRACE,
721                         "%s meta_back_dobind[%d]: "
722                         "(%s)\n",
723                         op->o_log_prefix, i,
724                         rootdn ? rootdn : "anonymous" );
725
726                 if ( rootdn ) {
727                         LDAP_BACK_CONN_ISBOUND_SET( msc );
728                 } else {
729                         LDAP_BACK_CONN_ISANON_SET( msc );
730                 }
731                 ++bound;
732         }
733
734 done:;
735         ldap_pvt_thread_mutex_unlock( &mc->mc_mutex );
736
737         Debug( LDAP_DEBUG_TRACE,
738                 "%s meta_back_dobind: conn=%ld bound=%d\n",
739                 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
740
741         if ( bound == 0 ) {
742                 meta_back_release_conn( op, mc );
743
744                 if ( sendok & LDAP_BACK_SENDERR ) {
745                         if ( rs->sr_err == LDAP_SUCCESS ) {
746                                 rs->sr_err = LDAP_BUSY;
747                         }
748                         send_ldap_result( op, rs );
749                 }
750         }
751
752         return( bound > 0 );
753 }
754
755 /*
756  * meta_back_default_rebind
757  *
758  * This is a callback used for chasing referrals using the same
759  * credentials as the original user on this session.
760  */
761 static int 
762 meta_back_default_rebind(
763         LDAP                    *ld,
764         LDAP_CONST char         *url,
765         ber_tag_t               request,
766         ber_int_t               msgid,
767         void                    *params )
768 {
769         metasingleconn_t        *msc = ( metasingleconn_t * )params;
770
771         return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
772                         LDAP_SASL_SIMPLE, &msc->msc_cred,
773                         NULL, NULL, NULL );
774 }
775
776 /*
777  * FIXME: error return must be handled in a cleaner way ...
778  */
779 int
780 meta_back_op_result(
781         metaconn_t      *mc,
782         Operation       *op,
783         SlapReply       *rs,
784         int             candidate )
785 {
786         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
787
788         int                     i,
789                                 rerr = LDAP_SUCCESS;
790         char                    *rmsg = NULL,
791                                 *rmatch = NULL;
792         const char              *save_rmsg = NULL,
793                                 *save_rmatch = NULL;
794         void                    *rmatch_ctx = NULL;
795
796         if ( candidate != META_TARGET_NONE ) {
797                 metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
798
799                 rs->sr_err = LDAP_SUCCESS;
800
801                 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
802                 if ( rs->sr_err != LDAP_SUCCESS ) {
803                         /*
804                          * better check the type of error. In some cases
805                          * (search ?) it might be better to return a
806                          * success if at least one of the targets gave
807                          * positive result ...
808                          */
809                         ldap_get_option( msc->msc_ld,
810                                         LDAP_OPT_ERROR_STRING, &rmsg );
811                         if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
812                                 ldap_memfree( rmsg );
813                                 rmsg = NULL;
814                         }
815
816                         ldap_get_option( msc->msc_ld,
817                                         LDAP_OPT_MATCHED_DN, &rmatch );
818                         if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
819                                 ldap_memfree( rmatch );
820                                 rmatch = NULL;
821                         }
822
823                         rerr = rs->sr_err = slap_map_api2result( rs );
824
825                         Debug(LDAP_DEBUG_ANY,
826                                         "==> meta_back_op_result: target"
827                                         " <%d> sending msg \"%s\""
828                                         " (matched \"%s\")\n", 
829                                         candidate, ( rmsg ? rmsg : "" ),
830                                         ( rmatch ? rmatch : "" ) );
831                 }
832
833         } else {
834                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
835                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
836                         char                    *msg = NULL;
837                         char                    *match = NULL;
838
839                         rs->sr_err = LDAP_SUCCESS;
840
841                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
842                         if ( rs->sr_err != LDAP_SUCCESS ) {
843                                 /*
844                                  * better check the type of error. In some cases
845                                  * (search ?) it might be better to return a
846                                  * success if at least one of the targets gave
847                                  * positive result ...
848                                  */
849                                 ldap_get_option( msc->msc_ld,
850                                                 LDAP_OPT_ERROR_STRING, &msg );
851                                 if ( msg != NULL && msg[ 0 ] == '\0' ) {
852                                         ldap_memfree( msg );
853                                         msg = NULL;
854                                 }
855
856                                 ldap_get_option( msc->msc_ld,
857                                                 LDAP_OPT_MATCHED_DN, &match );
858                                 if ( match != NULL && match[ 0 ] == '\0' ) {
859                                         ldap_memfree( match );
860                                         match = NULL;
861                                 }
862
863                                 rs->sr_err = slap_map_api2result( rs );
864         
865                                 Debug(LDAP_DEBUG_ANY,
866                                                 "==> meta_back_op_result: target"
867                                                 " <%d> sending msg \"%s\""
868                                                 " (matched \"%s\")\n", 
869                                                 i, ( msg ? msg : "" ),
870                                                 ( match ? match : "" ) );
871         
872                                 /*
873                                  * FIXME: need to rewrite "match" (need rwinfo)
874                                  */
875                                 switch ( rs->sr_err ) {
876                                 default:
877                                         rerr = rs->sr_err;
878                                         if ( msg != NULL ) {
879                                                 if ( rmsg ) {
880                                                         ldap_memfree( rmsg );
881                                                 }
882                                                 rmsg = msg;
883                                                 msg = NULL;
884                                         }
885                                         if ( match != NULL ) {
886                                                 if ( rmatch ) {
887                                                         ldap_memfree( rmatch );
888                                                 }
889                                                 rmatch = match;
890                                                 match = NULL;
891                                         }
892                                         break;
893                                 }
894
895                                 if ( msg ) {
896                                         ldap_memfree( msg );
897                                 }
898         
899                                 if ( match ) {
900                                         ldap_memfree( match );
901                                 }
902                         }
903                 }
904         }
905         
906         rs->sr_err = rerr;
907         if ( rmsg != NULL ) {
908                 save_rmsg = rs->sr_text;
909                 rs->sr_text = rmsg;
910         }
911         if ( rmatch != NULL ) {
912                 struct berval   dn, pdn;
913
914                 ber_str2bv( rmatch, 0, 0, &dn );
915                 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
916                         ldap_memfree( rmatch );
917                         rmatch_ctx = op->o_tmpmemctx;
918                         rmatch = pdn.bv_val;
919                 }
920                 save_rmatch = rs->sr_matched;
921                 rs->sr_matched = rmatch;
922         }
923         send_ldap_result( op, rs );
924         if ( rmsg != NULL ) {
925                 ber_memfree( rmsg );
926                 rs->sr_text = save_rmsg;
927         }
928         if ( rmatch != NULL ) {
929                 ber_memfree_x( rmatch, rmatch_ctx );
930                 rs->sr_matched = save_rmatch;
931         }
932
933         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
934 }
935