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