]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
Round two of referrals/aliases: WORK IN PROGRESS, may not compile!
[openldap] / servers / slapd / back-ldbm / search.c
1 /* search.c - ldbm backend search function */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
13
14 static ID_BLOCK *base_candidate(
15         Backend *be, Entry *e );
16
17 static ID_BLOCK *search_candidates(
18         Backend *be, Entry *e, Filter *filter,
19         int scope, int deref, int manageDSAit );
20
21
22 int
23 ldbm_back_search(
24     Backend     *be,
25     Connection  *conn,
26     Operation   *op,
27     char        *base,
28     int         scope,
29     int         deref,
30     int         slimit,
31     int         tlimit,
32     Filter      *filter,
33     char        *filterstr,
34     char        **attrs,
35     int         attrsonly
36 )
37 {
38         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
39         int             err;
40         time_t          stoptime;
41         ID_BLOCK                *candidates;
42         ID              id;
43         Entry           *e;
44         struct berval **v2refs = NULL;
45         Entry   *matched = NULL;
46         char    *matched_dn = NULL;
47         int             nentries = 0;
48         int             manageDSAit = get_manageDSAit( op );
49
50         Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
51
52         /* get entry with reader lock */
53         switch ( deref ) {
54 #ifdef SLAPD_ALIASES
55         case LDAP_DEREF_FINDING:
56         case LDAP_DEREF_ALWAYS:
57                 e = alias_dn2entry_r( be, base, &matched, &err );
58                 break;
59 #endif
60         default:
61                 e = dn2entry_r( be, base, &matched );
62                 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
63         }
64
65         if ( e == NULL ) {
66                 char *matched_dn = NULL;
67                 struct berval **refs = NULL;
68
69                 if ( matched != NULL ) {
70                         matched_dn = ch_strdup( matched->e_dn );
71                         refs = is_entry_referral( matched )
72                                 ? get_entry_referrals( be, conn, op, matched )
73                                 : NULL;
74                         cache_return_entry_r( &li->li_cache, matched );
75                 } else {
76                         refs = default_referral;
77                 }
78
79                 send_ldap_result( conn, op, err,
80                         matched_dn, NULL, refs, NULL );
81
82                 if( matched != NULL ) {
83                         ber_bvecfree( refs );
84                         free( matched_dn );
85                 }
86
87                 return 1;
88         }
89
90         if (!manageDSAit && is_entry_referral( e ) ) {
91                 /* entry is a referral, don't allow add */
92                 char *matched_dn = ch_strdup( e->e_dn );
93                 struct berval **refs = get_entry_referrals( be,
94                         conn, op, e );
95
96                 cache_return_entry_r( &li->li_cache, matched );
97
98                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
99                     0, 0 );
100
101                 send_ldap_result( conn, op, LDAP_REFERRAL,
102                     matched_dn, NULL, refs, NULL );
103
104                 ber_bvecfree( refs );
105                 free( matched_dn );
106
107                 return 1;
108         }
109
110         if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
111                 tlimit = -1;    /* allow root to set no limit */
112         } else {
113                 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
114                     be->be_timelimit : tlimit;
115                 stoptime = op->o_time + tlimit;
116         }
117
118         if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
119                 slimit = -1;    /* allow root to set no limit */
120         } else {
121                 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
122                     be->be_sizelimit : slimit;
123         }
124
125         if ( scope == LDAP_SCOPE_BASE) {
126                 candidates = base_candidate( be, e );
127
128         } else {
129                 candidates = search_candidates( be, e, filter,
130                     scope, deref, manageDSAit );
131         }
132
133         matched_dn = ch_strdup( e->e_dn );
134         cache_return_entry_r( &li->li_cache, e );
135
136         /* null candidates means we could not find the base object */
137         if ( candidates == NULL ) {
138                 /* return a NO SUCH OBJECT */
139                 Debug( LDAP_DEBUG_TRACE, "no candidates\n", 0,
140                     0, 0 );
141
142                 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
143                     matched_dn, "no search candidates", NULL, NULL );
144
145                 free( matched_dn );
146                 return 1;
147         }
148
149         for ( id = idl_firstid( candidates ); id != NOID;
150             id = idl_nextid( candidates, id ) ) {
151
152                 /* check for abandon */
153                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
154                 if ( op->o_abandon ) {
155                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
156                         idl_free( candidates );
157                         ber_bvecfree( v2refs );
158                         free( matched_dn );
159                         return( 0 );
160                 }
161                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
162
163                 /* check time limit */
164                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
165                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
166                                 NULL, NULL, v2refs, NULL, nentries );
167                         idl_free( candidates );
168                         ber_bvecfree( v2refs );
169                         free( matched_dn );
170                         return( 0 );
171                 }
172
173                 /* get the entry with reader lock */
174                 switch ( deref ) {
175 #ifdef SLAPD_ALIASES
176                 case LDAP_DEREF_SEARCHING:
177                 case LDAP_DEREF_ALWAYS:
178                         e = alias_id2entry_r( be, id, &matched, &err );
179                 break;
180 #endif
181                 default:
182                         e = dn2entry_r( be, base, &matched );
183                         err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
184                 }
185
186                 if ( e == NULL ) {
187                         Debug( LDAP_DEBUG_ARGS, "candidate %ld not found\n",
188                                id, 0, 0 );
189                         continue;
190                 }
191
192                 /*
193                  * if it's a referral, add it to the list of referrals. only do
194                  * this for subtree searches, and don't check the filter
195                  * explicitly here since it's only a candidate anyway.
196                  */
197                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
198                         is_entry_referral( e ) )
199                 {
200                         struct berval **refs = get_entry_referrals( be, conn, op, e );
201
202                         send_search_reference( be, conn, op,
203                                 e, refs, NULL, &v2refs );
204
205                         ber_bvecfree( refs );
206
207                 /* otherwise it's an entry - see if it matches the filter */
208                 } else {
209                         /* if it matches the filter and scope, send it */
210                         if ( test_filter( be, conn, op, e, filter ) == 0 ) {
211                                 int             scopeok;
212                                 char    *dn;
213
214                                 /* check scope */
215                                 if ( scope == LDAP_SCOPE_ONELEVEL ) {
216                                         if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
217                                                 (void) dn_normalize_case( dn );
218                                                 scopeok = (dn == matched_dn)
219                                                         ? 1
220                                                         : (strcmp( dn, matched_dn ) ? 0 : 1 );
221                                                 free( dn );
222                                         } else {
223                                                 scopeok = (matched_dn == NULL || *matched_dn == '\0');
224                                         }
225                                 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
226                                         dn = ch_strdup( e->e_ndn );
227                                         scopeok = dn_issuffix( dn, matched_dn );
228                                         free( dn );
229                                 } else {
230                                         scopeok = 1;
231                                 }
232
233                                 if ( scopeok ) {
234                                         /* check size limit */
235                                         if ( --slimit == -1 ) {
236                                                 cache_return_entry_r( &li->li_cache, e );
237                                                 send_search_result( conn, op,
238                                                         LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
239                                                         v2refs, NULL, nentries );
240                                                 idl_free( candidates );
241                                                 ber_bvecfree( v2refs );
242                                                 free( matched_dn );
243                                                 return( 0 );
244                                         }
245
246                                         if (e) {
247                                                 switch ( send_search_entry( be, conn, op, e,
248                                                         attrs, attrsonly, 0, NULL ) ) {
249                                                 case 0:         /* entry sent ok */
250                                                         nentries++;
251                                                         break;
252                                                 case 1:         /* entry not sent */
253                                                         break;
254                                                 case -1:        /* connection closed */
255                                                         cache_return_entry_r( &li->li_cache, e );
256                                                         idl_free( candidates );
257                                                         ber_bvecfree( v2refs );
258                                                         free( matched_dn );
259                                                         return( 0 );
260                                                 }
261                                         }
262                                 }
263                         }
264                 }
265
266                 if( e != NULL ) {
267                         /* free reader lock */
268                         cache_return_entry_r( &li->li_cache, e );
269                 }
270
271                 ldap_pvt_thread_yield();
272         }
273         idl_free( candidates );
274
275         send_search_result( conn, op,
276                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
277                 NULL, NULL, v2refs, NULL, nentries );
278
279         ber_bvecfree( v2refs );
280
281         return( 0 );
282 }
283
284 static ID_BLOCK *
285 base_candidate(
286     Backend     *be,
287         Entry   *e
288 )
289 {
290         ID_BLOCK                *idl;
291
292         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
293                 e->e_dn, 0, 0);
294
295         idl = idl_alloc( 1 );
296         idl_insert( &idl, e->e_id, 1 );
297
298         return( idl );
299 }
300
301 static ID_BLOCK *
302 search_candidates(
303     Backend     *be,
304     Entry       *e,
305     Filter      *filter,
306     int         scope,
307         int             deref,
308         int             manageDSAit
309 )
310 {
311         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
312         ID_BLOCK                *candidates;
313         Filter          *f, *rf, *af, *lf;
314
315         Debug(LDAP_DEBUG_TRACE, "search_candidates: base: \"%s\" %s\n",
316                 e->e_dn, 0, 0 );
317
318         f = NULL;
319
320         if( !manageDSAit ) {
321                 /* match referrals */
322                 rf = (Filter *) ch_malloc( sizeof(Filter) );
323                 rf->f_next = NULL;
324                 rf->f_choice = LDAP_FILTER_OR;
325                 rf->f_or = (Filter *) ch_malloc( sizeof(Filter) );
326                 rf->f_or->f_choice = LDAP_FILTER_EQUALITY;
327                 rf->f_or->f_avtype = ch_strdup( "objectclass" );
328                 rf->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
329                 rf->f_or->f_avvalue.bv_len = sizeof("REFERRAL")-1;
330                 rf->f_or->f_next = filter;
331                 f = rf;
332         } else {
333                 rf = NULL;
334                 f = filter;
335         }
336
337         if( deref == LDAP_DEREF_SEARCHING || deref == LDAP_DEREF_ALWAYS ) {
338                 /* match aliases */
339                 af = (Filter *) ch_malloc( sizeof(Filter) );
340                 af->f_next = NULL;
341                 af->f_choice = LDAP_FILTER_OR;
342                 af->f_or = (Filter *) ch_malloc( sizeof(Filter) );
343                 af->f_or->f_choice = LDAP_FILTER_EQUALITY;
344                 af->f_or->f_avtype = ch_strdup( "objectclass" );
345                 af->f_or->f_avvalue.bv_val = ch_strdup( "ALIAS" );
346                 af->f_or->f_avvalue.bv_len = sizeof("ALIAS")-1;
347                 af->f_or->f_next = f;
348                 f = af;
349         } else {
350                 af = NULL;
351         }
352
353         lf = (Filter *) ch_malloc( sizeof(Filter) );
354         lf->f_next = NULL;
355         lf->f_choice = LDAP_FILTER_AND;
356         lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
357         lf->f_and->f_next = f;
358
359         if ( scope == LDAP_SCOPE_SUBTREE ) {
360                 lf->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
361                 lf->f_and->f_sub_type = ch_strdup( "dn" );
362                 lf->f_and->f_sub_initial = NULL;
363                 lf->f_and->f_sub_any = NULL;
364                 lf->f_and->f_sub_final = ch_strdup( e->e_ndn );
365                 value_normalize( lf->f_and->f_sub_final, SYNTAX_DN|SYNTAX_CIS );
366
367         } else {
368                 char buf[16];
369                 lf->f_and->f_choice = LDAP_FILTER_EQUALITY;
370                 lf->f_and->f_ava.ava_type = ch_strdup( "id2children" );
371                 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
372                 lf->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
373                 lf->f_and->f_ava.ava_value.bv_len = strlen( buf );
374         }
375
376         candidates = filter_candidates( be, lf );
377
378         /* free up filter additions we allocated above */
379         lf->f_and->f_next = NULL;
380         filter_free( lf );
381
382         if( af != NULL ) {
383                 af->f_or->f_next = NULL;
384                 filter_free( af );
385         }
386
387         if( rf != NULL ) {
388                 rf->f_or->f_next = NULL;
389                 filter_free( rf );
390         }
391
392         return( candidates );
393 }