summaryrefslogtreecommitdiffstats
path: root/lib/dbwrap
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/dbwrap/dbwrap.c736
-rw-r--r--lib/dbwrap/dbwrap.h252
-rw-r--r--lib/dbwrap/dbwrap_local_open.c46
-rw-r--r--lib/dbwrap/dbwrap_private.h93
-rw-r--r--lib/dbwrap/dbwrap_rbt.c589
-rw-r--r--lib/dbwrap/dbwrap_rbt.h29
-rw-r--r--lib/dbwrap/dbwrap_tdb.c518
-rw-r--r--lib/dbwrap/dbwrap_tdb.h35
-rw-r--r--lib/dbwrap/dbwrap_util.c756
-rw-r--r--lib/dbwrap/wscript_build8
10 files changed, 3062 insertions, 0 deletions
diff --git a/lib/dbwrap/dbwrap.c b/lib/dbwrap/dbwrap.c
new file mode 100644
index 0000000..19f6c29
--- /dev/null
+++ b/lib/dbwrap/dbwrap.c
@@ -0,0 +1,736 @@
+/*
+ Unix SMB/CIFS implementation.
+ Database interface wrapper
+ Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
+
+ Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "lib/util/debug.h"
+#include "lib/util/fault.h"
+#include "lib/util/talloc_stack.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_private.h"
+#include "lib/util/util_tdb.h"
+#include "lib/util/tevent_ntstatus.h"
+
+/*
+ * Fall back using fetch if no genuine exists operation is provided
+ */
+
+static int dbwrap_fallback_exists(struct db_context *db, TDB_DATA key)
+{
+ NTSTATUS status = dbwrap_parse_record(db, key, NULL, NULL);
+ return NT_STATUS_IS_OK(status) ? 1 : 0;
+}
+
+static int delete_record(struct db_record *rec, void *data)
+{
+ NTSTATUS status = dbwrap_record_delete(rec);
+ return NT_STATUS_IS_OK(status) ? 0 : -1;
+}
+
+/*
+ * Fallback wipe implementation using traverse and delete if no genuine
+ * wipe operation is provided
+ */
+static int dbwrap_fallback_wipe(struct db_context *db)
+{
+ NTSTATUS status = dbwrap_trans_traverse(db, delete_record, NULL);
+ return NT_STATUS_IS_OK(status) ? 0 : -1;
+}
+
+static int do_nothing(struct db_record *rec, void *unused)
+{
+ return 0;
+}
+
+/*
+ * Fallback check operation: just traverse.
+ */
+static int dbwrap_fallback_check(struct db_context *db)
+{
+ NTSTATUS status = dbwrap_traverse_read(db, do_nothing, NULL, NULL);
+ return NT_STATUS_IS_OK(status) ? 0 : -1;
+}
+
+/*
+ * Wrapper functions for the backend methods
+ */
+
+TDB_DATA dbwrap_record_get_key(const struct db_record *rec)
+{
+ return rec->key;
+}
+
+TDB_DATA dbwrap_record_get_value(const struct db_record *rec)
+{
+ SMB_ASSERT(rec->value_valid);
+ return rec->value;
+}
+
+NTSTATUS dbwrap_record_storev(struct db_record *rec,
+ const TDB_DATA *dbufs, int num_dbufs, int flags)
+{
+ NTSTATUS status;
+
+ /*
+ * Invalidate before rec->storev() is called, give
+ * rec->storev() the chance to re-validate rec->value.
+ */
+ rec->value_valid = false;
+
+ status = rec->storev(rec, dbufs, num_dbufs, flags);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
+ return NT_STATUS_OK;
+}
+
+NTSTATUS dbwrap_record_store(struct db_record *rec, TDB_DATA data, int flags)
+{
+ return dbwrap_record_storev(rec, &data, 1, flags);
+}
+
+NTSTATUS dbwrap_record_delete(struct db_record *rec)
+{
+ NTSTATUS status;
+
+ status = rec->delete_rec(rec);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
+
+ rec->value = tdb_null;
+
+ return NT_STATUS_OK;
+}
+
+struct dbwrap_merge_dbs_state {
+ struct db_context *to;
+ int flags;
+};
+
+/* Copy a single record to the db_context passed in private_data */
+static int dbwrap_merge_dbs_copy_record(struct db_record *rec,
+ void *private_data)
+{
+ struct dbwrap_merge_dbs_state *state = private_data;
+
+ TDB_DATA data = dbwrap_record_get_value(rec);
+ TDB_DATA key = dbwrap_record_get_key(rec);
+ NTSTATUS status = dbwrap_store(state->to, key, data, state->flags);
+
+ return NT_STATUS_IS_OK(status) ? 0 : 1;
+}
+
+NTSTATUS
+dbwrap_merge_dbs(struct db_context *to, struct db_context *from, int flags)
+{
+ struct dbwrap_merge_dbs_state state = {.to = to, .flags = flags};
+
+ return dbwrap_traverse(from,
+ dbwrap_merge_dbs_copy_record,
+ &state,
+ NULL);
+}
+
+const char *locked_dbs[DBWRAP_LOCK_ORDER_MAX];
+
+static void debug_lock_order(int level)
+{
+ int i;
+ DEBUG(level, ("lock order:"));
+ for (i=0; i<DBWRAP_LOCK_ORDER_MAX; i++) {
+ DEBUGADD(level,
+ (" %d:%s",
+ i + 1,
+ locked_dbs[i] ? locked_dbs[i] : "<none>"));
+ }
+ DEBUGADD(level, ("\n"));
+}
+
+void dbwrap_lock_order_lock(const char *db_name,
+ enum dbwrap_lock_order lock_order)
+{
+ int idx;
+
+ DBG_INFO("check lock order %d for %s\n",
+ (int)lock_order,
+ db_name);
+
+ if (!DBWRAP_LOCK_ORDER_VALID(lock_order)) {
+ DBG_ERR("Invalid lock order %d of %s\n",
+ lock_order,
+ db_name);
+ smb_panic("lock order violation");
+ }
+
+ for (idx=lock_order-1; idx<DBWRAP_LOCK_ORDER_MAX; idx++) {
+ if (locked_dbs[idx] != NULL) {
+ DBG_ERR("Lock order violation: Trying %s at %d while "
+ "%s at %d is locked\n",
+ db_name,
+ (int)lock_order,
+ locked_dbs[idx],
+ idx + 1);
+ debug_lock_order(0);
+ smb_panic("lock order violation");
+ }
+ }
+
+ locked_dbs[lock_order-1] = db_name;
+
+ debug_lock_order(10);
+}
+
+void dbwrap_lock_order_unlock(const char *db_name,
+ enum dbwrap_lock_order lock_order)
+{
+ DBG_INFO("release lock order %d for %s\n",
+ (int)lock_order,
+ db_name);
+
+ if (!DBWRAP_LOCK_ORDER_VALID(lock_order)) {
+ DBG_ERR("Invalid lock order %d of %s\n",
+ lock_order,
+ db_name);
+ smb_panic("lock order violation");
+ }
+
+ if (locked_dbs[lock_order-1] == NULL) {
+ DBG_ERR("db %s at order %d unlocked\n",
+ db_name,
+ (int)lock_order);
+ smb_panic("lock order violation");
+ }
+
+ if (locked_dbs[lock_order-1] != db_name) {
+ DBG_ERR("locked db at lock order %d is %s, expected %s\n",
+ (int)lock_order,
+ locked_dbs[lock_order-1],
+ db_name);
+ smb_panic("lock order violation");
+ }
+
+ locked_dbs[lock_order-1] = NULL;
+}
+
+struct dbwrap_lock_order_state {
+ struct db_context *db;
+};
+
+static int dbwrap_lock_order_state_destructor(
+ struct dbwrap_lock_order_state *s)
+{
+ struct db_context *db = s->db;
+ dbwrap_lock_order_unlock(db->name, db->lock_order);
+ return 0;
+}
+
+static struct dbwrap_lock_order_state *dbwrap_check_lock_order(
+ struct db_context *db, TALLOC_CTX *mem_ctx)
+{
+ struct dbwrap_lock_order_state *state;
+
+ state = talloc(mem_ctx, struct dbwrap_lock_order_state);
+ if (state == NULL) {
+ DBG_WARNING("talloc failed\n");
+ return NULL;
+ }
+ state->db = db;
+
+ dbwrap_lock_order_lock(db->name, db->lock_order);
+ talloc_set_destructor(state, dbwrap_lock_order_state_destructor);
+
+ return state;
+}
+
+static struct db_record *dbwrap_fetch_locked_internal(
+ struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key,
+ struct db_record *(*db_fn)(struct db_context *db, TALLOC_CTX *mem_ctx,
+ TDB_DATA key))
+{
+ struct db_record *rec;
+ struct dbwrap_lock_order_state *lock_order = NULL;
+
+ if (db->lock_order != DBWRAP_LOCK_ORDER_NONE) {
+ lock_order = dbwrap_check_lock_order(db, mem_ctx);
+ if (lock_order == NULL) {
+ return NULL;
+ }
+ }
+ rec = db_fn(db, mem_ctx, key);
+ if (rec == NULL) {
+ TALLOC_FREE(lock_order);
+ return NULL;
+ }
+ (void)talloc_steal(rec, lock_order);
+ rec->db = db;
+ return rec;
+}
+
+struct db_record *dbwrap_fetch_locked(struct db_context *db,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA key)
+{
+ return dbwrap_fetch_locked_internal(db, mem_ctx, key,
+ db->fetch_locked);
+}
+
+struct db_context *dbwrap_record_get_db(struct db_record *rec)
+{
+ return rec->db;
+}
+
+struct dbwrap_fetch_state {
+ TALLOC_CTX *mem_ctx;
+ TDB_DATA data;
+};
+
+static void dbwrap_fetch_parser(TDB_DATA key, TDB_DATA data,
+ void *private_data)
+{
+ struct dbwrap_fetch_state *state =
+ (struct dbwrap_fetch_state *)private_data;
+
+ state->data.dsize = data.dsize;
+ state->data.dptr = (uint8_t *)talloc_memdup(state->mem_ctx, data.dptr,
+ data.dsize);
+}
+
+NTSTATUS dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
+ TDB_DATA key, TDB_DATA *value)
+{
+ struct dbwrap_fetch_state state;
+ NTSTATUS status;
+
+ if (value == NULL) {
+ return NT_STATUS_INVALID_PARAMETER;
+ }
+
+ state.mem_ctx = mem_ctx;
+
+ status = dbwrap_parse_record(db, key, dbwrap_fetch_parser, &state);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
+ if ((state.data.dsize != 0) && (state.data.dptr == NULL)) {
+ return NT_STATUS_NO_MEMORY;
+ }
+ *value = state.data;
+ return NT_STATUS_OK;
+}
+
+bool dbwrap_exists(struct db_context *db, TDB_DATA key)
+{
+ int result;
+ if (db->exists != NULL) {
+ result = db->exists(db, key);
+ } else {
+ result = dbwrap_fallback_exists(db,key);
+ }
+ return (result == 1);
+}
+
+struct dbwrap_store_state {
+ TDB_DATA data;
+ int flags;
+ NTSTATUS status;
+};
+
+static void dbwrap_store_fn(
+ struct db_record *rec,
+ TDB_DATA value,
+ void *private_data)
+{
+ struct dbwrap_store_state *state = private_data;
+ state->status = dbwrap_record_store(rec, state->data, state->flags);
+}
+
+NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key,
+ TDB_DATA data, int flags)
+{
+ struct dbwrap_store_state state = { .data = data, .flags = flags };
+ NTSTATUS status;
+
+ status = dbwrap_do_locked(db, key, dbwrap_store_fn, &state);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
+
+ return state.status;
+}
+
+struct dbwrap_delete_state {
+ NTSTATUS status;
+};
+
+static void dbwrap_delete_fn(
+ struct db_record *rec,
+ TDB_DATA value,
+ void *private_data)
+{
+ struct dbwrap_delete_state *state = private_data;
+ state->status = dbwrap_record_delete(rec);
+}
+
+NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key)
+{
+ struct dbwrap_delete_state state = { .status = NT_STATUS_NOT_FOUND };
+ NTSTATUS status;
+
+ status = dbwrap_do_locked(db, key, dbwrap_delete_fn, &state);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
+
+ return state.status;
+}
+
+NTSTATUS dbwrap_traverse(struct db_context *db,
+ int (*f)(struct db_record*, void*),
+ void *private_data,
+ int *count)
+{
+ int ret = db->traverse(db, f, private_data);
+
+ if (ret < 0) {
+ return NT_STATUS_INTERNAL_DB_CORRUPTION;
+ }
+
+ if (count != NULL) {
+ *count = ret;
+ }
+
+ return NT_STATUS_OK;
+}
+
+NTSTATUS dbwrap_traverse_read(struct db_context *db,
+ int (*f)(struct db_record*, void*),
+ void *private_data,
+ int *count)
+{
+ int ret = db->traverse_read(db, f, private_data);
+
+ if (ret < 0) {
+ return NT_STATUS_INTERNAL_DB_CORRUPTION;
+ }
+
+ if (count != NULL) {
+ *count = ret;
+ }
+
+ return NT_STATUS_OK;
+}
+
+static void dbwrap_null_parser(TDB_DATA key, TDB_DATA val, void* data)
+{
+ return;
+}
+
+NTSTATUS dbwrap_parse_record(struct db_context *db, TDB_DATA key,
+ void (*parser)(TDB_DATA key, TDB_DATA data,
+ void *private_data),
+ void *private_data)
+{
+ if (parser == NULL) {
+ parser = dbwrap_null_parser;
+ }
+ return db->parse_record(db, key, parser, private_data);
+}
+
+struct dbwrap_parse_record_state {
+ struct db_context *db;
+ TDB_DATA key;
+ uint8_t _keybuf[64];
+};
+
+static void dbwrap_parse_record_done(struct tevent_req *subreq);
+
+struct tevent_req *dbwrap_parse_record_send(
+ TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct db_context *db,
+ TDB_DATA key,
+ void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data),
+ void *private_data,
+ enum dbwrap_req_state *req_state)
+{
+ struct tevent_req *req = NULL;
+ struct tevent_req *subreq = NULL;
+ struct dbwrap_parse_record_state *state = NULL;
+ NTSTATUS status;
+
+ req = tevent_req_create(mem_ctx, &state, struct dbwrap_parse_record_state);
+ if (req == NULL) {
+ *req_state = DBWRAP_REQ_ERROR;
+ return NULL;
+ }
+
+ *state = (struct dbwrap_parse_record_state) {
+ .db = db,
+ };
+
+ if (parser == NULL) {
+ parser = dbwrap_null_parser;
+ }
+
+ *req_state = DBWRAP_REQ_INIT;
+
+ if (db->parse_record_send == NULL) {
+ /*
+ * Backend doesn't implement async version, call sync one
+ */
+ status = db->parse_record(db, key, parser, private_data);
+ if (tevent_req_nterror(req, status)) {
+ *req_state = DBWRAP_REQ_DONE;
+ return tevent_req_post(req, ev);
+ }
+
+ *req_state = DBWRAP_REQ_DONE;
+ tevent_req_done(req);
+ return tevent_req_post(req, ev);
+ }
+
+ /*
+ * Copy the key into our state ensuring the key data buffer is always
+ * available to all the dbwrap backends over the entire lifetime of the
+ * async request. Otherwise the caller might have free'd the key buffer.
+ */
+ if (key.dsize > sizeof(state->_keybuf)) {
+ state->key.dptr = talloc_memdup(state, key.dptr, key.dsize);
+ if (tevent_req_nomem(state->key.dptr, req)) {
+ return tevent_req_post(req, ev);
+ }
+ } else {
+ memcpy(state->_keybuf, key.dptr, key.dsize);
+ state->key.dptr = state->_keybuf;
+ }
+ state->key.dsize = key.dsize;
+
+ subreq = db->parse_record_send(state,
+ ev,
+ db,
+ state->key,
+ parser,
+ private_data,
+ req_state);
+ if (tevent_req_nomem(subreq, req)) {
+ *req_state = DBWRAP_REQ_ERROR;
+ return tevent_req_post(req, ev);
+ }
+
+ tevent_req_set_callback(subreq,
+ dbwrap_parse_record_done,
+ req);
+ return req;
+}
+
+static void dbwrap_parse_record_done(struct tevent_req *subreq)
+{
+ struct tevent_req *req = tevent_req_callback_data(
+ subreq, struct tevent_req);
+ struct dbwrap_parse_record_state *state = tevent_req_data(
+ req, struct dbwrap_parse_record_state);
+ NTSTATUS status;
+
+ status = state->db->parse_record_recv(subreq);
+ TALLOC_FREE(subreq);
+ if (!NT_STATUS_IS_OK(status)) {
+ tevent_req_nterror(req, status);
+ return;
+ }
+
+ tevent_req_done(req);
+}
+
+NTSTATUS dbwrap_parse_record_recv(struct tevent_req *req)
+{
+ return tevent_req_simple_recv_ntstatus(req);
+}
+
+NTSTATUS dbwrap_do_locked(struct db_context *db, TDB_DATA key,
+ void (*fn)(struct db_record *rec,
+ TDB_DATA value,
+ void *private_data),
+ void *private_data)
+{
+ struct db_record *rec;
+
+ if (db->do_locked != NULL) {
+ NTSTATUS status;
+
+ if (db->lock_order != DBWRAP_LOCK_ORDER_NONE) {
+ dbwrap_lock_order_lock(db->name, db->lock_order);
+ }
+
+ status = db->do_locked(db, key, fn, private_data);
+
+ if (db->lock_order != DBWRAP_LOCK_ORDER_NONE) {
+ dbwrap_lock_order_unlock(db->name, db->lock_order);
+ }
+
+ return status;
+ }
+
+ rec = dbwrap_fetch_locked(db, db, key);
+ if (rec == NULL) {
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ /*
+ * Invalidate rec->value, nobody shall assume it's set from
+ * within dbwrap_do_locked().
+ */
+ rec->value_valid = false;
+
+ fn(rec, rec->value, private_data);
+
+ TALLOC_FREE(rec);
+
+ return NT_STATUS_OK;
+}
+
+int dbwrap_wipe(struct db_context *db)
+{
+ if (db->wipe == NULL) {
+ return dbwrap_fallback_wipe(db);
+ }
+ return db->wipe(db);
+}
+
+int dbwrap_check(struct db_context *db)
+{
+ if (db->check == NULL) {
+ return dbwrap_fallback_check(db);
+ }
+ return db->check(db);
+}
+
+int dbwrap_get_seqnum(struct db_context *db)
+{
+ return db->get_seqnum(db);
+}
+
+int dbwrap_transaction_start(struct db_context *db)
+{
+ if (!db->persistent) {
+ /*
+ * dbwrap_ctdb has two different data models for persistent
+ * and non-persistent databases. Transactions are supported
+ * only for the persistent databases. This check is here to
+ * prevent breakages of the cluster case, autobuild at this
+ * point only tests non-clustered Samba. Before removing this
+ * check, please make sure that this facility has also been
+ * added to dbwrap_ctdb.
+ *
+ * Thanks, vl
+ */
+ DEBUG(1, ("transactions not supported on non-persistent "
+ "database %s\n", db->name));
+ return -1;
+ }
+ return db->transaction_start(db);
+}
+
+NTSTATUS dbwrap_transaction_start_nonblock(struct db_context *db)
+{
+ if (db->transaction_start_nonblock) {
+ return db->transaction_start_nonblock(db);
+ } else {
+ return dbwrap_transaction_start(db) == 0 ? NT_STATUS_OK
+ : NT_STATUS_UNSUCCESSFUL;
+ }
+}
+
+int dbwrap_transaction_commit(struct db_context *db)
+{
+ return db->transaction_commit(db);
+}
+
+int dbwrap_transaction_cancel(struct db_context *db)
+{
+ return db->transaction_cancel(db);
+}
+
+size_t dbwrap_db_id(struct db_context *db, uint8_t *id, size_t idlen)
+{
+ return db->id(db, id, idlen);
+}
+
+bool dbwrap_is_persistent(struct db_context *db)
+{
+ return db->persistent;
+}
+
+const char *dbwrap_name(struct db_context *db)
+{
+ return db->name;
+}
+
+static ssize_t tdb_data_buf(const TDB_DATA *dbufs, int num_dbufs,
+ uint8_t *buf, size_t buflen)
+{
+ size_t needed = 0;
+ uint8_t *p = buf;
+ int i;
+
+ for (i=0; i<num_dbufs; i++) {
+ size_t thislen = dbufs[i].dsize;
+
+ needed += thislen;
+ if (needed < thislen) {
+ /* wrap */
+ return -1;
+ }
+
+ if (p != NULL && (thislen != 0) && (needed <= buflen)) {
+ memcpy(p, dbufs[i].dptr, thislen);
+ p += thislen;
+ }
+ }
+
+ return needed;
+}
+
+
+NTSTATUS dbwrap_merge_dbufs(TDB_DATA *buf, TALLOC_CTX *mem_ctx,
+ const TDB_DATA *dbufs, int num_dbufs)
+{
+ ssize_t len = tdb_data_buf(dbufs, num_dbufs, NULL, 0);
+
+ if (len == -1) {
+ return NT_STATUS_INVALID_PARAMETER;
+ }
+
+ if (buf->dsize != len) {
+ uint8_t *tmp;
+
+ tmp = talloc_realloc(mem_ctx, buf->dptr, uint8_t, len);
+ if (tmp == NULL && len != 0) {
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ buf->dptr = tmp;
+ buf->dsize = len;
+ }
+
+ tdb_data_buf(dbufs, num_dbufs, buf->dptr, buf->dsize);
+
+ return NT_STATUS_OK;
+}
diff --git a/lib/dbwrap/dbwrap.h b/lib/dbwrap/dbwrap.h
new file mode 100644
index 0000000..abc5161
--- /dev/null
+++ b/lib/dbwrap/dbwrap.h
@@ -0,0 +1,252 @@
+/*
+ Unix SMB/CIFS implementation.
+ Database interface wrapper around tdb
+ Copyright (C) Volker Lendecke 2005-2007
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __DBWRAP_H__
+#define __DBWRAP_H__
+
+#include "replace.h"
+#include <talloc.h>
+#include <tevent.h>
+#include "libcli/util/ntstatus.h"
+#include "tdb.h"
+
+struct db_record;
+struct db_context;
+
+enum dbwrap_lock_order {
+ DBWRAP_LOCK_ORDER_NONE = 0, /* Don't check lock orders for this db. */
+ DBWRAP_LOCK_ORDER_1 = 1,
+ DBWRAP_LOCK_ORDER_2 = 2,
+ DBWRAP_LOCK_ORDER_3 = 3,
+ DBWRAP_LOCK_ORDER_4 = 4
+};
+
+#define DBWRAP_FLAG_NONE 0x0000000000000000ULL
+#define DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS 0x0000000000000001ULL
+
+enum dbwrap_req_state {
+ /**
+ * We are creating the request
+ */
+ DBWRAP_REQ_INIT,
+ /**
+ * The request is queued and waiting to be dispatched
+ */
+ DBWRAP_REQ_QUEUED,
+ /**
+ * We are waiting to receive the reply
+ */
+ DBWRAP_REQ_DISPATCHED,
+ /**
+ * The request is finished
+ */
+ DBWRAP_REQ_DONE,
+ /**
+ * The request errored out
+ */
+ DBWRAP_REQ_ERROR
+};
+
+/* The following definitions come from lib/dbwrap.c */
+
+TDB_DATA dbwrap_record_get_key(const struct db_record *rec);
+TDB_DATA dbwrap_record_get_value(const struct db_record *rec);
+NTSTATUS dbwrap_record_store(struct db_record *rec, TDB_DATA data, int flags);
+NTSTATUS dbwrap_record_storev(struct db_record *rec,
+ const TDB_DATA *dbufs, int num_dbufs, int flags);
+NTSTATUS dbwrap_record_delete(struct db_record *rec);
+
+/**
+ * @brief Adds TDB records from one db_context to another
+ *
+ * @param to Destination db_context
+ * @param from Source db_context
+ * @param flags (TDB_INSERT or TDB_REPLACE)
+ *
+ * @return NT_STATUS_OK on success or NT_STATUS_INTERNAL_DB_CORRUPTION
+ */
+NTSTATUS
+dbwrap_merge_dbs(struct db_context *to, struct db_context *from, int flags);
+
+struct db_record *dbwrap_fetch_locked(struct db_context *db,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA key);
+struct db_context *dbwrap_record_get_db(struct db_record *rec);
+
+void dbwrap_lock_order_lock(const char *db_name,
+ enum dbwrap_lock_order lock_order);
+void dbwrap_lock_order_unlock(const char *db_name,
+ enum dbwrap_lock_order lock_order);
+
+NTSTATUS dbwrap_do_locked(struct db_context *db, TDB_DATA key,
+ void (*fn)(struct db_record *rec,
+ TDB_DATA value,
+ void *private_data),
+ void *private_data);
+
+NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key);
+NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key,
+ TDB_DATA data, int flags);
+NTSTATUS dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
+ TDB_DATA key, TDB_DATA *value);
+bool dbwrap_exists(struct db_context *db, TDB_DATA key);
+NTSTATUS dbwrap_traverse(struct db_context *db,
+ int (*f)(struct db_record*, void*),
+ void *private_data,
+ int *count);
+NTSTATUS dbwrap_traverse_read(struct db_context *db,
+ int (*f)(struct db_record*, void*),
+ void *private_data,
+ int *count);
+NTSTATUS dbwrap_parse_record(struct db_context *db, TDB_DATA key,
+ void (*parser)(TDB_DATA key, TDB_DATA data,
+ void *private_data),
+ void *private_data);
+/**
+ * Async implementation of dbwrap_parse_record
+ *
+ * @param[in] mem_ctx talloc memory context to use.
+ *
+ * @param[in] ev tevent context to use
+ *
+ * @param[in] db Database to query
+ *
+ * @param[in] key Record key, the function makes a copy of this
+ *
+ * @param[in] parser Parser callback function
+ *
+ * @param[in] private_data Private data for the callback function
+ *
+ * @param[out] req_state Pointer to a enum dbwrap_req_state variable
+ *
+ * @note req_state is updated in the send function. To determine the final
+ * result of the request the caller should therefore not rely on req_state. The
+ * primary use case is to give the caller an indication whether the request is
+ * already sent to ctdb (DBWRAP_REQ_DISPATCHED) or if it's still stuck in the
+ * sendqueue (DBWRAP_REQ_QUEUED).
+ **/
+struct tevent_req *dbwrap_parse_record_send(
+ TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct db_context *db,
+ TDB_DATA key,
+ void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data),
+ void *private_data,
+ enum dbwrap_req_state *req_state);
+NTSTATUS dbwrap_parse_record_recv(struct tevent_req *req);
+int dbwrap_wipe(struct db_context *db);
+int dbwrap_check(struct db_context *db);
+int dbwrap_get_seqnum(struct db_context *db);
+/* Returns 0 if unknown. */
+int dbwrap_transaction_start(struct db_context *db);
+NTSTATUS dbwrap_transaction_start_nonblock(struct db_context *db);
+int dbwrap_transaction_commit(struct db_context *db);
+int dbwrap_transaction_cancel(struct db_context *db);
+size_t dbwrap_db_id(struct db_context *db, uint8_t *id, size_t idlen);
+bool dbwrap_is_persistent(struct db_context *db);
+const char *dbwrap_name(struct db_context *db);
+
+/* The following definitions come from lib/dbwrap_util.c */
+
+NTSTATUS dbwrap_purge(struct db_context *db, TDB_DATA key);
+NTSTATUS dbwrap_purge_bystring(struct db_context *db, const char *key);
+NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key);
+NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
+ TDB_DATA data, int flags);
+NTSTATUS dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
+ const char *key, TDB_DATA *value);
+
+NTSTATUS dbwrap_fetch_int32(struct db_context *db, TDB_DATA key,
+ int32_t *result);
+NTSTATUS dbwrap_fetch_int32_bystring(struct db_context *db, const char *keystr,
+ int32_t *result);
+NTSTATUS dbwrap_store_int32_bystring(struct db_context *db, const char *keystr,
+ int32_t v);
+NTSTATUS dbwrap_fetch_uint32_bystring(struct db_context *db,
+ const char *keystr, uint32_t *val);
+NTSTATUS dbwrap_store_uint32_bystring(struct db_context *db,
+ const char *keystr, uint32_t v);
+NTSTATUS dbwrap_change_uint32_atomic_bystring(struct db_context *db,
+ const char *keystr,
+ uint32_t *oldval,
+ uint32_t change_val);
+NTSTATUS dbwrap_trans_change_uint32_atomic_bystring(struct db_context *db,
+ const char *keystr,
+ uint32_t *oldval,
+ uint32_t change_val);
+NTSTATUS dbwrap_change_int32_atomic(struct db_context *db,
+ TDB_DATA key,
+ int32_t *oldval,
+ int32_t change_val);
+NTSTATUS dbwrap_change_int32_atomic_bystring(struct db_context *db,
+ const char *keystr,
+ int32_t *oldval,
+ int32_t change_val);
+NTSTATUS dbwrap_trans_change_int32_atomic_bystring(struct db_context *db,
+ const char *keystr,
+ int32_t *oldval,
+ int32_t change_val);
+NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf,
+ int flag);
+NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key);
+NTSTATUS dbwrap_trans_store_int32_bystring(struct db_context *db,
+ const char *keystr,
+ int32_t v);
+NTSTATUS dbwrap_trans_store_uint32_bystring(struct db_context *db,
+ const char *keystr,
+ uint32_t v);
+NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key,
+ TDB_DATA data, int flags);
+NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key);
+NTSTATUS dbwrap_trans_do(struct db_context *db,
+ NTSTATUS (*action)(struct db_context *, void *),
+ void *private_data);
+NTSTATUS dbwrap_trans_traverse(struct db_context *db,
+ int (*f)(struct db_record*, void*),
+ void *private_data);
+
+NTSTATUS dbwrap_delete_bystring_upper(struct db_context *db, const char *key);
+NTSTATUS dbwrap_store_bystring_upper(struct db_context *db, const char *key,
+ TDB_DATA data, int flags);
+NTSTATUS dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx,
+ const char *key, TDB_DATA *value);
+
+size_t dbwrap_marshall(struct db_context *db, uint8_t *buf, size_t bufsize);
+NTSTATUS dbwrap_parse_marshall_buf(const uint8_t *buf, size_t buflen,
+ bool (*fn)(TDB_DATA key, TDB_DATA value,
+ void *private_data),
+ void *private_data);
+NTSTATUS dbwrap_unmarshall(struct db_context *db, const uint8_t *buf,
+ size_t buflen);
+
+NTSTATUS dbwrap_merge_dbufs(TDB_DATA *buf, TALLOC_CTX *mem_ctx,
+ const TDB_DATA *dbufs, int num_dbufs);
+
+
+/**
+ * This opens a tdb file
+ */
+struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
+ const char *name,
+ int hash_size, int tdb_flags,
+ int open_flags, mode_t mode,
+ enum dbwrap_lock_order lock_order,
+ uint64_t dbwrap_flags);
+
+#endif /* __DBWRAP_H__ */
diff --git a/lib/dbwrap/dbwrap_local_open.c b/lib/dbwrap/dbwrap_local_open.c
new file mode 100644
index 0000000..20c5fa0
--- /dev/null
+++ b/lib/dbwrap/dbwrap_local_open.c
@@ -0,0 +1,46 @@
+/*
+ Unix SMB/CIFS implementation.
+ Database interface wrapper: local open code.
+
+ Copyright (C) Rusty Russell 2012
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_tdb.h"
+#include "tdb.h"
+
+struct db_context *dbwrap_local_open(TALLOC_CTX *mem_ctx,
+ const char *name,
+ int hash_size, int tdb_flags,
+ int open_flags, mode_t mode,
+ enum dbwrap_lock_order lock_order,
+ uint64_t dbwrap_flags)
+{
+ struct db_context *db = NULL;
+
+ db = db_open_tdb(
+ mem_ctx,
+ name,
+ hash_size,
+ tdb_flags,
+ open_flags,
+ mode,
+ lock_order,
+ dbwrap_flags);
+
+ return db;
+}
diff --git a/lib/dbwrap/dbwrap_private.h b/lib/dbwrap/dbwrap_private.h
new file mode 100644
index 0000000..3ac5ebf
--- /dev/null
+++ b/lib/dbwrap/dbwrap_private.h
@@ -0,0 +1,93 @@
+/*
+ Unix SMB/CIFS implementation.
+ Database interface wrapper around tdb - private header
+
+ Copyright (C) Volker Lendecke 2005-2007
+ Copyright (C) Gregor Beck 2011
+ Copyright (C) Michael Adam 2011
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __DBWRAP_PRIVATE_H__
+#define __DBWRAP_PRIVATE_H__
+
+struct tevent_context;
+struct tevent_req;
+
+struct db_record {
+ struct db_context *db;
+ TDB_DATA key, value;
+ bool value_valid;
+ NTSTATUS (*storev)(struct db_record *rec, const TDB_DATA *dbufs,
+ int num_dbufs, int flag);
+ NTSTATUS (*delete_rec)(struct db_record *rec);
+ void *private_data;
+};
+
+struct db_context {
+ struct db_record *(*fetch_locked)(struct db_context *db,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA key);
+ int (*traverse)(struct db_context *db,
+ int (*f)(struct db_record *rec,
+ void *private_data),
+ void *private_data);
+ int (*traverse_read)(struct db_context *db,
+ int (*f)(struct db_record *rec,
+ void *private_data),
+ void *private_data);
+ int (*get_seqnum)(struct db_context *db);
+ int (*transaction_start)(struct db_context *db);
+ NTSTATUS (*transaction_start_nonblock)(struct db_context *db);
+ int (*transaction_commit)(struct db_context *db);
+ int (*transaction_cancel)(struct db_context *db);
+ NTSTATUS (*parse_record)(struct db_context *db, TDB_DATA key,
+ void (*parser)(TDB_DATA key, TDB_DATA data,
+ void *private_data),
+ void *private_data);
+ struct tevent_req *(*parse_record_send)(
+ TALLOC_CTX *mem_ctx,
+ struct tevent_context *ev,
+ struct db_context *db,
+ TDB_DATA key,
+ void (*parser)(TDB_DATA key, TDB_DATA data, void *private_data),
+ void *private_data,
+ enum dbwrap_req_state *req_state);
+ NTSTATUS (*parse_record_recv)(struct tevent_req *req);
+ NTSTATUS (*do_locked)(struct db_context *db, TDB_DATA key,
+ void (*fn)(struct db_record *rec,
+ TDB_DATA value,
+ void *private_data),
+ void *private_data);
+ int (*exists)(struct db_context *db,TDB_DATA key);
+ int (*wipe)(struct db_context *db);
+ int (*check)(struct db_context *db);
+ size_t (*id)(struct db_context *db, uint8_t *id, size_t idlen);
+
+ const char *name;
+ void *private_data;
+ enum dbwrap_lock_order lock_order;
+ bool persistent;
+};
+
+#define DBWRAP_LOCK_ORDER_MIN DBWRAP_LOCK_ORDER_1
+#define DBWRAP_LOCK_ORDER_MAX DBWRAP_LOCK_ORDER_4
+
+#define DBWRAP_LOCK_ORDER_VALID(order) \
+ (((order) >= DBWRAP_LOCK_ORDER_MIN) && \
+ ((order) <= DBWRAP_LOCK_ORDER_MAX))
+
+#endif /* __DBWRAP_PRIVATE_H__ */
+
diff --git a/lib/dbwrap/dbwrap_rbt.c b/lib/dbwrap/dbwrap_rbt.c
new file mode 100644
index 0000000..89d30bb
--- /dev/null
+++ b/lib/dbwrap/dbwrap_rbt.c
@@ -0,0 +1,589 @@
+/*
+ Unix SMB/CIFS implementation.
+ Database interface wrapper around red-black trees
+ Copyright (C) Volker Lendecke 2007, 2008
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_private.h"
+#include "dbwrap/dbwrap_rbt.h"
+#include "../lib/util/rbtree.h"
+#include "../lib/util/dlinklist.h"
+
+#define DBWRAP_RBT_ALIGN(_size_) (((_size_)+15)&~15)
+
+struct db_rbt_ctx {
+ struct rb_root tree;
+ struct db_rbt_node *nodes;
+ size_t traverse_read;
+ struct db_rbt_node **traverse_nextp;
+};
+
+struct db_rbt_rec {
+ struct db_rbt_node *node;
+};
+
+/* The structure that ends up in the tree */
+
+struct db_rbt_node {
+ struct rb_node rb_node;
+ size_t keysize, valuesize;
+ struct db_rbt_node *prev, *next;
+};
+
+/*
+ * Hide the ugly pointer calculations in a function
+ */
+
+static struct db_rbt_node *db_rbt2node(struct rb_node *node)
+{
+ return (struct db_rbt_node *)
+ ((char *)node - offsetof(struct db_rbt_node, rb_node));
+}
+
+/*
+ * Compare two keys
+ */
+
+static int db_rbt_compare(TDB_DATA a, TDB_DATA b)
+{
+ int res;
+
+ res = memcmp(a.dptr, b.dptr, MIN(a.dsize, b.dsize));
+
+ if ((res < 0) || ((res == 0) && (a.dsize < b.dsize))) {
+ return -1;
+ }
+ if ((res > 0) || ((res == 0) && (a.dsize > b.dsize))) {
+ return 1;
+ }
+ return 0;
+}
+
+/*
+ * dissect a db_rbt_node into its implicit key and value parts
+ */
+
+static void db_rbt_parse_node(struct db_rbt_node *node,
+ TDB_DATA *key, TDB_DATA *value)
+{
+ size_t key_offset, value_offset;
+
+ key_offset = DBWRAP_RBT_ALIGN(sizeof(struct db_rbt_node));
+ key->dptr = ((uint8_t *)node) + key_offset;
+ key->dsize = node->keysize;
+
+ value_offset = DBWRAP_RBT_ALIGN(node->keysize);
+ value->dptr = key->dptr + value_offset;
+ value->dsize = node->valuesize;
+}
+
+static ssize_t db_rbt_reclen(size_t keylen, size_t valuelen)
+{
+ size_t len, tmp;
+
+ len = DBWRAP_RBT_ALIGN(sizeof(struct db_rbt_node));
+
+ tmp = DBWRAP_RBT_ALIGN(keylen);
+ if (tmp < keylen) {
+ goto overflow;
+ }
+
+ len += tmp;
+ if (len < tmp) {
+ goto overflow;
+ }
+
+ len += valuelen;
+ if (len < valuelen) {
+ goto overflow;
+ }
+
+ return len;
+overflow:
+ return -1;
+}
+
+static NTSTATUS db_rbt_storev(struct db_record *rec,
+ const TDB_DATA *dbufs, int num_dbufs, int flag)
+{
+ struct db_rbt_ctx *db_ctx = talloc_get_type_abort(
+ rec->db->private_data, struct db_rbt_ctx);
+ struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data;
+ struct db_rbt_node *node;
+
+ struct rb_node ** p;
+ struct rb_node *parent = NULL;
+ struct db_rbt_node *parent_node = NULL;
+
+ ssize_t reclen;
+ TDB_DATA data, this_key, this_val;
+ void *to_free = NULL;
+
+ if (db_ctx->traverse_read > 0) {
+ return NT_STATUS_MEDIA_WRITE_PROTECTED;
+ }
+
+ if ((flag == TDB_INSERT) && (rec_priv->node != NULL)) {
+ return NT_STATUS_OBJECT_NAME_COLLISION;
+ }
+
+ if ((flag == TDB_MODIFY) && (rec_priv->node == NULL)) {
+ return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+ }
+
+ if (num_dbufs == 1) {
+ data = dbufs[0];
+ } else {
+ NTSTATUS status;
+
+ data = (TDB_DATA) {0};
+ status = dbwrap_merge_dbufs(&data, rec, dbufs, num_dbufs);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
+ to_free = data.dptr;
+ }
+
+ if (rec_priv->node != NULL) {
+
+ /*
+ * The record was around previously
+ */
+
+ db_rbt_parse_node(rec_priv->node, &this_key, &this_val);
+
+ SMB_ASSERT(this_key.dsize == rec->key.dsize);
+ SMB_ASSERT(memcmp(this_key.dptr, rec->key.dptr,
+ this_key.dsize) == 0);
+
+ if (this_val.dsize >= data.dsize) {
+ /*
+ * The new value fits into the old space
+ */
+ memcpy(this_val.dptr, data.dptr, data.dsize);
+ rec_priv->node->valuesize = data.dsize;
+ TALLOC_FREE(to_free);
+ return NT_STATUS_OK;
+ }
+ }
+
+ reclen = db_rbt_reclen(rec->key.dsize, data.dsize);
+ if (reclen == -1) {
+ TALLOC_FREE(to_free);
+ return NT_STATUS_INSUFFICIENT_RESOURCES;
+ }
+
+ node = talloc_zero_size(db_ctx, reclen);
+ if (node == NULL) {
+ TALLOC_FREE(to_free);
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ if (rec_priv->node != NULL) {
+ if (db_ctx->traverse_nextp != NULL) {
+ if (*db_ctx->traverse_nextp == rec_priv->node) {
+ *db_ctx->traverse_nextp = node;
+ }
+ }
+
+ /*
+ * We need to delete the key from the tree and start fresh,
+ * there's not enough space in the existing record
+ */
+
+ rb_erase(&rec_priv->node->rb_node, &db_ctx->tree);
+ DLIST_REMOVE(db_ctx->nodes, rec_priv->node);
+
+ /*
+ * Keep the existing node around for a while: If the record
+ * existed before, we reference the key data in there.
+ */
+ }
+
+ node->keysize = rec->key.dsize;
+ node->valuesize = data.dsize;
+
+ db_rbt_parse_node(node, &this_key, &this_val);
+
+ memcpy(this_key.dptr, rec->key.dptr, node->keysize);
+ TALLOC_FREE(rec_priv->node);
+ rec_priv->node = node;
+
+ if (node->valuesize > 0) {
+ memcpy(this_val.dptr, data.dptr, node->valuesize);
+ }
+
+ parent = NULL;
+ p = &db_ctx->tree.rb_node;
+
+ while (*p) {
+ struct db_rbt_node *r;
+ TDB_DATA search_key, search_val;
+ int res;
+
+ r = db_rbt2node(*p);
+
+ parent = (*p);
+ parent_node = r;
+
+ db_rbt_parse_node(r, &search_key, &search_val);
+
+ res = db_rbt_compare(this_key, search_key);
+
+ if (res == -1) {
+ p = &(*p)->rb_left;
+ }
+ else if (res == 1) {
+ p = &(*p)->rb_right;
+ }
+ else {
+ smb_panic("someone messed with the tree");
+ }
+ }
+
+ rb_link_node(&node->rb_node, parent, p);
+ DLIST_ADD_AFTER(db_ctx->nodes, node, parent_node);
+ rb_insert_color(&node->rb_node, &db_ctx->tree);
+
+ TALLOC_FREE(to_free);
+
+ return NT_STATUS_OK;
+}
+
+static NTSTATUS db_rbt_delete(struct db_record *rec)
+{
+ struct db_rbt_ctx *db_ctx = talloc_get_type_abort(
+ rec->db->private_data, struct db_rbt_ctx);
+ struct db_rbt_rec *rec_priv = (struct db_rbt_rec *)rec->private_data;
+
+ if (db_ctx->traverse_read > 0) {
+ return NT_STATUS_MEDIA_WRITE_PROTECTED;
+ }
+
+ if (rec_priv->node == NULL) {
+ return NT_STATUS_OK;
+ }
+
+ if (db_ctx->traverse_nextp != NULL) {
+ if (*db_ctx->traverse_nextp == rec_priv->node) {
+ *db_ctx->traverse_nextp = rec_priv->node->next;
+ }
+ }
+
+ rb_erase(&rec_priv->node->rb_node, &db_ctx->tree);
+ DLIST_REMOVE(db_ctx->nodes, rec_priv->node);
+ TALLOC_FREE(rec_priv->node);
+
+ return NT_STATUS_OK;
+}
+
+struct db_rbt_search_result {
+ TDB_DATA key;
+ TDB_DATA val;
+ struct db_rbt_node* node;
+};
+
+static bool db_rbt_search_internal(struct db_context *db, TDB_DATA key,
+ struct db_rbt_search_result *result)
+{
+ struct db_rbt_ctx *ctx = talloc_get_type_abort(
+ db->private_data, struct db_rbt_ctx);
+
+ struct rb_node *n;
+ bool found = false;
+ struct db_rbt_node *r = NULL;
+ TDB_DATA search_key = { 0 };
+ TDB_DATA search_val = { 0 };
+
+ n = ctx->tree.rb_node;
+
+ while (n != NULL) {
+ int res;
+
+ r = db_rbt2node(n);
+
+ db_rbt_parse_node(r, &search_key, &search_val);
+
+ res = db_rbt_compare(key, search_key);
+
+ if (res == -1) {
+ n = n->rb_left;
+ }
+ else if (res == 1) {
+ n = n->rb_right;
+ }
+ else {
+ found = true;
+ break;
+ }
+ }
+ if (result != NULL) {
+ if (found) {
+ result->key = search_key;
+ result->val = search_val;
+ result->node = r;
+ } else {
+ ZERO_STRUCT(*result);
+ }
+ }
+ return found;
+}
+
+static struct db_record *db_rbt_fetch_locked(struct db_context *db_ctx,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA key)
+{
+ struct db_rbt_rec *rec_priv;
+ struct db_record *result;
+ size_t size;
+ bool found;
+ struct db_rbt_search_result res;
+
+ found = db_rbt_search_internal(db_ctx, key, &res);
+
+ /*
+ * In this low-level routine, play tricks to reduce the number of
+ * tallocs to one. Not recommended for general use, but here it pays
+ * off.
+ */
+
+ size = DBWRAP_RBT_ALIGN(sizeof(struct db_record))
+ + sizeof(struct db_rbt_rec);
+
+ if (!found) {
+ /*
+ * We need to keep the key around for later store
+ */
+ size += key.dsize;
+ }
+
+ result = (struct db_record *)talloc_size(mem_ctx, size);
+ if (result == NULL) {
+ return NULL;
+ }
+
+ rec_priv = (struct db_rbt_rec *)
+ ((char *)result + DBWRAP_RBT_ALIGN(sizeof(struct db_record)));
+
+ result->storev = db_rbt_storev;
+ result->delete_rec = db_rbt_delete;
+ result->private_data = rec_priv;
+
+ rec_priv->node = res.node;
+ result->value = res.val;
+ result->value_valid = true;
+
+ if (found) {
+ result->key = res.key;
+ }
+ else {
+ result->key.dptr = (uint8_t *)
+ ((char *)rec_priv + sizeof(*rec_priv));
+ result->key.dsize = key.dsize;
+ memcpy(result->key.dptr, key.dptr, key.dsize);
+ }
+
+ return result;
+}
+
+static int db_rbt_exists(struct db_context *db, TDB_DATA key)
+{
+ return db_rbt_search_internal(db, key, NULL);
+}
+
+static int db_rbt_wipe(struct db_context *db)
+{
+ struct db_rbt_ctx *old_ctx = talloc_get_type_abort(
+ db->private_data, struct db_rbt_ctx);
+ struct db_rbt_ctx *new_ctx = talloc_zero(db, struct db_rbt_ctx);
+ if (new_ctx == NULL) {
+ return -1;
+ }
+ db->private_data = new_ctx;
+ talloc_free(old_ctx);
+ return 0;
+}
+
+static NTSTATUS db_rbt_parse_record(struct db_context *db, TDB_DATA key,
+ void (*parser)(TDB_DATA key, TDB_DATA data,
+ void *private_data),
+ void *private_data)
+{
+ struct db_rbt_search_result res;
+ bool found = db_rbt_search_internal(db, key, &res);
+
+ if (!found) {
+ return NT_STATUS_NOT_FOUND;
+ }
+ parser(res.key, res.val, private_data);
+ return NT_STATUS_OK;
+}
+
+static int db_rbt_traverse_internal(struct db_context *db,
+ int (*f)(struct db_record *db,
+ void *private_data),
+ void *private_data, uint32_t* count,
+ bool rw)
+{
+ struct db_rbt_ctx *ctx = talloc_get_type_abort(
+ db->private_data, struct db_rbt_ctx);
+ struct db_rbt_node *cur = NULL;
+ struct db_rbt_node *next = NULL;
+ int ret;
+
+ for (cur = ctx->nodes; cur != NULL; cur = next) {
+ struct db_record rec;
+ struct db_rbt_rec rec_priv;
+
+ rec_priv.node = cur;
+ next = rec_priv.node->next;
+
+ ZERO_STRUCT(rec);
+ rec.db = db;
+ rec.private_data = &rec_priv;
+ rec.storev = db_rbt_storev;
+ rec.delete_rec = db_rbt_delete;
+ db_rbt_parse_node(rec_priv.node, &rec.key, &rec.value);
+ rec.value_valid = true;
+
+ if (rw) {
+ ctx->traverse_nextp = &next;
+ }
+ ret = f(&rec, private_data);
+ (*count) ++;
+ if (rw) {
+ ctx->traverse_nextp = NULL;
+ }
+ if (ret != 0) {
+ return ret;
+ }
+ if (rec_priv.node != NULL) {
+ next = rec_priv.node->next;
+ }
+ }
+
+ return 0;
+}
+
+static int db_rbt_traverse_read(struct db_context *db,
+ int (*f)(struct db_record *db,
+ void *private_data),
+ void *private_data)
+{
+ struct db_rbt_ctx *ctx = talloc_get_type_abort(
+ db->private_data, struct db_rbt_ctx);
+ uint32_t count = 0;
+ int ret;
+
+ ctx->traverse_read++;
+ ret = db_rbt_traverse_internal(db,
+ f, private_data, &count,
+ false /* rw */);
+ ctx->traverse_read--;
+ if (ret != 0) {
+ return -1;
+ }
+ if (count > INT_MAX) {
+ return -1;
+ }
+ return count;
+}
+
+static int db_rbt_traverse(struct db_context *db,
+ int (*f)(struct db_record *db,
+ void *private_data),
+ void *private_data)
+{
+ struct db_rbt_ctx *ctx = talloc_get_type_abort(
+ db->private_data, struct db_rbt_ctx);
+ uint32_t count = 0;
+ int ret;
+
+ if (ctx->traverse_nextp != NULL) {
+ return -1;
+ };
+
+ if (ctx->traverse_read > 0) {
+ return db_rbt_traverse_read(db, f, private_data);
+ }
+
+ ret = db_rbt_traverse_internal(db,
+ f, private_data, &count,
+ true /* rw */);
+ if (ret != 0) {
+ return -1;
+ }
+ if (count > INT_MAX) {
+ return -1;
+ }
+ return count;
+}
+
+static int db_rbt_get_seqnum(struct db_context *db)
+{
+ return 0;
+}
+
+static int db_rbt_trans_dummy(struct db_context *db)
+{
+ /*
+ * Transactions are pretty pointless in-memory, just return success.
+ */
+ return 0;
+}
+
+static size_t db_rbt_id(struct db_context *db, uint8_t *id, size_t idlen)
+{
+ if (idlen >= sizeof(struct db_context *)) {
+ memcpy(id, &db, sizeof(struct db_context *));
+ }
+ return sizeof(struct db_context *);
+}
+
+struct db_context *db_open_rbt(TALLOC_CTX *mem_ctx)
+{
+ struct db_context *result;
+
+ result = talloc_zero(mem_ctx, struct db_context);
+
+ if (result == NULL) {
+ return NULL;
+ }
+
+ result->private_data = talloc_zero(result, struct db_rbt_ctx);
+
+ if (result->private_data == NULL) {
+ TALLOC_FREE(result);
+ return NULL;
+ }
+
+ result->fetch_locked = db_rbt_fetch_locked;
+ result->traverse = db_rbt_traverse;
+ result->traverse_read = db_rbt_traverse_read;
+ result->get_seqnum = db_rbt_get_seqnum;
+ result->transaction_start = db_rbt_trans_dummy;
+ result->transaction_commit = db_rbt_trans_dummy;
+ result->transaction_cancel = db_rbt_trans_dummy;
+ result->exists = db_rbt_exists;
+ result->wipe = db_rbt_wipe;
+ result->parse_record = db_rbt_parse_record;
+ result->id = db_rbt_id;
+ result->name = "dbwrap rbt";
+
+ return result;
+}
diff --git a/lib/dbwrap/dbwrap_rbt.h b/lib/dbwrap/dbwrap_rbt.h
new file mode 100644
index 0000000..1716879
--- /dev/null
+++ b/lib/dbwrap/dbwrap_rbt.h
@@ -0,0 +1,29 @@
+/*
+ Unix SMB/CIFS implementation.
+ Database interface wrapper around red-black trees
+ Copyright (C) Volker Lendecke 2007, 2008
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __DBWRAP_RBT_H__
+#define __DBWRAP_RBT_H__
+
+#include <talloc.h>
+
+struct db_context;
+
+struct db_context *db_open_rbt(TALLOC_CTX *mem_ctx);
+
+#endif /* __DBWRAP_RBT_H__ */
diff --git a/lib/dbwrap/dbwrap_tdb.c b/lib/dbwrap/dbwrap_tdb.c
new file mode 100644
index 0000000..6cd95fa
--- /dev/null
+++ b/lib/dbwrap/dbwrap_tdb.c
@@ -0,0 +1,518 @@
+/*
+ Unix SMB/CIFS implementation.
+ Database interface wrapper around tdb
+ Copyright (C) Volker Lendecke 2005-2007
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "dbwrap/dbwrap.h"
+#include "dbwrap/dbwrap_private.h"
+#include "dbwrap/dbwrap_tdb.h"
+#include "lib/tdb_wrap/tdb_wrap.h"
+#include "lib/util/util_tdb.h"
+#include "lib/util/debug.h"
+#include "lib/util/samba_util.h"
+#include "system/filesys.h"
+#include "lib/param/param.h"
+#include "libcli/util/error.h"
+
+struct db_tdb_ctx {
+ struct tdb_wrap *wtdb;
+
+ struct {
+ dev_t dev;
+ ino_t ino;
+ } id;
+};
+
+static NTSTATUS db_tdb_storev(struct db_record *rec,
+ const TDB_DATA *dbufs, int num_dbufs, int flag);
+static NTSTATUS db_tdb_delete(struct db_record *rec);
+
+static void db_tdb_log_key(const char *prefix, TDB_DATA key)
+{
+ if (DEBUGLEVEL < 10) {
+ return;
+ }
+ if (DEBUGLEVEL == 10) {
+ /*
+ * Only fully spam at debuglevel > 10
+ */
+ key.dsize = MIN(10, key.dsize);
+ }
+
+ if (key.dsize < 1024) {
+ char keystr[key.dsize*2+1];
+ hex_encode_buf(keystr, key.dptr, key.dsize);
+ DBG_DEBUG("%s key %s\n", prefix, keystr);
+ return;
+ }
+
+ dump_data(DEBUGLEVEL, key.dptr, key.dsize);
+}
+
+static int db_tdb_record_destr(struct db_record* data)
+{
+ struct db_tdb_ctx *ctx =
+ talloc_get_type_abort(data->private_data, struct db_tdb_ctx);
+
+ db_tdb_log_key("Unlocking", data->key);
+ tdb_chainunlock(ctx->wtdb->tdb, data->key);
+ return 0;
+}
+
+struct tdb_fetch_locked_state {
+ TALLOC_CTX *mem_ctx;
+ struct db_record *result;
+};
+
+static int db_tdb_fetchlock_parse(TDB_DATA key, TDB_DATA data,
+ void *private_data)
+{
+ struct tdb_fetch_locked_state *state =
+ (struct tdb_fetch_locked_state *)private_data;
+ struct db_record *result;
+
+ result = (struct db_record *)talloc_size(
+ state->mem_ctx,
+ sizeof(struct db_record) + key.dsize + data.dsize);
+
+ if (result == NULL) {
+ return 0;
+ }
+ state->result = result;
+
+ result->key.dsize = key.dsize;
+ result->key.dptr = ((uint8_t *)result) + sizeof(struct db_record);
+ memcpy(result->key.dptr, key.dptr, key.dsize);
+
+ result->value.dsize = data.dsize;
+
+ if (data.dsize > 0) {
+ result->value.dptr = result->key.dptr+key.dsize;
+ memcpy(result->value.dptr, data.dptr, data.dsize);
+ }
+ else {
+ result->value.dptr = NULL;
+ }
+ result->value_valid = true;
+
+ return 0;
+}
+
+static struct db_record *db_tdb_fetch_locked_internal(
+ struct db_context *db,
+ struct db_tdb_ctx *ctx,
+ TALLOC_CTX *mem_ctx,
+ TDB_DATA key)
+{
+ struct tdb_fetch_locked_state state;
+ int ret;
+
+ state = (struct tdb_fetch_locked_state) {
+ .mem_ctx = mem_ctx,
+ };
+
+ ret = tdb_parse_record(ctx->wtdb->tdb,
+ key,
+ db_tdb_fetchlock_parse,
+ &state);
+ if ((ret < 0) && (tdb_error(ctx->wtdb->tdb) != TDB_ERR_NOEXIST)) {
+ tdb_chainunlock(ctx->wtdb->tdb, key);
+ return NULL;
+ }
+
+ if (state.result == NULL) {
+ db_tdb_fetchlock_parse(key, tdb_null, &state);
+ }
+
+ if (state.result == NULL) {
+ tdb_chainunlock(ctx->wtdb->tdb, key);
+ return NULL;
+ }
+
+ talloc_set_destructor(state.result, db_tdb_record_destr);
+
+ state.result->private_data = ctx;
+ state.result->storev = db_tdb_storev;
+ state.result->delete_rec = db_tdb_delete;
+
+ DBG_DEBUG("Allocated locked data %p\n", state.result);
+
+ return state.result;
+}
+
+static struct db_record *db_tdb_fetch_locked(
+ struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
+{
+ struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
+ struct db_tdb_ctx);
+
+ db_tdb_log_key("Locking", key);
+ if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
+ DEBUG(3, ("tdb_chainlock failed\n"));
+ return NULL;
+ }
+ return db_tdb_fetch_locked_internal(db, ctx, mem_ctx, key);
+}
+
+static NTSTATUS db_tdb_do_locked(struct db_context *db, TDB_DATA key,
+ void (*fn)(struct db_record *rec,
+ TDB_DATA value,
+ void *private_data),
+ void *private_data)
+{
+ struct db_tdb_ctx *ctx = talloc_get_type_abort(
+ db->private_data, struct db_tdb_ctx);
+ uint8_t *buf = NULL;
+ struct db_record rec;
+ int ret;
+
+ ret = tdb_chainlock(ctx->wtdb->tdb, key);
+ if (ret == -1) {
+ enum TDB_ERROR err = tdb_error(ctx->wtdb->tdb);
+ DBG_DEBUG("tdb_chainlock failed: %s\n",
+ tdb_errorstr(ctx->wtdb->tdb));
+ return map_nt_error_from_tdb(err);
+ }
+
+ ret = tdb_fetch_talloc(ctx->wtdb->tdb, key, ctx, &buf);
+
+ if ((ret != 0) && (ret != ENOENT)) {
+ DBG_DEBUG("tdb_fetch_talloc failed: %s\n",
+ strerror(errno));
+ tdb_chainunlock(ctx->wtdb->tdb, key);
+ return map_nt_error_from_unix_common(ret);
+ }
+
+ rec = (struct db_record) {
+ .db = db, .key = key,
+ .value_valid = false,
+ .storev = db_tdb_storev, .delete_rec = db_tdb_delete,
+ .private_data = ctx
+ };
+
+ fn(&rec,
+ (TDB_DATA) { .dptr = buf, .dsize = talloc_get_size(buf) },
+ private_data);
+
+ tdb_chainunlock(ctx->wtdb->tdb, key);
+
+ talloc_free(buf);
+
+ return NT_STATUS_OK;
+}
+
+static int db_tdb_exists(struct db_context *db, TDB_DATA key)
+{
+ struct db_tdb_ctx *ctx = talloc_get_type_abort(
+ db->private_data, struct db_tdb_ctx);
+ return tdb_exists(ctx->wtdb->tdb, key);
+}
+
+static int db_tdb_wipe(struct db_context *db)
+{
+ struct db_tdb_ctx *ctx = talloc_get_type_abort(
+ db->private_data, struct db_tdb_ctx);
+ return tdb_wipe_all(ctx->wtdb->tdb);
+}
+
+static int db_tdb_check(struct db_context *db)
+{
+ struct db_tdb_ctx *ctx = talloc_get_type_abort(
+ db->private_data, struct db_tdb_ctx);
+ return tdb_check(ctx->wtdb->tdb, NULL, NULL);
+}
+
+struct db_tdb_parse_state {
+ void (*parser)(TDB_DATA key, TDB_DATA data,
+ void *private_data);
+ void *private_data;
+};
+
+/*
+ * tdb_parse_record expects a parser returning int, mixing up tdb and
+ * parser errors. Wrap around that by always returning 0 and have
+ * dbwrap_parse_record expect a parser returning void.
+ */
+
+static int db_tdb_parser(TDB_DATA key, TDB_DATA data, void *private_data)
+{
+ struct db_tdb_parse_state *state =
+ (struct db_tdb_parse_state *)private_data;
+ state->parser(key, data, state->private_data);
+ return 0;
+}
+
+static NTSTATUS db_tdb_parse(struct db_context *db, TDB_DATA key,
+ void (*parser)(TDB_DATA key, TDB_DATA data,
+ void *private_data),
+ void *private_data)
+{
+ struct db_tdb_ctx *ctx = talloc_get_type_abort(
+ db->private_data, struct db_tdb_ctx);
+ struct db_tdb_parse_state state;
+ int ret;
+
+ state.parser = parser;
+ state.private_data = private_data;
+
+ ret = tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_parser, &state);
+
+ if (ret != 0) {
+ return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb));
+ }
+ return NT_STATUS_OK;
+}
+
+static NTSTATUS db_tdb_storev(struct db_record *rec,
+ const TDB_DATA *dbufs, int num_dbufs, int flag)
+{
+ struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
+ struct db_tdb_ctx);
+ struct tdb_context *tdb = ctx->wtdb->tdb;
+ NTSTATUS status = NT_STATUS_OK;
+ int ret;
+
+ /*
+ * This has a bug: We need to replace rec->value for correct
+ * operation, but right now brlock and locking don't use the value
+ * anymore after it was stored.
+ */
+
+ ret = tdb_storev(tdb, rec->key, dbufs, num_dbufs, flag);
+ if (ret == -1) {
+ enum TDB_ERROR err = tdb_error(tdb);
+ status = map_nt_error_from_tdb(err);
+ }
+ return status;
+}
+
+static NTSTATUS db_tdb_delete(struct db_record *rec)
+{
+ struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
+ struct db_tdb_ctx);
+
+ if (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) {
+ return NT_STATUS_OK;
+ }
+
+ if (tdb_error(ctx->wtdb->tdb) == TDB_ERR_NOEXIST) {
+ return NT_STATUS_NOT_FOUND;
+ }
+
+ return NT_STATUS_UNSUCCESSFUL;
+}
+
+struct db_tdb_traverse_ctx {
+ struct db_context *db;
+ int (*f)(struct db_record *rec, void *private_data);
+ void *private_data;
+};
+
+static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
+ void *private_data)
+{
+ struct db_tdb_traverse_ctx *ctx =
+ (struct db_tdb_traverse_ctx *)private_data;
+ struct db_record rec;
+
+ rec.key = kbuf;
+ rec.value = dbuf;
+ rec.value_valid = true;
+ rec.storev = db_tdb_storev;
+ rec.delete_rec = db_tdb_delete;
+ rec.private_data = ctx->db->private_data;
+ rec.db = ctx->db;
+
+ return ctx->f(&rec, ctx->private_data);
+}
+
+static int db_tdb_traverse(struct db_context *db,
+ int (*f)(struct db_record *rec, void *private_data),
+ void *private_data)
+{
+ struct db_tdb_ctx *db_ctx =
+ talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
+ struct db_tdb_traverse_ctx ctx;
+
+ ctx.db = db;
+ ctx.f = f;
+ ctx.private_data = private_data;
+ return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx);
+}
+
+static NTSTATUS db_tdb_storev_deny(struct db_record *rec,
+ const TDB_DATA *dbufs, int num_dbufs,
+ int flag)
+{
+ return NT_STATUS_MEDIA_WRITE_PROTECTED;
+}
+
+static NTSTATUS db_tdb_delete_deny(struct db_record *rec)
+{
+ return NT_STATUS_MEDIA_WRITE_PROTECTED;
+}
+
+static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
+ void *private_data)
+{
+ struct db_tdb_traverse_ctx *ctx =
+ (struct db_tdb_traverse_ctx *)private_data;
+ struct db_record rec;
+
+ rec.key = kbuf;
+ rec.value = dbuf;
+ rec.value_valid = true;
+ rec.storev = db_tdb_storev_deny;
+ rec.delete_rec = db_tdb_delete_deny;
+ rec.private_data = ctx->db->private_data;
+ rec.db = ctx->db;
+
+ return ctx->f(&rec, ctx->private_data);
+}
+
+static int db_tdb_traverse_read(struct db_context *db,
+ int (*f)(struct db_record *rec, void *private_data),
+ void *private_data)
+{
+ struct db_tdb_ctx *db_ctx =
+ talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
+ struct db_tdb_traverse_ctx ctx;
+
+ ctx.db = db;
+ ctx.f = f;
+ ctx.private_data = private_data;
+ return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx);
+}
+
+static int db_tdb_get_seqnum(struct db_context *db)
+
+{
+ struct db_tdb_ctx *db_ctx =
+ talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
+ return tdb_get_seqnum(db_ctx->wtdb->tdb);
+}
+
+static int db_tdb_transaction_start(struct db_context *db)
+{
+ struct db_tdb_ctx *db_ctx =
+ talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
+ return tdb_transaction_start(db_ctx->wtdb->tdb) ? -1 : 0;
+}
+
+static NTSTATUS db_tdb_transaction_start_nonblock(struct db_context *db)
+{
+ struct db_tdb_ctx *db_ctx =
+ talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
+ int ret;
+
+ ret = tdb_transaction_start_nonblock(db_ctx->wtdb->tdb);
+ if (ret != 0) {
+ return map_nt_error_from_tdb(tdb_error(db_ctx->wtdb->tdb));
+ }
+ return NT_STATUS_OK;
+}
+
+static int db_tdb_transaction_commit(struct db_context *db)
+{
+ struct db_tdb_ctx *db_ctx =
+ talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
+ return tdb_transaction_commit(db_ctx->wtdb->tdb) ? -1 : 0;
+}
+
+static int db_tdb_transaction_cancel(struct db_context *db)
+{
+ struct db_tdb_ctx *db_ctx =
+ talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
+ tdb_transaction_cancel(db_ctx->wtdb->tdb);
+ return 0;
+}
+
+static size_t db_tdb_id(struct db_context *db, uint8_t *id, size_t idlen)
+{
+ struct db_tdb_ctx *db_ctx =
+ talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
+
+ if (idlen >= sizeof(db_ctx->id)) {
+ memcpy(id, &db_ctx->id, sizeof(db_ctx->id));
+ }
+
+ return sizeof(db_ctx->id);
+}
+
+struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
+ const char *name,
+ int hash_size, int tdb_flags,
+ int open_flags, mode_t mode,
+ enum dbwrap_lock_order lock_order,
+ uint64_t dbwrap_flags)
+{
+ struct db_context *result = NULL;
+ struct db_tdb_ctx *db_tdb;
+ struct stat st;
+
+ result = talloc_zero(mem_ctx, struct db_context);
+ if (result == NULL) {
+ DEBUG(0, ("talloc failed\n"));
+ goto fail;
+ }
+
+ result->private_data = db_tdb = talloc(result, struct db_tdb_ctx);
+ if (db_tdb == NULL) {
+ DEBUG(0, ("talloc failed\n"));
+ goto fail;
+ }
+ result->lock_order = lock_order;
+
+ db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags,
+ open_flags, mode);
+ if (db_tdb->wtdb == NULL) {
+ DEBUG(3, ("Could not open tdb: %s\n", strerror(errno)));
+ goto fail;
+ }
+
+ ZERO_STRUCT(db_tdb->id);
+
+ if (fstat(tdb_fd(db_tdb->wtdb->tdb), &st) == -1) {
+ DEBUG(3, ("fstat failed: %s\n", strerror(errno)));
+ goto fail;
+ }
+ db_tdb->id.dev = st.st_dev;
+ db_tdb->id.ino = st.st_ino;
+
+ result->fetch_locked = db_tdb_fetch_locked;
+ result->do_locked = db_tdb_do_locked;
+ result->traverse = db_tdb_traverse;
+ result->traverse_read = db_tdb_traverse_read;
+ result->parse_record = db_tdb_parse;
+ result->get_seqnum = db_tdb_get_seqnum;
+ result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
+ result->transaction_start = db_tdb_transaction_start;
+ result->transaction_start_nonblock = db_tdb_transaction_start_nonblock;
+ result->transaction_commit = db_tdb_transaction_commit;
+ result->transaction_cancel = db_tdb_transaction_cancel;
+ result->exists = db_tdb_exists;
+ result->wipe = db_tdb_wipe;
+ result->id = db_tdb_id;
+ result->check = db_tdb_check;
+ result->name = tdb_name(db_tdb->wtdb->tdb);
+ return result;
+
+ fail:
+ TALLOC_FREE(result);
+ return NULL;
+}
diff --git a/lib/dbwrap/dbwrap_tdb.h b/lib/dbwrap/dbwrap_tdb.h
new file mode 100644
index 0000000..d5f49c7
--- /dev/null
+++ b/lib/dbwrap/dbwrap_tdb.h
@@ -0,0 +1,35 @@
+/*
+ Unix SMB/CIFS implementation.
+ Database interface wrapper around tdb
+ Copyright (C) Volker Lendecke 2005-2007
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __DBWRAP_TDB_H__
+#define __DBWRAP_TDB_H__
+
+#include <talloc.h>
+
+struct db_context;
+
+struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
+ const char *name,
+ int hash_size, int tdb_flags,
+ int open_flags, mode_t mode,
+ enum dbwrap_lock_order lock_order,
+ uint64_t dbwrap_flags);
+
+
+#endif /* __DBWRAP_TDB_H__ */
diff --git a/lib/dbwrap/dbwrap_util.c b/lib/dbwrap/dbwrap_util.c
new file mode 100644
index 0000000..312fb41
--- /dev/null
+++ b/lib/dbwrap/dbwrap_util.c
@@ -0,0 +1,756 @@
+/*
+ Unix SMB/CIFS implementation.
+ Utility functions for the dbwrap API
+ Copyright (C) Volker Lendecke 2007
+ Copyright (C) Michael Adam 2009
+ Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
+
+ Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "dbwrap.h"
+#include "lib/util/util_tdb.h"
+
+struct dbwrap_fetch_int32_state {
+ NTSTATUS status;
+ int32_t result;
+};
+
+static void dbwrap_fetch_int32_parser(TDB_DATA key, TDB_DATA data,
+ void *private_data)
+{
+ struct dbwrap_fetch_int32_state *state =
+ (struct dbwrap_fetch_int32_state *)private_data;
+
+ if (data.dsize != sizeof(state->result)) {
+ state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
+ return;
+ }
+ state->result = IVAL(data.dptr, 0);
+ state->status = NT_STATUS_OK;
+}
+
+NTSTATUS dbwrap_fetch_int32(struct db_context *db, TDB_DATA key,
+ int32_t *result)
+{
+ struct dbwrap_fetch_int32_state state;
+ NTSTATUS status;
+
+ if (result == NULL) {
+ return NT_STATUS_INVALID_PARAMETER;
+ }
+
+ state.status = NT_STATUS_INTERNAL_ERROR;
+
+ status = dbwrap_parse_record(db, key, dbwrap_fetch_int32_parser, &state);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
+
+ if (NT_STATUS_IS_OK(state.status)) {
+ *result = state.result;
+ }
+ return state.status;
+}
+
+NTSTATUS dbwrap_fetch_int32_bystring(struct db_context *db, const char *keystr,
+ int32_t *result)
+{
+ return dbwrap_fetch_int32(db, string_term_tdb_data(keystr), result);
+}
+
+NTSTATUS dbwrap_store_int32_bystring(struct db_context *db, const char *keystr,
+ int32_t v)
+{
+ uint8_t v_store[sizeof(int32_t)];
+ TDB_DATA data = { .dptr = v_store, .dsize = sizeof(v_store) };
+ NTSTATUS status;
+
+ SIVAL(v_store, 0, v);
+
+ status = dbwrap_store(db, string_term_tdb_data(keystr), data,
+ TDB_REPLACE);
+ return status;
+}
+
+struct dbwrap_fetch_uint32_state {
+ NTSTATUS status;
+ uint32_t result;
+};
+
+static void dbwrap_fetch_uint32_parser(TDB_DATA key, TDB_DATA data,
+ void *private_data)
+{
+ struct dbwrap_fetch_uint32_state *state =
+ (struct dbwrap_fetch_uint32_state *)private_data;
+
+ if (data.dsize != sizeof(state->result)) {
+ state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
+ return;
+ }
+ state->result = IVAL(data.dptr, 0);
+ state->status = NT_STATUS_OK;
+}
+
+NTSTATUS dbwrap_fetch_uint32_bystring(struct db_context *db,
+ const char *keystr, uint32_t *val)
+{
+ struct dbwrap_fetch_uint32_state state;
+ NTSTATUS status;
+
+ if (val == NULL) {
+ return NT_STATUS_INVALID_PARAMETER;
+ }
+
+ state.status = NT_STATUS_INTERNAL_ERROR;
+
+ status = dbwrap_parse_record(db, string_term_tdb_data(keystr),
+ dbwrap_fetch_uint32_parser, &state);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
+ if (NT_STATUS_IS_OK(state.status)) {
+ *val = state.result;
+ }
+ return state.status;
+}
+
+NTSTATUS dbwrap_store_uint32_bystring(struct db_context *db,
+ const char *keystr, uint32_t v)
+{
+ uint8_t v_store[sizeof(uint32_t)];
+ TDB_DATA data = { .dptr = v_store, .dsize = sizeof(v_store) };
+ NTSTATUS status;
+
+ SIVAL(v_store, 0, v);
+
+ status = dbwrap_store(db, string_term_tdb_data(keystr), data,
+ TDB_REPLACE);
+ return status;
+}
+
+/**
+ * Atomic unsigned integer change (addition):
+ *
+ * if value does not exist yet in the db, use *oldval as initial old value.
+ * return old value in *oldval.
+ * store *oldval + change_val to db.
+ */
+
+struct dbwrap_change_uint32_atomic_context {
+ const char *keystr;
+ uint32_t *oldval;
+ uint32_t change_val;
+ NTSTATUS status;
+};
+
+static void dbwrap_change_uint32_atomic_action_fn(struct db_record *rec,
+ TDB_DATA value,
+ void *private_data)
+{
+ struct dbwrap_change_uint32_atomic_context *state = private_data;
+ uint8_t v_store[4];
+ TDB_DATA data = {
+ .dptr = v_store,
+ .dsize = sizeof(v_store),
+ };
+ uint32_t val = UINT32_MAX;
+
+ if (value.dptr == NULL) {
+ val = *(state->oldval);
+ } else if (value.dsize == sizeof(val)) {
+ val = PULL_LE_U32(value.dptr, 0);
+ *(state->oldval) = val;
+ } else {
+ state->status = NT_STATUS_UNSUCCESSFUL;
+ return;
+ }
+
+ val += state->change_val;
+ PUSH_LE_U32(v_store, 0, val);
+
+ state->status = dbwrap_record_store(rec, data, TDB_REPLACE);
+}
+
+static NTSTATUS dbwrap_change_uint32_atomic_action(struct db_context *db,
+ void *private_data)
+{
+ struct dbwrap_change_uint32_atomic_context *state = private_data;
+ NTSTATUS status;
+
+ status = dbwrap_do_locked(db,
+ string_term_tdb_data(state->keystr),
+ dbwrap_change_uint32_atomic_action_fn,
+ state);
+ if (!NT_STATUS_IS_OK(status)) {
+ DBG_DEBUG("dbwrap_do_locked() failed: %s\n",
+ nt_errstr(status));
+ return status;
+ }
+ if (!NT_STATUS_IS_OK(state->status)) {
+ DBG_DEBUG("dbwrap_change_uint32_atomic_action_fn() "
+ "failed: %s\n",
+ nt_errstr(status));
+ return status;
+ }
+ return NT_STATUS_OK;
+}
+
+NTSTATUS dbwrap_change_uint32_atomic_bystring(struct db_context *db,
+ const char *keystr,
+ uint32_t *oldval,
+ uint32_t change_val)
+{
+ NTSTATUS ret;
+ struct dbwrap_change_uint32_atomic_context state;
+
+ state.keystr = keystr;
+ state.oldval = oldval;
+ state.change_val = change_val;
+
+ ret = dbwrap_change_uint32_atomic_action(db, &state);
+
+ return ret;
+}
+
+NTSTATUS dbwrap_trans_change_uint32_atomic_bystring(struct db_context *db,
+ const char *keystr,
+ uint32_t *oldval,
+ uint32_t change_val)
+{
+ NTSTATUS ret;
+ struct dbwrap_change_uint32_atomic_context state;
+
+ state.keystr = keystr;
+ state.oldval = oldval;
+ state.change_val = change_val;
+
+ ret = dbwrap_trans_do(db, dbwrap_change_uint32_atomic_action, &state);
+
+ return ret;
+}
+
+/**
+ * Atomic integer change (addition):
+ *
+ * if value does not exist yet in the db, use *oldval as initial old value.
+ * return old value in *oldval.
+ * store *oldval + change_val to db.
+ */
+
+struct dbwrap_change_int32_atomic_context {
+ TDB_DATA key;
+ int32_t *oldval;
+ int32_t change_val;
+ NTSTATUS status;
+};
+
+static void dbwrap_change_int32_atomic_action_fn(struct db_record *rec,
+ TDB_DATA value,
+ void *private_data)
+{
+ struct dbwrap_change_int32_atomic_context *state = private_data;
+ uint8_t v_store[4];
+ TDB_DATA data = {
+ .dptr = v_store,
+ .dsize = sizeof(v_store),
+ };
+ int32_t val = -1;
+
+ if (value.dptr == NULL) {
+ val = *(state->oldval);
+ } else if (value.dsize == sizeof(val)) {
+ val = PULL_LE_U32(value.dptr, 0);
+ *(state->oldval) = val;
+ } else {
+ state->status = NT_STATUS_UNSUCCESSFUL;
+ return;
+ }
+
+ val += state->change_val;
+ PUSH_LE_U32(v_store, 0, val);
+
+ state->status = dbwrap_record_store(rec, data, TDB_REPLACE);
+}
+
+static NTSTATUS dbwrap_change_int32_atomic_action(struct db_context *db,
+ void *private_data)
+{
+ struct dbwrap_change_int32_atomic_context *state = private_data;
+ NTSTATUS status;
+
+ status = dbwrap_do_locked(db,
+ state->key,
+ dbwrap_change_int32_atomic_action_fn,
+ state);
+ if (!NT_STATUS_IS_OK(status)) {
+ DBG_DEBUG("dbwrap_do_locked() failed: %s\n",
+ nt_errstr(status));
+ return status;
+ }
+ if (!NT_STATUS_IS_OK(state->status)) {
+ DBG_DEBUG("dbwrap_change_int32_atomic_action_fn() "
+ "failed: %s\n",
+ nt_errstr(status));
+ return status;
+ }
+ return NT_STATUS_OK;
+}
+
+NTSTATUS dbwrap_change_int32_atomic(struct db_context *db,
+ TDB_DATA key,
+ int32_t *oldval,
+ int32_t change_val)
+{
+ NTSTATUS ret;
+ struct dbwrap_change_int32_atomic_context state;
+
+ state.key = key;
+ state.oldval = oldval;
+ state.change_val = change_val;
+
+ ret = dbwrap_change_int32_atomic_action(db, &state);
+
+ return ret;
+}
+
+NTSTATUS dbwrap_change_int32_atomic_bystring(struct db_context *db,
+ const char *keystr,
+ int32_t *oldval,
+ int32_t change_val)
+{
+ return dbwrap_change_int32_atomic(db, string_term_tdb_data(keystr),
+ oldval, change_val);
+}
+
+NTSTATUS dbwrap_trans_change_int32_atomic_bystring(struct db_context *db,
+ const char *keystr,
+ int32_t *oldval,
+ int32_t change_val)
+{
+ NTSTATUS ret;
+ struct dbwrap_change_int32_atomic_context state;
+
+ state.key = string_term_tdb_data(keystr);
+ state.oldval = oldval;
+ state.change_val = change_val;
+
+ ret = dbwrap_trans_do(db, dbwrap_change_int32_atomic_action, &state);
+
+ return ret;
+}
+
+struct dbwrap_store_context {
+ TDB_DATA *key;
+ TDB_DATA *dbuf;
+ int flag;
+};
+
+static NTSTATUS dbwrap_store_action(struct db_context *db, void *private_data)
+{
+ NTSTATUS status;
+ struct dbwrap_store_context *store_ctx;
+
+ store_ctx = (struct dbwrap_store_context *)private_data;
+
+ status = dbwrap_store(db, *(store_ctx->key), *(store_ctx->dbuf),
+ store_ctx->flag);
+ if (!NT_STATUS_IS_OK(status)) {
+ DEBUG(5, ("store returned %s\n", nt_errstr(status)));
+ }
+
+ return status;
+}
+
+NTSTATUS dbwrap_trans_store(struct db_context *db, TDB_DATA key, TDB_DATA dbuf,
+ int flag)
+{
+ NTSTATUS status;
+ struct dbwrap_store_context store_ctx;
+
+ store_ctx.key = &key;
+ store_ctx.dbuf = &dbuf;
+ store_ctx.flag = flag;
+
+ status = dbwrap_trans_do(db, dbwrap_store_action, &store_ctx);
+
+ return status;
+}
+
+static NTSTATUS dbwrap_delete_action(struct db_context * db, void *private_data)
+{
+ NTSTATUS status;
+ TDB_DATA *key = (TDB_DATA *)private_data;
+
+ status = dbwrap_delete(db, *key);
+ if (!NT_STATUS_IS_OK(status)) {
+ DBG_INFO("dbwrap_record_delete returned %s\n",
+ nt_errstr(status));
+ }
+ return status;
+}
+
+NTSTATUS dbwrap_trans_delete(struct db_context *db, TDB_DATA key)
+{
+ NTSTATUS status;
+
+ status = dbwrap_trans_do(db, dbwrap_delete_action, &key);
+
+ return status;
+}
+
+NTSTATUS dbwrap_trans_store_int32_bystring(struct db_context *db,
+ const char *keystr,
+ int32_t v)
+{
+ int32_t v_store;
+
+ SIVAL(&v_store, 0, v);
+
+ return dbwrap_trans_store(db, string_term_tdb_data(keystr),
+ make_tdb_data((const uint8_t *)&v_store,
+ sizeof(v_store)),
+ TDB_REPLACE);
+}
+
+NTSTATUS dbwrap_trans_store_uint32_bystring(struct db_context *db,
+ const char *keystr,
+ uint32_t v)
+{
+ uint32_t v_store;
+
+ SIVAL(&v_store, 0, v);
+
+ return dbwrap_trans_store(db, string_term_tdb_data(keystr),
+ make_tdb_data((const uint8_t *)&v_store,
+ sizeof(v_store)),
+ TDB_REPLACE);
+}
+
+NTSTATUS dbwrap_trans_store_bystring(struct db_context *db, const char *key,
+ TDB_DATA data, int flags)
+{
+ return dbwrap_trans_store(db, string_term_tdb_data(key), data, flags);
+}
+
+NTSTATUS dbwrap_trans_delete_bystring(struct db_context *db, const char *key)
+{
+ return dbwrap_trans_delete(db, string_term_tdb_data(key));
+}
+
+/**
+ * Wrap db action(s) into a transaction.
+ */
+NTSTATUS dbwrap_trans_do(struct db_context *db,
+ NTSTATUS (*action)(struct db_context *, void *),
+ void *private_data)
+{
+ int res;
+ NTSTATUS status;
+
+ res = dbwrap_transaction_start(db);
+ if (res != 0) {
+ DEBUG(5, ("transaction_start failed\n"));
+ return NT_STATUS_INTERNAL_DB_CORRUPTION;
+ }
+
+ status = action(db, private_data);
+ if (!NT_STATUS_IS_OK(status)) {
+ if (dbwrap_transaction_cancel(db) != 0) {
+ smb_panic("Cancelling transaction failed");
+ }
+ return status;
+ }
+
+ res = dbwrap_transaction_commit(db);
+ if (res == 0) {
+ return NT_STATUS_OK;
+ }
+
+ DEBUG(2, ("transaction_commit failed\n"));
+ return NT_STATUS_INTERNAL_DB_CORRUPTION;
+}
+
+struct dbwrap_trans_traverse_action_ctx {
+ int (*f)(struct db_record* rec, void* private_data);
+ void* private_data;
+};
+
+
+static NTSTATUS dbwrap_trans_traverse_action(struct db_context* db, void* private_data)
+{
+ struct dbwrap_trans_traverse_action_ctx* ctx =
+ (struct dbwrap_trans_traverse_action_ctx*)private_data;
+
+ NTSTATUS status = dbwrap_traverse(db, ctx->f, ctx->private_data, NULL);
+
+ return status;
+}
+
+NTSTATUS dbwrap_trans_traverse(struct db_context *db,
+ int (*f)(struct db_record*, void*),
+ void *private_data)
+{
+ struct dbwrap_trans_traverse_action_ctx ctx = {
+ .f = f,
+ .private_data = private_data,
+ };
+ return dbwrap_trans_do(db, dbwrap_trans_traverse_action, &ctx);
+}
+
+NTSTATUS dbwrap_purge(struct db_context *db, TDB_DATA key)
+{
+ NTSTATUS status;
+
+ status = dbwrap_delete(db, key);
+ if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
+ status = NT_STATUS_OK;
+ }
+
+ return status;
+}
+
+NTSTATUS dbwrap_purge_bystring(struct db_context *db, const char *key)
+{
+ return dbwrap_purge(db, string_term_tdb_data(key));
+}
+
+NTSTATUS dbwrap_delete_bystring(struct db_context *db, const char *key)
+{
+ return dbwrap_delete(db, string_term_tdb_data(key));
+}
+
+NTSTATUS dbwrap_store_bystring(struct db_context *db, const char *key,
+ TDB_DATA data, int flags)
+{
+ return dbwrap_store(db, string_term_tdb_data(key), data, flags);
+}
+
+NTSTATUS dbwrap_fetch_bystring(struct db_context *db, TALLOC_CTX *mem_ctx,
+ const char *key, TDB_DATA *value)
+{
+ return dbwrap_fetch(db, mem_ctx, string_term_tdb_data(key), value);
+}
+
+
+
+NTSTATUS dbwrap_delete_bystring_upper(struct db_context *db, const char *key)
+{
+ char *key_upper;
+ NTSTATUS status;
+
+ key_upper = talloc_strdup_upper(talloc_tos(), key);
+ if (key_upper == NULL) {
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ status = dbwrap_delete_bystring(db, key_upper);
+
+ talloc_free(key_upper);
+ return status;
+}
+
+NTSTATUS dbwrap_store_bystring_upper(struct db_context *db, const char *key,
+ TDB_DATA data, int flags)
+{
+ char *key_upper;
+ NTSTATUS status;
+
+ key_upper = talloc_strdup_upper(talloc_tos(), key);
+ if (key_upper == NULL) {
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ status = dbwrap_store_bystring(db, key_upper, data, flags);
+
+ talloc_free(key_upper);
+ return status;
+}
+
+NTSTATUS dbwrap_fetch_bystring_upper(struct db_context *db, TALLOC_CTX *mem_ctx,
+ const char *key, TDB_DATA *value)
+{
+ char *key_upper;
+ NTSTATUS status;
+
+ key_upper = talloc_strdup_upper(talloc_tos(), key);
+ if (key_upper == NULL) {
+ return NT_STATUS_NO_MEMORY;
+ }
+
+ status = dbwrap_fetch_bystring(db, mem_ctx, key_upper, value);
+
+ talloc_free(key_upper);
+ return status;
+}
+
+struct dbwrap_marshall_state {
+ uint8_t *buf;
+ size_t bufsize;
+ size_t dbsize;
+};
+
+static int dbwrap_marshall_fn(struct db_record *rec, void *private_data)
+{
+ struct dbwrap_marshall_state *state = private_data;
+ TDB_DATA key, value;
+ size_t new_dbsize;
+
+ key = dbwrap_record_get_key(rec);
+ value = dbwrap_record_get_value(rec);
+
+ new_dbsize = state->dbsize;
+ new_dbsize += 8 + key.dsize;
+ new_dbsize += 8 + value.dsize;
+
+ if (new_dbsize <= state->bufsize) {
+ uint8_t *p = state->buf + state->dbsize;
+
+ SBVAL(p, 0, key.dsize);
+ p += 8;
+ memcpy(p, key.dptr, key.dsize);
+ p += key.dsize;
+
+ SBVAL(p, 0, value.dsize);
+ p += 8;
+ memcpy(p, value.dptr, value.dsize);
+ }
+ state->dbsize = new_dbsize;
+ return 0;
+}
+
+size_t dbwrap_marshall(struct db_context *db, uint8_t *buf, size_t bufsize)
+{
+ struct dbwrap_marshall_state state;
+
+ state.bufsize = bufsize;
+ state.buf = buf;
+ state.dbsize = 0;
+
+ dbwrap_traverse_read(db, dbwrap_marshall_fn, &state, NULL);
+
+ return state.dbsize;
+}
+
+static ssize_t dbwrap_unmarshall_get_data(const uint8_t *buf, size_t buflen,
+ size_t ofs, TDB_DATA *pdata)
+{
+ uint64_t space, len;
+ const uint8_t *p;
+
+ if (ofs == buflen) {
+ return 0;
+ }
+ if (ofs > buflen) {
+ return -1;
+ }
+
+ space = buflen - ofs;
+ if (space < 8) {
+ return -1;
+ }
+
+ p = buf + ofs;
+ len = BVAL(p, 0);
+
+ p += 8;
+ space -= 8;
+
+ if (len > space) {
+ return -1;
+ }
+
+ *pdata = (TDB_DATA) { .dptr = discard_const_p(uint8_t, p),
+ .dsize = len };
+ return len + 8;
+}
+
+NTSTATUS dbwrap_parse_marshall_buf(const uint8_t *buf, size_t buflen,
+ bool (*fn)(TDB_DATA key, TDB_DATA value,
+ void *private_data),
+ void *private_data)
+{
+ size_t ofs = 0;
+
+ while (true) {
+ ssize_t len;
+ TDB_DATA key, value;
+ bool ok;
+
+ len = dbwrap_unmarshall_get_data(buf, buflen, ofs, &key);
+ if (len == 0) {
+ break;
+ }
+ if (len == -1) {
+ return NT_STATUS_INVALID_PARAMETER;
+ }
+ ofs += len;
+
+ len = dbwrap_unmarshall_get_data(buf, buflen, ofs, &value);
+ if (len == 0) {
+ break;
+ }
+ if (len == -1) {
+ return NT_STATUS_INVALID_PARAMETER;
+ }
+ ofs += len;
+
+ ok = fn(key, value, private_data);
+ if (!ok) {
+ break;
+ }
+ }
+
+ return NT_STATUS_OK;
+}
+
+struct dbwrap_unmarshall_state {
+ struct db_context *db;
+ NTSTATUS ret;
+};
+
+static bool dbwrap_unmarshall_fn(TDB_DATA key, TDB_DATA value,
+ void *private_data)
+{
+ struct dbwrap_unmarshall_state *state = private_data;
+ NTSTATUS status;
+
+ status = dbwrap_store(state->db, key, value, 0);
+ if (!NT_STATUS_IS_OK(status)) {
+ DBG_DEBUG("dbwrap_record_store failed: %s\n",
+ nt_errstr(status));
+ state->ret = status;
+ return false;
+ }
+
+ return true;
+}
+
+NTSTATUS dbwrap_unmarshall(struct db_context *db, const uint8_t *buf,
+ size_t buflen)
+{
+ struct dbwrap_unmarshall_state state = { .db = db };
+ NTSTATUS status;
+
+ status = dbwrap_parse_marshall_buf(buf, buflen,
+ dbwrap_unmarshall_fn, &state);
+ if (!NT_STATUS_IS_OK(status)) {
+ return status;
+ }
+ return state.ret;
+}
diff --git a/lib/dbwrap/wscript_build b/lib/dbwrap/wscript_build
new file mode 100644
index 0000000..0e5f961
--- /dev/null
+++ b/lib/dbwrap/wscript_build
@@ -0,0 +1,8 @@
+SRC = '''dbwrap.c dbwrap_util.c dbwrap_rbt.c dbwrap_tdb.c
+ dbwrap_local_open.c'''
+DEPS= '''samba-util util_tdb samba-errors tdb tdb-wrap tevent tevent-util'''
+
+bld.SAMBA_LIBRARY('dbwrap',
+ source=SRC,
+ deps=DEPS,
+ private_library=True)