2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-2000 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: amcheck.c,v 1.50.2.19.2.7.2.22 2004/03/16 19:03:39 martinea Exp $
29 * checks for common problems in server and clients
44 #include "pipespawn.h"
45 #include "amfeatures.h"
48 * If we don't have the new-style wait access functions, use our own,
49 * compatible with old-style BSD systems at least. Note that we don't
50 * care about the case w_stopval == WSTOPPED since we don't ask to see
51 * stopped processes, so should never get them from wait.
54 # define WEXITSTATUS(r) (((union wait *) &(r))->w_retcode)
55 # define WTERMSIG(r) (((union wait *) &(r))->w_termsig)
58 # define WIFSIGNALED(r) (((union wait *) &(r))->w_termsig != 0)
61 #define BUFFER_SIZE 32768
63 static int conf_ctimeout;
67 static disklist_t *origqp;
69 static uid_t uid_dumpuser;
74 int start_client_checks P((int fd));
75 int start_server_check P((int fd, int do_localchk, int do_tapechk));
76 int main P((int argc, char **argv));
77 int scan_init P((int rc, int ns, int bk));
78 int taperscan_slot P((int rc, char *slotstr, char *device));
79 char *taper_scan P((void));
80 int test_server_pgm P((FILE *outf, char *dir, char *pgm,
81 int suid, uid_t dumpuid));
85 error("Usage: amcheck%s [-M <username>] [-mawsclt] <conf> [host [disk]* ]*", versionsuffix());
88 static unsigned long malloc_hist_1, malloc_size_1;
89 static unsigned long malloc_hist_2, malloc_size_2;
91 static am_feature_t *our_features = NULL;
92 static char *our_feature_string = NULL;
98 char buffer[BUFFER_SIZE];
100 char *mainfname = NULL;
101 char pid_str[NUM_STR_SIZE];
102 int do_clientchk, clientchk_pid, client_probs;
103 int do_localchk, do_tapechk, serverchk_pid, server_probs;
105 int opt, size, result_port, tempfd, mainfd;
115 char *tempfname = NULL;
122 for(fd = 3; fd < FD_SETSIZE; fd++) {
124 * Make sure nobody spoofs us with a lot of extra open files
125 * that would cause an open we do to get a very high file
126 * descriptor, which in turn might be used as an index into
127 * an array (e.g. an fd_set).
134 set_pname("amcheck");
137 malloc_size_1 = malloc_inuse(&malloc_hist_1);
139 ap_snprintf(pid_str, sizeof(pid_str), "%ld", (long)getpid());
141 erroutput_type = ERR_INTERACTIVE;
143 our_features = am_init_feature_set();
144 our_feature_string = am_feature_to_string(our_features);
146 /* set up dgram port first thing */
150 if(dgram_bind(msg, &result_port) == -1)
151 error("could not bind result datagram port: %s", strerror(errno));
154 /* set both real and effective uid's to real uid, likewise for gid */
160 alwaysmail = mailout = overwrite = 0;
161 do_localchk = do_tapechk = do_clientchk = 0;
163 server_probs = client_probs = 0;
164 tempfd = mainfd = -1;
166 /* process arguments */
168 while((opt = getopt(argc, argv, "M:mawsclt")) != EOF) {
170 case 'M': mailto=stralloc(optarg);
175 printf("You can't use -%c because configure didn't find a mailer.\n",
185 printf("You can't use -%c because configure didn't find a mailer.\n",
190 case 's': do_localchk = 1; do_tapechk = 1;
193 case 'c': do_clientchk = 1;
196 case 'l': do_localchk = 1;
199 case 'w': do_tapechk = 1; overwrite = 1;
202 case 't': do_tapechk = 1;
210 argc -= optind, argv += optind;
212 do_localchk = do_clientchk = do_tapechk = 1;
215 if(argc < 1) usage();
217 config_name = stralloc(*argv);
219 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
220 conffile = stralloc2(config_dir, CONFFILE_NAME);
221 if(read_conffile(conffile)) {
222 error("errors processing config file \"%s\"", conffile);
225 conf_ctimeout = getconf_int(CNF_CTIMEOUT);
226 conf_diskfile = getconf_str(CNF_DISKFILE);
227 if (*conf_diskfile == '/') {
228 conf_diskfile = stralloc(conf_diskfile);
230 conf_diskfile = stralloc2(config_dir, conf_diskfile);
232 if((origqp = read_diskfile(conf_diskfile)) == NULL) {
233 error("could not load disklist %s", conf_diskfile);
235 match_disklist(origqp, argc-1, argv+1);
236 amfree(conf_diskfile);
239 * Make sure we are running as the dump user.
241 dumpuser = getconf_str(CNF_DUMPUSER);
242 if ((pw = getpwnam(dumpuser)) == NULL) {
243 error("cannot look up dump user \"%s\"", dumpuser);
245 uid_dumpuser = pw->pw_uid;
246 if ((pw = getpwuid(uid_me)) == NULL) {
247 error("cannot look up my own uid (%ld)", (long)uid_me);
249 if (uid_me != uid_dumpuser) {
250 error("running as user \"%s\" instead of \"%s\"",
256 * If both server and client side checks are being done, the server
257 * check output goes to the main output, while the client check output
258 * goes to a temporary file and is copied to the main output when done.
260 * If the output is to be mailed, the main output is also a disk file,
261 * otherwise it is stdout.
263 if(do_clientchk && (do_localchk || do_tapechk)) {
264 /* we need the temp file */
265 tempfname = vstralloc(AMANDA_TMPDIR, "/amcheck.temp.", pid_str, NULL);
266 if((tempfd = open(tempfname, O_RDWR|O_CREAT|O_TRUNC, 0600)) == -1)
267 error("could not open %s: %s", tempfname, strerror(errno));
268 unlink(tempfname); /* so it goes away on close */
273 /* the main fd is a file too */
274 mainfname = vstralloc(AMANDA_TMPDIR, "/amcheck.main.", pid_str, NULL);
275 if((mainfd = open(mainfname, O_RDWR|O_CREAT|O_TRUNC, 0600)) == -1)
276 error("could not open %s: %s", mainfname, strerror(errno));
277 unlink(mainfname); /* so it goes away on close */
281 /* just use stdout */
284 /* start server side checks */
286 if(do_localchk || do_tapechk) {
287 serverchk_pid = start_server_check(mainfd, do_localchk, do_tapechk);
292 /* start client side checks */
295 clientchk_pid = start_client_checks((do_localchk || do_tapechk) ? tempfd : mainfd);
300 /* wait for child processes and note any problems */
303 if((pid = wait(&retstat)) == -1) {
304 if(errno == EINTR) continue;
306 } else if(pid == clientchk_pid) {
307 client_probs = WIFSIGNALED(retstat) || WEXITSTATUS(retstat);
309 } else if(pid == serverchk_pid) {
310 server_probs = WIFSIGNALED(retstat) || WEXITSTATUS(retstat);
313 char number[NUM_STR_SIZE];
314 char *wait_msg = NULL;
316 ap_snprintf(number, sizeof(number), "%ld", (long)pid);
317 wait_msg = vstralloc("parent: reaped bogus pid ", number, "\n",
319 for(l = 0, n = strlen(wait_msg); l < n; l += s) {
320 if((s = write(mainfd, wait_msg + l, n - l)) < 0) {
321 error("write main file: %s", strerror(errno));
329 /* copy temp output to main output and write tagline */
331 if(do_clientchk && (do_localchk || do_tapechk)) {
332 if(lseek(tempfd, 0, 0) == -1)
333 error("seek temp file: %s", strerror(errno));
335 while((size=read(tempfd, buffer, sizeof(buffer))) > 0) {
336 for(l = 0; l < size; l += s) {
337 if((s = write(mainfd, buffer + l, size - l)) < 0) {
338 error("write main file: %s", strerror(errno));
343 error("read temp file: %s", strerror(errno));
347 version_string = vstralloc("\n",
348 "(brought to you by Amanda ", version(), ")\n",
350 for(l = 0, n = strlen(version_string); l < n; l += s) {
351 if((s = write(mainfd, version_string + l, n - l)) < 0) {
352 error("write main file: %s", strerror(errno));
355 amfree(version_string);
358 amfree(our_feature_string);
359 am_release_feature_set(our_features);
362 malloc_size_2 = malloc_inuse(&malloc_hist_2);
364 if(malloc_size_1 != malloc_size_2) {
365 malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
368 /* send mail if requested, but only if there were problems */
371 #define MAILTO_LIMIT 10
373 if((server_probs || client_probs || alwaysmail) && mailout) {
386 char *extra_info = NULL;
391 char number[NUM_STR_SIZE];
394 if(lseek(mainfd, (off_t)0, SEEK_SET) == -1) {
395 error("lseek main file: %s", strerror(errno));
397 if(alwaysmail && !(server_probs || client_probs)) {
398 subject = stralloc2(getconf_str(CNF_ORG),
399 " AMCHECK REPORT: NO PROBLEMS FOUND");
401 subject = stralloc2(getconf_str(CNF_ORG),
402 " AMANDA PROBLEM: FIX BEFORE RUN, IF POSSIBLE");
405 * Variable arg lists are hard to deal with when we do not know
406 * ourself how many args are involved. Split the address list
407 * and hope there are not more than 9 entries.
409 * Remember that split() returns the original input string in
410 * argv[0], so we have to skip over that.
412 a = (char **) alloc((MAILTO_LIMIT + 1) * sizeof(char *));
413 memset(a, 0, (MAILTO_LIMIT + 1) * sizeof(char *));
418 n = split(getconf_str(CNF_MAILTO), a, MAILTO_LIMIT, " ");
421 if((nullfd = open("/dev/null", O_RDWR)) < 0) {
422 error("nullfd: /dev/null: %s", strerror(errno));
424 mailpid = pipespawn(MAILER, STDIN_PIPE | STDERR_PIPE,
425 &mailfd, &nullfd, &errfd,
428 a[1], a[2], a[3], a[4],
429 a[5], a[6], a[7], a[8], a[9],
433 * There is the potential for a deadlock here since we are writing
434 * to the process and then reading stderr, but in the normal case,
435 * nothing should be coming back to us, and hopefully in error
436 * cases, the pipe will break and we will exit out of the loop.
438 signal(SIGPIPE, SIG_IGN);
439 while((r = fullread(mainfd, buffer, sizeof(buffer))) > 0) {
440 if((w = fullwrite(mailfd, buffer, r)) != r) {
441 if(w < 0 && errno == EPIPE) {
442 strappend(extra_info, "EPIPE writing to mail process\n");
445 error("mailfd write: %s", strerror(errno));
447 error("mailfd write: wrote %d instead of %d", w, r);
452 ferr = fdopen(errfd, "r");
453 for(; (line = agets(ferr)) != NULL; free(line)) {
454 strappend(extra_info, line);
455 strappend(extra_info, "\n");
460 while ((wpid = wait(&retstat)) != -1) {
461 if (WIFSIGNALED(retstat)) {
463 rc = sig = WTERMSIG(retstat);
466 rc = ret = WEXITSTATUS(retstat);
470 strappend(err, "got signal ");
473 strappend(err, "returned ");
475 ap_snprintf(number, sizeof(number), "%d", ret);
476 strappend(err, number);
481 fputs(extra_info, stderr);
484 error("error running mailer %s: %s", MAILER, err);
489 return (server_probs || client_probs);
492 /* --------------------------------------------------- */
494 int nslots, backwards, found, got_match, tapedays;
496 char *first_match_label = NULL, *first_match = NULL, *found_device = NULL;
498 char *searchlabel, *labelstr;
502 int scan_init(rc, ns, bk)
506 error("could not get changer info: %s", changer_resultstr);
514 int taperscan_slot(rc, slotstr, device)
522 fprintf(errf, "%s: fatal slot %s: %s\n",
523 get_pname(), slotstr, changer_resultstr);
527 fprintf(errf, "%s: slot %s: %s\n",
528 get_pname(), slotstr, changer_resultstr);
532 if((errstr = tape_rdlabel(device, &datestamp, &label)) != NULL) {
533 fprintf(errf, "%s: slot %s: %s\n", get_pname(), slotstr, errstr);
535 /* got an amanda tape */
536 fprintf(errf, "%s: slot %s: date %-8s label %s",
537 get_pname(), slotstr, datestamp, label);
538 if(searchlabel != NULL
539 && (strcmp(label, FAKE_LABEL) == 0
540 || strcmp(label, searchlabel) == 0)) {
541 /* it's the one we are looking for, stop here */
542 fprintf(errf, " (exact label match)\n");
543 found_device = newstralloc(found_device, device);
547 else if(!match(labelstr, label))
548 fprintf(errf, " (no match)\n");
550 /* not an exact label match, but a labelstr match */
551 /* check against tape list */
552 tp = lookup_tapelabel(label);
554 fprintf(errf, " (Not in tapelist)\n");
555 else if(!reusable_tape(tp))
556 fprintf(errf, " (active tape)\n");
557 else if(got_match == 0 && tp->datestamp == 0) {
559 first_match = newstralloc(first_match, slotstr);
560 first_match_label = newstralloc(first_match_label, label);
561 fprintf(errf, " (new tape)\n");
563 found_device = newstralloc(found_device, device);
567 fprintf(errf, " (labelstr match)\n");
570 first_match = newstralloc(first_match, slotstr);
571 first_match_label = newstralloc(first_match_label, label);
572 fprintf(errf, " (first labelstr match)\n");
573 if(!backwards || !searchlabel) {
575 found_device = newstralloc(found_device, device);
587 char *outslot = NULL;
589 if((tp = lookup_last_reusable_tape(0)) == NULL)
592 searchlabel = tp->label;
597 changer_find(scan_init, taperscan_slot, searchlabel);
599 if(found == 2 || found == 3)
600 searchlabel = first_match_label;
601 else if(!found && got_match) {
602 searchlabel = first_match_label;
603 amfree(found_device);
604 if(changer_loadslot(first_match, &outslot, &found_device) == 0) {
609 changer_resultstr = newvstralloc(changer_resultstr,
610 "label ", searchlabel,
611 " or new tape not found in rack",
614 changer_resultstr = newstralloc(changer_resultstr,
615 "new tape not found in rack");
620 return found ? found_device : NULL;
623 int test_server_pgm(outf, dir, pgm, suid, dumpuid)
633 pgm = vstralloc(dir, "/", pgm, versionsuffix(), NULL);
634 if(stat(pgm, &statbuf) == -1) {
635 fprintf(outf, "ERROR: program %s: does not exist\n",
638 } else if (!S_ISREG(statbuf.st_mode)) {
639 fprintf(outf, "ERROR: program %s: not a file\n",
642 } else if (access(pgm, X_OK) == -1) {
643 fprintf(outf, "ERROR: program %s: not executable\n",
648 && (statbuf.st_uid != 0 || (statbuf.st_mode & 04000) == 0)) {
649 fprintf(outf, "WARNING: program %s: not setuid-root\n",
656 int start_server_check(fd, do_localchk, do_tapechk)
659 char *errstr, *tapename;
660 generic_fs_stats_t fs;
665 int confbad = 0, tapebad = 0, disklow = 0, logbad = 0;
666 int userbad = 0, infobad = 0, indexbad = 0, pgmbad = 0;
667 int testtape = do_tapechk;
669 switch(pid = fork()) {
670 case -1: error("could not fork server check: %s", strerror(errno));
679 set_pname("amcheck-server");
685 if((outf = fdopen(fd, "w")) == NULL)
686 error("fdopen %d: %s", fd, strerror(errno));
689 fprintf(outf, "Amanda Tape Server Host Check\n");
690 fprintf(outf, "-----------------------------\n");
693 * Check various server side config file settings.
701 ColumnSpec = getconf_str(CNF_COLUMNSPEC);
702 if(SetColumDataFromString(ColumnData, ColumnSpec, &errstr) < 0) {
703 fprintf(outf, "ERROR: %s\n", errstr);
707 tp = lookup_tapetype(getconf_str(CNF_TAPETYPE));
708 lbl_templ = tp->lbl_templ;
709 if(strcmp(lbl_templ, "") != 0) {
710 if(strchr(lbl_templ, '/') == NULL) {
711 lbl_templ = stralloc2(config_dir, lbl_templ);
713 lbl_templ = stralloc(lbl_templ);
715 if(access(lbl_templ, R_OK) == -1) {
717 "ERROR: cannot access lbl_templ file %s: %s\n",
723 fprintf(outf, "ERROR: lbl_templ set but no LPRCMD defined, you should reconfigure amanda\n and make sure it find a lpr or lp command.\n");
730 * Look up the programs used on the server side.
733 if(access(libexecdir, X_OK) == -1) {
734 fprintf(outf, "ERROR: program dir %s: not accessible\n",
739 || test_server_pgm(outf, libexecdir, "planner",
742 || test_server_pgm(outf, libexecdir, "dumper",
745 || test_server_pgm(outf, libexecdir, "driver",
748 || test_server_pgm(outf, libexecdir, "taper",
751 || test_server_pgm(outf, libexecdir, "amtrmidx",
754 || test_server_pgm(outf, libexecdir, "amlogroll",
757 if(access(sbindir, X_OK) == -1) {
758 fprintf(outf, "ERROR: program dir %s: not accessible\n",
763 || test_server_pgm(outf, sbindir, "amgetconf",
766 || test_server_pgm(outf, sbindir, "amcheck",
769 || test_server_pgm(outf, sbindir, "amdump",
772 || test_server_pgm(outf, sbindir, "amreport",
775 if(access(COMPRESS_PATH, X_OK) == -1) {
776 fprintf(outf, "WARNING: %s is not executable, server-compression and indexing will not work\n",
782 * Check that the directory for the tapelist file is writable, as well
783 * as the tapelist file itself (if it already exists). Also, check for
784 * a "hold" file (just because it is convenient to do it here) and warn
785 * if tapedev is set to the null device.
788 if(do_localchk || do_tapechk) {
795 conf_tapelist=getconf_str(CNF_TAPELIST);
796 if (*conf_tapelist == '/') {
797 tapefile = stralloc(conf_tapelist);
799 tapefile = stralloc2(config_dir, conf_tapelist);
802 * XXX There Really Ought to be some error-checking here... dhw
804 tape_dir = stralloc(tapefile);
805 if ((lastslash = strrchr((const char *)tape_dir, '/')) != NULL) {
808 * else whine Really Loudly about a path with no slashes??!?
811 if(access(tape_dir, W_OK) == -1) {
812 fprintf(outf, "ERROR: tapelist dir %s: not writable\n", tape_dir);
814 } else if(access(tapefile, F_OK) == 0 && access(tapefile, W_OK) != 0) {
815 fprintf(outf, "ERROR: tape list %s: not writable\n", tapefile);
817 } else if(read_tapelist(tapefile)) {
818 fprintf(outf, "ERROR: tape list %s: parse error\n", tapefile);
821 holdfile = vstralloc(config_dir, "/", "hold", NULL);
822 if(access(holdfile, F_OK) != -1) {
823 fprintf(outf, "WARNING: hold file %s exists\n", holdfile);
828 tapename = getconf_str(CNF_TAPEDEV);
829 if (strncmp(tapename, "null:", 5) == 0) {
831 "WARNING: tapedev is %s, dumps will be thrown away\n",
838 /* check available disk space */
841 for(hdp = holdingdisks; hdp != NULL; hdp = hdp->next) {
842 if(get_fs_stats(hdp->diskdir, &fs) == -1) {
843 fprintf(outf, "ERROR: holding disk %s: statfs: %s\n",
844 hdp->diskdir, strerror(errno));
847 else if(access(hdp->diskdir, W_OK) == -1) {
848 fprintf(outf, "ERROR: holding disk %s: not writable: %s\n",
849 hdp->diskdir, strerror(errno));
852 else if(fs.avail == -1) {
854 "WARNING: holding disk %s: available space unknown (%ld KB requested)\n",
855 hdp->diskdir, (long)hdp->disksize);
858 else if(hdp->disksize > 0) {
859 if(fs.avail < hdp->disksize) {
861 "WARNING: holding disk %s: only %ld KB free (%ld KB requested)\n",
862 hdp->diskdir, (long)fs.avail, (long)hdp->disksize);
867 "Holding disk %s: %ld KB disk space available, that's plenty\n",
868 hdp->diskdir, fs.avail);
871 if(fs.avail < -hdp->disksize) {
873 "WARNING: holding disk %s: only %ld KB free, using nothing\n",
874 hdp->diskdir, fs.avail);
879 "Holding disk %s: %ld KB disk space available, using %ld KB\n",
880 hdp->diskdir, fs.avail, fs.avail + hdp->disksize);
885 /* check that the log file is writable if it already exists */
891 struct stat stat_old;
893 conf_logdir = getconf_str(CNF_LOGDIR);
894 if (*conf_logdir == '/') {
895 conf_logdir = stralloc(conf_logdir);
897 conf_logdir = stralloc2(config_dir, conf_logdir);
899 logfile = vstralloc(conf_logdir, "/log", NULL);
901 if(access(conf_logdir, W_OK) == -1) {
902 fprintf(outf, "ERROR: log dir %s: not writable\n", conf_logdir);
906 if(access(logfile, F_OK) == 0) {
909 if(access(logfile, W_OK) != 0)
910 fprintf(outf, "ERROR: log file %s: not writable\n",
914 olddir = vstralloc(conf_logdir, "/oldlog", NULL);
915 if (stat(olddir,&stat_old) == 0) { /* oldlog exist */
916 if(!(S_ISDIR(stat_old.st_mode))) {
917 fprintf(outf, "ERROR: oldlog directory \"%s\" is not a directory\n", olddir);
919 if(access(olddir, W_OK) == -1) {
920 fprintf(outf, "ERROR: oldlog dir %s: not writable\n", olddir);
923 else if(lstat(olddir,&stat_old) == 0) {
924 fprintf(outf, "ERROR: oldlog directory \"%s\" is not a directory\n", olddir);
928 logfile = newvstralloc(logfile, conf_logdir, "/amdump", NULL);
929 if (access(logfile, F_OK) == 0) {
940 /* check that the tape is a valid amanda tape */
942 tapedays = getconf_int(CNF_TAPECYCLE);
943 labelstr = getconf_str(CNF_LABELSTR);
944 tapename = getconf_str(CNF_TAPEDEV);
946 if (!getconf_seen(CNF_TPCHANGER) && getconf_int(CNF_RUNTAPES) != 1) {
948 "WARNING: if a tape changer is not available, runtapes must be set to 1\n");
951 if(changer_init() && (tapename = taper_scan()) == NULL) {
952 fprintf(outf, "ERROR: %s\n", changer_resultstr);
954 } else if(tape_access(tapename,F_OK|R_OK|W_OK) == -1) {
955 fprintf(outf, "ERROR: %s: %s\n", tapename, strerror(errno));
957 } else if((errstr = tape_rdlabel(tapename, &datestamp, &label)) != NULL) {
958 fprintf(outf, "ERROR: %s: %s\n", tapename, errstr);
960 } else if(strcmp(label, FAKE_LABEL) != 0) {
961 if(!match(labelstr, label)) {
962 fprintf(outf, "ERROR: label %s doesn't match labelstr \"%s\"\n",
967 tp = lookup_tapelabel(label);
969 fprintf(outf, "ERROR: label %s match labelstr but it not listed in the tapelist file.\n", label);
972 else if(tp != NULL && !reusable_tape(tp)) {
973 fprintf(outf, "ERROR: cannot overwrite active tape %s\n",
982 tape_t *exptape = lookup_last_reusable_tape(0);
983 fprintf(outf, " (expecting ");
984 if(exptape != NULL) fprintf(outf, "tape %s or ", exptape->label);
985 fprintf(outf, "a new tape)\n");
988 if(!tapebad && overwrite) {
989 if((errstr = tape_writable(tapename)) != NULL) {
991 "ERROR: tape %s label ok, but is not writable\n",
995 else fprintf(outf, "Tape %s is writable\n", label);
997 else fprintf(outf, "NOTE: skipping tape-writable test\n");
1000 fprintf(outf, "Tape %s label ok\n", label);
1001 } else if (do_tapechk) {
1002 fprintf(outf, "WARNING: skipping tape test because amdump or amflush seem to be running\n");
1003 fprintf(outf, "WARNING: if they are not, you must run amcleanup\n");
1005 fprintf(outf, "NOTE: skipping tape checks\n");
1009 * See if the information file and index directory for each client
1010 * and disk is OK. Since we may be seeing clients and/or disks for
1011 * the first time, these are just warnings, not errors.
1014 char *conf_infofile;
1015 char *conf_indexdir;
1016 char *hostinfodir = NULL;
1017 char *hostindexdir = NULL;
1018 char *diskdir = NULL;
1019 char *infofile = NULL;
1020 struct stat statbuf;
1023 int indexdir_checked = 0;
1024 int hostindexdir_checked = 0;
1027 int conf_tapecycle, conf_runspercycle;
1029 conf_tapecycle = getconf_int(CNF_TAPECYCLE);
1030 conf_runspercycle = getconf_int(CNF_RUNSPERCYCLE);
1032 if(conf_tapecycle <= conf_runspercycle) {
1033 fprintf(outf, "WARNING: tapecycle (%d) <= runspercycle (%d).\n",
1034 conf_tapecycle, conf_runspercycle);
1037 conf_infofile = stralloc(getconf_str(CNF_INFOFILE));
1038 if (*conf_infofile != '/') {
1039 char *ci = stralloc2(config_dir, conf_infofile);
1040 amfree(conf_infofile);
1043 conf_indexdir = stralloc(getconf_str(CNF_INDEXDIR));
1044 if (*conf_indexdir != '/') {
1045 char *ci = stralloc2(config_dir, conf_indexdir);
1046 amfree(conf_indexdir);
1050 if(stat(conf_infofile, &statbuf) == -1) {
1051 fprintf(outf, "NOTE: info dir %s: does not exist\n", conf_infofile);
1052 fprintf(outf, "NOTE: it will be created on the next run\n");
1053 amfree(conf_infofile);
1054 } else if (!S_ISDIR(statbuf.st_mode)) {
1055 fprintf(outf, "ERROR: info dir %s: not a directory\n", conf_infofile);
1056 amfree(conf_infofile);
1058 } else if (access(conf_infofile, W_OK) == -1) {
1059 fprintf(outf, "ERROR: info dir %s: not writable\n", conf_infofile);
1060 amfree(conf_infofile);
1063 strappend(conf_infofile, "/");
1066 while(!empty(*origqp)) {
1067 hostp = origqp->head->host;
1068 host = sanitise_filename(hostp->hostname);
1071 hostinfodir = newstralloc2(hostinfodir, conf_infofile, host);
1072 if(stat(hostinfodir, &statbuf) == -1) {
1073 fprintf(outf, "NOTE: info dir %s: does not exist\n",
1075 amfree(hostinfodir);
1076 } else if (!S_ISDIR(statbuf.st_mode)) {
1077 fprintf(outf, "ERROR: info dir %s: not a directory\n",
1079 amfree(hostinfodir);
1081 } else if (access(hostinfodir, W_OK) == -1) {
1082 fprintf(outf, "ERROR: info dir %s: not writable\n",
1084 amfree(hostinfodir);
1087 strappend(hostinfodir, "/");
1091 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1092 disk = sanitise_filename(dp->name);
1095 diskdir = newstralloc2(diskdir, hostinfodir, disk);
1096 infofile = vstralloc(diskdir, "/", "info", NULL);
1097 if(stat(diskdir, &statbuf) == -1) {
1098 fprintf(outf, "NOTE: info dir %s: does not exist\n",
1100 } else if (!S_ISDIR(statbuf.st_mode)) {
1101 fprintf(outf, "ERROR: info dir %s: not a directory\n",
1104 } else if (access(diskdir, W_OK) == -1) {
1105 fprintf(outf, "ERROR: info dir %s: not writable\n",
1108 } else if(stat(infofile, &statbuf) == -1) {
1109 fprintf(outf, "WARNING: info file %s: does not exist\n",
1111 } else if (!S_ISREG(statbuf.st_mode)) {
1112 fprintf(outf, "ERROR: info file %s: not a file\n",
1115 } else if (access(infofile, R_OK) == -1) {
1116 fprintf(outf, "ERROR: info file %s: not readable\n",
1124 if(! indexdir_checked) {
1125 if(stat(conf_indexdir, &statbuf) == -1) {
1126 fprintf(outf, "NOTE: index dir %s: does not exist\n",
1128 amfree(conf_indexdir);
1129 } else if (!S_ISDIR(statbuf.st_mode)) {
1130 fprintf(outf, "ERROR: index dir %s: not a directory\n",
1132 amfree(conf_indexdir);
1134 } else if (access(conf_indexdir, W_OK) == -1) {
1135 fprintf(outf, "ERROR: index dir %s: not writable\n",
1137 amfree(conf_indexdir);
1140 strappend(conf_indexdir, "/");
1142 indexdir_checked = 1;
1145 if(! hostindexdir_checked) {
1146 hostindexdir = stralloc2(conf_indexdir, host);
1147 if(stat(hostindexdir, &statbuf) == -1) {
1148 fprintf(outf, "NOTE: index dir %s: does not exist\n",
1150 amfree(hostindexdir);
1151 } else if (!S_ISDIR(statbuf.st_mode)) {
1152 fprintf(outf, "ERROR: index dir %s: not a directory\n",
1154 amfree(hostindexdir);
1156 } else if (access(hostindexdir, W_OK) == -1) {
1157 fprintf(outf, "ERROR: index dir %s: not writable\n",
1159 amfree(hostindexdir);
1162 strappend(hostindexdir, "/");
1164 hostindexdir_checked = 1;
1167 diskdir = newstralloc2(diskdir, hostindexdir, disk);
1168 if(stat(diskdir, &statbuf) == -1) {
1169 fprintf(outf, "NOTE: index dir %s: does not exist\n",
1171 } else if (!S_ISDIR(statbuf.st_mode)) {
1172 fprintf(outf, "ERROR: index dir %s: not a directory\n",
1175 } else if (access(diskdir, W_OK) == -1) {
1176 fprintf(outf, "ERROR: index dir %s: is not writable\n",
1184 remove_disk(origqp, dp);
1187 amfree(hostindexdir);
1188 hostindexdir_checked = 0;
1191 amfree(hostinfodir);
1192 amfree(conf_infofile);
1193 amfree(conf_indexdir);
1199 amfree(config_name);
1201 fprintf(outf, "Server check took %s seconds\n", walltime_str(curclock()));
1205 malloc_size_2 = malloc_inuse(&malloc_hist_2);
1207 if(malloc_size_1 != malloc_size_2) {
1208 malloc_list(fd, malloc_hist_1, malloc_hist_2);
1223 /* --------------------------------------------------- */
1229 #ifdef KRB4_SECURITY
1233 static void handle_response P((proto_t *p, pkt_t *pkt));
1235 #define HOST_READY ((void *)0) /* must be 0 */
1236 #define HOST_ACTIVE ((void *)1)
1237 #define HOST_DONE ((void *)2)
1239 #define DISK_READY ((void *)0) /* must be 0 */
1240 #define DISK_ACTIVE ((void *)1)
1241 #define DISK_DONE ((void *)2)
1243 int start_host(hostp)
1251 char number[NUM_STR_SIZE];
1253 if(hostp->up != HOST_READY) {
1258 * The first time through here we send a "noop" request. This will
1259 * return the feature list from the client if it supports that.
1260 * If it does not, handle_result() will set the feature list to an
1261 * empty structure. In either case, we do the disks on the second
1262 * (and subsequent) pass(es).
1265 if(hostp->features != NULL) { /* selfcheck service */
1266 int has_features = am_has_feature(hostp->features,
1267 fe_req_options_features);
1268 int has_hostname = am_has_feature(hostp->features,
1269 fe_req_options_hostname);
1270 int has_maxdumps = am_has_feature(hostp->features,
1271 fe_req_options_maxdumps);
1273 if(!am_has_feature(hostp->features, fe_selfcheck_req) &&
1274 !am_has_feature(hostp->features, fe_selfcheck_req_device)) {
1276 "ERROR: Client %s does not support selfcheck REQ packet.\n",
1279 if(!am_has_feature(hostp->features, fe_selfcheck_rep)) {
1281 "ERROR: Client %s does not support selfcheck REP packet.\n",
1284 if(!am_has_feature(hostp->features, fe_sendsize_req_options) &&
1285 !am_has_feature(hostp->features, fe_sendsize_req_no_options) &&
1286 !am_has_feature(hostp->features, fe_sendsize_req_device)) {
1288 "ERROR: Client %s does not support sendsize REQ packet.\n",
1291 if(!am_has_feature(hostp->features, fe_sendsize_rep)) {
1293 "ERROR: Client %s does not support sendsize REP packet.\n",
1296 if(!am_has_feature(hostp->features, fe_sendbackup_req) &&
1297 !am_has_feature(hostp->features, fe_sendbackup_req_device)) {
1299 "ERROR: Client %s does not support sendbackup REQ packet.\n",
1302 if(!am_has_feature(hostp->features, fe_sendbackup_rep)) {
1304 "ERROR: Client %s does not support sendbackup REP packet.\n",
1308 ap_snprintf(number, sizeof(number), "%d", hostp->maxdumps);
1309 req = vstralloc("SERVICE ", "selfcheck", "\n",
1311 has_features ? "features=" : "",
1312 has_features ? our_feature_string : "",
1313 has_features ? ";" : "",
1314 has_maxdumps ? "maxdumps=" : "",
1315 has_maxdumps ? number : "",
1316 has_maxdumps ? ";" : "",
1317 has_hostname ? "hostname=" : "",
1318 has_hostname ? hostp->hostname : "",
1319 has_hostname ? ";" : "",
1323 req_len = strlen(req);
1324 req_len += 128; /* room for SECURITY ... */
1325 req_len += 256; /* room for non-disk answers */
1326 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1331 if(dp->todo == 0) continue;
1333 if(dp->up != DISK_READY) {
1336 o = optionstr(dp, hostp->features, outf);
1339 if(!am_has_feature(hostp->features, fe_selfcheck_req_device)) {
1341 "ERROR: %s:%s (%s): selfcheck does not support device.\n",
1342 hostp->hostname, dp->name, dp->device);
1344 if(!am_has_feature(hostp->features, fe_sendsize_req_device)) {
1346 "ERROR: %s:%s (%s): sendsize does not support device.\n",
1347 hostp->hostname, dp->name, dp->device);
1349 if(!am_has_feature(hostp->features, fe_sendbackup_req_device)) {
1351 "ERROR: %s:%s (%s): sendbackup does not support device.\n",
1352 hostp->hostname, dp->name, dp->device);
1355 if(strcmp(dp->program, "DUMP") == 0 &&
1356 !am_has_feature(hostp->features, fe_program_dump)) {
1357 fprintf(outf, "ERROR: %s:%s does not support DUMP.\n",
1358 hostp->hostname, dp->name);
1360 if(strcmp(dp->program, "GNUTAR") == 0 &&
1361 !am_has_feature(hostp->features, fe_program_gnutar)) {
1362 fprintf(outf, "ERROR: %s:%s does not support GNUTAR.\n",
1363 hostp->hostname, dp->name);
1365 l = vstralloc(dp->program,
1369 dp->device ? dp->device : "",
1377 * Allow 2X for error response in return packet.
1379 if(req_len + l_len > MAX_DGRAM / 2) {
1386 dp->up = DISK_ACTIVE;
1391 else { /* noop service */
1392 req = vstralloc("SERVICE ", "noop", "\n",
1394 "features=", our_feature_string, ";",
1397 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1398 if(dp->todo == 0) continue;
1400 if(dp->up != DISK_READY) {
1406 if(disk_count == 0) {
1408 hostp->up = HOST_DONE;
1412 #ifdef KRB4_SECURITY
1413 if(hostp->disks->auth == AUTH_KRB4)
1414 rc = make_krb_request(hostp->hostname, kamanda_port, req,
1415 hostp, conf_ctimeout, handle_response);
1418 rc = make_request(hostp->hostname, amanda_port, req,
1419 hostp, conf_ctimeout, handle_response);
1421 req = NULL; /* do not own this any more */
1424 /* couldn't resolve hostname */
1426 "ERROR: %s: could not resolve hostname\n", hostp->hostname);
1428 hostp->up = HOST_DONE;
1430 hostp->up = HOST_ACTIVE;
1435 int start_client_checks(fd)
1441 struct servent *amandad;
1444 switch(pid = fork()) {
1445 case -1: error("could not fork client check: %s", strerror(errno));
1454 set_pname("amcheck-clients");
1458 if((outf = fdopen(fd, "w")) == NULL)
1459 error("fdopen %d: %s", fd, strerror(errno));
1462 fprintf(outf, "\nAmanda Backup Client Hosts Check\n");
1463 fprintf(outf, "--------------------------------\n");
1465 #ifdef KRB4_SECURITY
1466 kerberos_service_init();
1469 proto_init(msg->socket, time(0), 1024);
1471 /* get remote service port */
1472 if((amandad = getservbyname(AMANDA_SERVICE_NAME, "udp")) == NULL)
1473 amanda_port = AMANDA_SERVICE_DEFAULT;
1475 amanda_port = ntohs(amandad->s_port);
1477 #ifdef KRB4_SECURITY
1478 if((amandad = getservbyname(KAMANDA_SERVICE_NAME, "udp")) == NULL)
1479 kamanda_port = KAMANDA_SERVICE_DEFAULT;
1481 kamanda_port = ntohs(amandad->s_port);
1484 hostcount = remote_errors = 0;
1486 for(dp = origqp->head; dp != NULL; dp = dp->next) {
1488 if(hostp->up == HOST_READY) {
1489 if(start_host(hostp) == 1) {
1500 "Client check: %d host%s checked in %s seconds, %d problem%s found\n",
1501 hostcount, (hostcount == 1) ? "" : "s",
1502 walltime_str(curclock()),
1503 remote_errors, (remote_errors == 1) ? "" : "s");
1507 amfree(config_name);
1509 malloc_size_2 = malloc_inuse(&malloc_hist_2);
1511 if(malloc_size_1 != malloc_size_2) {
1512 malloc_list(fd, malloc_hist_1, malloc_hist_2);
1515 exit(userbad || remote_errors > 0);
1520 static void handle_response(p, pkt)
1532 hostp = (host_t *) p->datap;
1533 hostp->up = HOST_READY;
1535 if(p->state == S_FAILED && pkt == NULL) {
1536 if(p->prevstate == S_REPWAIT) {
1538 "WARNING: %s: selfcheck reply timed out.\n",
1543 "WARNING: %s: selfcheck request timed out. Host down?\n",
1547 hostp->up = HOST_DONE;
1551 #ifdef KRB4_SECURITY
1552 if(hostp->disks->auth == AUTH_KRB4 &&
1553 !check_mutual_authenticator(host2key(hostp->hostname), pkt, p)) {
1554 fprintf(outf, "ERROR: %s [mutual-authentication failed]\n",
1557 hostp->up = HOST_DONE;
1563 fprintf(errf, "got %sresponse from %s:\n----\n%s----\n\n",
1564 (p->state == S_FAILED) ? "NAK " : "", hostp->hostname, pkt->body);
1572 if (s[-2] == '\n') {
1576 #define sc "OPTIONS "
1577 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
1580 #define sc "features="
1581 t = strstr(line, sc);
1582 if(t != NULL && (isspace((int)t[-1]) || t[-1] == ';')) {
1585 am_release_feature_set(hostp->features);
1586 if((hostp->features = am_string_to_feature(t)) == NULL) {
1587 fprintf(outf, "ERROR: %s: bad features value: %s\n",
1588 hostp->hostname, line);
1596 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
1602 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
1603 t = line + sizeof(sc)-1;
1607 skip_whitespace(t, tch);
1609 * If the "error" is that the "noop" service is unknown, it
1610 * just means the client is "old" (does not support the service).
1611 * We can ignore this.
1613 if(hostp->features == NULL
1614 && p->state == S_FAILED
1615 && (strcmp(t - 1, "unknown service: noop") == 0
1616 || strcmp(t - 1, "noop: invalid service") == 0)) {
1618 fprintf(outf, "ERROR: %s%s: %s\n",
1619 (p->state == S_FAILED) ? "NAK " : "",
1623 hostp->up = HOST_DONE;
1628 fprintf(outf, "ERROR: %s: unknown response: %s\n",
1629 hostp->hostname, line);
1631 hostp->up = HOST_DONE;
1633 if(hostp->up == HOST_READY && hostp->features == NULL) {
1635 * The client does not support the features list, so give it an
1638 dbprintf(("%s: no feature set from host %s\n",
1639 debug_prefix_time(NULL), hostp->hostname));
1640 hostp->features = am_set_default_feature_set();
1642 for(dp = hostp->disks; dp != NULL; dp = dp->hostnext) {
1643 if(dp->up == DISK_ACTIVE) {