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