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