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.95.2.2 2007/01/19 01:03:34 martinea Exp $
30 * do self-check and send back any error messages
37 #include "amandates.h"
40 #include "pipespawn.h"
41 #include "amfeatures.h"
42 #include "client_util.h"
43 #include "clientconf.h"
57 int need_xfsrestore=0;
62 int need_compress_path=0;
64 int program_is_wrapper=0;
66 static char *amandad_auth = NULL;
67 static am_feature_t *our_features = NULL;
68 static char *our_feature_string = NULL;
69 static g_option_t *g_options = NULL;
72 int main(int argc, char **argv);
74 static void check_options(char *program, char *calcprog, char *disk, char *amdevice, option_t *options);
75 static void check_disk(char *program, char *calcprog, char *disk, char *amdevice, int level, char *optstr);
76 static void check_overall(void);
77 static void check_access(char *filename, int mode);
78 static int check_file_exist(char *filename);
79 static void check_file(char *filename, int mode);
80 static void check_dir(char *dirname, int mode);
81 static void check_suid(char *filename);
82 static void check_space(char *dir, off_t kbytes);
92 char *calcprog = NULL;
95 char *amdevice = NULL;
96 char *qamdevice = NULL;
98 char *err_extra = NULL;
103 #if defined(USE_DBMALLOC)
104 unsigned long malloc_hist_1, malloc_size_1;
105 unsigned long malloc_hist_2, malloc_size_2;
113 set_pname("selfcheck");
115 /* Don't die when child closes pipe */
116 signal(SIGPIPE, SIG_IGN);
118 #if defined(USE_DBMALLOC)
119 malloc_size_1 = malloc_inuse(&malloc_hist_1);
122 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
123 dbopen(DBG_SUBDIR_CLIENT);
125 dbprintf(("%s: version %s\n", get_pname(), version()));
127 if(argc > 2 && strcmp(argv[1], "amandad") == 0) {
128 amandad_auth = stralloc(argv[2]);
131 conffile = vstralloc(CONFIG_DIR, "/", "amanda-client.conf", NULL);
132 if (read_clientconf(conffile) > 0) {
133 printf("ERROR [reading conffile: %s]\n", conffile);
134 error("error reading conffile: %s", conffile);
139 our_features = am_init_feature_set();
140 our_feature_string = am_feature_to_string(our_features);
142 /* handle all service requests */
145 for(; (line = agets(stdin)) != NULL; free(line)) {
150 #define sc "OPTIONS "
151 if(strncmp(line, sc, SIZEOF(sc)-1) == 0) {
153 g_options = parse_g_options(line+8, 1);
154 if(!g_options->hostname) {
155 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
156 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
157 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
161 if(am_has_feature(g_options->features, fe_rep_options_features)) {
162 printf("features=%s;", our_feature_string);
164 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
165 printf("hostname=%s;", g_options->hostname);
170 if (g_options->config) {
171 conffile = vstralloc(CONFIG_DIR, "/", g_options->config, "/",
172 "amanda-client.conf", NULL);
173 if (read_clientconf(conffile) > 0) {
174 printf("ERROR [reading conffile: %s]\n", conffile);
175 error("error reading conffile: %s", conffile);
180 dbrename(g_options->config, DBG_SUBDIR_CLIENT);
189 skip_whitespace(s, ch); /* find program name */
191 goto err; /* no program */
194 skip_non_whitespace(s, ch);
195 s[-1] = '\0'; /* terminate the program name */
197 program_is_wrapper = 0;
198 if(strcmp(program,"DUMPER")==0) {
199 program_is_wrapper = 1;
200 skip_whitespace(s, ch); /* find dumper name */
202 goto err; /* no program */
205 skip_non_whitespace(s, ch);
206 s[-1] = '\0'; /* terminate the program name */
209 if(strncmp(program, "CALCSIZE", 8) == 0) {
210 skip_whitespace(s, ch); /* find program name */
212 goto err; /* no program */
215 skip_non_whitespace(s, ch);
222 skip_whitespace(s, ch); /* find disk name */
224 goto err; /* no disk */
227 skip_quoted_string(s, ch);
228 s[-1] = '\0'; /* terminate the disk name */
229 disk = unquote_string(qdisk);
231 skip_whitespace(s, ch); /* find the device or level */
233 goto err; /* no device or level */
235 if(!isdigit((int)s[-1])) {
237 skip_quoted_string(s, ch);
238 s[-1] = '\0'; /* terminate the device */
239 qamdevice = stralloc(fp);
240 amdevice = unquote_string(qamdevice);
241 skip_whitespace(s, ch); /* find level number */
244 amdevice = stralloc(disk);
247 /* find level number */
248 if (ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
249 goto err; /* bad level */
253 skip_whitespace(s, ch);
254 #define sc "OPTIONS "
255 if (ch && strncmp (s - 1, sc, SIZEOF(sc)-1) == 0) {
259 skip_whitespace(s, ch); /* find the option string */
261 goto err; /* bad options string */
264 skip_quoted_string(s, ch);
265 s[-1] = '\0'; /* terminate the options */
266 options = parse_options(optstr, disk, amdevice, g_options->features, 1);
268 check_options(program, calcprog, disk, amdevice, options);
269 check_disk(program, calcprog, disk, amdevice, level, &optstr[2]);
271 free_sl(options->exclude_file);
272 free_sl(options->exclude_list);
273 free_sl(options->include_file);
274 free_sl(options->include_list);
275 amfree(options->auth);
276 amfree(options->str);
278 } else if (ch == '\0') {
279 /* check all since no option */
292 need_compress_path=1;
295 check_disk(program, calcprog, disk, amdevice, level, "");
298 goto err; /* bad syntax */
307 amfree(our_feature_string);
308 am_release_feature_set(our_features);
310 am_release_feature_set(g_options->features);
311 g_options->features = NULL;
312 amfree(g_options->str);
313 amfree(g_options->hostname);
316 #if defined(USE_DBMALLOC)
317 malloc_size_2 = malloc_inuse(&malloc_hist_2);
319 if(malloc_size_1 != malloc_size_2) {
322 malloc_list(dbfd(), malloc_hist_1, malloc_hist_2);
330 printf("ERROR [BOGUS REQUEST PACKET]\n");
331 dbprintf(("%s: REQ packet is bogus%s%s\n",
332 debug_prefix_time(NULL),
333 err_extra ? ": " : "",
334 err_extra ? err_extra : ""));
348 char *myprogram = program;
350 if(strcmp(myprogram,"CALCSIZE") == 0) {
353 char *file_exclude = NULL;
354 char *file_include = NULL;
356 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
357 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
358 if(options->include_file) nb_include += options->include_file->nb_element;
359 if(options->include_list) nb_include += options->include_list->nb_element;
361 if(nb_exclude > 0) file_exclude = build_exclude(disk, amdevice, options, 1);
362 if(nb_include > 0) file_include = build_include(disk, amdevice, options, 1);
364 amfree(file_exclude);
365 amfree(file_include);
368 if (calcprog == NULL) {
369 printf("ERROR [no program name for calcsize]\n");
371 myprogram = calcprog;
375 if(strcmp(myprogram,"GNUTAR") == 0) {
377 if(amdevice[0] == '/' && amdevice[1] == '/') {
378 if(options->exclude_file && options->exclude_file->nb_element > 1) {
379 printf("ERROR [samba support only one exclude file]\n");
381 if(options->exclude_list && options->exclude_list->nb_element > 0 &&
382 options->exclude_optional==0) {
383 printf("ERROR [samba does not support exclude list]\n");
385 if(options->include_file && options->include_file->nb_element > 0) {
386 printf("ERROR [samba does not support include file]\n");
388 if(options->include_list && options->include_list->nb_element > 0 &&
389 options->include_optional==0) {
390 printf("ERROR [samba does not support include list]\n");
397 char *file_exclude = NULL;
398 char *file_include = NULL;
400 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
401 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
402 if(options->include_file) nb_include += options->include_file->nb_element;
403 if(options->include_list) nb_include += options->include_list->nb_element;
405 if(nb_exclude > 0) file_exclude = build_exclude(disk, amdevice, options, 1);
406 if(nb_include > 0) file_include = build_include(disk, amdevice, options, 1);
408 amfree(file_exclude);
409 amfree(file_include);
415 if(strcmp(myprogram,"DUMP") == 0) {
416 if(options->exclude_file && options->exclude_file->nb_element > 0) {
417 printf("ERROR [DUMP does not support exclude file]\n");
419 if(options->exclude_list && options->exclude_list->nb_element > 0) {
420 printf("ERROR [DUMP does not support exclude list]\n");
422 if(options->include_file && options->include_file->nb_element > 0) {
423 printf("ERROR [DUMP does not support include file]\n");
425 if(options->include_list && options->include_list->nb_element > 0) {
426 printf("ERROR [DUMP does not support include list]\n");
434 if (strcmp(amname_to_fstype(amdevice), "advfs") == 0)
441 if (options->createindex)
448 if (strcmp(amname_to_fstype(amdevice), "xfs") == 0)
455 if (options->createindex)
462 if (strcmp(amname_to_fstype(amdevice), "vxfs") == 0)
468 if (options->createindex)
475 if (options->createindex)
479 /* AIX backup program */
481 if (options->createindex)
485 if ((options->compress == COMPR_BEST) || (options->compress == COMPR_FAST)
486 || (options->compress == COMPR_CUST)) {
487 need_compress_path=1;
489 if(options->auth && amandad_auth) {
490 if(strcasecmp(options->auth, amandad_auth) != 0) {
491 fprintf(stdout,"ERROR [client configured for auth=%s while server requested '%s']\n",
492 amandad_auth, options->auth);
506 char *device = stralloc("nodevice");
508 char *user_and_password = NULL;
510 char *share = NULL, *subdir = NULL;
515 char *extra_info = NULL;
516 char *myprogram = program;
517 char *qdisk = quote_string(disk);
518 char *qamdevice = quote_string(amdevice);
519 char *qdevice = NULL;
521 (void)level; /* Quiet unused parameter warning */
523 dbprintf(("%s: checking disk %s\n", debug_prefix_time(NULL), qdisk));
525 if(strcmp(myprogram,"CALCSIZE") == 0) {
526 if(amdevice[0] == '/' && amdevice[1] == '/') {
527 err = vstralloc("Can't use CALCSIZE for samba estimate,",
533 myprogram = calcprog;
536 if (strcmp(myprogram, "GNUTAR")==0) {
537 if(amdevice[0] == '/' && amdevice[1] == '/') {
539 int nullfd, checkerr;
545 char number[NUM_STR_SIZE];
554 parsesharename(amdevice, &share, &subdir);
556 err = stralloc2("cannot parse for share/subdir disk entry ", amdevice);
559 if ((subdir) && (SAMBA_VERSION < 2)) {
560 err = vstralloc("subdirectory specified for share '",
562 "' but samba not v2 or better",
566 if ((user_and_password = findpass(share, &domain)) == NULL) {
567 err = stralloc2("cannot find password for ", amdevice);
570 lpass = strlen(user_and_password);
571 if ((pwtext = strchr(user_and_password, '%')) == NULL) {
572 err = stralloc2("password field not \'user%pass\' for ", amdevice);
576 pwtext_len = (size_t)strlen(pwtext);
578 if ((device = makesharename(share, 0)) == NULL) {
579 err = stralloc2("cannot make share name of ", share);
583 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
584 err = stralloc2("Cannot access /dev/null : ", strerror(errno));
588 if (pwtext_len > 0) {
589 pw_fd_env = "PASSWD_FD";
591 pw_fd_env = "dummy_PASSWD_FD";
593 checkpid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE,
594 &nullfd, &nullfd, &checkerr,
595 pw_fd_env, &passwdfd,
598 *user_and_password ? "-U" : skip_argument,
599 *user_and_password ? user_and_password : skip_argument,
601 domain ? "-W" : skip_argument,
602 domain ? domain : skip_argument,
603 #if SAMBA_VERSION >= 2
604 subdir ? "-D" : skip_argument,
605 subdir ? subdir : skip_argument,
613 && fullwrite(passwdfd, pwtext, (size_t)pwtext_len) < 0) {
614 err = vstralloc("password write failed: ",
623 memset(user_and_password, '\0', (size_t)lpass);
624 amfree(user_and_password);
626 ferr = fdopen(checkerr, "r");
628 printf("ERROR [Can't fdopen: %s]\n", strerror(errno));
629 error("Can't fdopen: %s", strerror(errno));
634 for(sep = ""; (line = agets(ferr)) != NULL; free(line)) {
637 strappend(extra_info, sep);
638 strappend(extra_info, line);
640 if(strstr(line, "ERRDOS") != NULL) {
647 while ((wpid = wait(&retstat)) != -1) {
648 if (WIFSIGNALED(retstat)) {
650 rc = sig = WTERMSIG(retstat);
653 rc = ret = WEXITSTATUS(retstat);
658 strappend(err, "got signal ");
661 strappend(err, "returned ");
663 snprintf(number, (size_t)sizeof(number), "%d", ret);
664 strappend(err, number);
667 if (errdos != 0 || rc != 0) {
668 err = newvstralloc(err,
669 "samba access error: ",
672 extra_info ? extra_info : "",
678 err = stralloc2("This client is not configured for samba: ", qdisk);
684 device = amname_to_dirname(amdevice);
685 } else if (strcmp(myprogram, "DUMP") == 0) {
686 if(amdevice[0] == '/' && amdevice[1] == '/') {
687 err = vstralloc("The DUMP program cannot handle samba shares,",
695 if (strcmp(amname_to_fstype(amdevice), "advfs") == 0)
701 device = amname_to_dirname(amdevice);
707 device = amname_to_devname(amdevice);
715 else { /* program_is_wrapper==1 */
717 fflush(stdout);fflush(stdin);
718 switch (pid_wrapper = fork()) {
720 printf("ERROR [fork: %s]\n", strerror(errno));
721 error("fork: %s", strerror(errno));
727 char *cmd = vstralloc(DUMPER_DIR, "/", program, NULL);
728 argvchild[0] = program;
729 argvchild[1] = "selfcheck";
731 argvchild[3] = amdevice;
732 argvchild[4] = optstr;
734 execve(cmd,argvchild,safe_env());
737 default: /* parent */
740 waitpid(pid_wrapper, &status, 0);
743 fflush(stdout);fflush(stdin);
750 qdevice = quote_string(device);
751 dbprintf(("%s: device %s\n", debug_prefix_time(NULL), qdevice));
753 /* skip accessability test if this is an AFS entry */
754 if(strncmp(device, "afs:", 4) != 0) {
755 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
756 access_result = open(device, O_RDONLY);
757 access_type = "open";
759 access_result = access(device, amode);
760 access_type = "access";
762 if(access_result == -1) {
763 err = vstralloc("could not ", access_type, " ", qdevice,
764 " (", qdisk, "): ", strerror(errno), NULL);
766 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
767 aclose(access_result);
775 if(user_and_password) {
776 memset(user_and_password, '\0', (size_t)lpass);
777 amfree(user_and_password);
782 printf("ERROR [%s]\n", err);
783 dbprintf(("%s: %s\n", debug_prefix_time(NULL), err));
786 printf("OK %s\n", qdisk);
787 dbprintf(("%s: disk %s OK\n", debug_prefix_time(NULL), qdisk));
788 printf("OK %s\n", qamdevice);
789 dbprintf(("%s: amdevice %s OK\n",
790 debug_prefix_time(NULL), qamdevice));
791 printf("OK %s\n", qdevice);
792 dbprintf(("%s: device %s OK\n", debug_prefix_time(NULL), qdevice));
795 dbprintf(("%s: extra info: %s\n", debug_prefix_time(NULL), extra_info));
803 /* XXX perhaps do something with level: read dumpdates and sanity check */
812 char *gnutar_list_dir;
813 int need_amandates = 0;
817 cmd = vstralloc(libexecdir, "/", "runtar", versionsuffix(), NULL);
818 check_file(cmd,X_OK);
825 cmd = vstralloc(libexecdir, "/", "rundump", versionsuffix(), NULL);
826 check_file(cmd,X_OK);
833 check_file(DUMP, X_OK);
835 printf("ERROR [DUMP program not available]\n");
841 check_file(RESTORE, X_OK);
843 printf("ERROR [RESTORE program not available]\n");
849 check_file(VDUMP, X_OK);
851 printf("ERROR [VDUMP program not available]\n");
855 if ( need_vrestore ) {
857 check_file(VRESTORE, X_OK);
859 printf("ERROR [VRESTORE program not available]\n");
865 check_file(XFSDUMP, F_OK);
867 printf("ERROR [XFSDUMP program not available]\n");
871 if( need_xfsrestore ) {
873 check_file(XFSRESTORE, X_OK);
875 printf("ERROR [XFSRESTORE program not available]\n");
881 check_file(VXDUMP, X_OK);
883 printf("ERROR [VXDUMP program not available]\n");
887 if( need_vxrestore ) {
889 check_file(VXRESTORE, X_OK);
891 printf("ERROR [VXRESTORE program not available]\n");
897 check_file(GNUTAR, X_OK);
899 printf("ERROR [GNUTAR program not available]\n");
902 gnutar_list_dir = client_getconf_str(CLN_GNUTAR_LIST_DIR);
903 if (strlen(gnutar_list_dir) == 0)
904 gnutar_list_dir = NULL;
906 check_dir(gnutar_list_dir, R_OK|W_OK);
909 if (need_amandates) {
910 char *amandates_file;
911 amandates_file = client_getconf_str(CLN_AMANDATES);
912 check_file(amandates_file, R_OK|W_OK);
914 if( need_calcsize ) {
917 cmd = vstralloc(libexecdir, "/", "calcsize", versionsuffix(), NULL);
919 check_file(cmd, X_OK);
926 check_file(SAMBA_CLIENT, X_OK);
928 printf("ERROR [SMBCLIENT program not available]\n");
930 testfd = open("/etc/amandapass", R_OK);
932 if(fstat(testfd, &buf) == 0) {
933 if ((buf.st_mode & 0x7) != 0) {
934 printf("ERROR [/etc/amandapass is world readable!]\n");
936 printf("OK [/etc/amandapass is readable, but not by all]\n");
939 printf("OK [unable to stat /etc/amandapass: %s]\n",
944 printf("ERROR [unable to open /etc/amandapass: %s]\n",
949 if (need_compress_path )
950 check_file(COMPRESS_PATH, X_OK);
952 if (need_dump || need_xfsdump ) {
953 if (check_file_exist("/etc/dumpdates")) {
954 check_file("/etc/dumpdates",
963 if (access("/etc", R_OK|W_OK) == -1) {
964 printf("ERROR [dump will not be able to create the /etc/dumpdates file: %s]\n", strerror(errno));
971 if (check_file_exist("/etc/vdumpdates")) {
972 check_file("/etc/vdumpdates", F_OK);
976 check_access("/dev/null", R_OK|W_OK);
977 check_space(AMANDA_TMPDIR, (off_t)64); /* for amandad i/o */
980 check_space(AMANDA_DBGDIR, (off_t)64); /* for amandad i/o */
983 check_space("/etc", (off_t)64); /* for /etc/dumpdates writing */
991 generic_fs_stats_t statp;
992 char *quoted = quote_string(dir);
994 if(get_fs_stats(dir, &statp) == -1) {
995 printf("ERROR [cannot statfs %s: %s]\n", quoted, strerror(errno));
996 } else if(statp.avail < kbytes) {
997 printf("ERROR [dir %s needs " OFF_T_FMT "KB, only has "
998 OFF_T_FMT "KB available.]\n", quoted,
999 (OFF_T_FMT_TYPE)kbytes,
1000 (OFF_T_FMT_TYPE)statp.avail);
1002 printf("OK %s has more than " OFF_T_FMT " KB available.\n",
1003 quoted, (OFF_T_FMT_TYPE)kbytes);
1013 char *noun, *adjective;
1014 char *quoted = quote_string(filename);
1017 noun = "find", adjective = "exists";
1018 else if((mode & X_OK) == X_OK)
1019 noun = "execute", adjective = "executable";
1020 else if((mode & (W_OK|R_OK)) == (W_OK|R_OK))
1021 noun = "read/write", adjective = "read/writable";
1023 noun = "access", adjective = "accessible";
1025 if(access(filename, mode) == -1)
1026 printf("ERROR [can not %s %s: %s]\n", noun, quoted, strerror(errno));
1028 printf("OK %s %s\n", quoted, adjective);
1036 struct stat stat_buf;
1038 if (stat(filename, &stat_buf) != 0) {
1039 if(errno == ENOENT) {
1051 struct stat stat_buf;
1054 if(!stat(filename, &stat_buf)) {
1055 if(!S_ISREG(stat_buf.st_mode)) {
1056 quoted = quote_string(filename);
1057 printf("ERROR [%s is not a file]\n", quoted);
1061 check_access(filename, mode);
1069 struct stat stat_buf;
1073 if(!stat(dirname, &stat_buf)) {
1074 if(!S_ISDIR(stat_buf.st_mode)) {
1075 quoted = quote_string(dirname);
1076 printf("ERROR [%s is not a directory]\n", quoted);
1080 dir = stralloc2(dirname, "/.");
1081 check_access(dir, mode);
1089 /* The following is only valid for real Unixs */
1090 #ifndef IGNORE_UID_CHECK
1091 struct stat stat_buf;
1092 char *quoted = quote_string(filename);
1094 if(!stat(filename, &stat_buf)) {
1095 if(stat_buf.st_uid != 0 ) {
1096 printf("ERROR [%s is not owned by root]\n", quoted);
1098 if((stat_buf.st_mode & S_ISUID) != S_ISUID) {
1099 printf("ERROR [%s is not SUID root]\n", quoted);
1103 printf("ERROR [can not stat %s]\n", quoted);
1107 (void)filename; /* Quiet unused parameter warning */