s3:build: move lib/dbwrap/dbwrap_util.o to the other dbwrap objects...
[kai/samba.git] / 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/tdb_wrap/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_t 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_t fsh(const uint8_t *p, int len)
45 {
46         uint32_t n = 0;
47         int i;
48         for (i=0; i<len; i++) {
49                 n = ((n << 5) + n) ^ (uint32_t)(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_t *)talloc_memdup(result, key.dptr,
103                                                     key.dsize);
104         if (result->key.dptr == NULL) {
105                 DEBUG(0, ("talloc failed\n"));
106                 TALLOC_FREE(result);
107                 return NULL;
108         }
109
110         /* Cut to 8 bits */
111         file->hash = fsh(key.dptr, key.dsize);
112         file->name = hex_encode_talloc(file, (unsigned char *)key.dptr, key.dsize);
113         if (file->name == NULL) {
114                 DEBUG(0, ("hex_encode failed\n"));
115                 TALLOC_FREE(result);
116                 return NULL;
117         }
118
119         file->path = talloc_asprintf(file, "%s/%2.2X/%s", ctx->dirname,
120                                      file->hash, file->name);
121         if (file->path == NULL) {
122                 DEBUG(0, ("talloc_asprintf failed\n"));
123                 TALLOC_FREE(result);
124                 return NULL;
125         }
126
127         become_root();
128         file->fd = open(file->path, O_RDWR|O_CREAT, 0644);
129         unbecome_root();
130
131         if (file->fd < 0) {
132                 DEBUG(3, ("Could not open/create %s: %s\n",
133                           file->path, strerror(errno)));
134                 TALLOC_FREE(result);
135                 return NULL;
136         }
137
138         talloc_set_destructor(file, db_locked_file_destr);
139
140         fl.l_type = F_WRLCK;
141         fl.l_whence = SEEK_SET;
142         fl.l_start = 0;
143         fl.l_len = 1;
144         fl.l_pid = 0;
145
146         do {
147                 ret = fcntl(file->fd, F_SETLKW, &fl);
148         } while ((ret == -1) && (errno == EINTR));
149
150         if (ret == -1) {
151                 DEBUG(3, ("Could not get lock on %s: %s\n",
152                           file->path, strerror(errno)));
153                 TALLOC_FREE(result);
154                 return NULL;
155         }
156
157         if (sys_fstat(file->fd, &statbuf, false) != 0) {
158                 DEBUG(3, ("Could not fstat %s: %s\n",
159                           file->path, strerror(errno)));
160                 TALLOC_FREE(result);
161                 return NULL;
162         }
163
164         if (statbuf.st_ex_nlink == 0) {
165                 /* Someone has deleted it under the lock, retry */
166                 TALLOC_FREE(result);
167                 goto again;
168         }
169
170         result->value.dsize = 0;
171         result->value.dptr = NULL;
172
173         if (statbuf.st_ex_size != 0) {
174                 NTSTATUS status;
175
176                 result->value.dsize = statbuf.st_ex_size;
177                 result->value.dptr = talloc_array(result, uint8_t,
178                                                   statbuf.st_ex_size);
179                 if (result->value.dptr == NULL) {
180                         DEBUG(1, ("talloc failed\n"));
181                         TALLOC_FREE(result);
182                         return NULL;
183                 }
184
185                 status = read_data(file->fd, (char *)result->value.dptr,
186                                   result->value.dsize);
187                 if (!NT_STATUS_IS_OK(status)) {
188                         DEBUG(3, ("read_data failed: %s\n",
189                                   nt_errstr(status)));
190                         TALLOC_FREE(result);
191                         return NULL;
192                 }
193         }
194
195         ctx->locked_record = result;
196         file->parent = (struct db_file_ctx *)talloc_reference(file, ctx);
197
198         return result;
199 }
200
201 static NTSTATUS db_file_store_root(int fd, TDB_DATA data)
202 {
203         if (lseek(fd, 0, SEEK_SET) != 0) {
204                 DEBUG(0, ("lseek failed: %s\n", strerror(errno)));
205                 return map_nt_error_from_unix(errno);
206         }
207
208         if (write_data(fd, (char *)data.dptr, data.dsize) != data.dsize) {
209                 DEBUG(3, ("write_data failed: %s\n", strerror(errno)));
210                 return map_nt_error_from_unix(errno);
211         }
212
213         if (ftruncate(fd, data.dsize) != 0) {
214                 DEBUG(3, ("ftruncate failed: %s\n", strerror(errno)));
215                 return map_nt_error_from_unix(errno);
216         }
217
218         return NT_STATUS_OK;
219 }
220
221 static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag)
222 {
223         struct db_locked_file *file =
224                 talloc_get_type_abort(rec->private_data,
225                                       struct db_locked_file);
226         NTSTATUS status;
227
228         become_root();
229         status = db_file_store_root(file->fd, data);
230         unbecome_root();
231
232         return status;
233 }
234
235 static NTSTATUS db_file_delete(struct db_record *rec)
236 {
237         struct db_locked_file *file =
238                 talloc_get_type_abort(rec->private_data,
239                                       struct db_locked_file);
240         int res;
241
242         become_root();
243         res = unlink(file->path);
244         unbecome_root();
245
246         if (res == -1) {
247                 DEBUG(3, ("unlink(%s) failed: %s\n", file->path,
248                           strerror(errno)));
249                 return map_nt_error_from_unix(errno);
250         }
251
252         return NT_STATUS_OK;
253 }
254
255 static int db_file_traverse(struct db_context *db,
256                             int (*fn)(struct db_record *rec,
257                                       void *private_data),
258                             void *private_data)
259 {
260         struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data,
261                                                         struct db_file_ctx);
262         TALLOC_CTX *mem_ctx = talloc_init("traversal %s\n", ctx->dirname);
263
264         int i;
265         int count = 0;
266
267         for (i=0; i<256; i++) {
268                 const char *dirname = talloc_asprintf(mem_ctx, "%s/%2.2X",
269                                                       ctx->dirname, i);
270                 DIR *dir;
271                 struct dirent *dirent;
272
273                 if (dirname == NULL) {
274                         DEBUG(0, ("talloc failed\n"));
275                         TALLOC_FREE(mem_ctx);
276                         return -1;
277                 }
278
279                 dir = opendir(dirname);
280                 if (dir == NULL) {
281                         DEBUG(3, ("Could not open dir %s: %s\n", dirname,
282                                   strerror(errno)));
283                         TALLOC_FREE(mem_ctx);
284                         return -1;
285                 }
286
287                 while ((dirent = readdir(dir)) != NULL) {
288                         DATA_BLOB keyblob;
289                         TDB_DATA key;
290                         struct db_record *rec;
291
292                         if ((dirent->d_name[0] == '.') &&
293                             ((dirent->d_name[1] == '\0') ||
294                              ((dirent->d_name[1] == '.') &&
295                               (dirent->d_name[2] == '\0')))) {
296                                 continue;
297                         }
298
299                         keyblob = strhex_to_data_blob(mem_ctx, dirent->d_name);
300                         if (keyblob.data == NULL) {
301                                 DEBUG(5, ("strhex_to_data_blob failed\n"));
302                                 continue;
303                         }
304
305                         key.dptr = keyblob.data;
306                         key.dsize = keyblob.length;
307
308                         if ((ctx->locked_record != NULL) &&
309                             (key.dsize == ctx->locked_record->key.dsize) &&
310                             (memcmp(key.dptr, ctx->locked_record->key.dptr,
311                                     key.dsize) == 0)) {
312                                 count += 1;
313                                 if (fn(ctx->locked_record,
314                                        private_data) != 0) {
315                                         TALLOC_FREE(mem_ctx);
316                                         closedir(dir);
317                                         return count;
318                                 }
319                         }
320
321                         rec = db_file_fetch_locked(db, mem_ctx, key);
322                         if (rec == NULL) {
323                                 /* Someone might have deleted it */
324                                 continue;
325                         }
326
327                         if (rec->value.dptr == NULL) {
328                                 TALLOC_FREE(rec);
329                                 continue;
330                         }
331
332                         count += 1;
333
334                         if (fn(rec, private_data) != 0) {
335                                 TALLOC_FREE(mem_ctx);
336                                 closedir(dir);
337                                 return count;
338                         }
339                         TALLOC_FREE(rec);
340                 }
341
342                 closedir(dir);
343         }
344
345         TALLOC_FREE(mem_ctx);
346         return count;
347 }
348
349 struct db_context *db_open_file(TALLOC_CTX *mem_ctx,
350                                 struct messaging_context *msg_ctx,
351                                 const char *name,
352                                 int hash_size, int tdb_flags,
353                                 int open_flags, mode_t mode)
354 {
355         struct db_context *result = NULL;
356         struct db_file_ctx *ctx;
357
358         if (!(result = talloc_zero(mem_ctx, struct db_context))) {
359                 DEBUG(0, ("talloc failed\n"));
360                 return NULL;
361         }
362
363         if (!(ctx = talloc(result, struct db_file_ctx))) {
364                 DEBUG(0, ("talloc failed\n"));
365                 TALLOC_FREE(result);
366                 return NULL;
367         }
368
369         result->private_data = ctx;
370         result->fetch_locked = db_file_fetch_locked;
371         result->try_fetch_locked = NULL;
372         result->traverse = db_file_traverse;
373         result->traverse_read = db_file_traverse;
374         result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
375
376         ctx->locked_record = NULL;
377         if (!(ctx->dirname = talloc_strdup(ctx, name))) {
378                 DEBUG(0, ("talloc failed\n"));
379                 TALLOC_FREE(result);
380                 return NULL;
381         }
382
383         if (open_flags & O_CREAT) {
384                 int ret, i;
385
386                 mode |= (mode & S_IRUSR) ? S_IXUSR : 0;
387                 mode |= (mode & S_IRGRP) ? S_IXGRP : 0;
388                 mode |= (mode & S_IROTH) ? S_IXOTH : 0;
389
390                 ret = mkdir(name, mode);
391                 if ((ret != 0) && (errno != EEXIST)) {
392                         DEBUG(5, ("mkdir(%s,%o) failed: %s\n", name, mode,
393                                   strerror(errno)));
394                         TALLOC_FREE(result);
395                         return NULL;
396                 }
397
398                 for (i=0; i<256; i++) {
399                         char *path;
400                         path = talloc_asprintf(result, "%s/%2.2X", name, i);
401                         if (path == NULL) {
402                                 DEBUG(0, ("asprintf failed\n"));
403                                 TALLOC_FREE(result);
404                                 return NULL;
405                         }
406                         ret = mkdir(path, mode);
407                         if ((ret != 0) && (errno != EEXIST)) {
408                                 DEBUG(5, ("mkdir(%s,%o) failed: %s\n", path,
409                                           mode, strerror(errno)));
410                                 TALLOC_FREE(result);
411                                 return NULL;
412                         }
413                         TALLOC_FREE(path);
414                 }
415         }
416
417         return result;
418 }