version 1.48, 2000/06/30 15:09:26
|
version 1.49, 2000/07/01 02:57:54
|
Line 1
|
Line 1
|
/* filter.c - routines for parsing and dealing with filters */ |
/* filter.c - routines for parsing and dealing with filters */ |
/* $OpenLDAP: pkg/ldap/servers/slapd/filter.c,v 1.47 2000/06/06 17:43:19 kurt Exp $ */ |
/* $OpenLDAP: pkg/ldap/servers/slapd/filter.c,v 1.48 2000/06/30 15:09:26 kurt Exp $ */ |
/* |
/* |
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved. |
* Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved. |
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file |
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file |
Line 28 static int get_substring_filter(
|
Line 28 static int get_substring_filter(
|
char **fstr, |
char **fstr, |
const char **text ); |
const char **text ); |
|
|
|
static int escape_value( |
|
struct berval *in, |
|
struct berval *out ); |
|
|
int |
int |
get_filter( |
get_filter( |
Connection *conn, |
Connection *conn, |
Line 41 get_filter(
|
Line 45 get_filter(
|
int err; |
int err; |
Filter *f; |
Filter *f; |
char *ftmp = NULL; |
char *ftmp = NULL; |
|
struct berval escaped; |
|
|
Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_FILTER, "begin get_filter\n", 0, 0, 0 ); |
|
|
Line 102 get_filter(
|
Line 107 get_filter(
|
|
|
assert( f->f_ava != NULL ); |
assert( f->f_ava != NULL ); |
|
|
|
escape_value( f->f_av_value, &escaped ); |
|
|
*fstr = ch_malloc( sizeof("(=)") |
*fstr = ch_malloc( sizeof("(=)") |
+ f->f_av_desc->ad_cname->bv_len |
+ f->f_av_desc->ad_cname->bv_len |
+ f->f_av_value->bv_len ); |
+ escaped.bv_len ); |
|
|
sprintf( *fstr, "(%s=%s)", |
sprintf( *fstr, "(%s=%s)", |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_value->bv_val ); |
escaped.bv_val ); |
|
|
|
ber_memfree( escaped.bv_val ); |
break; |
break; |
|
|
case LDAP_FILTER_SUBSTRINGS: |
case LDAP_FILTER_SUBSTRINGS: |
Line 125 get_filter(
|
Line 133 get_filter(
|
break; |
break; |
} |
} |
|
|
|
escape_value( f->f_av_value, &escaped ); |
|
|
*fstr = ch_malloc( sizeof("(>=)") |
*fstr = ch_malloc( sizeof("(>=)") |
+ f->f_av_desc->ad_cname->bv_len |
+ f->f_av_desc->ad_cname->bv_len |
+ f->f_av_value->bv_len ); |
+ escaped.bv_len ); |
|
|
sprintf( *fstr, "(%s>=%s)", |
sprintf( *fstr, "(%s>=%s)", |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_value->bv_val ); |
escaped.bv_val ); |
|
|
|
ber_memfree( escaped.bv_val ); |
break; |
break; |
|
|
case LDAP_FILTER_LE: |
case LDAP_FILTER_LE: |
Line 144 get_filter(
|
Line 155 get_filter(
|
} |
} |
|
|
|
|
|
escape_value( f->f_av_value, &escaped ); |
|
|
*fstr = ch_malloc( sizeof("(<=)") |
*fstr = ch_malloc( sizeof("(<=)") |
+ f->f_av_desc->ad_cname->bv_len |
+ f->f_av_desc->ad_cname->bv_len |
+ f->f_av_value->bv_len ); |
+ escaped.bv_len ); |
|
|
sprintf( *fstr, "(%s<=%s)", |
sprintf( *fstr, "(%s<=%s)", |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_value->bv_val ); |
escaped.bv_val ); |
|
|
|
ber_memfree( escaped.bv_val ); |
break; |
break; |
|
|
case LDAP_FILTER_PRESENT: { |
case LDAP_FILTER_PRESENT: { |
Line 190 get_filter(
|
Line 204 get_filter(
|
break; |
break; |
} |
} |
|
|
|
escape_value( f->f_av_value, &escaped ); |
|
|
*fstr = ch_malloc( sizeof("(~=)") |
*fstr = ch_malloc( sizeof("(~=)") |
+ f->f_av_desc->ad_cname->bv_len |
+ f->f_av_desc->ad_cname->bv_len |
+ f->f_av_value->bv_len ); |
+ escaped.bv_len ); |
|
|
sprintf( *fstr, "(%s~=%s)", |
sprintf( *fstr, "(%s~=%s)", |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_value->bv_val ); |
escaped.bv_val ); |
|
|
|
ber_memfree( escaped.bv_val ); |
break; |
break; |
|
|
case LDAP_FILTER_AND: |
case LDAP_FILTER_AND: |
Line 334 get_substring_filter(
|
Line 351 get_substring_filter(
|
ber_len_t len; |
ber_len_t len; |
ber_tag_t rc; |
ber_tag_t rc; |
struct berval *value; |
struct berval *value; |
|
struct berval escaped; |
char *last; |
char *last; |
struct berval type; |
struct berval type; |
struct berval *nvalue; |
struct berval *nvalue; |
Line 433 get_substring_filter(
|
Line 451 get_substring_filter(
|
f->f_sub_initial = value; |
f->f_sub_initial = value; |
|
|
if( fstr ) { |
if( fstr ) { |
|
escape_value( value, &escaped ); |
*fstr = ch_realloc( *fstr, |
*fstr = ch_realloc( *fstr, |
strlen( *fstr ) + value->bv_len + 1 ); |
strlen( *fstr ) + escaped.bv_len + 1 ); |
strcat( *fstr, value->bv_val ); |
strcat( *fstr, escaped.bv_val ); |
|
ber_memfree( escaped.bv_val ); |
} |
} |
break; |
break; |
|
|
Line 447 get_substring_filter(
|
Line 467 get_substring_filter(
|
} |
} |
|
|
if( fstr ) { |
if( fstr ) { |
|
escape_value( value, &escaped ); |
*fstr = ch_realloc( *fstr, |
*fstr = ch_realloc( *fstr, |
strlen( *fstr ) + value->bv_len + 2 ); |
strlen( *fstr ) + escaped.bv_len + 2 ); |
strcat( *fstr, "*" ); |
strcat( *fstr, "*" ); |
strcat( *fstr, value->bv_val ); |
strcat( *fstr, escaped.bv_val ); |
|
ber_memfree( escaped.bv_val ); |
} |
} |
break; |
break; |
|
|
Line 463 get_substring_filter(
|
Line 485 get_substring_filter(
|
f->f_sub_final = value; |
f->f_sub_final = value; |
|
|
if( fstr ) { |
if( fstr ) { |
|
escape_value( value, &escaped ); |
*fstr = ch_realloc( *fstr, |
*fstr = ch_realloc( *fstr, |
strlen( *fstr ) + value->bv_len + 2 ); |
strlen( *fstr ) + escaped.bv_len + 2 ); |
strcat( *fstr, "*" ); |
strcat( *fstr, "*" ); |
strcat( *fstr, value->bv_val ); |
strcat( *fstr, escaped.bv_val ); |
|
ber_memfree( escaped.bv_val ); |
} |
} |
break; |
break; |
|
|
Line 561 filter_free( Filter *f )
|
Line 585 filter_free( Filter *f )
|
} |
} |
|
|
#ifdef LDAP_DEBUG |
#ifdef LDAP_DEBUG |
|
|
void |
void |
filter_print( Filter *f ) |
filter_print( Filter *f ) |
{ |
{ |
int i; |
int i; |
Filter *p; |
Filter *p; |
|
struct berval escaped; |
|
|
if ( f == NULL ) { |
if ( f == NULL ) { |
fprintf( stderr, "No filter!" ); |
fprintf( stderr, "No filter!" ); |
Line 574 filter_print( Filter *f )
|
Line 598 filter_print( Filter *f )
|
|
|
switch ( f->f_choice ) { |
switch ( f->f_choice ) { |
case LDAP_FILTER_EQUALITY: |
case LDAP_FILTER_EQUALITY: |
|
escape_value( f->f_av_value, &escaped ); |
fprintf( stderr, "(%s=%s)", |
fprintf( stderr, "(%s=%s)", |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_value->bv_val ); |
escaped.bv_val ); |
|
ber_memfree( escaped.bv_val ); |
break; |
break; |
|
|
case LDAP_FILTER_GE: |
case LDAP_FILTER_GE: |
|
escape_value( f->f_av_value, &escaped ); |
fprintf( stderr, "(%s>=%s)", |
fprintf( stderr, "(%s>=%s)", |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_desc->ad_cname->bv_val, |
f->f_av_value->bv_val ); |
escaped.bv_val ); |
|
ber_memfree( escaped.bv_val ); |
break; |
break; |
|
|
case LDAP_FILTER_LE: |
case LDAP_FILTER_LE: |
|
escape_value( f->f_av_value, &escaped ); |
fprintf( stderr, "(%s<=%s)", |
fprintf( stderr, "(%s<=%s)", |
f->f_ava->aa_desc->ad_cname->bv_val, |
f->f_ava->aa_desc->ad_cname->bv_val, |
f->f_ava->aa_value->bv_val ); |
escaped.bv_val ); |
|
ber_memfree( escaped.bv_val ); |
break; |
break; |
|
|
case LDAP_FILTER_APPROX: |
case LDAP_FILTER_APPROX: |
|
escape_value( f->f_av_value, &escaped ); |
fprintf( stderr, "(%s~=%s)", |
fprintf( stderr, "(%s~=%s)", |
f->f_ava->aa_desc->ad_cname->bv_val, |
f->f_ava->aa_desc->ad_cname->bv_val, |
f->f_ava->aa_value->bv_val ); |
escaped.bv_val ); |
|
ber_memfree( escaped.bv_val ); |
break; |
break; |
|
|
case LDAP_FILTER_SUBSTRINGS: |
case LDAP_FILTER_SUBSTRINGS: |
fprintf( stderr, "(%s=" /*)*/, |
fprintf( stderr, "(%s=" /*)*/, |
f->f_sub_desc->ad_cname->bv_val ); |
f->f_sub_desc->ad_cname->bv_val ); |
if ( f->f_sub_initial != NULL ) { |
if ( f->f_sub_initial != NULL ) { |
|
escape_value( f->f_sub_initial, &escaped ); |
fprintf( stderr, "%s", |
fprintf( stderr, "%s", |
f->f_sub_initial->bv_val ); |
escaped.bv_val ); |
|
ber_memfree( escaped.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++ ) { |
|
escape_value( f->f_sub_any[i], &escaped ); |
fprintf( stderr, "*%s", |
fprintf( stderr, "*%s", |
f->f_sub_any[i]->bv_val ); |
escaped.bv_val ); |
|
ber_memfree( escaped.bv_val ); |
} |
} |
} |
} |
if ( f->f_sub_final != NULL ) { |
if ( f->f_sub_final != NULL ) { |
|
escape_value( f->f_sub_final, &escaped ); |
fprintf( stderr, |
fprintf( stderr, |
"*%s", f->f_sub_final->bv_val ); |
"*%s", escaped.bv_val ); |
|
ber_memfree( escaped.bv_val ); |
} |
} |
fprintf( stderr, /*(*/ ")" ); |
fprintf( stderr, /*(*/ ")" ); |
break; |
break; |
Line 649 filter_print( Filter *f )
|
Line 687 filter_print( Filter *f )
|
} |
} |
|
|
#endif /* ldap_debug */ |
#endif /* ldap_debug */ |
|
|
|
int escape_value( |
|
struct berval *in, |
|
struct berval *out ) |
|
{ |
|
ber_len_t i; |
|
assert( in ); |
|
assert( out ); |
|
|
|
out->bv_val = (char *) ch_malloc( in->bv_len * 3 ) + 1; |
|
out->bv_len = 0; |
|
|
|
#undef NIBBLE |
|
#undef ESCAPE_LO |
|
#undef ESCAPE_HI |
|
#define NIBBLE(c) ((c)&0x0f) |
|
#define ESCAPE_LO(c) ( NIBBLE(c) + ( NIBBLE(c) < 10 ? '0' : 'A' - 10 ) ) |
|
#define ESCAPE_HI(c) ( ESCAPE_LO((c)>>4) ) |
|
|
|
for( i=0; i < in->bv_len ; i++ ) { |
|
if( FILTER_ESCAPE(in->bv_val[i]) ) { |
|
out->bv_val[out->bv_len++] = '\\'; |
|
out->bv_val[out->bv_len++] = ESCAPE_HI( in->bv_val[i] ); |
|
out->bv_val[out->bv_len++] = ESCAPE_LO( in->bv_val[i] ); |
|
} else { |
|
out->bv_val[out->bv_len++] = in->bv_val[i]; |
|
} |
|
} |
|
|
|
out->bv_val[out->bv_len] = '\0'; |
|
return LDAP_SUCCESS; |
|
} |
|
|
|
|