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( Backend *be,
19 Connection *conn, Operation *op,
20 Entry *e, Filter *flist );
21 static int test_filter_or( Backend *be,
22 Connection *conn, Operation *op,
23 Entry *e, Filter *flist );
24 static int test_substrings_filter( Backend *be,
25 Connection *conn, Operation *op,
27 static int test_ava_filter( Backend *be,
28 Connection *conn, Operation *op,
29 Entry *e, AttributeAssertion *ava, int type );
30 static int test_mra_filter( Backend *be,
31 Connection *conn, Operation *op,
32 Entry *e, MatchingRuleAssertion *mra );
33 static int test_presence_filter( Backend *be,
34 Connection *conn, Operation *op,
35 Entry *e, AttributeDescription *desc );
39 * test_filter - test a filter against a single entry.
41 * LDAP_COMPARE_TRUE filter matched
42 * LDAP_COMPARE_FALSE filter did not match
43 * SLAPD_COMPARE_UNDEFINED filter is undefined
44 * or an ldap result code indicating error
59 LDAP_LOG( FILTER, ENTRY, "test_filter: begin\n", 0, 0, 0 );
61 Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
65 switch ( f->f_choice ) {
66 case SLAPD_FILTER_COMPUTED:
68 LDAP_LOG( FILTER, DETAIL1,
69 "test_filter: COMPUTED %s (%d)\n",
70 f->f_result == LDAP_COMPARE_FALSE ? "false" :
71 f->f_result == LDAP_COMPARE_TRUE ? "true" :
72 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
73 "error", f->f_result, 0 );
75 Debug( LDAP_DEBUG_FILTER, " COMPUTED %s (%d)\n",
76 f->f_result == LDAP_COMPARE_FALSE ? "false" :
77 f->f_result == LDAP_COMPARE_TRUE ? "true" :
78 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : "error",
85 case LDAP_FILTER_EQUALITY:
87 LDAP_LOG( FILTER, DETAIL1, "test_filter: EQUALITY\n", 0, 0, 0 );
89 Debug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
92 rc = test_ava_filter( be, conn, op, e, f->f_ava,
93 LDAP_FILTER_EQUALITY );
96 case LDAP_FILTER_SUBSTRINGS:
98 LDAP_LOG( FILTER, DETAIL1, "test_filter SUBSTRINGS\n", 0, 0, 0 );
100 Debug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
103 rc = test_substrings_filter( be, conn, op, e, f );
107 rc = test_ava_filter( be, conn, op, e, f->f_ava,
112 rc = test_ava_filter( be, conn, op, e, f->f_ava,
116 case LDAP_FILTER_PRESENT:
118 LDAP_LOG( FILTER, DETAIL1, "test_filter: PRESENT\n", 0, 0, 0 );
120 Debug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
123 rc = test_presence_filter( be, conn, op, e, f->f_desc );
126 case LDAP_FILTER_APPROX:
128 LDAP_LOG( FILTER, DETAIL1, "test_filter: APPROX\n", 0, 0, 0 );
130 Debug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
132 rc = test_ava_filter( be, conn, op, e, f->f_ava,
133 LDAP_FILTER_APPROX );
136 case LDAP_FILTER_AND:
138 LDAP_LOG( FILTER, DETAIL1, "test_filter: AND\n", 0, 0, 0 );
140 Debug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
143 rc = test_filter_and( be, conn, op, e, f->f_and );
148 LDAP_LOG( FILTER, DETAIL1, "test_filter: OR\n", 0, 0, 0 );
150 Debug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
153 rc = test_filter_or( be, conn, op, e, f->f_or );
156 case LDAP_FILTER_NOT:
158 LDAP_LOG( FILTER, DETAIL1, "test_filter: NOT\n", 0, 0, 0 );
160 Debug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
163 rc = test_filter( be, conn, op, e, f->f_not );
165 /* Flip true to false and false to true
166 * but leave Undefined alone.
169 case LDAP_COMPARE_TRUE:
170 rc = LDAP_COMPARE_FALSE;
172 case LDAP_COMPARE_FALSE:
173 rc = LDAP_COMPARE_TRUE;
178 case LDAP_FILTER_EXT:
180 LDAP_LOG( FILTER, DETAIL1, "test_filter: EXT\n", 0, 0, 0 );
182 Debug( LDAP_DEBUG_FILTER, " EXT\n", 0, 0, 0 );
185 rc = test_mra_filter( be, conn, op, e, f->f_mra );
190 LDAP_LOG( FILTER, INFO,
191 "test_filter: unknown filter type %lu\n", f->f_choice, 0, 0 );
193 Debug( LDAP_DEBUG_ANY, " unknown filter type %lu\n",
197 rc = LDAP_PROTOCOL_ERROR;
201 LDAP_LOG( FILTER, RESULTS, "test_filter: return=%d\n", rc, 0, 0 );
203 Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
209 static int test_mra_filter(
214 MatchingRuleAssertion *mra )
218 if ( mra->ma_desc ) {
220 * if ma_desc is available, then we're filtering for
221 * one attribute, and SEARCH permissions can be checked
224 if( !access_allowed( be, conn, op, e,
225 mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
227 return LDAP_INSUFFICIENT_ACCESS;
230 for(a = attrs_find( e->e_attrs, mra->ma_desc );
232 a = attrs_find( a->a_next, mra->ma_desc ) )
240 for ( ; bv->bv_val != NULL; bv++ )
246 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
247 bv, &mra->ma_value, &text );
249 if( rc != LDAP_SUCCESS ) {
254 return LDAP_COMPARE_TRUE;
261 * No attribute description: test all
263 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
264 struct berval *bv, value;
265 const char *text = NULL;
268 /* check if matching is appropriate */
269 if ( !mr_usable_with_at( mra->ma_rule, a->a_desc->ad_type )) {
273 /* normalize for equality */
275 rc = asserted_value_validate_normalize( a->a_desc, mra->ma_rule,
276 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
277 &mra->ma_value, &value, &text );
279 rc = value_validate_normalize( a->a_desc,
281 &mra->ma_value, &value, &text );
283 if ( rc != LDAP_SUCCESS ) {
287 /* check search access */
288 if ( !access_allowed( be, conn, op, e,
289 a->a_desc, &value, ACL_SEARCH, NULL ) ) {
295 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ )
297 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ )
303 rc = value_match( &ret, a->a_desc, mra->ma_rule, 0,
306 if( rc != LDAP_SUCCESS ) {
311 return LDAP_COMPARE_TRUE;
317 /* check attrs in DN AVAs if required */
318 if ( mra->ma_dnattrs ) {
323 /* parse and pretty the dn */
324 rc = dnPrettyDN( NULL, &e->e_name, &dn );
325 if ( rc != LDAP_SUCCESS ) {
326 return LDAP_INVALID_SYNTAX;
329 /* for each AVA of each RDN ... */
330 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
331 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
333 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
334 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
335 struct berval *bv = &ava->la_value, value;
336 AttributeDescription *ad = (AttributeDescription *)ava->la_private;
343 if ( mra->ma_desc ) {
344 /* have a mra type? check for subtype */
345 if ( !is_ad_subtype( ad, mra->ma_desc ) ) {
348 value = mra->ma_value;
351 const char *text = NULL;
353 /* check if matching is appropriate */
354 if ( !mr_usable_with_at( mra->ma_rule, ad->ad_type )) {
358 /* normalize for equality */
360 rc = asserted_value_validate_normalize( ad,
362 SLAP_MR_EXT|SLAP_MR_VALUE_OF_ASSERTION_SYNTAX,
363 &mra->ma_value, &value, &text );
365 rc = value_validate_normalize( ad, SLAP_MR_EQUALITY,
366 &mra->ma_value, &value, &text );
368 if ( rc != LDAP_SUCCESS ) {
372 /* check search access */
373 if ( !access_allowed( be, conn, op, e,
374 ad, &value, ACL_SEARCH, NULL ) ) {
380 rc = value_match( &ret, ad, mra->ma_rule, 0,
383 if( rc != LDAP_SUCCESS ) {
390 return LDAP_COMPARE_TRUE;
396 return LDAP_COMPARE_FALSE;
405 AttributeAssertion *ava,
411 if ( !access_allowed( be, conn, op, e,
412 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
414 return LDAP_INSUFFICIENT_ACCESS;
417 for(a = attrs_find( e->e_attrs, ava->aa_desc );
419 a = attrs_find( a->a_next, ava->aa_desc ) )
425 case LDAP_FILTER_APPROX:
426 mr = a->a_desc->ad_type->sat_approx;
427 if( mr != NULL ) break;
429 /* use EQUALITY matching rule if no APPROX rule */
431 case LDAP_FILTER_EQUALITY:
432 mr = a->a_desc->ad_type->sat_equality;
437 mr = a->a_desc->ad_type->sat_ordering;
449 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ )
451 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ )
458 rc = value_match( &ret, a->a_desc, mr, 0,
459 bv, &ava->aa_value, &text );
461 if( rc != LDAP_SUCCESS ) {
466 case LDAP_FILTER_EQUALITY:
467 case LDAP_FILTER_APPROX:
469 return LDAP_COMPARE_TRUE;
475 return LDAP_COMPARE_TRUE;
481 return LDAP_COMPARE_TRUE;
488 if ( ava->aa_desc == slap_schema.si_ad_hasSubordinates
489 && be && be->be_has_subordinates ) {
494 * No other match should be allowed ...
496 assert( type == LDAP_FILTER_EQUALITY );
498 if ( (*be->be_has_subordinates)( be, conn, op, e, &hasSubordinates ) ) {
502 if ( hasSubordinates == LDAP_COMPARE_TRUE ) {
504 hs.bv_len = sizeof( "TRUE" ) - 1;
506 } else if ( hasSubordinates == LDAP_COMPARE_FALSE ) {
508 hs.bv_len = sizeof( "FALSE" ) - 1;
514 if ( bvmatch( &ava->aa_value, &hs ) ) {
515 return LDAP_COMPARE_TRUE;
518 return LDAP_COMPARE_FALSE;
521 return( LDAP_COMPARE_FALSE );
526 test_presence_filter(
531 AttributeDescription *desc
536 if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_SEARCH, NULL ) )
538 return LDAP_INSUFFICIENT_ACCESS;
541 a = attrs_find( e->e_attrs, desc );
543 if ( a == NULL && desc == slap_schema.si_ad_hasSubordinates ) {
546 * XXX: fairly optimistic: if the function is defined,
547 * then PRESENCE must succeed, because hasSubordinate
548 * is boolean-valued; I think we may live with this
549 * simplification by now
551 if ( be && be->be_has_subordinates ) {
552 return LDAP_COMPARE_TRUE;
555 return LDAP_COMPARE_FALSE;
558 return a != NULL ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
572 int rtn = LDAP_COMPARE_TRUE; /* True if empty */
575 LDAP_LOG( FILTER, ENTRY, "test_filter_and: begin\n", 0, 0, 0 );
577 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
581 for ( f = flist; f != NULL; f = f->f_next ) {
582 int rc = test_filter( be, conn, op, e, f );
584 if ( rc == LDAP_COMPARE_FALSE ) {
585 /* filter is False */
590 if ( rc != LDAP_COMPARE_TRUE ) {
591 /* filter is Undefined unless later elements are False */
597 LDAP_LOG( FILTER, RESULTS, "test_filter_and: rc=%d\n", rtn, 0, 0 );
599 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
615 int rtn = LDAP_COMPARE_FALSE; /* False if empty */
618 LDAP_LOG( FILTER, ENTRY, "test_filter_or: begin\n", 0, 0, 0 );
620 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
624 for ( f = flist; f != NULL; f = f->f_next ) {
625 int rc = test_filter( be, conn, op, e, f );
627 if ( rc == LDAP_COMPARE_TRUE ) {
633 if ( rc != LDAP_COMPARE_FALSE ) {
634 /* filter is Undefined unless later elements are True */
640 LDAP_LOG( FILTER, ENTRY, "test_filter_or: result=%d\n", rtn, 0, 0 );
642 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
650 test_substrings_filter(
661 LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: begin\n", 0, 0, 0 );
663 Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
667 if ( !access_allowed( be, conn, op, e,
668 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
670 return LDAP_INSUFFICIENT_ACCESS;
673 for(a = attrs_find( e->e_attrs, f->f_sub_desc );
675 a = attrs_find( a->a_next, f->f_sub_desc ) )
677 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
685 for ( bv = a->a_nvals; bv->bv_val != NULL; bv++ )
687 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ )
694 rc = value_match( &ret, a->a_desc, mr, 0,
695 bv, f->f_sub, &text );
697 if( rc != LDAP_SUCCESS ) {
702 return LDAP_COMPARE_TRUE;
708 LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: return FALSE\n", 0, 0, 0 );
710 Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n", 0, 0, 0 );
713 return LDAP_COMPARE_FALSE;