smbd: Make reopen_from_fsp() public
[samba4-gss.git] / source3 / lib / tdb_validate.c
blob5b821a9731b3c44f482cd4274291195461b3c546
1 /*
2 * Unix SMB/CIFS implementation.
4 * A general tdb content validation mechanism
6 * Copyright (C) Michael Adam 2007
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.
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.
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/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "util_tdb.h"
25 #include "tdb_validate.h"
28 * internal validation function, executed by the child.
30 static int tdb_validate_child(struct tdb_context *tdb,
31 tdb_validate_data_func validate_fn)
33 int ret = 1;
34 int check_rc;
35 int num_entries = 0;
36 struct tdb_validation_status v_status;
38 v_status.tdb_error = False;
39 v_status.bad_freelist = False;
40 v_status.bad_entry = False;
41 v_status.unknown_key = False;
42 v_status.success = True;
44 if (!tdb) {
45 v_status.tdb_error = True;
46 v_status.success = False;
47 goto out;
51 * we can simplify this by passing a check function,
52 * but I don't want to change all the callers...
54 check_rc = tdb_check(tdb, NULL, NULL);
55 if (check_rc != 0) {
56 v_status.tdb_error = True;
57 v_status.success = False;
58 goto out;
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;
68 DEBUG(10,("tdb_validate_child: tdb %s freelist has %d entries\n",
69 tdb_name(tdb), num_entries));
71 /* Now traverse the tdb to validate it. */
72 num_entries = tdb_traverse(tdb, validate_fn, (void *)&v_status);
73 if (!v_status.success) {
74 goto out;
75 } else if (num_entries < 0) {
76 v_status.tdb_error = True;
77 v_status.success = False;
78 goto out;
81 DEBUG(10,("tdb_validate_child: tdb %s is good with %d entries\n",
82 tdb_name(tdb), num_entries));
83 ret = 0; /* Cache is good. */
85 out:
86 DBG_DEBUG("summary of validation status:\n"
87 " * tdb error: %s\n"
88 " * bad freelist: %s\n"
89 " * bad entry: %s\n"
90 " * unknown key: %s\n"
91 " => overall success: %s\n",
92 v_status.tdb_error ? "yes" : "no",
93 v_status.bad_freelist ? "yes" : "no",
94 v_status.bad_entry ? "yes" : "no",
95 v_status.unknown_key ? "yes" : "no",
96 v_status.success ? "yes" : "no");
98 return ret;
102 * tdb validation function.
103 * returns 0 if tdb is ok, != 0 if it isn't.
104 * this function expects an opened tdb.
106 int tdb_validate(struct tdb_context *tdb, tdb_validate_data_func validate_fn)
108 pid_t child_pid = -1;
109 int child_status = 0;
110 int wait_pid = 0;
111 int ret = 1;
113 if (tdb == NULL) {
114 DEBUG(1, ("Error: tdb_validate called with tdb == NULL\n"));
115 return ret;
118 DEBUG(5, ("tdb_validate called for tdb '%s'\n", tdb_name(tdb)));
120 /* fork and let the child do the validation.
121 * benefit: no need to twist signal handlers and panic functions.
122 * just let the child panic. we catch the signal. */
124 DEBUG(10, ("tdb_validate: forking to let child do validation.\n"));
125 child_pid = fork();
126 if (child_pid == 0) {
127 /* child code */
128 DEBUG(10, ("tdb_validate (validation child): created\n"));
129 DEBUG(10, ("tdb_validate (validation child): "
130 "calling tdb_validate_child\n"));
131 exit(tdb_validate_child(tdb, validate_fn));
133 else if (child_pid < 0) {
134 DEBUG(1, ("tdb_validate: fork for validation failed.\n"));
135 goto done;
138 /* parent */
140 DEBUG(10, ("tdb_validate: fork succeeded, child PID = %u\n",
141 (unsigned int)child_pid));
143 DEBUG(10, ("tdb_validate: waiting for child to finish...\n"));
144 while ((wait_pid = waitpid(child_pid, &child_status, 0)) < 0) {
145 if (errno == EINTR) {
146 DEBUG(10, ("tdb_validate: got signal during waitpid, "
147 "retrying\n"));
148 errno = 0;
149 continue;
151 DEBUG(1, ("tdb_validate: waitpid failed with error '%s'.\n",
152 strerror(errno)));
153 goto done;
155 if (wait_pid != child_pid) {
156 DEBUG(1, ("tdb_validate: waitpid returned pid %d, "
157 "but %u was expected\n", wait_pid, (unsigned int)child_pid));
158 goto done;
161 DEBUG(10, ("tdb_validate: validating child returned.\n"));
162 if (WIFEXITED(child_status)) {
163 DEBUG(10, ("tdb_validate: child exited, code %d.\n",
164 WEXITSTATUS(child_status)));
165 ret = WEXITSTATUS(child_status);
167 if (WIFSIGNALED(child_status)) {
168 DEBUG(10, ("tdb_validate: child terminated by signal %d\n",
169 WTERMSIG(child_status)));
170 #ifdef WCOREDUMP
171 if (WCOREDUMP(child_status)) {
172 DEBUGADD(10, ("core dumped\n"));
174 #endif
175 ret = WTERMSIG(child_status);
177 if (WIFSTOPPED(child_status)) {
178 DEBUG(10, ("tdb_validate: child was stopped by signal %d\n",
179 WSTOPSIG(child_status)));
180 ret = WSTOPSIG(child_status);
183 done:
184 DEBUG(5, ("tdb_validate returning code '%d' for tdb '%s'\n", ret,
185 tdb_name(tdb)));
187 return ret;
191 * tdb validation function.
192 * returns 0 if tdb is ok, != 0 if it isn't.
193 * this is a wrapper around the actual validation function that opens and closes
194 * the tdb.
196 int tdb_validate_open(const char *tdb_path, tdb_validate_data_func validate_fn)
198 TDB_CONTEXT *tdb = NULL;
199 int ret = 1;
201 DEBUG(5, ("tdb_validate_open called for tdb '%s'\n", tdb_path));
203 tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDWR, 0);
204 if (!tdb) {
205 DEBUG(1, ("Error opening tdb %s\n", tdb_path));
206 return ret;
209 ret = tdb_validate(tdb, validate_fn);
210 tdb_close(tdb);
211 return ret;
215 * tdb backup function and helpers for tdb_validate wrapper with backup
216 * handling.
219 /* this structure eliminates the need for a global overall status for
220 * the traverse-copy */
221 struct tdb_copy_data {
222 struct tdb_context *dst;
223 bool success;
226 static int traverse_copy_fn(struct tdb_context *tdb, TDB_DATA key,
227 TDB_DATA dbuf, void *private_data)
229 struct tdb_copy_data *data = (struct tdb_copy_data *)private_data;
231 if (tdb_store(data->dst, key, dbuf, TDB_INSERT) != 0) {
232 DEBUG(4, ("Failed to insert into %s: %s\n", tdb_name(data->dst),
233 strerror(errno)));
234 data->success = False;
235 return 1;
237 return 0;
240 static int tdb_copy(struct tdb_context *src, struct tdb_context *dst)
242 struct tdb_copy_data data;
243 int count;
245 data.dst = dst;
246 data.success = True;
248 count = tdb_traverse(src, traverse_copy_fn, (void *)(&data));
249 if ((count < 0) || (data.success == False)) {
250 return -1;
252 return count;
255 static int tdb_verify_basic(struct tdb_context *tdb)
257 return tdb_traverse(tdb, NULL, NULL);
260 /* this backup function is essentially taken from lib/tdb/tools/tdbbackup.tdb
262 static int tdb_backup(TALLOC_CTX *ctx, const char *src_path,
263 const char *dst_path, int hash_size)
265 struct tdb_context *src_tdb = NULL;
266 struct tdb_context *dst_tdb = NULL;
267 char *tmp_path = NULL;
268 struct stat st;
269 int count1, count2;
270 int saved_errno = 0;
271 int ret = -1;
273 if (stat(src_path, &st) != 0) {
274 DEBUG(3, ("Could not stat '%s': %s\n", src_path,
275 strerror(errno)));
276 goto done;
279 /* open old tdb RDWR - so we can lock it */
280 src_tdb = tdb_open_log(src_path, 0, TDB_DEFAULT, O_RDWR, 0);
281 if (src_tdb == NULL) {
282 DEBUG(3, ("Failed to open tdb '%s'\n", src_path));
283 goto done;
286 if (tdb_lockall(src_tdb) != 0) {
287 DEBUG(3, ("Failed to lock tdb '%s'\n", src_path));
288 goto done;
291 tmp_path = talloc_asprintf(ctx, "%s%s", dst_path, ".tmp");
292 if (!tmp_path) {
293 DEBUG(3, ("talloc fail\n"));
294 goto done;
297 unlink(tmp_path);
299 if (!hash_size) {
300 hash_size = tdb_hash_size(src_tdb);
303 dst_tdb = tdb_open_log(tmp_path, hash_size,
304 TDB_DEFAULT, O_RDWR | O_CREAT | O_EXCL,
305 st.st_mode & 0777);
306 if (dst_tdb == NULL) {
307 DEBUG(3, ("Error creating tdb '%s': %s\n", tmp_path,
308 strerror(errno)));
309 saved_errno = errno;
310 unlink(tmp_path);
311 goto done;
314 count1 = tdb_copy(src_tdb, dst_tdb);
315 if (count1 < 0) {
316 DEBUG(3, ("Failed to copy tdb '%s': %s\n", src_path,
317 strerror(errno)));
318 tdb_close(dst_tdb);
319 goto done;
322 /* reopen ro and do basic verification */
323 tdb_close(dst_tdb);
324 dst_tdb = tdb_open_log(tmp_path, 0, TDB_DEFAULT, O_RDONLY, 0);
325 if (!dst_tdb) {
326 DEBUG(3, ("Failed to reopen tdb '%s': %s\n", tmp_path,
327 strerror(errno)));
328 goto done;
330 count2 = tdb_verify_basic(dst_tdb);
331 if (count2 != count1) {
332 DEBUG(3, ("Failed to verify result of copying tdb '%s'.\n",
333 src_path));
334 tdb_close(dst_tdb);
335 goto done;
338 DEBUG(10, ("tdb_backup: successfully copied %d entries\n", count1));
340 /* make sure the new tdb has reached stable storage
341 * then rename it to its destination */
342 fsync(tdb_fd(dst_tdb));
343 tdb_close(dst_tdb);
344 unlink(dst_path);
345 if (rename(tmp_path, dst_path) != 0) {
346 DEBUG(3, ("Failed to rename '%s' to '%s': %s\n",
347 tmp_path, dst_path, strerror(errno)));
348 goto done;
351 /* success */
352 ret = 0;
354 done:
355 if (src_tdb != NULL) {
356 tdb_close(src_tdb);
358 if (tmp_path != NULL) {
359 unlink(tmp_path);
360 TALLOC_FREE(tmp_path);
362 if (saved_errno != 0) {
363 errno = saved_errno;
365 return ret;
368 static int rename_file_with_suffix(TALLOC_CTX *ctx, const char *path,
369 const char *suffix)
371 int ret = -1;
372 char *dst_path;
374 dst_path = talloc_asprintf(ctx, "%s%s", path, suffix);
375 if (dst_path == NULL) {
376 DEBUG(3, ("error out of memory\n"));
377 return ret;
380 ret = (rename(path, dst_path) != 0);
382 if (ret == 0) {
383 DEBUG(5, ("moved '%s' to '%s'\n", path, dst_path));
384 } else if (errno == ENOENT) {
385 DEBUG(3, ("file '%s' does not exist - so not moved\n", path));
386 ret = 0;
387 } else {
388 DEBUG(3, ("error renaming %s to %s: %s\n", path, dst_path,
389 strerror(errno)));
392 TALLOC_FREE(dst_path);
393 return ret;
397 * do a backup of a tdb, moving the destination out of the way first
399 static int tdb_backup_with_rotate(TALLOC_CTX *ctx, const char *src_path,
400 const char *dst_path, int hash_size,
401 const char *rotate_suffix,
402 bool retry_norotate_if_nospc,
403 bool rename_as_last_resort_if_nospc)
405 int ret;
407 rename_file_with_suffix(ctx, dst_path, rotate_suffix);
409 ret = tdb_backup(ctx, src_path, dst_path, hash_size);
411 if (ret != 0) {
412 DEBUG(10, ("backup of %s failed: %s\n", src_path, strerror(errno)));
414 if ((ret != 0) && (errno == ENOSPC) && retry_norotate_if_nospc)
416 char *rotate_path = talloc_asprintf(ctx, "%s%s", dst_path,
417 rotate_suffix);
418 if (rotate_path == NULL) {
419 DEBUG(10, ("talloc fail\n"));
420 return -1;
422 DEBUG(10, ("backup of %s failed due to lack of space\n",
423 src_path));
424 DEBUGADD(10, ("trying to free some space by removing rotated "
425 "dst %s\n", rotate_path));
426 if (unlink(rotate_path) == -1) {
427 DEBUG(10, ("unlink of %s failed: %s\n", rotate_path,
428 strerror(errno)));
429 } else {
430 ret = tdb_backup(ctx, src_path, dst_path, hash_size);
432 TALLOC_FREE(rotate_path);
435 if ((ret != 0) && (errno == ENOSPC) && rename_as_last_resort_if_nospc)
437 DEBUG(10, ("backup of %s failed due to lack of space\n",
438 src_path));
439 DEBUGADD(10, ("using 'rename' as a last resort\n"));
440 ret = rename(src_path, dst_path);
443 return ret;
447 * validation function with backup handling:
449 * - calls tdb_validate
450 * - if the tdb is ok, create a backup "name.bak", possibly moving
451 * existing backup to name.bak.old,
452 * return 0 (success) even if the backup fails
453 * - if the tdb is corrupt:
454 * - move the tdb to "name.corrupt"
455 * - check if there is valid backup.
456 * if so, restore the backup.
457 * if restore is successful, return 0 (success),
458 * - otherwise return -1 (failure)
460 int tdb_validate_and_backup(const char *tdb_path,
461 tdb_validate_data_func validate_fn)
463 int ret = -1;
464 const char *backup_suffix = ".bak";
465 const char *corrupt_suffix = ".corrupt";
466 const char *rotate_suffix = ".old";
467 char *tdb_path_backup;
468 struct stat st;
469 TALLOC_CTX *ctx = NULL;
471 ctx = talloc_new(NULL);
472 if (ctx == NULL) {
473 DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
474 goto done;
477 tdb_path_backup = talloc_asprintf(ctx, "%s%s", tdb_path, backup_suffix);
478 if (!tdb_path_backup) {
479 DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
480 goto done;
483 ret = tdb_validate_open(tdb_path, validate_fn);
485 if (ret == 0) {
486 DEBUG(1, ("tdb '%s' is valid\n", tdb_path));
487 ret = tdb_backup_with_rotate(ctx, tdb_path, tdb_path_backup, 0,
488 rotate_suffix, True, False);
489 if (ret != 0) {
490 DEBUG(1, ("Error creating backup of tdb '%s'\n",
491 tdb_path));
492 /* the actual validation was successful: */
493 ret = 0;
494 } else {
495 DEBUG(1, ("Created backup '%s' of tdb '%s'\n",
496 tdb_path_backup, tdb_path));
498 } else {
499 DEBUG(1, ("tdb '%s' is invalid\n", tdb_path));
501 ret =stat(tdb_path_backup, &st);
502 if (ret != 0) {
503 DEBUG(5, ("Could not stat '%s': %s\n", tdb_path_backup,
504 strerror(errno)));
505 DEBUG(1, ("No backup found.\n"));
506 } else {
507 DEBUG(1, ("backup '%s' found.\n", tdb_path_backup));
508 ret = tdb_validate_open(tdb_path_backup, validate_fn);
509 if (ret != 0) {
510 DEBUG(1, ("Backup '%s' is invalid.\n",
511 tdb_path_backup));
515 if (ret != 0) {
516 int renamed = rename_file_with_suffix(ctx, tdb_path,
517 corrupt_suffix);
518 if (renamed != 0) {
519 DEBUG(1, ("Error moving tdb to '%s%s'\n",
520 tdb_path, corrupt_suffix));
521 } else {
522 DEBUG(1, ("Corrupt tdb stored as '%s%s'\n",
523 tdb_path, corrupt_suffix));
525 goto done;
528 DEBUG(1, ("valid backup '%s' found\n", tdb_path_backup));
529 ret = tdb_backup_with_rotate(ctx, tdb_path_backup, tdb_path, 0,
530 corrupt_suffix, True, True);
531 if (ret != 0) {
532 DEBUG(1, ("Error restoring backup from '%s'\n",
533 tdb_path_backup));
534 } else {
535 DEBUG(1, ("Restored tdb backup from '%s'\n",
536 tdb_path_backup));
540 done:
541 TALLOC_FREE(ctx);
542 return ret;