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