summaryrefslogtreecommitdiff
path: root/lib/dbwrap
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2013-02-20 14:59:42 +1030
committerRusty Russell <rusty@rustcorp.com.au>2013-02-20 05:31:19 +0100
commitf3c0d1d7fe71be7e8b2d1a9780586f8654b46469 (patch)
tree6a810060c42d3798eaa6f39534a311bbdbf830f6 /lib/dbwrap
parent48a42708aead320bfbcee16ae76d095c014802de (diff)
downloadsamba-f3c0d1d7fe71be7e8b2d1a9780586f8654b46469.tar.gz
samba-f3c0d1d7fe71be7e8b2d1a9780586f8654b46469.tar.bz2
samba-f3c0d1d7fe71be7e8b2d1a9780586f8654b46469.zip
dbwrap_local_open: open NTDB if extension is .ntdb
This switches dbwrap_local_open() based on the extension of the database name, so it handles both TDB and NTDB files. Moreover, if asked to open a .ntdb, and there's no ntdb file but there's a .tdb file, it converts that then moves it to .tdb.bak before opening, and turn the .tdb file into a dangling symlink to make sure it's never accidentally re-created or used: $ ls -l secrets.tdb lrwxrwxrwx 1 rusty rusty 23 Feb 11 11:31 secrets.tdb -> This is now in an NTDB This provides transparent upgrade if people decide to use NTDB on a database. Downgrade would be manual, eg: ntdbdump foo.ntdb | tdbrestore foo.tdb && mv foo.ntdb foo.ntdb.bak Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Diffstat (limited to 'lib/dbwrap')
-rw-r--r--lib/dbwrap/dbwrap_local_open.c162
1 files changed, 157 insertions, 5 deletions
diff --git a/lib/dbwrap/dbwrap_local_open.c b/lib/dbwrap/dbwrap_local_open.c
index fb5f17ebab..87c7c68418 100644
--- a/lib/dbwrap/dbwrap_local_open.c
+++ b/lib/dbwrap/dbwrap_local_open.c
@@ -21,6 +21,7 @@
#include "includes.h"
#include "dbwrap/dbwrap.h"
#include "dbwrap/dbwrap_tdb.h"
+#include "dbwrap/dbwrap_ntdb.h"
#include "tdb.h"
#ifndef DISABLE_NTDB
#include "lib/util/util_ntdb.h"
@@ -29,6 +30,135 @@
#include "system/filesys.h"
#include "ccan/str/str.h"
+#ifndef DISABLE_NTDB
+struct flag_map {
+ int tdb_flag;
+ int ntdb_flag;
+};
+
+static const struct flag_map tdb_ntdb_flags[] = {
+ { TDB_CLEAR_IF_FIRST, NTDB_CLEAR_IF_FIRST },
+ { TDB_INTERNAL, NTDB_INTERNAL },
+ { TDB_NOLOCK, NTDB_NOLOCK },
+ { TDB_NOMMAP, NTDB_NOMMAP },
+ { TDB_CONVERT, NTDB_CONVERT },
+ { TDB_NOSYNC, NTDB_NOSYNC },
+ { TDB_SEQNUM, NTDB_SEQNUM },
+ { TDB_VOLATILE, 0 },
+ { TDB_ALLOW_NESTING, NTDB_ALLOW_NESTING },
+ { TDB_DISALLOW_NESTING, 0 },
+ { TDB_INCOMPATIBLE_HASH, 0 }
+};
+
+static int tdb_flags_to_ntdb_flags(int tdb_flags)
+{
+ unsigned int i;
+ int ntdb_flags = 0;
+
+ /* TDB allows nesting unless told not to. */
+ if (!(tdb_flags & TDB_DISALLOW_NESTING))
+ ntdb_flags |= NTDB_ALLOW_NESTING;
+
+ for (i = 0; i < sizeof(tdb_ntdb_flags)/sizeof(tdb_ntdb_flags[0]); i++) {
+ if (tdb_flags & tdb_ntdb_flags[i].tdb_flag) {
+ tdb_flags &= ~tdb_ntdb_flags[i].tdb_flag;
+ ntdb_flags |= tdb_ntdb_flags[i].ntdb_flag;
+ }
+ }
+
+ SMB_ASSERT(tdb_flags == 0);
+ return ntdb_flags;
+}
+
+struct trav_data {
+ struct db_context *ntdb;
+ NTSTATUS status;
+};
+
+static int write_to_ntdb(struct db_record *rec, void *_tdata)
+{
+ struct trav_data *tdata = _tdata;
+ TDB_DATA key, value;
+
+ key = dbwrap_record_get_key(rec);
+ value = dbwrap_record_get_value(rec);
+
+ tdata->status = dbwrap_store(tdata->ntdb, key, value, TDB_INSERT);
+ if (!NT_STATUS_IS_OK(tdata->status)) {
+ return 1;
+ }
+ return 0;
+}
+
+static bool tdb_to_ntdb(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
+ const char *tdbname, const char *ntdbname)
+{
+ struct db_context *ntdb, *tdb;
+ char *bakname;
+ const char *tdbbase, *bakbase;
+ struct trav_data tdata;
+ struct stat st;
+
+ /* We need permissions from the tdb file. */
+ if (stat(tdbname, &st) == -1) {
+ DEBUG(0, ("tdb_to_ntdb: fstat %s failed: %s\n",
+ tdbname, strerror(errno)));
+ return false;
+ }
+ tdb = db_open_tdb(ctx, lp_ctx, tdbname, 0,
+ TDB_DEFAULT, O_RDONLY, 0, 0);
+ if (!tdb) {
+ DEBUG(0, ("tdb_to_ntdb: could not open %s: %s\n",
+ tdbname, strerror(errno)));
+ return false;
+ }
+ ntdb = db_open_ntdb(ctx, lp_ctx, ntdbname, dbwrap_hash_size(tdb),
+ TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL,
+ st.st_mode & 0777, 0);
+ if (!ntdb) {
+ DEBUG(0, ("tdb_to_ntdb: could not create %s: %s\n",
+ ntdbname, strerror(errno)));
+ return false;
+ }
+ bakname = talloc_asprintf(ctx, "%s.bak", tdbname);
+ if (!bakname) {
+ DEBUG(0, ("tdb_to_ntdb: could not allocate\n"));
+ return false;
+ }
+
+ tdata.status = NT_STATUS_OK;
+ tdata.ntdb = ntdb;
+ if (!NT_STATUS_IS_OK(dbwrap_traverse_read(tdb, write_to_ntdb, &tdata,
+ NULL))) {
+ return false;
+ }
+ if (!NT_STATUS_IS_OK(tdata.status)) {
+ return false;
+ }
+
+ if (rename(tdbname, bakname) != 0) {
+ DEBUG(0, ("tdb_to_ntdb: could not rename %s to %s\n",
+ tdbname, bakname));
+ unlink(ntdbname);
+ return false;
+ }
+
+ /* Make sure it's never accidentally used. */
+ symlink("This is now in an NTDB", tdbname);
+
+ /* Make message a bit shorter by using basenames. */
+ tdbbase = strrchr(tdbname, '/');
+ if (!tdbbase)
+ tdbbase = tdbname;
+ bakbase = strrchr(bakname, '/');
+ if (!bakbase)
+ bakbase = bakname;
+ DEBUG(1, ("Upgraded %s from %s (which moved to %s)\n",
+ ntdbname, tdbbase, bakbase));
+ return true;
+}
+#endif /* !DISABLE_NTDB */
+
struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
struct loadparm_context *lp_ctx,
const char *name,
@@ -54,7 +184,10 @@ struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
"%.*s.tdb",
(int)strlen(name) - 5, name);
} else {
- ntdbname = tdbname = name;
+ DEBUG(1, ("WARNING: database '%s' does not end in .[n]tdb:"
+ " treating it as a TDB file!\n", name));
+ ntdbname = talloc_strdup(tmp_ctx, name);
+ tdbname = name;
}
if (ntdbname == NULL || tdbname == NULL) {
@@ -62,10 +195,29 @@ struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
goto out;
}
- /* We currently always open a tdb, not an ntdb. */
- db = db_open_tdb(mem_ctx, lp_ctx, tdbname, hash_size,
- tdb_flags, open_flags, mode,
- lock_order);
+ if (name == ntdbname) {
+#ifdef DISABLE_NTDB
+ DEBUG(1, ("WARNING: no ntdb support to open '%s'\n", name));
+#else
+ int ntdb_flags = tdb_flags_to_ntdb_flags(tdb_flags);
+
+ /* For non-internal databases, we upgrade on demand. */
+ if (!(tdb_flags & TDB_INTERNAL)) {
+ if (!file_exist(ntdbname) && file_exist(tdbname)) {
+ if (!tdb_to_ntdb(tmp_ctx, lp_ctx,
+ tdbname, ntdbname)) {
+ goto out;
+ }
+ }
+ }
+ db = db_open_ntdb(mem_ctx, lp_ctx, ntdbname, hash_size,
+ ntdb_flags, open_flags, mode, lock_order);
+#endif
+ } else {
+ db = db_open_tdb(mem_ctx, lp_ctx, tdbname, hash_size,
+ tdb_flags, open_flags, mode,
+ lock_order);
+ }
out:
talloc_free(tmp_ctx);
return db;