]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
9a7a0a16f275f036c78f1129651af2dceac3c721
[openldap] / servers / slapd / back-ldbm / search.c
1 /* search.c - ldbm backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
18
19 static ID_BLOCK *base_candidate(
20         Backend *be, Entry *e );
21
22 static ID_BLOCK *search_candidates(
23         Backend *be, Entry *e, Filter *filter,
24         int scope, int deref, int manageDSAit );
25
26
27 int
28 ldbm_back_search(
29     Backend     *be,
30     Connection  *conn,
31     Operation   *op,
32     const char  *base,
33     const char  *nbase,
34     int         scope,
35     int         deref,
36     int         slimit,
37     int         tlimit,
38     Filter      *filter,
39     const char  *filterstr,
40     char        **attrs,
41     int         attrsonly )
42 {
43         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
44         int             rc, err;
45         const char *text = NULL;
46         time_t          stoptime;
47         ID_BLOCK                *candidates;
48         ID              id, cursor;
49         Entry           *e;
50         struct berval **v2refs = NULL;
51         Entry   *matched = NULL;
52         char    *realbase = NULL;
53         int             nentries = 0;
54         int             manageDSAit = get_manageDSAit( op );
55
56         Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
57
58         /* grab giant lock for reading */
59         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
60
61         if ( *nbase == '\0' ) {
62                 /* DIT root special case */
63                 static const Entry root = { NOID, "", "", NULL, NULL };
64                 e = (Entry *) &root;
65
66                 /* need normalized dn below */
67                 realbase = ch_strdup( e->e_ndn );
68
69                 candidates = search_candidates( be, e, filter,
70                     scope, deref, manageDSAit );
71
72                 goto searchit;
73                 
74         } else if ( deref & LDAP_DEREF_FINDING ) {
75                 /* deref dn and get entry with reader lock */
76                 e = deref_dn_r( be, nbase, &err, &matched, &text );
77
78                 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
79
80         } else {
81                 /* get entry with reader lock */
82                 e = dn2entry_r( be, nbase, &matched );
83                 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
84                 text = NULL;
85         }
86
87         if ( e == NULL ) {
88                 char *matched_dn = NULL;
89                 struct berval **refs = NULL;
90
91                 if ( matched != NULL ) {
92                         matched_dn = ch_strdup( matched->e_dn );
93
94                         refs = is_entry_referral( matched )
95                                 ? get_entry_referrals( be, conn, op, matched )
96                                 : NULL;
97
98                         cache_return_entry_r( &li->li_cache, matched );
99
100                 } else {
101                         refs = default_referral;
102                 }
103
104                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
105
106                 send_ldap_result( conn, op, err,
107                         matched_dn, text, refs, NULL );
108
109                 if( matched != NULL ) {
110                         ber_bvecfree( refs );
111                         free( matched_dn );
112                 }
113
114                 return 1;
115         }
116
117         if (!manageDSAit && is_entry_referral( e ) ) {
118                 /* entry is a referral, don't allow add */
119                 char *matched_dn = ch_strdup( e->e_dn );
120                 struct berval **refs = get_entry_referrals( be,
121                         conn, op, e );
122
123                 cache_return_entry_r( &li->li_cache, e );
124                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
125
126                 Debug( LDAP_DEBUG_TRACE,
127                         "ldbm_search: entry is referral\n",
128                         0, 0, 0 );
129
130                 send_ldap_result( conn, op, LDAP_REFERRAL,
131                     matched_dn, NULL, refs, NULL );
132
133                 ber_bvecfree( refs );
134                 free( matched_dn );
135
136                 return 1;
137         }
138
139         if ( is_entry_alias( e ) ) {
140                 /* don't deref */
141                 deref = LDAP_DEREF_NEVER;
142         }
143
144         if ( scope == LDAP_SCOPE_BASE ) {
145                 candidates = base_candidate( be, e );
146
147         } else {
148                 candidates = search_candidates( be, e, filter,
149                     scope, deref, manageDSAit );
150         }
151
152         /* need normalized dn below */
153         realbase = ch_strdup( e->e_ndn );
154
155         cache_return_entry_r( &li->li_cache, e );
156
157 searchit:
158         if ( candidates == NULL ) {
159                 /* no candidates */
160                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
161                         0, 0, 0 );
162
163                 send_search_result( conn, op,
164                         LDAP_SUCCESS,
165                         NULL, NULL, NULL, NULL, 0 );
166
167                 rc = 1;
168                 goto done;
169         }
170
171         if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
172                 tlimit = -1;    /* allow root to set no limit */
173         } else {
174                 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
175                     be->be_timelimit : tlimit;
176                 stoptime = op->o_time + tlimit;
177         }
178
179         if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
180                 slimit = -1;    /* allow root to set no limit */
181         } else {
182                 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
183                     be->be_sizelimit : slimit;
184         }
185
186         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
187             id = idl_nextid( candidates, &cursor ) )
188         {
189                 int             scopeok = 0;
190
191                 /* check for abandon */
192                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
193
194                 if ( op->o_abandon ) {
195                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
196                         rc = 0;
197                         goto done;
198                 }
199
200                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
201
202                 /* check time limit */
203                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
204                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
205                                 NULL, NULL, v2refs, NULL, nentries );
206                         rc = 0;
207                         goto done;
208                 }
209
210                 /* get the entry with reader lock */
211                 e = id2entry_r( be, id );
212
213                 if ( e == NULL ) {
214                         Debug( LDAP_DEBUG_TRACE,
215                                 "ldbm_search: candidate %ld not found\n",
216                                 id, 0, 0 );
217
218                         goto loop_continue;
219                 }
220
221                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
222                         Entry *matched;
223                         int err;
224                         const char *text;
225                         
226                         e = deref_entry_r( be, e, &err, &matched, &text );
227
228                         if( e == NULL ) {
229                                 e = matched;
230                                 goto loop_continue;
231                         }
232
233                         if( e->e_id == id ) {
234                                 /* circular loop */
235                                 goto loop_continue;
236                         }
237
238                         /* need to skip alias which deref into scope */
239                         if( scope & LDAP_SCOPE_ONELEVEL ) {
240                                 char *pdn = dn_parent( NULL, e->e_ndn );
241                                 if ( pdn != NULL ) {
242                                         if( strcmp( pdn, realbase ) ) {
243                                                 free( pdn );
244                                                 goto loop_continue;
245                                         }
246                                         free(pdn);
247                                 }
248
249                         } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
250                                 /* alias is within scope */
251                                 Debug( LDAP_DEBUG_TRACE,
252                                         "ldbm_search: \"%s\" in subtree\n",
253                                         e->e_dn, 0, 0 );
254                                 goto loop_continue;
255                         }
256
257                         scopeok = 1;
258                 }
259
260                 /*
261                  * if it's a referral, add it to the list of referrals. only do
262                  * this for non-base searches, and don't check the filter
263                  * explicitly here since it's only a candidate anyway.
264                  */
265                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
266                         is_entry_referral( e ) )
267                 {
268                         struct berval **refs = get_entry_referrals(
269                                 be, conn, op, e );
270
271                         send_search_reference( be, conn, op,
272                                 e, refs, scope, NULL, &v2refs );
273
274                         ber_bvecfree( refs );
275
276                         goto loop_continue;
277                 }
278
279                 /* if it matches the filter and scope, send it */
280                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
281                         char    *dn;
282
283                         /* check scope */
284                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
285                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
286                                         (void) dn_normalize( dn );
287                                         scopeok = (dn == realbase)
288                                                 ? 1
289                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
290                                         free( dn );
291
292                                 } else {
293                                         scopeok = (realbase == NULL || *realbase == '\0');
294                                 }
295
296                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
297                                 dn = ch_strdup( e->e_ndn );
298                                 scopeok = dn_issuffix( dn, realbase );
299                                 free( dn );
300
301                         } else {
302                                 scopeok = 1;
303                         }
304
305                         if ( scopeok ) {
306                                 /* check size limit */
307                                 if ( --slimit == -1 ) {
308                                         cache_return_entry_r( &li->li_cache, e );
309                                         send_search_result( conn, op,
310                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
311                                                 v2refs, NULL, nentries );
312                                         rc = 0;
313                                         goto done;
314                                 }
315
316                                 if (e) {
317                                         int result = send_search_entry(be, conn, op,
318                                                 e, attrs, attrsonly, NULL);
319
320                                         switch (result) {
321                                         case 0:         /* entry sent ok */
322                                                 nentries++;
323                                                 break;
324                                         case 1:         /* entry not sent */
325                                                 break;
326                                         case -1:        /* connection closed */
327                                                 cache_return_entry_r( &li->li_cache, e );
328                                                 rc = 0;
329                                                 goto done;
330                                         }
331                                 }
332                         } else {
333                                 Debug( LDAP_DEBUG_TRACE,
334                                         "ldbm_search: candidate %ld scope not okay\n",
335                                         id, 0, 0 );
336                         }
337                 } else {
338                         Debug( LDAP_DEBUG_TRACE,
339                                 "ldbm_search: candidate %ld does not match filter\n",
340                                 id, 0, 0 );
341                 }
342
343 loop_continue:
344                 if( e != NULL ) {
345                         /* free reader lock */
346                         cache_return_entry_r( &li->li_cache, e );
347                 }
348
349                 ldap_pvt_thread_yield();
350         }
351         send_search_result( conn, op,
352                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
353                 NULL, NULL, v2refs, NULL, nentries );
354
355         rc = 0;
356
357 done:
358         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
359
360         if( candidates != NULL )
361                 idl_free( candidates );
362
363         ber_bvecfree( v2refs );
364         if( realbase ) free( realbase );
365
366         return rc;
367 }
368
369 static ID_BLOCK *
370 base_candidate(
371     Backend     *be,
372         Entry   *e )
373 {
374         ID_BLOCK                *idl;
375
376         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
377                 e->e_dn, 0, 0);
378
379         idl = idl_alloc( 1 );
380         idl_insert( &idl, e->e_id, 1 );
381
382         return( idl );
383 }
384
385 static ID_BLOCK *
386 search_candidates(
387     Backend     *be,
388     Entry       *e,
389     Filter      *filter,
390     int         scope,
391         int             deref,
392         int             manageDSAit )
393 {
394         ID_BLOCK                *candidates;
395         Filter          f, fand, rf, af, xf;
396     AttributeAssertion aa_ref, aa_alias;
397         static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
398         static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
399
400         Debug(LDAP_DEBUG_TRACE,
401                 "search_candidates: base=\"%s\" s=%d d=%d\n",
402                 e->e_ndn, scope, deref );
403
404         xf.f_or = filter;
405         xf.f_choice = LDAP_FILTER_OR;
406         xf.f_next = NULL;
407
408         if( !manageDSAit ) {
409                 /* match referrals */
410                 rf.f_choice = LDAP_FILTER_EQUALITY;
411                 rf.f_ava = &aa_ref;
412                 rf.f_av_desc = slap_schema.si_ad_objectClass;
413                 rf.f_av_value = &bv_ref;
414                 rf.f_next = xf.f_or;
415                 xf.f_or = &rf;
416         }
417
418         if( deref & LDAP_DEREF_SEARCHING ) {
419                 /* match aliases */
420                 af.f_choice = LDAP_FILTER_EQUALITY;
421                 af.f_ava = &aa_alias;
422                 af.f_av_desc = slap_schema.si_ad_objectClass;
423                 af.f_av_value = &bv_alias;
424                 af.f_next = xf.f_or;
425                 xf.f_or = &af;
426         }
427
428         f.f_next = NULL;
429         f.f_choice = LDAP_FILTER_AND;
430         f.f_and = &fand;
431         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
432                 ? SLAPD_FILTER_DN_SUBTREE
433                 : SLAPD_FILTER_DN_ONE;
434         fand.f_dn = e->e_ndn;
435         fand.f_next = xf.f_or == filter ? filter : &xf ;
436
437         candidates = filter_candidates( be, &f );
438
439         return( candidates );
440 }