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