Home | History | Annotate | Line # | Download | only in sysinst
configmenu.c revision 1.15
      1 /* $NetBSD: configmenu.c,v 1.15 2022/04/21 17:30:15 martin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jeffrey C. Rizzo
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /* configmenu.c -- post-installation system configuration menu. */
     33 
     34 #include <stdio.h>
     35 #include <curses.h>
     36 #include <unistd.h>
     37 #include <errno.h>
     38 #include "defs.h"
     39 #include "msg_defs.h"
     40 #include "menu_defs.h"
     41 
     42 
     43 static int set_network(struct menudesc*, void *);
     44 static int set_timezone_menu(struct menudesc *, void *);
     45 static int set_root_shell(struct menudesc *, void *);
     46 static int change_root_password(struct menudesc *, void *);
     47 static int add_new_user(struct menudesc *, void *);
     48 #if CHECK_ENTROPY
     49 static int add_entropy(struct menudesc *, void *);
     50 #endif
     51 static int set_binpkg(struct menudesc *, void *);
     52 static int set_pkgsrc(struct menudesc *, void *);
     53 static void config_list_init(void);
     54 static void get_rootsh(void);
     55 static int toggle_rcvar(struct menudesc *, void *);
     56 static void configmenu_hdr(struct menudesc *, void *);
     57 static int check_root_password(void);
     58 
     59 char pkgpath[STRSIZE];
     60 char pkgsrcpath[STRSIZE];
     61 
     62 extern const char *tz_default;
     63 
     64 enum {
     65 	CONFIGOPT_NETCONF,
     66 	CONFIGOPT_TZ,
     67 	CONFIGOPT_ROOTSH,
     68 	CONFIGOPT_ROOTPW,
     69 	CONFIGOPT_BINPKG,
     70 	CONFIGOPT_PKGSRC,
     71 	CONFIGOPT_SSHD,
     72 	CONFIGOPT_NTPD,
     73 	CONFIGOPT_NTPDATE,
     74 	CONFIGOPT_MDNSD,
     75 	CONFIGOPT_XDM,
     76 	CONFIGOPT_CGD,
     77 	CONFIGOPT_LVM,
     78 	CONFIGOPT_RAIDFRAME,
     79 	CONFIGOPT_ADDUSER,
     80 	CONFIGOPT_ADD_ENTROPY,
     81 	CONFIGOPT_LAST
     82 };
     83 
     84 typedef struct configinfo {
     85 	const char	*optname;
     86 	uint		opt;
     87 	const char	*rcvar;
     88 	int		(*action)(struct menudesc *, void *);
     89 	const char	*setting;
     90 } configinfo;
     91 
     92 
     93 configinfo config_list[] = {
     94 	{MSG_Configure_network, CONFIGOPT_NETCONF, NULL, set_network, MSG_configure},
     95 	{MSG_timezone, CONFIGOPT_TZ, NULL, set_timezone_menu, NULL},
     96 	{MSG_Root_shell, CONFIGOPT_ROOTSH, NULL, set_root_shell, NULL},
     97 	{MSG_change_rootpw, CONFIGOPT_ROOTPW, NULL, change_root_password, MSG_change},
     98 	{MSG_enable_binpkg, CONFIGOPT_BINPKG, NULL, set_binpkg, MSG_install},
     99 	{MSG_get_pkgsrc, CONFIGOPT_PKGSRC, NULL, set_pkgsrc, MSG_install},
    100 	{MSG_enable_sshd, CONFIGOPT_SSHD, "sshd", toggle_rcvar, NULL},
    101 	{MSG_enable_ntpd, CONFIGOPT_NTPD, "ntpd", toggle_rcvar, NULL},
    102 	{MSG_run_ntpdate, CONFIGOPT_NTPDATE, "ntpdate", toggle_rcvar, NULL},
    103 	{MSG_enable_mdnsd, CONFIGOPT_MDNSD, "mdnsd", toggle_rcvar, NULL},
    104 	{MSG_enable_xdm, CONFIGOPT_XDM, "xdm", toggle_rcvar, NULL},
    105 	{MSG_enable_cgd, CONFIGOPT_CGD, "cgd", toggle_rcvar, NULL},
    106 	{MSG_enable_lvm, CONFIGOPT_LVM, "lvm", toggle_rcvar, NULL},
    107 	{MSG_enable_raid, CONFIGOPT_RAIDFRAME, "raidframe", toggle_rcvar, NULL},
    108 	{MSG_add_a_user, CONFIGOPT_ADDUSER, NULL, add_new_user, ""},
    109 #if CHECK_ENTROPY
    110 	{MSG_Configure_entropy, CONFIGOPT_ADD_ENTROPY, NULL, add_entropy, ""},
    111 #endif
    112 	{NULL,		CONFIGOPT_LAST,	NULL, NULL, NULL}
    113 };
    114 
    115 static void
    116 config_list_init(void)
    117 {
    118 	int i;
    119 
    120 	for (i=0; i < CONFIGOPT_LAST; i++) {
    121 		switch (i) {
    122 		case CONFIGOPT_TZ:
    123 			get_tz_default();
    124 			config_list[CONFIGOPT_TZ].setting = tz_default;
    125 			break;
    126 		case CONFIGOPT_ROOTSH:
    127 			get_rootsh();
    128 			break;
    129 		case CONFIGOPT_ROOTPW:
    130 			if (check_root_password())
    131 				config_list[i].setting = MSG_password_set;
    132 			else
    133 				config_list[i].setting = MSG_empty;
    134 			break;
    135 		default:
    136 			if (config_list[i].rcvar != NULL) {
    137 				if (check_rcvar(config_list[i].rcvar))
    138 					config_list[i].setting = MSG_YES;
    139 				else
    140 					config_list[i].setting = MSG_NO;
    141 			}
    142 			break;
    143 		}
    144 	}
    145 }
    146 
    147 static void
    148 get_rootsh(void)
    149 {
    150 	static char *buf = NULL;
    151 
    152 	if (buf != NULL)
    153 		free(buf);
    154 
    155 	if (target_already_root())
    156 		collect(T_OUTPUT, &buf,
    157 		    "/usr/bin/awk -F: '$1==\"root\" { print $NF; exit }'"
    158 		    " /etc/passwd");
    159 	else
    160 		collect(T_OUTPUT, &buf,
    161 		    "chroot %s /usr/bin/awk -F: '$1==\"root\" { print $NF; exit }'"
    162 		    " /etc/passwd",target_prefix());
    163 
    164 	config_list[CONFIGOPT_ROOTSH].setting = (const char *)buf;
    165 }
    166 
    167 static void
    168 set_config(menudesc *menu, int opt, void *arg)
    169 {
    170 	configinfo	**configp = arg;
    171 	configinfo	*config = configp[opt];
    172 	const char	*optname, *setting;
    173 
    174 	optname = config->optname;
    175 	setting = msg_string(config->setting);
    176 
    177 	wprintw(menu->mw, "%-50s %-10s", msg_string(optname), setting);
    178 }
    179 
    180 static int
    181 init_config_menu(configinfo *conf, menu_ent *me, configinfo **ce)
    182 {
    183 	int	opt;
    184 	int	configopts;
    185 
    186 	for (configopts = 0; ; conf++) {
    187 		opt = conf->opt;
    188 		if (opt == CONFIGOPT_LAST)
    189 			break;
    190 #if CHECK_ENTROPY
    191 		if (opt == CONFIGOPT_ADD_ENTROPY && entropy_needed() == 0)
    192 			continue;
    193 #endif
    194 		*ce = conf;
    195 		memset(me, 0, sizeof(*me));
    196 		me->opt_action = conf->action;
    197 		configopts++;
    198 		ce++;
    199 		me++;
    200 	}
    201 
    202 	return configopts;
    203 }
    204 
    205 static int
    206 /*ARGSUSED*/
    207 set_timezone_menu(struct menudesc *menu, void *arg)
    208 {
    209 	configinfo **confp = arg;
    210 	set_timezone();
    211 	get_tz_default();
    212 	confp[menu->cursel]->setting = tz_default;
    213 	return 0;
    214 }
    215 
    216 static int
    217 set_root_shell(struct menudesc *menu, void *arg)
    218 {
    219 	configinfo **confp = arg;
    220 
    221 	process_menu(MENU_rootsh, &confp[menu->cursel]->setting);
    222 	if (run_program(RUN_PROGRESS | RUN_CHROOT,
    223 		"chpass -s %s root", confp[menu->cursel]->setting) != 0)
    224 		confp[menu->cursel]->setting = MSG_failed;
    225 	return 0;
    226 }
    227 
    228 static int
    229 set_network(struct menudesc *menu, void *arg)
    230 {
    231 	network_up = 0;
    232 	if (config_network())
    233 		mnt_net_config();
    234 	return 0;
    235 }
    236 
    237 static int
    238 check_root_password(void)
    239 {
    240 	char *buf;
    241 	int rval;
    242 
    243 	if (target_already_root())
    244 		collect(T_OUTPUT, &buf, "getent passwd root | cut -d: -f2");
    245 	else
    246 		collect(T_OUTPUT, &buf, "chroot %s getent passwd root | "
    247 		    "chroot %s cut -d: -f2",
    248 		    target_prefix(), target_prefix());
    249 
    250 	if (logfp)
    251 		fprintf(logfp,"buf %s strlen(buf) %zu\n", buf, strlen(buf));
    252 
    253 	if (strlen(buf) <= 1)  /* newline */
    254 		rval = 0;
    255 	else
    256 		rval = 1;
    257 	free(buf);
    258 	return rval;
    259 }
    260 
    261 #if CHECK_ENTROPY
    262 static int
    263 add_entropy(struct menudesc *menu, void *arg)
    264 {
    265 	do_add_entropy();
    266 	return 0;
    267 }
    268 #endif
    269 
    270 static int
    271 add_new_user(struct menudesc *menu, void *arg)
    272 {
    273 	char username[STRSIZE] = "";
    274 	int inwheel=0;
    275 
    276 	msg_prompt(MSG_addusername, NULL, username, sizeof username -1);
    277 	if (strlen(username) == 0)
    278 		return 0;
    279 	inwheel = ask_yesno(MSG_addusertowheel);
    280 	ushell = "/bin/csh";
    281 	process_menu(MENU_usersh, NULL);
    282 	if (inwheel)
    283 		run_program(RUN_PROGRESS | RUN_CHROOT,
    284 		    "/usr/sbin/useradd -m -s %s -G wheel %s",
    285 		    ushell, username);
    286 	else
    287 		run_program(RUN_PROGRESS | RUN_CHROOT,
    288 		    "/usr/sbin/useradd -m -s %s %s", ushell, username);
    289 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
    290 	    "passwd -l %s", username);
    291 	return 0;
    292 }
    293 
    294 void
    295 root_pw_setup(void)
    296 {
    297 	msg_display(MSG_force_rootpw);
    298 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT | RUN_STDSCR,
    299 	    "passwd -l root");
    300 }
    301 
    302 static int
    303 change_root_password(struct menudesc *menu, void *arg)
    304 {
    305 	configinfo **confp = arg;
    306 
    307 	msg_display(MSG_rootpw);
    308 	if (ask_yesno(NULL)) {
    309 		if (run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
    310 			"passwd -l root") == 0)
    311 			confp[menu->cursel]->setting = MSG_password_set;
    312 		else
    313 			confp[menu->cursel]->setting = MSG_failed;
    314 	}
    315 	return 0;
    316 }
    317 
    318 static int
    319 set_binpkg(struct menudesc *menu, void *arg)
    320 {
    321 	configinfo **confp = arg;
    322 	char additional_pkgs[STRSIZE] = {0};
    323 	int allok = 0;
    324 	arg_rv parm;
    325 
    326 	do {
    327 		parm.rv = -1;
    328 		parm.arg = additional_pkgs;
    329 		process_menu(MENU_binpkg, &parm);
    330 		if (parm.rv == SET_SKIP) {
    331 			confp[menu->cursel]->setting = MSG_abandoned;
    332 			return 0;
    333 		}
    334 
    335 		make_url(pkgpath, &pkg, pkg_dir);
    336 		if (run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
    337 			"pkg_add %s/pkgin", pkgpath) == 0) {
    338 			allok = 1;
    339 		}
    340 	} while (allok == 0);
    341 
    342 	/* configure pkgin to use $pkgpath as a repository */
    343 	replace("/usr/pkg/etc/pkgin/repositories.conf", "s,^[^#].*$,%s,",
    344 	    pkgpath);
    345 
    346 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
    347 		"/usr/pkg/bin/pkgin -y update");
    348 
    349 	if (strlen(additional_pkgs) > 0)
    350 		run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
    351 		"/usr/pkg/bin/pkgin -y install %s", additional_pkgs);
    352 
    353 	hit_enter_to_continue(MSG_binpkg_installed, NULL);
    354 
    355 	confp[menu->cursel]->setting = MSG_DONE;
    356 	return 0;
    357 }
    358 
    359 static int
    360 set_pkgsrc(struct menudesc *menu, void *arg)
    361 {
    362 	configinfo **confp = arg;
    363 	distinfo dist;
    364 
    365 	dist.name = "pkgsrc";
    366 	dist.set = SET_PKGSRC;
    367 	dist.desc = "source for 3rd-party packages";
    368 	dist.marker_file = NULL;
    369 
    370 	int status = SET_RETRY;
    371 
    372 	do {
    373 		status = get_pkgsrc();
    374 		if (status == SET_OK) {
    375 			status = extract_file(&dist, 0);
    376 			continue;
    377 		} else if (status == SET_SKIP) {
    378 			confp[menu->cursel]->setting = MSG_abandoned;
    379 			return 0;
    380 		}
    381 		if (!ask_yesno(MSG_retry_pkgsrc_network)) {
    382 			confp[menu->cursel]->setting = MSG_abandoned;
    383 			return 1;
    384 		}
    385 	}
    386 	while (status == SET_RETRY);
    387 
    388 	confp[menu->cursel]->setting = MSG_DONE;
    389 	return 0;
    390 }
    391 
    392 static int
    393 toggle_rcvar(struct menudesc *menu, void *arg)
    394 {
    395 	configinfo **confp = arg;
    396 	int s;
    397 	const char *setting, *varname;
    398 	char pattern[STRSIZE];
    399 	char buf[STRSIZE];
    400 	char *cp;
    401 	int found = 0;
    402 	FILE *fp;
    403 
    404 	varname = confp[menu->cursel]->rcvar;
    405 
    406 	s = check_rcvar(varname);
    407 
    408 	/* we're toggling, so invert the sense */
    409 	if (s) {
    410 		confp[menu->cursel]->setting = MSG_NO;
    411 		setting = "NO";
    412 	} else {
    413 		confp[menu->cursel]->setting = MSG_YES;
    414 		setting = "YES";
    415 	}
    416 
    417 	if (!(fp = fopen(target_expand("/etc/rc.conf"), "r"))) {
    418 		msg_fmt_display(MSG_openfail, "%s%s",
    419 		    target_expand("/etc/rc.conf"), strerror(errno));
    420 		hit_enter_to_continue(NULL, NULL);
    421 		return 0;
    422 	}
    423 
    424 	while (fgets(buf, sizeof buf, fp) != NULL) {
    425 		cp = buf + strspn(buf, " \t"); /* Skip initial spaces */
    426 		if (strncmp(cp, varname, strlen(varname)) == 0) {
    427 			cp += strlen(varname);
    428 			if (*cp != '=')
    429 				continue;
    430 			buf[strlen(buf) - 1] = 0;
    431 			snprintf(pattern, sizeof pattern,
    432 					"s,^%s$,%s=%s,",
    433 					buf, varname, setting);
    434 			found = 1;
    435 			break;
    436 		}
    437 	}
    438 
    439 	fclose(fp);
    440 
    441 	if (!found) {
    442 		add_rc_conf("%s=%s\n", varname, setting);
    443 		if (logfp) {
    444 			fprintf(logfp, "adding %s=%s\n", varname, setting);
    445 			fflush(logfp);
    446 		}
    447 	} else {
    448 		if (logfp) {
    449 			fprintf(logfp, "replacement pattern is %s\n", pattern);
    450 			fflush(logfp);
    451 		}
    452 		replace("/etc/rc.conf", "%s", pattern);
    453 	}
    454 
    455 	return 0;
    456 }
    457 
    458 static void
    459 configmenu_hdr(struct menudesc *menu, void *arg)
    460 {
    461 	msg_display(MSG_configmenu);
    462 }
    463 
    464 void
    465 do_configmenu(struct install_partition_desc *install)
    466 {
    467 	int		menu_no;
    468 	int		opts;
    469 	menu_ent	me[CONFIGOPT_LAST];
    470 	configinfo	*ce[CONFIGOPT_LAST];
    471 
    472 	memset(me, 0, sizeof(me));
    473 
    474 	/* if the target isn't mounted already, figure it out. */
    475 	if (install != NULL && target_mounted() == 0) {
    476 		partman_go = 0;
    477 		if (find_disks(msg_string(MSG_configure_prior), true) < 0)
    478 			return;
    479 
    480 		if (mount_disks(install) != 0)
    481 			return;
    482 	}
    483 
    484 	config_list_init();
    485 	make_url(pkgpath, &pkg, pkg_dir);
    486 	opts = init_config_menu(config_list, me, ce);
    487 
    488 	wrefresh(curscr);
    489 	wmove(stdscr, 0, 0);
    490 	wclear(stdscr);
    491 	wrefresh(stdscr);
    492 
    493 	menu_no = new_menu(NULL, me, opts, 0, -4, 0, 70,
    494 		MC_SCROLL | MC_NOBOX | MC_DFLTEXIT,
    495 		configmenu_hdr, set_config, NULL, NULL,
    496 		MSG_doneconfig);
    497 
    498 	process_menu(menu_no, ce);
    499 	free_menu(menu_no);
    500 }
    501