]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
fe2755c67a31971b7cff0dac9549bbebfc1de30c
[openldap] / servers / slapd / back-ldap / search.c
1 /* search.c - ldap backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7 /* This is an altered version */
8 /*
9  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
10  * 
11  * Permission is granted to anyone to use this software for any purpose
12  * on any computer system, and to alter it and redistribute it, subject
13  * to the following restrictions:
14  * 
15  * 1. The author is not responsible for the consequences of use of this
16  *    software, no matter how awful, even if they arise from flaws in it.
17  * 
18  * 2. The origin of this software must not be misrepresented, either by
19  *    explicit claim or by omission.  Since few users ever read sources,
20  *    credits should appear in the documentation.
21  * 
22  * 3. Altered versions must be plainly marked as such, and must not be
23  *    misrepresented as being the original software.  Since few users
24  *    ever read sources, credits should appear in the documentation.
25  * 
26  * 4. This notice may not be removed or altered.
27  *
28  *
29  *
30  * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
31  * 
32  * This software is being modified by Pierangelo Masarati.
33  * The previously reported conditions apply to the modified code as well.
34  * Changes in the original code are highlighted where required.
35  * Credits for the original code go to the author, Howard Chu.
36  */
37
38 #include "portable.h"
39
40 #include <stdio.h>
41
42 #include <ac/socket.h>
43 #include <ac/string.h>
44 #include <ac/time.h>
45
46 #include "slap.h"
47 #include "back-ldap.h"
48 #undef ldap_debug       /* silence a warning in ldap-int.h */
49 #include "../../../libraries/libldap/ldap-int.h"
50
51 #include "lutil.h"
52
53 static int
54 ldap_build_entry( Operation *op, LDAPMessage *e, Entry *ent,
55          struct berval *bdn, int flags );
56 #define LDAP_BUILD_ENTRY_PRIVATE        0x01
57 #define LDAP_BUILD_ENTRY_NORMALIZE      0x02
58
59 static struct berval dummy = { 0, NULL };
60
61 int
62 ldap_back_search(
63     Operation   *op,
64     SlapReply *rs )
65 {
66         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
67         struct ldapconn *lc;
68         struct timeval  tv;
69         LDAPMessage             *res, *e;
70         int     rc = 0, msgid; 
71         char *match = NULL;
72         char **mapped_attrs = NULL;
73         struct berval mbase;
74         struct berval mfilter = { 0, NULL };
75         struct slap_limits_set *limit = NULL;
76         int isroot = 0;
77         dncookie dc;
78
79         lc = ldap_back_getconn(op, rs);
80         if ( !lc ) {
81                 return( -1 );
82         }
83
84         /*
85          * FIXME: in case of values return filter, we might want
86          * to map attrs and maybe rewrite value
87          */
88         if ( !ldap_back_dobind( lc, op, rs ) ) {
89                 return( -1 );
90         }
91
92         /* if not root, get appropriate limits */
93         if ( be_isroot( op->o_bd, &op->o_ndn ) ) {
94                 isroot = 1;
95         } else {
96                 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
97         }
98         
99         /* if no time limit requested, rely on remote server limits */
100         /* if requested limit higher than hard limit, abort */
101         if ( !isroot && op->oq_search.rs_tlimit > limit->lms_t_hard ) {
102                 /* no hard limit means use soft instead */
103                 if ( limit->lms_t_hard == 0
104                                 && limit->lms_t_soft > -1
105                                 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
106                         op->oq_search.rs_tlimit = limit->lms_t_soft;
107                         
108                 /* positive hard limit means abort */
109                 } else if ( limit->lms_t_hard > 0 ) {
110                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
111                         rc = 0;
112                         goto finish;
113                 }
114                 
115                 /* negative hard limit means no limit */
116         }
117         
118         /* if no size limit requested, rely on remote server limits */
119         /* if requested limit higher than hard limit, abort */
120         if ( !isroot && op->oq_search.rs_slimit > limit->lms_s_hard ) {
121                 /* no hard limit means use soft instead */
122                 if ( limit->lms_s_hard == 0
123                                 && limit->lms_s_soft > -1
124                                 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
125                         op->oq_search.rs_slimit = limit->lms_s_soft;
126                         
127                 /* positive hard limit means abort */
128                 } else if ( limit->lms_s_hard > 0 ) {
129                         rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
130                         rc = 0;
131                         goto finish;
132                 }
133                 
134                 /* negative hard limit means no limit */
135         }
136
137         /* should we check return values? */
138         if (op->oq_search.rs_deref != -1)
139                 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->oq_search.rs_deref);
140         if (op->oq_search.rs_tlimit != -1) {
141                 tv.tv_sec = op->oq_search.rs_tlimit;
142                 tv.tv_usec = 0;
143         } else {
144                 tv.tv_sec = 0;
145         }
146
147         /*
148          * Rewrite the search base, if required
149          */
150         dc.li = li;
151 #ifdef ENABLE_REWRITE
152         dc.conn = op->o_conn;
153         dc.rs = rs;
154         dc.ctx = "searchBase";
155 #else
156         dc.tofrom = 1;
157         dc.normalized = 0;
158 #endif
159         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mbase ) ) {
160                 send_ldap_result( op, rs );
161                 return -1;
162         }
163
164 #ifdef ENABLE_REWRITE
165         rc = ldap_back_filter_map_rewrite_( li->rwinfo, op->o_conn,
166                         &li->at_map, &li->oc_map, op->oq_search.rs_filter, &mfilter, 
167                         BACKLDAP_MAP );
168 #else /* ! ENABLE_REWRITE */
169         rc = ldap_back_filter_map_rewrite_( &li->at_map, &li->oc_map, 
170                         op->oq_search.rs_filter, &mfilter, BACKLDAP_MAP );
171 #endif /* ! ENABLE_REWRITE */
172
173         if ( rc ) {
174                 rs->sr_err = LDAP_OTHER;
175                 rs->sr_text = "Rewrite error";
176                 rc = -1;
177                 goto finish;
178         }
179
180         rs->sr_err = ldap_back_map_attrs( &li->at_map, op->oq_search.rs_attrs,
181                         BACKLDAP_MAP, &mapped_attrs );
182         if ( rs->sr_err ) {
183                 rc = -1;
184                 goto finish;
185         }
186
187 #if 0
188         if ( mapped_attrs == NULL && op->oq_search.rs_attrs) {
189                 int count;
190
191                 /* this can happen only if ch_calloc() fails
192                  * in ldap_back_map_attrs() */
193                 for (count=0; op->oq_search.rs_attrs[count].an_name.bv_val; count++);
194                 mapped_attrs = ch_malloc( (count+1) * sizeof(char *));
195                 for (count=0; op->oq_search.rs_attrs[count].an_name.bv_val; count++) {
196                         mapped_attrs[count] = op->oq_search.rs_attrs[count].an_name.bv_val;
197                 }
198                 mapped_attrs[count] = NULL;
199         }
200 #endif
201
202         rs->sr_err = ldap_search_ext(lc->ld, mbase.bv_val,
203                         op->oq_search.rs_scope, mfilter.bv_val,
204                         mapped_attrs, op->oq_search.rs_attrsonly,
205                         op->o_ctrls, NULL,
206                         tv.tv_sec ? &tv : NULL, op->oq_search.rs_slimit,
207                         &msgid);
208         if ( rs->sr_err != LDAP_SUCCESS ) {
209 fail:;
210                 rc = ldap_back_op_result(lc, op, rs, msgid, 0);
211                 goto finish;
212         }
213
214         /* We pull apart the ber result, stuff it into a slapd entry, and
215          * let send_search_entry stuff it back into ber format. Slow & ugly,
216          * but this is necessary for version matching, and for ACL processing.
217          */
218
219         for ( rc=0; rc != -1; rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
220         {
221                 /* check for abandon */
222                 if (op->o_abandon) {
223                         ldap_abandon(lc->ld, msgid);
224                         rc = 0;
225                         goto finish;
226                 }
227
228                 if (rc == 0) {
229                         tv.tv_sec = 0;
230                         tv.tv_usec = 100000;
231                         ldap_pvt_thread_yield();
232
233                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
234                         Entry ent;
235                         struct berval bdn;
236                         e = ldap_first_entry(lc->ld,res);
237                         if ( ldap_build_entry(op, e, &ent, &bdn,
238                                                 LDAP_BUILD_ENTRY_PRIVATE) == LDAP_SUCCESS ) {
239                                 rs->sr_entry = &ent;
240                                 rs->sr_attrs = op->oq_search.rs_attrs;
241                                 send_search_entry( op, rs );
242                                 while (ent.e_attrs) {
243                                         Attribute *a;
244                                         BerVarray v;
245
246                                         a = ent.e_attrs;
247                                         ent.e_attrs = a->a_next;
248
249                                         v = a->a_vals;
250                                         if (a->a_vals != &dummy)
251                                                 ber_bvarray_free(a->a_vals);
252                                         if (a->a_nvals != v)
253                                                 ber_bvarray_free(a->a_nvals);
254                                         ch_free(a);
255                                 }
256                                 
257                                 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
258                                         free( ent.e_dn );
259                                 if ( ent.e_ndn )
260                                         free( ent.e_ndn );
261                         }
262                         ldap_msgfree(res);
263
264                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
265                         char            **references = NULL;
266                         int             cnt;
267
268                         rc = ldap_parse_reference( lc->ld, res,
269                                         &references, &rs->sr_ctrls, 1 );
270
271                         if ( rc != LDAP_SUCCESS ) {
272                                 continue;
273                         }
274
275                         if ( references == NULL ) {
276                                 continue;
277                         }
278
279                         for ( cnt = 0; references[ cnt ]; cnt++ )
280                                 /* NO OP */ ;
281                                 
282                         rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
283
284                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
285                                 rs->sr_ref[ cnt ].bv_val = references[ cnt ];
286                                 rs->sr_ref[ cnt ].bv_len = strlen( references[ cnt ] );
287                         }
288
289                         /* ignore return value by now */
290                         ( void )send_search_reference( op, rs );
291
292                         /* cleanup */
293                         if ( references ) {
294                                 ldap_value_free( references );
295                                 ch_free( rs->sr_ref );
296                                 rs->sr_ref = NULL;
297                         }
298
299                         if ( rs->sr_ctrls ) {
300                                 ldap_controls_free( rs->sr_ctrls );
301                                 rs->sr_ctrls = NULL;
302                         }
303
304                 } else {
305                         rc = ldap_parse_result(lc->ld, res, &rs->sr_err, &match,
306                                 (char **)&rs->sr_text, NULL, NULL, 1);
307                         if (rc != LDAP_SUCCESS ) rs->sr_err = rc;
308                         rs->sr_err = ldap_back_map_result(rs);
309                         rc = 0;
310                         break;
311                 }
312         }
313
314         if (rc == -1)
315                 goto fail;
316
317         /*
318          * Rewrite the matched portion of the search base, if required
319          */
320         if ( match && *match ) {
321                 struct berval dn, mdn;
322
323 #ifdef ENABLE_REWRITE
324                 dc.ctx = "matchedDn";
325 #else
326                 dc.tofrom = 0;
327                 dc.normalized = 0;
328 #endif
329                 ber_str2bv(match, 0, 0, &dn);
330                 ldap_back_dn_massage(&dc, &dn, &mdn);
331                 rs->sr_matched = mdn.bv_val;
332         }
333         if ( rs->sr_v2ref ) {
334                 rs->sr_err = LDAP_REFERRAL;
335         }
336
337 finish:;
338         send_ldap_result( op, rs );
339
340         if ( match ) {
341                 if ( rs->sr_matched != match ) {
342                         free( (char *)rs->sr_matched );
343                 }
344                 rs->sr_matched = NULL;
345                 LDAP_FREE(match);
346         }
347         if ( rs->sr_text ) {
348                 LDAP_FREE( (char *)rs->sr_text );
349                 rs->sr_text = NULL;
350         }
351         if ( mapped_attrs ) {
352                 ch_free( mapped_attrs );
353         }
354         if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
355                 ch_free( mfilter.bv_val );
356         }
357         if ( mbase.bv_val != op->o_req_dn.bv_val ) {
358                 free( mbase.bv_val );
359         }
360         
361         return rc;
362 }
363
364 static int
365 ldap_build_entry(
366         Operation *op,
367         LDAPMessage *e,
368         Entry *ent,
369         struct berval *bdn,
370         int flags
371 )
372 {
373         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
374         struct berval a, mapped;
375         BerElement ber = *e->lm_ber;
376         Attribute *attr, **attrp;
377         struct berval *bv;
378         const char *text;
379         int last;
380         int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
381         int normalize = flags & LDAP_BUILD_ENTRY_NORMALIZE;
382         dncookie dc;
383
384         /* safe assumptions ... */
385         assert( ent );
386         ent->e_bv.bv_val = NULL;
387
388         if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
389                 return LDAP_DECODING_ERROR;
390         }
391
392         /*
393          * Rewrite the dn of the result, if needed
394          */
395         dc.li = li;
396 #ifdef ENABLE_REWRITE
397         dc.conn = op->o_conn;
398         dc.rs = NULL;
399         dc.ctx = "searchResult";
400 #else
401         dc.tofrom = 0;
402         dc.normalized = 0;
403 #endif
404         if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
405                 return LDAP_OTHER;
406         }
407
408         /*
409          * Note: this may fail if the target host(s) schema differs
410          * from the one known to the meta, and a DN with unknown
411          * attributes is returned.
412          * 
413          * FIXME: should we log anything, or delegate to dnNormalize2?
414          */
415         if ( dnNormalize2( NULL, &ent->e_name, &ent->e_nname ) != LDAP_SUCCESS ) {
416                 return LDAP_INVALID_DN_SYNTAX;
417         }
418         
419         ent->e_id = 0;
420         ent->e_attrs = 0;
421         ent->e_private = 0;
422         attrp = &ent->e_attrs;
423
424         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
425                 ldap_back_map(&li->at_map, &a, &mapped, BACKLDAP_REMAP);
426                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
427                         continue;
428                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
429                 if (attr == NULL)
430                         continue;
431                 attr->a_flags = 0;
432                 attr->a_next = 0;
433                 attr->a_desc = NULL;
434                 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
435                         if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text) 
436                                         != LDAP_SUCCESS) {
437 #ifdef NEW_LOGGING
438                                 LDAP_LOG( BACK_LDAP, DETAIL1, 
439                                         "slap_bv2undef_ad(%s):  %s\n", mapped.bv_val, text, 0 );
440 #else /* !NEW_LOGGING */
441                                 Debug( LDAP_DEBUG_ANY, 
442                                                 "slap_bv2undef_ad(%s):  "
443                                                 "%s\n%s", mapped.bv_val, text, "" );
444 #endif /* !NEW_LOGGING */
445                                 ch_free(attr);
446                                 continue;
447                         }
448                 }
449
450                 /* no subschemaSubentry */
451                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
452
453                         /* 
454                          * We eat target's subschemaSubentry because
455                          * a search for this value is likely not
456                          * to resolve to the appropriate backend;
457                          * later, the local subschemaSubentry is
458                          * added.
459                          */
460                         ( void )ber_scanf( &ber, "x" /* [W] */ );
461
462                         ch_free(attr);
463                         continue;
464                 }
465                 
466                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
467                                 || attr->a_vals == NULL ) {
468                         /*
469                          * Note: attr->a_vals can be null when using
470                          * values result filter
471                          */
472                         if (private) {
473                                 attr->a_vals = &dummy;
474                         } else {
475                                 attr->a_vals = ch_malloc(sizeof(struct berval));
476                                 attr->a_vals->bv_val = NULL;
477                                 attr->a_vals->bv_len = 0;
478                         }
479                         last = 0;
480                 } else {
481                         for ( last = 0; attr->a_vals[last].bv_val; last++ );
482                 }
483                 if ( last == 0 ) {
484                         /* empty */
485                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
486                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
487                         for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
488                                 ldap_back_map(&li->oc_map, bv, &mapped,
489                                                 BACKLDAP_REMAP);
490                                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
491                                         LBER_FREE(bv->bv_val);
492                                         bv->bv_val = NULL;
493                                         if (--last < 0)
494                                                 break;
495                                         *bv = attr->a_vals[last];
496                                         attr->a_vals[last].bv_val = NULL;
497                                         bv--;
498
499                                 } else if ( mapped.bv_val != bv->bv_val ) {
500                                         /*
501                                          * FIXME: after LBER_FREEing
502                                          * the value is replaced by
503                                          * ch_alloc'ed memory
504                                          */
505                                         LBER_FREE(bv->bv_val);
506                                         ber_dupbv( bv, &mapped );
507                                 }
508                         }
509
510                 /*
511                  * It is necessary to try to rewrite attributes with
512                  * dn syntax because they might be used in ACLs as
513                  * members of groups; since ACLs are applied to the
514                  * rewritten stuff, no dn-based subject clause could
515                  * be used at the ldap backend side (see
516                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
517                  * The problem can be overcome by moving the dn-based
518                  * ACLs to the target directory server, and letting
519                  * everything pass thru the ldap backend.
520                  */
521                 } else if ( attr->a_desc->ad_type->sat_syntax ==
522                                 slap_schema.si_syn_distinguishedName ) {
523                         for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
524                                 struct berval   newval = {0,NULL};
525                                 
526                                 ldap_back_dn_massage( &dc, bv, &newval );
527                                 if ( newval.bv_val && bv->bv_val != newval.bv_val ) {
528                                         LBER_FREE( bv->bv_val );
529                                         *bv = newval;
530                                 }
531                         }
532                 }
533
534 next_attr:;
535
536                 if ( normalize && last && attr->a_desc->ad_type->sat_equality &&
537                         attr->a_desc->ad_type->sat_equality->smr_normalize ) {
538                         int i;
539
540                         attr->a_nvals = ch_malloc((last+1)*sizeof(struct berval));
541                         for (i=0; i<last; i++) {
542                                 attr->a_desc->ad_type->sat_equality->smr_normalize(
543                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
544                                         attr->a_desc->ad_type->sat_syntax,
545                                         attr->a_desc->ad_type->sat_equality,
546                                         &attr->a_vals[i], &attr->a_nvals[i] );
547                         }
548                         attr->a_nvals[i].bv_val = NULL;
549                         attr->a_nvals[i].bv_len = 0;
550                 } else {
551                         attr->a_nvals = attr->a_vals;
552                 }
553                 *attrp = attr;
554                 attrp = &attr->a_next;
555         }
556         /* make sure it's free'able */
557         if (!private && ent->e_name.bv_val == bdn->bv_val)
558                 ber_dupbv( &ent->e_name, bdn );
559         return LDAP_SUCCESS;
560 }
561
562 /* return 0 IFF we can retrieve the entry with ndn
563  */
564 int
565 ldap_back_entry_get(
566         Operation *op,
567         struct berval   *ndn,
568         ObjectClass *oc,
569         AttributeDescription *at,
570         int rw,
571         Entry **ent
572 )
573 {
574         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;    
575         struct ldapconn *lc;
576         int rc = 1, is_oc;
577         struct berval mapped = { 0, NULL }, bdn, mdn;
578         LDAPMessage     *result = NULL, *e = NULL;
579         char *gattr[3];
580         char *filter = NULL;
581         Connection *oconn;
582         SlapReply rs;
583         dncookie dc;
584
585         /* Tell getconn this is a privileged op */
586         is_oc = op->o_do_not_cache;
587         op->o_do_not_cache = 1;
588         lc = ldap_back_getconn(op, &rs);
589         oconn = op->o_conn;
590         op->o_conn = NULL;
591         if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
592                 op->o_do_not_cache = is_oc;
593                 op->o_conn = oconn;
594                 return 1;
595         }
596         op->o_do_not_cache = is_oc;
597         op->o_conn = oconn;
598
599         /*
600          * Rewrite the search base, if required
601          */
602         dc.li = li;
603 #ifdef ENABLE_REWRITE
604         dc.conn = op->o_conn;
605         dc.rs = &rs;
606         dc.ctx = "searchBase";
607 #else
608         dc.tofrom = 1;
609         dc.normalized = 1;
610 #endif
611         if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
612                 return 1;
613         }
614
615         ldap_back_map(&li->at_map, &at->ad_cname, &mapped, BACKLDAP_MAP);
616         if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
617                 rc = 1;
618                 goto cleanup;
619         }
620
621         is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
622         if (oc && !is_oc) {
623                 gattr[0] = "objectclass";
624                 gattr[1] = mapped.bv_val;
625                 gattr[2] = NULL;
626         } else {
627                 gattr[0] = mapped.bv_val;
628                 gattr[1] = NULL;
629         }
630         if (oc) {
631                 char *ptr;
632                 ldap_back_map(&li->oc_map, &oc->soc_cname, &mapped,
633                                                 BACKLDAP_MAP);
634                 filter = ch_malloc(sizeof("(objectclass=)") + mapped.bv_len);
635                 ptr = lutil_strcopy(filter, "(objectclass=");
636                 ptr = lutil_strcopy(ptr, mapped.bv_val);
637                 *ptr++ = ')';
638                 *ptr++ = '\0';
639         }
640                 
641         if (ldap_search_ext_s(lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
642                                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
643                                 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
644         {
645                 goto cleanup;
646         }
647
648         if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
649                 goto cleanup;
650         }
651
652         *ent = ch_calloc(1,sizeof(Entry));
653
654         rc = ldap_build_entry(op, e, *ent, &bdn, LDAP_BUILD_ENTRY_NORMALIZE);
655
656         if (rc != LDAP_SUCCESS) {
657                 ch_free(*ent);
658                 *ent = NULL;
659         }
660
661 cleanup:
662         if (result) {
663                 ldap_msgfree(result);
664         }
665
666         if ( filter ) {
667                 ch_free( filter );
668         }
669
670         if ( mdn.bv_val != ndn->bv_val ) {
671                 ch_free( mdn.bv_val );
672         }
673
674         return(rc);
675 }
676