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