1 /* filterentry.c - apply a filter to an entry */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
18 static int test_filter_and( Operation *op, Entry *e, Filter *flist );
19 static int test_filter_or( Operation *op, Entry *e, Filter *flist );
20 static int test_substrings_filter( Operation *op, Entry *e, Filter *f);
21 static int test_ava_filter( Operation *op, Entry *e, AttributeAssertion *ava, int type );
22 static int test_mra_filter( Operation *op, Entry *e, MatchingRuleAssertion *mra );
23 static int test_presence_filter( Operation *op, Entry *e, AttributeDescription *desc );
27 * test_filter - test a filter against a single entry.
29 * LDAP_COMPARE_TRUE filter matched
30 * LDAP_COMPARE_FALSE filter did not match
31 * SLAPD_COMPARE_UNDEFINED filter is undefined
32 * or an ldap result code indicating error
44 LDAP_LOG( FILTER, ENTRY, "test_filter: begin\n", 0, 0, 0 );
46 Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
49 switch ( f->f_choice ) {
50 case SLAPD_FILTER_COMPUTED:
52 LDAP_LOG( FILTER, DETAIL1,
53 "test_filter: COMPUTED %s (%d)\n",
54 f->f_result == LDAP_COMPARE_FALSE ? "false" :
55 f->f_result == LDAP_COMPARE_TRUE ? "true" :
56 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
57 "error", f->f_result, 0 );
59 Debug( LDAP_DEBUG_FILTER, " COMPUTED %s (%d)\n",
60 f->f_result == LDAP_COMPARE_FALSE ? "false" :
61 f->f_result == LDAP_COMPARE_TRUE ? "true" :
62 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : "error",
69 case LDAP_FILTER_EQUALITY:
71 LDAP_LOG( FILTER, DETAIL1, "test_filter: EQUALITY\n", 0, 0, 0 );
73 Debug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
76 rc = test_ava_filter( op, e, f->f_ava,
77 LDAP_FILTER_EQUALITY );
80 case LDAP_FILTER_SUBSTRINGS:
82 LDAP_LOG( FILTER, DETAIL1, "test_filter SUBSTRINGS\n", 0, 0, 0 );
84 Debug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
87 rc = test_substrings_filter( op, e, f );
91 rc = test_ava_filter( op, e, f->f_ava,
96 rc = test_ava_filter( op, e, f->f_ava,
100 case LDAP_FILTER_PRESENT:
102 LDAP_LOG( FILTER, DETAIL1, "test_filter: PRESENT\n", 0, 0, 0 );
104 Debug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
107 rc = test_presence_filter( op, e, f->f_desc );
110 case LDAP_FILTER_APPROX:
112 LDAP_LOG( FILTER, DETAIL1, "test_filter: APPROX\n", 0, 0, 0 );
114 Debug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
116 rc = test_ava_filter( op, e, f->f_ava,
117 LDAP_FILTER_APPROX );
120 case LDAP_FILTER_AND:
122 LDAP_LOG( FILTER, DETAIL1, "test_filter: AND\n", 0, 0, 0 );
124 Debug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
127 rc = test_filter_and( op, e, f->f_and );
132 LDAP_LOG( FILTER, DETAIL1, "test_filter: OR\n", 0, 0, 0 );
134 Debug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
137 rc = test_filter_or( op, e, f->f_or );
140 case LDAP_FILTER_NOT:
142 LDAP_LOG( FILTER, DETAIL1, "test_filter: NOT\n", 0, 0, 0 );
144 Debug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
147 rc = test_filter( op, e, f->f_not );
149 /* Flip true to false and false to true
150 * but leave Undefined alone.
153 case LDAP_COMPARE_TRUE:
154 rc = LDAP_COMPARE_FALSE;
156 case LDAP_COMPARE_FALSE:
157 rc = LDAP_COMPARE_TRUE;
162 case LDAP_FILTER_EXT:
164 LDAP_LOG( FILTER, DETAIL1, "test_filter: EXT\n", 0, 0, 0 );
166 Debug( LDAP_DEBUG_FILTER, " EXT\n", 0, 0, 0 );
169 rc = test_mra_filter( op, e, f->f_mra );
174 LDAP_LOG( FILTER, INFO,
175 "test_filter: unknown filter type %lu\n", f->f_choice, 0, 0 );
177 Debug( LDAP_DEBUG_ANY, " unknown filter type %lu\n",
181 rc = LDAP_PROTOCOL_ERROR;
185 LDAP_LOG( FILTER, RESULTS, "test_filter: return=%d\n", rc, 0, 0 );
187 Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
193 static int test_mra_filter(
196 MatchingRuleAssertion *mra )
200 if ( mra->ma_desc ) {
202 * if ma_desc is available, then we're filtering for
203 * one attribute, and SEARCH permissions can be checked
206 if( !access_allowed( op, e,
207 mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
209 return LDAP_INSUFFICIENT_ACCESS;
212 for(a = attrs_find( e->e_attrs, mra->ma_desc );
214 a = attrs_find( a->a_next, mra->ma_desc ) )
218 /* If ma_rule is not the same as the attribute's
219 * normal rule, then we can't use the a_nvals.
221 if (mra->ma_rule == a->a_desc->ad_type->sat_equality)
226 for ( ; bv->bv_val != NULL; bv++ )
232 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
233 bv, &mra->ma_value, &text );
235 if( rc != LDAP_SUCCESS ) {
240 return LDAP_COMPARE_TRUE;
247 * No attribute description: test all
249 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
250 struct berval *bv, value;
251 const char *text = NULL;
254 /* check if matching is appropriate */
255 if ( !mr_usable_with_at( mra->ma_rule, a->a_desc->ad_type )) {
259 /* normalize for equality */
261 rc = asserted_value_validate_normalize( a->a_desc, mra->ma_rule,
262 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
263 &mra->ma_value, &value, &text );
265 rc = value_validate_normalize( a->a_desc,
267 &mra->ma_value, &value, &text );
269 if ( rc != LDAP_SUCCESS ) {
273 /* check search access */
274 if ( !access_allowed( op, e,
275 a->a_desc, &value, ACL_SEARCH, NULL ) ) {
281 if (mra->ma_rule == a->a_desc->ad_type->sat_equality)
286 for ( ; bv->bv_val != NULL; bv++ )
291 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
294 if( rc != LDAP_SUCCESS ) {
299 return LDAP_COMPARE_TRUE;
305 /* check attrs in DN AVAs if required */
306 if ( mra->ma_dnattrs ) {
311 /* parse and pretty the dn */
312 rc = dnPrettyDN( NULL, &e->e_name, &dn );
313 if ( rc != LDAP_SUCCESS ) {
314 return LDAP_INVALID_SYNTAX;
317 /* for each AVA of each RDN ... */
318 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
319 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
321 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
322 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
323 struct berval *bv = &ava->la_value, value;
324 AttributeDescription *ad = (AttributeDescription *)ava->la_private;
331 if ( mra->ma_desc ) {
332 /* have a mra type? check for subtype */
333 if ( !is_ad_subtype( ad, mra->ma_desc ) ) {
336 value = mra->ma_value;
339 const char *text = NULL;
341 /* check if matching is appropriate */
342 if ( !mr_usable_with_at( mra->ma_rule, ad->ad_type )) {
346 /* normalize for equality */
348 rc = asserted_value_validate_normalize( ad,
350 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
351 &mra->ma_value, &value, &text );
353 rc = value_validate_normalize( ad, SLAP_MR_EQUALITY,
354 &mra->ma_value, &value, &text );
356 if ( rc != LDAP_SUCCESS ) {
360 /* check search access */
361 if ( !access_allowed( op, e,
362 ad, &value, ACL_SEARCH, NULL ) ) {
368 rc = value_match( &ret, ad, mra->ma_rule, 0,
371 if( rc != LDAP_SUCCESS ) {
378 return LDAP_COMPARE_TRUE;
384 return LDAP_COMPARE_FALSE;
391 AttributeAssertion *ava,
397 if ( !access_allowed( op, e,
398 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
400 return LDAP_INSUFFICIENT_ACCESS;
403 for(a = attrs_find( e->e_attrs, ava->aa_desc );
405 a = attrs_find( a->a_next, ava->aa_desc ) )
411 case LDAP_FILTER_APPROX:
412 mr = a->a_desc->ad_type->sat_approx;
413 if( mr != NULL ) break;
415 /* use EQUALITY matching rule if no APPROX rule */
417 case LDAP_FILTER_EQUALITY:
418 mr = a->a_desc->ad_type->sat_equality;
423 mr = a->a_desc->ad_type->sat_ordering;
435 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ )
437 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ )
444 rc = value_match( &ret, a->a_desc, mr, 0,
445 bv, &ava->aa_value, &text );
447 if( rc != LDAP_SUCCESS ) {
452 case LDAP_FILTER_EQUALITY:
453 case LDAP_FILTER_APPROX:
455 return LDAP_COMPARE_TRUE;
461 return LDAP_COMPARE_TRUE;
467 return LDAP_COMPARE_TRUE;
474 if ( ava->aa_desc == slap_schema.si_ad_hasSubordinates
475 && op->o_bd && op->o_bd->be_has_subordinates ) {
480 * No other match should be allowed ...
482 assert( type == LDAP_FILTER_EQUALITY );
484 if (op->o_bd->be_has_subordinates( op, e, &hasSubordinates ) != LDAP_SUCCESS) {
488 if ( hasSubordinates == LDAP_COMPARE_TRUE ) {
490 hs.bv_len = sizeof( "TRUE" ) - 1;
492 } else if ( hasSubordinates == LDAP_COMPARE_FALSE ) {
494 hs.bv_len = sizeof( "FALSE" ) - 1;
500 if ( bvmatch( &ava->aa_value, &hs ) ) {
501 return LDAP_COMPARE_TRUE;
504 return LDAP_COMPARE_FALSE;
507 return( LDAP_COMPARE_FALSE );
512 test_presence_filter(
515 AttributeDescription *desc
520 if ( !access_allowed( op, e, desc, NULL, ACL_SEARCH, NULL ) )
522 return LDAP_INSUFFICIENT_ACCESS;
525 a = attrs_find( e->e_attrs, desc );
527 if ( a == NULL && desc == slap_schema.si_ad_hasSubordinates ) {
530 * XXX: fairly optimistic: if the function is defined,
531 * then PRESENCE must succeed, because hasSubordinate
532 * is boolean-valued; I think we may live with this
533 * simplification by now
535 if ( op->o_bd && op->o_bd->be_has_subordinates ) {
536 return LDAP_COMPARE_TRUE;
539 return LDAP_COMPARE_FALSE;
542 return a != NULL ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
554 int rtn = LDAP_COMPARE_TRUE; /* True if empty */
557 LDAP_LOG( FILTER, ENTRY, "test_filter_and: begin\n", 0, 0, 0 );
559 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
563 for ( f = flist; f != NULL; f = f->f_next ) {
564 int rc = test_filter( op, e, f );
566 if ( rc == LDAP_COMPARE_FALSE ) {
567 /* filter is False */
572 if ( rc != LDAP_COMPARE_TRUE ) {
573 /* filter is Undefined unless later elements are False */
579 LDAP_LOG( FILTER, RESULTS, "test_filter_and: rc=%d\n", rtn, 0, 0 );
581 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
595 int rtn = LDAP_COMPARE_FALSE; /* False if empty */
598 LDAP_LOG( FILTER, ENTRY, "test_filter_or: begin\n", 0, 0, 0 );
600 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
604 for ( f = flist; f != NULL; f = f->f_next ) {
605 int rc = test_filter( op, e, f );
607 if ( rc == LDAP_COMPARE_TRUE ) {
613 if ( rc != LDAP_COMPARE_FALSE ) {
614 /* filter is Undefined unless later elements are True */
620 LDAP_LOG( FILTER, ENTRY, "test_filter_or: result=%d\n", rtn, 0, 0 );
622 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
630 test_substrings_filter(
639 LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: begin\n", 0, 0, 0 );
641 Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
645 if ( !access_allowed( op, e,
646 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
648 return LDAP_INSUFFICIENT_ACCESS;
651 for(a = attrs_find( e->e_attrs, f->f_sub_desc );
653 a = attrs_find( a->a_next, f->f_sub_desc ) )
655 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
663 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ )
665 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ )
672 rc = value_match( &ret, a->a_desc, mr, 0,
673 bv, f->f_sub, &text );
675 if( rc != LDAP_SUCCESS ) {
680 return LDAP_COMPARE_TRUE;
686 LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: return FALSE\n", 0, 0, 0 );
688 Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n", 0, 0, 0 );
691 return LDAP_COMPARE_FALSE;