1 /* filterentry.c - apply a filter to an entry */
4 * Copyright 1998-2002 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 #ifndef SLAP_X_MRA_MATCH_DNATTRS
219 if( !access_allowed( be, conn, op, e,
220 mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
222 return LDAP_INSUFFICIENT_ACCESS;
224 #else /* SLAP_X_MRA_MATCH_DNATTRS */
225 if ( mra->ma_desc ) {
227 * if ma_desc is available, then we're filtering for
228 * one attribute, and SEARCH permissions can be checked
231 if( !access_allowed( be, conn, op, e,
232 mra->ma_desc, &mra->ma_value, ACL_SEARCH, NULL ) )
234 return LDAP_INSUFFICIENT_ACCESS;
236 #endif /* SLAP_X_MRA_MATCH_DNATTRS */
238 for(a = attrs_find( e->e_attrs, mra->ma_desc );
240 a = attrs_find( a->a_next, mra->ma_desc ) )
243 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
248 rc = value_match( &ret, a->a_desc, mra->ma_rule,
249 SLAP_MR_ASSERTION_SYNTAX_MATCH,
250 bv, &mra->ma_value, &text );
252 if( rc != LDAP_SUCCESS ) {
257 return LDAP_COMPARE_TRUE;
261 #ifdef SLAP_X_MRA_MATCH_DNATTRS
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 = value_validate_normalize( a->a_desc,
280 &mra->ma_value, &value, &text );
281 if ( rc != LDAP_SUCCESS ) {
285 /* check search access */
286 if ( !access_allowed( be, conn, op, e,
287 a->a_desc, &value, ACL_SEARCH, NULL ) ) {
292 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
296 rc = value_match( &ret, a->a_desc, mra->ma_rule,
297 SLAP_MR_ASSERTION_SYNTAX_MATCH,
300 if( rc != LDAP_SUCCESS ) {
305 return LDAP_COMPARE_TRUE;
311 /* check attrs in DN AVAs if required */
312 if ( mra->ma_dnattrs ) {
317 /* parse and pretty the dn */
318 rc = dnPrettyDN( NULL, &e->e_name, &dn );
319 if ( rc != LDAP_SUCCESS ) {
320 return LDAP_INVALID_SYNTAX;
323 /* for each AVA of each RDN ... */
324 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
325 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
327 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
328 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
329 struct berval *bv = &ava->la_value, value;
330 AttributeDescription *ad = (AttributeDescription *)ava->la_private;
337 if ( mra->ma_desc ) {
338 /* have a mra type? check for subtype */
339 if ( !is_ad_subtype( ad, mra->ma_desc ) ) {
342 value = mra->ma_value;
345 const char *text = NULL;
347 /* check if matching is appropriate */
348 if ( !mr_usable_with_at( mra->ma_rule, ad->ad_type )) {
352 /* normalize for equality */
353 rc = value_validate_normalize( ad, SLAP_MR_EQUALITY,
354 &mra->ma_value, &value, &text );
355 if ( rc != LDAP_SUCCESS ) {
359 /* check search access */
360 if ( !access_allowed( be, conn, op, e,
361 ad, &value, ACL_SEARCH, NULL ) ) {
367 rc = value_match( &ret, ad, mra->ma_rule,
368 SLAP_MR_ASSERTION_SYNTAX_MATCH,
371 if( rc != LDAP_SUCCESS ) {
378 return LDAP_COMPARE_TRUE;
383 #endif /* SLAP_X_MRA_MATCH_DNATTRS */
385 return LDAP_COMPARE_FALSE;
394 AttributeAssertion *ava,
400 if ( !access_allowed( be, conn, op, e,
401 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
403 return LDAP_INSUFFICIENT_ACCESS;
406 for(a = attrs_find( e->e_attrs, ava->aa_desc );
408 a = attrs_find( a->a_next, ava->aa_desc ) )
414 case LDAP_FILTER_APPROX:
415 mr = a->a_desc->ad_type->sat_approx;
416 if( mr != NULL ) break;
418 /* use EQUALITY matching rule if no APPROX rule */
420 case LDAP_FILTER_EQUALITY:
421 mr = a->a_desc->ad_type->sat_equality;
426 mr = a->a_desc->ad_type->sat_ordering;
437 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
442 rc = value_match( &ret, a->a_desc, mr,
443 SLAP_MR_ASSERTION_SYNTAX_MATCH,
444 bv, &ava->aa_value, &text );
446 if( rc != LDAP_SUCCESS ) {
451 case LDAP_FILTER_EQUALITY:
452 case LDAP_FILTER_APPROX:
454 return LDAP_COMPARE_TRUE;
460 return LDAP_COMPARE_TRUE;
466 return LDAP_COMPARE_TRUE;
473 return( LDAP_COMPARE_FALSE );
478 test_presence_filter(
483 AttributeDescription *desc
486 if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_SEARCH, NULL ) )
488 return LDAP_INSUFFICIENT_ACCESS;
491 return attrs_find( e->e_attrs, desc ) != NULL
492 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
506 int rtn = LDAP_COMPARE_TRUE; /* True if empty */
509 LDAP_LOG( FILTER, ENTRY, "test_filter_and: begin\n", 0, 0, 0 );
511 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
515 for ( f = flist; f != NULL; f = f->f_next ) {
516 int rc = test_filter( be, conn, op, e, f );
518 if ( rc == LDAP_COMPARE_FALSE ) {
519 /* filter is False */
524 if ( rc != LDAP_COMPARE_TRUE ) {
525 /* filter is Undefined unless later elements are False */
531 LDAP_LOG( FILTER, RESULTS, "test_filter_and: rc=%d\n", rtn, 0, 0 );
533 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
549 int rtn = LDAP_COMPARE_FALSE; /* False if empty */
552 LDAP_LOG( FILTER, ENTRY, "test_filter_or: begin\n", 0, 0, 0 );
554 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
558 for ( f = flist; f != NULL; f = f->f_next ) {
559 int rc = test_filter( be, conn, op, e, f );
561 if ( rc == LDAP_COMPARE_TRUE ) {
567 if ( rc != LDAP_COMPARE_FALSE ) {
568 /* filter is Undefined unless later elements are True */
574 LDAP_LOG( FILTER, ENTRY, "test_filter_or: result=%d\n", rtn, 0, 0 );
576 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
584 test_substrings_filter(
595 LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: begin\n", 0, 0, 0 );
597 Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
601 if ( !access_allowed( be, conn, op, e,
602 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
604 return LDAP_INSUFFICIENT_ACCESS;
607 for(a = attrs_find( e->e_attrs, f->f_sub_desc );
609 a = attrs_find( a->a_next, f->f_sub_desc ) )
611 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
618 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
623 rc = value_match( &ret, a->a_desc, mr,
624 SLAP_MR_ASSERTION_SYNTAX_MATCH,
625 bv, f->f_sub, &text );
627 if( rc != LDAP_SUCCESS ) {
632 return LDAP_COMPARE_TRUE;
638 LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: return FALSE\n", 0, 0, 0 );
640 Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n", 0, 0, 0 );
643 return LDAP_COMPARE_FALSE;