+/* search.c */
/* $OpenLDAP$ */
-/*
- * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
- */
-/* Portions
- * Copyright (c) 1990 Regents of the University of Michigan.
- * All rights reserved.
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2009 The OpenLDAP Foundation.
+ * All rights reserved.
*
- * search.c
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* Portions Copyright (c) 1990 Regents of the University of Michigan.
+ * All rights reserved.
*/
#include "portable.h"
static int put_substring_filter LDAP_P((
BerElement *ber,
char *type,
- char *str ));
+ char *str,
+ char *nextstar ));
static int put_filter_list LDAP_P((
BerElement *ber,
int parens, balance, escape;
/*
- * A Filter looks like this:
- * Filter ::= CHOICE {
- * and [0] SET OF Filter,
- * or [1] SET OF Filter,
- * not [2] Filter,
- * equalityMatch [3] AttributeValueAssertion,
- * substrings [4] SubstringFilter,
- * greaterOrEqual [5] AttributeValueAssertion,
- * lessOrEqual [6] AttributeValueAssertion,
- * present [7] AttributeType,
- * approxMatch [8] AttributeValueAssertion,
- * extensibleMatch [9] MatchingRuleAssertion -- LDAPv3
- * }
+ * A Filter looks like this (RFC 4511 as extended by RFC 4526):
+ * Filter ::= CHOICE {
+ * and [0] SET SIZE (0..MAX) OF filter Filter,
+ * or [1] SET SIZE (0..MAX) OF filter Filter,
+ * not [2] Filter,
+ * equalityMatch [3] AttributeValueAssertion,
+ * substrings [4] SubstringFilter,
+ * greaterOrEqual [5] AttributeValueAssertion,
+ * lessOrEqual [6] AttributeValueAssertion,
+ * present [7] AttributeDescription,
+ * approxMatch [8] AttributeValueAssertion,
+ * extensibleMatch [9] MatchingRuleAssertion,
+ * ... }
*
- * SubstringFilter ::= SEQUENCE {
- * type AttributeType,
- * SEQUENCE OF CHOICE {
- * initial [0] IA5String,
- * any [1] IA5String,
- * final [2] IA5String
- * }
- * }
+ * SubstringFilter ::= SEQUENCE {
+ * type AttributeDescription,
+ * substrings SEQUENCE SIZE (1..MAX) OF substring CHOICE {
+ * initial [0] AssertionValue, -- only once
+ * any [1] AssertionValue,
+ * final [2] AssertionValue -- only once
+ * }
+ * }
*
- * MatchingRuleAssertion ::= SEQUENCE { -- LDAPv3
- * matchingRule [1] MatchingRuleId OPTIONAL,
- * type [2] AttributeDescription OPTIONAL,
- * matchValue [3] AssertionValue,
- * dnAttributes [4] BOOLEAN DEFAULT FALSE }
+ * MatchingRuleAssertion ::= SEQUENCE {
+ * matchingRule [1] MatchingRuleId OPTIONAL,
+ * type [2] AttributeDescription OPTIONAL,
+ * matchValue [3] AssertionValue,
+ * dnAttributes [4] BOOLEAN DEFAULT FALSE }
*
- * Note: tags in a choice are always explicit
+ * Note: tags in a CHOICE are always explicit
*/
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_ARGS, "ldap_pvt_put_filter: \"%s\"\n",
- str_in ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_filter: \"%s\"\n", str_in, 0, 0 );
-#endif
freeme = LDAP_STRDUP( str_in );
if( freeme == NULL ) return LDAP_NO_MEMORY;
switch ( *str ) {
case '&':
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_DETAIL1,
- "ldap_pvt_put_filter: AND\n" ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_filter: AND\n",
0, 0, 0 );
-#endif
str = put_complex_filter( ber, str,
LDAP_FILTER_AND, 0 );
break;
case '|':
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_DETAIL1,
- "ldap_pvt_put_filter: OR\n" ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_filter: OR\n",
0, 0, 0 );
-#endif
str = put_complex_filter( ber, str,
LDAP_FILTER_OR, 0 );
break;
case '!':
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_DETAIL1,
- "ldap_pvt_put_filter: NOT\n" ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_filter: NOT\n",
0, 0, 0 );
-#endif
str = put_complex_filter( ber, str,
LDAP_FILTER_NOT, 0 );
parens--;
break;
+ case '(':
+ rc = -1;
+ goto done;
+
default:
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_DETAIL1,
- "ldap_pvt_put_filter: simple\n" ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_filter: simple\n",
0, 0, 0 );
-#endif
balance = 1;
escape = 0;
break;
case /*'('*/ ')':
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_DETAIL1,
- "ldap_pvt_put_filter: end\n" ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_filter: end\n",
0, 0, 0 );
-#endif
if ( ber_printf( ber, /*"["*/ "]" ) == -1 ) {
rc = -1;
goto done;
break;
default: /* assume it's a simple type=value filter */
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_DETAIL1,
- "ldap_pvt_put_filter: default\n" ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_filter: default\n",
0, 0, 0 );
-#endif
next = strchr( str, '\0' );
if ( put_simple_filter( ber, str ) == -1 ) {
rc = -1;
str = next;
break;
}
+ if ( !parens )
+ break;
}
- rc = parens ? -1 : 0;
+ rc = ( parens || *str ) ? -1 : 0;
done:
LDAP_FREE( freeme );
char *next = NULL;
char save;
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_ARGS,
- "put_filter_list \"%s\"\n", str ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_filter_list \"%s\"\n",
str, 0, 0 );
-#endif
while ( *str ) {
while ( *str && LDAP_SPACE( (unsigned char) *str ) ) {
ber_tag_t ftype;
int rc = -1;
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_ARGS,
- "put_simple_filter: \"%s\"\n", str ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_simple_filter: \"%s\"\n",
str, 0, 0 );
-#endif
str = LDAP_STRDUP( str );
if( str == NULL ) return -1;
break;
case ':':
- /* RFC2254 extensible filters are off the form:
+ /* RFC 4515 extensible filters are off the form:
* type [:dn] [:rule] := value
* or [:dn]:rule := value
*/
if( rule == NULL ) {
/* one colon */
- if ( strcmp(dn, "dn") == 0 ) {
+ if ( strcasecmp(dn, "dn") == 0 ) {
/* must have attribute */
if( !ldap_is_desc( str ) ) {
goto done;
/* two colons */
*rule++ = '\0';
- if ( strcmp(dn, "dn") != 0 ) {
+ if ( strcasecmp(dn, "dn") != 0 ) {
/* must have "dn" */
goto done;
}
ftype = LDAP_FILTER_PRESENT;
} else {
- rc = put_substring_filter( ber, str, value );
+ rc = put_substring_filter( ber, str, value, nextstar );
goto done;
}
} break;
}
static int
-put_substring_filter( BerElement *ber, char *type, char *val )
+put_substring_filter( BerElement *ber, char *type, char *val, char *nextstar )
{
- char *nextstar;
int gotstar = 0;
ber_tag_t ftype = LDAP_FILTER_SUBSTRINGS;
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_ARGS,
- "put_substring_filter \"%s=%s\"\n", type, val ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_substring_filter \"%s=%s\"\n",
type, val, 0 );
-#endif
if ( ber_printf( ber, "t{s{" /*"}}"*/, ftype, type ) == -1 ) {
return -1;
}
for( ; *val; val=nextstar ) {
- nextstar = ldap_pvt_find_wildcard( val );
+ if ( gotstar )
+ nextstar = ldap_pvt_find_wildcard( val );
if ( nextstar == NULL ) {
return -1;
}
-
+
if ( *nextstar == '\0' ) {
ftype = LDAP_SUBSTRING_FINAL;
} else {
if ( *val != '\0' || ftype == LDAP_SUBSTRING_ANY ) {
ber_slen_t len = ldap_pvt_filter_value_unescape( val );
- if ( len < 0 ) {
+ if ( len <= 0 ) {
return -1;
}
return 0;
}
-int
-ldap_pvt_put_vrFilter( BerElement *ber, const char *str_in )
+static int
+put_vrFilter( BerElement *ber, const char *str_in )
{
int rc;
char *freeme;
* matchingRule [1] MatchingRuleId OPTIONAL,
* type [2] AttributeDescription OPTIONAL,
* matchValue [3] AssertionValue }
+ *
+ * (Source: RFC 3876)
*/
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_ARGS, "ldap_pvt_put_vrFilter: \"%s\"\n",
- str_in ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_vrFilter: \"%s\"\n", str_in, 0, 0 );
-#endif
freeme = LDAP_STRDUP( str_in );
if( freeme == NULL ) return LDAP_NO_MEMORY;
default:
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_DETAIL1,
- "ldap_pvt_put_vrFilter: simple\n" ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_vrFilter: simple\n",
0, 0, 0 );
-#endif
balance = 1;
escape = 0;
break;
case /*'('*/ ')':
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_DETAIL1,
- "ldap_pvt_put_filter: end\n" ));
-#else
- Debug( LDAP_DEBUG_TRACE, "put_filter: end\n",
+ Debug( LDAP_DEBUG_TRACE, "put_vrFilter: end\n",
0, 0, 0 );
-#endif
if ( ber_printf( ber, /*"["*/ "]" ) == -1 ) {
rc = -1;
goto done;
break;
default: /* assume it's a simple type=value filter */
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_DETAIL1,
- "ldap_pvt_put_filter: default\n" ));
-#else
- Debug( LDAP_DEBUG_TRACE, "put_filter: default\n",
+ Debug( LDAP_DEBUG_TRACE, "put_vrFilter: default\n",
0, 0, 0 );
-#endif
next = strchr( str, '\0' );
- if ( put_simple_filter( ber, str ) == -1 ) {
+ if ( put_simple_vrFilter( ber, str ) == -1 ) {
rc = -1;
goto done;
}
}
int
-put_vrFilter( BerElement *ber, const char *str_in )
+ldap_put_vrFilter( BerElement *ber, const char *str_in )
{
int rc =0;
rc = -1;
}
- rc = ldap_pvt_put_vrFilter( ber, str_in );
+ rc = put_vrFilter( ber, str_in );
if ( ber_printf( ber, /*"{"*/ "N}" ) == -1 ) {
rc = -1;
char *next = NULL;
char save;
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_ARGS,
- "put_vrFilter_list \"%s\"\n", str ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_vrFilter_list \"%s\"\n",
str, 0, 0 );
-#endif
while ( *str ) {
while ( *str && LDAP_SPACE( (unsigned char) *str ) ) {
/* now we have "(filter)" with str pointing to it */
*next = '\0';
- if ( ldap_pvt_put_vrFilter( ber, str ) == -1 ) return -1;
+ if ( put_vrFilter( ber, str ) == -1 ) return -1;
*next = save;
str = next;
}
ber_tag_t ftype;
int rc = -1;
-#ifdef NEW_LOGGING
- LDAP_LOG (( "filter", LDAP_LEVEL_ARGS,
- "put_simple_vrFilter: \"%s\"\n", str ));
-#else
Debug( LDAP_DEBUG_TRACE, "put_simple_vrFilter: \"%s\"\n",
str, 0, 0 );
-#endif
str = LDAP_STRDUP( str );
if( str == NULL ) return -1;
{
char *rule = strchr( str, ':' );
- *rule++ = '\0';
if( rule == NULL ) {
/* must have attribute */
goto done;
}
rule = "";
-
} else {
*rule++ = '\0';
}
-
if ( *str == '\0' && ( !rule || *rule == '\0' ) ) {
/* must have either type or rule */
goto done;
ftype = LDAP_FILTER_PRESENT;
} else {
- rc = put_substring_filter( ber, str, value );
+ rc = put_substring_filter( ber, str, value, nextstar );
goto done;
}
} break;