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