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,
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;
160 memfree = slap_sl_free;
162 memctx = op->o_tmpmemctx;
163 memfree = op->o_tmpfree;
166 #ifdef LDAP_COMP_MATCH
167 /* Component Matching */
169 mra->ma_rule->smr_usage & (SLAP_MR_COMPONENT) ){
170 return test_comp_filter_entry( op, e, mra );
173 if ( mra->ma_desc ) {
175 * if ma_desc is available, then we're filtering for
176 * one attribute, and SEARCH permissions can be checked
179 if( !access_allowed( op, e,
180 mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
182 return LDAP_INSUFFICIENT_ACCESS;
185 for(a = attrs_find( e->e_attrs, mra->ma_desc );
187 a = attrs_find( a->a_next, mra->ma_desc ) )
190 /* If ma_rule is not the same as the attribute's
191 * normal rule, then we can't use the a_nvals.
193 if (mra->ma_rule == a->a_desc->ad_type->sat_equality) {
199 for ( ; bv->bv_val != NULL; bv++ ) {
204 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
205 bv, &mra->ma_value, &text );
207 if( rc != LDAP_SUCCESS ) return rc;
208 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
214 * No attribute description: test all
216 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
217 struct berval *bv, value;
218 const char *text = NULL;
221 /* check if matching is appropriate */
222 if ( !mr_usable_with_at( mra->ma_rule, a->a_desc->ad_type )) {
226 /* normalize for equality */
227 rc = asserted_value_validate_normalize( a->a_desc, mra->ma_rule,
228 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
229 &mra->ma_value, &value, &text, memctx );
230 if ( rc != LDAP_SUCCESS ) continue;
232 /* check search access */
233 if ( !access_allowed( op, e,
234 a->a_desc, &value, ACL_SEARCH, NULL ) ) {
235 memfree( value.bv_val, memctx );
240 if (mra->ma_rule == a->a_desc->ad_type->sat_equality) {
246 for ( ; bv->bv_val != NULL; bv++ ) {
249 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
252 if( rc != LDAP_SUCCESS ) break;
255 rc = LDAP_COMPARE_TRUE;
259 memfree( value.bv_val, memctx );
260 if ( rc != LDAP_SUCCESS ) return rc;
264 /* check attrs in DN AVAs if required */
265 if ( mra->ma_dnattrs ) {
270 /* parse and pretty the dn */
271 rc = dnPrettyDN( NULL, &e->e_name, &dn, memctx );
272 if ( rc != LDAP_SUCCESS ) {
273 return LDAP_INVALID_SYNTAX;
276 /* for each AVA of each RDN ... */
277 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
278 LDAPRDN rdn = dn[ iRDN ];
280 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
281 LDAPAVA *ava = rdn[ iAVA ];
282 struct berval *bv = &ava->la_value, value;
283 AttributeDescription *ad =
284 (AttributeDescription *)ava->la_private;
290 if ( mra->ma_desc ) {
291 /* have a mra type? check for subtype */
292 if ( !is_ad_subtype( ad, mra->ma_desc ) ) {
295 value = mra->ma_value;
298 const char *text = NULL;
300 /* check if matching is appropriate */
301 if ( !mr_usable_with_at( mra->ma_rule, ad->ad_type )) {
305 /* normalize for equality */
306 rc = asserted_value_validate_normalize( ad,
308 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
309 &mra->ma_value, &value, &text, memctx );
310 if ( rc != LDAP_SUCCESS ) continue;
312 /* check search access */
313 if ( !access_allowed( op, e,
314 ad, &value, ACL_SEARCH, NULL ) )
316 memfree( value.bv_val, memctx );
322 rc = value_match( &ret, ad, mra->ma_rule, 0,
325 if ( value.bv_val != mra->ma_value.bv_val ) {
326 memfree( value.bv_val, memctx );
329 if ( rc == LDAP_SUCCESS && ret == 0 ) rc = LDAP_COMPARE_TRUE;
331 if( rc != LDAP_SUCCESS ) {
332 ldap_dnfree_x( dn, memctx );
337 ldap_dnfree_x( dn, memctx );
340 return LDAP_COMPARE_FALSE;
347 AttributeAssertion *ava,
352 if ( !access_allowed( op, e,
353 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
355 return LDAP_INSUFFICIENT_ACCESS;
358 if ( ava->aa_desc == slap_schema.si_ad_hasSubordinates
359 && op && op->o_bd && op->o_bd->be_has_subordinates )
364 if( type != LDAP_FILTER_EQUALITY &&
365 type != LDAP_FILTER_APPROX )
367 /* No other match is allowed */
371 if ( op->o_bd->be_has_subordinates( op, e, &hasSubordinates ) !=
377 if ( hasSubordinates == LDAP_COMPARE_TRUE ) {
380 } else if ( hasSubordinates == LDAP_COMPARE_FALSE ) {
387 if ( bvmatch( &ava->aa_value, &hs ) ) return LDAP_COMPARE_TRUE;
388 return LDAP_COMPARE_FALSE;
392 for(a = attrs_find( e->e_attrs, ava->aa_desc );
394 a = attrs_find( a->a_next, ava->aa_desc ) )
400 case LDAP_FILTER_APPROX:
401 mr = a->a_desc->ad_type->sat_approx;
402 if( mr != NULL ) break;
404 /* use EQUALITY matching rule if no APPROX rule */
406 case LDAP_FILTER_EQUALITY:
407 mr = a->a_desc->ad_type->sat_equality;
412 mr = a->a_desc->ad_type->sat_ordering;
419 if( mr == NULL ) continue;
421 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
426 rc = value_match( &ret, a->a_desc, mr, 0,
427 bv, &ava->aa_value, &text );
429 if( rc != LDAP_SUCCESS ) return rc;
432 case LDAP_FILTER_EQUALITY:
433 case LDAP_FILTER_APPROX:
434 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
438 if ( ret >= 0 ) return LDAP_COMPARE_TRUE;
442 if ( ret <= 0 ) return LDAP_COMPARE_TRUE;
448 return LDAP_COMPARE_FALSE;
453 test_presence_filter(
456 AttributeDescription *desc )
460 if ( !access_allowed( op, e, desc, NULL, ACL_SEARCH, NULL ) ) {
461 return LDAP_INSUFFICIENT_ACCESS;
464 a = attrs_find( e->e_attrs, desc );
465 if ( a == NULL && desc == slap_schema.si_ad_hasSubordinates ) {
468 * XXX: fairly optimistic: if the function is defined,
469 * then PRESENCE must succeed, because hasSubordinate
470 * is boolean-valued; I think we may live with this
471 * simplification by now
473 if ( op && op->o_bd && op->o_bd->be_has_subordinates ) {
474 return LDAP_COMPARE_TRUE;
477 return LDAP_COMPARE_FALSE;
480 return a != NULL ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
491 int rtn = LDAP_COMPARE_TRUE; /* True if empty */
493 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
496 for ( f = flist; f != NULL; f = f->f_next ) {
497 int rc = test_filter( op, e, f );
499 if ( rc == LDAP_COMPARE_FALSE ) {
500 /* filter is False */
505 if ( rc != LDAP_COMPARE_TRUE ) {
506 /* filter is Undefined unless later elements are False */
511 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
523 int rtn = LDAP_COMPARE_FALSE; /* False if empty */
525 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
527 for ( f = flist; f != NULL; f = f->f_next ) {
528 int rc = test_filter( op, e, f );
530 if ( rc == LDAP_COMPARE_TRUE ) {
536 if ( rc != LDAP_COMPARE_FALSE ) {
537 /* filter is Undefined unless later elements are True */
542 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
548 test_substrings_filter(
555 Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
557 if ( !access_allowed( op, e,
558 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
560 return LDAP_INSUFFICIENT_ACCESS;
563 for(a = attrs_find( e->e_attrs, f->f_sub_desc );
565 a = attrs_find( a->a_next, f->f_sub_desc ) )
567 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
570 if( mr == NULL ) continue;
572 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ ) {
577 rc = value_match( &ret, a->a_desc, mr, 0,
578 bv, f->f_sub, &text );
580 if( rc != LDAP_SUCCESS ) return rc;
581 if ( ret == 0 ) return LDAP_COMPARE_TRUE;
585 Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n",
587 return LDAP_COMPARE_FALSE;