2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 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 * Author: James da Silva, Systems Design and Analysis Group
24 * Computer Science Department
25 * University of Maryland at College Park
28 * $Id: selfcheck.c,v 1.40.2.3.4.4.2.22.2.2 2004/08/12 12:30:53 martinea Exp $
30 * do self-check and send back any error messages
38 #include "amandates.h"
40 #include "pipespawn.h"
41 #include "amfeatures.h"
42 #include "client_util.h"
55 int need_xfsrestore=0;
60 int need_compress_path=0;
63 static am_feature_t *our_features = NULL;
64 static char *our_feature_string = NULL;
65 static g_option_t *g_options = NULL;
68 int main P((int argc, char **argv));
70 static void check_options P((char *program, char *calcprog, char *disk, char *device, option_t *options));
71 static void check_disk P((char *program, char *calcprog, char *disk, char *amdevice, int level));
72 static void check_overall P((void));
73 static void check_access P((char *filename, int mode));
74 static void check_file P((char *filename, int mode));
75 static void check_dir P((char *dirname, int mode));
76 static void check_suid P((char *filename));
77 static void check_space P((char *dir, long kbytes));
86 char *calcprog = NULL;
90 char *err_extra = NULL;
94 unsigned long malloc_hist_1, malloc_size_1;
95 unsigned long malloc_hist_2, malloc_size_2;
100 for(fd = 3; fd < FD_SETSIZE; fd++) {
102 * Make sure nobody spoofs us with a lot of extra open files
103 * that would cause an open we do to get a very high file
104 * descriptor, which in turn might be used as an index into
105 * an array (e.g. an fd_set).
112 set_pname("selfcheck");
114 malloc_size_1 = malloc_inuse(&malloc_hist_1);
116 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
119 dbprintf(("%s: version %s\n", argv[0], version()));
121 our_features = am_init_feature_set();
122 our_feature_string = am_feature_to_string(our_features);
124 /* handle all service requests */
126 for(; (line = agets(stdin)) != NULL; free(line)) {
127 #define sc "OPTIONS "
128 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
130 g_options = parse_g_options(line+8, 1);
131 if(!g_options->hostname) {
132 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
133 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
134 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
138 if(am_has_feature(g_options->features, fe_rep_options_features)) {
139 printf("features=%s;", our_feature_string);
141 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
142 printf("hostname=%s;", g_options->hostname);
152 skip_whitespace(s, ch); /* find program name */
154 goto err; /* no program */
157 skip_non_whitespace(s, ch);
158 s[-1] = '\0'; /* terminate the program name */
160 if(strncmp(program, "CALCSIZE", 8) == 0) {
161 skip_whitespace(s, ch); /* find program name */
163 goto err; /* no program */
166 skip_non_whitespace(s, ch);
173 skip_whitespace(s, ch); /* find disk name */
175 goto err; /* no disk */
178 skip_non_whitespace(s, ch);
179 s[-1] = '\0'; /* terminate the disk name */
181 skip_whitespace(s, ch); /* find the device or level */
183 goto err; /* no device or level */
185 if(!isdigit((int)s[-1])) {
187 skip_non_whitespace(s, ch);
188 s[-1] = '\0'; /* terminate the device */
189 device = stralloc(fp);
190 skip_whitespace(s, ch); /* find level number */
193 device = stralloc(disk);
196 /* find level number */
197 if (ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
198 goto err; /* bad level */
202 skip_whitespace(s, ch);
203 #define sc "OPTIONS "
204 if (ch && strncmp (s - 1, sc, sizeof(sc)-1) == 0) {
208 skip_whitespace(s, ch); /* find the option string */
210 goto err; /* bad options string */
213 skip_non_whitespace(s, ch);
214 s[-1] = '\0'; /* terminate the options */
215 options = parse_options(optstr, disk, device, g_options->features, 1);
216 check_options(program, calcprog, disk, device, options);
217 check_disk(program, calcprog, disk, device, level);
218 free_sl(options->exclude_file);
219 free_sl(options->exclude_list);
220 free_sl(options->include_file);
221 free_sl(options->include_list);
222 amfree(options->str);
224 } else if (ch == '\0') {
225 /* check all since no option */
238 need_compress_path=1;
240 check_disk(program, calcprog, disk, device, level);
242 goto err; /* bad syntax */
250 amfree(our_feature_string);
251 am_release_feature_set(our_features);
253 am_release_feature_set(g_options->features);
254 g_options->features = NULL;
255 amfree(g_options->str);
256 amfree(g_options->hostname);
259 malloc_size_2 = malloc_inuse(&malloc_hist_2);
261 if(malloc_size_1 != malloc_size_2) {
262 #if defined(USE_DBMALLOC)
265 malloc_list(dbfd(), malloc_hist_1, malloc_hist_2);
273 printf("ERROR [BOGUS REQUEST PACKET]\n");
274 dbprintf(("%s: REQ packet is bogus%s%s\n",
275 debug_prefix_time(NULL),
276 err_extra ? ": " : "",
277 err_extra ? err_extra : ""));
284 check_options(program, calcprog, disk, device, options)
285 char *program, *calcprog, *disk, *device;
288 char *myprogram = program;
290 if(strcmp(myprogram,"CALCSIZE") == 0) {
293 char *file_exclude = NULL;
294 char *file_include = NULL;
296 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
297 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
298 if(options->include_file) nb_include += options->include_file->nb_element;
299 if(options->include_list) nb_include += options->include_list->nb_element;
301 if(nb_exclude > 0) file_exclude = build_exclude(disk, device, options, 1);
302 if(nb_include > 0) file_include = build_include(disk, device, options, 1);
304 amfree(file_exclude);
305 amfree(file_include);
308 myprogram = calcprog;
311 if(strcmp(myprogram,"GNUTAR") == 0) {
313 if(disk[0] == '/' && disk[1] == '/') {
314 if(options->exclude_file && options->exclude_file->nb_element > 1) {
315 printf("ERROR [samba support only one exclude file]\n");
317 if(options->exclude_list && options->exclude_list->nb_element > 0 &&
318 options->exclude_optional==0) {
319 printf("ERROR [samba does not support exclude list]\n");
321 if(options->include_file && options->include_file->nb_element > 0) {
322 printf("ERROR [samba does not support include file]\n");
324 if(options->include_list && options->include_list->nb_element > 0 &&
325 options->include_optional==0) {
326 printf("ERROR [samba does not support include list]\n");
333 char *file_exclude = NULL;
334 char *file_include = NULL;
336 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
337 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
338 if(options->include_file) nb_include += options->include_file->nb_element;
339 if(options->include_list) nb_include += options->include_list->nb_element;
341 if(nb_exclude > 0) file_exclude = build_exclude(disk, device, options, 1);
342 if(nb_include > 0) file_include = build_include(disk, device, options, 1);
344 amfree(file_exclude);
345 amfree(file_include);
351 if(strcmp(myprogram,"DUMP") == 0) {
352 if(options->exclude_file && options->exclude_file->nb_element > 0) {
353 printf("ERROR [DUMP does not support exclude file]\n");
355 if(options->exclude_list && options->exclude_list->nb_element > 0) {
356 printf("ERROR [DUMP does not support exclude list]\n");
358 if(options->include_file && options->include_file->nb_element > 0) {
359 printf("ERROR [DUMP does not support include file]\n");
361 if(options->include_list && options->include_list->nb_element > 0) {
362 printf("ERROR [DUMP does not support include list]\n");
370 if (strcmp(amname_to_fstype(disk), "advfs") == 0)
377 if (options->createindex)
384 if (strcmp(amname_to_fstype(disk), "xfs") == 0)
391 if (options->createindex)
398 if (strcmp(amname_to_fstype(disk), "vxfs") == 0)
404 if (options->createindex)
411 if (options->createindex)
415 /* AIX backup program */
417 if (options->createindex)
421 if(options->compress == COMPR_BEST || options->compress == COMPR_FAST)
422 need_compress_path=1;
425 static void check_disk(program, calcprog, disk, amdevice, level)
426 char *program, *calcprog, *disk, *amdevice;
431 char *user_and_password = NULL, *domain = NULL;
432 char *share = NULL, *subdir = NULL;
437 char *extra_info = NULL;
438 char *myprogram = program;
440 if(strcmp(myprogram,"CALCSIZE") == 0) {
441 if(amdevice[0] == '/' && amdevice[1] == '/') {
442 err = vstralloc("Can't use CALCSIZE for samba estimate,",
448 myprogram = calcprog;
451 dbprintf(("%s: checking disk %s\n", debug_prefix_time(NULL), disk));
453 if (strcmp(myprogram, "GNUTAR") == 0) {
454 if(amdevice[0] == '/' && amdevice[1] == '/') {
456 int nullfd, checkerr;
462 char number[NUM_STR_SIZE];
471 parsesharename(amdevice, &share, &subdir);
473 err = stralloc2("cannot parse for share/subdir disk entry ", amdevice);
476 if ((subdir) && (SAMBA_VERSION < 2)) {
477 err = vstralloc("subdirectory specified for share '",
479 "' but samba not v2 or better",
483 if ((user_and_password = findpass(share, &domain)) == NULL) {
484 err = stralloc2("cannot find password for ", amdevice);
487 lpass = strlen(user_and_password);
488 if ((pwtext = strchr(user_and_password, '%')) == NULL) {
489 err = stralloc2("password field not \'user%pass\' for ", amdevice);
493 pwtext_len = strlen(pwtext);
494 if ((device = makesharename(share, 0)) == NULL) {
495 err = stralloc2("cannot make share name of ", share);
499 nullfd = open("/dev/null", O_RDWR);
500 if (pwtext_len > 0) {
501 pw_fd_env = "PASSWD_FD";
503 pw_fd_env = "dummy_PASSWD_FD";
505 checkpid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE,
506 &nullfd, &nullfd, &checkerr,
507 pw_fd_env, &passwdfd,
510 *user_and_password ? "-U" : skip_argument,
511 *user_and_password ? user_and_password : skip_argument,
513 domain ? "-W" : skip_argument,
514 domain ? domain : skip_argument,
515 #if SAMBA_VERSION >= 2
516 subdir ? "-D" : skip_argument,
517 subdir ? subdir : skip_argument,
522 memset(domain, '\0', strlen(domain));
526 if (pwtext_len > 0 && fullwrite(passwdfd, pwtext, pwtext_len) < 0) {
527 err = vstralloc("password write failed: ",
535 memset(user_and_password, '\0', lpass);
536 amfree(user_and_password);
538 ferr = fdopen(checkerr, "r");
541 for(sep = ""; (line = agets(ferr)) != NULL; free(line)) {
542 strappend(extra_info, sep);
543 strappend(extra_info, line);
545 if(strstr(line, "ERRDOS") != NULL) {
552 while ((wpid = wait(&retstat)) != -1) {
553 if (WIFSIGNALED(retstat)) {
555 rc = sig = WTERMSIG(retstat);
558 rc = ret = WEXITSTATUS(retstat);
563 strappend(err, "got signal ");
566 strappend(err, "returned ");
568 ap_snprintf(number, sizeof(number), "%d", ret);
569 strappend(err, number);
572 if (errdos != 0 || rc != 0) {
573 err = newvstralloc(err,
574 "samba access error: ",
577 extra_info ? extra_info : "",
583 err = stralloc2("This client is not configured for samba: ", amdevice);
588 device = amname_to_dirname(amdevice);
590 if(amdevice[0] == '/' && amdevice[1] == '/') {
591 err = vstralloc("The DUMP program cannot handle samba shares,",
599 if (strcmp(amname_to_fstype(amdevice), "advfs") == 0)
604 device = amname_to_dirname(amdevice);
609 device = amname_to_devname(amdevice);
618 dbprintf(("%s: device %s\n", debug_prefix_time(NULL), device));
620 /* skip accessability test if this is an AFS entry */
621 if(strncmp(device, "afs:", 4) != 0) {
622 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
623 access_result = open(device, O_RDONLY);
624 access_type = "open";
626 access_result = access(device, amode);
627 access_type = "access";
629 if(access_result == -1) {
630 err = vstralloc("could not ", access_type, " ", device,
631 " (", disk, "): ", strerror(errno), NULL);
633 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
634 aclose(access_result);
642 if(user_and_password) {
643 memset(user_and_password, '\0', lpass);
644 amfree(user_and_password);
647 memset(domain, '\0', strlen(domain));
652 printf("ERROR [%s]\n", err);
653 dbprintf(("%s: %s\n", debug_prefix_time(NULL), err));
656 printf("OK %s\n", disk);
657 dbprintf(("%s: disk \"%s\" OK\n", debug_prefix_time(NULL), disk));
658 printf("OK %s\n", amdevice);
659 dbprintf(("%s: amdevice \"%s\" OK\n",
660 debug_prefix_time(NULL), amdevice));
661 printf("OK %s\n", device);
662 dbprintf(("%s: device \"%s\" OK\n", debug_prefix_time(NULL), device));
665 dbprintf(("%s: extra info: %s\n", debug_prefix_time(NULL), extra_info));
670 /* XXX perhaps do something with level: read dumpdates and sanity check */
673 static void check_overall()
681 cmd = vstralloc(libexecdir, "/", "runtar", versionsuffix(), NULL);
682 check_file(cmd,X_OK);
689 cmd = vstralloc(libexecdir, "/", "rundump", versionsuffix(), NULL);
690 check_file(cmd,X_OK);
697 check_file(DUMP, X_OK);
699 printf("ERROR [DUMP program not available]\n");
705 check_file(RESTORE, X_OK);
707 printf("ERROR [RESTORE program not available]\n");
713 check_file(VDUMP, X_OK);
715 printf("ERROR [VDUMP program not available]\n");
719 if ( need_vrestore ) {
721 check_file(VRESTORE, X_OK);
723 printf("ERROR [VRESTORE program not available]\n");
729 check_file(XFSDUMP, F_OK);
731 printf("ERROR [XFSDUMP program not available]\n");
735 if( need_xfsrestore ) {
737 check_file(XFSRESTORE, X_OK);
739 printf("ERROR [XFSRESTORE program not available]\n");
745 check_file(VXDUMP, X_OK);
747 printf("ERROR [VXDUMP program not available]\n");
751 if( need_vxrestore ) {
753 check_file(VXRESTORE, X_OK);
755 printf("ERROR [VXRESTORE program not available]\n");
761 check_file(GNUTAR, X_OK);
763 printf("ERROR [GNUTAR program not available]\n");
765 #ifdef AMANDATES_FILE
766 check_file(AMANDATES_FILE, R_OK|W_OK);
768 #ifdef GNUTAR_LISTED_INCREMENTAL_DIR
769 check_dir(GNUTAR_LISTED_INCREMENTAL_DIR,R_OK|W_OK);
773 if( need_calcsize ) {
776 cmd = vstralloc(libexecdir, "/", "calcsize", versionsuffix(), NULL);
778 check_file(cmd, X_OK);
785 check_file(SAMBA_CLIENT, X_OK);
787 printf("ERROR [SMBCLIENT program not available]\n");
789 testfd = open("/etc/amandapass", R_OK);
791 if(fstat(testfd, &buf) == 0) {
792 if ((buf.st_mode & 0x7) != 0) {
793 printf("ERROR [/etc/amandapass is world readable!]\n");
795 printf("OK [/etc/amandapass is readable, but not by all]\n");
798 printf("OK [unable to stat /etc/amandapass: %s]\n",
803 printf("ERROR [unable to open /etc/amandapass: %s]\n",
808 if( need_compress_path ) {
809 check_file(COMPRESS_PATH, X_OK);
812 if( need_dump || need_xfsdump )
813 check_file("/var/lib/dumpdates",
822 check_file("/etc/vdumpdates", F_OK);
825 check_access("/dev/null", R_OK|W_OK);
826 check_space(AMANDA_TMPDIR, 64); /* for amandad i/o */
829 check_space(AMANDA_DBGDIR, 64); /* for amandad i/o */
832 check_space("/var/lib", 64); /* for /var/lib/dumpdates writing */
835 static void check_space(dir, kbytes)
839 generic_fs_stats_t statp;
841 if(get_fs_stats(dir, &statp) == -1)
842 printf("ERROR [cannot statfs %s: %s]\n", dir, strerror(errno));
843 else if(statp.avail < kbytes)
844 printf("ERROR [dir %s needs %ldKB, only has %ldKB available.]\n",
845 dir, kbytes, statp.avail);
847 printf("OK %s has more than %ld KB available.\n", dir, kbytes);
850 static void check_access(filename, mode)
854 char *noun, *adjective;
857 noun = "find", adjective = "exists";
858 else if((mode & X_OK) == X_OK)
859 noun = "execute", adjective = "executable";
860 else if((mode & (W_OK|R_OK)) == (W_OK|R_OK))
861 noun = "read/write", adjective = "read/writable";
863 noun = "access", adjective = "accessible";
865 if(access(filename, mode) == -1)
866 printf("ERROR [can not %s %s: %s]\n", noun, filename, strerror(errno));
868 printf("OK %s %s\n", filename, adjective);
871 static void check_file(filename, mode)
875 struct stat stat_buf;
876 if(!stat(filename, &stat_buf)) {
877 if(!S_ISREG(stat_buf.st_mode)) {
878 printf("ERROR [%s is not a file]\n", filename);
881 check_access(filename, mode);
884 static void check_dir(dirname, mode)
888 struct stat stat_buf;
891 if(!stat(dirname, &stat_buf)) {
892 if(!S_ISDIR(stat_buf.st_mode)) {
893 printf("ERROR [%s is not a directory]\n", dirname);
896 dir = stralloc2(dirname, "/.");
897 check_access(dir, mode);
901 static void check_suid(filename)
904 /* The following is only valid for real Unixs */
905 #ifndef IGNORE_UID_CHECK
906 struct stat stat_buf;
907 if(!stat(filename, &stat_buf)) {
908 if(stat_buf.st_uid != 0 ) {
909 printf("ERROR [%s is not owned by root]\n",filename);
911 if((stat_buf.st_mode & S_ISUID) != S_ISUID) {
912 printf("ERROR [%s is not SUID root]\n",filename);
916 printf("ERROR [can not stat %s]\n",filename);