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