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