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