Merge commit 'upstream/3.1.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;
310
311         dles = amxml_parse_node_FILE(stdin, &errmsg);
312         if (errmsg) {
313             err_extra = errmsg;
314             goto err;
315         }
316         for (dle = dles; dle != NULL; dle = dle->next) {
317             run_client_scripts(EXECUTE_ON_PRE_HOST_AMCHECK, g_options, dle,
318                                stdout);
319         }
320         for (dle = dles; dle != NULL; dle = dle->next) {
321             check_options(dle);
322             run_client_scripts(EXECUTE_ON_PRE_DLE_AMCHECK, g_options, dle,
323                                stdout);
324             check_disk(dle);
325             run_client_scripts(EXECUTE_ON_POST_DLE_AMCHECK, g_options, dle,
326                                stdout);
327         }
328         for (dle = dles; dle != NULL; dle = dle->next) {
329             run_client_scripts(EXECUTE_ON_POST_HOST_AMCHECK, g_options, dle,
330                                stdout);
331         }
332     }
333
334     check_overall();
335
336     amfree(line);
337     amfree(our_feature_string);
338     am_release_feature_set(our_features);
339     our_features = NULL;
340     free_g_options(g_options);
341
342     dbclose();
343     return 0;
344
345  err:
346     if (err_extra) {
347         g_printf(_("ERROR [FORMAT ERROR IN REQUEST PACKET %s]\n"), err_extra);
348         dbprintf(_("REQ packet is bogus: %s\n"), err_extra);
349     } else {
350         g_printf(_("ERROR [FORMAT ERROR IN REQUEST PACKET]\n"));
351         dbprintf(_("REQ packet is bogus\n"));
352     }
353     dbclose();
354     return 1;
355 }
356
357
358 static void
359 check_options(
360     dle_t *dle)
361 {
362     if (GPOINTER_TO_INT(dle->estimatelist->data) == ES_CALCSIZE) {
363         need_calcsize=1;
364     }
365
366     if (strcmp(dle->program,"GNUTAR") == 0) {
367         need_gnutar=1;
368         if(dle->device && dle->device[0] == '/' && dle->device[1] == '/') {
369             if(dle->exclude_file && dle->exclude_file->nb_element > 1) {
370                 g_printf(_("ERROR [samba support only one exclude file]\n"));
371             }
372             if (dle->exclude_list && dle->exclude_list->nb_element > 0 &&
373                 dle->exclude_optional==0) {
374                 g_printf(_("ERROR [samba does not support exclude list]\n"));
375             }
376             if (dle->include_file && dle->include_file->nb_element > 0) {
377                 g_printf(_("ERROR [samba does not support include file]\n"));
378             }
379             if (dle->include_list && dle->include_list->nb_element > 0 &&
380                 dle->include_optional==0) {
381                 g_printf(_("ERROR [samba does not support include list]\n"));
382             }
383             need_samba=1;
384         } else {
385             int nb_exclude = 0;
386             int nb_include = 0;
387             char *file_exclude = NULL;
388             char *file_include = NULL;
389
390             if (dle->exclude_file) nb_exclude += dle->exclude_file->nb_element;
391             if (dle->exclude_list) nb_exclude += dle->exclude_list->nb_element;
392             if (dle->include_file) nb_include += dle->include_file->nb_element;
393             if (dle->include_list) nb_include += dle->include_list->nb_element;
394
395             if (nb_exclude > 0) file_exclude = build_exclude(dle, 1);
396             if (nb_include > 0) file_include = build_include(dle, 1);
397
398             amfree(file_exclude);
399             amfree(file_include);
400
401             need_runtar=1;
402         }
403     }
404
405     if (strcmp(dle->program,"DUMP") == 0) {
406         if (dle->exclude_file && dle->exclude_file->nb_element > 0) {
407             g_printf(_("ERROR [DUMP does not support exclude file]\n"));
408         }
409         if (dle->exclude_list && dle->exclude_list->nb_element > 0) {
410             g_printf(_("ERROR [DUMP does not support exclude list]\n"));
411         }
412         if (dle->include_file && dle->include_file->nb_element > 0) {
413             g_printf(_("ERROR [DUMP does not support include file]\n"));
414         }
415         if (dle->include_list && dle->include_list->nb_element > 0) {
416             g_printf(_("ERROR [DUMP does not support include list]\n"));
417         }
418 #ifdef USE_RUNDUMP
419         need_rundump=1;
420 #endif
421 #ifndef AIX_BACKUP
422 #ifdef VDUMP
423 #ifdef DUMP
424         if (dle->device && strcmp(amname_to_fstype(dle->device), "advfs") == 0)
425 #else
426         if (1)
427 #endif
428         {
429             need_vdump=1;
430             need_rundump=1;
431             if (dle->create_index)
432                 need_vrestore=1;
433         }
434         else
435 #endif /* VDUMP */
436 #ifdef XFSDUMP
437 #ifdef DUMP
438         if (dle->device && strcmp(amname_to_fstype(dle->device), "xfs") == 0)
439 #else
440         if (1)
441 #endif
442         {
443             need_xfsdump=1;
444             need_rundump=1;
445             if (dle->create_index)
446                 need_xfsrestore=1;
447         }
448         else
449 #endif /* XFSDUMP */
450 #ifdef VXDUMP
451 #ifdef DUMP
452         if (dle->device && strcmp(amname_to_fstype(dle->device), "vxfs") == 0)
453 #else
454         if (1)
455 #endif
456         {
457             need_vxdump=1;
458             if (dle->create_index)
459                 need_vxrestore=1;
460         }
461         else
462 #endif /* VXDUMP */
463         {
464             need_dump=1;
465             if (dle->create_index)
466                 need_restore=1;
467         }
468 #else
469         /* AIX backup program */
470         need_dump=1;
471         if (dle->create_index)
472             need_restore=1;
473 #endif
474     }
475     if ((dle->compress == COMP_BEST) || (dle->compress == COMP_FAST) 
476                 || (dle->compress == COMP_CUST)) {
477         need_compress_path=1;
478     }
479     if (dle->auth && amandad_auth) {
480         if (strcasecmp(dle->auth, amandad_auth) != 0) {
481             g_fprintf(stdout,_("ERROR [client configured for auth=%s while server requested '%s']\n"),
482                     amandad_auth, dle->auth);
483             if (strcmp(dle->auth, "ssh") == 0)  {       
484                 g_fprintf(stderr, _("ERROR [The auth in ~/.ssh/authorized_keys "
485                                   "should be \"--auth=ssh\", or use another auth "
486                                   " for the DLE]\n"));
487             }
488             else {
489                 g_fprintf(stderr, _("ERROR [The auth in the inetd/xinetd configuration "
490                                   " must be the same as the DLE]\n"));
491             }           
492         }
493     }
494 }
495
496 static void
497 check_disk(
498     dle_t *dle)
499 {
500     char *device = NULL;
501     char *err = NULL;
502     char *user_and_password = NULL;
503     char *domain = NULL;
504     char *share = NULL, *subdir = NULL;
505     size_t lpass = 0;
506     int amode = R_OK;
507     int access_result;
508     char *access_type;
509     char *extra_info = NULL;
510     char *qdisk = NULL;
511     char *qamdevice = NULL;
512     char *qdevice = NULL;
513
514     if (dle->disk) {
515         need_global_check=1;
516         qdisk = quote_string(dle->disk);
517         qamdevice = quote_string(dle->device);
518         device = stralloc("nodevice");
519         dbprintf(_("checking disk %s\n"), qdisk);
520         if (GPOINTER_TO_INT(dle->estimatelist->data) == ES_CALCSIZE) {
521             if (dle->device[0] == '/' && dle->device[1] == '/') {
522                 err = vstrallocf(
523                     _("Can't use CALCSIZE for samba estimate, use CLIENT: %s"),
524                     dle->device);
525                 goto common_exit;
526             }
527         }
528
529         if (strcmp(dle->program, "GNUTAR")==0) {
530             if(dle->device[0] == '/' && dle->device[1] == '/') {
531                 #ifdef SAMBA_CLIENT
532                 int nullfd, checkerr;
533                 int passwdfd;
534                 char *pwtext;
535                 size_t pwtext_len;
536                 pid_t checkpid;
537                 amwait_t retstat;
538                 pid_t wpid;
539                 int rc;
540                 char *line;
541                 char *sep;
542                 FILE *ferr;
543                 char *pw_fd_env;
544                 int errdos;
545
546                 parsesharename(dle->device, &share, &subdir);
547                 if (!share) {
548                     err = vstrallocf(
549                               _("cannot parse for share/subdir disk entry %s"),
550                               dle->device);
551                     goto common_exit;
552                 }
553                 if ((subdir) && (SAMBA_VERSION < 2)) {
554                     err = vstrallocf(_("subdirectory specified for share '%s' but, samba is not v2 or better"),
555                                      dle->device);
556                     goto common_exit;
557                 }
558                 if ((user_and_password = findpass(share, &domain)) == NULL) {
559                     err = vstrallocf(_("cannot find password for %s"),
560                                      dle->device);
561                     goto common_exit;
562                 }
563                 lpass = strlen(user_and_password);
564                 if ((pwtext = strchr(user_and_password, '%')) == NULL) {
565                     err = vstrallocf(
566                                 _("password field not \'user%%pass\' for %s"),
567                                 dle->device);
568                     goto common_exit;
569                 }
570                 *pwtext++ = '\0';
571                 pwtext_len = (size_t)strlen(pwtext);
572                 amfree(device);
573                 if ((device = makesharename(share, 0)) == NULL) {
574                     err = vstrallocf(_("cannot make share name of %s"), share);
575                     goto common_exit;
576                 }
577
578                 if ((nullfd = open("/dev/null", O_RDWR)) == -1) {
579                     err = vstrallocf(_("Cannot access /dev/null : %s"),
580                                      strerror(errno));
581                     goto common_exit;
582                 }
583
584                 if (pwtext_len > 0) {
585                     pw_fd_env = "PASSWD_FD";
586                 } else {
587                     pw_fd_env = "dummy_PASSWD_FD";
588                 }
589                 checkpid = pipespawn(SAMBA_CLIENT, STDERR_PIPE|PASSWD_PIPE, 0,
590                                      &nullfd, &nullfd, &checkerr,
591                                      pw_fd_env, &passwdfd,
592                                      "smbclient",
593                                      device,
594                                      *user_and_password ? "-U" : skip_argument,
595                                      *user_and_password ? user_and_password
596                                                         : skip_argument,
597                                      "-E",
598                                      domain ? "-W" : skip_argument,
599                                      domain ? domain : skip_argument,
600 #if SAMBA_VERSION >= 2
601                                      subdir ? "-D" : skip_argument,
602                                      subdir ? subdir : skip_argument,
603 #endif
604                                      "-c", "quit",
605                                      NULL);
606                 amfree(domain);
607                 aclose(nullfd);
608                 /*@ignore@*/
609                 if ((pwtext_len > 0) &&
610                     full_write(passwdfd, pwtext, pwtext_len) < pwtext_len) {
611                     err = vstrallocf(_("password write failed: %s: %s"),
612                                      dle->device, strerror(errno));
613                     aclose(passwdfd);
614                     goto common_exit;
615                 }
616                 /*@end@*/
617                 memset(user_and_password, '\0', (size_t)lpass);
618                 amfree(user_and_password);
619                 aclose(passwdfd);
620                 ferr = fdopen(checkerr, "r");
621                 if (!ferr) {
622                     g_printf(_("ERROR [Can't fdopen: %s]\n"), strerror(errno));
623                     error(_("Can't fdopen: %s"), strerror(errno));
624                     /*NOTREACHED*/
625                 }
626                 sep = "";
627                 errdos = 0;
628                 for(sep = ""; (line = agets(ferr)) != NULL; free(line)) {
629                     if (line[0] == '\0')
630                         continue;
631                     strappend(extra_info, sep);
632                     strappend(extra_info, line);
633                     sep = ": ";
634                     if(strstr(line, "ERRDOS") != NULL) {
635                         errdos = 1;
636                     }
637                 }
638                 afclose(ferr);
639                 checkerr = -1;
640                 rc = 0;
641                 sep = "";
642                 while ((wpid = wait(&retstat)) != -1) {
643                     if (!WIFEXITED(retstat) || WEXITSTATUS(retstat) != 0) {
644                         char *exitstr = str_exit_status("smbclient", retstat);
645                         strappend(err, sep);
646                         strappend(err, exitstr);
647                         sep = "\n";
648                         amfree(exitstr);
649
650                         rc = 1;
651                     }
652                 }
653                 if (errdos != 0 || rc != 0) {
654                     if (extra_info) {
655                         err = newvstrallocf(err,
656                                             _("samba access error: %s: %s %s"),
657                                             dle->device, extra_info, err);
658                         amfree(extra_info);
659                     } else {
660                         err = newvstrallocf(err,
661                                             _("samba access error: %s: %s"),
662                                            dle->device, err);
663                     }
664                 }
665 #else
666                 err = vstrallocf(
667                               _("This client is not configured for samba: %s"),
668                               qdisk);
669 #endif
670                 goto common_exit;
671             }
672             amode = F_OK;
673             amfree(device);
674             device = amname_to_dirname(dle->device);
675         } else if (strcmp(dle->program, "DUMP") == 0) {
676             if(dle->device[0] == '/' && dle->device[1] == '/') {
677                 err = vstrallocf(
678                   _("The DUMP program cannot handle samba shares, use GNUTAR: %s"),
679                   qdisk);
680                 goto common_exit;
681             }
682 #ifdef VDUMP                                                            /* { */
683 #ifdef DUMP                                                             /* { */
684             if (strcmp(amname_to_fstype(dle->device), "advfs") == 0)
685 #else                                                                   /* }{*/
686             if (1)
687 #endif                                                                  /* } */
688             {
689                 amfree(device);
690                 device = amname_to_dirname(dle->device);
691                 amode = F_OK;
692             } else
693 #endif                                                                  /* } */
694             {
695                 amfree(device);
696                 device = amname_to_devname(dle->device);
697 #ifdef USE_RUNDUMP
698                 amode = F_OK;
699 #else
700                 amode = R_OK;
701 #endif
702             }
703         }
704     }
705     if (dle->program_is_application_api) {
706         pid_t                    application_api_pid;
707         backup_support_option_t *bsu;
708         int                      app_err[2];
709         GPtrArray               *errarray;
710
711         bsu = backup_support_option(dle->program, g_options, dle->disk,
712                                     dle->device, &errarray);
713
714         if (!bsu) {
715             char  *line;
716             guint  i;
717             for (i=0; i < errarray->len; i++) {
718                 line = g_ptr_array_index(errarray, i);
719                 fprintf(stdout, _("ERROR Application '%s': %s\n"),
720                         dle->program, line);
721                 amfree(line);
722             }
723             err = vstrallocf(_("Application '%s': can't run support command"),
724                              dle->program);
725             goto common_exit;
726         }
727
728         if (dle->data_path == DATA_PATH_AMANDA &&
729             (bsu->data_path_set & DATA_PATH_AMANDA)==0) {
730             g_printf("ERROR application %s doesn't support amanda data-path\n",
731                      dle->program);
732         }
733         if (dle->data_path == DATA_PATH_DIRECTTCP &&
734             (bsu->data_path_set & DATA_PATH_DIRECTTCP)==0) {
735             g_printf("ERROR application %s doesn't support directtcp data-path\n",
736                      dle->program);
737         }
738         if (GPOINTER_TO_INT(dle->estimatelist->data) == ES_CALCSIZE &&
739                             !bsu->calcsize) {
740             g_printf("ERROR application %s doesn't support calcsize estimate\n",
741                      dle->program);
742         }
743         if (dle->include_file && dle->include_file->nb_element > 0 &&
744             !bsu->include_file) {
745             g_printf("ERROR application %s doesn't support include-file\n",
746                    dle->program);
747         }
748         if (dle->include_list && dle->include_list->nb_element > 0 &&
749             !bsu->include_list) {
750             g_printf("ERROR application %s doesn't support include-list\n",
751                    dle->program);
752         }
753         if (dle->include_optional && !bsu->include_optional) {
754             g_printf("ERROR application %s doesn't support optional include\n",
755                    dle->program);
756         }
757         if (dle->exclude_file && dle->exclude_file->nb_element > 0 &&
758             !bsu->exclude_file) {
759             g_printf("ERROR application %s doesn't support exclude-file\n",
760                    dle->program);
761         }
762         if (dle->exclude_list && dle->exclude_list->nb_element > 0 &&
763             !bsu->exclude_list) {
764             g_printf("ERROR application %s doesn't support exclude-list\n",
765                    dle->program);
766         }
767         if (dle->exclude_optional && !bsu->exclude_optional) {
768             g_printf("ERROR application %s doesn't support optional exclude\n",
769                    dle->program);
770         }
771         fflush(stdout);fflush(stderr);
772
773         if (pipe(app_err) < 0) {
774             err = vstrallocf(_("Application '%s': can't create pipe"),
775                              dle->program);
776             goto common_exit;
777         }
778
779         switch (application_api_pid = fork()) {
780         case -1:
781             err = vstrallocf(_("fork failed: %s"), strerror(errno));
782             goto common_exit;
783
784         case 0: /* child */
785             {
786                 GPtrArray *argv_ptr = g_ptr_array_new();
787                 guint i;
788                 char *cmd = vstralloc(APPLICATION_DIR, "/", dle->program, NULL);
789                 GSList   *scriptlist;
790                 script_t *script;
791                 estimatelist_t el;
792                 char *cmdline;
793
794                 aclose(app_err[0]);
795                 dup2(app_err[1], 2);
796
797                 g_ptr_array_add(argv_ptr, stralloc(dle->program));
798                 g_ptr_array_add(argv_ptr, stralloc("selfcheck"));
799                 if (bsu->message_line == 1) {
800                     g_ptr_array_add(argv_ptr, stralloc("--message"));
801                     g_ptr_array_add(argv_ptr, stralloc("line"));
802                 }
803                 if (g_options->config != NULL && bsu->config == 1) {
804                     g_ptr_array_add(argv_ptr, stralloc("--config"));
805                     g_ptr_array_add(argv_ptr, stralloc(g_options->config));
806                 }
807                 if (g_options->hostname != NULL && bsu->host == 1) {
808                     g_ptr_array_add(argv_ptr, stralloc("--host"));
809                     g_ptr_array_add(argv_ptr, stralloc(g_options->hostname));
810                 }
811                 if (dle->disk != NULL && bsu->disk == 1) {
812                     g_ptr_array_add(argv_ptr, stralloc("--disk"));
813                     g_ptr_array_add(argv_ptr, stralloc(dle->disk));
814                 }
815                 if (dle->device) {
816                     g_ptr_array_add(argv_ptr, stralloc("--device"));
817                     g_ptr_array_add(argv_ptr, stralloc(dle->device));
818                 }
819                 if (dle->create_index && bsu->index_line == 1) {
820                     g_ptr_array_add(argv_ptr, stralloc("--index"));
821                     g_ptr_array_add(argv_ptr, stralloc("line"));
822                 }
823                 if (dle->record && bsu->record == 1) {
824                     g_ptr_array_add(argv_ptr, stralloc("--record"));
825                 }
826                 
827                 for (el = dle->estimatelist; el != NULL; el=el->next) {
828                     estimate_t estimate = (estimate_t)GPOINTER_TO_INT(el->data);
829                     if (estimate == ES_CALCSIZE && bsu->calcsize == 1) {
830                         g_ptr_array_add(argv_ptr, stralloc("--calcsize"));
831                     }
832                 }
833                 application_property_add_to_argv(argv_ptr, dle, bsu,
834                                                  g_options->features);
835
836                 for (scriptlist = dle->scriptlist; scriptlist != NULL;
837                      scriptlist = scriptlist->next) {
838                     script = (script_t *)scriptlist->data;
839                     if (script->result && script->result->proplist) {
840                         property_add_to_argv(argv_ptr,
841                                              script->result->proplist);
842                     }
843                 }
844
845                 g_ptr_array_add(argv_ptr, NULL);
846
847                 cmdline = stralloc(cmd);
848                 for (i = 0; i < argv_ptr->len-1; i++) {
849                     char *quoted = quote_string(
850                                         (char *)g_ptr_array_index(argv_ptr,i));
851                     cmdline = vstrextend(&cmdline, " ", quoted, NULL);
852                     amfree(quoted);
853                 }
854                 dbprintf(_("Spawning \"%s\" in pipeline\n"), cmdline);
855                 amfree(cmdline);
856
857                 safe_fd(-1, 0);
858                 execve(cmd, (char **)argv_ptr->pdata, safe_env());
859                 g_printf(_("ERROR [Can't execute %s: %s]\n"), cmd, strerror(errno));
860                 exit(127);
861             }
862         default: /* parent */
863             {
864                 int   status;
865                 FILE *app_stderr;
866                 char *line;
867
868                 aclose(app_err[1]);
869                 app_stderr = fdopen(app_err[0], "r");
870                 while((line = agets(app_stderr)) != NULL) {
871                     if (strlen(line) > 0) {
872                         fprintf(stdout, "ERROR Application '%s': %s\n",
873                                 dle->program, line);
874                         dbprintf("ERROR %s\n", line);
875                     }
876                     amfree(line);
877                 }
878                 fclose(app_stderr);
879                 if (waitpid(application_api_pid, &status, 0) < 0) {
880                     err = vstrallocf(_("waitpid failed: %s"),
881                                          strerror(errno));
882                     goto common_exit;
883                 } else if (!WIFEXITED(status)) {
884                     err = vstrallocf(_("Application '%s': exited with signal %d"),
885                                      dle->program, WTERMSIG(status));
886                     goto common_exit;
887                 } else if (WEXITSTATUS(status) != 0) {
888                     err = vstrallocf(_("Application '%s': exited with status %d"),
889                                      dle->program, WEXITSTATUS(status));
890                     goto common_exit;
891                 }
892             }
893         }
894         amfree(bsu);
895         fflush(stdout);fflush(stderr);
896         amfree(device);
897         amfree(qamdevice);
898         amfree(qdisk);
899         return;
900     }
901
902     if (device) {
903         qdevice = quote_string(device);
904         dbprintf(_("device %s\n"), qdevice);
905
906         /* skip accessability test if this is an AFS entry */
907         if(strncmp_const(device, "afs:") != 0) {
908 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
909             access_result = open(device, O_RDONLY);
910             access_type = "open";
911 #else
912             access_result = access(device, amode);
913             access_type = "access";
914 #endif
915             if(access_result == -1) {
916                 err = vstrallocf(_("Could not access %s (%s): %s"),
917                                  qdevice, qdisk, strerror(errno));
918             }
919 #ifdef CHECK_FOR_ACCESS_WITH_OPEN
920             aclose(access_result);
921 #endif
922         }
923     }
924
925 common_exit:
926
927     if (!qdevice)
928         qdevice = quote_string(device);
929
930     amfree(share);
931     amfree(subdir);
932     if(user_and_password) {
933         memset(user_and_password, '\0', (size_t)lpass);
934         amfree(user_and_password);
935     }
936     amfree(domain);
937
938     if(err) {
939         g_printf(_("ERROR %s\n"), err);
940         dbprintf(_("%s\n"), err);
941         amfree(err);
942     } else {
943         if (dle->disk) {
944             g_printf("OK %s\n", qdisk);
945             dbprintf(_("disk %s OK\n"), qdisk);
946         }
947         if (dle->device) {
948             g_printf("OK %s\n", qamdevice);
949             dbprintf(_("amdevice %s OK\n"), qamdevice);
950         }
951         if (device) {
952             g_printf("OK %s\n", qdevice);
953             dbprintf(_("device %s OK\n"), qdevice);
954         }
955     }
956     if(extra_info) {
957         dbprintf(_("extra info: %s\n"), extra_info);
958         amfree(extra_info);
959     }
960     amfree(qdisk);
961     amfree(qdevice);
962     amfree(qamdevice);
963     amfree(device);
964
965     /* XXX perhaps do something with level: read dumpdates and sanity check */
966 }
967
968 static void
969 check_overall(void)
970 {
971     char *cmd;
972     struct stat buf;
973     int testfd;
974     char *gnutar_list_dir;
975     int   need_amandates = 0;
976
977     if( need_runtar )
978     {
979         cmd = vstralloc(amlibexecdir, "/", "runtar", NULL);
980         check_file(cmd,X_OK);
981         check_suid(cmd);
982         amfree(cmd);
983     }
984
985     if( need_rundump )
986     {
987         cmd = vstralloc(amlibexecdir, "/", "rundump", NULL);
988         check_file(cmd,X_OK);
989         check_suid(cmd);
990         amfree(cmd);
991     }
992
993     if( need_dump ) {
994 #ifdef DUMP
995         check_file(DUMP, X_OK);
996 #else
997         g_printf(_("ERROR [DUMP program not available]\n"));
998 #endif
999     }
1000
1001     if( need_restore ) {
1002 #ifdef RESTORE
1003         check_file(RESTORE, X_OK);
1004 #else
1005         g_printf(_("ERROR [RESTORE program not available]\n"));
1006 #endif
1007     }
1008
1009     if ( need_vdump ) {
1010 #ifdef VDUMP
1011         check_file(VDUMP, X_OK);
1012 #else
1013         g_printf(_("ERROR [VDUMP program not available]\n"));
1014 #endif
1015     }
1016
1017     if ( need_vrestore ) {
1018 #ifdef VRESTORE
1019         check_file(VRESTORE, X_OK);
1020 #else
1021         g_printf(_("ERROR [VRESTORE program not available]\n"));
1022 #endif
1023     }
1024
1025     if( need_xfsdump ) {
1026 #ifdef XFSDUMP
1027         check_file(XFSDUMP, F_OK);
1028 #else
1029         g_printf(_("ERROR [XFSDUMP program not available]\n"));
1030 #endif
1031     }
1032
1033     if( need_xfsrestore ) {
1034 #ifdef XFSRESTORE
1035         check_file(XFSRESTORE, X_OK);
1036 #else
1037         g_printf(_("ERROR [XFSRESTORE program not available]\n"));
1038 #endif
1039     }
1040
1041     if( need_vxdump ) {
1042 #ifdef VXDUMP
1043         check_file(VXDUMP, X_OK);
1044 #else
1045         g_printf(_("ERROR [VXDUMP program not available]\n"));
1046 #endif
1047     }
1048
1049     if( need_vxrestore ) {
1050 #ifdef VXRESTORE
1051         check_file(VXRESTORE, X_OK);
1052 #else
1053         g_printf(_("ERROR [VXRESTORE program not available]\n"));
1054 #endif
1055     }
1056
1057     if( need_gnutar ) {
1058 #ifdef GNUTAR
1059         check_file(GNUTAR, X_OK);
1060 #else
1061         g_printf(_("ERROR [GNUTAR program not available]\n"));
1062 #endif
1063         gnutar_list_dir = getconf_str(CNF_GNUTAR_LIST_DIR);
1064         if (strlen(gnutar_list_dir) == 0)
1065             gnutar_list_dir = NULL;
1066         if (gnutar_list_dir) {
1067             /* make sure our listed-incremental dir is ready */
1068             check_dir(gnutar_list_dir, R_OK|W_OK);
1069         } else {
1070             /* no listed-incremental dir, so check that amandates is ready */
1071             need_amandates = 1;
1072         }
1073     }
1074
1075     if( need_calcsize ) {
1076         char *cmd;
1077
1078         cmd = vstralloc(amlibexecdir, "/", "calcsize", NULL);
1079
1080         check_file(cmd, X_OK);
1081
1082         amfree(cmd);
1083
1084         /* calcsize uses amandates */
1085         need_amandates = 1;
1086     }
1087
1088     if (need_amandates) {
1089         char *amandates_file;
1090         amandates_file = getconf_str(CNF_AMANDATES);
1091         check_file(amandates_file, R_OK|W_OK);
1092     }
1093
1094     if( need_samba ) {
1095 #ifdef SAMBA_CLIENT
1096         check_file(SAMBA_CLIENT, X_OK);
1097 #else
1098         g_printf(_("ERROR [SMBCLIENT program not available]\n"));
1099 #endif
1100         testfd = open("/etc/amandapass", R_OK);
1101         if (testfd >= 0) {
1102             if(fstat(testfd, &buf) == 0) {
1103                 if ((buf.st_mode & 0x7) != 0) {
1104                     g_printf(_("ERROR [/etc/amandapass is world readable!]\n"));
1105                 } else {
1106                     g_printf(_("OK [/etc/amandapass is readable, but not by all]\n"));
1107                 }
1108             } else {
1109                 g_printf(_("OK [unable to stat /etc/amandapass: %s]\n"),
1110                        strerror(errno));
1111             }
1112             aclose(testfd);
1113         } else {
1114             g_printf(_("ERROR [unable to open /etc/amandapass: %s]\n"),
1115                    strerror(errno));
1116         }
1117     }
1118
1119     if (need_compress_path )
1120         check_file(COMPRESS_PATH, X_OK);
1121
1122     if (need_dump || need_xfsdump ) {
1123         if (check_file_exist("/var/lib/dumpdates")) {
1124             check_file("/var/lib/dumpdates",
1125 #ifdef USE_RUNDUMP
1126                        F_OK
1127 #else
1128                        R_OK|W_OK
1129 #endif
1130                       );
1131         } else {
1132 #ifndef USE_RUNDUMP
1133             if (access("/etc", R_OK|W_OK) == -1) {
1134                 g_printf(_("ERROR [dump will not be able to create the /var/lib/dumpdates file: %s]\n"), strerror(errno));
1135             }
1136 #endif
1137         }
1138     }
1139
1140     if (need_vdump) {
1141         if (check_file_exist("/var/lib/vdumpdates")) {
1142             check_file("/var/lib/vdumpdates", F_OK);
1143         }
1144     }
1145
1146     if (need_global_check) {
1147     check_access("/dev/null", R_OK|W_OK);
1148     check_space(AMANDA_TMPDIR, (off_t)64);      /* for amandad i/o */
1149
1150 #ifdef AMANDA_DBGDIR
1151     check_space(AMANDA_DBGDIR, (off_t)64);      /* for amandad i/o */
1152 #endif
1153
1154     check_space("/var/lib", (off_t)64);         /* for /var/lib/dumpdates writing */
1155     check_space("/etc", (off_t)64);             /* for /etc/dumpdates writing */
1156     }
1157 }
1158
1159 static void
1160 check_space(
1161     char *      dir,
1162     off_t       kbytes)
1163 {
1164     struct fs_usage fsusage;
1165     char *quoted = quote_string(dir);
1166     intmax_t kb_avail;
1167
1168     if(get_fs_usage(dir, NULL, &fsusage) == -1) {
1169         g_printf(_("ERROR [cannot get filesystem usage for %s: %s]\n"), quoted, strerror(errno));
1170         amfree(quoted);
1171         return;
1172     }
1173
1174     /* do the division first to avoid potential integer overflow */
1175     kb_avail = fsusage.fsu_bavail / 1024 * fsusage.fsu_blocksize;
1176
1177     if (fsusage.fsu_bavail_top_bit_set || fsusage.fsu_bavail == 0) {
1178         g_printf(_("ERROR [dir %s needs %lldKB, has nothing available.]\n"), quoted,
1179                 (long long)kbytes);
1180     } else if (kb_avail < kbytes) {
1181         g_printf(_("ERROR [dir %s needs %lldKB, only has %lldKB available.]\n"), quoted,
1182                 (long long)kbytes,
1183                 (long long)kb_avail);
1184     } else {
1185         g_printf(_("OK %s has more than %lldKB available.\n"),
1186                 quoted, (long long)kbytes);
1187     }
1188     amfree(quoted);
1189 }
1190
1191 static int
1192 check_file_exist(
1193     char *filename)
1194 {
1195     struct stat stat_buf;
1196
1197     if (stat(filename, &stat_buf) != 0) {
1198         if(errno == ENOENT) {
1199             return 0;
1200         }
1201     }
1202     return 1;
1203 }
1204