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