File: selfcheck.c

package info (click to toggle)
amanda 1%3A2.4.0-3
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 3,228 kB
  • ctags: 3,359
  • sloc: ansic: 30,812; sh: 3,768; makefile: 616; perl: 591; awk: 331; lex: 121; yacc: 73
file content (528 lines) | stat: -rw-r--r-- 12,101 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
 * Copyright (c) 1991 University of Maryland
 * All Rights Reserved.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of U.M. not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  U.M. makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: James da Silva, Systems Design and Analysis Group
 *			   Computer Science Department
 *			   University of Maryland at College Park
 */
/* 
 * $Id$
 *
 * do self-check and send back any error messages
 */

#include "amanda.h"
#include "statfs.h"
#include "version.h"
#include "getfsent.h"
#include "amandates.h"

#ifdef SAMBA_CLIENT
#include "findpass.h"
#endif

int need_samba=0;
int need_rundump=0;
int need_dump=0;
int need_restore=0;
int need_vdump=0;
int need_vrestore=0;
int need_xfsdump=0;
int need_xfsrestore=0;
int need_vxdump=0;
int need_vxrestore=0;
int need_runtar=0;
int need_gnutar=0;
int need_compress_path=0;

char *pname = "selfcheck";

/* local functions */
int main P((int argc, char **argv));

static void check_options P((char *program, char *disk, char *str));
static void check_disk P((char *program, char *disk, int level));
static void check_overall P((void));
static void check_file P((char *filename, int mode));
static void check_space P((char *dir, long kbytes));

int main(argc, argv)
int argc;
char **argv;
{
    int level;
    char *line = NULL;
    char *program = NULL;
    char *disk = NULL;
    char *optstr = NULL;
    char *s;
    int ch;
    int fd;

    /* initialize */

    for(fd = 3; fd < FD_SETSIZE; fd++) {
	/*
	 * Make sure nobody spoofs us with a lot of extra open files
	 * that would cause an open we do to get a very high file
	 * descriptor, which in turn might be used as an index into
	 * an array (e.g. an fd_set).
	 */
	close(fd);
    }

    chdir("/tmp");
    erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
    umask(0007);
    dbopen();
    dbprintf(("%s: version %s\n", argv[0], version()));

    /* handle all service requests */

    for(; (line = agets(stdin)) != NULL; free(line)) {
#define sc "OPTIONS"
	if(strncmp(line, sc, sizeof(sc)-1) == 0) {
#undef sc
	    /* we don't recognize any options yet */
	    printf("OPTIONS ;\n");
	    continue;
	}

	s = line;
	ch = *s++;

	skip_whitespace(s, ch);			/* find program name */
	if (ch == '\0') {
	    goto err;				/* no program */
	}
	program = s - 1;
	skip_non_whitespace(s, ch);
	s[-1] = '\0';				/* terminate the program name */

	skip_whitespace(s, ch);			/* find disk name */
	if (ch == '\0') {
	    goto err;				/* no disk */
	}
	disk = s - 1;
	skip_non_whitespace(s, ch);
	s[-1] = '\0';				/* terminate the disk name */

	skip_whitespace(s, ch);			/* find level number */
	if (ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
	    goto err;				/* bad level */
	}
	skip_integer(s, ch);

#define sc "OPTIONS"
	skip_whitespace(s, ch);
	if (ch && strncmp (s - 1, sc, sizeof(sc)-1) == 0) {
	    s += sizeof(sc)-1;
	    ch = s[-1];
#undef sc
	    skip_whitespace(s, ch);		/* find the option string */
	    if(ch == '\0') {
		goto err;			/* bad options string */
	    }
	    optstr = s - 1;
	    skip_non_whitespace(s, ch);
	    s[-1] = '\0';			/* terminate the options */
	    check_options(program, disk, optstr);
	    check_disk(program, disk, level);
	} else if (ch == '\0') {
	    /* check all since no option */
	    need_samba=1;
	    need_rundump=1;
	    need_dump=1;
	    need_restore=1;
	    need_vdump=1;
	    need_vrestore=1;
	    need_xfsdump=1;
	    need_xfsrestore=1;
	    need_vxdump=1;
	    need_vxrestore=1;
	    need_runtar=1;
	    need_gnutar=1;
	    need_compress_path=1;
	    check_disk(program, disk, level);
	} else {
	    goto err;				/* bad syntax */
	}
    }

    check_overall();

    afree(line);
    dbclose();
    return 0;

 err:
    afree(line);
    printf("ERROR [BOGUS REQUEST PACKET]\n");
    dbprintf(("REQ packet is bogus\n"));
    dbclose();
    return 1;
}


static void check_options(program, disk, str)
char *program, *disk, *str;
{
    int as_index = 0;
    char *device = NULL;

    if(strstr(str,"index") != NULL)
	as_index=1;
    if(strcmp(program,"GNUTAR") == 0) {
	need_runtar=1;
	need_gnutar=1;
        if(disk[0] == '/' && disk[1] == '/')
	    need_samba=1;
    }
    if(strcmp(program,"DUMP") == 0) {
#ifdef USE_RUNDUMP
	need_rundump=1;
#endif
#ifndef AIX_BACKUP
	device = stralloc(amname_to_devname(disk));

#ifdef VDUMP
#ifdef DUMP
	if (strcmp(amname_to_fstype(device), "advfs") == 0)
#else
	if (1)
#endif
	{
	    need_vdump=1;
	    need_rundump=1;
	    if (as_index)
		need_vrestore=1;
	}
	else
#endif /* VDUMP */
#ifdef XFSDUMP
#ifdef DUMP
	if (strcmp(amname_to_fstype(device), "xfs") == 0)
#else
	if (1)
#endif
	{
	    need_xfsdump=1;
	    if (as_index)
		need_xfsrestore=1;
	}
	else
#endif /* XFSDUMP */
#ifdef VXDUMP
#ifdef DUMP
	if (strcmp(amname_to_fstype(device), "vxfs") == 0)
#else
	if (1)
#endif
	{
	    need_vxdump=1;
	    if (as_index)
		need_vxrestore=1;
	}
	else
#endif /* VXDUMP */
	{
	    need_dump=1;
	    if (as_index)
		need_restore=1;
	}
#else
	/* AIX backup program */
	need_dump=1;
	if (as_index)
	    need_restore=1;
#endif
    }
    if(strstr(str, "compress") != NULL)
	need_compress_path=1;
    if(strstr(str, "index") != NULL) {
	/* do nothing */
    }
    afree(device);
}

static void check_disk(program, disk, level)
char *program, *disk;
int level;
{
    char *device = NULL;
    char *err = NULL;
    int amode;

    if (strcmp(program, "GNUTAR") == 0) {
#ifdef SAMBA_CLIENT
        if(disk[0] == '/' && disk[1] == '/') {
	    char *cmd, *pass, *domain = NULL;

	    if ((pass = findpass(disk, &domain)) == NULL) {
		printf("ERROR [can't find password for %s]\n", disk);
		return;
	    }
	    if ((device = makesharename(disk, 1)) == NULL) {
		memset(pass, '\0', strlen(pass));
		afree(pass);
		if(domain) {
		    memset(domain, '\0', strlen(domain));
		    afree(domain);
		}
		printf("ERROR [can't make share name of %s]\n", disk);
		return;
	    }
	    cmd = vstralloc(SAMBA_CLIENT,
			    " ", device,
			    " \'", pass, "\'",
			    " -E",
			    " -U ", SAMBA_USER,
			    /* if domain is NULL, just quit */
			    domain ? " -W " : " -c quit",
			    domain ? domain : NULL,
			    " -c quit",
			    NULL);
	    memset(pass, '\0', strlen(pass));
	    afree(pass);
	    printf("running %s %s XXXX -E -U %s%s%s -c quit\n",
		   SAMBA_CLIENT, device, SAMBA_USER,
		   domain ? " -W " : "", domain ? domain : "");
	    if(domain) {
		memset(domain, '\0', strlen(domain));
		afree(domain);
	    }
	    if (system(cmd) & 0xff00)
		printf("ERROR [PC SHARE %s access error: host down or invalid password?]\n", disk);
	    else
		printf("OK %s\n", disk);
	    memset(cmd, '\0', strlen(cmd));
	    afree(cmd);
	    return;
	}
#endif
	amode = F_OK;
	device = stralloc(amname_to_dirname(disk));
    } else {
        device = stralloc(amname_to_devname(disk));
#ifdef VDUMP
#ifdef DUMP
        if (strcmp(amname_to_fstype(device), "advfs") == 0) {
#else
	if (1) {
#endif
	    device = newstralloc(device, amname_to_dirname(disk));
	    amode = F_OK;
	} else
#endif
	{
	    device = stralloc(amname_to_devname(disk));
#ifdef USE_RUNDUMP
	    amode = F_OK;
#else
	    amode = R_OK;
#endif
	}
    }

    dbprintf(("checking disk %s: device %s", disk, device));

#ifndef CHECK_FOR_ACCESS_WITH_OPEN
    if(access(device, amode) == -1) {
	    err = strerror(errno);
	    printf("ERROR [can not access %s (%s): %s]\n", device, disk, err);
    } else {
	    printf("OK %s\n", disk);
    }

#else
    {
	/* XXX better check in this case */
	int tstfd;
	if((tstfd = open(device, O_RDONLY)) == -1) {
	    err = strerror(errno);
	    printf("ERROR [could not open %s (%s): %s]\n", device, disk, err);
	} else {
	    printf("OK %s\n", device);
	}
	aclose(tstfd);
    }
#endif

    dbprintf((": %s\n", err ? err : "OK"));
    afree(device);

    /* XXX perhaps do something with level: read dumpdates and sanity check */
}

static void check_overall()
{
    char *cmd;

#ifdef SAMBA_CLIENT
    struct stat buf;
    int testfd;
#endif

    if( need_runtar )
    {
	cmd = vstralloc(libexecdir, "/", "runtar", versionsuffix(), NULL);
	check_file(cmd,X_OK);
	afree(cmd);
    }

    if( need_rundump )
    {
	cmd = vstralloc(libexecdir, "/", "rundump", versionsuffix(), NULL);
	check_file(cmd,X_OK);
	afree(cmd);
    }

#ifdef DUMP
    if( need_dump )
	check_file(DUMP, X_OK);
#endif

#ifdef RESTORE
    if( need_restore )
	check_file(RESTORE, X_OK);
#endif

#ifdef VDUMP
    if ( need_vdump )
	check_file(VDUMP, X_OK);
#endif

#ifdef VRESTORE
    if ( need_vrestore )
	check_file(VRESTORE, X_OK);
#endif

#ifdef XFSDUMP
    if( need_xfsdump )
	check_file(XFSDUMP, F_OK);
#endif

#ifdef XFSRESTORE
    if( need_xfsrestore )
	check_file(XFSRESTORE, X_OK);
#endif

#ifdef VXDUMP
    if( need_vxdump )
	check_file(VXDUMP, X_OK);
#endif

#ifdef VXRESTORE
    if( need_vxrestore )
	check_file(VXRESTORE, X_OK);
#endif

#ifdef GNUTAR
    if( need_gnutar )
    {
	check_file(GNUTAR, X_OK);
	check_file(AMANDATES_FILE, R_OK|W_OK);
#ifdef GNUTAR_LISTED_INCREMENTAL_DIR
	check_file(GNUTAR_LISTED_INCREMENTAL_DIR,R_OK|W_OK);
#endif
    }
#endif

#ifdef SAMBA_CLIENT
    if( need_samba )
    {
	check_file(SAMBA_CLIENT, X_OK);
	testfd = open("/etc/amandapass", R_OK);
	if (testfd >= 0) {
	    if(!fstat(testfd, &buf)) {
		if (buf.st_mode & 0x7)
		    printf("ERROR [/etc/amandapass is world readable!]\n");
		else
		    printf("OK [/etc/amandapass is readable, but not by all]\n");
	    } else {
		printf("OK [unable to access /etc/amandapass?]\n");
	    }
	    aclose(testfd);
	} else {
	    printf("ERROR [unable to access /etc/amandapass?]\n");
	}
    }
#endif
    if( need_compress_path )
	check_file(COMPRESS_PATH, X_OK);
#if defined(DUMP) || defined(XFSDUMP)
    if( need_dump || need_xfsdump )
	check_file("/var/lib/dumpdates",
#ifdef USE_RUNDUMP
		   F_OK
#else
		   R_OK|W_OK
#endif
		   );
#endif
#ifdef VDUMP
    if (need_vdump)
        check_file("/etc/vdumpdates", F_OK);
#endif
    check_file("/dev/null", R_OK|W_OK);
    check_space("/tmp", 64);		/* for amandad i/o */
#ifdef DEBUG_DIR
    check_space(DEBUG_DIR, 64);		/* for amandad i/o */
#endif
    check_space("/etc", 64);		/* for /var/lib/dumpdates writing */
}

static void check_space(dir, kbytes)
char *dir;
long kbytes;
{
    generic_fs_stats_t statp;

    if(get_fs_stats(dir, &statp) == -1)
	printf("ERROR [cannot statfs %s: %s]\n", dir, strerror(errno));
    else if(statp.avail < kbytes)
	printf("ERROR [dir %s needs %ldKB, only has %ldKB available.]\n",
	       dir, kbytes, statp.avail);
    else
	printf("OK %s has more than %ld KB available.\n", dir, kbytes);
}

static void check_file(filename, mode)
char *filename;
int mode;
{
    char *noun, *adjective;

    if(mode == F_OK)
        noun = "find", adjective = "exists";
    else if((mode & X_OK) == X_OK)
	noun = "execute", adjective = "executable";
    else if((mode & (W_OK|R_OK)) == (W_OK|R_OK))
	noun = "read/write", adjective = "read/writable";
    else 
	noun = "access", adjective = "accessible";

    if(access(filename, mode) == -1)
	printf("ERROR [can not %s %s: %s]\n", noun, filename, strerror(errno));
    else
	printf("OK %s %s\n", filename, adjective);
}