ldb: ensure that ldbedit operates like ldbmodify: set DONT_CREATE_DB
authorAndrew Bartlett <abartlet@samba.org>
Sat, 29 Feb 2020 03:03:00 +0000 (16:03 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 2 Mar 2020 02:47:30 +0000 (02:47 +0000)
ldb* tools, when passed a raw filename assume tdb://

By default, ldb_tdb will call tdb with O_CREAT.

TDB, when passed O_CREAT and a not-tdb file, will wipe the file.

This means that if you run ldbedit <path to mdb-format-ldb file> the file
will be wiped, which is unexpected.  I noticed this while trying to
corrupt a sam.ldb backend file (for testing), but instead I wiped it!

Ideally tdb would not do that, but the behaviour has been this way for
decades.  Ideally ldb would have had a "create db" command, but this
has been the job of ldbadd for over a decade.

So this just blunts the knife for ldbedit.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=14302

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
lib/ldb/tools/cmdline.c
lib/ldb/tools/cmdline.h
lib/ldb/tools/ldbedit.c

index a2fe97ee6b2841effef9cd86215c997f6823b011..d53161574fdb4bda2f8a5b346befbd6ae5e90a9f 100644 (file)
@@ -96,6 +96,7 @@ static bool add_control(TALLOC_CTX *mem_ctx, const char *control)
 static struct ldb_cmdline *ldb_cmdline_process_internal(struct ldb_context *ldb,
                                        int argc, const char **argv,
                                        void (*usage)(struct ldb_context *),
+                                       bool dont_create,
                                        bool search)
 {
        struct ldb_cmdline *ret=NULL;
@@ -326,14 +327,21 @@ struct ldb_cmdline *ldb_cmdline_process_search(struct ldb_context *ldb,
                                               int argc, const char **argv,
                                               void (*usage)(struct ldb_context *))
 {
-       return ldb_cmdline_process_internal(ldb, argc, argv, usage, true);
+       return ldb_cmdline_process_internal(ldb, argc, argv, usage, true, true);
+}
+
+struct ldb_cmdline *ldb_cmdline_process_edit(struct ldb_context *ldb,
+                                            int argc, const char **argv,
+                                            void (*usage)(struct ldb_context *))
+{
+       return ldb_cmdline_process_internal(ldb, argc, argv, usage, false, true);
 }
 
 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb,
                                        int argc, const char **argv,
                                        void (*usage)(struct ldb_context *))
 {
-       return ldb_cmdline_process_internal(ldb, argc, argv, usage, false);
+       return ldb_cmdline_process_internal(ldb, argc, argv, usage, false, false);
 }
 
 /* this function check controls reply and determines if more
index 9af0ea168b79e200cc8c3c98112579ae6aee6a9e..dbc216aab1720ee256458f4376c94af6f9709087 100644 (file)
@@ -49,6 +49,9 @@ struct ldb_cmdline {
 struct ldb_cmdline *ldb_cmdline_process_search(struct ldb_context *ldb,
                                               int argc, const char **argv,
                                               void (*usage)(struct ldb_context *));
+struct ldb_cmdline *ldb_cmdline_process_edit(struct ldb_context *ldb,
+                                            int argc, const char **argv,
+                                            void (*usage)(struct ldb_context *));
 struct ldb_cmdline *ldb_cmdline_process(struct ldb_context *ldb, int argc,
                                        const char **argv,
                                        void (*usage)(struct ldb_context *));
index 5b83783448232f1381d823d8dc85b0a66f26077a..497ef97024444b091109dfa4c30bab66826bd231 100644 (file)
@@ -327,7 +327,7 @@ int main(int argc, const char **argv)
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       options = ldb_cmdline_process(ldb, argc, argv, usage);
+       options = ldb_cmdline_process_edit(ldb, argc, argv, usage);
 
        /* the check for '=' is for compatibility with ldapsearch */
        if (options->argc > 0 &&