1 /* filterentry.c - apply a filter to an entry */
13 static int test_filter_list(Backend *be, Connection *conn, Operation *op, Entry *e, Filter *flist, int ftype);
14 static int test_substring_filter(Backend *be, Connection *conn, Operation *op, Entry *e, Filter *f);
15 static int test_ava_filter(Backend *be, Connection *conn, Operation *op, Entry *e, Ava *ava, int type);
16 static int test_approx_filter(Backend *be, Connection *conn, Operation *op, Entry *e, Ava *ava);
17 static int test_presence_filter(Backend *be, Connection *conn, Operation *op, Entry *e, char *type);
20 * test_filter - test a filter against a single entry.
21 * returns 0 filter matched
22 * -1 filter did not match
23 * >0 an ldap error code
37 Debug( LDAP_DEBUG_FILTER, "=> test_filter\n", 0, 0, 0 );
39 switch ( f->f_choice ) {
40 case LDAP_FILTER_EQUALITY:
41 Debug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
42 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
43 LDAP_FILTER_EQUALITY );
46 case LDAP_FILTER_SUBSTRINGS:
47 Debug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
48 rc = test_substring_filter( be, conn, op, e, f );
52 Debug( LDAP_DEBUG_FILTER, " GE\n", 0, 0, 0 );
53 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
58 Debug( LDAP_DEBUG_FILTER, " LE\n", 0, 0, 0 );
59 rc = test_ava_filter( be, conn, op, e, &f->f_ava,
63 case LDAP_FILTER_PRESENT:
64 Debug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
65 rc = test_presence_filter( be, conn, op, e, f->f_type );
68 case LDAP_FILTER_APPROX:
69 Debug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
70 rc = test_approx_filter( be, conn, op, e, &f->f_ava );
74 Debug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
75 rc = test_filter_list( be, conn, op, e, f->f_and,
80 Debug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
81 rc = test_filter_list( be, conn, op, e, f->f_or,
86 Debug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
87 rc = (! test_filter( be, conn, op, e, f->f_not ) );
91 Debug( LDAP_DEBUG_ANY, " unknown filter type %lu\n",
96 Debug( LDAP_DEBUG_FILTER, "<= test_filter %d\n", rc, 0, 0 );
113 if ( be != NULL && ! access_allowed( be, conn, op, e,
114 ava->ava_type, &ava->ava_value, ACL_SEARCH ) )
119 if ( (a = attr_find( e->e_attrs, ava->ava_type )) == NULL ) {
123 if ( a->a_syntax == 0 ) {
124 a->a_syntax = attr_syntax( ava->ava_type );
126 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
127 rc = value_cmp( a->a_vals[i], &ava->ava_value, a->a_syntax,
131 case LDAP_FILTER_EQUALITY:
155 test_presence_filter(
163 if ( be != NULL && ! access_allowed( be, conn, op, e,
164 type, NULL, ACL_SEARCH ) )
169 return( attr_find( e->e_attrs, type ) != NULL ? 0 : -1 );
181 char *w1, *w2, *c1, *c2;
185 if ( be != NULL && ! access_allowed( be, conn, op, e,
186 ava->ava_type, NULL, ACL_SEARCH ) )
191 if ( (a = attr_find( e->e_attrs, ava->ava_type )) == NULL ) {
195 /* for each value in the attribute */
196 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
198 * try to match words in the filter value in order
199 * in the attribute value.
202 w2 = a->a_vals[i]->bv_val;
203 /* for each word in the filter value */
204 for ( w1 = first_word( ava->ava_value.bv_val ); w1 != NULL;
205 w1 = next_word( w1 ) ) {
206 if ( (c1 = phonetic( w1 )) == NULL ) {
211 * for each word in the attribute value from
212 * where we left off...
214 for ( w2 = first_word( w2 ); w2 != NULL;
215 w2 = next_word( w2 ) ) {
217 if ( strcmp( c1, c2 ) == 0 ) {
226 * if we stopped because we ran out of words
227 * before making a match, go on to the next
228 * value. otherwise try to keep matching
229 * words in this value from where we left off.
234 w2 = next_word( w2 );
238 * if we stopped because we ran out of words we
262 Debug( LDAP_DEBUG_FILTER, "=> test_filter_list\n", 0, 0, 0 );
265 for ( f = flist; f != NULL; f = f->f_next ) {
266 if ( test_filter( be, conn, op, e, f ) != 0 ) {
267 if ( ftype == LDAP_FILTER_AND ) {
268 Debug( LDAP_DEBUG_FILTER,
269 "<= test_filter_list 1\n", 0, 0, 0 );
277 Debug( LDAP_DEBUG_FILTER, "<= test_filter_list %d\n", nomatch, 0, 0 );
282 strcpy_special( char *d, char *s )
304 test_substring_filter(
314 char *p, *end, *realval, *tmp;
320 Debug( LDAP_DEBUG_FILTER, "begin test_substring_filter\n", 0, 0, 0 );
322 if ( be != NULL && ! access_allowed( be, conn, op, e,
323 f->f_sub_type, NULL, ACL_SEARCH ) )
328 if ( (a = attr_find( e->e_attrs, f->f_sub_type )) == NULL ) {
332 if ( a->a_syntax & SYNTAX_BIN ) {
333 Debug( LDAP_DEBUG_FILTER, "test_substring_filter bin attr\n",
339 * construct a regular expression corresponding to the
340 * filter and let regex do the work
345 end = pat + sizeof(pat) - 2; /* leave room for null */
346 if ( f->f_sub_initial != NULL ) {
348 p = strchr( p, '\0' );
349 /* 2 * in case every char is special */
350 if ( p + 2 * strlen( f->f_sub_initial ) > end ) {
351 Debug( LDAP_DEBUG_ANY, "not enough pattern space\n",
355 strcpy_special( p, f->f_sub_initial );
356 p = strchr( p, '\0' );
358 if ( f->f_sub_any != NULL ) {
359 for ( i = 0; f->f_sub_any[i] != NULL; i++ ) {
361 if ( p + 2 * strlen( f->f_sub_any[i] ) + 2 > end ) {
362 Debug( LDAP_DEBUG_ANY,
363 "not enough pattern space\n", 0, 0, 0 );
367 p = strchr( p, '\0' );
368 strcpy_special( p, f->f_sub_any[i] );
369 p = strchr( p, '\0' );
372 if ( f->f_sub_final != NULL ) {
374 if ( p + 2 * strlen( f->f_sub_final ) + 2 > end ) {
375 Debug( LDAP_DEBUG_ANY, "not enough pattern space\n",
380 p = strchr( p, '\0' );
381 strcpy_special( p, f->f_sub_final );
382 p = strchr( p, '\0' );
386 /* compile the regex */
387 Debug( LDAP_DEBUG_FILTER, "test_substring_filter: regcomp pat: %s\n",
389 if ((rc = regcomp(&re, pat, 0))) {
392 regerror(rc, &re, error, sizeof(error));
393 Debug( LDAP_DEBUG_ANY, "regcomp failed (%s) %s\n",
398 /* for each value in the attribute see if regex matches */
399 for ( i = 0; a->a_vals[i] != NULL; i++ ) {
402 if ( val->bv_len < sizeof(buf) ) {
403 strcpy( buf, val->bv_val );
406 tmp = (char *) ch_malloc( val->bv_len + 1 );
407 strcpy( tmp, val->bv_val );
410 value_normalize( realval, a->a_syntax );
412 rc = !regexec(&re, realval, 0, NULL, 0);
425 Debug( LDAP_DEBUG_FILTER, "end test_substring_filter 1\n", 0, 0, 0 );