version 1.65, 2001/12/24 05:36:45
|
version 1.66, 2001/12/26 12:47:10
|
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.64 2001/12/05 15:41:45 kurt Exp $ */ |
/* $OpenLDAP: pkg/ldap/servers/slapd/filter.c,v 1.65 2001/12/24 05:36:45 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 18 static int get_filter_list(
|
Line 18 static int get_filter_list(
|
Connection *conn, |
Connection *conn, |
BerElement *ber, |
BerElement *ber, |
Filter **f, |
Filter **f, |
char **fstr, |
struct berval *fstr, |
const char **text ); |
const char **text ); |
|
|
static int get_substring_filter( |
static int get_substring_filter( |
Connection *conn, |
Connection *conn, |
BerElement *ber, |
BerElement *ber, |
Filter *f, |
Filter *f, |
char **fstr, |
struct berval *fstr, |
const char **text ); |
const char **text ); |
|
|
static int filter_escape_value( |
static int filter_escape_value( |
Line 37 get_filter(
|
Line 37 get_filter(
|
Connection *conn, |
Connection *conn, |
BerElement *ber, |
BerElement *ber, |
Filter **filt, |
Filter **filt, |
char **fstr, |
struct berval *fstr, |
const char **text ) |
const char **text ) |
{ |
{ |
ber_tag_t tag; |
ber_tag_t tag; |
ber_len_t len; |
ber_len_t len; |
int err; |
int err; |
Filter *f; |
Filter *f; |
char *ftmp = NULL; |
struct berval ftmp = { 0, NULL }; |
struct berval escaped; |
struct berval escaped; |
|
|
#ifdef NEW_LOGGING |
#ifdef NEW_LOGGING |
Line 97 get_filter(
|
Line 97 get_filter(
|
f->f_next = NULL; |
f->f_next = NULL; |
|
|
err = LDAP_SUCCESS; |
err = LDAP_SUCCESS; |
*fstr = NULL; |
*fstr = ftmp; |
f->f_choice = tag; |
f->f_choice = tag; |
|
|
switch ( f->f_choice ) { |
switch ( f->f_choice ) { |
Line 117 get_filter(
|
Line 117 get_filter(
|
|
|
filter_escape_value( f->f_av_value, &escaped ); |
filter_escape_value( f->f_av_value, &escaped ); |
|
|
*fstr = ch_malloc( sizeof("(=)") |
fstr->bv_len = sizeof("(=)")-1 |
+ f->f_av_desc->ad_cname.bv_len |
+ f->f_av_desc->ad_cname.bv_len |
+ escaped.bv_len ); |
+ escaped.bv_len; |
|
|
sprintf( *fstr, "(%s=%s)", |
fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); |
|
|
|
sprintf( fstr->bv_val, "(%s=%s)", |
f->f_av_desc->ad_cname.bv_val, |
f->f_av_desc->ad_cname.bv_val, |
escaped.bv_val ); |
escaped.bv_val ); |
|
|
Line 152 get_filter(
|
Line 154 get_filter(
|
|
|
filter_escape_value( f->f_av_value, &escaped ); |
filter_escape_value( f->f_av_value, &escaped ); |
|
|
*fstr = ch_malloc( sizeof("(>=)") |
fstr->bv_len = sizeof("(>=)")-1 |
+ f->f_av_desc->ad_cname.bv_len |
+ f->f_av_desc->ad_cname.bv_len |
+ escaped.bv_len ); |
+ escaped.bv_len; |
|
|
|
fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); |
|
|
sprintf( *fstr, "(%s>=%s)", |
sprintf( fstr->bv_val, "(%s>=%s)", |
f->f_av_desc->ad_cname.bv_val, |
f->f_av_desc->ad_cname.bv_val, |
escaped.bv_val ); |
escaped.bv_val ); |
|
|
Line 178 get_filter(
|
Line 182 get_filter(
|
|
|
filter_escape_value( f->f_av_value, &escaped ); |
filter_escape_value( f->f_av_value, &escaped ); |
|
|
*fstr = ch_malloc( sizeof("(<=)") |
fstr->bv_len = sizeof("(<=)")-1 |
+ f->f_av_desc->ad_cname.bv_len |
+ f->f_av_desc->ad_cname.bv_len |
+ escaped.bv_len ); |
+ escaped.bv_len; |
|
|
|
fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); |
|
|
sprintf( *fstr, "(%s<=%s)", |
sprintf( fstr->bv_val, "(%s<=%s)", |
f->f_av_desc->ad_cname.bv_val, |
f->f_av_desc->ad_cname.bv_val, |
escaped.bv_val ); |
escaped.bv_val ); |
|
|
Line 211 get_filter(
|
Line 217 get_filter(
|
/* unrecognized attribute description or other error */ |
/* unrecognized attribute description or other error */ |
f->f_choice = SLAPD_FILTER_COMPUTED; |
f->f_choice = SLAPD_FILTER_COMPUTED; |
f->f_result = LDAP_COMPARE_FALSE; |
f->f_result = LDAP_COMPARE_FALSE; |
*fstr = ch_strdup( "(unrecogized=*)" ); |
ber_str2bv("(unrecognized=*)", |
|
sizeof("(unrecognized=*)")-1, 1, fstr); |
ch_free( type.bv_val ); |
ch_free( type.bv_val ); |
err = LDAP_SUCCESS; |
err = LDAP_SUCCESS; |
break; |
break; |
Line 219 get_filter(
|
Line 226 get_filter(
|
|
|
ch_free( type.bv_val ); |
ch_free( type.bv_val ); |
|
|
*fstr = ch_malloc( sizeof("(=*)") |
fstr->bv_len = sizeof("(=*)") - 1 |
+ f->f_desc->ad_cname.bv_len ); |
+ f->f_desc->ad_cname.bv_len; |
sprintf( *fstr, "(%s=*)", |
fstr->bv_val = ch_malloc( fstr->bv_len + 1); |
|
sprintf( fstr->bv_val, "(%s=*)", |
f->f_desc->ad_cname.bv_val ); |
f->f_desc->ad_cname.bv_val ); |
|
|
} break; |
} break; |
Line 240 get_filter(
|
Line 248 get_filter(
|
|
|
filter_escape_value( f->f_av_value, &escaped ); |
filter_escape_value( f->f_av_value, &escaped ); |
|
|
*fstr = ch_malloc( sizeof("(~=)") |
fstr->bv_len = sizeof("(~=)") - 1 |
+ f->f_av_desc->ad_cname.bv_len |
+ f->f_av_desc->ad_cname.bv_len |
+ escaped.bv_len ); |
+ escaped.bv_len; |
|
fstr->bv_val = ch_malloc( fstr->bv_len + 1); |
|
|
sprintf( *fstr, "(%s~=%s)", |
sprintf( fstr->bv_val, "(%s~=%s)", |
f->f_av_desc->ad_cname.bv_val, |
f->f_av_desc->ad_cname.bv_val, |
escaped.bv_val ); |
escaped.bv_val ); |
|
|
Line 262 get_filter(
|
Line 271 get_filter(
|
if ( err != LDAP_SUCCESS ) { |
if ( err != LDAP_SUCCESS ) { |
break; |
break; |
} |
} |
*fstr = ch_malloc( sizeof("(&)") |
fstr->bv_len = sizeof("(&)") - 1 + ftmp.bv_len; |
+ ( ftmp == NULL ? 0 : strlen( ftmp ) ) ); |
fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); |
sprintf( *fstr, "(&%s)", |
sprintf( fstr->bv_val, "(&%s)", |
ftmp == NULL ? "" : ftmp ); |
ftmp.bv_len ? ftmp.bv_val : "" ); |
break; |
break; |
|
|
case LDAP_FILTER_OR: |
case LDAP_FILTER_OR: |
Line 279 get_filter(
|
Line 288 get_filter(
|
if ( err != LDAP_SUCCESS ) { |
if ( err != LDAP_SUCCESS ) { |
break; |
break; |
} |
} |
*fstr = ch_malloc( sizeof("(!)") |
fstr->bv_len = sizeof("(|)") - 1 + ftmp.bv_len; |
+ ( ftmp == NULL ? 0 : strlen( ftmp ) ) ); |
fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); |
sprintf( *fstr, "(|%s)", |
sprintf( fstr->bv_val, "(|%s)", |
ftmp == NULL ? "" : ftmp ); |
ftmp.bv_len ? ftmp.bv_val : "" ); |
break; |
break; |
|
|
case LDAP_FILTER_NOT: |
case LDAP_FILTER_NOT: |
Line 297 get_filter(
|
Line 306 get_filter(
|
if ( err != LDAP_SUCCESS ) { |
if ( err != LDAP_SUCCESS ) { |
break; |
break; |
} |
} |
*fstr = ch_malloc( sizeof("(!)") |
fstr->bv_len = sizeof("(!)") - 1 + ftmp.bv_len; |
+ ( ftmp == NULL ? 0 : strlen( ftmp ) ) ); |
fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); |
sprintf( *fstr, "(!%s)", |
sprintf( fstr->bv_val, "(!%s)", |
ftmp == NULL ? "" : ftmp ); |
ftmp.bv_len ? ftmp.bv_val : "" ); |
break; |
break; |
|
|
case LDAP_FILTER_EXT: |
case LDAP_FILTER_EXT: |
Line 320 get_filter(
|
Line 329 get_filter(
|
|
|
filter_escape_value( f->f_mr_value, &escaped ); |
filter_escape_value( f->f_mr_value, &escaped ); |
|
|
*fstr = ch_malloc( sizeof("(:dn::=)") |
fstr->bv_len = sizeof("(:dn::=)") - 1 |
+ (f->f_mr_desc ? f->f_mr_desc->ad_cname.bv_len : 0) |
+ (f->f_mr_desc ? f->f_mr_desc->ad_cname.bv_len : 0) |
+ (f->f_mr_rule_text ? strlen(f->f_mr_rule_text) : 0) |
+ f->f_mr_rule_text.bv_len |
+ escaped.bv_len ); |
+ escaped.bv_len; |
|
|
sprintf( *fstr, "(%s%s%s%s:=%s)", |
fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); |
|
sprintf( fstr->bv_val, "(%s%s%s%s:=%s)", |
(f->f_mr_desc ? f->f_mr_desc->ad_cname.bv_val : ""), |
(f->f_mr_desc ? f->f_mr_desc->ad_cname.bv_val : ""), |
(f->f_mr_dnattrs ? ":dn" : ""), |
(f->f_mr_dnattrs ? ":dn" : ""), |
(f->f_mr_rule_text ? ":" : ""), |
(f->f_mr_rule_text.bv_len ? ":" : ""), |
(f->f_mr_rule_text ? f->f_mr_rule_text : ""), |
(f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : ""), |
escaped.bv_val ); |
escaped.bv_val ); |
|
|
ber_memfree( escaped.bv_val ); |
ber_memfree( escaped.bv_val ); |
Line 347 get_filter(
|
Line 357 get_filter(
|
#endif |
#endif |
f->f_choice = SLAPD_FILTER_COMPUTED; |
f->f_choice = SLAPD_FILTER_COMPUTED; |
f->f_result = SLAPD_COMPARE_UNDEFINED; |
f->f_result = SLAPD_COMPARE_UNDEFINED; |
*fstr = ch_strdup( "(undefined)" ); |
ber_str2bv( "(undefined)", sizeof("(undefined)") - 1, |
|
1, fstr ); |
break; |
break; |
} |
} |
|
|
free( ftmp ); |
free( ftmp.bv_val ); |
|
|
if ( err != LDAP_SUCCESS ) { |
if ( err != LDAP_SUCCESS ) { |
if ( *fstr != NULL ) { |
if ( fstr->bv_val != NULL ) { |
free( *fstr ); |
free( fstr->bv_val ); |
} |
} |
|
|
if( err != SLAPD_DISCONNECT ) { |
if( err != SLAPD_DISCONNECT ) { |
/* ignore error */ |
/* ignore error */ |
f->f_choice = SLAPD_FILTER_COMPUTED; |
f->f_choice = SLAPD_FILTER_COMPUTED; |
f->f_result = SLAPD_COMPARE_UNDEFINED; |
f->f_result = SLAPD_COMPARE_UNDEFINED; |
*fstr = ch_strdup( "(badfilter)" ); |
ber_str2bv( "(badfilter)", sizeof("(badfilter)") - 1, |
|
1, fstr ); |
err = LDAP_SUCCESS; |
err = LDAP_SUCCESS; |
*filt = f; |
*filt = f; |
|
|
Line 384 get_filter(
|
Line 396 get_filter(
|
|
|
static int |
static int |
get_filter_list( Connection *conn, BerElement *ber, |
get_filter_list( Connection *conn, BerElement *ber, |
Filter **f, char **fstr, |
Filter **f, struct berval *fstr, |
const char **text ) |
const char **text ) |
{ |
{ |
Filter **new; |
Filter **new; |
int err; |
int err; |
ber_tag_t tag; |
ber_tag_t tag; |
ber_len_t len; |
ber_len_t len; |
char *last, *ftmp; |
char *last; |
|
struct berval ftmp; |
|
|
#ifdef NEW_LOGGING |
#ifdef NEW_LOGGING |
LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY, |
LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY, |
Line 399 get_filter_list( Connection *conn, BerEl
|
Line 412 get_filter_list( Connection *conn, BerEl
|
#else |
#else |
Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 ); |
Debug( LDAP_DEBUG_FILTER, "begin get_filter_list\n", 0, 0, 0 ); |
#endif |
#endif |
*fstr = NULL; |
|
new = f; |
new = f; |
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 ) ) |
Line 408 get_filter_list( Connection *conn, BerEl
|
Line 420 get_filter_list( Connection *conn, BerEl
|
if ( err != LDAP_SUCCESS ) |
if ( err != LDAP_SUCCESS ) |
return( err ); |
return( err ); |
|
|
if ( *fstr == NULL ) { |
if ( !fstr->bv_len ) { |
*fstr = ftmp; |
*fstr = ftmp; |
} else { |
} else { |
*fstr = ch_realloc( *fstr, strlen( *fstr ) + |
int i = fstr->bv_len; |
strlen( ftmp ) + 1 ); |
fstr->bv_len += ftmp.bv_len; |
strcat( *fstr, ftmp ); |
fstr->bv_val = ch_realloc( fstr->bv_val, |
free( ftmp ); |
fstr->bv_len + 1 ); |
|
strcpy( fstr->bv_val+i, ftmp.bv_val ); |
|
free( ftmp.bv_val ); |
} |
} |
new = &(*new)->f_next; |
new = &(*new)->f_next; |
} |
} |
Line 434 get_substring_filter(
|
Line 448 get_substring_filter(
|
Connection *conn, |
Connection *conn, |
BerElement *ber, |
BerElement *ber, |
Filter *f, |
Filter *f, |
char **fstr, |
struct berval *fstr, |
const char **text |
const char **text |
) |
) |
{ |
{ |
Line 469 get_substring_filter(
|
Line 483 get_substring_filter(
|
ch_free( f->f_sub ); |
ch_free( f->f_sub ); |
f->f_choice = SLAPD_FILTER_COMPUTED; |
f->f_choice = SLAPD_FILTER_COMPUTED; |
f->f_result = SLAPD_COMPARE_UNDEFINED; |
f->f_result = SLAPD_COMPARE_UNDEFINED; |
*fstr = ch_strdup( "(undefined)" ); |
ber_str2bv( "(undefined)", sizeof("(undefined)")-1, 1, fstr ); |
return LDAP_SUCCESS; |
return LDAP_SUCCESS; |
} |
} |
|
|
Line 477 get_substring_filter(
|
Line 491 get_substring_filter(
|
f->f_sub_any = NULL; |
f->f_sub_any = NULL; |
f->f_sub_final = NULL; |
f->f_sub_final = NULL; |
|
|
if( fstr ) { |
fstr->bv_len = sizeof("(=" /*)*/) - 1 + |
*fstr = ch_malloc( sizeof("(=" /*)*/) + |
f->f_sub_desc->ad_cname.bv_len; |
f->f_sub_desc->ad_cname.bv_len ); |
fstr->bv_val = ch_malloc( fstr->bv_len + 1 ); |
sprintf( *fstr, "(%s=" /*)*/, f->f_sub_desc->ad_cname.bv_val ); |
sprintf( fstr->bv_val, "(%s=" /*)*/, f->f_sub_desc->ad_cname.bv_val ); |
} |
|
|
|
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 ) ) |
Line 560 get_substring_filter(
|
Line 573 get_substring_filter(
|
|
|
f->f_sub_initial = value; |
f->f_sub_initial = value; |
|
|
if( fstr ) { |
if( fstr->bv_val ) { |
|
int i = fstr->bv_len; |
filter_escape_value( value, &escaped ); |
filter_escape_value( value, &escaped ); |
*fstr = ch_realloc( *fstr, |
fstr->bv_len += escaped.bv_len; |
strlen( *fstr ) + escaped.bv_len + 1 ); |
fstr->bv_val = ch_realloc( fstr->bv_val, |
strcat( *fstr, escaped.bv_val ); |
fstr->bv_len + 1 ); |
|
strcpy( fstr->bv_val+i, escaped.bv_val ); |
ber_memfree( escaped.bv_val ); |
ber_memfree( escaped.bv_val ); |
} |
} |
break; |
break; |
Line 588 get_substring_filter(
|
Line 603 get_substring_filter(
|
goto return_error; |
goto return_error; |
} |
} |
|
|
if( fstr ) { |
if( fstr->bv_val ) { |
|
int i = fstr->bv_len; |
filter_escape_value( value, &escaped ); |
filter_escape_value( value, &escaped ); |
*fstr = ch_realloc( *fstr, |
fstr->bv_len += escaped.bv_len + 2; |
strlen( *fstr ) + escaped.bv_len + 2 ); |
fstr->bv_val = ch_realloc( fstr->bv_val, |
strcat( *fstr, "*" ); |
fstr->bv_len + 1 ); |
strcat( *fstr, escaped.bv_val ); |
strcpy( fstr->bv_val+i, "*" ); |
|
strcpy( fstr->bv_val+i+1, escaped.bv_val ); |
ber_memfree( escaped.bv_val ); |
ber_memfree( escaped.bv_val ); |
} |
} |
break; |
break; |
Line 614 get_substring_filter(
|
Line 631 get_substring_filter(
|
|
|
f->f_sub_final = value; |
f->f_sub_final = value; |
|
|
if( fstr ) { |
if( fstr->bv_val ) { |
|
int i = fstr->bv_len; |
filter_escape_value( value, &escaped ); |
filter_escape_value( value, &escaped ); |
*fstr = ch_realloc( *fstr, |
fstr->bv_len += escaped.bv_len + 2; |
strlen( *fstr ) + escaped.bv_len + 2 ); |
fstr->bv_val = ch_realloc( fstr->bv_val, |
strcat( *fstr, "*" ); |
fstr->bv_len + 1 ); |
strcat( *fstr, escaped.bv_val ); |
strcpy( fstr->bv_val+i, "*" ); |
|
strcpy( fstr->bv_val+i+1, escaped.bv_val ); |
ber_memfree( escaped.bv_val ); |
ber_memfree( escaped.bv_val ); |
} |
} |
break; |
break; |
Line 646 return_error:
|
Line 665 return_error:
|
Debug( LDAP_DEBUG_FILTER, " error=%ld\n", |
Debug( LDAP_DEBUG_FILTER, " error=%ld\n", |
(long) rc, 0, 0 ); |
(long) rc, 0, 0 ); |
#endif |
#endif |
if( fstr ) { |
if( fstr->bv_val ) { |
free( *fstr ); |
free( fstr->bv_val ); |
*fstr = NULL; |
fstr->bv_val = NULL; |
|
fstr->bv_len = 0; |
} |
} |
|
|
ber_bvfree( f->f_sub_initial ); |
ber_bvfree( f->f_sub_initial ); |
Line 659 return_error:
|
Line 679 return_error:
|
} |
} |
} |
} |
|
|
if( fstr ) { |
if( fstr->bv_val ) { |
*fstr = ch_realloc( *fstr, strlen( *fstr ) + 3 ); |
int i = fstr->bv_len; |
|
fstr->bv_len += 3; |
|
fstr->bv_val = ch_realloc( fstr->bv_val, fstr->bv_len + 3 ); |
if ( f->f_sub_final == NULL ) { |
if ( f->f_sub_final == NULL ) { |
strcat( *fstr, "*" ); |
strcpy( fstr->bv_val+i, "*" ); |
|
i++; |
} |
} |
strcat( *fstr, /*(*/ ")" ); |
strcpy( fstr->bv_val+i, /*(*/ ")" ); |
} |
} |
|
|
#ifdef NEW_LOGGING |
#ifdef NEW_LOGGING |