mirror of
https://git.openldap.org/openldap/openldap.git
synced 2025-01-06 10:46:21 +08:00
Escape filter strings
This commit is contained in:
parent
8917a2d772
commit
af611de371
@ -28,6 +28,10 @@ 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,
|
||||||
@ -41,6 +45,7 @@ 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 );
|
||||||
|
|
||||||
@ -102,14 +107,17 @@ 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:
|
||||||
@ -125,14 +133,17 @@ 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:
|
||||||
@ -144,14 +155,17 @@ 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: {
|
||||||
@ -190,14 +204,17 @@ 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:
|
||||||
@ -334,6 +351,7 @@ 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;
|
||||||
@ -433,9 +451,11 @@ 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;
|
||||||
|
|
||||||
@ -447,10 +467,12 @@ 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;
|
||||||
|
|
||||||
@ -463,10 +485,12 @@ 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;
|
||||||
|
|
||||||
@ -561,12 +585,12 @@ 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!" );
|
||||||
@ -574,45 +598,59 @@ 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;
|
||||||
@ -649,3 +687,37 @@ 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user