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 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 ) )
235 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
240 rc = value_match( &ret, a->a_desc, mra->ma_rule,
241 SLAP_MR_ASSERTION_SYNTAX_MATCH,
242 bv, &mra->ma_value, &text );
244 if( rc != LDAP_SUCCESS ) {
249 return LDAP_COMPARE_TRUE;
256 * No attribute description: test all
258 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
259 struct berval *bv, value;
260 const char *text = NULL;
263 /* check if matching is appropriate */
264 if ( !mr_usable_with_at( mra->ma_rule, a->a_desc->ad_type )) {
268 /* normalize for equality */
269 rc = value_validate_normalize( a->a_desc,
271 &mra->ma_value, &value, &text );
272 if ( rc != LDAP_SUCCESS ) {
276 /* check search access */
277 if ( !access_allowed( be, conn, op, e,
278 a->a_desc, &value, ACL_SEARCH, NULL ) ) {
283 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
287 rc = value_match( &ret, a->a_desc, mra->ma_rule,
288 SLAP_MR_ASSERTION_SYNTAX_MATCH,
291 if( rc != LDAP_SUCCESS ) {
296 return LDAP_COMPARE_TRUE;
302 /* check attrs in DN AVAs if required */
303 if ( mra->ma_dnattrs ) {
308 /* parse and pretty the dn */
309 rc = dnPrettyDN( NULL, &e->e_name, &dn );
310 if ( rc != LDAP_SUCCESS ) {
311 return LDAP_INVALID_SYNTAX;
314 /* for each AVA of each RDN ... */
315 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
316 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
318 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
319 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
320 struct berval *bv = &ava->la_value, value;
321 AttributeDescription *ad = (AttributeDescription *)ava->la_private;
328 if ( mra->ma_desc ) {
329 /* have a mra type? check for subtype */
330 if ( !is_ad_subtype( ad, mra->ma_desc ) ) {
333 value = mra->ma_value;
336 const char *text = NULL;
338 /* check if matching is appropriate */
339 if ( !mr_usable_with_at( mra->ma_rule, ad->ad_type )) {
343 /* normalize for equality */
344 rc = value_validate_normalize( ad, SLAP_MR_EQUALITY,
345 &mra->ma_value, &value, &text );
346 if ( rc != LDAP_SUCCESS ) {
350 /* check search access */
351 if ( !access_allowed( be, conn, op, e,
352 ad, &value, ACL_SEARCH, NULL ) ) {
358 rc = value_match( &ret, ad, mra->ma_rule,
359 SLAP_MR_ASSERTION_SYNTAX_MATCH,
362 if( rc != LDAP_SUCCESS ) {
369 return LDAP_COMPARE_TRUE;
375 return LDAP_COMPARE_FALSE;
384 AttributeAssertion *ava,
390 if ( !access_allowed( be, conn, op, e,
391 ava->aa_desc, &ava->aa_value, ACL_SEARCH, NULL ) )
393 return LDAP_INSUFFICIENT_ACCESS;
396 for(a = attrs_find( e->e_attrs, ava->aa_desc );
398 a = attrs_find( a->a_next, ava->aa_desc ) )
404 case LDAP_FILTER_APPROX:
405 mr = a->a_desc->ad_type->sat_approx;
406 if( mr != NULL ) break;
408 /* use EQUALITY matching rule if no APPROX rule */
410 case LDAP_FILTER_EQUALITY:
411 mr = a->a_desc->ad_type->sat_equality;
416 mr = a->a_desc->ad_type->sat_ordering;
427 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
432 rc = value_match( &ret, a->a_desc, mr,
433 SLAP_MR_ASSERTION_SYNTAX_MATCH,
434 bv, &ava->aa_value, &text );
436 if( rc != LDAP_SUCCESS ) {
441 case LDAP_FILTER_EQUALITY:
442 case LDAP_FILTER_APPROX:
444 return LDAP_COMPARE_TRUE;
450 return LDAP_COMPARE_TRUE;
456 return LDAP_COMPARE_TRUE;
463 if ( ava->aa_desc == slap_schema.si_ad_hasSubordinates
464 && be && be->be_has_subordinates ) {
469 * No other match should be allowed ...
471 assert( type == LDAP_FILTER_EQUALITY );
473 if ( (*be->be_has_subordinates)( be, conn, op, e, &hasSubordinates ) ) {
477 if ( hasSubordinates == LDAP_COMPARE_TRUE ) {
479 hs.bv_len = sizeof( "TRUE" ) - 1;
481 } else if ( hasSubordinates == LDAP_COMPARE_FALSE ) {
483 hs.bv_len = sizeof( "FALSE" ) - 1;
489 if ( bvmatch( &ava->aa_value, &hs ) ) {
490 return LDAP_COMPARE_TRUE;
493 return LDAP_COMPARE_FALSE;
496 return( LDAP_COMPARE_FALSE );
501 test_presence_filter(
506 AttributeDescription *desc
511 if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_SEARCH, NULL ) )
513 return LDAP_INSUFFICIENT_ACCESS;
516 a = attrs_find( e->e_attrs, desc );
518 if ( a == NULL && desc == slap_schema.si_ad_hasSubordinates ) {
521 * XXX: fairly optimistic: if the function is defined,
522 * then PRESENCE must succeed, because hasSubordinate
523 * is boolean-valued; I think we may live with this
524 * simplification by now
526 if ( be && be->be_has_subordinates ) {
527 return LDAP_COMPARE_TRUE;
530 return LDAP_COMPARE_FALSE;
533 return a != NULL ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
547 int rtn = LDAP_COMPARE_TRUE; /* True if empty */
550 LDAP_LOG( FILTER, ENTRY, "test_filter_and: begin\n", 0, 0, 0 );
552 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
556 for ( f = flist; f != NULL; f = f->f_next ) {
557 int rc = test_filter( be, conn, op, e, f );
559 if ( rc == LDAP_COMPARE_FALSE ) {
560 /* filter is False */
565 if ( rc != LDAP_COMPARE_TRUE ) {
566 /* filter is Undefined unless later elements are False */
572 LDAP_LOG( FILTER, RESULTS, "test_filter_and: rc=%d\n", rtn, 0, 0 );
574 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
590 int rtn = LDAP_COMPARE_FALSE; /* False if empty */
593 LDAP_LOG( FILTER, ENTRY, "test_filter_or: begin\n", 0, 0, 0 );
595 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
599 for ( f = flist; f != NULL; f = f->f_next ) {
600 int rc = test_filter( be, conn, op, e, f );
602 if ( rc == LDAP_COMPARE_TRUE ) {
608 if ( rc != LDAP_COMPARE_FALSE ) {
609 /* filter is Undefined unless later elements are True */
615 LDAP_LOG( FILTER, ENTRY, "test_filter_or: result=%d\n", rtn, 0, 0 );
617 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
625 test_substrings_filter(
636 LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: begin\n", 0, 0, 0 );
638 Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
642 if ( !access_allowed( be, conn, op, e,
643 f->f_sub_desc, NULL, ACL_SEARCH, NULL ) )
645 return LDAP_INSUFFICIENT_ACCESS;
648 for(a = attrs_find( e->e_attrs, f->f_sub_desc );
650 a = attrs_find( a->a_next, f->f_sub_desc ) )
652 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
659 for ( bv = a->a_vals; bv->bv_val != NULL; bv++ ) {
664 rc = value_match( &ret, a->a_desc, mr,
665 SLAP_MR_ASSERTION_SYNTAX_MATCH,
666 bv, f->f_sub, &text );
668 if( rc != LDAP_SUCCESS ) {
673 return LDAP_COMPARE_TRUE;
679 LDAP_LOG( FILTER, ENTRY, "test_substrings_filter: return FALSE\n", 0, 0, 0 );
681 Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n", 0, 0, 0 );
684 return LDAP_COMPARE_FALSE;