]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
bdee339fb2ab4e76291bf4c9c7d3fbe7f668ceb8
[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 {
44         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
45         int             rc, err;
46         const char *text = NULL;
47         time_t          stoptime;
48         ID_BLOCK                *candidates;
49         ID              id, cursor;
50         Entry           *e;
51         struct berval **v2refs = NULL;
52         Entry   *matched = NULL;
53         char    *realbase = NULL;
54         int             nentries = 0;
55         int             manageDSAit = get_manageDSAit( op );
56
57         Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
58
59         /* get entry with reader lock */
60         if ( deref & LDAP_DEREF_FINDING ) {
61                 e = deref_dn_r( be, nbase, &err, &matched, &text );
62
63         } else {
64                 e = dn2entry_r( be, nbase, &matched );
65                 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
66                 text = NULL;
67         }
68
69         if ( e == NULL ) {
70                 char *matched_dn = NULL;
71                 struct berval **refs = NULL;
72
73                 if ( matched != NULL ) {
74                         matched_dn = ch_strdup( matched->e_dn );
75
76                         refs = is_entry_referral( matched )
77                                 ? get_entry_referrals( be, conn, op, matched )
78                                 : NULL;
79
80                         cache_return_entry_r( &li->li_cache, matched );
81                 } else {
82                         refs = default_referral;
83                 }
84
85                 send_ldap_result( conn, op, err,
86                         matched_dn, text, refs, NULL );
87
88                 if( matched != NULL ) {
89                         ber_bvecfree( refs );
90                         free( matched_dn );
91                 }
92
93                 return 1;
94         }
95
96         if (!manageDSAit && is_entry_referral( e ) ) {
97                 /* entry is a referral, don't allow add */
98                 char *matched_dn = ch_strdup( e->e_dn );
99                 struct berval **refs = get_entry_referrals( be,
100                         conn, op, e );
101
102                 cache_return_entry_r( &li->li_cache, e );
103
104                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
105                     0, 0 );
106
107                 send_ldap_result( conn, op, LDAP_REFERRAL,
108                     matched_dn, NULL, refs, NULL );
109
110                 ber_bvecfree( refs );
111                 free( matched_dn );
112
113                 return 1;
114         }
115
116         if ( is_entry_alias( e ) ) {
117                 /* don't deref */
118                 deref = LDAP_DEREF_NEVER;
119         }
120
121         if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
122                 tlimit = -1;    /* allow root to set no limit */
123         } else {
124                 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
125                     be->be_timelimit : tlimit;
126                 stoptime = op->o_time + tlimit;
127         }
128
129         if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
130                 slimit = -1;    /* allow root to set no limit */
131         } else {
132                 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
133                     be->be_sizelimit : slimit;
134         }
135
136         if ( scope == LDAP_SCOPE_BASE ) {
137                 candidates = base_candidate( be, e );
138
139         } else {
140                 candidates = search_candidates( be, e, filter,
141                     scope, deref, manageDSAit );
142         }
143
144         /* need normalized dn below */
145         realbase = ch_strdup( e->e_ndn );
146
147         cache_return_entry_r( &li->li_cache, e );
148
149         if ( candidates == NULL ) {
150                 /* no candidates */
151                 Debug( LDAP_DEBUG_TRACE, "no candidates\n", 0,
152                     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         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
163             id = idl_nextid( candidates, &cursor ) )
164         {
165                 int             scopeok = 0;
166
167                 /* check for abandon */
168                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
169
170                 if ( op->o_abandon ) {
171                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
172                         rc = 0;
173                         goto done;
174                 }
175
176                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
177
178                 /* check time limit */
179                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
180                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
181                                 NULL, NULL, v2refs, NULL, nentries );
182                         rc = 0;
183                         goto done;
184                 }
185
186                 /* get the entry with reader lock */
187                 e = id2entry_r( be, id );
188
189                 if ( e == NULL ) {
190                         Debug( LDAP_DEBUG_ARGS, "search: candidate %ld not found\n",
191                                 id, 0, 0 );
192
193                         goto loop_continue;
194                 }
195
196                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
197                         Entry *matched;
198                         int err;
199                         const char *text;
200                         
201                         e = deref_entry_r( be, e, &err, &matched, &text );
202
203                         if( e == NULL ) {
204                                 e = matched;
205                                 goto loop_continue;
206                         }
207
208                         if( e->e_id == id ) {
209                                 /* circular loop */
210                                 goto loop_continue;
211                         }
212
213                         /* need to skip alias which deref into scope */
214                         if( scope & LDAP_SCOPE_ONELEVEL ) {
215                                 char *pdn = dn_parent( NULL, e->e_ndn );
216                                 if ( pdn != NULL ) {
217                                         if( strcmp( pdn, realbase ) ) {
218                                                 free( pdn );
219                                                 goto loop_continue;
220                                         }
221                                         free(pdn);
222                                 }
223
224                         } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
225                                 /* alias is within scope */
226                                 Debug( LDAP_DEBUG_ARGS, "search: \"%s\" in subtree\n",
227                                         e->e_dn, 0, 0 );
228                                 goto loop_continue;
229                         }
230
231                         scopeok = 1;
232                 }
233
234                 /*
235                  * if it's a referral, add it to the list of referrals. only do
236                  * this for non-base searches, and don't check the filter
237                  * explicitly here since it's only a candidate anyway.
238                  */
239                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
240                         is_entry_referral( e ) )
241                 {
242                         struct berval **refs = get_entry_referrals(
243                                 be, conn, op, e );
244
245                         send_search_reference( be, conn, op,
246                                 e, refs, scope, NULL, &v2refs );
247
248                         ber_bvecfree( refs );
249
250                         goto loop_continue;
251                 }
252
253                 /* if it matches the filter and scope, send it */
254                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
255                         char    *dn;
256
257                         /* check scope */
258                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
259                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
260                                         (void) dn_normalize( dn );
261                                         scopeok = (dn == realbase)
262                                                 ? 1
263                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
264                                         free( dn );
265
266                                 } else {
267                                         scopeok = (realbase == NULL || *realbase == '\0');
268                                 }
269
270                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
271                                 dn = ch_strdup( e->e_ndn );
272                                 scopeok = dn_issuffix( dn, realbase );
273                                 free( dn );
274
275                         } else {
276                                 scopeok = 1;
277                         }
278
279                         if ( scopeok ) {
280                                 /* check size limit */
281                                 if ( --slimit == -1 ) {
282                                         cache_return_entry_r( &li->li_cache, e );
283                                         send_search_result( conn, op,
284                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
285                                                 v2refs, NULL, nentries );
286                                         rc = 0;
287                                         goto done;
288                                 }
289
290                                 if (e) {
291                                         int result = send_search_entry(be, conn, op,
292                                                 e, attrs, attrsonly, NULL);
293
294                                         switch (result) {
295                                         case 0:         /* entry sent ok */
296                                                 nentries++;
297                                                 break;
298                                         case 1:         /* entry not sent */
299                                                 break;
300                                         case -1:        /* connection closed */
301                                                 cache_return_entry_r( &li->li_cache, e );
302                                                 rc = 0;
303                                                 goto done;
304                                         }
305                                 }
306                         } else {
307                                 Debug( LDAP_DEBUG_TRACE, "candidate %ld scope not okay\n",
308                                         id, 0, 0 );
309                         }
310                 } else {
311                         Debug( LDAP_DEBUG_TRACE, "candidate %ld does match filter\n",
312                                 id, 0, 0 );
313                 }
314
315 loop_continue:
316                 if( e != NULL ) {
317                         /* free reader lock */
318                         cache_return_entry_r( &li->li_cache, e );
319                 }
320
321                 ldap_pvt_thread_yield();
322         }
323         send_search_result( conn, op,
324                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
325                 NULL, NULL, v2refs, NULL, nentries );
326
327         rc = 0;
328
329 done:
330         if( candidates != NULL )
331                 idl_free( candidates );
332
333         ber_bvecfree( v2refs );
334         if( realbase ) free( realbase );
335
336         return rc;
337 }
338
339 static ID_BLOCK *
340 base_candidate(
341     Backend     *be,
342         Entry   *e
343 )
344 {
345         ID_BLOCK                *idl;
346
347         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
348                 e->e_dn, 0, 0);
349
350         idl = idl_alloc( 1 );
351         idl_insert( &idl, e->e_id, 1 );
352
353         return( idl );
354 }
355
356 static ID_BLOCK *
357 search_candidates(
358     Backend     *be,
359     Entry       *e,
360     Filter      *filter,
361     int         scope,
362         int             deref,
363         int             manageDSAit
364 )
365 {
366         ID_BLOCK                *candidates;
367         candidates = filter_candidates( be, filter );
368
369         return( candidates );
370 }