]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
reset SlapReply flags
[openldap] / servers / slapd / back-ldbm / search.c
1 /* search.c - ldbm backend search function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20
21 #include <ac/string.h>
22 #include <ac/socket.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26 #include "proto-back-ldbm.h"
27
28 static ID_BLOCK *base_candidate(
29         Backend *be, Entry *e );
30
31 static ID_BLOCK *search_candidates(
32         Operation *op, Entry *e, Filter *filter,
33         int scope, int deref, int manageDSAit );
34
35
36 int
37 ldbm_back_search(
38     Operation   *op,
39     SlapReply   *rs )
40 {
41         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
42         int             rc, err;
43         const char *text = NULL;
44         time_t          stoptime;
45         ID_BLOCK                *candidates;
46         ID              id, cursor;
47         Entry           *e;
48         Entry   *matched = NULL;
49         struct berval   realbase = { 0, NULL };
50         int             manageDSAit = get_manageDSAit( op );
51
52 #ifdef NEW_LOGGING
53         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_search: enter\n", 0, 0, 0 );
54 #else
55         Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
56 #endif
57
58         /* grab giant lock for reading */
59         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
60
61         if ( op->o_req_ndn.bv_len == 0 ) {
62                 /* DIT root special case */
63                 e = (Entry *) &slap_entry_root;
64
65                 /* need normalized dn below */
66                 ber_dupbv( &realbase, &e->e_nname );
67
68                 candidates = search_candidates( op, e, op->ors_filter,
69                         op->ors_scope, op->ors_deref,
70                         manageDSAit || get_domainScope(op) );
71
72                 goto searchit;
73                 
74         } else if ( op->ors_deref & LDAP_DEREF_FINDING ) {
75                 /* deref dn and get entry with reader lock */
76                 e = deref_dn_r( op->o_bd, &op->o_req_ndn,
77                         &rs->sr_err, &matched, &rs->sr_text );
78
79                 if( rs->sr_err == LDAP_NO_SUCH_OBJECT ) rs->sr_err = LDAP_REFERRAL;
80
81         } else {
82                 /* get entry with reader lock */
83                 e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
84                 rs->sr_err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
85                 rs->sr_text = NULL;
86         }
87
88         if ( e == NULL ) {
89                 struct berval matched_dn = { 0, NULL };
90
91                 if ( matched != NULL ) {
92                         BerVarray erefs;
93                         ber_dupbv( &matched_dn, &matched->e_name );
94
95                         erefs = is_entry_referral( matched )
96                                 ? get_entry_referrals( op, matched )
97                                 : NULL;
98
99                         cache_return_entry_r( &li->li_cache, matched );
100
101                         if( erefs ) {
102                                 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
103                                         &op->o_req_dn, op->ors_scope );
104
105                                 ber_bvarray_free( erefs );
106                         }
107
108                 } else {
109                         rs->sr_ref = referral_rewrite( default_referral,
110                                 NULL, &op->o_req_dn, op->ors_scope );
111                 }
112
113                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
114
115                 rs->sr_matched = matched_dn.bv_val;
116                 send_ldap_result( op, rs );
117
118                 ber_bvarray_free( rs->sr_ref );
119                 ber_memfree( matched_dn.bv_val );
120                 rs->sr_ref = NULL;
121                 rs->sr_matched = NULL;
122                 return LDAP_REFERRAL;
123         }
124
125         if (!manageDSAit && is_entry_referral( e ) ) {
126                 /* entry is a referral, don't allow add */
127                 struct berval matched_dn;
128                 BerVarray erefs;
129
130                 ber_dupbv( &matched_dn, &e->e_name );
131                 erefs = get_entry_referrals( op, e );
132                 rs->sr_ref = NULL;
133
134                 cache_return_entry_r( &li->li_cache, e );
135                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
136
137 #ifdef NEW_LOGGING
138                 LDAP_LOG( BACK_LDBM, INFO,
139                         "ldbm_search: entry (%s) is a referral.\n",
140                         e->e_dn, 0, 0 );
141 #else
142                 Debug( LDAP_DEBUG_TRACE,
143                         "ldbm_search: entry is referral\n",
144                         0, 0, 0 );
145 #endif
146
147                 if( erefs ) {
148                         rs->sr_ref = referral_rewrite( erefs, &matched_dn,
149                                 &op->o_req_dn, op->ors_scope );
150
151                         ber_bvarray_free( erefs );
152                 }
153
154                 rs->sr_matched = matched_dn.bv_val;
155                 if( rs->sr_ref ) {
156                         rs->sr_err = LDAP_REFERRAL;
157                         send_ldap_result( op, rs );
158                         ber_bvarray_free( rs->sr_ref );
159
160                 } else {
161                         send_ldap_error( op, rs, LDAP_OTHER,
162                         "bad referral object" );
163                 }
164
165                 ber_memfree( matched_dn.bv_val );
166                 rs->sr_ref = NULL;
167                 rs->sr_matched = NULL;
168                 return LDAP_OTHER;
169         }
170
171         if ( is_entry_alias( e ) ) {
172                 /* don't deref */
173                 op->ors_deref = LDAP_DEREF_NEVER;
174         }
175
176         if ( op->ors_scope == LDAP_SCOPE_BASE ) {
177                 candidates = base_candidate( op->o_bd, e );
178
179         } else {
180                 candidates = search_candidates( op, e, op->ors_filter,
181                     op->ors_scope, op->ors_deref, manageDSAit );
182         }
183
184         /* need normalized dn below */
185         ber_dupbv( &realbase, &e->e_nname );
186
187         cache_return_entry_r( &li->li_cache, e );
188
189 searchit:
190         if ( candidates == NULL ) {
191                 /* no candidates */
192 #ifdef NEW_LOGGING
193                 LDAP_LOG( BACK_LDBM, INFO,
194                         "ldbm_search: no candidates\n" , 0, 0, 0);
195 #else
196                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
197                         0, 0, 0 );
198 #endif
199
200                 rs->sr_err = LDAP_SUCCESS;
201                 send_ldap_result( op, rs );
202
203                 rc = LDAP_SUCCESS;
204                 goto done;
205         }
206
207         /* if candidates exceed to-be-checked entries, abort */
208         if ( op->ors_limit      /* isroot == TRUE */
209                         && op->ors_limit->lms_s_unchecked != -1
210                         && ID_BLOCK_NIDS( candidates ) > (unsigned) op->ors_limit->lms_s_unchecked )
211         {
212                 send_ldap_error( op, rs, LDAP_ADMINLIMIT_EXCEEDED, NULL );
213                 rc = LDAP_SUCCESS;
214                 goto done;
215         }
216         
217         /* compute it anyway; root does not use it */
218         stoptime = op->o_time + op->ors_tlimit;
219         rs->sr_attrs = op->ors_attrs;
220
221         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
222             id = idl_nextid( candidates, &cursor ) )
223         {
224                 int scopeok = 0;
225                 int result = 0;
226
227                 /* check for abandon */
228                 if ( op->o_abandon ) {
229                         rc = LDAP_SUCCESS;
230                         goto done;
231                 }
232
233                 /* check time limit */
234                 if ( op->ors_tlimit != -1 && slap_get_time() > stoptime ) {
235                         rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
236                         send_ldap_result( op, rs );
237                         rc = LDAP_SUCCESS;
238                         goto done;
239                 }
240
241                 /* get the entry with reader lock */
242                 e = id2entry_r( op->o_bd, id );
243
244                 if ( e == NULL ) {
245 #ifdef NEW_LOGGING
246                         LDAP_LOG( BACK_LDBM, INFO,
247                                 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
248 #else
249                         Debug( LDAP_DEBUG_TRACE,
250                                 "ldbm_search: candidate %ld not found\n",
251                                 id, 0, 0 );
252 #endif
253
254                         goto loop_continue;
255                 }
256
257                 rs->sr_entry = e;
258
259 #ifdef LDBM_SUBENTRIES
260                 if ( is_entry_subentry( e ) ) {
261                         if( op->ors_scope != LDAP_SCOPE_BASE ) {
262                                 if(!get_subentries_visibility( op )) {
263                                         /* only subentries are visible */
264                                         goto loop_continue;
265                                 }
266                         } else if ( get_subentries( op ) &&
267                                 !get_subentries_visibility( op ))
268                         {
269                                 /* only subentries are visible */
270                                 goto loop_continue;
271                         }
272                 } else if ( get_subentries_visibility( op )) {
273                         /* only subentries are visible */
274                         goto loop_continue;
275                 }
276 #endif
277
278                 if ( op->ors_deref & LDAP_DEREF_SEARCHING &&
279                         is_entry_alias( e ) )
280                 {
281                         Entry *matched;
282                         int err;
283                         const char *text;
284                         
285                         e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
286
287                         if( e == NULL ) {
288                                 e = matched;
289                                 goto loop_continue;
290                         }
291
292                         if( e->e_id == id ) {
293                                 /* circular loop */
294                                 goto loop_continue;
295                         }
296
297                         /* need to skip alias which deref into scope */
298                         if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
299                                 struct berval pdn;
300                                 dnParent( &e->e_nname, &pdn );
301                                 if ( ber_bvcmp( &pdn, &realbase ) ) {
302                                         goto loop_continue;
303                                 }
304
305                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
306                                 /* alias is within scope */
307 #ifdef NEW_LOGGING
308                                 LDAP_LOG( BACK_LDBM, DETAIL1,
309                                         "ldbm_search: alias \"%s\" in subtree\n",
310                                         e->e_dn, 0, 0 );
311 #else
312                                 Debug( LDAP_DEBUG_TRACE,
313                                         "ldbm_search: alias \"%s\" in subtree\n",
314                                         e->e_dn, 0, 0 );
315 #endif
316
317                                 goto loop_continue;
318                         }
319
320                         rs->sr_entry = e;
321
322                         scopeok = 1;
323                 }
324
325                 /*
326                  * If it's a referral, add it to the list of referrals.
327                  * Only do this for non-base searches, and don't check
328                  * the filter explicitly here since it's only a candidate
329                  * anyway.
330                  */
331                 if ( !manageDSAit && op->ors_scope != LDAP_SCOPE_BASE &&
332                         is_entry_referral( e ) )
333                 {
334                         struct berval   dn;
335
336                         /* check scope */
337                         if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
338                                 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
339                                         dnParent( &e->e_nname, &dn );
340                                         scopeok = dn_match( &dn, &realbase );
341                                 } else {
342                                         scopeok = (realbase.bv_len == 0);
343                                 }
344
345                         } else if ( !scopeok
346                                 && op->ors_scope == LDAP_SCOPE_SUBTREE )
347                         {
348                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
349
350                         } else if ( !scopeok
351                                 && op->ors_scope == LDAP_SCOPE_SUBORDINATE )
352                         {
353                                 scopeok = !dn_match( &e->e_nname, &realbase )
354                                         && dnIsSuffix( &e->e_nname, &realbase );
355
356                         } else {
357                                 scopeok = 1;
358                         }
359
360                         if( scopeok ) {
361                                 BerVarray erefs = get_entry_referrals( op, e );
362                                 rs->sr_ref = referral_rewrite( erefs,
363                                         &e->e_name, NULL,
364                                         op->ors_scope == LDAP_SCOPE_ONELEVEL
365                                                 ? LDAP_SCOPE_BASE
366                                                 : LDAP_SCOPE_SUBTREE );
367
368                                 send_search_reference( op, rs );
369
370                                 ber_bvarray_free( rs->sr_ref );
371                                 rs->sr_ref = NULL;
372
373                         } else {
374 #ifdef NEW_LOGGING
375                                 LDAP_LOG( BACK_LDBM, DETAIL2,
376                                         "ldbm_search: candidate referral %ld scope not okay\n",
377                                         id, 0, 0 );
378 #else
379                                 Debug( LDAP_DEBUG_TRACE,
380                                         "ldbm_search: candidate referral %ld scope not okay\n",
381                                         id, 0, 0 );
382 #endif
383                         }
384
385                         goto loop_continue;
386                 }
387
388                 if ( !manageDSAit && is_entry_glue( e )) {
389                         goto loop_continue;
390                 }
391
392                 /* if it matches the filter and scope, send it */
393                 result = test_filter( op, e, op->ors_filter );
394
395                 if ( result == LDAP_COMPARE_TRUE ) {
396                         struct berval   dn;
397
398                         /* check scope */
399                         if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
400                                 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
401                                         dnParent( &e->e_nname, &dn );
402                                         scopeok = dn_match( &dn, &realbase );
403                                 } else {
404                                         scopeok = (realbase.bv_len == 0);
405                                 }
406
407                         } else if ( !scopeok &&
408                                 op->ors_scope == LDAP_SCOPE_SUBTREE )
409                         {
410                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
411
412                         } else if ( !scopeok &&
413                                 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
414                         {
415                                 scopeok = !dn_match( &e->e_nname, &realbase )
416                                         && dnIsSuffix( &e->e_nname, &realbase );
417
418                         } else {
419                                 scopeok = 1;
420                         }
421
422                         if ( scopeok ) {
423                                 /* check size limit */
424                                 if ( --op->ors_slimit == -1 ) {
425                                         cache_return_entry_r( &li->li_cache, e );
426                                         rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
427                                         send_ldap_result( op, rs );
428                                         rc = LDAP_SUCCESS;
429                                         goto done;
430                                 }
431
432                                 if (e) {
433                                         rs->sr_flags = 0;
434                                         result = send_search_entry( op, rs );
435
436                                         switch (result) {
437                                         case 0:         /* entry sent ok */
438                                                 break;
439                                         case 1:         /* entry not sent */
440                                                 break;
441                                         case -1:        /* connection closed */
442                                                 cache_return_entry_r( &li->li_cache, e );
443                                                 rc = LDAP_SUCCESS;
444                                                 goto done;
445                                         }
446                                 }
447
448                         } else {
449 #ifdef NEW_LOGGING
450                                 LDAP_LOG( BACK_LDBM, DETAIL2,
451                                         "ldbm_search: candidate entry %ld scope not okay\n", 
452                                         id, 0, 0 );
453 #else
454                                 Debug( LDAP_DEBUG_TRACE,
455                                         "ldbm_search: candidate entry %ld scope not okay\n",
456                                         id, 0, 0 );
457 #endif
458                         }
459
460                 } else {
461 #ifdef NEW_LOGGING
462                         LDAP_LOG( BACK_LDBM, DETAIL2,
463                                 "ldbm_search: candidate entry %ld does not match filter\n", 
464                                 id, 0, 0 );
465 #else
466                         Debug( LDAP_DEBUG_TRACE,
467                                 "ldbm_search: candidate entry %ld does not match filter\n",
468                                 id, 0, 0 );
469 #endif
470                 }
471
472 loop_continue:
473                 if( e != NULL ) {
474                         /* free reader lock */
475                         cache_return_entry_r( &li->li_cache, e );
476                 }
477
478                 ldap_pvt_thread_yield();
479         }
480
481         rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
482         rs->sr_ref = rs->sr_v2ref;
483         send_ldap_result( op, rs );
484
485         rc = LDAP_SUCCESS;
486
487 done:
488         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
489
490         if( candidates != NULL )
491                 idl_free( candidates );
492
493         if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
494         if( realbase.bv_val ) free( realbase.bv_val );
495
496         return rc;
497 }
498
499 static ID_BLOCK *
500 base_candidate(
501     Backend     *be,
502         Entry   *e )
503 {
504         ID_BLOCK                *idl;
505
506 #ifdef NEW_LOGGING
507         LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
508 #else
509         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
510                 e->e_dn, 0, 0);
511 #endif
512
513
514         idl = idl_alloc( 1 );
515         idl_insert( &idl, e->e_id, 1 );
516
517         return( idl );
518 }
519
520 static ID_BLOCK *
521 search_candidates(
522     Operation   *op,
523     Entry       *e,
524     Filter      *filter,
525     int         scope,
526         int             deref,
527         int             manageDSAit )
528 {
529         ID_BLOCK                *candidates;
530         Filter          f, fand, rf, af, xf;
531     AttributeAssertion aa_ref, aa_alias;
532         struct berval bv_ref = { sizeof("referral")-1, "referral" };
533         struct berval bv_alias = { sizeof("alias")-1, "alias" };
534 #ifdef LDBM_SUBENTRIES
535         Filter  sf;
536         AttributeAssertion aa_subentry;
537 #endif
538
539 #ifdef NEW_LOGGING
540         LDAP_LOG( BACK_LDBM, DETAIL1,
541                 "search_candidates: base (%s) scope %d deref %d\n",
542                 e->e_ndn, scope, deref );
543 #else
544         Debug(LDAP_DEBUG_TRACE,
545                 "search_candidates: base=\"%s\" s=%d d=%d\n",
546                 e->e_ndn, scope, deref );
547 #endif
548
549
550         xf.f_or = filter;
551         xf.f_choice = LDAP_FILTER_OR;
552         xf.f_next = NULL;
553
554         if( !manageDSAit ) {
555                 /* match referrals */
556                 rf.f_choice = LDAP_FILTER_EQUALITY;
557                 rf.f_ava = &aa_ref;
558                 rf.f_av_desc = slap_schema.si_ad_objectClass;
559                 rf.f_av_value = bv_ref;
560                 rf.f_next = xf.f_or;
561                 xf.f_or = &rf;
562         }
563
564         if( deref & LDAP_DEREF_SEARCHING ) {
565                 /* match aliases */
566                 af.f_choice = LDAP_FILTER_EQUALITY;
567                 af.f_ava = &aa_alias;
568                 af.f_av_desc = slap_schema.si_ad_objectClass;
569                 af.f_av_value = bv_alias;
570                 af.f_next = xf.f_or;
571                 xf.f_or = &af;
572         }
573
574         f.f_next = NULL;
575         f.f_choice = LDAP_FILTER_AND;
576         f.f_and = &fand;
577         fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
578                 ? SLAPD_FILTER_DN_ONE
579                 : SLAPD_FILTER_DN_SUBTREE;
580         fand.f_dn = &e->e_nname;
581         fand.f_next = xf.f_or == filter ? filter : &xf ;
582
583 #ifdef LDBM_SUBENTRIES
584         if ( get_subentries_visibility( op )) {
585                 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
586                 sf.f_choice = LDAP_FILTER_EQUALITY;
587                 sf.f_ava = &aa_subentry;
588                 sf.f_av_desc = slap_schema.si_ad_objectClass;
589                 sf.f_av_value = bv_subentry;
590                 sf.f_next = fand.f_next;
591                 fand.f_next = &sf;
592         }
593 #endif
594
595         candidates = filter_candidates( op, &f );
596         return( candidates );
597 }