]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
4dbb0488f8431ca421f986727849ae935eaf7d46
[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, &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         switch ( rc ) {
158         case LDAP_SUCCESS:
159                 break;
160
161         case LDAP_COMPARE_FALSE:
162                 rs->sr_err = LDAP_SUCCESS;
163                 rs->sr_text = NULL;
164                 rc = 0;
165                 goto finish;
166
167         default:
168                 rs->sr_err = LDAP_OTHER;
169                 rs->sr_text = "Rewrite error";
170                 dontfreetext = 1;
171                 rc = -1;
172                 goto finish;
173         }
174
175         rs->sr_err = ldap_back_map_attrs( &li->rwmap.rwm_at,
176                         op->oq_search.rs_attrs,
177                         BACKLDAP_MAP, &mapped_attrs );
178         if ( rs->sr_err ) {
179                 rc = -1;
180                 goto finish;
181         }
182
183 #ifdef LDAP_BACK_PROXY_AUTHZ
184         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
185         if ( rc != LDAP_SUCCESS ) {
186                 dontfreetext = 1;
187                 goto finish;
188         }
189 #endif /* LDAP_BACK_PROXY_AUTHZ */
190         
191         rs->sr_err = ldap_search_ext(lc->ld, mbase.bv_val,
192                         op->oq_search.rs_scope, mfilter.bv_val,
193                         mapped_attrs, op->oq_search.rs_attrsonly,
194 #ifdef LDAP_BACK_PROXY_AUTHZ
195                         ctrls,
196 #else /* ! LDAP_BACK_PROXY_AUTHZ */
197                         op->o_ctrls,
198 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
199                         NULL,
200                         tv.tv_sec ? &tv : NULL, op->oq_search.rs_slimit,
201                         &msgid );
202
203         if ( rs->sr_err != LDAP_SUCCESS ) {
204 fail:;
205                 rc = ldap_back_op_result(lc, op, rs, msgid, 0);
206                 goto finish;
207         }
208
209         /* We pull apart the ber result, stuff it into a slapd entry, and
210          * let send_search_entry stuff it back into ber format. Slow & ugly,
211          * but this is necessary for version matching, and for ACL processing.
212          */
213
214         for ( rc=0; rc != -1; rc = ldap_result(lc->ld, msgid, 0, &tv, &res))
215         {
216                 /* check for abandon */
217                 if (op->o_abandon) {
218                         ldap_abandon(lc->ld, msgid);
219                         rc = 0;
220                         goto finish;
221                 }
222
223                 if (rc == 0) {
224                         tv.tv_sec = 0;
225                         tv.tv_usec = 100000;
226                         ldap_pvt_thread_yield();
227
228                 } else if (rc == LDAP_RES_SEARCH_ENTRY) {
229                         Entry ent = {0};
230                         struct berval bdn;
231                         int abort = 0;
232                         e = ldap_first_entry(lc->ld,res);
233                         if ( ( rc = ldap_build_entry(op, e, &ent, &bdn,
234                                                 LDAP_BUILD_ENTRY_PRIVATE)) == LDAP_SUCCESS ) {
235                                 rs->sr_entry = &ent;
236                                 rs->sr_attrs = op->oq_search.rs_attrs;
237                                 abort = send_search_entry( op, rs );
238                                 while (ent.e_attrs) {
239                                         Attribute *a;
240                                         BerVarray v;
241
242                                         a = ent.e_attrs;
243                                         ent.e_attrs = a->a_next;
244
245                                         v = a->a_vals;
246                                         if (a->a_vals != &dummy)
247                                                 ber_bvarray_free(a->a_vals);
248                                         if (a->a_nvals != v)
249                                                 ber_bvarray_free(a->a_nvals);
250                                         ch_free(a);
251                                 }
252                                 
253                                 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
254                                         free( ent.e_dn );
255                                 if ( ent.e_ndn )
256                                         free( ent.e_ndn );
257                         }
258                         ldap_msgfree(res);
259                         if ( abort ) {
260                                 ldap_abandon(lc->ld, msgid);
261                                 goto finish;
262                         }
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,
306                                         &match.bv_val, (char **)&rs->sr_text,
307                                         NULL, NULL, 1);
308                         if (rc != LDAP_SUCCESS ) rs->sr_err = rc;
309                         rs->sr_err = ldap_back_map_result(rs);
310                         rc = 0;
311                         break;
312                 }
313         }
314
315         if (rc == -1)
316                 goto fail;
317
318         /*
319          * Rewrite the matched portion of the search base, if required
320          */
321         if ( match.bv_val && *match.bv_val ) {
322                 struct berval mdn;
323
324 #ifdef ENABLE_REWRITE
325                 dc.ctx = "matchedDn";
326 #else
327                 dc.tofrom = 0;
328                 dc.normalized = 0;
329 #endif
330                 match.bv_len = strlen( match.bv_val );
331                 ldap_back_dn_massage(&dc, &match, &mdn);
332                 rs->sr_matched = mdn.bv_val;
333         }
334         if ( rs->sr_v2ref ) {
335                 rs->sr_err = LDAP_REFERRAL;
336         }
337
338 finish:;
339         send_ldap_result( op, rs );
340
341 #ifdef LDAP_BACK_PROXY_AUTHZ
342         if ( ctrls && ctrls != op->o_ctrls ) {
343                 free( ctrls[ 0 ] );
344                 free( ctrls );
345         }
346 #endif /* LDAP_BACK_PROXY_AUTHZ */
347
348         if ( match.bv_val ) {
349                 if ( rs->sr_matched != match.bv_val ) {
350                         free( (char *)rs->sr_matched );
351                 }
352                 rs->sr_matched = NULL;
353                 LDAP_FREE( match.bv_val );
354         }
355         if ( rs->sr_text ) {
356                 if ( !dontfreetext ) {
357                         LDAP_FREE( (char *)rs->sr_text );
358                 }
359                 rs->sr_text = NULL;
360         }
361         if ( mapped_attrs ) {
362                 ch_free( mapped_attrs );
363         }
364         if ( mfilter.bv_val != op->oq_search.rs_filterstr.bv_val ) {
365                 ch_free( mfilter.bv_val );
366         }
367         if ( mbase.bv_val != op->o_req_dn.bv_val ) {
368                 free( mbase.bv_val );
369         }
370         
371         return rc;
372 }
373
374 static int
375 ldap_build_entry(
376         Operation *op,
377         LDAPMessage *e,
378         Entry *ent,
379         struct berval *bdn,
380         int flags
381 )
382 {
383         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
384         struct berval a, mapped;
385         BerElement ber = *e->lm_ber;
386         Attribute *attr, **attrp;
387         struct berval *bv;
388         const char *text;
389         int last;
390         int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
391         int normalize = flags & LDAP_BUILD_ENTRY_NORMALIZE;
392         dncookie dc;
393
394         /* safe assumptions ... */
395         assert( ent );
396         ent->e_bv.bv_val = NULL;
397
398         if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
399                 return LDAP_DECODING_ERROR;
400         }
401
402         /*
403          * Rewrite the dn of the result, if needed
404          */
405         dc.rwmap = &li->rwmap;
406 #ifdef ENABLE_REWRITE
407         dc.conn = op->o_conn;
408         dc.rs = NULL;
409         dc.ctx = "searchResult";
410 #else
411         dc.tofrom = 0;
412         dc.normalized = 0;
413 #endif
414         if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
415                 return LDAP_OTHER;
416         }
417
418         /*
419          * Note: this may fail if the target host(s) schema differs
420          * from the one known to the meta, and a DN with unknown
421          * attributes is returned.
422          * 
423          * FIXME: should we log anything, or delegate to dnNormalize?
424          */
425         if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname,
426                 op->o_tmpmemctx ) != LDAP_SUCCESS )
427         {
428                 return LDAP_INVALID_DN_SYNTAX;
429         }
430         
431         attrp = &ent->e_attrs;
432
433 #ifdef ENABLE_REWRITE
434         dc.ctx = "searchAttrDN";
435 #endif
436         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
437                 ldap_back_map(&li->rwmap.rwm_at, &a, &mapped, BACKLDAP_REMAP);
438                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0')
439                         continue;
440                 attr = (Attribute *)ch_malloc( sizeof(Attribute) );
441                 if (attr == NULL)
442                         continue;
443                 attr->a_flags = 0;
444                 attr->a_next = 0;
445                 attr->a_desc = NULL;
446                 if (slap_bv2ad(&mapped, &attr->a_desc, &text) != LDAP_SUCCESS) {
447                         if (slap_bv2undef_ad(&mapped, &attr->a_desc, &text) 
448                                         != LDAP_SUCCESS) {
449 #ifdef NEW_LOGGING
450                                 LDAP_LOG( BACK_LDAP, DETAIL1, 
451                                         "slap_bv2undef_ad(%s):  %s\n", mapped.bv_val, text, 0 );
452 #else /* !NEW_LOGGING */
453                                 Debug( LDAP_DEBUG_ANY, 
454                                                 "slap_bv2undef_ad(%s):  "
455                                                 "%s\n%s", mapped.bv_val, text, "" );
456 #endif /* !NEW_LOGGING */
457                                 ch_free(attr);
458                                 continue;
459                         }
460                 }
461
462                 /* no subschemaSubentry */
463                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
464
465                         /* 
466                          * We eat target's subschemaSubentry because
467                          * a search for this value is likely not
468                          * to resolve to the appropriate backend;
469                          * later, the local subschemaSubentry is
470                          * added.
471                          */
472                         ( void )ber_scanf( &ber, "x" /* [W] */ );
473
474                         ch_free(attr);
475                         continue;
476                 }
477                 
478                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
479                                 || attr->a_vals == NULL ) {
480                         /*
481                          * Note: attr->a_vals can be null when using
482                          * values result filter
483                          */
484                         if (private) {
485                                 attr->a_vals = &dummy;
486                         } else {
487                                 attr->a_vals = ch_malloc(sizeof(struct berval));
488                                 attr->a_vals->bv_val = NULL;
489                                 attr->a_vals->bv_len = 0;
490                         }
491                         last = 0;
492                 } else {
493                         for ( last = 0; attr->a_vals[last].bv_val; last++ );
494                 }
495                 if ( last == 0 ) {
496                         /* empty */
497                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
498                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass ) {
499                         for ( bv = attr->a_vals; bv->bv_val; bv++ ) {
500                                 ldap_back_map(&li->rwmap.rwm_oc, bv, &mapped,
501                                                 BACKLDAP_REMAP);
502                                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
503                                         LBER_FREE(bv->bv_val);
504                                         bv->bv_val = NULL;
505                                         if (--last < 0)
506                                                 break;
507                                         *bv = attr->a_vals[last];
508                                         attr->a_vals[last].bv_val = NULL;
509                                         bv--;
510
511                                 } else if ( mapped.bv_val != bv->bv_val ) {
512                                         /*
513                                          * FIXME: after LBER_FREEing
514                                          * the value is replaced by
515                                          * ch_alloc'ed memory
516                                          */
517                                         LBER_FREE(bv->bv_val);
518                                         ber_dupbv( bv, &mapped );
519                                 }
520                         }
521
522                 /*
523                  * It is necessary to try to rewrite attributes with
524                  * dn syntax because they might be used in ACLs as
525                  * members of groups; since ACLs are applied to the
526                  * rewritten stuff, no dn-based subject clause could
527                  * be used at the ldap backend side (see
528                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
529                  * The problem can be overcome by moving the dn-based
530                  * ACLs to the target directory server, and letting
531                  * everything pass thru the ldap backend.
532                  */
533                 } else if ( attr->a_desc->ad_type->sat_syntax ==
534                                 slap_schema.si_syn_distinguishedName ) {
535                         ldap_dnattr_result_rewrite( &dc, attr->a_vals );
536                 }
537
538                 if ( normalize && last && attr->a_desc->ad_type->sat_equality &&
539                         attr->a_desc->ad_type->sat_equality->smr_normalize ) {
540                         int i;
541
542                         attr->a_nvals = ch_malloc((last+1)*sizeof(struct berval));
543                         for (i=0; i<last; i++) {
544                                 attr->a_desc->ad_type->sat_equality->smr_normalize(
545                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
546                                         attr->a_desc->ad_type->sat_syntax,
547                                         attr->a_desc->ad_type->sat_equality,
548                                         &attr->a_vals[i], &attr->a_nvals[i],
549                                         NULL /* op->o_tmpmemctx */ );
550                         }
551                         attr->a_nvals[i].bv_val = NULL;
552                         attr->a_nvals[i].bv_len = 0;
553                 } else {
554                         attr->a_nvals = attr->a_vals;
555                 }
556                 *attrp = attr;
557                 attrp = &attr->a_next;
558         }
559
560         /* make sure it's free'able */
561         if (!private && ent->e_name.bv_val == bdn->bv_val)
562                 ber_dupbv( &ent->e_name, bdn );
563         return LDAP_SUCCESS;
564 }
565
566 /* return 0 IFF we can retrieve the entry with ndn
567  */
568 int
569 ldap_back_entry_get(
570         Operation *op,
571         struct berval   *ndn,
572         ObjectClass *oc,
573         AttributeDescription *at,
574         int rw,
575         Entry **ent
576 )
577 {
578         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;    
579         struct ldapconn *lc;
580         int rc = 1, is_oc;
581         struct berval mapped = { 0, NULL }, bdn, mdn;
582         LDAPMessage     *result = NULL, *e = NULL;
583         char *gattr[3];
584         char *filter = NULL;
585         Connection *oconn;
586         SlapReply rs;
587         dncookie dc;
588
589         /* Tell getconn this is a privileged op */
590         is_oc = op->o_do_not_cache;
591         op->o_do_not_cache = 1;
592         lc = ldap_back_getconn(op, &rs);
593         oconn = op->o_conn;
594         op->o_conn = NULL;
595         if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
596                 op->o_do_not_cache = is_oc;
597                 op->o_conn = oconn;
598                 return 1;
599         }
600         op->o_do_not_cache = is_oc;
601         op->o_conn = oconn;
602
603         /*
604          * Rewrite the search base, if required
605          */
606         dc.rwmap = &li->rwmap;
607 #ifdef ENABLE_REWRITE
608         dc.conn = op->o_conn;
609         dc.rs = &rs;
610         dc.ctx = "searchBase";
611 #else
612         dc.tofrom = 1;
613         dc.normalized = 1;
614 #endif
615         if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
616                 return 1;
617         }
618
619         if ( at ) {
620                 ldap_back_map(&li->rwmap.rwm_at, &at->ad_cname, &mapped, BACKLDAP_MAP);
621                 if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
622                         rc = 1;
623                         goto cleanup;
624                 }
625         }
626
627         is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
628         if (oc && !is_oc) {
629                 gattr[0] = "objectclass";
630                 gattr[1] = mapped.bv_val;
631                 gattr[2] = NULL;
632         } else {
633                 gattr[0] = mapped.bv_val;
634                 gattr[1] = NULL;
635         }
636         if (oc) {
637                 char *ptr;
638                 ldap_back_map(&li->rwmap.rwm_oc, &oc->soc_cname, &mapped,
639                                                 BACKLDAP_MAP);
640                 filter = ch_malloc(sizeof("(objectclass=)") + mapped.bv_len);
641                 ptr = lutil_strcopy(filter, "(objectclass=");
642                 ptr = lutil_strcopy(ptr, mapped.bv_val);
643                 *ptr++ = ')';
644                 *ptr++ = '\0';
645         }
646
647         if (ldap_search_ext_s(lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
648                                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
649                                 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
650         {
651                 goto cleanup;
652         }
653
654         if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
655                 goto cleanup;
656         }
657
658         *ent = ch_calloc(1,sizeof(Entry));
659
660         rc = ldap_build_entry(op, e, *ent, &bdn, LDAP_BUILD_ENTRY_NORMALIZE);
661
662         if (rc != LDAP_SUCCESS) {
663                 ch_free(*ent);
664                 *ent = NULL;
665         }
666
667 cleanup:
668         if (result) {
669                 ldap_msgfree(result);
670         }
671
672         if ( filter ) {
673                 ch_free( filter );
674         }
675
676         if ( mdn.bv_val != ndn->bv_val ) {
677                 ch_free( mdn.bv_val );
678         }
679
680         return(rc);
681 }
682