This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
- version 2 of the License, or (at your option) any later version.
+ version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
*/
-#include "includes.h"
-#include "ldb/include/ldb.h"
-#include <ctype.h>
-
+#include "ldb_private.h"
+#include "system/locale.h"
/*
a filter is defined by:
struct ldb_val ret;
int slen = str?strlen(str):0;
- ret.data = talloc_size(mem_ctx, slen+1);
+ ret.data = (uint8_t *)talloc_size(mem_ctx, slen+1);
ret.length = 0;
if (ret.data == NULL) return ret;
return ret;
}
+/*
+ encode a string as a RFC2254 binary string, escaping any
+ non-printable or '\' characters. This routine is suitable for use
+ in escaping user data in ldap filters.
+*/
+char *ldb_binary_encode_string(void *mem_ctx, const char *string)
+{
+ struct ldb_val val;
+ val.data = discard_const_p(uint8_t, string);
+ val.length = strlen(string);
+ return ldb_binary_encode(mem_ctx, val);
+}
+
/* find the first matching wildcard */
static char *ldb_parse_find_wildcard(char *value)
{
val++;
}
- ret[val] = NULL;
+ if (ret != NULL) {
+ ret[val] = NULL;
+ }
return ret;
}
/* retrieve attributetype name */
t = p;
+ if (*p == '@') { /* for internal attributes the first char can be @ */
+ p++;
+ }
+
while ((isascii(*p) && isalnum((unsigned char)*p)) || (*p == '-')) { /* attribute names can only be alphanums */
p++;
}
}
/* save name */
- name = talloc_memdup(mem_ctx, t, t1 - t + 1);
+ name = (char *)talloc_memdup(mem_ctx, t, t1 - t + 1);
if (name == NULL) return 0;
name[t1 - t] = '\0';
while (isspace((unsigned char)*p)) p++;
- /* retieve value */
+ /* retrieve value */
t = p;
while (*p && ((*p != ')') || ((*p == ')') && (*(p - 1) == '\\')))) p++;
- val = talloc_memdup(mem_ctx, t, p - t + 1);
+ val = (char *)talloc_memdup(mem_ctx, t, p - t + 1);
if (val == NULL) {
talloc_free(name);
return 0;
struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s)
{
if (s == NULL || *s == 0) {
- s = "(|(objectClass=*)(dn=*))";
+ s = "(|(objectClass=*)(distinguishedName=*))";
}
while (isspace((unsigned char)*s)) s++;
talloc_free(s);
return ret;
case LDB_OP_SUBSTRING:
- ret = talloc_strdup(mem_ctx, (tree->u.substring.start_with_wildcard)?"*":"");
+ ret = talloc_asprintf(mem_ctx, "(%s=%s", tree->u.substring.attr,
+ tree->u.substring.start_with_wildcard?"*":"");
if (ret == NULL) return NULL;
for (i = 0; tree->u.substring.chunks[i]; i++) {
s2 = ldb_binary_encode(mem_ctx, *(tree->u.substring.chunks[i]));
talloc_free(ret);
return NULL;
}
- s = talloc_asprintf_append(ret, "%s*", s2);
+ if (tree->u.substring.chunks[i+1] ||
+ tree->u.substring.end_with_wildcard) {
+ s = talloc_asprintf_append(ret, "%s*", s2);
+ } else {
+ s = talloc_asprintf_append(ret, "%s", s2);
+ }
if (s == NULL) {
talloc_free(ret);
return NULL;
}
ret = s;
}
- if ( ! tree->u.substring.end_with_wildcard ) {
- ret[strlen(ret) - 1] = '\0'; /* remove last wildcard */
+ s = talloc_asprintf_append(ret, ")");
+ if (s == NULL) {
+ talloc_free(ret);
+ return NULL;
}
+ ret = s;
return ret;
case LDB_OP_GREATER:
s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
return NULL;
}
+
+
+/*
+ replace any occurances of an attribute name in the parse tree with a
+ new name
+*/
+void ldb_parse_tree_attr_replace(struct ldb_parse_tree *tree,
+ const char *attr,
+ const char *replace)
+{
+ int i;
+ switch (tree->operation) {
+ case LDB_OP_AND:
+ case LDB_OP_OR:
+ for (i=0;i<tree->u.list.num_elements;i++) {
+ ldb_parse_tree_attr_replace(tree->u.list.elements[i],
+ attr, replace);
+ }
+ break;
+ case LDB_OP_NOT:
+ ldb_parse_tree_attr_replace(tree->u.isnot.child, attr, replace);
+ break;
+ case LDB_OP_EQUALITY:
+ case LDB_OP_GREATER:
+ case LDB_OP_LESS:
+ case LDB_OP_APPROX:
+ if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
+ tree->u.equality.attr = replace;
+ }
+ break;
+ case LDB_OP_SUBSTRING:
+ if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
+ tree->u.substring.attr = replace;
+ }
+ break;
+ case LDB_OP_PRESENT:
+ if (ldb_attr_cmp(tree->u.present.attr, attr) == 0) {
+ tree->u.present.attr = replace;
+ }
+ break;
+ case LDB_OP_EXTENDED:
+ if (tree->u.extended.attr &&
+ ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
+ tree->u.extended.attr = replace;
+ }
+ break;
+ }
+}