--- servers/slapd/filter.c 1999/08/02 03:29:35 1.14 +++ servers/slapd/filter.c 2000/02/06 20:09:45 1.21 @@ -1,4 +1,9 @@ /* filter.c - routines for parsing and dealing with filters */ +/* $OpenLDAP: pkg/ldap/servers/slapd/filter.c,v 1.20 2000/01/31 21:14:16 kurt Exp $ */ +/* + * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved. + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + */ #include "portable.h" @@ -63,6 +68,7 @@ get_filter( Connection *conn, BerElement f->f_choice = ber_peek_tag( ber, &len ); switch ( f->f_choice ) { +#ifndef SLAPD_SCHEMA_NOT_COMPAT case LDAP_FILTER_EQUALITY: Debug( LDAP_DEBUG_FILTER, "EQUALITY\n", 0, 0, 0 ); if ( (err = get_ava( ber, &f->f_ava )) == LDAP_SUCCESS ) { @@ -119,6 +125,7 @@ get_filter( Connection *conn, BerElement f->f_avvalue.bv_val ); } break; +#endif case LDAP_FILTER_AND: Debug( LDAP_DEBUG_FILTER, "AND\n", 0, 0, 0 ); @@ -153,6 +160,13 @@ get_filter( Connection *conn, BerElement } break; + case LDAP_FILTER_EXT: + /* not yet implemented */ + Debug( LDAP_DEBUG_ANY, "extensible match not yet implemented.\n", + f->f_choice, 0, 0 ); + err = -1; + break; + case LBER_DEFAULT: Debug( LDAP_DEBUG_ANY, "decoding filter error\n", 0, 0, 0 ); @@ -213,6 +227,8 @@ get_filter_list( Connection *conn, BerEl return( LDAP_SUCCESS ); } +#ifndef SLAPD_SCHEMA_NOT_COMPAT + static int get_substring_filter( Connection *conn, @@ -224,7 +240,8 @@ get_substring_filter( ber_tag_t tag; ber_len_t len; ber_tag_t rc; - char *val, *last; + struct berval *val; + char *last; int syntax; Debug( LDAP_DEBUG_FILTER, "begin get_substring_filter\n", 0, 0, 0 ); @@ -232,78 +249,132 @@ get_substring_filter( if ( ber_scanf( ber, "{a" /*}*/, &f->f_sub_type ) == LBER_ERROR ) { return( -1 ); } + 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 ); +#endif + f->f_sub_initial = NULL; f->f_sub_any = NULL; f->f_sub_final = NULL; - *fstr = ch_malloc( strlen( f->f_sub_type ) + 3 ); - sprintf( *fstr, "(%s=", f->f_sub_type ); + if( fstr ) { + *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; tag = ber_next_element( ber, &len, last ) ) { - rc = ber_scanf( ber, "a", &val ); + rc = ber_scanf( ber, "O", &val ); if ( rc == LBER_ERROR ) { - return( -1 ); + rc = -1; + goto return_error; } - if ( val == NULL || *val == '\0' ) { - if ( val != NULL ) { - free( val ); - } - return( LDAP_INVALID_SYNTAX ); - } - value_normalize( val, syntax ); + + if ( val == NULL || val->bv_len == 0 ) { + ber_bvfree( val ); + rc = LDAP_INVALID_SYNTAX; + goto return_error; + } + + 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 ); +#endif + + /* this is bogus, value_normalize should take a berval */ + val->bv_len = strlen( val->bv_val ); switch ( tag ) { case LDAP_SUBSTRING_INITIAL: Debug( LDAP_DEBUG_FILTER, " INITIAL\n", 0, 0, 0 ); if ( f->f_sub_initial != NULL ) { - return( LDAP_PROTOCOL_ERROR ); + ber_bvfree( val ); + goto return_error; } f->f_sub_initial = val; - *fstr = ch_realloc( *fstr, strlen( *fstr ) + - strlen( val ) + 1 ); - strcat( *fstr, val ); + + if( fstr ) { + *fstr = ch_realloc( *fstr, + strlen( *fstr ) + val->bv_len + 1 ); + strcat( *fstr, val->bv_val ); + } break; case LDAP_SUBSTRING_ANY: Debug( LDAP_DEBUG_FILTER, " ANY\n", 0, 0, 0 ); - charray_add( &f->f_sub_any, val ); - *fstr = ch_realloc( *fstr, strlen( *fstr ) + - strlen( val ) + 2 ); - strcat( *fstr, "*" ); - strcat( *fstr, val ); + charray_add( (char ***) &f->f_sub_any, (char *) val ); + + if( fstr ) { + *fstr = ch_realloc( *fstr, + strlen( *fstr ) + val->bv_len + 2 ); + strcat( *fstr, "*" ); + strcat( *fstr, val->bv_val ); + } break; case LDAP_SUBSTRING_FINAL: Debug( LDAP_DEBUG_FILTER, " FINAL\n", 0, 0, 0 ); if ( f->f_sub_final != NULL ) { - return( LDAP_PROTOCOL_ERROR ); + ber_bvfree( val ); + goto return_error; } f->f_sub_final = val; - *fstr = ch_realloc( *fstr, strlen( *fstr ) + - strlen( val ) + 2 ); - 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; default: Debug( LDAP_DEBUG_FILTER, " unknown type\n", tag, 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 ) { - strcat( *fstr, "*" ); + + if( 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 ); return( LDAP_SUCCESS ); } +#endif /* not compat */ + void filter_free( Filter *f ) { @@ -314,30 +385,49 @@ filter_free( Filter *f ) } 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_GE: case LDAP_FILTER_LE: case LDAP_FILTER_APPROX: +#ifdef SLAPD_SCHEMA_NOT_COMPAT + ava_free( f->f_ava, 1 ); +#else ava_free( &f->f_ava, 0 ); +#endif break; 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 ) { free( f->f_sub_type ); } 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 ) { - free( f->f_sub_final ); - } - break; - - case LDAP_FILTER_PRESENT: - if ( f->f_type != NULL ) { - free( f->f_type ); + ber_bvfree( f->f_sub_final ); } +#endif break; case LDAP_FILTER_AND: @@ -354,6 +444,7 @@ filter_free( Filter *f ) f->f_choice, 0, 0 ); break; } + free( f ); } @@ -393,16 +484,15 @@ filter_print( Filter *f ) case LDAP_FILTER_SUBSTRINGS: fprintf( stderr, "(%s=", f->f_sub_type ); 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 ) { 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 ) { - fprintf( stderr, "*%s", f->f_sub_final ); + fprintf( stderr, "*%s", f->f_sub_final->bv_val ); } break;