Add db_tdb_fetch
[ira/wip.git] / source3 / lib / 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
22 struct db_tdb_ctx {
23         struct tdb_wrap *wtdb;
24 };
25
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);
28
29 static int db_tdb_record_destr(struct db_record* data)
30 {
31         struct db_tdb_ctx *ctx =
32                 talloc_get_type_abort(data->private_data, struct db_tdb_ctx);
33
34         DEBUG(10, (DEBUGLEVEL > 10
35                    ? "Unlocking key %s\n" : "Unlocking key %.20s\n",
36                    hex_encode(data, (unsigned char *)data->key.dptr,
37                               data->key.dsize)));
38
39         if (tdb_chainunlock(ctx->wtdb->tdb, data->key) != 0) {
40                 DEBUG(0, ("tdb_chainunlock failed\n"));
41                 return -1;
42         }
43         return 0;
44 }
45
46 static struct db_record *db_tdb_fetch_locked(struct db_context *db,
47                                      TALLOC_CTX *mem_ctx, TDB_DATA key)
48 {
49         struct db_tdb_ctx *ctx = talloc_get_type_abort(db->private_data,
50                                                        struct db_tdb_ctx);
51         struct db_record *result;
52         TDB_DATA value;
53
54         result = TALLOC_P(mem_ctx, struct db_record);
55         if (result == NULL) {
56                 DEBUG(0, ("talloc failed\n"));
57                 return NULL;
58         }
59
60         result->key.dsize = key.dsize;
61         result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
62         if (result->key.dptr == NULL) {
63                 DEBUG(0, ("talloc failed\n"));
64                 TALLOC_FREE(result);
65                 return NULL;
66         }
67
68         result->value.dptr = NULL;
69         result->value.dsize = 0;
70         result->private_data = talloc_reference(result, ctx);
71         result->store = db_tdb_store;
72         result->delete_rec = db_tdb_delete;
73
74         if (DEBUGLEVEL >= 10) {
75                 char *keystr = hex_encode(NULL, key.dptr, key.dsize);
76                 DEBUG(10, (DEBUGLEVEL > 10
77                            ? "Locking key %s\n" : "Locking key %.20s\n",
78                            keystr));
79                 TALLOC_FREE(keystr);
80         }
81
82         if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
83                 DEBUG(3, ("tdb_chainlock failed\n"));
84                 TALLOC_FREE(result);
85                 return NULL;
86         }
87
88         talloc_set_destructor(result, db_tdb_record_destr);
89
90         value = tdb_fetch(ctx->wtdb->tdb, key);
91
92         if (value.dptr == NULL) {
93                 return result;
94         }
95
96         result->value.dsize = value.dsize;
97         result->value.dptr = (uint8 *)talloc_memdup(result, value.dptr,
98                                                     value.dsize);
99         if (result->value.dptr == NULL) {
100                 DEBUG(3, ("talloc failed\n"));
101                 TALLOC_FREE(result);
102                 return NULL;
103         }
104
105         SAFE_FREE(value.dptr);
106
107         DEBUG(10, ("Allocated locked data 0x%p\n", result));
108
109         return result;
110 }
111
112 static int db_tdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
113                         TDB_DATA key, TDB_DATA *pdata)
114 {
115         struct db_tdb_ctx *ctx = talloc_get_type_abort(
116                 db->private_data, struct db_tdb_ctx);
117
118         TDB_DATA data;
119
120         data = tdb_fetch(ctx->wtdb->tdb, key);
121
122         if (data.dptr == NULL) {
123                 pdata->dptr = NULL;
124                 pdata->dsize = 0;
125                 return 0;
126         }
127
128         pdata->dptr = (uint8 *)talloc_memdup(mem_ctx, data.dptr, data.dsize);
129         SAFE_FREE(data.dptr);
130
131         if (pdata->dptr == NULL) {
132                 return -1;
133         }
134         pdata->dsize = data.dsize;
135         return 0;
136 }
137
138 static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag)
139 {
140         struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
141                                                        struct db_tdb_ctx);
142
143         /*
144          * This has a bug: We need to replace rec->value for correct
145          * operation, but right now brlock and locking don't use the value
146          * anymore after it was stored.
147          */
148
149         return (tdb_store(ctx->wtdb->tdb, rec->key, data, flag) == 0) ?
150                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
151 }
152
153 static NTSTATUS db_tdb_delete(struct db_record *rec)
154 {
155         struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
156                                                        struct db_tdb_ctx);
157         int res;
158         
159         res = tdb_delete(ctx->wtdb->tdb, rec->key);
160
161         if (res == 0) {
162                 return NT_STATUS_OK;
163         }
164
165         return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb));
166 }
167
168 struct db_tdb_traverse_ctx {
169         struct db_context *db;
170         int (*f)(struct db_record *rec, void *private_data);
171         void *private_data;
172 };
173
174 static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
175                                 void *private_data)
176 {
177         struct db_tdb_traverse_ctx *ctx =
178                 (struct db_tdb_traverse_ctx *)private_data;
179         struct db_record rec;
180
181         rec.key = kbuf;
182         rec.value = dbuf;
183         rec.store = db_tdb_store;
184         rec.delete_rec = db_tdb_delete;
185         rec.private_data = ctx->db->private_data;
186
187         return ctx->f(&rec, ctx->private_data);
188 }
189
190 static int db_tdb_traverse(struct db_context *db,
191                            int (*f)(struct db_record *rec, void *private_data),
192                            void *private_data)
193 {
194         struct db_tdb_ctx *db_ctx =
195                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
196         struct db_tdb_traverse_ctx ctx;
197
198         ctx.db = db;
199         ctx.f = f;
200         ctx.private_data = private_data;
201         return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx);
202 }
203
204 static NTSTATUS db_tdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
205 {
206         return NT_STATUS_MEDIA_WRITE_PROTECTED;
207 }
208
209 static NTSTATUS db_tdb_delete_deny(struct db_record *rec)
210 {
211         return NT_STATUS_MEDIA_WRITE_PROTECTED;
212 }
213
214 static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
215                                 void *private_data)
216 {
217         struct db_tdb_traverse_ctx *ctx =
218                 (struct db_tdb_traverse_ctx *)private_data;
219         struct db_record rec;
220
221         rec.key = kbuf;
222         rec.value = dbuf;
223         rec.store = db_tdb_store_deny;
224         rec.delete_rec = db_tdb_delete_deny;
225         rec.private_data = ctx->db->private_data;
226
227         return ctx->f(&rec, ctx->private_data);
228 }
229
230 static int db_tdb_traverse_read(struct db_context *db,
231                            int (*f)(struct db_record *rec, void *private_data),
232                            void *private_data)
233 {
234         struct db_tdb_ctx *db_ctx =
235                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
236         struct db_tdb_traverse_ctx ctx;
237
238         ctx.db = db;
239         ctx.f = f;
240         ctx.private_data = private_data;
241         return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx);
242 }
243
244 static int db_tdb_get_seqnum(struct db_context *db)
245
246 {
247         struct db_tdb_ctx *db_ctx =
248                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
249         return tdb_get_seqnum(db_ctx->wtdb->tdb);
250 }
251
252 struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
253                                const char *name,
254                                int hash_size, int tdb_flags,
255                                int open_flags, mode_t mode)
256 {
257         struct db_context *result = NULL;
258         struct db_tdb_ctx *db_tdb;
259
260         result = TALLOC_ZERO_P(mem_ctx, struct db_context);
261         if (result == NULL) {
262                 DEBUG(0, ("talloc failed\n"));
263                 goto fail;
264         }
265
266         result->private_data = db_tdb = TALLOC_P(result, struct db_tdb_ctx);
267         if (db_tdb == NULL) {
268                 DEBUG(0, ("talloc failed\n"));
269                 goto fail;
270         }
271
272         db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags,
273                                      open_flags, mode);
274         if (db_tdb->wtdb == NULL) {
275                 DEBUG(3, ("Could not open tdb: %s\n", strerror(errno)));
276                 goto fail;
277         }
278
279         result->fetch_locked = db_tdb_fetch_locked;
280         result->fetch = db_tdb_fetch;
281         result->traverse = db_tdb_traverse;
282         result->traverse_read = db_tdb_traverse_read;
283         result->get_seqnum = db_tdb_get_seqnum;
284         return result;
285
286  fail:
287         if (result != NULL) {
288                 TALLOC_FREE(result);
289         }
290         return NULL;
291 }