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