]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/search.c
Fix matched values bug
[openldap] / servers / slapd / back-meta / search.c
1 /*
2  * Copyright 1998-2002 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 "../../../libraries/libldap/ldap-int.h"
81
82 static void
83 meta_send_entry(
84                 Backend         *be,
85                 Operation       *op,
86                 struct metaconn *lc,
87                 int             i,
88                 LDAPMessage     *e,
89                 AttributeName   *attrs,
90                 int             attrsonly
91 );
92
93 static int
94 is_one_level_rdn(
95                 const char      *rdn,
96                 int             from
97 );
98
99 int
100 meta_back_search(
101                 Backend         *be,
102                 Connection      *conn,
103                 Operation       *op,
104                 struct berval   *base,
105                 struct berval   *nbase,
106                 int             scope,
107                 int             deref,
108                 int             slimit,
109                 int             tlimit,
110                 Filter          *filter,
111                 struct berval   *filterstr,
112                 AttributeName   *attrs,
113                 int             attrsonly
114 )
115 {
116         struct metainfo *li = ( struct metainfo * )be->be_private;
117         struct metaconn *lc;
118         struct metasingleconn **lsc;
119         struct timeval  tv;
120         LDAPMessage     *res, *e;
121         int     count, rc = 0, *msgid, sres = LDAP_NO_SUCH_OBJECT;
122         char *match = NULL, *err = NULL;
123         char *mbase = NULL, *mmatch = NULL;
124         struct berval mfilter;
125                 
126         int i, last = 0, candidates = 0, op_type;
127         struct slap_limits_set *limit = NULL;
128         int isroot = 0;
129
130         if ( scope == LDAP_SCOPE_BASE ) {
131                 op_type = META_OP_REQUIRE_SINGLE;
132         } else {
133                 op_type = META_OP_ALLOW_MULTIPLE;
134         }
135         
136         lc = meta_back_getconn( li, conn, op, op_type, nbase, NULL );
137         if ( !lc || !meta_back_dobind( lc, op ) ) {
138                 return -1;
139         }
140
141         /*
142          * Array of message id of each target
143          */
144         msgid = ch_calloc( sizeof( int ), li->ntargets );
145         if ( msgid == NULL ) {
146                 send_search_result( conn, op, LDAP_OPERATIONS_ERROR,
147                                 NULL, NULL, NULL, NULL, 0 );
148                 return -1;
149         }
150         
151         /* if not root, get appropriate limits */
152         if ( be_isroot( be, &op->o_ndn ) ) {
153                 isroot = 1;
154         } else {
155                 ( void ) get_limits( be, &op->o_ndn, &limit );
156         }
157
158         /* if no time limit requested, rely on remote server limits */
159         /* if requested limit higher than hard limit, abort */
160         if ( !isroot && tlimit > limit->lms_t_hard ) {
161                 /* no hard limit means use soft instead */
162                 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
163                         tlimit = limit->lms_t_soft;
164                         
165                 /* positive hard limit means abort */
166                 } else if ( limit->lms_t_hard > 0 ) {
167                         send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
168                                         NULL, NULL, NULL, NULL, 0 );
169                         rc = 0;
170                         goto finish;
171                 }
172                 
173                 /* negative hard limit means no limit */
174         }
175         
176         /* if no size limit requested, rely on remote server limits */
177         /* if requested limit higher than hard limit, abort */
178         if ( !isroot && slimit > limit->lms_s_hard ) {
179                 /* no hard limit means use soft instead */
180                 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
181                         slimit = limit->lms_s_soft;
182                         
183                 /* positive hard limit means abort */
184                 } else if ( limit->lms_s_hard > 0 ) {
185                         send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
186                                         NULL, NULL, NULL, NULL, 0 );
187                         rc = 0;
188                         goto finish;
189                 }
190                 
191                 /* negative hard limit means no limit */
192         }
193
194         /*
195          * Inits searches
196          */
197         for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
198                 char    *realbase = ( char * )base->bv_val;
199                 int     realscope = scope;
200                 ber_len_t suffixlen;
201                 char    *mapped_filter, **mapped_attrs;
202                 
203                 if ( lsc[ 0 ]->candidate != META_CANDIDATE ) {
204                         continue;
205                 }
206
207                 if ( deref != -1 ) {
208                         ldap_set_option( lsc[ 0 ]->ld, LDAP_OPT_DEREF,
209                                         ( void * )&deref);
210                 }
211                 if ( tlimit != -1 ) {
212                         ldap_set_option( lsc[ 0 ]->ld, LDAP_OPT_TIMELIMIT,
213                                         ( void * )&tlimit);
214                 }
215                 if ( slimit != -1 ) {
216                         ldap_set_option( lsc[ 0 ]->ld, LDAP_OPT_SIZELIMIT,
217                                         ( void * )&slimit);
218                 }
219
220                 /*
221                  * modifies the base according to the scope, if required
222                  */
223                 suffixlen = li->targets[ i ]->suffix.bv_len;
224                 if ( suffixlen > nbase->bv_len ) {
225                         switch ( scope ) {
226                         case LDAP_SCOPE_SUBTREE:
227                                 /*
228                                  * make the target suffix the new base
229                                  * FIXME: this is very forgiving, because
230                                  * illegal bases may be turned into 
231                                  * the suffix of the target.
232                                  */
233                                 if ( dnIsSuffix( &li->targets[ i ]->suffix,
234                                                 nbase ) ) {
235                                         realbase = li->targets[ i ]->suffix.bv_val;
236                                 } else {
237                                         /*
238                                          * this target is no longer candidate
239                                          */
240                                         lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
241                                         continue;
242                                 }
243                                 break;
244
245                         case LDAP_SCOPE_ONELEVEL:
246                                 if ( is_one_level_rdn( li->targets[ i ]->suffix.bv_val,
247                                                 suffixlen - nbase->bv_len - 1 ) 
248                         && dnIsSuffix( &li->targets[ i ]->suffix, nbase ) ) {
249                                         /*
250                                          * if there is exactly one level,
251                                          * make the target suffix the new
252                                          * base, and make scope "base"
253                                          */
254                                         realbase = li->targets[ i ]->suffix.bv_val;
255                                         realscope = LDAP_SCOPE_BASE;
256                                         break;
257                                 } /* else continue with the next case */
258
259                         case LDAP_SCOPE_BASE:
260                                 /*
261                                  * this target is no longer candidate
262                                  */
263                                 lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
264                                 continue;
265                         }
266
267                 }
268
269                 /*
270                  * Rewrite the search base, if required
271                  */
272                 switch ( rewrite_session( li->targets[ i ]->rwinfo,
273                                         "searchBase",
274                                         realbase, conn, &mbase ) ) {
275                 case REWRITE_REGEXEC_OK:
276                 if ( mbase == NULL ) {
277                         mbase = realbase;
278                 }
279 #ifdef NEW_LOGGING
280                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
281                                 "[rw] searchBase: \"%s\" -> \"%s\"\n",
282                                 base->bv_val, mbase ));
283 #else /* !NEW_LOGGING */
284                 Debug( LDAP_DEBUG_ARGS, "rw> searchBase: \"%s\" -> \"%s\"\n%s",
285                                 base->bv_val, mbase, "" );
286 #endif /* !NEW_LOGGING */
287                 break;
288                 
289                 case REWRITE_REGEXEC_UNWILLING:
290                         send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
291                                         NULL, "Unwilling to perform",
292                                         NULL, NULL );
293                         rc = -1;
294                         goto finish;
295
296                 case REWRITE_REGEXEC_ERR:
297                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
298                                         NULL, "Operations error",
299                                         NULL, NULL );
300                         rc = -1;
301                         goto finish;
302                 }
303         
304                 /*
305                  * Rewrite the search filter, if required
306                  */
307                 switch ( rewrite_session( li->targets[ i ]->rwinfo,
308                                         "searchFilter",
309                                         filterstr->bv_val, conn, &mfilter.bv_val ) ) {
310                 case REWRITE_REGEXEC_OK:
311                         if ( mfilter.bv_val != NULL && mfilter.bv_val[ 0 ] != '\0') {
312                                 mfilter.bv_len = strlen( mfilter.bv_val );
313                         } else {
314                                 if ( mfilter.bv_val != NULL ) {
315                                         free( mfilter.bv_val );
316                                 }
317                                 mfilter = *filterstr;
318                         }
319 #ifdef NEW_LOGGING
320                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
321                                         "[rw] searchFilter: \"%s\" -> \"%s\"\n",
322                                         filterstr->bv_val, mfilter.bv_val ));
323 #else /* !NEW_LOGGING */
324                         Debug( LDAP_DEBUG_ARGS,
325                                 "rw> searchFilter: \"%s\" -> \"%s\"\n%s",
326                                 filterstr->bv_val, mfilter.bv_val, "" );
327 #endif /* !NEW_LOGGING */
328                         break;
329                 
330                 case REWRITE_REGEXEC_UNWILLING:
331                         send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
332                                         NULL, NULL, NULL, NULL );
333                         rc = -1;
334                         goto finish;
335
336                 case REWRITE_REGEXEC_ERR:
337                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
338                                         NULL, NULL, NULL, NULL );
339                         rc = -1;
340                         goto finish;
341                 }
342
343                 /*
344                  * Maps attributes in filter
345                  */
346                 mapped_filter = ldap_back_map_filter( &li->targets[ i ]->at_map,
347                                 &li->targets[ i ]->oc_map, &mfilter, 0 );
348                 if ( mapped_filter == NULL ) {
349                         mapped_filter = ( char * )mfilter.bv_val;
350                 } else {
351                         free( mfilter.bv_val );
352                 }
353                 mfilter.bv_val = NULL;
354                 mfilter.bv_len = 0;
355         
356                 /*
357                  * Maps required attributes
358                  */
359                 mapped_attrs = ldap_back_map_attrs( &li->targets[ i ]->at_map,
360                                 attrs, 0 );
361                 if ( mapped_attrs == NULL && attrs) {
362                         for ( count=0; attrs[ count ].an_name.bv_val; count++ );
363                         mapped_attrs = ch_malloc( ( count + 1 ) * sizeof(char *));
364                         for ( count=0; attrs[ count ].an_name.bv_val; count++ ) {
365                                 mapped_attrs[ count ] = attrs[ count ].an_name.bv_val;
366                         }
367                         mapped_attrs[ count ] = NULL;
368                 }
369
370                 /*
371                  * Starts the search
372                  */
373                 msgid[ i ] = ldap_search( lsc[ 0 ]->ld, mbase, realscope,
374                                 mapped_filter, mapped_attrs, attrsonly ); 
375                 if ( msgid[ i ] == -1 ) {
376                         lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
377                         continue;
378                 }
379
380                 if ( mapped_attrs ) {
381                         free( mapped_attrs );
382                         mapped_attrs = NULL;
383                 }
384                 if ( mapped_filter != filterstr->bv_val ) {
385                         free( mapped_filter );
386                         mapped_filter = NULL;
387                 }
388                 if ( mbase != realbase ) {
389                         free( mbase );
390                         mbase = NULL;
391                 }
392
393                 ++candidates;
394         }
395
396         /* We pull apart the ber result, stuff it into a slapd entry, and
397          * let send_search_entry stuff it back into ber format. Slow & ugly,
398          * but this is necessary for version matching, and for ACL processing.
399          */
400
401
402         /*
403          * In case there are no candidates, no cycle takes place...
404          */
405         for ( count = 0, rc = 0; candidates > 0; ) {
406                 int ab, gotit = 0;
407
408                 /* check for abandon */
409                 ab = op->o_abandon;
410
411                 for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; lsc++, i++ ) {
412                         if ( lsc[ 0 ]->candidate != META_CANDIDATE ) {
413                                 continue;
414                         }
415                         
416                         if ( ab ) {
417                                 ldap_abandon( lsc[ 0 ]->ld, msgid[ i ] );
418                                 rc = 0;
419                                 break;
420                         }
421
422                         if ( slimit > 0 && count == slimit ) {
423                                 send_search_result( conn, op,
424                                                 LDAP_SIZELIMIT_EXCEEDED,
425                                                 NULL, NULL, NULL, NULL, count );
426                                 goto finish;
427                         }
428
429                         rc = ldap_result( lsc[ 0 ]->ld, msgid[ i ],
430                                         0, &tv, &res );
431
432                         if ( rc == 0 ) {
433                                 continue;
434                         } else if ( rc == -1 ) {
435                                 /* something REALLY bad happened! */
436                                 ( void )meta_clear_unused_candidates( li,
437                                                 lc, -1, 0 );
438                                 send_search_result( conn, op,
439                                                 LDAP_OPERATIONS_ERROR,
440                                                 "", "", NULL, NULL, count );
441                                 
442                                 /* anything else needs be done? */
443                                 goto finish;
444                         } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
445                                 e = ldap_first_entry( lsc[ 0 ]->ld, res );
446                                 meta_send_entry(be, op, lc, i, e, attrs,
447                                                 attrsonly);
448                                 count++;
449                                 ldap_msgfree( res );
450                                 gotit = 1;
451                         } else {
452                                 sres = ldap_result2error( lsc[ 0 ]->ld,
453                                                 res, 1 );
454                                 sres = ldap_back_map_result( sres );
455                                 if ( err != NULL ) {
456                                         free( err );
457                                 }
458                                 ldap_get_option( lsc[ 0 ]->ld,
459                                                 LDAP_OPT_ERROR_STRING, &err );
460                                 if ( match != NULL ) {
461                                         free( match );
462                                 }
463                                 ldap_get_option( lsc[ 0 ]->ld,
464                                                 LDAP_OPT_MATCHED_DN, &match );
465
466 #ifdef NEW_LOGGING
467                                 LDAP_LOG(( "backend", LDAP_LEVEL_ERR,
468                                                 "meta_back_search [%d]"
469                                                 " match=\"%s\" err=\"%s\"\n",
470                                                 i, match, err ));
471 #else /* !NEW_LOGGING */
472                                 Debug( LDAP_DEBUG_ANY,
473         "=>meta_back_search [%d] match=\"%s\" err=\"%s\"\n",
474                                         i, match, err );        
475 #endif /* !NEW_LOGGING */
476                                 
477                                 last = i;
478                                 rc = 0;
479
480                                 /*
481                                  * When no candidates are left,
482                                  * the outer cycle finishes
483                                  */
484                                 lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
485                                 --candidates;
486                         }
487                 }
488
489                 if ( ab ) {
490                         goto finish;
491                 }
492
493                 if ( gotit == 0 ) {
494                         tv.tv_sec = 0;
495                         tv.tv_usec = 100000;
496                         ldap_pvt_thread_yield();
497                 } else {
498                         tv.tv_sec = 0;
499                         tv.tv_usec = 0;
500                 }
501         }
502
503         if ( rc == -1 ) {
504                 /*
505                  * FIXME: need a strategy to handle errors
506                  */
507                 rc = meta_back_op_result( lc, op );
508                 goto finish;
509         }
510
511         /*
512          * Rewrite the matched portion of the search base, if required
513          * 
514          * FIXME: only the last one gets caught!
515          */
516         if ( match != NULL ) {
517                 switch ( rewrite_session( li->targets[ last ]->rwinfo,
518                                         "matchedDn", match, conn, &mmatch ) ) {
519                 case REWRITE_REGEXEC_OK:
520                         if ( mmatch == NULL ) {
521                                 mmatch = ( char * )match;
522                         }
523 #ifdef NEW_LOGGING
524                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
525                                         "[rw] matchedDn: \"%s\" -> \"%s\"\n",
526                                         match, mmatch ));
527 #else /* !NEW_LOGGING */
528                         Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
529                                        " \"%s\" -> \"%s\"\n%s",
530                                        match, mmatch, "" );
531 #endif /* !NEW_LOGGING */
532                         break;
533                         
534                 case REWRITE_REGEXEC_UNWILLING:
535                         send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
536                                         NULL, NULL, NULL, NULL );
537                         rc = -1;
538                         goto finish;
539                         
540                 case REWRITE_REGEXEC_ERR:
541                         send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
542                                         NULL, NULL, NULL, NULL );
543                         rc = -1;
544                         goto finish;
545                 }
546         }
547
548         send_search_result( conn, op, sres,
549                 mmatch, err, NULL, NULL, count );
550
551 finish:;
552         if ( match ) {
553                 if ( mmatch != match ) {
554                         free( mmatch );
555                 }
556                 free(match);
557         }
558         
559         if ( err ) {
560                 free( err );
561         }
562         
563         if ( msgid ) {
564                 free( msgid );
565         }
566         
567         return rc;
568 }
569
570 static void
571 meta_send_entry(
572                 Backend         *be,
573                 Operation       *op,
574                 struct metaconn *lc,
575                 int             target,
576                 LDAPMessage     *e,
577                 AttributeName   *attrs,
578                 int             attrsonly
579 )
580 {
581         struct metainfo         *li = ( struct metainfo * )be->be_private;
582         struct berval           a, mapped;
583         Entry                   ent;
584         BerElement              ber = *e->lm_ber;
585         Attribute               *attr, **attrp;
586         struct berval           dummy = { 0, NULL };
587         struct berval           *bv, bdn;
588         const char              *text;
589
590         if ( ber_scanf( &ber, "{m{", &bdn ) == LBER_ERROR ) {
591                 return;
592         }
593
594         /*
595          * Rewrite the dn of the result, if needed
596          */
597         switch ( rewrite_session( li->targets[ target ]->rwinfo,
598                                 "searchResult", bdn.bv_val, lc->conn, &ent.e_name.bv_val ) ) {
599         case REWRITE_REGEXEC_OK:
600                 if ( ent.e_name.bv_val == NULL ) {
601                         ent.e_name = bdn;
602
603                 } else {
604 #ifdef NEW_LOGGING
605                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
606                                         "[rw] searchResult[%d]:"
607                                         " \"%s\" -> \"%s\"\n",
608                                         target, bdn.bv_val, ent.e_name.bv_val ));
609 #else /* !NEW_LOGGING */
610                         Debug( LDAP_DEBUG_ARGS, "rw> searchResult[%d]: \"%s\""
611                                         " -> \"%s\"\n", target, bdn.bv_val, ent.e_name.bv_val );
612 #endif /* !NEW_LOGGING */
613                         ent.e_name.bv_len = strlen( ent.e_name.bv_val );
614                 }
615                 break;
616                 
617         case REWRITE_REGEXEC_ERR:
618         case REWRITE_REGEXEC_UNWILLING:
619                 return;
620         }
621
622         dnNormalize2( NULL, &ent.e_name, &ent.e_nname );
623
624         /*
625          * cache dn
626          */
627         if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
628                 ( void )meta_dncache_update_entry( &li->cache,
629                                                    &ent.e_nname,
630                                                    target );
631         }
632
633         ent.e_id = 0;
634         ent.e_attrs = 0;
635         ent.e_private = 0;
636         attrp = &ent.e_attrs;
637
638         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
639                 ldap_back_map( &li->targets[ target ]->at_map, 
640                                 &a, &mapped, 1 );
641                 if ( mapped.bv_val == NULL ) {
642                         continue;
643                 }
644                 attr = ( Attribute * )ch_malloc( sizeof( Attribute ) );
645                 if ( attr == NULL ) {
646                         continue;
647                 }
648                 attr->a_flags = 0;
649                 attr->a_next = 0;
650                 attr->a_desc = NULL;
651                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text )
652                                 != LDAP_SUCCESS) {
653                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text ) 
654                                         != LDAP_SUCCESS) {
655 #ifdef NEW_LOGGING
656                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
657                                                 "slap_bv2undef_ad(%s): "
658                                                 "%s\n", mapped.bv_val, text ));
659 #else /* !NEW_LOGGING */
660                                 Debug( LDAP_DEBUG_ANY,
661                                                 "slap_bv2undef_ad(%s): "
662                                                 "%s\n%s", mapped.bv_val, text, "" );
663 #endif /* !NEW_LOGGING */
664                                 ch_free( attr );
665                                 continue;
666                         }
667                 }
668
669                 /* no subschemaSubentry */
670                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
671                         ch_free(attr);
672                         continue;
673                 }
674
675                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR ) {
676                         attr->a_vals = &dummy;
677                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
678                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
679                         int i, last;
680                         for ( last = 0; attr->a_vals[ last ].bv_val; ++last );
681                         for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
682                                 ldap_back_map( &li->targets[ target]->oc_map,
683                                                 bv, &mapped, 1 );
684                                 if ( mapped.bv_val == NULL ) {
685                                         free( bv->bv_val );
686                                         bv->bv_val = NULL;
687                                         if ( --last < 0 ) {
688                                                 break;
689                                         }
690                                         *bv = attr->a_vals[ last ];
691                                         attr->a_vals[ last ].bv_val = NULL;
692                                         i--;
693
694                                 } else if ( mapped.bv_val != bv->bv_val ) {
695                                         free( bv->bv_val );
696                                         ber_dupbv( bv, &mapped );
697                                 }
698                         }
699                 /*
700                  * It is necessary to try to rewrite attributes with
701                  * dn syntax because they might be used in ACLs as
702                  * members of groups; since ACLs are applied to the
703                  * rewritten stuff, no dn-based subecj clause could
704                  * be used at the ldap backend side (see
705                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
706                  * The problem can be overcome by moving the dn-based
707                  * ACLs to the target directory server, and letting
708                  * everything pass thru the ldap backend.
709                  */
710                 } else if ( strcmp( attr->a_desc->ad_type->sat_syntax->ssyn_oid,
711                                         SLAPD_DN_SYNTAX ) == 0 ) {
712                         int i;
713                         for ( i = 0, bv = attr->a_vals; bv->bv_val; bv++, i++ ) {
714                                 char *newval;
715
716                                 switch ( rewrite_session( li->targets[ target ]->rwinfo,
717                                                         "searchResult",
718                                                         bv->bv_val,
719                                                         lc->conn, &newval )) {
720                                 case REWRITE_REGEXEC_OK:
721                                         /* left as is */
722                                         if ( newval == NULL ) {
723                                                 break;
724                                         }
725 #ifdef NEW_LOGGING
726                                         LDAP_LOG(( "backend",
727                                                         LDAP_LEVEL_DETAIL1,
728                                                         "[rw] searchResult on"
729                                                         " attr=%s:"
730                                                         " \"%s\" -> \"%s\"\n",
731                                         attr->a_desc->ad_type->sat_cname.bv_val,
732                                                         bv->bv_val, newval ));
733 #else /* !NEW_LOGGING */
734                                         Debug( LDAP_DEBUG_ARGS,
735                                                 "rw> searchResult on attr=%s:"
736                                                 " \"%s\" -> \"%s\"\n",
737                                         attr->a_desc->ad_type->sat_cname.bv_val,
738                                                 bv->bv_val, newval );
739 #endif /* !NEW_LOGGING */
740                                         free( bv->bv_val );
741                                         bv->bv_val = newval;
742                                         bv->bv_len = strlen( newval );
743
744                                         break;
745
746                                 case REWRITE_REGEXEC_UNWILLING:
747                                         
748                                 case REWRITE_REGEXEC_ERR:
749                                         /*
750                                          * FIXME: better give up,
751                                          * skip the attribute
752                                          * or leave it untouched?
753                                          */
754                                         break;
755                                 }
756                         }
757                 }
758                 *attrp = attr;
759                 attrp = &attr->a_next;
760         }
761         send_search_entry( be, lc->conn, op, &ent, attrs, attrsonly, NULL );
762         while ( ent.e_attrs ) {
763                 attr = ent.e_attrs;
764                 ent.e_attrs = attr->a_next;
765                 if ( attr->a_vals != &dummy ) {
766                         ber_bvarray_free( attr->a_vals );
767                 }
768                 free( attr );
769         }
770         
771         if ( ent.e_dn && ent.e_dn != bdn.bv_val ) {
772                 free( ent.e_dn );
773         }
774         if ( ent.e_ndn ) {
775                 free( ent.e_ndn );
776         }
777 }
778
779 static int
780 is_one_level_rdn(
781                 const char      *rdn,
782                 int             from
783 )
784 {
785         for ( ; from--; ) {
786                 if ( DN_SEPARATOR( rdn[ from ] ) ) {
787                         return 0;
788                 }
789         }
790
791         return 1;
792 }
793