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, 0);
108 DEBUG(0, ("tdb_to_ntdb: could not open %s: %s\n",
109 tdbname, strerror(errno)));
112 ntdb = db_open_ntdb(ctx, lp_ctx, ntdbname, dbwrap_hash_size(tdb),
113 TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL,
114 st.st_mode & 0777, 0);
116 DEBUG(0, ("tdb_to_ntdb: could not create %s: %s\n",
117 ntdbname, strerror(errno)));
120 bakname = talloc_asprintf(ctx, "%s.bak", tdbname);
122 DEBUG(0, ("tdb_to_ntdb: could not allocate\n"));
126 tdata.status = NT_STATUS_OK;
128 if (!NT_STATUS_IS_OK(dbwrap_traverse_read(tdb, write_to_ntdb, &tdata,
132 if (!NT_STATUS_IS_OK(tdata.status)) {
136 if (rename(tdbname, bakname) != 0) {
137 DEBUG(0, ("tdb_to_ntdb: could not rename %s to %s\n",
143 /* Make sure it's never accidentally used. */
144 symlink("This is now in an NTDB", tdbname);
146 /* Make message a bit shorter by using basenames. */
147 tdbbase = strrchr(tdbname, '/');
150 bakbase = strrchr(bakname, '/');
153 DEBUG(1, ("Upgraded %s from %s (which moved to %s)\n",
154 ntdbname, tdbbase, bakbase));
158 struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
159 struct loadparm_context *lp_ctx,
161 int hash_size, int tdb_flags,
162 int open_flags, mode_t mode,
163 enum dbwrap_lock_order lock_order)
165 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
166 const char *ntdbname, *tdbname;
167 struct db_context *db = NULL;
169 /* Get both .ntdb and .tdb variants of the name. */
171 tdbname = ntdbname = "unnamed database";
172 } else if (strends(name, ".tdb")) {
174 ntdbname = talloc_asprintf(tmp_ctx,
176 (int)strlen(name) - 4, name);
177 } else if (strends(name, ".ntdb")) {
179 tdbname = talloc_asprintf(tmp_ctx,
181 (int)strlen(name) - 5, name);
183 DEBUG(1, ("WARNING: database '%s' does not end in .[n]tdb:"
184 " treating it as a TDB file!\n", name));
185 ntdbname = talloc_strdup(tmp_ctx, name);
189 if (ntdbname == NULL || tdbname == NULL) {
190 DEBUG(0, ("talloc failed\n"));
194 if (name == ntdbname) {
195 int ntdb_flags = tdb_flags_to_ntdb_flags(tdb_flags);
197 /* For non-internal databases, we upgrade on demand. */
198 if (!(tdb_flags & TDB_INTERNAL)) {
199 if (!file_exist(ntdbname) && file_exist(tdbname)) {
200 if (!tdb_to_ntdb(tmp_ctx, lp_ctx,
201 tdbname, ntdbname)) {
206 db = db_open_ntdb(mem_ctx, lp_ctx, ntdbname, hash_size,
207 ntdb_flags, open_flags, mode, lock_order);
209 if (!streq(ntdbname, tdbname) && file_exist(ntdbname)) {
210 DEBUG(0, ("Refusing to open '%s' when '%s' exists\n",
214 db = db_open_tdb(mem_ctx, lp_ctx, tdbname, hash_size,
215 tdb_flags, open_flags, mode,
219 talloc_free(tmp_ctx);