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