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