xref: /netbsd/usr.sbin/sysinst/configmenu.c (revision cd68f3e3)
1*cd68f3e3Smartin /* $NetBSD: configmenu.c,v 1.17 2022/05/18 16:39:03 martin Exp $ */
226165e63Sdholland 
326165e63Sdholland /*-
426165e63Sdholland  * Copyright (c) 2012 The NetBSD Foundation, Inc.
526165e63Sdholland  * All rights reserved.
626165e63Sdholland  *
726165e63Sdholland  * This code is derived from software contributed to The NetBSD Foundation
826165e63Sdholland  * by Jeffrey C. Rizzo
926165e63Sdholland  *
1026165e63Sdholland  * Redistribution and use in source and binary forms, with or without
1126165e63Sdholland  * modification, are permitted provided that the following conditions
1226165e63Sdholland  * are met:
1326165e63Sdholland  * 1. Redistributions of source code must retain the above copyright
1426165e63Sdholland  *    notice, this list of conditions and the following disclaimer.
1526165e63Sdholland  * 2. Redistributions in binary form must reproduce the above copyright
1626165e63Sdholland  *    notice, this list of conditions and the following disclaimer in the
1726165e63Sdholland  *    documentation and/or other materials provided with the distribution.
1826165e63Sdholland  *
1926165e63Sdholland  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2026165e63Sdholland  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2126165e63Sdholland  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2226165e63Sdholland  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2326165e63Sdholland  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2426165e63Sdholland  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2526165e63Sdholland  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2626165e63Sdholland  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2726165e63Sdholland  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2826165e63Sdholland  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2926165e63Sdholland  * POSSIBILITY OF SUCH DAMAGE.
3026165e63Sdholland  */
3126165e63Sdholland 
3226165e63Sdholland /* configmenu.c -- post-installation system configuration menu. */
3326165e63Sdholland 
3426165e63Sdholland #include <stdio.h>
3526165e63Sdholland #include <curses.h>
3626165e63Sdholland #include <unistd.h>
37da5a563bSmartin #include <errno.h>
3826165e63Sdholland #include "defs.h"
3926165e63Sdholland #include "msg_defs.h"
4026165e63Sdholland #include "menu_defs.h"
4126165e63Sdholland 
4226165e63Sdholland 
4326165e63Sdholland static int set_network(struct menudesc*, void *);
4426165e63Sdholland static int set_timezone_menu(struct menudesc *, void *);
4526165e63Sdholland static int set_root_shell(struct menudesc *, void *);
4626165e63Sdholland static int change_root_password(struct menudesc *, void *);
4726165e63Sdholland static int add_new_user(struct menudesc *, void *);
487eabed45Smartin #if CHECK_ENTROPY
49ffd5ced0Smartin static int add_entropy(struct menudesc *, void *);
507eabed45Smartin #endif
5126165e63Sdholland static int set_binpkg(struct menudesc *, void *);
5226165e63Sdholland static int set_pkgsrc(struct menudesc *, void *);
5326165e63Sdholland static void config_list_init(void);
5426165e63Sdholland static void get_rootsh(void);
5526165e63Sdholland static int toggle_rcvar(struct menudesc *, void *);
562964e80aSjmcneill static int toggle_mdnsd(struct menudesc *, void *);
5726165e63Sdholland static void configmenu_hdr(struct menudesc *, void *);
5826165e63Sdholland static int check_root_password(void);
5926165e63Sdholland 
6026165e63Sdholland char pkgpath[STRSIZE];
6126165e63Sdholland char pkgsrcpath[STRSIZE];
6226165e63Sdholland 
6326165e63Sdholland extern const char *tz_default;
6426165e63Sdholland 
6526165e63Sdholland enum {
6626165e63Sdholland 	CONFIGOPT_NETCONF,
6726165e63Sdholland 	CONFIGOPT_TZ,
6826165e63Sdholland 	CONFIGOPT_ROOTSH,
6926165e63Sdholland 	CONFIGOPT_ROOTPW,
7026165e63Sdholland 	CONFIGOPT_BINPKG,
7126165e63Sdholland 	CONFIGOPT_PKGSRC,
7226165e63Sdholland 	CONFIGOPT_SSHD,
7326165e63Sdholland 	CONFIGOPT_NTPD,
7426165e63Sdholland 	CONFIGOPT_NTPDATE,
7526165e63Sdholland 	CONFIGOPT_MDNSD,
76da5a563bSmartin 	CONFIGOPT_XDM,
77da5a563bSmartin 	CONFIGOPT_CGD,
78da5a563bSmartin 	CONFIGOPT_LVM,
79da5a563bSmartin 	CONFIGOPT_RAIDFRAME,
8026165e63Sdholland 	CONFIGOPT_ADDUSER,
81ffd5ced0Smartin 	CONFIGOPT_ADD_ENTROPY,
8226165e63Sdholland 	CONFIGOPT_LAST
8326165e63Sdholland };
8426165e63Sdholland 
8526165e63Sdholland typedef struct configinfo {
8626165e63Sdholland 	const char	*optname;
8726165e63Sdholland 	uint		opt;
8826165e63Sdholland 	const char	*rcvar;
8926165e63Sdholland 	int		(*action)(struct menudesc *, void *);
9026165e63Sdholland 	const char	*setting;
9126165e63Sdholland } configinfo;
9226165e63Sdholland 
9326165e63Sdholland 
9426165e63Sdholland configinfo config_list[] = {
9526165e63Sdholland 	{MSG_Configure_network, CONFIGOPT_NETCONF, NULL, set_network, MSG_configure},
9626165e63Sdholland 	{MSG_timezone, CONFIGOPT_TZ, NULL, set_timezone_menu, NULL},
9726165e63Sdholland 	{MSG_Root_shell, CONFIGOPT_ROOTSH, NULL, set_root_shell, NULL},
9826165e63Sdholland 	{MSG_change_rootpw, CONFIGOPT_ROOTPW, NULL, change_root_password, MSG_change},
99da5a563bSmartin 	{MSG_enable_binpkg, CONFIGOPT_BINPKG, NULL, set_binpkg, MSG_install},
10026165e63Sdholland 	{MSG_get_pkgsrc, CONFIGOPT_PKGSRC, NULL, set_pkgsrc, MSG_install},
10126165e63Sdholland 	{MSG_enable_sshd, CONFIGOPT_SSHD, "sshd", toggle_rcvar, NULL},
10226165e63Sdholland 	{MSG_enable_ntpd, CONFIGOPT_NTPD, "ntpd", toggle_rcvar, NULL},
10326165e63Sdholland 	{MSG_run_ntpdate, CONFIGOPT_NTPDATE, "ntpdate", toggle_rcvar, NULL},
1042964e80aSjmcneill 	{MSG_enable_mdnsd, CONFIGOPT_MDNSD, "mdnsd", toggle_mdnsd, NULL},
105da5a563bSmartin 	{MSG_enable_xdm, CONFIGOPT_XDM, "xdm", toggle_rcvar, NULL},
106da5a563bSmartin 	{MSG_enable_cgd, CONFIGOPT_CGD, "cgd", toggle_rcvar, NULL},
107da5a563bSmartin 	{MSG_enable_lvm, CONFIGOPT_LVM, "lvm", toggle_rcvar, NULL},
108da5a563bSmartin 	{MSG_enable_raid, CONFIGOPT_RAIDFRAME, "raidframe", toggle_rcvar, NULL},
10926165e63Sdholland 	{MSG_add_a_user, CONFIGOPT_ADDUSER, NULL, add_new_user, ""},
110ffd5ced0Smartin #if CHECK_ENTROPY
111ffd5ced0Smartin 	{MSG_Configure_entropy, CONFIGOPT_ADD_ENTROPY, NULL, add_entropy, ""},
112ffd5ced0Smartin #endif
11326165e63Sdholland 	{NULL,		CONFIGOPT_LAST,	NULL, NULL, NULL}
11426165e63Sdholland };
11526165e63Sdholland 
11626165e63Sdholland static void
config_list_init(void)11726165e63Sdholland config_list_init(void)
11826165e63Sdholland {
11926165e63Sdholland 	int i;
12026165e63Sdholland 
12126165e63Sdholland 	for (i=0; i < CONFIGOPT_LAST; i++) {
12226165e63Sdholland 		switch (i) {
12326165e63Sdholland 		case CONFIGOPT_TZ:
12426165e63Sdholland 			get_tz_default();
12526165e63Sdholland 			config_list[CONFIGOPT_TZ].setting = tz_default;
12626165e63Sdholland 			break;
12726165e63Sdholland 		case CONFIGOPT_ROOTSH:
12826165e63Sdholland 			get_rootsh();
12926165e63Sdholland 			break;
13026165e63Sdholland 		case CONFIGOPT_ROOTPW:
13126165e63Sdholland 			if (check_root_password())
13226165e63Sdholland 				config_list[i].setting = MSG_password_set;
13326165e63Sdholland 			else
13426165e63Sdholland 				config_list[i].setting = MSG_empty;
13526165e63Sdholland 			break;
13626165e63Sdholland 		default:
13726165e63Sdholland 			if (config_list[i].rcvar != NULL) {
13826165e63Sdholland 				if (check_rcvar(config_list[i].rcvar))
13926165e63Sdholland 					config_list[i].setting = MSG_YES;
14026165e63Sdholland 				else
14126165e63Sdholland 					config_list[i].setting = MSG_NO;
14226165e63Sdholland 			}
14326165e63Sdholland 			break;
14426165e63Sdholland 		}
14526165e63Sdholland 	}
14626165e63Sdholland }
14726165e63Sdholland 
14826165e63Sdholland static void
get_rootsh(void)14926165e63Sdholland get_rootsh(void)
15026165e63Sdholland {
15126165e63Sdholland 	static char *buf = NULL;
15226165e63Sdholland 
15326165e63Sdholland 	if (buf != NULL)
15426165e63Sdholland 		free(buf);
15526165e63Sdholland 
15626165e63Sdholland 	if (target_already_root())
15726165e63Sdholland 		collect(T_OUTPUT, &buf,
15826165e63Sdholland 		    "/usr/bin/awk -F: '$1==\"root\" { print $NF; exit }'"
15926165e63Sdholland 		    " /etc/passwd");
16026165e63Sdholland 	else
16126165e63Sdholland 		collect(T_OUTPUT, &buf,
16226165e63Sdholland 		    "chroot %s /usr/bin/awk -F: '$1==\"root\" { print $NF; exit }'"
16326165e63Sdholland 		    " /etc/passwd",target_prefix());
16426165e63Sdholland 
16526165e63Sdholland 	config_list[CONFIGOPT_ROOTSH].setting = (const char *)buf;
16626165e63Sdholland }
16726165e63Sdholland 
16826165e63Sdholland static void
set_config(menudesc * menu,int opt,void * arg)16926165e63Sdholland set_config(menudesc *menu, int opt, void *arg)
17026165e63Sdholland {
17126165e63Sdholland 	configinfo	**configp = arg;
17226165e63Sdholland 	configinfo	*config = configp[opt];
17326165e63Sdholland 	const char	*optname, *setting;
17426165e63Sdholland 
17526165e63Sdholland 	optname = config->optname;
17626165e63Sdholland 	setting = msg_string(config->setting);
17726165e63Sdholland 
17826165e63Sdholland 	wprintw(menu->mw, "%-50s %-10s", msg_string(optname), setting);
17926165e63Sdholland }
18026165e63Sdholland 
18126165e63Sdholland static int
init_config_menu(configinfo * conf,menu_ent * me,configinfo ** ce)18226165e63Sdholland init_config_menu(configinfo *conf, menu_ent *me, configinfo **ce)
18326165e63Sdholland {
18426165e63Sdholland 	int	opt;
18526165e63Sdholland 	int	configopts;
18626165e63Sdholland 
18726165e63Sdholland 	for (configopts = 0; ; conf++) {
18826165e63Sdholland 		opt = conf->opt;
18926165e63Sdholland 		if (opt == CONFIGOPT_LAST)
19026165e63Sdholland 			break;
191ffd5ced0Smartin #if CHECK_ENTROPY
192ffd5ced0Smartin 		if (opt == CONFIGOPT_ADD_ENTROPY && entropy_needed() == 0)
193ffd5ced0Smartin 			continue;
194ffd5ced0Smartin #endif
19526165e63Sdholland 		*ce = conf;
19622f633feSchristos 		memset(me, 0, sizeof(*me));
19726165e63Sdholland 		me->opt_action = conf->action;
19826165e63Sdholland 		configopts++;
19926165e63Sdholland 		ce++;
20026165e63Sdholland 		me++;
20126165e63Sdholland 	}
20226165e63Sdholland 
20326165e63Sdholland 	return configopts;
20426165e63Sdholland }
20526165e63Sdholland 
20626165e63Sdholland static int
20726165e63Sdholland /*ARGSUSED*/
set_timezone_menu(struct menudesc * menu,void * arg)20826165e63Sdholland set_timezone_menu(struct menudesc *menu, void *arg)
20926165e63Sdholland {
21026165e63Sdholland 	configinfo **confp = arg;
21126165e63Sdholland 	set_timezone();
21226165e63Sdholland 	get_tz_default();
21326165e63Sdholland 	confp[menu->cursel]->setting = tz_default;
21426165e63Sdholland 	return 0;
21526165e63Sdholland }
21626165e63Sdholland 
21726165e63Sdholland static int
set_root_shell(struct menudesc * menu,void * arg)21826165e63Sdholland set_root_shell(struct menudesc *menu, void *arg)
21926165e63Sdholland {
22026165e63Sdholland 	configinfo **confp = arg;
22126165e63Sdholland 
22226165e63Sdholland 	process_menu(MENU_rootsh, &confp[menu->cursel]->setting);
223da5a563bSmartin 	if (run_program(RUN_PROGRESS | RUN_CHROOT,
224da5a563bSmartin 		"chpass -s %s root", confp[menu->cursel]->setting) != 0)
225da5a563bSmartin 		confp[menu->cursel]->setting = MSG_failed;
22626165e63Sdholland 	return 0;
22726165e63Sdholland }
22826165e63Sdholland 
22926165e63Sdholland static int
set_network(struct menudesc * menu,void * arg)23026165e63Sdholland set_network(struct menudesc *menu, void *arg)
23126165e63Sdholland {
23226165e63Sdholland 	network_up = 0;
233*cd68f3e3Smartin 	if (config_network(1))
23426165e63Sdholland 		mnt_net_config();
23526165e63Sdholland 	return 0;
23626165e63Sdholland }
23726165e63Sdholland 
23826165e63Sdholland static int
check_root_password(void)23926165e63Sdholland check_root_password(void)
24026165e63Sdholland {
24126165e63Sdholland 	char *buf;
24226165e63Sdholland 	int rval;
24326165e63Sdholland 
24426165e63Sdholland 	if (target_already_root())
24526165e63Sdholland 		collect(T_OUTPUT, &buf, "getent passwd root | cut -d: -f2");
24626165e63Sdholland 	else
24726165e63Sdholland 		collect(T_OUTPUT, &buf, "chroot %s getent passwd root | "
24826165e63Sdholland 		    "chroot %s cut -d: -f2",
24926165e63Sdholland 		    target_prefix(), target_prefix());
25026165e63Sdholland 
25126165e63Sdholland 	if (logfp)
25226165e63Sdholland 		fprintf(logfp,"buf %s strlen(buf) %zu\n", buf, strlen(buf));
25326165e63Sdholland 
25426165e63Sdholland 	if (strlen(buf) <= 1)  /* newline */
25526165e63Sdholland 		rval = 0;
25626165e63Sdholland 	else
25726165e63Sdholland 		rval = 1;
25826165e63Sdholland 	free(buf);
25926165e63Sdholland 	return rval;
26026165e63Sdholland }
26126165e63Sdholland 
262ffd5ced0Smartin #if CHECK_ENTROPY
263ffd5ced0Smartin static int
add_entropy(struct menudesc * menu,void * arg)264ffd5ced0Smartin add_entropy(struct menudesc *menu, void *arg)
265ffd5ced0Smartin {
266ffd5ced0Smartin 	do_add_entropy();
267ffd5ced0Smartin 	return 0;
268ffd5ced0Smartin }
269ffd5ced0Smartin #endif
270ffd5ced0Smartin 
27126165e63Sdholland static int
add_new_user(struct menudesc * menu,void * arg)27226165e63Sdholland add_new_user(struct menudesc *menu, void *arg)
27326165e63Sdholland {
274889f657fSsnj 	char username[STRSIZE] = "";
27526165e63Sdholland 	int inwheel=0;
27626165e63Sdholland 
27726165e63Sdholland 	msg_prompt(MSG_addusername, NULL, username, sizeof username -1);
278889f657fSsnj 	if (strlen(username) == 0)
279889f657fSsnj 		return 0;
280e78ec759Smartin 	inwheel = ask_yesno(MSG_addusertowheel);
28126165e63Sdholland 	ushell = "/bin/csh";
28226165e63Sdholland 	process_menu(MENU_usersh, NULL);
28326165e63Sdholland 	if (inwheel)
28426165e63Sdholland 		run_program(RUN_PROGRESS | RUN_CHROOT,
28526165e63Sdholland 		    "/usr/sbin/useradd -m -s %s -G wheel %s",
28626165e63Sdholland 		    ushell, username);
28726165e63Sdholland 	else
28826165e63Sdholland 		run_program(RUN_PROGRESS | RUN_CHROOT,
28926165e63Sdholland 		    "/usr/sbin/useradd -m -s %s %s", ushell, username);
29026165e63Sdholland 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
29126165e63Sdholland 	    "passwd -l %s", username);
29226165e63Sdholland 	return 0;
29326165e63Sdholland }
29426165e63Sdholland 
295691c8254Smartin void
root_pw_setup(void)296691c8254Smartin root_pw_setup(void)
297691c8254Smartin {
298691c8254Smartin 	msg_display(MSG_force_rootpw);
299691c8254Smartin 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT | RUN_STDSCR,
300691c8254Smartin 	    "passwd -l root");
301691c8254Smartin }
302691c8254Smartin 
30326165e63Sdholland static int
change_root_password(struct menudesc * menu,void * arg)30426165e63Sdholland change_root_password(struct menudesc *menu, void *arg)
30526165e63Sdholland {
30626165e63Sdholland 	configinfo **confp = arg;
30726165e63Sdholland 
30826165e63Sdholland 	msg_display(MSG_rootpw);
30986ffd738Smartin 	if (ask_yesno(NULL)) {
310da5a563bSmartin 		if (run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
311da5a563bSmartin 			"passwd -l root") == 0)
31226165e63Sdholland 			confp[menu->cursel]->setting = MSG_password_set;
313da5a563bSmartin 		else
314da5a563bSmartin 			confp[menu->cursel]->setting = MSG_failed;
315da5a563bSmartin 	}
31626165e63Sdholland 	return 0;
31726165e63Sdholland }
31826165e63Sdholland 
31926165e63Sdholland static int
set_binpkg(struct menudesc * menu,void * arg)32026165e63Sdholland set_binpkg(struct menudesc *menu, void *arg)
32126165e63Sdholland {
32226165e63Sdholland 	configinfo **confp = arg;
323da5a563bSmartin 	char additional_pkgs[STRSIZE] = {0};
324da5a563bSmartin 	int allok = 0;
32586ffd738Smartin 	arg_rv parm;
32626165e63Sdholland 
327da5a563bSmartin 	do {
32886ffd738Smartin 		parm.rv = -1;
32986ffd738Smartin 		parm.arg = additional_pkgs;
33086ffd738Smartin 		process_menu(MENU_binpkg, &parm);
33186ffd738Smartin 		if (parm.rv == SET_SKIP) {
332da5a563bSmartin 			confp[menu->cursel]->setting = MSG_abandoned;
33326165e63Sdholland 			return 0;
33426165e63Sdholland 		}
33526165e63Sdholland 
336da5a563bSmartin 		make_url(pkgpath, &pkg, pkg_dir);
337da5a563bSmartin 		if (run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
338da5a563bSmartin 			"pkg_add %s/pkgin", pkgpath) == 0) {
339da5a563bSmartin 			allok = 1;
340da5a563bSmartin 		}
341da5a563bSmartin 	} while (allok == 0);
342da5a563bSmartin 
34326165e63Sdholland 	/* configure pkgin to use $pkgpath as a repository */
344237fac5eSchristos 	replace("/usr/pkg/etc/pkgin/repositories.conf", "s,^[^#].*$,%s,",
345237fac5eSchristos 	    pkgpath);
34626165e63Sdholland 
34726165e63Sdholland 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
34826165e63Sdholland 		"/usr/pkg/bin/pkgin -y update");
34926165e63Sdholland 
350da5a563bSmartin 	if (strlen(additional_pkgs) > 0)
351da5a563bSmartin 		run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
352da5a563bSmartin 		"/usr/pkg/bin/pkgin -y install %s", additional_pkgs);
353da5a563bSmartin 
354dcc57e24Smartin 	hit_enter_to_continue(MSG_binpkg_installed, NULL);
35526165e63Sdholland 
35626165e63Sdholland 	confp[menu->cursel]->setting = MSG_DONE;
35726165e63Sdholland 	return 0;
35826165e63Sdholland }
35926165e63Sdholland 
36026165e63Sdholland static int
set_pkgsrc(struct menudesc * menu,void * arg)36126165e63Sdholland set_pkgsrc(struct menudesc *menu, void *arg)
36226165e63Sdholland {
36326165e63Sdholland 	configinfo **confp = arg;
36426165e63Sdholland 	distinfo dist;
36526165e63Sdholland 
36626165e63Sdholland 	dist.name = "pkgsrc";
36726165e63Sdholland 	dist.set = SET_PKGSRC;
36826165e63Sdholland 	dist.desc = "source for 3rd-party packages";
36926165e63Sdholland 	dist.marker_file = NULL;
37026165e63Sdholland 
37126165e63Sdholland 	int status = SET_RETRY;
37226165e63Sdholland 
37326165e63Sdholland 	do {
37426165e63Sdholland 		status = get_pkgsrc();
37526165e63Sdholland 		if (status == SET_OK) {
37626165e63Sdholland 			status = extract_file(&dist, 0);
37726165e63Sdholland 			continue;
37826165e63Sdholland 		} else if (status == SET_SKIP) {
37926165e63Sdholland 			confp[menu->cursel]->setting = MSG_abandoned;
38026165e63Sdholland 			return 0;
38126165e63Sdholland 		}
382e78ec759Smartin 		if (!ask_yesno(MSG_retry_pkgsrc_network)) {
38326165e63Sdholland 			confp[menu->cursel]->setting = MSG_abandoned;
38426165e63Sdholland 			return 1;
38526165e63Sdholland 		}
38626165e63Sdholland 	}
38726165e63Sdholland 	while (status == SET_RETRY);
38826165e63Sdholland 
38926165e63Sdholland 	confp[menu->cursel]->setting = MSG_DONE;
39026165e63Sdholland 	return 0;
39126165e63Sdholland }
39226165e63Sdholland 
39326165e63Sdholland static int
toggle_rcvar(struct menudesc * menu,void * arg)39426165e63Sdholland toggle_rcvar(struct menudesc *menu, void *arg)
39526165e63Sdholland {
39626165e63Sdholland 	configinfo **confp = arg;
39726165e63Sdholland 	int s;
39826165e63Sdholland 	const char *setting, *varname;
39926165e63Sdholland 	char pattern[STRSIZE];
40026165e63Sdholland 	char buf[STRSIZE];
40126165e63Sdholland 	char *cp;
40226165e63Sdholland 	int found = 0;
40326165e63Sdholland 	FILE *fp;
40426165e63Sdholland 
40526165e63Sdholland 	varname = confp[menu->cursel]->rcvar;
40626165e63Sdholland 
40726165e63Sdholland 	s = check_rcvar(varname);
40826165e63Sdholland 
40926165e63Sdholland 	/* we're toggling, so invert the sense */
41026165e63Sdholland 	if (s) {
41126165e63Sdholland 		confp[menu->cursel]->setting = MSG_NO;
41226165e63Sdholland 		setting = "NO";
41326165e63Sdholland 	} else {
41426165e63Sdholland 		confp[menu->cursel]->setting = MSG_YES;
41526165e63Sdholland 		setting = "YES";
41626165e63Sdholland 	}
41726165e63Sdholland 
41826165e63Sdholland 	if (!(fp = fopen(target_expand("/etc/rc.conf"), "r"))) {
419f9805030Schristos 		msg_fmt_display(MSG_openfail, "%s%s",
420f9805030Schristos 		    target_expand("/etc/rc.conf"), strerror(errno));
421dcc57e24Smartin 		hit_enter_to_continue(NULL, NULL);
422da5a563bSmartin 		return 0;
42326165e63Sdholland 	}
42426165e63Sdholland 
42526165e63Sdholland 	while (fgets(buf, sizeof buf, fp) != NULL) {
42626165e63Sdholland 		cp = buf + strspn(buf, " \t"); /* Skip initial spaces */
42726165e63Sdholland 		if (strncmp(cp, varname, strlen(varname)) == 0) {
42826165e63Sdholland 			cp += strlen(varname);
42926165e63Sdholland 			if (*cp != '=')
43026165e63Sdholland 				continue;
43126165e63Sdholland 			buf[strlen(buf) - 1] = 0;
43226165e63Sdholland 			snprintf(pattern, sizeof pattern,
43326165e63Sdholland 					"s,^%s$,%s=%s,",
43426165e63Sdholland 					buf, varname, setting);
43526165e63Sdholland 			found = 1;
43626165e63Sdholland 			break;
43726165e63Sdholland 		}
43826165e63Sdholland 	}
43926165e63Sdholland 
44026165e63Sdholland 	fclose(fp);
44126165e63Sdholland 
44226165e63Sdholland 	if (!found) {
44326165e63Sdholland 		add_rc_conf("%s=%s\n", varname, setting);
44426165e63Sdholland 		if (logfp) {
44526165e63Sdholland 			fprintf(logfp, "adding %s=%s\n", varname, setting);
44626165e63Sdholland 			fflush(logfp);
44726165e63Sdholland 		}
44826165e63Sdholland 	} else {
44926165e63Sdholland 		if (logfp) {
45026165e63Sdholland 			fprintf(logfp, "replacement pattern is %s\n", pattern);
45126165e63Sdholland 			fflush(logfp);
45226165e63Sdholland 		}
453237fac5eSchristos 		replace("/etc/rc.conf", "%s", pattern);
45426165e63Sdholland 	}
45526165e63Sdholland 
45626165e63Sdholland 	return 0;
45726165e63Sdholland }
45826165e63Sdholland 
4592964e80aSjmcneill static int
toggle_mdnsd(struct menudesc * menu,void * arg)4602964e80aSjmcneill toggle_mdnsd(struct menudesc *menu, void *arg)
4612964e80aSjmcneill {
4622964e80aSjmcneill 	configinfo **confp = arg;
4632964e80aSjmcneill 	int s;
4642964e80aSjmcneill 	const char *setting, *varname;
4652964e80aSjmcneill 
4662964e80aSjmcneill 	varname = confp[menu->cursel]->rcvar;
4672964e80aSjmcneill 
4682964e80aSjmcneill 	s = check_rcvar(varname);
4692964e80aSjmcneill 
4702964e80aSjmcneill 	/* we're toggling, so invert the sense */
4712964e80aSjmcneill 	if (s) {
4722964e80aSjmcneill 		confp[menu->cursel]->setting = MSG_NO;
4732964e80aSjmcneill 		setting = "files dns";
4742964e80aSjmcneill 	} else {
4752964e80aSjmcneill 		confp[menu->cursel]->setting = MSG_YES;
4762964e80aSjmcneill 		setting = "files multicast_dns dns";
4772964e80aSjmcneill 	}
4782964e80aSjmcneill 
4792964e80aSjmcneill 	if (logfp) {
4802964e80aSjmcneill 		fprintf(logfp, "setting hosts: %s\n", setting);
4812964e80aSjmcneill 		fflush(logfp);
4822964e80aSjmcneill 	}
4832964e80aSjmcneill 	replace("/etc/nsswitch.conf", "s/^hosts:.*/hosts:\t\t%s/", setting);
4842964e80aSjmcneill 
4852964e80aSjmcneill 	toggle_rcvar(menu, arg);
4862964e80aSjmcneill 
4872964e80aSjmcneill 	return 0;
4882964e80aSjmcneill }
4892964e80aSjmcneill 
49026165e63Sdholland static void
configmenu_hdr(struct menudesc * menu,void * arg)49126165e63Sdholland configmenu_hdr(struct menudesc *menu, void *arg)
49226165e63Sdholland {
49326165e63Sdholland 	msg_display(MSG_configmenu);
49426165e63Sdholland }
49526165e63Sdholland 
49626165e63Sdholland void
do_configmenu(struct install_partition_desc * install)497dcc57e24Smartin do_configmenu(struct install_partition_desc *install)
49826165e63Sdholland {
49926165e63Sdholland 	int		menu_no;
50026165e63Sdholland 	int		opts;
50126165e63Sdholland 	menu_ent	me[CONFIGOPT_LAST];
50226165e63Sdholland 	configinfo	*ce[CONFIGOPT_LAST];
50326165e63Sdholland 
504dcc57e24Smartin 	memset(me, 0, sizeof(me));
505dcc57e24Smartin 
50626165e63Sdholland 	/* if the target isn't mounted already, figure it out. */
507dcc57e24Smartin 	if (install != NULL && target_mounted() == 0) {
508da5a563bSmartin 		partman_go = 0;
50908448ab1Smartin 		if (find_disks(msg_string(MSG_configure_prior), true) < 0)
51026165e63Sdholland 			return;
51126165e63Sdholland 
512dcc57e24Smartin 		if (mount_disks(install) != 0)
51326165e63Sdholland 			return;
51426165e63Sdholland 	}
51526165e63Sdholland 
51626165e63Sdholland 	config_list_init();
51726165e63Sdholland 	make_url(pkgpath, &pkg, pkg_dir);
51826165e63Sdholland 	opts = init_config_menu(config_list, me, ce);
51926165e63Sdholland 
520da5a563bSmartin 	wrefresh(curscr);
521da5a563bSmartin 	wmove(stdscr, 0, 0);
522da5a563bSmartin 	wclear(stdscr);
523da5a563bSmartin 	wrefresh(stdscr);
524da5a563bSmartin 
52526165e63Sdholland 	menu_no = new_menu(NULL, me, opts, 0, -4, 0, 70,
52626165e63Sdholland 		MC_SCROLL | MC_NOBOX | MC_DFLTEXIT,
52733798e4cSmartin 		configmenu_hdr, set_config, NULL, NULL,
52826165e63Sdholland 		MSG_doneconfig);
52926165e63Sdholland 
53026165e63Sdholland 	process_menu(menu_no, ce);
53126165e63Sdholland 	free_menu(menu_no);
53226165e63Sdholland }
533