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