r7527: - added a ldb_search_bytree() interface, which takes a ldb_parse_tree
authorAndrew Tridgell <tridge@samba.org>
Mon, 13 Jun 2005 09:10:17 +0000 (09:10 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:18:06 +0000 (13:18 -0500)
  instead of a search expression. This allows our ldap server to pass
  its ASN.1 parsed search expressions straight to ldb, instead of going
  via strings.

- updated all the ldb modules code to handle the new interface

- got rid of the separate ldb_parse.h now that the ldb_parse
  structures are exposed externally

- moved to C99 structure initialisation in ldb

- switched ldap server to using ldb_search_bytree()
(This used to be commit 96620ab2ee5d440bbbc51c1bc0cad9977770f897)

29 files changed:
source4/cldap_server/cldap_server.c
source4/cldap_server/netlogon.c
source4/dsdb/samdb/ldb_modules/samldb.c
source4/include/structs.h
source4/ldap_server/ldap_backend.c
source4/ldap_server/ldap_hacked_ldb.c
source4/ldap_server/ldap_simple_ldb.c
source4/lib/ldb/common/ldb.c
source4/lib/ldb/common/ldb_modules.c
source4/lib/ldb/common/ldb_msg.c
source4/lib/ldb/common/ldb_parse.c
source4/lib/ldb/include/ldb.h
source4/lib/ldb/include/ldb_parse.h [deleted file]
source4/lib/ldb/include/ldb_private.h
source4/lib/ldb/ldb_ldap/ldb_ldap.c
source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c
source4/lib/ldb/ldb_tdb/ldb_index.c
source4/lib/ldb/ldb_tdb/ldb_match.c
source4/lib/ldb/ldb_tdb/ldb_search.c
source4/lib/ldb/ldb_tdb/ldb_tdb.c
source4/lib/ldb/ldb_tdb/ldb_tdb.h
source4/lib/ldb/modules/schema.c
source4/lib/ldb/modules/skel.c
source4/lib/ldb/modules/timestamps.c
source4/libcli/cldap/cldap.c
source4/libcli/ldap/ldap.c
source4/libcli/ldap/ldap.h
source4/libcli/util/asn1.c
source4/torture/ldap/basic.c

index a15b93f91cd675730061f401395f001d59069fe1..c6eaf6395a5c536f7b95d84f00076c0adc5fb029 100644 (file)
@@ -46,10 +46,11 @@ static void cldapd_request_handler(struct cldap_socket *cldap,
        if (search->num_attributes == 1 &&
            strcasecmp(search->attributes[0], "netlogon") == 0) {
                cldapd_netlogon_request(cldap, ldap_msg->messageid,
-                                       search->filter, src_address, src_port);
+                                       search->tree, src_address, src_port);
        } else {
                DEBUG(0,("Unknown CLDAP search for '%s'\n", 
-                        ldap_msg->r.SearchRequest.filter));
+                        ldb_filter_from_tree(ldap_msg, 
+                                             ldap_msg->r.SearchRequest.tree)));
                cldap_empty_reply(cldap, ldap_msg->messageid, src_address, src_port);
        }
 }
index f4f1c226af3a5e8503347acb9b8d6047cff225d5..4ccec27d011fad70e4e2cf7e6546cc2fb6a7f3a9 100644 (file)
@@ -177,11 +177,10 @@ static NTSTATUS cldapd_netlogon_fill(struct cldapd_server *cldapd,
 */
 void cldapd_netlogon_request(struct cldap_socket *cldap, 
                             uint32_t message_id,
-                            const char *filter,
+                            struct ldb_parse_tree *tree,
                             const char *src_address, int src_port)
 {
        struct cldapd_server *cldapd = talloc_get_type(cldap->incoming.private, struct cldapd_server);
-       struct ldb_parse_tree *tree;
        int i;
        const char *domain = NULL;
        const char *host = NULL;
@@ -195,11 +194,6 @@ void cldapd_netlogon_request(struct cldap_socket *cldap,
 
        TALLOC_CTX *tmp_ctx = talloc_new(cldap);
 
-       DEBUG(5,("cldap filter='%s'\n", filter));
-
-       tree = ldb_parse_tree(tmp_ctx, filter);
-       if (tree == NULL) goto failed;
-
        if (tree->operation != LDB_OP_AND) goto failed;
 
        /* extract the query elements */
index a392f978652b8d215c9af8af7e5f67261f6bde91..5472bed1079510f4696c755817fec3925da833e0 100644 (file)
@@ -51,6 +51,14 @@ static int samldb_search(struct ldb_module *module, const char *base,
        return ldb_next_search(module, base, scope, expression, attrs, res);
 }
 
+static int samldb_search_bytree(struct ldb_module *module, const char *base,
+                               enum ldb_scope scope, struct ldb_parse_tree *tree,
+                               const char * const *attrs, struct ldb_message ***res)
+{
+       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "samldb_search\n");
+       return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
+}
+
 /*
   allocate a new id, attempting to do it atomically
   return 0 on failure, the id on success
@@ -599,15 +607,16 @@ static int samldb_destructor(void *module_ctx)
 }
 
 static const struct ldb_module_ops samldb_ops = {
-       "samldb",
-       samldb_search,
-       samldb_add_record,
-       samldb_modify_record,
-       samldb_delete_record,
-       samldb_rename_record,
-       samldb_lock,
-       samldb_unlock,
-       samldb_errstring
+       .name          = "samldb",
+       .search        = samldb_search,
+       .search_bytree = samldb_search_bytree,
+       .add_record    = samldb_add_record,
+       .modify_record = samldb_modify_record,
+       .delete_record = samldb_delete_record,
+       .rename_record = samldb_rename_record,
+       .named_lock    = samldb_lock,
+       .named_unlock  = samldb_unlock,
+       .errstring     = samldb_errstring
 };
 
 
index ddc572982d79e79ce6f73e47a41e7ea2ff8c1b06..04fdaaba15740f03e584d88829d15f0668d5eef6 100644 (file)
@@ -157,6 +157,7 @@ struct netr_LMSessionKey;
 struct ldb_val;
 struct ldb_message;
 struct ldb_context;
+struct ldb_parse_tree;
 
 struct dom_sid;
 struct security_token;
index d87ea657d7b4fcbb9ef051f345e81d84878895a7..1c2ba870189ff74c08edec9d3e8adcbe28993d6d 100644 (file)
@@ -89,7 +89,7 @@ static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
 
        DEBUG(10, ("SearchRequest"));
        DEBUGADD(10, (" basedn: %s", req->basedn));
-       DEBUGADD(10, (" filter: %s\n", req->filter));
+       DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
 
        part = ldapsrv_get_partition(call->conn, req->basedn, req->scope);
 
index 0829ae7753d7d254ac2ca44079da4cef7dc41f2c..154211ba63252470e23c3a40ab2ee070345dd880 100644 (file)
@@ -99,7 +99,7 @@ static NTSTATUS convert_values(TALLOC_CTX *mem_ctx,
 
        if (strcasecmp(attrs->name, "ncname") == 0)
        {
-               char *filter = talloc_strdup(mem_ctx, r->filter);
+               char *filter = ldb_filter_from_tree(mem_ctx, r->tree);
                struct ldb_message **res = NULL;
                int count;
                const char *dom_dn;
@@ -333,8 +333,8 @@ static NTSTATUS hacked_Search(struct ldapsrv_partition *partition, struct ldapsr
                attrs[j] = NULL;
        }
 DEBUG(0,("hacked basedn: %s\n", basedn_str));
-DEBUGADD(0,("hacked filter: %s\n", r->filter));
-       count = ldb_search(samdb, basedn_str, scope, r->filter, attrs, &res);
+DEBUGADD(0,("hacked filter: %s\n", ldb_filter_from_tree(r, r->tree)));
+       count = ldb_search_bytree(samdb, basedn_str, scope, r->tree, attrs, &res);
        talloc_steal(samdb, res);
 
        if (count < 1) {
index 905acc10f1debcc6a65c7118f961b2ea5c96831b..d6b84a37bab3ff5c40dbf666fe724313d1e27beb 100644 (file)
@@ -62,7 +62,7 @@ static NTSTATUS sldb_Search(struct ldapsrv_partition *partition, struct ldapsrv_
        VALID_DN_SYNTAX(basedn,0);
 
        DEBUG(10, ("sldb_Search: basedn: [%s]\n", basedn->dn));
-       DEBUG(10, ("sldb_Search: filter: [%s]\n", r->filter));
+       DEBUG(10, ("sldb_Search: filter: [%s]\n", ldb_filter_from_tree(call, r->tree)));
 
        switch (r->scope) {
                case LDAP_SEARCH_SCOPE_BASE:
@@ -90,7 +90,7 @@ static NTSTATUS sldb_Search(struct ldapsrv_partition *partition, struct ldapsrv_
                attrs[i] = NULL;
        }
 
-       count = ldb_search(samdb, basedn->dn, scope, r->filter, attrs, &res);
+       count = ldb_search_bytree(samdb, basedn->dn, scope, r->tree, attrs, &res);
        talloc_steal(samdb, res);
 
        for (i=0; i < count; i++) {
index 649b0a0f24d1244ff27683e49a97973f3596e732..f5c6d551ea05e6eb3fd79572a7ace05d50fda28b 100644 (file)
@@ -100,6 +100,23 @@ int ldb_search(struct ldb_context *ldb,
        return ldb->modules->ops->search(ldb->modules, base, scope, expression, attrs, res);
 }
 
+/*
+  search the database given a LDAP-like search expression
+
+  return the number of records found, or -1 on error
+
+  Use talloc_free to free the ldb_message returned in 'res'
+
+*/
+int ldb_search_bytree(struct ldb_context *ldb, 
+                     const char *base,
+                     enum ldb_scope scope,
+                     struct ldb_parse_tree *tree,
+                     const char * const *attrs, struct ldb_message ***res)
+{
+       return ldb->modules->ops->search_bytree(ldb->modules, base, scope, tree, attrs, res);
+}
+
 /*
   add a record to the database. Will fail if a record with the given class and key
   already exists
index 644154d645952f269aba14b751a6a14b9fd2317c..7df9901ae9b07588a346ef63d400d1c3a0956a6c 100644 (file)
@@ -213,10 +213,10 @@ int ldb_load_modules(struct ldb_context *ldb, const char *options[])
 */
 
 int ldb_next_search(struct ldb_module *module, 
-              const char *base,
-              enum ldb_scope scope,
-              const char *expression,
-              const char * const *attrs, struct ldb_message ***res)
+                   const char *base,
+                   enum ldb_scope scope,
+                   const char *expression,
+                   const char * const *attrs, struct ldb_message ***res)
 {
        if (!module->next) {
                return -1;
@@ -224,6 +224,18 @@ int ldb_next_search(struct ldb_module *module,
        return module->next->ops->search(module->next, base, scope, expression, attrs, res);
 }
 
+int ldb_next_search_bytree(struct ldb_module *module, 
+                          const char *base,
+                          enum ldb_scope scope,
+                          struct ldb_parse_tree *tree,
+                          const char * const *attrs, struct ldb_message ***res)
+{
+       if (!module->next) {
+               return -1;
+       }
+       return module->next->ops->search_bytree(module->next, base, scope, tree, attrs, res);
+}
+
 int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message)
 {
        if (!module->next) {
index 7a6dea049ae15fc1031bb8eba6c0d7d3a82fb084..8d921b989b377c6c3f2f69f52d01793db65423c3 100644 (file)
@@ -95,8 +95,7 @@ struct ldb_val *ldb_msg_find_val(const struct ldb_message_element *el,
 /*
   duplicate a ldb_val structure
 */
-struct ldb_val ldb_val_dup(TALLOC_CTX *mem_ctx, 
-                          const struct ldb_val *v)
+struct ldb_val ldb_val_dup(void *mem_ctx, const struct ldb_val *v)
 {
        struct ldb_val v2;
        v2.length = v->length;
index e2eb0f8c78cbe79e4ff7f659025607e41c2484ad..afcda60e8e7c2d55f8185fa99a6564f44630101a 100644 (file)
@@ -43,7 +43,6 @@
 
 #include "includes.h"
 #include "ldb/include/ldb.h"
-#include "ldb/include/ldb_parse.h"
 #include <ctype.h>
 
 
@@ -64,7 +63,7 @@ a filter is defined by:
 /*
   return next token element. Caller frees
 */
-static char *ldb_parse_lex(TALLOC_CTX *ctx, const char **s, const char *sep)
+static char *ldb_parse_lex(void *ctx, const char **s, const char *sep)
 {
        const char *p = *s;
        char *ret;
@@ -130,13 +129,13 @@ static const char *match_brace(const char *s)
    decode a RFC2254 binary string representation of a buffer.
    Used in LDAP filters.
 */
-struct ldb_val ldb_binary_decode(TALLOC_CTX *ctx, const char *str)
+struct ldb_val ldb_binary_decode(void *mem_ctx, const char *str)
 {
        int i, j;
        struct ldb_val ret;
        int slen = str?strlen(str):0;
 
-       ret.data = talloc_size(ctx, slen+1);
+       ret.data = talloc_size(mem_ctx, slen+1);
        ret.length = 0;
        if (ret.data == NULL) return ret;
 
@@ -165,7 +164,7 @@ struct ldb_val ldb_binary_decode(TALLOC_CTX *ctx, const char *str)
    encode a blob as a RFC2254 binary string, escaping any
    non-printable or '\' characters
 */
-char *ldb_binary_encode(TALLOC_CTX *ctx, struct ldb_val val)
+char *ldb_binary_encode(void *mem_ctx, struct ldb_val val)
 {
        int i;
        char *ret;
@@ -177,7 +176,7 @@ char *ldb_binary_encode(TALLOC_CTX *ctx, struct ldb_val val)
                        len += 2;
                }
        }
-       ret = talloc_array(ctx, char, len+1);
+       ret = talloc_array(mem_ctx, char, len+1);
        if (ret == NULL) return NULL;
 
        len = 0;
@@ -195,17 +194,17 @@ char *ldb_binary_encode(TALLOC_CTX *ctx, struct ldb_val val)
        return ret;     
 }
 
-static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *ctx, const char **s);
+static struct ldb_parse_tree *ldb_parse_filter(void *mem_ctx, const char **s);
 
 /*
   <simple> ::= <attributetype> <filtertype> <attributevalue>
 */
-static struct ldb_parse_tree *ldb_parse_simple(TALLOC_CTX *ctx, const char *s)
+static struct ldb_parse_tree *ldb_parse_simple(void *mem_ctx, const char *s)
 {
        char *eq, *val, *l;
        struct ldb_parse_tree *ret;
 
-       ret = talloc(ctx, struct ldb_parse_tree);
+       ret = talloc(mem_ctx, struct ldb_parse_tree);
        if (!ret) {
                errno = ENOMEM;
                return NULL;
@@ -249,12 +248,12 @@ static struct ldb_parse_tree *ldb_parse_simple(TALLOC_CTX *ctx, const char *s)
   <or> ::= '|' <filterlist>
   <filterlist> ::= <filter> | <filter> <filterlist>
 */
-static struct ldb_parse_tree *ldb_parse_filterlist(TALLOC_CTX *ctx,
+static struct ldb_parse_tree *ldb_parse_filterlist(void *mem_ctx,
                                                   enum ldb_parse_op op, const char *s)
 {
        struct ldb_parse_tree *ret, *next;
 
-       ret = talloc(ctx, struct ldb_parse_tree);
+       ret = talloc(mem_ctx, struct ldb_parse_tree);
        if (!ret) {
                errno = ENOMEM;
                return NULL;
@@ -300,11 +299,11 @@ static struct ldb_parse_tree *ldb_parse_filterlist(TALLOC_CTX *ctx,
 /*
   <not> ::= '!' <filter>
 */
-static struct ldb_parse_tree *ldb_parse_not(TALLOC_CTX *ctx, const char *s)
+static struct ldb_parse_tree *ldb_parse_not(void *mem_ctx, const char *s)
 {
        struct ldb_parse_tree *ret;
 
-       ret = talloc(ctx, struct ldb_parse_tree);
+       ret = talloc(mem_ctx, struct ldb_parse_tree);
        if (!ret) {
                errno = ENOMEM;
                return NULL;
@@ -324,39 +323,39 @@ static struct ldb_parse_tree *ldb_parse_not(TALLOC_CTX *ctx, const char *s)
   parse a filtercomp
   <filtercomp> ::= <and> | <or> | <not> | <simple>
 */
-static struct ldb_parse_tree *ldb_parse_filtercomp(TALLOC_CTX *ctx, const char *s)
+static struct ldb_parse_tree *ldb_parse_filtercomp(void *mem_ctx, const char *s)
 {
        while (isspace(*s)) s++;
 
        switch (*s) {
        case '&':
-               return ldb_parse_filterlist(ctx, LDB_OP_AND, s+1);
+               return ldb_parse_filterlist(mem_ctx, LDB_OP_AND, s+1);
 
        case '|':
-               return ldb_parse_filterlist(ctx, LDB_OP_OR, s+1);
+               return ldb_parse_filterlist(mem_ctx, LDB_OP_OR, s+1);
 
        case '!':
-               return ldb_parse_not(ctx, s+1);
+               return ldb_parse_not(mem_ctx, s+1);
 
        case '(':
        case ')':
                return NULL;
        }
 
-       return ldb_parse_simple(ctx, s);
+       return ldb_parse_simple(mem_ctx, s);
 }
 
 
 /*
   <filter> ::= '(' <filtercomp> ')'
 */
-static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *ctx, const char **s)
+static struct ldb_parse_tree *ldb_parse_filter(void *mem_ctx, const char **s)
 {
        char *l, *s2;
        const char *p, *p2;
        struct ldb_parse_tree *ret;
 
-       l = ldb_parse_lex(ctx, s, LDB_ALL_SEP);
+       l = ldb_parse_lex(mem_ctx, s, LDB_ALL_SEP);
        if (!l) {
                return NULL;
        }
@@ -373,13 +372,13 @@ static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *ctx, const char **s)
        }
        p2 = p + 1;
 
-       s2 = talloc_strndup(ctx, *s, p - *s);
+       s2 = talloc_strndup(mem_ctx, *s, p - *s);
        if (!s2) {
                errno = ENOMEM;
                return NULL;
        }
 
-       ret = ldb_parse_filtercomp(ctx, s2);
+       ret = ldb_parse_filtercomp(mem_ctx, s2);
        talloc_free(s2);
 
        *s = p2;
@@ -393,7 +392,7 @@ static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *ctx, const char **s)
 
   expression ::= <simple> | <filter>
 */
-struct ldb_parse_tree *ldb_parse_tree(TALLOC_CTX *mem_ctx, const char *s)
+struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s)
 {
        while (isspace(*s)) s++;
 
@@ -408,7 +407,7 @@ struct ldb_parse_tree *ldb_parse_tree(TALLOC_CTX *mem_ctx, const char *s)
 /*
   construct a ldap parse filter given a parse tree
 */
-char *ldb_filter_from_tree(TALLOC_CTX *mem_ctx, struct ldb_parse_tree *tree)
+char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree)
 {
        char *s, *s2, *ret;
        int i;
index 91a826447a2aef8e32dccd4bdf1b29fbc970750c..02df0ae8103087a467f769b5e2fb5fcd46548ea9 100644 (file)
@@ -145,6 +145,31 @@ struct ldb_debug_ops {
 #endif
 
 
+/* structues for ldb_parse_tree handling code */
+enum ldb_parse_op {LDB_OP_SIMPLE=1, LDB_OP_AND='&', LDB_OP_OR='|', LDB_OP_NOT='!'};
+
+struct ldb_parse_tree {
+       enum ldb_parse_op operation;
+       union {
+               struct {
+                       char *attr;
+                       struct ldb_val value;
+               } simple;
+               struct {
+                       unsigned int num_elements;
+                       struct ldb_parse_tree **elements;
+               } list;
+               struct {
+                       struct ldb_parse_tree *child;
+               } not;
+       } u;
+};
+
+struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s);
+char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree);
+char *ldb_binary_encode(void *ctx, struct ldb_val val);
+
+
 /* 
  connect to a database. The URL can either be one of the following forms
    ldb://path
@@ -171,6 +196,15 @@ int ldb_search(struct ldb_context *ldb,
               const char *expression,
               const char * const *attrs, struct ldb_message ***res);
 
+/*
+  like ldb_search() but takes a parse tree
+*/
+int ldb_search_bytree(struct ldb_context *ldb, 
+                     const char *base,
+                     enum ldb_scope scope,
+                     struct ldb_parse_tree *tree,
+                     const char * const *attrs, struct ldb_message ***res);
+
 /*
   add a record to the database. Will fail if a record with the given class and key
   already exists
diff --git a/source4/lib/ldb/include/ldb_parse.h b/source4/lib/ldb/include/ldb_parse.h
deleted file mode 100644 (file)
index c8d89d2..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/* 
-   ldb database library
-
-   Copyright (C) Andrew Tridgell  2004
-
-     ** NOTE! The following LGPL license applies to the ldb
-     ** library. This does NOT imply that all of Samba is released
-     ** under the LGPL
-   
-   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.
-
-   This library is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   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
-*/
-
-/*
- *  Name: ldb
- *
- *  Component: ldb expression parse header
- *
- *  Description: structure for expression parsing
- *
- *  Author: Andrew Tridgell
- */
-
-#ifndef _LDB_PARSE_H
-#define _LDB_PARSE_H 1
-
-enum ldb_parse_op {LDB_OP_SIMPLE=1, LDB_OP_AND='&', LDB_OP_OR='|', LDB_OP_NOT='!'};
-
-struct ldb_parse_tree {
-       enum ldb_parse_op operation;
-       union {
-               struct {
-                       char *attr;
-                       struct ldb_val value;
-               } simple;
-               struct {
-                       unsigned int num_elements;
-                       struct ldb_parse_tree **elements;
-               } list;
-               struct {
-                       struct ldb_parse_tree *child;
-               } not;
-       } u;
-};
-
-struct ldb_parse_tree *ldb_parse_tree(TALLOC_CTX *mem_ctx, const char *s);
-char *ldb_filter_from_tree(TALLOC_CTX *mem_ctx, struct ldb_parse_tree *tree);
-char *ldb_binary_encode(TALLOC_CTX *ctx, struct ldb_val val);
-
-#endif
index 69bf4a6dc64aad0358ed195ad5895c0a841a289a..414d8c14a11004a490252d341470a363b152bf23 100644 (file)
@@ -57,6 +57,8 @@ struct ldb_module_ops {
        const char *name;
        int (*search)(struct ldb_module *, const char *, enum ldb_scope,
                      const char *, const char * const [], struct ldb_message ***);
+       int (*search_bytree)(struct ldb_module *, const char *, enum ldb_scope,
+                            struct ldb_parse_tree *, const char * const [], struct ldb_message ***);
        int (*add_record)(struct ldb_module *, const struct ldb_message *);
        int (*modify_record)(struct ldb_module *, const struct ldb_message *);
        int (*delete_record)(struct ldb_module *, const char *);
@@ -88,6 +90,11 @@ int ldb_next_search(struct ldb_module *module,
               enum ldb_scope scope,
               const char *expression,
               const char * const *attrs, struct ldb_message ***res);
+int ldb_next_search_bytree(struct ldb_module *module, 
+                          const char *base,
+                          enum ldb_scope scope,
+                          struct ldb_parse_tree *tree,
+                          const char * const *attrs, struct ldb_message ***res);
 int ldb_next_add_record(struct ldb_module *module, const struct ldb_message *message);
 int ldb_next_modify_record(struct ldb_module *module, const struct ldb_message *message);
 int ldb_next_delete_record(struct ldb_module *module, const char *dn);
index 95b620f571cbe994b03f7205f8fcc9fa92088898..fceaf0219617281888f7a4b0972c585b9a5c1987 100644 (file)
@@ -285,6 +285,27 @@ failed:
 }
 
 
+/*
+  search for matching records using a ldb_parse_tree
+*/
+static int lldb_search_bytree(struct ldb_module *module, const char *base,
+                             enum ldb_scope scope, struct ldb_parse_tree *tree,
+                             const char * const *attrs, struct ldb_message ***res)
+{
+       struct lldb_private *lldb = module->private_data;
+       char *expression;
+       int ret;
+
+       expression = ldb_filter_from_tree(lldb, tree);
+       if (expression == NULL) {
+               return -1;
+       }
+       ret = lldb_search(module, base, scope, expression, attrs, res);
+       talloc_free(expression);
+       return ret;
+}
+
+
 /*
   convert a ldb_message structure to a list of LDAPMod structures
   ready for ldap_add() or ldap_modify()
@@ -447,15 +468,16 @@ static const char *lldb_errstring(struct ldb_module *module)
 
 
 static const struct ldb_module_ops lldb_ops = {
-       "ldap",
-       lldb_search,
-       lldb_add,
-       lldb_modify,
-       lldb_delete,
-       lldb_rename,
-       lldb_lock,
-       lldb_unlock,
-       lldb_errstring
+       .name          = "ldap",
+       .search        = lldb_search,
+       .search_bytree = lldb_search_bytree,
+       .add_record    = lldb_add,
+       .modify_record = lldb_modify,
+       .delete_record = lldb_delete,
+       .rename_record = lldb_rename,
+       .named_lock    = lldb_lock,
+       .named_unlock  = lldb_unlock,
+       .errstring     = lldb_errstring
 };
 
 
index d9e72c80894da9fe92d6cce41883f27336595daf..747938116a8752476919ee332d165a18b1ab31b7 100644 (file)
@@ -36,7 +36,6 @@
 #include "includes.h"
 #include "ldb/include/ldb.h"
 #include "ldb/include/ldb_private.h"
-#include "ldb/include/ldb_parse.h"
 #include "ldb/include/ldb_explode_dn.h"
 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
 
@@ -162,15 +161,16 @@ new_attr(struct ldb_module * module,
  * Table of operations for the sqlite3 backend
  */
 static const struct ldb_module_ops lsqlite3_ops = {
-       "sqlite",
-       lsqlite3_search,
-       lsqlite3_add,
-       lsqlite3_modify,
-       lsqlite3_delete,
-       lsqlite3_rename,
-       lsqlite3_lock,
-       lsqlite3_unlock,
-       lsqlite3_errstring
+       .name          = "sqlite",
+       .search        = lsqlite3_search,
+       .search_bytree = lsqlite3_search_bytree,
+       .add_record    = lsqlite3_add,
+       .modify_record = lsqlite3_modify,
+       .delete_record = lsqlite3_delete,
+       .rename_record = lsqlite3_rename,
+       .named_lock    = lsqlite3_lock,
+       .named_unlock  = lsqlite3_unlock,
+       .errstring     = lsqlite3_errstring
 };
 
 
index 1cf1b5c5312565878f7e4180384c4ffbda46d1ef..b6ba413ba5bbff8ddd64ab031939f13753a2a927 100644 (file)
@@ -36,7 +36,6 @@
 #include "ldb/include/ldb.h"
 #include "ldb/include/ldb_private.h"
 #include "ldb/ldb_tdb/ldb_tdb.h"
-#include "ldb/include/ldb_parse.h"
 
 /*
   find an element in a list, using the given comparison function and
index 3bb63e2b5bc7b90f5e387532019f8172ba1f6734..abaa5e98c640d2ebbc999e9a219e6065800ea538 100644 (file)
@@ -36,7 +36,6 @@
 #include "ldb/include/ldb.h"
 #include "ldb/include/ldb_private.h"
 #include "ldb/ldb_tdb/ldb_tdb.h"
-#include "ldb/include/ldb_parse.h"
 #include <fnmatch.h>
 
 /*
index e036ae09660b32a435e94bce3f31763a3f1e2133..17eff6f0a6a368d7347b58e3bd1b655c41ae710d 100644 (file)
@@ -36,7 +36,6 @@
 #include "ldb/include/ldb.h"
 #include "ldb/include/ldb_private.h"
 #include "ldb/ldb_tdb/ldb_tdb.h"
-#include "ldb/include/ldb_parse.h"
 
 /*
   add one element to a message
@@ -463,13 +462,11 @@ static int ltdb_search_full(struct ldb_module *module,
   search the database with a LDAP-like expression.
   choses a search method
 */
-int ltdb_search(struct ldb_module *module, const char *base,
-               enum ldb_scope scope, const char *expression,
-               const char * const attrs[], struct ldb_message ***res)
+int ltdb_search_bytree(struct ldb_module *module, const char *base,
+                      enum ldb_scope scope, struct ldb_parse_tree *tree,
+                      const char * const attrs[], struct ldb_message ***res)
 {
-       struct ldb_context *ldb = module->ldb;
        struct ltdb_private *ltdb = module->private_data;
-       struct ldb_parse_tree *tree;
        int ret;
 
        if (ltdb_lock_read(module) != 0) {
@@ -485,14 +482,6 @@ int ltdb_search(struct ldb_module *module, const char *base,
 
        *res = NULL;
 
-       /* form a parse tree for the expression */
-       tree = ldb_parse_tree(ldb, expression);
-       if (!tree) {
-               ltdb->last_err_string = "expression parse failed";
-               ltdb_unlock_read(module);
-               return -1;
-       }
-
        if (tree->operation == LDB_OP_SIMPLE && 
            (ldb_attr_cmp(tree->u.simple.attr, "dn") == 0 ||
             ldb_attr_cmp(tree->u.simple.attr, "distinguishedName") == 0) &&
@@ -506,9 +495,32 @@ int ltdb_search(struct ldb_module *module, const char *base,
                }
        }
 
-       talloc_free(tree);
        ltdb_unlock_read(module);
 
        return ret;
 }
 
+
+/*
+  search the database with a LDAP-like expression.
+  choses a search method
+*/
+int ltdb_search(struct ldb_module *module, const char *base,
+               enum ldb_scope scope, const char *expression,
+               const char * const attrs[], struct ldb_message ***res)
+{
+       struct ltdb_private *ltdb = module->private_data;
+       struct ldb_parse_tree *tree;
+       int ret;
+
+       tree = ldb_parse_tree(ltdb, expression);
+       if (tree == NULL) {
+               ltdb->last_err_string = "expression parse failed";
+               return -1;
+       }
+
+       ret = ltdb_search_bytree(module, base, scope, tree, attrs, res);
+       talloc_free(tree);
+       return ret;
+}
+
index 9c126c1dfd849bf77b620350eb27fe9f0b178a8a..0366809c3387ecd2745ced543a36f9ce9e5e1ad0 100644 (file)
@@ -800,15 +800,16 @@ static const char *ltdb_errstring(struct ldb_module *module)
 
 
 static const struct ldb_module_ops ltdb_ops = {
-       "tdb",
-       ltdb_search,
-       ltdb_add,
-       ltdb_modify,
-       ltdb_delete,
-       ltdb_rename,
-       ltdb_lock,
-       ltdb_unlock,
-       ltdb_errstring
+       .name          = "tdb",
+       .search        = ltdb_search,
+       .search_bytree = ltdb_search_bytree,
+       .add_record    = ltdb_add,
+       .modify_record = ltdb_modify,
+       .delete_record = ltdb_delete,
+       .rename_record = ltdb_rename,
+       .named_lock    = ltdb_lock,
+       .named_unlock  = ltdb_unlock,
+       .errstring     = ltdb_errstring
 };
 
 
index eb6c7825d2f46a44bfcc68bb6dd819b397dfdc50..891522f3008118c3235cb478c34b424b42a7e608 100644 (file)
@@ -100,6 +100,10 @@ int ltdb_add_attr_results(struct ldb_module *module, struct ldb_message *msg,
 int ltdb_search(struct ldb_module *module, const char *base,
                enum ldb_scope scope, const char *expression,
                const char * const attrs[], struct ldb_message ***res);
+int ltdb_search_bytree(struct ldb_module *module, const char *base,
+                      enum ldb_scope scope, struct ldb_parse_tree *tree,
+                      const char * const attrs[], struct ldb_message ***res);
+
 
 /* The following definitions come from lib/ldb/ldb_tdb/ldb_tdb.c  */
 struct TDB_DATA ltdb_key(struct ldb_module *module, const char *dn);
index 29e519441603c03d956a4b0d0b85a57f7ad5ea03..e11c8b4e4eb7c217be35fd63c433374bc8173c17 100644 (file)
@@ -306,6 +306,13 @@ static int schema_search(struct ldb_module *module, const char *base,
        return ldb_next_search(module, base, scope, expression, attrs, res); 
 }
 
+static int schema_search_bytree(struct ldb_module *module, const char *base,
+                               enum ldb_scope scope, struct ldb_parse_tree *tree,
+                               const char * const *attrs, struct ldb_message ***res)
+{
+       return ldb_next_search_bytree(module, base, scope, tree, attrs, res); 
+}
+
 /* add_record */
 static int schema_add_record(struct ldb_module *module, const struct ldb_message *msg)
 {
@@ -541,15 +548,16 @@ static int schema_destructor(void *module_ctx)
 }
 
 static const struct ldb_module_ops schema_ops = {
-       "schema",
-       schema_search,
-       schema_add_record,
-       schema_modify_record,
-       schema_delete_record,
-       schema_rename_record,
-       schema_named_lock,
-       schema_named_unlock,
-       schema_errstring,
+       .name          = "schema",
+       .search        = schema_search,
+       .search_bytree = schema_search_bytree,
+       .add_record    = schema_add_record,
+       .modify_record = schema_modify_record,
+       .delete_record = schema_delete_record,
+       .rename_record = schema_rename_record,
+       .named_lock    = schema_named_lock,
+       .named_unlock  = schema_named_unlock,
+       .errstring     = schema_errstring,
 };
 
 #ifdef HAVE_DLOPEN_DISABLED
index 505b19a288e0f9b415ddcf9125092d45591940b9..1221ac70f140ef9a8c8ab51cb04f6b9a8d5bcffe 100644 (file)
@@ -103,6 +103,7 @@ static int skel_destructor(void *module_ctx)
 static const struct ldb_module_ops skel_ops = {
        .name           = "skel",
        .search         = skel_search,
+       .search_bytree  = skel_search_bytree,
        .add_record     = skel_add_record,
        .modify_record  = skel_modify_record,
        .delete_record  = skel_delete_record,
index 1c01bd14fdd84ac77ea56449873641701c053165..c1db85a2847a037ac6d4cbe188887b6768bed1d3 100644 (file)
@@ -49,6 +49,14 @@ static int timestamps_search(struct ldb_module *module, const char *base,
        return ldb_next_search(module, base, scope, expression, attrs, res);
 }
 
+static int timestamps_search_bytree(struct ldb_module *module, const char *base,
+                                   enum ldb_scope scope, struct ldb_parse_tree *tree,
+                                   const char * const *attrs, struct ldb_message ***res)
+{
+       ldb_debug(module->ldb, LDB_DEBUG_TRACE, "timestamps_search\n");
+       return ldb_next_search_bytree(module, base, scope, tree, attrs, res);
+}
+
 static int add_time_element(struct ldb_module *module, struct ldb_message *msg, 
                            const char *attr_name, const char *time_string, unsigned int flags)
 {
@@ -247,15 +255,16 @@ static int timestamps_destructor(void *module_ctx)
 }
 
 static const struct ldb_module_ops timestamps_ops = {
-       "timestamps",
-       timestamps_search,
-       timestamps_add_record,
-       timestamps_modify_record,
-       timestamps_delete_record,
-       timestamps_rename_record,
-       timestamps_lock,
-       timestamps_unlock,
-       timestamps_errstring
+       .name          = "timestamps",
+       .search        = timestamps_search,
+       .search_bytree = timestamps_search_bytree,
+       .add_record    = timestamps_add_record,
+       .modify_record = timestamps_modify_record,
+       .delete_record = timestamps_delete_record,
+       .rename_record = timestamps_rename_record,
+       .named_lock    = timestamps_lock,
+       .named_unlock  = timestamps_unlock,
+       .errstring     = timestamps_errstring
 };
 
 
index 41d5f1a06e4d25811332fd626a7f9f867dbf4f38..71326caa3729218dcb71197b7e3e5fbdddfe831a 100644 (file)
@@ -328,7 +328,10 @@ struct cldap_request *cldap_search_send(struct cldap_socket *cldap,
        search->attributesonly = False;
        search->num_attributes = str_list_length(io->in.attributes);
        search->attributes     = io->in.attributes;
-       search->filter         = io->in.filter;
+       search->tree           = ldb_parse_tree(req, io->in.filter);
+       if (search->tree == NULL) {
+               goto failed;
+       }
 
        if (!ldap_encode(&msg, &req->encoded)) {
                DEBUG(0,("Failed to encode cldap message to %s:%d\n",
index 2718dd7e34031855238ce276d0b67bf452f8bc08..0d310b4eed1d8fe566cba2e7fc75821ec63b6508 100644 (file)
@@ -152,7 +152,6 @@ BOOL ldap_encode(struct ldap_message *msg, DATA_BLOB *result)
        }
        case LDAP_TAG_SearchRequest: {
                struct ldap_SearchRequest *r = &msg->r.SearchRequest;
-               struct ldb_parse_tree *tree;
                asn1_push_tag(&data, ASN1_APPLICATION(msg->type));
                asn1_write_OctetString(&data, r->basedn, strlen(r->basedn));
                asn1_write_enumerated(&data, r->scope);
@@ -161,14 +160,7 @@ BOOL ldap_encode(struct ldap_message *msg, DATA_BLOB *result)
                asn1_write_Integer(&data, r->timelimit);
                asn1_write_BOOLEAN(&data, r->attributesonly);
 
-               tree = ldb_parse_tree(NULL, r->filter);
-
-               if (tree == NULL)
-                       return False;
-
-               ldap_push_filter(&data, tree);
-
-               talloc_free(tree);
+               ldap_push_filter(&data, r->tree);
 
                asn1_push_tag(&data, ASN1_SEQUENCE(0));
                for (i=0; i<r->num_attributes; i++) {
@@ -176,7 +168,6 @@ BOOL ldap_encode(struct ldap_message *msg, DATA_BLOB *result)
                                               strlen(r->attributes[i]));
                }
                asn1_pop_tag(&data);
-
                asn1_pop_tag(&data);
                break;
        }
@@ -413,6 +404,10 @@ static void ldap_decode_response(TALLOC_CTX *mem_ctx,
        }
 }
 
+
+/*
+  parse the ASN.1 formatted search string into a ldb_parse_tree
+*/
 static struct ldb_parse_tree *ldap_decode_filter_tree(TALLOC_CTX *mem_ctx, 
                                                      struct asn1_data *data)
 {
@@ -540,25 +535,6 @@ failed:
 }
 
 
-static BOOL ldap_decode_filter(TALLOC_CTX *mem_ctx, struct asn1_data *data,
-                              const char **filterp)
-{
-       struct ldb_parse_tree *tree;
-
-       tree = ldap_decode_filter_tree(mem_ctx, data);
-       if (tree == NULL) {
-               return False;
-       }
-       *filterp = ldb_filter_from_tree(mem_ctx, tree);
-       talloc_free(tree);
-       if (*filterp == NULL) {
-               return False;
-       }
-       return True;
-}
-
-
-
 static void ldap_decode_attrib(TALLOC_CTX *mem_ctx, struct asn1_data *data,
                               struct ldap_attribute *attrib)
 {
@@ -674,9 +650,10 @@ BOOL ldap_decode(struct asn1_data *data, struct ldap_message *msg)
                asn1_read_Integer(data, &r->timelimit);
                asn1_read_BOOLEAN(data, &r->attributesonly);
 
-               /* Maybe create a TALLOC_CTX for the filter? This can waste
-                * quite a bit of memory recursing down. */
-               ldap_decode_filter(msg->mem_ctx, data, &r->filter);
+               r->tree = ldap_decode_filter_tree(msg->mem_ctx, data);
+               if (r->tree == NULL) {
+                       return False;
+               }
 
                asn1_start_tag(data, ASN1_SEQUENCE(0));
 
index 12d30a2610403594e9c8efc3125048142238be44..a44c249e7a46432969de708262d87c1f9814dd3d 100644 (file)
@@ -23,7 +23,6 @@
 #define _SMB_LDAP_H
 
 #include "lib/ldb/include/ldb.h"
-#include "lib/ldb/include/ldb_parse.h"
 
 enum ldap_request_tag {
        LDAP_TAG_BindRequest = 0,
@@ -152,7 +151,7 @@ struct ldap_SearchRequest {
        uint32_t timelimit;
        uint32_t sizelimit;
        BOOL attributesonly;
-       const char *filter;
+       struct ldb_parse_tree *tree;
        int num_attributes;
        const char **attributes;
 };
index 1124cc1701be94686ff4a75fb9156268e2338355..dff31f6411ca20ba5fd60dcb187d5191f6907f4e 100644 (file)
@@ -509,9 +509,11 @@ BOOL asn1_read_OctetString(struct asn1_data *data, DATA_BLOB *blob)
                data->has_error = True;
                return False;
        }
-       *blob = data_blob(NULL, len);
+       *blob = data_blob(NULL, len+1);
        asn1_read(data, blob->data, len);
        asn1_end_tag(data);
+       blob->length--;
+       blob->data[len] = 0;
        
        if (data->has_error) {
                data_blob_free(blob);
index c0daefa99dd3ee1b41b291cf6bdfc5f8165b899c..931a1ee2cc244844b763595f9ff6e5dadceca5be 100644 (file)
@@ -22,6 +22,7 @@
 */
 
 #include "includes.h"
+#include "lib/ldb/include/ldb.h"
 #include "libcli/ldap/ldap.h"
 #include "lib/cmdline/popt_common.h"
 
@@ -96,7 +97,7 @@ static BOOL test_search_rootDSE(struct ldap_connection *conn, char **basedn)
        msg->r.SearchRequest.timelimit = 0;
        msg->r.SearchRequest.sizelimit = 0;
        msg->r.SearchRequest.attributesonly = False;
-       msg->r.SearchRequest.filter = talloc_strdup(msg->mem_ctx, "(objectclass=*)");
+       msg->r.SearchRequest.tree = ldb_parse_tree(msg->mem_ctx, "(objectclass=*)");
        msg->r.SearchRequest.num_attributes = 0;
        msg->r.SearchRequest.attributes = NULL;