s3-includes: only include system/filesys.h when needed.
[kai/samba.git] / source3 / lib / tdb_validate.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * A general tdb content validation mechanism
5  *
6  * Copyright (C) Michael Adam      2007
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "tdb_validate.h"
25
26 /*
27  * internal validation function, executed by the child.
28  */
29 static int tdb_validate_child(struct tdb_context *tdb,
30                               tdb_validate_data_func validate_fn)
31 {
32         int ret = 1;
33         int num_entries = 0;
34         struct tdb_validation_status v_status;
35
36         v_status.tdb_error = False;
37         v_status.bad_freelist = False;
38         v_status.bad_entry = False;
39         v_status.unknown_key = False;
40         v_status.success = True;
41
42         if (!tdb) {
43                 v_status.tdb_error = True;
44                 v_status.success = False;
45                 goto out;
46         }
47
48         /*
49          * we can simplify this by passing a check function,
50          * but I don't want to change all the callers...
51          */
52         ret = tdb_check(tdb, NULL, NULL);
53         if (ret == -1) {
54                 v_status.tdb_error = True;
55                 v_status.success = False;
56                 goto out;
57         }
58
59         /* Check if the tdb's freelist is good. */
60         if (tdb_validate_freelist(tdb, &num_entries) == -1) {
61                 v_status.bad_freelist = True;
62                 v_status.success = False;
63                 goto out;
64         }
65
66         DEBUG(10,("tdb_validate_child: tdb %s freelist has %d entries\n",
67                   tdb_name(tdb), num_entries));
68
69         /* Now traverse the tdb to validate it. */
70         num_entries = tdb_traverse(tdb, validate_fn, (void *)&v_status);
71         if (!v_status.success) {
72                 goto out;
73         } else if (num_entries == -1) {
74                 v_status.tdb_error = True;
75                 v_status.success = False;
76                 goto out;
77         }
78
79         DEBUG(10,("tdb_validate_child: tdb %s is good with %d entries\n",
80                   tdb_name(tdb), num_entries));
81         ret = 0; /* Cache is good. */
82
83 out:
84         DEBUG(10,   ("tdb_validate_child: summary of validation status:\n"));
85         DEBUGADD(10,(" * tdb error: %s\n", v_status.tdb_error ? "yes" : "no"));
86         DEBUGADD(10,(" * bad freelist: %s\n",v_status.bad_freelist?"yes":"no"));
87         DEBUGADD(10,(" * bad entry: %s\n", v_status.bad_entry ? "yes" : "no"));
88         DEBUGADD(10,(" * unknown key: %s\n", v_status.unknown_key?"yes":"no"));
89         DEBUGADD(10,(" => overall success: %s\n", v_status.success?"yes":"no"));
90
91         return ret;
92 }
93
94 /*
95  * tdb validation function.
96  * returns 0 if tdb is ok, != 0 if it isn't.
97  * this function expects an opened tdb.
98  */
99 int tdb_validate(struct tdb_context *tdb, tdb_validate_data_func validate_fn)
100 {
101         pid_t child_pid = -1;
102         int child_status = 0;
103         int wait_pid = 0;
104         int ret = 1;
105
106         if (tdb == NULL) {
107                 DEBUG(1, ("Error: tdb_validate called with tdb == NULL\n"));
108                 return ret;
109         }
110
111         DEBUG(5, ("tdb_validate called for tdb '%s'\n", tdb_name(tdb)));
112
113         /* fork and let the child do the validation.
114          * benefit: no need to twist signal handlers and panic functions.
115          * just let the child panic. we catch the signal. */
116
117         DEBUG(10, ("tdb_validate: forking to let child do validation.\n"));
118         child_pid = sys_fork();
119         if (child_pid == 0) {
120                 /* child code */
121                 DEBUG(10, ("tdb_validate (validation child): created\n"));
122                 DEBUG(10, ("tdb_validate (validation child): "
123                            "calling tdb_validate_child\n"));
124                 exit(tdb_validate_child(tdb, validate_fn));
125         }
126         else if (child_pid < 0) {
127                 DEBUG(1, ("tdb_validate: fork for validation failed.\n"));
128                 goto done;
129         }
130
131         /* parent */
132
133         DEBUG(10, ("tdb_validate: fork succeeded, child PID = %u\n",
134                 (unsigned int)child_pid));
135
136         DEBUG(10, ("tdb_validate: waiting for child to finish...\n"));
137         while  ((wait_pid = sys_waitpid(child_pid, &child_status, 0)) < 0) {
138                 if (errno == EINTR) {
139                         DEBUG(10, ("tdb_validate: got signal during waitpid, "
140                                    "retrying\n"));
141                         errno = 0;
142                         continue;
143                 }
144                 DEBUG(1, ("tdb_validate: waitpid failed with error '%s'.\n",
145                           strerror(errno)));
146                 goto done;
147         }
148         if (wait_pid != child_pid) {
149                 DEBUG(1, ("tdb_validate: waitpid returned pid %d, "
150                           "but %u was expected\n", wait_pid, (unsigned int)child_pid));
151                 goto done;
152         }
153
154         DEBUG(10, ("tdb_validate: validating child returned.\n"));
155         if (WIFEXITED(child_status)) {
156                 DEBUG(10, ("tdb_validate: child exited, code %d.\n",
157                            WEXITSTATUS(child_status)));
158                 ret = WEXITSTATUS(child_status);
159         }
160         if (WIFSIGNALED(child_status)) {
161                 DEBUG(10, ("tdb_validate: child terminated by signal %d\n",
162                            WTERMSIG(child_status)));
163 #ifdef WCOREDUMP
164                 if (WCOREDUMP(child_status)) {
165                         DEBUGADD(10, ("core dumped\n"));
166                 }
167 #endif
168                 ret = WTERMSIG(child_status);
169         }
170         if (WIFSTOPPED(child_status)) {
171                 DEBUG(10, ("tdb_validate: child was stopped by signal %d\n",
172                            WSTOPSIG(child_status)));
173                 ret = WSTOPSIG(child_status);
174         }
175
176 done:
177         DEBUG(5, ("tdb_validate returning code '%d' for tdb '%s'\n", ret,
178                   tdb_name(tdb)));
179
180         return ret;
181 }
182
183 /*
184  * tdb validation function.
185  * returns 0 if tdb is ok, != 0 if it isn't.
186  * this is a wrapper around the actual validation function that opens and closes
187  * the tdb.
188  */
189 int tdb_validate_open(const char *tdb_path, tdb_validate_data_func validate_fn)
190 {
191         TDB_CONTEXT *tdb = NULL;
192         int ret = 1;
193
194         DEBUG(5, ("tdb_validate_open called for tdb '%s'\n", tdb_path));
195
196         tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDWR, 0);
197         if (!tdb) {
198                 DEBUG(1, ("Error opening tdb %s\n", tdb_path));
199                 return ret;
200         }
201
202         ret = tdb_validate(tdb, validate_fn);
203         tdb_close(tdb);
204         return ret;
205 }
206
207 /*
208  * tdb backup function and helpers for tdb_validate wrapper with backup
209  * handling.
210  */
211
212 /* this structure eliminates the need for a global overall status for
213  * the traverse-copy */
214 struct tdb_copy_data {
215         struct tdb_context *dst;
216         bool success;
217 };
218
219 static int traverse_copy_fn(struct tdb_context *tdb, TDB_DATA key,
220                             TDB_DATA dbuf, void *private_data)
221 {
222         struct tdb_copy_data *data = (struct tdb_copy_data *)private_data;
223
224         if (tdb_store(data->dst, key, dbuf, TDB_INSERT) != 0) {
225                 DEBUG(4, ("Failed to insert into %s: %s\n", tdb_name(data->dst),
226                           strerror(errno)));
227                 data->success = False;
228                 return 1;
229         }
230         return 0;
231 }
232
233 static int tdb_copy(struct tdb_context *src, struct tdb_context *dst)
234 {
235         struct tdb_copy_data data;
236         int count;
237
238         data.dst = dst;
239         data.success = True;
240
241         count = tdb_traverse(src, traverse_copy_fn, (void *)(&data));
242         if ((count < 0) || (data.success == False)) {
243                 return -1;
244         }
245         return count;
246 }
247
248 static int tdb_verify_basic(struct tdb_context *tdb)
249 {
250         return tdb_traverse(tdb, NULL, NULL);
251 }
252
253 /* this backup function is essentially taken from lib/tdb/tools/tdbbackup.tdb
254  */
255 static int tdb_backup(TALLOC_CTX *ctx, const char *src_path,
256                       const char *dst_path, int hash_size)
257 {
258         struct tdb_context *src_tdb = NULL;
259         struct tdb_context *dst_tdb = NULL;
260         char *tmp_path = NULL;
261         struct stat st;
262         int count1, count2;
263         int saved_errno = 0;
264         int ret = -1;
265
266         if (stat(src_path, &st) != 0) {
267                 DEBUG(3, ("Could not stat '%s': %s\n", src_path,
268                           strerror(errno)));
269                 goto done;
270         }
271
272         /* open old tdb RDWR - so we can lock it */
273         src_tdb = tdb_open_log(src_path, 0, TDB_DEFAULT, O_RDWR, 0);
274         if (src_tdb == NULL) {
275                 DEBUG(3, ("Failed to open tdb '%s'\n", src_path));
276                 goto done;
277         }
278
279         if (tdb_lockall(src_tdb) != 0) {
280                 DEBUG(3, ("Failed to lock tdb '%s'\n", src_path));
281                 goto done;
282         }
283
284         tmp_path = talloc_asprintf(ctx, "%s%s", dst_path, ".tmp");
285         if (!tmp_path) {
286                 DEBUG(3, ("talloc fail\n"));
287                 goto done;
288         }
289
290         unlink(tmp_path);
291         dst_tdb = tdb_open_log(tmp_path,
292                                hash_size ? hash_size : tdb_hash_size(src_tdb),
293                                TDB_DEFAULT, O_RDWR | O_CREAT | O_EXCL,
294                                st.st_mode & 0777);
295         if (dst_tdb == NULL) {
296                 DEBUG(3, ("Error creating tdb '%s': %s\n", tmp_path,
297                           strerror(errno)));
298                 saved_errno = errno;
299                 unlink(tmp_path);
300                 goto done;
301         }
302
303         count1 = tdb_copy(src_tdb, dst_tdb);
304         if (count1 < 0) {
305                 DEBUG(3, ("Failed to copy tdb '%s': %s\n", src_path,
306                           strerror(errno)));
307                 tdb_close(dst_tdb);
308                 goto done;
309         }
310
311         /* reopen ro and do basic verification */
312         tdb_close(dst_tdb);
313         dst_tdb = tdb_open_log(tmp_path, 0, TDB_DEFAULT, O_RDONLY, 0);
314         if (!dst_tdb) {
315                 DEBUG(3, ("Failed to reopen tdb '%s': %s\n", tmp_path,
316                           strerror(errno)));
317                 goto done;
318         }
319         count2 = tdb_verify_basic(dst_tdb);
320         if (count2 != count1) {
321                 DEBUG(3, ("Failed to verify result of copying tdb '%s'.\n",
322                           src_path));
323                 tdb_close(dst_tdb);
324                 goto done;
325         }
326
327         DEBUG(10, ("tdb_backup: successfully copied %d entries\n", count1));
328
329         /* make sure the new tdb has reached stable storage
330          * then rename it to its destination */
331         fsync(tdb_fd(dst_tdb));
332         tdb_close(dst_tdb);
333         unlink(dst_path);
334         if (rename(tmp_path, dst_path) != 0) {
335                 DEBUG(3, ("Failed to rename '%s' to '%s': %s\n",
336                           tmp_path, dst_path, strerror(errno)));
337                 goto done;
338         }
339
340         /* success */
341         ret = 0;
342
343 done:
344         if (src_tdb != NULL) {
345                 tdb_close(src_tdb);
346         }
347         if (tmp_path != NULL) {
348                 unlink(tmp_path);
349                 TALLOC_FREE(tmp_path);
350         }
351         if (saved_errno != 0) {
352                 errno = saved_errno;
353         }
354         return ret;
355 }
356
357 static int rename_file_with_suffix(TALLOC_CTX *ctx, const char *path,
358                                    const char *suffix)
359 {
360         int ret = -1;
361         char *dst_path;
362
363         dst_path = talloc_asprintf(ctx, "%s%s", path, suffix);
364         if (dst_path == NULL) {
365                 DEBUG(3, ("error out of memory\n"));
366                 return ret;
367         }
368
369         ret = (rename(path, dst_path) != 0);
370
371         if (ret == 0) {
372                 DEBUG(5, ("moved '%s' to '%s'\n", path, dst_path));
373         } else if (errno == ENOENT) {
374                 DEBUG(3, ("file '%s' does not exist - so not moved\n", path));
375                 ret = 0;
376         } else {
377                 DEBUG(3, ("error renaming %s to %s: %s\n", path, dst_path,
378                           strerror(errno)));
379         }
380
381         TALLOC_FREE(dst_path);
382         return ret;
383 }
384
385 /*
386  * do a backup of a tdb, moving the destination out of the way first
387  */
388 static int tdb_backup_with_rotate(TALLOC_CTX *ctx, const char *src_path,
389                                   const char *dst_path, int hash_size,
390                                   const char *rotate_suffix,
391                                   bool retry_norotate_if_nospc,
392                                   bool rename_as_last_resort_if_nospc)
393 {
394         int ret;
395
396         rename_file_with_suffix(ctx, dst_path, rotate_suffix);
397
398         ret = tdb_backup(ctx, src_path, dst_path, hash_size);
399
400         if (ret != 0) {
401                 DEBUG(10, ("backup of %s failed: %s\n", src_path, strerror(errno)));
402         }
403         if ((ret != 0) && (errno == ENOSPC) && retry_norotate_if_nospc)
404         {
405                 char *rotate_path = talloc_asprintf(ctx, "%s%s", dst_path,
406                                                     rotate_suffix);
407                 if (rotate_path == NULL) {
408                         DEBUG(10, ("talloc fail\n"));
409                         return -1;
410                 }
411                 DEBUG(10, ("backup of %s failed due to lack of space\n",
412                            src_path));
413                 DEBUGADD(10, ("trying to free some space by removing rotated "
414                               "dst %s\n", rotate_path));
415                 if (unlink(rotate_path) == -1) {
416                         DEBUG(10, ("unlink of %s failed: %s\n", rotate_path,
417                                    strerror(errno)));
418                 } else {
419                         ret = tdb_backup(ctx, src_path, dst_path, hash_size);
420                 }
421                 TALLOC_FREE(rotate_path);
422         }
423
424         if ((ret != 0) && (errno == ENOSPC) && rename_as_last_resort_if_nospc)
425         {
426                 DEBUG(10, ("backup of %s failed due to lack of space\n", 
427                            src_path));
428                 DEBUGADD(10, ("using 'rename' as a last resort\n"));
429                 ret = rename(src_path, dst_path);
430         }
431
432         return ret;
433 }
434
435 /*
436  * validation function with backup handling:
437  *
438  *  - calls tdb_validate
439  *  - if the tdb is ok, create a backup "name.bak", possibly moving
440  *    existing backup to name.bak.old,
441  *    return 0 (success) even if the backup fails
442  *  - if the tdb is corrupt:
443  *    - move the tdb to "name.corrupt"
444  *    - check if there is valid backup.
445  *      if so, restore the backup.
446  *      if restore is successful, return 0 (success),
447  *    - otherwise return -1 (failure)
448  */
449 int tdb_validate_and_backup(const char *tdb_path,
450                             tdb_validate_data_func validate_fn)
451 {
452         int ret = -1;
453         const char *backup_suffix = ".bak";
454         const char *corrupt_suffix = ".corrupt";
455         const char *rotate_suffix = ".old";
456         char *tdb_path_backup;
457         struct stat st;
458         TALLOC_CTX *ctx = NULL;
459
460         ctx = talloc_new(NULL);
461         if (ctx == NULL) {
462                 DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
463                 goto done;
464         }
465
466         tdb_path_backup = talloc_asprintf(ctx, "%s%s", tdb_path, backup_suffix);
467         if (!tdb_path_backup) {
468                 DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
469                 goto done;
470         }
471
472         ret = tdb_validate_open(tdb_path, validate_fn);
473
474         if (ret == 0) {
475                 DEBUG(1, ("tdb '%s' is valid\n", tdb_path));
476                 ret = tdb_backup_with_rotate(ctx, tdb_path, tdb_path_backup, 0,
477                                              rotate_suffix, True, False);
478                 if (ret != 0) {
479                         DEBUG(1, ("Error creating backup of tdb '%s'\n",
480                                   tdb_path));
481                         /* the actual validation was successful: */
482                         ret = 0;
483                 } else {
484                         DEBUG(1, ("Created backup '%s' of tdb '%s'\n",
485                                   tdb_path_backup, tdb_path));
486                 }
487         } else {
488                 DEBUG(1, ("tdb '%s' is invalid\n", tdb_path));
489
490                 ret =stat(tdb_path_backup, &st);
491                 if (ret != 0) {
492                         DEBUG(5, ("Could not stat '%s': %s\n", tdb_path_backup,
493                                   strerror(errno)));
494                         DEBUG(1, ("No backup found.\n"));
495                 } else {
496                         DEBUG(1, ("backup '%s' found.\n", tdb_path_backup));
497                         ret = tdb_validate_open(tdb_path_backup, validate_fn);
498                         if (ret != 0) {
499                                 DEBUG(1, ("Backup '%s' is invalid.\n",
500                                           tdb_path_backup));
501                         }
502                 }
503
504                 if (ret != 0) {
505                         int renamed = rename_file_with_suffix(ctx, tdb_path,
506                                                               corrupt_suffix);
507                         if (renamed != 0) {
508                                 DEBUG(1, ("Error moving tdb to '%s%s'\n",
509                                           tdb_path, corrupt_suffix));
510                         } else {
511                                 DEBUG(1, ("Corrupt tdb stored as '%s%s'\n",
512                                           tdb_path, corrupt_suffix));
513                         }
514                         goto done;
515                 }
516
517                 DEBUG(1, ("valid backup '%s' found\n", tdb_path_backup));
518                 ret = tdb_backup_with_rotate(ctx, tdb_path_backup, tdb_path, 0,
519                                              corrupt_suffix, True, True);
520                 if (ret != 0) {
521                         DEBUG(1, ("Error restoring backup from '%s'\n",
522                                   tdb_path_backup));
523                 } else {
524                         DEBUG(1, ("Restored tdb backup from '%s'\n",
525                                   tdb_path_backup));
526                 }
527         }
528
529 done:
530         TALLOC_FREE(ctx);
531         return ret;
532 }