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 2003/11/18 18:04:26 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;
62 static am_feature_t *our_features = NULL;
63 static char *our_feature_string = NULL;
64 static g_option_t *g_options = NULL;
67 int main P((int argc, char **argv));
69 static void check_options P((char *program, char *disk, char *device, option_t *options));
70 static void check_disk P((char *program, char *disk, char *amdevice, int level));
71 static void check_overall P((void));
72 static void check_access P((char *filename, int mode));
73 static void check_file P((char *filename, int mode));
74 static void check_dir P((char *dirname, int mode));
75 static void check_suid P((char *filename));
76 static void check_space P((char *dir, long kbytes));
88 char *err_extra = NULL;
92 unsigned long malloc_hist_1, malloc_size_1;
93 unsigned long malloc_hist_2, malloc_size_2;
98 for(fd = 3; fd < FD_SETSIZE; fd++) {
100 * Make sure nobody spoofs us with a lot of extra open files
101 * that would cause an open we do to get a very high file
102 * descriptor, which in turn might be used as an index into
103 * an array (e.g. an fd_set).
110 set_pname("selfcheck");
112 malloc_size_1 = malloc_inuse(&malloc_hist_1);
114 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
117 dbprintf(("%s: version %s\n", argv[0], version()));
119 our_features = am_init_feature_set();
120 our_feature_string = am_feature_to_string(our_features);
122 /* handle all service requests */
124 for(; (line = agets(stdin)) != NULL; free(line)) {
125 #define sc "OPTIONS "
126 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
128 g_options = parse_g_options(line+8, 1);
129 if(!g_options->hostname) {
130 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
131 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
132 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
136 if(am_has_feature(g_options->features, fe_rep_options_features)) {
137 printf("features=%s;", our_feature_string);
139 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
140 printf("hostname=%s;", g_options->hostname);
150 skip_whitespace(s, ch); /* find program name */
152 goto err; /* no program */
155 skip_non_whitespace(s, ch);
156 s[-1] = '\0'; /* terminate the program name */
158 skip_whitespace(s, ch); /* find disk name */
160 goto err; /* no disk */
163 skip_non_whitespace(s, ch);
164 s[-1] = '\0'; /* terminate the disk name */
166 skip_whitespace(s, ch); /* find the device or level */
168 goto err; /* no device or level */
170 if(!isdigit((int)s[-1])) {
172 skip_non_whitespace(s, ch);
173 s[-1] = '\0'; /* terminate the device */
174 device = stralloc(fp);
175 skip_whitespace(s, ch); /* find level number */
178 device = stralloc(disk);
181 /* find level number */
182 if (ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
183 goto err; /* bad level */
187 skip_whitespace(s, ch);
188 #define sc "OPTIONS "
189 if (ch && strncmp (s - 1, sc, sizeof(sc)-1) == 0) {
193 skip_whitespace(s, ch); /* find the option string */
195 goto err; /* bad options string */
198 skip_non_whitespace(s, ch);
199 s[-1] = '\0'; /* terminate the options */
200 options = parse_options(optstr, disk, device, g_options->features, 1);
201 check_options(program, disk, device, options);
202 check_disk(program, disk, device, level);
203 free_sl(options->exclude_file);
204 free_sl(options->exclude_list);
205 free_sl(options->include_file);
206 free_sl(options->include_list);
207 amfree(options->str);
209 } else if (ch == '\0') {
210 /* check all since no option */
223 need_compress_path=1;
224 check_disk(program, disk, device, level);
226 goto err; /* bad syntax */
234 amfree(our_feature_string);
235 am_release_feature_set(our_features);
237 am_release_feature_set(g_options->features);
238 g_options->features = NULL;
239 amfree(g_options->str);
240 amfree(g_options->hostname);
243 malloc_size_2 = malloc_inuse(&malloc_hist_2);
245 if(malloc_size_1 != malloc_size_2) {
246 #if defined(USE_DBMALLOC)
249 malloc_list(dbfd(), malloc_hist_1, malloc_hist_2);
257 printf("ERROR [BOGUS REQUEST PACKET]\n");
258 dbprintf(("%s: REQ packet is bogus%s%s\n",
259 debug_prefix_time(NULL),
260 err_extra ? ": " : "",
261 err_extra ? err_extra : ""));
268 check_options(program, disk, device, options)
269 char *program, *disk, *device;
272 if(strcmp(program,"GNUTAR") == 0) {
274 if(disk[0] == '/' && disk[1] == '/') {
275 if(options->exclude_file && options->exclude_file->nb_element > 1) {
276 printf("ERROR [samba support only one exclude file]\n");
278 if(options->exclude_list && options->exclude_list->nb_element > 0 &&
279 options->exclude_optional==0) {
280 printf("ERROR [samba does not support exclude list]\n");
282 if(options->include_file && options->include_file->nb_element > 0) {
283 printf("ERROR [samba does not support include file]\n");
285 if(options->include_list && options->include_list->nb_element > 0 &&
286 options->include_optional==0) {
287 printf("ERROR [samba does not support include list]\n");
294 char *file_exclude = NULL;
295 char *file_include = NULL;
297 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
298 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
299 if(options->include_file) nb_include += options->include_file->nb_element;
300 if(options->include_list) nb_include += options->include_list->nb_element;
302 if(nb_exclude > 0) file_exclude = build_exclude(disk, device, options, 1);
303 if(nb_include > 0) file_include = build_include(disk, device, options, 1);
305 amfree(file_exclude);
306 amfree(file_include);
311 if(strcmp(program,"DUMP") == 0) {
312 if(options->exclude_file && options->exclude_file->nb_element > 0) {
313 printf("ERROR [DUMP does not support exclude file]\n");
315 if(options->exclude_list && options->exclude_list->nb_element > 0) {
316 printf("ERROR [DUMP does not support exclude list]\n");
318 if(options->include_file && options->include_file->nb_element > 0) {
319 printf("ERROR [DUMP does not support include file]\n");
321 if(options->include_list && options->include_list->nb_element > 0) {
322 printf("ERROR [DUMP does not support include list]\n");
330 if (strcmp(amname_to_fstype(disk), "advfs") == 0)
337 if (options->createindex)
344 if (strcmp(amname_to_fstype(disk), "xfs") == 0)
351 if (options->createindex)
358 if (strcmp(amname_to_fstype(disk), "vxfs") == 0)
364 if (options->createindex)
371 if (options->createindex)
375 /* AIX backup program */
377 if (options->createindex)
381 if(options->compress == COMPR_BEST || options->compress == COMPR_FAST)
382 need_compress_path=1;
385 static void check_disk(program, disk, amdevice, level)
386 char *program, *disk, *amdevice;
391 char *user_and_password = NULL, *domain = NULL;
392 char *share = NULL, *subdir = NULL;
397 char *extra_info = NULL;
399 dbprintf(("%s: checking disk %s\n", debug_prefix_time(NULL), disk));
401 if (strcmp(program, "GNUTAR") == 0) {
402 if(amdevice[0] == '/' && amdevice[1] == '/') {
404 int nullfd, checkerr;
410 char number[NUM_STR_SIZE];
419 parsesharename(amdevice, &share, &subdir);
421 err = stralloc2("cannot parse for share/subdir disk entry ", amdevice);
424 if ((subdir) && (SAMBA_VERSION < 2)) {
425 err = vstralloc("subdirectory specified for share '",
427 "' but samba not v2 or better",
431 if ((user_and_password = findpass(share, &domain)) == NULL) {
432 err = stralloc2("cannot find password for ", amdevice);
435 lpass = strlen(user_and_password);
436 if ((pwtext = strchr(user_and_password, '%')) == NULL) {
437 err = stralloc2("password field not \'user%pass\' for ", amdevice);
441 pwtext_len = strlen(pwtext);
442 if ((device = makesharename(share, 0)) == NULL) {
443 err = stralloc2("cannot make share name of ", share);
447 nullfd = open("/dev/null", O_RDWR);
448 if (pwtext_len > 0) {
449 pw_fd_env = "PASSWD_FD";
451 pw_fd_env = "dummy_PASSWD_FD";
453 checkpid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE,
454 &nullfd, &nullfd, &checkerr,
455 pw_fd_env, &passwdfd,
458 *user_and_password ? "-U" : skip_argument,
459 *user_and_password ? user_and_password : skip_argument,
461 domain ? "-W" : skip_argument,
462 domain ? domain : skip_argument,
463 #if SAMBA_VERSION >= 2
464 subdir ? "-D" : skip_argument,
465 subdir ? subdir : skip_argument,
470 memset(domain, '\0', strlen(domain));
474 if (pwtext_len > 0 && fullwrite(passwdfd, pwtext, pwtext_len) < 0) {
475 err = vstralloc("password write failed: ",
483 memset(user_and_password, '\0', lpass);
484 amfree(user_and_password);
486 ferr = fdopen(checkerr, "r");
489 for(sep = ""; (line = agets(ferr)) != NULL; free(line)) {
490 strappend(extra_info, sep);
491 strappend(extra_info, line);
493 if(strstr(line, "ERRDOS") != NULL) {
500 while ((wpid = wait(&retstat)) != -1) {
501 if (WIFSIGNALED(retstat)) {
503 rc = sig = WTERMSIG(retstat);
506 rc = ret = WEXITSTATUS(retstat);
511 strappend(err, "got signal ");
514 strappend(err, "returned ");
516 ap_snprintf(number, sizeof(number), "%d", ret);
517 strappend(err, number);
520 if (errdos != 0 || rc != 0) {
521 err = newvstralloc(err,
522 "samba access error: ",
525 extra_info ? extra_info : "",
531 err = stralloc2("This client is not configured for samba: ", amdevice);
536 device = amname_to_dirname(amdevice);
538 if(amdevice[0] == '/' && amdevice[1] == '/') {
539 err = vstralloc("The DUMP program cannot handle samba shares,",
547 if (strcmp(amname_to_fstype(amdevice), "advfs") == 0)
552 device = amname_to_dirname(amdevice);
557 device = amname_to_devname(amdevice);
566 dbprintf(("%s: device %s\n", debug_prefix_time(NULL), device));
568 /* skip accessability test if this is an AFS entry */
569 if(strncmp(device, "afs:", 4) != 0) {
570 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
571 access_result = open(device, O_RDONLY);
572 access_type = "open";
574 access_result = access(device, amode);
575 access_type = "access";
577 if(access_result == -1) {
578 err = vstralloc("could not ", access_type, " ", device,
579 " (", disk, "): ", strerror(errno), NULL);
581 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
582 aclose(access_result);
590 if(user_and_password) {
591 memset(user_and_password, '\0', lpass);
592 amfree(user_and_password);
595 memset(domain, '\0', strlen(domain));
600 printf("ERROR [%s]\n", err);
601 dbprintf(("%s: %s\n", debug_prefix_time(NULL), err));
604 printf("OK %s\n", disk);
605 dbprintf(("%s: disk \"%s\" OK\n", debug_prefix_time(NULL), disk));
606 printf("OK %s\n", amdevice);
607 dbprintf(("%s: amdevice \"%s\" OK\n",
608 debug_prefix_time(NULL), amdevice));
609 printf("OK %s\n", device);
610 dbprintf(("%s: device \"%s\" OK\n", debug_prefix_time(NULL), device));
613 dbprintf(("%s: extra info: %s\n", debug_prefix_time(NULL), extra_info));
618 /* XXX perhaps do something with level: read dumpdates and sanity check */
621 static void check_overall()
629 cmd = vstralloc(libexecdir, "/", "runtar", versionsuffix(), NULL);
630 check_file(cmd,X_OK);
637 cmd = vstralloc(libexecdir, "/", "rundump", versionsuffix(), NULL);
638 check_file(cmd,X_OK);
645 check_file(DUMP, X_OK);
647 printf("ERROR [DUMP program not available]\n");
653 check_file(RESTORE, X_OK);
655 printf("ERROR [RESTORE program not available]\n");
661 check_file(VDUMP, X_OK);
663 printf("ERROR [VDUMP program not available]\n");
667 if ( need_vrestore ) {
669 check_file(VRESTORE, X_OK);
671 printf("ERROR [VRESTORE program not available]\n");
677 check_file(XFSDUMP, F_OK);
679 printf("ERROR [XFSDUMP program not available]\n");
683 if( need_xfsrestore ) {
685 check_file(XFSRESTORE, X_OK);
687 printf("ERROR [XFSRESTORE program not available]\n");
693 check_file(VXDUMP, X_OK);
695 printf("ERROR [VXDUMP program not available]\n");
699 if( need_vxrestore ) {
701 check_file(VXRESTORE, X_OK);
703 printf("ERROR [VXRESTORE program not available]\n");
709 check_file(GNUTAR, X_OK);
711 printf("ERROR [GNUTAR program not available]\n");
713 #ifdef AMANDATES_FILE
714 check_file(AMANDATES_FILE, R_OK|W_OK);
716 #ifdef GNUTAR_LISTED_INCREMENTAL_DIR
717 check_dir(GNUTAR_LISTED_INCREMENTAL_DIR,R_OK|W_OK);
723 check_file(SAMBA_CLIENT, X_OK);
725 printf("ERROR [SMBCLIENT program not available]\n");
727 testfd = open("/etc/amandapass", R_OK);
729 if(fstat(testfd, &buf) == 0) {
730 if ((buf.st_mode & 0x7) != 0) {
731 printf("ERROR [/etc/amandapass is world readable!]\n");
733 printf("OK [/etc/amandapass is readable, but not by all]\n");
736 printf("OK [unable to stat /etc/amandapass: %s]\n",
741 printf("ERROR [unable to open /etc/amandapass: %s]\n",
746 if( need_compress_path ) {
747 check_file(COMPRESS_PATH, X_OK);
750 if( need_dump || need_xfsdump )
751 check_file("/etc/dumpdates",
760 check_file("/etc/vdumpdates", F_OK);
763 check_access("/dev/null", R_OK|W_OK);
764 check_space(AMANDA_TMPDIR, 64); /* for amandad i/o */
767 check_space(AMANDA_DBGDIR, 64); /* for amandad i/o */
770 check_space("/etc", 64); /* for /etc/dumpdates writing */
773 static void check_space(dir, kbytes)
777 generic_fs_stats_t statp;
779 if(get_fs_stats(dir, &statp) == -1)
780 printf("ERROR [cannot statfs %s: %s]\n", dir, strerror(errno));
781 else if(statp.avail < kbytes)
782 printf("ERROR [dir %s needs %ldKB, only has %ldKB available.]\n",
783 dir, kbytes, statp.avail);
785 printf("OK %s has more than %ld KB available.\n", dir, kbytes);
788 static void check_access(filename, mode)
792 char *noun, *adjective;
795 noun = "find", adjective = "exists";
796 else if((mode & X_OK) == X_OK)
797 noun = "execute", adjective = "executable";
798 else if((mode & (W_OK|R_OK)) == (W_OK|R_OK))
799 noun = "read/write", adjective = "read/writable";
801 noun = "access", adjective = "accessible";
803 if(access(filename, mode) == -1)
804 printf("ERROR [can not %s %s: %s]\n", noun, filename, strerror(errno));
806 printf("OK %s %s\n", filename, adjective);
809 static void check_file(filename, mode)
813 struct stat stat_buf;
814 if(!stat(filename, &stat_buf)) {
815 if(!S_ISREG(stat_buf.st_mode)) {
816 printf("ERROR [%s is not a file]\n", filename);
819 check_access(filename, mode);
822 static void check_dir(dirname, mode)
826 struct stat stat_buf;
829 if(!stat(dirname, &stat_buf)) {
830 if(!S_ISDIR(stat_buf.st_mode)) {
831 printf("ERROR [%s is not a directory]\n", dirname);
834 dir = stralloc2(dirname, "/.");
835 check_access(dir, mode);
839 static void check_suid(filename)
842 /* The following is only valid for real Unixs */
843 #ifndef IGNORE_UID_CHECK
844 struct stat stat_buf;
845 if(!stat(filename, &stat_buf)) {
846 if(stat_buf.st_uid != 0 ) {
847 printf("ERROR [%s is not owned by root]\n",filename);
849 if((stat_buf.st_mode & S_ISUID) != S_ISUID) {
850 printf("ERROR [%s is not SUID root]\n",filename);
854 printf("ERROR [can not stat %s]\n",filename);