source4/auth/kerberos/kerberos-notes.txt: Fix typo.
[Samba.git] / lib / dbwrap / dbwrap_local_open.c
blob56048afa849ff5c24a18f42319d4dea1303be5e8
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, 0);
107 if (!tdb) {
108 DEBUG(0, ("tdb_to_ntdb: could not open %s: %s\n",
109 tdbname, strerror(errno)));
110 return false;
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);
115 if (!ntdb) {
116 DEBUG(0, ("tdb_to_ntdb: could not create %s: %s\n",
117 ntdbname, strerror(errno)));
118 return false;
120 bakname = talloc_asprintf(ctx, "%s.bak", tdbname);
121 if (!bakname) {
122 DEBUG(0, ("tdb_to_ntdb: could not allocate\n"));
123 return false;
126 tdata.status = NT_STATUS_OK;
127 tdata.ntdb = ntdb;
128 if (!NT_STATUS_IS_OK(dbwrap_traverse_read(tdb, write_to_ntdb, &tdata,
129 NULL))) {
130 return false;
132 if (!NT_STATUS_IS_OK(tdata.status)) {
133 return false;
136 if (rename(tdbname, bakname) != 0) {
137 DEBUG(0, ("tdb_to_ntdb: could not rename %s to %s\n",
138 tdbname, bakname));
139 unlink(ntdbname);
140 return false;
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, '/');
148 if (!tdbbase)
149 tdbbase = tdbname;
150 bakbase = strrchr(bakname, '/');
151 if (!bakbase)
152 bakbase = bakname;
153 DEBUG(1, ("Upgraded %s from %s (which moved to %s)\n",
154 ntdbname, tdbbase, bakbase));
155 return true;
158 struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
159 struct loadparm_context *lp_ctx,
160 const char *name,
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. */
170 if (!name) {
171 tdbname = ntdbname = "unnamed database";
172 } else if (strends(name, ".tdb")) {
173 tdbname = name;
174 ntdbname = talloc_asprintf(tmp_ctx,
175 "%.*s.ntdb",
176 (int)strlen(name) - 4, name);
177 } else if (strends(name, ".ntdb")) {
178 ntdbname = name;
179 tdbname = talloc_asprintf(tmp_ctx,
180 "%.*s.tdb",
181 (int)strlen(name) - 5, name);
182 } else {
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);
186 tdbname = name;
189 if (ntdbname == NULL || tdbname == NULL) {
190 DEBUG(0, ("talloc failed\n"));
191 goto out;
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)) {
202 goto out;
206 db = db_open_ntdb(mem_ctx, lp_ctx, ntdbname, hash_size,
207 ntdb_flags, open_flags, mode, lock_order);
208 } else {
209 if (!streq(ntdbname, tdbname) && file_exist(ntdbname)) {
210 DEBUG(0, ("Refusing to open '%s' when '%s' exists\n",
211 tdbname, ntdbname));
212 goto out;
214 db = db_open_tdb(mem_ctx, lp_ctx, tdbname, hash_size,
215 tdb_flags, open_flags, mode,
216 lock_order);
218 out:
219 talloc_free(tmp_ctx);
220 return db;