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