]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/search.c
fix size/time limit (ITS#4145)
[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-2005 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 "slap.h"
32 #include "../back-ldap/back-ldap.h"
33 #include "back-meta.h"
34 #undef ldap_debug       /* silence a warning in ldap-int.h */
35 #include "ldap_log.h"
36 #include "../../../libraries/libldap/ldap-int.h"
37
38 static int
39 meta_send_entry(
40         Operation       *op,
41         SlapReply       *rs,
42         metaconn_t      *mc,
43         int             i,
44         LDAPMessage     *e );
45
46 static int
47 meta_back_search_start(
48         Operation               *op,
49         SlapReply               *rs,
50         dncookie                *dc,
51         metasingleconn_t        *msc,
52         int                     candidate,
53         SlapReply               *candidates
54 )
55 {
56         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
57         struct berval   realbase = op->o_req_dn;
58         int             realscope = op->ors_scope;
59         ber_len_t       suffixlen = 0;
60         struct berval   mbase = BER_BVNULL; 
61         struct berval   mfilter = BER_BVNULL;
62         char            **mapped_attrs = NULL;
63         int             rc;
64         struct timeval  tv, *tvp = NULL;
65
66         /* should we check return values? */
67         if ( op->ors_deref != -1 ) {
68                 ldap_set_option( msc->msc_ld, LDAP_OPT_DEREF,
69                                 ( void * )&op->ors_deref );
70         }
71
72         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
73                 tv.tv_sec = op->ors_tlimit > 0 ? op->ors_tlimit : 1;
74                 tvp = &tv;
75         }
76
77         dc->target = &mi->mi_targets[ candidate ];
78
79         /*
80          * modifies the base according to the scope, if required
81          */
82         suffixlen = mi->mi_targets[ candidate ].mt_nsuffix.bv_len;
83         if ( suffixlen > op->o_req_ndn.bv_len ) {
84                 switch ( op->ors_scope ) {
85                 case LDAP_SCOPE_SUBTREE:
86                         /*
87                          * make the target suffix the new base
88                          * FIXME: this is very forgiving, because
89                          * "illegal" searchBases may be turned
90                          * into the suffix of the target; however,
91                          * the requested searchBase already passed
92                          * thru the candidate analyzer...
93                          */
94                         if ( dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix,
95                                         &op->o_req_ndn ) )
96                         {
97                                 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
98 #ifdef LDAP_SCOPE_SUBORDINATE
99                                 if ( mi->mi_targets[ candidate ].mt_scope == LDAP_SCOPE_SUBORDINATE ) {
100                                         realscope = LDAP_SCOPE_SUBORDINATE;
101                                 }
102 #endif /* LDAP_SCOPE_SUBORDINATE */
103
104                         } else {
105                                 /*
106                                  * this target is no longer candidate
107                                  */
108                                 return 0;
109                         }
110                         break;
111
112 #ifdef LDAP_SCOPE_SUBORDINATE
113                 case LDAP_SCOPE_SUBORDINATE:
114 #endif /* LDAP_SCOPE_SUBORDINATE */
115                 case LDAP_SCOPE_ONELEVEL:
116                 {
117                         struct berval   rdn = mi->mi_targets[ candidate ].mt_nsuffix;
118                         rdn.bv_len -= op->o_req_ndn.bv_len + STRLENOF( "," );
119                         if ( dnIsOneLevelRDN( &rdn )
120                                         && dnIsSuffix( &mi->mi_targets[ candidate ].mt_nsuffix, &op->o_req_ndn ) )
121                         {
122                                 /*
123                                  * if there is exactly one level,
124                                  * make the target suffix the new
125                                  * base, and make scope "base"
126                                  */
127                                 realbase = mi->mi_targets[ candidate ].mt_nsuffix;
128 #ifdef LDAP_SCOPE_SUBORDINATE
129                                 if ( op->ors_scope == LDAP_SCOPE_SUBORDINATE ) {
130                                         if ( mi->mi_targets[ candidate ].mt_scope == LDAP_SCOPE_SUBORDINATE ) {
131                                                 realscope = LDAP_SCOPE_SUBORDINATE;
132                                         } else {
133                                                 realscope = LDAP_SCOPE_SUBTREE;
134                                         }
135                                 } else
136 #endif /* LDAP_SCOPE_SUBORDINATE */
137                                 {
138                                         realscope = LDAP_SCOPE_BASE;
139                                 }
140                                 break;
141                         } /* else continue with the next case */
142                 }
143
144                 case LDAP_SCOPE_BASE:
145                         /*
146                          * this target is no longer candidate
147                          */
148                         return 0;
149                 }
150         }
151
152         /*
153          * Rewrite the search base, if required
154          */
155         dc->ctx = "searchBase";
156         switch ( ldap_back_dn_massage( dc, &realbase, &mbase ) ) {
157         default:
158                 break;
159
160         case REWRITE_REGEXEC_UNWILLING:
161                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
162                 rs->sr_text = "Operation not allowed";
163                 send_ldap_result( op, rs );
164                 return -1;
165
166         case REWRITE_REGEXEC_ERR:
167
168                 /*
169                  * this target is no longer candidate
170                  */
171                 return 0;
172         }
173
174         /*
175          * Maps filter
176          */
177         rc = ldap_back_filter_map_rewrite( dc, op->ors_filter,
178                         &mfilter, BACKLDAP_MAP );
179         switch ( rc ) {
180         case LDAP_SUCCESS:
181                 break;
182
183         case LDAP_COMPARE_FALSE:
184         default:
185                 /*
186                  * this target is no longer candidate
187                  */
188                 rc = 0;
189                 goto done;
190         }
191
192         /*
193          * Maps required attributes
194          */
195         rc = ldap_back_map_attrs( &mi->mi_targets[ candidate ].mt_rwmap.rwm_at,
196                         op->ors_attrs, BACKLDAP_MAP, &mapped_attrs );
197         if ( rc != LDAP_SUCCESS ) {
198                 /*
199                  * this target is no longer candidate
200                  */
201                 rc = 0;
202                 goto done;
203         }
204
205         /*
206          * Starts the search
207          */
208         rc = ldap_search_ext( msc->msc_ld,
209                         mbase.bv_val, realscope, mfilter.bv_val,
210                         mapped_attrs, op->ors_attrsonly,
211                         op->o_ctrls, NULL, tvp, op->ors_slimit,
212                         &candidates[ candidate ].sr_msgid ); 
213         if ( rc == LDAP_SUCCESS ) {
214                 rc = 1;
215
216         } else {
217                 candidates[ candidate ].sr_msgid = -1;
218                 rc = 0;
219         }
220
221 done:;
222         if ( mapped_attrs ) {
223                 free( mapped_attrs );
224         }
225         if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
226                 free( mfilter.bv_val );
227         }
228         if ( mbase.bv_val != realbase.bv_val ) {
229                 free( mbase.bv_val );
230         }
231
232         return rc;
233 }
234
235 int
236 meta_back_search( Operation *op, SlapReply *rs )
237 {
238         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
239         metaconn_t      *mc;
240         struct timeval  tv = { 0, 0 };
241         time_t          stoptime;
242         LDAPMessage     *res = NULL, *e;
243         int             rc = 0, sres = LDAP_SUCCESS;
244         char            *matched = NULL;
245         int             last = 0, ncandidates = 0,
246                         initial_candidates = 0, candidate_match = 0;
247         long            i;
248         dncookie        dc;
249         int             is_ok = 0;
250         void            *savepriv;
251         SlapReply       *candidates = meta_back_candidates_get( op );
252
253         /*
254          * controls are set in ldap_back_dobind()
255          * 
256          * FIXME: in case of values return filter, we might want
257          * to map attrs and maybe rewrite value
258          */
259         mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_SENDERR );
260         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
261                 return rs->sr_err;
262         }
263
264         dc.conn = op->o_conn;
265         dc.rs = rs;
266
267         /*
268          * Inits searches
269          */
270         for ( i = 0; i < mi->mi_ntargets; i++ ) {
271                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
272
273                 candidates[ i ].sr_msgid = -1;
274                 candidates[ i ].sr_matched = NULL;
275                 candidates[ i ].sr_text = NULL;
276                 candidates[ i ].sr_ref = NULL;
277                 candidates[ i ].sr_ctrls = NULL;
278
279                 if ( candidates[ i ].sr_tag != META_CANDIDATE
280                         || candidates[ i ].sr_err != LDAP_SUCCESS )
281                 {
282                         continue;
283                 }
284
285                 switch ( meta_back_search_start( op, rs, &dc, msc, i, candidates ) )
286                 {
287                 case 0:
288                         break;
289
290                 case 1:
291                         ++ncandidates;
292                         break;
293
294                 case -1:
295                         rc = -1;
296                         goto finish;
297                 }
298         }
299
300         initial_candidates = ncandidates;
301
302 #if 0
303         {
304                 char    cnd[BUFSIZ];
305                 int     i;
306
307                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
308                         if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
309                                 cnd[ i ] = '*';
310                         } else {
311                                 cnd[ i ] = ' ';
312                         }
313                 }
314                 cnd[ i ] = '\0';
315
316                 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: ncandidates=%d "
317                         "cnd=\"%s\"\n", op->o_log_prefix, ncandidates, cnd );
318         }
319 #endif
320
321         if ( initial_candidates == 0 ) {
322                 /* NOTE: here we are not sending any matchedDN;
323                  * this is intended, because if the back-meta
324                  * is serving this search request, but no valid
325                  * candidate could be looked up, it means that
326                  * there is a hole in the mapping of the targets
327                  * and thus no knowledge of any remote superior
328                  * is available */
329                 Debug( LDAP_DEBUG_ANY, "%s meta_back_search: "
330                         "base=\"%s\" scope=%d: "
331                         "no candidate could be selected\n",
332                         op->o_log_prefix, op->o_req_dn.bv_val,
333                         op->ors_scope );
334
335                 /* FIXME: we're sending the first error we encounter;
336                  * maybe we should pick the worst... */
337                 rc = LDAP_NO_SUCH_OBJECT;
338                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
339                         if ( candidates[ i ].sr_tag == META_CANDIDATE
340                                 && candidates[ i ].sr_err != LDAP_SUCCESS )
341                         {
342                                 rc = candidates[ i ].sr_err;
343                                 break;
344                         }
345                 }
346
347                 send_ldap_error( op, rs, rc, NULL );
348
349                 goto finish;
350         }
351
352         /* We pull apart the ber result, stuff it into a slapd entry, and
353          * let send_search_entry stuff it back into ber format. Slow & ugly,
354          * but this is necessary for version matching, and for ACL processing.
355          */
356
357         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
358                 stoptime = op->o_time + op->ors_tlimit;
359         }
360
361         /*
362          * In case there are no candidates, no cycle takes place...
363          *
364          * FIXME: we might use a queue, to better balance the load 
365          * among the candidates
366          */
367         for ( rc = 0; ncandidates > 0; ) {
368                 int     gotit = 0, doabandon = 0;
369
370                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
371                         metasingleconn_t        *msc = &mc->mc_conns[ i ];
372
373                         if ( candidates[ i ].sr_msgid == -1 ) {
374                                 continue;
375                         }
376
377                         /* check for abandon */
378                         if ( op->o_abandon ) {
379                                 break;
380                         }
381                         
382                         /*
383                          * FIXME: handle time limit as well?
384                          * Note that target servers are likely 
385                          * to handle it, so at some time we'll
386                          * get a LDAP_TIMELIMIT_EXCEEDED from
387                          * one of them ...
388                          */
389                         rc = ldap_result( msc->msc_ld, candidates[ i ].sr_msgid,
390                                         0, &tv, &res );
391
392                         if ( rc == 0 ) {
393                                 /* FIXME: res should not need to be freed */
394                                 assert( res == NULL );
395
396                                 /* check time limit */
397                                 if ( op->ors_tlimit != SLAP_NO_LIMIT
398                                                 && slap_get_time() > stoptime )
399                                 {
400                                         doabandon = 1;
401                                         rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
402                                         savepriv = op->o_private;
403                                         op->o_private = (void *)i;
404                                         send_ldap_result( op, rs );
405                                         op->o_private = savepriv;
406                                         goto finish;
407                                 }
408
409                                 continue;
410
411                         } else if ( rc == -1 ) {
412 really_bad:;
413                                 /* something REALLY bad happened! */
414                                 ( void )meta_clear_unused_candidates( op, -1 );
415                                 rs->sr_err = LDAP_OTHER;
416                                 savepriv = op->o_private;
417                                 op->o_private = (void *)i;
418                                 send_ldap_result( op, rs );
419                                 op->o_private = savepriv;
420                                 
421                                 /* anything else needs be done? */
422
423                                 /* FIXME: res should not need to be freed */
424                                 assert( res == NULL );
425
426                                 goto finish;
427
428                         } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
429                                 if ( --op->ors_slimit == -1 ) {
430                                         ldap_msgfree( res );
431                                         res = NULL;
432
433                                         rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
434                                         savepriv = op->o_private;
435                                         op->o_private = (void *)i;
436                                         send_ldap_result( op, rs );
437                                         op->o_private = savepriv;
438                                         goto finish;
439                                 }
440
441                                 is_ok++;
442
443                                 e = ldap_first_entry( msc->msc_ld, res );
444                                 savepriv = op->o_private;
445                                 op->o_private = (void *)i;
446                                 meta_send_entry( op, rs, mc, i, e );
447                                 op->o_private = savepriv;
448
449                                 ldap_msgfree( res );
450                                 res = NULL;
451
452                                 gotit = 1;
453
454 #if 0
455                                 /*
456                                  * If scope is BASE, we need to jump out
457                                  * as soon as one entry is found; if
458                                  * the target pool is properly crafted,
459                                  * this should correspond to the sole
460                                  * entry that has the base DN
461                                  */
462                                 /* FIXME: this defeats the purpose of
463                                  * doing a search with scope == base and
464                                  * sizelimit = 1 to determine if a
465                                  * candidate is actually unique */
466                                 if ( op->ors_scope == LDAP_SCOPE_BASE
467                                                 && rs->sr_nentries > 0 )
468                                 {
469                                         doabandon = 1;
470                                         ncandidates = 0;
471                                         sres = LDAP_SUCCESS;
472                                         break;
473                                 }
474 #endif
475
476                         } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
477                                 char            **references = NULL;
478                                 int             cnt;
479
480                                 is_ok++;
481
482                                 rc = ldap_parse_reference( msc->msc_ld, res,
483                                                 &references, &rs->sr_ctrls, 1 );
484                                 res = NULL;
485
486                                 if ( rc != LDAP_SUCCESS ) {
487                                         continue;
488                                 }
489
490                                 if ( references == NULL ) {
491                                         continue;
492                                 }
493
494 #ifdef ENABLE_REWRITE
495                                 dc.ctx = "referralDN";
496 #else /* ! ENABLE_REWRITE */
497                                 dc.tofrom = 0;
498                                 dc.normalized = 0;
499 #endif /* ! ENABLE_REWRITE */
500
501                                 /* FIXME: merge all and return at the end */
502
503                                 for ( cnt = 0; references[ cnt ]; cnt++ )
504                                         ;
505
506                                 rs->sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
507
508                                 for ( cnt = 0; references[ cnt ]; cnt++ ) {
509                                         ber_str2bv( references[ cnt ], 0, 1, &rs->sr_ref[ cnt ] );
510                                 }
511                                 BER_BVZERO( &rs->sr_ref[ cnt ] );
512
513                                 ( void )ldap_back_referral_result_rewrite( &dc, rs->sr_ref );
514
515                                 if ( rs->sr_ref != NULL && !BER_BVISNULL( &rs->sr_ref[ 0 ] ) ) {
516                                         /* ignore return value by now */
517                                         savepriv = op->o_private;
518                                         op->o_private = (void *)i;
519                                         ( void )send_search_reference( op, rs );
520                                         op->o_private = savepriv;
521
522                                         ber_bvarray_free( rs->sr_ref );
523                                         rs->sr_ref = NULL;
524                                 }
525
526                                 /* cleanup */
527                                 if ( references ) {
528                                         ldap_value_free( references );
529                                 }
530
531                                 if ( rs->sr_ctrls ) {
532                                         ldap_controls_free( rs->sr_ctrls );
533                                         rs->sr_ctrls = NULL;
534                                 }
535
536                         } else if ( rc == LDAP_RES_SEARCH_RESULT ) {
537                                 char            buf[ SLAP_TEXT_BUFLEN ];
538                                 char            **references = NULL;
539
540                                 if ( ldap_parse_result( msc->msc_ld,
541                                                         res,
542                                                         &candidates[ i ].sr_err,
543                                                         (char **)&candidates[ i ].sr_matched,
544                                                         NULL /* (char **)&candidates[ i ].sr_text */ ,
545                                                         &references,
546                                                         &candidates[ i ].sr_ctrls, 1 ) != LDAP_SUCCESS )
547                                 {
548                                         res = NULL;
549                                         ldap_get_option( msc->msc_ld,
550                                                         LDAP_OPT_ERROR_NUMBER,
551                                                         &rs->sr_err );
552                                         sres = slap_map_api2result( rs );
553                                         goto really_bad;
554                                 }
555
556                                 rs->sr_err = candidates[ i ].sr_err;
557                                 sres = slap_map_api2result( rs );
558                                 res = NULL;
559
560                                 /* massage matchedDN if need be */
561                                 if ( candidates[ i ].sr_matched != NULL ) {
562                                         if ( candidates[ i ].sr_matched[ 0 ] == '\0' ) {
563                                                 ldap_memfree( (char *)candidates[ i ].sr_matched );
564                                                 candidates[ i ].sr_matched = NULL;
565
566                                         } else {
567                                                 struct berval   match, mmatch;
568
569                                                 ber_str2bv( candidates[ i ].sr_matched,
570                                                         0, 0, &match );
571
572                                                 dc.ctx = "matchedDN";
573                                                 dc.target = &mi->mi_targets[ i ];
574
575                                                 if ( !ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
576                                                         if ( mmatch.bv_val == match.bv_val ) {
577                                                                 candidates[ i ].sr_matched = ch_strdup( mmatch.bv_val );
578
579                                                         } else {
580                                                                 candidates[ i ].sr_matched = mmatch.bv_val;
581                                                         }
582
583                                                         candidate_match++;
584                                                 } 
585                                                 ldap_memfree( match.bv_val );
586                                         }
587                                 }
588
589                                 /* just get rid of the error message, if any */
590                                 if ( candidates[ i ].sr_text && candidates[ i ].sr_text[ 0 ] == '\0' )
591                                 {
592                                         ldap_memfree( (char *)candidates[ i ].sr_text );
593                                         candidates[ i ].sr_text = NULL;
594                                 }
595
596                                 /* add references to array */
597                                 if ( references ) {
598                                         BerVarray       sr_ref;
599                                         int             cnt;
600
601                                         for ( cnt = 0; references[ cnt ]; cnt++ )
602                                                 ;
603
604                                         sr_ref = ch_calloc( sizeof( struct berval ), cnt + 1 );
605
606                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
607                                                 ber_str2bv( references[ cnt ], 0, 1, &sr_ref[ cnt ] );
608                                         }
609                                         BER_BVZERO( &sr_ref[ cnt ] );
610
611                                         ( void )ldap_back_referral_result_rewrite( &dc, sr_ref );
612                                 
613                                         /* cleanup */
614                                         ldap_value_free( references );
615
616                                         if ( rs->sr_v2ref == NULL ) {
617                                                 rs->sr_v2ref = sr_ref;
618
619                                         } else {
620                                                 for ( cnt = 0; !BER_BVISNULL( &sr_ref[ cnt ] ); cnt++ ) {
621                                                         ber_bvarray_add( &rs->sr_v2ref, &sr_ref[ cnt ] );
622                                                 }
623                                                 ber_memfree( sr_ref );
624                                         }
625                                 }
626
627                                 rs->sr_err = candidates[ i ].sr_err;
628                                 sres = slap_map_api2result( rs );
629
630                                 snprintf( buf, sizeof( buf ),
631                                         "%s meta_back_search[%ld] "
632                                         "match=\"%s\" err=%ld\n",
633                                         op->o_log_prefix, i,
634                                         candidates[ i ].sr_matched ? candidates[ i ].sr_matched : "",
635                                         (long) candidates[ i ].sr_err );
636                                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
637
638                                 switch ( sres ) {
639                                 case LDAP_NO_SUCH_OBJECT:
640                                         /* is_ok is touched any time a valid
641                                          * (even intermediate) result is
642                                          * returned; as a consequence, if
643                                          * a candidate returns noSuchObject
644                                          * it is ignored and the candidate
645                                          * is simply demoted. */
646                                         if ( is_ok ) {
647                                                 sres = LDAP_SUCCESS;
648                                         }
649                                         break;
650
651                                 case LDAP_SUCCESS:
652                                 case LDAP_REFERRAL:
653                                         is_ok++;
654                                         break;
655
656                                 default:
657                                         if ( META_BACK_ONERR_STOP( mi ) ) {
658                                                 savepriv = op->o_private;
659                                                 op->o_private = (void *)i;
660                                                 send_ldap_result( op, rs );
661                                                 op->o_private = savepriv;
662                                                 goto finish;
663                                         }
664                                         break;
665                                 }
666
667                                 last = i;
668                                 rc = 0;
669
670                                 /*
671                                  * When no candidates are left,
672                                  * the outer cycle finishes
673                                  */
674                                 candidates[ i ].sr_msgid = -1;
675                                 --ncandidates;
676
677                         } else {
678                                 assert( 0 );
679                                 goto really_bad;
680                         }
681                 }
682
683                 /* check for abandon */
684                 if ( op->o_abandon || doabandon ) {
685                         for ( i = 0; i < mi->mi_ntargets; i++ ) {
686                                 metasingleconn_t        *msc = &mc->mc_conns[ i ];
687
688                                 if ( candidates[ i ].sr_msgid != -1 ) {
689                                         ldap_abandon_ext( msc->msc_ld,
690                                                 candidates[ i ].sr_msgid,
691                                                 NULL, NULL );
692                                         candidates[ i ].sr_msgid = -1;
693                                 }
694                         }
695
696                         if ( op->o_abandon ) {
697                                 rc = SLAPD_ABANDON;
698                                 goto finish;
699                         }
700                 }
701
702                 if ( gotit == 0 ) {
703                         LDAP_BACK_TV_SET( &tv );
704                         ldap_pvt_thread_yield();
705
706                 } else {
707                         tv.tv_sec = 0;
708                         tv.tv_usec = 0;
709                 }
710         }
711
712         if ( rc == -1 ) {
713                 /*
714                  * FIXME: need a better strategy to handle errors
715                  */
716                 rc = meta_back_op_result( mc, op, rs, META_TARGET_NONE );
717                 goto finish;
718         }
719
720         /*
721          * Rewrite the matched portion of the search base, if required
722          * 
723          * FIXME: only the last one gets caught!
724          */
725         savepriv = op->o_private;
726         op->o_private = (void *)(long)mi->mi_ntargets;
727         if ( candidate_match > 0 ) {
728                 struct berval   pmatched = BER_BVNULL;
729
730                 /* we use the first one */
731                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
732                         if ( candidates[ i ].sr_tag == META_CANDIDATE
733                                         && candidates[ i ].sr_matched )
734                         {
735                                 struct berval   bv, pbv;
736                                 int             rc;
737
738                                 ber_str2bv( candidates[ i ].sr_matched, 0, 0, &bv );
739                                 rc = dnPretty( NULL, &bv, &pbv, op->o_tmpmemctx );
740
741                                 if ( rc == LDAP_SUCCESS ) {
742
743                                         /* NOTE: if they all are superiors
744                                          * of the baseDN, the shorter is also 
745                                          * superior of the longer... */
746                                         if ( pbv.bv_len > pmatched.bv_len ) {
747                                                 if ( !BER_BVISNULL( &pmatched ) ) {
748                                                         op->o_tmpfree( pmatched.bv_val, op->o_tmpmemctx );
749                                                 }
750                                                 pmatched = pbv;
751                                                 op->o_private = (void *)i;
752
753                                         } else {
754                                                 op->o_tmpfree( pbv.bv_val, op->o_tmpmemctx );
755                                         }
756                                 }
757
758                                 if ( candidates[ i ].sr_matched != NULL ) {
759                                         free( (char *)candidates[ i ].sr_matched );
760                                         candidates[ i ].sr_matched = NULL;
761                                 }
762                         }
763                 }
764
765                 if ( !BER_BVISNULL( &pmatched ) ) {
766                         matched = pmatched.bv_val;
767                 }
768
769         } else if ( sres == LDAP_NO_SUCH_OBJECT ) {
770                 matched = op->o_bd->be_suffix[ 0 ].bv_val;
771         }
772
773 #if 0
774         {
775                 char    buf[BUFSIZ];
776                 char    cnd[BUFSIZ];
777                 int     i;
778
779                 for ( i = 0; i < mi->mi_ntargets; i++ ) {
780                         if ( candidates[ i ].sr_tag == META_CANDIDATE ) {
781                                 cnd[ i ] = '*';
782                         } else {
783                                 cnd[ i ] = ' ';
784                         }
785                 }
786                 cnd[ i ] = '\0';
787
788                 snprintf( buf, sizeof( buf ), "%s meta_back_search: is_scope=%d is_ok=%d cnd=\"%s\"\n",
789                         op->o_log_prefix, initial_candidates, is_ok, cnd );
790
791                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
792         }
793 #endif
794
795         /*
796          * In case we returned at least one entry, we return LDAP_SUCCESS
797          * otherwise, the latter error code we got
798          *
799          * FIXME: we should handle error codes and return the more 
800          * important/reasonable
801          */
802
803         if ( sres == LDAP_SUCCESS && rs->sr_v2ref ) {
804                 sres = LDAP_REFERRAL;
805         }
806         rs->sr_err = sres;
807         rs->sr_matched = matched;
808         rs->sr_ref = ( sres == LDAP_REFERRAL ? rs->sr_v2ref : NULL );
809         send_ldap_result( op, rs );
810         op->o_private = savepriv;
811         rs->sr_matched = NULL;
812         rs->sr_ref = NULL;
813
814 finish:;
815         if ( matched && matched != op->o_bd->be_suffix[ 0 ].bv_val ) {
816                 op->o_tmpfree( matched, op->o_tmpmemctx );
817         }
818
819         if ( rs->sr_v2ref ) {
820                 ber_bvarray_free( rs->sr_v2ref );
821         }
822
823         for ( i = 0; i < mi->mi_ntargets; i++ ) {
824                 if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
825                         continue;
826                 }
827
828                 if ( candidates[ i ].sr_matched ) {
829                         free( (char *)candidates[ i ].sr_matched );
830                         candidates[ i ].sr_matched = NULL;
831                 }
832
833                 if ( candidates[ i ].sr_text ) {
834                         ldap_memfree( (char *)candidates[ i ].sr_text );
835                         candidates[ i ].sr_text = NULL;
836                 }
837
838                 if ( candidates[ i ].sr_ref ) {
839                         ber_bvarray_free( candidates[ i ].sr_ref );
840                         candidates[ i ].sr_ref = NULL;
841                 }
842
843                 if ( candidates[ i ].sr_ctrls ) {
844                         ldap_controls_free( candidates[ i ].sr_ctrls );
845                         candidates[ i ].sr_ctrls = NULL;
846                 }
847         }
848
849         meta_back_release_conn( op, mc );
850
851         return rs->sr_err;
852 }
853
854 static int
855 meta_send_entry(
856         Operation       *op,
857         SlapReply       *rs,
858         metaconn_t      *mc,
859         int             target,
860         LDAPMessage     *e )
861 {
862         metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
863         struct berval           a, mapped;
864         Entry                   ent = { 0 };
865         BerElement              ber = *e->lm_ber;
866         Attribute               *attr, **attrp;
867         struct berval           bdn,
868                                 dn = BER_BVNULL;
869         const char              *text;
870         dncookie                dc;
871         int                     rc;
872
873         if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
874                 return LDAP_DECODING_ERROR;
875         }
876
877         /*
878          * Rewrite the dn of the result, if needed
879          */
880         dc.target = &mi->mi_targets[ target ];
881         dc.conn = op->o_conn;
882         dc.rs = rs;
883         dc.ctx = "searchResult";
884
885         rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &dn );
886         if ( rs->sr_err != LDAP_SUCCESS) {
887                 return rs->sr_err;
888         }
889
890         /*
891          * Note: this may fail if the target host(s) schema differs
892          * from the one known to the meta, and a DN with unknown
893          * attributes is returned.
894          * 
895          * FIXME: should we log anything, or delegate to dnNormalize?
896          */
897         rc = dnPrettyNormal( NULL, &dn, &ent.e_name, &ent.e_nname,
898                 op->o_tmpmemctx );
899         if ( dn.bv_val != bdn.bv_val ) {
900                 free( dn.bv_val );
901         }
902         BER_BVZERO( &dn );
903
904         if ( rc != LDAP_SUCCESS ) {
905                 return LDAP_INVALID_DN_SYNTAX;
906         }
907
908         /*
909          * cache dn
910          */
911         if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
912                 ( void )meta_dncache_update_entry( &mi->mi_cache,
913                                 &ent.e_nname, target );
914         }
915
916         attrp = &ent.e_attrs;
917
918         dc.ctx = "searchAttrDN";
919         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
920                 int                             last = 0;
921                 slap_syntax_validate_func       *validate;
922                 slap_syntax_transform_func      *pretty;
923
924                 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_at, 
925                                 &a, &mapped, BACKLDAP_REMAP );
926                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0' ) {
927                         ( void )ber_scanf( &ber, "x" /* [W] */ );
928                         continue;
929                 }
930                 attr = ( Attribute * )ch_calloc( 1, sizeof( Attribute ) );
931                 if ( attr == NULL ) {
932                         continue;
933                 }
934                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
935                                 != LDAP_SUCCESS) {
936                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text,
937                                 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
938                         {
939                                 char    buf[ SLAP_TEXT_BUFLEN ];
940
941                                 snprintf( buf, sizeof( buf ),
942                                         "%s meta_send_entry(\"%s\"): "
943                                         "slap_bv2undef_ad(%s): %s\n",
944                                         op->o_log_prefix, ent.e_name.bv_val,
945                                         mapped.bv_val, text );
946
947                                 Debug( LDAP_DEBUG_ANY, "%s", buf, 0, 0 );
948                                 ch_free( attr );
949                                 continue;
950                         }
951                 }
952
953                 /* no subschemaSubentry */
954                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
955                         || attr->a_desc == slap_schema.si_ad_entryDN )
956                 {
957
958                         /* 
959                          * We eat target's subschemaSubentry because
960                          * a search for this value is likely not
961                          * to resolve to the appropriate backend;
962                          * later, the local subschemaSubentry is
963                          * added.
964                          *
965                          * We also eat entryDN because the frontend
966                          * will reattach it without checking if already
967                          * present...
968                          */
969                         ( void )ber_scanf( &ber, "x" /* [W] */ );
970
971                         ch_free(attr);
972                         continue;
973                 }
974
975                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR 
976                                 || attr->a_vals == NULL )
977                 {
978                         attr->a_vals = (struct berval *)&slap_dummy_bv;
979
980                 } else {
981                         for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); ++last )
982                                 ;
983                 }
984
985                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
986                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
987
988                 if ( !validate && !pretty ) {
989                         attr_free( attr );
990                         goto next_attr;
991                 }
992
993                 if ( attr->a_desc == slap_schema.si_ad_objectClass
994                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
995                 {
996                         struct berval   *bv;
997
998                         for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
999                                 ldap_back_map( &mi->mi_targets[ target ].mt_rwmap.rwm_oc,
1000                                                 bv, &mapped, BACKLDAP_REMAP );
1001                                 if ( BER_BVISNULL( &mapped ) || mapped.bv_val[0] == '\0') {
1002                                         free( bv->bv_val );
1003                                         BER_BVZERO( bv );
1004                                         if ( --last < 0 ) {
1005                                                 break;
1006                                         }
1007                                         *bv = attr->a_vals[ last ];
1008                                         BER_BVZERO( &attr->a_vals[ last ] );
1009                                         bv--;
1010
1011                                 } else if ( mapped.bv_val != bv->bv_val ) {
1012                                         free( bv->bv_val );
1013                                         ber_dupbv( bv, &mapped );
1014                                 }
1015                         }
1016                 /*
1017                  * It is necessary to try to rewrite attributes with
1018                  * dn syntax because they might be used in ACLs as
1019                  * members of groups; since ACLs are applied to the
1020                  * rewritten stuff, no dn-based subecj clause could
1021                  * be used at the ldap backend side (see
1022                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
1023                  * The problem can be overcome by moving the dn-based
1024                  * ACLs to the target directory server, and letting
1025                  * everything pass thru the ldap backend.
1026                  */
1027                 } else {
1028                         int     i;
1029
1030                         if ( attr->a_desc->ad_type->sat_syntax ==
1031                                 slap_schema.si_syn_distinguishedName )
1032                         {
1033                                 ldap_dnattr_result_rewrite( &dc, attr->a_vals );
1034
1035                         } else if ( attr->a_desc == slap_schema.si_ad_ref ) {
1036                                 ldap_back_referral_result_rewrite( &dc, attr->a_vals );
1037
1038                         }
1039
1040                         for ( i = 0; i < last; i++ ) {
1041                                 struct berval   pval;
1042                                 int             rc;
1043
1044                                 if ( pretty ) {
1045                                         rc = pretty( attr->a_desc->ad_type->sat_syntax,
1046                                                 &attr->a_vals[i], &pval, NULL );
1047
1048                                 } else {
1049                                         rc = validate( attr->a_desc->ad_type->sat_syntax,
1050                                                 &attr->a_vals[i] );
1051                                 }
1052
1053                                 if ( rc ) {
1054                                         LBER_FREE( attr->a_vals[i].bv_val );
1055                                         if ( --last == i ) {
1056                                                 BER_BVZERO( &attr->a_vals[ i ] );
1057                                                 break;
1058                                         }
1059                                         attr->a_vals[i] = attr->a_vals[last];
1060                                         BER_BVZERO( &attr->a_vals[last] );
1061                                         i--;
1062                                         continue;
1063                                 }
1064
1065                                 if ( pretty ) {
1066                                         LBER_FREE( attr->a_vals[i].bv_val );
1067                                         attr->a_vals[i] = pval;
1068                                 }
1069                         }
1070
1071                         if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
1072                                 attr_free( attr );
1073                                 goto next_attr;
1074                         }
1075                 }
1076
1077                 if ( last && attr->a_desc->ad_type->sat_equality &&
1078                         attr->a_desc->ad_type->sat_equality->smr_normalize )
1079                 {
1080                         int i;
1081
1082                         attr->a_nvals = ch_malloc( ( last + 1 ) * sizeof( struct berval ) );
1083                         for ( i = 0; i<last; i++ ) {
1084                                 attr->a_desc->ad_type->sat_equality->smr_normalize(
1085                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
1086                                         attr->a_desc->ad_type->sat_syntax,
1087                                         attr->a_desc->ad_type->sat_equality,
1088                                         &attr->a_vals[i], &attr->a_nvals[i],
1089                                         NULL );
1090                         }
1091                         BER_BVZERO( &attr->a_nvals[i] );
1092
1093                 } else {
1094                         attr->a_nvals = attr->a_vals;
1095                 }
1096
1097                 *attrp = attr;
1098                 attrp = &attr->a_next;
1099 next_attr:;
1100         }
1101         rs->sr_entry = &ent;
1102         rs->sr_attrs = op->ors_attrs;
1103         rs->sr_flags = 0;
1104         send_search_entry( op, rs );
1105         rs->sr_entry = NULL;
1106         rs->sr_attrs = NULL;
1107         
1108         if ( !BER_BVISNULL( &ent.e_name ) ) {
1109                 free( ent.e_name.bv_val );
1110                 BER_BVZERO( &ent.e_name );
1111         }
1112         if ( !BER_BVISNULL( &ent.e_nname ) ) {
1113                 free( ent.e_nname.bv_val );
1114                 BER_BVZERO( &ent.e_nname );
1115         }
1116         entry_clean( &ent );
1117
1118         return LDAP_SUCCESS;
1119 }
1120