xref: /netbsd-src/usr.sbin/sysinst/main.c (revision f436dff43b1138c30490dfcc2b8bdd60576dd78a)
1*f436dff4Smartin /*	$NetBSD: main.c,v 1.33 2024/02/21 20:31:57 martin Exp $	*/
250dbef1aSdholland 
350dbef1aSdholland /*
450dbef1aSdholland  * Copyright 1997 Piermont Information Systems Inc.
550dbef1aSdholland  * All rights reserved.
650dbef1aSdholland  *
750dbef1aSdholland  * Written by Philip A. Nelson for Piermont Information Systems Inc.
850dbef1aSdholland  *
950dbef1aSdholland  * Redistribution and use in source and binary forms, with or without
1050dbef1aSdholland  * modification, are permitted provided that the following conditions
1150dbef1aSdholland  * are met:
1250dbef1aSdholland  * 1. Redistributions of source code must retain the above copyright
1350dbef1aSdholland  *    notice, this list of conditions and the following disclaimer.
1450dbef1aSdholland  * 2. Redistributions in binary form must reproduce the above copyright
1550dbef1aSdholland  *    notice, this list of conditions and the following disclaimer in the
1650dbef1aSdholland  *    documentation and/or other materials provided with the distribution.
1750dbef1aSdholland  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
1850dbef1aSdholland  *    or promote products derived from this software without specific prior
1950dbef1aSdholland  *    written permission.
2050dbef1aSdholland  *
2150dbef1aSdholland  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
2250dbef1aSdholland  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2350dbef1aSdholland  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2450dbef1aSdholland  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
2550dbef1aSdholland  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2650dbef1aSdholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2750dbef1aSdholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2850dbef1aSdholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2950dbef1aSdholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3050dbef1aSdholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3150dbef1aSdholland  * THE POSSIBILITY OF SUCH DAMAGE.
3250dbef1aSdholland  *
3350dbef1aSdholland  */
3450dbef1aSdholland 
3550dbef1aSdholland /* main sysinst program. */
3650dbef1aSdholland 
3750dbef1aSdholland #include <sys/types.h>
3850dbef1aSdholland #include <sys/stat.h>
39438e08f5Smartin #include <sys/syslimits.h>
4050dbef1aSdholland #include <sys/uio.h>
4150dbef1aSdholland #include <stdio.h>
4250dbef1aSdholland #include <signal.h>
4350dbef1aSdholland #include <curses.h>
4450dbef1aSdholland #include <unistd.h>
4550dbef1aSdholland #include <fcntl.h>
4650dbef1aSdholland #include <dirent.h>
4750dbef1aSdholland #include <locale.h>
4850dbef1aSdholland 
4950dbef1aSdholland #include "defs.h"
5050dbef1aSdholland #include "md.h"
5150dbef1aSdholland #include "msg_defs.h"
5250dbef1aSdholland #include "menu_defs.h"
5350dbef1aSdholland #include "txtwalk.h"
5450dbef1aSdholland 
55e45e0240Sjoerg int debug;
56e45e0240Sjoerg char machine[SSTRSIZE];
57e45e0240Sjoerg int ignorerror;
58e45e0240Sjoerg int ttysig_ignore;
59e45e0240Sjoerg pid_t ttysig_forward;
60e45e0240Sjoerg uint sizemult;
61e45e0240Sjoerg int partman_go;
62e45e0240Sjoerg FILE *logfp;
63e45e0240Sjoerg FILE *script;
64e45e0240Sjoerg daddr_t root_limit;
65e45e0240Sjoerg struct pm_head_t pm_head;
66e45e0240Sjoerg struct pm_devs *pm;
67e45e0240Sjoerg struct pm_devs *pm_new;
68e45e0240Sjoerg char xfer_dir[STRSIZE];
69e45e0240Sjoerg int  clean_xfer_dir;
70e45e0240Sjoerg char ext_dir_bin[STRSIZE];
71e45e0240Sjoerg char ext_dir_src[STRSIZE];
72e45e0240Sjoerg char ext_dir_pkgsrc[STRSIZE];
73e45e0240Sjoerg char set_dir_bin[STRSIZE];
74e45e0240Sjoerg char set_dir_src[STRSIZE];
75e45e0240Sjoerg char pkg_dir[STRSIZE];
76e45e0240Sjoerg char pkgsrc_dir[STRSIZE];
77e45e0240Sjoerg const char *ushell;
78e45e0240Sjoerg struct ftpinfo ftp, pkg, pkgsrc;
79e45e0240Sjoerg int (*fetch_fn)(const char *);
80e45e0240Sjoerg char nfs_host[STRSIZE];
81e45e0240Sjoerg char nfs_dir[STRSIZE];
82e45e0240Sjoerg char cdrom_dev[SSTRSIZE];
83e45e0240Sjoerg char fd_dev[SSTRSIZE];
84e45e0240Sjoerg const char *fd_type;
85e45e0240Sjoerg char localfs_dev[SSTRSIZE];
86e45e0240Sjoerg char localfs_fs[SSTRSIZE];
87e45e0240Sjoerg char localfs_dir[STRSIZE];
88e45e0240Sjoerg char targetroot_mnt[SSTRSIZE];
89e45e0240Sjoerg int  mnt2_mounted;
90e45e0240Sjoerg char dist_postfix[SSTRSIZE];
91e45e0240Sjoerg char dist_tgz_postfix[SSTRSIZE];
92e45e0240Sjoerg WINDOW *mainwin;
93e45e0240Sjoerg 
943b7d675bSmartin static void select_language(const char*);
9550dbef1aSdholland __dead static void usage(void);
9650dbef1aSdholland __dead static void miscsighandler(int);
9750dbef1aSdholland static void ttysighandler(int);
9850dbef1aSdholland static void cleanup(void);
9950dbef1aSdholland static void process_f_flag(char *);
100ecc52c0dSmartin static bool no_openssl_trust_anchors_available(void);
10150dbef1aSdholland 
10250dbef1aSdholland static int exit_cleanly = 0;	/* Did we finish nicely? */
10350dbef1aSdholland FILE *logfp;			/* log file */
10450dbef1aSdholland FILE *script;			/* script file */
10550dbef1aSdholland 
1064103857bSmartin const char *multname;
1074103857bSmartin const char *err_outofmem;
1084103857bSmartin 
10950dbef1aSdholland #ifdef DEBUG
11050dbef1aSdholland extern int log_flip(void);
11150dbef1aSdholland #endif
11250dbef1aSdholland 
1134b2364d9Smartin /* Definion for colors */
1144b2364d9Smartin 
1154b2364d9Smartin struct {
1164b2364d9Smartin 	unsigned int bg;
1174b2364d9Smartin 	unsigned int fg;
1184b2364d9Smartin } clr_arg;
1194b2364d9Smartin 
12050dbef1aSdholland /* String defaults and stuff for processing the -f file argument. */
12150dbef1aSdholland 
12250dbef1aSdholland struct f_arg {
12350dbef1aSdholland 	const char *name;
12450dbef1aSdholland 	const char *dflt;
12550dbef1aSdholland 	char *var;
12650dbef1aSdholland 	int size;
12750dbef1aSdholland };
12850dbef1aSdholland 
12950dbef1aSdholland static const struct f_arg fflagopts[] = {
130bcf119c0Smartin 	{"release", REL, NULL, 0},
13150dbef1aSdholland 	{"machine", MACH, machine, sizeof machine},
132abef26c3Smartin 	{"xfer dir", "/usr/INSTALL", xfer_dir, sizeof xfer_dir},
13350dbef1aSdholland 	{"ext dir", "", ext_dir_bin, sizeof ext_dir_bin},
13450dbef1aSdholland 	{"ext src dir", "", ext_dir_src, sizeof ext_dir_src},
1350578ab99Smartin 	{"ftp host", SYSINST_FTP_HOST, ftp.xfer_host[XFER_HOST(XFER_FTP)], sizeof ftp.xfer_host[XFER_HOST(XFER_FTP)]},
1360578ab99Smartin 	{"http host", SYSINST_HTTP_HOST, ftp.xfer_host[XFER_HOST(XFER_HTTP)], sizeof ftp.xfer_host[XFER_HOST(XFER_HTTP)]},
13750dbef1aSdholland 	{"ftp dir", SYSINST_FTP_DIR, ftp.dir, sizeof ftp.dir},
138009ce005Smartin 	{"ftp prefix", "/" ARCH_SUBDIR "/binary/sets", set_dir_bin, sizeof set_dir_bin},
13950dbef1aSdholland 	{"ftp src prefix", "/source/sets", set_dir_src, sizeof set_dir_src},
14050dbef1aSdholland 	{"ftp user", "ftp", ftp.user, sizeof ftp.user},
14150dbef1aSdholland 	{"ftp pass", "", ftp.pass, sizeof ftp.pass},
14250dbef1aSdholland 	{"ftp proxy", "", ftp.proxy, sizeof ftp.proxy},
14350dbef1aSdholland 	{"nfs host", "", nfs_host, sizeof nfs_host},
14450dbef1aSdholland 	{"nfs dir", "/bsd/release", nfs_dir, sizeof nfs_dir},
14550dbef1aSdholland 	{"cd dev", 0, cdrom_dev, sizeof cdrom_dev}, /* default filled in init */
14650dbef1aSdholland 	{"fd dev", "/dev/fd0a", fd_dev, sizeof fd_dev},
14750dbef1aSdholland 	{"local dev", "", localfs_dev, sizeof localfs_dev},
14850dbef1aSdholland 	{"local fs", "ffs", localfs_fs, sizeof localfs_fs},
14950dbef1aSdholland 	{"local dir", "release", localfs_dir, sizeof localfs_dir},
15050dbef1aSdholland 	{"targetroot mount", "/targetroot", targetroot_mnt, sizeof targetroot_mnt},
1510ef20e92Smartin 	{"dist postfix", "." SETS_TAR_SUFF, dist_postfix, sizeof dist_postfix},
1520ef20e92Smartin 	{"dist tgz postfix", ".tgz", dist_tgz_postfix, sizeof dist_tgz_postfix},
1530578ab99Smartin 	{"pkg host", SYSINST_PKG_HOST, pkg.xfer_host[XFER_HOST(XFER_FTP)], sizeof pkg.xfer_host[XFER_HOST(XFER_FTP)]},
1540578ab99Smartin 	{"pkg http host", SYSINST_PKG_HTTP_HOST, pkg.xfer_host[XFER_HOST(XFER_HTTP)], sizeof pkg.xfer_host[XFER_HOST(XFER_HTTP)]},
15550dbef1aSdholland 	{"pkg dir", SYSINST_PKG_DIR, pkg.dir, sizeof pkg.dir},
156009ce005Smartin 	{"pkg prefix", "/" PKG_ARCH_SUBDIR "/" PKG_SUBDIR "/All", pkg_dir, sizeof pkg_dir},
15750dbef1aSdholland 	{"pkg user", "ftp", pkg.user, sizeof pkg.user},
15850dbef1aSdholland 	{"pkg pass", "", pkg.pass, sizeof pkg.pass},
15950dbef1aSdholland 	{"pkg proxy", "", pkg.proxy, sizeof pkg.proxy},
1600578ab99Smartin 	{"pkgsrc host", SYSINST_PKGSRC_HOST, pkgsrc.xfer_host[XFER_HOST(XFER_FTP)], sizeof pkgsrc.xfer_host[XFER_HOST(XFER_FTP)]},
1610578ab99Smartin 	{"pkgsrc http host", SYSINST_PKGSRC_HTTP_HOST, pkgsrc.xfer_host[XFER_HOST(XFER_HTTP)], sizeof pkgsrc.xfer_host[XFER_HOST(XFER_HTTP)]},
16250dbef1aSdholland 	{"pkgsrc dir", "", pkgsrc.dir, sizeof pkgsrc.dir},
16350dbef1aSdholland 	{"pkgsrc prefix", "pub/pkgsrc/stable", pkgsrc_dir, sizeof pkgsrc_dir},
16450dbef1aSdholland 	{"pkgsrc user", "ftp", pkgsrc.user, sizeof pkgsrc.user},
16550dbef1aSdholland 	{"pkgsrc pass", "", pkgsrc.pass, sizeof pkgsrc.pass},
16650dbef1aSdholland 	{"pkgsrc proxy", "", pkgsrc.proxy, sizeof pkgsrc.proxy},
16750dbef1aSdholland 
16850dbef1aSdholland 	{NULL, NULL, NULL, 0}
16950dbef1aSdholland };
17050dbef1aSdholland 
17150dbef1aSdholland static void
init(void)17250dbef1aSdholland init(void)
17350dbef1aSdholland {
17450dbef1aSdholland 	const struct f_arg *arg;
17550dbef1aSdholland 
17650dbef1aSdholland 	sizemult = 1;
17750dbef1aSdholland 	clean_xfer_dir = 0;
17850dbef1aSdholland 	mnt2_mounted = 0;
17950dbef1aSdholland 	fd_type = "msdos";
1804b2364d9Smartin 
1814b2364d9Smartin 	pm_head = (struct pm_head_t) SLIST_HEAD_INITIALIZER(pm_head);
1824b2364d9Smartin 	SLIST_INIT(&pm_head);
1834103857bSmartin 	pm_new = malloc(sizeof (struct pm_devs));
1844b2364d9Smartin 	memset(pm_new, 0, sizeof *pm_new);
18550dbef1aSdholland 
18650dbef1aSdholland 	for (arg = fflagopts; arg->name != NULL; arg++) {
18734fc30f8Smartin 		if (arg->var == NULL)
18834fc30f8Smartin 			continue;
18950dbef1aSdholland 		if (arg->var == cdrom_dev)
1903b44a3d7Smartin 			get_default_cdrom(arg->var, arg->size);
19150dbef1aSdholland 		else
19250dbef1aSdholland 			strlcpy(arg->var, arg->dflt, arg->size);
19350dbef1aSdholland 	}
1940578ab99Smartin 	ftp.xfer = pkg.xfer = pkgsrc.xfer = XFER_HTTPS;
1954b2364d9Smartin 
1964b2364d9Smartin 	clr_arg.bg=COLOR_BLUE;
1974b2364d9Smartin 	clr_arg.fg=COLOR_WHITE;
19850dbef1aSdholland }
19950dbef1aSdholland 
2004103857bSmartin static void
init_lang(void)2014103857bSmartin init_lang(void)
2024103857bSmartin {
2034103857bSmartin 	sizemult = 1;
2044103857bSmartin 	err_outofmem = msg_string(MSG_out_of_memory);
2054103857bSmartin 	multname = msg_string(MSG_secname);
2064103857bSmartin }
2074103857bSmartin 
20850dbef1aSdholland int
main(int argc,char ** argv)20950dbef1aSdholland main(int argc, char **argv)
21050dbef1aSdholland {
21150dbef1aSdholland 	int ch;
2123b7d675bSmartin 	const char *msg_cat_dir = NULL;
21350dbef1aSdholland 
21450dbef1aSdholland 	init();
2154103857bSmartin 
21650dbef1aSdholland #ifdef DEBUG
21750dbef1aSdholland 	log_flip();
21850dbef1aSdholland #endif
2194b2364d9Smartin 
22050dbef1aSdholland 	/* Check for TERM ... */
22150dbef1aSdholland 	if (!getenv("TERM")) {
22250dbef1aSdholland 		(void)fprintf(stderr,
22350dbef1aSdholland 			 "sysinst: environment variable TERM not set.\n");
22450dbef1aSdholland 		exit(4);
22550dbef1aSdholland 	}
22650dbef1aSdholland 
22750dbef1aSdholland 	/* argv processing */
2283b7d675bSmartin 	while ((ch = getopt(argc, argv, "Dr:f:C:m:"
229d1110ccaSrin #ifndef NO_PARTMAN
230d1110ccaSrin 	    "p"
231d1110ccaSrin #endif
232d1110ccaSrin 	    )) != -1)
23350dbef1aSdholland 		switch(ch) {
23450dbef1aSdholland 		case 'D':	/* set to get past certain errors in testing */
23550dbef1aSdholland 			debug = 1;
23650dbef1aSdholland 			break;
23750dbef1aSdholland 		case 'r':
238bcf119c0Smartin 			/* Release name - ignore for compatibility with older versions */
23950dbef1aSdholland 			break;
24050dbef1aSdholland 		case 'f':
24150dbef1aSdholland 			/* Definition file to read. */
24250dbef1aSdholland 			process_f_flag(optarg);
24350dbef1aSdholland 			break;
2444b2364d9Smartin 		case 'C':
2454b2364d9Smartin 			/* Define colors */
2464b2364d9Smartin 			sscanf(optarg, "%u:%u", &clr_arg.bg, &clr_arg.fg);
2474b2364d9Smartin 			break;
2483b7d675bSmartin 		case 'm':
2493b7d675bSmartin 			/* set message catalog directory */
2503b7d675bSmartin 			msg_cat_dir = optarg;
2513b7d675bSmartin 			break;
252d1110ccaSrin #ifndef NO_PARTMAN
2534b2364d9Smartin 		case 'p':
2544b2364d9Smartin 			/* Partition tool */
2554b2364d9Smartin 			partman_go = 1;
2564b2364d9Smartin 			break;
257d1110ccaSrin #endif
25850dbef1aSdholland 		case '?':
25950dbef1aSdholland 		default:
26050dbef1aSdholland 			usage();
26150dbef1aSdholland 		}
26250dbef1aSdholland 
26350dbef1aSdholland 	md_init();
26450dbef1aSdholland 
2654103857bSmartin 	/* Initialize the partitioning subsystem */
2664103857bSmartin 	partitions_init();
2674103857bSmartin 
268ecc52c0dSmartin 	/* do we need to tell ftp(1) to avoid checking certificate chains? */
269ecc52c0dSmartin 	if (no_openssl_trust_anchors_available())
270ecc52c0dSmartin 		setenv("FTPSSLNOVERIFY", "1", 1);
271ecc52c0dSmartin 
27250dbef1aSdholland 	/* initialize message window */
27350dbef1aSdholland 	if (menu_init()) {
27450dbef1aSdholland 		__menu_initerror();
27550dbef1aSdholland 		exit(4);
27650dbef1aSdholland 	}
27750dbef1aSdholland 
27850dbef1aSdholland 	/*
27950dbef1aSdholland 	 * Put 'messages' in a window that has a one-character border
28050dbef1aSdholland 	 * on the real screen.
28150dbef1aSdholland 	 */
2824b2364d9Smartin 	mainwin = newwin(getmaxy(stdscr) - 2, getmaxx(stdscr) - 2, 1, 1);
2834b2364d9Smartin 	if (mainwin == NULL) {
28450dbef1aSdholland 		(void)fprintf(stderr,
28550dbef1aSdholland 			 "sysinst: screen too small\n");
28650dbef1aSdholland 		exit(1);
28750dbef1aSdholland 	}
28850dbef1aSdholland 	if (has_colors()) {
2894b2364d9Smartin 		start_color();
2904b2364d9Smartin 		do_coloring(clr_arg.fg,clr_arg.bg);
29150dbef1aSdholland 	}
2924b2364d9Smartin 	msg_window(mainwin);
29350dbef1aSdholland 
29450dbef1aSdholland 	/* Watch for signals and clean up */
29550dbef1aSdholland 	(void)atexit(cleanup);
29650dbef1aSdholland 	(void)signal(SIGINT, ttysighandler);
29750dbef1aSdholland 	(void)signal(SIGQUIT, ttysighandler);
29850dbef1aSdholland 	(void)signal(SIGHUP, miscsighandler);
29950dbef1aSdholland 
30050dbef1aSdholland 	/* redraw screen */
30150dbef1aSdholland 	touchwin(stdscr);
30250dbef1aSdholland 	refresh();
30350dbef1aSdholland 
30450dbef1aSdholland 	/* Ensure we have mountpoint for target filesystems */
30550dbef1aSdholland 	mkdir(targetroot_mnt, S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH);
30650dbef1aSdholland 
3073b7d675bSmartin 	select_language(msg_cat_dir);
30850dbef1aSdholland 	get_kb_encoding();
3094103857bSmartin 	init_lang();
31050dbef1aSdholland 
311*f436dff4Smartin 	/* remove some invalid menu entries */
312*f436dff4Smartin 	if (!has_colors())
313*f436dff4Smartin 		remove_color_options();
314*f436dff4Smartin 
31550dbef1aSdholland 	/* Menu processing */
3164b2364d9Smartin 	if (partman_go)
317c38850fbSmartin 		partman(NULL);
3184b2364d9Smartin 	else
31950dbef1aSdholland 		process_menu(MENU_netbsd, NULL);
32050dbef1aSdholland 
3214103857bSmartin #ifndef NO_PARTMAN
3224103857bSmartin 	/* clean up internal storage */
3234103857bSmartin 	pm_destroy_all();
3244103857bSmartin #endif
3254103857bSmartin 
326f77b58b1Smartin 	partitions_cleanup();
327f77b58b1Smartin 
32850dbef1aSdholland 	exit_cleanly = 1;
32950dbef1aSdholland 	return 0;
33050dbef1aSdholland }
33150dbef1aSdholland 
33250dbef1aSdholland static int
set_language(menudesc * m,void * arg)33350dbef1aSdholland set_language(menudesc *m, void *arg)
33450dbef1aSdholland {
33550dbef1aSdholland 	char **fnames = arg;
33650dbef1aSdholland 
33750dbef1aSdholland 	msg_file(fnames[m->cursel]);
33850dbef1aSdholland 	return 1;
33950dbef1aSdholland }
34050dbef1aSdholland 
3413b7d675bSmartin /*
3423b7d675bSmartin  * Search for sysinstmsg.* files in the given dir, collect
3433b7d675bSmartin  * their names and return the number of files found.
3443b7d675bSmartin  * fnames[0] is preallocated and duplicates are ignored.
3453b7d675bSmartin  */
3463b7d675bSmartin struct found_msgs {
3473b7d675bSmartin 	char **lang_msg, **fnames;
3483b7d675bSmartin 	int max_lang, num_lang;
3493b7d675bSmartin 
3503b7d675bSmartin };
35150dbef1aSdholland static void
find_language_files(const char * path,struct found_msgs * res)3523b7d675bSmartin find_language_files(const char *path, struct found_msgs *res)
35350dbef1aSdholland {
35450dbef1aSdholland 	DIR *dir;
35550dbef1aSdholland 	struct dirent *dirent;
3563b7d675bSmartin 	char fname[PATH_MAX];
35750dbef1aSdholland 	const char *cp;
35850dbef1aSdholland 
3593b7d675bSmartin 	res->num_lang = 0;
3603b7d675bSmartin 	dir = opendir(path);
36150dbef1aSdholland 	if (!dir)
36250dbef1aSdholland 		return;
36350dbef1aSdholland 
36450dbef1aSdholland 	while ((dirent = readdir(dir)) != 0) {
36550dbef1aSdholland 		if (memcmp(dirent->d_name, "sysinstmsgs.", 12))
36650dbef1aSdholland 			continue;
3673b7d675bSmartin 
3683b7d675bSmartin 		if (res->num_lang == 0)
3693b7d675bSmartin 			res->num_lang = 1;
3703b7d675bSmartin 		strcpy(fname, path);
3713b7d675bSmartin 		strcat(fname, "/");
372438e08f5Smartin 		strcat(fname, dirent->d_name);
373438e08f5Smartin 		if (msg_file(fname))
37450dbef1aSdholland 			continue;
37550dbef1aSdholland 		cp = msg_string(MSG_sysinst_message_language);
3763b7d675bSmartin 		if (!strcmp(cp, res->lang_msg[0]))
37750dbef1aSdholland 			continue;
3783b7d675bSmartin 		if (res->num_lang == res->max_lang) {
37950dbef1aSdholland 			char **new;
3803b7d675bSmartin 			res->max_lang *= 2;
3813b7d675bSmartin 			new = realloc(res->lang_msg,
3823b7d675bSmartin 			    res->max_lang * sizeof *res->lang_msg);
38350dbef1aSdholland 			if (!new)
38450dbef1aSdholland 				break;
3853b7d675bSmartin 			res->lang_msg = new;
3863b7d675bSmartin 			new = realloc(res->fnames,
3873b7d675bSmartin 			    res->max_lang * sizeof *res->fnames);
38850dbef1aSdholland 			if (!new)
38950dbef1aSdholland 				break;
3903b7d675bSmartin 			res->fnames = new;
39150dbef1aSdholland 		}
3923b7d675bSmartin 		res->fnames[res->num_lang] = strdup(fname);
3933b7d675bSmartin 		res->lang_msg[res->num_lang++] = strdup(cp);
39450dbef1aSdholland 	}
39550dbef1aSdholland 
3963b7d675bSmartin 	closedir(dir);
3973b7d675bSmartin }
3983b7d675bSmartin 
3993b7d675bSmartin static void
select_language(const char * msg_cat_path)4003b7d675bSmartin select_language(const char *msg_cat_path)
4013b7d675bSmartin {
4023b7d675bSmartin 	struct found_msgs found;
4033b7d675bSmartin 	menu_ent *opt = 0;
4043b7d675bSmartin 	const char *cp;
4053b7d675bSmartin 	int lang_menu = -1;
4063b7d675bSmartin 	int lang;
4073b7d675bSmartin 
4083b7d675bSmartin 	found.max_lang = 16;
4093b7d675bSmartin 	found.num_lang = 0;
4103b7d675bSmartin 	found.lang_msg = malloc(found.max_lang * sizeof *found.lang_msg);
4113b7d675bSmartin 	found.fnames = malloc(found.max_lang * sizeof *found.fnames);
4123b7d675bSmartin 	if (!found.lang_msg || !found.fnames)
4133b7d675bSmartin 		goto done;
4143b7d675bSmartin 	found.lang_msg[0] = strdup(msg_string(MSG_sysinst_message_language));
4153b7d675bSmartin 	found.fnames[0] = NULL;
4163b7d675bSmartin 
4173b7d675bSmartin 	if (msg_cat_path != NULL)
4183b7d675bSmartin 		find_language_files(msg_cat_path, &found);
4193b7d675bSmartin 	if (found.num_lang == 0)
4203b7d675bSmartin 		find_language_files(".", &found);
4213b7d675bSmartin #ifdef CATALOG_DIR
4223b7d675bSmartin 	if (found.num_lang == 0)
4233b7d675bSmartin 		find_language_files(CATALOG_DIR, &found);
4243b7d675bSmartin #endif
4253b7d675bSmartin 
4263b7d675bSmartin 	msg_file(0);
4273b7d675bSmartin 
4283b7d675bSmartin 	if (found.num_lang <= 1)
42950dbef1aSdholland 		goto done;
43050dbef1aSdholland 
4313b7d675bSmartin 	opt = calloc(found.num_lang, sizeof *opt);
43250dbef1aSdholland 	if (!opt)
43350dbef1aSdholland 		goto done;
43450dbef1aSdholland 
4353b7d675bSmartin 	for (lang = 0; lang < found.num_lang; lang++) {
4363b7d675bSmartin 		opt[lang].opt_name = found.lang_msg[lang];
43750dbef1aSdholland 		opt[lang].opt_action = set_language;
43850dbef1aSdholland 	}
43950dbef1aSdholland 
4403b7d675bSmartin 	lang_menu = new_menu(NULL, opt, found.num_lang, -1, 12, 0, 0,
4413b7d675bSmartin 	    MC_NOEXITOPT, NULL, NULL, NULL, NULL, NULL);
44250dbef1aSdholland 
44350dbef1aSdholland 	if (lang_menu != -1) {
44450dbef1aSdholland 		msg_display(MSG_hello);
4453b7d675bSmartin 		process_menu(lang_menu, found.fnames);
44650dbef1aSdholland 	}
44750dbef1aSdholland 
44850dbef1aSdholland     done:
44950dbef1aSdholland 	if (lang_menu != -1)
45050dbef1aSdholland 		free_menu(lang_menu);
45150dbef1aSdholland 	free(opt);
4523b7d675bSmartin 	for (int i = 0; i < found.num_lang; i++) {
4533b7d675bSmartin 		free(found.lang_msg[i]);
4543b7d675bSmartin 		free(found.fnames[i]);
45550dbef1aSdholland 	}
4563b7d675bSmartin 	free(found.lang_msg);
4573b7d675bSmartin 	free(found.fnames);
45850dbef1aSdholland 
45950dbef1aSdholland 	/* set locale according to selected language */
46050dbef1aSdholland 	cp = msg_string(MSG_sysinst_message_locale);
46150dbef1aSdholland 	if (cp) {
46250dbef1aSdholland 		setlocale(LC_CTYPE, cp);
46350dbef1aSdholland 		setenv("LC_CTYPE", cp, 1);
46450dbef1aSdholland 	}
46550dbef1aSdholland }
46650dbef1aSdholland 
46750dbef1aSdholland #ifndef md_may_remove_boot_medium
46850dbef1aSdholland #define md_may_remove_boot_medium()	(boot_media_still_needed()<=0)
46950dbef1aSdholland #endif
47050dbef1aSdholland 
47150dbef1aSdholland /* toplevel menu handler ... */
47250dbef1aSdholland void
toplevel(void)47350dbef1aSdholland toplevel(void)
47450dbef1aSdholland {
4754b2364d9Smartin 	/*
4764b2364d9Smartin 	 * Undo any stateful side-effects of previous menu choices.
4774b2364d9Smartin 	 * XXX must be idempotent, since we get run each time the main
4784b2364d9Smartin 	 *     menu is displayed.
4794b2364d9Smartin 	 */
480fe0740f8Smartin 	char *home = getenv("HOME");
481fe0740f8Smartin 	if (home != NULL)
4823221cb6fSmartin 		if (chdir(home) != 0)
4833221cb6fSmartin 			(void)chdir("/");
4844b2364d9Smartin 	unwind_mounts();
48598c332e3Smartin 	clear_swap();
48650dbef1aSdholland 
48750dbef1aSdholland 	/* Display banner message in (english, francais, deutsch..) */
48850dbef1aSdholland 	msg_display(MSG_hello);
48950dbef1aSdholland 	msg_display_add(MSG_md_hello);
49050dbef1aSdholland 	if (md_may_remove_boot_medium())
49150dbef1aSdholland 		msg_display_add(MSG_md_may_remove_boot_medium);
49250dbef1aSdholland 	msg_display_add(MSG_thanks);
49350dbef1aSdholland }
49450dbef1aSdholland 
49550dbef1aSdholland 
49650dbef1aSdholland /* The usage ... */
49750dbef1aSdholland 
49850dbef1aSdholland static void
usage(void)49950dbef1aSdholland usage(void)
50050dbef1aSdholland {
50150dbef1aSdholland 
50258fed2b5Swiz 	(void)fprintf(stderr, "usage: sysinst [-C bg:fg] [-D"
503eb0dcb1bSmartin #ifndef NO_PARTMAN
50458fed2b5Swiz 	    "p"
505eb0dcb1bSmartin #endif
50658fed2b5Swiz 	    "] [-f definition_file] "
50758fed2b5Swiz 	    "[-m message_catalog_dir]"
508eb0dcb1bSmartin 	    "\n"
509eb0dcb1bSmartin 	    "where:\n"
51058fed2b5Swiz 	    "\t-C bg:fg\n\t\tuse different color scheme\n"
511eb0dcb1bSmartin 	    "\t-D\n\t\trun in debug mode\n"
512eb0dcb1bSmartin 	    "\t-f definition_file\n\t\toverride built-in defaults from file\n"
5133b7d675bSmartin 	    "\t-m msg_catalog_dir\n\t\tuse translation files from msg_catalog_dir\n"
514eb0dcb1bSmartin #ifndef NO_PARTMAN
515eb0dcb1bSmartin 	    "\t-p\n\t\tonly run the partition editor, no installation\n"
516eb0dcb1bSmartin #endif
517eb0dcb1bSmartin 	    );
518eb0dcb1bSmartin 
51950dbef1aSdholland 	exit(1);
52050dbef1aSdholland }
52150dbef1aSdholland 
52250dbef1aSdholland /* ARGSUSED */
52350dbef1aSdholland static void
miscsighandler(int signo)52450dbef1aSdholland miscsighandler(int signo)
52550dbef1aSdholland {
52650dbef1aSdholland 
52750dbef1aSdholland 	/*
52850dbef1aSdholland 	 * we need to cleanup(), but it was already scheduled with atexit(),
52950dbef1aSdholland 	 * so it'll be invoked on exit().
53050dbef1aSdholland 	 */
53150dbef1aSdholland 	exit(1);
53250dbef1aSdholland }
53350dbef1aSdholland 
53450dbef1aSdholland static void
ttysighandler(int signo)53550dbef1aSdholland ttysighandler(int signo)
53650dbef1aSdholland {
53750dbef1aSdholland 
53850dbef1aSdholland 	/*
53950dbef1aSdholland 	 * if we want to ignore a TTY signal (SIGINT or SIGQUIT), then we
54050dbef1aSdholland 	 * just return.  If we want to forward a TTY signal, we forward it
54150dbef1aSdholland 	 * to the specified process group.
54250dbef1aSdholland 	 *
54350dbef1aSdholland 	 * This functionality is used when setting up and displaying child
54450dbef1aSdholland 	 * output so that the child gets the signal and presumably dies,
54550dbef1aSdholland 	 * but sysinst continues.  We use this rather than actually ignoring
546cc67c547Smsaitoh 	 * the signals, because that will be passed on to a child
54750dbef1aSdholland 	 * through fork/exec, whereas special handlers get reset on exec..
54850dbef1aSdholland 	 */
54950dbef1aSdholland 	if (ttysig_ignore)
55050dbef1aSdholland 		return;
55150dbef1aSdholland 	if (ttysig_forward) {
55250dbef1aSdholland 		killpg(ttysig_forward, signo);
55350dbef1aSdholland 		return;
55450dbef1aSdholland 	}
55550dbef1aSdholland 
55650dbef1aSdholland 	/*
55750dbef1aSdholland 	 * we need to cleanup(), but it was already scheduled with atexit(),
55850dbef1aSdholland 	 * so it'll be invoked on exit().
55950dbef1aSdholland 	 */
56050dbef1aSdholland 	exit(1);
56150dbef1aSdholland }
56250dbef1aSdholland 
56350dbef1aSdholland static void
cleanup(void)56450dbef1aSdholland cleanup(void)
56550dbef1aSdholland {
56650dbef1aSdholland 	time_t tloc;
56750dbef1aSdholland 
56850dbef1aSdholland 	(void)time(&tloc);
56950dbef1aSdholland 
57050dbef1aSdholland #if 0
57150dbef1aSdholland 	restore_etc();
57250dbef1aSdholland #endif
57350dbef1aSdholland 	/* Ensure we aren't inside the target tree */
57450dbef1aSdholland 	chdir(getenv("HOME"));
57550dbef1aSdholland 	unwind_mounts();
57650dbef1aSdholland 	umount_mnt2();
57798c332e3Smartin 	clear_swap();
57850dbef1aSdholland 
57950dbef1aSdholland 	endwin();
58050dbef1aSdholland 
58150dbef1aSdholland 	if (logfp) {
582e47dca20Schristos 		fprintf(logfp, "Log ended at: %s\n", safectime(&tloc));
58350dbef1aSdholland 		fflush(logfp);
58450dbef1aSdholland 		fclose(logfp);
58550dbef1aSdholland 		logfp = NULL;
58650dbef1aSdholland 	}
58750dbef1aSdholland 	if (script) {
588e47dca20Schristos 		fprintf(script, "# Script ended at: %s\n", safectime(&tloc));
58950dbef1aSdholland 		fflush(script);
59050dbef1aSdholland 		fclose(script);
59150dbef1aSdholland 		script = NULL;
59250dbef1aSdholland 	}
59350dbef1aSdholland 
59450dbef1aSdholland 	if (!exit_cleanly)
59550dbef1aSdholland 		fprintf(stderr, "\n\nsysinst terminated.\n");
59650dbef1aSdholland }
59750dbef1aSdholland 
59850dbef1aSdholland 
59950dbef1aSdholland /* process function ... */
60050dbef1aSdholland 
60150dbef1aSdholland void
process_f_flag(char * f_name)60250dbef1aSdholland process_f_flag(char *f_name)
60350dbef1aSdholland {
60450dbef1aSdholland 	char buffer[STRSIZE];
60550dbef1aSdholland 	int len;
60650dbef1aSdholland 	const struct f_arg *arg;
60750dbef1aSdholland 	FILE *fp;
6084103857bSmartin 	char *cp, *cp1, *err;
60950dbef1aSdholland 
61050dbef1aSdholland 	/* open the file */
61150dbef1aSdholland 	fp = fopen(f_name, "r");
61250dbef1aSdholland 	if (fp == NULL) {
6134103857bSmartin 		const char *args[] = { f_name };
6144103857bSmartin 		err = str_arg_subst(msg_string(MSG_config_open_error),
6154103857bSmartin 		    __arraycount(args), args);
6164103857bSmartin 		fprintf(stderr, "%s\n", err);
6174103857bSmartin 		free(err);
61850dbef1aSdholland 		exit(1);
61950dbef1aSdholland 	}
62050dbef1aSdholland 
62150dbef1aSdholland 	while (fgets(buffer, sizeof buffer, fp) != NULL) {
62250dbef1aSdholland 		cp = buffer + strspn(buffer, " \t");
62350dbef1aSdholland 		if (strchr("#\r\n", *cp) != NULL)
62450dbef1aSdholland 			continue;
62550dbef1aSdholland 		for (arg = fflagopts; arg->name != NULL; arg++) {
62650dbef1aSdholland 			len = strlen(arg->name);
62750dbef1aSdholland 			if (memcmp(cp, arg->name, len) != 0)
62850dbef1aSdholland 				continue;
629bcf119c0Smartin 			if (arg->var == NULL || arg->size == 0)
630bcf119c0Smartin 				continue;
63150dbef1aSdholland 			cp1 = cp + len;
63250dbef1aSdholland 			cp1 += strspn(cp1, " \t");
63350dbef1aSdholland 			if (*cp1++ != '=')
63450dbef1aSdholland 				continue;
63550dbef1aSdholland 			cp1 += strspn(cp1, " \t");
63650dbef1aSdholland 			len = strcspn(cp1, " \n\r\t");
63750dbef1aSdholland 			cp1[len] = 0;
63850dbef1aSdholland 			strlcpy(arg->var, cp1, arg->size);
63950dbef1aSdholland 			break;
64050dbef1aSdholland 		}
64150dbef1aSdholland 	}
64250dbef1aSdholland 
64350dbef1aSdholland 	fclose(fp);
64450dbef1aSdholland }
645ecc52c0dSmartin 
646ecc52c0dSmartin /*
647ecc52c0dSmartin  * return true if we do not have any root certificates installed,
648ecc52c0dSmartin  * so can not verify any trust chain.
649ecc52c0dSmartin  * We rely on /etc/openssl being the OPENSSLDIR and test the
650ecc52c0dSmartin  * "all in one" /etc/openssl/cert.pem first, if that is not found
651ecc52c0dSmartin  * check if there are multiple regular files or symlinks in
652ecc52c0dSmartin  * /etc/openssl/certs/.
653ecc52c0dSmartin  */
654ecc52c0dSmartin static bool
no_openssl_trust_anchors_available(void)655ecc52c0dSmartin no_openssl_trust_anchors_available(void)
656ecc52c0dSmartin {
657ecc52c0dSmartin 	struct stat sb;
658ecc52c0dSmartin 	DIR *dir;
659ecc52c0dSmartin 	struct dirent *ent;
660ecc52c0dSmartin 	size_t cnt;
661ecc52c0dSmartin 
662ecc52c0dSmartin 	/* check the omnibus single file variant first */
663ecc52c0dSmartin 	if (stat("/etc/openssl/cert.pem", &sb) == 0 &&
664ecc52c0dSmartin 	    S_ISREG(sb.st_mode) && sb.st_size > 0)
665ecc52c0dSmartin 		return false;	/* exists and is a non-empty file */
666ecc52c0dSmartin 
667ecc52c0dSmartin 	/* look for files/symlinks in the certs subdirectory */
668ecc52c0dSmartin 	dir = opendir("/etc/openssl/certs");
669ecc52c0dSmartin 	if (dir == NULL)
670ecc52c0dSmartin 		return true;
671ecc52c0dSmartin 	for (cnt = 0; cnt < 2; ) {
672ecc52c0dSmartin 		ent = readdir(dir);
673ecc52c0dSmartin 		if (ent == NULL)
674ecc52c0dSmartin 			break;
675ecc52c0dSmartin 		switch (ent->d_type) {
676ecc52c0dSmartin 		case DT_REG:
677ecc52c0dSmartin 		case DT_LNK:
678ecc52c0dSmartin 			cnt++;
679ecc52c0dSmartin 			break;
680ecc52c0dSmartin 		default:
681ecc52c0dSmartin 			break;
682ecc52c0dSmartin 		}
683ecc52c0dSmartin 	}
684ecc52c0dSmartin 	closedir(dir);
685ecc52c0dSmartin 
686ecc52c0dSmartin 	return cnt < 2;
687ecc52c0dSmartin }
688