1/* 2 Unix SMB/CIFS implementation. 3 low level tdb backup and restore utility 4 Copyright (C) Andrew Tridgell 2002 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19*/ 20 21#ifdef STANDALONE 22#if HAVE_CONFIG_H 23#include <config.h> 24#endif 25 26#include <errno.h> 27#include <stdlib.h> 28#include <stdio.h> 29#include <fcntl.h> 30#include <unistd.h> 31#include <string.h> 32#include <fcntl.h> 33#include <time.h> 34#include <sys/mman.h> 35 36#include <sys/stat.h> 37#include <sys/time.h> 38#include <ctype.h> 39#include <signal.h> 40 41#else 42#include "includes.h" 43#endif 44 45#include "tdb.h" 46 47static int failed; 48 49char *add_suffix(const char *name, const char *suffix) 50{ 51 char *ret; 52 int len = strlen(name) + strlen(suffix) + 1; 53 ret = malloc(len); 54 if (!ret) { 55 fprintf(stderr,"Out of memory!\n"); 56 exit(1); 57 } 58 snprintf(ret, len, "%s%s", name, suffix); 59 return ret; 60} 61 62static int copy_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state) 63{ 64 TDB_CONTEXT *tdb_new = (TDB_CONTEXT *)state; 65 66 if (tdb_store(tdb_new, key, dbuf, TDB_INSERT) != 0) { 67 fprintf(stderr,"Failed to insert into %s\n", tdb_new->name); 68 failed = 1; 69 return 1; 70 } 71 return 0; 72} 73 74 75static int test_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state) 76{ 77 return 0; 78} 79 80/* 81 carefully backup a tdb, validating the contents and 82 only doing the backup if its OK 83 this function is also used for restore 84*/ 85int backup_tdb(const char *old_name, const char *new_name) 86{ 87 TDB_CONTEXT *tdb; 88 TDB_CONTEXT *tdb_new; 89 char *tmp_name; 90 struct stat st; 91 int count1, count2; 92 93 tmp_name = add_suffix(new_name, ".tmp"); 94 95 /* stat the old tdb to find its permissions */ 96 if (stat(old_name, &st) != 0) { 97 perror(old_name); 98 return 1; 99 } 100 101 /* open the old tdb */ 102 tdb = tdb_open(old_name, 0, 0, O_RDWR, 0); 103 if (!tdb) { 104 printf("Failed to open %s\n", old_name); 105 return 1; 106 } 107 108 /* create the new tdb */ 109 unlink(tmp_name); 110 tdb_new = tdb_open(tmp_name, tdb->header.hash_size, 111 TDB_DEFAULT, O_RDWR|O_CREAT|O_EXCL, 112 st.st_mode & 0777); 113 if (!tdb_new) { 114 perror(tmp_name); 115 free(tmp_name); 116 return 1; 117 } 118 119 /* lock the old tdb */ 120 if (tdb_lockall(tdb) != 0) { 121 fprintf(stderr,"Failed to lock %s\n", old_name); 122 tdb_close(tdb); 123 tdb_close(tdb_new); 124 unlink(tmp_name); 125 free(tmp_name); 126 return 1; 127 } 128 129 failed = 0; 130 131 /* traverse and copy */ 132 count1 = tdb_traverse(tdb, copy_fn, (void *)tdb_new); 133 if (count1 < 0 || failed) { 134 fprintf(stderr,"failed to copy %s\n", old_name); 135 tdb_close(tdb); 136 tdb_close(tdb_new); 137 unlink(tmp_name); 138 free(tmp_name); 139 return 1; 140 } 141 142 /* close the old tdb */ 143 tdb_close(tdb); 144 145 /* close the new tdb and re-open read-only */ 146 tdb_close(tdb_new); 147 tdb_new = tdb_open(tmp_name, 0, TDB_DEFAULT, O_RDONLY, 0); 148 if (!tdb_new) { 149 fprintf(stderr,"failed to reopen %s\n", tmp_name); 150 unlink(tmp_name); 151 perror(tmp_name); 152 free(tmp_name); 153 return 1; 154 } 155 156 /* traverse the new tdb to confirm */ 157 count2 = tdb_traverse(tdb_new, test_fn, 0); 158 if (count2 != count1) { 159 fprintf(stderr,"failed to copy %s\n", old_name); 160 tdb_close(tdb_new); 161 unlink(tmp_name); 162 free(tmp_name); 163 return 1; 164 } 165 166 /* make sure the new tdb has reached stable storage */ 167 fsync(tdb_new->fd); 168 169 /* close the new tdb and rename it to .bak */ 170 tdb_close(tdb_new); 171 unlink(new_name); 172 if (rename(tmp_name, new_name) != 0) { 173 perror(new_name); 174 free(tmp_name); 175 return 1; 176 } 177 178 free(tmp_name); 179 180 return 0; 181} 182 183 184 185/* 186 verify a tdb and if it is corrupt then restore from *.bak 187*/ 188int verify_tdb(const char *fname, const char *bak_name) 189{ 190 TDB_CONTEXT *tdb; 191 int count = -1; 192 193 /* open the tdb */ 194 tdb = tdb_open(fname, 0, 0, O_RDONLY, 0); 195 196 /* traverse the tdb, then close it */ 197 if (tdb) { 198 count = tdb_traverse(tdb, test_fn, NULL); 199 tdb_close(tdb); 200 } 201 202 /* count is < 0 means an error */ 203 if (count < 0) { 204 printf("restoring %s\n", fname); 205 return backup_tdb(bak_name, fname); 206 } 207 208 printf("%s : %d records\n", fname, count); 209 210 return 0; 211} 212