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