]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
b31bc484d257330a096e8f1ac6bcf22229ec445e
[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-2008 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         ldapinfo_t      *li = (ldapinfo_t *) 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 ( LDAP_BACK_T_F( li ) ) {
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 ( LDAP_BACK_T_F( li ) ) {
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         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
145
146         ldapconn_t      *lc = NULL;
147         struct timeval  tv;
148         time_t          stoptime = (time_t)(-1);
149         LDAPMessage     *res,
150                         *e;
151         int             rc = 0,
152                         msgid; 
153         struct berval   match = BER_BVNULL,
154                         filter = BER_BVNULL;
155         int             free_filter = 0;
156         int             i;
157         char            **attrs = NULL;
158         int             freetext = 0;
159         int             do_retry = 1, dont_retry = 0;
160         LDAPControl     **ctrls = NULL;
161         char            **references = NULL;
162
163         /* FIXME: shouldn't this be null? */
164         const char      *save_matched = rs->sr_matched;
165
166         if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
167                 return rs->sr_err;
168         }
169
170         /*
171          * FIXME: in case of values return filter, we might want
172          * to map attrs and maybe rewrite value
173          */
174
175         /* should we check return values? */
176         if ( op->ors_deref != -1 ) {
177                 ldap_set_option( lc->lc_ld, LDAP_OPT_DEREF,
178                                 (void *)&op->ors_deref );
179         }
180
181         if ( op->ors_tlimit != SLAP_NO_LIMIT ) {
182                 tv.tv_sec = op->ors_tlimit;
183                 tv.tv_usec = 0;
184                 stoptime = op->o_time + op->ors_tlimit;
185
186         } else {
187                 LDAP_BACK_TV_SET( &tv );
188         }
189
190         if ( op->ors_attrs ) {
191                 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ )
192                         /* just count attrs */ ;
193
194                 attrs = ch_malloc( ( i + 1 )*sizeof( char * ) );
195                 if ( attrs == NULL ) {
196                         rs->sr_err = LDAP_NO_MEMORY;
197                         rc = -1;
198                         goto finish;
199                 }
200         
201                 for ( i = 0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++ ) {
202                         attrs[ i ] = op->ors_attrs[i].an_name.bv_val;
203                 }
204                 attrs[ i ] = NULL;
205         }
206
207         ctrls = op->o_ctrls;
208         rc = ldap_back_proxy_authz_ctrl( &lc->lc_bound_ndn,
209                 li->li_version, &li->li_idassert, op, rs, &ctrls );
210         if ( rc != LDAP_SUCCESS ) {
211                 goto finish;
212         }
213
214         /* deal with <draft-zeilenga-ldap-t-f> filters */
215         filter = op->ors_filterstr;
216 retry:
217         rs->sr_err = ldap_search_ext( lc->lc_ld, op->o_req_dn.bv_val,
218                         op->ors_scope, filter.bv_val,
219                         attrs, op->ors_attrsonly, ctrls, NULL,
220                         tv.tv_sec ? &tv : NULL,
221                         op->ors_slimit, &msgid );
222
223         if ( rs->sr_err != LDAP_SUCCESS ) {
224                 switch ( rs->sr_err ) {
225                 case LDAP_SERVER_DOWN:
226                         if ( do_retry ) {
227                                 do_retry = 0;
228                                 if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_DONTSEND ) ) {
229                                         goto retry;
230                                 }
231                         }
232
233                         if ( lc == NULL ) {
234                                 /* reset by ldap_back_retry ... */
235                                 rs->sr_err = slap_map_api2result( rs );
236
237                         } else {
238                                 rc = ldap_back_op_result( lc, op, rs, msgid, 0, LDAP_BACK_DONTSEND );
239                         }
240                                 
241                         goto finish;
242
243                 case LDAP_FILTER_ERROR:
244                         if ( ldap_back_munge_filter( op, &filter ) ) {
245                                 free_filter = 1;
246                                 goto retry;
247                         }
248
249                         /* invalid filters return success with no data */
250                         rs->sr_err = LDAP_SUCCESS;
251                         rs->sr_text = NULL;
252                         goto finish;
253                 
254                 default:
255                         rs->sr_err = slap_map_api2result( rs );
256                         rs->sr_text = NULL;
257                         goto finish;
258                 }
259         }
260
261         /* if needed, initialize timeout */
262         if ( li->li_timeout[ SLAP_OP_SEARCH ] ) {
263                 if ( tv.tv_sec == 0 || tv.tv_sec > li->li_timeout[ SLAP_OP_SEARCH ] ) {
264                         tv.tv_sec = li->li_timeout[ SLAP_OP_SEARCH ];
265                         tv.tv_usec = 0;
266                 }
267         }
268
269         /* We pull apart the ber result, stuff it into a slapd entry, and
270          * let send_search_entry stuff it back into ber format. Slow & ugly,
271          * but this is necessary for version matching, and for ACL processing.
272          */
273
274         for ( rc = -2; rc != -1; rc = ldap_result( lc->lc_ld, msgid, LDAP_MSG_ONE, &tv, &res ) )
275         {
276                 /* check for abandon */
277                 if ( op->o_abandon || LDAP_BACK_CONN_ABANDON( lc ) ) {
278                         if ( rc > 0 ) {
279                                 ldap_msgfree( res );
280                         }
281                         (void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
282                         rc = SLAPD_ABANDON;
283                         goto finish;
284                 }
285
286                 if ( rc == 0 || rc == -2 ) {
287                         ldap_pvt_thread_yield();
288
289                         /* check timeout */
290                         if ( li->li_timeout[ SLAP_OP_SEARCH ] ) {
291                                 if ( rc == 0 ) {
292                                         (void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
293                                         rs->sr_text = "Operation timed out";
294                                         rc = rs->sr_err = op->o_protocol >= LDAP_VERSION3 ?
295                                                 LDAP_ADMINLIMIT_EXCEEDED : LDAP_OTHER;
296                                         goto finish;
297                                 }
298
299                         } else {
300                                 LDAP_BACK_TV_SET( &tv );
301                         }
302
303                         /* check time limit */
304                         if ( op->ors_tlimit != SLAP_NO_LIMIT
305                                         && slap_get_time() > stoptime )
306                         {
307                                 (void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
308                                 rc = rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
309                                 goto finish;
310                         }
311                         continue;
312
313                 } else {
314                         /* only touch when activity actually took place... */
315                         if ( li->li_idle_timeout && lc ) {
316                                 lc->lc_time = op->o_time;
317                         }
318
319                         /* don't retry any more */
320                         dont_retry = 1;
321                 }
322
323
324                 if ( rc == LDAP_RES_SEARCH_ENTRY ) {
325                         Entry           ent = { 0 };
326                         struct berval   bdn = BER_BVNULL;
327
328                         do_retry = 0;
329
330                         e = ldap_first_entry( lc->lc_ld, res );
331                         rc = ldap_build_entry( op, e, &ent, &bdn );
332                         if ( rc == LDAP_SUCCESS ) {
333                                 ldap_get_entry_controls( lc->lc_ld, res, &rs->sr_ctrls );
334                                 rs->sr_entry = &ent;
335                                 rs->sr_attrs = op->ors_attrs;
336                                 rs->sr_operational_attrs = NULL;
337                                 rs->sr_flags = 0;
338                                 rs->sr_err = LDAP_SUCCESS;
339                                 rc = rs->sr_err = send_search_entry( op, rs );
340                                 if ( rs->sr_ctrls ) {
341                                         ldap_controls_free( rs->sr_ctrls );
342                                         rs->sr_ctrls = NULL;
343                                 }
344                                 rs->sr_entry = NULL;
345                                 if ( !BER_BVISNULL( &ent.e_name ) ) {
346                                         assert( ent.e_name.bv_val != bdn.bv_val );
347                                         op->o_tmpfree( ent.e_name.bv_val, op->o_tmpmemctx );
348                                         BER_BVZERO( &ent.e_name );
349                                 }
350                                 if ( !BER_BVISNULL( &ent.e_nname ) ) {
351                                         op->o_tmpfree( ent.e_nname.bv_val, op->o_tmpmemctx );
352                                         BER_BVZERO( &ent.e_nname );
353                                 }
354                                 entry_clean( &ent );
355                         }
356                         ldap_msgfree( res );
357                         if ( rc != LDAP_SUCCESS ) {
358                                 if ( rc == LDAP_UNAVAILABLE ) {
359                                         rc = rs->sr_err = LDAP_OTHER;
360                                 } else {
361                                         (void)ldap_back_cancel( lc, op, rs, msgid, LDAP_BACK_DONTSEND );
362                                 }
363                                 goto finish;
364                         }
365
366                 } else if ( rc == LDAP_RES_SEARCH_REFERENCE ) {
367                         do_retry = 0;
368                         rc = ldap_parse_reference( lc->lc_ld, res,
369                                         &references, &rs->sr_ctrls, 1 );
370
371                         if ( rc != LDAP_SUCCESS ) {
372                                 continue;
373                         }
374
375                         /* FIXME: there MUST be at least one */
376                         if ( references && references[ 0 ] && references[ 0 ][ 0 ] ) {
377                                 int             cnt;
378
379                                 for ( cnt = 0; references[ cnt ]; cnt++ )
380                                         /* NO OP */ ;
381
382                                 /* FIXME: there MUST be at least one */
383                                 rs->sr_ref = op->o_tmpalloc( ( cnt + 1 ) * sizeof( struct berval ),
384                                         op->o_tmpmemctx );
385
386                                 for ( cnt = 0; references[ cnt ]; cnt++ ) {
387                                         ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
388                                 }
389                                 BER_BVZERO( &rs->sr_ref[ cnt ] );
390
391                                 /* ignore return value by now */
392                                 rs->sr_entry = NULL;
393                                 ( void )send_search_reference( op, rs );
394
395                         } else {
396                                 Debug( LDAP_DEBUG_ANY,
397                                         "%s ldap_back_search: "
398                                         "got SEARCH_REFERENCE "
399                                         "with no referrals\n",
400                                         op->o_log_prefix, 0, 0 );
401                         }
402
403                         /* cleanup */
404                         if ( references ) {
405                                 ber_memvfree( (void **)references );
406                                 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
407                                 rs->sr_ref = NULL;
408                                 references = NULL;
409                         }
410
411                         if ( rs->sr_ctrls ) {
412                                 ldap_controls_free( rs->sr_ctrls );
413                                 rs->sr_ctrls = NULL;
414                         }
415
416                 } else {
417                         char            *err = NULL;
418
419                         rc = ldap_parse_result( lc->lc_ld, res, &rs->sr_err,
420                                         &match.bv_val, &err,
421                                         &references, &rs->sr_ctrls, 1 );
422                         if ( rc != LDAP_SUCCESS ) {
423                                 rs->sr_err = rc;
424                         }
425                         rs->sr_err = slap_map_api2result( rs );
426                         if ( err ) {
427                                 rs->sr_text = err;
428                                 freetext = 1;
429                         }
430
431                         /* RFC 4511: referrals can only appear
432                          * if result code is LDAP_REFERRAL */
433                         if ( references 
434                                 && references[ 0 ]
435                                 && references[ 0 ][ 0 ] )
436                         {
437                                 if ( rs->sr_err != LDAP_REFERRAL ) {
438                                         Debug( LDAP_DEBUG_ANY,
439                                                 "%s ldap_back_search: "
440                                                 "got referrals with err=%d\n",
441                                                 op->o_log_prefix,
442                                                 rs->sr_err, 0 );
443
444                                 } else {
445                                         int     cnt;
446
447                                         for ( cnt = 0; references[ cnt ]; cnt++ )
448                                                 /* NO OP */ ;
449                                 
450                                         rs->sr_ref = op->o_tmpalloc( ( cnt + 1 ) * sizeof( struct berval ),
451                                                 op->o_tmpmemctx );
452
453                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
454                                                 /* duplicating ...*/
455                                                 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
456                                         }
457                                         BER_BVZERO( &rs->sr_ref[ cnt ] );
458                                 }
459
460                         } else if ( rs->sr_err == LDAP_REFERRAL ) {
461                                 Debug( LDAP_DEBUG_ANY,
462                                         "%s ldap_back_search: "
463                                         "got err=%d with null "
464                                         "or empty referrals\n",
465                                         op->o_log_prefix,
466                                         rs->sr_err, 0 );
467
468                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
469                         }
470
471                         if ( match.bv_val != NULL ) {
472 #ifndef LDAP_NULL_IS_NULL
473                                 if ( match.bv_val[ 0 ] == '\0' ) {
474                                         LDAP_FREE( match.bv_val );
475                                         BER_BVZERO( &match );
476                                 } else
477 #endif /* LDAP_NULL_IS_NULL */
478                                 {
479                                         match.bv_len = strlen( match.bv_val );
480                                 }
481                         }
482 #ifndef LDAP_NULL_IS_NULL
483                         if ( rs->sr_text != NULL && rs->sr_text[ 0 ] == '\0' ) {
484                                 LDAP_FREE( (char *)rs->sr_text );
485                                 rs->sr_text = NULL;
486                         }
487 #endif /* LDAP_NULL_IS_NULL */
488
489                         rc = 0;
490                         break;
491                 }
492
493                 /* if needed, restore timeout */
494                 if ( li->li_timeout[ SLAP_OP_SEARCH ] ) {
495                         if ( tv.tv_sec == 0 || tv.tv_sec > li->li_timeout[ SLAP_OP_SEARCH ] ) {
496                                 tv.tv_sec = li->li_timeout[ SLAP_OP_SEARCH ];
497                                 tv.tv_usec = 0;
498                         }
499                 }
500         }
501
502         if ( rc == -1 && dont_retry == 0 ) {
503                 if ( do_retry ) {
504                         do_retry = 0;
505                         if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_DONTSEND ) ) {
506                                 goto retry;
507                         }
508                 }
509                 rs->sr_err = LDAP_SERVER_DOWN;
510                 rs->sr_err = slap_map_api2result( rs );
511                 goto finish;
512         }
513
514         /*
515          * Rewrite the matched portion of the search base, if required
516          */
517         if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
518                 struct berval   pmatch;
519
520                 if ( dnPretty( NULL, &match, &pmatch, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
521                         rs->sr_matched = pmatch.bv_val;
522                         LDAP_FREE( match.bv_val );
523
524                 } else {
525                         rs->sr_matched = match.bv_val;
526                 }
527         }
528
529         if ( rs->sr_v2ref ) {
530                 rs->sr_err = LDAP_REFERRAL;
531         }
532
533 finish:;
534         if ( LDAP_BACK_QUARANTINE( li ) ) {
535                 ldap_back_quarantine( op, rs );
536         }
537
538 #if 0
539         /* let send_ldap_result play cleanup handlers (ITS#4645) */
540         if ( rc != SLAPD_ABANDON )
541 #endif
542         {
543                 send_ldap_result( op, rs );
544         }
545
546         (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
547
548         if ( rs->sr_ctrls ) {
549                 ldap_controls_free( rs->sr_ctrls );
550                 rs->sr_ctrls = NULL;
551         }
552
553         if ( rs->sr_matched != NULL && rs->sr_matched != save_matched ) {
554                 if ( rs->sr_matched != match.bv_val ) {
555                         ber_memfree_x( (char *)rs->sr_matched, op->o_tmpmemctx );
556
557                 } else {
558                         LDAP_FREE( match.bv_val );
559                 }
560                 rs->sr_matched = save_matched;
561         }
562
563         if ( free_filter ) {
564                 op->o_tmpfree( filter.bv_val, op->o_tmpmemctx );
565         }
566
567         if ( rs->sr_text ) {
568                 if ( freetext ) {
569                         LDAP_FREE( (char *)rs->sr_text );
570                 }
571                 rs->sr_text = NULL;
572         }
573
574         if ( rs->sr_ref ) {
575                 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
576                 rs->sr_ref = NULL;
577         }
578
579         if ( references ) {
580                 ber_memvfree( (void **)references );
581         }
582
583         if ( attrs ) {
584                 ch_free( attrs );
585         }
586
587         if ( lc != NULL ) {
588                 ldap_back_release_conn( li, lc );
589         }
590
591         return rs->sr_err;
592 }
593
594 static int
595 ldap_build_entry(
596                 Operation       *op,
597                 LDAPMessage     *e,
598                 Entry           *ent,
599                 struct berval   *bdn )
600 {
601         struct berval   a;
602         BerElement      ber = *e->lm_ber;
603         Attribute       *attr, **attrp;
604         const char      *text;
605         int             last;
606         char *lastb;
607         ber_len_t len;
608
609         /* safe assumptions ... */
610         assert( ent != NULL );
611         BER_BVZERO( &ent->e_bv );
612
613         if ( ber_scanf( &ber, "{m", bdn ) == LBER_ERROR ) {
614                 return LDAP_DECODING_ERROR;
615         }
616
617         /*
618          * Note: this may fail if the target host(s) schema differs
619          * from the one known to the meta, and a DN with unknown
620          * attributes is returned.
621          * 
622          * FIXME: should we log anything, or delegate to dnNormalize?
623          */
624         /* Note: if the distinguished values or the naming attributes
625          * change, should we massage them as well?
626          */
627         if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
628                 op->o_tmpmemctx ) != LDAP_SUCCESS )
629         {
630                 return LDAP_INVALID_DN_SYNTAX;
631         }
632
633         ent->e_attrs = NULL;
634         if ( ber_first_element( &ber, &len, &lastb ) != LBER_SEQUENCE ) {
635                 return LDAP_SUCCESS;
636         }
637
638         attrp = &ent->e_attrs;
639         while ( ber_next_element( &ber, &len, lastb ) == LBER_SEQUENCE &&
640                 ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
641                 int                             i;
642                 slap_syntax_validate_func       *validate;
643                 slap_syntax_transform_func      *pretty;
644
645                 attr = (Attribute *)ch_malloc( sizeof( Attribute ) );
646                 if ( attr == NULL ) {
647                         continue;
648                 }
649                 attr->a_flags = 0;
650                 attr->a_next = 0;
651                 attr->a_desc = NULL;
652                 if ( slap_bv2ad( &a, &attr->a_desc, &text ) 
653                                 != LDAP_SUCCESS )
654                 {
655                         if ( slap_bv2undef_ad( &a, &attr->a_desc, &text,
656                                 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
657                         {
658                                 Debug( LDAP_DEBUG_ANY, 
659                                         "%s ldap_build_entry: "
660                                         "slap_bv2undef_ad(%s): %s\n",
661                                         op->o_log_prefix, a.bv_val, text );
662                                 ch_free( attr );
663                                 continue;
664                         }
665                 }
666
667                 /* no subschemaSubentry */
668                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
669                         || attr->a_desc == slap_schema.si_ad_entryDN )
670                 {
671
672                         /* 
673                          * We eat target's subschemaSubentry because
674                          * a search for this value is likely not
675                          * to resolve to the appropriate backend;
676                          * later, the local subschemaSubentry is
677                          * added.
678                          *
679                          * We also eat entryDN because the frontend
680                          * will reattach it without checking if already
681                          * present...
682                          */
683                         ( void )ber_scanf( &ber, "x" /* [W] */ );
684
685                         ch_free( attr );
686                         continue;
687                 }
688                 
689                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
690                                 || attr->a_vals == NULL )
691                 {
692                         /*
693                          * Note: attr->a_vals can be null when using
694                          * values result filter
695                          */
696                         attr->a_vals = (struct berval *)&slap_dummy_bv;
697                         last = 0;
698
699                 } else {
700                         for ( last = 0; !BER_BVISNULL( &attr->a_vals[ last ] ); last++ )
701                                 /* just count vals */ ;
702                 }
703
704                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
705                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
706
707                 if ( !validate && !pretty ) {
708                         attr->a_nvals = NULL;
709                         attr_free( attr );
710                         goto next_attr;
711                 }
712
713                 for ( i = 0; i < last; i++ ) {
714                         struct berval   pval;
715                         int             rc;
716
717                         if ( pretty ) {
718                                 rc = pretty( attr->a_desc->ad_type->sat_syntax,
719                                         &attr->a_vals[i], &pval, NULL );
720
721                         } else {
722                                 rc = validate( attr->a_desc->ad_type->sat_syntax,
723                                         &attr->a_vals[i] );
724                         }
725
726                         if ( rc != LDAP_SUCCESS ) {
727                                 /* check if, by chance, it's an undefined objectClass */
728                                 if ( attr->a_desc == slap_schema.si_ad_objectClass &&
729                                                 oc_bvfind_undef( &attr->a_vals[i] ) != NULL )
730                                 {
731                                         ber_dupbv( &pval, &attr->a_vals[i] );
732
733                                 } else {
734                                         attr->a_nvals = NULL;
735                                         attr_free( attr );
736                                         goto next_attr;
737                                 }
738                         }
739
740                         if ( pretty ) {
741                                 LBER_FREE( attr->a_vals[i].bv_val );
742                                 attr->a_vals[i] = pval;
743                         }
744                 }
745
746                 if ( last && attr->a_desc->ad_type->sat_equality &&
747                                 attr->a_desc->ad_type->sat_equality->smr_normalize )
748                 {
749                         attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
750                         for ( i = 0; i < last; i++ ) {
751                                 int             rc;
752
753                                 /*
754                                  * check that each value is valid per syntax
755                                  * and pretty if appropriate
756                                  */
757                                 rc = attr->a_desc->ad_type->sat_equality->smr_normalize(
758                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
759                                         attr->a_desc->ad_type->sat_syntax,
760                                         attr->a_desc->ad_type->sat_equality,
761                                         &attr->a_vals[i], &attr->a_nvals[i],
762                                         NULL );
763
764                                 if ( rc != LDAP_SUCCESS ) {
765                                         BER_BVZERO( &attr->a_nvals[i] );
766                                         ch_free( attr );
767                                         goto next_attr;
768                                 }
769                         }
770                         BER_BVZERO( &attr->a_nvals[i] );
771
772                 } else {
773                         attr->a_nvals = attr->a_vals;
774                 }
775                 *attrp = attr;
776                 attrp = &attr->a_next;
777
778 next_attr:;
779         }
780
781         return LDAP_SUCCESS;
782 }
783
784 /* return 0 IFF we can retrieve the entry with ndn
785  */
786 int
787 ldap_back_entry_get(
788                 Operation               *op,
789                 struct berval           *ndn,
790                 ObjectClass             *oc,
791                 AttributeDescription    *at,
792                 int                     rw,
793                 Entry                   **ent )
794 {
795         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
796
797         ldapconn_t      *lc = NULL;
798         int             rc = 1,
799                         do_not_cache;
800         struct berval   bdn;
801         LDAPMessage     *result = NULL,
802                         *e = NULL;
803         char            *attr[3], **attrp = NULL;
804         char            *filter = NULL;
805         SlapReply       rs;
806         int             do_retry = 1;
807         LDAPControl     **ctrls = NULL;
808
809         *ent = NULL;
810
811         /* Tell getconn this is a privileged op */
812         do_not_cache = op->o_do_not_cache;
813         op->o_do_not_cache = 1;
814         if ( !ldap_back_dobind( &lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
815                 op->o_do_not_cache = do_not_cache;
816                 return rs.sr_err;
817         }
818         op->o_do_not_cache = do_not_cache;
819
820         if ( at ) {
821                 attrp = attr;
822                 if ( oc && at != slap_schema.si_ad_objectClass ) {
823                         attr[0] = slap_schema.si_ad_objectClass->ad_cname.bv_val;
824                         attr[1] = at->ad_cname.bv_val;
825                         attr[2] = NULL;
826
827                 } else {
828                         attr[0] = at->ad_cname.bv_val;
829                         attr[1] = NULL;
830                 }
831         }
832
833         if ( oc ) {
834                 char    *ptr;
835
836                 filter = op->o_tmpalloc( STRLENOF( "(objectClass=" ")" ) 
837                                 + oc->soc_cname.bv_len + 1, op->o_tmpmemctx );
838                 ptr = lutil_strcopy( filter, "(objectClass=" );
839                 ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
840                 *ptr++ = ')';
841                 *ptr++ = '\0';
842         }
843
844 retry:
845         ctrls = op->o_ctrls;
846         rc = ldap_back_proxy_authz_ctrl( &lc->lc_bound_ndn,
847                 li->li_version, &li->li_idassert, op, &rs, &ctrls );
848         if ( rc != LDAP_SUCCESS ) {
849                 goto cleanup;
850         }
851
852         /* TODO: timeout? */
853         rc = ldap_search_ext_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
854                                 attrp, 0, ctrls, NULL,
855                                 NULL, LDAP_NO_LIMIT, &result );
856         if ( rc != LDAP_SUCCESS ) {
857                 if ( rc == LDAP_SERVER_DOWN && do_retry ) {
858                         do_retry = 0;
859                         if ( ldap_back_retry( &lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
860                                 /* if the identity changed, there might be need to re-authz */
861                                 (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
862                                 goto retry;
863                         }
864                 }
865                 goto cleanup;
866         }
867
868         e = ldap_first_entry( lc->lc_ld, result );
869         if ( e == NULL ) {
870                 /* the entry exists, but it doesn't match the filter? */
871                 goto cleanup;
872         }
873
874         *ent = ch_calloc( 1, sizeof( Entry ) );
875         if ( *ent == NULL ) {
876                 rc = LDAP_NO_MEMORY;
877                 goto cleanup;
878         }
879
880         rc = ldap_build_entry( op, e, *ent, &bdn );
881
882         if ( rc != LDAP_SUCCESS ) {
883                 entry_free( *ent );
884                 *ent = NULL;
885         }
886
887 cleanup:
888         (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
889
890         if ( result ) {
891                 ldap_msgfree( result );
892         }
893
894         if ( filter ) {
895                 op->o_tmpfree( filter, op->o_tmpmemctx );
896         }
897
898         if ( lc != NULL ) {
899                 ldap_back_release_conn( li, lc );
900         }
901
902         return rc;
903 }
904