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