8e513b5ad2bc193172eb23a6e19784d7b603c87c
[samba.git] / source3 / lib / dbwrap / dbwrap_file.c
1 /*
2    Unix SMB/CIFS implementation.
3    Database interface using a file per record
4    Copyright (C) Volker Lendecke 2005
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_file.h"
23 #include "dbwrap/dbwrap_private.h"
24 #include "lib/util/tdb_wrap.h"
25
26 struct db_file_ctx {
27         const char *dirname;
28
29         /* We only support one locked record at a time -- everything else
30          * would lead to a potential deadlock anyway! */
31         struct db_record *locked_record;
32 };
33
34 struct db_locked_file {
35         int fd;
36         uint8 hash;
37         const char *name;
38         const char *path;
39         struct db_file_ctx *parent;
40 };
41
42 /* Copy from statcache.c... */
43
44 static uint32 fsh(const uint8 *p, int len)
45 {
46         uint32 n = 0;
47         int i;
48         for (i=0; i<len; i++) {
49                 n = ((n << 5) + n) ^ (uint32)(p[i]);
50         }
51         return n;
52 }
53
54 static int db_locked_file_destr(struct db_locked_file *data)
55 {
56         if (data->parent != NULL) {
57                 data->parent->locked_record = NULL;
58         }
59
60         if (close(data->fd) != 0) {
61                 DEBUG(3, ("close failed: %s\n", strerror(errno)));
62                 return -1;
63         }
64
65         return 0;
66 }
67
68 static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag);
69 static NTSTATUS db_file_delete(struct db_record *rec);
70
71 static struct db_record *db_file_fetch_locked(struct db_context *db,
72                                               TALLOC_CTX *mem_ctx,
73                                               TDB_DATA key)
74 {
75         struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data,
76                                                         struct db_file_ctx);
77         struct db_record *result;
78         struct db_locked_file *file;
79         struct flock fl;
80         SMB_STRUCT_STAT statbuf;
81         int ret;
82
83         SMB_ASSERT(ctx->locked_record == NULL);
84
85  again:
86         if (!(result = talloc(mem_ctx, struct db_record))) {
87                 DEBUG(0, ("talloc failed\n"));
88                 return NULL;
89         }
90
91         if (!(file = talloc(result, struct db_locked_file))) {
92                 DEBUG(0, ("talloc failed\n"));
93                 TALLOC_FREE(result);
94                 return NULL;
95         }
96
97         result->private_data = file;
98         result->store = db_file_store;
99         result->delete_rec = db_file_delete;
100
101         result->key.dsize = key.dsize;
102         result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
103         if (result->key.dptr == NULL) {
104                 DEBUG(0, ("talloc failed\n"));
105                 TALLOC_FREE(result);
106                 return NULL;
107         }
108
109         /* Cut to 8 bits */
110         file->hash = fsh(key.dptr, key.dsize);
111         file->name = hex_encode_talloc(file, (unsigned char *)key.dptr, key.dsize);
112         if (file->name == NULL) {
113                 DEBUG(0, ("hex_encode failed\n"));
114                 TALLOC_FREE(result);
115                 return NULL;
116         }
117
118         file->path = talloc_asprintf(file, "%s/%2.2X/%s", ctx->dirname,
119                                      file->hash, file->name);
120         if (file->path == NULL) {
121                 DEBUG(0, ("talloc_asprintf failed\n"));
122                 TALLOC_FREE(result);
123                 return NULL;
124         }
125
126         become_root();
127         file->fd = open(file->path, O_RDWR|O_CREAT, 0644);
128         unbecome_root();
129
130         if (file->fd < 0) {
131                 DEBUG(3, ("Could not open/create %s: %s\n",
132                           file->path, strerror(errno)));
133                 TALLOC_FREE(result);
134                 return NULL;
135         }
136
137         talloc_set_destructor(file, db_locked_file_destr);
138
139         fl.l_type = F_WRLCK;
140         fl.l_whence = SEEK_SET;
141         fl.l_start = 0;
142         fl.l_len = 1;
143         fl.l_pid = 0;
144
145         do {
146                 ret = fcntl(file->fd, F_SETLKW, &fl);
147         } while ((ret == -1) && (errno == EINTR));
148
149         if (ret == -1) {
150                 DEBUG(3, ("Could not get lock on %s: %s\n",
151                           file->path, strerror(errno)));
152                 TALLOC_FREE(result);
153                 return NULL;
154         }
155
156         if (sys_fstat(file->fd, &statbuf, false) != 0) {
157                 DEBUG(3, ("Could not fstat %s: %s\n",
158                           file->path, strerror(errno)));
159                 TALLOC_FREE(result);
160                 return NULL;
161         }
162
163         if (statbuf.st_ex_nlink == 0) {
164                 /* Someone has deleted it under the lock, retry */
165                 TALLOC_FREE(result);
166                 goto again;
167         }
168
169         result->value.dsize = 0;
170         result->value.dptr = NULL;
171
172         if (statbuf.st_ex_size != 0) {
173                 NTSTATUS status;
174
175                 result->value.dsize = statbuf.st_ex_size;
176                 result->value.dptr = talloc_array(result, uint8,
177                                                   statbuf.st_ex_size);
178                 if (result->value.dptr == NULL) {
179                         DEBUG(1, ("talloc failed\n"));
180                         TALLOC_FREE(result);
181                         return NULL;
182                 }
183
184                 status = read_data(file->fd, (char *)result->value.dptr,
185                                   result->value.dsize);
186                 if (!NT_STATUS_IS_OK(status)) {
187                         DEBUG(3, ("read_data failed: %s\n",
188                                   nt_errstr(status)));
189                         TALLOC_FREE(result);
190                         return NULL;
191                 }
192         }
193
194         ctx->locked_record = result;
195         file->parent = (struct db_file_ctx *)talloc_reference(file, ctx);
196
197         return result;
198 }
199
200 static NTSTATUS db_file_store_root(int fd, TDB_DATA data)
201 {
202         if (sys_lseek(fd, 0, SEEK_SET) != 0) {
203                 DEBUG(0, ("sys_lseek failed: %s\n", strerror(errno)));
204                 return map_nt_error_from_unix(errno);
205         }
206
207         if (write_data(fd, (char *)data.dptr, data.dsize) != data.dsize) {
208                 DEBUG(3, ("write_data failed: %s\n", strerror(errno)));
209                 return map_nt_error_from_unix(errno);
210         }
211
212         if (ftruncate(fd, data.dsize) != 0) {
213                 DEBUG(3, ("ftruncate failed: %s\n", strerror(errno)));
214                 return map_nt_error_from_unix(errno);
215         }
216
217         return NT_STATUS_OK;
218 }
219
220 static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag)
221 {
222         struct db_locked_file *file =
223                 talloc_get_type_abort(rec->private_data,
224                                       struct db_locked_file);
225         NTSTATUS status;
226
227         become_root();
228         status = db_file_store_root(file->fd, data);
229         unbecome_root();
230
231         return status;
232 }
233
234 static NTSTATUS db_file_delete(struct db_record *rec)
235 {
236         struct db_locked_file *file =
237                 talloc_get_type_abort(rec->private_data,
238                                       struct db_locked_file);
239         int res;
240
241         become_root();
242         res = unlink(file->path);
243         unbecome_root();
244
245         if (res == -1) {
246                 DEBUG(3, ("unlink(%s) failed: %s\n", file->path,
247                           strerror(errno)));
248                 return map_nt_error_from_unix(errno);
249         }
250
251         return NT_STATUS_OK;
252 }
253
254 static int db_file_traverse(struct db_context *db,
255                             int (*fn)(struct db_record *rec,
256                                       void *private_data),
257                             void *private_data)
258 {
259         struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data,
260                                                         struct db_file_ctx);
261         TALLOC_CTX *mem_ctx = talloc_init("traversal %s\n", ctx->dirname);
262
263         int i;
264         int count = 0;
265
266         for (i=0; i<256; i++) {
267                 const char *dirname = talloc_asprintf(mem_ctx, "%s/%2.2X",
268                                                       ctx->dirname, i);
269                 DIR *dir;
270                 struct dirent *dirent;
271
272                 if (dirname == NULL) {
273                         DEBUG(0, ("talloc failed\n"));
274                         TALLOC_FREE(mem_ctx);
275                         return -1;
276                 }
277
278                 dir = opendir(dirname);
279                 if (dir == NULL) {
280                         DEBUG(3, ("Could not open dir %s: %s\n", dirname,
281                                   strerror(errno)));
282                         TALLOC_FREE(mem_ctx);
283                         return -1;
284                 }
285
286                 while ((dirent = readdir(dir)) != NULL) {
287                         DATA_BLOB keyblob;
288                         TDB_DATA key;
289                         struct db_record *rec;
290
291                         if ((dirent->d_name[0] == '.') &&
292                             ((dirent->d_name[1] == '\0') ||
293                              ((dirent->d_name[1] == '.') &&
294                               (dirent->d_name[2] == '\0')))) {
295                                 continue;
296                         }
297
298                         keyblob = strhex_to_data_blob(mem_ctx, dirent->d_name);
299                         if (keyblob.data == NULL) {
300                                 DEBUG(5, ("strhex_to_data_blob failed\n"));
301                                 continue;
302                         }
303
304                         key.dptr = keyblob.data;
305                         key.dsize = keyblob.length;
306
307                         if ((ctx->locked_record != NULL) &&
308                             (key.dsize == ctx->locked_record->key.dsize) &&
309                             (memcmp(key.dptr, ctx->locked_record->key.dptr,
310                                     key.dsize) == 0)) {
311                                 count += 1;
312                                 if (fn(ctx->locked_record,
313                                        private_data) != 0) {
314                                         TALLOC_FREE(mem_ctx);
315                                         closedir(dir);
316                                         return count;
317                                 }
318                         }
319
320                         rec = db_file_fetch_locked(db, mem_ctx, key);
321                         if (rec == NULL) {
322                                 /* Someone might have deleted it */
323                                 continue;
324                         }
325
326                         if (rec->value.dptr == NULL) {
327                                 TALLOC_FREE(rec);
328                                 continue;
329                         }
330
331                         count += 1;
332
333                         if (fn(rec, private_data) != 0) {
334                                 TALLOC_FREE(mem_ctx);
335                                 closedir(dir);
336                                 return count;
337                         }
338                         TALLOC_FREE(rec);
339                 }
340
341                 closedir(dir);
342         }
343
344         TALLOC_FREE(mem_ctx);
345         return count;
346 }
347
348 struct db_context *db_open_file(TALLOC_CTX *mem_ctx,
349                                 struct messaging_context *msg_ctx,
350                                 const char *name,
351                                 int hash_size, int tdb_flags,
352                                 int open_flags, mode_t mode)
353 {
354         struct db_context *result = NULL;
355         struct db_file_ctx *ctx;
356
357         if (!(result = talloc_zero(mem_ctx, struct db_context))) {
358                 DEBUG(0, ("talloc failed\n"));
359                 return NULL;
360         }
361
362         if (!(ctx = talloc(result, struct db_file_ctx))) {
363                 DEBUG(0, ("talloc failed\n"));
364                 TALLOC_FREE(result);
365                 return NULL;
366         }
367
368         result->private_data = ctx;
369         result->fetch_locked = db_file_fetch_locked;
370         result->traverse = db_file_traverse;
371         result->traverse_read = db_file_traverse;
372         result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
373
374         ctx->locked_record = NULL;
375         if (!(ctx->dirname = talloc_strdup(ctx, name))) {
376                 DEBUG(0, ("talloc failed\n"));
377                 TALLOC_FREE(result);
378                 return NULL;
379         }
380
381         if (open_flags & O_CREAT) {
382                 int ret, i;
383
384                 mode |= (mode & S_IRUSR) ? S_IXUSR : 0;
385                 mode |= (mode & S_IRGRP) ? S_IXGRP : 0;
386                 mode |= (mode & S_IROTH) ? S_IXOTH : 0;
387
388                 ret = mkdir(name, mode);
389                 if ((ret != 0) && (errno != EEXIST)) {
390                         DEBUG(5, ("mkdir(%s,%o) failed: %s\n", name, mode,
391                                   strerror(errno)));
392                         TALLOC_FREE(result);
393                         return NULL;
394                 }
395
396                 for (i=0; i<256; i++) {
397                         char *path;
398                         path = talloc_asprintf(result, "%s/%2.2X", name, i);
399                         if (path == NULL) {
400                                 DEBUG(0, ("asprintf failed\n"));
401                                 TALLOC_FREE(result);
402                                 return NULL;
403                         }
404                         ret = mkdir(path, mode);
405                         if ((ret != 0) && (errno != EEXIST)) {
406                                 DEBUG(5, ("mkdir(%s,%o) failed: %s\n", path,
407                                           mode, strerror(errno)));
408                                 TALLOC_FREE(result);
409                                 return NULL;
410                         }
411                         TALLOC_FREE(path);
412                 }
413         }
414
415         return result;
416 }