/* str2filter.c - parse an rfc 1588 string filter */
+/* $OpenLDAP$ */
+/*
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
#include "portable.h"
#include <ac/socket.h>
#include "slap.h"
+#include <ldap_pvt.h>
-static char *find_matching_paren(char *s);
-static Filter *str2list(char *str, long unsigned int ftype);
-static Filter *str2simple(char *str);
-static int str2subvals(char *val, Filter *f);
+static char *find_matching_paren( const char *s );
+static Filter *str2list( const char *str, long unsigned int ftype);
+static Filter *str2simple( const char *str);
+static int str2subvals( const char *val, Filter *f);
Filter *
-str2filter( char *str )
+str2filter( const char *str )
{
Filter *f = NULL;
- char *end;
+ char *end, *freeme;
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+ "str2filter: \"%s\"\n", str ));
+#else
Debug( LDAP_DEBUG_FILTER, "str2filter \"%s\"\n", str, 0, 0 );
+#endif
if ( str == NULL || *str == '\0' ) {
- return( NULL );
+ return NULL;
}
+ str = freeme = ch_strdup( str );
+
switch ( *str ) {
case '(':
if ( (end = find_matching_paren( str )) == NULL ) {
filter_free( f );
- return( NULL );
+ free( freeme );
+ return NULL;
}
*end = '\0';
str++;
switch ( *str ) {
case '&':
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+ "str2filter: AND\n" ));
+#else
Debug( LDAP_DEBUG_FILTER, "str2filter: AND\n",
0, 0, 0 );
+#endif
str++;
f = str2list( str, LDAP_FILTER_AND );
break;
case '|':
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+ "str2filter: OR\n" ));
+#else
Debug( LDAP_DEBUG_FILTER, "put_filter: OR\n",
0, 0, 0 );
+#endif
str++;
f = str2list( str, LDAP_FILTER_OR );
break;
case '!':
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+ "str2filter: NOT\n" ));
+#else
Debug( LDAP_DEBUG_FILTER, "put_filter: NOT\n",
0, 0, 0 );
+#endif
str++;
f = str2list( str, LDAP_FILTER_NOT );
break;
default:
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+ "str2filter: simple\n" ));
+#else
Debug( LDAP_DEBUG_FILTER, "str2filter: simple\n",
0, 0, 0 );
+#endif
f = str2simple( str );
break;
break;
default: /* assume it's a simple type=value filter */
- Debug( LDAP_DEBUG_FILTER, "str2filter: default\n", 0, 0,
- 0 );
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "filter", LDAP_LEVEL_DETAIL1,
+ "str2filter: default\n" ));
+#else
+ Debug( LDAP_DEBUG_FILTER, "str2filter: default\n",
+ 0, 0, 0 );
+#endif
f = str2simple( str );
break;
}
+ free( freeme );
return( f );
}
*/
static Filter *
-str2list( char *str, unsigned long ftype )
+str2list( const char *str, unsigned long ftype )
{
Filter *f;
Filter **fp;
char *next;
char save;
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+ "str2list: \"%s\"\n", str ));
+#else
Debug( LDAP_DEBUG_FILTER, "str2list \"%s\"\n", str, 0, 0 );
+#endif
f = (Filter *) ch_calloc( 1, sizeof(Filter) );
f->f_choice = ftype;
fp = &f->f_list;
while ( *str ) {
- while ( *str && isspace( *str ) )
+ while ( *str && isspace( (unsigned char) *str ) )
str++;
if ( *str == '\0' )
break;
}
static Filter *
-str2simple( char *str )
+str2simple( const char *str )
{
Filter *f;
char *s;
char *value, savechar;
+ int rc;
+ const char *text;
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+ "str2simple: \"%s\"\n", str ));
+#else
Debug( LDAP_DEBUG_FILTER, "str2simple \"%s\"\n", str, 0, 0 );
+#endif
if ( (s = strchr( str, '=' )) == NULL ) {
- return( NULL );
+ return NULL;
}
- value = s + 1;
- *s-- = '\0';
+ value = &s[1];
+
+ *s-- = '\0'; /* we shouldn't be mucking with str */
savechar = *s;
f = (Filter *) ch_calloc( 1, sizeof(Filter) );
f->f_choice = LDAP_FILTER_APPROX;
*s = '\0';
break;
- default:
- if ( strchr( value, '*' ) == NULL ) {
- f->f_choice = LDAP_FILTER_EQUALITY;
- } else if ( strcmp( value, "*" ) == 0 ) {
- f->f_choice = LDAP_FILTER_PRESENT;
- } else {
- f->f_choice = LDAP_FILTER_SUBSTRINGS;
- f->f_sub_type = strdup( str );
- if ( str2subvals( value, f ) != 0 ) {
+ case ':':
+ f->f_choice = LDAP_FILTER_EXT;
+ *s = '\0';
+ return NULL;
+ break;
+
+ default: {
+ char *nextstar = ldap_pvt_find_wildcard( value );
+
+ if ( nextstar == NULL ) {
filter_free( f );
*(value-1) = '=';
- return( NULL );
+ return NULL;
+
+ } else if ( *nextstar == '\0' ) {
+ f->f_choice = LDAP_FILTER_EQUALITY;
+
+ } else if ( strcmp( value, "*" ) == 0 ) {
+ f->f_choice = LDAP_FILTER_PRESENT;
+
+ } else {
+ f->f_choice = LDAP_FILTER_SUBSTRINGS;
+ f->f_sub = ch_calloc( 1, sizeof( SubstringsAssertion ) );
+
+ rc = slap_str2ad( str, &f->f_sub_desc, &text );
+ if( rc != LDAP_SUCCESS ) {
+ filter_free( f );
+ *(value-1) = '=';
+ return NULL;
+ }
+
+ if ( str2subvals( value, f ) != 0 ) {
+ filter_free( f );
+ *(value-1) = '=';
+ return NULL;
+ }
+ *(value-1) = '=';
+ return f;
}
- *(value-1) = '=';
- return( f );
- }
- break;
+ } break;
}
if ( f->f_choice == LDAP_FILTER_PRESENT ) {
- f->f_type = strdup( str );
+ rc = slap_str2ad( str, &f->f_desc, &text );
+ if( rc != LDAP_SUCCESS ) {
+ filter_free( f );
+ *(value-1) = '=';
+ return NULL;
+ }
+
} else {
- f->f_avtype = strdup( str );
- f->f_avvalue.bv_val = strdup( value );
- f->f_avvalue.bv_len = strlen( value );
+ ber_slen_t len;
+ char *tmp;
+
+ f->f_ava = ch_calloc( 1, sizeof( AttributeAssertion ) );
+ f->f_av_desc = NULL;
+ rc = slap_str2ad( str, &f->f_av_desc, &text );
+ if( rc != LDAP_SUCCESS ) {
+ filter_free( f );
+ *(value-1) = '=';
+ return NULL;
+ }
+
+ tmp = ch_strdup( value );
+ len = ldap_pvt_filter_value_unescape( tmp );
+ if( len < 0 ) {
+ filter_free( f );
+ *(value-1) = '=';
+ free( tmp );
+ return NULL;
+ }
+ ber_str2bv( tmp, 0, 0, &f->f_av_value );
}
*s = savechar;
*(value-1) = '=';
- return( f );
+
+ return f;
}
static int
-str2subvals( char *val, Filter *f )
+str2subvals( const char *in, Filter *f )
{
- char *nextstar;
+ ber_slen_t len;
+ char *nextstar, *val, *freeme;
int gotstar;
+ int final;
+
+#ifdef NEW_LOGGING
+ LDAP_LOG(( "filter", LDAP_LEVEL_ENTRY,
+ "str2subvals: \"%s\"\n", in ));
+#else
+ Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", in, 0, 0 );
+#endif
+
+ if( in == NULL ) return 0;
+
+ val = freeme = ch_strdup( in );
+ gotstar = final = 0;
+
+ while ( *val ) {
+ nextstar = ldap_pvt_find_wildcard( val );
+
+ if ( nextstar == NULL ) {
+ free( freeme );
+ return -1;
+
+ } else if( *nextstar == '\0' ) {
+ final = 1;
+
+ } else {
+ gotstar++;
+ *nextstar = '\0';
+ }
+
+ len = ldap_pvt_filter_value_unescape( val );
+ if( len < 0 ) {
+ free( freeme );
+ return -1;
+ }
- Debug( LDAP_DEBUG_FILTER, "str2subvals \"%s\"\n", val, 0, 0 );
+ if ( final ) {
+ ber_str2bv( val, 0, 1, &f->f_sub_final );
- gotstar = 0;
- while ( val != NULL && *val ) {
- if ( (nextstar = strchr( val, '*' )) != NULL )
- *nextstar++ = '\0';
+ } else if ( gotstar <= 1 ) {
+ ber_str2bv( val, 0, 1, &f->f_sub_initial );
- if ( gotstar == 0 ) {
- f->f_sub_initial = strdup( val );
- } else if ( nextstar == NULL ) {
- f->f_sub_final = strdup( val );
} else {
- charray_add( &f->f_sub_any, strdup( val ) );
+ charray_add( (char ***) &f->f_sub_any,
+ (char *) ber_bvstrdup( val ) );
}
- gotstar = 1;
- if ( nextstar != NULL )
- *(nextstar-1) = '*';
- val = nextstar;
+ val = nextstar+1;
}
- return( 0 );
+ free( freeme );
+ return 0;
}
/*
*/
static char *
-find_matching_paren( char *s )
+find_matching_paren( const char *s )
{
int balance, escape;
balance--;
}
if ( balance == 0 ) {
- return( s );
+ return (char *) s;
}
if ( *s == '\\' && ! escape )
escape = 1;
escape = 0;
}
- return( NULL );
+ return NULL;
}