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
58 Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
60 switch ( f->f_choice ) {
61 case SLAPD_FILTER_COMPUTED:
62 Debug( LDAP_DEBUG_FILTER, " COMPUTED %s (%d)\n",
63 f->f_result == LDAP_COMPARE_FALSE ? "false" :
64 f->f_result == LDAP_COMPARE_TRUE ? "true" :
65 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : "error",
70 case LDAP_FILTER_EQUALITY:
71 Debug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
72 rc = test_ava_filter( be, conn, op, e, f->f_ava,
73 LDAP_FILTER_EQUALITY );
76 case LDAP_FILTER_SUBSTRINGS:
77 Debug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
78 rc = test_substrings_filter( be, conn, op, e, f );
82 rc = test_ava_filter( be, conn, op, e, f->f_ava,
87 rc = test_ava_filter( be, conn, op, e, f->f_ava,
91 case LDAP_FILTER_PRESENT:
92 Debug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
93 rc = test_presence_filter( be, conn, op, e, f->f_desc );
96 case LDAP_FILTER_APPROX:
97 Debug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
98 rc = test_ava_filter( be, conn, op, e, f->f_ava,
102 case LDAP_FILTER_AND:
103 Debug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
104 rc = test_filter_and( be, conn, op, e, f->f_and );
108 Debug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
109 rc = test_filter_or( be, conn, op, e, f->f_or );
112 case LDAP_FILTER_NOT:
113 Debug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
114 rc = test_filter( be, conn, op, e, f->f_not );
117 case LDAP_COMPARE_TRUE:
118 rc = LDAP_COMPARE_FALSE;
120 case LDAP_COMPARE_FALSE:
121 rc = LDAP_COMPARE_TRUE;
126 #ifdef SLAPD_EXT_FILTERS
127 case LDAP_FILTER_EXT:
128 Debug( LDAP_DEBUG_FILTER, " EXT\n", 0, 0, 0 );
129 rc = test_mra_filter( be, conn, op, e, f->f_mra );
134 Debug( LDAP_DEBUG_ANY, " unknown filter type %lu\n",
136 rc = LDAP_PROTOCOL_ERROR;
139 Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
150 AttributeAssertion *ava,
157 if ( be != NULL && ! access_allowed( be, conn, op, e,
158 ava->aa_desc, ava->aa_value, ACL_SEARCH ) )
160 return LDAP_INSUFFICIENT_ACCESS;
163 for(a = attrs_find( e->e_attrs, ava->aa_desc );
165 a = attrs_find( a->a_next, ava->aa_desc ) )
170 case LDAP_FILTER_APPROX:
171 mr = a->a_desc->ad_type->sat_approx;
172 if( mr != NULL ) break;
174 /* use EQUALITY matching rule if no APPROX rule */
176 case LDAP_FILTER_EQUALITY:
177 mr = a->a_desc->ad_type->sat_equality;
182 mr = a->a_desc->ad_type->sat_ordering;
193 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
198 rc = value_match( &ret, a->a_desc, mr, 0,
199 a->a_vals[i], ava->aa_value,
202 if( rc != LDAP_SUCCESS ) {
207 case LDAP_FILTER_EQUALITY:
208 case LDAP_FILTER_APPROX:
210 return LDAP_COMPARE_TRUE;
216 return LDAP_COMPARE_TRUE;
222 return LDAP_COMPARE_TRUE;
229 return( LDAP_COMPARE_FALSE );
234 test_presence_filter(
239 AttributeDescription *desc
242 if ( be != NULL && ! access_allowed( be, conn, op, e,
243 desc, NULL, ACL_SEARCH ) )
245 return LDAP_INSUFFICIENT_ACCESS;
248 return attrs_find( e->e_attrs, desc ) != NULL
249 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
263 int rtn = LDAP_COMPARE_TRUE;
265 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
267 for ( f = flist; f != NULL; f = f->f_next ) {
268 int rc = test_filter( be, conn, op, e, f );
270 if ( rc == LDAP_COMPARE_FALSE ) {
271 rtn = LDAP_COMPARE_FALSE;
274 if ( rc != LDAP_COMPARE_TRUE ) {
279 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
293 int rtn = LDAP_COMPARE_FALSE;
295 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
297 for ( f = flist; f != NULL; f = f->f_next ) {
298 int rc = test_filter( be, conn, op, e, f );
300 if ( rc == LDAP_COMPARE_TRUE ) {
301 rtn = LDAP_COMPARE_TRUE;
304 if ( rc != LDAP_COMPARE_TRUE ) {
309 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
315 test_substrings_filter(
325 Debug( LDAP_DEBUG_FILTER, "begin test_substrings_filter\n", 0, 0, 0 );
327 if ( be != NULL && ! access_allowed( be, conn, op, e,
328 f->f_sub_desc, NULL, ACL_SEARCH ) )
330 return LDAP_INSUFFICIENT_ACCESS;
333 for(a = attrs_find( e->e_attrs, f->f_sub_desc );
335 a = attrs_find( a->a_next, f->f_sub_desc ) )
338 MatchingRule *mr = a->a_desc->ad_type->sat_substr;
344 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
349 rc = value_match( &ret, a->a_desc, mr, 0,
350 a->a_vals[i], f->f_sub,
353 if( rc != LDAP_SUCCESS ) {
358 return LDAP_COMPARE_TRUE;
363 Debug( LDAP_DEBUG_FILTER, "end test_substrings_filter 1\n", 0, 0, 0 );
364 return LDAP_COMPARE_FALSE;