2 Unix SMB/CIFS implementation.
3 Database interface wrapper around tdb
4 Copyright (C) Volker Lendecke 2005-2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 struct tdb_wrap *wtdb;
26 static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag);
27 static NTSTATUS db_tdb_delete(struct db_record *rec);
29 static int db_tdb_record_destr(struct db_record* data)
31 struct db_tdb_ctx *ctx =
32 talloc_get_type_abort(data->private_data, struct db_tdb_ctx);
34 DEBUG(10, ("Unlocking key %s\n",
35 hex_encode(data, (unsigned char *)data->key.dptr,
38 if (tdb_chainunlock(ctx->wtdb->tdb, data->key) != 0) {
39 DEBUG(0, ("tdb_chainunlock failed\n"));
45 static struct db_record *db_tdb_fetch_locked(struct db_context *db,
46 TALLOC_CTX *mem_ctx, TDB_DATA key)
48 struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
50 struct db_record *result;
53 result = TALLOC_P(mem_ctx, struct db_record);
55 DEBUG(0, ("talloc failed\n"));
59 result->key.dsize = key.dsize;
60 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
61 if (result->key.dptr == NULL) {
62 DEBUG(0, ("talloc failed\n"));
67 result->value.dptr = NULL;
68 result->value.dsize = 0;
69 result->private_data = talloc_reference(result, ctx);
70 result->store = db_tdb_store;
71 result->delete_rec = db_tdb_delete;
74 char *keystr = hex_encode(NULL, (unsigned char *)key.dptr,
76 DEBUG(10, ("Locking key %s\n", keystr));
80 if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
81 DEBUG(3, ("tdb_chainlock failed\n"));
86 talloc_set_destructor(result, db_tdb_record_destr);
88 value = tdb_fetch(ctx->wtdb->tdb, key);
90 if (value.dptr == NULL) {
94 result->value.dsize = value.dsize;
95 result->value.dptr = (uint8 *)talloc_memdup(result, value.dptr,
97 if (result->value.dptr == NULL) {
98 DEBUG(3, ("talloc failed\n"));
103 SAFE_FREE(value.dptr);
105 DEBUG(10, ("Allocated locked data 0x%p\n", result));
110 static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag)
112 struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
116 * This has a bug: We need to replace rec->value for correct
117 * operation, but right now brlock and locking don't use the value
118 * anymore after it was stored.
121 return (tdb_store(ctx->wtdb->tdb, rec->key, data, flag) == 0) ?
122 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
125 static NTSTATUS db_tdb_delete(struct db_record *rec)
127 struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
131 res = tdb_delete(ctx->wtdb->tdb, rec->key);
137 return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb));
140 struct db_tdb_traverse_ctx {
141 struct db_context *db;
142 int (*f)(struct db_record *rec, void *private_data);
146 static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
149 struct db_tdb_traverse_ctx *ctx =
150 (struct db_tdb_traverse_ctx *)private_data;
151 struct db_record rec;
155 rec.store = db_tdb_store;
156 rec.delete_rec = db_tdb_delete;
157 rec.private_data = ctx->db->private_data;
159 return ctx->f(&rec, ctx->private_data);
162 static int db_tdb_traverse(struct db_context *db,
163 int (*f)(struct db_record *rec, void *private_data),
166 struct db_tdb_ctx *db_ctx =
167 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
168 struct db_tdb_traverse_ctx ctx;
172 ctx.private_data = private_data;
173 return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx);
176 static NTSTATUS db_tdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
178 return NT_STATUS_MEDIA_WRITE_PROTECTED;
181 static NTSTATUS db_tdb_delete_deny(struct db_record *rec)
183 return NT_STATUS_MEDIA_WRITE_PROTECTED;
186 static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
189 struct db_tdb_traverse_ctx *ctx =
190 (struct db_tdb_traverse_ctx *)private_data;
191 struct db_record rec;
195 rec.store = db_tdb_store_deny;
196 rec.delete_rec = db_tdb_delete_deny;
197 rec.private_data = ctx->db->private_data;
199 return ctx->f(&rec, ctx->private_data);
202 static int db_tdb_traverse_read(struct db_context *db,
203 int (*f)(struct db_record *rec, void *private_data),
206 struct db_tdb_ctx *db_ctx =
207 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
208 struct db_tdb_traverse_ctx ctx;
212 ctx.private_data = private_data;
213 return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx);
216 static int db_tdb_get_seqnum(struct db_context *db)
219 struct db_tdb_ctx *db_ctx =
220 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
221 return tdb_get_seqnum(db_ctx->wtdb->tdb);
224 struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
226 int hash_size, int tdb_flags,
227 int open_flags, mode_t mode)
229 struct db_context *result = NULL;
230 struct db_tdb_ctx *db_tdb;
232 result = TALLOC_ZERO_P(mem_ctx, struct db_context);
233 if (result == NULL) {
234 DEBUG(0, ("talloc failed\n"));
238 result->private_data = db_tdb = TALLOC_P(result, struct db_tdb_ctx);
239 if (db_tdb == NULL) {
240 DEBUG(0, ("talloc failed\n"));
244 db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags,
246 if (db_tdb->wtdb == NULL) {
247 DEBUG(3, ("Could not open tdb: %s\n", strerror(errno)));
251 result->fetch_locked = db_tdb_fetch_locked;
252 result->traverse = db_tdb_traverse;
253 result->traverse_read = db_tdb_traverse_read;
254 result->get_seqnum = db_tdb_get_seqnum;
258 if (result != NULL) {