2 Unix SMB/CIFS implementation.
3 Database interface wrapper around ntdb
4 Copyright (C) Volker Lendecke 2005-2007
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/>.
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_private.h"
24 #include "dbwrap/dbwrap_ntdb.h"
25 #include "system/filesys.h"
26 #include "lib/util/util_ntdb.h"
27 #include "ccan/str/str.h"
30 struct ntdb_context
*ntdb
;
38 static int tdb_store_flag_to_ntdb(int tdb_flag
)
41 /* In fact, any value defaults to TDB_REPLACE in tdb! */
50 smb_panic("unknown tdb_flag");
54 static NTSTATUS
db_ntdb_store(struct db_record
*rec
, NTDB_DATA data
, int flag
)
56 int ntdb_flag
= tdb_store_flag_to_ntdb(flag
);
57 struct db_ntdb_ctx
*ctx
= talloc_get_type_abort(rec
->private_data
,
61 * This has a bug: We need to replace rec->value for correct
62 * operation, but right now brlock and locking don't use the value
63 * anymore after it was stored.
66 if (ntdb_store(ctx
->ntdb
, rec
->key
, data
, ntdb_flag
) == NTDB_SUCCESS
) {
69 return NT_STATUS_UNSUCCESSFUL
;
72 static NTSTATUS
db_ntdb_delete(struct db_record
*rec
)
75 struct db_ntdb_ctx
*ctx
= talloc_get_type_abort(rec
->private_data
,
78 err
= ntdb_delete(ctx
->ntdb
, rec
->key
);
79 if (err
== NTDB_SUCCESS
) {
83 if (err
== NTDB_ERR_NOEXIST
) {
84 return NT_STATUS_NOT_FOUND
;
87 return NT_STATUS_UNSUCCESSFUL
;
90 static void db_ntdb_log_key(const char *prefix
, NTDB_DATA key
)
95 if (DEBUGLEVEL
< 10) {
99 if (DEBUGLEVEL
== 10) {
101 * Only fully spam at debuglevel > 10
103 len
= MIN(10, key
.dsize
);
105 keystr
= hex_encode_talloc(talloc_tos(), (unsigned char *)(key
.dptr
),
107 DEBUG(10, ("%s key %s\n", prefix
, keystr
));
111 static int db_ntdb_record_destr(struct db_record
* data
)
113 struct db_ntdb_ctx
*ctx
=
114 talloc_get_type_abort(data
->private_data
, struct db_ntdb_ctx
);
116 db_ntdb_log_key("Unlocking", data
->key
);
117 ntdb_chainunlock(ctx
->ntdb
, data
->key
);
121 struct ntdb_fetch_locked_state
{
123 struct db_record
*result
;
126 static enum NTDB_ERROR
db_ntdb_fetchlock_parse(NTDB_DATA key
, NTDB_DATA data
,
127 struct ntdb_fetch_locked_state
*state
)
129 struct db_record
*result
;
131 result
= (struct db_record
*)talloc_size(
133 sizeof(struct db_record
) + key
.dsize
+ data
.dsize
);
135 if (result
== NULL
) {
138 state
->result
= result
;
140 result
->key
.dsize
= key
.dsize
;
141 result
->key
.dptr
= ((uint8_t *)result
) + sizeof(struct db_record
);
142 memcpy(result
->key
.dptr
, key
.dptr
, key
.dsize
);
144 result
->value
.dsize
= data
.dsize
;
146 if (data
.dsize
> 0) {
147 result
->value
.dptr
= result
->key
.dptr
+key
.dsize
;
148 memcpy(result
->value
.dptr
, data
.dptr
, data
.dsize
);
151 result
->value
.dptr
= NULL
;
157 static struct db_record
*db_ntdb_fetch_locked_internal(
158 struct db_context
*db
, TALLOC_CTX
*mem_ctx
, NTDB_DATA key
)
160 struct db_ntdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
162 struct ntdb_fetch_locked_state state
;
164 NTDB_DATA null
= { NULL
, 0 };
166 state
.mem_ctx
= mem_ctx
;
169 err
= ntdb_parse_record(ctx
->ntdb
, key
, db_ntdb_fetchlock_parse
,
171 if (err
!= NTDB_SUCCESS
&& err
!= NTDB_ERR_NOEXIST
) {
172 ntdb_chainunlock(ctx
->ntdb
, key
);
176 if (state
.result
== NULL
) {
177 db_ntdb_fetchlock_parse(key
, null
, &state
);
180 if (state
.result
== NULL
) {
181 ntdb_chainunlock(ctx
->ntdb
, key
);
185 talloc_set_destructor(state
.result
, db_ntdb_record_destr
);
187 state
.result
->private_data
= talloc_reference(state
.result
, ctx
);
188 state
.result
->store
= db_ntdb_store
;
189 state
.result
->delete_rec
= db_ntdb_delete
;
191 DEBUG(10, ("Allocated locked data 0x%p\n", state
.result
));
196 static struct db_record
*db_ntdb_fetch_locked(
197 struct db_context
*db
, TALLOC_CTX
*mem_ctx
, NTDB_DATA key
)
199 struct db_ntdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
202 db_ntdb_log_key("Locking", key
);
203 if (ntdb_chainlock(ctx
->ntdb
, key
) != 0) {
204 DEBUG(3, ("ntdb_chainlock failed\n"));
207 return db_ntdb_fetch_locked_internal(db
, mem_ctx
, key
);
210 /* Proxy which sets waitflag to false so we never block. */
211 static int lock_nonblock(int fd
, int rw
, off_t off
, off_t len
, bool waitflag
,
214 struct ntdb_attribute_flock
*orig
= _orig
;
216 return orig
->lock(fd
, rw
, off
, len
, false, orig
->data
);
219 static enum NTDB_ERROR
enable_nonblock(struct ntdb_context
*ntdb
,
220 union ntdb_attribute
*orig
)
222 union ntdb_attribute locking
;
223 enum NTDB_ERROR ecode
;
225 orig
->base
.attr
= NTDB_ATTRIBUTE_FLOCK
;
226 ecode
= ntdb_get_attribute(ntdb
, orig
);
227 if (ecode
!= NTDB_SUCCESS
) {
231 /* Replace locking function with our own. */
233 locking
.flock
.data
= orig
;
234 locking
.flock
.lock
= lock_nonblock
;
236 return ntdb_set_attribute(ntdb
, &locking
);
239 static void disable_nonblock(struct ntdb_context
*ntdb
)
241 ntdb_unset_attribute(ntdb
, NTDB_ATTRIBUTE_FLOCK
);
244 static enum NTDB_ERROR
ntdb_chainlock_nonblock(struct ntdb_context
*ntdb
,
247 union ntdb_attribute orig
;
248 enum NTDB_ERROR ecode
;
250 ecode
= enable_nonblock(ntdb
, &orig
);
252 ecode
= ntdb_chainlock(ntdb
, key
);
253 disable_nonblock(ntdb
);
258 static struct db_record
*db_ntdb_try_fetch_locked(
259 struct db_context
*db
, TALLOC_CTX
*mem_ctx
, TDB_DATA key
)
261 struct db_ntdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
264 db_ntdb_log_key("Trying to lock", key
);
265 if (ntdb_chainlock_nonblock(ctx
->ntdb
, key
) != 0) {
266 DEBUG(3, ("ntdb_chainlock_nonblock failed\n"));
269 return db_ntdb_fetch_locked_internal(db
, mem_ctx
, key
);
272 static int db_ntdb_exists(struct db_context
*db
, TDB_DATA key
)
274 struct db_ntdb_ctx
*ctx
= talloc_get_type_abort(
275 db
->private_data
, struct db_ntdb_ctx
);
276 return ntdb_exists(ctx
->ntdb
, key
);
279 static int db_ntdb_wipe(struct db_context
*db
)
281 struct db_ntdb_ctx
*ctx
= talloc_get_type_abort(
282 db
->private_data
, struct db_ntdb_ctx
);
283 if (ntdb_wipe_all(ctx
->ntdb
) != NTDB_SUCCESS
) {
289 static int db_ntdb_check(struct db_context
*db
)
291 struct db_ntdb_ctx
*ctx
= talloc_get_type_abort(
292 db
->private_data
, struct db_ntdb_ctx
);
293 if (ntdb_check(ctx
->ntdb
, NULL
, NULL
) != NTDB_SUCCESS
) {
299 struct db_ntdb_parse_state
{
300 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
306 * ntdb_parse_record expects a parser returning enum NTDB_ERROR,
307 * mixing up ntdb and parser errors. Wrap around that by always
308 * returning NTDB_SUCCESS and have dbwrap_parse_record expect a parser
312 static enum NTDB_ERROR
db_ntdb_parser(NTDB_DATA key
, NTDB_DATA data
,
313 struct db_ntdb_parse_state
*state
)
315 state
->parser(key
, data
, state
->private_data
);
319 static NTSTATUS
db_ntdb_parse(struct db_context
*db
, TDB_DATA key
,
320 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
324 struct db_ntdb_ctx
*ctx
= talloc_get_type_abort(
325 db
->private_data
, struct db_ntdb_ctx
);
326 struct db_ntdb_parse_state state
;
329 state
.parser
= parser
;
330 state
.private_data
= private_data
;
332 err
= ntdb_parse_record(ctx
->ntdb
, key
, db_ntdb_parser
, &state
);
333 return map_nt_error_from_ntdb(err
);
336 struct db_ntdb_traverse_ctx
{
337 struct db_context
*db
;
338 int (*f
)(struct db_record
*rec
, void *private_data
);
342 static int db_ntdb_traverse_func(struct ntdb_context
*ntdb
,
343 NTDB_DATA kbuf
, NTDB_DATA dbuf
,
344 struct db_ntdb_traverse_ctx
*ctx
)
346 struct db_record rec
;
350 rec
.store
= db_ntdb_store
;
351 rec
.delete_rec
= db_ntdb_delete
;
352 rec
.private_data
= ctx
->db
->private_data
;
355 return ctx
->f(&rec
, ctx
->private_data
);
358 static int db_ntdb_traverse(struct db_context
*db
,
359 int (*f
)(struct db_record
*rec
, void *private_data
),
362 struct db_ntdb_ctx
*db_ctx
=
363 talloc_get_type_abort(db
->private_data
, struct db_ntdb_ctx
);
364 struct db_ntdb_traverse_ctx ctx
;
369 ctx
.private_data
= private_data
;
370 ret
= ntdb_traverse(db_ctx
->ntdb
, db_ntdb_traverse_func
, &ctx
);
376 /* Make sure we don't truncate! */
377 if ((int)ret
!= ret
) {
383 static NTSTATUS
db_ntdb_store_deny(struct db_record
*rec
, NTDB_DATA data
, int flag
)
385 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
388 static NTSTATUS
db_ntdb_delete_deny(struct db_record
*rec
)
390 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
393 static int db_ntdb_traverse_read_func(struct ntdb_context
*ntdb
,
394 NTDB_DATA kbuf
, NTDB_DATA dbuf
,
395 struct db_ntdb_traverse_ctx
*ctx
)
397 struct db_record rec
;
401 rec
.store
= db_ntdb_store_deny
;
402 rec
.delete_rec
= db_ntdb_delete_deny
;
403 rec
.private_data
= ctx
->db
->private_data
;
406 return ctx
->f(&rec
, ctx
->private_data
);
409 static int db_ntdb_traverse_read(struct db_context
*db
,
410 int (*f
)(struct db_record
*rec
,
414 struct db_ntdb_ctx
*db_ctx
=
415 talloc_get_type_abort(db
->private_data
, struct db_ntdb_ctx
);
416 struct db_ntdb_traverse_ctx ctx
;
421 ctx
.private_data
= private_data
;
423 /* This is a bit of paranoia to check that f() isn't altering
425 if (ntdb_get_flags(db_ctx
->ntdb
) & NTDB_RDONLY
) {
426 ret
= ntdb_traverse(db_ctx
->ntdb
, db_ntdb_traverse_read_func
,
429 ntdb_add_flag(db_ctx
->ntdb
, NTDB_RDONLY
);
430 ret
= ntdb_traverse(db_ctx
->ntdb
, db_ntdb_traverse_read_func
,
432 ntdb_remove_flag(db_ctx
->ntdb
, NTDB_RDONLY
);
439 /* Make sure we don't truncate! */
440 if ((int)ret
!= ret
) {
446 static int db_ntdb_get_seqnum(struct db_context
*db
)
449 struct db_ntdb_ctx
*db_ctx
=
450 talloc_get_type_abort(db
->private_data
, struct db_ntdb_ctx
);
451 return ntdb_get_seqnum(db_ctx
->ntdb
);
454 static int db_ntdb_transaction_start(struct db_context
*db
)
456 struct db_ntdb_ctx
*db_ctx
=
457 talloc_get_type_abort(db
->private_data
, struct db_ntdb_ctx
);
458 return ntdb_transaction_start(db_ctx
->ntdb
) == NTDB_SUCCESS
? 0 : -1;
461 static NTSTATUS
db_ntdb_transaction_start_nonblock(struct db_context
*db
)
463 union ntdb_attribute orig
;
464 enum NTDB_ERROR ecode
;
465 struct db_ntdb_ctx
*db_ctx
=
466 talloc_get_type_abort(db
->private_data
, struct db_ntdb_ctx
);
468 ecode
= enable_nonblock(db_ctx
->ntdb
, &orig
);
470 ecode
= ntdb_transaction_start(db_ctx
->ntdb
);
471 disable_nonblock(db_ctx
->ntdb
);
473 return map_nt_error_from_ntdb(ecode
);
476 static int db_ntdb_transaction_commit(struct db_context
*db
)
478 struct db_ntdb_ctx
*db_ctx
=
479 talloc_get_type_abort(db
->private_data
, struct db_ntdb_ctx
);
480 return ntdb_transaction_commit(db_ctx
->ntdb
) == NTDB_SUCCESS
? 0 : -1;
483 static int db_ntdb_transaction_cancel(struct db_context
*db
)
485 struct db_ntdb_ctx
*db_ctx
=
486 talloc_get_type_abort(db
->private_data
, struct db_ntdb_ctx
);
487 ntdb_transaction_cancel(db_ctx
->ntdb
);
491 static void db_ntdb_id(struct db_context
*db
, const uint8_t **id
, size_t *idlen
)
493 struct db_ntdb_ctx
*db_ctx
=
494 talloc_get_type_abort(db
->private_data
, struct db_ntdb_ctx
);
495 *id
= (uint8_t *)&db_ctx
->id
;
496 *idlen
= sizeof(db_ctx
->id
);
499 /* Don't ask this to open a .tdb file: dbwrap_local_open will catch that. */
500 struct db_context
*db_open_ntdb(TALLOC_CTX
*mem_ctx
,
501 struct loadparm_context
*lp_ctx
,
502 const char *ntdbname
,
503 int hash_size
, int ntdb_flags
,
504 int open_flags
, mode_t mode
,
505 enum dbwrap_lock_order lock_order
)
507 struct db_context
*result
= NULL
;
508 struct db_ntdb_ctx
*db_ntdb
;
510 union ntdb_attribute hattr
;
512 if ((ntdb_flags
& NTDB_INTERNAL
) && !ntdbname
) {
513 ntdbname
= "unnamed";
516 /* Extra paranoia. */
517 if (strends(ntdbname
, ".tdb")) {
518 DEBUG(0, ("can't try to open %s with ntdb!", ntdbname
));
522 /* We only use this if hsize is non-zero. */
523 hattr
.base
.attr
= NTDB_ATTRIBUTE_HASHSIZE
;
524 hattr
.base
.next
= NULL
;
525 hattr
.hashsize
.size
= hash_size
;
527 result
= talloc_zero(mem_ctx
, struct db_context
);
528 if (result
== NULL
) {
529 DEBUG(0, ("talloc failed\n"));
533 result
->private_data
= db_ntdb
= talloc(result
, struct db_ntdb_ctx
);
534 if (db_ntdb
== NULL
) {
535 DEBUG(0, ("talloc failed\n"));
538 result
->lock_order
= lock_order
;
540 db_ntdb
->ntdb
= ntdb_new(db_ntdb
, ntdbname
, ntdb_flags
,
542 hash_size
? &hattr
: NULL
, lp_ctx
);
543 if (db_ntdb
->ntdb
== NULL
) {
544 DEBUG(3, ("Could not open ntdb %s: %s\n",
545 ntdbname
, strerror(errno
)));
549 ZERO_STRUCT(db_ntdb
->id
);
551 if (fstat(ntdb_fd(db_ntdb
->ntdb
), &st
) == -1) {
552 DEBUG(3, ("fstat failed: %s\n", strerror(errno
)));
555 db_ntdb
->id
.dev
= st
.st_dev
;
556 db_ntdb
->id
.ino
= st
.st_ino
;
558 result
->fetch_locked
= db_ntdb_fetch_locked
;
559 result
->try_fetch_locked
= db_ntdb_try_fetch_locked
;
560 result
->traverse
= db_ntdb_traverse
;
561 result
->traverse_read
= db_ntdb_traverse_read
;
562 result
->parse_record
= db_ntdb_parse
;
563 result
->get_seqnum
= db_ntdb_get_seqnum
;
564 result
->persistent
= ((ntdb_flags
& NTDB_CLEAR_IF_FIRST
) == 0);
565 result
->transaction_start
= db_ntdb_transaction_start
;
566 result
->transaction_start_nonblock
= db_ntdb_transaction_start_nonblock
;
567 result
->transaction_commit
= db_ntdb_transaction_commit
;
568 result
->transaction_cancel
= db_ntdb_transaction_cancel
;
569 result
->exists
= db_ntdb_exists
;
570 result
->wipe
= db_ntdb_wipe
;
571 result
->id
= db_ntdb_id
;
572 result
->check
= db_ntdb_check
;
573 result
->stored_callback
= NULL
;
574 result
->name
= ntdb_name(db_ntdb
->ntdb
);
575 result
->hash_size
= hash_size
;
579 if (result
!= NULL
) {