dbwrap: explicitly use DBWRAP_LOCK_ORDER_NONE in tdb->ntdb conversion
[Samba.git] / lib / dbwrap / dbwrap_local_open.c
blob6e40139962ba70e30d57b7c1cfacfe0f5afd4e08
1 /*
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/>.
21 #include "includes.h"
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_tdb.h"
24 #include "dbwrap/dbwrap_ntdb.h"
25 #include "tdb.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"
31 struct flag_map {
32 int tdb_flag;
33 int ntdb_flag;
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 },
44 { TDB_VOLATILE, 0 },
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)
52 unsigned int i;
53 int ntdb_flags = 0;
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);
67 return ntdb_flags;
70 struct trav_data {
71 struct db_context *ntdb;
72 NTSTATUS status;
75 static int write_to_ntdb(struct db_record *rec, void *_tdata)
77 struct trav_data *tdata = _tdata;
78 TDB_DATA key, value;
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)) {
85 return 1;
87 return 0;
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;
94 char *bakname;
95 const char *tdbbase, *bakbase;
96 struct trav_data tdata;
97 struct stat st;
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)));
103 return false;
105 tdb = db_open_tdb(ctx, lp_ctx, tdbname, 0,
106 TDB_DEFAULT, O_RDONLY, 0, DBWRAP_LOCK_ORDER_NONE,
107 DBWRAP_FLAG_NONE);
108 if (!tdb) {
109 DEBUG(0, ("tdb_to_ntdb: could not open %s: %s\n",
110 tdbname, strerror(errno)));
111 return false;
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,
116 DBWRAP_FLAG_NONE);
117 if (!ntdb) {
118 DEBUG(0, ("tdb_to_ntdb: could not create %s: %s\n",
119 ntdbname, strerror(errno)));
120 return false;
122 bakname = talloc_asprintf(ctx, "%s.bak", tdbname);
123 if (!bakname) {
124 DEBUG(0, ("tdb_to_ntdb: could not allocate\n"));
125 return false;
128 tdata.status = NT_STATUS_OK;
129 tdata.ntdb = ntdb;
130 if (!NT_STATUS_IS_OK(dbwrap_traverse_read(tdb, write_to_ntdb, &tdata,
131 NULL))) {
132 return false;
134 if (!NT_STATUS_IS_OK(tdata.status)) {
135 return false;
138 if (rename(tdbname, bakname) != 0) {
139 DEBUG(0, ("tdb_to_ntdb: could not rename %s to %s\n",
140 tdbname, bakname));
141 unlink(ntdbname);
142 return false;
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, '/');
150 if (!tdbbase)
151 tdbbase = tdbname;
152 bakbase = strrchr(bakname, '/');
153 if (!bakbase)
154 bakbase = bakname;
155 DEBUG(1, ("Upgraded %s from %s (which moved to %s)\n",
156 ntdbname, tdbbase, bakbase));
157 return true;
160 struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
161 struct loadparm_context *lp_ctx,
162 const char *name,
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. */
173 if (!name) {
174 tdbname = ntdbname = "unnamed database";
175 } else if (strends(name, ".tdb")) {
176 tdbname = name;
177 ntdbname = talloc_asprintf(tmp_ctx,
178 "%.*s.ntdb",
179 (int)strlen(name) - 4, name);
180 } else if (strends(name, ".ntdb")) {
181 ntdbname = name;
182 tdbname = talloc_asprintf(tmp_ctx,
183 "%.*s.tdb",
184 (int)strlen(name) - 5, name);
185 } else {
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);
189 tdbname = name;
192 if (ntdbname == NULL || tdbname == NULL) {
193 DEBUG(0, ("talloc failed\n"));
194 goto out;
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)) {
205 goto out;
209 db = db_open_ntdb(mem_ctx, lp_ctx, ntdbname, hash_size,
210 ntdb_flags, open_flags, mode, lock_order,
211 dbwrap_flags);
212 } else {
213 if (!streq(ntdbname, tdbname) && file_exist(ntdbname)) {
214 DEBUG(0, ("Refusing to open '%s' when '%s' exists\n",
215 tdbname, ntdbname));
216 goto out;
218 db = db_open_tdb(mem_ctx, lp_ctx, tdbname, hash_size,
219 tdb_flags, open_flags, mode,
220 lock_order, dbwrap_flags);
222 out:
223 talloc_free(tmp_ctx);
224 return db;