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