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.3 2005/09/20 21:31:52 jrjackson 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;
93 unsigned long malloc_hist_1, malloc_size_1;
94 unsigned long malloc_hist_2, malloc_size_2;
102 set_pname("selfcheck");
104 malloc_size_1 = malloc_inuse(&malloc_hist_1);
106 erroutput_type = (ERR_INTERACTIVE|ERR_SYSLOG);
109 dbprintf(("%s: version %s\n", argv[0], version()));
111 our_features = am_init_feature_set();
112 our_feature_string = am_feature_to_string(our_features);
114 /* handle all service requests */
116 for(; (line = agets(stdin)) != NULL; free(line)) {
117 #define sc "OPTIONS "
118 if(strncmp(line, sc, sizeof(sc)-1) == 0) {
120 g_options = parse_g_options(line+8, 1);
121 if(!g_options->hostname) {
122 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
123 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
124 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
128 if(am_has_feature(g_options->features, fe_rep_options_features)) {
129 printf("features=%s;", our_feature_string);
131 if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
132 printf("hostname=%s;", g_options->hostname);
142 skip_whitespace(s, ch); /* find program name */
144 goto err; /* no program */
147 skip_non_whitespace(s, ch);
148 s[-1] = '\0'; /* terminate the program name */
150 if(strncmp(program, "CALCSIZE", 8) == 0) {
151 skip_whitespace(s, ch); /* find program name */
153 goto err; /* no program */
156 skip_non_whitespace(s, ch);
163 skip_whitespace(s, ch); /* find disk name */
165 goto err; /* no disk */
168 skip_non_whitespace(s, ch);
169 s[-1] = '\0'; /* terminate the disk name */
171 skip_whitespace(s, ch); /* find the device or level */
173 goto err; /* no device or level */
175 if(!isdigit((int)s[-1])) {
177 skip_non_whitespace(s, ch);
178 s[-1] = '\0'; /* terminate the device */
179 device = stralloc(fp);
180 skip_whitespace(s, ch); /* find level number */
183 device = stralloc(disk);
186 /* find level number */
187 if (ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
188 goto err; /* bad level */
192 skip_whitespace(s, ch);
193 #define sc "OPTIONS "
194 if (ch && strncmp (s - 1, sc, sizeof(sc)-1) == 0) {
198 skip_whitespace(s, ch); /* find the option string */
200 goto err; /* bad options string */
203 skip_non_whitespace(s, ch);
204 s[-1] = '\0'; /* terminate the options */
205 options = parse_options(optstr, disk, device, g_options->features, 1);
206 check_options(program, calcprog, disk, device, options);
207 check_disk(program, calcprog, disk, device, level);
208 free_sl(options->exclude_file);
209 free_sl(options->exclude_list);
210 free_sl(options->include_file);
211 free_sl(options->include_list);
212 amfree(options->str);
214 } else if (ch == '\0') {
215 /* check all since no option */
228 need_compress_path=1;
230 check_disk(program, calcprog, disk, device, level);
232 goto err; /* bad syntax */
240 amfree(our_feature_string);
241 am_release_feature_set(our_features);
243 am_release_feature_set(g_options->features);
244 g_options->features = NULL;
245 amfree(g_options->str);
246 amfree(g_options->hostname);
249 malloc_size_2 = malloc_inuse(&malloc_hist_2);
251 if(malloc_size_1 != malloc_size_2) {
252 #if defined(USE_DBMALLOC)
255 malloc_list(dbfd(), malloc_hist_1, malloc_hist_2);
263 printf("ERROR [BOGUS REQUEST PACKET]\n");
264 dbprintf(("%s: REQ packet is bogus%s%s\n",
265 debug_prefix_time(NULL),
266 err_extra ? ": " : "",
267 err_extra ? err_extra : ""));
274 check_options(program, calcprog, disk, device, options)
275 char *program, *calcprog, *disk, *device;
278 char *myprogram = program;
280 if(strcmp(myprogram,"CALCSIZE") == 0) {
283 char *file_exclude = NULL;
284 char *file_include = NULL;
286 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
287 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
288 if(options->include_file) nb_include += options->include_file->nb_element;
289 if(options->include_list) nb_include += options->include_list->nb_element;
291 if(nb_exclude > 0) file_exclude = build_exclude(disk, device, options, 1);
292 if(nb_include > 0) file_include = build_include(disk, device, options, 1);
294 amfree(file_exclude);
295 amfree(file_include);
298 myprogram = calcprog;
301 if(strcmp(myprogram,"GNUTAR") == 0) {
303 if(disk[0] == '/' && disk[1] == '/') {
304 if(options->exclude_file && options->exclude_file->nb_element > 1) {
305 printf("ERROR [samba support only one exclude file]\n");
307 if(options->exclude_list && options->exclude_list->nb_element > 0 &&
308 options->exclude_optional==0) {
309 printf("ERROR [samba does not support exclude list]\n");
311 if(options->include_file && options->include_file->nb_element > 0) {
312 printf("ERROR [samba does not support include file]\n");
314 if(options->include_list && options->include_list->nb_element > 0 &&
315 options->include_optional==0) {
316 printf("ERROR [samba does not support include list]\n");
323 char *file_exclude = NULL;
324 char *file_include = NULL;
326 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
327 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
328 if(options->include_file) nb_include += options->include_file->nb_element;
329 if(options->include_list) nb_include += options->include_list->nb_element;
331 if(nb_exclude > 0) file_exclude = build_exclude(disk, device, options, 1);
332 if(nb_include > 0) file_include = build_include(disk, device, options, 1);
334 amfree(file_exclude);
335 amfree(file_include);
341 if(strcmp(myprogram,"DUMP") == 0) {
342 if(options->exclude_file && options->exclude_file->nb_element > 0) {
343 printf("ERROR [DUMP does not support exclude file]\n");
345 if(options->exclude_list && options->exclude_list->nb_element > 0) {
346 printf("ERROR [DUMP does not support exclude list]\n");
348 if(options->include_file && options->include_file->nb_element > 0) {
349 printf("ERROR [DUMP does not support include file]\n");
351 if(options->include_list && options->include_list->nb_element > 0) {
352 printf("ERROR [DUMP does not support include list]\n");
360 if (strcmp(amname_to_fstype(disk), "advfs") == 0)
367 if (options->createindex)
374 if (strcmp(amname_to_fstype(disk), "xfs") == 0)
381 if (options->createindex)
388 if (strcmp(amname_to_fstype(disk), "vxfs") == 0)
394 if (options->createindex)
401 if (options->createindex)
405 /* AIX backup program */
407 if (options->createindex)
411 if(options->compress == COMPR_BEST || options->compress == COMPR_FAST)
412 need_compress_path=1;
415 static void check_disk(program, calcprog, disk, amdevice, level)
416 char *program, *calcprog, *disk, *amdevice;
421 char *user_and_password = NULL, *domain = NULL;
422 char *share = NULL, *subdir = NULL;
427 char *extra_info = NULL;
428 char *myprogram = program;
430 if(strcmp(myprogram,"CALCSIZE") == 0) {
431 if(amdevice[0] == '/' && amdevice[1] == '/') {
432 err = vstralloc("Can't use CALCSIZE for samba estimate,",
438 myprogram = calcprog;
441 dbprintf(("%s: checking disk %s\n", debug_prefix_time(NULL), disk));
443 if (strcmp(myprogram, "GNUTAR") == 0) {
444 if(amdevice[0] == '/' && amdevice[1] == '/') {
446 int nullfd, checkerr;
452 char number[NUM_STR_SIZE];
461 parsesharename(amdevice, &share, &subdir);
463 err = stralloc2("cannot parse for share/subdir disk entry ", amdevice);
466 if ((subdir) && (SAMBA_VERSION < 2)) {
467 err = vstralloc("subdirectory specified for share '",
469 "' but samba not v2 or better",
473 if ((user_and_password = findpass(share, &domain)) == NULL) {
474 err = stralloc2("cannot find password for ", amdevice);
477 lpass = strlen(user_and_password);
478 if ((pwtext = strchr(user_and_password, '%')) == NULL) {
479 err = stralloc2("password field not \'user%pass\' for ", amdevice);
483 pwtext_len = strlen(pwtext);
484 if ((device = makesharename(share, 0)) == NULL) {
485 err = stralloc2("cannot make share name of ", share);
489 nullfd = open("/dev/null", O_RDWR);
490 if (pwtext_len > 0) {
491 pw_fd_env = "PASSWD_FD";
493 pw_fd_env = "dummy_PASSWD_FD";
495 checkpid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE,
496 &nullfd, &nullfd, &checkerr,
497 pw_fd_env, &passwdfd,
500 *user_and_password ? "-U" : skip_argument,
501 *user_and_password ? user_and_password : skip_argument,
503 domain ? "-W" : skip_argument,
504 domain ? domain : skip_argument,
505 #if SAMBA_VERSION >= 2
506 subdir ? "-D" : skip_argument,
507 subdir ? subdir : skip_argument,
512 memset(domain, '\0', strlen(domain));
516 if (pwtext_len > 0 && fullwrite(passwdfd, pwtext, pwtext_len) < 0) {
517 err = vstralloc("password write failed: ",
525 memset(user_and_password, '\0', lpass);
526 amfree(user_and_password);
528 ferr = fdopen(checkerr, "r");
531 for(sep = ""; (line = agets(ferr)) != NULL; free(line)) {
532 strappend(extra_info, sep);
533 strappend(extra_info, line);
535 if(strstr(line, "ERRDOS") != NULL) {
542 while ((wpid = wait(&retstat)) != -1) {
543 if (WIFSIGNALED(retstat)) {
545 rc = sig = WTERMSIG(retstat);
548 rc = ret = WEXITSTATUS(retstat);
553 strappend(err, "got signal ");
556 strappend(err, "returned ");
558 ap_snprintf(number, sizeof(number), "%d", ret);
559 strappend(err, number);
562 if (errdos != 0 || rc != 0) {
563 err = newvstralloc(err,
564 "samba access error: ",
567 extra_info ? extra_info : "",
573 err = stralloc2("This client is not configured for samba: ", amdevice);
578 device = amname_to_dirname(amdevice);
580 if(amdevice[0] == '/' && amdevice[1] == '/') {
581 err = vstralloc("The DUMP program cannot handle samba shares,",
589 if (strcmp(amname_to_fstype(amdevice), "advfs") == 0)
594 device = amname_to_dirname(amdevice);
599 device = amname_to_devname(amdevice);
608 dbprintf(("%s: device %s\n", debug_prefix_time(NULL), device));
610 /* skip accessability test if this is an AFS entry */
611 if(strncmp(device, "afs:", 4) != 0) {
612 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
613 access_result = open(device, O_RDONLY);
614 access_type = "open";
616 access_result = access(device, amode);
617 access_type = "access";
619 if(access_result == -1) {
620 err = vstralloc("could not ", access_type, " ", device,
621 " (", disk, "): ", strerror(errno), NULL);
623 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
624 aclose(access_result);
632 if(user_and_password) {
633 memset(user_and_password, '\0', lpass);
634 amfree(user_and_password);
637 memset(domain, '\0', strlen(domain));
642 printf("ERROR [%s]\n", err);
643 dbprintf(("%s: %s\n", debug_prefix_time(NULL), err));
646 printf("OK %s\n", disk);
647 dbprintf(("%s: disk \"%s\" OK\n", debug_prefix_time(NULL), disk));
648 printf("OK %s\n", amdevice);
649 dbprintf(("%s: amdevice \"%s\" OK\n",
650 debug_prefix_time(NULL), amdevice));
651 printf("OK %s\n", device);
652 dbprintf(("%s: device \"%s\" OK\n", debug_prefix_time(NULL), device));
655 dbprintf(("%s: extra info: %s\n", debug_prefix_time(NULL), extra_info));
660 /* XXX perhaps do something with level: read dumpdates and sanity check */
663 static void check_overall()
671 cmd = vstralloc(libexecdir, "/", "runtar", versionsuffix(), NULL);
672 check_file(cmd,X_OK);
679 cmd = vstralloc(libexecdir, "/", "rundump", versionsuffix(), NULL);
680 check_file(cmd,X_OK);
687 check_file(DUMP, X_OK);
689 printf("ERROR [DUMP program not available]\n");
695 check_file(RESTORE, X_OK);
697 printf("ERROR [RESTORE program not available]\n");
703 check_file(VDUMP, X_OK);
705 printf("ERROR [VDUMP program not available]\n");
709 if ( need_vrestore ) {
711 check_file(VRESTORE, X_OK);
713 printf("ERROR [VRESTORE program not available]\n");
719 check_file(XFSDUMP, F_OK);
721 printf("ERROR [XFSDUMP program not available]\n");
725 if( need_xfsrestore ) {
727 check_file(XFSRESTORE, X_OK);
729 printf("ERROR [XFSRESTORE program not available]\n");
735 check_file(VXDUMP, X_OK);
737 printf("ERROR [VXDUMP program not available]\n");
741 if( need_vxrestore ) {
743 check_file(VXRESTORE, X_OK);
745 printf("ERROR [VXRESTORE program not available]\n");
751 check_file(GNUTAR, X_OK);
753 printf("ERROR [GNUTAR program not available]\n");
755 #ifdef AMANDATES_FILE
756 check_file(AMANDATES_FILE, R_OK|W_OK);
758 #ifdef GNUTAR_LISTED_INCREMENTAL_DIR
759 check_dir(GNUTAR_LISTED_INCREMENTAL_DIR,R_OK|W_OK);
763 if( need_calcsize ) {
766 cmd = vstralloc(libexecdir, "/", "calcsize", versionsuffix(), NULL);
768 check_file(cmd, X_OK);
775 check_file(SAMBA_CLIENT, X_OK);
777 printf("ERROR [SMBCLIENT program not available]\n");
779 testfd = open("/etc/amandapass", R_OK);
781 if(fstat(testfd, &buf) == 0) {
782 if ((buf.st_mode & 0x7) != 0) {
783 printf("ERROR [/etc/amandapass is world readable!]\n");
785 printf("OK [/etc/amandapass is readable, but not by all]\n");
788 printf("OK [unable to stat /etc/amandapass: %s]\n",
793 printf("ERROR [unable to open /etc/amandapass: %s]\n",
798 if( need_compress_path ) {
799 check_file(COMPRESS_PATH, X_OK);
802 if( need_dump || need_xfsdump )
803 check_file("/var/lib/dumpdates",
812 check_file("/etc/vdumpdates", F_OK);
815 check_access("/dev/null", R_OK|W_OK);
816 check_space(AMANDA_TMPDIR, 64); /* for amandad i/o */
819 check_space(AMANDA_DBGDIR, 64); /* for amandad i/o */
822 check_space("/var/lib", 64); /* for /var/lib/dumpdates writing */
825 static void check_space(dir, kbytes)
829 generic_fs_stats_t statp;
831 if(get_fs_stats(dir, &statp) == -1)
832 printf("ERROR [cannot statfs %s: %s]\n", dir, strerror(errno));
833 else if(statp.avail < kbytes)
834 printf("ERROR [dir %s needs %ldKB, only has %ldKB available.]\n",
835 dir, kbytes, statp.avail);
837 printf("OK %s has more than %ld KB available.\n", dir, kbytes);
840 static void check_access(filename, mode)
844 char *noun, *adjective;
847 noun = "find", adjective = "exists";
848 else if((mode & X_OK) == X_OK)
849 noun = "execute", adjective = "executable";
850 else if((mode & (W_OK|R_OK)) == (W_OK|R_OK))
851 noun = "read/write", adjective = "read/writable";
853 noun = "access", adjective = "accessible";
855 if(access(filename, mode) == -1)
856 printf("ERROR [can not %s %s: %s]\n", noun, filename, strerror(errno));
858 printf("OK %s %s\n", filename, adjective);
861 static void check_file(filename, mode)
865 struct stat stat_buf;
866 if(!stat(filename, &stat_buf)) {
867 if(!S_ISREG(stat_buf.st_mode)) {
868 printf("ERROR [%s is not a file]\n", filename);
871 check_access(filename, mode);
874 static void check_dir(dirname, mode)
878 struct stat stat_buf;
881 if(!stat(dirname, &stat_buf)) {
882 if(!S_ISDIR(stat_buf.st_mode)) {
883 printf("ERROR [%s is not a directory]\n", dirname);
886 dir = stralloc2(dirname, "/.");
887 check_access(dir, mode);
891 static void check_suid(filename)
894 /* The following is only valid for real Unixs */
895 #ifndef IGNORE_UID_CHECK
896 struct stat stat_buf;
897 if(!stat(filename, &stat_buf)) {
898 if(stat_buf.st_uid != 0 ) {
899 printf("ERROR [%s is not owned by root]\n",filename);
901 if((stat_buf.st_mode & S_ISUID) != S_ISUID) {
902 printf("ERROR [%s is not SUID root]\n",filename);
906 printf("ERROR [can not stat %s]\n",filename);