1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | Database interface wrapper around tdb
|
---|
4 | Copyright (C) Volker Lendecke 2005-2007
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "includes.h"
|
---|
21 | #include "dbwrap/dbwrap.h"
|
---|
22 | #include "dbwrap/dbwrap_private.h"
|
---|
23 | #include "dbwrap/dbwrap_tdb.h"
|
---|
24 | #include "lib/tdb_wrap/tdb_wrap.h"
|
---|
25 | #include "lib/util/util_tdb.h"
|
---|
26 | #include "system/filesys.h"
|
---|
27 | #include "lib/param/param.h"
|
---|
28 |
|
---|
29 | struct db_tdb_ctx {
|
---|
30 | struct tdb_wrap *wtdb;
|
---|
31 |
|
---|
32 | struct {
|
---|
33 | dev_t dev;
|
---|
34 | ino_t ino;
|
---|
35 | } id;
|
---|
36 | };
|
---|
37 |
|
---|
38 | static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag);
|
---|
39 | static NTSTATUS db_tdb_delete(struct db_record *rec);
|
---|
40 |
|
---|
41 | static void db_tdb_log_key(const char *prefix, TDB_DATA key)
|
---|
42 | {
|
---|
43 | size_t len;
|
---|
44 | char *keystr;
|
---|
45 | TALLOC_CTX *frame;
|
---|
46 | if (DEBUGLEVEL < 10) {
|
---|
47 | return;
|
---|
48 | }
|
---|
49 | frame = talloc_stackframe();
|
---|
50 | len = key.dsize;
|
---|
51 | if (DEBUGLEVEL == 10) {
|
---|
52 | /*
|
---|
53 | * Only fully spam at debuglevel > 10
|
---|
54 | */
|
---|
55 | len = MIN(10, key.dsize);
|
---|
56 | }
|
---|
57 | keystr = hex_encode_talloc(frame, (unsigned char *)(key.dptr),
|
---|
58 | len);
|
---|
59 | DEBUG(10, ("%s key %s\n", prefix, keystr));
|
---|
60 | TALLOC_FREE(frame);
|
---|
61 | }
|
---|
62 |
|
---|
63 | static int db_tdb_record_destr(struct db_record* data)
|
---|
64 | {
|
---|
65 | struct db_tdb_ctx *ctx =
|
---|
66 | talloc_get_type_abort(data->private_data, struct db_tdb_ctx);
|
---|
67 |
|
---|
68 | db_tdb_log_key("Unlocking", data->key);
|
---|
69 | tdb_chainunlock(ctx->wtdb->tdb, data->key);
|
---|
70 | return 0;
|
---|
71 | }
|
---|
72 |
|
---|
73 | struct tdb_fetch_locked_state {
|
---|
74 | TALLOC_CTX *mem_ctx;
|
---|
75 | struct db_record *result;
|
---|
76 | };
|
---|
77 |
|
---|
78 | static int db_tdb_fetchlock_parse(TDB_DATA key, TDB_DATA data,
|
---|
79 | void *private_data)
|
---|
80 | {
|
---|
81 | struct tdb_fetch_locked_state *state =
|
---|
82 | (struct tdb_fetch_locked_state *)private_data;
|
---|
83 | struct db_record *result;
|
---|
84 |
|
---|
85 | result = (struct db_record *)talloc_size(
|
---|
86 | state->mem_ctx,
|
---|
87 | sizeof(struct db_record) + key.dsize + data.dsize);
|
---|
88 |
|
---|
89 | if (result == NULL) {
|
---|
90 | return 0;
|
---|
91 | }
|
---|
92 | state->result = result;
|
---|
93 |
|
---|
94 | result->key.dsize = key.dsize;
|
---|
95 | result->key.dptr = ((uint8_t *)result) + sizeof(struct db_record);
|
---|
96 | memcpy(result->key.dptr, key.dptr, key.dsize);
|
---|
97 |
|
---|
98 | result->value.dsize = data.dsize;
|
---|
99 |
|
---|
100 | if (data.dsize > 0) {
|
---|
101 | result->value.dptr = result->key.dptr+key.dsize;
|
---|
102 | memcpy(result->value.dptr, data.dptr, data.dsize);
|
---|
103 | }
|
---|
104 | else {
|
---|
105 | result->value.dptr = NULL;
|
---|
106 | }
|
---|
107 |
|
---|
108 | return 0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | static struct db_record *db_tdb_fetch_locked_internal(
|
---|
112 | struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
|
---|
113 | {
|
---|
114 | struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
---|
115 | struct db_tdb_ctx);
|
---|
116 | struct tdb_fetch_locked_state state;
|
---|
117 |
|
---|
118 | state.mem_ctx = mem_ctx;
|
---|
119 | state.result = NULL;
|
---|
120 |
|
---|
121 | if ((tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetchlock_parse,
|
---|
122 | &state) < 0) &&
|
---|
123 | (tdb_error(ctx->wtdb->tdb) != TDB_ERR_NOEXIST)) {
|
---|
124 | tdb_chainunlock(ctx->wtdb->tdb, key);
|
---|
125 | return NULL;
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (state.result == NULL) {
|
---|
129 | db_tdb_fetchlock_parse(key, tdb_null, &state);
|
---|
130 | }
|
---|
131 |
|
---|
132 | if (state.result == NULL) {
|
---|
133 | tdb_chainunlock(ctx->wtdb->tdb, key);
|
---|
134 | return NULL;
|
---|
135 | }
|
---|
136 |
|
---|
137 | talloc_set_destructor(state.result, db_tdb_record_destr);
|
---|
138 |
|
---|
139 | state.result->private_data = ctx;
|
---|
140 | state.result->store = db_tdb_store;
|
---|
141 | state.result->delete_rec = db_tdb_delete;
|
---|
142 |
|
---|
143 | DEBUG(10, ("Allocated locked data 0x%p\n", state.result));
|
---|
144 |
|
---|
145 | return state.result;
|
---|
146 | }
|
---|
147 |
|
---|
148 | static struct db_record *db_tdb_fetch_locked(
|
---|
149 | struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
|
---|
150 | {
|
---|
151 | struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
---|
152 | struct db_tdb_ctx);
|
---|
153 |
|
---|
154 | db_tdb_log_key("Locking", key);
|
---|
155 | if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
|
---|
156 | DEBUG(3, ("tdb_chainlock failed\n"));
|
---|
157 | return NULL;
|
---|
158 | }
|
---|
159 | return db_tdb_fetch_locked_internal(db, mem_ctx, key);
|
---|
160 | }
|
---|
161 |
|
---|
162 | static struct db_record *db_tdb_try_fetch_locked(
|
---|
163 | struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
|
---|
164 | {
|
---|
165 | struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
---|
166 | struct db_tdb_ctx);
|
---|
167 |
|
---|
168 | db_tdb_log_key("Trying to lock", key);
|
---|
169 | if (tdb_chainlock_nonblock(ctx->wtdb->tdb, key) != 0) {
|
---|
170 | DEBUG(3, ("tdb_chainlock_nonblock failed\n"));
|
---|
171 | return NULL;
|
---|
172 | }
|
---|
173 | return db_tdb_fetch_locked_internal(db, mem_ctx, key);
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | static int db_tdb_exists(struct db_context *db, TDB_DATA key)
|
---|
178 | {
|
---|
179 | struct db_tdb_ctx *ctx = talloc_get_type_abort(
|
---|
180 | db->private_data, struct db_tdb_ctx);
|
---|
181 | return tdb_exists(ctx->wtdb->tdb, key);
|
---|
182 | }
|
---|
183 |
|
---|
184 | static int db_tdb_wipe(struct db_context *db)
|
---|
185 | {
|
---|
186 | struct db_tdb_ctx *ctx = talloc_get_type_abort(
|
---|
187 | db->private_data, struct db_tdb_ctx);
|
---|
188 | return tdb_wipe_all(ctx->wtdb->tdb);
|
---|
189 | }
|
---|
190 |
|
---|
191 | static int db_tdb_check(struct db_context *db)
|
---|
192 | {
|
---|
193 | struct db_tdb_ctx *ctx = talloc_get_type_abort(
|
---|
194 | db->private_data, struct db_tdb_ctx);
|
---|
195 | return tdb_check(ctx->wtdb->tdb, NULL, NULL);
|
---|
196 | }
|
---|
197 |
|
---|
198 | struct db_tdb_parse_state {
|
---|
199 | void (*parser)(TDB_DATA key, TDB_DATA data,
|
---|
200 | void *private_data);
|
---|
201 | void *private_data;
|
---|
202 | };
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * tdb_parse_record expects a parser returning int, mixing up tdb and
|
---|
206 | * parser errors. Wrap around that by always returning 0 and have
|
---|
207 | * dbwrap_parse_record expect a parser returning void.
|
---|
208 | */
|
---|
209 |
|
---|
210 | static int db_tdb_parser(TDB_DATA key, TDB_DATA data, void *private_data)
|
---|
211 | {
|
---|
212 | struct db_tdb_parse_state *state =
|
---|
213 | (struct db_tdb_parse_state *)private_data;
|
---|
214 | state->parser(key, data, state->private_data);
|
---|
215 | return 0;
|
---|
216 | }
|
---|
217 |
|
---|
218 | static NTSTATUS db_tdb_parse(struct db_context *db, TDB_DATA key,
|
---|
219 | void (*parser)(TDB_DATA key, TDB_DATA data,
|
---|
220 | void *private_data),
|
---|
221 | void *private_data)
|
---|
222 | {
|
---|
223 | struct db_tdb_ctx *ctx = talloc_get_type_abort(
|
---|
224 | db->private_data, struct db_tdb_ctx);
|
---|
225 | struct db_tdb_parse_state state;
|
---|
226 | int ret;
|
---|
227 |
|
---|
228 | state.parser = parser;
|
---|
229 | state.private_data = private_data;
|
---|
230 |
|
---|
231 | ret = tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_parser, &state);
|
---|
232 |
|
---|
233 | if (ret != 0) {
|
---|
234 | return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb));
|
---|
235 | }
|
---|
236 | return NT_STATUS_OK;
|
---|
237 | }
|
---|
238 |
|
---|
239 | static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag)
|
---|
240 | {
|
---|
241 | struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
|
---|
242 | struct db_tdb_ctx);
|
---|
243 |
|
---|
244 | /*
|
---|
245 | * This has a bug: We need to replace rec->value for correct
|
---|
246 | * operation, but right now brlock and locking don't use the value
|
---|
247 | * anymore after it was stored.
|
---|
248 | */
|
---|
249 |
|
---|
250 | return (tdb_store(ctx->wtdb->tdb, rec->key, data, flag) == 0) ?
|
---|
251 | NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
|
---|
252 | }
|
---|
253 |
|
---|
254 | static NTSTATUS db_tdb_delete(struct db_record *rec)
|
---|
255 | {
|
---|
256 | struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
|
---|
257 | struct db_tdb_ctx);
|
---|
258 |
|
---|
259 | if (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) {
|
---|
260 | return NT_STATUS_OK;
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (tdb_error(ctx->wtdb->tdb) == TDB_ERR_NOEXIST) {
|
---|
264 | return NT_STATUS_NOT_FOUND;
|
---|
265 | }
|
---|
266 |
|
---|
267 | return NT_STATUS_UNSUCCESSFUL;
|
---|
268 | }
|
---|
269 |
|
---|
270 | struct db_tdb_traverse_ctx {
|
---|
271 | struct db_context *db;
|
---|
272 | int (*f)(struct db_record *rec, void *private_data);
|
---|
273 | void *private_data;
|
---|
274 | };
|
---|
275 |
|
---|
276 | static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
|
---|
277 | void *private_data)
|
---|
278 | {
|
---|
279 | struct db_tdb_traverse_ctx *ctx =
|
---|
280 | (struct db_tdb_traverse_ctx *)private_data;
|
---|
281 | struct db_record rec;
|
---|
282 |
|
---|
283 | rec.key = kbuf;
|
---|
284 | rec.value = dbuf;
|
---|
285 | rec.store = db_tdb_store;
|
---|
286 | rec.delete_rec = db_tdb_delete;
|
---|
287 | rec.private_data = ctx->db->private_data;
|
---|
288 | rec.db = ctx->db;
|
---|
289 |
|
---|
290 | return ctx->f(&rec, ctx->private_data);
|
---|
291 | }
|
---|
292 |
|
---|
293 | static int db_tdb_traverse(struct db_context *db,
|
---|
294 | int (*f)(struct db_record *rec, void *private_data),
|
---|
295 | void *private_data)
|
---|
296 | {
|
---|
297 | struct db_tdb_ctx *db_ctx =
|
---|
298 | talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
|
---|
299 | struct db_tdb_traverse_ctx ctx;
|
---|
300 |
|
---|
301 | ctx.db = db;
|
---|
302 | ctx.f = f;
|
---|
303 | ctx.private_data = private_data;
|
---|
304 | return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx);
|
---|
305 | }
|
---|
306 |
|
---|
307 | static NTSTATUS db_tdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
|
---|
308 | {
|
---|
309 | return NT_STATUS_MEDIA_WRITE_PROTECTED;
|
---|
310 | }
|
---|
311 |
|
---|
312 | static NTSTATUS db_tdb_delete_deny(struct db_record *rec)
|
---|
313 | {
|
---|
314 | return NT_STATUS_MEDIA_WRITE_PROTECTED;
|
---|
315 | }
|
---|
316 |
|
---|
317 | static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
|
---|
318 | void *private_data)
|
---|
319 | {
|
---|
320 | struct db_tdb_traverse_ctx *ctx =
|
---|
321 | (struct db_tdb_traverse_ctx *)private_data;
|
---|
322 | struct db_record rec;
|
---|
323 |
|
---|
324 | rec.key = kbuf;
|
---|
325 | rec.value = dbuf;
|
---|
326 | rec.store = db_tdb_store_deny;
|
---|
327 | rec.delete_rec = db_tdb_delete_deny;
|
---|
328 | rec.private_data = ctx->db->private_data;
|
---|
329 | rec.db = ctx->db;
|
---|
330 |
|
---|
331 | return ctx->f(&rec, ctx->private_data);
|
---|
332 | }
|
---|
333 |
|
---|
334 | static int db_tdb_traverse_read(struct db_context *db,
|
---|
335 | int (*f)(struct db_record *rec, void *private_data),
|
---|
336 | void *private_data)
|
---|
337 | {
|
---|
338 | struct db_tdb_ctx *db_ctx =
|
---|
339 | talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
|
---|
340 | struct db_tdb_traverse_ctx ctx;
|
---|
341 |
|
---|
342 | ctx.db = db;
|
---|
343 | ctx.f = f;
|
---|
344 | ctx.private_data = private_data;
|
---|
345 | return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx);
|
---|
346 | }
|
---|
347 |
|
---|
348 | static int db_tdb_get_seqnum(struct db_context *db)
|
---|
349 |
|
---|
350 | {
|
---|
351 | struct db_tdb_ctx *db_ctx =
|
---|
352 | talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
|
---|
353 | return tdb_get_seqnum(db_ctx->wtdb->tdb);
|
---|
354 | }
|
---|
355 |
|
---|
356 | static int db_tdb_transaction_start(struct db_context *db)
|
---|
357 | {
|
---|
358 | struct db_tdb_ctx *db_ctx =
|
---|
359 | talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
|
---|
360 | return tdb_transaction_start(db_ctx->wtdb->tdb) ? -1 : 0;
|
---|
361 | }
|
---|
362 |
|
---|
363 | static NTSTATUS db_tdb_transaction_start_nonblock(struct db_context *db)
|
---|
364 | {
|
---|
365 | struct db_tdb_ctx *db_ctx =
|
---|
366 | talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
|
---|
367 | int ret;
|
---|
368 |
|
---|
369 | ret = tdb_transaction_start_nonblock(db_ctx->wtdb->tdb);
|
---|
370 | if (ret != 0) {
|
---|
371 | return map_nt_error_from_tdb(tdb_error(db_ctx->wtdb->tdb));
|
---|
372 | }
|
---|
373 | return NT_STATUS_OK;
|
---|
374 | }
|
---|
375 |
|
---|
376 | static int db_tdb_transaction_commit(struct db_context *db)
|
---|
377 | {
|
---|
378 | struct db_tdb_ctx *db_ctx =
|
---|
379 | talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
|
---|
380 | return tdb_transaction_commit(db_ctx->wtdb->tdb) ? -1 : 0;
|
---|
381 | }
|
---|
382 |
|
---|
383 | static int db_tdb_transaction_cancel(struct db_context *db)
|
---|
384 | {
|
---|
385 | struct db_tdb_ctx *db_ctx =
|
---|
386 | talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
|
---|
387 | tdb_transaction_cancel(db_ctx->wtdb->tdb);
|
---|
388 | return 0;
|
---|
389 | }
|
---|
390 |
|
---|
391 | static size_t db_tdb_id(struct db_context *db, uint8_t *id, size_t idlen)
|
---|
392 | {
|
---|
393 | struct db_tdb_ctx *db_ctx =
|
---|
394 | talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
|
---|
395 |
|
---|
396 | if (idlen >= sizeof(db_ctx->id)) {
|
---|
397 | memcpy(id, &db_ctx->id, sizeof(db_ctx->id));
|
---|
398 | }
|
---|
399 |
|
---|
400 | return sizeof(db_ctx->id);
|
---|
401 | }
|
---|
402 |
|
---|
403 | struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
|
---|
404 | const char *name,
|
---|
405 | int hash_size, int tdb_flags,
|
---|
406 | int open_flags, mode_t mode,
|
---|
407 | enum dbwrap_lock_order lock_order,
|
---|
408 | uint64_t dbwrap_flags)
|
---|
409 | {
|
---|
410 | struct db_context *result = NULL;
|
---|
411 | struct db_tdb_ctx *db_tdb;
|
---|
412 | struct stat st;
|
---|
413 |
|
---|
414 | result = talloc_zero(mem_ctx, struct db_context);
|
---|
415 | if (result == NULL) {
|
---|
416 | DEBUG(0, ("talloc failed\n"));
|
---|
417 | goto fail;
|
---|
418 | }
|
---|
419 |
|
---|
420 | result->private_data = db_tdb = talloc(result, struct db_tdb_ctx);
|
---|
421 | if (db_tdb == NULL) {
|
---|
422 | DEBUG(0, ("talloc failed\n"));
|
---|
423 | goto fail;
|
---|
424 | }
|
---|
425 | result->lock_order = lock_order;
|
---|
426 |
|
---|
427 | db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags,
|
---|
428 | open_flags, mode);
|
---|
429 | if (db_tdb->wtdb == NULL) {
|
---|
430 | DEBUG(3, ("Could not open tdb: %s\n", strerror(errno)));
|
---|
431 | goto fail;
|
---|
432 | }
|
---|
433 |
|
---|
434 | ZERO_STRUCT(db_tdb->id);
|
---|
435 |
|
---|
436 | if (fstat(tdb_fd(db_tdb->wtdb->tdb), &st) == -1) {
|
---|
437 | DEBUG(3, ("fstat failed: %s\n", strerror(errno)));
|
---|
438 | goto fail;
|
---|
439 | }
|
---|
440 | db_tdb->id.dev = st.st_dev;
|
---|
441 | db_tdb->id.ino = st.st_ino;
|
---|
442 |
|
---|
443 | result->fetch_locked = db_tdb_fetch_locked;
|
---|
444 | result->try_fetch_locked = db_tdb_try_fetch_locked;
|
---|
445 | result->traverse = db_tdb_traverse;
|
---|
446 | result->traverse_read = db_tdb_traverse_read;
|
---|
447 | result->parse_record = db_tdb_parse;
|
---|
448 | result->get_seqnum = db_tdb_get_seqnum;
|
---|
449 | result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
|
---|
450 | result->transaction_start = db_tdb_transaction_start;
|
---|
451 | result->transaction_start_nonblock = db_tdb_transaction_start_nonblock;
|
---|
452 | result->transaction_commit = db_tdb_transaction_commit;
|
---|
453 | result->transaction_cancel = db_tdb_transaction_cancel;
|
---|
454 | result->exists = db_tdb_exists;
|
---|
455 | result->wipe = db_tdb_wipe;
|
---|
456 | result->id = db_tdb_id;
|
---|
457 | result->check = db_tdb_check;
|
---|
458 | result->name = tdb_name(db_tdb->wtdb->tdb);
|
---|
459 | return result;
|
---|
460 |
|
---|
461 | fail:
|
---|
462 | TALLOC_FREE(result);
|
---|
463 | return NULL;
|
---|
464 | }
|
---|