buildtools/wafsamba: Ensure default python picked up is python3
[Samba.git] / lib / dbwrap / dbwrap_tdb.c
blob00d283c66e6c6ea0e7b6487f25fcb3bb373681a4
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper around tdb
4 Copyright (C) Volker Lendecke 2005-2007
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.
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.
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/>.
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 #include "libcli/util/error.h"
30 struct db_tdb_ctx {
31 struct tdb_wrap *wtdb;
33 struct {
34 dev_t dev;
35 ino_t ino;
36 } id;
39 static NTSTATUS db_tdb_storev(struct db_record *rec,
40 const TDB_DATA *dbufs, int num_dbufs, int flag);
41 static NTSTATUS db_tdb_delete(struct db_record *rec);
43 static void db_tdb_log_key(const char *prefix, TDB_DATA key)
45 size_t len;
46 char *keystr;
47 TALLOC_CTX *frame;
48 if (DEBUGLEVEL < 10) {
49 return;
51 frame = talloc_stackframe();
52 len = key.dsize;
53 if (DEBUGLEVEL == 10) {
55 * Only fully spam at debuglevel > 10
57 len = MIN(10, key.dsize);
59 keystr = hex_encode_talloc(frame, (unsigned char *)(key.dptr),
60 len);
61 DBG_DEBUG("%s key %s\n", prefix, keystr);
62 TALLOC_FREE(frame);
65 static int db_tdb_record_destr(struct db_record* data)
67 struct db_tdb_ctx *ctx =
68 talloc_get_type_abort(data->private_data, struct db_tdb_ctx);
70 db_tdb_log_key("Unlocking", data->key);
71 tdb_chainunlock(ctx->wtdb->tdb, data->key);
72 return 0;
75 struct tdb_fetch_locked_state {
76 TALLOC_CTX *mem_ctx;
77 struct db_record *result;
80 static int db_tdb_fetchlock_parse(TDB_DATA key, TDB_DATA data,
81 void *private_data)
83 struct tdb_fetch_locked_state *state =
84 (struct tdb_fetch_locked_state *)private_data;
85 struct db_record *result;
87 result = (struct db_record *)talloc_size(
88 state->mem_ctx,
89 sizeof(struct db_record) + key.dsize + data.dsize);
91 if (result == NULL) {
92 return 0;
94 state->result = result;
96 result->key.dsize = key.dsize;
97 result->key.dptr = ((uint8_t *)result) + sizeof(struct db_record);
98 memcpy(result->key.dptr, key.dptr, key.dsize);
100 result->value.dsize = data.dsize;
102 if (data.dsize > 0) {
103 result->value.dptr = result->key.dptr+key.dsize;
104 memcpy(result->value.dptr, data.dptr, data.dsize);
106 else {
107 result->value.dptr = NULL;
110 return 0;
113 static struct db_record *db_tdb_fetch_locked_internal(
114 struct db_context *db,
115 struct db_tdb_ctx *ctx,
116 TALLOC_CTX *mem_ctx,
117 TDB_DATA key)
119 struct tdb_fetch_locked_state state;
120 int ret;
122 state = (struct tdb_fetch_locked_state) {
123 .mem_ctx = mem_ctx,
126 ret = tdb_parse_record(ctx->wtdb->tdb,
127 key,
128 db_tdb_fetchlock_parse,
129 &state);
130 if ((ret < 0) && (tdb_error(ctx->wtdb->tdb) != TDB_ERR_NOEXIST)) {
131 tdb_chainunlock(ctx->wtdb->tdb, key);
132 return NULL;
135 if (state.result == NULL) {
136 db_tdb_fetchlock_parse(key, tdb_null, &state);
139 if (state.result == NULL) {
140 tdb_chainunlock(ctx->wtdb->tdb, key);
141 return NULL;
144 talloc_set_destructor(state.result, db_tdb_record_destr);
146 state.result->private_data = ctx;
147 state.result->storev = db_tdb_storev;
148 state.result->delete_rec = db_tdb_delete;
150 DBG_DEBUG("Allocated locked data %p\n", state.result);
152 return state.result;
155 static struct db_record *db_tdb_fetch_locked(
156 struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
158 struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
159 struct db_tdb_ctx);
161 db_tdb_log_key("Locking", key);
162 if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
163 DEBUG(3, ("tdb_chainlock failed\n"));
164 return NULL;
166 return db_tdb_fetch_locked_internal(db, ctx, mem_ctx, key);
169 static struct db_record *db_tdb_try_fetch_locked(
170 struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
172 struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
173 struct db_tdb_ctx);
175 db_tdb_log_key("Trying to lock", key);
176 if (tdb_chainlock_nonblock(ctx->wtdb->tdb, key) != 0) {
177 DEBUG(3, ("tdb_chainlock_nonblock failed\n"));
178 return NULL;
180 return db_tdb_fetch_locked_internal(db, ctx, mem_ctx, key);
183 static NTSTATUS db_tdb_do_locked(struct db_context *db, TDB_DATA key,
184 void (*fn)(struct db_record *rec,
185 void *private_data),
186 void *private_data)
188 struct db_tdb_ctx *ctx = talloc_get_type_abort(
189 db->private_data, struct db_tdb_ctx);
190 uint8_t *buf = NULL;
191 struct db_record rec;
192 int ret;
194 ret = tdb_chainlock(ctx->wtdb->tdb, key);
195 if (ret == -1) {
196 enum TDB_ERROR err = tdb_error(ctx->wtdb->tdb);
197 DBG_DEBUG("tdb_chainlock failed: %s\n",
198 tdb_errorstr(ctx->wtdb->tdb));
199 return map_nt_error_from_tdb(err);
202 ret = tdb_fetch_talloc(ctx->wtdb->tdb, key, ctx, &buf);
204 if ((ret != 0) && (ret != ENOENT)) {
205 DBG_DEBUG("tdb_fetch_talloc failed: %s\n",
206 strerror(errno));
207 tdb_chainunlock(ctx->wtdb->tdb, key);
208 return map_nt_error_from_unix_common(ret);
211 rec = (struct db_record) {
212 .db = db, .key = key,
213 .value = (struct TDB_DATA) { .dptr = buf,
214 .dsize = talloc_get_size(buf) },
215 .storev = db_tdb_storev, .delete_rec = db_tdb_delete,
216 .private_data = ctx
219 fn(&rec, private_data);
221 talloc_free(buf);
223 tdb_chainunlock(ctx->wtdb->tdb, key);
225 return NT_STATUS_OK;
228 static int db_tdb_exists(struct db_context *db, TDB_DATA key)
230 struct db_tdb_ctx *ctx = talloc_get_type_abort(
231 db->private_data, struct db_tdb_ctx);
232 return tdb_exists(ctx->wtdb->tdb, key);
235 static int db_tdb_wipe(struct db_context *db)
237 struct db_tdb_ctx *ctx = talloc_get_type_abort(
238 db->private_data, struct db_tdb_ctx);
239 return tdb_wipe_all(ctx->wtdb->tdb);
242 static int db_tdb_check(struct db_context *db)
244 struct db_tdb_ctx *ctx = talloc_get_type_abort(
245 db->private_data, struct db_tdb_ctx);
246 return tdb_check(ctx->wtdb->tdb, NULL, NULL);
249 struct db_tdb_parse_state {
250 void (*parser)(TDB_DATA key, TDB_DATA data,
251 void *private_data);
252 void *private_data;
256 * tdb_parse_record expects a parser returning int, mixing up tdb and
257 * parser errors. Wrap around that by always returning 0 and have
258 * dbwrap_parse_record expect a parser returning void.
261 static int db_tdb_parser(TDB_DATA key, TDB_DATA data, void *private_data)
263 struct db_tdb_parse_state *state =
264 (struct db_tdb_parse_state *)private_data;
265 state->parser(key, data, state->private_data);
266 return 0;
269 static NTSTATUS db_tdb_parse(struct db_context *db, TDB_DATA key,
270 void (*parser)(TDB_DATA key, TDB_DATA data,
271 void *private_data),
272 void *private_data)
274 struct db_tdb_ctx *ctx = talloc_get_type_abort(
275 db->private_data, struct db_tdb_ctx);
276 struct db_tdb_parse_state state;
277 int ret;
279 state.parser = parser;
280 state.private_data = private_data;
282 ret = tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_parser, &state);
284 if (ret != 0) {
285 return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb));
287 return NT_STATUS_OK;
290 static NTSTATUS db_tdb_storev(struct db_record *rec,
291 const TDB_DATA *dbufs, int num_dbufs, int flag)
293 struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
294 struct db_tdb_ctx);
295 int ret;
298 * This has a bug: We need to replace rec->value for correct
299 * operation, but right now brlock and locking don't use the value
300 * anymore after it was stored.
303 ret = tdb_storev(ctx->wtdb->tdb, rec->key, dbufs, num_dbufs, flag);
304 return (ret == 0) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
307 static NTSTATUS db_tdb_delete(struct db_record *rec)
309 struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
310 struct db_tdb_ctx);
312 if (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) {
313 return NT_STATUS_OK;
316 if (tdb_error(ctx->wtdb->tdb) == TDB_ERR_NOEXIST) {
317 return NT_STATUS_NOT_FOUND;
320 return NT_STATUS_UNSUCCESSFUL;
323 struct db_tdb_traverse_ctx {
324 struct db_context *db;
325 int (*f)(struct db_record *rec, void *private_data);
326 void *private_data;
329 static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
330 void *private_data)
332 struct db_tdb_traverse_ctx *ctx =
333 (struct db_tdb_traverse_ctx *)private_data;
334 struct db_record rec;
336 rec.key = kbuf;
337 rec.value = dbuf;
338 rec.storev = db_tdb_storev;
339 rec.delete_rec = db_tdb_delete;
340 rec.private_data = ctx->db->private_data;
341 rec.db = ctx->db;
343 return ctx->f(&rec, ctx->private_data);
346 static int db_tdb_traverse(struct db_context *db,
347 int (*f)(struct db_record *rec, void *private_data),
348 void *private_data)
350 struct db_tdb_ctx *db_ctx =
351 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
352 struct db_tdb_traverse_ctx ctx;
354 ctx.db = db;
355 ctx.f = f;
356 ctx.private_data = private_data;
357 return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx);
360 static NTSTATUS db_tdb_storev_deny(struct db_record *rec,
361 const TDB_DATA *dbufs, int num_dbufs,
362 int flag)
364 return NT_STATUS_MEDIA_WRITE_PROTECTED;
367 static NTSTATUS db_tdb_delete_deny(struct db_record *rec)
369 return NT_STATUS_MEDIA_WRITE_PROTECTED;
372 static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
373 void *private_data)
375 struct db_tdb_traverse_ctx *ctx =
376 (struct db_tdb_traverse_ctx *)private_data;
377 struct db_record rec;
379 rec.key = kbuf;
380 rec.value = dbuf;
381 rec.storev = db_tdb_storev_deny;
382 rec.delete_rec = db_tdb_delete_deny;
383 rec.private_data = ctx->db->private_data;
384 rec.db = ctx->db;
386 return ctx->f(&rec, ctx->private_data);
389 static int db_tdb_traverse_read(struct db_context *db,
390 int (*f)(struct db_record *rec, void *private_data),
391 void *private_data)
393 struct db_tdb_ctx *db_ctx =
394 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
395 struct db_tdb_traverse_ctx ctx;
397 ctx.db = db;
398 ctx.f = f;
399 ctx.private_data = private_data;
400 return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx);
403 static int db_tdb_get_seqnum(struct db_context *db)
406 struct db_tdb_ctx *db_ctx =
407 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
408 return tdb_get_seqnum(db_ctx->wtdb->tdb);
411 static int db_tdb_transaction_start(struct db_context *db)
413 struct db_tdb_ctx *db_ctx =
414 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
415 return tdb_transaction_start(db_ctx->wtdb->tdb) ? -1 : 0;
418 static NTSTATUS db_tdb_transaction_start_nonblock(struct db_context *db)
420 struct db_tdb_ctx *db_ctx =
421 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
422 int ret;
424 ret = tdb_transaction_start_nonblock(db_ctx->wtdb->tdb);
425 if (ret != 0) {
426 return map_nt_error_from_tdb(tdb_error(db_ctx->wtdb->tdb));
428 return NT_STATUS_OK;
431 static int db_tdb_transaction_commit(struct db_context *db)
433 struct db_tdb_ctx *db_ctx =
434 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
435 return tdb_transaction_commit(db_ctx->wtdb->tdb) ? -1 : 0;
438 static int db_tdb_transaction_cancel(struct db_context *db)
440 struct db_tdb_ctx *db_ctx =
441 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
442 tdb_transaction_cancel(db_ctx->wtdb->tdb);
443 return 0;
446 static size_t db_tdb_id(struct db_context *db, uint8_t *id, size_t idlen)
448 struct db_tdb_ctx *db_ctx =
449 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
451 if (idlen >= sizeof(db_ctx->id)) {
452 memcpy(id, &db_ctx->id, sizeof(db_ctx->id));
455 return sizeof(db_ctx->id);
458 struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
459 const char *name,
460 int hash_size, int tdb_flags,
461 int open_flags, mode_t mode,
462 enum dbwrap_lock_order lock_order,
463 uint64_t dbwrap_flags)
465 struct db_context *result = NULL;
466 struct db_tdb_ctx *db_tdb;
467 struct stat st;
469 result = talloc_zero(mem_ctx, struct db_context);
470 if (result == NULL) {
471 DEBUG(0, ("talloc failed\n"));
472 goto fail;
475 result->private_data = db_tdb = talloc(result, struct db_tdb_ctx);
476 if (db_tdb == NULL) {
477 DEBUG(0, ("talloc failed\n"));
478 goto fail;
480 result->lock_order = lock_order;
482 db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags,
483 open_flags, mode);
484 if (db_tdb->wtdb == NULL) {
485 DEBUG(3, ("Could not open tdb: %s\n", strerror(errno)));
486 goto fail;
489 ZERO_STRUCT(db_tdb->id);
491 if (fstat(tdb_fd(db_tdb->wtdb->tdb), &st) == -1) {
492 DEBUG(3, ("fstat failed: %s\n", strerror(errno)));
493 goto fail;
495 db_tdb->id.dev = st.st_dev;
496 db_tdb->id.ino = st.st_ino;
498 result->fetch_locked = db_tdb_fetch_locked;
499 result->try_fetch_locked = db_tdb_try_fetch_locked;
500 result->do_locked = db_tdb_do_locked;
501 result->traverse = db_tdb_traverse;
502 result->traverse_read = db_tdb_traverse_read;
503 result->parse_record = db_tdb_parse;
504 result->get_seqnum = db_tdb_get_seqnum;
505 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
506 result->transaction_start = db_tdb_transaction_start;
507 result->transaction_start_nonblock = db_tdb_transaction_start_nonblock;
508 result->transaction_commit = db_tdb_transaction_commit;
509 result->transaction_cancel = db_tdb_transaction_cancel;
510 result->exists = db_tdb_exists;
511 result->wipe = db_tdb_wipe;
512 result->id = db_tdb_id;
513 result->check = db_tdb_check;
514 result->name = tdb_name(db_tdb->wtdb->tdb);
515 return result;
517 fail:
518 TALLOC_FREE(result);
519 return NULL;