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 * or an ldap error code
72 Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
74 switch ( f->f_choice ) {
75 case LDAP_FILTER_EQUALITY:
76 Debug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
77 #ifdef SLAPD_SCHEMA_NOT_COMPAT
78 rc = test_ava_filter( be, conn, op, e, f->f_ava,
79 LDAP_FILTER_EQUALITY );
81 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
82 LDAP_FILTER_EQUALITY );
86 case LDAP_FILTER_SUBSTRINGS:
87 Debug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
88 rc = test_substring_filter( be, conn, op, e, f );
92 #ifdef SLAPD_SCHEMA_NOT_COMPAT
93 rc = test_ava_filter( be, conn, op, e, f->f_ava,
96 Debug( LDAP_DEBUG_FILTER, " GE\n", 0, 0, 0 );
97 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
103 #ifdef SLAPD_SCHEMA_NOT_COMPAT
104 rc = test_ava_filter( be, conn, op, e, f->f_ava,
107 Debug( LDAP_DEBUG_FILTER, " LE\n", 0, 0, 0 );
108 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
113 case LDAP_FILTER_PRESENT:
114 Debug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
115 #ifdef SLAPD_SCHEMA_NOT_COMPAT
116 rc = test_presence_filter( be, conn, op, e, f->f_desc );
118 rc = test_presence_filter( be, conn, op, e, f->f_type );
122 case LDAP_FILTER_APPROX:
123 Debug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
124 #ifdef SLAPD_SCHEMA_NOT_COMPAT
125 rc = test_ava_filter( be, conn, op, e, f->f_ava,
126 LDAP_FILTER_APPROX );
128 rc = test_approx_filter( be, conn, op, e, &f->f_ava );
132 case LDAP_FILTER_AND:
133 Debug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
134 rc = test_filter_and( be, conn, op, e, f->f_and );
138 Debug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
139 rc = test_filter_or( be, conn, op, e, f->f_or );
142 case LDAP_FILTER_NOT:
143 Debug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
144 rc = test_filter( be, conn, op, e, f->f_not );
147 case LDAP_COMPARE_TRUE:
148 rc = LDAP_COMPARE_FALSE;
150 case LDAP_COMPARE_FALSE:
151 rc = LDAP_COMPARE_TRUE;
156 #ifdef SLAPD_EXT_FILTERS
157 case LDAP_FILTER_EXT:
158 Debug( LDAP_DEBUG_FILTER, " EXT\n", 0, 0, 0 );
159 #if SLAPD_SCHEMA_NOT_COMPAT
160 rc = test_mra_filter( be, conn, op, e, f->f_mra );
168 Debug( LDAP_DEBUG_FILTER, " UNDEFINED\n", 0, 0, 0 );
173 Debug( LDAP_DEBUG_ANY, " unknown filter type %lu\n",
178 Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
189 #ifdef SLAPD_SCHEMA_NOT_COMPAT
190 AttributeAssertion *ava,
200 #ifdef SLAPD_SCHEMA_NOT_COMPAT
201 if ( be != NULL && ! access_allowed( be, conn, op, e,
202 ava->aa_desc, ava->aa_value, ACL_SEARCH ) )
205 if ( be != NULL && ! access_allowed( be, conn, op, e,
206 ava->ava_type, &ava->ava_value, ACL_SEARCH ) )
212 #ifdef SLAPD_SCHEMA_NOT_COMPAT
213 for(a = attrs_find( e->e_attrs, ava->aa_desc );
215 a = attrs_find( a, ava->aa_desc ) )
217 a = attr_find( e->e_attrs, ava->ava_type );
221 #ifndef SLAPD_SCHEMA_NOT_COMPAT
222 if ( a->a_syntax == 0 ) {
223 a->a_syntax = attr_syntax( ava->ava_type );
227 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
230 #ifdef SLAPD_SCHEMA_NOT_COMPAT
231 /* not yet implemented */
234 rc = value_cmp( a->a_vals[i], &ava->ava_value, a->a_syntax,
239 case LDAP_FILTER_EQUALITY:
240 case LDAP_FILTER_APPROX:
242 return LDAP_COMPARE_TRUE;
248 return LDAP_COMPARE_TRUE;
254 return LDAP_COMPARE_TRUE;
261 return( LDAP_COMPARE_FALSE );
266 test_presence_filter(
271 #ifdef SLAPD_SCHEMA_NOT_COMPAT
272 AttributeDescription *desc
278 if ( be != NULL && ! access_allowed( be, conn, op, e,
279 desc, NULL, ACL_SEARCH ) )
284 #ifdef SLAPD_SCHEMA_NOT_COMPAT
285 return attrs_find( e->e_attrs, desc ) != NULL
287 return attr_find( e->e_attrs, desc ) != NULL
289 ? LDAP_COMPARE_TRUE : LDAP_COMPARE_FALSE;
292 #ifndef SLAPD_SCHEMA_NOT_COMPAT
302 char *w1, *w2, *c1, *c2;
306 if ( be != NULL && ! access_allowed( be, conn, op, e,
307 ava->ava_type, NULL, ACL_SEARCH ) )
312 a = attr_find( e->e_attrs, ava->ava_type );
314 /* for each value in the attribute */
315 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
317 * try to match words in the filter value in order
318 * in the attribute value.
321 w2 = a->a_vals[i]->bv_val;
322 /* for each word in the filter value */
323 for ( w1 = first_word( ava->ava_value.bv_val ); w1 != NULL;
324 w1 = next_word( w1 ) ) {
325 if ( (c1 = phonetic( w1 )) == NULL ) {
330 * for each word in the attribute value from
331 * where we left off...
333 for ( w2 = first_word( w2 ); w2 != NULL;
334 w2 = next_word( w2 ) ) {
336 if ( strcmp( c1, c2 ) == 0 ) {
345 * if we stopped because we ran out of words
346 * before making a match, go on to the next
347 * value. otherwise try to keep matching
348 * words in this value from where we left off.
353 w2 = next_word( w2 );
357 * if we stopped because we ran out of words we
361 return LDAP_COMPARE_TRUE;
366 return LDAP_COMPARE_FALSE;
380 int rtn = LDAP_COMPARE_TRUE;
382 Debug( LDAP_DEBUG_FILTER, "=> test_filter_and\n", 0, 0, 0 );
384 for ( f = flist; f != NULL; f = f->f_next ) {
385 int rc = test_filter( be, conn, op, e, f );
387 if ( rc == LDAP_COMPARE_FALSE ) {
388 rtn = LDAP_COMPARE_FALSE;
391 if ( rc != LDAP_COMPARE_TRUE ) {
396 Debug( LDAP_DEBUG_FILTER, "<= test_filter_and %d\n", rtn, 0, 0 );
410 int rtn = LDAP_COMPARE_FALSE;
412 Debug( LDAP_DEBUG_FILTER, "=> test_filter_or\n", 0, 0, 0 );
414 for ( f = flist; f != NULL; f = f->f_next ) {
415 int rc = test_filter( be, conn, op, e, f );
417 if ( rc == LDAP_COMPARE_TRUE ) {
418 rtn = LDAP_COMPARE_TRUE;
421 if ( rc != LDAP_COMPARE_TRUE ) {
426 Debug( LDAP_DEBUG_FILTER, "<= test_filter_or %d\n", rtn, 0, 0 );
430 #ifndef SLAPD_SCHEMA_NOT_COMPAT
432 strcpy_regex( char *d, char *s )
461 test_substring_filter(
469 #ifndef SLAPD_SCHEMA_NOT_COMPAT
472 char *p, *end, *realval, *tmp;
478 Debug( LDAP_DEBUG_FILTER, "begin test_substring_filter\n", 0, 0, 0 );
480 if ( be != NULL && ! access_allowed( be, conn, op, e,
481 f->f_sub_type, NULL, ACL_SEARCH ) )
486 if ( (a = attr_find( e->e_attrs, f->f_sub_type )) == NULL ) {
487 return LDAP_COMPARE_FALSE;
490 if ( a->a_syntax & SYNTAX_BIN ) {
491 Debug( LDAP_DEBUG_FILTER, "test_substring_filter bin attr\n",
497 * construct a regular expression corresponding to the
498 * filter and let regex do the work
503 end = pat + sizeof(pat) - 2; /* leave room for null */
504 if ( f->f_sub_initial != NULL ) {
506 p = strchr( p, '\0' );
507 /* 2 * in case every char is special */
508 if ( p + 2 * f->f_sub_initial->bv_len > end ) {
509 Debug( LDAP_DEBUG_ANY, "not enough pattern space\n",
513 strcpy_regex( p, f->f_sub_initial->bv_val );
514 p = strchr( p, '\0' );
516 if ( f->f_sub_any != NULL ) {
517 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
519 if ( p + 2 * f->f_sub_any[i]->bv_len + 2 > end ) {
520 Debug( LDAP_DEBUG_ANY,
521 "not enough pattern space\n", 0, 0, 0 );
525 p = strchr( p, '\0' );
526 strcpy_regex( p, f->f_sub_any[i]->bv_val );
527 p = strchr( p, '\0' );
530 if ( f->f_sub_final != NULL ) {
532 if ( p + 2 * f->f_sub_final->bv_len + 2 > end ) {
533 Debug( LDAP_DEBUG_ANY, "not enough pattern space\n",
538 p = strchr( p, '\0' );
539 strcpy_regex( p, f->f_sub_final->bv_val );
540 p = strchr( p, '\0' );
544 /* compile the regex */
545 Debug( LDAP_DEBUG_FILTER, "test_substring_filter: regcomp pat: %s\n",
547 if ((rc = regcomp(&re, pat, REG_EXTENDED|REG_NOSUB))) {
550 regerror(rc, &re, error, sizeof(error));
551 Debug( LDAP_DEBUG_ANY, "regcomp failed (%s) %s\n",
556 /* for each value in the attribute see if regex matches */
557 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
560 if ( val->bv_len < sizeof(buf) ) {
561 strcpy( buf, val->bv_val );
564 tmp = (char *) ch_malloc( val->bv_len + 1 );
565 strcpy( tmp, val->bv_val );
569 value_normalize( realval, a->a_syntax );
571 rc = !regexec(&re, realval, 0, NULL, 0);
578 return LDAP_COMPARE_TRUE;
585 Debug( LDAP_DEBUG_FILTER, "end test_substring_filter 1\n", 0, 0, 0 );
586 return LDAP_COMPARE_FALSE;