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