[PATCH 1/2] s4: make ldbadd/ldbmodify/ldbdelete really use the --controls switch

Matthieu Patou mat at matws.net
Tue Dec 22 10:44:19 MST 2009


---
 source4/lib/ldb/tools/config.mk   |   13 +++
 source4/lib/ldb/tools/ldbadd.c    |    9 ++-
 source4/lib/ldb/tools/ldbdel.c    |   14 +++-
 source4/lib/ldb/tools/ldbmodify.c |   14 +++-
 source4/lib/ldb/tools/ldbutil.c   |  148 +++++++++++++++++++++++++++++++++++++
 source4/lib/ldb/tools/ldbutil.h   |   41 ++++++++++
 6 files changed, 230 insertions(+), 9 deletions(-)
 create mode 100644 source4/lib/ldb/tools/ldbutil.c
 create mode 100644 source4/lib/ldb/tools/ldbutil.h

diff --git a/source4/lib/ldb/tools/config.mk b/source4/lib/ldb/tools/config.mk
index 6b57929..f0d0e85 100644
--- a/source4/lib/ldb/tools/config.mk
+++ b/source4/lib/ldb/tools/config.mk
@@ -1,4 +1,14 @@
 ################################################
+# Start SUBSYSTEM LIBLDB_UTIL
+[SUBSYSTEM::LIBLDB_UTIL]
+CFLAGS = -I$(ldbsrcdir) -I$(ldbsrcdir)/include
+PUBLIC_DEPENDENCIES = LIBLDB
+# End SUBSYSTEM LIBLDB_UTIL
+################################################
+
+LIBLDB_UTIL_OBJ_FILES = $(ldbsrcdir)/tools/ldbutil.o
+
+################################################
 # Start SUBSYSTEM LIBLDB_CMDLINE
 [SUBSYSTEM::LIBLDB_CMDLINE]
 CFLAGS = -I$(ldbsrcdir) -I$(ldbsrcdir)/include
@@ -14,6 +24,7 @@ LIBLDB_CMDLINE_OBJ_FILES = $(ldbsrcdir)/tools/cmdline.o
 [BINARY::ldbadd]
 INSTALLDIR = BINDIR
 PRIVATE_DEPENDENCIES = \
+		LIBLDB_UTIL \
 		LIBLDB_CMDLINE LIBCLI_RESOLVE
 # End BINARY ldbadd
 ################################################
@@ -28,6 +39,7 @@ MANPAGES += $(ldbsrcdir)/man/ldbadd.1
 [BINARY::ldbdel]
 INSTALLDIR = BINDIR
 PRIVATE_DEPENDENCIES = \
+		LIBLDB_UTIL \
 		LIBLDB_CMDLINE
 # End BINARY ldbdel
 ################################################
@@ -41,6 +53,7 @@ MANPAGES += $(ldbsrcdir)/man/ldbdel.1
 [BINARY::ldbmodify]
 INSTALLDIR = BINDIR
 PRIVATE_DEPENDENCIES = \
+		LIBLDB_UTIL \
 		LIBLDB_CMDLINE
 # End BINARY ldbmodify
 ################################################
diff --git a/source4/lib/ldb/tools/ldbadd.c b/source4/lib/ldb/tools/ldbadd.c
index a87c99a..4ff07a5 100644
--- a/source4/lib/ldb/tools/ldbadd.c
+++ b/source4/lib/ldb/tools/ldbadd.c
@@ -33,6 +33,7 @@
 
 #include "ldb.h"
 #include "tools/cmdline.h"
+#include "ldbutil.h"
 
 static int failures;
 static struct ldb_cmdline *options;
@@ -53,6 +54,12 @@ static int process_file(struct ldb_context *ldb, FILE *f, int *count)
 {
 	struct ldb_ldif *ldif;
 	int ret = LDB_SUCCESS;
+        struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
+	if (options->controls != NULL &&  req_ctrls== NULL) {
+		printf("parsing controls failed: %s\n", ldb_errstring(ldb));
+		return -1;
+	}
+
 
 	while ((ldif = ldb_ldif_read_file(ldb, f))) {
 		if (ldif->changetype != LDB_CHANGETYPE_ADD &&
@@ -63,7 +70,7 @@ static int process_file(struct ldb_context *ldb, FILE *f, int *count)
 
 		ldif->msg = ldb_msg_canonicalize(ldb, ldif->msg);
 
-		ret = ldb_add(ldb, ldif->msg);
+		ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls);
 		if (ret != LDB_SUCCESS) {
 			fprintf(stderr, "ERR: \"%s\" on DN %s\n", 
 				ldb_errstring(ldb), ldb_dn_get_linearized(ldif->msg->dn));
diff --git a/source4/lib/ldb/tools/ldbdel.c b/source4/lib/ldb/tools/ldbdel.c
index 5740f22..4180afb 100644
--- a/source4/lib/ldb/tools/ldbdel.c
+++ b/source4/lib/ldb/tools/ldbdel.c
@@ -33,6 +33,7 @@
 
 #include "ldb.h"
 #include "tools/cmdline.h"
+#include "ldbutil.h"
 
 static int dn_cmp(const void *p1, const void *p2)
 {
@@ -42,7 +43,7 @@ static int dn_cmp(const void *p1, const void *p2)
 	return ldb_dn_compare(msg1->dn, msg2->dn);
 }
 
-static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
+static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn,struct ldb_control **req_ctrls)
 {
 	int ret, i, total=0;
 	const char *attrs[] = { NULL };
@@ -55,7 +56,7 @@ static int ldb_delete_recursive(struct ldb_context *ldb, struct ldb_dn *dn)
 	qsort(res->msgs, res->count, sizeof(res->msgs[0]), dn_cmp);
 
 	for (i = 0; i < res->count; i++) {
-		if (ldb_delete(ldb, res->msgs[i]->dn) == 0) {
+		if (ldb_delete_ctrl(ldb, res->msgs[i]->dn,req_ctrls) == 0) {
 			total++;
 		} else {
 			printf("Failed to delete '%s' - %s\n",
@@ -95,6 +96,11 @@ int main(int argc, const char **argv)
 		usage();
 		exit(1);
 	}
+        struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
+	if (options->controls != NULL &&  req_ctrls== NULL) {
+		printf("parsing controls failed: %s\n", ldb_errstring(ldb));
+		return -1;
+	}
 
 	for (i=0;i<options->argc;i++) {
 		struct ldb_dn *dn;
@@ -105,9 +111,9 @@ int main(int argc, const char **argv)
 			exit(1);
 		}
 		if (options->recursive) {
-			ret = ldb_delete_recursive(ldb, dn);
+			ret = ldb_delete_recursive(ldb, dn,req_ctrls);
 		} else {
-			ret = ldb_delete(ldb, dn);
+			ret = ldb_delete_ctrl(ldb, dn,req_ctrls);
 			if (ret == 0) {
 				printf("Deleted 1 record\n");
 			}
diff --git a/source4/lib/ldb/tools/ldbmodify.c b/source4/lib/ldb/tools/ldbmodify.c
index 4936880..5756586 100644
--- a/source4/lib/ldb/tools/ldbmodify.c
+++ b/source4/lib/ldb/tools/ldbmodify.c
@@ -33,6 +33,7 @@
 
 #include "ldb.h"
 #include "tools/cmdline.h"
+#include "ldbutil.h"
 
 static int failures;
 static struct ldb_cmdline *options;
@@ -52,18 +53,23 @@ static int process_file(struct ldb_context *ldb, FILE *f, int *count)
 {
 	struct ldb_ldif *ldif;
 	int ret = LDB_SUCCESS;
-	
+	struct ldb_control **req_ctrls = ldb_parse_control_strings(ldb, ldb, (const char **)options->controls);
+	if (options->controls != NULL &&  req_ctrls== NULL) {
+		printf("parsing controls failed: %s\n", ldb_errstring(ldb));
+		return -1;
+	}
+
 	while ((ldif = ldb_ldif_read_file(ldb, f))) {
 		switch (ldif->changetype) {
 		case LDB_CHANGETYPE_NONE:
 		case LDB_CHANGETYPE_ADD:
-			ret = ldb_add(ldb, ldif->msg);
+			ret = ldb_add_ctrl(ldb, ldif->msg,req_ctrls);
 			break;
 		case LDB_CHANGETYPE_DELETE:
-			ret = ldb_delete(ldb, ldif->msg->dn);
+			ret = ldb_delete_ctrl(ldb, ldif->msg->dn,req_ctrls);
 			break;
 		case LDB_CHANGETYPE_MODIFY:
-			ret = ldb_modify(ldb, ldif->msg);
+			ret = ldb_modify_ctrl(ldb, ldif->msg,req_ctrls);
 			break;
 		}
 		if (ret != LDB_SUCCESS) {
diff --git a/source4/lib/ldb/tools/ldbutil.c b/source4/lib/ldb/tools/ldbutil.c
new file mode 100644
index 0000000..7a525f4
--- /dev/null
+++ b/source4/lib/ldb/tools/ldbutil.c
@@ -0,0 +1,148 @@
+/* 
+   ldb database library utility
+
+   Copyright (C) Matthieu Patou 2009
+
+     ** 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 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
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ *  Name: ldb
+ *
+ *  Description: Common function used by ldb_add/ldb_modify/ldb_delete
+ *
+ *  Author: Matthieu Patou
+ */
+
+#include "ldb.h"
+
+/* autostarts a transacion if none active */
+static int ldb_do_autotransaction(struct ldb_context *ldb,
+				       struct ldb_request *req)
+{
+	int ret;
+
+	ret = ldb_transaction_start(ldb);
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	ret = ldb_request(ldb, req);
+	if (ret == LDB_SUCCESS) {
+		ret = ldb_wait(req->handle, LDB_WAIT_ALL);
+	}
+
+	if (ret == LDB_SUCCESS) {
+		return ldb_transaction_commit(ldb);
+	}
+	ldb_transaction_cancel(ldb);
+
+	if (ldb_errstring(ldb) == NULL) {
+		/* no error string was setup by the backend */
+		ldb_asprintf_errstring(ldb, "%s (%d)", ldb_strerror(ret), ret);
+	}
+
+	return ret;
+}
+/*
+  Same as ldb_add but accept control
+*/
+int ldb_add_ctrl(struct ldb_context *ldb,
+		const struct ldb_message *message,
+		struct ldb_control **controls)
+{
+	struct ldb_request *req;
+	int ret;
+
+	ret = ldb_msg_sanity_check(ldb, message);
+	if (ret != LDB_SUCCESS) {
+		return ret;
+	}
+
+	ret = ldb_build_add_req(&req, ldb, ldb,
+					message,
+					controls,
+					NULL,
+					ldb_op_default_callback,
+					NULL);
+
+	if (ret != LDB_SUCCESS) return ret;
+
+	/* do request and autostart a transaction */
+	ret = ldb_do_autotransaction(ldb, req);
+
+	talloc_free(req);
+	return ret;
+}
+
+/*
+  same as ldb_delete but accept control
+*/
+int ldb_delete_ctrl(struct ldb_context *ldb, struct ldb_dn *dn,
+		struct ldb_control **controls)
+{
+	struct ldb_request *req;
+	int ret;
+
+	ret = ldb_build_del_req(&req, ldb, ldb,
+					dn,
+					controls,
+					NULL,
+					ldb_op_default_callback,
+					NULL);
+
+	if (ret != LDB_SUCCESS) return ret;
+
+	/* do request and autostart a transaction */
+	ret = ldb_do_autotransaction(ldb, req);
+
+	talloc_free(req);
+	return ret;
+}
+
+
+/*
+  same as ldb_modify, but accepts controls
+*/
+int ldb_modify_ctrl(struct ldb_context *ldb,
+                    const struct ldb_message *message,
+                    struct ldb_control **controls)
+{
+        struct ldb_request *req;
+        int ret;
+
+        ret = ldb_msg_sanity_check(ldb, message);
+        if (ret != LDB_SUCCESS) {
+                return ret;
+        }
+
+        ret = ldb_build_mod_req(&req, ldb, ldb,
+                                        message,
+                                        controls,
+                                        NULL,
+                                        ldb_op_default_callback,
+                                        NULL);
+
+        if (ret != LDB_SUCCESS) return ret;
+
+        /* do request and autostart a transaction */
+        ret = ldb_do_autotransaction(ldb, req);
+
+        talloc_free(req);
+        return ret;
+}
diff --git a/source4/lib/ldb/tools/ldbutil.h b/source4/lib/ldb/tools/ldbutil.h
new file mode 100644
index 0000000..1204461
--- /dev/null
+++ b/source4/lib/ldb/tools/ldbutil.h
@@ -0,0 +1,41 @@
+/* 
+   ldb database library utility header file
+
+   Copyright (C) Matthieu Patou 2009
+
+     ** 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 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
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ *  Name: ldb
+ *
+ *  Description: Common function used by ldb_add/ldb_modify/ldb_delete
+ *
+ *  Author: Matthieu Patou
+ */
+
+#include "ldb.h"
+
+int ldb_add_ctrl(struct ldb_context *ldb,
+		const struct ldb_message *message,
+		struct ldb_control **controls);
+int ldb_delete_ctrl(struct ldb_context *ldb, struct ldb_dn *dn,
+		struct ldb_control **controls);
+int ldb_modify_ctrl(struct ldb_context *ldb,
+                const struct ldb_message *message,
+                struct ldb_control **controls);
-- 
1.6.3.3


--------------030607090207050307060500
Content-Type: text/x-patch;
 name="0002-s4-tests-controls-parsing-and-using-for-ldbadd-ldbed.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename*0="0002-s4-tests-controls-parsing-and-using-for-ldbadd-ldbed.pa";
 filename*1="tch"



More information about the samba-technical mailing list