xref: /netbsd-src/usr.sbin/sysinst/configmenu.c (revision 96b81bf6d31a6cd02ad43fc5677d10eee621ac50)
1*96b81bf6Smartin /* $NetBSD: configmenu.c,v 1.19 2024/03/24 16:06:37 martin Exp $ */
250dbef1aSdholland 
350dbef1aSdholland /*-
450dbef1aSdholland  * Copyright (c) 2012 The NetBSD Foundation, Inc.
550dbef1aSdholland  * All rights reserved.
650dbef1aSdholland  *
750dbef1aSdholland  * This code is derived from software contributed to The NetBSD Foundation
850dbef1aSdholland  * by Jeffrey C. Rizzo
950dbef1aSdholland  *
1050dbef1aSdholland  * Redistribution and use in source and binary forms, with or without
1150dbef1aSdholland  * modification, are permitted provided that the following conditions
1250dbef1aSdholland  * are met:
1350dbef1aSdholland  * 1. Redistributions of source code must retain the above copyright
1450dbef1aSdholland  *    notice, this list of conditions and the following disclaimer.
1550dbef1aSdholland  * 2. Redistributions in binary form must reproduce the above copyright
1650dbef1aSdholland  *    notice, this list of conditions and the following disclaimer in the
1750dbef1aSdholland  *    documentation and/or other materials provided with the distribution.
1850dbef1aSdholland  *
1950dbef1aSdholland  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2050dbef1aSdholland  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2150dbef1aSdholland  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2250dbef1aSdholland  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2350dbef1aSdholland  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2450dbef1aSdholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2550dbef1aSdholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2650dbef1aSdholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2750dbef1aSdholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2850dbef1aSdholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2950dbef1aSdholland  * POSSIBILITY OF SUCH DAMAGE.
3050dbef1aSdholland  */
3150dbef1aSdholland 
3250dbef1aSdholland /* configmenu.c -- post-installation system configuration menu. */
3350dbef1aSdholland 
3450dbef1aSdholland #include <stdio.h>
3550dbef1aSdholland #include <curses.h>
3650dbef1aSdholland #include <unistd.h>
374b2364d9Smartin #include <errno.h>
3850dbef1aSdholland #include "defs.h"
3950dbef1aSdholland #include "msg_defs.h"
4050dbef1aSdholland #include "menu_defs.h"
4150dbef1aSdholland 
4250dbef1aSdholland 
4350dbef1aSdholland static int set_network(struct menudesc*, void *);
4450dbef1aSdholland static int set_timezone_menu(struct menudesc *, void *);
4550dbef1aSdholland static int set_root_shell(struct menudesc *, void *);
4650dbef1aSdholland static int change_root_password(struct menudesc *, void *);
4750dbef1aSdholland static int add_new_user(struct menudesc *, void *);
4840c10745Smartin #if CHECK_ENTROPY
4903af0822Smartin static int add_entropy(struct menudesc *, void *);
5040c10745Smartin #endif
5150dbef1aSdholland static int set_binpkg(struct menudesc *, void *);
5250dbef1aSdholland static int set_pkgsrc(struct menudesc *, void *);
5350dbef1aSdholland static void config_list_init(void);
5450dbef1aSdholland static void get_rootsh(void);
5550dbef1aSdholland static int toggle_rcvar(struct menudesc *, void *);
5654302b39Sjmcneill static int toggle_mdnsd(struct menudesc *, void *);
5750dbef1aSdholland static void configmenu_hdr(struct menudesc *, void *);
5850dbef1aSdholland static int check_root_password(void);
5950dbef1aSdholland 
6050dbef1aSdholland char pkgpath[STRSIZE];
6150dbef1aSdholland char pkgsrcpath[STRSIZE];
6250dbef1aSdholland 
6350dbef1aSdholland extern const char *tz_default;
6450dbef1aSdholland 
6550dbef1aSdholland enum {
6650dbef1aSdholland 	CONFIGOPT_NETCONF,
6750dbef1aSdholland 	CONFIGOPT_TZ,
6850dbef1aSdholland 	CONFIGOPT_ROOTSH,
6950dbef1aSdholland 	CONFIGOPT_ROOTPW,
7050dbef1aSdholland 	CONFIGOPT_BINPKG,
7150dbef1aSdholland 	CONFIGOPT_PKGSRC,
7250dbef1aSdholland 	CONFIGOPT_SSHD,
7350dbef1aSdholland 	CONFIGOPT_NTPD,
7450dbef1aSdholland 	CONFIGOPT_NTPDATE,
7550dbef1aSdholland 	CONFIGOPT_MDNSD,
764b2364d9Smartin 	CONFIGOPT_XDM,
774b2364d9Smartin 	CONFIGOPT_CGD,
784b2364d9Smartin 	CONFIGOPT_LVM,
794b2364d9Smartin 	CONFIGOPT_RAIDFRAME,
8050dbef1aSdholland 	CONFIGOPT_ADDUSER,
8103af0822Smartin 	CONFIGOPT_ADD_ENTROPY,
8250dbef1aSdholland 	CONFIGOPT_LAST
8350dbef1aSdholland };
8450dbef1aSdholland 
8550dbef1aSdholland typedef struct configinfo {
8650dbef1aSdholland 	const char	*optname;
8750dbef1aSdholland 	uint		opt;
8850dbef1aSdholland 	const char	*rcvar;
8950dbef1aSdholland 	int		(*action)(struct menudesc *, void *);
9050dbef1aSdholland 	const char	*setting;
9150dbef1aSdholland } configinfo;
9250dbef1aSdholland 
9350dbef1aSdholland 
9450dbef1aSdholland configinfo config_list[] = {
9550dbef1aSdholland 	{MSG_Configure_network, CONFIGOPT_NETCONF, NULL, set_network, MSG_configure},
9650dbef1aSdholland 	{MSG_timezone, CONFIGOPT_TZ, NULL, set_timezone_menu, NULL},
9750dbef1aSdholland 	{MSG_Root_shell, CONFIGOPT_ROOTSH, NULL, set_root_shell, NULL},
9850dbef1aSdholland 	{MSG_change_rootpw, CONFIGOPT_ROOTPW, NULL, change_root_password, MSG_change},
994b2364d9Smartin 	{MSG_enable_binpkg, CONFIGOPT_BINPKG, NULL, set_binpkg, MSG_install},
10050dbef1aSdholland 	{MSG_get_pkgsrc, CONFIGOPT_PKGSRC, NULL, set_pkgsrc, MSG_install},
10150dbef1aSdholland 	{MSG_enable_sshd, CONFIGOPT_SSHD, "sshd", toggle_rcvar, NULL},
10250dbef1aSdholland 	{MSG_enable_ntpd, CONFIGOPT_NTPD, "ntpd", toggle_rcvar, NULL},
10350dbef1aSdholland 	{MSG_run_ntpdate, CONFIGOPT_NTPDATE, "ntpdate", toggle_rcvar, NULL},
10454302b39Sjmcneill 	{MSG_enable_mdnsd, CONFIGOPT_MDNSD, "mdnsd", toggle_mdnsd, NULL},
1054b2364d9Smartin 	{MSG_enable_xdm, CONFIGOPT_XDM, "xdm", toggle_rcvar, NULL},
1064b2364d9Smartin 	{MSG_enable_cgd, CONFIGOPT_CGD, "cgd", toggle_rcvar, NULL},
1074b2364d9Smartin 	{MSG_enable_lvm, CONFIGOPT_LVM, "lvm", toggle_rcvar, NULL},
1084b2364d9Smartin 	{MSG_enable_raid, CONFIGOPT_RAIDFRAME, "raidframe", toggle_rcvar, NULL},
10950dbef1aSdholland 	{MSG_add_a_user, CONFIGOPT_ADDUSER, NULL, add_new_user, ""},
11003af0822Smartin #if CHECK_ENTROPY
11103af0822Smartin 	{MSG_Configure_entropy, CONFIGOPT_ADD_ENTROPY, NULL, add_entropy, ""},
11203af0822Smartin #endif
11350dbef1aSdholland 	{NULL,		CONFIGOPT_LAST,	NULL, NULL, NULL}
11450dbef1aSdholland };
11550dbef1aSdholland 
11650dbef1aSdholland static void
config_list_init(void)11750dbef1aSdholland config_list_init(void)
11850dbef1aSdholland {
11950dbef1aSdholland 	int i;
12050dbef1aSdholland 
12150dbef1aSdholland 	for (i=0; i < CONFIGOPT_LAST; i++) {
12250dbef1aSdholland 		switch (i) {
12350dbef1aSdholland 		case CONFIGOPT_TZ:
12450dbef1aSdholland 			get_tz_default();
12550dbef1aSdholland 			config_list[CONFIGOPT_TZ].setting = tz_default;
12650dbef1aSdholland 			break;
12750dbef1aSdholland 		case CONFIGOPT_ROOTSH:
12850dbef1aSdholland 			get_rootsh();
12950dbef1aSdholland 			break;
13050dbef1aSdholland 		case CONFIGOPT_ROOTPW:
13150dbef1aSdholland 			if (check_root_password())
13250dbef1aSdholland 				config_list[i].setting = MSG_password_set;
13350dbef1aSdholland 			else
13450dbef1aSdholland 				config_list[i].setting = MSG_empty;
13550dbef1aSdholland 			break;
13650dbef1aSdholland 		default:
13750dbef1aSdholland 			if (config_list[i].rcvar != NULL) {
13850dbef1aSdholland 				if (check_rcvar(config_list[i].rcvar))
13950dbef1aSdholland 					config_list[i].setting = MSG_YES;
14050dbef1aSdholland 				else
14150dbef1aSdholland 					config_list[i].setting = MSG_NO;
14250dbef1aSdholland 			}
14350dbef1aSdholland 			break;
14450dbef1aSdholland 		}
14550dbef1aSdholland 	}
14650dbef1aSdholland }
14750dbef1aSdholland 
14850dbef1aSdholland static void
get_rootsh(void)14950dbef1aSdholland get_rootsh(void)
15050dbef1aSdholland {
15150dbef1aSdholland 	static char *buf = NULL;
15250dbef1aSdholland 
15350dbef1aSdholland 	if (buf != NULL)
15450dbef1aSdholland 		free(buf);
15550dbef1aSdholland 
15650dbef1aSdholland 	if (target_already_root())
15750dbef1aSdholland 		collect(T_OUTPUT, &buf,
15850dbef1aSdholland 		    "/usr/bin/awk -F: '$1==\"root\" { print $NF; exit }'"
15950dbef1aSdholland 		    " /etc/passwd");
16050dbef1aSdholland 	else
16150dbef1aSdholland 		collect(T_OUTPUT, &buf,
16250dbef1aSdholland 		    "chroot %s /usr/bin/awk -F: '$1==\"root\" { print $NF; exit }'"
16350dbef1aSdholland 		    " /etc/passwd",target_prefix());
16450dbef1aSdholland 
16550dbef1aSdholland 	config_list[CONFIGOPT_ROOTSH].setting = (const char *)buf;
16650dbef1aSdholland }
16750dbef1aSdholland 
16850dbef1aSdholland static void
set_config(menudesc * menu,int opt,void * arg)16950dbef1aSdholland set_config(menudesc *menu, int opt, void *arg)
17050dbef1aSdholland {
17150dbef1aSdholland 	configinfo	**configp = arg;
17250dbef1aSdholland 	configinfo	*config = configp[opt];
17350dbef1aSdholland 	const char	*optname, *setting;
17450dbef1aSdholland 
17550dbef1aSdholland 	optname = config->optname;
17650dbef1aSdholland 	setting = msg_string(config->setting);
17750dbef1aSdholland 
17850dbef1aSdholland 	wprintw(menu->mw, "%-50s %-10s", msg_string(optname), setting);
17950dbef1aSdholland }
18050dbef1aSdholland 
18150dbef1aSdholland static int
init_config_menu(configinfo * conf,menu_ent * me,configinfo ** ce)18250dbef1aSdholland init_config_menu(configinfo *conf, menu_ent *me, configinfo **ce)
18350dbef1aSdholland {
18450dbef1aSdholland 	int	opt;
18550dbef1aSdholland 	int	configopts;
18650dbef1aSdholland 
18750dbef1aSdholland 	for (configopts = 0; ; conf++) {
18850dbef1aSdholland 		opt = conf->opt;
18950dbef1aSdholland 		if (opt == CONFIGOPT_LAST)
19050dbef1aSdholland 			break;
19103af0822Smartin #if CHECK_ENTROPY
19203af0822Smartin 		if (opt == CONFIGOPT_ADD_ENTROPY && entropy_needed() == 0)
19303af0822Smartin 			continue;
19403af0822Smartin #endif
19550dbef1aSdholland 		*ce = conf;
196ce713491Schristos 		memset(me, 0, sizeof(*me));
19750dbef1aSdholland 		me->opt_action = conf->action;
19850dbef1aSdholland 		configopts++;
19950dbef1aSdholland 		ce++;
20050dbef1aSdholland 		me++;
20150dbef1aSdholland 	}
20250dbef1aSdholland 
20350dbef1aSdholland 	return configopts;
20450dbef1aSdholland }
20550dbef1aSdholland 
20650dbef1aSdholland static int
20750dbef1aSdholland /*ARGSUSED*/
set_timezone_menu(struct menudesc * menu,void * arg)20850dbef1aSdholland set_timezone_menu(struct menudesc *menu, void *arg)
20950dbef1aSdholland {
21050dbef1aSdholland 	configinfo **confp = arg;
21150dbef1aSdholland 	set_timezone();
21250dbef1aSdholland 	get_tz_default();
21350dbef1aSdholland 	confp[menu->cursel]->setting = tz_default;
21450dbef1aSdholland 	return 0;
21550dbef1aSdholland }
21650dbef1aSdholland 
21750dbef1aSdholland static int
set_root_shell(struct menudesc * menu,void * arg)21850dbef1aSdholland set_root_shell(struct menudesc *menu, void *arg)
21950dbef1aSdholland {
22050dbef1aSdholland 	configinfo **confp = arg;
22150dbef1aSdholland 
22250dbef1aSdholland 	process_menu(MENU_rootsh, &confp[menu->cursel]->setting);
2234b2364d9Smartin 	if (run_program(RUN_PROGRESS | RUN_CHROOT,
2244b2364d9Smartin 		"chpass -s %s root", confp[menu->cursel]->setting) != 0)
2254b2364d9Smartin 		confp[menu->cursel]->setting = MSG_failed;
22650dbef1aSdholland 	return 0;
22750dbef1aSdholland }
22850dbef1aSdholland 
22950dbef1aSdholland static int
set_network(struct menudesc * menu,void * arg)23050dbef1aSdholland set_network(struct menudesc *menu, void *arg)
23150dbef1aSdholland {
23250dbef1aSdholland 	network_up = 0;
233a4e88c60Smartin 	if (config_network(1))
23450dbef1aSdholland 		mnt_net_config();
23550dbef1aSdholland 	return 0;
23650dbef1aSdholland }
23750dbef1aSdholland 
23850dbef1aSdholland static int
check_root_password(void)23950dbef1aSdholland check_root_password(void)
24050dbef1aSdholland {
24150dbef1aSdholland 	char *buf;
24250dbef1aSdholland 	int rval;
24350dbef1aSdholland 
24450dbef1aSdholland 	if (target_already_root())
24550dbef1aSdholland 		collect(T_OUTPUT, &buf, "getent passwd root | cut -d: -f2");
24650dbef1aSdholland 	else
24750dbef1aSdholland 		collect(T_OUTPUT, &buf, "chroot %s getent passwd root | "
24850dbef1aSdholland 		    "chroot %s cut -d: -f2",
24950dbef1aSdholland 		    target_prefix(), target_prefix());
25050dbef1aSdholland 
25150dbef1aSdholland 	if (logfp)
25250dbef1aSdholland 		fprintf(logfp,"buf %s strlen(buf) %zu\n", buf, strlen(buf));
25350dbef1aSdholland 
25450dbef1aSdholland 	if (strlen(buf) <= 1)  /* newline */
25550dbef1aSdholland 		rval = 0;
25650dbef1aSdholland 	else
25750dbef1aSdholland 		rval = 1;
25850dbef1aSdholland 	free(buf);
25950dbef1aSdholland 	return rval;
26050dbef1aSdholland }
26150dbef1aSdholland 
26203af0822Smartin #if CHECK_ENTROPY
26303af0822Smartin static int
add_entropy(struct menudesc * menu,void * arg)26403af0822Smartin add_entropy(struct menudesc *menu, void *arg)
26503af0822Smartin {
26603af0822Smartin 	do_add_entropy();
26703af0822Smartin 	return 0;
26803af0822Smartin }
26903af0822Smartin #endif
27003af0822Smartin 
27150dbef1aSdholland static int
add_new_user(struct menudesc * menu,void * arg)27250dbef1aSdholland add_new_user(struct menudesc *menu, void *arg)
27350dbef1aSdholland {
274362cd6eeSsnj 	char username[STRSIZE] = "";
27550dbef1aSdholland 	int inwheel=0;
27650dbef1aSdholland 
27750dbef1aSdholland 	msg_prompt(MSG_addusername, NULL, username, sizeof username -1);
278362cd6eeSsnj 	if (strlen(username) == 0)
279362cd6eeSsnj 		return 0;
2805270515fSmartin 	inwheel = ask_yesno(MSG_addusertowheel);
28150dbef1aSdholland 	ushell = "/bin/csh";
28250dbef1aSdholland 	process_menu(MENU_usersh, NULL);
28350dbef1aSdholland 	if (inwheel)
28450dbef1aSdholland 		run_program(RUN_PROGRESS | RUN_CHROOT,
28550dbef1aSdholland 		    "/usr/sbin/useradd -m -s %s -G wheel %s",
28650dbef1aSdholland 		    ushell, username);
28750dbef1aSdholland 	else
28850dbef1aSdholland 		run_program(RUN_PROGRESS | RUN_CHROOT,
28950dbef1aSdholland 		    "/usr/sbin/useradd -m -s %s %s", ushell, username);
29050dbef1aSdholland 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
29150dbef1aSdholland 	    "passwd -l %s", username);
29250dbef1aSdholland 	return 0;
29350dbef1aSdholland }
29450dbef1aSdholland 
29518183f70Smartin void
root_pw_setup(void)29618183f70Smartin root_pw_setup(void)
29718183f70Smartin {
29818183f70Smartin 	msg_display(MSG_force_rootpw);
29918183f70Smartin 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT | RUN_STDSCR,
30018183f70Smartin 	    "passwd -l root");
30118183f70Smartin }
30218183f70Smartin 
30350dbef1aSdholland static int
change_root_password(struct menudesc * menu,void * arg)30450dbef1aSdholland change_root_password(struct menudesc *menu, void *arg)
30550dbef1aSdholland {
30650dbef1aSdholland 	configinfo **confp = arg;
30750dbef1aSdholland 
30850dbef1aSdholland 	msg_display(MSG_rootpw);
309e21052b4Smartin 	if (ask_yesno(NULL)) {
3104b2364d9Smartin 		if (run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
3114b2364d9Smartin 			"passwd -l root") == 0)
31250dbef1aSdholland 			confp[menu->cursel]->setting = MSG_password_set;
3134b2364d9Smartin 		else
3144b2364d9Smartin 			confp[menu->cursel]->setting = MSG_failed;
3154b2364d9Smartin 	}
31650dbef1aSdholland 	return 0;
31750dbef1aSdholland }
31850dbef1aSdholland 
31950dbef1aSdholland static int
set_binpkg(struct menudesc * menu,void * arg)32050dbef1aSdholland set_binpkg(struct menudesc *menu, void *arg)
32150dbef1aSdholland {
32250dbef1aSdholland 	configinfo **confp = arg;
3234b2364d9Smartin 	char additional_pkgs[STRSIZE] = {0};
3244b2364d9Smartin 	int allok = 0;
325e21052b4Smartin 	arg_rv parm;
32650dbef1aSdholland 
3270578ab99Smartin 	if (config_network(0))
3280578ab99Smartin 		mnt_net_config();
3290578ab99Smartin 
3304b2364d9Smartin 	do {
331e21052b4Smartin 		parm.rv = -1;
332e21052b4Smartin 		parm.arg = additional_pkgs;
333e21052b4Smartin 		process_menu(MENU_binpkg, &parm);
334e21052b4Smartin 		if (parm.rv == SET_SKIP) {
3354b2364d9Smartin 			confp[menu->cursel]->setting = MSG_abandoned;
33650dbef1aSdholland 			return 0;
33750dbef1aSdholland 		}
33850dbef1aSdholland 
3390578ab99Smartin 		/*
3400578ab99Smartin 		 * Make sure we have the TLS certs in a usable state
3410578ab99Smartin 		 * (if target is a new installation)
3420578ab99Smartin 		 */
343*96b81bf6Smartin 		if (pkg.xfer == XFER_HTTPS)
3440578ab99Smartin 			run_program(RUN_CHROOT | RUN_SILENT,
3450578ab99Smartin 			    "/bin/sh /etc/rc.d/certctl_init onestart");
3460578ab99Smartin 
347*96b81bf6Smartin 		make_url(pkgpath, &pkg, pkg_dir);
3484b2364d9Smartin 		if (run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
3494b2364d9Smartin 			"pkg_add %s/pkgin", pkgpath) == 0) {
3504b2364d9Smartin 			allok = 1;
3514b2364d9Smartin 		}
3524b2364d9Smartin 	} while (allok == 0);
3534b2364d9Smartin 
35450dbef1aSdholland 	/* configure pkgin to use $pkgpath as a repository */
355b43b0484Schristos 	replace("/usr/pkg/etc/pkgin/repositories.conf", "s,^[^#].*$,%s,",
356b43b0484Schristos 	    pkgpath);
35750dbef1aSdholland 
35850dbef1aSdholland 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
35950dbef1aSdholland 		"/usr/pkg/bin/pkgin -y update");
36050dbef1aSdholland 
3614b2364d9Smartin 	if (strlen(additional_pkgs) > 0)
3624b2364d9Smartin 		run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
3634b2364d9Smartin 		"/usr/pkg/bin/pkgin -y install %s", additional_pkgs);
3644b2364d9Smartin 
3654103857bSmartin 	hit_enter_to_continue(MSG_binpkg_installed, NULL);
36650dbef1aSdholland 
36750dbef1aSdholland 	confp[menu->cursel]->setting = MSG_DONE;
36850dbef1aSdholland 	return 0;
36950dbef1aSdholland }
37050dbef1aSdholland 
37150dbef1aSdholland static int
set_pkgsrc(struct menudesc * menu,void * arg)37250dbef1aSdholland set_pkgsrc(struct menudesc *menu, void *arg)
37350dbef1aSdholland {
37450dbef1aSdholland 	configinfo **confp = arg;
37550dbef1aSdholland 	distinfo dist;
37650dbef1aSdholland 
37750dbef1aSdholland 	dist.name = "pkgsrc";
37850dbef1aSdholland 	dist.set = SET_PKGSRC;
37950dbef1aSdholland 	dist.desc = "source for 3rd-party packages";
38050dbef1aSdholland 	dist.marker_file = NULL;
38150dbef1aSdholland 
38250dbef1aSdholland 	int status = SET_RETRY;
38350dbef1aSdholland 
38450dbef1aSdholland 	do {
38550dbef1aSdholland 		status = get_pkgsrc();
38650dbef1aSdholland 		if (status == SET_OK) {
38750dbef1aSdholland 			status = extract_file(&dist, 0);
38850dbef1aSdholland 			continue;
38950dbef1aSdholland 		} else if (status == SET_SKIP) {
39050dbef1aSdholland 			confp[menu->cursel]->setting = MSG_abandoned;
39150dbef1aSdholland 			return 0;
39250dbef1aSdholland 		}
3935270515fSmartin 		if (!ask_yesno(MSG_retry_pkgsrc_network)) {
39450dbef1aSdholland 			confp[menu->cursel]->setting = MSG_abandoned;
39550dbef1aSdholland 			return 1;
39650dbef1aSdholland 		}
39750dbef1aSdholland 	}
39850dbef1aSdholland 	while (status == SET_RETRY);
39950dbef1aSdholland 
40050dbef1aSdholland 	confp[menu->cursel]->setting = MSG_DONE;
40150dbef1aSdholland 	return 0;
40250dbef1aSdholland }
40350dbef1aSdholland 
40450dbef1aSdholland static int
toggle_rcvar(struct menudesc * menu,void * arg)40550dbef1aSdholland toggle_rcvar(struct menudesc *menu, void *arg)
40650dbef1aSdholland {
40750dbef1aSdholland 	configinfo **confp = arg;
40850dbef1aSdholland 	int s;
40950dbef1aSdholland 	const char *setting, *varname;
41050dbef1aSdholland 	char pattern[STRSIZE];
41150dbef1aSdholland 	char buf[STRSIZE];
41250dbef1aSdholland 	char *cp;
41350dbef1aSdholland 	int found = 0;
41450dbef1aSdholland 	FILE *fp;
41550dbef1aSdholland 
41650dbef1aSdholland 	varname = confp[menu->cursel]->rcvar;
41750dbef1aSdholland 
41850dbef1aSdholland 	s = check_rcvar(varname);
41950dbef1aSdholland 
42050dbef1aSdholland 	/* we're toggling, so invert the sense */
42150dbef1aSdholland 	if (s) {
42250dbef1aSdholland 		confp[menu->cursel]->setting = MSG_NO;
42350dbef1aSdholland 		setting = "NO";
42450dbef1aSdholland 	} else {
42550dbef1aSdholland 		confp[menu->cursel]->setting = MSG_YES;
42650dbef1aSdholland 		setting = "YES";
42750dbef1aSdholland 	}
42850dbef1aSdholland 
42950dbef1aSdholland 	if (!(fp = fopen(target_expand("/etc/rc.conf"), "r"))) {
43024ecf24eSchristos 		msg_fmt_display(MSG_openfail, "%s%s",
43124ecf24eSchristos 		    target_expand("/etc/rc.conf"), strerror(errno));
4324103857bSmartin 		hit_enter_to_continue(NULL, NULL);
4334b2364d9Smartin 		return 0;
43450dbef1aSdholland 	}
43550dbef1aSdholland 
43650dbef1aSdholland 	while (fgets(buf, sizeof buf, fp) != NULL) {
43750dbef1aSdholland 		cp = buf + strspn(buf, " \t"); /* Skip initial spaces */
43850dbef1aSdholland 		if (strncmp(cp, varname, strlen(varname)) == 0) {
43950dbef1aSdholland 			cp += strlen(varname);
44050dbef1aSdholland 			if (*cp != '=')
44150dbef1aSdholland 				continue;
44250dbef1aSdholland 			buf[strlen(buf) - 1] = 0;
44350dbef1aSdholland 			snprintf(pattern, sizeof pattern,
44450dbef1aSdholland 					"s,^%s$,%s=%s,",
44550dbef1aSdholland 					buf, varname, setting);
44650dbef1aSdholland 			found = 1;
44750dbef1aSdholland 			break;
44850dbef1aSdholland 		}
44950dbef1aSdholland 	}
45050dbef1aSdholland 
45150dbef1aSdholland 	fclose(fp);
45250dbef1aSdholland 
45350dbef1aSdholland 	if (!found) {
45450dbef1aSdholland 		add_rc_conf("%s=%s\n", varname, setting);
45550dbef1aSdholland 		if (logfp) {
45650dbef1aSdholland 			fprintf(logfp, "adding %s=%s\n", varname, setting);
45750dbef1aSdholland 			fflush(logfp);
45850dbef1aSdholland 		}
45950dbef1aSdholland 	} else {
46050dbef1aSdholland 		if (logfp) {
46150dbef1aSdholland 			fprintf(logfp, "replacement pattern is %s\n", pattern);
46250dbef1aSdholland 			fflush(logfp);
46350dbef1aSdholland 		}
464b43b0484Schristos 		replace("/etc/rc.conf", "%s", pattern);
46550dbef1aSdholland 	}
46650dbef1aSdholland 
46750dbef1aSdholland 	return 0;
46850dbef1aSdholland }
46950dbef1aSdholland 
47054302b39Sjmcneill static int
toggle_mdnsd(struct menudesc * menu,void * arg)47154302b39Sjmcneill toggle_mdnsd(struct menudesc *menu, void *arg)
47254302b39Sjmcneill {
47354302b39Sjmcneill 	configinfo **confp = arg;
47454302b39Sjmcneill 	int s;
47554302b39Sjmcneill 	const char *setting, *varname;
47654302b39Sjmcneill 
47754302b39Sjmcneill 	varname = confp[menu->cursel]->rcvar;
47854302b39Sjmcneill 
47954302b39Sjmcneill 	s = check_rcvar(varname);
48054302b39Sjmcneill 
48154302b39Sjmcneill 	/* we're toggling, so invert the sense */
48254302b39Sjmcneill 	if (s) {
48354302b39Sjmcneill 		confp[menu->cursel]->setting = MSG_NO;
48454302b39Sjmcneill 		setting = "files dns";
48554302b39Sjmcneill 	} else {
48654302b39Sjmcneill 		confp[menu->cursel]->setting = MSG_YES;
48754302b39Sjmcneill 		setting = "files multicast_dns dns";
48854302b39Sjmcneill 	}
48954302b39Sjmcneill 
49054302b39Sjmcneill 	if (logfp) {
49154302b39Sjmcneill 		fprintf(logfp, "setting hosts: %s\n", setting);
49254302b39Sjmcneill 		fflush(logfp);
49354302b39Sjmcneill 	}
49454302b39Sjmcneill 	replace("/etc/nsswitch.conf", "s/^hosts:.*/hosts:\t\t%s/", setting);
49554302b39Sjmcneill 
49654302b39Sjmcneill 	toggle_rcvar(menu, arg);
49754302b39Sjmcneill 
49854302b39Sjmcneill 	return 0;
49954302b39Sjmcneill }
50054302b39Sjmcneill 
50150dbef1aSdholland static void
configmenu_hdr(struct menudesc * menu,void * arg)50250dbef1aSdholland configmenu_hdr(struct menudesc *menu, void *arg)
50350dbef1aSdholland {
50450dbef1aSdholland 	msg_display(MSG_configmenu);
50550dbef1aSdholland }
50650dbef1aSdholland 
50750dbef1aSdholland void
do_configmenu(struct install_partition_desc * install)5084103857bSmartin do_configmenu(struct install_partition_desc *install)
50950dbef1aSdholland {
51050dbef1aSdholland 	int		menu_no;
51150dbef1aSdholland 	int		opts;
51250dbef1aSdholland 	menu_ent	me[CONFIGOPT_LAST];
51350dbef1aSdholland 	configinfo	*ce[CONFIGOPT_LAST];
51450dbef1aSdholland 
5154103857bSmartin 	memset(me, 0, sizeof(me));
5164103857bSmartin 
51750dbef1aSdholland 	/* if the target isn't mounted already, figure it out. */
5184103857bSmartin 	if (install != NULL && target_mounted() == 0) {
5194b2364d9Smartin 		partman_go = 0;
520b1fa9754Smartin 		if (find_disks(msg_string(MSG_configure_prior), true) < 0)
52150dbef1aSdholland 			return;
52250dbef1aSdholland 
5234103857bSmartin 		if (mount_disks(install) != 0)
52450dbef1aSdholland 			return;
52550dbef1aSdholland 	}
52650dbef1aSdholland 
52750dbef1aSdholland 	config_list_init();
52850dbef1aSdholland 	make_url(pkgpath, &pkg, pkg_dir);
52950dbef1aSdholland 	opts = init_config_menu(config_list, me, ce);
53050dbef1aSdholland 
5314b2364d9Smartin 	wrefresh(curscr);
5324b2364d9Smartin 	wmove(stdscr, 0, 0);
5334b2364d9Smartin 	wclear(stdscr);
5344b2364d9Smartin 	wrefresh(stdscr);
5354b2364d9Smartin 
53650dbef1aSdholland 	menu_no = new_menu(NULL, me, opts, 0, -4, 0, 70,
53750dbef1aSdholland 		MC_SCROLL | MC_NOBOX | MC_DFLTEXIT,
5387ca7eecaSmartin 		configmenu_hdr, set_config, NULL, NULL,
53950dbef1aSdholland 		MSG_doneconfig);
54050dbef1aSdholland 
54150dbef1aSdholland 	process_menu(menu_no, ce);
54250dbef1aSdholland 	free_menu(menu_no);
54350dbef1aSdholland }
544