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/>.
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"
31 struct tdb_wrap
*wtdb
;
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
)
48 if (DEBUGLEVEL
< 10) {
51 frame
= talloc_stackframe();
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
),
61 DBG_DEBUG("%s key %s\n", prefix
, keystr
);
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
);
75 struct tdb_fetch_locked_state
{
77 struct db_record
*result
;
80 static int db_tdb_fetchlock_parse(TDB_DATA key
, TDB_DATA 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(
89 sizeof(struct db_record
) + key
.dsize
+ data
.dsize
);
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
);
107 result
->value
.dptr
= NULL
;
113 static struct db_record
*db_tdb_fetch_locked_internal(
114 struct db_context
*db
,
115 struct db_tdb_ctx
*ctx
,
119 struct tdb_fetch_locked_state state
;
122 state
= (struct tdb_fetch_locked_state
) {
126 ret
= tdb_parse_record(ctx
->wtdb
->tdb
,
128 db_tdb_fetchlock_parse
,
130 if ((ret
< 0) && (tdb_error(ctx
->wtdb
->tdb
) != TDB_ERR_NOEXIST
)) {
131 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
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
);
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
);
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
,
161 db_tdb_log_key("Locking", key
);
162 if (tdb_chainlock(ctx
->wtdb
->tdb
, key
) != 0) {
163 DEBUG(3, ("tdb_chainlock failed\n"));
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
,
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"));
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
,
188 struct db_tdb_ctx
*ctx
= talloc_get_type_abort(
189 db
->private_data
, struct db_tdb_ctx
);
191 struct db_record rec
;
194 ret
= tdb_chainlock(ctx
->wtdb
->tdb
, key
);
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",
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
,
219 fn(&rec
, private_data
);
223 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
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
,
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
);
269 static NTSTATUS
db_tdb_parse(struct db_context
*db
, TDB_DATA key
,
270 void (*parser
)(TDB_DATA key
, TDB_DATA 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
;
279 state
.parser
= parser
;
280 state
.private_data
= private_data
;
282 ret
= tdb_parse_record(ctx
->wtdb
->tdb
, key
, db_tdb_parser
, &state
);
285 return map_nt_error_from_tdb(tdb_error(ctx
->wtdb
->tdb
));
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
,
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
,
312 if (tdb_delete(ctx
->wtdb
->tdb
, rec
->key
) == 0) {
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
);
329 static int db_tdb_traverse_func(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
332 struct db_tdb_traverse_ctx
*ctx
=
333 (struct db_tdb_traverse_ctx
*)private_data
;
334 struct db_record rec
;
338 rec
.storev
= db_tdb_storev
;
339 rec
.delete_rec
= db_tdb_delete
;
340 rec
.private_data
= ctx
->db
->private_data
;
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
),
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
;
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
,
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
,
375 struct db_tdb_traverse_ctx
*ctx
=
376 (struct db_tdb_traverse_ctx
*)private_data
;
377 struct db_record rec
;
381 rec
.storev
= db_tdb_storev_deny
;
382 rec
.delete_rec
= db_tdb_delete_deny
;
383 rec
.private_data
= ctx
->db
->private_data
;
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
),
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
;
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
);
424 ret
= tdb_transaction_start_nonblock(db_ctx
->wtdb
->tdb
);
426 return map_nt_error_from_tdb(tdb_error(db_ctx
->wtdb
->tdb
));
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
);
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
,
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
;
469 result
= talloc_zero(mem_ctx
, struct db_context
);
470 if (result
== NULL
) {
471 DEBUG(0, ("talloc failed\n"));
475 result
->private_data
= db_tdb
= talloc(result
, struct db_tdb_ctx
);
476 if (db_tdb
== NULL
) {
477 DEBUG(0, ("talloc failed\n"));
480 result
->lock_order
= lock_order
;
482 db_tdb
->wtdb
= tdb_wrap_open(db_tdb
, name
, hash_size
, tdb_flags
,
484 if (db_tdb
->wtdb
== NULL
) {
485 DEBUG(3, ("Could not open tdb: %s\n", strerror(errno
)));
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
)));
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
);