1 /* filterentry.c - apply a filter to an entry */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
15 #ifndef SLAPD_SCHEMA_NOT_COMPAT
21 static int test_filter_and( Backend *be,
22 Connection *conn, Operation *op,
23 Entry *e, Filter *flist );
24 static int test_filter_or( Backend *be,
25 Connection *conn, Operation *op,
26 Entry *e, Filter *flist );
27 static int test_substring_filter( Backend *be,
28 Connection *conn, Operation *op,
30 #ifdef SLAPD_SCHEMA_NOT_COMPAT
31 static int test_ava_filter( Backend *be,
32 Connection *conn, Operation *op,
33 Entry *e, AttributeAssertion *ava, int type );
34 static int test_mra_filter( Backend *be,
35 Connection *conn, Operation *op,
36 Entry *e, MatchingRuleAssertion *mra );
37 static int test_presence_filter( Backend *be,
38 Connection *conn, Operation *op,
39 Entry *e, AttributeDescription *desc );
41 static int test_ava_filter(Backend *be,
42 Connection *conn, Operation *op,
43 Entry *e, Ava *ava, int type);
44 static int test_approx_filter(Backend *be,
45 Connection *conn, Operation *op,
47 static int test_presence_filter(Backend *be,
48 Connection *conn, Operation *op,
49 Entry *e, char *type);
54 * test_filter - test a filter against a single entry.
56 * LDAP_COMPARE_TRUE filter matched
57 * LDAP_COMPARE_FALSE filter did not match
58 * SLAPD_COMPARE_UNDEFINED filter is undefined
59 * or an ldap result code indicating error
73 Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
75 switch ( f->f_choice ) {
76 #ifdef SLAPD_SCHEMA_NOT_COMPAT
77 case SLAPD_FILTER_COMPUTED:
78 Debug( LDAP_DEBUG_FILTER, " COMPUTED %s (%d)\n",
79 f->f_result == LDAP_COMPARE_FALSE ? "false" :
80 f->f_result == LDAP_COMPARE_TRUE ? "true" :
81 f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : "error",
87 case LDAP_FILTER_EQUALITY:
88 Debug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
89 #ifdef SLAPD_SCHEMA_NOT_COMPAT
90 rc = test_ava_filter( be, conn, op, e, f->f_ava,
91 LDAP_FILTER_EQUALITY );
93 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
94 LDAP_FILTER_EQUALITY );
98 case LDAP_FILTER_SUBSTRINGS:
99 Debug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
100 rc = test_substring_filter( be, conn, op, e, f );
104 #ifdef SLAPD_SCHEMA_NOT_COMPAT
105 rc = test_ava_filter( be, conn, op, e, f->f_ava,
108 Debug( LDAP_DEBUG_FILTER, " GE\n", 0, 0, 0 );
109 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
115 #ifdef SLAPD_SCHEMA_NOT_COMPAT
116 rc = test_ava_filter( be, conn, op, e, f->f_ava,
119 Debug( LDAP_DEBUG_FILTER, " LE\n", 0, 0, 0 );
120 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
125 case LDAP_FILTER_PRESENT:
126 Debug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
127 #ifdef SLAPD_SCHEMA_NOT_COMPAT
128 rc = test_presence_filter( be, conn, op, e, f->f_desc );
130 rc = test_presence_filter( be, conn, op, e, f->f_type );
134 case LDAP_FILTER_APPROX:
135 Debug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
136 #ifdef SLAPD_SCHEMA_NOT_COMPAT
137 rc = test_ava_filter( be, conn, op, e, f->f_ava,
138 LDAP_FILTER_APPROX );
140 rc = test_approx_filter( be, conn, op, e, &f->f_ava );
144 case LDAP_FILTER_AND:
145 Debug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
146 rc = test_filter_and( be, conn, op, e, f->f_and );
150 Debug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
151 rc = test_filter_or( be, conn, op, e, f->f_or );
154 case LDAP_FILTER_NOT:
155 Debug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
156 rc = test_filter( be, conn, op, e, f->f_not );
159 case LDAP_COMPARE_TRUE:
160 rc = LDAP_COMPARE_FALSE;
162 case LDAP_COMPARE_FALSE:
163 rc = LDAP_COMPARE_TRUE;
168 #ifdef SLAPD_EXT_FILTERS
169 case LDAP_FILTER_EXT:
170 Debug( LDAP_DEBUG_FILTER, " EXT\n", 0, 0, 0 );
171 #if SLAPD_SCHEMA_NOT_COMPAT
172 rc = test_mra_filter( be, conn, op, e, f->f_mra );
180 Debug( LDAP_DEBUG_FILTER, " UNDEFINED\n", 0, 0, 0 );
185 Debug( LDAP_DEBUG_ANY, " unknown filter type %lu\n",
190 Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
201 #ifdef SLAPD_SCHEMA_NOT_COMPAT
202 AttributeAssertion *ava,
212 #ifdef SLAPD_SCHEMA_NOT_COMPAT
213 if ( be != NULL && ! access_allowed( be, conn, op, e,
214 ava->aa_desc, ava->aa_value, ACL_SEARCH ) )
217 if ( be != NULL && ! access_allowed( be, conn, op, e,
218 ava->ava_type, &ava->ava_value, ACL_SEARCH ) )
224 #ifdef SLAPD_SCHEMA_NOT_COMPAT
225 for(a = attrs_find( e->e_attrs, ava->aa_desc );
227 a = attrs_find( a, ava->aa_desc ) )
229 a = attr_find( e->e_attrs, ava->ava_type );
233 #ifndef SLAPD_SCHEMA_NOT_COMPAT
234 if ( a->a_syntax == 0 ) {
235 a->a_syntax = attr_syntax( ava->ava_type );
239 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
242 #ifdef SLAPD_SCHEMA_NOT_COMPAT
243 /* not yet implemented */
246 rc = value_cmp( a->a_vals[i], &ava->ava_value, a->a_syntax,
251 case LDAP_FILTER_EQUALITY:
252 case LDAP_FILTER_APPROX:
254 return LDAP_COMPARE_TRUE;
260 return LDAP_COMPARE_TRUE;
266 return LDAP_COMPARE_TRUE;
273 return( LDAP_COMPARE_FALSE );
278 test_presence_filter(
283 #ifdef SLAPD_SCHEMA_NOT_COMPAT
284 AttributeDescription *desc
290 if ( be != NULL && ! access_allowed( be, conn, op, e,
291 desc, NULL, ACL_SEARCH ) )
296 #ifdef SLAPD_SCHEMA_NOT_COMPAT
297 return attrs_find( e->e_attrs, desc ) != NULL
299 return attr_find( e->e_attrs, desc ) != NULL
301 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
304 #ifndef SLAPD_SCHEMA_NOT_COMPAT
314 char *w1, *w2, *c1, *c2;
318 if ( be != NULL && ! access_allowed( be, conn, op, e,
319 ava->ava_type, NULL, ACL_SEARCH ) )
324 a = attr_find( e->e_attrs, ava->ava_type );
326 /* for each value in the attribute */
327 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
329 * try to match words in the filter value in order
330 * in the attribute value.
333 w2 = a->a_vals[i]->bv_val;
334 /* for each word in the filter value */
335 for ( w1 = first_word( ava->ava_value.bv_val ); w1 != NULL;
336 w1 = next_word( w1 ) ) {
337 if ( (c1 = phonetic( w1 )) == NULL ) {
342 * for each word in the attribute value from
343 * where we left off...
345 for ( w2 = first_word( w2 ); w2 != NULL;
346 w2 = next_word( w2 ) ) {
348 if ( strcmp( c1, c2 ) == 0 ) {
357 * if we stopped because we ran out of words
358 * before making a match, go on to the next
359 * value. otherwise try to keep matching
360 * words in this value from where we left off.
365 w2 = next_word( w2 );
369 * if we stopped because we ran out of words we
373 return LDAP_COMPARE_TRUE;
378 return LDAP_COMPARE_FALSE;
392 int rtn = LDAP_COMPARE_TRUE;
394 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
396 for ( f = flist; f != NULL; f = f->f_next ) {
397 int rc = test_filter( be, conn, op, e, f );
399 if ( rc == LDAP_COMPARE_FALSE ) {
400 rtn = LDAP_COMPARE_FALSE;
403 if ( rc != LDAP_COMPARE_TRUE ) {
408 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
422 int rtn = LDAP_COMPARE_FALSE;
424 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
426 for ( f = flist; f != NULL; f = f->f_next ) {
427 int rc = test_filter( be, conn, op, e, f );
429 if ( rc == LDAP_COMPARE_TRUE ) {
430 rtn = LDAP_COMPARE_TRUE;
433 if ( rc != LDAP_COMPARE_TRUE ) {
438 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
442 #ifndef SLAPD_SCHEMA_NOT_COMPAT
444 strcpy_regex( char *d, char *s )
473 test_substring_filter(
481 #ifndef SLAPD_SCHEMA_NOT_COMPAT
484 char *p, *end, *realval, *tmp;
490 Debug( LDAP_DEBUG_FILTER, "begin test_substring_filter\n", 0, 0, 0 );
492 if ( be != NULL && ! access_allowed( be, conn, op, e,
493 f->f_sub_type, NULL, ACL_SEARCH ) )
498 if ( (a = attr_find( e->e_attrs, f->f_sub_type )) == NULL ) {
499 return LDAP_COMPARE_FALSE;
502 if ( a->a_syntax & SYNTAX_BIN ) {
503 Debug( LDAP_DEBUG_FILTER, "test_substring_filter bin attr\n",
509 * construct a regular expression corresponding to the
510 * filter and let regex do the work
515 end = pat + sizeof(pat) - 2; /* leave room for null */
516 if ( f->f_sub_initial != NULL ) {
518 p = strchr( p, '\0' );
519 /* 2 * in case every char is special */
520 if ( p + 2 * f->f_sub_initial->bv_len > end ) {
521 Debug( LDAP_DEBUG_ANY, "not enough pattern space\n",
525 strcpy_regex( p, f->f_sub_initial->bv_val );
526 p = strchr( p, '\0' );
528 if ( f->f_sub_any != NULL ) {
529 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
531 if ( p + 2 * f->f_sub_any[i]->bv_len + 2 > end ) {
532 Debug( LDAP_DEBUG_ANY,
533 "not enough pattern space\n", 0, 0, 0 );
537 p = strchr( p, '\0' );
538 strcpy_regex( p, f->f_sub_any[i]->bv_val );
539 p = strchr( p, '\0' );
542 if ( f->f_sub_final != NULL ) {
544 if ( p + 2 * f->f_sub_final->bv_len + 2 > end ) {
545 Debug( LDAP_DEBUG_ANY, "not enough pattern space\n",
550 p = strchr( p, '\0' );
551 strcpy_regex( p, f->f_sub_final->bv_val );
552 p = strchr( p, '\0' );
556 /* compile the regex */
557 Debug( LDAP_DEBUG_FILTER, "test_substring_filter: regcomp pat: %s\n",
559 if ((rc = regcomp(&re, pat, REG_EXTENDED|REG_NOSUB))) {
562 regerror(rc, &re, error, sizeof(error));
563 Debug( LDAP_DEBUG_ANY, "regcomp failed (%s) %s\n",
568 /* for each value in the attribute see if regex matches */
569 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
572 if ( val->bv_len < sizeof(buf) ) {
573 strcpy( buf, val->bv_val );
576 tmp = (char *) ch_malloc( val->bv_len + 1 );
577 strcpy( tmp, val->bv_val );
581 value_normalize( realval, a->a_syntax );
583 rc = !regexec(&re, realval, 0, NULL, 0);
590 return LDAP_COMPARE_TRUE;
597 Debug( LDAP_DEBUG_FILTER, "end test_substring_filter 1\n", 0, 0, 0 );
598 return LDAP_COMPARE_FALSE;