]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
cleanup for release engineering
[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 #ifdef LDAP_SCOPE_SUBORDINATE
351                         } else if ( !scopeok
352                                 && op->ors_scope == LDAP_SCOPE_SUBORDINATE )
353                         {
354                                 scopeok = !dn_match( &e->e_nname, &realbase )
355                                         && dnIsSuffix( &e->e_nname, &realbase );
356 #endif
357
358                         } else {
359                                 scopeok = 1;
360                         }
361
362                         if( scopeok ) {
363                                 BerVarray erefs = get_entry_referrals( op, e );
364                                 rs->sr_ref = referral_rewrite( erefs,
365                                         &e->e_name, NULL,
366                                         op->ors_scope == LDAP_SCOPE_ONELEVEL
367                                                 ? LDAP_SCOPE_BASE
368                                                 : LDAP_SCOPE_SUBTREE );
369
370                                 send_search_reference( op, rs );
371
372                                 ber_bvarray_free( rs->sr_ref );
373                                 rs->sr_ref = NULL;
374
375                         } else {
376 #ifdef NEW_LOGGING
377                                 LDAP_LOG( BACK_LDBM, DETAIL2,
378                                         "ldbm_search: candidate referral %ld scope not okay\n",
379                                         id, 0, 0 );
380 #else
381                                 Debug( LDAP_DEBUG_TRACE,
382                                         "ldbm_search: candidate referral %ld scope not okay\n",
383                                         id, 0, 0 );
384 #endif
385                         }
386
387                         goto loop_continue;
388                 }
389
390                 if ( !manageDSAit && is_entry_glue( e )) {
391                         goto loop_continue;
392                 }
393
394                 /* if it matches the filter and scope, send it */
395                 result = test_filter( op, e, op->ors_filter );
396
397                 if ( result == LDAP_COMPARE_TRUE ) {
398                         struct berval   dn;
399
400                         /* check scope */
401                         if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
402                                 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
403                                         dnParent( &e->e_nname, &dn );
404                                         scopeok = dn_match( &dn, &realbase );
405                                 } else {
406                                         scopeok = (realbase.bv_len == 0);
407                                 }
408
409                         } else if ( !scopeok &&
410                                 op->ors_scope == LDAP_SCOPE_SUBTREE )
411                         {
412                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
413
414 #ifdef LDAP_SCOPE_SUBORDINATE
415                         } else if ( !scopeok &&
416                                 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
417                         {
418                                 scopeok = !dn_match( &e->e_nname, &realbase )
419                                         && dnIsSuffix( &e->e_nname, &realbase );
420 #endif
421
422                         } else {
423                                 scopeok = 1;
424                         }
425
426                         if ( scopeok ) {
427                                 /* check size limit */
428                                 if ( --op->ors_slimit == -1 ) {
429                                         cache_return_entry_r( &li->li_cache, e );
430                                         rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
431                                         send_ldap_result( op, rs );
432                                         rc = LDAP_SUCCESS;
433                                         goto done;
434                                 }
435
436                                 if (e) {
437                                         rs->sr_flags = 0;
438                                         result = send_search_entry( op, rs );
439
440                                         switch (result) {
441                                         case 0:         /* entry sent ok */
442                                                 break;
443                                         case 1:         /* entry not sent */
444                                                 break;
445                                         case -1:        /* connection closed */
446                                                 cache_return_entry_r( &li->li_cache, e );
447                                                 rc = LDAP_SUCCESS;
448                                                 goto done;
449                                         }
450                                 }
451
452                         } else {
453 #ifdef NEW_LOGGING
454                                 LDAP_LOG( BACK_LDBM, DETAIL2,
455                                         "ldbm_search: candidate entry %ld scope not okay\n", 
456                                         id, 0, 0 );
457 #else
458                                 Debug( LDAP_DEBUG_TRACE,
459                                         "ldbm_search: candidate entry %ld scope not okay\n",
460                                         id, 0, 0 );
461 #endif
462                         }
463
464                 } else {
465 #ifdef NEW_LOGGING
466                         LDAP_LOG( BACK_LDBM, DETAIL2,
467                                 "ldbm_search: candidate entry %ld does not match filter\n", 
468                                 id, 0, 0 );
469 #else
470                         Debug( LDAP_DEBUG_TRACE,
471                                 "ldbm_search: candidate entry %ld does not match filter\n",
472                                 id, 0, 0 );
473 #endif
474                 }
475
476 loop_continue:
477                 if( e != NULL ) {
478                         /* free reader lock */
479                         cache_return_entry_r( &li->li_cache, e );
480                 }
481
482                 ldap_pvt_thread_yield();
483         }
484
485         rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
486         rs->sr_ref = rs->sr_v2ref;
487         send_ldap_result( op, rs );
488
489         rc = LDAP_SUCCESS;
490
491 done:
492         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
493
494         if( candidates != NULL )
495                 idl_free( candidates );
496
497         if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
498         if( realbase.bv_val ) free( realbase.bv_val );
499
500         return rc;
501 }
502
503 static ID_BLOCK *
504 base_candidate(
505     Backend     *be,
506         Entry   *e )
507 {
508         ID_BLOCK                *idl;
509
510 #ifdef NEW_LOGGING
511         LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
512 #else
513         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
514                 e->e_dn, 0, 0);
515 #endif
516
517
518         idl = idl_alloc( 1 );
519         idl_insert( &idl, e->e_id, 1 );
520
521         return( idl );
522 }
523
524 static ID_BLOCK *
525 search_candidates(
526     Operation   *op,
527     Entry       *e,
528     Filter      *filter,
529     int         scope,
530         int             deref,
531         int             manageDSAit )
532 {
533         ID_BLOCK                *candidates;
534         Filter          f, fand, rf, af, xf;
535     AttributeAssertion aa_ref, aa_alias;
536         struct berval bv_ref = { sizeof("referral")-1, "referral" };
537         struct berval bv_alias = { sizeof("alias")-1, "alias" };
538 #ifdef LDBM_SUBENTRIES
539         Filter  sf;
540         AttributeAssertion aa_subentry;
541 #endif
542
543 #ifdef NEW_LOGGING
544         LDAP_LOG( BACK_LDBM, DETAIL1,
545                 "search_candidates: base (%s) scope %d deref %d\n",
546                 e->e_ndn, scope, deref );
547 #else
548         Debug(LDAP_DEBUG_TRACE,
549                 "search_candidates: base=\"%s\" s=%d d=%d\n",
550                 e->e_ndn, scope, deref );
551 #endif
552
553
554         xf.f_or = filter;
555         xf.f_choice = LDAP_FILTER_OR;
556         xf.f_next = NULL;
557
558         if( !manageDSAit ) {
559                 /* match referrals */
560                 rf.f_choice = LDAP_FILTER_EQUALITY;
561                 rf.f_ava = &aa_ref;
562                 rf.f_av_desc = slap_schema.si_ad_objectClass;
563                 rf.f_av_value = bv_ref;
564                 rf.f_next = xf.f_or;
565                 xf.f_or = &rf;
566         }
567
568         if( deref & LDAP_DEREF_SEARCHING ) {
569                 /* match aliases */
570                 af.f_choice = LDAP_FILTER_EQUALITY;
571                 af.f_ava = &aa_alias;
572                 af.f_av_desc = slap_schema.si_ad_objectClass;
573                 af.f_av_value = bv_alias;
574                 af.f_next = xf.f_or;
575                 xf.f_or = &af;
576         }
577
578         f.f_next = NULL;
579         f.f_choice = LDAP_FILTER_AND;
580         f.f_and = &fand;
581         fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
582                 ? SLAPD_FILTER_DN_ONE
583                 : SLAPD_FILTER_DN_SUBTREE;
584         fand.f_dn = &e->e_nname;
585         fand.f_next = xf.f_or == filter ? filter : &xf ;
586
587 #ifdef LDBM_SUBENTRIES
588         if ( get_subentries_visibility( op )) {
589                 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
590                 sf.f_choice = LDAP_FILTER_EQUALITY;
591                 sf.f_ava = &aa_subentry;
592                 sf.f_av_desc = slap_schema.si_ad_objectClass;
593                 sf.f_av_value = bv_subentry;
594                 sf.f_next = fand.f_next;
595                 fand.f_next = &sf;
596         }
597 #endif
598
599         candidates = filter_candidates( op, &f );
600         return( candidates );
601 }