The branch, master has been updated
       via  8dc636a... s4: tests controls parsing and using for 
ldbadd/ldbedit/ldbmodify
       via  3bd4f67... s4: make ldbadd/ldbmodify/ldbdelete really use the 
--controls switch
      from  5aa0d97... s3: wbinfo --ping-dc is not cacheable

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 8dc636ad674a11825e9043fc356209bf2e28bcff
Author: Matthieu Patou <[email protected]>
Date:   Sat Dec 5 17:56:35 2009 +0300

    s4: tests controls parsing and using for ldbadd/ldbedit/ldbmodify

commit 3bd4f6792c63fffec66548ae5cfde60e45f865fa
Author: Matthieu Patou <[email protected]>
Date:   Tue Dec 22 20:44:19 2009 +0300

    s4: make ldbadd/ldbmodify/ldbdelete really use the --controls switch

-----------------------------------------------------------------------

Summary of changes:
 source4/lib/ldb/config.mk              |   13 +++
 source4/lib/ldb/tests/sample_module.c  |   30 ++++++-
 source4/lib/ldb/tests/test-controls.sh |   46 ++++++++++
 source4/lib/ldb/tests/test-tdb.sh      |    2 +
 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 +++++++++
 10 files changed, 320 insertions(+), 10 deletions(-)
 create mode 100755 source4/lib/ldb/tests/test-controls.sh
 create mode 100644 source4/lib/ldb/tools/ldbutil.c
 create mode 100644 source4/lib/ldb/tools/ldbutil.h


Changeset truncated at 500 lines:

diff --git a/source4/lib/ldb/config.mk b/source4/lib/ldb/config.mk
index 4a1f814..7d110fc 100644
--- a/source4/lib/ldb/config.mk
+++ b/source4/lib/ldb/config.mk
@@ -11,6 +11,19 @@ ldb_asq_OBJ_FILES = $(ldbsrcdir)/modules/asq.o
 ################################################
 
 ################################################
+# Start MODULE sample_module
+[MODULE::sample]
+PRIVATE_DEPENDENCIES = LIBTALLOC LIBTEVENT
+CFLAGS = -I$(ldbsrcdir)/include
+INIT_FUNCTION = LDB_MODULE(sample)
+SUBSYSTEM = LIBTESTLDB
+
+# End MODULE sample_module
+################################################
+sample_OBJ_FILES = $(ldbsrcdir)/tests/sample_module.o
+
+
+################################################
 # Start MODULE ldb_server_sort
 [MODULE::ldb_server_sort]
 PRIVATE_DEPENDENCIES = LIBTALLOC LIBTEVENT
diff --git a/source4/lib/ldb/tests/sample_module.c 
b/source4/lib/ldb/tests/sample_module.c
index bbe4419..bb7906e 100644
--- a/source4/lib/ldb/tests/sample_module.c
+++ b/source4/lib/ldb/tests/sample_module.c
@@ -25,12 +25,40 @@
 
 int sample_add(struct ldb_module *mod, struct ldb_request *req)
 {
+       struct ldb_control *control;
+       struct ldb_control *controls;
        ldb_msg_add_fmt(req->op.add.message, "touchedBy", "sample");
 
-       return ldb_next_request(mod, req);
+
+       /* check if there's a relax control */
+       control = ldb_request_get_control(req, LDB_CONTROL_RELAX_OID);
+       if (control == NULL) {
+               /* not found go on */
+               return ldb_next_request(mod, req);
+       } else {
+               return LDB_ERR_UNWILLING_TO_PERFORM;
+       }
+}
+
+int sample_modify(struct ldb_module *mod, struct ldb_request *req)
+{
+       struct ldb_control *control;
+       struct ldb_control *controls;
+
+       /* check if there's a relax control */
+       control = ldb_request_get_control(req, LDB_CONTROL_RELAX_OID);
+       if (control == NULL) {
+               /* not found go on */
+               return ldb_next_request(mod, req);
+       } else {
+               return LDB_ERR_UNWILLING_TO_PERFORM;
+       }
 }
 
+
 const struct ldb_module_ops ldb_sample_module_ops = {
        .name              = "sample",
        .add               = sample_add,
+       .del               = sample_modify,
+       .modify            = sample_modify,
 };
diff --git a/source4/lib/ldb/tests/test-controls.sh 
b/source4/lib/ldb/tests/test-controls.sh
new file mode 100755
index 0000000..db139bb
--- /dev/null
+++ b/source4/lib/ldb/tests/test-controls.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+if [ -n "$TEST_DATA_PREFIX" ]; then
+       LDB_URL="$TEST_DATA_PREFIX/tdbtest.ldb"
+else
+       LDB_URL="tdbtest.ldb"
+fi
+export LDB_URL
+
+PATH=bin:$PATH
+export PATH
+
+rm -f $LDB_URL*
+LDB_MODULES_PATH=`dirname $0`/../../../bin/modules/testldb
+echo $LDB_MODULES_PATH
+
+echo "LDB_URL: $LDB_URL"
+cat <<EOF | $VALGRIND ldbadd || exit 1
+dn: @MODULES
+...@list: sample
+EOF
+
+cat <<EOF | $VALGRIND ldbadd || exit 1
+dn: dc=bar
+dc: bar
+someThing: someThingElse
+EOF
+
+$VALGRIND ldbsearch "(touchedBy=sample)" | grep "touchedBy: sample" || exit 1
+# This action are expected to fails because the sample module return an error 
when presented the relax control
+
+cat <<EOF | $VALGRIND ldbadd --controls "relax:0" && exit 1
+dn: dc=foobar
+dc: foobar
+someThing: someThingElse
+EOF
+
+cat <<EOF | $VALGRIND ldbmodify --controls "relax:0" && exit 1
+dn: dc=bar
+changetype: replace
+replace someThing
+someThing: someThingElseBetter
+EOF
+
+
+set
diff --git a/source4/lib/ldb/tests/test-tdb.sh 
b/source4/lib/ldb/tests/test-tdb.sh
index 1c35451..9da1e57 100755
--- a/source4/lib/ldb/tests/test-tdb.sh
+++ b/source4/lib/ldb/tests/test-tdb.sh
@@ -29,3 +29,5 @@ $VALGRIND ldbadd$EXEEXT $LDBDIR/tests/init.ldif || exit 1
 . $LDBDIR/tests/test-extended.sh
 
 . $LDBDIR/tests/test-tdb-features.sh
+
+. $LDBDIR/tests/test-controls.sh
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..3d03da6
--- /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..7747dbe
--- /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,


-- 
Samba Shared Repository

Reply via email to