version 1.16, 1999/09/08 17:06:33
|
version 1.24, 2000/02/29 22:48:01
|
Line 1
|
Line 1
|
/* filter.c - routines for parsing and dealing with filters */ |
/* filter.c - routines for parsing and dealing with filters */ |
/* $OpenLDAP$ */ |
/* $OpenLDAP: pkg/ldap/servers/slapd/filter.c,v 1.23 2000/02/14 19:57:40 kurt Exp $ */ |
/* |
/* |
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. |
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. |
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file |
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file |
Line 20 static int get_substring_filter(Connecti
|
Line 20 static int get_substring_filter(Connecti
|
int |
int |
get_filter( Connection *conn, BerElement *ber, Filter **filt, char **fstr ) |
get_filter( Connection *conn, BerElement *ber, Filter **filt, char **fstr ) |
{ |
{ |
|
ber_tag_t tag; |
ber_len_t len; |
ber_len_t len; |
int err; |
int err; |
Filter *f; |
Filter *f; |
Line 60 get_filter( Connection *conn, BerElement
|
Line 61 get_filter( Connection *conn, BerElement
|
* |
* |
*/ |
*/ |
|
|
|
tag = ber_peek_tag( ber, &len ); |
|
|
|
if( tag == LBER_ERROR ) { |
|
return SLAPD_DISCONNECT; |
|
} |
|
|
f = (Filter *) ch_malloc( sizeof(Filter) ); |
f = (Filter *) ch_malloc( sizeof(Filter) ); |
f->f_next = NULL; |
f->f_next = NULL; |
|
|
err = LDAP_SUCCESS; |
err = LDAP_SUCCESS; |
*fstr = NULL; |
*fstr = NULL; |
f->f_choice = ber_peek_tag( ber, &len ); |
f->f_choice = tag; |
|
|
switch ( f->f_choice ) { |
switch ( f->f_choice ) { |
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
/* not yet implemented */ |
|
#else |
case LDAP_FILTER_EQUALITY: |
case LDAP_FILTER_EQUALITY: |
Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 ); |
if ( (err = get_ava( ber, &f->f_ava )) == LDAP_SUCCESS ) { |
if ( (err = get_ava( ber, &f->f_ava )) == LDAP_SUCCESS ) { |
Line 106 get_filter( Connection *conn, BerElement
|
Line 116 get_filter( Connection *conn, BerElement
|
case LDAP_FILTER_PRESENT: |
case LDAP_FILTER_PRESENT: |
Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_FILTER, "PRESENT\n", 0, 0, 0 ); |
if ( ber_scanf( ber, "a", &f->f_type ) == LBER_ERROR ) { |
if ( ber_scanf( ber, "a", &f->f_type ) == LBER_ERROR ) { |
err = -1; |
err = SLAPD_DISCONNECT; |
} else { |
break; |
err = LDAP_SUCCESS; |
|
attr_normalize( f->f_type ); |
|
*fstr = ch_malloc( 5 + strlen( f->f_type ) ); |
|
sprintf( *fstr, "(%s=*)", f->f_type ); |
|
} |
} |
|
|
|
err = LDAP_SUCCESS; |
|
attr_normalize( f->f_type ); |
|
*fstr = ch_malloc( 5 + strlen( f->f_type ) ); |
|
sprintf( *fstr, "(%s=*)", f->f_type ); |
break; |
break; |
|
|
case LDAP_FILTER_APPROX: |
case LDAP_FILTER_APPROX: |
Line 124 get_filter( Connection *conn, BerElement
|
Line 135 get_filter( Connection *conn, BerElement
|
f->f_avvalue.bv_val ); |
f->f_avvalue.bv_val ); |
} |
} |
break; |
break; |
|
#endif |
|
|
case LDAP_FILTER_AND: |
case LDAP_FILTER_AND: |
Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 ); |
Line 158 get_filter( Connection *conn, BerElement
|
Line 170 get_filter( Connection *conn, BerElement
|
} |
} |
break; |
break; |
|
|
case LBER_DEFAULT: |
case LDAP_FILTER_EXT: |
Debug( LDAP_DEBUG_ANY, "decoding filter error\n", |
/* not yet implemented */ |
0, 0, 0 ); |
Debug( LDAP_DEBUG_ANY, "extensible match not yet implemented.\n", |
err = -1; |
f->f_choice, 0, 0 ); |
|
err = LDAP_PROTOCOL_ERROR; |
break; |
break; |
|
|
default: |
default: |
Line 218 get_filter_list( Connection *conn, BerEl
|
Line 231 get_filter_list( Connection *conn, BerEl
|
return( LDAP_SUCCESS ); |
return( LDAP_SUCCESS ); |
} |
} |
|
|
|
#ifndef SLAPD_SCHEMA_NOT_COMPAT |
|
|
static int |
static int |
get_substring_filter( |
get_substring_filter( |
Connection *conn, |
Connection *conn, |
Line 229 get_substring_filter(
|
Line 244 get_substring_filter(
|
ber_tag_t tag; |
ber_tag_t tag; |
ber_len_t len; |
ber_len_t len; |
ber_tag_t rc; |
ber_tag_t rc; |
char *val, *last; |
struct berval *val; |
|
char *last; |
int syntax; |
int syntax; |
|
|
Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 ); |
|
|
if ( ber_scanf( ber, "{a" /*}*/, &f->f_sub_type ) == LBER_ERROR ) { |
if ( ber_scanf( ber, "{a" /*}*/, &f->f_sub_type ) == LBER_ERROR ) { |
return( -1 ); |
return SLAPD_DISCONNECT; |
} |
} |
|
|
attr_normalize( f->f_sub_type ); |
attr_normalize( f->f_sub_type ); |
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
/* not yet implemented */ |
|
#else |
|
/* should get real syntax and see if we have a substring matching rule */ |
syntax = attr_syntax( f->f_sub_type ); |
syntax = attr_syntax( f->f_sub_type ); |
|
#endif |
|
|
f->f_sub_initial = NULL; |
f->f_sub_initial = NULL; |
f->f_sub_any = NULL; |
f->f_sub_any = NULL; |
f->f_sub_final = NULL; |
f->f_sub_final = NULL; |
|
|
*fstr = ch_malloc( strlen( f->f_sub_type ) + 3 ); |
if( fstr ) { |
sprintf( *fstr, "(%s=", f->f_sub_type ); |
*fstr = ch_malloc( strlen( f->f_sub_type ) + 3 ); |
|
sprintf( *fstr, "(%s=" /*)*/, f->f_sub_type ); |
|
} |
|
|
for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT; |
for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT; |
tag = ber_next_element( ber, &len, last ) ) |
tag = ber_next_element( ber, &len, last ) ) |
{ |
{ |
rc = ber_scanf( ber, "a", &val ); |
rc = ber_scanf( ber, "O", &val ); |
if ( rc == LBER_ERROR ) { |
if ( rc == LBER_ERROR ) { |
return( -1 ); |
rc = SLAPD_DISCONNECT; |
|
goto return_error; |
} |
} |
if ( val == NULL || *val == '\0' ) { |
|
if ( val != NULL ) { |
if ( val == NULL || val->bv_len == 0 ) { |
free( val ); |
ber_bvfree( val ); |
} |
rc = LDAP_INVALID_SYNTAX; |
return( LDAP_INVALID_SYNTAX ); |
goto return_error; |
} |
} |
value_normalize( val, syntax ); |
|
|
rc = LDAP_PROTOCOL_ERROR; |
|
|
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
/* not yet implemented */ |
|
#else |
|
/* we should call a substring syntax normalization routine */ |
|
value_normalize( val->bv_val, syntax ); |
|
/* this is bogus, value_normalize should take a berval */ |
|
val->bv_len = strlen( val->bv_val ); |
|
#endif |
|
|
switch ( tag ) { |
switch ( tag ) { |
case LDAP_SUBSTRING_INITIAL: |
case LDAP_SUBSTRING_INITIAL: |
Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 ); |
if ( f->f_sub_initial != NULL ) { |
if ( f->f_sub_initial != NULL ) { |
return( LDAP_PROTOCOL_ERROR ); |
ber_bvfree( val ); |
|
goto return_error; |
} |
} |
f->f_sub_initial = val; |
f->f_sub_initial = val; |
*fstr = ch_realloc( *fstr, strlen( *fstr ) + |
|
strlen( val ) + 1 ); |
if( fstr ) { |
strcat( *fstr, val ); |
*fstr = ch_realloc( *fstr, |
|
strlen( *fstr ) + val->bv_len + 1 ); |
|
strcat( *fstr, val->bv_val ); |
|
} |
break; |
break; |
|
|
case LDAP_SUBSTRING_ANY: |
case LDAP_SUBSTRING_ANY: |
Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 ); |
charray_add( &f->f_sub_any, val ); |
if( ber_bvecadd( &f->f_sub_any, val ) < 0 ) { |
*fstr = ch_realloc( *fstr, strlen( *fstr ) + |
ber_bvfree( val ); |
strlen( val ) + 2 ); |
goto return_error; |
strcat( *fstr, "*" ); |
} |
strcat( *fstr, val ); |
|
|
if( fstr ) { |
|
*fstr = ch_realloc( *fstr, |
|
strlen( *fstr ) + val->bv_len + 2 ); |
|
strcat( *fstr, "*" ); |
|
strcat( *fstr, val->bv_val ); |
|
} |
break; |
break; |
|
|
case LDAP_SUBSTRING_FINAL: |
case LDAP_SUBSTRING_FINAL: |
Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 ); |
if ( f->f_sub_final != NULL ) { |
if ( f->f_sub_final != NULL ) { |
return( LDAP_PROTOCOL_ERROR ); |
ber_bvfree( val ); |
|
goto return_error; |
} |
} |
f->f_sub_final = val; |
f->f_sub_final = val; |
*fstr = ch_realloc( *fstr, strlen( *fstr ) + |
|
strlen( val ) + 2 ); |
if( fstr ) { |
strcat( *fstr, "*" ); |
*fstr = ch_realloc( *fstr, |
strcat( *fstr, val ); |
strlen( *fstr ) + val->bv_len + 2 ); |
|
strcat( *fstr, "*" ); |
|
strcat( *fstr, val->bv_val ); |
|
} |
break; |
break; |
|
|
default: |
default: |
Debug( LDAP_DEBUG_FILTER, " unknown type\n", tag, 0, |
Debug( LDAP_DEBUG_FILTER, " unknown type\n", tag, 0, |
0 ); |
0 ); |
return( LDAP_PROTOCOL_ERROR ); |
|
|
ber_bvfree( val ); |
|
|
|
return_error: |
|
Debug( LDAP_DEBUG_FILTER, " error=%d\n", rc, 0, 0 ); |
|
|
|
if( fstr ) { |
|
free( *fstr ); |
|
*fstr = NULL; |
|
} |
|
|
|
ch_free( f->f_sub_type ); |
|
ber_bvfree( f->f_sub_initial ); |
|
ber_bvecfree( f->f_sub_any ); |
|
ber_bvfree( f->f_sub_final ); |
|
return rc; |
} |
} |
} |
} |
*fstr = ch_realloc( *fstr, strlen( *fstr ) + 3 ); |
|
if ( f->f_sub_final == NULL ) { |
if( fstr ) { |
strcat( *fstr, "*" ); |
*fstr = ch_realloc( *fstr, strlen( *fstr ) + 3 ); |
|
if ( f->f_sub_final == NULL ) { |
|
strcat( *fstr, "*" ); |
|
} |
|
strcat( *fstr, /*(*/ ")" ); |
} |
} |
strcat( *fstr, ")" ); |
|
|
|
Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_FILTER, "end get_substring_filter\n", 0, 0, 0 ); |
return( LDAP_SUCCESS ); |
return( LDAP_SUCCESS ); |
} |
} |
|
|
|
#endif /* not compat */ |
|
|
void |
void |
filter_free( Filter *f ) |
filter_free( Filter *f ) |
{ |
{ |
Line 319 filter_free( Filter *f )
|
Line 391 filter_free( Filter *f )
|
} |
} |
|
|
switch ( f->f_choice ) { |
switch ( f->f_choice ) { |
|
case LDAP_FILTER_PRESENT: |
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
ad_free( f->f_desc, 1 ); |
|
#else |
|
if ( f->f_type != NULL ) { |
|
free( f->f_type ); |
|
} |
|
#endif |
|
break; |
|
|
case LDAP_FILTER_EQUALITY: |
case LDAP_FILTER_EQUALITY: |
case LDAP_FILTER_GE: |
case LDAP_FILTER_GE: |
case LDAP_FILTER_LE: |
case LDAP_FILTER_LE: |
case LDAP_FILTER_APPROX: |
case LDAP_FILTER_APPROX: |
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
ava_free( f->f_ava, 1 ); |
|
#else |
ava_free( &f->f_ava, 0 ); |
ava_free( &f->f_ava, 0 ); |
|
#endif |
break; |
break; |
|
|
case LDAP_FILTER_SUBSTRINGS: |
case LDAP_FILTER_SUBSTRINGS: |
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
ad_free( f->f_sub_desc, 1 ); |
|
if ( f->f_sub_initial != NULL ) { |
|
ber_bvfree( f->f_sub_initial ); |
|
} |
|
ber_bvecfree( f->f_sub_any ); |
|
if ( f->f_sub_final != NULL ) { |
|
ber_bvfree( f->f_sub_final ); |
|
} |
|
#else |
if ( f->f_sub_type != NULL ) { |
if ( f->f_sub_type != NULL ) { |
free( f->f_sub_type ); |
free( f->f_sub_type ); |
} |
} |
if ( f->f_sub_initial != NULL ) { |
if ( f->f_sub_initial != NULL ) { |
free( f->f_sub_initial ); |
ber_bvfree( f->f_sub_initial ); |
} |
} |
charray_free( f->f_sub_any ); |
ber_bvecfree( f->f_sub_any ); |
if ( f->f_sub_final != NULL ) { |
if ( f->f_sub_final != NULL ) { |
free( f->f_sub_final ); |
ber_bvfree( f->f_sub_final ); |
} |
|
break; |
|
|
|
case LDAP_FILTER_PRESENT: |
|
if ( f->f_type != NULL ) { |
|
free( f->f_type ); |
|
} |
} |
|
#endif |
break; |
break; |
|
|
case LDAP_FILTER_AND: |
case LDAP_FILTER_AND: |
Line 354 filter_free( Filter *f )
|
Line 445 filter_free( Filter *f )
|
} |
} |
break; |
break; |
|
|
|
case SLAPD_FILTER_COMPUTED: |
|
break; |
|
|
default: |
default: |
Debug( LDAP_DEBUG_ANY, "unknown filter type %lu\n", |
Debug( LDAP_DEBUG_ANY, "unknown filter type %lu\n", |
f->f_choice, 0, 0 ); |
f->f_choice, 0, 0 ); |
break; |
break; |
} |
} |
|
|
free( f ); |
free( f ); |
} |
} |
|
|
Line 371 filter_print( Filter *f )
|
Line 466 filter_print( Filter *f )
|
Filter *p; |
Filter *p; |
|
|
if ( f == NULL ) { |
if ( f == NULL ) { |
fprintf( stderr, "NULL" ); |
fprintf( stderr, "No filter!" ); |
} |
} |
|
|
switch ( f->f_choice ) { |
switch ( f->f_choice ) { |
case LDAP_FILTER_EQUALITY: |
case LDAP_FILTER_EQUALITY: |
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
fprintf( stderr, "(%s=%s)", |
|
f->f_ava->aa_desc->ad_cname, |
|
f->f_ava->aa_value->bv_val ); |
|
#else |
fprintf( stderr, "(%s=%s)", f->f_ava.ava_type, |
fprintf( stderr, "(%s=%s)", f->f_ava.ava_type, |
f->f_ava.ava_value.bv_val ); |
f->f_ava.ava_value.bv_val ); |
|
#endif |
break; |
break; |
|
|
case LDAP_FILTER_GE: |
case LDAP_FILTER_GE: |
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
fprintf( stderr, "(%s>=%s)", |
|
f->f_ava->aa_desc->ad_cname, |
|
f->f_ava->aa_value->bv_val ); |
|
#else |
fprintf( stderr, "(%s>=%s)", f->f_ava.ava_type, |
fprintf( stderr, "(%s>=%s)", f->f_ava.ava_type, |
f->f_ava.ava_value.bv_val ); |
f->f_ava.ava_value.bv_val ); |
|
#endif |
break; |
break; |
|
|
case LDAP_FILTER_LE: |
case LDAP_FILTER_LE: |
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
fprintf( stderr, "(%s<=%s)", |
|
f->f_ava->aa_desc->ad_cname, |
|
f->f_ava->aa_value->bv_val ); |
|
#else |
fprintf( stderr, "(%s<=%s)", f->f_ava.ava_type, |
fprintf( stderr, "(%s<=%s)", f->f_ava.ava_type, |
f->f_ava.ava_value.bv_val ); |
f->f_ava.ava_value.bv_val ); |
|
#endif |
break; |
break; |
|
|
case LDAP_FILTER_APPROX: |
case LDAP_FILTER_APPROX: |
|
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
fprintf( stderr, "(%s~=%s)", |
|
f->f_ava->aa_desc->ad_cname, |
|
f->f_ava->aa_value->bv_val ); |
|
#else |
fprintf( stderr, "(%s~=%s)", f->f_ava.ava_type, |
fprintf( stderr, "(%s~=%s)", f->f_ava.ava_type, |
f->f_ava.ava_value.bv_val ); |
f->f_ava.ava_value.bv_val ); |
|
#endif |
break; |
break; |
|
|
case LDAP_FILTER_SUBSTRINGS: |
case LDAP_FILTER_SUBSTRINGS: |
fprintf( stderr, "(%s=", f->f_sub_type ); |
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
fprintf( stderr, "(%s=" /*)*/, f->f_sub_desc->ad_cname ); |
|
#else |
|
fprintf( stderr, "(%s=" /*)*/, f->f_sub_type ); |
|
#endif |
if ( f->f_sub_initial != NULL ) { |
if ( f->f_sub_initial != NULL ) { |
fprintf( stderr, "%s", f->f_sub_initial ); |
fprintf( stderr, "%s", f->f_sub_initial->bv_val ); |
} |
} |
if ( f->f_sub_any != NULL ) { |
if ( f->f_sub_any != NULL ) { |
for ( i = 0; f->f_sub_any[i] != NULL; i++ ) { |
for ( i = 0; f->f_sub_any[i] != NULL; i++ ) { |
fprintf( stderr, "*%s", f->f_sub_any[i] ); |
fprintf( stderr, "*%s", f->f_sub_any[i]->bv_val ); |
} |
} |
} |
} |
charray_free( f->f_sub_any ); |
|
if ( f->f_sub_final != NULL ) { |
if ( f->f_sub_final != NULL ) { |
fprintf( stderr, "*%s", f->f_sub_final ); |
fprintf( stderr, "*%s", f->f_sub_final->bv_val ); |
} |
} |
|
fprintf( stderr, /*(*/ ")" ); |
break; |
break; |
|
|
case LDAP_FILTER_PRESENT: |
case LDAP_FILTER_PRESENT: |
fprintf( stderr, "%s=*", f->f_type ); |
#ifdef SLAPD_SCHEMA_NOT_COMPAT |
|
fprintf( stderr, "(%s=*)", |
|
f->f_desc->ad_cname ); |
|
#else |
|
fprintf( stderr, "(%s=*)", f->f_type ); |
|
#endif |
break; |
break; |
|
|
case LDAP_FILTER_AND: |
case LDAP_FILTER_AND: |
case LDAP_FILTER_OR: |
case LDAP_FILTER_OR: |
case LDAP_FILTER_NOT: |
case LDAP_FILTER_NOT: |
fprintf( stderr, "(%c", f->f_choice == LDAP_FILTER_AND ? '&' : |
fprintf( stderr, "(%c" /*)*/, |
|
f->f_choice == LDAP_FILTER_AND ? '&' : |
f->f_choice == LDAP_FILTER_OR ? '|' : '!' ); |
f->f_choice == LDAP_FILTER_OR ? '|' : '!' ); |
for ( p = f->f_list; p != NULL; p = p->f_next ) { |
for ( p = f->f_list; p != NULL; p = p->f_next ) { |
filter_print( p ); |
filter_print( p ); |
} |
} |
fprintf( stderr, ")" ); |
fprintf( stderr, /*(*/ ")" ); |
|
break; |
|
|
|
case SLAPD_FILTER_COMPUTED: |
|
fprintf( stderr, "(%s)", |
|
f->f_result == LDAP_COMPARE_FALSE ? "false" : |
|
f->f_result == LDAP_COMPARE_TRUE ? "true" : |
|
f->f_result == SLAPD_COMPARE_UNDEFINED ? "undefined" : |
|
"error" ); |
break; |
break; |
|
|
default: |
default: |
fprintf( stderr, "unknown type %lu", f->f_choice ); |
fprintf( stderr, "(unknown filter %lu)", f->f_choice ); |
break; |
break; |
} |
} |
} |
} |