audit: remove static local realms
[openafs.git] / src / tools / dumpscan / afsdump_scan.c
1 /*
2  * CMUCS AFStools
3  * dumpscan - routines for scanning and manipulating AFS volume dumps
4  *
5  * Copyright (c) 1998 Carnegie Mellon University
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software_Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie Mellon
26  * the rights to redistribute these changes.
27  */
28
29 /* afsdump_scan.c - General-purpose dump scanner */
30
31 #include <afsconfig.h>
32 #include <afs/param.h>
33
34 #include <roken.h>
35
36 #include <afs/com_err.h>
37 #include <afs/pterror.h>
38 #include <afs/vlserver.h>
39 #include <afs/cellconfig.h>
40 #include <rx/rxkad.h>
41 #include <afs/volser.h>
42 #include <ubik.h>
43
44 #include "dumpscan.h"
45 #include "dumpscan_errs.h"
46 #include "xf_errs.h"
47 extern int optind;
48 extern char *optarg;
49
50 extern XFILE repair_output;
51 extern afs_uint32 repair_dumphdr_cb(afs_dump_header *, XFILE *, void *);
52 extern afs_uint32 repair_volhdr_cb(afs_vol_header *, XFILE *, void *);
53 extern afs_uint32 repair_vnode_cb(afs_vnode *, XFILE *, void *);
54
55 char *argv0;
56 static char *input_path, *gendump_path;
57 static afs_uint32 printflags, repairflags;
58 static int quiet, verbose, error_count;
59
60 static path_hashinfo phi;
61 static dump_parser dp;
62
63
64 /* Print a usage message and exit */
65 static void
66 usage(int status, char *msg)
67 {
68     if (msg)
69         fprintf(stderr, "%s: %s\n", argv0, msg);
70     fprintf(stderr, "Usage: %s [options] [file]\n", argv0);
71     fprintf(stderr, "  -Pxxx  Set print options:\n");
72     fprintf(stderr, "          B = Print backup system header (if any)\n");
73     fprintf(stderr, "          H = Print AFS dump header\n");
74     fprintf(stderr, "          V = Print AFS volume header\n");
75     fprintf(stderr, "          v = List vnodes\n");
76     fprintf(stderr, "          p = Include path to each vnode\n");
77     fprintf(stderr, "          i = Include info for each vnode\n");
78     fprintf(stderr, "          d = List directory contents\n");
79     fprintf(stderr, "          a = List access control lists\n");
80     fprintf(stderr, "          g = Print debugging info\n");
81     fprintf(stderr, "  -Rxxx  Set repair options:\n");
82     fprintf(stderr, "          0 = Skip null tags\n");
83     fprintf(stderr, "          b = Seek backward to find skipped tags\n");
84     fprintf(stderr, "          d = Resync after vnode data\n");
85     fprintf(stderr, "          v = Resync after corrupted vnodes\n");
86     fprintf(stderr, "  -h     Print this help message\n");
87     fprintf(stderr, "  -gxxx  Generate a new dump in file xxx\n");
88     fprintf(stderr, "  -q     Quiet mode (don't print errors)\n");
89     fprintf(stderr, "  -v     Verbose mode\n");
90     exit(status);
91 }
92
93
94 /* Parse the argument given to the -P option.
95  * Returns the resulting * dumpscan print flags (DSPRINT_*).
96  * If an unrecognized flag is used, prints an error message and exits.
97  */
98 static afs_uint32
99 parse_printflags(char *flags)
100 {
101     afs_uint32 result = 0;
102     char *x;
103
104     for (x = flags; *x; x++)
105         switch (*x) {
106         case 'B':
107             result |= DSPRINT_BCKHDR;
108             continue;
109         case 'H':
110             result |= DSPRINT_DUMPHDR;
111             continue;
112         case 'V':
113             result |= DSPRINT_VOLHDR;
114             continue;
115         case 'v':
116             result |= DSPRINT_ITEM;
117             continue;
118         case 'p':
119             result |= DSPRINT_PATH;
120             continue;
121         case 'i':
122             result |= DSPRINT_VNODE;
123             continue;
124         case 'd':
125             result |= DSPRINT_DIR;
126             continue;
127         case 'a':
128             result |= DSPRINT_ACL;
129             continue;
130         case 'g':
131             result |= DSPRINT_DEBUG;
132             continue;
133         default:
134             usage(1, "Invalid print options!");
135         }
136     return result;
137 }
138
139
140 /* Parse the argument given to the -R option.
141  * Returns the resulting * dumpscan repair flags (DSFIX_*).
142  * If an unrecognized flag is used, prints an error message and exits.
143  */
144 static afs_uint32
145 parse_repairflags(char *flags)
146 {
147     afs_uint32 result = 0;
148     char *x;
149
150     for (x = flags; *x; x++)
151         switch (*x) {
152         case '0':
153             result |= DSFIX_SKIP;
154             continue;
155         case 'b':
156             result |= DSFIX_RSKIP;
157             continue;
158         case 'd':
159             result |= DSFIX_VDSYNC;
160             continue;
161         case 'v':
162             result |= DSFIX_VFSYNC;
163             continue;
164         default:
165             usage(1, "Invalid repair options!");
166         }
167     return result;
168 }
169
170
171 /* Parse the command-line options */
172 static void
173 parse_options(int argc, char **argv)
174 {
175     int c;
176
177     /* Set the program name */
178     if ((argv0 = strrchr(argv[0], '/')))
179         argv0++;
180     else
181         argv0 = argv[0];
182
183     /* Initialize options */
184     input_path = gendump_path = 0;
185     printflags = repairflags = 0;
186     quiet = verbose = 0;
187
188     /* Initialize other stuff */
189     error_count = 0;
190
191     /* Parse the options */
192     while ((c = getopt(argc, argv, "P:R:g:hqv")) != EOF) {
193         switch (c) {
194         case 'P':
195             printflags = parse_printflags(optarg);
196             continue;
197         case 'R':
198             repairflags = parse_repairflags(optarg);
199             continue;
200         case 'g':
201             gendump_path = optarg;
202             continue;
203         case 'q':
204             quiet = 1;
205             continue;
206         case 'v':
207             verbose = 1;
208             continue;
209         case 'h':
210             usage(0, 0);
211         default:
212             usage(1, "Invalid option!");
213         }
214     }
215
216     if (quiet && verbose)
217         usage(1, "Can't specify both -q and -v");
218
219     /* Parse non-option arguments */
220     if (argc - optind > 1)
221         usage(1, "Too many arguments!");
222     input_path = (argc == optind) ? "-" : argv[optind];
223 }
224
225
226 /* A callback to count and print errors */
227 static afs_uint32
228 my_error_cb(afs_uint32 code, int fatal, void *ref, char *msg, ...)
229 {
230     va_list alist;
231
232     error_count++;
233     if (!quiet) {
234         va_start(alist, msg);
235         afs_com_err_va(argv0, code, msg, alist);
236         va_end(alist);
237     }
238     return 0;
239 }
240
241
242 /* A callback to print the path of a vnode. */
243 static afs_uint32
244 print_vnode_path(afs_vnode * v, XFILE * X, void *refcon)
245 {
246     afs_uint32 r;
247     char *name = 0;
248
249     /* Do repair, but only for known vnode types */
250     if (gendump_path && (!(v->field_mask & F_VNODE_TYPE)
251                          || ((v->type != vFile) && (v->type != vDirectory)
252                              && (v->type != vSymlink)))) {
253         r = repair_vnode_cb(v, X, refcon);
254         if (r)
255             return r;
256     }
257     r = Path_Build(X, &phi, v->vnode, &name, 0);
258     if (!r && name)
259         printf(" Path: %s\n", name);
260     if (name)
261         free(name);
262     return r;
263 }
264
265
266 /* Setup for generating a repaired dump */
267 static afs_uint32
268 setup_repair(void)
269 {
270     afs_uint32 r;
271
272     r = xfopen(&repair_output, O_RDWR | O_CREAT | O_TRUNC, gendump_path);
273     if (r)
274         return r;
275
276     dp.cb_dumphdr = repair_dumphdr_cb;
277     dp.cb_volhdr = repair_volhdr_cb;
278     dp.cb_vnode_dir = repair_vnode_cb;
279     dp.cb_vnode_file = repair_vnode_cb;
280     dp.cb_vnode_link = repair_vnode_cb;
281     dp.cb_vnode_empty = repair_vnode_cb;
282     return 0;
283 }
284
285 extern afs_uint32 DumpDumpEnd(XFILE * OX);
286
287 /* Main program */
288 int
289 main(int argc, char **argv)
290 {
291     XFILE input_file;
292     afs_uint32 r;
293
294     parse_options(argc, argv);
295     initialize_acfg_error_table();
296     initialize_AVds_error_table();
297     initialize_rxk_error_table();
298     initialize_u_error_table();
299     initialize_vl_error_table();
300     initialize_vols_error_table();
301     initialize_xFil_error_table();
302     r = xfopen(&input_file, O_RDONLY, input_path);
303     if (r) {
304         afs_com_err(argv0, r, "opening %s", input_path);
305         exit(2);
306     }
307
308     memset(&dp, 0, sizeof(dp));
309     dp.cb_error = my_error_cb;
310     dp.repair_flags = repairflags;
311     if (input_file.is_seekable)
312         dp.flags |= DSFLAG_SEEK;
313     else {
314         if (repairflags)
315             fprintf(stderr,
316                     "Repair modes available only for seekable dumps\n");
317         if (printflags & DSPRINT_PATH)
318             fprintf(stderr,
319                     "Path-printing available only for seekable dumps\n");
320         if (repairflags || (printflags & DSPRINT_PATH))
321             exit(1);
322     }
323
324     if (gendump_path && (r = setup_repair())) {
325         afs_com_err(argv0, r, "setting up repair output");
326         xfclose(&input_file);
327         exit(2);
328     }
329
330     if (printflags & DSPRINT_PATH) {
331         dt_uint64 where;
332
333         dp.print_flags = printflags & DSPRINT_DEBUG;
334         memset(&phi, 0, sizeof(phi));
335         phi.p = &dp;
336
337         if ((r = xftell(&input_file, &where))
338             || (r = Path_PreScan(&input_file, &phi, 0))
339             || (r = xfseek(&input_file, &where))) {
340             afs_com_err(argv0, r, "- path initialization failed");
341             xfclose(&input_file);
342             exit(2);
343         }
344
345         dp.cb_vnode_dir = print_vnode_path;
346         dp.cb_vnode_file = print_vnode_path;
347         dp.cb_vnode_link = print_vnode_path;
348         dp.cb_vnode_empty = print_vnode_path;
349         dp.cb_vnode_wierd = print_vnode_path;
350     }
351
352     dp.print_flags = printflags;
353     r = ParseDumpFile(&input_file, &dp);
354     xfclose(&input_file);
355     if (gendump_path) {
356         if (!r)
357             r = DumpDumpEnd(&repair_output);
358         if (!r)
359             r = xfclose(&repair_output);
360         else
361             xfclose(&repair_output);
362     }
363
364     if (verbose && error_count)
365         fprintf(stderr, "*** %d errors\n", error_count);
366     if (r && !quiet)
367         fprintf(stderr, "*** FAILED: %s\n", afs_error_message(r));
368     exit(0);
369 }