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