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