]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
fix typo; try to delete dn2id in case of late failure
[openldap] / servers / slapd / back-ldbm / search.c
1 /* search.c - ldbm backend search function */
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
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         int timelimit = -1, sizelimit = -1;
57         int isroot = 0;
58                 
59 #ifdef NEW_LOGGING
60         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
61                    "ldbm_back_search: enter\n" ));
62 #else
63         Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
64 #endif
65
66
67         if ( *nbase == '\0' ) {
68                 /* DIT root special case */
69                 static const Entry root = { NOID, "", "", NULL, NULL };
70                 e = (Entry *) &root;
71
72                 /* need normalized dn below */
73                 realbase = ch_strdup( e->e_ndn );
74
75                 candidates = search_candidates( be, e, filter,
76                     scope, deref, manageDSAit );
77
78                 goto searchit;
79                 
80         } else if ( deref & LDAP_DEREF_FINDING ) {
81                 /* deref dn and get entry with reader lock */
82                 e = deref_dn_r( be, nbase, &err, &matched, &text );
83
84                 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
85
86         } else {
87                 /* get entry with reader lock */
88                 e = dn2entry_r( be, nbase, &matched );
89                 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
90                 text = NULL;
91         }
92
93         if ( e == NULL ) {
94                 char *matched_dn = NULL;
95                 struct berval **refs = NULL;
96
97                 if ( matched != NULL ) {
98                         matched_dn = ch_strdup( matched->e_dn );
99
100                         refs = is_entry_referral( matched )
101                                 ? get_entry_referrals( be, conn, op, matched )
102                                 : NULL;
103
104                         cache_return_entry_r( &li->li_cache, matched );
105
106                 } else {
107                         refs = default_referral;
108                 }
109
110                 send_ldap_result( conn, op, err,
111                         matched_dn, text, refs, NULL );
112
113                 if( matched != NULL ) {
114                         ber_bvecfree( refs );
115                         free( matched_dn );
116                 }
117
118                 return 1;
119         }
120
121         if (!manageDSAit && is_entry_referral( e ) ) {
122                 /* entry is a referral, don't allow add */
123                 char *matched_dn = ch_strdup( e->e_dn );
124                 struct berval **refs = get_entry_referrals( be,
125                         conn, op, e );
126
127                 cache_return_entry_r( &li->li_cache, e );
128
129 #ifdef NEW_LOGGING
130                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
131                            "ldbm_search: entry (%s) is a referral.\n",
132                            e->e_dn ));
133 #else
134                 Debug( LDAP_DEBUG_TRACE,
135                         "ldbm_search: entry is referral\n",
136                         0, 0, 0 );
137 #endif
138
139
140                 send_ldap_result( conn, op, LDAP_REFERRAL,
141                     matched_dn, NULL, refs, NULL );
142
143                 ber_bvecfree( refs );
144                 free( matched_dn );
145
146                 return 1;
147         }
148
149         if ( is_entry_alias( e ) ) {
150                 /* don't deref */
151                 deref = LDAP_DEREF_NEVER;
152         }
153
154         if ( scope == LDAP_SCOPE_BASE ) {
155                 candidates = base_candidate( be, e );
156
157         } else {
158                 candidates = search_candidates( be, e, filter,
159                     scope, deref, manageDSAit );
160         }
161
162         /* need normalized dn below */
163         realbase = ch_strdup( e->e_ndn );
164
165         cache_return_entry_r( &li->li_cache, e );
166
167 searchit:
168         if ( candidates == NULL ) {
169                 /* no candidates */
170 #ifdef NEW_LOGGING
171                 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
172                            "ldbm_search: no candidates\n" ));
173 #else
174                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
175                         0, 0, 0 );
176 #endif
177
178
179                 send_search_result( conn, op,
180                         LDAP_SUCCESS,
181                         NULL, NULL, NULL, NULL, 0 );
182
183                 rc = 1;
184                 goto done;
185         }
186
187         if ( be_isroot( be, op->o_ndn ) ) {
188                 isroot = 1;
189         } else {
190                 if ( get_limits( be, op->o_ndn, &timelimit, &sizelimit) ) {
191                         timelimit = be->be_timelimit;
192                         sizelimit = be->be_sizelimit;
193                 }
194         }
195
196         if ( tlimit == 0 && isroot ) {
197                 tlimit = -1;    /* allow root to set no limit */
198         } else {
199                 tlimit = (tlimit > timelimit || tlimit < 1) ?
200                     timelimit : tlimit;
201                 stoptime = op->o_time + tlimit;
202         }
203
204         if ( slimit == 0 && isroot ) {
205                 slimit = -1;    /* allow root to set no limit */
206         } else {
207                 slimit = (slimit > sizelimit || slimit < 1) ?
208                     sizelimit : slimit;
209         }
210
211         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
212             id = idl_nextid( candidates, &cursor ) )
213         {
214                 int             scopeok = 0;
215
216                 /* check for abandon */
217                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
218
219                 if ( op->o_abandon ) {
220                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
221                         rc = 0;
222                         goto done;
223                 }
224
225                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
226
227                 /* check time limit */
228                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
229                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
230                                 NULL, NULL, v2refs, NULL, nentries );
231                         rc = 0;
232                         goto done;
233                 }
234
235                 /* get the entry with reader lock */
236                 e = id2entry_r( be, id );
237
238                 if ( e == NULL ) {
239 #ifdef NEW_LOGGING
240                         LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
241                                    "ldbm_search: candidate %ld not found.\n", id ));
242 #else
243                         Debug( LDAP_DEBUG_TRACE,
244                                 "ldbm_search: candidate %ld not found\n",
245                                 id, 0, 0 );
246 #endif
247
248
249                         goto loop_continue;
250                 }
251
252                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
253                         Entry *matched;
254                         int err;
255                         const char *text;
256                         
257                         e = deref_entry_r( be, e, &err, &matched, &text );
258
259                         if( e == NULL ) {
260                                 e = matched;
261                                 goto loop_continue;
262                         }
263
264                         if( e->e_id == id ) {
265                                 /* circular loop */
266                                 goto loop_continue;
267                         }
268
269                         /* need to skip alias which deref into scope */
270                         if( scope & LDAP_SCOPE_ONELEVEL ) {
271                                 char *pdn = dn_parent( NULL, e->e_ndn );
272                                 if ( pdn != NULL ) {
273                                         if( strcmp( pdn, realbase ) ) {
274                                                 free( pdn );
275                                                 goto loop_continue;
276                                         }
277                                         free(pdn);
278                                 }
279
280                         } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
281                                 /* alias is within scope */
282 #ifdef NEW_LOGGING
283                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
284                                            "ldbm_search: \"%s\" in subtree\n", e->e_dn ));
285 #else
286                                 Debug( LDAP_DEBUG_TRACE,
287                                         "ldbm_search: \"%s\" in subtree\n",
288                                         e->e_dn, 0, 0 );
289 #endif
290
291                                 goto loop_continue;
292                         }
293
294                         scopeok = 1;
295                 }
296
297                 /*
298                  * if it's a referral, add it to the list of referrals. only do
299                  * this for non-base searches, and don't check the filter
300                  * explicitly here since it's only a candidate anyway.
301                  */
302                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
303                         is_entry_referral( e ) )
304                 {
305                         struct berval **refs = get_entry_referrals(
306                                 be, conn, op, e );
307
308                         send_search_reference( be, conn, op,
309                                 e, refs, scope, NULL, &v2refs );
310
311                         ber_bvecfree( refs );
312
313                         goto loop_continue;
314                 }
315
316                 /* if it matches the filter and scope, send it */
317                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
318                         char    *dn;
319
320                         /* check scope */
321                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
322                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
323                                         (void) dn_normalize( dn );
324                                         scopeok = (dn == realbase)
325                                                 ? 1
326                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
327                                         free( dn );
328
329                                 } else {
330                                         scopeok = (realbase == NULL || *realbase == '\0');
331                                 }
332
333                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
334                                 dn = ch_strdup( e->e_ndn );
335                                 scopeok = dn_issuffix( dn, realbase );
336                                 free( dn );
337
338                         } else {
339                                 scopeok = 1;
340                         }
341
342                         if ( scopeok ) {
343                                 /* check size limit */
344                                 if ( --slimit == -1 ) {
345                                         cache_return_entry_r( &li->li_cache, e );
346                                         send_search_result( conn, op,
347                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
348                                                 v2refs, NULL, nentries );
349                                         rc = 0;
350                                         goto done;
351                                 }
352
353                                 if (e) {
354                                         int result = send_search_entry(be, conn, op,
355                                                 e, attrs, attrsonly, NULL);
356
357                                         switch (result) {
358                                         case 0:         /* entry sent ok */
359                                                 nentries++;
360                                                 break;
361                                         case 1:         /* entry not sent */
362                                                 break;
363                                         case -1:        /* connection closed */
364                                                 cache_return_entry_r( &li->li_cache, e );
365                                                 rc = 0;
366                                                 goto done;
367                                         }
368                                 }
369                         } else {
370 #ifdef NEW_LOGGING
371                                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
372                                            "ldbm_search: candidate %ld scope not okay\n", id ));
373 #else
374                                 Debug( LDAP_DEBUG_TRACE,
375                                         "ldbm_search: candidate %ld scope not okay\n",
376                                         id, 0, 0 );
377 #endif
378
379                         }
380                 } else {
381 #ifdef NEW_LOGGING
382                         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
383                                    "ldbm_search: candidate %ld does not match filter\n", id ));
384 #else
385                         Debug( LDAP_DEBUG_TRACE,
386                                 "ldbm_search: candidate %ld does not match filter\n",
387                                 id, 0, 0 );
388 #endif
389
390                 }
391
392 loop_continue:
393                 if( e != NULL ) {
394                         /* free reader lock */
395                         cache_return_entry_r( &li->li_cache, e );
396                 }
397
398                 ldap_pvt_thread_yield();
399         }
400         send_search_result( conn, op,
401                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
402                 NULL, NULL, v2refs, NULL, nentries );
403
404         rc = 0;
405
406 done:
407         if( candidates != NULL )
408                 idl_free( candidates );
409
410         ber_bvecfree( v2refs );
411         if( realbase ) free( realbase );
412
413         return rc;
414 }
415
416 static ID_BLOCK *
417 base_candidate(
418     Backend     *be,
419         Entry   *e )
420 {
421         ID_BLOCK                *idl;
422
423 #ifdef NEW_LOGGING
424         LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
425                    "base_candidate: base (%s)\n", e->e_dn ));
426 #else
427         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
428                 e->e_dn, 0, 0);
429 #endif
430
431
432         idl = idl_alloc( 1 );
433         idl_insert( &idl, e->e_id, 1 );
434
435         return( idl );
436 }
437
438 static ID_BLOCK *
439 search_candidates(
440     Backend     *be,
441     Entry       *e,
442     Filter      *filter,
443     int         scope,
444         int             deref,
445         int             manageDSAit )
446 {
447         ID_BLOCK                *candidates;
448         Filter          f, fand, rf, af, xf;
449     AttributeAssertion aa_ref, aa_alias;
450         static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
451         static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
452
453 #ifdef NEW_LOGGING
454         LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
455                    "search_candidates: base (%s) scope %d deref %d\n",
456                    e->e_ndn, scope, deref ));
457 #else
458         Debug(LDAP_DEBUG_TRACE,
459                 "search_candidates: base=\"%s\" s=%d d=%d\n",
460                 e->e_ndn, scope, deref );
461 #endif
462
463
464         xf.f_or = filter;
465         xf.f_choice = LDAP_FILTER_OR;
466         xf.f_next = NULL;
467
468         if( !manageDSAit ) {
469                 /* match referrals */
470                 rf.f_choice = LDAP_FILTER_EQUALITY;
471                 rf.f_ava = &aa_ref;
472                 rf.f_av_desc = slap_schema.si_ad_objectClass;
473                 rf.f_av_value = &bv_ref;
474                 rf.f_next = xf.f_or;
475                 xf.f_or = &rf;
476         }
477
478         if( deref & LDAP_DEREF_SEARCHING ) {
479                 /* match aliases */
480                 af.f_choice = LDAP_FILTER_EQUALITY;
481                 af.f_ava = &aa_alias;
482                 af.f_av_desc = slap_schema.si_ad_objectClass;
483                 af.f_av_value = &bv_alias;
484                 af.f_next = xf.f_or;
485                 xf.f_or = &af;
486         }
487
488         f.f_next = NULL;
489         f.f_choice = LDAP_FILTER_AND;
490         f.f_and = &fand;
491         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
492                 ? SLAPD_FILTER_DN_SUBTREE
493                 : SLAPD_FILTER_DN_ONE;
494         fand.f_dn = e->e_ndn;
495         fand.f_next = xf.f_or == filter ? filter : &xf ;
496
497         candidates = filter_candidates( be, &f );
498
499         return( candidates );
500 }