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.76 2006/01/14 04:37:18 paddy_s 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"
55 int need_xfsrestore=0;
60 int need_compress_path=0;
62 int program_is_wrapper=0;
64 static am_feature_t *our_features = NULL;
65 static char *our_feature_string = NULL;
66 static g_option_t *g_options = NULL;
69 int main P((int argc, char **argv));
71 static void check_options P((char *program, char *calcprog, char *disk, char *amdevice, option_t *options));
72 static void check_disk P((char *program, char *calcprog, char *disk, char *amdevice, int level, char *optstr));
73 static void check_overall P((void));
74 static void check_access P((char *filename, int mode));
75 static void check_file P((char *filename, int mode));
76 static void check_dir P((char *dirname, int mode));
77 static void check_suid P((char *filename));
78 static void check_space P((char *dir, long kbytes));
87 char *calcprog = NULL;
89 char *amdevice = NULL;
91 char *err_extra = NULL;
94 unsigned long malloc_hist_1, malloc_size_1;
95 unsigned long malloc_hist_2, malloc_size_2;
103 set_pname("selfcheck");
105 /* Don't die when child closes pipe */
106 signal(SIGPIPE, SIG_IGN);
108 malloc_size_1 = malloc_inuse(&malloc_hist_1);
110 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
113 dbprintf(("%s: version %s\n", get_pname(), version()));
115 our_features = am_init_feature_set();
116 our_feature_string = am_feature_to_string(our_features);
118 /* handle all service requests */
120 for(; (line = agets(stdin)) != NULL; free(line)) {
121 #define sc "OPTIONS "
122 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
124 g_options = parse_g_options(line+8, 1);
125 if(!g_options->hostname) {
126 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
127 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
128 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
132 if(am_has_feature(g_options->features, fe_rep_options_features)) {
133 printf("features=%s;", our_feature_string);
135 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
136 printf("hostname=%s;", g_options->hostname);
146 skip_whitespace(s, ch); /* find program name */
148 goto err; /* no program */
151 skip_non_whitespace(s, ch);
152 s[-1] = '\0'; /* terminate the program name */
154 program_is_wrapper = 0;
155 if(strcmp(program,"DUMPER")==0) {
156 program_is_wrapper = 1;
157 skip_whitespace(s, ch); /* find dumper name */
159 goto err; /* no program */
162 skip_non_whitespace(s, ch);
163 s[-1] = '\0'; /* terminate the program name */
166 if(strncmp(program, "CALCSIZE", 8) == 0) {
167 skip_whitespace(s, ch); /* find program name */
169 goto err; /* no program */
172 skip_non_whitespace(s, ch);
179 skip_whitespace(s, ch); /* find disk name */
181 goto err; /* no disk */
184 skip_non_whitespace(s, ch);
185 s[-1] = '\0'; /* terminate the disk name */
187 skip_whitespace(s, ch); /* find the device or level */
189 goto err; /* no device or level */
191 if(!isdigit((int)s[-1])) {
193 skip_non_whitespace(s, ch);
194 s[-1] = '\0'; /* terminate the device */
195 amdevice = stralloc(fp);
196 skip_whitespace(s, ch); /* find level number */
199 amdevice = stralloc(disk);
202 /* find level number */
203 if (ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
204 goto err; /* bad level */
208 skip_whitespace(s, ch);
209 #define sc "OPTIONS "
210 if (ch && strncmp (s - 1, sc, sizeof(sc)-1) == 0) {
214 skip_whitespace(s, ch); /* find the option string */
216 goto err; /* bad options string */
219 skip_non_whitespace(s, ch);
220 s[-1] = '\0'; /* terminate the options */
221 options = parse_options(optstr, disk, amdevice, g_options->features, 1);
222 check_options(program, calcprog, disk, amdevice, options);
223 check_disk(program, calcprog, disk, amdevice, level, &optstr[2]);
224 free_sl(options->exclude_file);
225 free_sl(options->exclude_list);
226 free_sl(options->include_file);
227 free_sl(options->include_list);
228 amfree(options->auth);
229 amfree(options->str);
231 } else if (ch == '\0') {
232 /* check all since no option */
245 need_compress_path=1;
247 check_disk(program, calcprog, disk, amdevice, level, "");
249 goto err; /* bad syntax */
257 amfree(our_feature_string);
258 am_release_feature_set(our_features);
260 am_release_feature_set(g_options->features);
261 g_options->features = NULL;
262 amfree(g_options->str);
263 amfree(g_options->hostname);
266 malloc_size_2 = malloc_inuse(&malloc_hist_2);
268 if(malloc_size_1 != malloc_size_2) {
269 #if defined(USE_DBMALLOC)
272 malloc_list(dbfd(), malloc_hist_1, malloc_hist_2);
280 printf("ERROR [BOGUS REQUEST PACKET]\n");
281 dbprintf(("%s: REQ packet is bogus%s%s\n",
282 debug_prefix_time(NULL),
283 err_extra ? ": " : "",
284 err_extra ? err_extra : ""));
291 check_options(program, calcprog, disk, amdevice, options)
292 char *program, *calcprog, *disk, *amdevice;
295 char *myprogram = program;
297 if(strcmp(myprogram,"CALCSIZE") == 0) {
300 char *file_exclude = NULL;
301 char *file_include = NULL;
303 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
304 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
305 if(options->include_file) nb_include += options->include_file->nb_element;
306 if(options->include_list) nb_include += options->include_list->nb_element;
308 if(nb_exclude > 0) file_exclude = build_exclude(disk, amdevice, options, 1);
309 if(nb_include > 0) file_include = build_include(disk, amdevice, options, 1);
311 amfree(file_exclude);
312 amfree(file_include);
315 myprogram = calcprog;
318 if(strcmp(myprogram,"GNUTAR") == 0) {
320 if(amdevice[0] == '/' && amdevice[1] == '/') {
321 if(options->exclude_file && options->exclude_file->nb_element > 1) {
322 printf("ERROR [samba support only one exclude file]\n");
324 if(options->exclude_list && options->exclude_list->nb_element > 0 &&
325 options->exclude_optional==0) {
326 printf("ERROR [samba does not support exclude list]\n");
328 if(options->include_file && options->include_file->nb_element > 0) {
329 printf("ERROR [samba does not support include file]\n");
331 if(options->include_list && options->include_list->nb_element > 0 &&
332 options->include_optional==0) {
333 printf("ERROR [samba does not support include list]\n");
340 char *file_exclude = NULL;
341 char *file_include = NULL;
343 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
344 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
345 if(options->include_file) nb_include += options->include_file->nb_element;
346 if(options->include_list) nb_include += options->include_list->nb_element;
348 if(nb_exclude > 0) file_exclude = build_exclude(disk, amdevice, options, 1);
349 if(nb_include > 0) file_include = build_include(disk, amdevice, options, 1);
351 amfree(file_exclude);
352 amfree(file_include);
358 if(strcmp(myprogram,"DUMP") == 0) {
359 if(options->exclude_file && options->exclude_file->nb_element > 0) {
360 printf("ERROR [DUMP does not support exclude file]\n");
362 if(options->exclude_list && options->exclude_list->nb_element > 0) {
363 printf("ERROR [DUMP does not support exclude list]\n");
365 if(options->include_file && options->include_file->nb_element > 0) {
366 printf("ERROR [DUMP does not support include file]\n");
368 if(options->include_list && options->include_list->nb_element > 0) {
369 printf("ERROR [DUMP does not support include list]\n");
377 if (strcmp(amname_to_fstype(amdevice), "advfs") == 0)
384 if (options->createindex)
391 if (strcmp(amname_to_fstype(amdevice), "xfs") == 0)
398 if (options->createindex)
405 if (strcmp(amname_to_fstype(amdevice), "vxfs") == 0)
411 if (options->createindex)
418 if (options->createindex)
422 /* AIX backup program */
424 if (options->createindex)
428 if(program_is_wrapper==1) {
430 if(options->compress == COMPR_BEST || options->compress == COMPR_FAST || options->compress == COMPR_CUST)
431 need_compress_path=1;
434 static void check_disk(program, calcprog, disk, amdevice, level, optstr)
435 char *program, *calcprog, *disk, *amdevice;
441 char *user_and_password = NULL, *domain = NULL;
442 char *share = NULL, *subdir = NULL;
447 char *extra_info = NULL;
448 char *myprogram = program;
450 if(strcmp(myprogram,"CALCSIZE") == 0) {
451 if(amdevice[0] == '/' && amdevice[1] == '/') {
452 err = vstralloc("Can't use CALCSIZE for samba estimate,",
458 myprogram = calcprog;
461 dbprintf(("%s: checking disk %s\n", debug_prefix_time(NULL), disk));
463 if (strcmp(myprogram, "GNUTAR") == 0) {
464 if(amdevice[0] == '/' && amdevice[1] == '/') {
466 int nullfd, checkerr;
472 char number[NUM_STR_SIZE];
481 parsesharename(amdevice, &share, &subdir);
483 err = stralloc2("cannot parse for share/subdir disk entry ", amdevice);
486 if ((subdir) && (SAMBA_VERSION < 2)) {
487 err = vstralloc("subdirectory specified for share '",
489 "' but samba not v2 or better",
493 if ((user_and_password = findpass(share, &domain)) == NULL) {
494 err = stralloc2("cannot find password for ", amdevice);
497 lpass = strlen(user_and_password);
498 if ((pwtext = strchr(user_and_password, '%')) == NULL) {
499 err = stralloc2("password field not \'user%pass\' for ", amdevice);
503 pwtext_len = strlen(pwtext);
504 if ((device = makesharename(share, 0)) == NULL) {
505 err = stralloc2("cannot make share name of ", share);
509 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
510 err = stralloc2("Cannot access /dev/null : ", strerror(errno));
514 if (pwtext_len > 0) {
515 pw_fd_env = "PASSWD_FD";
517 pw_fd_env = "dummy_PASSWD_FD";
519 checkpid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE,
520 &nullfd, &nullfd, &checkerr,
521 pw_fd_env, &passwdfd,
524 *user_and_password ? "-U" : skip_argument,
525 *user_and_password ? user_and_password : skip_argument,
527 domain ? "-W" : skip_argument,
528 domain ? domain : skip_argument,
529 #if SAMBA_VERSION >= 2
530 subdir ? "-D" : skip_argument,
531 subdir ? subdir : skip_argument,
536 memset(domain, '\0', strlen(domain));
540 if (pwtext_len > 0 && fullwrite(passwdfd, pwtext, pwtext_len) < 0) {
541 err = vstralloc("password write failed: ",
549 memset(user_and_password, '\0', lpass);
550 amfree(user_and_password);
552 ferr = fdopen(checkerr, "r");
555 for(sep = ""; (line = agets(ferr)) != NULL; free(line)) {
556 strappend(extra_info, sep);
557 strappend(extra_info, line);
559 if(strstr(line, "ERRDOS") != NULL) {
566 while ((wpid = wait(&retstat)) != -1) {
567 if (WIFSIGNALED(retstat)) {
569 rc = sig = WTERMSIG(retstat);
572 rc = ret = WEXITSTATUS(retstat);
577 strappend(err, "got signal ");
580 strappend(err, "returned ");
582 snprintf(number, sizeof(number), "%d", ret);
583 strappend(err, number);
586 if (errdos != 0 || rc != 0) {
587 err = newvstralloc(err,
588 "samba access error: ",
591 extra_info ? extra_info : "",
597 err = stralloc2("This client is not configured for samba: ", disk);
602 device = amname_to_dirname(amdevice);
603 } else if (strcmp(program, "DUMP") == 0) {
604 if(amdevice[0] == '/' && amdevice[1] == '/') {
605 err = vstralloc("The DUMP program cannot handle samba shares,",
613 if (strcmp(amname_to_fstype(amdevice), "advfs") == 0)
618 device = amname_to_dirname(amdevice);
623 device = amname_to_devname(amdevice);
631 else { /* program_is_wrapper==1 */
633 fflush(stdout);fflush(stdin);
634 switch (pid_wrapper = fork()) {
635 case -1: error("fork: %s", strerror(errno));
639 char *cmd = vstralloc(DUMPER_DIR, "/", program, NULL);
640 argvchild[0] = program;
641 argvchild[1] = "selfcheck";
643 argvchild[3] = amdevice;
644 argvchild[4] = optstr;
646 execve(cmd,argvchild,safe_env());
649 default: /* parent */
652 waitpid(pid_wrapper, &status, 0);
655 fflush(stdout);fflush(stdin);
659 dbprintf(("%s: device %s\n", debug_prefix_time(NULL), device));
661 /* skip accessability test if this is an AFS entry */
662 if(strncmp(device, "afs:", 4) != 0) {
663 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
664 access_result = open(device, O_RDONLY);
665 access_type = "open";
667 access_result = access(device, amode);
668 access_type = "access";
670 if(access_result == -1) {
671 err = vstralloc("could not ", access_type, " ", device,
672 " (", disk, "): ", strerror(errno), NULL);
674 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
675 aclose(access_result);
683 if(user_and_password) {
684 memset(user_and_password, '\0', lpass);
685 amfree(user_and_password);
688 memset(domain, '\0', strlen(domain));
693 printf("ERROR [%s]\n", err);
694 dbprintf(("%s: %s\n", debug_prefix_time(NULL), err));
697 printf("OK %s\n", disk);
698 dbprintf(("%s: disk \"%s\" OK\n", debug_prefix_time(NULL), disk));
699 printf("OK %s\n", amdevice);
700 dbprintf(("%s: amdevice \"%s\" OK\n",
701 debug_prefix_time(NULL), amdevice));
702 printf("OK %s\n", device);
703 dbprintf(("%s: device \"%s\" OK\n", debug_prefix_time(NULL), device));
706 dbprintf(("%s: extra info: %s\n", debug_prefix_time(NULL), extra_info));
711 /* XXX perhaps do something with level: read dumpdates and sanity check */
714 static void check_overall()
722 cmd = vstralloc(libexecdir, "/", "runtar", versionsuffix(), NULL);
723 check_file(cmd,X_OK);
730 cmd = vstralloc(libexecdir, "/", "rundump", versionsuffix(), NULL);
731 check_file(cmd,X_OK);
738 check_file(DUMP, X_OK);
740 printf("ERROR [DUMP program not available]\n");
746 check_file(RESTORE, X_OK);
748 printf("ERROR [RESTORE program not available]\n");
754 check_file(VDUMP, X_OK);
756 printf("ERROR [VDUMP program not available]\n");
760 if ( need_vrestore ) {
762 check_file(VRESTORE, X_OK);
764 printf("ERROR [VRESTORE program not available]\n");
770 check_file(XFSDUMP, F_OK);
772 printf("ERROR [XFSDUMP program not available]\n");
776 if( need_xfsrestore ) {
778 check_file(XFSRESTORE, X_OK);
780 printf("ERROR [XFSRESTORE program not available]\n");
786 check_file(VXDUMP, X_OK);
788 printf("ERROR [VXDUMP program not available]\n");
792 if( need_vxrestore ) {
794 check_file(VXRESTORE, X_OK);
796 printf("ERROR [VXRESTORE program not available]\n");
802 check_file(GNUTAR, X_OK);
804 printf("ERROR [GNUTAR program not available]\n");
806 #ifdef AMANDATES_FILE
807 check_file(AMANDATES_FILE, R_OK|W_OK);
809 #ifdef GNUTAR_LISTED_INCREMENTAL_DIR
810 check_dir(GNUTAR_LISTED_INCREMENTAL_DIR,R_OK|W_OK);
814 if( need_calcsize ) {
817 cmd = vstralloc(libexecdir, "/", "calcsize", versionsuffix(), NULL);
819 check_file(cmd, X_OK);
826 check_file(SAMBA_CLIENT, X_OK);
828 printf("ERROR [SMBCLIENT program not available]\n");
830 testfd = open("/etc/amandapass", R_OK);
832 if(fstat(testfd, &buf) == 0) {
833 if ((buf.st_mode & 0x7) != 0) {
834 printf("ERROR [/etc/amandapass is world readable!]\n");
836 printf("OK [/etc/amandapass is readable, but not by all]\n");
839 printf("OK [unable to stat /etc/amandapass: %s]\n",
844 printf("ERROR [unable to open /etc/amandapass: %s]\n",
849 if( need_compress_path )
850 check_file(COMPRESS_PATH, X_OK);
852 if( need_dump || need_xfsdump )
853 check_file("/etc/dumpdates",
862 check_file("/etc/vdumpdates", F_OK);
864 check_access("/dev/null", R_OK|W_OK);
865 check_space(AMANDA_TMPDIR, 64); /* for amandad i/o */
868 check_space(AMANDA_DBGDIR, 64); /* for amandad i/o */
871 check_space("/etc", 64); /* for /etc/dumpdates writing */
874 static void check_space(dir, kbytes)
878 generic_fs_stats_t statp;
880 if(get_fs_stats(dir, &statp) == -1)
881 printf("ERROR [cannot statfs %s: %s]\n", dir, strerror(errno));
882 else if(statp.avail < kbytes)
883 printf("ERROR [dir %s needs %ldKB, only has %ldKB available.]\n",
884 dir, kbytes, statp.avail);
886 printf("OK %s has more than %ld KB available.\n", dir, kbytes);
889 static void check_access(filename, mode)
893 char *noun, *adjective;
896 noun = "find", adjective = "exists";
897 else if((mode & X_OK) == X_OK)
898 noun = "execute", adjective = "executable";
899 else if((mode & (W_OK|R_OK)) == (W_OK|R_OK))
900 noun = "read/write", adjective = "read/writable";
902 noun = "access", adjective = "accessible";
904 if(access(filename, mode) == -1)
905 printf("ERROR [can not %s %s: %s]\n", noun, filename, strerror(errno));
907 printf("OK %s %s\n", filename, adjective);
910 static void check_file(filename, mode)
914 struct stat stat_buf;
915 if(!stat(filename, &stat_buf)) {
916 if(!S_ISREG(stat_buf.st_mode)) {
917 printf("ERROR [%s is not a file]\n", filename);
920 check_access(filename, mode);
923 static void check_dir(dirname, mode)
927 struct stat stat_buf;
930 if(!stat(dirname, &stat_buf)) {
931 if(!S_ISDIR(stat_buf.st_mode)) {
932 printf("ERROR [%s is not a directory]\n", dirname);
935 dir = stralloc2(dirname, "/.");
936 check_access(dir, mode);
940 static void check_suid(filename)
943 /* The following is only valid for real Unixs */
944 #ifndef IGNORE_UID_CHECK
945 struct stat stat_buf;
946 if(!stat(filename, &stat_buf)) {
947 if(stat_buf.st_uid != 0 ) {
948 printf("ERROR [%s is not owned by root]\n",filename);
950 if((stat_buf.st_mode & S_ISUID) != S_ISUID) {
951 printf("ERROR [%s is not SUID root]\n",filename);
955 printf("ERROR [can not stat %s]\n",filename);