dbwrap: add dbwrap_check() function.
[ddiss/samba.git] / lib / dbwrap / dbwrap_tdb.c
1 /*
2    Unix SMB/CIFS implementation.
3    Database interface wrapper around tdb
4    Copyright (C) Volker Lendecke 2005-2007
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "dbwrap/dbwrap.h"
22 #include "dbwrap/dbwrap_private.h"
23 #include "dbwrap/dbwrap_tdb.h"
24 #include "lib/tdb_wrap/tdb_wrap.h"
25 #include "lib/util/util_tdb.h"
26 #include "system/filesys.h"
27 #include "ccan/str/str.h"
28
29 struct db_tdb_ctx {
30         struct tdb_wrap *wtdb;
31
32         struct {
33                 dev_t dev;
34                 ino_t ino;
35         } id;
36 };
37
38 static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag);
39 static NTSTATUS db_tdb_delete(struct db_record *rec);
40
41 static void db_tdb_log_key(const char *prefix, TDB_DATA key)
42 {
43         size_t len;
44         char *keystr;
45
46         if (DEBUGLEVEL < 10) {
47                 return;
48         }
49         len = key.dsize;
50         if (DEBUGLEVEL == 10) {
51                 /*
52                  * Only fully spam at debuglevel > 10
53                  */
54                 len = MIN(10, key.dsize);
55         }
56         keystr = hex_encode_talloc(talloc_tos(), (unsigned char *)(key.dptr),
57                                    len);
58         DEBUG(10, ("%s key %s\n", prefix, keystr));
59         TALLOC_FREE(keystr);
60 }
61
62 static int db_tdb_record_destr(struct db_record* data)
63 {
64         struct db_tdb_ctx *ctx =
65                 talloc_get_type_abort(data->private_data, struct db_tdb_ctx);
66
67         db_tdb_log_key("Unlocking", data->key);
68         tdb_chainunlock(ctx->wtdb->tdb, data->key);
69         return 0;
70 }
71
72 struct tdb_fetch_locked_state {
73         TALLOC_CTX *mem_ctx;
74         struct db_record *result;
75 };
76
77 static int db_tdb_fetchlock_parse(TDB_DATA key, TDB_DATA data,
78                                   void *private_data)
79 {
80         struct tdb_fetch_locked_state *state =
81                 (struct tdb_fetch_locked_state *)private_data;
82         struct db_record *result;
83
84         result = (struct db_record *)talloc_size(
85                 state->mem_ctx,
86                 sizeof(struct db_record) + key.dsize + data.dsize);
87
88         if (result == NULL) {
89                 return 0;
90         }
91         state->result = result;
92
93         result->key.dsize = key.dsize;
94         result->key.dptr = ((uint8_t *)result) + sizeof(struct db_record);
95         memcpy(result->key.dptr, key.dptr, key.dsize);
96
97         result->value.dsize = data.dsize;
98
99         if (data.dsize > 0) {
100                 result->value.dptr = result->key.dptr+key.dsize;
101                 memcpy(result->value.dptr, data.dptr, data.dsize);
102         }
103         else {
104                 result->value.dptr = NULL;
105         }
106
107         return 0;
108 }
109
110 static struct db_record *db_tdb_fetch_locked_internal(
111         struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
112 {
113         struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
114                                                        struct db_tdb_ctx);
115         struct tdb_fetch_locked_state state;
116
117         state.mem_ctx = mem_ctx;
118         state.result = NULL;
119
120         if ((tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetchlock_parse,
121                               &state) < 0) &&
122             (tdb_error(ctx->wtdb->tdb) != TDB_ERR_NOEXIST)) {
123                 tdb_chainunlock(ctx->wtdb->tdb, key);
124                 return NULL;
125         }
126
127         if (state.result == NULL) {
128                 db_tdb_fetchlock_parse(key, tdb_null, &state);
129         }
130
131         if (state.result == NULL) {
132                 tdb_chainunlock(ctx->wtdb->tdb, key);
133                 return NULL;
134         }
135
136         talloc_set_destructor(state.result, db_tdb_record_destr);
137
138         state.result->private_data = talloc_reference(state.result, ctx);
139         state.result->store = db_tdb_store;
140         state.result->delete_rec = db_tdb_delete;
141
142         DEBUG(10, ("Allocated locked data 0x%p\n", state.result));
143
144         return state.result;
145 }
146
147 static struct db_record *db_tdb_fetch_locked(
148         struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
149 {
150         struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
151                                                        struct db_tdb_ctx);
152
153         db_tdb_log_key("Locking", key);
154         if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
155                 DEBUG(3, ("tdb_chainlock failed\n"));
156                 return NULL;
157         }
158         return db_tdb_fetch_locked_internal(db, mem_ctx, key);
159 }
160
161 static struct db_record *db_tdb_try_fetch_locked(
162         struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key)
163 {
164         struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
165                                                        struct db_tdb_ctx);
166
167         db_tdb_log_key("Trying to lock", key);
168         if (tdb_chainlock_nonblock(ctx->wtdb->tdb, key) != 0) {
169                 DEBUG(3, ("tdb_chainlock_nonblock failed\n"));
170                 return NULL;
171         }
172         return db_tdb_fetch_locked_internal(db, mem_ctx, key);
173 }
174
175
176 static int db_tdb_exists(struct db_context *db, TDB_DATA key)
177 {
178         struct db_tdb_ctx *ctx = talloc_get_type_abort(
179                 db->private_data, struct db_tdb_ctx);
180         return tdb_exists(ctx->wtdb->tdb, key);
181 }
182
183 static int db_tdb_wipe(struct db_context *db)
184 {
185         struct db_tdb_ctx *ctx = talloc_get_type_abort(
186                 db->private_data, struct db_tdb_ctx);
187         return tdb_wipe_all(ctx->wtdb->tdb);
188 }
189
190 static int db_tdb_check(struct db_context *db)
191 {
192         struct db_tdb_ctx *ctx = talloc_get_type_abort(
193                 db->private_data, struct db_tdb_ctx);
194         return tdb_check(ctx->wtdb->tdb, NULL, NULL);
195 }
196
197 struct db_tdb_parse_state {
198         void (*parser)(TDB_DATA key, TDB_DATA data,
199                        void *private_data);
200         void *private_data;
201 };
202
203 /*
204  * tdb_parse_record expects a parser returning int, mixing up tdb and
205  * parser errors. Wrap around that by always returning 0 and have
206  * dbwrap_parse_record expect a parser returning void.
207  */
208
209 static int db_tdb_parser(TDB_DATA key, TDB_DATA data, void *private_data)
210 {
211         struct db_tdb_parse_state *state =
212                 (struct db_tdb_parse_state *)private_data;
213         state->parser(key, data, state->private_data);
214         return 0;
215 }
216
217 static NTSTATUS db_tdb_parse(struct db_context *db, TDB_DATA key,
218                              void (*parser)(TDB_DATA key, TDB_DATA data,
219                                            void *private_data),
220                              void *private_data)
221 {
222         struct db_tdb_ctx *ctx = talloc_get_type_abort(
223                 db->private_data, struct db_tdb_ctx);
224         struct db_tdb_parse_state state;
225         int ret;
226
227         state.parser = parser;
228         state.private_data = private_data;
229
230         ret = tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_parser, &state);
231
232         if (ret != 0) {
233                 return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb));
234         }
235         return NT_STATUS_OK;
236 }
237
238 static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag)
239 {
240         struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
241                                                        struct db_tdb_ctx);
242
243         /*
244          * This has a bug: We need to replace rec->value for correct
245          * operation, but right now brlock and locking don't use the value
246          * anymore after it was stored.
247          */
248
249         return (tdb_store(ctx->wtdb->tdb, rec->key, data, flag) == 0) ?
250                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
251 }
252
253 static NTSTATUS db_tdb_delete(struct db_record *rec)
254 {
255         struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
256                                                        struct db_tdb_ctx);
257
258         if (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) {
259                 return NT_STATUS_OK;
260         }
261
262         if (tdb_error(ctx->wtdb->tdb) == TDB_ERR_NOEXIST) {
263                 return NT_STATUS_NOT_FOUND;
264         }
265
266         return NT_STATUS_UNSUCCESSFUL;
267 }
268
269 struct db_tdb_traverse_ctx {
270         struct db_context *db;
271         int (*f)(struct db_record *rec, void *private_data);
272         void *private_data;
273 };
274
275 static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
276                                 void *private_data)
277 {
278         struct db_tdb_traverse_ctx *ctx =
279                 (struct db_tdb_traverse_ctx *)private_data;
280         struct db_record rec;
281
282         rec.key = kbuf;
283         rec.value = dbuf;
284         rec.store = db_tdb_store;
285         rec.delete_rec = db_tdb_delete;
286         rec.private_data = ctx->db->private_data;
287         rec.db = ctx->db;
288
289         return ctx->f(&rec, ctx->private_data);
290 }
291
292 static int db_tdb_traverse(struct db_context *db,
293                            int (*f)(struct db_record *rec, void *private_data),
294                            void *private_data)
295 {
296         struct db_tdb_ctx *db_ctx =
297                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
298         struct db_tdb_traverse_ctx ctx;
299
300         ctx.db = db;
301         ctx.f = f;
302         ctx.private_data = private_data;
303         return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx);
304 }
305
306 static NTSTATUS db_tdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
307 {
308         return NT_STATUS_MEDIA_WRITE_PROTECTED;
309 }
310
311 static NTSTATUS db_tdb_delete_deny(struct db_record *rec)
312 {
313         return NT_STATUS_MEDIA_WRITE_PROTECTED;
314 }
315
316 static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
317                                 void *private_data)
318 {
319         struct db_tdb_traverse_ctx *ctx =
320                 (struct db_tdb_traverse_ctx *)private_data;
321         struct db_record rec;
322
323         rec.key = kbuf;
324         rec.value = dbuf;
325         rec.store = db_tdb_store_deny;
326         rec.delete_rec = db_tdb_delete_deny;
327         rec.private_data = ctx->db->private_data;
328         rec.db = ctx->db;
329
330         return ctx->f(&rec, ctx->private_data);
331 }
332
333 static int db_tdb_traverse_read(struct db_context *db,
334                            int (*f)(struct db_record *rec, void *private_data),
335                            void *private_data)
336 {
337         struct db_tdb_ctx *db_ctx =
338                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
339         struct db_tdb_traverse_ctx ctx;
340
341         ctx.db = db;
342         ctx.f = f;
343         ctx.private_data = private_data;
344         return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx);
345 }
346
347 static int db_tdb_get_seqnum(struct db_context *db)
348
349 {
350         struct db_tdb_ctx *db_ctx =
351                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
352         return tdb_get_seqnum(db_ctx->wtdb->tdb);
353 }
354
355 static int db_tdb_transaction_start(struct db_context *db)
356 {
357         struct db_tdb_ctx *db_ctx =
358                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
359         return tdb_transaction_start(db_ctx->wtdb->tdb) ? -1 : 0;
360 }
361
362 static int db_tdb_transaction_commit(struct db_context *db)
363 {
364         struct db_tdb_ctx *db_ctx =
365                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
366         return tdb_transaction_commit(db_ctx->wtdb->tdb) ? -1 : 0;
367 }
368
369 static int db_tdb_transaction_cancel(struct db_context *db)
370 {
371         struct db_tdb_ctx *db_ctx =
372                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
373         tdb_transaction_cancel(db_ctx->wtdb->tdb);
374         return 0;
375 }
376
377 static void db_tdb_id(struct db_context *db, const uint8_t **id, size_t *idlen)
378 {
379         struct db_tdb_ctx *db_ctx =
380                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
381         *id = (uint8_t *)&db_ctx->id;
382         *idlen = sizeof(db_ctx->id);
383 }
384
385 struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
386                                struct loadparm_context *lp_ctx,
387                                const char *name,
388                                int hash_size, int tdb_flags,
389                                int open_flags, mode_t mode,
390                                enum dbwrap_lock_order lock_order)
391 {
392         struct db_context *result = NULL;
393         struct db_tdb_ctx *db_tdb;
394         struct stat st;
395
396         /* Extra paranoia. */
397         if (name && strends(name, ".ntdb")) {
398                 DEBUG(0, ("can't try to open %s with tdb!\n", name));
399                 return NULL;
400         }
401
402         result = talloc_zero(mem_ctx, struct db_context);
403         if (result == NULL) {
404                 DEBUG(0, ("talloc failed\n"));
405                 goto fail;
406         }
407
408         result->private_data = db_tdb = talloc(result, struct db_tdb_ctx);
409         if (db_tdb == NULL) {
410                 DEBUG(0, ("talloc failed\n"));
411                 goto fail;
412         }
413         result->lock_order = lock_order;
414
415         db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags,
416                                      open_flags, mode, lp_ctx);
417         if (db_tdb->wtdb == NULL) {
418                 DEBUG(3, ("Could not open tdb: %s\n", strerror(errno)));
419                 goto fail;
420         }
421
422         ZERO_STRUCT(db_tdb->id);
423
424         if (fstat(tdb_fd(db_tdb->wtdb->tdb), &st) == -1) {
425                 DEBUG(3, ("fstat failed: %s\n", strerror(errno)));
426                 goto fail;
427         }
428         db_tdb->id.dev = st.st_dev;
429         db_tdb->id.ino = st.st_ino;
430
431         result->fetch_locked = db_tdb_fetch_locked;
432         result->try_fetch_locked = db_tdb_try_fetch_locked;
433         result->traverse = db_tdb_traverse;
434         result->traverse_read = db_tdb_traverse_read;
435         result->parse_record = db_tdb_parse;
436         result->get_seqnum = db_tdb_get_seqnum;
437         result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
438         result->transaction_start = db_tdb_transaction_start;
439         result->transaction_commit = db_tdb_transaction_commit;
440         result->transaction_cancel = db_tdb_transaction_cancel;
441         result->exists = db_tdb_exists;
442         result->wipe = db_tdb_wipe;
443         result->id = db_tdb_id;
444         result->check = db_tdb_check;
445         result->stored_callback = NULL;
446         return result;
447
448  fail:
449         if (result != NULL) {
450                 TALLOC_FREE(result);
451         }
452         return NULL;
453 }