be691dc7ec9c5113b3f055faf11a589b9d1bd5c4
[samba.git] / source / 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, (unsigned char *)key.dptr,
76                                           key.dsize);
77                 DEBUG(10, (DEBUGLEVEL > 10
78                            ? "Locking key %s\n" : "Locking key %20s\n",
79                            keystr));
80                 TALLOC_FREE(keystr);
81         }
82
83         if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
84                 DEBUG(3, ("tdb_chainlock failed\n"));
85                 TALLOC_FREE(result);
86                 return NULL;
87         }
88
89         talloc_set_destructor(result, db_tdb_record_destr);
90
91         value = tdb_fetch(ctx->wtdb->tdb, key);
92
93         if (value.dptr == NULL) {
94                 return result;
95         }
96
97         result->value.dsize = value.dsize;
98         result->value.dptr = (uint8 *)talloc_memdup(result, value.dptr,
99                                                     value.dsize);
100         if (result->value.dptr == NULL) {
101                 DEBUG(3, ("talloc failed\n"));
102                 TALLOC_FREE(result);
103                 return NULL;
104         }
105
106         SAFE_FREE(value.dptr);
107
108         DEBUG(10, ("Allocated locked data 0x%p\n", result));
109
110         return result;
111 }
112
113 static NTSTATUS db_tdb_store(struct db_record *rec, TDB_DATA data, int flag)
114 {
115         struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
116                                                        struct db_tdb_ctx);
117
118         /*
119          * This has a bug: We need to replace rec->value for correct
120          * operation, but right now brlock and locking don't use the value
121          * anymore after it was stored.
122          */
123
124         return (tdb_store(ctx->wtdb->tdb, rec->key, data, flag) == 0) ?
125                 NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL;
126 }
127
128 static NTSTATUS db_tdb_delete(struct db_record *rec)
129 {
130         struct db_tdb_ctx *ctx = talloc_get_type_abort(rec->private_data,
131                                                        struct db_tdb_ctx);
132         int res;
133         
134         res = tdb_delete(ctx->wtdb->tdb, rec->key);
135
136         if (res == 0) {
137                 return NT_STATUS_OK;
138         }
139
140         return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb));
141 }
142
143 struct db_tdb_traverse_ctx {
144         struct db_context *db;
145         int (*f)(struct db_record *rec, void *private_data);
146         void *private_data;
147 };
148
149 static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
150                                 void *private_data)
151 {
152         struct db_tdb_traverse_ctx *ctx =
153                 (struct db_tdb_traverse_ctx *)private_data;
154         struct db_record rec;
155
156         rec.key = kbuf;
157         rec.value = dbuf;
158         rec.store = db_tdb_store;
159         rec.delete_rec = db_tdb_delete;
160         rec.private_data = ctx->db->private_data;
161
162         return ctx->f(&rec, ctx->private_data);
163 }
164
165 static int db_tdb_traverse(struct db_context *db,
166                            int (*f)(struct db_record *rec, void *private_data),
167                            void *private_data)
168 {
169         struct db_tdb_ctx *db_ctx =
170                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
171         struct db_tdb_traverse_ctx ctx;
172
173         ctx.db = db;
174         ctx.f = f;
175         ctx.private_data = private_data;
176         return tdb_traverse(db_ctx->wtdb->tdb, db_tdb_traverse_func, &ctx);
177 }
178
179 static NTSTATUS db_tdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
180 {
181         return NT_STATUS_MEDIA_WRITE_PROTECTED;
182 }
183
184 static NTSTATUS db_tdb_delete_deny(struct db_record *rec)
185 {
186         return NT_STATUS_MEDIA_WRITE_PROTECTED;
187 }
188
189 static int db_tdb_traverse_read_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
190                                 void *private_data)
191 {
192         struct db_tdb_traverse_ctx *ctx =
193                 (struct db_tdb_traverse_ctx *)private_data;
194         struct db_record rec;
195
196         rec.key = kbuf;
197         rec.value = dbuf;
198         rec.store = db_tdb_store_deny;
199         rec.delete_rec = db_tdb_delete_deny;
200         rec.private_data = ctx->db->private_data;
201
202         return ctx->f(&rec, ctx->private_data);
203 }
204
205 static int db_tdb_traverse_read(struct db_context *db,
206                            int (*f)(struct db_record *rec, void *private_data),
207                            void *private_data)
208 {
209         struct db_tdb_ctx *db_ctx =
210                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
211         struct db_tdb_traverse_ctx ctx;
212
213         ctx.db = db;
214         ctx.f = f;
215         ctx.private_data = private_data;
216         return tdb_traverse_read(db_ctx->wtdb->tdb, db_tdb_traverse_read_func, &ctx);
217 }
218
219 static int db_tdb_get_seqnum(struct db_context *db)
220
221 {
222         struct db_tdb_ctx *db_ctx =
223                 talloc_get_type_abort(db->private_data, struct db_tdb_ctx);
224         return tdb_get_seqnum(db_ctx->wtdb->tdb);
225 }
226
227 struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
228                                const char *name,
229                                int hash_size, int tdb_flags,
230                                int open_flags, mode_t mode)
231 {
232         struct db_context *result = NULL;
233         struct db_tdb_ctx *db_tdb;
234
235         result = TALLOC_ZERO_P(mem_ctx, struct db_context);
236         if (result == NULL) {
237                 DEBUG(0, ("talloc failed\n"));
238                 goto fail;
239         }
240
241         result->private_data = db_tdb = TALLOC_P(result, struct db_tdb_ctx);
242         if (db_tdb == NULL) {
243                 DEBUG(0, ("talloc failed\n"));
244                 goto fail;
245         }
246
247         db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags,
248                                      open_flags, mode);
249         if (db_tdb->wtdb == NULL) {
250                 DEBUG(3, ("Could not open tdb: %s\n", strerror(errno)));
251                 goto fail;
252         }
253
254         result->fetch_locked = db_tdb_fetch_locked;
255         result->traverse = db_tdb_traverse;
256         result->traverse_read = db_tdb_traverse_read;
257         result->get_seqnum = db_tdb_get_seqnum;
258         return result;
259
260  fail:
261         if (result != NULL) {
262                 TALLOC_FREE(result);
263         }
264         return NULL;
265 }