Home | History | Annotate | Line # | Download | only in sysinst
configmenu.c revision 1.13
      1 /* $NetBSD: configmenu.c,v 1.13 2021/10/08 15:59:55 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 static int add_entropy(struct menudesc *, void *);
     49 static int set_binpkg(struct menudesc *, void *);
     50 static int set_pkgsrc(struct menudesc *, void *);
     51 static void config_list_init(void);
     52 static void get_rootsh(void);
     53 static int toggle_rcvar(struct menudesc *, void *);
     54 static void configmenu_hdr(struct menudesc *, void *);
     55 static int check_root_password(void);
     56 
     57 char pkgpath[STRSIZE];
     58 char pkgsrcpath[STRSIZE];
     59 
     60 extern const char *tz_default;
     61 
     62 enum {
     63 	CONFIGOPT_NETCONF,
     64 	CONFIGOPT_TZ,
     65 	CONFIGOPT_ROOTSH,
     66 	CONFIGOPT_ROOTPW,
     67 	CONFIGOPT_BINPKG,
     68 	CONFIGOPT_PKGSRC,
     69 	CONFIGOPT_SSHD,
     70 	CONFIGOPT_NTPD,
     71 	CONFIGOPT_NTPDATE,
     72 	CONFIGOPT_MDNSD,
     73 	CONFIGOPT_XDM,
     74 	CONFIGOPT_CGD,
     75 	CONFIGOPT_LVM,
     76 	CONFIGOPT_RAIDFRAME,
     77 	CONFIGOPT_ADDUSER,
     78 	CONFIGOPT_ADD_ENTROPY,
     79 	CONFIGOPT_LAST
     80 };
     81 
     82 typedef struct configinfo {
     83 	const char	*optname;
     84 	uint		opt;
     85 	const char	*rcvar;
     86 	int		(*action)(struct menudesc *, void *);
     87 	const char	*setting;
     88 } configinfo;
     89 
     90 
     91 configinfo config_list[] = {
     92 	{MSG_Configure_network, CONFIGOPT_NETCONF, NULL, set_network, MSG_configure},
     93 	{MSG_timezone, CONFIGOPT_TZ, NULL, set_timezone_menu, NULL},
     94 	{MSG_Root_shell, CONFIGOPT_ROOTSH, NULL, set_root_shell, NULL},
     95 	{MSG_change_rootpw, CONFIGOPT_ROOTPW, NULL, change_root_password, MSG_change},
     96 	{MSG_enable_binpkg, CONFIGOPT_BINPKG, NULL, set_binpkg, MSG_install},
     97 	{MSG_get_pkgsrc, CONFIGOPT_PKGSRC, NULL, set_pkgsrc, MSG_install},
     98 	{MSG_enable_sshd, CONFIGOPT_SSHD, "sshd", toggle_rcvar, NULL},
     99 	{MSG_enable_ntpd, CONFIGOPT_NTPD, "ntpd", toggle_rcvar, NULL},
    100 	{MSG_run_ntpdate, CONFIGOPT_NTPDATE, "ntpdate", toggle_rcvar, NULL},
    101 	{MSG_enable_mdnsd, CONFIGOPT_MDNSD, "mdnsd", toggle_rcvar, NULL},
    102 	{MSG_enable_xdm, CONFIGOPT_XDM, "xdm", toggle_rcvar, NULL},
    103 	{MSG_enable_cgd, CONFIGOPT_CGD, "cgd", toggle_rcvar, NULL},
    104 	{MSG_enable_lvm, CONFIGOPT_LVM, "lvm", toggle_rcvar, NULL},
    105 	{MSG_enable_raid, CONFIGOPT_RAIDFRAME, "raidframe", toggle_rcvar, NULL},
    106 	{MSG_add_a_user, CONFIGOPT_ADDUSER, NULL, add_new_user, ""},
    107 #if CHECK_ENTROPY
    108 	{MSG_Configure_entropy, CONFIGOPT_ADD_ENTROPY, NULL, add_entropy, ""},
    109 #endif
    110 	{NULL,		CONFIGOPT_LAST,	NULL, NULL, NULL}
    111 };
    112 
    113 static void
    114 config_list_init(void)
    115 {
    116 	int i;
    117 
    118 	for (i=0; i < CONFIGOPT_LAST; i++) {
    119 		switch (i) {
    120 		case CONFIGOPT_TZ:
    121 			get_tz_default();
    122 			config_list[CONFIGOPT_TZ].setting = tz_default;
    123 			break;
    124 		case CONFIGOPT_ROOTSH:
    125 			get_rootsh();
    126 			break;
    127 		case CONFIGOPT_ROOTPW:
    128 			if (check_root_password())
    129 				config_list[i].setting = MSG_password_set;
    130 			else
    131 				config_list[i].setting = MSG_empty;
    132 			break;
    133 		default:
    134 			if (config_list[i].rcvar != NULL) {
    135 				if (check_rcvar(config_list[i].rcvar))
    136 					config_list[i].setting = MSG_YES;
    137 				else
    138 					config_list[i].setting = MSG_NO;
    139 			}
    140 			break;
    141 		}
    142 	}
    143 }
    144 
    145 static void
    146 get_rootsh(void)
    147 {
    148 	static char *buf = NULL;
    149 
    150 	if (buf != NULL)
    151 		free(buf);
    152 
    153 	if (target_already_root())
    154 		collect(T_OUTPUT, &buf,
    155 		    "/usr/bin/awk -F: '$1==\"root\" { print $NF; exit }'"
    156 		    " /etc/passwd");
    157 	else
    158 		collect(T_OUTPUT, &buf,
    159 		    "chroot %s /usr/bin/awk -F: '$1==\"root\" { print $NF; exit }'"
    160 		    " /etc/passwd",target_prefix());
    161 
    162 	config_list[CONFIGOPT_ROOTSH].setting = (const char *)buf;
    163 }
    164 
    165 static void
    166 set_config(menudesc *menu, int opt, void *arg)
    167 {
    168 	configinfo	**configp = arg;
    169 	configinfo	*config = configp[opt];
    170 	const char	*optname, *setting;
    171 
    172 	optname = config->optname;
    173 	setting = msg_string(config->setting);
    174 
    175 	wprintw(menu->mw, "%-50s %-10s", msg_string(optname), setting);
    176 }
    177 
    178 static int
    179 init_config_menu(configinfo *conf, menu_ent *me, configinfo **ce)
    180 {
    181 	int	opt;
    182 	int	configopts;
    183 
    184 	for (configopts = 0; ; conf++) {
    185 		opt = conf->opt;
    186 		if (opt == CONFIGOPT_LAST)
    187 			break;
    188 #if CHECK_ENTROPY
    189 		if (opt == CONFIGOPT_ADD_ENTROPY && entropy_needed() == 0)
    190 			continue;
    191 #endif
    192 		*ce = conf;
    193 		memset(me, 0, sizeof(*me));
    194 		me->opt_action = conf->action;
    195 		configopts++;
    196 		ce++;
    197 		me++;
    198 	}
    199 
    200 	return configopts;
    201 }
    202 
    203 static int
    204 /*ARGSUSED*/
    205 set_timezone_menu(struct menudesc *menu, void *arg)
    206 {
    207 	configinfo **confp = arg;
    208 	set_timezone();
    209 	get_tz_default();
    210 	confp[menu->cursel]->setting = tz_default;
    211 	return 0;
    212 }
    213 
    214 static int
    215 set_root_shell(struct menudesc *menu, void *arg)
    216 {
    217 	configinfo **confp = arg;
    218 
    219 	process_menu(MENU_rootsh, &confp[menu->cursel]->setting);
    220 	if (run_program(RUN_PROGRESS | RUN_CHROOT,
    221 		"chpass -s %s root", confp[menu->cursel]->setting) != 0)
    222 		confp[menu->cursel]->setting = MSG_failed;
    223 	return 0;
    224 }
    225 
    226 static int
    227 set_network(struct menudesc *menu, void *arg)
    228 {
    229 	network_up = 0;
    230 	if (config_network())
    231 		mnt_net_config();
    232 	return 0;
    233 }
    234 
    235 static int
    236 check_root_password(void)
    237 {
    238 	char *buf;
    239 	int rval;
    240 
    241 	if (target_already_root())
    242 		collect(T_OUTPUT, &buf, "getent passwd root | cut -d: -f2");
    243 	else
    244 		collect(T_OUTPUT, &buf, "chroot %s getent passwd root | "
    245 		    "chroot %s cut -d: -f2",
    246 		    target_prefix(), target_prefix());
    247 
    248 	if (logfp)
    249 		fprintf(logfp,"buf %s strlen(buf) %zu\n", buf, strlen(buf));
    250 
    251 	if (strlen(buf) <= 1)  /* newline */
    252 		rval = 0;
    253 	else
    254 		rval = 1;
    255 	free(buf);
    256 	return rval;
    257 }
    258 
    259 #if CHECK_ENTROPY
    260 static int
    261 add_entropy(struct menudesc *menu, void *arg)
    262 {
    263 	do_add_entropy();
    264 	return 0;
    265 }
    266 #endif
    267 
    268 static int
    269 add_new_user(struct menudesc *menu, void *arg)
    270 {
    271 	char username[STRSIZE] = "";
    272 	int inwheel=0;
    273 
    274 	msg_prompt(MSG_addusername, NULL, username, sizeof username -1);
    275 	if (strlen(username) == 0)
    276 		return 0;
    277 	inwheel = ask_yesno(MSG_addusertowheel);
    278 	ushell = "/bin/csh";
    279 	process_menu(MENU_usersh, NULL);
    280 	if (inwheel)
    281 		run_program(RUN_PROGRESS | RUN_CHROOT,
    282 		    "/usr/sbin/useradd -m -s %s -G wheel %s",
    283 		    ushell, username);
    284 	else
    285 		run_program(RUN_PROGRESS | RUN_CHROOT,
    286 		    "/usr/sbin/useradd -m -s %s %s", ushell, username);
    287 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
    288 	    "passwd -l %s", username);
    289 	return 0;
    290 }
    291 
    292 static int
    293 change_root_password(struct menudesc *menu, void *arg)
    294 {
    295 	configinfo **confp = arg;
    296 
    297 	msg_display(MSG_rootpw);
    298 	if (ask_yesno(NULL)) {
    299 		if (run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
    300 			"passwd -l root") == 0)
    301 			confp[menu->cursel]->setting = MSG_password_set;
    302 		else
    303 			confp[menu->cursel]->setting = MSG_failed;
    304 	}
    305 	return 0;
    306 }
    307 
    308 static int
    309 set_binpkg(struct menudesc *menu, void *arg)
    310 {
    311 	configinfo **confp = arg;
    312 	char additional_pkgs[STRSIZE] = {0};
    313 	int allok = 0;
    314 	arg_rv parm;
    315 
    316 	do {
    317 		parm.rv = -1;
    318 		parm.arg = additional_pkgs;
    319 		process_menu(MENU_binpkg, &parm);
    320 		if (parm.rv == SET_SKIP) {
    321 			confp[menu->cursel]->setting = MSG_abandoned;
    322 			return 0;
    323 		}
    324 
    325 		make_url(pkgpath, &pkg, pkg_dir);
    326 		if (run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
    327 			"pkg_add %s/pkgin", pkgpath) == 0) {
    328 			allok = 1;
    329 		}
    330 	} while (allok == 0);
    331 
    332 	/* configure pkgin to use $pkgpath as a repository */
    333 	replace("/usr/pkg/etc/pkgin/repositories.conf", "s,^[^#].*$,%s,",
    334 	    pkgpath);
    335 
    336 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
    337 		"/usr/pkg/bin/pkgin -y update");
    338 
    339 	if (strlen(additional_pkgs) > 0)
    340 		run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
    341 		"/usr/pkg/bin/pkgin -y install %s", additional_pkgs);
    342 
    343 	hit_enter_to_continue(MSG_binpkg_installed, NULL);
    344 
    345 	confp[menu->cursel]->setting = MSG_DONE;
    346 	return 0;
    347 }
    348 
    349 static int
    350 set_pkgsrc(struct menudesc *menu, void *arg)
    351 {
    352 	configinfo **confp = arg;
    353 	distinfo dist;
    354 
    355 	dist.name = "pkgsrc";
    356 	dist.set = SET_PKGSRC;
    357 	dist.desc = "source for 3rd-party packages";
    358 	dist.marker_file = NULL;
    359 
    360 	int status = SET_RETRY;
    361 
    362 	do {
    363 		status = get_pkgsrc();
    364 		if (status == SET_OK) {
    365 			status = extract_file(&dist, 0);
    366 			continue;
    367 		} else if (status == SET_SKIP) {
    368 			confp[menu->cursel]->setting = MSG_abandoned;
    369 			return 0;
    370 		}
    371 		if (!ask_yesno(MSG_retry_pkgsrc_network)) {
    372 			confp[menu->cursel]->setting = MSG_abandoned;
    373 			return 1;
    374 		}
    375 	}
    376 	while (status == SET_RETRY);
    377 
    378 	confp[menu->cursel]->setting = MSG_DONE;
    379 	return 0;
    380 }
    381 
    382 static int
    383 toggle_rcvar(struct menudesc *menu, void *arg)
    384 {
    385 	configinfo **confp = arg;
    386 	int s;
    387 	const char *setting, *varname;
    388 	char pattern[STRSIZE];
    389 	char buf[STRSIZE];
    390 	char *cp;
    391 	int found = 0;
    392 	FILE *fp;
    393 
    394 	varname = confp[menu->cursel]->rcvar;
    395 
    396 	s = check_rcvar(varname);
    397 
    398 	/* we're toggling, so invert the sense */
    399 	if (s) {
    400 		confp[menu->cursel]->setting = MSG_NO;
    401 		setting = "NO";
    402 	} else {
    403 		confp[menu->cursel]->setting = MSG_YES;
    404 		setting = "YES";
    405 	}
    406 
    407 	if (!(fp = fopen(target_expand("/etc/rc.conf"), "r"))) {
    408 		msg_fmt_display(MSG_openfail, "%s%s",
    409 		    target_expand("/etc/rc.conf"), strerror(errno));
    410 		hit_enter_to_continue(NULL, NULL);
    411 		return 0;
    412 	}
    413 
    414 	while (fgets(buf, sizeof buf, fp) != NULL) {
    415 		cp = buf + strspn(buf, " \t"); /* Skip initial spaces */
    416 		if (strncmp(cp, varname, strlen(varname)) == 0) {
    417 			cp += strlen(varname);
    418 			if (*cp != '=')
    419 				continue;
    420 			buf[strlen(buf) - 1] = 0;
    421 			snprintf(pattern, sizeof pattern,
    422 					"s,^%s$,%s=%s,",
    423 					buf, varname, setting);
    424 			found = 1;
    425 			break;
    426 		}
    427 	}
    428 
    429 	fclose(fp);
    430 
    431 	if (!found) {
    432 		add_rc_conf("%s=%s\n", varname, setting);
    433 		if (logfp) {
    434 			fprintf(logfp, "adding %s=%s\n", varname, setting);
    435 			fflush(logfp);
    436 		}
    437 	} else {
    438 		if (logfp) {
    439 			fprintf(logfp, "replacement pattern is %s\n", pattern);
    440 			fflush(logfp);
    441 		}
    442 		replace("/etc/rc.conf", "%s", pattern);
    443 	}
    444 
    445 	return 0;
    446 }
    447 
    448 static void
    449 configmenu_hdr(struct menudesc *menu, void *arg)
    450 {
    451 	msg_display(MSG_configmenu);
    452 }
    453 
    454 void
    455 do_configmenu(struct install_partition_desc *install)
    456 {
    457 	int		menu_no;
    458 	int		opts;
    459 	menu_ent	me[CONFIGOPT_LAST];
    460 	configinfo	*ce[CONFIGOPT_LAST];
    461 
    462 	memset(me, 0, sizeof(me));
    463 
    464 	/* if the target isn't mounted already, figure it out. */
    465 	if (install != NULL && target_mounted() == 0) {
    466 		partman_go = 0;
    467 		if (find_disks(msg_string(MSG_configure_prior), true) < 0)
    468 			return;
    469 
    470 		if (mount_disks(install) != 0)
    471 			return;
    472 	}
    473 
    474 	config_list_init();
    475 	make_url(pkgpath, &pkg, pkg_dir);
    476 	opts = init_config_menu(config_list, me, ce);
    477 
    478 	wrefresh(curscr);
    479 	wmove(stdscr, 0, 0);
    480 	wclear(stdscr);
    481 	wrefresh(stdscr);
    482 
    483 	menu_no = new_menu(NULL, me, opts, 0, -4, 0, 70,
    484 		MC_SCROLL | MC_NOBOX | MC_DFLTEXIT,
    485 		configmenu_hdr, set_config, NULL, NULL,
    486 		MSG_doneconfig);
    487 
    488 	process_menu(menu_no, ce);
    489 	free_menu(menu_no);
    490 }
    491