2 Unix SMB/CIFS implementation.
3 Database interface wrapper: local open code.
5 Copyright (C) Rusty Russell 2012
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_tdb.h"
24 #include "dbwrap/dbwrap_ntdb.h"
26 #include "lib/util/util_ntdb.h"
27 #include "lib/param/param.h"
28 #include "system/filesys.h"
29 #include "ccan/str/str.h"
36 static const struct flag_map tdb_ntdb_flags[] = {
37 { TDB_CLEAR_IF_FIRST, NTDB_CLEAR_IF_FIRST },
38 { TDB_INTERNAL, NTDB_INTERNAL },
39 { TDB_NOLOCK, NTDB_NOLOCK },
40 { TDB_NOMMAP, NTDB_NOMMAP },
41 { TDB_CONVERT, NTDB_CONVERT },
42 { TDB_NOSYNC, NTDB_NOSYNC },
43 { TDB_SEQNUM, NTDB_SEQNUM },
45 { TDB_ALLOW_NESTING, NTDB_ALLOW_NESTING },
46 { TDB_DISALLOW_NESTING, 0 },
47 { TDB_INCOMPATIBLE_HASH, 0 }
50 static int tdb_flags_to_ntdb_flags(int tdb_flags)
55 /* TDB allows nesting unless told not to. */
56 if (!(tdb_flags & TDB_DISALLOW_NESTING))
57 ntdb_flags |= NTDB_ALLOW_NESTING;
59 for (i = 0; i < sizeof(tdb_ntdb_flags)/sizeof(tdb_ntdb_flags[0]); i++) {
60 if (tdb_flags & tdb_ntdb_flags[i].tdb_flag) {
61 tdb_flags &= ~tdb_ntdb_flags[i].tdb_flag;
62 ntdb_flags |= tdb_ntdb_flags[i].ntdb_flag;
66 SMB_ASSERT(tdb_flags == 0);
71 struct db_context *ntdb;
75 static int write_to_ntdb(struct db_record *rec, void *_tdata)
77 struct trav_data *tdata = _tdata;
80 key = dbwrap_record_get_key(rec);
81 value = dbwrap_record_get_value(rec);
83 tdata->status = dbwrap_store(tdata->ntdb, key, value, TDB_INSERT);
84 if (!NT_STATUS_IS_OK(tdata->status)) {
90 static bool tdb_to_ntdb(TALLOC_CTX *ctx, struct loadparm_context *lp_ctx,
91 const char *tdbname, const char *ntdbname)
93 struct db_context *ntdb, *tdb;
95 const char *tdbbase, *bakbase;
96 struct trav_data tdata;
99 /* We need permissions from the tdb file. */
100 if (stat(tdbname, &st) == -1) {
101 DEBUG(0, ("tdb_to_ntdb: fstat %s failed: %s\n",
102 tdbname, strerror(errno)));
105 tdb = db_open_tdb(ctx, lp_ctx, tdbname, 0,
106 TDB_DEFAULT, O_RDONLY, 0, DBWRAP_LOCK_ORDER_NONE,
109 DEBUG(0, ("tdb_to_ntdb: could not open %s: %s\n",
110 tdbname, strerror(errno)));
113 ntdb = db_open_ntdb(ctx, lp_ctx, ntdbname, dbwrap_hash_size(tdb),
114 TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL,
115 st.st_mode & 0777, DBWRAP_LOCK_ORDER_NONE,
118 DEBUG(0, ("tdb_to_ntdb: could not create %s: %s\n",
119 ntdbname, strerror(errno)));
122 bakname = talloc_asprintf(ctx, "%s.bak", tdbname);
124 DEBUG(0, ("tdb_to_ntdb: could not allocate\n"));
128 tdata.status = NT_STATUS_OK;
130 if (!NT_STATUS_IS_OK(dbwrap_traverse_read(tdb, write_to_ntdb, &tdata,
134 if (!NT_STATUS_IS_OK(tdata.status)) {
138 if (rename(tdbname, bakname) != 0) {
139 DEBUG(0, ("tdb_to_ntdb: could not rename %s to %s\n",
145 /* Make sure it's never accidentally used. */
146 symlink("This is now in an NTDB", tdbname);
148 /* Make message a bit shorter by using basenames. */
149 tdbbase = strrchr(tdbname, '/');
152 bakbase = strrchr(bakname, '/');
155 DEBUG(1, ("Upgraded %s from %s (which moved to %s)\n",
156 ntdbname, tdbbase, bakbase));
160 struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
161 struct loadparm_context *lp_ctx,
163 int hash_size, int tdb_flags,
164 int open_flags, mode_t mode,
165 enum dbwrap_lock_order lock_order,
166 uint64_t dbwrap_flags)
168 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
169 const char *ntdbname, *tdbname;
170 struct db_context *db = NULL;
172 /* Get both .ntdb and .tdb variants of the name. */
174 tdbname = ntdbname = "unnamed database";
175 } else if (strends(name, ".tdb")) {
177 ntdbname = talloc_asprintf(tmp_ctx,
179 (int)strlen(name) - 4, name);
180 } else if (strends(name, ".ntdb")) {
182 tdbname = talloc_asprintf(tmp_ctx,
184 (int)strlen(name) - 5, name);
186 DEBUG(1, ("WARNING: database '%s' does not end in .[n]tdb:"
187 " treating it as a TDB file!\n", name));
188 ntdbname = talloc_strdup(tmp_ctx, name);
192 if (ntdbname == NULL || tdbname == NULL) {
193 DEBUG(0, ("talloc failed\n"));
197 if (name == ntdbname) {
198 int ntdb_flags = tdb_flags_to_ntdb_flags(tdb_flags);
200 /* For non-internal databases, we upgrade on demand. */
201 if (!(tdb_flags & TDB_INTERNAL)) {
202 if (!file_exist(ntdbname) && file_exist(tdbname)) {
203 if (!tdb_to_ntdb(tmp_ctx, lp_ctx,
204 tdbname, ntdbname)) {
209 db = db_open_ntdb(mem_ctx, lp_ctx, ntdbname, hash_size,
210 ntdb_flags, open_flags, mode, lock_order,
213 if (!streq(ntdbname, tdbname) && file_exist(ntdbname)) {
214 DEBUG(0, ("Refusing to open '%s' when '%s' exists\n",
218 db = db_open_tdb(mem_ctx, lp_ctx, tdbname, hash_size,
219 tdb_flags, open_flags, mode,
220 lock_order, dbwrap_flags);
223 talloc_free(tmp_ctx);