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