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