]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/search.c
minor cleanup
[openldap] / servers / slapd / back-meta / search.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2010 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/socket.h>
28 #include <ac/string.h>
29 #include <ac/time.h>
30
31 #include "lutil.h"
32 #include "slap.h"
33 #include "../back-ldap/back-ldap.h"
34 #include "back-meta.h"
35 #undef ldap_debug       /* silence a warning in ldap-int.h */
36 #include "ldap_log.h"
37 #include "../../../libraries/libldap/ldap-int.h"
38
39 /* IGNORE means that target does not (no longer) participate
40  * in the search;
41  * NOTREADY means the search on that target has not been initialized yet
42  */
43 #define META_MSGID_IGNORE       (-1)
44 #define META_MSGID_NEED_BIND    (-2)
45 #define META_MSGID_CONNECTING   (-3)
46
47 static int
48 meta_send_entry(
49         Operation       *op,
50         SlapReply       *rs,
51         metaconn_t      *mc,
52         int             i,
53         LDAPMessage     *e );
54
55 typedef enum meta_search_candidate_t {
56         META_SEARCH_UNDEFINED = -2,
57         META_SEARCH_ERR = -1,
58         META_SEARCH_NOT_CANDIDATE,
59         META_SEARCH_CANDIDATE,
60         META_SEARCH_BINDING,
61         META_SEARCH_NEED_BIND,
62         META_SEARCH_CONNECTING
63 } meta_search_candidate_t;
64
65 /*
66  * meta_search_dobind_init()
67  *
68  * initiates bind for a candidate target of a search.
69  */
70 static meta_search_candidate_t
71 meta_search_dobind_init(
72         Operation               *op,
73         SlapReply               *rs,
74         metaconn_t              **mcp,
75         int                     candidate,
76         SlapReply               *candidates )
77 {
78         metaconn_t              *mc = *mcp;
79         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
80         metatarget_t            *mt = mi->mi_targets[ candidate ];
81         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
82
83         struct berval           binddn = msc->msc_bound_ndn,
84                                 cred = msc->msc_cred;
85         int                     method;
86
87         int                     rc;
88
89         meta_search_candidate_t retcode;
90
91         Debug( LDAP_DEBUG_TRACE, "%s >>> meta_search_dobind_init[%d]\n",
92                 op->o_log_prefix, candidate, 0 );
93
94         /*
95          * all the targets are already bound as pseudoroot
96          */
97         if ( mc->mc_authz_target == META_BOUND_ALL ) {
98                 return META_SEARCH_CANDIDATE;
99         }
100
101         retcode = META_SEARCH_BINDING;
102         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
103         if ( LDAP_BACK_CONN_ISBOUND( msc ) || LDAP_BACK_CONN_ISANON( msc ) ) {
104                 /* already bound (or anonymous) */
105
106 #ifdef DEBUG_205
107                 char    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
108                 int     bound = 0;
109
110                 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
111                         bound = 1;
112                 }
113
114                 snprintf( buf, sizeof( buf ), " mc=%p ld=%p%s DN=\"%s\"",
115                         (void *)mc, (void *)msc->msc_ld,
116                         bound ? " bound" : " anonymous",
117                         bound == 0 ? "" : msc->msc_bound_ndn.bv_val );
118                 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
119                         op->o_log_prefix, candidate, buf );
120 #endif /* DEBUG_205 */
121
122                 retcode = META_SEARCH_CANDIDATE;
123
124         } else if ( META_BACK_CONN_CREATING( msc ) || LDAP_BACK_CONN_BINDING( msc ) ) {
125                 /* another thread is binding the target for this conn; wait */
126
127 #ifdef DEBUG_205
128                 char    buf[ SLAP_TEXT_BUFLEN ] = { '\0' };
129
130                 snprintf( buf, sizeof( buf ), " mc=%p ld=%p needbind",
131                         (void *)mc, (void *)msc->msc_ld );
132                 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
133                         op->o_log_prefix, candidate, buf );
134 #endif /* DEBUG_205 */
135
136                 candidates[ candidate ].sr_msgid = META_MSGID_NEED_BIND;
137                 retcode = META_SEARCH_NEED_BIND;
138
139         } else {
140                 /* we'll need to bind the target for this conn */
141
142 #ifdef DEBUG_205
143                 char buf[ SLAP_TEXT_BUFLEN ];
144
145                 snprintf( buf, sizeof( buf ), " mc=%p ld=%p binding",
146                         (void *)mc, (void *)msc->msc_ld );
147                 Debug( LDAP_DEBUG_ANY, "### %s meta_search_dobind_init[%d]%s\n",
148                         op->o_log_prefix, candidate, buf );
149 #endif /* DEBUG_205 */
150
151                 if ( msc->msc_ld == NULL ) {
152                         /* for some reason (e.g. because formerly in "binding"
153                          * state, with eventual connection expiration or invalidation)
154                          * it was not initialized as expected */
155
156                         Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p ld=NULL\n",
157                                 op->o_log_prefix, candidate, (void *)mc );
158
159                         rc = meta_back_init_one_conn( op, rs, *mcp, candidate,
160                                 LDAP_BACK_CONN_ISPRIV( *mcp ), LDAP_BACK_DONTSEND, 0 );
161                         switch ( rc ) {
162                         case LDAP_SUCCESS:
163                                 assert( msc->msc_ld != NULL );
164                                 break;
165
166                         case LDAP_SERVER_DOWN:
167                         case LDAP_UNAVAILABLE:
168                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
169                                 goto down;
170         
171                         default:
172                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
173                                 goto other;
174                         }
175                 }
176
177                 LDAP_BACK_CONN_BINDING_SET( msc );
178         }
179
180         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
181
182         if ( retcode != META_SEARCH_BINDING ) {
183                 return retcode;
184         }
185
186         /* NOTE: this obsoletes pseudorootdn */
187         if ( op->o_conn != NULL &&
188                 !op->o_do_not_cache &&
189                 ( BER_BVISNULL( &msc->msc_bound_ndn ) ||
190                         BER_BVISEMPTY( &msc->msc_bound_ndn ) ||
191                         ( mt->mt_idassert_flags & LDAP_BACK_AUTH_OVERRIDE ) ) )
192         {
193                 rc = meta_back_proxy_authz_cred( mc, candidate, op, rs, LDAP_BACK_DONTSEND, &binddn, &cred, &method );
194                 switch ( rc ) {
195                 case LDAP_SUCCESS:
196                         break;
197                 case LDAP_UNAVAILABLE:
198                         goto down;
199                 default:
200                         goto other;
201                 }
202
203                 /* NOTE: we copy things here, even if bind didn't succeed yet,
204                  * because the connection is not shared until bind is over */
205                 if ( !BER_BVISNULL( &binddn ) ) {
206                         ber_bvreplace( &msc->msc_bound_ndn, &binddn );
207                         if ( META_BACK_TGT_SAVECRED( mt ) && !BER_BVISNULL( &cred ) ) {
208                                 if ( !BER_BVISNULL( &msc->msc_cred ) ) {
209                                         memset( msc->msc_cred.bv_val, 0,
210                                                 msc->msc_cred.bv_len );
211                                 }
212                                 ber_bvreplace( &msc->msc_cred, &cred );
213                         }
214                 }
215
216                 if ( LDAP_BACK_CONN_ISBOUND( msc ) ) {
217                         /* apparently, idassert was configured with SASL bind,
218                          * so bind occurred inside meta_back_proxy_authz_cred() */
219                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
220                         LDAP_BACK_CONN_BINDING_CLEAR( msc );
221                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
222                         return META_SEARCH_CANDIDATE;
223                 }
224
225                 /* paranoid */
226                 switch ( method ) {
227                 case LDAP_AUTH_NONE:
228                 case LDAP_AUTH_SIMPLE:
229                         /* do a simple bind with binddn, cred */
230                         break;
231
232                 default:
233                         assert( 0 );
234                         break;
235                 }
236         }
237
238         assert( msc->msc_ld != NULL );
239
240         if ( !BER_BVISEMPTY( &binddn ) && BER_BVISEMPTY( &cred ) ) {
241                 /* bind anonymously? */
242                 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p: "
243                         "non-empty dn with empty cred; binding anonymously\n",
244                         op->o_log_prefix, candidate, (void *)mc );
245                 cred = slap_empty_bv;
246                 
247         } else if ( BER_BVISEMPTY( &binddn ) && !BER_BVISEMPTY( &cred ) ) {
248                 /* error */
249                 Debug( LDAP_DEBUG_ANY, "%s meta_search_dobind_init[%d] mc=%p: "
250                         "empty dn with non-empty cred: error\n",
251                         op->o_log_prefix, candidate, (void *)mc );
252                 goto other;
253         }
254
255         /* connect must be async only the first time... */
256         ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_ON );
257
258 retry:;
259         rc = ldap_sasl_bind( msc->msc_ld, binddn.bv_val, LDAP_SASL_SIMPLE, &cred,
260                         NULL, NULL, &candidates[ candidate ].sr_msgid );
261
262 #ifdef DEBUG_205
263         {
264                 char buf[ SLAP_TEXT_BUFLEN ];
265
266                 snprintf( buf, sizeof( buf ), "meta_search_dobind_init[%d] mc=%p ld=%p rc=%d",
267                         candidate, (void *)mc, (void *)mc->mc_conns[ candidate ].msc_ld, rc );
268                 Debug( LDAP_DEBUG_ANY, "### %s %s\n",
269                         op->o_log_prefix, buf, 0 );
270         }
271 #endif /* DEBUG_205 */
272
273         switch ( rc ) {
274         case LDAP_SUCCESS:
275                 assert( candidates[ candidate ].sr_msgid >= 0 );
276                 META_BINDING_SET( &candidates[ candidate ] );
277                 return META_SEARCH_BINDING;
278
279         case LDAP_X_CONNECTING:
280                 /* must retry, same conn */
281                 candidates[ candidate ].sr_msgid = META_MSGID_CONNECTING;
282                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
283                 LDAP_BACK_CONN_BINDING_CLEAR( msc );
284                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
285                 return META_SEARCH_CONNECTING;
286
287         case LDAP_SERVER_DOWN:
288 down:;
289                 /* This is the worst thing that could happen:
290                  * the search will wait until the retry is over. */
291                 if ( !META_IS_RETRYING( &candidates[ candidate ] ) ) {
292                         META_RETRYING_SET( &candidates[ candidate ] );
293
294                         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
295
296                         assert( mc->mc_refcnt > 0 );
297                         if ( LogTest( LDAP_DEBUG_ANY ) ) {
298                                 char    buf[ SLAP_TEXT_BUFLEN ];
299
300                                 /* this lock is required; however,
301                                  * it's invoked only when logging is on */
302                                 ldap_pvt_thread_mutex_lock( &mt->mt_uri_mutex );
303                                 snprintf( buf, sizeof( buf ),
304                                         "retrying URI=\"%s\" DN=\"%s\"",
305                                         mt->mt_uri,
306                                         BER_BVISNULL( &msc->msc_bound_ndn ) ?
307                                                 "" : msc->msc_bound_ndn.bv_val );
308                                 ldap_pvt_thread_mutex_unlock( &mt->mt_uri_mutex );
309
310                                 Debug( LDAP_DEBUG_ANY,
311                                         "%s meta_search_dobind_init[%d]: %s.\n",
312                                         op->o_log_prefix, candidate, buf );
313                         }
314
315                         meta_clear_one_candidate( op, mc, candidate );
316                         LDAP_BACK_CONN_ISBOUND_CLEAR( msc );
317
318                         ( void )rewrite_session_delete( mt->mt_rwmap.rwm_rw, op->o_conn );
319
320                         /* mc here must be the regular mc, reset and ready for init */
321                         rc = meta_back_init_one_conn( op, rs, mc, candidate,
322                                 LDAP_BACK_CONN_ISPRIV( mc ), LDAP_BACK_DONTSEND, 0 );
323
324                         if ( rc == LDAP_SUCCESS ) {
325                                 LDAP_BACK_CONN_BINDING_SET( msc );
326                         }
327
328                         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
329
330                         if ( rc == LDAP_SUCCESS ) {
331                                 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
332                                 goto retry;
333                         }
334                 }
335
336                 if ( *mcp == NULL ) {
337                         retcode = META_SEARCH_ERR;
338                         rs->sr_err = LDAP_UNAVAILABLE;
339                         candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
340                         break;
341                 }
342                 /* fall thru */
343
344         default:
345 other:;
346                 rs->sr_err = rc;
347                 rc = slap_map_api2result( rs );
348
349                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
350                 meta_clear_one_candidate( op, mc, candidate );
351                 candidates[ candidate ].sr_err = rc;
352                 if ( META_BACK_ONERR_STOP( mi ) ) {
353                         LDAP_BACK_CONN_TAINTED_SET( mc );
354                         meta_back_release_conn_lock( mi, mc, 0 );
355                         *mcp = NULL;
356                         rs->sr_err = rc;
357
358                         retcode = META_SEARCH_ERR;
359
360                 } else {
361                         retcode = META_SEARCH_NOT_CANDIDATE;
362                 }
363                 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
364                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
365                 break;
366         }
367
368         return retcode;
369 }
370
371 static meta_search_candidate_t
372 meta_search_dobind_result(
373         Operation               *op,
374         SlapReply               *rs,
375         metaconn_t              **mcp,
376         int                     candidate,
377         SlapReply               *candidates,
378         LDAPMessage             *res )
379 {
380         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
381         metaconn_t              *mc = *mcp;
382         metasingleconn_t        *msc = &mc->mc_conns[ candidate ];
383
384         meta_search_candidate_t retcode = META_SEARCH_NOT_CANDIDATE;
385         int                     rc;
386
387         assert( msc->msc_ld != NULL );
388
389         /* FIXME: matched? referrals? response controls? */
390         rc = ldap_parse_result( msc->msc_ld, res,
391                 &candidates[ candidate ].sr_err,
392                 NULL, NULL, NULL, NULL, 0 );
393         if ( rc != LDAP_SUCCESS ) {
394                 candidates[ candidate ].sr_err = rc;
395         }
396         rc = slap_map_api2result( &candidates[ candidate ] );
397
398         ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
399         LDAP_BACK_CONN_BINDING_CLEAR( msc );
400         if ( rc != LDAP_SUCCESS ) {
401                 meta_clear_one_candidate( op, mc, candidate );
402                 candidates[ candidate ].sr_err = rc;
403                 if ( META_BACK_ONERR_STOP( mi ) ) {
404                         LDAP_BACK_CONN_TAINTED_SET( mc );
405                         meta_back_release_conn_lock( mi, mc, 0 );
406                         *mcp = NULL;
407                         retcode = META_SEARCH_ERR;
408                         rs->sr_err = rc;
409                 }
410
411         } else {
412                 /* FIXME: check if bound as idassert authcDN! */
413                 if ( BER_BVISNULL( &msc->msc_bound_ndn )
414                         || BER_BVISEMPTY( &msc->msc_bound_ndn ) )
415                 {
416                         LDAP_BACK_CONN_ISANON_SET( msc );
417
418                 } else {
419                         LDAP_BACK_CONN_ISBOUND_SET( msc );
420                 }
421                 retcode = META_SEARCH_CANDIDATE;
422
423                 /* connect must be async */
424                 ldap_set_option( msc->msc_ld, LDAP_OPT_CONNECT_ASYNC, LDAP_OPT_OFF );
425         }
426
427         candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
428         META_BINDING_CLEAR( &candidates[ candidate ] );
429
430         ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
431
432         return retcode;
433 }
434
435 static meta_search_candidate_t
436 meta_back_search_start(
437         Operation               *op,
438         SlapReply               *rs,
439         dncookie                *dc,
440         metaconn_t              **mcp,
441         int                     candidate,
442         SlapReply               *candidates )
443 {
444         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
445         metatarget_t            *mt = mi->mi_targets[ candidate ];
446         metasingleconn_t        *msc = &(*mcp)->mc_conns[ candidate ];
447         struct berval           realbase = op->o_req_dn;
448         int                     realscope = op->ors_scope;
449         struct berval           mbase = BER_BVNULL; 
450         struct berval           mfilter = BER_BVNULL;
451         char                    **mapped_attrs = NULL;
452         int                     rc;
453         meta_search_candidate_t retcode;
454         struct timeval          tv, *tvp = NULL;
455         int                     nretries = 1;
456         LDAPControl             **ctrls = NULL;
457
458         /* this should not happen; just in case... */
459         if ( msc->msc_ld == NULL ) {
460                 Debug( LDAP_DEBUG_ANY,
461                         "%s: meta_back_search_start candidate=%d ld=NULL%s.\n",
462                         op->o_log_prefix, candidate,
463                         META_BACK_ONERR_STOP( mi ) ? "" : " (ignored)" );
464                 candidates[ candidate ].sr_err = LDAP_OTHER;
465                 if ( META_BACK_ONERR_STOP( mi ) ) {
466                         return META_SEARCH_ERR;
467                 }
468                 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
469                 return META_SEARCH_NOT_CANDIDATE;
470         }
471
472         Debug( LDAP_DEBUG_TRACE, "%s >>> meta_back_search_start[%d]\n", op->o_log_prefix, candidate, 0 );
473
474         /*
475          * modifies the base according to the scope, if required
476          */
477         if ( mt->mt_nsuffix.bv_len > op->o_req_ndn.bv_len ) {
478                 switch ( op->ors_scope ) {
479                 case LDAP_SCOPE_SUBTREE:
480                         /*
481                          * make the target suffix the new base
482                          * FIXME: this is very forgiving, because
483                          * "illegal" searchBases may be turned
484                          * into the suffix of the target; however,
485                          * the requested searchBase already passed
486                          * thru the candidate analyzer...
487                          */
488                         if ( dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) ) {
489                                 realbase = mt->mt_nsuffix;
490                                 if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
491                                         realscope = LDAP_SCOPE_SUBORDINATE;
492                                 }
493
494                         } else {
495                                 /*
496                                  * this target is no longer candidate
497                                  */
498                                 retcode = META_SEARCH_NOT_CANDIDATE;
499                                 goto doreturn;
500                         }
501                         break;
502
503                 case LDAP_SCOPE_SUBORDINATE:
504                 case LDAP_SCOPE_ONELEVEL:
505                 {
506                         struct berval   rdn = mt->mt_nsuffix;
507                         rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
508                         if ( dnIsOneLevelRDN( &rdn )
509                                         && dnIsSuffix( &mt->mt_nsuffix, &op->o_req_ndn ) )
510                         {
511                                 /*
512                                  * if there is exactly one level,
513                                  * make the target suffix the new
514                                  * base, and make scope "base"
515                                  */
516                                 realbase = mt->mt_nsuffix;
517                                 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
518                                         if ( mt->mt_scope == LDAP_SCOPE_SUBORDINATE ) {
519                                                 realscope = LDAP_SCOPE_SUBORDINATE;
520                                         } else {
521                                                 realscope = LDAP_SCOPE_SUBTREE;
522                                         }
523                                 } else {
524                                         realscope = LDAP_SCOPE_BASE;
525                                 }
526                                 break;
527                         } /* else continue with the next case */
528                 }
529
530                 case LDAP_SCOPE_BASE:
531                         /*
532                          * this target is no longer candidate
533                          */
534                         retcode = META_SEARCH_NOT_CANDIDATE;
535                         goto doreturn;
536                 }
537         }
538
539         /* initiate dobind */
540         retcode = meta_search_dobind_init( op, rs, mcp, candidate, candidates );
541
542         Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%d]=%d\n", op->o_log_prefix, candidate, retcode );
543
544         if ( retcode != META_SEARCH_CANDIDATE ) {
545                 goto doreturn;
546         }
547
548         /*
549          * Rewrite the search base, if required
550          */
551         dc->target = mt;
552         dc->ctx = "searchBase";
553         switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
554         case LDAP_SUCCESS:
555                 break;
556
557         case LDAP_UNWILLING_TO_PERFORM:
558                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
559                 rs->sr_text = "Operation not allowed";
560                 send_ldap_result( op, rs );
561                 retcode = META_SEARCH_ERR;
562                 goto doreturn;
563
564         default:
565
566                 /*
567                  * this target is no longer candidate
568                  */
569                 retcode = META_SEARCH_NOT_CANDIDATE;
570                 goto doreturn;
571         }
572
573         /*
574          * Maps filter
575          */
576         rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
577                         &mfilter, BACKLDAP_MAP, op->o_tmpmemctx );
578         switch ( rc ) {
579         case LDAP_SUCCESS:
580                 break;
581
582         case LDAP_COMPARE_FALSE:
583         default:
584                 /*
585                  * this target is no longer candidate
586                  */
587                 retcode = META_SEARCH_NOT_CANDIDATE;
588                 goto done;
589         }
590
591         /*
592          * Maps required attributes
593          */
594         rc = ldap_back_map_attrs( op, &mt->mt_rwmap.rwm_at,
595                         op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
596         if ( rc != LDAP_SUCCESS ) {
597                 /*
598                  * this target is no longer candidate
599                  */
600                 retcode = META_SEARCH_NOT_CANDIDATE;
601                 goto done;
602         }
603
604         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
605                 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
606                 tv.tv_usec = 0;
607                 tvp = &tv;
608         }
609
610 retry:;
611         ctrls = op->o_ctrls;
612         if ( meta_back_controls_add( op, rs, *mcp, candidate, &ctrls )
613                 != LDAP_SUCCESS )
614         {
615                 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
616                 retcode = META_SEARCH_NOT_CANDIDATE;
617                 goto done;
618         }
619
620         /*
621          * Starts the search
622          */
623         assert( msc->msc_ld != NULL );
624         rc = ldap_pvt_search( msc->msc_ld,
625                         mbase.bv_val, realscope, mfilter.bv_val,
626                         mapped_attrs, op->ors_attrsonly,
627                         ctrls, NULL, tvp, op->ors_slimit, op->ors_deref,
628                         &candidates[ candidate ].sr_msgid ); 
629         switch ( rc ) {
630         case LDAP_SUCCESS:
631                 retcode = META_SEARCH_CANDIDATE;
632                 break;
633         
634         case LDAP_SERVER_DOWN:
635                 if ( nretries && meta_back_retry( op, rs, mcp, candidate, LDAP_BACK_DONTSEND ) ) {
636                         nretries = 0;
637                         /* if the identity changed, there might be need to re-authz */
638                         (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
639                         goto retry;
640                 }
641
642                 if ( *mcp == NULL ) {
643                         retcode = META_SEARCH_ERR;
644                         candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
645                         break;
646                 }
647                 /* fall thru */
648
649         default:
650                 candidates[ candidate ].sr_msgid = META_MSGID_IGNORE;
651                 retcode = META_SEARCH_NOT_CANDIDATE;
652         }
653
654 done:;
655         (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
656
657         if ( mapped_attrs ) {
658                 ber_memfree_x( mapped_attrs, op->o_tmpmemctx );
659         }
660         if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
661                 ber_memfree_x( mfilter.bv_val, op->o_tmpmemctx );
662         }
663         if ( mbase.bv_val != realbase.bv_val ) {
664                 free( mbase.bv_val );
665         }
666
667 doreturn:;
668         Debug( LDAP_DEBUG_TRACE, "%s <<< meta_back_search_start[%d]=%d\n", op->o_log_prefix, candidate, retcode );
669
670         return retcode;
671 }
672
673 int
674 meta_back_search( Operation *op, SlapReply *rs )
675 {
676         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
677         metaconn_t      *mc;
678         struct timeval  save_tv = { 0, 0 },
679                         tv;
680         time_t          stoptime = (time_t)(-1),
681                         lastres_time = slap_get_time(),
682                         timeout = 0;
683         int             rc = 0, sres = LDAP_SUCCESS;
684         char            *matched = NULL;
685         int             last = 0, ncandidates = 0,
686                         initial_candidates = 0, candidate_match = 0,
687                         needbind = 0;
688         ldap_back_send_t        sendok = LDAP_BACK_SENDERR;
689         long            i;
690         dncookie        dc;
691         int             is_ok = 0;
692         void            *savepriv;
693         SlapReply       *candidates = NULL;
694         int             do_taint = 0;
695
696         /*
697          * controls are set in ldap_back_dobind()
698          * 
699          * FIXME: in case of values return filter, we might want
700          * to map attrs and maybe rewrite value
701          */
702 getconn:;
703         mc = meta_back_getconn( op, rs, NULL, sendok );
704         if ( !mc ) {
705                 return rs->sr_err;
706         }
707
708         dc.conn = op->o_conn;
709         dc.rs = rs;
710
711         if ( candidates == NULL ) candidates = meta_back_candidates_get( op );
712         /*
713          * Inits searches
714          */
715         for ( i = 0; i < mi->mi_ntargets; i++ ) {
716                 /* reset sr_msgid; it is used in most loops
717                  * to check if that target is still to be considered */
718                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
719
720                 /* a target is marked as candidate by meta_back_getconn();
721                  * if for any reason (an error, it's over or so) it is
722                  * no longer active, sr_msgid is set to META_MSGID_IGNORE
723                  * but it remains candidate, which means it has been active
724                  * at some point during the operation.  This allows to 
725                  * use its response code and more to compute the final
726                  * response */
727                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
728                         continue;
729                 }
730
731                 candidates[ i ].sr_matched = NULL;
732                 candidates[ i ].sr_text = NULL;
733                 candidates[ i ].sr_ref = NULL;
734                 candidates[ i ].sr_ctrls = NULL;
735
736                 /* get largest timeout among candidates */
737                 if ( mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ]
738                         && mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ] > timeout )
739                 {
740                         timeout = mi->mi_targets[ i ]->mt_timeout[ SLAP_OP_SEARCH ];
741                 }
742         }
743
744         for ( i = 0; i < mi->mi_ntargets; i++ ) {
745                 if ( !META_IS_CANDIDATE( &candidates[ i ] )
746                         || candidates[ i ].sr_err != LDAP_SUCCESS )
747                 {
748                         continue;
749                 }
750
751                 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
752                 {
753                 case META_SEARCH_NOT_CANDIDATE:
754                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
755                         break;
756
757                 case META_SEARCH_NEED_BIND:
758                         ++needbind;
759                         /* fallthru */
760
761                 case META_SEARCH_CONNECTING:
762                 case META_SEARCH_CANDIDATE:
763                 case META_SEARCH_BINDING:
764                         candidates[ i ].sr_type = REP_INTERMEDIATE;
765                         ++ncandidates;
766                         break;
767
768                 case META_SEARCH_ERR:
769                         savepriv = op->o_private;
770                         op->o_private = (void *)i;
771                         send_ldap_result( op, rs );
772                         op->o_private = savepriv;
773                         rc = -1;
774                         goto finish;
775
776                 default:
777                         assert( 0 );
778                         break;
779                 }
780         }
781
782         if ( ncandidates > 0 && needbind == ncandidates ) {
783                 /*
784                  * give up the second time...
785                  *
786                  * NOTE: this should not occur the second time, since a fresh
787                  * connection has ben created; however, targets may also
788                  * need bind because the bind timed out or so.
789                  */
790                 if ( sendok & LDAP_BACK_BINDING ) {
791                         Debug( LDAP_DEBUG_ANY,
792                                 "%s meta_back_search: unable to initialize conn\n",
793                                 op->o_log_prefix, 0, 0 );
794                         rs->sr_err = LDAP_UNAVAILABLE;
795                         rs->sr_text = "unable to initialize connection to remote targets";
796                         send_ldap_result( op, rs );
797                         rc = -1;
798                         goto finish;
799                 }
800
801                 /* FIXME: better create a separate connection? */
802                 sendok |= LDAP_BACK_BINDING;
803
804 #ifdef DEBUG_205
805                 Debug( LDAP_DEBUG_ANY, "*** %s drop mc=%p create new connection\n",
806                         op->o_log_prefix, (void *)mc, 0 );
807 #endif /* DEBUG_205 */
808
809                 meta_back_release_conn( mi, mc );
810                 mc = NULL;
811
812                 needbind = 0;
813                 ncandidates = 0;
814
815                 goto getconn;
816         }
817
818         initial_candidates = ncandidates;
819
820         if ( LogTest( LDAP_DEBUG_TRACE ) ) {
821                 char    cnd[ SLAP_TEXT_BUFLEN ];
822                 int     c;
823
824                 for ( c = 0; c < mi->mi_ntargets; c++ ) {
825                         if ( META_IS_CANDIDATE( &candidates[ c ] ) ) {
826                                 cnd[ c ] = '*';
827                         } else {
828                                 cnd[ c ] = ' ';
829                         }
830                 }
831                 cnd[ c ] = '\0';
832
833                 Debug( LDAP_DEBUG_TRACE, "%s meta_back_search: ncandidates=%d "
834                         "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
835         }
836
837         if ( initial_candidates == 0 ) {
838                 /* NOTE: here we are not sending any matchedDN;
839                  * this is intended, because if the back-meta
840                  * is serving this search request, but no valid
841                  * candidate could be looked up, it means that
842                  * there is a hole in the mapping of the targets
843                  * and thus no knowledge of any remote superior
844                  * is available */
845                 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
846                         "base=\"%s\" scope=%d: "
847                         "no candidate could be selected\n",
848                         op->o_log_prefix, op->o_req_dn.bv_val,
849                         op->ors_scope );
850
851                 /* FIXME: we're sending the first error we encounter;
852                  * maybe we should pick the worst... */
853                 rc = LDAP_NO_SUCH_OBJECT;
854                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
855                         if ( META_IS_CANDIDATE( &candidates[ i ] )
856                                 && candidates[ i ].sr_err != LDAP_SUCCESS )
857                         {
858                                 rc = candidates[ i ].sr_err;
859                                 break;
860                         }
861                 }
862
863                 send_ldap_error( op, rs, rc, NULL );
864
865                 goto finish;
866         }
867
868         /* We pull apart the ber result, stuff it into a slapd entry, and
869          * let send_search_entry stuff it back into ber format. Slow & ugly,
870          * but this is necessary for version matching, and for ACL processing.
871          */
872
873         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
874                 stoptime = op->o_time + op->ors_tlimit;
875         }
876
877         /*
878          * In case there are no candidates, no cycle takes place...
879          *
880          * FIXME: we might use a queue, to better balance the load 
881          * among the candidates
882          */
883         for ( rc = 0; ncandidates > 0; ) {
884                 int     gotit = 0,
885                         doabandon = 0,
886                         alreadybound = ncandidates;
887
888                 /* check timeout */
889                 if ( timeout && lastres_time > 0
890                         && ( slap_get_time() - lastres_time ) > timeout )
891                 {
892                         doabandon = 1;
893                         rs->sr_text = "Operation timed out";
894                         rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
895                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
896                         savepriv = op->o_private;
897                         op->o_private = (void *)i;
898                         send_ldap_result( op, rs );
899                         op->o_private = savepriv;
900                         goto finish;
901                 }
902
903                 /* check time limit */
904                 if ( op->ors_tlimit != SLAP_NO_LIMIT
905                                 && slap_get_time() > stoptime )
906                 {
907                         doabandon = 1;
908                         rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
909                         savepriv = op->o_private;
910                         op->o_private = (void *)i;
911                         send_ldap_result( op, rs );
912                         op->o_private = savepriv;
913                         goto finish;
914                 }
915
916                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
917                         meta_search_candidate_t retcode = META_SEARCH_UNDEFINED;
918                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
919                         LDAPMessage             *res = NULL, *msg;
920
921                         /* if msgid is invalid, don't ldap_result() */
922                         if ( candidates[ i ].sr_msgid == META_MSGID_IGNORE ) {
923                                 continue;
924                         }
925
926                         /* if target still needs bind, retry */
927                         if ( candidates[ i ].sr_msgid == META_MSGID_NEED_BIND
928                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
929                         {
930                                 /* initiate dobind */
931                                 retcode = meta_search_dobind_init( op, rs, &mc, i, candidates );
932
933                                 Debug( LDAP_DEBUG_TRACE, "%s <<< meta_search_dobind_init[%ld]=%d\n",
934                                         op->o_log_prefix, i, retcode );
935
936                                 switch ( retcode ) {
937                                 case META_SEARCH_NEED_BIND:
938                                         alreadybound--;
939                                         /* fallthru */
940
941                                 case META_SEARCH_CONNECTING:
942                                 case META_SEARCH_BINDING:
943                                         break;
944
945                                 case META_SEARCH_ERR:
946                                         candidates[ i ].sr_err = rs->sr_err;
947                                         if ( META_BACK_ONERR_STOP( mi ) ) {
948                                                 savepriv = op->o_private;
949                                                 op->o_private = (void *)i;
950                                                 send_ldap_result( op, rs );
951                                                 op->o_private = savepriv;
952                                                 goto finish;
953                                         }
954                                         /* fallthru */
955
956                                 case META_SEARCH_NOT_CANDIDATE:
957                                         /*
958                                          * When no candidates are left,
959                                          * the outer cycle finishes
960                                          */
961                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
962                                         assert( ncandidates > 0 );
963                                         --ncandidates;
964                                         break;
965
966                                 case META_SEARCH_CANDIDATE:
967                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
968                                         switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
969                                         {
970                                         case META_SEARCH_CANDIDATE:
971                                                 assert( candidates[ i ].sr_msgid >= 0 );
972                                                 break;
973
974                                         case META_SEARCH_ERR:
975                                                 candidates[ i ].sr_err = rs->sr_err;
976                                                 if ( META_BACK_ONERR_STOP( mi ) ) {
977                                                         savepriv = op->o_private;
978                                                         op->o_private = (void *)i;
979                                                         send_ldap_result( op, rs );
980                                                         op->o_private = savepriv;
981                                                         goto finish;
982                                                 }
983                                                 /* fallthru */
984
985                                         case META_SEARCH_NOT_CANDIDATE:
986                                                 /* means that meta_back_search_start()
987                                                  * failed but onerr == continue */
988                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
989                                                 assert( ncandidates > 0 );
990                                                 --ncandidates;
991                                                 break;
992
993                                         default:
994                                                 /* impossible */
995                                                 assert( 0 );
996                                                 break;
997                                         }
998                                         break;
999
1000                                 default:
1001                                         /* impossible */
1002                                         assert( 0 );
1003                                         break;
1004                                 }
1005                                 continue;
1006                         }
1007
1008                         /* check for abandon */
1009                         if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1010                                 break;
1011                         }
1012
1013 #ifdef DEBUG_205
1014                         if ( msc->msc_ld == NULL ) {
1015                                 char    buf[ SLAP_TEXT_BUFLEN ];
1016
1017                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1018                                 snprintf( buf, sizeof( buf ),
1019                                         "%s meta_back_search[%ld] mc=%p msgid=%d%s%s%s\n",
1020                                         op->o_log_prefix, (long)i, (void *)mc,
1021                                         candidates[ i ].sr_msgid,
1022                                         META_IS_BINDING( &candidates[ i ] ) ? " binding" : "",
1023                                         LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] ) ? " connbinding" : "",
1024                                         META_BACK_CONN_CREATING( &mc->mc_conns[ i ] ) ? " conncreating" : "" );
1025                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1026                                         
1027                                 Debug( LDAP_DEBUG_ANY, "!!! %s\n", buf, 0, 0 );
1028                         }
1029 #endif /* DEBUG_205 */
1030                         
1031                         /*
1032                          * FIXME: handle time limit as well?
1033                          * Note that target servers are likely 
1034                          * to handle it, so at some time we'll
1035                          * get a LDAP_TIMELIMIT_EXCEEDED from
1036                          * one of them ...
1037                          */
1038                         tv = save_tv;
1039                         rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
1040                                         LDAP_MSG_RECEIVED, &tv, &res );
1041                         switch ( rc ) {
1042                         case 0:
1043                                 /* FIXME: res should not need to be freed */
1044                                 assert( res == NULL );
1045                                 continue;
1046
1047                         case -1:
1048 really_bad:;
1049                                 /* something REALLY bad happened! */
1050                                 if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1051                                         candidates[ i ].sr_type = REP_RESULT;
1052
1053                                         if ( meta_back_retry( op, rs, &mc, i, LDAP_BACK_DONTSEND ) ) {
1054                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1055                                                 switch ( meta_back_search_start( op, rs, &dc, &mc, i, candidates ) )
1056                                                 {
1057                                                         /* means that failed but onerr == continue */
1058                                                 case META_SEARCH_NOT_CANDIDATE:
1059                                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1060
1061                                                         assert( ncandidates > 0 );
1062                                                         --ncandidates;
1063
1064                                                         candidates[ i ].sr_err = rs->sr_err;
1065                                                         if ( META_BACK_ONERR_STOP( mi ) ) {
1066                                                                 savepriv = op->o_private;
1067                                                                 op->o_private = (void *)i;
1068                                                                 send_ldap_result( op, rs );
1069                                                                 op->o_private = savepriv;
1070                                                                 goto finish;
1071                                                         }
1072                                                         /* fall thru */
1073
1074                                                 case META_SEARCH_CANDIDATE:
1075                                                         /* get back into business... */
1076                                                         continue;
1077
1078                                                 case META_SEARCH_BINDING:
1079                                                 case META_SEARCH_CONNECTING:
1080                                                 case META_SEARCH_NEED_BIND:
1081                                                 case META_SEARCH_UNDEFINED:
1082                                                         assert( 0 );
1083
1084                                                 default:
1085                                                         /* unrecoverable error */
1086                                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1087                                                         rc = rs->sr_err = LDAP_OTHER;
1088                                                         goto finish;
1089                                                 }
1090                                         }
1091
1092                                         candidates[ i ].sr_err = rs->sr_err;
1093                                         if ( META_BACK_ONERR_STOP( mi ) ) {
1094                                                 savepriv = op->o_private;
1095                                                 op->o_private = (void *)i;
1096                                                 send_ldap_result( op, rs );
1097                                                 op->o_private = savepriv;
1098                                                 goto finish;
1099                                         }
1100                                 }
1101
1102                                 /*
1103                                  * When no candidates are left,
1104                                  * the outer cycle finishes
1105                                  */
1106                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1107                                 assert( ncandidates > 0 );
1108                                 --ncandidates;
1109                                 rs->sr_err = candidates[ i ].sr_err;
1110                                 continue;
1111
1112                         default:
1113                                 lastres_time = slap_get_time();
1114
1115                                 /* only touch when activity actually took place... */
1116                                 if ( mi->mi_idle_timeout != 0 && msc->msc_time < lastres_time ) {
1117                                         msc->msc_time = lastres_time;
1118                                 }
1119                                 break;
1120                         }
1121
1122                         for ( msg = ldap_first_message( msc->msc_ld, res );
1123                                 msg != NULL;
1124                                 msg = ldap_next_message( msc->msc_ld, msg ) )
1125                         {
1126                                 rc = ldap_msgtype( msg );
1127                                 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
1128                                         LDAPMessage     *e;
1129
1130                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1131                                                 /* don't retry any more... */
1132                                                 candidates[ i ].sr_type = REP_RESULT;
1133                                         }
1134
1135                                         is_ok++;
1136
1137                                         e = ldap_first_entry( msc->msc_ld, msg );
1138                                         savepriv = op->o_private;
1139                                         op->o_private = (void *)i;
1140                                         rs->sr_err = meta_send_entry( op, rs, mc, i, e );
1141
1142                                         switch ( rs->sr_err ) {
1143                                         case LDAP_SIZELIMIT_EXCEEDED:
1144                                                 savepriv = op->o_private;
1145                                                 op->o_private = (void *)i;
1146                                                 send_ldap_result( op, rs );
1147                                                 op->o_private = savepriv;
1148                                                 rs->sr_err = LDAP_SUCCESS;
1149                                                 ldap_msgfree( res );
1150                                                 res = NULL;
1151                                                 goto finish;
1152
1153                                         case LDAP_UNAVAILABLE:
1154                                                 rs->sr_err = LDAP_OTHER;
1155                                                 ldap_msgfree( res );
1156                                                 res = NULL;
1157                                                 goto finish;
1158                                         }
1159                                         op->o_private = savepriv;
1160
1161                                         /* don't wait any longer... */
1162                                         gotit = 1;
1163                                         save_tv.tv_sec = 0;
1164                                         save_tv.tv_usec = 0;
1165
1166                                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
1167                                         char            **references = NULL;
1168                                         int             cnt;
1169
1170                                         if ( META_BACK_TGT_NOREFS( mi->mi_targets[ i ] ) ) {
1171                                                 continue;
1172                                         }
1173
1174                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1175                                                 /* don't retry any more... */
1176                                                 candidates[ i ].sr_type = REP_RESULT;
1177                                         }
1178         
1179                                         is_ok++;
1180         
1181                                         rc = ldap_parse_reference( msc->msc_ld, msg,
1182                                                         &references, &rs->sr_ctrls, 0 );
1183         
1184                                         if ( rc != LDAP_SUCCESS ) {
1185                                                 continue;
1186                                         }
1187         
1188                                         if ( references == NULL ) {
1189                                                 continue;
1190                                         }
1191
1192 #ifdef ENABLE_REWRITE
1193                                         dc.ctx = "referralDN";
1194 #else /* ! ENABLE_REWRITE */
1195                                         dc.tofrom = 0;
1196                                         dc.normalized = 0;
1197 #endif /* ! ENABLE_REWRITE */
1198
1199                                         /* FIXME: merge all and return at the end */
1200         
1201                                         for ( cnt = 0; references[ cnt ]; cnt++ )
1202                                                 ;
1203         
1204                                         rs->sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1205                                                 op->o_tmpmemctx );
1206         
1207                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
1208                                                 ber_str2bv_x( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ],
1209                                                 op->o_tmpmemctx );
1210                                         }
1211                                         BER_BVZERO( &rs->sr_ref[ cnt ] );
1212         
1213                                         ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref,
1214                                                 op->o_tmpmemctx );
1215
1216                                         if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
1217                                                 /* ignore return value by now */
1218                                                 savepriv = op->o_private;
1219                                                 op->o_private = (void *)i;
1220                                                 ( void )send_search_reference( op, rs );
1221                                                 op->o_private = savepriv;
1222         
1223                                                 ber_bvarray_free_x( rs->sr_ref, op->o_tmpmemctx );
1224                                                 rs->sr_ref = NULL;
1225                                         }
1226
1227                                         /* cleanup */
1228                                         if ( references ) {
1229                                                 ber_memvfree( (void **)references );
1230                                         }
1231
1232                                         if ( rs->sr_ctrls ) {
1233                                                 ldap_controls_free( rs->sr_ctrls );
1234                                                 rs->sr_ctrls = NULL;
1235                                         }
1236
1237                                 } else if ( rc == LDAP_RES_INTERMEDIATE ) {
1238                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1239                                                 /* don't retry any more... */
1240                                                 candidates[ i ].sr_type = REP_RESULT;
1241                                         }
1242         
1243                                         /* FIXME: response controls
1244                                          * are passed without checks */
1245                                         rs->sr_err = ldap_parse_intermediate( msc->msc_ld,
1246                                                 msg,
1247                                                 (char **)&rs->sr_rspoid,
1248                                                 &rs->sr_rspdata,
1249                                                 &rs->sr_ctrls,
1250                                                 0 );
1251                                         if ( rs->sr_err != LDAP_SUCCESS ) {
1252                                                 candidates[ i ].sr_type = REP_RESULT;
1253                                                 ldap_msgfree( res );
1254                                                 res = NULL;
1255                                                 goto really_bad;
1256                                         }
1257
1258                                         slap_send_ldap_intermediate( op, rs );
1259
1260                                         if ( rs->sr_rspoid != NULL ) {
1261                                                 ber_memfree( (char *)rs->sr_rspoid );
1262                                                 rs->sr_rspoid = NULL;
1263                                         }
1264
1265                                         if ( rs->sr_rspdata != NULL ) {
1266                                                 ber_bvfree( rs->sr_rspdata );
1267                                                 rs->sr_rspdata = NULL;
1268                                         }
1269
1270                                         if ( rs->sr_ctrls != NULL ) {
1271                                                 ldap_controls_free( rs->sr_ctrls );
1272                                                 rs->sr_ctrls = NULL;
1273                                         }
1274
1275                                 } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
1276                                         char            buf[ SLAP_TEXT_BUFLEN ];
1277                                         char            **references = NULL;
1278
1279                                         if ( candidates[ i ].sr_type == REP_INTERMEDIATE ) {
1280                                                 /* don't retry any more... */
1281                                                 candidates[ i ].sr_type = REP_RESULT;
1282                                         }
1283         
1284                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1285
1286                                         /* NOTE: ignores response controls
1287                                          * (and intermediate response controls
1288                                          * as well, except for those with search
1289                                          * references); this may not be correct,
1290                                          * but if they're not ignored then
1291                                          * back-meta would need to merge them
1292                                          * consistently (think of pagedResults...)
1293                                          */
1294                                         /* FIXME: response controls? */
1295                                         rs->sr_err = ldap_parse_result( msc->msc_ld,
1296                                                 msg,
1297                                                 &candidates[ i ].sr_err,
1298                                                 (char **)&candidates[ i ].sr_matched,
1299                                                 (char **)&candidates[ i ].sr_text,
1300                                                 &references,
1301                                                 NULL /* &candidates[ i ].sr_ctrls (unused) */ ,
1302                                                 0 );
1303                                         if ( rs->sr_err != LDAP_SUCCESS ) {
1304                                                 candidates[ i ].sr_err = rs->sr_err;
1305                                                 sres = slap_map_api2result( &candidates[ i ] );
1306                                                 candidates[ i ].sr_type = REP_RESULT;
1307                                                 ldap_msgfree( res );
1308                                                 res = NULL;
1309                                                 goto really_bad;
1310                                         }
1311
1312                                         rs->sr_err = candidates[ i ].sr_err;
1313
1314                                         /* massage matchedDN if need be */
1315                                         if ( candidates[ i ].sr_matched != NULL ) {
1316                                                 struct berval   match, mmatch;
1317
1318                                                 ber_str2bv( candidates[ i ].sr_matched,
1319                                                         0, 0, &match );
1320                                                 candidates[ i ].sr_matched = NULL;
1321
1322                                                 dc.ctx = "matchedDN";
1323                                                 dc.target = mi->mi_targets[ i ];
1324                                                 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
1325                                                         if ( mmatch.bv_val == match.bv_val ) {
1326                                                                 candidates[ i ].sr_matched
1327                                                                         = ch_strdup( mmatch.bv_val );
1328
1329                                                         } else {
1330                                                                 candidates[ i ].sr_matched = mmatch.bv_val;
1331                                                         }
1332
1333                                                         candidate_match++;
1334                                                 } 
1335                                                 ldap_memfree( match.bv_val );
1336                                         }
1337
1338                                         /* add references to array */
1339                                         /* RFC 4511: referrals can only appear
1340                                          * if result code is LDAP_REFERRAL */
1341                                         if ( references != NULL
1342                                                 && references[ 0 ] != NULL
1343                                                 && references[ 0 ][ 0 ] != '\0' )
1344                                         {
1345                                                 if ( rs->sr_err != LDAP_REFERRAL ) {
1346                                                         Debug( LDAP_DEBUG_ANY,
1347                                                                 "%s meta_back_search[%ld]: "
1348                                                                 "got referrals with err=%d\n",
1349                                                                 op->o_log_prefix,
1350                                                                 i, rs->sr_err );
1351
1352                                                 } else {
1353                                                         BerVarray       sr_ref;
1354                                                         int             cnt;
1355         
1356                                                         for ( cnt = 0; references[ cnt ]; cnt++ )
1357                                                                 ;
1358         
1359                                                         sr_ref = ber_memalloc_x( sizeof( struct berval ) * ( cnt + 1 ),
1360                                                                 op->o_tmpmemctx );
1361         
1362                                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
1363                                                                 ber_str2bv_x( references[ cnt ], 0, 1, &sr_ref[ cnt ],
1364                                                                         op->o_tmpmemctx );
1365                                                         }
1366                                                         BER_BVZERO( &sr_ref[ cnt ] );
1367         
1368                                                         ( void )ldap_back_referral_result_rewrite( &dc, sr_ref,
1369                                                                 op->o_tmpmemctx );
1370                                         
1371                                                         if ( rs->sr_v2ref == NULL ) {
1372                                                                 rs->sr_v2ref = sr_ref;
1373
1374                                                         } else {
1375                                                                 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
1376                                                                         ber_bvarray_add_x( &rs->sr_v2ref, &sr_ref[ cnt ],
1377                                                                                 op->o_tmpmemctx );
1378                                                                 }
1379                                                                 ber_memfree_x( sr_ref, op->o_tmpmemctx );
1380                                                         }
1381                                                 }
1382
1383                                         } else if ( rs->sr_err == LDAP_REFERRAL ) {
1384                                                 Debug( LDAP_DEBUG_ANY,
1385                                                         "%s meta_back_search[%ld]: "
1386                                                         "got err=%d with null "
1387                                                         "or empty referrals\n",
1388                                                         op->o_log_prefix,
1389                                                         i, rs->sr_err );
1390
1391                                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
1392                                         }
1393
1394                                         /* cleanup */
1395                                         ber_memvfree( (void **)references );
1396         
1397                                         sres = slap_map_api2result( rs );
1398         
1399                                         if ( LogTest( LDAP_DEBUG_TRACE | LDAP_DEBUG_ANY ) ) {
1400                                                 snprintf( buf, sizeof( buf ),
1401                                                         "%s meta_back_search[%ld] "
1402                                                         "match=\"%s\" err=%ld",
1403                                                         op->o_log_prefix, i,
1404                                                         candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
1405                                                         (long) candidates[ i ].sr_err );
1406                                                 if ( candidates[ i ].sr_err == LDAP_SUCCESS ) {
1407                                                         Debug( LDAP_DEBUG_TRACE, "%s.\n", buf, 0, 0 );
1408         
1409                                                 } else {
1410                                                         Debug( LDAP_DEBUG_ANY, "%s (%s).\n",
1411                                                                 buf, ldap_err2string( candidates[ i ].sr_err ), 0 );
1412                                                 }
1413                                         }
1414         
1415                                         switch ( sres ) {
1416                                         case LDAP_NO_SUCH_OBJECT:
1417                                                 /* is_ok is touched any time a valid
1418                                                  * (even intermediate) result is
1419                                                  * returned; as a consequence, if
1420                                                  * a candidate returns noSuchObject
1421                                                  * it is ignored and the candidate
1422                                                  * is simply demoted. */
1423                                                 if ( is_ok ) {
1424                                                         sres = LDAP_SUCCESS;
1425                                                 }
1426                                                 break;
1427         
1428                                         case LDAP_SUCCESS:
1429                                         case LDAP_REFERRAL:
1430                                                 is_ok++;
1431                                                 break;
1432         
1433                                         case LDAP_SIZELIMIT_EXCEEDED:
1434                                                 /* if a target returned sizelimitExceeded
1435                                                  * and the entry count is equal to the
1436                                                  * proxy's limit, the target would have
1437                                                  * returned more, and the error must be
1438                                                  * propagated to the client; otherwise,
1439                                                  * the target enforced a limit lower
1440                                                  * than what requested by the proxy;
1441                                                  * ignore it */
1442                                                 candidates[ i ].sr_err = rs->sr_err;
1443                                                 if ( rs->sr_nentries == op->ors_slimit
1444                                                         || META_BACK_ONERR_STOP( mi ) )
1445                                                 {
1446                                                         const char *save_text = rs->sr_text;
1447                                                         savepriv = op->o_private;
1448                                                         op->o_private = (void *)i;
1449                                                         rs->sr_text = candidates[ i ].sr_text;
1450                                                         send_ldap_result( op, rs );
1451                                                         rs->sr_text = save_text;
1452                                                         op->o_private = savepriv;
1453                                                         ldap_msgfree( res );
1454                                                         res = NULL;
1455                                                         goto finish;
1456                                                 }
1457                                                 break;
1458         
1459                                         default:
1460                                                 candidates[ i ].sr_err = rs->sr_err;
1461                                                 if ( META_BACK_ONERR_STOP( mi ) ) {
1462                                                         const char *save_text = rs->sr_text;
1463                                                         savepriv = op->o_private;
1464                                                         op->o_private = (void *)i;
1465                                                         rs->sr_text = candidates[ i ].sr_text;
1466                                                         send_ldap_result( op, rs );
1467                                                         rs->sr_text = save_text;
1468                                                         op->o_private = savepriv;
1469                                                         ldap_msgfree( res );
1470                                                         res = NULL;
1471                                                         goto finish;
1472                                                 }
1473                                                 break;
1474                                         }
1475         
1476                                         last = i;
1477                                         rc = 0;
1478         
1479                                         /*
1480                                          * When no candidates are left,
1481                                          * the outer cycle finishes
1482                                          */
1483                                         assert( ncandidates > 0 );
1484                                         --ncandidates;
1485
1486                                 } else if ( rc == LDAP_RES_BIND ) {
1487                                         meta_search_candidate_t retcode;
1488         
1489                                         retcode = meta_search_dobind_result( op, rs, &mc, i, candidates, msg );
1490                                         if ( retcode == META_SEARCH_CANDIDATE ) {
1491                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1492                                                 retcode = meta_back_search_start( op, rs, &dc, &mc, i, candidates );
1493                                         }
1494         
1495                                         switch ( retcode ) {
1496                                         case META_SEARCH_CANDIDATE:
1497                                                 break;
1498         
1499                                                 /* means that failed but onerr == continue */
1500                                         case META_SEARCH_NOT_CANDIDATE:
1501                                         case META_SEARCH_ERR:
1502                                                 candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1503                                                 assert( ncandidates > 0 );
1504                                                 --ncandidates;
1505         
1506                                                 candidates[ i ].sr_err = rs->sr_err;
1507                                                 if ( META_BACK_ONERR_STOP( mi ) ) {
1508                                                         savepriv = op->o_private;
1509                                                         op->o_private = (void *)i;
1510                                                         send_ldap_result( op, rs );
1511                                                         op->o_private = savepriv;
1512                                                         ldap_msgfree( res );
1513                                                         res = NULL;
1514                                                         goto finish;
1515                                                 }
1516                                                 goto free_message;
1517         
1518                                         default:
1519                                                 assert( 0 );
1520                                                 break;
1521                                         }
1522         
1523                                 } else {
1524                                         Debug( LDAP_DEBUG_ANY,
1525                                                 "%s meta_back_search[%ld]: "
1526                                                 "unrecognized response message tag=%d\n",
1527                                                 op->o_log_prefix,
1528                                                 i, rc );
1529                                 
1530                                         ldap_msgfree( res );
1531                                         res = NULL;
1532                                         goto really_bad;
1533                                 }
1534                         }
1535
1536 free_message:;
1537                         ldap_msgfree( res );
1538                         res = NULL;
1539                 }
1540
1541                 /* check for abandon */
1542                 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( mc ) ) {
1543                         for ( i = 0; i < mi->mi_ntargets; i++ ) {
1544                                 if ( candidates[ i ].sr_msgid >= 0
1545                                         || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1546                                 {
1547                                         if ( META_IS_BINDING( &candidates[ i ] )
1548                                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1549                                         {
1550                                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1551                                                 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1552                                                         || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1553                                                 {
1554                                                         /* if still binding, destroy */
1555
1556 #ifdef DEBUG_205
1557                                                         char buf[ SLAP_TEXT_BUFLEN ];
1558
1559                                                         snprintf( buf, sizeof( buf), "%s meta_back_search(abandon) "
1560                                                                 "ldap_unbind_ext[%ld] mc=%p ld=%p",
1561                                                                 op->o_log_prefix, i, (void *)mc,
1562                                                                 (void *)mc->mc_conns[i].msc_ld );
1563
1564                                                         Debug( LDAP_DEBUG_ANY, "### %s\n", buf, 0, 0 );
1565 #endif /* DEBUG_205 */
1566
1567                                                         meta_clear_one_candidate( op, mc, i );
1568                                                 }
1569                                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1570                                                 META_BINDING_CLEAR( &candidates[ i ] );
1571                                                 
1572                                         } else {
1573                                                 (void)meta_back_cancel( mc, op, rs,
1574                                                         candidates[ i ].sr_msgid, i,
1575                                                         LDAP_BACK_DONTSEND );
1576                                         }
1577
1578                                         candidates[ i ].sr_msgid = META_MSGID_IGNORE;
1579                                         assert( ncandidates > 0 );
1580                                         --ncandidates;
1581                                 }
1582                         }
1583
1584                         if ( op->o_abandon ) {
1585                                 rc = SLAPD_ABANDON;
1586                         }
1587
1588                         /* let send_ldap_result play cleanup handlers (ITS#4645) */
1589                         break;
1590                 }
1591
1592                 /* if no entry was found during this loop,
1593                  * set a minimal timeout */
1594                 if ( ncandidates > 0 && gotit == 0 ) {
1595                         if ( save_tv.tv_sec == 0 && save_tv.tv_usec == 0 ) {
1596                                 save_tv.tv_usec = LDAP_BACK_RESULT_UTIMEOUT/initial_candidates;
1597
1598                                 /* arbitrarily limit to something between 1 and 2 minutes */
1599                         } else if ( ( stoptime == -1 && save_tv.tv_sec < 60 )
1600                                 || save_tv.tv_sec < ( stoptime - slap_get_time() ) / ( 2 * ncandidates ) )
1601                         {
1602                                 /* double the timeout */
1603                                 lutil_timermul( &save_tv, 2, &save_tv );
1604                         }
1605
1606                         if ( alreadybound == 0 ) {
1607                                 tv = save_tv;
1608                                 (void)select( 0, NULL, NULL, NULL, &tv );
1609
1610                         } else {
1611                                 ldap_pvt_thread_yield();
1612                         }
1613                 }
1614         }
1615
1616         if ( rc == -1 ) {
1617                 /*
1618                  * FIXME: need a better strategy to handle errors
1619                  */
1620                 if ( mc ) {
1621                         rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE,
1622                                 -1, stoptime != -1 ? (stoptime - slap_get_time()) : 0,
1623                                 LDAP_BACK_SENDERR );
1624                 } else {
1625                         rc = rs->sr_err;
1626                 }
1627                 goto finish;
1628         }
1629
1630         /*
1631          * Rewrite the matched portion of the search base, if required
1632          * 
1633          * FIXME: only the last one gets caught!
1634          */
1635         savepriv = op->o_private;
1636         op->o_private = (void *)(long)mi->mi_ntargets;
1637         if ( candidate_match > 0 ) {
1638                 struct berval   pmatched = BER_BVNULL;
1639
1640                 /* we use the first one */
1641                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
1642                         if ( META_IS_CANDIDATE( &candidates[ i ] )
1643                                         && candidates[ i ].sr_matched != NULL )
1644                         {
1645                                 struct berval   bv, pbv;
1646                                 int             rc;
1647
1648                                 /* if we got success, and this target
1649                                  * returned noSuchObject, and its suffix
1650                                  * is a superior of the searchBase,
1651                                  * ignore the matchedDN */
1652                                 if ( sres == LDAP_SUCCESS
1653                                         && candidates[ i ].sr_err == LDAP_NO_SUCH_OBJECT
1654                                         && op->o_req_ndn.bv_len > mi->mi_targets[ i ]->mt_nsuffix.bv_len )
1655                                 {
1656                                         free( (char *)candidates[ i ].sr_matched );
1657                                         candidates[ i ].sr_matched = NULL;
1658                                         continue;
1659                                 }
1660
1661                                 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
1662                                 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
1663
1664                                 if ( rc == LDAP_SUCCESS ) {
1665
1666                                         /* NOTE: if they all are superiors
1667                                          * of the baseDN, the shorter is also 
1668                                          * superior of the longer... */
1669                                         if ( pbv.bv_len > pmatched.bv_len ) {
1670                                                 if ( !BER_BVISNULL( &pmatched ) ) {
1671                                                         op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
1672                                                 }
1673                                                 pmatched = pbv;
1674                                                 op->o_private = (void *)i;
1675
1676                                         } else {
1677                                                 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
1678                                         }
1679                                 }
1680
1681                                 if ( candidates[ i ].sr_matched != NULL ) {
1682                                         free( (char *)candidates[ i ].sr_matched );
1683                                         candidates[ i ].sr_matched = NULL;
1684                                 }
1685                         }
1686                 }
1687
1688                 if ( !BER_BVISNULL( &pmatched ) ) {
1689                         matched = pmatched.bv_val;
1690                 }
1691
1692         } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
1693                 matched = op->o_bd->be_suffix[ 0 ].bv_val;
1694         }
1695
1696         /*
1697          * In case we returned at least one entry, we return LDAP_SUCCESS
1698          * otherwise, the latter error code we got
1699          */
1700
1701         if ( sres == LDAP_SUCCESS ) {
1702                 if ( rs->sr_v2ref ) {
1703                         sres = LDAP_REFERRAL;
1704                 }
1705
1706                 if ( META_BACK_ONERR_REPORT( mi ) ) {
1707                         /*
1708                          * Report errors, if any
1709                          *
1710                          * FIXME: we should handle error codes and return the more 
1711                          * important/reasonable
1712                          */
1713                         for ( i = 0; i < mi->mi_ntargets; i++ ) {
1714                                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1715                                         continue;
1716                                 }
1717
1718                                 if ( candidates[ i ].sr_err != LDAP_SUCCESS
1719                                         && candidates[ i ].sr_err != LDAP_NO_SUCH_OBJECT )
1720                                 {
1721                                         sres = candidates[ i ].sr_err;
1722                                         break;
1723                                 }
1724                         }
1725                 }
1726         }
1727
1728         rs->sr_err = sres;
1729         rs->sr_matched = matched;
1730         rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
1731         send_ldap_result( op, rs );
1732         op->o_private = savepriv;
1733         rs->sr_matched = NULL;
1734         rs->sr_ref = NULL;
1735
1736 finish:;
1737         if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
1738                 op->o_tmpfree( matched, op->o_tmpmemctx );
1739         }
1740
1741         if ( rs->sr_v2ref ) {
1742                 ber_bvarray_free_x( rs->sr_v2ref, op->o_tmpmemctx );
1743         }
1744
1745         for ( i = 0; i < mi->mi_ntargets; i++ ) {
1746                 if ( !META_IS_CANDIDATE( &candidates[ i ] ) ) {
1747                         continue;
1748                 }
1749
1750                 if ( mc ) {
1751                         if ( META_IS_BINDING( &candidates[ i ] )
1752                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1753                         {
1754                                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1755                                 if ( LDAP_BACK_CONN_BINDING( &mc->mc_conns[ i ] )
1756                                         || candidates[ i ].sr_msgid == META_MSGID_CONNECTING )
1757                                 {
1758                                         assert( candidates[ i ].sr_msgid >= 0
1759                                                 || candidates[ i ].sr_msgid == META_MSGID_CONNECTING );
1760                                         assert( mc->mc_conns[ i ].msc_ld != NULL );
1761
1762 #ifdef DEBUG_205
1763                                         Debug( LDAP_DEBUG_ANY, "### %s meta_back_search(cleanup) "
1764                                                 "ldap_unbind_ext[%ld] ld=%p\n",
1765                                                 op->o_log_prefix, i, (void *)mc->mc_conns[i].msc_ld );
1766 #endif /* DEBUG_205 */
1767
1768                                         /* if still binding, destroy */
1769                                         meta_clear_one_candidate( op, mc, i );
1770                                 }
1771                                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1772                                 META_BINDING_CLEAR( &candidates[ i ] );
1773
1774                         } else if ( candidates[ i ].sr_msgid >= 0 ) {
1775                                 (void)meta_back_cancel( mc, op, rs,
1776                                         candidates[ i ].sr_msgid, i,
1777                                         LDAP_BACK_DONTSEND );
1778                         }
1779                 }
1780
1781                 if ( candidates[ i ].sr_matched ) {
1782                         free( (char *)candidates[ i ].sr_matched );
1783                         candidates[ i ].sr_matched = NULL;
1784                 }
1785
1786                 if ( candidates[ i ].sr_text ) {
1787                         ldap_memfree( (char *)candidates[ i ].sr_text );
1788                         candidates[ i ].sr_text = NULL;
1789                 }
1790
1791                 if ( candidates[ i ].sr_ref ) {
1792                         ber_bvarray_free( candidates[ i ].sr_ref );
1793                         candidates[ i ].sr_ref = NULL;
1794                 }
1795
1796                 if ( candidates[ i ].sr_ctrls ) {
1797                         ldap_controls_free( candidates[ i ].sr_ctrls );
1798                         candidates[ i ].sr_ctrls = NULL;
1799                 }
1800
1801                 if ( META_BACK_TGT_QUARANTINE( mi->mi_targets[ i ] ) ) {
1802                         meta_back_quarantine( op, &candidates[ i ], i );
1803                 }
1804
1805                 /* only in case of timelimit exceeded, if the timelimit exceeded because
1806                  * one contacted target never responded, invalidate the connection
1807                  * NOTE: should we quarantine the target as well?  right now, the connection
1808                  * is invalidated; the next time it will be recreated and the target
1809                  * will be quarantined if it cannot be contacted */
1810                 if ( mi->mi_idle_timeout != 0
1811                         && rs->sr_err == LDAP_TIMELIMIT_EXCEEDED
1812                         && op->o_time > mc->mc_conns[ i ].msc_time )
1813                 {
1814                         /* don't let anyone else use this expired connection */
1815                         do_taint++;
1816                 }
1817         }
1818
1819         if ( mc ) {
1820                 ldap_pvt_thread_mutex_lock( &mi->mi_conninfo.lai_mutex );
1821                 if ( do_taint ) {
1822                         LDAP_BACK_CONN_TAINTED_SET( mc );
1823                 }
1824                 meta_back_release_conn_lock( mi, mc, 0 );
1825                 ldap_pvt_thread_mutex_unlock( &mi->mi_conninfo.lai_mutex );
1826         }
1827
1828         return rs->sr_err;
1829 }
1830
1831 static int
1832 meta_send_entry(
1833         Operation       *op,
1834         SlapReply       *rs,
1835         metaconn_t      *mc,
1836         int             target,
1837         LDAPMessage     *e )
1838 {
1839         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
1840         struct berval           a, mapped;
1841         int                     check_duplicate_attrs = 0;
1842         int                     check_sorted_attrs = 0;
1843         Entry                   ent = { 0 };
1844         BerElement              ber = *e->lm_ber;
1845         Attribute               *attr, **attrp;
1846         struct berval           bdn,
1847                                 dn = BER_BVNULL;
1848         const char              *text;
1849         dncookie                dc;
1850         ber_len_t               len;
1851         int                     rc;
1852
1853         if ( ber_scanf( &ber, "l{", &len ) == LBER_ERROR ) {
1854                 return LDAP_DECODING_ERROR;
1855         }
1856
1857         if ( ber_set_option( &ber, LBER_OPT_REMAINING_BYTES, &len ) != LBER_OPT_SUCCESS ) {
1858                 return LDAP_OTHER;
1859         }
1860
1861         if ( ber_scanf( &ber, "m{", &bdn ) == LBER_ERROR ) {
1862                 return LDAP_DECODING_ERROR;
1863         }
1864
1865         /*
1866          * Rewrite the dn of the result, if needed
1867          */
1868         dc.target = mi->mi_targets[ target ];
1869         dc.conn = op->o_conn;
1870         dc.rs = rs;
1871         dc.ctx = "searchResult";
1872
1873         rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
1874         if ( rs->sr_err != LDAP_SUCCESS) {
1875                 return rs->sr_err;
1876         }
1877
1878         /*
1879          * Note: this may fail if the target host(s) schema differs
1880          * from the one known to the meta, and a DN with unknown
1881          * attributes is returned.
1882          * 
1883          * FIXME: should we log anything, or delegate to dnNormalize?
1884          */
1885         rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
1886                 op->o_tmpmemctx );
1887         if ( dn.bv_val != bdn.bv_val ) {
1888                 free( dn.bv_val );
1889         }
1890         BER_BVZERO( &dn );
1891
1892         if ( rc != LDAP_SUCCESS ) {
1893                 Debug( LDAP_DEBUG_ANY,
1894                         "%s meta_send_entry(\"%s\"): "
1895                         "invalid DN syntax\n",
1896                         op->o_log_prefix, ent.e_name.bv_val, 0 );
1897                 rc = LDAP_INVALID_DN_SYNTAX;
1898                 goto done;
1899         }
1900
1901         /*
1902          * cache dn
1903          */
1904         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
1905                 ( void )meta_dncache_update_entry( &mi->mi_cache,
1906                                 &ent.e_nname, target );
1907         }
1908
1909         attrp = &ent.e_attrs;
1910
1911         dc.ctx = "searchAttrDN";
1912         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
1913                 int                             last = 0;
1914                 slap_syntax_validate_func       *validate;
1915                 slap_syntax_transform_func      *pretty;
1916
1917                 if ( ber_pvt_ber_remaining( &ber ) < 0 ) {
1918                         Debug( LDAP_DEBUG_ANY,
1919                                 "%s meta_send_entry(\"%s\"): "
1920                                 "unable to parse attr \"%s\".\n",
1921                                 op->o_log_prefix, ent.e_name.bv_val, a.bv_val );
1922                                 
1923                         rc = LDAP_OTHER;
1924                         goto done;
1925                 }
1926
1927                 if ( ber_pvt_ber_remaining( &ber ) == 0 ) {
1928                         break;
1929                 }
1930
1931                 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_at, 
1932                                 &a, &mapped, BACKLDAP_REMAP );
1933                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
1934                         ( void )ber_scanf( &ber, "x" /* [W] */ );
1935                         continue;
1936                 }
1937                 if ( mapped.bv_val != a.bv_val ) {
1938                         /* will need to check for duplicate attrs */
1939                         check_duplicate_attrs++;
1940                 }
1941                 attr = attr_alloc( NULL );
1942                 if ( attr == NULL ) {
1943                         rc = LDAP_OTHER;
1944                         goto done;
1945                 }
1946                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
1947                                 != LDAP_SUCCESS) {
1948                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
1949                                 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
1950                         {
1951                                 char    buf[ SLAP_TEXT_BUFLEN ];
1952
1953                                 snprintf( buf, sizeof( buf ),
1954                                         "%s meta_send_entry(\"%s\"): "
1955                                         "slap_bv2undef_ad(%s): %s\n",
1956                                         op->o_log_prefix, ent.e_name.bv_val,
1957                                         mapped.bv_val, text );
1958
1959                                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
1960                                 ( void )ber_scanf( &ber, "x" /* [W] */ );
1961                                 attr_free( attr );
1962                                 continue;
1963                         }
1964                 }
1965
1966                 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
1967                         check_sorted_attrs = 1;
1968
1969                 /* no subschemaSubentry */
1970                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
1971                         || attr->a_desc == slap_schema.si_ad_entryDN )
1972                 {
1973
1974                         /* 
1975                          * We eat target's subschemaSubentry because
1976                          * a search for this value is likely not
1977                          * to resolve to the appropriate backend;
1978                          * later, the local subschemaSubentry is
1979                          * added.
1980                          *
1981                          * We also eat entryDN because the frontend
1982                          * will reattach it without checking if already
1983                          * present...
1984                          */
1985                         ( void )ber_scanf( &ber, "x" /* [W] */ );
1986                         attr_free(attr);
1987                         continue;
1988                 }
1989
1990                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR 
1991                                 || attr->a_vals == NULL )
1992                 {
1993                         attr->a_vals = (struct berval *)&slap_dummy_bv;
1994
1995                 } else {
1996                         for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
1997                                 ;
1998                 }
1999                 attr->a_numvals = last;
2000
2001                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
2002                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
2003
2004                 if ( !validate && !pretty ) {
2005                         attr_free( attr );
2006                         goto next_attr;
2007                 }
2008
2009                 if ( attr->a_desc == slap_schema.si_ad_objectClass
2010                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
2011                 {
2012                         struct berval   *bv;
2013
2014                         for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
2015                                 ObjectClass *oc;
2016
2017                                 ldap_back_map( &mi->mi_targets[ target ]->mt_rwmap.rwm_oc,
2018                                                 bv, &mapped, BACKLDAP_REMAP );
2019                                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
2020 remove_oc:;
2021                                         free( bv->bv_val );
2022                                         BER_BVZERO( bv );
2023                                         if ( --last < 0 ) {
2024                                                 break;
2025                                         }
2026                                         *bv = attr->a_vals[ last ];
2027                                         BER_BVZERO( &attr->a_vals[ last ] );
2028                                         bv--;
2029
2030                                 } else if ( mapped.bv_val != bv->bv_val ) {
2031                                         int     i;
2032
2033                                         for ( i = 0; !BER_BVISNULL( &attr->a_vals[ i ] ); i++ ) {
2034                                                 if ( &attr->a_vals[ i ] == bv ) {
2035                                                         continue;
2036                                                 }
2037
2038                                                 if ( ber_bvstrcasecmp( &mapped, &attr->a_vals[ i ] ) == 0 ) {
2039                                                         break;
2040                                                 }
2041                                         }
2042
2043                                         if ( !BER_BVISNULL( &attr->a_vals[ i ] ) ) {
2044                                                 goto remove_oc;
2045                                         }
2046
2047                                         ber_bvreplace( bv, &mapped );
2048
2049                                 } else if ( ( oc = oc_bvfind_undef( bv ) ) == NULL ) {
2050                                         goto remove_oc;
2051
2052                                 } else {
2053                                         ber_bvreplace( bv, &oc->soc_cname );
2054                                 }
2055                         }
2056                 /*
2057                  * It is necessary to try to rewrite attributes with
2058                  * dn syntax because they might be used in ACLs as
2059                  * members of groups; since ACLs are applied to the
2060                  * rewritten stuff, no dn-based subecj clause could
2061                  * be used at the ldap backend side (see
2062                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
2063                  * The problem can be overcome by moving the dn-based
2064                  * ACLs to the target directory server, and letting
2065                  * everything pass thru the ldap backend.
2066                  */
2067                 } else {
2068                         int     i;
2069
2070                         if ( attr->a_desc->ad_type->sat_syntax ==
2071                                 slap_schema.si_syn_distinguishedName )
2072                         {
2073                                 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
2074
2075                         } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
2076                                 ldap_back_referral_result_rewrite( &dc, attr->a_vals, NULL );
2077
2078                         }
2079
2080                         for ( i = 0; i < last; i++ ) {
2081                                 struct berval   pval;
2082                                 int             rc;
2083
2084                                 if ( pretty ) {
2085                                         rc = ordered_value_pretty( attr->a_desc,
2086                                                 &attr->a_vals[i], &pval, NULL );
2087
2088                                 } else {
2089                                         rc = ordered_value_validate( attr->a_desc,
2090                                                 &attr->a_vals[i], 0 );
2091                                 }
2092
2093                                 if ( rc ) {
2094                                         LBER_FREE( attr->a_vals[i].bv_val );
2095                                         if ( --last == i ) {
2096                                                 BER_BVZERO( &attr->a_vals[ i ] );
2097                                                 break;
2098                                         }
2099                                         attr->a_vals[i] = attr->a_vals[last];
2100                                         BER_BVZERO( &attr->a_vals[last] );
2101                                         i--;
2102                                         continue;
2103                                 }
2104
2105                                 if ( pretty ) {
2106                                         LBER_FREE( attr->a_vals[i].bv_val );
2107                                         attr->a_vals[i] = pval;
2108                                 }
2109                         }
2110
2111                         if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
2112                                 attr_free( attr );
2113                                 goto next_attr;
2114                         }
2115                 }
2116
2117                 if ( last && attr->a_desc->ad_type->sat_equality &&
2118                         attr->a_desc->ad_type->sat_equality->smr_normalize )
2119                 {
2120                         int i;
2121
2122                         attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
2123                         for ( i = 0; i<last; i++ ) {
2124                                 /* if normalizer fails, drop this value */
2125                                 if ( ordered_value_normalize(
2126                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2127                                         attr->a_desc,
2128                                         attr->a_desc->ad_type->sat_equality,
2129                                         &attr->a_vals[i], &attr->a_nvals[i],
2130                                         NULL )) {
2131                                         LBER_FREE( attr->a_vals[i].bv_val );
2132                                         if ( --last == i ) {
2133                                                 BER_BVZERO( &attr->a_vals[ i ] );
2134                                                 break;
2135                                         }
2136                                         attr->a_vals[i] = attr->a_vals[last];
2137                                         BER_BVZERO( &attr->a_vals[last] );
2138                                         i--;
2139                                 }
2140                         }
2141                         BER_BVZERO( &attr->a_nvals[i] );
2142                         if ( last == 0 ) {
2143                                 attr_free( attr );
2144                                 goto next_attr;
2145                         }
2146
2147                 } else {
2148                         attr->a_nvals = attr->a_vals;
2149                 }
2150
2151                 attr->a_numvals = last;
2152                 *attrp = attr;
2153                 attrp = &attr->a_next;
2154 next_attr:;
2155         }
2156
2157         /* only check if some mapping occurred */
2158         if ( check_duplicate_attrs ) {
2159                 Attribute       **ap;
2160
2161                 for ( ap = &ent.e_attrs; *ap != NULL; ap = &(*ap)->a_next ) {
2162                         Attribute       **tap;
2163
2164                         for ( tap = &(*ap)->a_next; *tap != NULL; ) {
2165                                 if ( (*tap)->a_desc == (*ap)->a_desc ) {
2166                                         Entry           e = { 0 };
2167                                         Modification    mod = { 0 };
2168                                         const char      *text = NULL;
2169                                         char            textbuf[ SLAP_TEXT_BUFLEN ];
2170                                         Attribute       *next = (*tap)->a_next;
2171
2172                                         BER_BVSTR( &e.e_name, "" );
2173                                         BER_BVSTR( &e.e_nname, "" );
2174                                         e.e_attrs = *ap;
2175                                         mod.sm_op = LDAP_MOD_ADD;
2176                                         mod.sm_desc = (*ap)->a_desc;
2177                                         mod.sm_type = mod.sm_desc->ad_cname;
2178                                         mod.sm_numvals = (*ap)->a_numvals;
2179                                         mod.sm_values = (*tap)->a_vals;
2180                                         if ( (*tap)->a_nvals != (*tap)->a_vals ) {
2181                                                 mod.sm_nvalues = (*tap)->a_nvals;
2182                                         }
2183
2184                                         (void)modify_add_values( &e, &mod,
2185                                                 /* permissive */ 1,
2186                                                 &text, textbuf, sizeof( textbuf ) );
2187
2188                                         /* should not insert new attrs! */
2189                                         assert( e.e_attrs == *ap );
2190
2191                                         attr_free( *tap );
2192                                         *tap = next;
2193
2194                                 } else {
2195                                         tap = &(*tap)->a_next;
2196                                 }
2197                         }
2198                 }
2199         }
2200
2201         /* Check for sorted attributes */
2202         if ( check_sorted_attrs ) {
2203                 for ( attr = ent.e_attrs; attr; attr = attr->a_next ) {
2204                         if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
2205                                 while ( attr->a_numvals > 1 ) {
2206                                         int i;
2207                                         int rc = slap_sort_vals( (Modifications *)attr, &text, &i, op->o_tmpmemctx );
2208                                         if ( rc != LDAP_TYPE_OR_VALUE_EXISTS )
2209                                                 break;
2210
2211                                         /* Strip duplicate values */
2212                                         if ( attr->a_nvals != attr->a_vals )
2213                                                 LBER_FREE( attr->a_nvals[i].bv_val );
2214                                         LBER_FREE( attr->a_vals[i].bv_val );
2215                                         attr->a_numvals--;
2216                                         if ( (unsigned)i < attr->a_numvals ) {
2217                                                 attr->a_vals[i] = attr->a_vals[attr->a_numvals];
2218                                                 if ( attr->a_nvals != attr->a_vals )
2219                                                         attr->a_nvals[i] = attr->a_nvals[attr->a_numvals];
2220                                         }
2221                                         BER_BVZERO(&attr->a_vals[attr->a_numvals]);
2222                                         if ( attr->a_nvals != attr->a_vals )
2223                                                 BER_BVZERO(&attr->a_nvals[attr->a_numvals]);
2224                                 }
2225                                 attr->a_flags |= SLAP_ATTR_SORTED_VALS;
2226                         }
2227                 }
2228         }
2229
2230         ldap_get_entry_controls( mc->mc_conns[target].msc_ld,
2231                 e, &rs->sr_ctrls );
2232         rs->sr_entry = &ent;
2233         rs->sr_attrs = op->ors_attrs;
2234         rs->sr_operational_attrs = NULL;
2235         rs->sr_flags = mi->mi_targets[ target ]->mt_rep_flags;
2236         rs->sr_err = LDAP_SUCCESS;
2237         rc = send_search_entry( op, rs );
2238         switch ( rc ) {
2239         case LDAP_UNAVAILABLE:
2240                 rc = LDAP_OTHER;
2241                 break;
2242         }
2243
2244 done:;
2245         rs->sr_entry = NULL;
2246         rs->sr_attrs = NULL;
2247         if ( rs->sr_ctrls != NULL ) {
2248                 ldap_controls_free( rs->sr_ctrls );
2249                 rs->sr_ctrls = NULL;
2250         }
2251         if ( !BER_BVISNULL( &ent.e_name ) ) {
2252                 free( ent.e_name.bv_val );
2253                 BER_BVZERO( &ent.e_name );
2254         }
2255         if ( !BER_BVISNULL( &ent.e_nname ) ) {
2256                 free( ent.e_nname.bv_val );
2257                 BER_BVZERO( &ent.e_nname );
2258         }
2259         entry_clean( &ent );
2260
2261         return rc;
2262 }
2263