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