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