1 /* filterentry.c - apply a filter to an entry */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17 * All rights reserved.
19 * Redistribution and use in source and binary forms are permitted
20 * provided that this notice is preserved and that due credit is given
21 * to the University of Michigan at Ann Arbor. The name of the University
22 * may not be used to endorse or promote products derived from this
23 * software without specific prior written permission. This software
24 * is provided ``as is'' without express or implied warranty.
31 #include <ac/socket.h>
32 #include <ac/string.h>
37 static int test_filter_and( Operation *op, Entry *e, Filter *flist );
38 static int test_filter_or( Operation *op, Entry *e, Filter *flist );
39 static int test_substrings_filter( Operation *op, Entry *e, Filter *f);
40 static int test_ava_filter( Operation *op, Entry *e, AttributeAssertion *ava, int type );
41 static int test_mra_filter( Operation *op, Entry *e, MatchingRuleAssertion *mra );
42 static int test_presence_filter( Operation *op, Entry *e, AttributeDescription *desc );
46 * test_filter - test a filter against a single entry.
48 * LDAP_COMPARE_TRUE filter matched
49 * LDAP_COMPARE_FALSE filter did not match
50 * SLAPD_COMPARE_UNDEFINED filter is undefined
51 * or an ldap result code indicating error
62 LDAP_LOG( FILTER, ENTRY, "test_filter: begin\n", 0, 0, 0 );
64 Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
67 switch ( f->f_choice ) {
68 case SLAPD_FILTER_COMPUTED:
70 LDAP_LOG( FILTER, DETAIL1,
71 "test_filter: COMPUTED %s (%d)\n",
72 f->f_result == LDAP_COMPARE_FALSE ? "false" :
73 f->f_result == LDAP_COMPARE_TRUE ? "true" :
74 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
75 "error", f->f_result, 0 );
77 Debug( LDAP_DEBUG_FILTER, " COMPUTED %s (%d)\n",
78 f->f_result == LDAP_COMPARE_FALSE ? "false" :
79 f->f_result == LDAP_COMPARE_TRUE ? "true" :
80 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : "error",
87 case LDAP_FILTER_EQUALITY:
89 LDAP_LOG( FILTER, DETAIL1, "test_filter: EQUALITY\n", 0, 0, 0 );
91 Debug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
94 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_EQUALITY );
97 case LDAP_FILTER_SUBSTRINGS:
99 LDAP_LOG( FILTER, DETAIL1, "test_filter SUBSTRINGS\n", 0, 0, 0 );
101 Debug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
104 rc = test_substrings_filter( op, e, f );
108 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_GE );
112 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_LE );
115 case LDAP_FILTER_PRESENT:
117 LDAP_LOG( FILTER, DETAIL1, "test_filter: PRESENT\n", 0, 0, 0 );
119 Debug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
121 rc = test_presence_filter( op, e, f->f_desc );
124 case LDAP_FILTER_APPROX:
126 LDAP_LOG( FILTER, DETAIL1, "test_filter: APPROX\n", 0, 0, 0 );
128 Debug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
130 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_APPROX );
133 case LDAP_FILTER_AND:
135 LDAP_LOG( FILTER, DETAIL1, "test_filter: AND\n", 0, 0, 0 );
137 Debug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
139 rc = test_filter_and( op, e, f->f_and );
144 LDAP_LOG( FILTER, DETAIL1, "test_filter: OR\n", 0, 0, 0 );
146 Debug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
148 rc = test_filter_or( op, e, f->f_or );
151 case LDAP_FILTER_NOT:
153 LDAP_LOG( FILTER, DETAIL1, "test_filter: NOT\n", 0, 0, 0 );
155 Debug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
157 rc = test_filter( op, e, f->f_not );
159 /* Flip true to false and false to true
160 * but leave Undefined alone.
163 case LDAP_COMPARE_TRUE:
164 rc = LDAP_COMPARE_FALSE;
166 case LDAP_COMPARE_FALSE:
167 rc = LDAP_COMPARE_TRUE;
172 case LDAP_FILTER_EXT:
174 LDAP_LOG( FILTER, DETAIL1, "test_filter: EXT\n", 0, 0, 0 );
176 Debug( LDAP_DEBUG_FILTER, " EXT\n", 0, 0, 0 );
178 rc = test_mra_filter( op, e, f->f_mra );
183 LDAP_LOG( FILTER, INFO,
184 "test_filter: unknown filter type %lu\n", f->f_choice, 0, 0 );
186 Debug( LDAP_DEBUG_ANY, " unknown filter type %lu\n",
189 rc = LDAP_PROTOCOL_ERROR;
193 LDAP_LOG( FILTER, RESULTS, "test_filter: return=%d\n", rc, 0, 0 );
195 Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
200 static int test_mra_filter(
203 MatchingRuleAssertion *mra )
207 BER_MEMFREE_FN *memfree;
211 memfree = slap_sl_free;
213 memctx = op->o_tmpmemctx;
214 memfree = op->o_tmpfree;
217 #ifdef LDAP_COMP_MATCH
218 /* Component Matching */
220 mra->ma_rule->smr_usage & (SLAP_MR_COMPONENT) ){
221 return test_comp_filter_entry( op, e, mra );
224 if ( mra->ma_desc ) {
226 * if ma_desc is available, then we're filtering for
227 * one attribute, and SEARCH permissions can be checked
230 if( !access_allowed( op, e,
231 mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
233 return LDAP_INSUFFICIENT_ACCESS;
236 for(a = attrs_find( e->e_attrs, mra->ma_desc );
238 a = attrs_find( a->a_next, mra->ma_desc ) )
241 /* If ma_rule is not the same as the attribute's
242 * normal rule, then we can't use the a_nvals.
244 if (mra->ma_rule == a->a_desc->ad_type->sat_equality) {
250 for ( ; bv->bv_val != NULL; bv++ ) {
255 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
256 bv, &mra->ma_value, &text );
258 if( rc != LDAP_SUCCESS ) return rc;
259 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
265 * No attribute description: test all
267 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
268 struct berval *bv, value;
269 const char *text = NULL;
272 /* check if matching is appropriate */
273 if ( !mr_usable_with_at( mra->ma_rule, a->a_desc->ad_type )) {
277 /* normalize for equality */
278 rc = asserted_value_validate_normalize( a->a_desc, mra->ma_rule,
279 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
280 &mra->ma_value, &value, &text, memctx );
281 if ( rc != LDAP_SUCCESS ) continue;
283 /* check search access */
284 if ( !access_allowed( op, e,
285 a->a_desc, &value, ACL_SEARCH, NULL ) ) {
286 memfree( value.bv_val, memctx );
291 if (mra->ma_rule == a->a_desc->ad_type->sat_equality) {
297 for ( ; bv->bv_val != NULL; bv++ ) {
300 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
303 if( rc != LDAP_SUCCESS ) break;
306 rc = LDAP_COMPARE_TRUE;
310 memfree( value.bv_val, memctx );
311 if ( rc != LDAP_SUCCESS ) return rc;
315 /* check attrs in DN AVAs if required */
316 if ( mra->ma_dnattrs ) {
321 /* parse and pretty the dn */
322 rc = dnPrettyDN( NULL, &e->e_name, &dn, memctx );
323 if ( rc != LDAP_SUCCESS ) {
324 return LDAP_INVALID_SYNTAX;
327 /* for each AVA of each RDN ... */
328 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
329 LDAPRDN rdn = dn[ iRDN ];
331 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
332 LDAPAVA *ava = rdn[ iAVA ];
333 struct berval *bv = &ava->la_value, value;
334 AttributeDescription *ad =
335 (AttributeDescription *)ava->la_private;
341 if ( mra->ma_desc ) {
342 /* have a mra type? check for subtype */
343 if ( !is_ad_subtype( ad, mra->ma_desc ) ) {
346 value = mra->ma_value;
349 const char *text = NULL;
351 /* check if matching is appropriate */
352 if ( !mr_usable_with_at( mra->ma_rule, ad->ad_type )) {
356 /* normalize for equality */
357 rc = asserted_value_validate_normalize( ad,
359 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
360 &mra->ma_value, &value, &text, memctx );
361 if ( rc != LDAP_SUCCESS ) continue;
363 /* check search access */
364 if ( !access_allowed( op, e,
365 ad, &value, ACL_SEARCH, NULL ) )
367 memfree( value.bv_val, memctx );
373 rc = value_match( &ret, ad, mra->ma_rule, 0,
376 if ( value.bv_val != mra->ma_value.bv_val ) {
377 memfree( value.bv_val, memctx );
380 if ( rc == LDAP_SUCCESS && ret == 0 ) rc = LDAP_COMPARE_TRUE;
382 if( rc != LDAP_SUCCESS ) {
383 ldap_dnfree_x( dn, memctx );
388 ldap_dnfree_x( dn, memctx );
391 return LDAP_COMPARE_FALSE;
398 AttributeAssertion *ava,
403 if ( !access_allowed( op, e,
404 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
406 return LDAP_INSUFFICIENT_ACCESS;
409 for(a = attrs_find( e->e_attrs, ava->aa_desc );
411 a = attrs_find( a->a_next, ava->aa_desc ) )
417 case LDAP_FILTER_APPROX:
418 mr = a->a_desc->ad_type->sat_approx;
419 if( mr != NULL ) break;
421 /* use EQUALITY matching rule if no APPROX rule */
423 case LDAP_FILTER_EQUALITY:
424 mr = a->a_desc->ad_type->sat_equality;
429 mr = a->a_desc->ad_type->sat_ordering;
440 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
445 rc = value_match( &ret, a->a_desc, mr, 0,
446 bv, &ava->aa_value, &text );
448 if( rc != LDAP_SUCCESS ) return rc;
451 case LDAP_FILTER_EQUALITY:
452 case LDAP_FILTER_APPROX:
453 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
457 if ( ret >= 0 ) return LDAP_COMPARE_TRUE;
461 if ( ret <= 0 ) return LDAP_COMPARE_TRUE;
467 if ( ava->aa_desc == slap_schema.si_ad_hasSubordinates
468 && op && op->o_bd && op->o_bd->be_has_subordinates )
474 * No other match should be allowed ...
476 assert( type == LDAP_FILTER_EQUALITY );
478 if ( op->o_bd->be_has_subordinates( op, e, &hasSubordinates ) !=
484 if ( hasSubordinates == LDAP_COMPARE_TRUE ) {
487 } else if ( hasSubordinates == LDAP_COMPARE_FALSE ) {
494 if ( bvmatch( &ava->aa_value, &hs ) ) return LDAP_COMPARE_TRUE;
495 return LDAP_COMPARE_FALSE;
498 return( LDAP_COMPARE_FALSE );
503 test_presence_filter(
506 AttributeDescription *desc )
510 if ( !access_allowed( op, e, desc, NULL, ACL_SEARCH, NULL ) ) {
511 return LDAP_INSUFFICIENT_ACCESS;
514 a = attrs_find( e->e_attrs, desc );
515 if ( a == NULL && desc == slap_schema.si_ad_hasSubordinates ) {
518 * XXX: fairly optimistic: if the function is defined,
519 * then PRESENCE must succeed, because hasSubordinate
520 * is boolean-valued; I think we may live with this
521 * simplification by now
523 if ( op && op->o_bd && op->o_bd->be_has_subordinates ) {
524 return LDAP_COMPARE_TRUE;
527 return LDAP_COMPARE_FALSE;
530 return a != NULL ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
541 int rtn = LDAP_COMPARE_TRUE; /* True if empty */
544 LDAP_LOG( FILTER, ENTRY, "test_filter_and: begin\n", 0, 0, 0 );
546 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
550 for ( f = flist; f != NULL; f = f->f_next ) {
551 int rc = test_filter( op, e, f );
553 if ( rc == LDAP_COMPARE_FALSE ) {
554 /* filter is False */
559 if ( rc != LDAP_COMPARE_TRUE ) {
560 /* filter is Undefined unless later elements are False */
566 LDAP_LOG( FILTER, RESULTS, "test_filter_and: rc=%d\n", rtn, 0, 0 );
568 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
582 int rtn = LDAP_COMPARE_FALSE; /* False if empty */
585 LDAP_LOG( FILTER, ENTRY, "test_filter_or: begin\n", 0, 0, 0 );
587 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
590 for ( f = flist; f != NULL; f = f->f_next ) {
591 int rc = test_filter( op, e, f );
593 if ( rc == LDAP_COMPARE_TRUE ) {
599 if ( rc != LDAP_COMPARE_FALSE ) {
600 /* filter is Undefined unless later elements are True */
606 LDAP_LOG( FILTER, ENTRY, "test_filter_or: result=%d\n", rtn, 0, 0 );
608 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
615 test_substrings_filter(
623 LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: begin\n", 0, 0, 0 );
625 Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
628 if ( !access_allowed( op, e,
629 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
631 return LDAP_INSUFFICIENT_ACCESS;
634 for(a = attrs_find( e->e_attrs, f->f_sub_desc );
636 a = attrs_find( a->a_next, f->f_sub_desc ) )
638 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
641 if( mr == NULL ) continue;
643 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
648 rc = value_match( &ret, a->a_desc, mr, 0,
649 bv, f->f_sub, &text );
651 if( rc != LDAP_SUCCESS ) return rc;
652 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
657 LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: return FALSE\n",
660 Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n",
663 return LDAP_COMPARE_FALSE;