svn commit: samba r7522 - in branches/SAMBA_4_0/source/lib/ldb: common include

tridge at samba.org tridge at samba.org
Mon Jun 13 06:52:48 GMT 2005


Author: tridge
Date: 2005-06-13 06:52:48 +0000 (Mon, 13 Jun 2005)
New Revision: 7522

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=7522

Log:
added a ldb_filter_from_tree() function that takes a ldb_parse_tree
and forms a ldab search filter expression. Next step is to make our
ldap server code go from ASN.1 to a ldb_parse_tree, instead of trying
to construct string filters, then add a ldb_search_tree() call to
allow for searches using parse trees.

all of this is being done as I am hitting bitwise '&' ldap search
expressions from w2k, and want to handle them cleanly.


Modified:
   branches/SAMBA_4_0/source/lib/ldb/common/ldb_parse.c
   branches/SAMBA_4_0/source/lib/ldb/include/ldb_parse.h


Changeset:
Modified: branches/SAMBA_4_0/source/lib/ldb/common/ldb_parse.c
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/common/ldb_parse.c	2005-06-13 06:42:36 UTC (rev 7521)
+++ branches/SAMBA_4_0/source/lib/ldb/common/ldb_parse.c	2005-06-13 06:52:48 UTC (rev 7522)
@@ -165,7 +165,7 @@
    encode a blob as a RFC2254 binary string, escaping any
    non-printable or '\' characters
 */
-const char *ldb_binary_encode(TALLOC_CTX *ctx, struct ldb_val val)
+char *ldb_binary_encode(TALLOC_CTX *ctx, struct ldb_val val)
 {
 	int i;
 	char *ret;
@@ -403,3 +403,56 @@
 
 	return ldb_parse_simple(mem_ctx, 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 *s, *s2, *ret;
+	int i;
+
+	switch (tree->operation) {
+	case LDB_OP_SIMPLE:
+		s = ldb_binary_encode(mem_ctx, tree->u.simple.value);
+		if (s == NULL) return NULL;
+		ret = talloc_asprintf(mem_ctx, "(%s=%s)", 
+				      tree->u.simple.attr, s);
+		talloc_free(s);
+		return ret;
+	case LDB_OP_AND:
+	case LDB_OP_OR:
+		ret = talloc_asprintf(mem_ctx, "(%c", (char)tree->operation);
+		if (ret == NULL) return NULL;
+		for (i=0;i<tree->u.list.num_elements;i++) {
+			s = ldb_filter_from_tree(mem_ctx, tree->u.list.elements[i]);
+			if (s == NULL) {
+				talloc_free(ret);
+				return NULL;
+			}
+			s2 = talloc_asprintf_append(ret, "%s", s);
+			talloc_free(s);
+			if (s2 == NULL) {
+				talloc_free(ret);
+				return NULL;
+			}
+			ret = s2;
+		}
+		s = talloc_asprintf_append(ret, ")");
+		if (s == NULL) {
+			talloc_free(ret);
+			return NULL;
+		}
+		return s;
+	case LDB_OP_NOT:
+		s = ldb_filter_from_tree(mem_ctx, tree->u.not.child);
+		if (s == NULL) return NULL;
+
+		ret = talloc_asprintf(mem_ctx, "(!%s)", s);
+		talloc_free(s);
+		return ret;
+	}
+	
+	return NULL;
+}

Modified: branches/SAMBA_4_0/source/lib/ldb/include/ldb_parse.h
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/include/ldb_parse.h	2005-06-13 06:42:36 UTC (rev 7521)
+++ branches/SAMBA_4_0/source/lib/ldb/include/ldb_parse.h	2005-06-13 06:52:48 UTC (rev 7522)
@@ -35,7 +35,7 @@
 #ifndef _LDB_PARSE_H
 #define _LDB_PARSE_H 1
 
-enum ldb_parse_op {LDB_OP_SIMPLE, LDB_OP_AND, LDB_OP_OR, LDB_OP_NOT};
+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;
@@ -55,6 +55,7 @@
 };
 
 struct ldb_parse_tree *ldb_parse_tree(TALLOC_CTX *mem_ctx, const char *s);
-const char *ldb_binary_encode(TALLOC_CTX *ctx, struct ldb_val val);
+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



More information about the samba-cvs mailing list