r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[abartlet/samba.git/.git] / source3 / lib / 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 /*
21  * Be aware that this is just sample code that has not seen too much testing
22  */
23
24 #include "includes.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         ssize_t nread;
82         int ret;
83
84         SMB_ASSERT(ctx->locked_record == NULL);
85
86  again:
87         if (!(result = TALLOC_P(mem_ctx, struct db_record))) {
88                 DEBUG(0, ("talloc failed\n"));
89                 return NULL;
90         }
91
92         if (!(file = TALLOC_P(result, struct db_locked_file))) {
93                 DEBUG(0, ("talloc failed\n"));
94                 TALLOC_FREE(result);
95                 return NULL;
96         }
97
98         result->private_data = file;
99         result->store = db_file_store;
100         result->delete_rec = db_file_delete;
101
102         result->key.dsize = key.dsize;
103         result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, 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(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) != 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_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_size != 0) {
174                 result->value.dsize = statbuf.st_size;
175                 result->value.dptr = TALLOC_ARRAY(result, uint8,
176                                                   statbuf.st_size);
177                 if (result->value.dptr == NULL) {
178                         DEBUG(1, ("talloc failed\n"));
179                         TALLOC_FREE(result);
180                         return NULL;
181                 }
182
183                 nread = read_data(file->fd, (char *)result->value.dptr,
184                                   result->value.dsize);
185                 if (nread != result->value.dsize) {
186                         DEBUG(3, ("read_data failed: %s\n", strerror(errno)));
187                         TALLOC_FREE(result);
188                         return NULL;
189                 }
190         }
191
192         ctx->locked_record = result;
193         file->parent = (struct db_file_ctx *)talloc_reference(file, ctx);
194
195         return result;
196 }
197
198 static NTSTATUS db_file_store_root(int fd, TDB_DATA data)
199 {
200         if (sys_lseek(fd, 0, SEEK_SET) != 0) {
201                 DEBUG(0, ("sys_lseek failed: %s\n", strerror(errno)));
202                 return map_nt_error_from_unix(errno);
203         }
204
205         if (write_data(fd, (char *)data.dptr, data.dsize) != data.dsize) {
206                 DEBUG(3, ("write_data failed: %s\n", strerror(errno)));
207                 return map_nt_error_from_unix(errno);
208         }
209
210         if (sys_ftruncate(fd, data.dsize) != 0) {
211                 DEBUG(3, ("sys_ftruncate failed: %s\n", strerror(errno)));
212                 return map_nt_error_from_unix(errno);
213         }
214
215         return NT_STATUS_OK;
216 }
217
218 static NTSTATUS db_file_store(struct db_record *rec, TDB_DATA data, int flag)
219 {
220         struct db_locked_file *file =
221                 talloc_get_type_abort(rec->private_data,
222                                       struct db_locked_file);
223         NTSTATUS status;
224
225         become_root();
226         status = db_file_store_root(file->fd, data);
227         unbecome_root();
228
229         return status;
230 }
231
232 static NTSTATUS db_file_delete(struct db_record *rec)
233 {
234         struct db_locked_file *file =
235                 talloc_get_type_abort(rec->private_data,
236                                       struct db_locked_file);
237         int res;
238
239         become_root();
240         res = unlink(file->path);
241         unbecome_root();
242
243         if (res == -1) {
244                 DEBUG(3, ("unlink(%s) failed: %s\n", file->path,
245                           strerror(errno)));
246                 return map_nt_error_from_unix(errno);
247         }
248
249         return NT_STATUS_OK;
250 }
251
252 static int db_file_traverse(struct db_context *db,
253                             int (*fn)(struct db_record *rec,
254                                       void *private_data),
255                             void *private_data)
256 {
257         struct db_file_ctx *ctx = talloc_get_type_abort(db->private_data,
258                                                         struct db_file_ctx);
259         TALLOC_CTX *mem_ctx = talloc_init("traversal %s\n", ctx->dirname);
260         
261         int i;
262         int count = 0;
263
264         for (i=0; i<256; i++) {
265                 const char *dirname = talloc_asprintf(mem_ctx, "%s/%2.2X",
266                                                       ctx->dirname, i);
267                 DIR *dir;
268                 struct dirent *dirent;
269
270                 if (dirname == NULL) {
271                         DEBUG(0, ("talloc failed\n"));
272                         TALLOC_FREE(mem_ctx);
273                         return -1;
274                 }
275
276                 dir = opendir(dirname);
277                 if (dir == NULL) {
278                         DEBUG(3, ("Could not open dir %s: %s\n", dirname,
279                                   strerror(errno)));
280                         TALLOC_FREE(mem_ctx);
281                         return -1;
282                 }
283
284                 while ((dirent = readdir(dir)) != NULL) {
285                         DATA_BLOB keyblob;
286                         TDB_DATA key;
287                         struct db_record *rec;
288
289                         if ((dirent->d_name[0] == '.') &&
290                             ((dirent->d_name[1] == '\0') ||
291                              ((dirent->d_name[1] == '.') &&
292                               (dirent->d_name[2] == '\0')))) {
293                                 continue;
294                         }
295
296                         keyblob = strhex_to_data_blob(mem_ctx, dirent->d_name);
297                         if (keyblob.data == NULL) {
298                                 DEBUG(5, ("strhex_to_data_blob failed\n"));
299                                 continue;
300                         }
301
302                         key.dptr = keyblob.data;
303                         key.dsize = keyblob.length;
304
305                         if ((ctx->locked_record != NULL) &&
306                             (key.dsize == ctx->locked_record->key.dsize) &&
307                             (memcmp(key.dptr, ctx->locked_record->key.dptr,
308                                     key.dsize) == 0)) {
309                                 count += 1;
310                                 if (fn(ctx->locked_record,
311                                        private_data) != 0) {
312                                         TALLOC_FREE(mem_ctx);
313                                         closedir(dir);
314                                         return count;
315                                 }
316                         }
317
318                         rec = db_file_fetch_locked(db, mem_ctx, key);
319                         if (rec == NULL) {
320                                 /* Someone might have deleted it */
321                                 continue;
322                         }
323
324                         if (rec->value.dptr == NULL) {
325                                 TALLOC_FREE(rec);
326                                 continue;
327                         }
328
329                         count += 1;
330
331                         if (fn(rec, private_data) != 0) {
332                                 TALLOC_FREE(mem_ctx);
333                                 closedir(dir);
334                                 return count;
335                         }
336                         TALLOC_FREE(rec);
337                 }
338
339                 closedir(dir);
340         }
341
342         TALLOC_FREE(mem_ctx);
343         return count;
344 }
345
346 struct db_context *db_open_file(TALLOC_CTX *mem_ctx,
347                                 struct messaging_context *msg_ctx,
348                                 const char *name,
349                                 int hash_size, int tdb_flags,
350                                 int open_flags, mode_t mode)
351 {
352         struct db_context *result = NULL;
353         struct db_file_ctx *ctx;
354
355         if (!(result = TALLOC_ZERO_P(mem_ctx, struct db_context))) {
356                 DEBUG(0, ("talloc failed\n"));
357                 return NULL;
358         }
359
360         if (!(ctx = TALLOC_P(result, struct db_file_ctx))) {
361                 DEBUG(0, ("talloc failed\n"));
362                 TALLOC_FREE(result);
363                 return NULL;
364         }
365
366         result->private_data = ctx;
367         result->fetch_locked = db_file_fetch_locked;
368         result->traverse = db_file_traverse;
369         result->traverse_read = db_file_traverse;
370
371         ctx->locked_record = NULL;
372         if (!(ctx->dirname = talloc_strdup(ctx, name))) {
373                 DEBUG(0, ("talloc failed\n"));
374                 TALLOC_FREE(result);
375                 return NULL;
376         }
377
378         if (open_flags & O_CREAT) {
379                 int ret, i;
380
381                 mode |= (mode & S_IRUSR) ? S_IXUSR : 0;
382                 mode |= (mode & S_IRGRP) ? S_IXGRP : 0;
383                 mode |= (mode & S_IROTH) ? S_IXOTH : 0;
384
385                 ret = mkdir(name, mode);
386                 if ((ret != 0) && (errno != EEXIST)) {
387                         DEBUG(5, ("mkdir(%s,%o) failed: %s\n", name, mode,
388                                   strerror(errno)));
389                         TALLOC_FREE(result);
390                         return NULL;
391                 }
392
393                 for (i=0; i<256; i++) {
394                         char *path;
395                         path = talloc_asprintf(result, "%s/%2.2X", name, i);
396                         if (path == NULL) {
397                                 DEBUG(0, ("asprintf failed\n"));
398                                 TALLOC_FREE(result);
399                                 return NULL;
400                         }
401                         ret = mkdir(path, mode);
402                         if ((ret != 0) && (errno != EEXIST)) {
403                                 DEBUG(5, ("mkdir(%s,%o) failed: %s\n", path,
404                                           mode, strerror(errno)));
405                                 TALLOC_FREE(result);
406                                 return NULL;
407                         }
408                         TALLOC_FREE(path);
409                 }
410         }
411
412         return result;
413 }