]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
e05a62654664e1f08afadfb15611eeb9c5c60ab8
[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
44 static struct berval dummy = BER_BVNULL;
45
46 int
47 ldap_back_search(
48     Operation   *op,
49     SlapReply *rs )
50 {
51         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
52         struct ldapconn *lc;
53         struct timeval  tv;
54         LDAPMessage             *res, *e;
55         int     rc = 0, msgid; 
56         struct berval match = BER_BVNULL;
57         char **mapped_attrs = NULL;
58         struct berval mbase;
59         struct berval mfilter = BER_BVNULL;
60         int dontfreetext = 0;
61         int freeconn = 0;
62         dncookie dc;
63         LDAPControl **ctrls = NULL;
64
65         lc = ldap_back_getconn(op, rs);
66         if ( !lc ) {
67                 return( -1 );
68         }
69
70         /*
71          * FIXME: in case of values return filter, we might want
72          * to map attrs and maybe rewrite value
73          */
74         if ( !ldap_back_dobind( lc, op, rs ) ) {
75                 return( -1 );
76         }
77
78         /* should we check return values? */
79         if ( op->ors_deref != -1 ) {
80                 ldap_set_option( lc->ld, LDAP_OPT_DEREF, (void *)&op->ors_deref );
81         }
82
83         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
84                 tv.tv_sec = op->ors_tlimit;
85                 tv.tv_usec = 0;
86
87         } else {
88                 tv.tv_sec = 0;
89         }
90
91         /*
92          * Rewrite the search base, if required
93          */
94         dc.rwmap = &li->rwmap;
95 #ifdef ENABLE_REWRITE
96         dc.conn = op->o_conn;
97         dc.rs = rs;
98         dc.ctx = "searchBase";
99 #else
100         dc.tofrom = 1;
101         dc.normalized = 0;
102 #endif
103         if ( ldap_back_dn_massage( &dc, &op->o_req_ndn, &mbase ) ) {
104                 send_ldap_result( op, rs );
105                 return -1;
106         }
107
108         rc = ldap_back_filter_map_rewrite( &dc, op->ors_filter,
109                         &mfilter, BACKLDAP_MAP );
110
111         switch ( rc ) {
112         case LDAP_SUCCESS:
113                 break;
114
115         case LDAP_COMPARE_FALSE:
116                 rs->sr_err = LDAP_SUCCESS;
117                 rs->sr_text = NULL;
118                 rc = 0;
119                 goto finish;
120
121         default:
122                 rs->sr_err = LDAP_OTHER;
123                 rs->sr_text = "Rewrite error";
124                 dontfreetext = 1;
125                 rc = -1;
126                 goto finish;
127         }
128
129         rs->sr_err = ldap_back_map_attrs( &li->rwmap.rwm_at,
130                         op->ors_attrs,
131                         BACKLDAP_MAP, &mapped_attrs );
132         if ( rs->sr_err != LDAP_SUCCESS ) {
133                 rc = -1;
134                 goto finish;
135         }
136
137         ctrls = op->o_ctrls;
138 #ifdef LDAP_BACK_PROXY_AUTHZ
139         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
140         if ( rc != LDAP_SUCCESS ) {
141                 dontfreetext = 1;
142                 goto finish;
143         }
144 #endif /* LDAP_BACK_PROXY_AUTHZ */
145         
146         rs->sr_err = ldap_search_ext( lc->ld, mbase.bv_val,
147                         op->ors_scope, mfilter.bv_val,
148                         mapped_attrs, op->ors_attrsonly,
149                         ctrls, NULL,
150                         tv.tv_sec ? &tv : NULL, op->ors_slimit,
151                         &msgid );
152
153         if ( rs->sr_err != LDAP_SUCCESS ) {
154 fail:;
155                 rc = ldap_back_op_result( lc, op, rs, msgid, 0 );
156                 if ( freeconn ) {
157                         ldap_back_freeconn( op, lc );
158                         lc = NULL;
159                 }
160                 goto finish;
161         }
162
163         /* We pull apart the ber result, stuff it into a slapd entry, and
164          * let send_search_entry stuff it back into ber format. Slow & ugly,
165          * but this is necessary for version matching, and for ACL processing.
166          */
167
168         for ( rc = 0; rc != -1; rc = ldap_result( lc->ld, msgid, 0, &tv, &res ) )
169         {
170                 /* check for abandon */
171                 if ( op->o_abandon ) {
172                         ldap_abandon( lc->ld, msgid );
173                         rc = 0;
174                         goto finish;
175                 }
176
177                 if ( rc == 0 ) {
178                         tv.tv_sec = 0;
179                         tv.tv_usec = 100000;
180                         ldap_pvt_thread_yield();
181
182                 } else if ( rc == LDAP_RES_SEARCH_ENTRY ) {
183                         Entry ent = {0};
184                         struct berval bdn;
185                         int abort = 0;
186                         e = ldap_first_entry( lc->ld, res );
187                         rc = ldap_build_entry( op, e, &ent, &bdn,
188                                         LDAP_BUILD_ENTRY_PRIVATE );
189                        if ( rc == LDAP_SUCCESS ) {
190                                 rs->sr_entry = &ent;
191                                 rs->sr_attrs = op->ors_attrs;
192                                 rs->sr_flags = 0;
193                                 abort = send_search_entry( op, rs );
194                                 while (ent.e_attrs) {
195                                         Attribute *a;
196                                         BerVarray v;
197
198                                         a = ent.e_attrs;
199                                         ent.e_attrs = a->a_next;
200
201                                         v = a->a_vals;
202                                         if ( a->a_vals != &dummy ) {
203                                                 ber_bvarray_free(a->a_vals);
204                                         }
205                                         if ( a->a_nvals != v ) {
206                                                 ber_bvarray_free(a->a_nvals);
207                                         }
208                                         ch_free(a);
209                                 }
210                                 
211                                 if ( ent.e_dn && ( ent.e_dn != bdn.bv_val ) )
212                                         free( ent.e_dn );
213                                 if ( ent.e_ndn )
214                                         free( ent.e_ndn );
215                         }
216                         ldap_msgfree( res );
217                         if ( abort ) {
218                                 ldap_abandon( lc->ld, msgid );
219                                 goto finish;
220                         }
221
222                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
223                         char            **references = NULL;
224                         int             cnt;
225
226                         rc = ldap_parse_reference( lc->ld, res,
227                                         &references, &rs->sr_ctrls, 1 );
228
229                         if ( rc != LDAP_SUCCESS ) {
230                                 continue;
231                         }
232
233                         if ( references == NULL ) {
234                                 continue;
235                         }
236
237                         for ( cnt = 0; references[ cnt ]; cnt++ )
238                                 /* NO OP */ ;
239                                 
240                         rs->sr_ref = ch_calloc( cnt + 1, sizeof( struct berval ) );
241
242                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
243                                 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
244                         }
245
246                         /* ignore return value by now */
247                         ( void )send_search_reference( op, rs );
248
249                         /* cleanup */
250                         if ( references ) {
251                                 ldap_value_free( references );
252                                 ch_free( rs->sr_ref );
253                                 rs->sr_ref = NULL;
254                         }
255
256                         if ( rs->sr_ctrls ) {
257                                 ldap_controls_free( rs->sr_ctrls );
258                                 rs->sr_ctrls = NULL;
259                         }
260
261                 } else {
262                         rc = ldap_parse_result( lc->ld, res, &rs->sr_err,
263                                         &match.bv_val, (char **)&rs->sr_text,
264                                         NULL, NULL, 1 );
265                         if (rc != LDAP_SUCCESS ) {
266                                 rs->sr_err = rc;
267                         }
268                         rs->sr_err = slap_map_api2result( rs );
269                         rc = 0;
270                         break;
271                 }
272         }
273
274         if ( rc == -1 ) {
275                 /* FIXME: invalidate the connection? */
276                 rs->sr_err = LDAP_SERVER_DOWN;
277                 freeconn = 1;
278                 goto fail;
279         }
280
281         /*
282          * Rewrite the matched portion of the search base, if required
283          */
284         if ( match.bv_val && *match.bv_val ) {
285                 struct berval mdn;
286
287 #ifdef ENABLE_REWRITE
288                 dc.ctx = "matchedDN";
289 #else
290                 dc.tofrom = 0;
291                 dc.normalized = 0;
292 #endif
293                 match.bv_len = strlen( match.bv_val );
294                 ldap_back_dn_massage( &dc, &match, &mdn );
295                 rs->sr_matched = mdn.bv_val;
296         }
297         if ( rs->sr_v2ref ) {
298                 rs->sr_err = LDAP_REFERRAL;
299         }
300
301 finish:;
302         send_ldap_result( op, rs );
303
304 #ifdef LDAP_BACK_PROXY_AUTHZ
305         if ( ctrls && ctrls != op->o_ctrls ) {
306                 free( ctrls[ 0 ] );
307                 free( ctrls );
308         }
309 #endif /* LDAP_BACK_PROXY_AUTHZ */
310
311         if ( match.bv_val ) {
312                 if ( rs->sr_matched != match.bv_val ) {
313                         free( (char *)rs->sr_matched );
314                 }
315                 rs->sr_matched = NULL;
316                 LDAP_FREE( match.bv_val );
317         }
318         if ( rs->sr_text ) {
319                 if ( !dontfreetext ) {
320                         LDAP_FREE( (char *)rs->sr_text );
321                 }
322                 rs->sr_text = NULL;
323         }
324         if ( mapped_attrs ) {
325                 ch_free( mapped_attrs );
326         }
327         if ( mfilter.bv_val != op->ors_filterstr.bv_val ) {
328                 ch_free( mfilter.bv_val );
329         }
330         if ( mbase.bv_val != op->o_req_ndn.bv_val ) {
331                 free( mbase.bv_val );
332         }
333         
334         return rc;
335 }
336
337 static int
338 ldap_build_entry(
339         Operation *op,
340         LDAPMessage *e,
341         Entry *ent,
342         struct berval *bdn,
343         int flags
344 )
345 {
346         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
347         struct berval a, mapped;
348         BerElement ber = *e->lm_ber;
349         Attribute *attr, **attrp;
350         struct berval *bv;
351         const char *text;
352         int last;
353         int private = flags & LDAP_BUILD_ENTRY_PRIVATE;
354         dncookie dc;
355
356         /* safe assumptions ... */
357         assert( ent );
358         ent->e_bv.bv_val = NULL;
359
360         if ( ber_scanf( &ber, "{m{", bdn ) == LBER_ERROR ) {
361                 return LDAP_DECODING_ERROR;
362         }
363
364         /*
365          * Rewrite the dn of the result, if needed
366          */
367         dc.rwmap = &li->rwmap;
368 #ifdef ENABLE_REWRITE
369         dc.conn = op->o_conn;
370         dc.rs = NULL;
371         dc.ctx = "searchResult";
372 #else
373         dc.tofrom = 0;
374         dc.normalized = 0;
375 #endif
376         if ( ldap_back_dn_massage( &dc, bdn, &ent->e_name ) ) {
377                 return LDAP_OTHER;
378         }
379
380         /*
381          * Note: this may fail if the target host(s) schema differs
382          * from the one known to the meta, and a DN with unknown
383          * attributes is returned.
384          * 
385          * FIXME: should we log anything, or delegate to dnNormalize?
386          */
387         /* Note: if the distinguished values or the naming attributes
388          * change, should we massage them as well?
389          */
390         if ( dnNormalize( 0, NULL, NULL, &ent->e_name, &ent->e_nname,
391                 op->o_tmpmemctx ) != LDAP_SUCCESS )
392         {
393                 return LDAP_INVALID_DN_SYNTAX;
394         }
395
396         attrp = &ent->e_attrs;
397
398 #ifdef ENABLE_REWRITE
399         dc.ctx = "searchAttrDN";
400 #endif
401         while ( ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
402                 int             i;
403                 slap_syntax_validate_func *validate =
404                         attr->a_desc->ad_type->sat_syntax->ssyn_validate;
405                 slap_syntax_transform_func *pretty =
406                         attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
407
408                 ldap_back_map( &li->rwmap.rwm_at, &a, &mapped, BACKLDAP_REMAP );
409                 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) )
410                         continue;
411                 attr = (Attribute *)ch_malloc( sizeof( Attribute ) );
412                 if ( attr == NULL )
413                         continue;
414                 attr->a_flags = 0;
415                 attr->a_next = 0;
416                 attr->a_desc = NULL;
417                 if ( slap_bv2ad( &mapped, &attr->a_desc, &text ) != LDAP_SUCCESS ) {
418                         if ( slap_bv2undef_ad( &mapped, &attr->a_desc, &text ) 
419                                         != LDAP_SUCCESS )
420                         {
421 #ifdef NEW_LOGGING
422                                 LDAP_LOG( BACK_LDAP, DETAIL1, 
423                                         "slap_bv2undef_ad(%s):  %s\n", mapped.bv_val, text, 0 );
424 #else /* !NEW_LOGGING */
425                                 Debug( LDAP_DEBUG_ANY, 
426                                         "slap_bv2undef_ad(%s):  %s\n", mapped.bv_val, text, 0 );
427 #endif /* !NEW_LOGGING */
428                                 ch_free( attr );
429                                 continue;
430                         }
431                 }
432
433                 /* no subschemaSubentry */
434                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry ) {
435
436                         /* 
437                          * We eat target's subschemaSubentry because
438                          * a search for this value is likely not
439                          * to resolve to the appropriate backend;
440                          * later, the local subschemaSubentry is
441                          * added.
442                          */
443                         ( void )ber_scanf( &ber, "x" /* [W] */ );
444
445                         ch_free(attr);
446                         continue;
447                 }
448                 
449                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
450                                 || attr->a_vals == NULL )
451                 {
452                         /*
453                          * Note: attr->a_vals can be null when using
454                          * values result filter
455                          */
456                         if ( private ) {
457                                 attr->a_vals = &dummy;
458                         } else {
459                                 attr->a_vals = ch_malloc( sizeof( struct berval ) );
460                                 BER_BVZERO( &attr->a_vals[0] );
461                         }
462                         last = 0;
463
464                 } else {
465                         for ( last = 0; !BER_BVISNULL( &attr->a_vals[last] ); last++ );
466                 }
467
468                 if ( last == 0 ) {
469                         /* empty */
470                 } else if ( attr->a_desc == slap_schema.si_ad_objectClass
471                                 || attr->a_desc == slap_schema.si_ad_structuralObjectClass )
472                 {
473                         for ( bv = attr->a_vals; !BER_BVISNULL( bv ); bv++ ) {
474                                 ldap_back_map( &li->rwmap.rwm_oc, bv, &mapped,
475                                                 BACKLDAP_REMAP );
476                                 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
477                                         LBER_FREE( bv->bv_val );
478                                         BER_BVZERO( bv );
479                                         if (--last < 0)
480                                                 break;
481                                         *bv = attr->a_vals[last];
482                                         BER_BVZERO( &attr->a_vals[last] );
483                                         bv--;
484
485                                 } else if ( mapped.bv_val != bv->bv_val ) {
486                                         /*
487                                          * FIXME: after LBER_FREEing
488                                          * the value is replaced by
489                                          * ch_alloc'ed memory
490                                          */
491                                         LBER_FREE( bv->bv_val );
492                                         ber_dupbv( bv, &mapped );
493                                 }
494                         }
495
496                 /*
497                  * It is necessary to try to rewrite attributes with
498                  * dn syntax because they might be used in ACLs as
499                  * members of groups; since ACLs are applied to the
500                  * rewritten stuff, no dn-based subject clause could
501                  * be used at the ldap backend side (see
502                  * http://www.OpenLDAP.org/faq/data/cache/452.html)
503                  * The problem can be overcome by moving the dn-based
504                  * ACLs to the target directory server, and letting
505                  * everything pass thru the ldap backend.
506                  */
507                 } else if ( attr->a_desc->ad_type->sat_syntax ==
508                                 slap_schema.si_syn_distinguishedName )
509                 {
510                         ldap_dnattr_result_rewrite( &dc, attr->a_vals );
511                 }
512
513                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
514                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
515
516                 if ( !validate && !pretty ) {
517                         attr->a_nvals = NULL;
518                         attr_free( attr );
519                         goto next_attr;
520                 }
521
522                 for ( i = 0; i < last; i++ ) {
523                         struct berval   pval;
524                         int             rc;
525
526                         if ( pretty ) {
527                                 rc = pretty( attr->a_desc->ad_type->sat_syntax,
528                                         &attr->a_vals[i], &pval, NULL );
529                         } else {
530                                 rc = validate( attr->a_desc->ad_type->sat_syntax,
531                                         &attr->a_vals[i] );
532                         }
533
534                         if ( rc != LDAP_SUCCESS ) {
535                                 attr->a_nvals = NULL;
536                                 attr_free( attr );
537                                 goto next_attr;
538                         }
539
540                         if ( pretty ) {
541                                 LBER_FREE( attr->a_vals[i].bv_val );
542                                 attr->a_vals[i] = pval;
543                         }
544                 }
545
546                 if ( last && attr->a_desc->ad_type->sat_equality &&
547                                 attr->a_desc->ad_type->sat_equality->smr_normalize )
548                 {
549                         attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
550                         for ( i = 0; i < last; i++ ) {
551                                 int             rc;
552
553                                 /*
554                                  * check that each value is valid per syntax
555                                  * and pretty if appropriate
556                                  */
557                                 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
558                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
559                                         attr->a_desc->ad_type->sat_syntax,
560                                         attr->a_desc->ad_type->sat_equality,
561                                         &attr->a_vals[i], &attr->a_nvals[i],
562                                         NULL /* op->o_tmpmemctx */ );
563
564                                 if ( rc != LDAP_SUCCESS ) {
565                                         BER_BVZERO( &attr->a_nvals[i] );
566                                         ch_free( attr );
567                                         goto next_attr;
568                                 }
569                         }
570                         BER_BVZERO( &attr->a_nvals[i] );
571
572                 } else {
573                         attr->a_nvals = attr->a_vals;
574                 }
575                 *attrp = attr;
576                 attrp = &attr->a_next;
577
578 next_attr:;
579         }
580
581         /* make sure it's free'able */
582         if ( !private && ent->e_name.bv_val == bdn->bv_val ) {
583                 ber_dupbv( &ent->e_name, bdn );
584         }
585         return LDAP_SUCCESS;
586 }
587
588 /* return 0 IFF we can retrieve the entry with ndn
589  */
590 int
591 ldap_back_entry_get(
592         Operation *op,
593         struct berval   *ndn,
594         ObjectClass *oc,
595         AttributeDescription *at,
596         int rw,
597         Entry **ent
598 )
599 {
600         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;    
601         struct ldapconn *lc;
602         int rc = 1, is_oc;
603         struct berval mapped = BER_BVNULL, bdn, mdn;
604         LDAPMessage     *result = NULL, *e = NULL;
605         char *gattr[3];
606         char *filter = NULL;
607         Connection *oconn;
608         SlapReply rs;
609         dncookie dc;
610
611         /* Tell getconn this is a privileged op */
612         is_oc = op->o_do_not_cache;
613         op->o_do_not_cache = 1;
614         lc = ldap_back_getconn(op, &rs);
615         oconn = op->o_conn;
616         op->o_conn = NULL;
617         if ( !lc || !ldap_back_dobind(lc, op, &rs) ) {
618                 op->o_do_not_cache = is_oc;
619                 op->o_conn = oconn;
620                 return 1;
621         }
622         op->o_do_not_cache = is_oc;
623         op->o_conn = oconn;
624
625         /*
626          * Rewrite the search base, if required
627          */
628         dc.rwmap = &li->rwmap;
629 #ifdef ENABLE_REWRITE
630         dc.conn = op->o_conn;
631         dc.rs = &rs;
632         dc.ctx = "searchBase";
633 #else
634         dc.tofrom = 1;
635         dc.normalized = 1;
636 #endif
637         if ( ldap_back_dn_massage( &dc, ndn, &mdn ) ) {
638                 return 1;
639         }
640
641         if ( at ) {
642                 ldap_back_map(&li->rwmap.rwm_at, &at->ad_cname, &mapped, BACKLDAP_MAP);
643                 if ( BER_BVISNULL( &mapped ) || BER_BVISEMPTY( &mapped ) ) {
644                         rc = 1;
645                         goto cleanup;
646                 }
647                 is_oc = ( strcasecmp( "objectclass", mapped.bv_val ) == 0 );
648                 if ( oc && !is_oc ) {
649                         gattr[0] = "objectclass";
650                         gattr[1] = mapped.bv_val;
651                         gattr[2] = NULL;
652
653                 } else {
654                         gattr[0] = mapped.bv_val;
655                         gattr[1] = NULL;
656                 }
657         }
658
659         if (oc) {
660                 char *ptr;
661                 ldap_back_map(&li->rwmap.rwm_oc, &oc->soc_cname, &mapped,
662                                                 BACKLDAP_MAP);
663                 filter = ch_malloc( STRLENOF( "(objectclass=)" ) + mapped.bv_len + 1 );
664                 ptr = lutil_strcopy( filter, "(objectclass=" );
665                 ptr = lutil_strcopy( ptr, mapped.bv_val );
666                 *ptr++ = ')';
667                 *ptr++ = '\0';
668         }
669
670         if ( ldap_search_ext_s( lc->ld, mdn.bv_val, LDAP_SCOPE_BASE, filter,
671                                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
672                                 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS )
673         {
674                 goto cleanup;
675         }
676
677         e = ldap_first_entry( lc->ld, result );
678         if ( e == NULL ) {
679                 goto cleanup;
680         }
681
682         *ent = ch_calloc( 1, sizeof( Entry ) );
683
684         rc = ldap_build_entry( op, e, *ent, &bdn, 0 );
685
686         if ( rc != LDAP_SUCCESS ) {
687                 ch_free( *ent );
688                 *ent = NULL;
689         }
690
691 cleanup:
692         if ( result ) {
693                 ldap_msgfree( result );
694         }
695
696         if ( filter ) {
697                 ch_free( filter );
698         }
699
700         if ( mdn.bv_val != ndn->bv_val ) {
701                 ch_free( mdn.bv_val );
702         }
703
704         return rc;
705 }
706