Tiny simplification to dom_sid_string_buf
[Samba.git] / source3 / lib / tdb_validate.c
blob7dd7dae5ac14b50e6e40447606f784771f017705
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 num_entries = 0;
35 struct tdb_validation_status v_status;
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;
43 if (!tdb) {
44 v_status.tdb_error = True;
45 v_status.success = False;
46 goto out;
50 * we can simplify this by passing a check function,
51 * but I don't want to change all the callers...
53 ret = tdb_check(tdb, NULL, NULL);
54 if (ret == -1) {
55 v_status.tdb_error = True;
56 v_status.success = False;
57 goto out;
60 /* Check if the tdb's freelist is good. */
61 if (tdb_validate_freelist(tdb, &num_entries) == -1) {
62 v_status.bad_freelist = True;
63 v_status.success = False;
64 goto out;
67 DEBUG(10,("tdb_validate_child: tdb %s freelist has %d entries\n",
68 tdb_name(tdb), num_entries));
70 /* Now traverse the tdb to validate it. */
71 num_entries = tdb_traverse(tdb, validate_fn, (void *)&v_status);
72 if (!v_status.success) {
73 goto out;
74 } else if (num_entries == -1) {
75 v_status.tdb_error = True;
76 v_status.success = False;
77 goto out;
80 DEBUG(10,("tdb_validate_child: tdb %s is good with %d entries\n",
81 tdb_name(tdb), num_entries));
82 ret = 0; /* Cache is good. */
84 out:
85 DEBUG(10, ("tdb_validate_child: summary of validation status:\n"));
86 DEBUGADD(10,(" * tdb error: %s\n", v_status.tdb_error ? "yes" : "no"));
87 DEBUGADD(10,(" * bad freelist: %s\n",v_status.bad_freelist?"yes":"no"));
88 DEBUGADD(10,(" * bad entry: %s\n", v_status.bad_entry ? "yes" : "no"));
89 DEBUGADD(10,(" * unknown key: %s\n", v_status.unknown_key?"yes":"no"));
90 DEBUGADD(10,(" => overall success: %s\n", v_status.success?"yes":"no"));
92 return ret;
96 * tdb validation function.
97 * returns 0 if tdb is ok, != 0 if it isn't.
98 * this function expects an opened tdb.
100 int tdb_validate(struct tdb_context *tdb, tdb_validate_data_func validate_fn)
102 pid_t child_pid = -1;
103 int child_status = 0;
104 int wait_pid = 0;
105 int ret = 1;
107 if (tdb == NULL) {
108 DEBUG(1, ("Error: tdb_validate called with tdb == NULL\n"));
109 return ret;
112 DEBUG(5, ("tdb_validate called for tdb '%s'\n", tdb_name(tdb)));
114 /* fork and let the child do the validation.
115 * benefit: no need to twist signal handlers and panic functions.
116 * just let the child panic. we catch the signal. */
118 DEBUG(10, ("tdb_validate: forking to let child do validation.\n"));
119 child_pid = sys_fork();
120 if (child_pid == 0) {
121 /* child code */
122 DEBUG(10, ("tdb_validate (validation child): created\n"));
123 DEBUG(10, ("tdb_validate (validation child): "
124 "calling tdb_validate_child\n"));
125 exit(tdb_validate_child(tdb, validate_fn));
127 else if (child_pid < 0) {
128 DEBUG(1, ("tdb_validate: fork for validation failed.\n"));
129 goto done;
132 /* parent */
134 DEBUG(10, ("tdb_validate: fork succeeded, child PID = %u\n",
135 (unsigned int)child_pid));
137 DEBUG(10, ("tdb_validate: waiting for child to finish...\n"));
138 while ((wait_pid = sys_waitpid(child_pid, &child_status, 0)) < 0) {
139 if (errno == EINTR) {
140 DEBUG(10, ("tdb_validate: got signal during waitpid, "
141 "retrying\n"));
142 errno = 0;
143 continue;
145 DEBUG(1, ("tdb_validate: waitpid failed with error '%s'.\n",
146 strerror(errno)));
147 goto done;
149 if (wait_pid != child_pid) {
150 DEBUG(1, ("tdb_validate: waitpid returned pid %d, "
151 "but %u was expected\n", wait_pid, (unsigned int)child_pid));
152 goto done;
155 DEBUG(10, ("tdb_validate: validating child returned.\n"));
156 if (WIFEXITED(child_status)) {
157 DEBUG(10, ("tdb_validate: child exited, code %d.\n",
158 WEXITSTATUS(child_status)));
159 ret = WEXITSTATUS(child_status);
161 if (WIFSIGNALED(child_status)) {
162 DEBUG(10, ("tdb_validate: child terminated by signal %d\n",
163 WTERMSIG(child_status)));
164 #ifdef WCOREDUMP
165 if (WCOREDUMP(child_status)) {
166 DEBUGADD(10, ("core dumped\n"));
168 #endif
169 ret = WTERMSIG(child_status);
171 if (WIFSTOPPED(child_status)) {
172 DEBUG(10, ("tdb_validate: child was stopped by signal %d\n",
173 WSTOPSIG(child_status)));
174 ret = WSTOPSIG(child_status);
177 done:
178 DEBUG(5, ("tdb_validate returning code '%d' for tdb '%s'\n", ret,
179 tdb_name(tdb)));
181 return ret;
185 * tdb validation function.
186 * returns 0 if tdb is ok, != 0 if it isn't.
187 * this is a wrapper around the actual validation function that opens and closes
188 * the tdb.
190 int tdb_validate_open(const char *tdb_path, tdb_validate_data_func validate_fn)
192 TDB_CONTEXT *tdb = NULL;
193 int ret = 1;
195 DEBUG(5, ("tdb_validate_open called for tdb '%s'\n", tdb_path));
197 tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDWR, 0);
198 if (!tdb) {
199 DEBUG(1, ("Error opening tdb %s\n", tdb_path));
200 return ret;
203 ret = tdb_validate(tdb, validate_fn);
204 tdb_close(tdb);
205 return ret;
209 * tdb backup function and helpers for tdb_validate wrapper with backup
210 * handling.
213 /* this structure eliminates the need for a global overall status for
214 * the traverse-copy */
215 struct tdb_copy_data {
216 struct tdb_context *dst;
217 bool success;
220 static int traverse_copy_fn(struct tdb_context *tdb, TDB_DATA key,
221 TDB_DATA dbuf, void *private_data)
223 struct tdb_copy_data *data = (struct tdb_copy_data *)private_data;
225 if (tdb_store(data->dst, key, dbuf, TDB_INSERT) != 0) {
226 DEBUG(4, ("Failed to insert into %s: %s\n", tdb_name(data->dst),
227 strerror(errno)));
228 data->success = False;
229 return 1;
231 return 0;
234 static int tdb_copy(struct tdb_context *src, struct tdb_context *dst)
236 struct tdb_copy_data data;
237 int count;
239 data.dst = dst;
240 data.success = True;
242 count = tdb_traverse(src, traverse_copy_fn, (void *)(&data));
243 if ((count < 0) || (data.success == False)) {
244 return -1;
246 return count;
249 static int tdb_verify_basic(struct tdb_context *tdb)
251 return tdb_traverse(tdb, NULL, NULL);
254 /* this backup function is essentially taken from lib/tdb/tools/tdbbackup.tdb
256 static int tdb_backup(TALLOC_CTX *ctx, const char *src_path,
257 const char *dst_path, int hash_size)
259 struct tdb_context *src_tdb = NULL;
260 struct tdb_context *dst_tdb = NULL;
261 char *tmp_path = NULL;
262 struct stat st;
263 int count1, count2;
264 int saved_errno = 0;
265 int ret = -1;
267 if (stat(src_path, &st) != 0) {
268 DEBUG(3, ("Could not stat '%s': %s\n", src_path,
269 strerror(errno)));
270 goto done;
273 /* open old tdb RDWR - so we can lock it */
274 src_tdb = tdb_open_log(src_path, 0, TDB_DEFAULT, O_RDWR, 0);
275 if (src_tdb == NULL) {
276 DEBUG(3, ("Failed to open tdb '%s'\n", src_path));
277 goto done;
280 if (tdb_lockall(src_tdb) != 0) {
281 DEBUG(3, ("Failed to lock tdb '%s'\n", src_path));
282 goto done;
285 tmp_path = talloc_asprintf(ctx, "%s%s", dst_path, ".tmp");
286 if (!tmp_path) {
287 DEBUG(3, ("talloc fail\n"));
288 goto done;
291 unlink(tmp_path);
292 dst_tdb = tdb_open_log(tmp_path,
293 hash_size ? hash_size : tdb_hash_size(src_tdb),
294 TDB_DEFAULT, O_RDWR | O_CREAT | O_EXCL,
295 st.st_mode & 0777);
296 if (dst_tdb == NULL) {
297 DEBUG(3, ("Error creating tdb '%s': %s\n", tmp_path,
298 strerror(errno)));
299 saved_errno = errno;
300 unlink(tmp_path);
301 goto done;
304 count1 = tdb_copy(src_tdb, dst_tdb);
305 if (count1 < 0) {
306 DEBUG(3, ("Failed to copy tdb '%s': %s\n", src_path,
307 strerror(errno)));
308 tdb_close(dst_tdb);
309 goto done;
312 /* reopen ro and do basic verification */
313 tdb_close(dst_tdb);
314 dst_tdb = tdb_open_log(tmp_path, 0, TDB_DEFAULT, O_RDONLY, 0);
315 if (!dst_tdb) {
316 DEBUG(3, ("Failed to reopen tdb '%s': %s\n", tmp_path,
317 strerror(errno)));
318 goto done;
320 count2 = tdb_verify_basic(dst_tdb);
321 if (count2 != count1) {
322 DEBUG(3, ("Failed to verify result of copying tdb '%s'.\n",
323 src_path));
324 tdb_close(dst_tdb);
325 goto done;
328 DEBUG(10, ("tdb_backup: successfully copied %d entries\n", count1));
330 /* make sure the new tdb has reached stable storage
331 * then rename it to its destination */
332 fsync(tdb_fd(dst_tdb));
333 tdb_close(dst_tdb);
334 unlink(dst_path);
335 if (rename(tmp_path, dst_path) != 0) {
336 DEBUG(3, ("Failed to rename '%s' to '%s': %s\n",
337 tmp_path, dst_path, strerror(errno)));
338 goto done;
341 /* success */
342 ret = 0;
344 done:
345 if (src_tdb != NULL) {
346 tdb_close(src_tdb);
348 if (tmp_path != NULL) {
349 unlink(tmp_path);
350 TALLOC_FREE(tmp_path);
352 if (saved_errno != 0) {
353 errno = saved_errno;
355 return ret;
358 static int rename_file_with_suffix(TALLOC_CTX *ctx, const char *path,
359 const char *suffix)
361 int ret = -1;
362 char *dst_path;
364 dst_path = talloc_asprintf(ctx, "%s%s", path, suffix);
365 if (dst_path == NULL) {
366 DEBUG(3, ("error out of memory\n"));
367 return ret;
370 ret = (rename(path, dst_path) != 0);
372 if (ret == 0) {
373 DEBUG(5, ("moved '%s' to '%s'\n", path, dst_path));
374 } else if (errno == ENOENT) {
375 DEBUG(3, ("file '%s' does not exist - so not moved\n", path));
376 ret = 0;
377 } else {
378 DEBUG(3, ("error renaming %s to %s: %s\n", path, dst_path,
379 strerror(errno)));
382 TALLOC_FREE(dst_path);
383 return ret;
387 * do a backup of a tdb, moving the destination out of the way first
389 static int tdb_backup_with_rotate(TALLOC_CTX *ctx, const char *src_path,
390 const char *dst_path, int hash_size,
391 const char *rotate_suffix,
392 bool retry_norotate_if_nospc,
393 bool rename_as_last_resort_if_nospc)
395 int ret;
397 rename_file_with_suffix(ctx, dst_path, rotate_suffix);
399 ret = tdb_backup(ctx, src_path, dst_path, hash_size);
401 if (ret != 0) {
402 DEBUG(10, ("backup of %s failed: %s\n", src_path, strerror(errno)));
404 if ((ret != 0) && (errno == ENOSPC) && retry_norotate_if_nospc)
406 char *rotate_path = talloc_asprintf(ctx, "%s%s", dst_path,
407 rotate_suffix);
408 if (rotate_path == NULL) {
409 DEBUG(10, ("talloc fail\n"));
410 return -1;
412 DEBUG(10, ("backup of %s failed due to lack of space\n",
413 src_path));
414 DEBUGADD(10, ("trying to free some space by removing rotated "
415 "dst %s\n", rotate_path));
416 if (unlink(rotate_path) == -1) {
417 DEBUG(10, ("unlink of %s failed: %s\n", rotate_path,
418 strerror(errno)));
419 } else {
420 ret = tdb_backup(ctx, src_path, dst_path, hash_size);
422 TALLOC_FREE(rotate_path);
425 if ((ret != 0) && (errno == ENOSPC) && rename_as_last_resort_if_nospc)
427 DEBUG(10, ("backup of %s failed due to lack of space\n",
428 src_path));
429 DEBUGADD(10, ("using 'rename' as a last resort\n"));
430 ret = rename(src_path, dst_path);
433 return ret;
437 * validation function with backup handling:
439 * - calls tdb_validate
440 * - if the tdb is ok, create a backup "name.bak", possibly moving
441 * existing backup to name.bak.old,
442 * return 0 (success) even if the backup fails
443 * - if the tdb is corrupt:
444 * - move the tdb to "name.corrupt"
445 * - check if there is valid backup.
446 * if so, restore the backup.
447 * if restore is successful, return 0 (success),
448 * - otherwise return -1 (failure)
450 int tdb_validate_and_backup(const char *tdb_path,
451 tdb_validate_data_func validate_fn)
453 int ret = -1;
454 const char *backup_suffix = ".bak";
455 const char *corrupt_suffix = ".corrupt";
456 const char *rotate_suffix = ".old";
457 char *tdb_path_backup;
458 struct stat st;
459 TALLOC_CTX *ctx = NULL;
461 ctx = talloc_new(NULL);
462 if (ctx == NULL) {
463 DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
464 goto done;
467 tdb_path_backup = talloc_asprintf(ctx, "%s%s", tdb_path, backup_suffix);
468 if (!tdb_path_backup) {
469 DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
470 goto done;
473 ret = tdb_validate_open(tdb_path, validate_fn);
475 if (ret == 0) {
476 DEBUG(1, ("tdb '%s' is valid\n", tdb_path));
477 ret = tdb_backup_with_rotate(ctx, tdb_path, tdb_path_backup, 0,
478 rotate_suffix, True, False);
479 if (ret != 0) {
480 DEBUG(1, ("Error creating backup of tdb '%s'\n",
481 tdb_path));
482 /* the actual validation was successful: */
483 ret = 0;
484 } else {
485 DEBUG(1, ("Created backup '%s' of tdb '%s'\n",
486 tdb_path_backup, tdb_path));
488 } else {
489 DEBUG(1, ("tdb '%s' is invalid\n", tdb_path));
491 ret =stat(tdb_path_backup, &st);
492 if (ret != 0) {
493 DEBUG(5, ("Could not stat '%s': %s\n", tdb_path_backup,
494 strerror(errno)));
495 DEBUG(1, ("No backup found.\n"));
496 } else {
497 DEBUG(1, ("backup '%s' found.\n", tdb_path_backup));
498 ret = tdb_validate_open(tdb_path_backup, validate_fn);
499 if (ret != 0) {
500 DEBUG(1, ("Backup '%s' is invalid.\n",
501 tdb_path_backup));
505 if (ret != 0) {
506 int renamed = rename_file_with_suffix(ctx, tdb_path,
507 corrupt_suffix);
508 if (renamed != 0) {
509 DEBUG(1, ("Error moving tdb to '%s%s'\n",
510 tdb_path, corrupt_suffix));
511 } else {
512 DEBUG(1, ("Corrupt tdb stored as '%s%s'\n",
513 tdb_path, corrupt_suffix));
515 goto done;
518 DEBUG(1, ("valid backup '%s' found\n", tdb_path_backup));
519 ret = tdb_backup_with_rotate(ctx, tdb_path_backup, tdb_path, 0,
520 corrupt_suffix, True, True);
521 if (ret != 0) {
522 DEBUG(1, ("Error restoring backup from '%s'\n",
523 tdb_path_backup));
524 } else {
525 DEBUG(1, ("Restored tdb backup from '%s'\n",
526 tdb_path_backup));
530 done:
531 TALLOC_FREE(ctx);
532 return ret;