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