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