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