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