]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/search.c
Changes from HEAD for beta
[openldap] / servers / slapd / back-meta / search.c
1 /*
2  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  *
5  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
6  *
7  * This work has been developed to fulfill the requirements
8  * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
9  * to the OpenLDAP Foundation in the hope that it may be useful
10  * to the Open Source community, but WITHOUT ANY WARRANTY.
11  *
12  * Permission is granted to anyone to use this software for any purpose
13  * on any computer system, and to alter it and redistribute it, subject
14  * to the following restrictions:
15  *
16  * 1. The author and SysNet s.n.c. are not responsible for the consequences
17  *    of use of this software, no matter how awful, even if they arise from 
18  *    flaws in it.
19  *
20  * 2. The origin of this software must not be misrepresented, either by
21  *    explicit claim or by omission.  Since few users ever read sources,
22  *    credits should appear in the documentation.
23  *
24  * 3. Altered versions must be plainly marked as such, and must not be
25  *    misrepresented as being the original software.  Since few users
26  *    ever read sources, credits should appear in the documentation.
27  *    SysNet s.n.c. cannot be responsible for the consequences of the
28  *    alterations.
29  *
30  * 4. This notice may not be removed or altered.
31  *
32  *
33  * This software is based on the backend back-ldap, implemented
34  * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
35  * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
36  * contributors. The contribution of the original software to the present
37  * implementation is acknowledged in this copyright statement.
38  *
39  * A special acknowledgement goes to Howard for the overall architecture
40  * (and for borrowing large pieces of code), and to Mark, who implemented
41  * from scratch the attribute/objectclass mapping.
42  *
43  * The original copyright statement follows.
44  *
45  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
46  *
47  * Permission is granted to anyone to use this software for any purpose
48  * on any computer system, and to alter it and redistribute it, subject
49  * to the following restrictions:
50  *
51  * 1. The author is not responsible for the consequences of use of this
52  *    software, no matter how awful, even if they arise from flaws in it.
53  *
54  * 2. The origin of this software must not be misrepresented, either by
55  *    explicit claim or by omission.  Since few users ever read sources,
56  *    credits should appear in the documentation.
57  *
58  * 3. Altered versions must be plainly marked as such, and must not be
59  *    misrepresented as being the original software.  Since few users
60  *    ever read sources, credits should appear in the
61  *    documentation.
62  *
63  * 4. This notice may not be removed or altered.
64  *                
65  */
66
67 #include "portable.h"
68
69 #include <stdio.h>
70
71 #include <ac/socket.h>
72 #include <ac/string.h>
73 #include <ac/time.h>
74
75 #include "slap.h"
76 #include "../back-ldap/back-ldap.h"
77 #include "back-meta.h"
78 #include "ldap_pvt.h"
79 #undef ldap_debug       /* silence a warning in ldap-int.h */
80 #include "ldap_log.h"
81 #include "../../../libraries/libldap/ldap-int.h"
82
83 static int
84 meta_send_entry(
85                 Operation       *op,
86                 SlapReply       *rs,
87                 struct metaconn *lc,
88                 int             i,
89                 LDAPMessage     *e
90 );
91
92 static int
93 is_one_level_rdn(
94                 const char      *rdn,
95                 int             from
96 );
97
98 int
99 meta_back_search( Operation *op, SlapReply *rs )
100 {
101         struct metainfo *li = ( struct metainfo * )op->o_bd->be_private;
102         struct metaconn *lc;
103         struct metasingleconn *lsc;
104         struct timeval  tv = { 0, 0 };
105         LDAPMessage     *res, *e;
106         int     rc = 0, *msgid, sres = LDAP_NO_SUCH_OBJECT;
107         char *err = NULL;
108         struct berval match = { 0, NULL }, mmatch = { 0, NULL };
109         BerVarray v2refs = NULL;
110                 
111         int i, last = 0, candidates = 0, initial_candidates = 0,
112                         candidate_match = 0;
113         struct slap_limits_set *limit = NULL;
114         int isroot = 0;
115         dncookie dc;
116
117         cache_manager*  cm = li->cm;
118
119         if (cm->caching) {
120                 return meta_back_cache_search(op, rs);
121         }
122         
123         /*
124          * controls are set in ldap_back_dobind()
125          * 
126          * FIXME: in case of values return filter, we might want
127          * to map attrs and maybe rewrite value
128          */
129         lc = meta_back_getconn( op, rs, META_OP_ALLOW_MULTIPLE, 
130                         &op->o_req_ndn, NULL );
131         if ( !lc ) {
132                 send_ldap_result( op, rs );
133                 return -1;
134         }
135
136         if ( !meta_back_dobind( lc, op ) ) {
137                 rs->sr_err = LDAP_OTHER;
138                 send_ldap_result( op, rs );
139                 return -1;
140         }
141
142         /*
143          * Array of message id of each target
144          */
145         msgid = ch_calloc( sizeof( int ), li->ntargets );
146         if ( msgid == NULL ) {
147                 rs->sr_err = LDAP_OTHER;
148                 send_ldap_result( op, rs );
149                 return -1;
150         }
151         
152         /* if not root, get appropriate limits */
153         if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
154                 isroot = 1;
155         } else {
156                 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
157         }
158
159         /* if no time limit requested, rely on remote server limits */
160         /* if requested limit higher than hard limit, abort */
161         if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
162                 /* no hard limit means use soft instead */
163                 if ( limit->lms_t_hard == 0
164                                 && limit->lms_t_soft > -1
165                                 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
166                         op->oq_search.rs_tlimit = limit->lms_t_soft;
167                         
168                 /* positive hard limit means abort */
169                 } else if ( limit->lms_t_hard > 0 ) {
170                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
171                         send_ldap_result( op, rs );
172                         rc = 0;
173                         goto finish;
174                 }
175                 
176                 /* negative hard limit means no limit */
177         }
178         
179         /* if no size limit requested, rely on remote server limits */
180         /* if requested limit higher than hard limit, abort */
181         if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
182                 /* no hard limit means use soft instead */
183                 if ( limit->lms_s_hard == 0
184                                 && limit->lms_s_soft > -1
185                                 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
186                         op->oq_search.rs_slimit = limit->lms_s_soft;
187                         
188                 /* positive hard limit means abort */
189                 } else if ( limit->lms_s_hard > 0 ) {
190                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
191                         send_ldap_result( op, rs );
192                         rc = 0;
193                         goto finish;
194                 }
195                 
196                 /* negative hard limit means no limit */
197         }
198
199
200         dc.conn = op->o_conn;
201         dc.rs = rs;
202
203         /*
204          * Inits searches
205          */
206         for ( i = 0, lsc = lc->conns; !META_LAST(lsc); ++i, ++lsc ) {
207                 struct berval   realbase = op->o_req_dn;
208                 int             realscope = op->oq_search.rs_scope;
209                 ber_len_t       suffixlen = 0;
210                 struct berval   mbase = { 0, NULL }; 
211                 struct berval   mfilter = { 0, NULL };
212                 char            **mapped_attrs = NULL;
213
214                 if ( lsc->candidate != META_CANDIDATE ) {
215                         msgid[ i ] = -1;
216                         continue;
217                 }
218
219                 /* should we check return values? */
220                 if ( op->oq_search.rs_deref != -1 ) {
221                         ldap_set_option( lsc->ld, LDAP_OPT_DEREF,
222                                         ( void * )&op->oq_search.rs_deref);
223                 }
224                 if ( op->oq_search.rs_tlimit != -1 ) {
225                         ldap_set_option( lsc->ld, LDAP_OPT_TIMELIMIT,
226                                         ( void * )&op->oq_search.rs_tlimit);
227                 }
228                 if ( op->oq_search.rs_slimit != -1 ) {
229                         ldap_set_option( lsc->ld, LDAP_OPT_SIZELIMIT,
230                                         ( void * )&op->oq_search.rs_slimit);
231                 }
232
233                 dc.rwmap = &li->targets[ i ]->rwmap;
234
235                 /*
236                  * modifies the base according to the scope, if required
237                  */
238                 suffixlen = li->targets[ i ]->suffix.bv_len;
239                 if ( suffixlen > op->o_req_ndn.bv_len ) {
240                         switch ( op->oq_search.rs_scope ) {
241                         case LDAP_SCOPE_SUBTREE:
242                                 /*
243                                  * make the target suffix the new base
244                                  * FIXME: this is very forgiving, because
245                                  * illegal bases may be turned into 
246                                  * the suffix of the target.
247                                  */
248                                 if ( dnIsSuffix( &li->targets[ i ]->suffix,
249                                                 &op->o_req_ndn ) ) {
250                                         realbase = li->targets[ i ]->suffix;
251                                 } else {
252                                         /*
253                                          * this target is no longer candidate
254                                          */
255                                         msgid[ i ] = -1;
256                                         goto new_candidate;
257                                 }
258                                 break;
259
260                         case LDAP_SCOPE_ONELEVEL:
261                                 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
262                                                 suffixlen - op->o_req_ndn.bv_len - 1 ) 
263                         && dnIsSuffix( &li->targets[ i ]->suffix, &op->o_req_ndn ) ) {
264                                         /*
265                                          * if there is exactly one level,
266                                          * make the target suffix the new
267                                          * base, and make scope "base"
268                                          */
269                                         realbase = li->targets[ i ]->suffix;
270                                         realscope = LDAP_SCOPE_BASE;
271                                         break;
272                                 } /* else continue with the next case */
273
274                         case LDAP_SCOPE_BASE:
275                                 /*
276                                  * this target is no longer candidate
277                                  */
278                                 msgid[ i ] = -1;
279                                 goto new_candidate;
280                         }
281
282                 }
283
284                 /*
285                  * Rewrite the search base, if required
286                  */
287                 dc.ctx = "searchBase";
288                 switch ( ldap_back_dn_massage( &dc, &realbase, &mbase ) ) {
289                 default:
290                         break;
291
292                 case REWRITE_REGEXEC_UNWILLING:
293                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
294                         rs->sr_text = "Operation not allowed";
295                         send_ldap_result( op, rs );
296                         rc = -1;
297                         goto finish;
298
299                 case REWRITE_REGEXEC_ERR:
300 #if 0
301                         rs->sr_err = LDAP_OTHER;
302                         rs->sr_text = "Rewrite error";
303                         send_ldap_result( op, rs );
304                         rc = -1;
305                         goto finish;
306 #endif 
307
308                         /*
309                          * this target is no longer candidate
310                          */
311                         msgid[ i ] = -1;
312                         goto new_candidate;
313                 }
314
315                 /*
316                  * Maps filter
317                  */
318                 rc = ldap_back_filter_map_rewrite( &dc,
319                                 op->oq_search.rs_filter,
320                                 &mfilter, BACKLDAP_MAP );
321                 if ( rc != 0 ) {
322                         /*
323                          * this target is no longer candidate
324                          */
325                         msgid[ i ] = -1;
326                         goto new_candidate;
327                 }
328
329                 /*
330                  * Maps required attributes
331                  */
332                 rc = ldap_back_map_attrs( &li->targets[ i ]->rwmap.rwm_at,
333                                 op->oq_search.rs_attrs, BACKLDAP_MAP,
334                                 &mapped_attrs );
335                 if ( rc != LDAP_SUCCESS ) {
336                         /*
337                          * this target is no longer candidate
338                          */
339                         msgid[ i ] = -1;
340                         goto new_candidate;
341                 }
342
343                 /*
344                  * Starts the search
345                  */
346                 msgid[ i ] = ldap_search( lsc->ld, mbase.bv_val, realscope,
347                                 mfilter.bv_val, mapped_attrs,
348                                 op->oq_search.rs_attrsonly ); 
349                 if ( mapped_attrs ) {
350                         free( mapped_attrs );
351                         mapped_attrs = NULL;
352                 }
353                 if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
354                         free( mfilter.bv_val );
355                         mfilter.bv_val = NULL;
356                 }
357                 if ( mbase.bv_val != realbase.bv_val ) {
358                         free( mbase.bv_val );
359                         mbase.bv_val = NULL;
360                 }
361
362                 if ( msgid[ i ] == -1 ) {
363                         continue;
364                 }
365
366                 ++candidates;
367
368 new_candidate:;
369         }
370
371         initial_candidates = candidates;
372
373         /* We pull apart the ber result, stuff it into a slapd entry, and
374          * let send_search_entry stuff it back into ber format. Slow & ugly,
375          * but this is necessary for version matching, and for ACL processing.
376          */
377
378
379         /*
380          * In case there are no candidates, no cycle takes place...
381          *
382          * FIXME: we might use a queue, to balance the load 
383          * among the candidates
384          */
385         for ( rc = 0; candidates > 0; ) {
386                 int ab, gotit = 0;
387
388                 /* check for abandon */
389                 ab = op->o_abandon;
390
391                 for ( i = 0, lsc = lc->conns; !META_LAST(lsc); lsc++, i++ ) {
392                         if ( msgid[ i ] == -1 ) {
393                                 continue;
394                         }
395                         
396                         if ( ab ) {
397                                 ldap_abandon( lsc->ld, msgid[ i ] );
398                                 rc = 0;
399                                 break;
400                         }
401
402                         if ( op->oq_search.rs_slimit > 0
403                                         && rs->sr_nentries == op->oq_search.rs_slimit ) {
404                                 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
405                                 rs->sr_v2ref = v2refs;
406                                 send_ldap_result( op, rs );
407                                 goto finish;
408                         }
409
410                         /*
411                          * FIXME: handle time limit as well?
412                          * Note that target servers are likely 
413                          * to handle it, so at some time we'll
414                          * get a LDAP_TIMELIMIT_EXCEEDED from
415                          * one of them ...
416                          */
417                         rc = ldap_result( lsc->ld, msgid[ i ],
418                                         0, &tv, &res );
419
420                         if ( rc == 0 ) {
421                                 continue;
422
423                         } else if ( rc == -1 ) {
424                                 /* something REALLY bad happened! */
425                                 ( void )meta_clear_unused_candidates( li,
426                                                 lc, -1, 0 );
427                                 rs->sr_err = LDAP_OTHER;
428                                 rs->sr_v2ref = v2refs;
429                                 send_ldap_result( op, rs );
430                                 
431                                 /* anything else needs be done? */
432                                 goto finish;
433
434                         } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
435                                 e = ldap_first_entry( lsc->ld, res );
436                                 meta_send_entry( op, rs, lc, i, e );
437
438                                 /*
439                                  * If scope is BASE, we need to jump out
440                                  * as soon as one entry is found; if
441                                  * the target pool is properly crafted,
442                                  * this should correspond to the sole
443                                  * entry that has the base DN
444                                  */
445                                 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE
446                                                 && rs->sr_nentries > 0 ) {
447                                         candidates = 0;
448                                         sres = LDAP_SUCCESS;
449                                         break;
450                                 }
451                                 ldap_msgfree( res );
452                                 gotit = 1;
453
454                         } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
455                                 char            **references = NULL;
456                                 int             cnt;
457
458                                 /*
459                                  * FIXME: should we collect references
460                                  * and send them alltogether at the end?
461                                  */
462
463                                 rc = ldap_parse_reference( lsc->ld, res,
464                                                 &references, &rs->sr_ctrls, 1 );
465
466                                 if ( rc != LDAP_SUCCESS ) {
467                                         continue;
468                                 }
469
470                                 if ( references == NULL ) {
471                                         continue;
472                                 }
473
474                                 for ( cnt = 0; references[ cnt ]; cnt++ )
475                                         /* NO OP */ ;
476                                 
477                                 rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
478
479                                 for ( cnt = 0; references[ cnt ]; cnt++ ) {
480                                         rs->sr_ref[ cnt ].bv_val = references[ cnt ];
481                                         rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
482                                 }
483
484                                 /* ignore return value by now */
485                                 ( void )send_search_reference( op, rs );
486
487                                 /* cleanup */
488                                 if ( references ) {
489                                         ldap_value_free( references );
490                                         ch_free( rs->sr_ref );
491                                         rs->sr_ref = NULL;
492                                 }
493
494                                 if ( rs->sr_ctrls ) {
495                                         ldap_controls_free( rs->sr_ctrls );
496                                         rs->sr_ctrls = NULL;
497                                 }
498
499                         } else {
500                                 rs->sr_err = ldap_result2error( lsc->ld,
501                                                 res, 1 );
502                                 sres = ldap_back_map_result( rs );
503                                 if ( err != NULL ) {
504                                         free( err );
505                                 }
506                                 ldap_get_option( lsc->ld,
507                                                 LDAP_OPT_ERROR_STRING, &err );
508                                 if ( match.bv_val != NULL ) {
509                                         free( match.bv_val );
510                                 }
511                                 ldap_get_option( lsc->ld,
512                                                 LDAP_OPT_MATCHED_DN, &match.bv_val );
513
514 #ifdef NEW_LOGGING
515                                 LDAP_LOG( BACK_META, ERR,
516                                         "meta_back_search [%d] "
517                                         "match=\"%s\" err=\"%s\"\n",
518                                         i, match.bv_val, err );
519 #else /* !NEW_LOGGING */
520                                 Debug( LDAP_DEBUG_ANY,
521                                         "=>meta_back_search [%d] "
522                                         "match=\"%s\" err=\"%s\"\n",
523                                         i, match.bv_val, err ); 
524 #endif /* !NEW_LOGGING */
525                                 candidate_match++;
526                                 last = i;
527                                 rc = 0;
528
529                                 /*
530                                  * When no candidates are left,
531                                  * the outer cycle finishes
532                                  */
533                                 msgid[ i ] = -1;
534                                 --candidates;
535                         }
536                 }
537
538                 if ( ab ) {
539                         goto finish;
540                 }
541
542                 if ( gotit == 0 ) {
543                         tv.tv_sec = 0;
544                         tv.tv_usec = 100000;
545                         ldap_pvt_thread_yield();
546                 } else {
547                         tv.tv_sec = 0;
548                         tv.tv_usec = 0;
549                 }
550         }
551
552         if ( rc == -1 ) {
553                 /*
554                  * FIXME: need a strategy to handle errors
555                  */
556                 rc = meta_back_op_result( lc, op, rs );
557                 goto finish;
558         }
559
560         /*
561          * Rewrite the matched portion of the search base, if required
562          * 
563          * FIXME: only the last one gets caught!
564          */
565         if ( candidate_match == initial_candidates
566                         && match.bv_val != NULL && *match.bv_val ) {
567                 dc.ctx = "matchedDn";
568                 dc.rwmap = &li->targets[ last ]->rwmap;
569
570                 if ( ldap_back_dn_massage( &dc, &match, &mmatch ) ) {
571                         mmatch.bv_val = NULL;
572                 }
573         }
574
575         /*
576          * In case we returned at least one entry, we return LDAP_SUCCESS
577          * otherwise, the latter error code we got
578          *
579          * FIXME: we should handle error codes and return the more 
580          * important/reasonable
581          */
582         if ( sres == LDAP_SUCCESS && v2refs ) {
583                 sres = LDAP_REFERRAL;
584         }
585         rs->sr_err = sres;
586         rs->sr_matched = mmatch.bv_val;
587         rs->sr_v2ref = v2refs;
588         send_ldap_result( op, rs );
589         rs->sr_matched = NULL;
590         rs->sr_v2ref = NULL;
591
592
593 finish:;
594         if ( match.bv_val ) {
595                 if ( mmatch.bv_val != match.bv_val ) {
596                         free( mmatch.bv_val );
597                 }
598                 free( match.bv_val );
599         }
600         
601         if ( err ) {
602                 free( err );
603         }
604         
605         if ( msgid ) {
606                 ch_free( msgid );
607         }
608
609         return rc;
610 }
611
612 static int
613 meta_send_entry(
614                 Operation       *op,
615                 SlapReply       *rs,
616                 struct metaconn *lc,
617                 int             target,
618                 LDAPMessage     *e
619 )
620 {
621         struct metainfo         *li = ( struct metainfo * )op->o_bd->be_private;
622         struct berval           a, mapped;
623         Entry                   ent;
624         BerElement              ber = *e->lm_ber;
625         Attribute               *attr, **attrp;
626         struct berval           dummy = { 0, NULL };
627         struct berval           *bv, bdn;
628         const char              *text;
629         dncookie                dc;
630
631         if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
632                 return LDAP_DECODING_ERROR;
633         }
634
635         /*
636          * Rewrite the dn of the result, if needed
637          */
638         dc.rwmap = &li->targets[ target ]->rwmap;
639         dc.conn = op->o_conn;
640         dc.rs = rs;
641         dc.ctx = "searchResult";
642
643         rs->sr_err = ldap_back_dn_massage( &dc, &bdn, &ent.e_name );
644         if ( rs->sr_err != LDAP_SUCCESS) {
645                 return rs->sr_err;
646         }
647
648         /*
649          * Note: this may fail if the target host(s) schema differs
650          * from the one known to the meta, and a DN with unknown
651          * attributes is returned.
652          * 
653          * FIXME: should we log anything, or delegate to dnNormalize?
654          */
655         if ( dnNormalize( 0, NULL, NULL, &ent.e_name, &ent.e_nname,
656                 &op->o_tmpmemctx ) != LDAP_SUCCESS )
657         {
658                 return LDAP_INVALID_DN_SYNTAX;
659         }
660
661         /*
662          * cache dn
663          */
664         if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
665                 ( void )meta_dncache_update_entry( &li->cache,
666                                 &ent.e_nname, target );
667         }
668
669         ent.e_id = 0;
670         ent.e_attrs = 0;
671         ent.e_private = 0;
672         attrp = &ent.e_attrs;
673
674         dc.ctx = "searchAttrDN";
675         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
676                 ldap_back_map( &li->targets[ target ]->rwmap.rwm_at, 
677                                 &a, &mapped, BACKLDAP_REMAP );
678                 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0' ) {
679                         continue;
680                 }
681                 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
682                 if ( attr == NULL ) {
683                         continue;
684                 }
685                 attr->a_flags = 0;
686                 attr->a_next = 0;
687                 attr->a_desc = NULL;
688                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
689                                 != LDAP_SUCCESS) {
690                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text ) 
691                                         != LDAP_SUCCESS) {
692 #ifdef NEW_LOGGING
693                                 LDAP_LOG( BACK_META, DETAIL1,
694                                         "slap_bv2undef_ad(%s): %s\n", mapped.bv_val, text, 0 );
695 #else /* !NEW_LOGGING */
696                                 Debug( LDAP_DEBUG_ANY,
697                                                 "slap_bv2undef_ad(%s): "
698                                                 "%s\n%s", mapped.bv_val, text, "" );
699 #endif /* !NEW_LOGGING */
700                                 ch_free( attr );
701                                 continue;
702                         }
703                 }
704
705                 /* no subschemaSubentry */
706                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
707
708                         /* 
709                          * We eat target's subschemaSubentry because
710                          * a search for this value is likely not
711                          * to resolve to the appropriate backend;
712                          * later, the local subschemaSubentry is
713                          * added.
714                          */
715                         ( void )ber_scanf( &ber, "x" /* [W] */ );
716
717                         ch_free(attr);
718                         continue;
719                 }
720
721                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR 
722                                 || attr->a_vals == NULL ) {
723                         attr->a_vals = &dummy;
724
725                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
726                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
727                         int             last;
728
729                         for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
730
731                         for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
732                                 ldap_back_map( &li->targets[ target ]->rwmap.rwm_oc,
733                                                 bv, &mapped, BACKLDAP_REMAP );
734                                 if ( mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
735                                         free( bv->bv_val );
736                                         bv->bv_val = NULL;
737                                         if ( --last < 0 ) {
738                                                 break;
739                                         }
740                                         *bv = attr->a_vals[ last ];
741                                         attr->a_vals[ last ].bv_val = NULL;
742                                         bv--;
743
744                                 } else if ( mapped.bv_val != bv->bv_val ) {
745                                         free( bv->bv_val );
746                                         ber_dupbv( bv, &mapped );
747                                 }
748                         }
749                 /*
750                  * It is necessary to try to rewrite attributes with
751                  * dn syntax because they might be used in ACLs as
752                  * members of groups; since ACLs are applied to the
753                  * rewritten stuff, no dn-based subecj clause could
754                  * be used at the ldap backend side (see
755                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
756                  * The problem can be overcome by moving the dn-based
757                  * ACLs to the target directory server, and letting
758                  * everything pass thru the ldap backend.
759                  */
760                 } else if ( attr->a_desc->ad_type->sat_syntax ==
761                                 slap_schema.si_syn_distinguishedName ) {
762                         ldap_dnattr_result_rewrite( &dc, attr->a_vals );
763                 }
764
765                 *attrp = attr;
766                 attrp = &attr->a_next;
767         }
768         rs->sr_entry = &ent;
769         rs->sr_attrs = op->oq_search.rs_attrs;
770         send_search_entry( op, rs );
771         rs->sr_entry = NULL;
772         rs->sr_attrs = NULL;
773         while ( ent.e_attrs ) {
774                 attr = ent.e_attrs;
775                 ent.e_attrs = attr->a_next;
776                 if ( attr->a_vals != &dummy ) {
777                         ber_bvarray_free( attr->a_vals );
778                 }
779                 free( attr );
780         }
781         
782         if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
783                 free( ent.e_dn );
784         }
785         if ( ent.e_ndn ) {
786                 free( ent.e_ndn );
787         }
788
789         return LDAP_SUCCESS;
790 }
791
792 static int
793 is_one_level_rdn(
794                 const char      *rdn,
795                 int             from
796 )
797 {
798         for ( ; from--; ) {
799                 if ( DN_SEPARATOR( rdn[ from ] ) ) {
800                         return 0;
801                 }
802         }
803
804         return 1;
805 }
806