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