2 Unix SMB/CIFS implementation.
3 Utility functions for the dbwrap API
4 Copyright (C) Volker Lendecke 2007
5 Copyright (C) Michael Adam 2009
6 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
8 Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "lib/util/util_tdb.h"
29 struct dbwrap_fetch_int32_state
{
34 static void dbwrap_fetch_int32_parser(TDB_DATA key
, TDB_DATA data
,
37 struct dbwrap_fetch_int32_state
*state
=
38 (struct dbwrap_fetch_int32_state
*)private_data
;
40 if (data
.dsize
!= sizeof(state
->result
)) {
41 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
44 state
->result
= IVAL(data
.dptr
, 0);
45 state
->status
= NT_STATUS_OK
;
48 NTSTATUS
dbwrap_fetch_int32(struct db_context
*db
, TDB_DATA key
,
51 struct dbwrap_fetch_int32_state state
;
55 return NT_STATUS_INVALID_PARAMETER
;
58 state
.status
= NT_STATUS_INTERNAL_ERROR
;
60 status
= dbwrap_parse_record(db
, key
, dbwrap_fetch_int32_parser
, &state
);
61 if (!NT_STATUS_IS_OK(status
)) {
65 if (NT_STATUS_IS_OK(state
.status
)) {
66 *result
= state
.result
;
71 NTSTATUS
dbwrap_fetch_int32_bystring(struct db_context
*db
, const char *keystr
,
74 return dbwrap_fetch_int32(db
, string_term_tdb_data(keystr
), result
);
77 NTSTATUS
dbwrap_store_int32_bystring(struct db_context
*db
, const char *keystr
,
80 struct db_record
*rec
;
84 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
85 string_term_tdb_data(keystr
));
87 return NT_STATUS_UNSUCCESSFUL
;
90 SIVAL(&v_store
, 0, v
);
92 status
= dbwrap_record_store(rec
,
93 make_tdb_data((const uint8_t *)&v_store
,
100 struct dbwrap_fetch_uint32_state
{
105 static void dbwrap_fetch_uint32_parser(TDB_DATA key
, TDB_DATA data
,
108 struct dbwrap_fetch_uint32_state
*state
=
109 (struct dbwrap_fetch_uint32_state
*)private_data
;
111 if (data
.dsize
!= sizeof(state
->result
)) {
112 state
->status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
115 state
->result
= IVAL(data
.dptr
, 0);
116 state
->status
= NT_STATUS_OK
;
119 NTSTATUS
dbwrap_fetch_uint32_bystring(struct db_context
*db
,
120 const char *keystr
, uint32_t *val
)
122 struct dbwrap_fetch_uint32_state state
;
126 return NT_STATUS_INVALID_PARAMETER
;
129 state
.status
= NT_STATUS_INTERNAL_ERROR
;
131 status
= dbwrap_parse_record(db
, string_term_tdb_data(keystr
),
132 dbwrap_fetch_uint32_parser
, &state
);
133 if (!NT_STATUS_IS_OK(status
)) {
136 if (NT_STATUS_IS_OK(state
.status
)) {
142 NTSTATUS
dbwrap_store_uint32_bystring(struct db_context
*db
,
143 const char *keystr
, uint32_t v
)
145 struct db_record
*rec
;
149 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
150 string_term_tdb_data(keystr
));
152 return NT_STATUS_INVALID_PARAMETER
;
155 SIVAL(&v_store
, 0, v
);
157 status
= dbwrap_record_store(rec
,
158 make_tdb_data((const uint8_t *)&v_store
,
166 * Atomic unsigned integer change (addition):
168 * if value does not exist yet in the db, use *oldval as initial old value.
169 * return old value in *oldval.
170 * store *oldval + change_val to db.
173 struct dbwrap_change_uint32_atomic_context
{
179 static NTSTATUS
dbwrap_change_uint32_atomic_action(struct db_context
*db
,
182 struct db_record
*rec
;
183 uint32_t val
= (uint32_t)-1;
186 struct dbwrap_change_uint32_atomic_context
*state
;
189 state
= (struct dbwrap_change_uint32_atomic_context
*)private_data
;
191 rec
= dbwrap_fetch_locked(db
, talloc_tos(),
192 string_term_tdb_data(state
->keystr
));
194 return NT_STATUS_UNSUCCESSFUL
;
197 value
= dbwrap_record_get_value(rec
);
199 if (value
.dptr
== NULL
) {
200 val
= *(state
->oldval
);
201 } else if (value
.dsize
== sizeof(val
)) {
202 val
= IVAL(value
.dptr
, 0);
203 *(state
->oldval
) = val
;
205 ret
= NT_STATUS_UNSUCCESSFUL
;
209 val
+= state
->change_val
;
211 SIVAL(&v_store
, 0, val
);
213 ret
= dbwrap_record_store(rec
,
214 make_tdb_data((const uint8_t *)&v_store
,
223 NTSTATUS
dbwrap_change_uint32_atomic_bystring(struct db_context
*db
,
229 struct dbwrap_change_uint32_atomic_context state
;
231 state
.keystr
= keystr
;
232 state
.oldval
= oldval
;
233 state
.change_val
= change_val
;
235 ret
= dbwrap_change_uint32_atomic_action(db
, &state
);
240 NTSTATUS
dbwrap_trans_change_uint32_atomic_bystring(struct db_context
*db
,
246 struct dbwrap_change_uint32_atomic_context state
;
248 state
.keystr
= keystr
;
249 state
.oldval
= oldval
;
250 state
.change_val
= change_val
;
252 ret
= dbwrap_trans_do(db
, dbwrap_change_uint32_atomic_action
, &state
);
258 * Atomic integer change (addition):
260 * if value does not exist yet in the db, use *oldval as initial old value.
261 * return old value in *oldval.
262 * store *oldval + change_val to db.
265 struct dbwrap_change_int32_atomic_context
{
271 static NTSTATUS
dbwrap_change_int32_atomic_action(struct db_context
*db
,
274 struct db_record
*rec
;
278 struct dbwrap_change_int32_atomic_context
*state
;
281 state
= (struct dbwrap_change_int32_atomic_context
*)private_data
;
283 rec
= dbwrap_fetch_locked(db
, talloc_tos(), state
->key
);
285 return NT_STATUS_UNSUCCESSFUL
;
288 value
= dbwrap_record_get_value(rec
);
290 if (value
.dptr
== NULL
) {
291 val
= *(state
->oldval
);
292 } else if (value
.dsize
== sizeof(val
)) {
293 val
= IVAL(value
.dptr
, 0);
294 *(state
->oldval
) = val
;
296 ret
= NT_STATUS_UNSUCCESSFUL
;
300 val
+= state
->change_val
;
302 SIVAL(&v_store
, 0, val
);
304 ret
= dbwrap_record_store(rec
,
305 make_tdb_data((const uint8_t *)&v_store
,
314 NTSTATUS
dbwrap_change_int32_atomic(struct db_context
*db
,
320 struct dbwrap_change_int32_atomic_context state
;
323 state
.oldval
= oldval
;
324 state
.change_val
= change_val
;
326 ret
= dbwrap_change_int32_atomic_action(db
, &state
);
331 NTSTATUS
dbwrap_change_int32_atomic_bystring(struct db_context
*db
,
336 return dbwrap_change_int32_atomic(db
, string_term_tdb_data(keystr
),
340 NTSTATUS
dbwrap_trans_change_int32_atomic_bystring(struct db_context
*db
,
346 struct dbwrap_change_int32_atomic_context state
;
348 state
.key
= string_term_tdb_data(keystr
);
349 state
.oldval
= oldval
;
350 state
.change_val
= change_val
;
352 ret
= dbwrap_trans_do(db
, dbwrap_change_int32_atomic_action
, &state
);
357 struct dbwrap_store_context
{
363 static NTSTATUS
dbwrap_store_action(struct db_context
*db
, void *private_data
)
365 struct db_record
*rec
= NULL
;
367 struct dbwrap_store_context
*store_ctx
;
369 store_ctx
= (struct dbwrap_store_context
*)private_data
;
371 rec
= dbwrap_fetch_locked(db
, talloc_tos(), *(store_ctx
->key
));
373 DEBUG(5, ("fetch_locked failed\n"));
374 return NT_STATUS_NO_MEMORY
;
377 status
= dbwrap_record_store(rec
, *(store_ctx
->dbuf
), store_ctx
->flag
);
378 if (!NT_STATUS_IS_OK(status
)) {
379 DEBUG(5, ("store returned %s\n", nt_errstr(status
)));
386 NTSTATUS
dbwrap_trans_store(struct db_context
*db
, TDB_DATA key
, TDB_DATA dbuf
,
390 struct dbwrap_store_context store_ctx
;
392 store_ctx
.key
= &key
;
393 store_ctx
.dbuf
= &dbuf
;
394 store_ctx
.flag
= flag
;
396 status
= dbwrap_trans_do(db
, dbwrap_store_action
, &store_ctx
);
401 static NTSTATUS
dbwrap_delete_action(struct db_context
* db
, void *private_data
)
404 struct db_record
*rec
;
405 TDB_DATA
*key
= (TDB_DATA
*)private_data
;
407 rec
= dbwrap_fetch_locked(db
, talloc_tos(), *key
);
409 DEBUG(5, ("fetch_locked failed\n"));
410 return NT_STATUS_NO_MEMORY
;
413 status
= dbwrap_record_delete(rec
);
414 if (!NT_STATUS_IS_OK(status
)) {
415 DEBUG(5, ("delete_rec returned %s\n", nt_errstr(status
)));
422 NTSTATUS
dbwrap_trans_delete(struct db_context
*db
, TDB_DATA key
)
426 status
= dbwrap_trans_do(db
, dbwrap_delete_action
, &key
);
431 NTSTATUS
dbwrap_trans_store_int32_bystring(struct db_context
*db
,
437 SIVAL(&v_store
, 0, v
);
439 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
440 make_tdb_data((const uint8_t *)&v_store
,
445 NTSTATUS
dbwrap_trans_store_uint32_bystring(struct db_context
*db
,
451 SIVAL(&v_store
, 0, v
);
453 return dbwrap_trans_store(db
, string_term_tdb_data(keystr
),
454 make_tdb_data((const uint8_t *)&v_store
,
459 NTSTATUS
dbwrap_trans_store_bystring(struct db_context
*db
, const char *key
,
460 TDB_DATA data
, int flags
)
462 return dbwrap_trans_store(db
, string_term_tdb_data(key
), data
, flags
);
465 NTSTATUS
dbwrap_trans_delete_bystring(struct db_context
*db
, const char *key
)
467 return dbwrap_trans_delete(db
, string_term_tdb_data(key
));
471 * Wrap db action(s) into a transaction.
473 NTSTATUS
dbwrap_trans_do(struct db_context
*db
,
474 NTSTATUS (*action
)(struct db_context
*, void *),
480 res
= dbwrap_transaction_start(db
);
482 DEBUG(5, ("transaction_start failed\n"));
483 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
486 status
= action(db
, private_data
);
487 if (!NT_STATUS_IS_OK(status
)) {
488 if (dbwrap_transaction_cancel(db
) != 0) {
489 smb_panic("Cancelling transaction failed");
494 res
= dbwrap_transaction_commit(db
);
499 DEBUG(2, ("transaction_commit failed\n"));
500 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
503 struct dbwrap_trans_traverse_action_ctx
{
504 int (*f
)(struct db_record
* rec
, void* private_data
);
509 static NTSTATUS
dbwrap_trans_traverse_action(struct db_context
* db
, void* private_data
)
511 struct dbwrap_trans_traverse_action_ctx
* ctx
=
512 (struct dbwrap_trans_traverse_action_ctx
*)private_data
;
514 NTSTATUS status
= dbwrap_traverse(db
, ctx
->f
, ctx
->private_data
, NULL
);
519 NTSTATUS
dbwrap_trans_traverse(struct db_context
*db
,
520 int (*f
)(struct db_record
*, void*),
523 struct dbwrap_trans_traverse_action_ctx ctx
= {
525 .private_data
= private_data
,
527 return dbwrap_trans_do(db
, dbwrap_trans_traverse_action
, &ctx
);
530 NTSTATUS
dbwrap_delete_bystring(struct db_context
*db
, const char *key
)
532 return dbwrap_delete(db
, string_term_tdb_data(key
));
535 NTSTATUS
dbwrap_store_bystring(struct db_context
*db
, const char *key
,
536 TDB_DATA data
, int flags
)
538 return dbwrap_store(db
, string_term_tdb_data(key
), data
, flags
);
541 NTSTATUS
dbwrap_fetch_bystring(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
542 const char *key
, TDB_DATA
*value
)
544 return dbwrap_fetch(db
, mem_ctx
, string_term_tdb_data(key
), value
);
549 NTSTATUS
dbwrap_delete_bystring_upper(struct db_context
*db
, const char *key
)
554 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
555 if (key_upper
== NULL
) {
556 return NT_STATUS_NO_MEMORY
;
559 status
= dbwrap_delete_bystring(db
, key_upper
);
561 talloc_free(key_upper
);
565 NTSTATUS
dbwrap_store_bystring_upper(struct db_context
*db
, const char *key
,
566 TDB_DATA data
, int flags
)
571 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
572 if (key_upper
== NULL
) {
573 return NT_STATUS_NO_MEMORY
;
576 status
= dbwrap_store_bystring(db
, key_upper
, data
, flags
);
578 talloc_free(key_upper
);
582 NTSTATUS
dbwrap_fetch_bystring_upper(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
583 const char *key
, TDB_DATA
*value
)
588 key_upper
= talloc_strdup_upper(talloc_tos(), key
);
589 if (key_upper
== NULL
) {
590 return NT_STATUS_NO_MEMORY
;
593 status
= dbwrap_fetch_bystring(db
, mem_ctx
, key_upper
, value
);
595 talloc_free(key_upper
);
599 struct dbwrap_marshall_state
{
605 static int dbwrap_marshall_fn(struct db_record
*rec
, void *private_data
)
607 struct dbwrap_marshall_state
*state
= private_data
;
611 key
= dbwrap_record_get_key(rec
);
612 value
= dbwrap_record_get_value(rec
);
614 new_dbsize
= state
->dbsize
;
615 new_dbsize
+= 8 + key
.dsize
;
616 new_dbsize
+= 8 + value
.dsize
;
618 if (new_dbsize
<= state
->bufsize
) {
619 uint8_t *p
= state
->buf
+ state
->dbsize
;
621 SBVAL(p
, 0, key
.dsize
);
623 memcpy(p
, key
.dptr
, key
.dsize
);
626 SBVAL(p
, 0, value
.dsize
);
628 memcpy(p
, value
.dptr
, value
.dsize
);
630 state
->dbsize
= new_dbsize
;
634 size_t dbwrap_marshall(struct db_context
*db
, uint8_t *buf
, size_t bufsize
)
636 struct dbwrap_marshall_state state
;
638 state
.bufsize
= bufsize
;
642 dbwrap_traverse_read(db
, dbwrap_marshall_fn
, &state
, NULL
);
647 static ssize_t
dbwrap_unmarshall_get_data(const uint8_t *buf
, size_t buflen
,
648 size_t ofs
, TDB_DATA
*pdata
)
660 space
= buflen
- ofs
;
675 *pdata
= (TDB_DATA
) { .dptr
= discard_const_p(uint8_t, p
),
680 NTSTATUS
dbwrap_parse_marshall_buf(const uint8_t *buf
, size_t buflen
,
681 bool (*fn
)(TDB_DATA key
, TDB_DATA value
,
692 len
= dbwrap_unmarshall_get_data(buf
, buflen
, ofs
, &key
);
697 return NT_STATUS_INVALID_PARAMETER
;
701 len
= dbwrap_unmarshall_get_data(buf
, buflen
, ofs
, &value
);
706 return NT_STATUS_INVALID_PARAMETER
;
710 ok
= fn(key
, value
, private_data
);
719 struct dbwrap_unmarshall_state
{
720 struct db_context
*db
;
724 static bool dbwrap_unmarshall_fn(TDB_DATA key
, TDB_DATA value
,
727 struct dbwrap_unmarshall_state
*state
= private_data
;
728 struct db_record
*rec
;
731 rec
= dbwrap_fetch_locked(state
->db
, state
->db
, key
);
733 DEBUG(10, ("%s: dbwrap_fetch_locked failed\n",
735 state
->ret
= NT_STATUS_NO_MEMORY
;
739 status
= dbwrap_record_store(rec
, value
, 0);
741 if (!NT_STATUS_IS_OK(status
)) {
742 DEBUG(10, ("%s: dbwrap_record_store failed: %s\n",
743 __func__
, nt_errstr(status
)));
751 NTSTATUS
dbwrap_unmarshall(struct db_context
*db
, const uint8_t *buf
,
754 struct dbwrap_unmarshall_state state
= { .db
= db
};
757 status
= dbwrap_parse_marshall_buf(buf
, buflen
,
758 dbwrap_unmarshall_fn
, &state
);
759 if (!NT_STATUS_IS_OK(status
)) {