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