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