]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/bind.c
more about previous commit
[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 ( mc->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 /*
286  * meta_back_single_bind
287  *
288  * attempts to perform a bind with creds
289  */
290 int
291 meta_back_single_bind(
292         Operation               *op,
293         SlapReply               *rs,
294         metaconn_t              *mc,
295         int                     candidate,
296         int                     massage )
297 {
298         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
299         metatarget_t            *mt = mi->mi_targets[ candidate ];
300         struct berval           mdn = BER_BVNULL;
301         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
302         int                     msgid,
303                                 rebinding = 0;
304
305         
306         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
307                 ch_free( msc->msc_bound_ndn.bv_val );
308                 BER_BVZERO( &msc->msc_bound_ndn );
309         }
310
311         if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
312                 /* destroy sensitive data */
313                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
314                 ch_free( msc->msc_cred.bv_val );
315                 BER_BVZERO( &msc->msc_cred );
316         }
317
318         /*
319          * Rewrite the bind dn if needed
320          */
321         if ( massage ) {
322                 dncookie                dc;
323
324                 dc.target = mt;
325                 dc.conn = op->o_conn;
326                 dc.rs = rs;
327                 dc.ctx = "bindDN";
328
329                 if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
330                         rs->sr_text = "DN rewrite error";
331                         rs->sr_err = LDAP_OTHER;
332                         return rs->sr_err;
333                 }
334
335         } else {
336                 mdn = op->o_req_dn;
337         }
338
339         /* FIXME: this fixes the bind problem right now; we need
340          * to use the asynchronous version to get the "matched"
341          * and more in case of failure ... */
342         /* FIXME: should we check if at least some of the op->o_ctrls
343          * can/should be passed? */
344 rebind:;
345         rs->sr_err = ldap_sasl_bind( msc->msc_ld, mdn.bv_val,
346                         LDAP_SASL_SIMPLE, &op->orb_cred,
347                         op->o_ctrls, NULL, &msgid );
348         if ( rs->sr_err == LDAP_SUCCESS ) {
349                 LDAPMessage     *res;
350                 struct timeval  tv;
351                 int             rc;
352                 int             nretries = mt->mt_nretries;
353                 char            buf[ SLAP_TEXT_BUFLEN ];
354
355                 LDAP_BACK_TV_SET( &tv );
356
357                 /*
358                  * handle response!!!
359                  */
360 retry:;
361                 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
362                 case 0:
363                         snprintf( buf, sizeof( buf ),
364                                 "ldap_result=0 nretries=%d%s",
365                                 nretries, rebinding ? " rebinding" : "" );
366                         Debug( LDAP_DEBUG_ANY,
367                                 "%s meta_back_single_bind[%d]: %s.\n",
368                                 op->o_log_prefix, candidate, buf );
369
370                         if ( nretries != META_RETRY_NEVER ) {
371                                 ldap_pvt_thread_yield();
372                                 if ( nretries > 0 ) {
373                                         nretries--;
374                                 }
375                                 tv = mt->mt_bind_timeout;
376                                 goto retry;
377                         }
378
379                         rs->sr_err = LDAP_BUSY;
380                         if ( rebinding ) {
381                                 (void)meta_back_cancel( mc, op, rs, msgid, candidate, LDAP_BACK_DONTSEND );
382                                 break;
383                         }
384
385                         /* FIXME: some times the request times out
386                          * while the other party is not willing to
387                          * send a response any more.  Give it a second
388                          * chance with a freshly bound connection */
389                         rebinding = 1;
390                         nretries = mt->mt_nretries;
391                         /* fallthru */
392
393                 case -1:
394                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
395                                 &rs->sr_err );
396
397                         if ( rebinding ) {
398                                 (void)meta_back_cancel( mc, op, rs, msgid, candidate, LDAP_BACK_DONTSEND );
399                         }
400
401                         snprintf( buf, sizeof( buf ),
402                                 "err=%d (%s) nretries=%d",
403                                 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
404                         Debug( LDAP_DEBUG_ANY,
405                                 "### %s meta_back_single_bind[%d]: %s.\n",
406                                 op->o_log_prefix, candidate, buf );
407
408                         rc = slap_map_api2result( rs );
409                         if ( rs->sr_err == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
410                                 rc = meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_DONTSEND );
411                                 if ( rc ) {
412                                         if ( nretries > 0 ) {
413                                                 nretries--;
414                                         }
415                                         ldap_pvt_thread_yield();
416                                         goto rebind;
417                                 }
418                                 goto return_results;
419                         }
420                         break;
421
422                 default:
423                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
424                                         NULL, NULL, NULL, NULL, 1 );
425                         if ( rc != LDAP_SUCCESS ) {
426                                 rs->sr_err = rc;
427                         }
428                         break;
429                 }
430         }
431
432         if ( rs->sr_err != LDAP_SUCCESS ) {
433                 rs->sr_err = slap_map_api2result( rs );
434                 goto return_results;
435         }
436
437         ber_bvreplace( &msc->msc_bound_ndn, &op->o_req_dn );
438         LDAP_BACK_CONN_ISBOUND_SET( msc );
439         mc->mc_authz_target = candidate;
440
441         if ( LDAP_BACK_SAVECRED( mi ) ) {
442                 ber_bvreplace( &msc->msc_cred, &op->orb_cred );
443                 ldap_set_rebind_proc( msc->msc_ld, mt->mt_rebind_f, msc );
444         }
445
446         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED
447                         && !BER_BVISEMPTY( &op->o_req_ndn ) )
448         {
449                 ( void )meta_dncache_update_entry( &mi->mi_cache,
450                                 &op->o_req_ndn, candidate );
451         }
452
453 return_results:;
454         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
455                 free( mdn.bv_val );
456         }
457
458         if ( META_BACK_TGT_QUARANTINE( mt ) ) {
459                 meta_back_quarantine( op, rs, candidate );
460         }
461
462         return rs->sr_err;
463 }
464
465 /*
466  * meta_back_single_dobind
467  */
468 int
469 meta_back_single_dobind(
470         Operation               *op,
471         SlapReply               *rs,
472         metaconn_t              **mcp,
473         int                     candidate,
474         ldap_back_send_t        sendok,
475         int                     nretries,
476         int                     dolock )
477 {
478         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
479         metatarget_t            *mt = mi->mi_targets[ candidate ];
480         metaconn_t              *mc = *mcp;
481         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
482         int                     rc;
483         static struct berval    cred = BER_BVC( "" );
484         int                     msgid,
485                                 rebinding = 0,
486                                 save_nretries = nretries;
487
488         assert( !LDAP_BACK_CONN_ISBOUND( msc ) );
489
490         /*
491          * meta_back_single_dobind() calls meta_back_single_bind()
492          * if required.
493          */
494         if ( be_isroot( op ) && !BER_BVISNULL( &mt->mt_pseudorootdn ) )
495         {
496                 Operation       op2 = *op;
497
498                 op2.o_tag = LDAP_REQ_BIND;
499                 op2.o_req_dn = mt->mt_pseudorootdn;
500                 op2.o_req_ndn = mt->mt_pseudorootdn;
501                 op2.orb_cred = mt->mt_pseudorootpw;
502                 op2.orb_method = LDAP_AUTH_SIMPLE;
503
504                 rc = meta_back_single_bind( &op2, rs, *mcp, candidate, 0 );
505                 goto done;
506         }
507
508         /*
509          * Otherwise an anonymous bind is performed
510          * (note: if the target was already bound, the anonymous
511          * bind clears the previous bind).
512          */
513         if ( !BER_BVISNULL( &msc->msc_bound_ndn ) ) {
514                 ber_memfree( msc->msc_bound_ndn.bv_val );
515                 BER_BVZERO( &msc->msc_bound_ndn );
516         }
517                 
518         if ( LDAP_BACK_SAVECRED( mi ) && !BER_BVISNULL( &msc->msc_cred ) ) {
519                 /* destroy sensitive data */
520                 memset( msc->msc_cred.bv_val, 0, msc->msc_cred.bv_len );
521                 ber_memfree( msc->msc_cred.bv_val );
522                 BER_BVZERO( &msc->msc_cred );
523         }
524
525         /* FIXME: should we check if at least some of the op->o_ctrls
526          * can/should be passed? */
527 rebind:;
528         rc = ldap_sasl_bind( msc->msc_ld, "", LDAP_SASL_SIMPLE, &cred,
529                         NULL, NULL, &msgid );
530         if ( rc == LDAP_SUCCESS ) {
531                 LDAPMessage     *res;
532                 struct timeval  tv;
533                 char            buf[ SLAP_TEXT_BUFLEN ];
534
535                 LDAP_BACK_TV_SET( &tv );
536
537                 /*
538                  * handle response!!!
539                  */
540 retry:;
541                 switch ( ldap_result( msc->msc_ld, msgid, LDAP_MSG_ALL, &tv, &res ) ) {
542                 case 0:
543                         snprintf( buf, sizeof( buf ),
544                                 "ldap_result=0 nretries=%d%s",
545                                 nretries, rebinding ? " rebinding" : "" );
546                         Debug( LDAP_DEBUG_ANY,
547                                 "%s meta_back_single_dobind[%d]: %s.\n",
548                                 op->o_log_prefix, candidate, buf );
549
550                         if ( nretries != META_RETRY_NEVER ) {
551                                 ldap_pvt_thread_yield();
552                                 if ( nretries > 0 ) {
553                                         nretries--;
554                                 }
555                                 tv = mt->mt_bind_timeout;
556                                 goto retry;
557                         }
558
559                         rc = LDAP_BUSY;
560                         if ( rebinding ) {
561                                 (void)meta_back_cancel( mc, op, rs, msgid, candidate, LDAP_BACK_DONTSEND );
562                                 break;
563                         }
564
565                         /* FIXME: some times the request times out
566                          * while the other party is not willing to
567                          * send a response any more.  Give it a second
568                          * chance with a freshly bound connection */
569                         rebinding = 1;
570                         nretries = save_nretries;
571                         /* fallthru */
572
573                 case -1:
574                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER,
575                                 &rs->sr_err );
576
577                         if ( rebinding ) {
578                                 (void)meta_back_cancel( mc, op, rs, msgid, candidate, LDAP_BACK_DONTSEND );
579                         }
580
581                         snprintf( buf, sizeof( buf ),
582                                 "err=%d (%s) nretries=%d",
583                                 rs->sr_err, ldap_err2string( rs->sr_err ), nretries );
584                         Debug( LDAP_DEBUG_ANY,
585                                 "### %s meta_back_single_dobind[%d]: %s.\n",
586                                 op->o_log_prefix, candidate, buf );
587
588                         rc = slap_map_api2result( rs );
589                         if ( rc == LDAP_UNAVAILABLE && nretries != META_RETRY_NEVER ) {
590                                 if ( dolock ) {
591                                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
592                                 }
593
594                                 if ( mc->mc_refcnt == 1 ) {
595                                         meta_clear_one_candidate( msc );
596                                         LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
597
598                                         ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
599
600                                         /* mc here must be the regular mc,
601                                          * reset and ready for init */
602                                         rc = meta_back_init_one_conn( op, rs,
603                                                 mc, candidate,
604                                                 LDAP_BACK_CONN_ISPRIV( mc ),
605                                                 LDAP_BACK_DONTSEND );
606                                         if ( rc == LDAP_SUCCESS ) {
607                                                 LDAP_BACK_CONN_BINDING_SET( msc );
608                                         }
609
610                                 } else {
611                                         /* can't do anything about it */
612                                         rc = LDAP_UNAVAILABLE;
613                                 }
614
615                                 if ( dolock ) {
616                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
617                                 }
618
619                                 if ( rc == LDAP_SUCCESS ) {
620                                         ldap_pvt_thread_yield();
621                                         if ( nretries > 0 ) {
622                                                 nretries--;
623                                         }
624                                         goto rebind;
625                                 }
626                         }
627                         break;
628
629                 default:
630                         rc = ldap_parse_result( msc->msc_ld, res, &rs->sr_err,
631                                         NULL, NULL, NULL, NULL, 1 );
632                         if ( rc == LDAP_SUCCESS ) {
633                                 rc = slap_map_api2result( rs );
634                         }
635                         break;
636                 }
637
638         } else {
639                 rs->sr_err = rc;
640                 rc = slap_map_api2result( rs );
641         }
642
643 done:;
644         rs->sr_err = rc;
645         if ( rc != LDAP_SUCCESS ) {
646                 if ( dolock ) {
647                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
648                 }
649                 LDAP_BACK_CONN_BINDING_CLEAR( msc );
650                 if ( META_BACK_ONERR_STOP( mi ) ) {
651                         LDAP_BACK_CONN_TAINTED_SET( mc );
652                         meta_back_release_conn_lock( op, mc, 0 );
653                         *mcp = NULL;
654                 }
655                 if ( dolock ) {
656                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
657                 }
658
659                 if ( META_BACK_ONERR_STOP( mi ) && ( sendok & LDAP_BACK_SENDERR ) ) {
660                         send_ldap_result( op, rs );
661                 }
662         }
663
664         if ( META_BACK_TGT_QUARANTINE( mt ) ) {
665                 meta_back_quarantine( op, rs, candidate );
666         }
667
668         return rc;
669 }
670
671 /*
672  * meta_back_dobind
673  */
674 int
675 meta_back_dobind(
676         Operation               *op,
677         SlapReply               *rs,
678         metaconn_t              *mc,
679         ldap_back_send_t        sendok )
680 {
681         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
682
683         int                     bound = 0,
684                                 i,
685                                 isroot = 0;
686
687         SlapReply               *candidates = meta_back_candidates_get( op );
688
689         if ( be_isroot( op ) ) {
690                 isroot = 1;
691         }
692
693         Debug( LDAP_DEBUG_TRACE,
694                 "%s meta_back_dobind: conn=%ld%s\n",
695                 op->o_log_prefix,
696                 LDAP_BACK_PCONN_ID( mc->mc_conn ),
697                 isroot ? " (isroot)" : "" );
698
699         /*
700          * all the targets are bound as pseudoroot
701          */
702         if ( mc->mc_authz_target == META_BOUND_ALL ) {
703                 bound = 1;
704                 goto done;
705         }
706
707         for ( i = 0; i < mi->mi_ntargets; i++ ) {
708                 metatarget_t            *mt = mi->mi_targets[ i ];
709                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
710                 int                     rc, do_retry = 1;
711
712                 /*
713                  * Not a candidate
714                  */
715                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
716                         continue;
717                 }
718
719                 assert( msc->msc_ld != NULL );
720
721                 /*
722                  * If the target is already bound it is skipped
723                  */
724
725 retry_binding:;
726                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
727                 if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
728                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
729                         ++bound;
730                         continue;
731
732                 } else if ( LDAP_BACK_CONN_BINDING( msc ) ) {
733                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
734                         ldap_pvt_thread_yield();
735                         goto retry_binding;
736
737                 }
738
739                 LDAP_BACK_CONN_BINDING_SET( msc );
740                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
741
742 retry:;
743                 rc = meta_back_single_dobind( op, rs, &mc, i,
744                         LDAP_BACK_DONTSEND, mt->mt_nretries, 1 );
745                 /*
746                  * NOTE: meta_back_single_dobind() already retries;
747                  * in case of failure, it resets mc...
748                  */
749                 if ( rc != LDAP_SUCCESS ) {
750                         char            buf[ SLAP_TEXT_BUFLEN ];
751
752                         if ( mc == NULL ) {
753                                 /* meta_back_single_dobind() already sent 
754                                  * response and released connection */
755                                 goto send_err;
756                         }
757
758
759                         if ( rc == LDAP_UNAVAILABLE && do_retry ) {
760                                 do_retry = 0;
761                                 if ( meta_back_retry( op, rs, &mc, i, sendok ) ) {
762                                         goto retry;
763                                 }
764
765                                 if ( mc != NULL ) {
766                                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
767                                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
768                                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
769                                 }
770
771                                 return 0;
772                         }
773
774                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
775                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
776                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
777
778                         snprintf( buf, sizeof( buf ),
779                                 "meta_back_dobind[%d]: (%s) err=%d (%s).",
780                                 i, isroot ? op->o_bd->be_rootdn.bv_val : "anonymous",
781                                 rc, ldap_err2string( rc ) );
782                         Debug( LDAP_DEBUG_ANY,
783                                 "%s %s\n",
784                                 op->o_log_prefix, buf, 0 );
785
786                         /*
787                          * null cred bind should always succeed
788                          * as anonymous, so a failure means
789                          * the target is no longer candidate possibly
790                          * due to technical reasons (remote host down?)
791                          * so better clear the handle
792                          */
793                         /* leave the target candidate, but record the error for later use */
794                         candidates[ i ].sr_err = rc;
795                         if ( META_BACK_ONERR_STOP( mi ) ) {
796                                 bound = 0;
797                                 goto done;
798                         }
799
800                         continue;
801                 } /* else */
802                 
803                 Debug( LDAP_DEBUG_TRACE,
804                         "%s meta_back_dobind[%d]: "
805                         "(%s)\n",
806                         op->o_log_prefix, i,
807                         isroot ? op->o_bd->be_rootdn.bv_val : "anonymous" );
808
809                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
810                 LDAP_BACK_CONN_BINDING_CLEAR( msc );
811                 if ( isroot ) {
812                         LDAP_BACK_CONN_ISBOUND_SET( msc );
813                 } else {
814                         LDAP_BACK_CONN_ISANON_SET( msc );
815                 }
816                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
817                 ++bound;
818         }
819
820 done:;
821         Debug( LDAP_DEBUG_TRACE,
822                 "%s meta_back_dobind: conn=%ld bound=%d\n",
823                 op->o_log_prefix, LDAP_BACK_PCONN_ID( mc->mc_conn ), bound );
824
825         if ( bound == 0 ) {
826                 meta_back_release_conn( op, mc );
827
828 send_err:;
829                 if ( sendok & LDAP_BACK_SENDERR ) {
830                         if ( rs->sr_err == LDAP_SUCCESS ) {
831                                 rs->sr_err = LDAP_BUSY;
832                         }
833                         send_ldap_result( op, rs );
834                 }
835
836                 return 0;
837         }
838
839         return ( bound > 0 );
840 }
841
842 /*
843  * meta_back_default_rebind
844  *
845  * This is a callback used for chasing referrals using the same
846  * credentials as the original user on this session.
847  */
848 int 
849 meta_back_default_rebind(
850         LDAP                    *ld,
851         LDAP_CONST char         *url,
852         ber_tag_t               request,
853         ber_int_t               msgid,
854         void                    *params )
855 {
856         metasingleconn_t        *msc = ( metasingleconn_t * )params;
857
858         return ldap_sasl_bind_s( ld, msc->msc_bound_ndn.bv_val,
859                         LDAP_SASL_SIMPLE, &msc->msc_cred,
860                         NULL, NULL, NULL );
861 }
862
863 /*
864  * meta_back_default_urllist
865  *
866  * This is a callback used for mucking with the urllist
867  */
868 int 
869 meta_back_default_urllist(
870         LDAP            *ld,
871         LDAPURLDesc     **urllist,
872         LDAPURLDesc     **url,
873         void            *params )
874 {
875         metatarget_t    *mt = (metatarget_t *)params;
876         LDAPURLDesc     **urltail;
877
878         if ( urllist == url ) {
879                 return LDAP_SUCCESS;
880         }
881
882         for ( urltail = &(*url)->lud_next; *urltail; urltail = &(*urltail)->lud_next )
883                 /* count */ ;
884
885         *urltail = *urllist;
886         *urllist = *url;
887         *url = NULL;
888
889         ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
890         if ( mt->mt_uri ) {
891                 ch_free( mt->mt_uri );
892         }
893
894         ldap_get_option( ld, LDAP_OPT_URI, (void *)&mt->mt_uri );
895         ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
896
897         return LDAP_SUCCESS;
898 }
899
900 int
901 meta_back_cancel(
902         metaconn_t              *mc,
903         Operation               *op,
904         SlapReply               *rs,
905         ber_int_t               msgid,
906         int                     candidate,
907         ldap_back_send_t        sendok )
908 {
909         metainfo_t              *mi = (metainfo_t *)op->o_bd->be_private;
910
911         metatarget_t            *mt = mi->mi_targets[ candidate ];
912         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
913
914         /* default behavior */
915         if ( META_BACK_TGT_ABANDON( mt ) ) {
916                 return ldap_abandon_ext( msc->msc_ld, msgid, NULL, NULL );
917         }
918
919         if ( META_BACK_TGT_IGNORE( mt ) ) {
920                 return LDAP_SUCCESS;
921         }
922
923         if ( META_BACK_TGT_CANCEL( mt ) ) {
924                 /* FIXME: asynchronous? */
925                 return ldap_cancel_s( msc->msc_ld, msgid, NULL, NULL );
926         }
927
928         assert( 0 );
929
930         return LDAP_OTHER;
931 }
932
933
934
935 /*
936  * FIXME: error return must be handled in a cleaner way ...
937  */
938 int
939 meta_back_op_result(
940         metaconn_t      *mc,
941         Operation       *op,
942         SlapReply       *rs,
943         int             candidate )
944 {
945         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
946
947         int                     i,
948                                 rerr = LDAP_SUCCESS;
949         char                    *rmsg = NULL,
950                                 *rmatch = NULL;
951         const char              *save_rmsg = NULL,
952                                 *save_rmatch = NULL;
953         void                    *rmatch_ctx = NULL;
954
955         assert( mc != NULL );
956
957         if ( candidate != META_TARGET_NONE ) {
958                 metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
959
960                 rs->sr_err = LDAP_SUCCESS;
961
962                 ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
963                 if ( rs->sr_err != LDAP_SUCCESS ) {
964                         /*
965                          * better check the type of error. In some cases
966                          * (search ?) it might be better to return a
967                          * success if at least one of the targets gave
968                          * positive result ...
969                          */
970                         ldap_get_option( msc->msc_ld,
971                                         LDAP_OPT_ERROR_STRING, &rmsg );
972                         if ( rmsg != NULL && rmsg[ 0 ] == '\0' ) {
973                                 ldap_memfree( rmsg );
974                                 rmsg = NULL;
975                         }
976
977                         ldap_get_option( msc->msc_ld,
978                                         LDAP_OPT_MATCHED_DN, &rmatch );
979                         if ( rmatch != NULL && rmatch[ 0 ] == '\0' ) {
980                                 ldap_memfree( rmatch );
981                                 rmatch = NULL;
982                         }
983
984                         rerr = rs->sr_err = slap_map_api2result( rs );
985
986                         Debug(LDAP_DEBUG_ANY,
987                                         "==> meta_back_op_result[%d] "
988                                         "text=\"%s\" matched=\"%s\"\n", 
989                                         candidate, ( rmsg ? rmsg : "" ),
990                                         ( rmatch ? rmatch : "" ) );
991                 }
992
993                 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ candidate ] ) ) {
994                         meta_back_quarantine( op, rs, candidate );
995                 }
996
997         } else {
998                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
999                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
1000                         char                    *msg = NULL;
1001                         char                    *match = NULL;
1002
1003                         rs->sr_err = LDAP_SUCCESS;
1004
1005                         ldap_get_option( msc->msc_ld, LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
1006                         if ( rs->sr_err != LDAP_SUCCESS ) {
1007                                 /*
1008                                  * better check the type of error. In some cases
1009                                  * (search ?) it might be better to return a
1010                                  * success if at least one of the targets gave
1011                                  * positive result ...
1012                                  */
1013                                 ldap_get_option( msc->msc_ld,
1014                                                 LDAP_OPT_ERROR_STRING, &msg );
1015                                 if ( msg != NULL && msg[ 0 ] == '\0' ) {
1016                                         ldap_memfree( msg );
1017                                         msg = NULL;
1018                                 }
1019
1020                                 ldap_get_option( msc->msc_ld,
1021                                                 LDAP_OPT_MATCHED_DN, &match );
1022                                 if ( match != NULL && match[ 0 ] == '\0' ) {
1023                                         ldap_memfree( match );
1024                                         match = NULL;
1025                                 }
1026
1027                                 rs->sr_err = slap_map_api2result( rs );
1028         
1029                                 Debug(LDAP_DEBUG_ANY,
1030                                                 "==> meta_back_op_result[%d] "
1031                                                 "text=\"%s\" matched=\"%s\"\n", 
1032                                                 i, ( msg ? msg : "" ),
1033                                                 ( match ? match : "" ) );
1034         
1035                                 /*
1036                                  * FIXME: need to rewrite "match" (need rwinfo)
1037                                  */
1038                                 switch ( rs->sr_err ) {
1039                                 default:
1040                                         rerr = rs->sr_err;
1041                                         if ( msg != NULL ) {
1042                                                 if ( rmsg ) {
1043                                                         ldap_memfree( rmsg );
1044                                                 }
1045                                                 rmsg = msg;
1046                                                 msg = NULL;
1047                                         }
1048                                         if ( match != NULL ) {
1049                                                 if ( rmatch ) {
1050                                                         ldap_memfree( rmatch );
1051                                                 }
1052                                                 rmatch = match;
1053                                                 match = NULL;
1054                                         }
1055                                         break;
1056                                 }
1057
1058                                 if ( msg ) {
1059                                         ldap_memfree( msg );
1060                                 }
1061         
1062                                 if ( match ) {
1063                                         ldap_memfree( match );
1064                                 }
1065                         }
1066
1067                         if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1068                                 meta_back_quarantine( op, rs, i );
1069                         }
1070                 }
1071         }
1072         
1073         rs->sr_err = rerr;
1074         if ( rmsg != NULL ) {
1075                 save_rmsg = rs->sr_text;
1076                 rs->sr_text = rmsg;
1077         }
1078         if ( rmatch != NULL ) {
1079                 struct berval   dn, pdn;
1080
1081                 ber_str2bv( rmatch, 0, 0, &dn );
1082                 if ( dnPretty( NULL, &dn, &pdn, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
1083                         ldap_memfree( rmatch );
1084                         rmatch_ctx = op->o_tmpmemctx;
1085                         rmatch = pdn.bv_val;
1086                 }
1087                 save_rmatch = rs->sr_matched;
1088                 rs->sr_matched = rmatch;
1089         }
1090         send_ldap_result( op, rs );
1091         if ( rmsg != NULL ) {
1092                 ber_memfree( rmsg );
1093                 rs->sr_text = save_rmsg;
1094         }
1095         if ( rmatch != NULL ) {
1096                 ber_memfree_x( rmatch, rmatch_ctx );
1097                 rs->sr_matched = save_rmatch;
1098         }
1099
1100         return ( ( rerr == LDAP_SUCCESS ) ? 0 : -1 );
1101 }
1102