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