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 2006/08/29 11:21:00 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 void check_file(char *filename, int mode);
79 static void check_dir(char *dirname, int mode);
80 static void check_suid(char *filename);
81 static void check_space(char *dir, off_t kbytes);
91 char *calcprog = NULL;
94 char *amdevice = NULL;
95 char *qamdevice = NULL;
97 char *err_extra = NULL;
102 #if defined(USE_DBMALLOC)
103 unsigned long malloc_hist_1, malloc_size_1;
104 unsigned long malloc_hist_2, malloc_size_2;
112 set_pname("selfcheck");
114 /* Don't die when child closes pipe */
115 signal(SIGPIPE, SIG_IGN);
117 #if defined(USE_DBMALLOC)
118 malloc_size_1 = malloc_inuse(&malloc_hist_1);
121 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
122 dbopen(DBG_SUBDIR_CLIENT);
124 dbprintf(("%s: version %s\n", get_pname(), version()));
126 if(argc > 2 && strcmp(argv[1], "amandad") == 0) {
127 amandad_auth = stralloc(argv[2]);
130 conffile = vstralloc(CONFIG_DIR, "/", "amanda-client.conf", NULL);
131 if (read_clientconf(conffile) > 0) {
132 printf("ERROR [reading conffile: %s]\n", conffile);
133 error("error reading conffile: %s", conffile);
138 our_features = am_init_feature_set();
139 our_feature_string = am_feature_to_string(our_features);
141 /* handle all service requests */
144 for(; (line = agets(stdin)) != NULL; free(line)) {
149 #define sc "OPTIONS "
150 if(strncmp(line, sc, SIZEOF(sc)-1) == 0) {
152 g_options = parse_g_options(line+8, 1);
153 if(!g_options->hostname) {
154 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
155 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
156 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
160 if(am_has_feature(g_options->features, fe_rep_options_features)) {
161 printf("features=%s;", our_feature_string);
163 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
164 printf("hostname=%s;", g_options->hostname);
169 if (g_options->config) {
170 conffile = vstralloc(CONFIG_DIR, "/", g_options->config, "/",
171 "amanda-client.conf", NULL);
172 if (read_clientconf(conffile) > 0) {
173 printf("ERROR [reading conffile: %s]\n", conffile);
174 error("error reading conffile: %s", conffile);
179 dbrename(g_options->config, DBG_SUBDIR_CLIENT);
188 skip_whitespace(s, ch); /* find program name */
190 goto err; /* no program */
193 skip_non_whitespace(s, ch);
194 s[-1] = '\0'; /* terminate the program name */
196 program_is_wrapper = 0;
197 if(strcmp(program,"DUMPER")==0) {
198 program_is_wrapper = 1;
199 skip_whitespace(s, ch); /* find dumper name */
201 goto err; /* no program */
204 skip_non_whitespace(s, ch);
205 s[-1] = '\0'; /* terminate the program name */
208 if(strncmp(program, "CALCSIZE", 8) == 0) {
209 skip_whitespace(s, ch); /* find program name */
211 goto err; /* no program */
214 skip_non_whitespace(s, ch);
221 skip_whitespace(s, ch); /* find disk name */
223 goto err; /* no disk */
226 skip_quoted_string(s, ch);
227 s[-1] = '\0'; /* terminate the disk name */
228 disk = unquote_string(qdisk);
230 skip_whitespace(s, ch); /* find the device or level */
232 goto err; /* no device or level */
234 if(!isdigit((int)s[-1])) {
236 skip_quoted_string(s, ch);
237 s[-1] = '\0'; /* terminate the device */
238 qamdevice = stralloc(fp);
239 amdevice = unquote_string(qamdevice);
240 skip_whitespace(s, ch); /* find level number */
243 amdevice = stralloc(disk);
246 /* find level number */
247 if (ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
248 goto err; /* bad level */
252 skip_whitespace(s, ch);
253 #define sc "OPTIONS "
254 if (ch && strncmp (s - 1, sc, SIZEOF(sc)-1) == 0) {
258 skip_whitespace(s, ch); /* find the option string */
260 goto err; /* bad options string */
263 skip_quoted_string(s, ch);
264 s[-1] = '\0'; /* terminate the options */
265 options = parse_options(optstr, disk, amdevice, g_options->features, 1);
267 check_options(program, calcprog, disk, amdevice, options);
268 check_disk(program, calcprog, disk, amdevice, level, &optstr[2]);
270 free_sl(options->exclude_file);
271 free_sl(options->exclude_list);
272 free_sl(options->include_file);
273 free_sl(options->include_list);
274 amfree(options->auth);
275 amfree(options->str);
277 } else if (ch == '\0') {
278 /* check all since no option */
291 need_compress_path=1;
294 check_disk(program, calcprog, disk, amdevice, level, "");
297 goto err; /* bad syntax */
306 amfree(our_feature_string);
307 am_release_feature_set(our_features);
309 am_release_feature_set(g_options->features);
310 g_options->features = NULL;
311 amfree(g_options->str);
312 amfree(g_options->hostname);
315 #if defined(USE_DBMALLOC)
316 malloc_size_2 = malloc_inuse(&malloc_hist_2);
318 if(malloc_size_1 != malloc_size_2) {
321 malloc_list(dbfd(), malloc_hist_1, malloc_hist_2);
329 printf("ERROR [BOGUS REQUEST PACKET]\n");
330 dbprintf(("%s: REQ packet is bogus%s%s\n",
331 debug_prefix_time(NULL),
332 err_extra ? ": " : "",
333 err_extra ? err_extra : ""));
347 char *myprogram = program;
349 if(strcmp(myprogram,"CALCSIZE") == 0) {
352 char *file_exclude = NULL;
353 char *file_include = NULL;
355 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
356 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
357 if(options->include_file) nb_include += options->include_file->nb_element;
358 if(options->include_list) nb_include += options->include_list->nb_element;
360 if(nb_exclude > 0) file_exclude = build_exclude(disk, amdevice, options, 1);
361 if(nb_include > 0) file_include = build_include(disk, amdevice, options, 1);
363 amfree(file_exclude);
364 amfree(file_include);
367 if (calcprog == NULL) {
368 printf("ERROR [no program name for calcsize]\n");
370 myprogram = calcprog;
374 if(strcmp(myprogram,"GNUTAR") == 0) {
376 if(amdevice[0] == '/' && amdevice[1] == '/') {
377 if(options->exclude_file && options->exclude_file->nb_element > 1) {
378 printf("ERROR [samba support only one exclude file]\n");
380 if(options->exclude_list && options->exclude_list->nb_element > 0 &&
381 options->exclude_optional==0) {
382 printf("ERROR [samba does not support exclude list]\n");
384 if(options->include_file && options->include_file->nb_element > 0) {
385 printf("ERROR [samba does not support include file]\n");
387 if(options->include_list && options->include_list->nb_element > 0 &&
388 options->include_optional==0) {
389 printf("ERROR [samba does not support include list]\n");
396 char *file_exclude = NULL;
397 char *file_include = NULL;
399 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
400 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
401 if(options->include_file) nb_include += options->include_file->nb_element;
402 if(options->include_list) nb_include += options->include_list->nb_element;
404 if(nb_exclude > 0) file_exclude = build_exclude(disk, amdevice, options, 1);
405 if(nb_include > 0) file_include = build_include(disk, amdevice, options, 1);
407 amfree(file_exclude);
408 amfree(file_include);
414 if(strcmp(myprogram,"DUMP") == 0) {
415 if(options->exclude_file && options->exclude_file->nb_element > 0) {
416 printf("ERROR [DUMP does not support exclude file]\n");
418 if(options->exclude_list && options->exclude_list->nb_element > 0) {
419 printf("ERROR [DUMP does not support exclude list]\n");
421 if(options->include_file && options->include_file->nb_element > 0) {
422 printf("ERROR [DUMP does not support include file]\n");
424 if(options->include_list && options->include_list->nb_element > 0) {
425 printf("ERROR [DUMP does not support include list]\n");
433 if (strcmp(amname_to_fstype(amdevice), "advfs") == 0)
440 if (options->createindex)
447 if (strcmp(amname_to_fstype(amdevice), "xfs") == 0)
454 if (options->createindex)
461 if (strcmp(amname_to_fstype(amdevice), "vxfs") == 0)
467 if (options->createindex)
474 if (options->createindex)
478 /* AIX backup program */
480 if (options->createindex)
484 if ((options->compress == COMPR_BEST) || (options->compress == COMPR_FAST)
485 || (options->compress == COMPR_CUST)) {
486 need_compress_path=1;
488 if(options->auth && amandad_auth) {
489 if(strcasecmp(options->auth, amandad_auth) != 0) {
490 fprintf(stdout,"ERROR [client configured for auth=%s while server requested '%s']\n",
491 amandad_auth, options->auth);
505 char *device = stralloc("nodevice");
507 char *user_and_password = NULL;
509 char *share = NULL, *subdir = NULL;
514 char *extra_info = NULL;
515 char *myprogram = program;
516 char *qdisk = quote_string(disk);
517 char *qamdevice = quote_string(amdevice);
518 char *qdevice = NULL;
520 (void)level; /* Quiet unused parameter warning */
522 dbprintf(("%s: checking disk %s\n", debug_prefix_time(NULL), qdisk));
524 if(strcmp(myprogram,"CALCSIZE") == 0) {
525 if(amdevice[0] == '/' && amdevice[1] == '/') {
526 err = vstralloc("Can't use CALCSIZE for samba estimate,",
532 myprogram = calcprog;
535 if (strcmp(myprogram, "GNUTAR")==0) {
536 if(amdevice[0] == '/' && amdevice[1] == '/') {
538 int nullfd, checkerr;
544 char number[NUM_STR_SIZE];
553 parsesharename(amdevice, &share, &subdir);
555 err = stralloc2("cannot parse for share/subdir disk entry ", amdevice);
558 if ((subdir) && (SAMBA_VERSION < 2)) {
559 err = vstralloc("subdirectory specified for share '",
561 "' but samba not v2 or better",
565 if ((user_and_password = findpass(share, &domain)) == NULL) {
566 err = stralloc2("cannot find password for ", amdevice);
569 lpass = strlen(user_and_password);
570 if ((pwtext = strchr(user_and_password, '%')) == NULL) {
571 err = stralloc2("password field not \'user%pass\' for ", amdevice);
575 pwtext_len = (size_t)strlen(pwtext);
577 if ((device = makesharename(share, 0)) == NULL) {
578 err = stralloc2("cannot make share name of ", share);
582 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
583 err = stralloc2("Cannot access /dev/null : ", strerror(errno));
587 if (pwtext_len > 0) {
588 pw_fd_env = "PASSWD_FD";
590 pw_fd_env = "dummy_PASSWD_FD";
592 checkpid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE,
593 &nullfd, &nullfd, &checkerr,
594 pw_fd_env, &passwdfd,
597 *user_and_password ? "-U" : skip_argument,
598 *user_and_password ? user_and_password : skip_argument,
600 domain ? "-W" : skip_argument,
601 domain ? domain : skip_argument,
602 #if SAMBA_VERSION >= 2
603 subdir ? "-D" : skip_argument,
604 subdir ? subdir : skip_argument,
612 && fullwrite(passwdfd, pwtext, (size_t)pwtext_len) < 0) {
613 err = vstralloc("password write failed: ",
622 memset(user_and_password, '\0', (size_t)lpass);
623 amfree(user_and_password);
625 ferr = fdopen(checkerr, "r");
627 printf("ERROR [Can't fdopen: %s]\n", strerror(errno));
628 error("Can't fdopen: %s", strerror(errno));
633 for(sep = ""; (line = agets(ferr)) != NULL; free(line)) {
636 strappend(extra_info, sep);
637 strappend(extra_info, line);
639 if(strstr(line, "ERRDOS") != NULL) {
646 while ((wpid = wait(&retstat)) != -1) {
647 if (WIFSIGNALED(retstat)) {
649 rc = sig = WTERMSIG(retstat);
652 rc = ret = WEXITSTATUS(retstat);
657 strappend(err, "got signal ");
660 strappend(err, "returned ");
662 snprintf(number, (size_t)sizeof(number), "%d", ret);
663 strappend(err, number);
666 if (errdos != 0 || rc != 0) {
667 err = newvstralloc(err,
668 "samba access error: ",
671 extra_info ? extra_info : "",
677 err = stralloc2("This client is not configured for samba: ", qdisk);
683 device = amname_to_dirname(amdevice);
684 } else if (strcmp(program, "DUMP") == 0) {
685 if(amdevice[0] == '/' && amdevice[1] == '/') {
686 err = vstralloc("The DUMP program cannot handle samba shares,",
694 if (strcmp(amname_to_fstype(amdevice), "advfs") == 0)
700 device = amname_to_dirname(amdevice);
706 device = amname_to_devname(amdevice);
714 else { /* program_is_wrapper==1 */
716 fflush(stdout);fflush(stdin);
717 switch (pid_wrapper = fork()) {
719 printf("ERROR [fork: %s]\n", strerror(errno));
720 error("fork: %s", strerror(errno));
726 char *cmd = vstralloc(DUMPER_DIR, "/", program, NULL);
727 argvchild[0] = program;
728 argvchild[1] = "selfcheck";
730 argvchild[3] = amdevice;
731 argvchild[4] = optstr;
733 execve(cmd,argvchild,safe_env());
736 default: /* parent */
739 waitpid(pid_wrapper, &status, 0);
742 fflush(stdout);fflush(stdin);
749 qdevice = quote_string(device);
750 dbprintf(("%s: device %s\n", debug_prefix_time(NULL), qdevice));
752 /* skip accessability test if this is an AFS entry */
753 if(strncmp(device, "afs:", 4) != 0) {
754 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
755 access_result = open(device, O_RDONLY);
756 access_type = "open";
758 access_result = access(device, amode);
759 access_type = "access";
761 if(access_result == -1) {
762 err = vstralloc("could not ", access_type, " ", qdevice,
763 " (", qdisk, "): ", strerror(errno), NULL);
765 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
766 aclose(access_result);
774 if(user_and_password) {
775 memset(user_and_password, '\0', (size_t)lpass);
776 amfree(user_and_password);
781 printf("ERROR [%s]\n", err);
782 dbprintf(("%s: %s\n", debug_prefix_time(NULL), err));
785 printf("OK %s\n", qdisk);
786 dbprintf(("%s: disk %s OK\n", debug_prefix_time(NULL), qdisk));
787 printf("OK %s\n", qamdevice);
788 dbprintf(("%s: amdevice %s OK\n",
789 debug_prefix_time(NULL), qamdevice));
790 printf("OK %s\n", qdevice);
791 dbprintf(("%s: device %s OK\n", debug_prefix_time(NULL), qdevice));
794 dbprintf(("%s: extra info: %s\n", debug_prefix_time(NULL), extra_info));
802 /* XXX perhaps do something with level: read dumpdates and sanity check */
811 char *gnutar_list_dir;
812 int need_amandates = 0;
816 cmd = vstralloc(libexecdir, "/", "runtar", versionsuffix(), NULL);
817 check_file(cmd,X_OK);
824 cmd = vstralloc(libexecdir, "/", "rundump", versionsuffix(), NULL);
825 check_file(cmd,X_OK);
832 check_file(DUMP, X_OK);
834 printf("ERROR [DUMP program not available]\n");
840 check_file(RESTORE, X_OK);
842 printf("ERROR [RESTORE program not available]\n");
848 check_file(VDUMP, X_OK);
850 printf("ERROR [VDUMP program not available]\n");
854 if ( need_vrestore ) {
856 check_file(VRESTORE, X_OK);
858 printf("ERROR [VRESTORE program not available]\n");
864 check_file(XFSDUMP, F_OK);
866 printf("ERROR [XFSDUMP program not available]\n");
870 if( need_xfsrestore ) {
872 check_file(XFSRESTORE, X_OK);
874 printf("ERROR [XFSRESTORE program not available]\n");
880 check_file(VXDUMP, X_OK);
882 printf("ERROR [VXDUMP program not available]\n");
886 if( need_vxrestore ) {
888 check_file(VXRESTORE, X_OK);
890 printf("ERROR [VXRESTORE program not available]\n");
896 check_file(GNUTAR, X_OK);
898 printf("ERROR [GNUTAR program not available]\n");
901 gnutar_list_dir = client_getconf_str(CLN_GNUTAR_LIST_DIR);
902 if (strlen(gnutar_list_dir) == 0)
903 gnutar_list_dir = NULL;
905 check_dir(gnutar_list_dir, R_OK|W_OK);
908 if (need_amandates) {
909 char *amandates_file;
910 amandates_file = client_getconf_str(CLN_AMANDATES);
911 check_file(amandates_file, R_OK|W_OK);
913 if( need_calcsize ) {
916 cmd = vstralloc(libexecdir, "/", "calcsize", versionsuffix(), NULL);
918 check_file(cmd, X_OK);
925 check_file(SAMBA_CLIENT, X_OK);
927 printf("ERROR [SMBCLIENT program not available]\n");
929 testfd = open("/etc/amandapass", R_OK);
931 if(fstat(testfd, &buf) == 0) {
932 if ((buf.st_mode & 0x7) != 0) {
933 printf("ERROR [/etc/amandapass is world readable!]\n");
935 printf("OK [/etc/amandapass is readable, but not by all]\n");
938 printf("OK [unable to stat /etc/amandapass: %s]\n",
943 printf("ERROR [unable to open /etc/amandapass: %s]\n",
948 if( need_compress_path )
949 check_file(COMPRESS_PATH, X_OK);
951 if( need_dump || need_xfsdump )
952 check_file("/etc/dumpdates",
961 check_file("/etc/vdumpdates", F_OK);
963 check_access("/dev/null", R_OK|W_OK);
964 check_space(AMANDA_TMPDIR, (off_t)64); /* for amandad i/o */
967 check_space(AMANDA_DBGDIR, (off_t)64); /* for amandad i/o */
970 check_space("/etc", (off_t)64); /* for /etc/dumpdates writing */
978 generic_fs_stats_t statp;
979 char *quoted = quote_string(dir);
981 if(get_fs_stats(dir, &statp) == -1) {
982 printf("ERROR [cannot statfs %s: %s]\n", quoted, strerror(errno));
983 } else if(statp.avail < kbytes) {
984 printf("ERROR [dir %s needs " OFF_T_FMT "KB, only has "
985 OFF_T_FMT "KB available.]\n", quoted,
986 (OFF_T_FMT_TYPE)kbytes,
987 (OFF_T_FMT_TYPE)statp.avail);
989 printf("OK %s has more than " OFF_T_FMT " KB available.\n",
990 quoted, (OFF_T_FMT_TYPE)kbytes);
1000 char *noun, *adjective;
1001 char *quoted = quote_string(filename);
1004 noun = "find", adjective = "exists";
1005 else if((mode & X_OK) == X_OK)
1006 noun = "execute", adjective = "executable";
1007 else if((mode & (W_OK|R_OK)) == (W_OK|R_OK))
1008 noun = "read/write", adjective = "read/writable";
1010 noun = "access", adjective = "accessible";
1012 if(access(filename, mode) == -1)
1013 printf("ERROR [can not %s %s: %s]\n", noun, quoted, strerror(errno));
1015 printf("OK %s %s\n", quoted, adjective);
1024 struct stat stat_buf;
1027 if(!stat(filename, &stat_buf)) {
1028 if(!S_ISREG(stat_buf.st_mode)) {
1029 quoted = quote_string(filename);
1030 printf("ERROR [%s is not a file]\n", quoted);
1034 check_access(filename, mode);
1042 struct stat stat_buf;
1046 if(!stat(dirname, &stat_buf)) {
1047 if(!S_ISDIR(stat_buf.st_mode)) {
1048 quoted = quote_string(dirname);
1049 printf("ERROR [%s is not a directory]\n", quoted);
1053 dir = stralloc2(dirname, "/.");
1054 check_access(dir, mode);
1062 /* The following is only valid for real Unixs */
1063 #ifndef IGNORE_UID_CHECK
1064 struct stat stat_buf;
1065 char *quoted = quote_string(filename);
1067 if(!stat(filename, &stat_buf)) {
1068 if(stat_buf.st_uid != 0 ) {
1069 printf("ERROR [%s is not owned by root]\n", quoted);
1071 if((stat_buf.st_mode & S_ISUID) != S_ISUID) {
1072 printf("ERROR [%s is not SUID root]\n", quoted);
1076 printf("ERROR [can not stat %s]\n", quoted);
1080 (void)filename; /* Quiet unused parameter warning */