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