xref: /netbsd-src/usr.sbin/sysinst/configmenu.c (revision 96b81bf6d31a6cd02ad43fc5677d10eee621ac50)
1 /* $NetBSD: configmenu.c,v 1.19 2024/03/24 16:06:37 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 	if (config_network(0))
328 		mnt_net_config();
329 
330 	do {
331 		parm.rv = -1;
332 		parm.arg = additional_pkgs;
333 		process_menu(MENU_binpkg, &parm);
334 		if (parm.rv == SET_SKIP) {
335 			confp[menu->cursel]->setting = MSG_abandoned;
336 			return 0;
337 		}
338 
339 		/*
340 		 * Make sure we have the TLS certs in a usable state
341 		 * (if target is a new installation)
342 		 */
343 		if (pkg.xfer == XFER_HTTPS)
344 			run_program(RUN_CHROOT | RUN_SILENT,
345 			    "/bin/sh /etc/rc.d/certctl_init onestart");
346 
347 		make_url(pkgpath, &pkg, pkg_dir);
348 		if (run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
349 			"pkg_add %s/pkgin", pkgpath) == 0) {
350 			allok = 1;
351 		}
352 	} while (allok == 0);
353 
354 	/* configure pkgin to use $pkgpath as a repository */
355 	replace("/usr/pkg/etc/pkgin/repositories.conf", "s,^[^#].*$,%s,",
356 	    pkgpath);
357 
358 	run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
359 		"/usr/pkg/bin/pkgin -y update");
360 
361 	if (strlen(additional_pkgs) > 0)
362 		run_program(RUN_DISPLAY | RUN_PROGRESS | RUN_CHROOT,
363 		"/usr/pkg/bin/pkgin -y install %s", additional_pkgs);
364 
365 	hit_enter_to_continue(MSG_binpkg_installed, NULL);
366 
367 	confp[menu->cursel]->setting = MSG_DONE;
368 	return 0;
369 }
370 
371 static int
set_pkgsrc(struct menudesc * menu,void * arg)372 set_pkgsrc(struct menudesc *menu, void *arg)
373 {
374 	configinfo **confp = arg;
375 	distinfo dist;
376 
377 	dist.name = "pkgsrc";
378 	dist.set = SET_PKGSRC;
379 	dist.desc = "source for 3rd-party packages";
380 	dist.marker_file = NULL;
381 
382 	int status = SET_RETRY;
383 
384 	do {
385 		status = get_pkgsrc();
386 		if (status == SET_OK) {
387 			status = extract_file(&dist, 0);
388 			continue;
389 		} else if (status == SET_SKIP) {
390 			confp[menu->cursel]->setting = MSG_abandoned;
391 			return 0;
392 		}
393 		if (!ask_yesno(MSG_retry_pkgsrc_network)) {
394 			confp[menu->cursel]->setting = MSG_abandoned;
395 			return 1;
396 		}
397 	}
398 	while (status == SET_RETRY);
399 
400 	confp[menu->cursel]->setting = MSG_DONE;
401 	return 0;
402 }
403 
404 static int
toggle_rcvar(struct menudesc * menu,void * arg)405 toggle_rcvar(struct menudesc *menu, void *arg)
406 {
407 	configinfo **confp = arg;
408 	int s;
409 	const char *setting, *varname;
410 	char pattern[STRSIZE];
411 	char buf[STRSIZE];
412 	char *cp;
413 	int found = 0;
414 	FILE *fp;
415 
416 	varname = confp[menu->cursel]->rcvar;
417 
418 	s = check_rcvar(varname);
419 
420 	/* we're toggling, so invert the sense */
421 	if (s) {
422 		confp[menu->cursel]->setting = MSG_NO;
423 		setting = "NO";
424 	} else {
425 		confp[menu->cursel]->setting = MSG_YES;
426 		setting = "YES";
427 	}
428 
429 	if (!(fp = fopen(target_expand("/etc/rc.conf"), "r"))) {
430 		msg_fmt_display(MSG_openfail, "%s%s",
431 		    target_expand("/etc/rc.conf"), strerror(errno));
432 		hit_enter_to_continue(NULL, NULL);
433 		return 0;
434 	}
435 
436 	while (fgets(buf, sizeof buf, fp) != NULL) {
437 		cp = buf + strspn(buf, " \t"); /* Skip initial spaces */
438 		if (strncmp(cp, varname, strlen(varname)) == 0) {
439 			cp += strlen(varname);
440 			if (*cp != '=')
441 				continue;
442 			buf[strlen(buf) - 1] = 0;
443 			snprintf(pattern, sizeof pattern,
444 					"s,^%s$,%s=%s,",
445 					buf, varname, setting);
446 			found = 1;
447 			break;
448 		}
449 	}
450 
451 	fclose(fp);
452 
453 	if (!found) {
454 		add_rc_conf("%s=%s\n", varname, setting);
455 		if (logfp) {
456 			fprintf(logfp, "adding %s=%s\n", varname, setting);
457 			fflush(logfp);
458 		}
459 	} else {
460 		if (logfp) {
461 			fprintf(logfp, "replacement pattern is %s\n", pattern);
462 			fflush(logfp);
463 		}
464 		replace("/etc/rc.conf", "%s", pattern);
465 	}
466 
467 	return 0;
468 }
469 
470 static int
toggle_mdnsd(struct menudesc * menu,void * arg)471 toggle_mdnsd(struct menudesc *menu, void *arg)
472 {
473 	configinfo **confp = arg;
474 	int s;
475 	const char *setting, *varname;
476 
477 	varname = confp[menu->cursel]->rcvar;
478 
479 	s = check_rcvar(varname);
480 
481 	/* we're toggling, so invert the sense */
482 	if (s) {
483 		confp[menu->cursel]->setting = MSG_NO;
484 		setting = "files dns";
485 	} else {
486 		confp[menu->cursel]->setting = MSG_YES;
487 		setting = "files multicast_dns dns";
488 	}
489 
490 	if (logfp) {
491 		fprintf(logfp, "setting hosts: %s\n", setting);
492 		fflush(logfp);
493 	}
494 	replace("/etc/nsswitch.conf", "s/^hosts:.*/hosts:\t\t%s/", setting);
495 
496 	toggle_rcvar(menu, arg);
497 
498 	return 0;
499 }
500 
501 static void
configmenu_hdr(struct menudesc * menu,void * arg)502 configmenu_hdr(struct menudesc *menu, void *arg)
503 {
504 	msg_display(MSG_configmenu);
505 }
506 
507 void
do_configmenu(struct install_partition_desc * install)508 do_configmenu(struct install_partition_desc *install)
509 {
510 	int		menu_no;
511 	int		opts;
512 	menu_ent	me[CONFIGOPT_LAST];
513 	configinfo	*ce[CONFIGOPT_LAST];
514 
515 	memset(me, 0, sizeof(me));
516 
517 	/* if the target isn't mounted already, figure it out. */
518 	if (install != NULL && target_mounted() == 0) {
519 		partman_go = 0;
520 		if (find_disks(msg_string(MSG_configure_prior), true) < 0)
521 			return;
522 
523 		if (mount_disks(install) != 0)
524 			return;
525 	}
526 
527 	config_list_init();
528 	make_url(pkgpath, &pkg, pkg_dir);
529 	opts = init_config_menu(config_list, me, ce);
530 
531 	wrefresh(curscr);
532 	wmove(stdscr, 0, 0);
533 	wclear(stdscr);
534 	wrefresh(stdscr);
535 
536 	menu_no = new_menu(NULL, me, opts, 0, -4, 0, 70,
537 		MC_SCROLL | MC_NOBOX | MC_DFLTEXIT,
538 		configmenu_hdr, set_config, NULL, NULL,
539 		MSG_doneconfig);
540 
541 	process_menu(menu_no, ce);
542 	free_menu(menu_no);
543 }
544