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