--- servers/slapd/filter.c 2008/02/11 23:24:16 1.125.2.13 +++ servers/slapd/filter.c 2005/01/01 19:49:49 1.126 @@ -1,8 +1,8 @@ /* filter.c - routines for parsing and dealing with filters */ -/* $OpenLDAP: pkg/ldap/servers/slapd/filter.c,v 1.125.2.12 2007/10/05 06:36:24 ando Exp $ */ +/* $OpenLDAP: pkg/ldap/servers/slapd/filter.c,v 1.125 2004/11/25 21:59:01 hyc Exp $ */ /* This work is part of OpenLDAP Software . * - * Copyright 1998-2008 The OpenLDAP Foundation. + * Copyright 1998-2005 The OpenLDAP Foundation. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -32,7 +32,6 @@ #include #include "slap.h" -#include "lutil.h" static int get_filter_list( Operation *op, @@ -46,6 +45,11 @@ static int get_ssa( SubstringsAssertion **s, const char **text ); +static int filter_escape_value_x( + struct berval *in, + struct berval *out, + void *ctx ); + static void simple_vrFilter2bv( Operation *op, ValuesReturnFilter *f, @@ -167,22 +171,17 @@ get_filter( err = slap_bv2ad( &type, &f.f_desc, text ); if( err != LDAP_SUCCESS ) { - err = slap_bv2undef_ad( &type, &f.f_desc, text, - SLAP_AD_PROXIED|SLAP_AD_NOINSERT ); - - if ( err != LDAP_SUCCESS ) { - /* unrecognized attribute description or other error */ - Debug( LDAP_DEBUG_ANY, - "get_filter: conn %lu unknown attribute " - "type=%s (%d)\n", - op->o_connid, type.bv_val, err ); + /* unrecognized attribute description or other error */ + Debug( LDAP_DEBUG_ANY, + "get_filter: conn %d unknown attribute " + "type=%s (%d)\n", + op->o_connid, type.bv_val, err ); - f.f_choice = SLAPD_FILTER_COMPUTED; - f.f_result = LDAP_COMPARE_FALSE; - err = LDAP_SUCCESS; - *text = NULL; - break; - } + f.f_choice = SLAPD_FILTER_COMPUTED; + f.f_result = LDAP_COMPARE_FALSE; + err = LDAP_SUCCESS; + *text = NULL; + break; } assert( f.f_desc != NULL ); @@ -349,22 +348,17 @@ get_ssa( rc = slap_bv2ad( &desc, &ssa.sa_desc, text ); if( rc != LDAP_SUCCESS ) { - rc = slap_bv2undef_ad( &desc, &ssa.sa_desc, text, - SLAP_AD_PROXIED|SLAP_AD_NOINSERT ); - - if( rc != LDAP_SUCCESS ) { - Debug( LDAP_DEBUG_ANY, - "get_ssa: conn %lu unknown attribute type=%s (%ld)\n", - op->o_connid, desc.bv_val, (long) rc ); - - /* skip over the rest of this filter */ - for ( tag = ber_first_element( ber, &len, &last ); - tag != LBER_DEFAULT; - tag = ber_next_element( ber, &len, last ) ) { - ber_scanf( ber, "x" ); - } - return rc; + Debug( LDAP_DEBUG_ANY, + "get_ssa: conn %d unknown attribute type=%s (%d)\n", + op->o_connid, desc.bv_val, rc ); + + /* skip over the rest of this filter */ + for ( tag = ber_first_element( ber, &len, &last ); + tag != LBER_DEFAULT; + tag = ber_next_element( ber, &len, last ) ) { + ber_scanf( ber, "x" ); } + return rc; } rc = LDAP_PROTOCOL_ERROR; @@ -388,39 +382,24 @@ get_ssa( switch ( tag ) { case LDAP_SUBSTRING_INITIAL: - if ( ssa.sa_initial.bv_val != NULL - || ssa.sa_any != NULL - || ssa.sa_final.bv_val != NULL ) - { - rc = LDAP_PROTOCOL_ERROR; - goto return_error; - } usage = SLAP_MR_SUBSTR_INITIAL; break; case LDAP_SUBSTRING_ANY: - if ( ssa.sa_final.bv_val != NULL ) { - rc = LDAP_PROTOCOL_ERROR; - goto return_error; - } usage = SLAP_MR_SUBSTR_ANY; break; case LDAP_SUBSTRING_FINAL: - if ( ssa.sa_final.bv_val != NULL ) { - rc = LDAP_PROTOCOL_ERROR; - goto return_error; - } - usage = SLAP_MR_SUBSTR_FINAL; break; default: + rc = LDAP_PROTOCOL_ERROR; + Debug( LDAP_DEBUG_FILTER, " unknown substring choice=%ld\n", (long) tag, 0, 0 ); - rc = LDAP_PROTOCOL_ERROR; goto return_error; } @@ -428,28 +407,57 @@ get_ssa( rc = asserted_value_validate_normalize( ssa.sa_desc, ssa.sa_desc->ad_type->sat_equality, usage, &value, &nvalue, text, op->o_tmpmemctx ); - if( rc != LDAP_SUCCESS ) goto return_error; + + if( rc != LDAP_SUCCESS ) { + goto return_error; + } + + rc = LDAP_PROTOCOL_ERROR; switch ( tag ) { case LDAP_SUBSTRING_INITIAL: Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 ); + + if ( ssa.sa_initial.bv_val != NULL + || ssa.sa_any != NULL + || ssa.sa_final.bv_val != NULL ) + { + slap_sl_free( nvalue.bv_val, op->o_tmpmemctx ); + goto return_error; + } + ssa.sa_initial = nvalue; break; case LDAP_SUBSTRING_ANY: Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 ); + + if ( ssa.sa_final.bv_val != NULL ) { + slap_sl_free( nvalue.bv_val, op->o_tmpmemctx ); + goto return_error; + } + ber_bvarray_add_x( &ssa.sa_any, &nvalue, op->o_tmpmemctx ); break; case LDAP_SUBSTRING_FINAL: Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 ); + + if ( ssa.sa_final.bv_val != NULL ) { + slap_sl_free( nvalue.bv_val, op->o_tmpmemctx ); + goto return_error; + } + ssa.sa_final = nvalue; break; default: + Debug( LDAP_DEBUG_FILTER, + " unknown substring type=%ld\n", + (long) tag, 0, 0 ); + assert( 0 ); slap_sl_free( nvalue.bv_val, op->o_tmpmemctx ); - rc = LDAP_PROTOCOL_ERROR; return_error: Debug( LDAP_DEBUG_FILTER, " error=%ld\n", @@ -469,6 +477,7 @@ return_error: } Debug( LDAP_DEBUG_FILTER, "end get_ssa\n", 0, 0, 0 ); + return rc /* LDAP_SUCCESS */ ; } @@ -562,14 +571,7 @@ filter2bv_x( Operation *op, Filter *f, s switch ( f->f_choice ) { case LDAP_FILTER_EQUALITY: - if ( f->f_av_desc->ad_type->sat_syntax == slap_schema.si_ad_entryUUID->ad_type->sat_syntax ) { - tmp.bv_val = op->o_tmpalloc( LDAP_LUTIL_UUIDSTR_BUFSIZE, op->o_tmpmemctx ); - tmp.bv_len = lutil_uuidstr_from_normalized( f->f_av_value.bv_val, - f->f_av_value.bv_len, tmp.bv_val, LDAP_LUTIL_UUIDSTR_BUFSIZE ); - assert( tmp.bv_len > 0 ); - } else { - filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx ); - } + filter_escape_value_x( &f->f_av_value, &tmp, op->o_tmpmemctx ); fstr->bv_len = f->f_av_desc->ad_cname.bv_len + tmp.bv_len + ( sizeof("(=)") - 1 ); @@ -577,7 +579,7 @@ filter2bv_x( Operation *op, Filter *f, s snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s=%s)", f->f_av_desc->ad_cname.bv_val, - tmp.bv_len ? tmp.bv_val : "(null)"); + tmp.bv_val ); ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); break; @@ -591,7 +593,7 @@ filter2bv_x( Operation *op, Filter *f, s snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s>=%s)", f->f_av_desc->ad_cname.bv_val, - tmp.bv_len ? tmp.bv_val : "(null)"); + tmp.bv_val ); ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); break; @@ -605,7 +607,7 @@ filter2bv_x( Operation *op, Filter *f, s snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s<=%s)", f->f_av_desc->ad_cname.bv_val, - tmp.bv_len ? tmp.bv_val : "(null)"); + tmp.bv_val ); ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); break; @@ -619,7 +621,7 @@ filter2bv_x( Operation *op, Filter *f, s snprintf( fstr->bv_val, fstr->bv_len + 1, "(%s~=%s)", f->f_av_desc->ad_cname.bv_val, - tmp.bv_len ? tmp.bv_val : "(null)"); + tmp.bv_val ); ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); break; @@ -642,7 +644,7 @@ filter2bv_x( Operation *op, Filter *f, s snprintf( &fstr->bv_val[len-2], tmp.bv_len+3, /* "(attr=" */ "%s*)", - tmp.bv_len ? tmp.bv_val : "(null)"); + tmp.bv_val ); ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); } @@ -659,7 +661,7 @@ filter2bv_x( Operation *op, Filter *f, s snprintf( &fstr->bv_val[len-1], tmp.bv_len+3, /* "(attr=[init]*[any*]" */ "%s*)", - tmp.bv_len ? tmp.bv_val : "(null)"); + tmp.bv_val ); ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); } } @@ -675,7 +677,7 @@ filter2bv_x( Operation *op, Filter *f, s snprintf( &fstr->bv_val[len-1], tmp.bv_len+3, /* "(attr=[init*][any*]" */ "%s)", - tmp.bv_len ? tmp.bv_val : "(null)"); + tmp.bv_val ); ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); } @@ -740,7 +742,7 @@ filter2bv_x( Operation *op, Filter *f, s f->f_mr_dnattrs ? ":dn" : "", f->f_mr_rule_text.bv_len ? ":" : "", f->f_mr_rule_text.bv_len ? f->f_mr_rule_text.bv_val : "", - tmp.bv_len ? tmp.bv_val : "(null)"); + tmp.bv_val ); ber_memfree_x( tmp.bv_val, op->o_tmpmemctx ); } break; @@ -785,79 +787,40 @@ filter2bv( Filter *f, struct berval *fst filter2bv_x( &op, f, fstr ); } -Filter * -filter_dup( Filter *f, void *memctx ) +static int +filter_escape_value_x( + struct berval *in, + struct berval *out, + void *ctx ) { - BerMemoryFunctions *mf = &slap_sl_mfuncs; - Filter *n; - - if ( !f ) - return NULL; - - n = mf->bmf_malloc( sizeof(Filter), memctx ); - n->f_choice = f->f_choice; - n->f_next = NULL; - - switch( f->f_choice ) { - case SLAPD_FILTER_COMPUTED: - n->f_result = f->f_result; - break; - case LDAP_FILTER_PRESENT: - n->f_desc = f->f_desc; - break; - case LDAP_FILTER_EQUALITY: - case LDAP_FILTER_GE: - case LDAP_FILTER_LE: - case LDAP_FILTER_APPROX: - /* Should this be ava_dup() ? */ - n->f_ava = mf->bmf_calloc( 1, sizeof(AttributeAssertion), memctx ); - *n->f_ava = *f->f_ava; - ber_dupbv_x( &n->f_av_value, &f->f_av_value, memctx ); - break; - case LDAP_FILTER_SUBSTRINGS: - n->f_sub = mf->bmf_calloc( 1, sizeof(SubstringsAssertion), memctx ); - n->f_sub_desc = f->f_sub_desc; - if ( !BER_BVISNULL( &f->f_sub_initial )) - ber_dupbv_x( &n->f_sub_initial, &f->f_sub_initial, memctx ); - if ( f->f_sub_any ) { - int i; - for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ); - n->f_sub_any = mf->bmf_malloc(( i+1 )*sizeof( struct berval ), - memctx ); - for ( i = 0; !BER_BVISNULL( &f->f_sub_any[i] ); i++ ) { - ber_dupbv_x( &n->f_sub_any[i], &f->f_sub_any[i], memctx ); - } - BER_BVZERO( &n->f_sub_any[i] ); - } - if ( !BER_BVISNULL( &f->f_sub_final )) - ber_dupbv_x( &n->f_sub_final, &f->f_sub_final, memctx ); - break; - case LDAP_FILTER_EXT: { - /* Should this be mra_dup() ? */ - ber_len_t length; - length = sizeof(MatchingRuleAssertion); - if ( !BER_BVISNULL( &f->f_mr_rule_text )) - length += f->f_mr_rule_text.bv_len + 1; - n->f_mra = mf->bmf_calloc( 1, length, memctx ); - *n->f_mra = *f->f_mra; - ber_dupbv_x( &n->f_mr_value, &f->f_mr_value, memctx ); - if ( !BER_BVISNULL( &f->f_mr_rule_text )) { - n->f_mr_rule_text.bv_val = (char *)(n->f_mra+1); - AC_MEMCPY(n->f_mr_rule_text.bv_val, - f->f_mr_rule_text.bv_val, f->f_mr_rule_text.bv_len ); - } - } break; - case LDAP_FILTER_AND: - case LDAP_FILTER_OR: - case LDAP_FILTER_NOT: { - Filter **p; - for ( p = &n->f_list, f = f->f_list; f; f = f->f_next ) { - *p = filter_dup( f, memctx ); - p = &(*p)->f_next; + ber_len_t i; + assert( in ); + assert( out ); + + i = in->bv_len * 3 + 1; + out->bv_val = ctx ? slap_sl_malloc( i, ctx ) : ch_malloc( i ); + out->bv_len = 0; + + for( i=0; i < in->bv_len ; i++ ) { + if( FILTER_ESCAPE(in->bv_val[i]) ) { + out->bv_val[out->bv_len++] = SLAP_ESCAPE_CHAR; + out->bv_val[out->bv_len++] = SLAP_ESCAPE_HI( in->bv_val[i] ); + out->bv_val[out->bv_len++] = SLAP_ESCAPE_LO( in->bv_val[i] ); + } else { + out->bv_val[out->bv_len++] = in->bv_val[i]; } - } break; } - return n; + + out->bv_val[out->bv_len] = '\0'; + return LDAP_SUCCESS; +} + +int +filter_escape_value( + struct berval *in, + struct berval *out ) +{ + return filter_escape_value_x( in, out, NULL ); } static int @@ -932,21 +895,16 @@ get_simple_vrFilter( err = slap_bv2ad( &type, &vrf.vrf_desc, text ); if( err != LDAP_SUCCESS ) { - err = slap_bv2undef_ad( &type, &vrf.vrf_desc, text, - SLAP_AD_PROXIED|SLAP_AD_NOINSERT ); - - if( err != LDAP_SUCCESS ) { - /* unrecognized attribute description or other error */ - Debug( LDAP_DEBUG_ANY, - "get_simple_vrFilter: conn %lu unknown " - "attribute type=%s (%d)\n", - op->o_connid, type.bv_val, err ); - - vrf.vrf_choice = SLAPD_FILTER_COMPUTED; - vrf.vrf_result = LDAP_COMPARE_FALSE; - err = LDAP_SUCCESS; - break; - } + /* unrecognized attribute description or other error */ + Debug( LDAP_DEBUG_ANY, + "get_simple_vrFilter: conn %d unknown " + "attribute type=%s (%d)\n", + op->o_connid, type.bv_val, err ); + + vrf.vrf_choice = SLAPD_FILTER_COMPUTED; + vrf.vrf_result = LDAP_COMPARE_FALSE; + err = LDAP_SUCCESS; + break; } } break; @@ -986,7 +944,7 @@ get_simple_vrFilter( } if ( err == LDAP_SUCCESS ) { - *filt = op->o_tmpalloc( sizeof vrf, op->o_tmpmemctx ); + *filt = ch_malloc( sizeof vrf ); **filt = vrf; }