xref: /netbsd-src/usr.sbin/sysinst/main.c (revision 2d8e86c2f207da6fbbd50f11b6f33765ebdfa0e9)
1 /*	$NetBSD: main.c,v 1.17 2019/06/22 20:46:07 christos Exp $	*/
2 
3 /*
4  * Copyright 1997 Piermont Information Systems Inc.
5  * All rights reserved.
6  *
7  * Written by Philip A. Nelson for Piermont Information Systems Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of Piermont Information Systems Inc. may not be used to endorse
18  *    or promote products derived from this software without specific prior
19  *    written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 /* main sysinst program. */
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/syslimits.h>
40 #include <sys/uio.h>
41 #include <stdio.h>
42 #include <signal.h>
43 #include <curses.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <dirent.h>
47 #include <locale.h>
48 
49 #include "defs.h"
50 #include "md.h"
51 #include "msg_defs.h"
52 #include "menu_defs.h"
53 #include "txtwalk.h"
54 
55 static void select_language(void);
56 __dead static void usage(void);
57 __dead static void miscsighandler(int);
58 static void ttysighandler(int);
59 static void cleanup(void);
60 static void process_f_flag(char *);
61 
62 static int exit_cleanly = 0;	/* Did we finish nicely? */
63 FILE *logfp;			/* log file */
64 FILE *script;			/* script file */
65 
66 const char *multname;
67 const char *err_outofmem;
68 
69 #ifdef DEBUG
70 extern int log_flip(void);
71 #endif
72 
73 /* Definion for colors */
74 
75 struct {
76 	unsigned int bg;
77 	unsigned int fg;
78 } clr_arg;
79 
80 /* String defaults and stuff for processing the -f file argument. */
81 
82 struct f_arg {
83 	const char *name;
84 	const char *dflt;
85 	char *var;
86 	int size;
87 };
88 
89 static const struct f_arg fflagopts[] = {
90 	{"release", REL, rel, sizeof rel},
91 	{"machine", MACH, machine, sizeof machine},
92 	{"xfer dir", "/usr/INSTALL", xfer_dir, sizeof xfer_dir},
93 	{"ext dir", "", ext_dir_bin, sizeof ext_dir_bin},
94 	{"ext src dir", "", ext_dir_src, sizeof ext_dir_src},
95 	{"ftp host", SYSINST_FTP_HOST, ftp.xfer_host[XFER_FTP], sizeof ftp.xfer_host[XFER_FTP]},
96 	{"http host", SYSINST_HTTP_HOST, ftp.xfer_host[XFER_HTTP], sizeof ftp.xfer_host[XFER_HTTP]},
97 	{"ftp dir", SYSINST_FTP_DIR, ftp.dir, sizeof ftp.dir},
98 	{"ftp prefix", "/" ARCH_SUBDIR "/binary/sets", set_dir_bin, sizeof set_dir_bin},
99 	{"ftp src prefix", "/source/sets", set_dir_src, sizeof set_dir_src},
100 	{"ftp user", "ftp", ftp.user, sizeof ftp.user},
101 	{"ftp pass", "", ftp.pass, sizeof ftp.pass},
102 	{"ftp proxy", "", ftp.proxy, sizeof ftp.proxy},
103 	{"nfs host", "", nfs_host, sizeof nfs_host},
104 	{"nfs dir", "/bsd/release", nfs_dir, sizeof nfs_dir},
105 	{"cd dev", 0, cdrom_dev, sizeof cdrom_dev}, /* default filled in init */
106 	{"fd dev", "/dev/fd0a", fd_dev, sizeof fd_dev},
107 	{"local dev", "", localfs_dev, sizeof localfs_dev},
108 	{"local fs", "ffs", localfs_fs, sizeof localfs_fs},
109 	{"local dir", "release", localfs_dir, sizeof localfs_dir},
110 	{"targetroot mount", "/targetroot", targetroot_mnt, sizeof targetroot_mnt},
111 	{"dist postfix", "." SETS_TAR_SUFF, dist_postfix, sizeof dist_postfix},
112 	{"dist tgz postfix", ".tgz", dist_tgz_postfix, sizeof dist_tgz_postfix},
113 	{"pkg host", SYSINST_PKG_HOST, pkg.xfer_host[XFER_FTP], sizeof pkg.xfer_host[XFER_FTP]},
114 	{"pkg http host", SYSINST_PKG_HTTP_HOST, pkg.xfer_host[XFER_HTTP], sizeof pkg.xfer_host[XFER_HTTP]},
115 	{"pkg dir", SYSINST_PKG_DIR, pkg.dir, sizeof pkg.dir},
116 	{"pkg prefix", "/" PKG_ARCH_SUBDIR "/" PKG_SUBDIR "/All", pkg_dir, sizeof pkg_dir},
117 	{"pkg user", "ftp", pkg.user, sizeof pkg.user},
118 	{"pkg pass", "", pkg.pass, sizeof pkg.pass},
119 	{"pkg proxy", "", pkg.proxy, sizeof pkg.proxy},
120 	{"pkgsrc host", SYSINST_PKGSRC_HOST, pkgsrc.xfer_host[XFER_FTP], sizeof pkgsrc.xfer_host[XFER_FTP]},
121 	{"pkgsrc http host", SYSINST_PKGSRC_HTTP_HOST, pkgsrc.xfer_host[XFER_HTTP], sizeof pkgsrc.xfer_host[XFER_HTTP]},
122 	{"pkgsrc dir", "", pkgsrc.dir, sizeof pkgsrc.dir},
123 	{"pkgsrc prefix", "pub/pkgsrc/stable", pkgsrc_dir, sizeof pkgsrc_dir},
124 	{"pkgsrc user", "ftp", pkgsrc.user, sizeof pkgsrc.user},
125 	{"pkgsrc pass", "", pkgsrc.pass, sizeof pkgsrc.pass},
126 	{"pkgsrc proxy", "", pkgsrc.proxy, sizeof pkgsrc.proxy},
127 
128 	{NULL, NULL, NULL, 0}
129 };
130 
131 static void
132 init(void)
133 {
134 	const struct f_arg *arg;
135 
136 	sizemult = 1;
137 	tmp_ramdisk_size = 0;
138 	clean_xfer_dir = 0;
139 	mnt2_mounted = 0;
140 	fd_type = "msdos";
141 
142 	pm_head = (struct pm_head_t) SLIST_HEAD_INITIALIZER(pm_head);
143 	SLIST_INIT(&pm_head);
144 	pm_new = malloc(sizeof (struct pm_devs));
145 	memset(pm_new, 0, sizeof *pm_new);
146 
147 	for (arg = fflagopts; arg->name != NULL; arg++) {
148 		if (arg->var == cdrom_dev)
149 			get_default_cdrom(arg->var, arg->size);
150 		else
151 			strlcpy(arg->var, arg->dflt, arg->size);
152 	}
153 	pkg.xfer = pkgsrc.xfer = XFER_HTTP;
154 
155 	clr_arg.bg=COLOR_BLUE;
156 	clr_arg.fg=COLOR_WHITE;
157 }
158 
159 static void
160 init_lang(void)
161 {
162 	sizemult = 1;
163 	err_outofmem = msg_string(MSG_out_of_memory);
164 	multname = msg_string(MSG_secname);
165 }
166 
167 __weakref_visible void prelim_menu(void)
168     __weak_reference(md_prelim_menu);
169 
170 int
171 main(int argc, char **argv)
172 {
173 	int ch;
174 
175 	init();
176 
177 #ifdef DEBUG
178 	log_flip();
179 #endif
180 
181 	/* Check for TERM ... */
182 	if (!getenv("TERM")) {
183 		(void)fprintf(stderr,
184 			 "sysinst: environment variable TERM not set.\n");
185 		exit(4);
186 	}
187 
188 	/* argv processing */
189 	while ((ch = getopt(argc, argv, "Dr:f:C:"
190 #ifndef NO_PARTMAN
191 	    "p"
192 #endif
193 	    )) != -1)
194 		switch(ch) {
195 		case 'D':	/* set to get past certain errors in testing */
196 			debug = 1;
197 			break;
198 		case 'r':
199 			/* Release name other than compiled in release. */
200 			strncpy(rel, optarg, sizeof rel);
201 			break;
202 		case 'f':
203 			/* Definition file to read. */
204 			process_f_flag(optarg);
205 			break;
206 		case 'C':
207 			/* Define colors */
208 			sscanf(optarg, "%u:%u", &clr_arg.bg, &clr_arg.fg);
209 			break;
210 #ifndef NO_PARTMAN
211 		case 'p':
212 			/* Partition tool */
213 			partman_go = 1;
214 			break;
215 #endif
216 		case '?':
217 		default:
218 			usage();
219 		}
220 
221 	md_init();
222 
223 	/* Initialize the partitioning subsystem */
224 	partitions_init();
225 
226 	/* initialize message window */
227 	if (menu_init()) {
228 		__menu_initerror();
229 		exit(4);
230 	}
231 
232 	/*
233 	 * Put 'messages' in a window that has a one-character border
234 	 * on the real screen.
235 	 */
236 	mainwin = newwin(getmaxy(stdscr) - 2, getmaxx(stdscr) - 2, 1, 1);
237 	if (mainwin == NULL) {
238 		(void)fprintf(stderr,
239 			 "sysinst: screen too small\n");
240 		exit(1);
241 	}
242 	if (has_colors()) {
243 		start_color();
244 		do_coloring(clr_arg.fg,clr_arg.bg);
245 	} else {
246 		remove_color_options();
247 	}
248 	msg_window(mainwin);
249 
250 	/* Watch for signals and clean up */
251 	(void)atexit(cleanup);
252 	(void)signal(SIGINT, ttysighandler);
253 	(void)signal(SIGQUIT, ttysighandler);
254 	(void)signal(SIGHUP, miscsighandler);
255 
256 	/* redraw screen */
257 	touchwin(stdscr);
258 	refresh();
259 
260 	/* Ensure we have mountpoint for target filesystems */
261 	mkdir(targetroot_mnt, S_IRWXU| S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH);
262 
263 	select_language();
264 	get_kb_encoding();
265 	init_lang();
266 
267 #ifdef __weak_reference
268 	/* if md wants to ask anything before we start, do it now */
269 	if (prelim_menu != 0)
270 		prelim_menu();
271 #endif
272 
273 	/* Menu processing */
274 	if (partman_go)
275 		partman();
276 	else
277 		process_menu(MENU_netbsd, NULL);
278 
279 #ifndef NO_PARTMAN
280 	/* clean up internal storage */
281 	pm_destroy_all();
282 #endif
283 
284 	exit_cleanly = 1;
285 	return 0;
286 }
287 
288 static int
289 set_language(menudesc *m, void *arg)
290 {
291 	char **fnames = arg;
292 
293 	msg_file(fnames[m->cursel]);
294 	return 1;
295 }
296 
297 static void
298 select_language(void)
299 {
300 	DIR *dir;
301 	struct dirent *dirent;
302 	char **lang_msg, **fnames;
303 	char prefix[PATH_MAX], fname[PATH_MAX];
304 	int max_lang = 16, num_lang = 0;
305 	const char *cp;
306 	menu_ent *opt = 0;
307 	int lang_menu = -1;
308 	int lang;
309 
310 #ifdef CATALOG_DIR
311 	strcpy(prefix, CATALOG_DIR "/");
312 	dir = opendir(CATALOG_DIR);
313 	if (!dir) {
314 		strcpy(prefix, "./");
315 		dir = opendir(".");
316 	}
317 #else
318 	dir = opendir(".");
319 	strcpy(prefix, "./");
320 #endif
321 	if (!dir)
322 		return;
323 
324 	lang_msg = malloc(max_lang * sizeof *lang_msg);
325 	fnames = malloc(max_lang * sizeof *fnames);
326 	if (!lang_msg || !fnames)
327 		goto done;
328 
329 	lang_msg[0] = strdup(msg_string(MSG_sysinst_message_language));
330 	fnames[0] = 0;
331 	num_lang = 1;
332 
333 	while ((dirent = readdir(dir)) != 0) {
334 		if (memcmp(dirent->d_name, "sysinstmsgs.", 12))
335 			continue;
336 		strcpy(fname, prefix);
337 		strcat(fname, dirent->d_name);
338 		if (msg_file(fname))
339 			continue;
340 		cp = msg_string(MSG_sysinst_message_language);
341 		if (!strcmp(cp, lang_msg[0]))
342 			continue;
343 		if (num_lang == max_lang) {
344 			char **new;
345 			max_lang *= 2;
346 			new = realloc(lang_msg, max_lang * sizeof *lang_msg);
347 			if (!new)
348 				break;
349 			lang_msg = new;
350 			new = realloc(fnames, max_lang * sizeof *fnames);
351 			if (!new)
352 				break;
353 			fnames = new;
354 		}
355 		fnames[num_lang] = strdup(fname);
356 		lang_msg[num_lang++] = strdup(cp);
357 	}
358 	msg_file(0);
359 	closedir(dir);
360 	dir = 0;
361 
362 	if (num_lang == 1)
363 		goto done;
364 
365 	opt = calloc(num_lang, sizeof *opt);
366 	if (!opt)
367 		goto done;
368 
369 	for (lang = 0; lang < num_lang; lang++) {
370 		opt[lang].opt_name = lang_msg[lang];
371 		opt[lang].opt_action = set_language;
372 	}
373 
374 	lang_menu = new_menu(NULL, opt, num_lang, -1, 12, 0, 0, MC_NOEXITOPT,
375 		NULL, NULL, NULL, NULL, NULL);
376 
377 	if (lang_menu != -1) {
378 		msg_display(MSG_hello);
379 		process_menu(lang_menu, fnames);
380 	}
381 
382     done:
383 	if (dir)
384 		closedir(dir);
385 	if (lang_menu != -1)
386 		free_menu(lang_menu);
387 	free(opt);
388 	while (num_lang) {
389 		free(lang_msg[--num_lang]);
390 		free(fnames[num_lang]);
391 	}
392 	free(lang_msg);
393 	free(fnames);
394 
395 	/* set locale according to selected language */
396 	cp = msg_string(MSG_sysinst_message_locale);
397 	if (cp) {
398 		setlocale(LC_CTYPE, cp);
399 		setenv("LC_CTYPE", cp, 1);
400 	}
401 }
402 
403 #ifndef md_may_remove_boot_medium
404 #define md_may_remove_boot_medium()	(boot_media_still_needed()<=0)
405 #endif
406 
407 /* toplevel menu handler ... */
408 void
409 toplevel(void)
410 {
411 	/*
412 	 * Undo any stateful side-effects of previous menu choices.
413 	 * XXX must be idempotent, since we get run each time the main
414 	 *     menu is displayed.
415 	 */
416 	char *home = getenv("HOME");
417 	if (home != NULL)
418 		if (chdir(home) != 0)
419 			(void)chdir("/");
420 	unwind_mounts();
421 
422 	/* Display banner message in (english, francais, deutsch..) */
423 	msg_display(MSG_hello);
424 	msg_display_add(MSG_md_hello);
425 	if (md_may_remove_boot_medium())
426 		msg_display_add(MSG_md_may_remove_boot_medium);
427 	msg_display_add(MSG_thanks);
428 }
429 
430 
431 /* The usage ... */
432 
433 static void
434 usage(void)
435 {
436 
437 	(void)fprintf(stderr, "usage: sysinst [-D] [-f definition_file] "
438 	    "[-r release] [-C bg:fg]"
439 #ifndef NO_PARTMAN
440 	    " [-p]"
441 #endif
442 	    "\n"
443 	    "where:\n"
444 	    "\t-D\n\t\trun in debug mode\n"
445 	    "\t-f definition_file\n\t\toverride built-in defaults from file\n"
446 	    "\t-r release\n\t\toverride release name\n"
447 	    "\t-C bg:fg\n\t\tuse different color scheme\n"
448 #ifndef NO_PARTMAN
449 	    "\t-p\n\t\tonly run the partition editor, no installation\n"
450 #endif
451 	    );
452 
453 	exit(1);
454 }
455 
456 /* ARGSUSED */
457 static void
458 miscsighandler(int signo)
459 {
460 
461 	/*
462 	 * we need to cleanup(), but it was already scheduled with atexit(),
463 	 * so it'll be invoked on exit().
464 	 */
465 	exit(1);
466 }
467 
468 static void
469 ttysighandler(int signo)
470 {
471 
472 	/*
473 	 * if we want to ignore a TTY signal (SIGINT or SIGQUIT), then we
474 	 * just return.  If we want to forward a TTY signal, we forward it
475 	 * to the specified process group.
476 	 *
477 	 * This functionality is used when setting up and displaying child
478 	 * output so that the child gets the signal and presumably dies,
479 	 * but sysinst continues.  We use this rather than actually ignoring
480 	 * the signals, because that will be be passed on to a child
481 	 * through fork/exec, whereas special handlers get reset on exec..
482 	 */
483 	if (ttysig_ignore)
484 		return;
485 	if (ttysig_forward) {
486 		killpg(ttysig_forward, signo);
487 		return;
488 	}
489 
490 	/*
491 	 * we need to cleanup(), but it was already scheduled with atexit(),
492 	 * so it'll be invoked on exit().
493 	 */
494 	exit(1);
495 }
496 
497 static void
498 cleanup(void)
499 {
500 	time_t tloc;
501 
502 	(void)time(&tloc);
503 
504 #if 0
505 	restore_etc();
506 #endif
507 	/* Ensure we aren't inside the target tree */
508 	chdir(getenv("HOME"));
509 	unwind_mounts();
510 	umount_mnt2();
511 
512 	endwin();
513 
514 	if (logfp) {
515 		fprintf(logfp, "Log ended at: %s\n", safectime(&tloc));
516 		fflush(logfp);
517 		fclose(logfp);
518 		logfp = NULL;
519 	}
520 	if (script) {
521 		fprintf(script, "# Script ended at: %s\n", safectime(&tloc));
522 		fflush(script);
523 		fclose(script);
524 		script = NULL;
525 	}
526 
527 	if (!exit_cleanly)
528 		fprintf(stderr, "\n\nsysinst terminated.\n");
529 }
530 
531 
532 /* process function ... */
533 
534 void
535 process_f_flag(char *f_name)
536 {
537 	char buffer[STRSIZE];
538 	int len;
539 	const struct f_arg *arg;
540 	FILE *fp;
541 	char *cp, *cp1, *err;
542 
543 	/* open the file */
544 	fp = fopen(f_name, "r");
545 	if (fp == NULL) {
546 		const char *args[] = { f_name };
547 		err = str_arg_subst(msg_string(MSG_config_open_error),
548 		    __arraycount(args), args);
549 		fprintf(stderr, "%s\n", err);
550 		free(err);
551 		exit(1);
552 	}
553 
554 	while (fgets(buffer, sizeof buffer, fp) != NULL) {
555 		cp = buffer + strspn(buffer, " \t");
556 		if (strchr("#\r\n", *cp) != NULL)
557 			continue;
558 		for (arg = fflagopts; arg->name != NULL; arg++) {
559 			len = strlen(arg->name);
560 			if (memcmp(cp, arg->name, len) != 0)
561 				continue;
562 			cp1 = cp + len;
563 			cp1 += strspn(cp1, " \t");
564 			if (*cp1++ != '=')
565 				continue;
566 			cp1 += strspn(cp1, " \t");
567 			len = strcspn(cp1, " \n\r\t");
568 			cp1[len] = 0;
569 			strlcpy(arg->var, cp1, arg->size);
570 			break;
571 		}
572 	}
573 
574 	fclose(fp);
575 }
576