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