1 /* filterentry.c - apply a filter to an entry */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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,
41 Entry *e, AttributeAssertion *ava, int type );
42 static int test_mra_filter( Operation *op,
43 Entry *e, MatchingRuleAssertion *mra );
44 static int test_presence_filter( Operation *op,
45 Entry *e, AttributeDescription *desc );
49 * test_filter - test a filter against a single entry.
51 * LDAP_COMPARE_TRUE filter matched
52 * LDAP_COMPARE_FALSE filter did not match
53 * SLAPD_COMPARE_UNDEFINED filter is undefined
54 * or an ldap result code indicating error
64 Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
66 switch ( f->f_choice ) {
67 case SLAPD_FILTER_COMPUTED:
68 Debug( LDAP_DEBUG_FILTER, " COMPUTED %s (%d)\n",
69 f->f_result == LDAP_COMPARE_FALSE ? "false" :
70 f->f_result == LDAP_COMPARE_TRUE ? "true" :
71 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : "error",
77 case LDAP_FILTER_EQUALITY:
78 Debug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
79 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_EQUALITY );
82 case LDAP_FILTER_SUBSTRINGS:
83 Debug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
84 rc = test_substrings_filter( op, e, f );
88 Debug( LDAP_DEBUG_FILTER, " GE\n", 0, 0, 0 );
89 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_GE );
93 Debug( LDAP_DEBUG_FILTER, " LE\n", 0, 0, 0 );
94 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_LE );
97 case LDAP_FILTER_PRESENT:
98 Debug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
99 rc = test_presence_filter( op, e, f->f_desc );
102 case LDAP_FILTER_APPROX:
103 Debug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
104 rc = test_ava_filter( op, e, f->f_ava, LDAP_FILTER_APPROX );
107 case LDAP_FILTER_AND:
108 Debug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
109 rc = test_filter_and( op, e, f->f_and );
113 Debug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
114 rc = test_filter_or( op, e, f->f_or );
117 case LDAP_FILTER_NOT:
118 Debug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
119 rc = test_filter( op, e, f->f_not );
121 /* Flip true to false and false to true
122 * but leave Undefined alone.
125 case LDAP_COMPARE_TRUE:
126 rc = LDAP_COMPARE_FALSE;
128 case LDAP_COMPARE_FALSE:
129 rc = LDAP_COMPARE_TRUE;
134 case LDAP_FILTER_EXT:
135 Debug( LDAP_DEBUG_FILTER, " EXT\n", 0, 0, 0 );
136 rc = test_mra_filter( op, e, f->f_mra );
140 Debug( LDAP_DEBUG_ANY, " unknown filter type %lu\n",
142 rc = LDAP_PROTOCOL_ERROR;
145 Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
149 static int test_mra_filter(
152 MatchingRuleAssertion *mra )
156 BER_MEMFREE_FN *memfree;
157 #ifdef LDAP_COMP_MATCH
158 int i, num_attr_vals;
163 memfree = slap_sl_free;
165 memctx = op->o_tmpmemctx;
166 memfree = op->o_tmpfree;
169 if ( mra->ma_desc ) {
171 * if ma_desc is available, then we're filtering for
172 * one attribute, and SEARCH permissions can be checked
175 if( !access_allowed( op, e,
176 mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
178 return LDAP_INSUFFICIENT_ACCESS;
181 if ( mra->ma_desc == slap_schema.si_ad_entryDN ) {
185 rc = value_match( &ret, slap_schema.si_ad_entryDN, mra->ma_rule,
186 0, &e->e_nname, &mra->ma_value, &text );
189 if( rc != LDAP_SUCCESS ) return rc;
190 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
191 return LDAP_COMPARE_FALSE;
194 for(a = attrs_find( e->e_attrs, mra->ma_desc );
196 a = attrs_find( a->a_next, mra->ma_desc ) )
199 #ifdef LDAP_COMP_MATCH
200 /* Component Matching */
202 mra->ma_rule->smr_usage & SLAP_MR_COMPONENT )
206 if ( !a->a_comp_data ) {
207 for ( ; a->a_vals[num_attr_vals].bv_val != NULL; num_attr_vals++ );
208 if ( num_attr_vals <= 0 )/* no attribute value */
209 return LDAP_INAPPROPRIATE_MATCHING;
212 /* following malloced will be freed by comp_tree_free () */
213 a->a_comp_data = malloc( sizeof( ComponentData ) + sizeof( ComponentSyntaxInfo* )*num_attr_vals );
215 if ( !a->a_comp_data )
216 return LDAP_NO_MEMORY;
217 a->a_comp_data->cd_tree = (ComponentSyntaxInfo**)((char*)a->a_comp_data + sizeof(ComponentData));
218 a->a_comp_data->cd_tree[ num_attr_vals - 1] = (ComponentSyntaxInfo*)NULL;
219 a->a_comp_data->cd_mem_op = nibble_mem_allocator ( 1024*16, 1024 );
224 /* If ma_rule is not the same as the attribute's
225 * normal rule, then we can't use the a_nvals.
227 if (mra->ma_rule == a->a_desc->ad_type->sat_equality) {
232 #ifdef LDAP_COMP_MATCH
235 for ( ; bv->bv_val != NULL; bv++ ) {
240 #ifdef LDAP_COMP_MATCH
242 mra->ma_rule->smr_usage & SLAP_MR_COMPONENT ) {
243 /* Check if decoded component trees are already linked */
245 a->a_comp_data->cd_tree[i] = attr_converter (a, a->a_desc->ad_type->sat_syntax, bv);
247 if ( !a->a_comp_data->cd_tree[i] )
248 return LDAP_OPERATIONS_ERROR;
249 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
250 (struct berval*)a->a_comp_data->cd_tree[i++], (void*)mra, &text );
254 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
255 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 );
289 #ifdef LDAP_COMP_MATCH
290 /* Component Matching */
292 mra->ma_rule->smr_usage & SLAP_MR_COMPONENT)
296 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
297 (struct berval*)a, (void*)mra, &text );
298 if( rc != LDAP_SUCCESS ) break;
301 rc = LDAP_COMPARE_TRUE;
309 if (mra->ma_rule == a->a_desc->ad_type->sat_equality) {
315 for ( ; bv->bv_val != NULL; bv++ ) {
318 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
321 if( rc != LDAP_SUCCESS ) break;
324 rc = LDAP_COMPARE_TRUE;
328 memfree( value.bv_val, memctx );
329 if ( rc != LDAP_SUCCESS ) return rc;
333 /* check attrs in DN AVAs if required */
334 if ( mra->ma_dnattrs ) {
339 /* parse and pretty the dn */
340 rc = dnPrettyDN( NULL, &e->e_name, &dn, memctx );
341 if ( rc != LDAP_SUCCESS ) {
342 return LDAP_INVALID_SYNTAX;
345 /* for each AVA of each RDN ... */
346 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
347 LDAPRDN rdn = dn[ iRDN ];
349 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
350 LDAPAVA *ava = rdn[ iAVA ];
351 struct berval *bv = &ava->la_value, value;
352 AttributeDescription *ad =
353 (AttributeDescription *)ava->la_private;
359 if ( mra->ma_desc ) {
360 /* have a mra type? check for subtype */
361 if ( !is_ad_subtype( ad, mra->ma_desc ) ) {
364 value = mra->ma_value;
367 const char *text = NULL;
369 /* check if matching is appropriate */
370 if ( !mr_usable_with_at( mra->ma_rule, ad->ad_type )) {
374 /* normalize for equality */
375 rc = asserted_value_validate_normalize( ad,
377 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
378 &mra->ma_value, &value, &text, memctx );
379 if ( rc != LDAP_SUCCESS ) continue;
381 /* check search access */
382 if ( !access_allowed( op, e,
383 ad, &value, ACL_SEARCH, NULL ) )
385 memfree( value.bv_val, memctx );
391 rc = value_match( &ret, ad, mra->ma_rule, 0,
393 if ( value.bv_val != mra->ma_value.bv_val ) {
394 memfree( value.bv_val, memctx );
397 if ( rc == LDAP_SUCCESS && ret == 0 ) rc = LDAP_COMPARE_TRUE;
399 if( rc != LDAP_SUCCESS ) {
400 ldap_dnfree_x( dn, memctx );
405 ldap_dnfree_x( dn, memctx );
408 return LDAP_COMPARE_FALSE;
415 AttributeAssertion *ava,
421 if ( !access_allowed( op, e,
422 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
424 return LDAP_INSUFFICIENT_ACCESS;
427 if ( ava->aa_desc == slap_schema.si_ad_hasSubordinates
428 && op && op->o_bd && op->o_bd->be_has_subordinates )
433 if( type != LDAP_FILTER_EQUALITY &&
434 type != LDAP_FILTER_APPROX )
436 /* No other match is allowed */
437 return LDAP_INAPPROPRIATE_MATCHING;
440 if ( op->o_bd->be_has_subordinates( op, e, &hasSubordinates ) !=
446 if ( hasSubordinates == LDAP_COMPARE_TRUE ) {
449 } else if ( hasSubordinates == LDAP_COMPARE_FALSE ) {
456 if ( bvmatch( &ava->aa_value, &hs ) ) return LDAP_COMPARE_TRUE;
457 return LDAP_COMPARE_FALSE;
460 if ( ava->aa_desc == slap_schema.si_ad_entryDN ) {
465 if( type != LDAP_FILTER_EQUALITY &&
466 type != LDAP_FILTER_APPROX )
468 /* No other match is allowed */
469 return LDAP_INAPPROPRIATE_MATCHING;
472 mr = slap_schema.si_ad_entryDN->ad_type->sat_equality;
475 rc = value_match( &match, slap_schema.si_ad_entryDN, mr, 0,
476 &e->e_nname, &ava->aa_value, &text );
478 if( rc != LDAP_SUCCESS ) return rc;
479 if( match == 0 ) return LDAP_COMPARE_TRUE;
480 return LDAP_COMPARE_FALSE;
483 rc = LDAP_COMPARE_FALSE;
485 for(a = attrs_find( e->e_attrs, ava->aa_desc );
487 a = attrs_find( a->a_next, ava->aa_desc ) )
492 if (( ava->aa_desc != a->a_desc ) && !access_allowed( op,
493 e, a->a_desc, &ava->aa_value, ACL_SEARCH, NULL ))
495 rc = LDAP_INSUFFICIENT_ACCESS;
500 case LDAP_FILTER_APPROX:
501 mr = a->a_desc->ad_type->sat_approx;
502 if( mr != NULL ) break;
504 /* use EQUALITY matching rule if no APPROX rule */
506 case LDAP_FILTER_EQUALITY:
507 mr = a->a_desc->ad_type->sat_equality;
512 mr = a->a_desc->ad_type->sat_ordering;
520 rc = LDAP_INAPPROPRIATE_MATCHING;
524 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
528 ret = value_match( &match, a->a_desc, mr, 0,
529 bv, &ava->aa_value, &text );
531 if( ret != LDAP_SUCCESS ) {
537 case LDAP_FILTER_EQUALITY:
538 case LDAP_FILTER_APPROX:
539 if ( match == 0 ) return LDAP_COMPARE_TRUE;
543 if ( match >= 0 ) return LDAP_COMPARE_TRUE;
547 if ( match <= 0 ) return LDAP_COMPARE_TRUE;
558 test_presence_filter(
561 AttributeDescription *desc )
566 if ( !access_allowed( op, e, desc, NULL, ACL_SEARCH, NULL ) ) {
567 return LDAP_INSUFFICIENT_ACCESS;
570 if ( desc == slap_schema.si_ad_hasSubordinates ) {
572 * XXX: fairly optimistic: if the function is defined,
573 * then PRESENCE must succeed, because hasSubordinate
574 * is boolean-valued; I think we may live with this
575 * simplification by now
577 if ( op && op->o_bd && op->o_bd->be_has_subordinates ) {
578 return LDAP_COMPARE_TRUE;
581 return LDAP_COMPARE_FALSE;
584 if ( desc == slap_schema.si_ad_entryDN ||
585 desc == slap_schema.si_ad_subschemaSubentry )
587 /* entryDN and subschemaSubentry are always present */
588 return LDAP_COMPARE_TRUE;
591 rc = LDAP_COMPARE_FALSE;
593 for(a = attrs_find( e->e_attrs, desc );
595 a = attrs_find( a->a_next, desc ) )
597 if (( desc != a->a_desc ) && !access_allowed( op,
598 e, a->a_desc, NULL, ACL_SEARCH, NULL ))
600 rc = LDAP_INSUFFICIENT_ACCESS;
604 rc = LDAP_COMPARE_TRUE;
619 int rtn = LDAP_COMPARE_TRUE; /* True if empty */
621 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
623 for ( f = flist; f != NULL; f = f->f_next ) {
624 int rc = test_filter( op, e, f );
626 if ( rc == LDAP_COMPARE_FALSE ) {
627 /* filter is False */
632 if ( rc != LDAP_COMPARE_TRUE ) {
633 /* filter is Undefined unless later elements are False */
638 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
650 int rtn = LDAP_COMPARE_FALSE; /* False if empty */
652 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
654 for ( f = flist; f != NULL; f = f->f_next ) {
655 int rc = test_filter( op, e, f );
657 if ( rc == LDAP_COMPARE_TRUE ) {
663 if ( rc != LDAP_COMPARE_FALSE ) {
664 /* filter is Undefined unless later elements are True */
669 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
675 test_substrings_filter(
683 Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
685 if ( !access_allowed( op, e,
686 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
688 return LDAP_INSUFFICIENT_ACCESS;
691 rc = LDAP_COMPARE_FALSE;
693 for(a = attrs_find( e->e_attrs, f->f_sub_desc );
695 a = attrs_find( a->a_next, f->f_sub_desc ) )
700 if (( f->f_sub_desc != a->a_desc ) && !access_allowed( op,
701 e, a->a_desc, NULL, ACL_SEARCH, NULL ))
703 rc = LDAP_INSUFFICIENT_ACCESS;
707 mr = a->a_desc->ad_type->sat_substr;
709 rc = LDAP_INAPPROPRIATE_MATCHING;
713 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
717 ret = value_match( &match, a->a_desc, mr, 0,
718 bv, f->f_sub, &text );
720 if( ret != LDAP_SUCCESS ) {
724 if ( match == 0 ) return LDAP_COMPARE_TRUE;
728 Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter %d\n",