dbwrap: dbwrap_local_open()
[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 struct db_tdb_parse_state {
191         void (*parser)(TDB_DATA key, TDB_DATA data,
192                        void *private_data);
193         void *private_data;
194 };
195
196 /*
197  * tdb_parse_record expects a parser returning int, mixing up tdb and
198  * parser errors. Wrap around that by always returning 0 and have
199  * dbwrap_parse_record expect a parser returning void.
200  */
201
202 static int db_tdb_parser(TDB_DATA key, TDB_DATA data, void *private_data)
203 {
204         struct db_tdb_parse_state *state =
205                 (struct db_tdb_parse_state *)private_data;
206         state->parser(key, data, state->private_data);
207         return 0;
208 }
209
210 static NTSTATUS db_tdb_parse(struct db_context *db, TDB_DATA key,
211                              void (*parser)(TDB_DATA key, TDB_DATA data,
212                                            void *private_data),
213                              void *private_data)
214 {
215         struct db_tdb_ctx *ctx = talloc_get_type_abort(
216                 db->private_data, struct db_tdb_ctx);
217         struct db_tdb_parse_state state;
218         int ret;
219
220         state.parser = parser;
221         state.private_data = private_data;
222
223         ret = tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_parser, &state);
224
225         if (ret != 0) {
226                 return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb));
227         }
228         return NT_STATUS_OK;
229 }
230
231 static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag)
232 {
233         struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
234                                                        struct db_tdb_ctx);
235
236         /*
237          * This has a bug: We need to replace rec->value for correct
238          * operation, but right now brlock and locking don't use the value
239          * anymore after it was stored.
240          */
241
242         return (tdb_store(ctx->wtdb->tdb, rec->key, data, flag) == 0) ?
243                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
244 }
245
246 static NTSTATUS db_tdb_delete(struct db_record *rec)
247 {
248         struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
249                                                        struct db_tdb_ctx);
250
251         if (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) {
252                 return NT_STATUS_OK;
253         }
254
255         if (tdb_error(ctx->wtdb->tdb) == TDB_ERR_NOEXIST) {
256                 return NT_STATUS_NOT_FOUND;
257         }
258
259         return NT_STATUS_UNSUCCESSFUL;
260 }
261
262 struct db_tdb_traverse_ctx {
263         struct db_context *db;
264         int (*f)(struct db_record *rec, void *private_data);
265         void *private_data;
266 };
267
268 static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
269                                 void *private_data)
270 {
271         struct db_tdb_traverse_ctx *ctx =
272                 (struct db_tdb_traverse_ctx *)private_data;
273         struct db_record rec;
274
275         rec.key = kbuf;
276         rec.value = dbuf;
277         rec.store = db_tdb_store;
278         rec.delete_rec = db_tdb_delete;
279         rec.private_data = ctx->db->private_data;
280         rec.db = ctx->db;
281
282         return ctx->f(&rec, ctx->private_data);
283 }
284
285 static int db_tdb_traverse(struct db_context *db,
286                            int (*f)(struct db_record *rec, void *private_data),
287                            void *private_data)
288 {
289         struct db_tdb_ctx *db_ctx =
290                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
291         struct db_tdb_traverse_ctx ctx;
292
293         ctx.db = db;
294         ctx.f = f;
295         ctx.private_data = private_data;
296         return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx);
297 }
298
299 static NTSTATUS db_tdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
300 {
301         return NT_STATUS_MEDIA_WRITE_PROTECTED;
302 }
303
304 static NTSTATUS db_tdb_delete_deny(struct db_record *rec)
305 {
306         return NT_STATUS_MEDIA_WRITE_PROTECTED;
307 }
308
309 static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
310                                 void *private_data)
311 {
312         struct db_tdb_traverse_ctx *ctx =
313                 (struct db_tdb_traverse_ctx *)private_data;
314         struct db_record rec;
315
316         rec.key = kbuf;
317         rec.value = dbuf;
318         rec.store = db_tdb_store_deny;
319         rec.delete_rec = db_tdb_delete_deny;
320         rec.private_data = ctx->db->private_data;
321         rec.db = ctx->db;
322
323         return ctx->f(&rec, ctx->private_data);
324 }
325
326 static int db_tdb_traverse_read(struct db_context *db,
327                            int (*f)(struct db_record *rec, void *private_data),
328                            void *private_data)
329 {
330         struct db_tdb_ctx *db_ctx =
331                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
332         struct db_tdb_traverse_ctx ctx;
333
334         ctx.db = db;
335         ctx.f = f;
336         ctx.private_data = private_data;
337         return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx);
338 }
339
340 static int db_tdb_get_seqnum(struct db_context *db)
341
342 {
343         struct db_tdb_ctx *db_ctx =
344                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
345         return tdb_get_seqnum(db_ctx->wtdb->tdb);
346 }
347
348 static int db_tdb_transaction_start(struct db_context *db)
349 {
350         struct db_tdb_ctx *db_ctx =
351                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
352         return tdb_transaction_start(db_ctx->wtdb->tdb) ? -1 : 0;
353 }
354
355 static int db_tdb_transaction_commit(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_commit(db_ctx->wtdb->tdb) ? -1 : 0;
360 }
361
362 static int db_tdb_transaction_cancel(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         tdb_transaction_cancel(db_ctx->wtdb->tdb);
367         return 0;
368 }
369
370 static void db_tdb_id(struct db_context *db, const uint8_t **id, size_t *idlen)
371 {
372         struct db_tdb_ctx *db_ctx =
373                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
374         *id = (uint8_t *)&db_ctx->id;
375         *idlen = sizeof(db_ctx->id);
376 }
377
378 struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
379                                struct loadparm_context *lp_ctx,
380                                const char *name,
381                                int hash_size, int tdb_flags,
382                                int open_flags, mode_t mode,
383                                enum dbwrap_lock_order lock_order)
384 {
385         struct db_context *result = NULL;
386         struct db_tdb_ctx *db_tdb;
387         struct stat st;
388
389         /* Extra paranoia. */
390         if (name && strends(name, ".ntdb")) {
391                 DEBUG(0, ("can't try to open %s with tdb!\n", name));
392                 return NULL;
393         }
394
395         result = talloc_zero(mem_ctx, struct db_context);
396         if (result == NULL) {
397                 DEBUG(0, ("talloc failed\n"));
398                 goto fail;
399         }
400
401         result->private_data = db_tdb = talloc(result, struct db_tdb_ctx);
402         if (db_tdb == NULL) {
403                 DEBUG(0, ("talloc failed\n"));
404                 goto fail;
405         }
406         result->lock_order = lock_order;
407
408         db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags,
409                                      open_flags, mode, lp_ctx);
410         if (db_tdb->wtdb == NULL) {
411                 DEBUG(3, ("Could not open tdb: %s\n", strerror(errno)));
412                 goto fail;
413         }
414
415         ZERO_STRUCT(db_tdb->id);
416
417         if (fstat(tdb_fd(db_tdb->wtdb->tdb), &st) == -1) {
418                 DEBUG(3, ("fstat failed: %s\n", strerror(errno)));
419                 goto fail;
420         }
421         db_tdb->id.dev = st.st_dev;
422         db_tdb->id.ino = st.st_ino;
423
424         result->fetch_locked = db_tdb_fetch_locked;
425         result->try_fetch_locked = db_tdb_try_fetch_locked;
426         result->traverse = db_tdb_traverse;
427         result->traverse_read = db_tdb_traverse_read;
428         result->parse_record = db_tdb_parse;
429         result->get_seqnum = db_tdb_get_seqnum;
430         result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
431         result->transaction_start = db_tdb_transaction_start;
432         result->transaction_commit = db_tdb_transaction_commit;
433         result->transaction_cancel = db_tdb_transaction_cancel;
434         result->exists = db_tdb_exists;
435         result->wipe = db_tdb_wipe;
436         result->id = db_tdb_id;
437         result->stored_callback = NULL;
438         return result;
439
440  fail:
441         if (result != NULL) {
442                 TALLOC_FREE(result);
443         }
444         return NULL;
445 }