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