1 /* filterentry.c - apply a filter to an entry */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/socket.h>
12 #include <ac/string.h>
17 static int test_filter_list(Backend *be, Connection *conn, Operation *op, Entry *e, Filter *flist, int ftype);
18 static int test_substring_filter(Backend *be, Connection *conn, Operation *op, Entry *e, Filter *f);
19 static int test_ava_filter(Backend *be, Connection *conn, Operation *op, Entry *e, Ava *ava, int type);
20 static int test_approx_filter(Backend *be, Connection *conn, Operation *op, Entry *e, Ava *ava);
21 static int test_presence_filter(Backend *be, Connection *conn, Operation *op, Entry *e, char *type);
24 * test_filter - test a filter against a single entry.
25 * returns 0 filter matched
26 * -1 filter did not match
27 * >0 an ldap error code
41 Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
43 switch ( f->f_choice ) {
44 case LDAP_FILTER_EQUALITY:
45 Debug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
46 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
47 LDAP_FILTER_EQUALITY );
50 case LDAP_FILTER_SUBSTRINGS:
51 Debug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
52 rc = test_substring_filter( be, conn, op, e, f );
56 Debug( LDAP_DEBUG_FILTER, " GE\n", 0, 0, 0 );
57 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
62 Debug( LDAP_DEBUG_FILTER, " LE\n", 0, 0, 0 );
63 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
67 case LDAP_FILTER_PRESENT:
68 Debug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
69 rc = test_presence_filter( be, conn, op, e, f->f_type );
72 case LDAP_FILTER_APPROX:
73 Debug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
74 rc = test_approx_filter( be, conn, op, e, &f->f_ava );
78 Debug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
79 rc = test_filter_list( be, conn, op, e, f->f_and,
84 Debug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
85 rc = test_filter_list( be, conn, op, e, f->f_or,
90 Debug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
91 rc = (! test_filter( be, conn, op, e, f->f_not ) );
95 Debug( LDAP_DEBUG_ANY, " unknown filter type %lu\n",
100 Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
117 if ( be != NULL && ! access_allowed( be, conn, op, e,
118 ava->ava_type, &ava->ava_value, ACL_SEARCH ) )
123 if ( (a = attr_find( e->e_attrs, ava->ava_type )) == NULL ) {
127 if ( a->a_syntax == 0 ) {
128 a->a_syntax = attr_syntax( ava->ava_type );
130 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
131 rc = value_cmp( a->a_vals[i], &ava->ava_value, a->a_syntax,
135 case LDAP_FILTER_EQUALITY:
159 test_presence_filter(
167 if ( be != NULL && ! access_allowed( be, conn, op, e,
168 type, NULL, ACL_SEARCH ) )
173 return( attr_find( e->e_attrs, type ) != NULL ? 0 : -1 );
185 char *w1, *w2, *c1, *c2;
189 if ( be != NULL && ! access_allowed( be, conn, op, e,
190 ava->ava_type, NULL, ACL_SEARCH ) )
195 if ( (a = attr_find( e->e_attrs, ava->ava_type )) == NULL ) {
199 /* for each value in the attribute */
200 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
202 * try to match words in the filter value in order
203 * in the attribute value.
206 w2 = a->a_vals[i]->bv_val;
207 /* for each word in the filter value */
208 for ( w1 = first_word( ava->ava_value.bv_val ); w1 != NULL;
209 w1 = next_word( w1 ) ) {
210 if ( (c1 = phonetic( w1 )) == NULL ) {
215 * for each word in the attribute value from
216 * where we left off...
218 for ( w2 = first_word( w2 ); w2 != NULL;
219 w2 = next_word( w2 ) ) {
221 if ( strcmp( c1, c2 ) == 0 ) {
230 * if we stopped because we ran out of words
231 * before making a match, go on to the next
232 * value. otherwise try to keep matching
233 * words in this value from where we left off.
238 w2 = next_word( w2 );
242 * if we stopped because we ran out of words we
266 Debug( LDAP_DEBUG_FILTER, "=> test_filter_list\n", 0, 0, 0 );
269 for ( f = flist; f != NULL; f = f->f_next ) {
270 if ( test_filter( be, conn, op, e, f ) != 0 ) {
271 if ( ftype == LDAP_FILTER_AND ) {
272 Debug( LDAP_DEBUG_FILTER,
273 "<= test_filter_list 1\n", 0, 0, 0 );
281 Debug( LDAP_DEBUG_FILTER, "<= test_filter_list %d\n", nomatch, 0, 0 );
286 strcpy_regex( char *d, char *s )
314 test_substring_filter(
324 char *p, *end, *realval, *tmp;
330 Debug( LDAP_DEBUG_FILTER, "begin test_substring_filter\n", 0, 0, 0 );
332 if ( be != NULL && ! access_allowed( be, conn, op, e,
333 f->f_sub_type, NULL, ACL_SEARCH ) )
338 if ( (a = attr_find( e->e_attrs, f->f_sub_type )) == NULL ) {
342 if ( a->a_syntax & SYNTAX_BIN ) {
343 Debug( LDAP_DEBUG_FILTER, "test_substring_filter bin attr\n",
349 * construct a regular expression corresponding to the
350 * filter and let regex do the work
355 end = pat + sizeof(pat) - 2; /* leave room for null */
356 if ( f->f_sub_initial != NULL ) {
358 p = strchr( p, '\0' );
359 /* 2 * in case every char is special */
360 if ( p + 2 * strlen( f->f_sub_initial ) > end ) {
361 Debug( LDAP_DEBUG_ANY, "not enough pattern space\n",
365 strcpy_regex( p, f->f_sub_initial );
366 p = strchr( p, '\0' );
368 if ( f->f_sub_any != NULL ) {
369 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
371 if ( p + 2 * strlen( f->f_sub_any[i] ) + 2 > end ) {
372 Debug( LDAP_DEBUG_ANY,
373 "not enough pattern space\n", 0, 0, 0 );
377 p = strchr( p, '\0' );
378 strcpy_regex( p, f->f_sub_any[i] );
379 p = strchr( p, '\0' );
382 if ( f->f_sub_final != NULL ) {
384 if ( p + 2 * strlen( f->f_sub_final ) + 2 > end ) {
385 Debug( LDAP_DEBUG_ANY, "not enough pattern space\n",
390 p = strchr( p, '\0' );
391 strcpy_regex( p, f->f_sub_final );
392 p = strchr( p, '\0' );
396 /* compile the regex */
397 Debug( LDAP_DEBUG_FILTER, "test_substring_filter: regcomp pat: %s\n",
399 if ((rc = regcomp(&re, pat, REG_EXTENDED|REG_NOSUB))) {
402 regerror(rc, &re, error, sizeof(error));
403 Debug( LDAP_DEBUG_ANY, "regcomp failed (%s) %s\n",
408 /* for each value in the attribute see if regex matches */
409 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
412 if ( val->bv_len < sizeof(buf) ) {
413 strcpy( buf, val->bv_val );
416 tmp = (char *) ch_malloc( val->bv_len + 1 );
417 strcpy( tmp, val->bv_val );
420 value_normalize( realval, a->a_syntax );
422 rc = !regexec(&re, realval, 0, NULL, 0);
435 Debug( LDAP_DEBUG_FILTER, "end test_substring_filter 1\n", 0, 0, 0 );