main.c revision 1.33.2.1 1 /* $NetBSD: main.c,v 1.33.2.1 2025/08/02 05:58:55 perseant 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 int debug;
56 char machine[SSTRSIZE];
57 int ignorerror;
58 int ttysig_ignore;
59 pid_t ttysig_forward;
60 uint sizemult;
61 int partman_go;
62 FILE *logfp;
63 FILE *script;
64 daddr_t root_limit;
65 struct pm_head_t pm_head;
66 struct pm_devs *pm;
67 struct pm_devs *pm_new;
68 char xfer_dir[STRSIZE];
69 int clean_xfer_dir;
70 char ext_dir_bin[STRSIZE];
71 char ext_dir_src[STRSIZE];
72 char ext_dir_pkgsrc[STRSIZE];
73 char set_dir_bin[STRSIZE];
74 char set_dir_src[STRSIZE];
75 char pkg_dir[STRSIZE];
76 char pkgsrc_dir[STRSIZE];
77 const char *ushell;
78 struct ftpinfo ftp, pkg, pkgsrc;
79 int (*fetch_fn)(const char *);
80 char nfs_host[STRSIZE];
81 char nfs_dir[STRSIZE];
82 char cdrom_dev[SSTRSIZE];
83 char fd_dev[SSTRSIZE];
84 const char *fd_type;
85 char localfs_dev[SSTRSIZE];
86 char localfs_fs[SSTRSIZE];
87 char localfs_dir[STRSIZE];
88 char targetroot_mnt[SSTRSIZE];
89 int mnt2_mounted;
90 char dist_postfix[SSTRSIZE];
91 char dist_tgz_postfix[SSTRSIZE];
92 WINDOW *mainwin;
93
94 static void select_language(const char*);
95 __dead static void usage(void);
96 __dead static void miscsighandler(int);
97 static void ttysighandler(int);
98 static void cleanup(void);
99 static void process_f_flag(char *);
100 static bool no_openssl_trust_anchors_available(void);
101
102 static int exit_cleanly = 0; /* Did we finish nicely? */
103 FILE *logfp; /* log file */
104 FILE *script; /* script file */
105
106 const char *multname;
107 const char *err_outofmem;
108
109 #ifdef DEBUG
110 extern int log_flip(void);
111 #endif
112
113 /* Definion for colors */
114
115 struct {
116 unsigned int bg;
117 unsigned int fg;
118 } clr_arg;
119
120 /* String defaults and stuff for processing the -f file argument. */
121
122 struct f_arg {
123 const char *name;
124 const char *dflt;
125 char *var;
126 int size;
127 };
128
129 static const struct f_arg fflagopts[] = {
130 {"release", REL, NULL, 0},
131 {"machine", MACH, machine, sizeof machine},
132 {"xfer dir", "/usr/INSTALL", xfer_dir, sizeof xfer_dir},
133 {"ext dir", "", ext_dir_bin, sizeof ext_dir_bin},
134 {"ext src dir", "", ext_dir_src, sizeof ext_dir_src},
135 {"ftp host", SYSINST_FTP_HOST, ftp.xfer_host[XFER_HOST(XFER_FTP)], sizeof ftp.xfer_host[XFER_HOST(XFER_FTP)]},
136 {"http host", SYSINST_HTTP_HOST, ftp.xfer_host[XFER_HOST(XFER_HTTP)], sizeof ftp.xfer_host[XFER_HOST(XFER_HTTP)]},
137 {"ftp dir", SYSINST_FTP_DIR, ftp.dir, sizeof ftp.dir},
138 {"ftp prefix", "/" ARCH_SUBDIR "/binary/sets", set_dir_bin, sizeof set_dir_bin},
139 {"ftp src prefix", "/source/sets", set_dir_src, sizeof set_dir_src},
140 {"ftp user", "ftp", ftp.user, sizeof ftp.user},
141 {"ftp pass", "", ftp.pass, sizeof ftp.pass},
142 {"ftp proxy", "", ftp.proxy, sizeof ftp.proxy},
143 {"nfs host", "", nfs_host, sizeof nfs_host},
144 {"nfs dir", "/bsd/release", nfs_dir, sizeof nfs_dir},
145 {"cd dev", 0, cdrom_dev, sizeof cdrom_dev}, /* default filled in init */
146 {"fd dev", "/dev/fd0a", fd_dev, sizeof fd_dev},
147 {"local dev", "", localfs_dev, sizeof localfs_dev},
148 {"local fs", "ffs", localfs_fs, sizeof localfs_fs},
149 {"local dir", "release", localfs_dir, sizeof localfs_dir},
150 {"targetroot mount", "/targetroot", targetroot_mnt, sizeof targetroot_mnt},
151 {"dist postfix", "." SETS_TAR_SUFF, dist_postfix, sizeof dist_postfix},
152 {"dist tgz postfix", ".tgz", dist_tgz_postfix, sizeof dist_tgz_postfix},
153 {"pkg host", SYSINST_PKG_HOST, pkg.xfer_host[XFER_HOST(XFER_FTP)], sizeof pkg.xfer_host[XFER_HOST(XFER_FTP)]},
154 {"pkg http host", SYSINST_PKG_HTTP_HOST, pkg.xfer_host[XFER_HOST(XFER_HTTP)], sizeof pkg.xfer_host[XFER_HOST(XFER_HTTP)]},
155 {"pkg dir", SYSINST_PKG_DIR, pkg.dir, sizeof pkg.dir},
156 {"pkg prefix", "/" PKG_ARCH_SUBDIR "/" PKG_SUBDIR "/All", pkg_dir, sizeof pkg_dir},
157 {"pkg user", "ftp", pkg.user, sizeof pkg.user},
158 {"pkg pass", "", pkg.pass, sizeof pkg.pass},
159 {"pkg proxy", "", pkg.proxy, sizeof pkg.proxy},
160 {"pkgsrc host", SYSINST_PKGSRC_HOST, pkgsrc.xfer_host[XFER_HOST(XFER_FTP)], sizeof pkgsrc.xfer_host[XFER_HOST(XFER_FTP)]},
161 {"pkgsrc http host", SYSINST_PKGSRC_HTTP_HOST, pkgsrc.xfer_host[XFER_HOST(XFER_HTTP)], sizeof pkgsrc.xfer_host[XFER_HOST(XFER_HTTP)]},
162 {"pkgsrc dir", "", pkgsrc.dir, sizeof pkgsrc.dir},
163 {"pkgsrc prefix", "pub/pkgsrc/stable", pkgsrc_dir, sizeof pkgsrc_dir},
164 {"pkgsrc user", "ftp", pkgsrc.user, sizeof pkgsrc.user},
165 {"pkgsrc pass", "", pkgsrc.pass, sizeof pkgsrc.pass},
166 {"pkgsrc proxy", "", pkgsrc.proxy, sizeof pkgsrc.proxy},
167
168 {NULL, NULL, NULL, 0}
169 };
170
171 static void
172 init(void)
173 {
174 const struct f_arg *arg;
175
176 sizemult = 1;
177 clean_xfer_dir = 0;
178 mnt2_mounted = 0;
179 fd_type = "msdos";
180
181 pm_head = (struct pm_head_t) SLIST_HEAD_INITIALIZER(pm_head);
182 SLIST_INIT(&pm_head);
183 pm_new = malloc(sizeof (struct pm_devs));
184 memset(pm_new, 0, sizeof *pm_new);
185
186 for (arg = fflagopts; arg->name != NULL; arg++) {
187 if (arg->var == NULL)
188 continue;
189 if (arg->var == cdrom_dev)
190 get_default_cdrom(arg->var, arg->size);
191 else
192 strlcpy(arg->var, arg->dflt, arg->size);
193 }
194 ftp.xfer = pkg.xfer = pkgsrc.xfer = XFER_HTTPS;
195
196 clr_arg.bg=COLOR_BLUE;
197 clr_arg.fg=COLOR_WHITE;
198 }
199
200 static void
201 init_lang(void)
202 {
203 sizemult = 1;
204 err_outofmem = msg_string(MSG_out_of_memory);
205 multname = msg_string(MSG_secname);
206 }
207
208 int
209 main(int argc, char **argv)
210 {
211 int ch, no_https = 0;
212 const char *msg_cat_dir = NULL;
213
214 init();
215
216 #ifdef DEBUG
217 log_flip();
218 #endif
219
220 /* Check for TERM ... */
221 if (!getenv("TERM")) {
222 (void)fprintf(stderr,
223 "sysinst: environment variable TERM not set.\n");
224 exit(4);
225 }
226
227 /* argv processing */
228 while ((ch = getopt(argc, argv, "Dr:f:C:m:"
229 #ifndef NO_PARTMAN
230 "p"
231 #endif
232 )) != -1)
233 switch(ch) {
234 case 'D': /* set to get past certain errors in testing */
235 debug = 1;
236 break;
237 case 'r':
238 /* Release name - ignore for compatibility with older versions */
239 break;
240 case 'f':
241 /* Definition file to read. */
242 process_f_flag(optarg);
243 break;
244 case 'C':
245 /* Define colors */
246 sscanf(optarg, "%u:%u", &clr_arg.bg, &clr_arg.fg);
247 break;
248 case 'm':
249 /* set message catalog directory */
250 msg_cat_dir = optarg;
251 break;
252 #ifndef NO_PARTMAN
253 case 'p':
254 /* Partition tool */
255 partman_go = 1;
256 break;
257 #endif
258 case '?':
259 default:
260 usage();
261 }
262
263 md_init();
264
265 /* Initialize the partitioning subsystem */
266 partitions_init();
267
268 /* do we need to tell ftp(1) to avoid checking certificate chains? */
269 if (no_openssl_trust_anchors_available()) {
270 setenv("FTPSSLNOVERIFY", "1", 1);
271 no_https = 1;
272 }
273
274 /* initialize message window */
275 if (menu_init()) {
276 __menu_initerror();
277 exit(4);
278 }
279
280 /*
281 * Put 'messages' in a window that has a one-character border
282 * on the real screen.
283 */
284 mainwin = newwin(getmaxy(stdscr) - 2, getmaxx(stdscr) - 2, 1, 1);
285 if (mainwin == NULL) {
286 (void)fprintf(stderr,
287 "sysinst: screen too small\n");
288 exit(1);
289 }
290 if (has_colors()) {
291 start_color();
292 do_coloring(clr_arg.fg,clr_arg.bg);
293 }
294 msg_window(mainwin);
295
296 /* Watch for signals and clean up */
297 (void)atexit(cleanup);
298 (void)signal(SIGINT, ttysighandler);
299 (void)signal(SIGQUIT, ttysighandler);
300 (void)signal(SIGHUP, miscsighandler);
301
302 /* redraw screen */
303 touchwin(stdscr);
304 refresh();
305
306 /* Ensure we have mountpoint for target filesystems */
307 mkdir(targetroot_mnt, S_IRWXU | S_IRGRP|S_IXGRP | S_IROTH|S_IXOTH);
308
309 select_language(msg_cat_dir);
310 get_kb_encoding();
311 init_lang();
312
313 /* remove some invalid menu entries */
314 if (!has_colors())
315 remove_color_options();
316 if (no_https)
317 remove_https_options();
318
319 /* Menu processing */
320 if (partman_go)
321 partman(NULL);
322 else
323 process_menu(MENU_netbsd, NULL);
324
325 #ifndef NO_PARTMAN
326 /* clean up internal storage */
327 pm_destroy_all();
328 #endif
329
330 partitions_cleanup();
331
332 exit_cleanly = 1;
333 return 0;
334 }
335
336 static int
337 set_language(menudesc *m, void *arg)
338 {
339 char **fnames = arg;
340
341 msg_file(fnames[m->cursel]);
342 return 1;
343 }
344
345 /*
346 * Search for sysinstmsg.* files in the given dir, collect
347 * their names and return the number of files found.
348 * fnames[0] is preallocated and duplicates are ignored.
349 */
350 struct found_msgs {
351 char **lang_msg, **fnames;
352 int max_lang, num_lang;
353
354 };
355 static void
356 find_language_files(const char *path, struct found_msgs *res)
357 {
358 DIR *dir;
359 struct dirent *dirent;
360 char fname[PATH_MAX];
361 const char *cp;
362
363 res->num_lang = 0;
364 dir = opendir(path);
365 if (!dir)
366 return;
367
368 while ((dirent = readdir(dir)) != 0) {
369 if (memcmp(dirent->d_name, "sysinstmsgs.", 12))
370 continue;
371
372 if (res->num_lang == 0)
373 res->num_lang = 1;
374 strcpy(fname, path);
375 strcat(fname, "/");
376 strcat(fname, dirent->d_name);
377 if (msg_file(fname))
378 continue;
379 cp = msg_string(MSG_sysinst_message_language);
380 if (!strcmp(cp, res->lang_msg[0]))
381 continue;
382 if (res->num_lang == res->max_lang) {
383 char **new;
384 res->max_lang *= 2;
385 new = realloc(res->lang_msg,
386 res->max_lang * sizeof *res->lang_msg);
387 if (!new)
388 break;
389 res->lang_msg = new;
390 new = realloc(res->fnames,
391 res->max_lang * sizeof *res->fnames);
392 if (!new)
393 break;
394 res->fnames = new;
395 }
396 res->fnames[res->num_lang] = strdup(fname);
397 res->lang_msg[res->num_lang++] = strdup(cp);
398 }
399
400 closedir(dir);
401 }
402
403 static void
404 select_language(const char *msg_cat_path)
405 {
406 struct found_msgs found;
407 menu_ent *opt = 0;
408 const char *cp;
409 int lang_menu = -1;
410 int lang;
411
412 found.max_lang = 16;
413 found.num_lang = 0;
414 found.lang_msg = malloc(found.max_lang * sizeof *found.lang_msg);
415 found.fnames = malloc(found.max_lang * sizeof *found.fnames);
416 if (!found.lang_msg || !found.fnames)
417 goto done;
418 found.lang_msg[0] = strdup(msg_string(MSG_sysinst_message_language));
419 found.fnames[0] = NULL;
420
421 if (msg_cat_path != NULL)
422 find_language_files(msg_cat_path, &found);
423 if (found.num_lang == 0)
424 find_language_files(".", &found);
425 #ifdef CATALOG_DIR
426 if (found.num_lang == 0)
427 find_language_files(CATALOG_DIR, &found);
428 #endif
429
430 msg_file(0);
431
432 if (found.num_lang <= 1)
433 goto done;
434
435 opt = calloc(found.num_lang, sizeof *opt);
436 if (!opt)
437 goto done;
438
439 for (lang = 0; lang < found.num_lang; lang++) {
440 opt[lang].opt_name = found.lang_msg[lang];
441 opt[lang].opt_action = set_language;
442 }
443
444 lang_menu = new_menu(NULL, opt, found.num_lang, -1, 12, 0, 0,
445 MC_NOEXITOPT, NULL, NULL, NULL, NULL, NULL);
446
447 if (lang_menu != -1) {
448 msg_display(MSG_hello);
449 process_menu(lang_menu, found.fnames);
450 }
451
452 done:
453 if (lang_menu != -1)
454 free_menu(lang_menu);
455 free(opt);
456 for (int i = 0; i < found.num_lang; i++) {
457 free(found.lang_msg[i]);
458 free(found.fnames[i]);
459 }
460 free(found.lang_msg);
461 free(found.fnames);
462
463 /* set locale according to selected language */
464 cp = msg_string(MSG_sysinst_message_locale);
465 if (cp) {
466 setlocale(LC_CTYPE, cp);
467 setenv("LC_CTYPE", cp, 1);
468 }
469 }
470
471 #ifndef md_may_remove_boot_medium
472 #define md_may_remove_boot_medium() (boot_media_still_needed()<=0)
473 #endif
474
475 /* toplevel menu handler ... */
476 void
477 toplevel(void)
478 {
479 /*
480 * Undo any stateful side-effects of previous menu choices.
481 * XXX must be idempotent, since we get run each time the main
482 * menu is displayed.
483 */
484 char *home = getenv("HOME");
485 if (home != NULL)
486 if (chdir(home) != 0)
487 (void)chdir("/");
488 unwind_mounts();
489 clear_swap();
490
491 /* Display banner message in (english, francais, deutsch..) */
492 msg_display(MSG_hello);
493 msg_display_add(MSG_md_hello);
494 if (md_may_remove_boot_medium())
495 msg_display_add(MSG_md_may_remove_boot_medium);
496 msg_display_add(MSG_thanks);
497 }
498
499
500 /* The usage ... */
501
502 static void
503 usage(void)
504 {
505
506 (void)fprintf(stderr, "usage: sysinst [-C bg:fg] [-D"
507 #ifndef NO_PARTMAN
508 "p"
509 #endif
510 "] [-f definition_file] "
511 "[-m message_catalog_dir]"
512 "\n"
513 "where:\n"
514 "\t-C bg:fg\n\t\tuse different color scheme\n"
515 "\t-D\n\t\trun in debug mode\n"
516 "\t-f definition_file\n\t\toverride built-in defaults from file\n"
517 "\t-m msg_catalog_dir\n\t\tuse translation files from msg_catalog_dir\n"
518 #ifndef NO_PARTMAN
519 "\t-p\n\t\tonly run the partition editor, no installation\n"
520 #endif
521 );
522
523 exit(1);
524 }
525
526 /* ARGSUSED */
527 static void
528 miscsighandler(int signo)
529 {
530
531 /*
532 * we need to cleanup(), but it was already scheduled with atexit(),
533 * so it'll be invoked on exit().
534 */
535 exit(1);
536 }
537
538 static void
539 ttysighandler(int signo)
540 {
541
542 /*
543 * if we want to ignore a TTY signal (SIGINT or SIGQUIT), then we
544 * just return. If we want to forward a TTY signal, we forward it
545 * to the specified process group.
546 *
547 * This functionality is used when setting up and displaying child
548 * output so that the child gets the signal and presumably dies,
549 * but sysinst continues. We use this rather than actually ignoring
550 * the signals, because that will be passed on to a child
551 * through fork/exec, whereas special handlers get reset on exec..
552 */
553 if (ttysig_ignore)
554 return;
555 if (ttysig_forward) {
556 killpg(ttysig_forward, signo);
557 return;
558 }
559
560 /*
561 * we need to cleanup(), but it was already scheduled with atexit(),
562 * so it'll be invoked on exit().
563 */
564 exit(1);
565 }
566
567 static void
568 cleanup(void)
569 {
570 time_t tloc;
571
572 (void)time(&tloc);
573
574 #if 0
575 restore_etc();
576 #endif
577 /* Ensure we aren't inside the target tree */
578 chdir(getenv("HOME"));
579 unwind_mounts();
580 umount_mnt2();
581 clear_swap();
582
583 endwin();
584
585 if (logfp) {
586 fprintf(logfp, "Log ended at: %s\n", safectime(&tloc));
587 fflush(logfp);
588 fclose(logfp);
589 logfp = NULL;
590 }
591 if (script) {
592 fprintf(script, "# Script ended at: %s\n", safectime(&tloc));
593 fflush(script);
594 fclose(script);
595 script = NULL;
596 }
597
598 if (!exit_cleanly)
599 fprintf(stderr, "\n\nsysinst terminated.\n");
600 }
601
602
603 /* process function ... */
604
605 void
606 process_f_flag(char *f_name)
607 {
608 char buffer[STRSIZE];
609 int len;
610 const struct f_arg *arg;
611 FILE *fp;
612 char *cp, *cp1, *err;
613
614 /* open the file */
615 fp = fopen(f_name, "r");
616 if (fp == NULL) {
617 const char *args[] = { f_name };
618 err = str_arg_subst(msg_string(MSG_config_open_error),
619 __arraycount(args), args);
620 fprintf(stderr, "%s\n", err);
621 free(err);
622 exit(1);
623 }
624
625 while (fgets(buffer, sizeof buffer, fp) != NULL) {
626 cp = buffer + strspn(buffer, " \t");
627 if (strchr("#\r\n", *cp) != NULL)
628 continue;
629 for (arg = fflagopts; arg->name != NULL; arg++) {
630 len = strlen(arg->name);
631 if (memcmp(cp, arg->name, len) != 0)
632 continue;
633 if (arg->var == NULL || arg->size == 0)
634 continue;
635 cp1 = cp + len;
636 cp1 += strspn(cp1, " \t");
637 if (*cp1++ != '=')
638 continue;
639 cp1 += strspn(cp1, " \t");
640 len = strcspn(cp1, " \n\r\t");
641 cp1[len] = 0;
642 strlcpy(arg->var, cp1, arg->size);
643 break;
644 }
645 }
646
647 fclose(fp);
648 }
649
650 /*
651 * return true if we do not have any root certificates installed,
652 * so can not verify any trust chain.
653 * We rely on /etc/openssl being the OPENSSLDIR and test the
654 * "all in one" /etc/openssl/cert.pem first, if that is not found
655 * check if there are multiple regular files or symlinks in
656 * /etc/openssl/certs/.
657 */
658 static bool
659 no_openssl_trust_anchors_available(void)
660 {
661 struct stat sb;
662 DIR *dir;
663 struct dirent *ent;
664 size_t cnt;
665
666 /* check the omnibus single file variant first */
667 if (stat("/etc/openssl/cert.pem", &sb) == 0 &&
668 S_ISREG(sb.st_mode) && sb.st_size > 0)
669 return false; /* exists and is a non-empty file */
670
671 /* look for files/symlinks in the certs subdirectory */
672 dir = opendir("/etc/openssl/certs");
673 if (dir == NULL)
674 return true;
675 for (cnt = 0; cnt < 2; ) {
676 ent = readdir(dir);
677 if (ent == NULL)
678 break;
679 switch (ent->d_type) {
680 case DT_REG:
681 case DT_LNK:
682 cnt++;
683 break;
684 default:
685 break;
686 }
687 }
688 closedir(dir);
689
690 return cnt < 2;
691 }
692