r25026: Move param/param.h out of includes.h
[jelmer/samba4-debian.git] / source / lib / dbwrap / dbwrap_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Database interface wrapper around tdb
5
6    Copyright (C) Volker Lendecke 2005
7    Copyright (C) Andrew Tridgell 2007
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "lib/tdb/include/tdb.h"
25 #include "lib/dbwrap/dbwrap.h"
26 #include "system/filesys.h"
27 #include "db_wrap.h"
28 #include "param/param.h"
29
30 struct db_tdb_ctx {
31         struct tdb_wrap *wtdb;
32 };
33
34 static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag);
35 static NTSTATUS db_tdb_delete(struct db_record *rec);
36
37 static int db_tdb_record_destr(struct db_record* data)
38 {
39         struct db_tdb_ctx *ctx = talloc_get_type(data->private_data, struct db_tdb_ctx);
40
41         if (tdb_chainunlock(ctx->wtdb->tdb, data->key) != 0) {
42                 DEBUG(0, ("tdb_chainunlock failed\n"));
43                 return -1;
44         }
45         return 0;
46 }
47
48 static int db_tdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key, TDB_DATA *data)
49 {
50         struct db_tdb_ctx *ctx = talloc_get_type(db->private_data, struct db_tdb_ctx);
51         TDB_DATA value;
52
53         value = tdb_fetch(ctx->wtdb->tdb, key);
54         if (value.dptr == NULL) {
55                 return -1;
56         }
57
58         data->dsize = value.dsize;
59         data->dptr  = (uint8_t *)talloc_memdup(mem_ctx, value.dptr, value.dsize);
60         free(value.dptr);
61         if (data->dptr == NULL) {
62                 errno = ENOMEM;
63                 return -1;
64         }
65
66         return 0;
67 }
68
69 static struct db_record *db_tdb_fetch_locked(struct db_context *db,
70                                      TALLOC_CTX *mem_ctx, TDB_DATA key)
71 {
72         struct db_tdb_ctx *ctx = talloc_get_type(db->private_data, struct db_tdb_ctx);
73         struct db_record *result;
74         TDB_DATA value;
75
76         result = talloc(mem_ctx, struct db_record);
77         if (result == NULL) {
78                 return NULL;
79         }
80
81         result->key.dsize = key.dsize;
82         result->key.dptr = (uint8_t *)talloc_memdup(result, key.dptr, key.dsize);
83         if (result->key.dptr == NULL) {
84                 talloc_free(result);
85                 return NULL;
86         }
87
88         result->value.dptr = NULL;
89         result->value.dsize = 0;
90         result->private_data = talloc_reference(result, ctx);
91         result->store = db_tdb_store;
92         result->delete_rec = db_tdb_delete;
93
94         if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
95                 talloc_free(result);
96                 return NULL;
97         }
98
99         talloc_set_destructor(result, db_tdb_record_destr);
100
101         value = tdb_fetch(ctx->wtdb->tdb, key);
102
103         if (value.dptr == NULL) {
104                 return result;
105         }
106
107         result->value.dsize = value.dsize;
108         result->value.dptr = (uint8_t *)talloc_memdup(result, value.dptr,
109                                                     value.dsize);
110         free(value.dptr);
111         if (result->value.dptr == NULL) {
112                 talloc_free(result);
113                 return NULL;
114         }
115
116         return result;
117 }
118
119 static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag)
120 {
121         struct db_tdb_ctx *ctx = talloc_get_type(rec->private_data, struct db_tdb_ctx);
122
123         /*
124          * This has a bug: We need to replace rec->value for correct
125          * operation, but right now brlock and locking don't use the value
126          * anymore after it was stored.
127          */
128
129         return (tdb_store(ctx->wtdb->tdb, rec->key, data, flag) == 0) ?
130                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
131 }
132
133 static NTSTATUS db_tdb_delete(struct db_record *rec)
134 {
135         struct db_tdb_ctx *ctx = talloc_get_type(rec->private_data, struct db_tdb_ctx);
136
137         return (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) ?
138                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
139 }
140
141 struct db_tdb_traverse_ctx {
142         struct db_context *db;
143         int (*f)(struct db_record *rec, void *private_data);
144         void *private_data;
145 };
146
147 static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
148                                 void *private_data)
149 {
150         struct db_tdb_traverse_ctx *ctx =
151                 (struct db_tdb_traverse_ctx *)private_data;
152         struct db_record rec;
153
154         rec.key = kbuf;
155         rec.value = dbuf;
156         rec.store = db_tdb_store;
157         rec.delete_rec = db_tdb_delete;
158         rec.private_data = ctx->db->private_data;
159
160         return ctx->f(&rec, ctx->private_data);
161 }
162
163 static int db_tdb_traverse(struct db_context *db,
164                            int (*f)(struct db_record *rec, void *private_data),
165                            void *private_data)
166 {
167         struct db_tdb_ctx *db_ctx =
168                 talloc_get_type(db->private_data, struct db_tdb_ctx);
169         struct db_tdb_traverse_ctx ctx;
170
171         ctx.db = db;
172         ctx.f = f;
173         ctx.private_data = private_data;
174         return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx);
175 }
176
177 static NTSTATUS db_tdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
178 {
179         return NT_STATUS_MEDIA_WRITE_PROTECTED;
180 }
181
182 static NTSTATUS db_tdb_delete_deny(struct db_record *rec)
183 {
184         return NT_STATUS_MEDIA_WRITE_PROTECTED;
185 }
186
187 static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
188                                 void *private_data)
189 {
190         struct db_tdb_traverse_ctx *ctx =
191                 (struct db_tdb_traverse_ctx *)private_data;
192         struct db_record rec;
193
194         rec.key = kbuf;
195         rec.value = dbuf;
196         rec.store = db_tdb_store_deny;
197         rec.delete_rec = db_tdb_delete_deny;
198         rec.private_data = ctx->db->private_data;
199
200         return ctx->f(&rec, ctx->private_data);
201 }
202
203 static int db_tdb_traverse_read(struct db_context *db,
204                            int (*f)(struct db_record *rec, void *private_data),
205                            void *private_data)
206 {
207         struct db_tdb_ctx *db_ctx =
208                 talloc_get_type(db->private_data, struct db_tdb_ctx);
209         struct db_tdb_traverse_ctx ctx;
210
211         ctx.db = db;
212         ctx.f = f;
213         ctx.private_data = private_data;
214         return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx);
215 }
216
217 static int db_tdb_get_seqnum(struct db_context *db)
218
219 {
220         struct db_tdb_ctx *db_ctx =
221                 talloc_get_type(db->private_data, struct db_tdb_ctx);
222         return tdb_get_seqnum(db_ctx->wtdb->tdb);
223 }
224
225 /*
226   open a temporary database
227  */
228 struct db_context *db_tmp_open_tdb(TALLOC_CTX *mem_ctx, const char *name, int tdb_flags)
229 {
230         struct db_context *result;
231         struct db_tdb_ctx *db_tdb;
232         char *path;
233
234         result = talloc_zero(mem_ctx, struct db_context);
235         if (result == NULL) goto failed;
236
237         db_tdb = talloc(result, struct db_tdb_ctx);
238         if (db_tdb == NULL) goto failed;
239
240         result->private_data = db_tdb;
241
242         /* the name passed in should not be a full path, it should be
243            just be the db name */
244         path = smbd_tmp_path(result, name);
245
246         db_tdb->wtdb = tdb_wrap_open(db_tdb, path, 0, tdb_flags,
247                                      O_CREAT|O_RDWR, 0666);
248         if (db_tdb->wtdb == NULL) {
249                 DEBUG(3, ("Could not open tdb '%s': %s\n", path, strerror(errno)));
250                 goto failed;
251         }
252         
253         talloc_free(path);
254
255         result->fetch_locked  = db_tdb_fetch_locked;
256         result->fetch         = db_tdb_fetch;
257         result->traverse      = db_tdb_traverse;
258         result->traverse_read = db_tdb_traverse_read;
259         result->get_seqnum    = db_tdb_get_seqnum;
260
261         return result;
262
263  failed:
264         talloc_free(result);
265         return NULL;
266 }