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