]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/search.c
f168a87a5dc3ee28370a80026d2214eb72b5dc2e
[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                                 rs->sr_err = rc;
493                         }
494                         rs->sr_err = slap_map_api2result( rs );
495                         if ( err ) {
496                                 rs->sr_text = err;
497                                 freetext = 1;
498                         }
499
500                         /* RFC 4511: referrals can only appear
501                          * if result code is LDAP_REFERRAL */
502                         if ( references 
503                                 && references[ 0 ]
504                                 && references[ 0 ][ 0 ] )
505                         {
506                                 if ( rs->sr_err != LDAP_REFERRAL ) {
507                                         Debug( LDAP_DEBUG_ANY,
508                                                 "%s ldap_back_search: "
509                                                 "got referrals with err=%d\n",
510                                                 op->o_log_prefix,
511                                                 rs->sr_err, 0 );
512
513                                 } else {
514                                         int     cnt;
515
516                                         for ( cnt = 0; references[ cnt ]; cnt++ )
517                                                 /* NO OP */ ;
518                                 
519                                         rs->sr_ref = op->o_tmpalloc( ( cnt + 1 ) * sizeof( struct berval ),
520                                                 op->o_tmpmemctx );
521
522                                         for ( cnt = 0; references[ cnt ]; cnt++ ) {
523                                                 /* duplicating ...*/
524                                                 ber_str2bv( references[ cnt ], 0, 0, &rs->sr_ref[ cnt ] );
525                                         }
526                                         BER_BVZERO( &rs->sr_ref[ cnt ] );
527                                 }
528
529                         } else if ( rs->sr_err == LDAP_REFERRAL ) {
530                                 Debug( LDAP_DEBUG_ANY,
531                                         "%s ldap_back_search: "
532                                         "got err=%d with null "
533                                         "or empty referrals\n",
534                                         op->o_log_prefix,
535                                         rs->sr_err, 0 );
536
537                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
538                         }
539
540                         if ( match.bv_val != NULL ) {
541                                 match.bv_len = strlen( match.bv_val );
542                         }
543
544                         rc = 0;
545                         break;
546                 }
547
548                 /* if needed, restore timeout */
549                 if ( li->li_timeout[ SLAP_OP_SEARCH ] ) {
550                         if ( tv.tv_sec == 0 || tv.tv_sec > li->li_timeout[ SLAP_OP_SEARCH ] ) {
551                                 tv.tv_sec = li->li_timeout[ SLAP_OP_SEARCH ];
552                                 tv.tv_usec = 0;
553                         }
554                 }
555         }
556
557         if ( rc == -1 && dont_retry == 0 ) {
558                 if ( do_retry ) {
559                         do_retry = 0;
560                         if ( ldap_back_retry( &lc, op, rs, LDAP_BACK_DONTSEND ) ) {
561                                 goto retry;
562                         }
563                 }
564                 rs->sr_err = LDAP_SERVER_DOWN;
565                 rs->sr_err = slap_map_api2result( rs );
566                 goto finish;
567         }
568
569         /*
570          * Rewrite the matched portion of the search base, if required
571          */
572         if ( !BER_BVISNULL( &match ) && !BER_BVISEMPTY( &match ) ) {
573                 struct berval   pmatch;
574
575                 if ( dnPretty( NULL, &match, &pmatch, op->o_tmpmemctx ) == LDAP_SUCCESS ) {
576                         rs->sr_matched = pmatch.bv_val;
577                         LDAP_FREE( match.bv_val );
578
579                 } else {
580                         rs->sr_matched = match.bv_val;
581                 }
582         }
583
584         if ( rs->sr_v2ref ) {
585                 rs->sr_err = LDAP_REFERRAL;
586         }
587
588 finish:;
589         if ( LDAP_BACK_QUARANTINE( li ) ) {
590                 ldap_back_quarantine( op, rs );
591         }
592
593         if ( freefilter && filter.bv_val != op->ors_filterstr.bv_val ) {
594                 op->o_tmpfree( filter.bv_val, op->o_tmpmemctx );
595         }
596
597 #if 0
598         /* let send_ldap_result play cleanup handlers (ITS#4645) */
599         if ( rc != SLAPD_ABANDON )
600 #endif
601         {
602                 send_ldap_result( op, rs );
603         }
604
605         (void)ldap_back_controls_free( op, rs, &ctrls );
606
607         if ( rs->sr_ctrls ) {
608                 ldap_controls_free( rs->sr_ctrls );
609                 rs->sr_ctrls = NULL;
610         }
611
612         if ( rs->sr_matched != NULL && rs->sr_matched != save_matched ) {
613                 if ( rs->sr_matched != match.bv_val ) {
614                         ber_memfree_x( (char *)rs->sr_matched, op->o_tmpmemctx );
615
616                 } else {
617                         LDAP_FREE( match.bv_val );
618                 }
619                 rs->sr_matched = save_matched;
620         }
621
622         if ( rs->sr_text ) {
623                 if ( freetext ) {
624                         LDAP_FREE( (char *)rs->sr_text );
625                 }
626                 rs->sr_text = NULL;
627         }
628
629         if ( rs->sr_ref ) {
630                 op->o_tmpfree( rs->sr_ref, op->o_tmpmemctx );
631                 rs->sr_ref = NULL;
632         }
633
634         if ( references ) {
635                 ber_memvfree( (void **)references );
636         }
637
638         if ( attrs ) {
639                 op->o_tmpfree( attrs, op->o_tmpmemctx );
640         }
641
642         if ( lc != NULL ) {
643                 ldap_back_release_conn( li, lc );
644         }
645
646         return rs->sr_err;
647 }
648
649 static int
650 ldap_build_entry(
651                 Operation       *op,
652                 LDAPMessage     *e,
653                 Entry           *ent,
654                 struct berval   *bdn )
655 {
656         struct berval   a;
657         BerElement      ber = *e->lm_ber;
658         Attribute       *attr, **attrp;
659         const char      *text;
660         int             last;
661         char *lastb;
662         ber_len_t len;
663
664         /* safe assumptions ... */
665         assert( ent != NULL );
666         BER_BVZERO( &ent->e_bv );
667
668         if ( ber_scanf( &ber, "{m", bdn ) == LBER_ERROR ) {
669                 return LDAP_DECODING_ERROR;
670         }
671
672         /*
673          * Note: this may fail if the target host(s) schema differs
674          * from the one known to the meta, and a DN with unknown
675          * attributes is returned.
676          * 
677          * FIXME: should we log anything, or delegate to dnNormalize?
678          */
679         /* Note: if the distinguished values or the naming attributes
680          * change, should we massage them as well?
681          */
682         if ( dnPrettyNormal( NULL, bdn, &ent->e_name, &ent->e_nname,
683                 op->o_tmpmemctx ) != LDAP_SUCCESS )
684         {
685                 return LDAP_INVALID_DN_SYNTAX;
686         }
687
688         ent->e_attrs = NULL;
689         if ( ber_first_element( &ber, &len, &lastb ) != LBER_SEQUENCE ) {
690                 return LDAP_SUCCESS;
691         }
692
693         attrp = &ent->e_attrs;
694         while ( ber_next_element( &ber, &len, lastb ) == LBER_SEQUENCE &&
695                 ber_scanf( &ber, "{m", &a ) != LBER_ERROR ) {
696                 int                             i;
697                 slap_syntax_validate_func       *validate;
698                 slap_syntax_transform_func      *pretty;
699
700                 attr = attr_alloc( NULL );
701                 if ( attr == NULL ) {
702                         return LDAP_OTHER;
703                 }
704                 if ( slap_bv2ad( &a, &attr->a_desc, &text ) 
705                                 != LDAP_SUCCESS )
706                 {
707                         if ( slap_bv2undef_ad( &a, &attr->a_desc, &text,
708                                 SLAP_AD_PROXIED ) != LDAP_SUCCESS )
709                         {
710                                 Debug( LDAP_DEBUG_ANY, 
711                                         "%s ldap_build_entry: "
712                                         "slap_bv2undef_ad(%s): %s\n",
713                                         op->o_log_prefix, a.bv_val, text );
714
715                                 ( void )ber_scanf( &ber, "x" /* [W] */ );
716                                 attr_free( attr );
717                                 continue;
718                         }
719                 }
720
721                 /* no subschemaSubentry */
722                 if ( attr->a_desc == slap_schema.si_ad_subschemaSubentry
723                         || attr->a_desc == slap_schema.si_ad_entryDN )
724                 {
725
726                         /* 
727                          * We eat target's subschemaSubentry because
728                          * a search for this value is likely not
729                          * to resolve to the appropriate backend;
730                          * later, the local subschemaSubentry is
731                          * added.
732                          *
733                          * We also eat entryDN because the frontend
734                          * will reattach it without checking if already
735                          * present...
736                          */
737                         ( void )ber_scanf( &ber, "x" /* [W] */ );
738                         attr_free( attr );
739                         continue;
740                 }
741                 
742                 if ( ber_scanf( &ber, "[W]", &attr->a_vals ) == LBER_ERROR
743                                 || attr->a_vals == NULL )
744                 {
745                         /*
746                          * Note: attr->a_vals can be null when using
747                          * values result filter
748                          */
749                         attr->a_vals = (struct berval *)&slap_dummy_bv;
750                 }
751
752                 validate = attr->a_desc->ad_type->sat_syntax->ssyn_validate;
753                 pretty = attr->a_desc->ad_type->sat_syntax->ssyn_pretty;
754
755                 if ( !validate && !pretty ) {
756                         attr->a_nvals = NULL;
757                         attr_free( attr );
758                         goto next_attr;
759                 }
760
761                 for ( i = 0; !BER_BVISNULL( &attr->a_vals[i] ); i++ ) ;
762                 last = i;
763
764                 /*
765                  * check that each value is valid per syntax
766                  * and pretty if appropriate
767                  */
768                 for ( i = 0; i<last; i++ ) {
769                         struct berval   pval;
770                         int             rc;
771
772                         if ( pretty ) {
773                                 rc = ordered_value_pretty( attr->a_desc,
774                                         &attr->a_vals[i], &pval, NULL );
775
776                         } else {
777                                 rc = ordered_value_validate( attr->a_desc,
778                                         &attr->a_vals[i], 0 );
779                         }
780
781                         if ( rc != LDAP_SUCCESS ) {
782                                 ObjectClass *oc;
783
784                                 /* check if, by chance, it's an undefined objectClass */
785                                 if ( attr->a_desc == slap_schema.si_ad_objectClass &&
786                                                 ( oc = oc_bvfind_undef( &attr->a_vals[i] ) ) != NULL )
787                                 {
788                                         ber_dupbv( &pval, &oc->soc_cname );
789                                         rc = LDAP_SUCCESS;
790
791                                 } else {
792                                         LBER_FREE( attr->a_vals[i].bv_val );
793                                         if ( --last == i ) {
794                                                 BER_BVZERO( &attr->a_vals[i] );
795                                                 break;
796                                         }
797                                         attr->a_vals[i] = attr->a_vals[last];
798                                         BER_BVZERO( &attr->a_vals[last] );
799                                         i--;
800                                 }
801                         }
802
803                         if ( rc == LDAP_SUCCESS && pretty ) {
804                                 LBER_FREE( attr->a_vals[i].bv_val );
805                                 attr->a_vals[i] = pval;
806                         }
807                 }
808                 attr->a_numvals = last = i;
809                 if ( last == 0 && attr->a_vals != &slap_dummy_bv ) {
810                         attr->a_nvals = NULL;
811                         attr_free( attr );
812                         goto next_attr;
813                 }
814
815                 if ( last && attr->a_desc->ad_type->sat_equality &&
816                                 attr->a_desc->ad_type->sat_equality->smr_normalize )
817                 {
818                         attr->a_nvals = ch_malloc( ( last + 1 )*sizeof( struct berval ) );
819                         for ( i = 0; i < last; i++ ) {
820                                 int             rc;
821
822                                 rc = ordered_value_normalize(
823                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
824                                         attr->a_desc,
825                                         attr->a_desc->ad_type->sat_equality,
826                                         &attr->a_vals[i], &attr->a_nvals[i],
827                                         NULL );
828
829                                 if ( rc != LDAP_SUCCESS ) {
830                                         LBER_FREE( attr->a_vals[i].bv_val );
831                                         if ( --last == i ) {
832                                                 BER_BVZERO( &attr->a_vals[i] );
833                                                 break;
834                                         }
835                                         attr->a_vals[i] = attr->a_vals[last];
836                                         BER_BVZERO( &attr->a_vals[last] );
837                                         i--;
838                                 }
839                         }
840                         BER_BVZERO( &attr->a_nvals[i] );
841                         if ( last == 0 ) {
842                                 attr_free( attr );
843                                 goto next_attr;
844                         }
845
846                 } else {
847                         attr->a_nvals = attr->a_vals;
848                 }
849
850                 attr->a_numvals = last;
851
852                 /* Handle sorted vals, strip dups but keep the attr */
853                 if ( attr->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
854                         while ( attr->a_numvals > 1 ) {
855                                 int rc = slap_sort_vals( (Modifications *)attr, &text, &i, op->o_tmpmemctx );
856                                 if ( rc != LDAP_TYPE_OR_VALUE_EXISTS )
857                                         break;
858
859                                 /* Strip duplicate values */
860                                 if ( attr->a_nvals != attr->a_vals )
861                                         LBER_FREE( attr->a_nvals[i].bv_val );
862                                 LBER_FREE( attr->a_vals[i].bv_val );
863                                 attr->a_numvals--;
864
865                                 assert( i >= 0 );
866                                 if ( (unsigned)i < attr->a_numvals ) {
867                                         attr->a_vals[i] = attr->a_vals[attr->a_numvals];
868                                         if ( attr->a_nvals != attr->a_vals )
869                                                 attr->a_nvals[i] = attr->a_nvals[attr->a_numvals];
870                                 }
871                                 BER_BVZERO(&attr->a_vals[attr->a_numvals]);
872                                 if ( attr->a_nvals != attr->a_vals )
873                                         BER_BVZERO(&attr->a_nvals[attr->a_numvals]);
874                         }
875                         attr->a_flags |= SLAP_ATTR_SORTED_VALS;
876                 }
877
878                 *attrp = attr;
879                 attrp = &attr->a_next;
880
881 next_attr:;
882         }
883
884         return LDAP_SUCCESS;
885 }
886
887 /* return 0 IFF we can retrieve the entry with ndn
888  */
889 int
890 ldap_back_entry_get(
891                 Operation               *op,
892                 struct berval           *ndn,
893                 ObjectClass             *oc,
894                 AttributeDescription    *at,
895                 int                     rw,
896                 Entry                   **ent )
897 {
898         ldapinfo_t      *li = (ldapinfo_t *) op->o_bd->be_private;
899
900         ldapconn_t      *lc = NULL;
901         int             rc,
902                         do_not_cache;
903         ber_tag_t       tag;
904         struct berval   bdn;
905         LDAPMessage     *result = NULL,
906                         *e = NULL;
907         char            *attr[3], **attrp = NULL;
908         char            *filter = NULL;
909         SlapReply       rs;
910         int             do_retry = 1;
911         LDAPControl     **ctrls = NULL;
912
913         *ent = NULL;
914
915         /* Tell getconn this is a privileged op */
916         do_not_cache = op->o_do_not_cache;
917         tag = op->o_tag;
918         /* do not cache */
919         op->o_do_not_cache = 1;
920         /* ldap_back_entry_get() is an entry lookup, so it does not need
921          * to know what the entry is being looked up for */
922         op->o_tag = LDAP_REQ_SEARCH;
923         rc = ldap_back_dobind( &lc, op, &rs, LDAP_BACK_DONTSEND );
924         op->o_do_not_cache = do_not_cache;
925         op->o_tag = tag;
926         if ( !rc ) {
927                 return rs.sr_err;
928         }
929
930         if ( at ) {
931                 attrp = attr;
932                 if ( oc && at != slap_schema.si_ad_objectClass ) {
933                         attr[0] = slap_schema.si_ad_objectClass->ad_cname.bv_val;
934                         attr[1] = at->ad_cname.bv_val;
935                         attr[2] = NULL;
936
937                 } else {
938                         attr[0] = at->ad_cname.bv_val;
939                         attr[1] = NULL;
940                 }
941         }
942
943         if ( oc ) {
944                 char    *ptr;
945
946                 filter = op->o_tmpalloc( STRLENOF( "(objectClass=" ")" ) 
947                                 + oc->soc_cname.bv_len + 1, op->o_tmpmemctx );
948                 ptr = lutil_strcopy( filter, "(objectClass=" );
949                 ptr = lutil_strcopy( ptr, oc->soc_cname.bv_val );
950                 *ptr++ = ')';
951                 *ptr++ = '\0';
952         }
953
954 retry:
955         ctrls = op->o_ctrls;
956         rc = ldap_back_controls_add( op, &rs, lc, &ctrls );
957         if ( rc != LDAP_SUCCESS ) {
958                 goto cleanup;
959         }
960
961         /* TODO: timeout? */
962         rc = ldap_pvt_search_s( lc->lc_ld, ndn->bv_val, LDAP_SCOPE_BASE, filter,
963                                 attrp, LDAP_DEREF_NEVER, ctrls, NULL,
964                                 NULL, LDAP_NO_LIMIT, 0, &result );
965         if ( rc != LDAP_SUCCESS ) {
966                 if ( rc == LDAP_SERVER_DOWN && do_retry ) {
967                         do_retry = 0;
968                         if ( ldap_back_retry( &lc, op, &rs, LDAP_BACK_DONTSEND ) ) {
969                                 /* if the identity changed, there might be need to re-authz */
970                                 (void)ldap_back_controls_free( op, &rs, &ctrls );
971                                 goto retry;
972                         }
973                 }
974                 goto cleanup;
975         }
976
977         e = ldap_first_entry( lc->lc_ld, result );
978         if ( e == NULL ) {
979                 /* the entry exists, but it doesn't match the filter? */
980                 goto cleanup;
981         }
982
983         *ent = entry_alloc();
984         if ( *ent == NULL ) {
985                 rc = LDAP_NO_MEMORY;
986                 goto cleanup;
987         }
988
989         rc = ldap_build_entry( op, e, *ent, &bdn );
990
991         if ( rc != LDAP_SUCCESS ) {
992                 entry_free( *ent );
993                 *ent = NULL;
994         }
995
996 cleanup:
997         (void)ldap_back_controls_free( op, &rs, &ctrls );
998
999         if ( result ) {
1000                 ldap_msgfree( result );
1001         }
1002
1003         if ( filter ) {
1004                 op->o_tmpfree( filter, op->o_tmpmemctx );
1005         }
1006
1007         if ( lc != NULL ) {
1008                 ldap_back_release_conn( li, lc );
1009         }
1010
1011         return rc;
1012 }
1013