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