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