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