Imported Upstream version 3.3.0
[debian/amanda] / client-src / selfcheck.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27 /* 
28  * $Id: selfcheck.c 10421 2008-03-06 18:48:30Z martineau $
29  *
30  * do self-check and send back any error messages
31  */
32
33 #include "amanda.h"
34 #include "fsusage.h"
35 #include "getfsent.h"
36 #include "amandates.h"
37 #include "clock.h"
38 #include "util.h"
39 #include "pipespawn.h"
40 #include "amfeatures.h"
41 #include "client_util.h"
42 #include "conffile.h"
43 #include "amandad.h"
44 #include "amxml.h"
45 #include "base64.h"
46
47 #ifdef SAMBA_CLIENT
48 #include "findpass.h"
49 #endif
50
51 int need_samba=0;
52 int need_rundump=0;
53 int need_dump=0;
54 int need_restore=0;
55 int need_vdump=0;
56 int need_vrestore=0;
57 int need_xfsdump=0;
58 int need_xfsrestore=0;
59 int need_vxdump=0;
60 int need_vxrestore=0;
61 int need_runtar=0;
62 int need_gnutar=0;
63 int need_compress_path=0;
64 int need_calcsize=0;
65 int need_global_check=0;
66 int program_is_application_api=0;
67
68 static char *amandad_auth = NULL;
69 static am_feature_t *our_features = NULL;
70 static char *our_feature_string = NULL;
71 static g_option_t *g_options = NULL;
72
73 /* local functions */
74 int main(int argc, char **argv);
75
76 static void check_options(dle_t *dle);
77 static void check_disk(dle_t *dle);
78 static void check_overall(void);
79 static int check_file_exist(char *filename);
80 static void check_space(char *dir, off_t kbytes);
81
82 int
83 main(
84     int         argc,
85     char **     argv)
86 {
87     char *line = NULL;
88     char *qdisk = NULL;
89     char *qamdevice = NULL;
90     char *optstr = NULL;
91     char *err_extra = NULL;
92     char *s, *fp;
93     int ch;
94     dle_t *dle;
95     int level;
96     GSList *errlist;
97     level_t *alevel;
98
99     /* initialize */
100
101     /*
102      * Configure program for internationalization:
103      *   1) Only set the message locale for now.
104      *   2) Set textdomain for all amanda related programs to "amanda"
105      *      We don't want to be forced to support dozens of message catalogs.
106      */  
107     setlocale(LC_MESSAGES, "C");
108     textdomain("amanda"); 
109
110     safe_fd(-1, 0);
111     openbsd_fd_inform();
112     safe_cd();
113
114     set_pname("selfcheck");
115
116     /* Don't die when child closes pipe */
117     signal(SIGPIPE, SIG_IGN);
118
119     add_amanda_log_handler(amanda_log_stderr);
120     add_amanda_log_handler(amanda_log_syslog);
121     dbopen(DBG_SUBDIR_CLIENT);
122     startclock();
123     dbprintf(_("version %s\n"), VERSION);
124
125     if(argc > 2 && strcmp(argv[1], "amandad") == 0) {
126         amandad_auth = stralloc(argv[2]);
127     }
128
129     config_init(CONFIG_INIT_CLIENT, NULL);
130     /* (check for config errors comes later) */
131
132     check_running_as(RUNNING_AS_CLIENT_LOGIN);
133
134     our_features = am_init_feature_set();
135     our_feature_string = am_feature_to_string(our_features);
136
137     /* handle all service requests */
138
139     /*@ignore@*/
140     for(; (line = agets(stdin)) != NULL; free(line)) {
141     /*@end@*/
142         if (line[0] == '\0')
143             continue;
144
145         if(strncmp_const(line, "OPTIONS ") == 0) {
146             g_options = parse_g_options(line+8, 1);
147             if(!g_options->hostname) {
148                 g_options->hostname = alloc(MAX_HOSTNAME_LENGTH+1);
149                 gethostname(g_options->hostname, MAX_HOSTNAME_LENGTH);
150                 g_options->hostname[MAX_HOSTNAME_LENGTH] = '\0';
151             }
152
153             g_printf("OPTIONS ");
154             if(am_has_feature(g_options->features, fe_rep_options_features)) {
155                 g_printf("features=%s;", our_feature_string);
156             }
157             if(am_has_feature(g_options->features, fe_rep_options_hostname)) {
158                 g_printf("hostname=%s;", g_options->hostname);
159             }
160             g_printf("\n");
161             fflush(stdout);
162
163             if (g_options->config) {
164                 /* overlay this configuration on the existing (nameless) configuration */
165                 config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
166                             g_options->config);
167
168                 dbrename(get_config_name(), DBG_SUBDIR_CLIENT);
169             }
170
171             /* check for any config errors now */
172             if (config_errors(&errlist) >= CFGERR_ERRORS) {
173                 char *errstr = config_errors_to_error_string(errlist);
174                 g_printf("%s\n", errstr);
175                 dbclose();
176                 return 1;
177             }
178
179             if (am_has_feature(g_options->features, fe_req_xml)) {
180                 break;
181             }
182             continue;
183         }
184
185         dle = alloc_dle();
186         s = line;
187         ch = *s++;
188
189         skip_whitespace(s, ch);                 /* find program name */
190         if (ch == '\0') {
191             goto err;                           /* no program */
192         }
193         dle->program = s - 1;
194         skip_non_whitespace(s, ch);
195         s[-1] = '\0';                           /* terminate the program name */
196
197         dle->program_is_application_api = 0;
198         if(strcmp(dle->program,"APPLICATION")==0) {
199             dle->program_is_application_api = 1;
200             skip_whitespace(s, ch);             /* find dumper name */
201             if (ch == '\0') {
202                 goto err;                       /* no program */
203             }
204             dle->program = s - 1;
205             skip_non_whitespace(s, ch);
206             s[-1] = '\0';                       /* terminate the program name */
207         }
208
209         if(strncmp_const(dle->program, "CALCSIZE") == 0) {
210             skip_whitespace(s, ch);             /* find program name */
211             if (ch == '\0') {
212                 goto err;                       /* no program */
213             }
214             dle->program = s - 1;
215             skip_non_whitespace(s, ch);
216             s[-1] = '\0';
217             dle->estimatelist = g_slist_append(dle->estimatelist,
218                                                GINT_TO_POINTER(ES_CALCSIZE));
219         }
220         else {
221             dle->estimatelist = g_slist_append(dle->estimatelist,
222                                                GINT_TO_POINTER(ES_CLIENT));
223         }
224
225         skip_whitespace(s, ch);                 /* find disk name */
226         if (ch == '\0') {
227             goto err;                           /* no disk */
228         }
229         qdisk = s - 1;
230         skip_quoted_string(s, ch);
231         s[-1] = '\0';                           /* terminate the disk name */
232         dle->disk = unquote_string(qdisk);
233
234         skip_whitespace(s, ch);                 /* find the device or level */
235         if (ch == '\0') {
236             goto err;                           /* no device or level */
237         }
238         if(!isdigit((int)s[-1])) {
239             fp = s - 1;
240             skip_quoted_string(s, ch);
241              s[-1] = '\0';                      /* terminate the device */
242             qamdevice = stralloc(fp);
243             dle->device = unquote_string(qamdevice);
244             skip_whitespace(s, ch);             /* find level number */
245         }
246         else {
247             dle->device = stralloc(dle->disk);
248             qamdevice = stralloc(qdisk);
249         }
250
251                                                 /* find level number */
252         if (ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
253             goto err;                           /* bad level */
254         }
255         alevel = g_new0(level_t, 1);
256         alevel->level = level;
257         dle->levellist = g_slist_append(dle->levellist, alevel);
258         skip_integer(s, ch);
259
260         skip_whitespace(s, ch);
261         if (ch && strncmp_const_skip(s - 1, "OPTIONS ", s, ch) == 0) {
262             skip_whitespace(s, ch);             /* find the option string */
263             if(ch == '\0') {
264                 goto err;                       /* bad options string */
265             }
266             optstr = s - 1;
267             skip_quoted_string(s, ch);
268             s[-1] = '\0';                       /* terminate the options */
269             parse_options(optstr, dle, g_options->features, 1);
270             /*@ignore@*/
271
272             check_options(dle);
273             check_disk(dle);
274
275             /*@end@*/
276         } else if (ch == '\0') {
277             /* check all since no option */
278             need_samba=1;
279             need_rundump=1;
280             need_dump=1;
281             need_restore=1;
282             need_vdump=1;
283             need_vrestore=1;
284             need_xfsdump=1;
285             need_xfsrestore=1;
286             need_vxdump=1;
287             need_vxrestore=1;
288             need_runtar=1;
289             need_gnutar=1;
290             need_compress_path=1;
291             need_calcsize=1;
292             need_global_check=1;
293             /*@ignore@*/
294             check_disk(dle);
295             /*@end@*/
296         } else {
297             goto err;                           /* bad syntax */
298         }
299         amfree(qamdevice);
300     }
301     if (g_options == NULL) {
302         g_printf(_("ERROR [Missing OPTIONS line in selfcheck input]\n"));
303         error(_("Missing OPTIONS line in selfcheck input\n"));
304         /*NOTREACHED*/
305     }
306
307     if (am_has_feature(g_options->features, fe_req_xml)) {
308         char  *errmsg = NULL;
309         dle_t *dles, *dle, *dle_next;
310
311         dles = amxml_parse_node_FILE(stdin, &errmsg);
312         if (errmsg) {
313             err_extra = errmsg;
314             goto err;
315         }
316         if (merge_dles_properties(dles, 1) == 0) {
317             goto checkoverall;
318         }
319         for (dle = dles; dle != NULL; dle = dle->next) {
320             run_client_scripts(EXECUTE_ON_PRE_HOST_AMCHECK, g_options, dle,
321                                stdout);
322         }
323         for (dle = dles; dle != NULL; dle = dle->next) {
324             check_options(dle);
325             run_client_scripts(EXECUTE_ON_PRE_DLE_AMCHECK, g_options, dle,
326                                stdout);
327             check_disk(dle);
328             run_client_scripts(EXECUTE_ON_POST_DLE_AMCHECK, g_options, dle,
329                                stdout);
330         }
331         for (dle = dles; dle != NULL; dle = dle->next) {
332             run_client_scripts(EXECUTE_ON_POST_HOST_AMCHECK, g_options, dle,
333                                stdout);
334         }
335         for (dle = dles; dle != NULL; dle = dle_next) {
336             dle_next = dle->next;
337             free_dle(dle);
338         }
339     }
340
341 checkoverall:
342     check_overall();
343
344     amfree(line);
345     amfree(our_feature_string);
346     am_release_feature_set(our_features);
347     our_features = NULL;
348     free_g_options(g_options);
349
350     dbclose();
351     return 0;
352
353  err:
354     if (err_extra) {
355         g_printf(_("ERROR [FORMAT ERROR IN REQUEST PACKET %s]\n"), err_extra);
356         dbprintf(_("REQ packet is bogus: %s\n"), err_extra);
357     } else {
358         g_printf(_("ERROR [FORMAT ERROR IN REQUEST PACKET]\n"));
359         dbprintf(_("REQ packet is bogus\n"));
360     }
361     dbclose();
362     return 1;
363 }
364
365
366 static void
367 check_options(
368     dle_t *dle)
369 {
370     if (GPOINTER_TO_INT(dle->estimatelist->data) == ES_CALCSIZE) {
371         need_calcsize=1;
372     }
373
374     if (strcmp(dle->program,"GNUTAR") == 0) {
375         need_gnutar=1;
376         if(dle->device && dle->device[0] == '/' && dle->device[1] == '/') {
377             if(dle->exclude_file && dle->exclude_file->nb_element > 1) {
378                 g_printf(_("ERROR [samba support only one exclude file]\n"));
379             }
380             if (dle->exclude_list && dle->exclude_list->nb_element > 0 &&
381                 dle->exclude_optional==0) {
382                 g_printf(_("ERROR [samba does not support exclude list]\n"));
383             }
384             if (dle->include_file && dle->include_file->nb_element > 0) {
385                 g_printf(_("ERROR [samba does not support include file]\n"));
386             }
387             if (dle->include_list && dle->include_list->nb_element > 0 &&
388                 dle->include_optional==0) {
389                 g_printf(_("ERROR [samba does not support include list]\n"));
390             }
391             need_samba=1;
392         } else {
393             int nb_exclude = 0;
394             int nb_include = 0;
395             char *file_exclude = NULL;
396             char *file_include = NULL;
397
398             if (dle->exclude_file) nb_exclude += dle->exclude_file->nb_element;
399             if (dle->exclude_list) nb_exclude += dle->exclude_list->nb_element;
400             if (dle->include_file) nb_include += dle->include_file->nb_element;
401             if (dle->include_list) nb_include += dle->include_list->nb_element;
402
403             if (nb_exclude > 0) file_exclude = build_exclude(dle, 1);
404             if (nb_include > 0) file_include = build_include(dle, 1);
405
406             amfree(file_exclude);
407             amfree(file_include);
408
409             need_runtar=1;
410         }
411     }
412
413     if (strcmp(dle->program,"DUMP") == 0) {
414         if (dle->exclude_file && dle->exclude_file->nb_element > 0) {
415             g_printf(_("ERROR [DUMP does not support exclude file]\n"));
416         }
417         if (dle->exclude_list && dle->exclude_list->nb_element > 0) {
418             g_printf(_("ERROR [DUMP does not support exclude list]\n"));
419         }
420         if (dle->include_file && dle->include_file->nb_element > 0) {
421             g_printf(_("ERROR [DUMP does not support include file]\n"));
422         }
423         if (dle->include_list && dle->include_list->nb_element > 0) {
424             g_printf(_("ERROR [DUMP does not support include list]\n"));
425         }
426 #ifdef USE_RUNDUMP
427         need_rundump=1;
428 #endif
429 #ifndef AIX_BACKUP
430 #ifdef VDUMP
431 #ifdef DUMP
432         if (dle->device && strcmp(amname_to_fstype(dle->device), "advfs") == 0)
433 #else
434         if (1)
435 #endif
436         {
437             need_vdump=1;
438             need_rundump=1;
439             if (dle->create_index)
440                 need_vrestore=1;
441         }
442         else
443 #endif /* VDUMP */
444 #ifdef XFSDUMP
445 #ifdef DUMP
446         if (dle->device && strcmp(amname_to_fstype(dle->device), "xfs") == 0)
447 #else
448         if (1)
449 #endif
450         {
451             need_xfsdump=1;
452             need_rundump=1;
453             if (dle->create_index)
454                 need_xfsrestore=1;
455         }
456         else
457 #endif /* XFSDUMP */
458 #ifdef VXDUMP
459 #ifdef DUMP
460         if (dle->device && strcmp(amname_to_fstype(dle->device), "vxfs") == 0)
461 #else
462         if (1)
463 #endif
464         {
465             need_vxdump=1;
466             if (dle->create_index)
467                 need_vxrestore=1;
468         }
469         else
470 #endif /* VXDUMP */
471         {
472             need_dump=1;
473             if (dle->create_index)
474                 need_restore=1;
475         }
476 #else
477         /* AIX backup program */
478         need_dump=1;
479         if (dle->create_index)
480             need_restore=1;
481 #endif
482     }
483     if ((dle->compress == COMP_BEST) || (dle->compress == COMP_FAST) 
484                 || (dle->compress == COMP_CUST)) {
485         need_compress_path=1;
486     }
487     if (dle->auth && amandad_auth) {
488         if (strcasecmp(dle->auth, amandad_auth) != 0) {
489             g_fprintf(stdout,_("ERROR [client configured for auth=%s while server requested '%s']\n"),
490                     amandad_auth, dle->auth);
491             if (strcmp(dle->auth, "ssh") == 0)  {       
492                 g_fprintf(stderr, _("ERROR [The auth in ~/.ssh/authorized_keys "
493                                   "should be \"--auth=ssh\", or use another auth "
494                                   " for the DLE]\n"));
495             }
496             else {
497                 g_fprintf(stderr, _("ERROR [The auth in the inetd/xinetd configuration "
498                                   " must be the same as the DLE]\n"));
499             }           
500         }
501     }
502 }
503
504 static void
505 check_disk(
506     dle_t *dle)
507 {
508     char *device = NULL;
509     char *err = NULL;
510     char *user_and_password = NULL;
511     char *domain = NULL;
512     char *share = NULL, *subdir = NULL;
513     size_t lpass = 0;
514     int amode = R_OK;
515     int access_result;
516     char *access_type;
517     char *extra_info = NULL;
518     char *qdisk = NULL;
519     char *qamdevice = NULL;
520     char *qdevice = NULL;
521
522     if (dle->disk) {
523         need_global_check=1;
524         qdisk = quote_string(dle->disk);
525         qamdevice = quote_string(dle->device);
526         device = stralloc("nodevice");
527         dbprintf(_("checking disk %s\n"), qdisk);
528         if (GPOINTER_TO_INT(dle->estimatelist->data) == ES_CALCSIZE) {
529             if (dle->device[0] == '/' && dle->device[1] == '/') {
530                 err = vstrallocf(
531                     _("Can't use CALCSIZE for samba estimate, use CLIENT: %s"),
532                     dle->device);
533                 goto common_exit;
534             }
535         }
536
537         if (strcmp(dle->program, "GNUTAR")==0) {
538             if(dle->device[0] == '/' && dle->device[1] == '/') {
539                 #ifdef SAMBA_CLIENT
540                 int nullfd, checkerr;
541                 int passwdfd;
542                 char *pwtext;
543                 size_t pwtext_len;
544                 pid_t checkpid;
545                 amwait_t retstat;
546                 pid_t wpid;
547                 int rc;
548                 char *line;
549                 char *sep;
550                 FILE *ferr;
551                 char *pw_fd_env;
552                 int errdos;
553
554                 parsesharename(dle->device, &share, &subdir);
555                 if (!share) {
556                     err = vstrallocf(
557                               _("cannot parse for share/subdir disk entry %s"),
558                               dle->device);
559                     goto common_exit;
560                 }
561                 if ((subdir) && (SAMBA_VERSION < 2)) {
562                     err = vstrallocf(_("subdirectory specified for share '%s' but, samba is not v2 or better"),
563                                      dle->device);
564                     goto common_exit;
565                 }
566                 if ((user_and_password = findpass(share, &domain)) == NULL) {
567                     err = vstrallocf(_("cannot find password for %s"),
568                                      dle->device);
569                     goto common_exit;
570                 }
571                 lpass = strlen(user_and_password);
572                 if ((pwtext = strchr(user_and_password, '%')) == NULL) {
573                     err = vstrallocf(
574                                 _("password field not \'user%%pass\' for %s"),
575                                 dle->device);
576                     goto common_exit;
577                 }
578                 *pwtext++ = '\0';
579                 pwtext_len = (size_t)strlen(pwtext);
580                 amfree(device);
581                 if ((device = makesharename(share, 0)) == NULL) {
582                     err = vstrallocf(_("cannot make share name of %s"), share);
583                     goto common_exit;
584                 }
585
586                 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
587                     err = vstrallocf(_("Cannot access /dev/null : %s"),
588                                      strerror(errno));
589                     goto common_exit;
590                 }
591
592                 if (pwtext_len > 0) {
593                     pw_fd_env = "PASSWD_FD";
594                 } else {
595                     pw_fd_env = "dummy_PASSWD_FD";
596                 }
597                 checkpid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE, 0,
598                                      &nullfd, &nullfd, &checkerr,
599                                      pw_fd_env, &passwdfd,
600                                      "smbclient",
601                                      device,
602                                      *user_and_password ? "-U" : skip_argument,
603                                      *user_and_password ? user_and_password
604                                                         : skip_argument,
605                                      "-E",
606                                      domain ? "-W" : skip_argument,
607                                      domain ? domain : skip_argument,
608 #if SAMBA_VERSION >= 2
609                                      subdir ? "-D" : skip_argument,
610                                      subdir ? subdir : skip_argument,
611 #endif
612                                      "-c", "quit",
613                                      NULL);
614                 amfree(domain);
615                 aclose(nullfd);
616                 /*@ignore@*/
617                 if ((pwtext_len > 0) &&
618                     full_write(passwdfd, pwtext, pwtext_len) < pwtext_len) {
619                     err = vstrallocf(_("password write failed: %s: %s"),
620                                      dle->device, strerror(errno));
621                     aclose(passwdfd);
622                     goto common_exit;
623                 }
624                 /*@end@*/
625                 memset(user_and_password, '\0', (size_t)lpass);
626                 amfree(user_and_password);
627                 aclose(passwdfd);
628                 ferr = fdopen(checkerr, "r");
629                 if (!ferr) {
630                     g_printf(_("ERROR [Can't fdopen: %s]\n"), strerror(errno));
631                     error(_("Can't fdopen: %s"), strerror(errno));
632                     /*NOTREACHED*/
633                 }
634                 sep = "";
635                 errdos = 0;
636                 for(sep = ""; (line = agets(ferr)) != NULL; free(line)) {
637                     if (line[0] == '\0')
638                         continue;
639                     strappend(extra_info, sep);
640                     strappend(extra_info, line);
641                     sep = ": ";
642                     if(strstr(line, "ERRDOS") != NULL) {
643                         errdos = 1;
644                     }
645                 }
646                 afclose(ferr);
647                 checkerr = -1;
648                 rc = 0;
649                 sep = "";
650                 while ((wpid = wait(&retstat)) != -1) {
651                     if (!WIFEXITED(retstat) || WEXITSTATUS(retstat) != 0) {
652                         char *exitstr = str_exit_status("smbclient", retstat);
653                         strappend(err, sep);
654                         strappend(err, exitstr);
655                         sep = "\n";
656                         amfree(exitstr);
657
658                         rc = 1;
659                     }
660                 }
661                 if (errdos != 0 || rc != 0) {
662                     if (extra_info) {
663                         err = newvstrallocf(err,
664                                             _("samba access error: %s: %s %s"),
665                                             dle->device, extra_info, err);
666                         amfree(extra_info);
667                     } else {
668                         err = newvstrallocf(err,
669                                             _("samba access error: %s: %s"),
670                                            dle->device, err);
671                     }
672                 }
673 #else
674                 err = vstrallocf(
675                               _("This client is not configured for samba: %s"),
676                               qdisk);
677 #endif
678                 goto common_exit;
679             }
680             amode = F_OK;
681             amfree(device);
682             device = amname_to_dirname(dle->device);
683         } else if (strcmp(dle->program, "DUMP") == 0) {
684             if(dle->device[0] == '/' && dle->device[1] == '/') {
685                 err = vstrallocf(
686                   _("The DUMP program cannot handle samba shares, use GNUTAR: %s"),
687                   qdisk);
688                 goto common_exit;
689             }
690 #ifdef VDUMP                                                            /* { */
691 #ifdef DUMP                                                             /* { */
692             if (strcmp(amname_to_fstype(dle->device), "advfs") == 0)
693 #else                                                                   /* }{*/
694             if (1)
695 #endif                                                                  /* } */
696             {
697                 amfree(device);
698                 device = amname_to_dirname(dle->device);
699                 amode = F_OK;
700             } else
701 #endif                                                                  /* } */
702             {
703                 amfree(device);
704                 device = amname_to_devname(dle->device);
705 #ifdef USE_RUNDUMP
706                 amode = F_OK;
707 #else
708                 amode = R_OK;
709 #endif
710             }
711         }
712     }
713     if (dle->program_is_application_api) {
714         pid_t                    application_api_pid;
715         backup_support_option_t *bsu;
716         int                      app_err[2];
717         GPtrArray               *errarray;
718
719         bsu = backup_support_option(dle->program, g_options, dle->disk,
720                                     dle->device, &errarray);
721
722         if (!bsu) {
723             char  *line;
724             guint  i;
725             for (i=0; i < errarray->len; i++) {
726                 line = g_ptr_array_index(errarray, i);
727                 fprintf(stdout, _("ERROR Application '%s': %s\n"),
728                         dle->program, line);
729                 amfree(line);
730             }
731             err = vstrallocf(_("Application '%s': can't run support command"),
732                              dle->program);
733             goto common_exit;
734         }
735
736         if (dle->data_path == DATA_PATH_AMANDA &&
737             (bsu->data_path_set & DATA_PATH_AMANDA)==0) {
738             g_printf("ERROR application %s doesn't support amanda data-path\n",
739                      dle->program);
740         }
741         if (dle->data_path == DATA_PATH_DIRECTTCP &&
742             (bsu->data_path_set & DATA_PATH_DIRECTTCP)==0) {
743             g_printf("ERROR application %s doesn't support directtcp data-path\n",
744                      dle->program);
745         }
746         if (GPOINTER_TO_INT(dle->estimatelist->data) == ES_CALCSIZE &&
747                             !bsu->calcsize) {
748             g_printf("ERROR application %s doesn't support calcsize estimate\n",
749                      dle->program);
750         }
751         if (dle->include_file && dle->include_file->nb_element > 0 &&
752             !bsu->include_file) {
753             g_printf("ERROR application %s doesn't support include-file\n",
754                    dle->program);
755         }
756         if (dle->include_list && dle->include_list->nb_element > 0 &&
757             !bsu->include_list) {
758             g_printf("ERROR application %s doesn't support include-list\n",
759                    dle->program);
760         }
761         if (dle->include_optional && !bsu->include_optional) {
762             g_printf("ERROR application %s doesn't support optional include\n",
763                    dle->program);
764         }
765         if (dle->exclude_file && dle->exclude_file->nb_element > 0 &&
766             !bsu->exclude_file) {
767             g_printf("ERROR application %s doesn't support exclude-file\n",
768                    dle->program);
769         }
770         if (dle->exclude_list && dle->exclude_list->nb_element > 0 &&
771             !bsu->exclude_list) {
772             g_printf("ERROR application %s doesn't support exclude-list\n",
773                    dle->program);
774         }
775         if (dle->exclude_optional && !bsu->exclude_optional) {
776             g_printf("ERROR application %s doesn't support optional exclude\n",
777                    dle->program);
778         }
779         fflush(stdout);fflush(stderr);
780
781         if (pipe(app_err) < 0) {
782             err = vstrallocf(_("Application '%s': can't create pipe"),
783                              dle->program);
784             goto common_exit;
785         }
786
787         switch (application_api_pid = fork()) {
788         case -1:
789             err = vstrallocf(_("fork failed: %s"), strerror(errno));
790             goto common_exit;
791
792         case 0: /* child */
793             {
794                 GPtrArray *argv_ptr = g_ptr_array_new();
795                 guint i;
796                 char *cmd = vstralloc(APPLICATION_DIR, "/", dle->program, NULL);
797                 GSList   *scriptlist;
798                 script_t *script;
799                 estimatelist_t el;
800                 char *cmdline;
801
802                 aclose(app_err[0]);
803                 dup2(app_err[1], 2);
804
805                 g_ptr_array_add(argv_ptr, stralloc(dle->program));
806                 g_ptr_array_add(argv_ptr, stralloc("selfcheck"));
807                 if (bsu->message_line == 1) {
808                     g_ptr_array_add(argv_ptr, stralloc("--message"));
809                     g_ptr_array_add(argv_ptr, stralloc("line"));
810                 }
811                 if (g_options->config != NULL && bsu->config == 1) {
812                     g_ptr_array_add(argv_ptr, stralloc("--config"));
813                     g_ptr_array_add(argv_ptr, stralloc(g_options->config));
814                 }
815                 if (g_options->hostname != NULL && bsu->host == 1) {
816                     g_ptr_array_add(argv_ptr, stralloc("--host"));
817                     g_ptr_array_add(argv_ptr, stralloc(g_options->hostname));
818                 }
819                 if (dle->disk != NULL && bsu->disk == 1) {
820                     g_ptr_array_add(argv_ptr, stralloc("--disk"));
821                     g_ptr_array_add(argv_ptr, stralloc(dle->disk));
822                 }
823                 if (dle->device) {
824                     g_ptr_array_add(argv_ptr, stralloc("--device"));
825                     g_ptr_array_add(argv_ptr, stralloc(dle->device));
826                 }
827                 if (dle->create_index && bsu->index_line == 1) {
828                     g_ptr_array_add(argv_ptr, stralloc("--index"));
829                     g_ptr_array_add(argv_ptr, stralloc("line"));
830                 }
831                 if (dle->record && bsu->record == 1) {
832                     g_ptr_array_add(argv_ptr, stralloc("--record"));
833                 }
834                 
835                 for (el = dle->estimatelist; el != NULL; el=el->next) {
836                     estimate_t estimate = (estimate_t)GPOINTER_TO_INT(el->data);
837                     if (estimate == ES_CALCSIZE && bsu->calcsize == 1) {
838                         g_ptr_array_add(argv_ptr, stralloc("--calcsize"));
839                     }
840                 }
841                 application_property_add_to_argv(argv_ptr, dle, bsu,
842                                                  g_options->features);
843
844                 for (scriptlist = dle->scriptlist; scriptlist != NULL;
845                      scriptlist = scriptlist->next) {
846                     script = (script_t *)scriptlist->data;
847                     if (script->result && script->result->proplist) {
848                         property_add_to_argv(argv_ptr,
849                                              script->result->proplist);
850                     }
851                 }
852
853                 g_ptr_array_add(argv_ptr, NULL);
854
855                 cmdline = stralloc(cmd);
856                 for (i = 0; i < argv_ptr->len-1; i++) {
857                     char *quoted = quote_string(
858                                         (char *)g_ptr_array_index(argv_ptr,i));
859                     cmdline = vstrextend(&cmdline, " ", quoted, NULL);
860                     amfree(quoted);
861                 }
862                 dbprintf(_("Spawning \"%s\" in pipeline\n"), cmdline);
863                 amfree(cmdline);
864
865                 safe_fd(-1, 0);
866                 execve(cmd, (char **)argv_ptr->pdata, safe_env());
867                 g_printf(_("ERROR [Can't execute %s: %s]\n"), cmd, strerror(errno));
868                 exit(127);
869             }
870         default: /* parent */
871             {
872                 int   status;
873                 FILE *app_stderr;
874                 char *line;
875
876                 aclose(app_err[1]);
877                 app_stderr = fdopen(app_err[0], "r");
878                 while((line = agets(app_stderr)) != NULL) {
879                     if (strlen(line) > 0) {
880                         fprintf(stdout, "ERROR Application '%s': %s\n",
881                                 dle->program, line);
882                         dbprintf("ERROR %s\n", line);
883                     }
884                     amfree(line);
885                 }
886                 fclose(app_stderr);
887                 if (waitpid(application_api_pid, &status, 0) < 0) {
888                     err = vstrallocf(_("waitpid failed: %s"),
889                                          strerror(errno));
890                     goto common_exit;
891                 } else if (!WIFEXITED(status)) {
892                     err = vstrallocf(_("Application '%s': exited with signal %d"),
893                                      dle->program, WTERMSIG(status));
894                     goto common_exit;
895                 } else if (WEXITSTATUS(status) != 0) {
896                     err = vstrallocf(_("Application '%s': exited with status %d"),
897                                      dle->program, WEXITSTATUS(status));
898                     goto common_exit;
899                 }
900             }
901         }
902         amfree(bsu);
903         fflush(stdout);fflush(stderr);
904         amfree(device);
905         amfree(qamdevice);
906         amfree(qdisk);
907         return;
908     }
909
910     if (device) {
911         qdevice = quote_string(device);
912         dbprintf(_("device %s\n"), qdevice);
913
914         /* skip accessability test if this is an AFS entry */
915         if(strncmp_const(device, "afs:") != 0) {
916 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
917             access_result = open(device, O_RDONLY);
918             access_type = "open";
919 #else
920             access_result = access(device, amode);
921             access_type = "access";
922 #endif
923             if(access_result == -1) {
924                 err = vstrallocf(_("Could not access %s (%s): %s"),
925                                  qdevice, qdisk, strerror(errno));
926             }
927 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
928             aclose(access_result);
929 #endif
930         }
931     }
932
933 common_exit:
934
935     if (!qdevice)
936         qdevice = quote_string(device);
937
938     amfree(share);
939     amfree(subdir);
940     if(user_and_password) {
941         memset(user_and_password, '\0', (size_t)lpass);
942         amfree(user_and_password);
943     }
944     amfree(domain);
945
946     if(err) {
947         g_printf(_("ERROR %s\n"), err);
948         dbprintf(_("%s\n"), err);
949         amfree(err);
950     } else {
951         if (dle->disk) {
952             g_printf("OK %s\n", qdisk);
953             dbprintf(_("disk %s OK\n"), qdisk);
954         }
955         if (dle->device) {
956             g_printf("OK %s\n", qamdevice);
957             dbprintf(_("amdevice %s OK\n"), qamdevice);
958         }
959         if (device) {
960             g_printf("OK %s\n", qdevice);
961             dbprintf(_("device %s OK\n"), qdevice);
962         }
963     }
964     if(extra_info) {
965         dbprintf(_("extra info: %s\n"), extra_info);
966         amfree(extra_info);
967     }
968     amfree(qdisk);
969     amfree(qdevice);
970     amfree(qamdevice);
971     amfree(device);
972
973     /* XXX perhaps do something with level: read dumpdates and sanity check */
974 }
975
976 static void
977 check_overall(void)
978 {
979     char *cmd;
980     struct stat buf;
981     int testfd;
982     char *gnutar_list_dir;
983     int   need_amandates = 0;
984
985     if( need_runtar )
986     {
987         cmd = vstralloc(amlibexecdir, "/", "runtar", NULL);
988         check_file(cmd,X_OK);
989         check_suid(cmd);
990         amfree(cmd);
991     }
992
993     if( need_rundump )
994     {
995         cmd = vstralloc(amlibexecdir, "/", "rundump", NULL);
996         check_file(cmd,X_OK);
997         check_suid(cmd);
998         amfree(cmd);
999     }
1000
1001     if( need_dump ) {
1002 #ifdef DUMP
1003         check_file(DUMP, X_OK);
1004 #else
1005         g_printf(_("ERROR [DUMP program not available]\n"));
1006 #endif
1007     }
1008
1009     if( need_restore ) {
1010 #ifdef RESTORE
1011         check_file(RESTORE, X_OK);
1012 #else
1013         g_printf(_("ERROR [RESTORE program not available]\n"));
1014 #endif
1015     }
1016
1017     if ( need_vdump ) {
1018 #ifdef VDUMP
1019         check_file(VDUMP, X_OK);
1020 #else
1021         g_printf(_("ERROR [VDUMP program not available]\n"));
1022 #endif
1023     }
1024
1025     if ( need_vrestore ) {
1026 #ifdef VRESTORE
1027         check_file(VRESTORE, X_OK);
1028 #else
1029         g_printf(_("ERROR [VRESTORE program not available]\n"));
1030 #endif
1031     }
1032
1033     if( need_xfsdump ) {
1034 #ifdef XFSDUMP
1035         check_file(XFSDUMP, F_OK);
1036 #else
1037         g_printf(_("ERROR [XFSDUMP program not available]\n"));
1038 #endif
1039     }
1040
1041     if( need_xfsrestore ) {
1042 #ifdef XFSRESTORE
1043         check_file(XFSRESTORE, X_OK);
1044 #else
1045         g_printf(_("ERROR [XFSRESTORE program not available]\n"));
1046 #endif
1047     }
1048
1049     if( need_vxdump ) {
1050 #ifdef VXDUMP
1051         check_file(VXDUMP, X_OK);
1052 #else
1053         g_printf(_("ERROR [VXDUMP program not available]\n"));
1054 #endif
1055     }
1056
1057     if( need_vxrestore ) {
1058 #ifdef VXRESTORE
1059         check_file(VXRESTORE, X_OK);
1060 #else
1061         g_printf(_("ERROR [VXRESTORE program not available]\n"));
1062 #endif
1063     }
1064
1065     if( need_gnutar ) {
1066 #ifdef GNUTAR
1067         check_file(GNUTAR, X_OK);
1068 #else
1069         g_printf(_("ERROR [GNUTAR program not available]\n"));
1070 #endif
1071         gnutar_list_dir = getconf_str(CNF_GNUTAR_LIST_DIR);
1072         if (strlen(gnutar_list_dir) == 0)
1073             gnutar_list_dir = NULL;
1074         if (gnutar_list_dir) {
1075             /* make sure our listed-incremental dir is ready */
1076             check_dir(gnutar_list_dir, R_OK|W_OK);
1077         } else {
1078             /* no listed-incremental dir, so check that amandates is ready */
1079             need_amandates = 1;
1080         }
1081     }
1082
1083     if( need_calcsize ) {
1084         char *cmd;
1085
1086         cmd = vstralloc(amlibexecdir, "/", "calcsize", NULL);
1087
1088         check_file(cmd, X_OK);
1089
1090         amfree(cmd);
1091
1092         /* calcsize uses amandates */
1093         need_amandates = 1;
1094     }
1095
1096     if (need_amandates) {
1097         char *amandates_file;
1098         amandates_file = getconf_str(CNF_AMANDATES);
1099         check_file(amandates_file, R_OK|W_OK);
1100     }
1101
1102     if( need_samba ) {
1103 #ifdef SAMBA_CLIENT
1104         check_file(SAMBA_CLIENT, X_OK);
1105 #else
1106         g_printf(_("ERROR [SMBCLIENT program not available]\n"));
1107 #endif
1108         testfd = open("/etc/amandapass", R_OK);
1109         if (testfd >= 0) {
1110             if(fstat(testfd, &buf) == 0) {
1111                 if ((buf.st_mode & 0x7) != 0) {
1112                     g_printf(_("ERROR [/etc/amandapass is world readable!]\n"));
1113                 } else {
1114                     g_printf(_("OK [/etc/amandapass is readable, but not by all]\n"));
1115                 }
1116             } else {
1117                 g_printf(_("OK [unable to stat /etc/amandapass: %s]\n"),
1118                        strerror(errno));
1119             }
1120             aclose(testfd);
1121         } else {
1122             g_printf(_("ERROR [unable to open /etc/amandapass: %s]\n"),
1123                    strerror(errno));
1124         }
1125     }
1126
1127     if (need_compress_path )
1128         check_file(COMPRESS_PATH, X_OK);
1129
1130     if (need_dump || need_xfsdump ) {
1131         if (check_file_exist("/etc/dumpdates")) {
1132             check_file("/etc/dumpdates",
1133 #ifdef USE_RUNDUMP
1134                        F_OK
1135 #else
1136                        R_OK|W_OK
1137 #endif
1138                       );
1139         } else {
1140 #ifndef USE_RUNDUMP
1141             if (access("/etc", R_OK|W_OK) == -1) {
1142                 g_printf(_("ERROR [dump will not be able to create the /etc/dumpdates file: %s]\n"), strerror(errno));
1143             }
1144 #endif
1145         }
1146     }
1147
1148     if (need_vdump) {
1149         if (check_file_exist("/etc/vdumpdates")) {
1150             check_file("/etc/vdumpdates", F_OK);
1151         }
1152     }
1153
1154     if (need_global_check) {
1155     check_access("/dev/null", R_OK|W_OK);
1156     check_space(AMANDA_TMPDIR, (off_t)64);      /* for amandad i/o */
1157
1158 #ifdef AMANDA_DBGDIR
1159     check_space(AMANDA_DBGDIR, (off_t)64);      /* for amandad i/o */
1160 #endif
1161
1162     check_space("/etc", (off_t)64);             /* for /etc/dumpdates writing */
1163     }
1164 }
1165
1166 static void
1167 check_space(
1168     char *      dir,
1169     off_t       kbytes)
1170 {
1171     struct fs_usage fsusage;
1172     char *quoted = quote_string(dir);
1173     intmax_t kb_avail;
1174
1175     if(get_fs_usage(dir, NULL, &fsusage) == -1) {
1176         g_printf(_("ERROR [cannot get filesystem usage for %s: %s]\n"), quoted, strerror(errno));
1177         amfree(quoted);
1178         return;
1179     }
1180
1181     /* do the division first to avoid potential integer overflow */
1182     kb_avail = fsusage.fsu_bavail / 1024 * fsusage.fsu_blocksize;
1183
1184     if (fsusage.fsu_bavail_top_bit_set || fsusage.fsu_bavail == 0) {
1185         g_printf(_("ERROR [dir %s needs %lldKB, has nothing available.]\n"), quoted,
1186                 (long long)kbytes);
1187     } else if (kb_avail < kbytes) {
1188         g_printf(_("ERROR [dir %s needs %lldKB, only has %lldKB available.]\n"), quoted,
1189                 (long long)kbytes,
1190                 (long long)kb_avail);
1191     } else {
1192         g_printf(_("OK %s has more than %lldKB available.\n"),
1193                 quoted, (long long)kbytes);
1194     }
1195     amfree(quoted);
1196 }
1197
1198 static int
1199 check_file_exist(
1200     char *filename)
1201 {
1202     struct stat stat_buf;
1203
1204     if (stat(filename, &stat_buf) != 0) {
1205         if(errno == ENOENT) {
1206             return 0;
1207         }
1208     }
1209     return 1;
1210 }
1211