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