1 /* filterentry.c - apply a filter to an entry */
4 * Copyright 1998-2000 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", LDAP_LEVEL_ENTRY,
60 "test_filter: begin\n" ));
62 Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
66 switch ( f->f_choice ) {
67 case SLAPD_FILTER_COMPUTED:
69 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
70 "test_filter: COMPUTED %s (%d)\n",
71 f->f_result == LDAP_COMPARE_FALSE ? "false" :
72 f->f_result == LDAP_COMPARE_TRUE ? "true" :
73 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" :
77 Debug( LDAP_DEBUG_FILTER, " COMPUTED %s (%d)\n",
78 f->f_result == LDAP_COMPARE_FALSE ? "false" :
79 f->f_result == LDAP_COMPARE_TRUE ? "true" :
80 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : "error",
87 case LDAP_FILTER_EQUALITY:
89 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
90 "test_filter: EQUALITY\n" ));
92 Debug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
95 rc = test_ava_filter( be, conn, op, e, f->f_ava,
96 LDAP_FILTER_EQUALITY );
99 case LDAP_FILTER_SUBSTRINGS:
101 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
102 "test_filter SUBSTRINGS\n" ));
104 Debug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
107 rc = test_substrings_filter( be, conn, op, e, f );
111 rc = test_ava_filter( be, conn, op, e, f->f_ava,
116 rc = test_ava_filter( be, conn, op, e, f->f_ava,
120 case LDAP_FILTER_PRESENT:
122 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
123 "test_filter: PRESENT\n" ));
125 Debug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
128 rc = test_presence_filter( be, conn, op, e, f->f_desc );
131 case LDAP_FILTER_APPROX:
133 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
134 "test_filter: APPROX\n" ));
136 Debug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
138 rc = test_ava_filter( be, conn, op, e, f->f_ava,
139 LDAP_FILTER_APPROX );
142 case LDAP_FILTER_AND:
144 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
145 "test_filter: AND\n" ));
147 Debug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
150 rc = test_filter_and( be, conn, op, e, f->f_and );
155 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
156 "test_filter: OR\n" ));
158 Debug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
161 rc = test_filter_or( be, conn, op, e, f->f_or );
164 case LDAP_FILTER_NOT:
166 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
167 "test_filter: NOT\n" ));
169 Debug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
172 rc = test_filter( be, conn, op, e, f->f_not );
174 /* Flip true to false and false to true
175 * but leave Undefined alone.
178 case LDAP_COMPARE_TRUE:
179 rc = LDAP_COMPARE_FALSE;
181 case LDAP_COMPARE_FALSE:
182 rc = LDAP_COMPARE_TRUE;
187 case LDAP_FILTER_EXT:
189 LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
190 "test_filter: EXT\n" ));
192 Debug( LDAP_DEBUG_FILTER, " EXT\n", 0, 0, 0 );
195 rc = test_mra_filter( be, conn, op, e, f->f_mra );
200 LDAP_LOG(( "filter", LDAP_LEVEL_INFO,
201 "test_filter: unknown filter type %lu\n",
204 Debug( LDAP_DEBUG_ANY, " unknown filter type %lu\n",
208 rc = LDAP_PROTOCOL_ERROR;
212 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
213 "test_filter: return=%d\n", rc ));
215 Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
221 static int test_mra_filter(
226 MatchingRuleAssertion *mra )
231 if( !access_allowed( be, conn, op, e,
232 mra->ma_desc, &mra->ma_value, ACL_SEARCH ) )
234 return LDAP_INSUFFICIENT_ACCESS;
237 if( strcmp(mra->ma_rule->smr_syntax->ssyn_oid,
238 mra->ma_desc->ad_type->sat_syntax->ssyn_oid) != 0)
240 return LDAP_INVALID_SYNTAX;
243 if( mra->ma_rule == NULL )
245 return LDAP_INAPPROPRIATE_MATCHING;
248 for(a = attrs_find( e->e_attrs, mra->ma_desc );
250 a = attrs_find( a->a_next, mra->ma_desc ) )
252 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
257 rc = value_match( &ret, a->a_desc, mra->ma_rule,
258 SLAP_MR_ASSERTION_SYNTAX_MATCH,
259 a->a_vals[i], &mra->ma_value,
262 if( rc != LDAP_SUCCESS ) {
267 return LDAP_COMPARE_TRUE;
272 return LDAP_COMPARE_FALSE;
281 AttributeAssertion *ava,
288 if ( !access_allowed( be, conn, op, e,
289 ava->aa_desc, &ava->aa_value, ACL_SEARCH ) )
291 return LDAP_INSUFFICIENT_ACCESS;
294 for(a = attrs_find( e->e_attrs, ava->aa_desc );
296 a = attrs_find( a->a_next, ava->aa_desc ) )
301 case LDAP_FILTER_APPROX:
302 mr = a->a_desc->ad_type->sat_approx;
303 if( mr != NULL ) break;
305 /* use EQUALITY matching rule if no APPROX rule */
307 case LDAP_FILTER_EQUALITY:
308 mr = a->a_desc->ad_type->sat_equality;
313 mr = a->a_desc->ad_type->sat_ordering;
324 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
329 rc = value_match( &ret, a->a_desc, mr,
330 SLAP_MR_ASSERTION_SYNTAX_MATCH,
331 a->a_vals[i], &ava->aa_value,
334 if( rc != LDAP_SUCCESS ) {
339 case LDAP_FILTER_EQUALITY:
340 case LDAP_FILTER_APPROX:
342 return LDAP_COMPARE_TRUE;
348 return LDAP_COMPARE_TRUE;
354 return LDAP_COMPARE_TRUE;
361 return( LDAP_COMPARE_FALSE );
366 test_presence_filter(
371 AttributeDescription *desc
374 if ( !access_allowed( be, conn, op, e, desc, NULL, ACL_SEARCH ) )
376 return LDAP_INSUFFICIENT_ACCESS;
379 return attrs_find( e->e_attrs, desc ) != NULL
380 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
394 int rtn = LDAP_COMPARE_TRUE; /* True if empty */
397 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
398 "test_filter_and: begin\n" ));
400 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
404 for ( f = flist; f != NULL; f = f->f_next ) {
405 int rc = test_filter( be, conn, op, e, f );
407 if ( rc == LDAP_COMPARE_FALSE ) {
408 /* filter is False */
413 if ( rc != LDAP_COMPARE_TRUE ) {
414 /* filter is Undefined unless later elements are False */
420 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
421 "test_filter_and: rc=%d\n", rtn ));
423 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
439 int rtn = LDAP_COMPARE_FALSE; /* False if empty */
442 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
443 "test_filter_or: begin\n" ));
445 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
449 for ( f = flist; f != NULL; f = f->f_next ) {
450 int rc = test_filter( be, conn, op, e, f );
452 if ( rc == LDAP_COMPARE_TRUE ) {
458 if ( rc != LDAP_COMPARE_FALSE ) {
459 /* filter is Undefined unless later elements are True */
465 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
466 "test_filter_or: result=%d\n", rtn ));
468 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
476 test_substrings_filter(
487 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
488 "test_substrings_filter: begin\n" ));
490 Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
494 if ( !access_allowed( be, conn, op, e,
495 f->f_sub_desc, NULL, ACL_SEARCH ) )
497 return LDAP_INSUFFICIENT_ACCESS;
500 for(a = attrs_find( e->e_attrs, f->f_sub_desc );
502 a = attrs_find( a->a_next, f->f_sub_desc ) )
505 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
511 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
516 rc = value_match( &ret, a->a_desc, mr,
517 SLAP_MR_ASSERTION_SYNTAX_MATCH,
518 a->a_vals[i], f->f_sub,
521 if( rc != LDAP_SUCCESS ) {
526 return LDAP_COMPARE_TRUE;
532 LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
533 "test_substrings_filter: return FALSE\n" ));
535 Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n", 0, 0, 0 );
538 return LDAP_COMPARE_FALSE;