tcpip.c revision 63832
1/* 2 * $FreeBSD: head/usr.sbin/sysinstall/tcpip.c 63832 2000-07-25 07:38:22Z jkh $ 3 * 4 * Copyright (c) 1995 5 * Gary J Palmer. All rights reserved. 6 * Copyright (c) 1996 7 * Jordan K. Hubbard. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer, 14 * verbatim and that no modifications are made prior to this 15 * point in the file. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 26 * OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 28 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 */ 32 33/* 34 * All kinds of hacking also performed by jkh on this code. Don't 35 * blame Gary for every bogosity you see here.. :-) 36 * 37 * -jkh 38 */ 39 40#include "sysinstall.h" 41#include <sys/param.h> 42#include <sys/sysctl.h> 43#include <sys/socket.h> 44#include <netinet/in.h> 45#include <netdb.h> 46 47/* The help file for the TCP/IP setup screen */ 48#define TCP_HELPFILE "tcp" 49 50/* These are nasty, but they make the layout structure a lot easier ... */ 51 52static char hostname[HOSTNAME_FIELD_LEN], domainname[HOSTNAME_FIELD_LEN], 53 gateway[IPADDR_FIELD_LEN], nameserver[INET6_ADDRSTRLEN]; 54static int okbutton, cancelbutton; 55static char ipaddr[IPADDR_FIELD_LEN], netmask[IPADDR_FIELD_LEN], extras[EXTRAS_FIELD_LEN]; 56static char ipv6addr[INET6_ADDRSTRLEN]; 57 58/* What the screen size is meant to be */ 59#define TCP_DIALOG_Y 0 60#define TCP_DIALOG_X 8 61#define TCP_DIALOG_WIDTH COLS - 16 62#define TCP_DIALOG_HEIGHT LINES - 2 63 64static Layout layout[] = { 65#define LAYOUT_HOSTNAME 0 66 { 1, 2, 25, HOSTNAME_FIELD_LEN - 1, 67 "Host:", "Your fully-qualified hostname, e.g. foo.bar.com", 68 hostname, STRINGOBJ, NULL }, 69#define LAYOUT_DOMAINNAME 1 70 { 1, 35, 20, HOSTNAME_FIELD_LEN - 1, 71 "Domain:", 72 "The name of the domain that your machine is in, e.g. bar.com", 73 domainname, STRINGOBJ, NULL }, 74#define LAYOUT_GATEWAY 2 75 { 5, 2, 18, IPADDR_FIELD_LEN - 1, 76 "IPv4 Gateway:", 77 "IPv4 address of host forwarding packets to non-local destinations", 78 gateway, STRINGOBJ, NULL }, 79#define LAYOUT_NAMESERVER 3 80 { 5, 35, 18, INET6_ADDRSTRLEN - 1, 81 "Name server:", "IPv4 or IPv6 address of your local DNS server", 82 nameserver, STRINGOBJ, NULL }, 83#define LAYOUT_IPADDR 4 84 { 10, 10, 18, IPADDR_FIELD_LEN - 1, 85 "IPv4 Address:", 86 "The IPv4 address to be used for this interface", 87 ipaddr, STRINGOBJ, NULL }, 88#define LAYOUT_NETMASK 5 89 { 10, 35, 18, IPADDR_FIELD_LEN - 1, 90 "Netmask:", 91 "The netmask for this interface, e.g. 0xffffff00 for a class C network", 92 netmask, STRINGOBJ, NULL }, 93#define LAYOUT_EXTRAS 6 94 { 14, 10, 37, HOSTNAME_FIELD_LEN - 1, 95 "Extra options to ifconfig:", 96 "Any interface-specific options to ifconfig you would like to add", 97 extras, STRINGOBJ, NULL }, 98#define LAYOUT_OKBUTTON 7 99 { 19, 15, 0, 0, 100 "OK", "Select this if you are happy with these settings", 101 &okbutton, BUTTONOBJ, NULL }, 102#define LAYOUT_CANCELBUTTON 8 103 { 19, 35, 0, 0, 104 "CANCEL", "Select this if you wish to cancel this screen", 105 &cancelbutton, BUTTONOBJ, NULL }, 106 { NULL }, 107}; 108 109#define _validByte(b) ((b) >= 0 && (b) <= 255) 110 111/* whine */ 112static void 113feepout(char *msg) 114{ 115 beep(); 116 msgConfirm(msg); 117} 118 119/* Very basic IP address integrity check - could be drastically improved */ 120static int 121verifyIP(char *ip) 122{ 123 int a, b, c, d; 124 125 if (ip && sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 && 126 _validByte(a) && _validByte(b) && _validByte(c) && 127 _validByte(d) && (d != 255)) 128 return 1; 129 else 130 return 0; 131} 132 133static int 134verifyIP6(char *ip) 135{ 136 struct addrinfo hints, *res; 137 138 memset(&hints, 0, sizeof(hints)); 139 hints.ai_family = AF_INET6; 140 hints.ai_socktype = SOCK_STREAM; 141 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST; 142 if (getaddrinfo(ip, NULL, &hints, &res) == 0) { 143 freeaddrinfo(res); 144 return 1; 145 } 146 return 0; 147} 148 149/* Check for the settings on the screen - the per-interface stuff is 150 moved to the main handling code now to do it on the fly - sigh */ 151static int 152verifySettings(void) 153{ 154 if (!hostname[0]) 155 feepout("Must specify a host name of some sort!"); 156 else if (gateway[0] && strcmp(gateway, "NO") && !verifyIP(gateway)) 157 feepout("Invalid gateway IPv4 address specified"); 158 else if (nameserver[0] && !verifyIP(nameserver) && !verifyIP6(nameserver)) 159 feepout("Invalid name server IP address specified"); 160 else if (netmask[0] && (netmask[0] < '0' && netmask[0] > '3')) 161 feepout("Invalid netmask value"); 162 else if (ipaddr[0] && !verifyIP(ipaddr)) 163 feepout("Invalid IPv4 address"); 164 else 165 return 1; 166 return 0; 167} 168 169static void 170dhcpGetInfo(Device *devp) 171{ 172 /* If it fails, do it the old-fashioned way */ 173 if (dhcpParseLeases("/var/db/dhclient.leases", hostname, domainname, 174 nameserver, ipaddr, gateway, netmask) == -1) { 175 FILE *ifp; 176 char *cp, cmd[256], data[2048]; 177 int i, j; 178 179 /* Bah, now we have to kludge getting the information from ifconfig */ 180 snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name); 181 ifp = popen(cmd, "r"); 182 if (ifp) { 183 j = fread(data, 1, sizeof(data), ifp); 184 fclose(ifp); 185 if (j < 0) /* paranoia */ 186 j = 0; 187 data[j] = '\0'; 188 if (isDebug()) 189 msgDebug("DHCP configured interface returns %s\n", data); 190 /* XXX This is gross as it assumes a certain ordering to 191 ifconfig's output! XXX */ 192 if ((cp = strstr(data, "inet ")) != NULL) { 193 i = 0; 194 cp += 5; /* move over keyword */ 195 while (*cp != ' ') 196 ipaddr[i++] = *(cp++); 197 ipaddr[i] = '\0'; 198 if (!strncmp(++cp, "netmask", 7)) { 199 i = 0; 200 cp += 8; 201 while (*cp != ' ') 202 netmask[i++] = *(cp++); 203 netmask[i] = '\0'; 204 } 205 } 206 } 207 } 208 209 /* If we didn't get a name server value, hunt for it in resolv.conf */ 210 if (!nameserver[0] && file_readable("/etc/resolv.conf")) 211 configEnvironmentResolv("/etc/resolv.conf"); 212 if (hostname[0]) 213 variable_set2(VAR_HOSTNAME, hostname, 0); 214} 215 216static void 217rtsolGetInfo(Device *devp) 218{ 219 FILE *ifp; 220 char *cp, cmd[256], data[2048]; 221 int i; 222 223 snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name); 224 if ((ifp = popen(cmd, "r")) == NULL) 225 return; 226 while (fgets(data, sizeof(data), ifp) != NULL) { 227 if (isDebug()) 228 msgDebug("RTSOL configured interface returns %s", data); 229 if ((cp = strstr(data, "inet6 ")) != NULL) { 230 cp += 6; /* move over keyword */ 231 if (strncmp(cp, "fe80:", 5)) { 232 i = 0; 233 while (*cp != ' ') 234 ipv6addr[i++] = *(cp++); 235 ipv6addr[i] = '\0'; 236 } 237 } 238 } 239 fclose(ifp); 240} 241 242/* This is it - how to get TCP setup values */ 243int 244tcpOpenDialog(Device *devp) 245{ 246 WINDOW *ds_win, *save = NULL; 247 ComposeObj *obj = NULL; 248 int n = 0, filled = 0, cancel = FALSE; 249 int max, ret = DITEM_SUCCESS; 250 int use_dhcp = FALSE; 251 int use_rtsol = FALSE; 252 char *tmp; 253 char title[80]; 254 255 save = savescr(); 256 /* Initialise vars from previous device values */ 257 if (devp->private) { 258 DevInfo *di = (DevInfo *)devp->private; 259 260 SAFE_STRCPY(ipaddr, di->ipaddr); 261 SAFE_STRCPY(netmask, di->netmask); 262 SAFE_STRCPY(extras, di->extras); 263 use_dhcp = di->use_dhcp; 264 use_rtsol = di->use_rtsol; 265 } 266 else { /* See if there are any defaults */ 267 char *cp; 268 269 /* Try a RTSOL scan if such behavior is desired */ 270 if (!variable_cmp(VAR_TRY_RTSOL, "YES") || !msgYesNo("Do you want to try IPv6 configuration of the interface?")) { 271 int i; 272 273 i = 0; 274 sysctlbyname("net.inet6.ip6.forwarding", NULL, 0, &i, sizeof(i)); 275 i = 1; 276 sysctlbyname("net.inet6.ip6.accept_rtadv", NULL, 0, &i, sizeof(i)); 277 vsystem("ifconfig %s up", devp->name); 278 Mkdir("/var/run"); 279 msgNotify("Scanning for RA servers..."); 280 if (0 == vsystem("rtsol %s", devp->name)) { 281 sleep(3); 282 rtsolGetInfo(devp); 283 use_rtsol = TRUE; 284 } else 285 use_rtsol = FALSE; 286 } 287 288 /* First try a DHCP scan if such behavior is desired */ 289 if (!variable_cmp(VAR_TRY_DHCP, "YES") || !msgYesNo("Do you want to try DHCP configuration of the interface?")) { 290 Mkdir("/var/db"); 291 Mkdir("/var/run"); 292 Mkdir("/tmp"); 293 msgNotify("Scanning for DHCP servers..."); 294 if (0 == vsystem("dhclient -1 %s", devp->name)) { 295 dhcpGetInfo(devp); 296 use_dhcp = TRUE; 297 } 298 else 299 use_dhcp = FALSE; 300 } 301 302 /* Special hack so it doesn't show up oddly in the tcpip setup menu */ 303 if (!strcmp(gateway, "NO")) 304 gateway[0] = '\0'; 305 306 /* Get old IP address from variable space, if available */ 307 if (!ipaddr[0]) { 308 if ((cp = variable_get(VAR_IPADDR)) != NULL) 309 SAFE_STRCPY(ipaddr, cp); 310 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_IPADDR))) != NULL) 311 SAFE_STRCPY(ipaddr, cp); 312 } 313 314 /* Get old netmask from variable space, if available */ 315 if (!netmask[0]) { 316 if ((cp = variable_get(VAR_NETMASK)) != NULL) 317 SAFE_STRCPY(netmask, cp); 318 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_NETMASK))) != NULL) 319 SAFE_STRCPY(netmask, cp); 320 } 321 322 /* Get old extras string from variable space, if available */ 323 if (!extras[0]) { 324 if ((cp = variable_get(VAR_EXTRAS)) != NULL) 325 SAFE_STRCPY(extras, cp); 326 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_EXTRAS))) != NULL) 327 SAFE_STRCPY(extras, cp); 328 } 329 } 330 331 /* Look up values already recorded with the system, or blank the string variables ready to accept some new data */ 332 if (!hostname[0]) { 333 tmp = variable_get(VAR_HOSTNAME); 334 if (tmp) 335 SAFE_STRCPY(hostname, tmp); 336 } 337 if (!domainname[0]) { 338 tmp = variable_get(VAR_DOMAINNAME); 339 if (tmp) 340 SAFE_STRCPY(domainname, tmp); 341 } 342 if (!gateway[0]) { 343 tmp = variable_get(VAR_GATEWAY); 344 if (tmp && strcmp(tmp, "NO")) 345 SAFE_STRCPY(gateway, tmp); 346 } 347 if (!nameserver[0]) { 348 tmp = variable_get(VAR_NAMESERVER); 349 if (tmp) 350 SAFE_STRCPY(nameserver, tmp); 351 } 352 353 /* If non-interactive, jump straight over the dialog crap and into config section */ 354 if (variable_get(VAR_NONINTERACTIVE) && 355 !variable_get(VAR_NETINTERACTIVE)) { 356 if (!hostname[0]) 357 msgConfirm("WARNING: hostname variable not set and is a non-optional\n" 358 "parameter. Please add this to your installation script\n" 359 "or set the netInteractive variable (see sysinstall man page)"); 360 else 361 goto netconfig; 362 } 363 364 /* Now do all the screen I/O */ 365 dialog_clear_norefresh(); 366 367 /* We need a curses window */ 368 tmp = " Network Configuration "; 369 if (ipv6addr[0]) 370 tmp = string_concat(tmp, "(IPv6 ready) "); 371 if (!(ds_win = openLayoutDialog(TCP_HELPFILE, tmp, 372 TCP_DIALOG_X, TCP_DIALOG_Y, TCP_DIALOG_WIDTH, TCP_DIALOG_HEIGHT))) { 373 beep(); 374 msgConfirm("Cannot open TCP/IP dialog window!!"); 375 restorescr(save); 376 return DITEM_FAILURE; 377 } 378 379 /* Draw interface configuration box */ 380 draw_box(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 8, TCP_DIALOG_HEIGHT - 13, TCP_DIALOG_WIDTH - 17, 381 dialog_attr, border_attr); 382 wattrset(ds_win, dialog_attr); 383 sprintf(title, " Configuration for Interface %s ", devp->name); 384 mvwaddstr(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 14, title); 385 386 /* Some more initialisation before we go into the main input loop */ 387 obj = initLayoutDialog(ds_win, layout, TCP_DIALOG_X, TCP_DIALOG_Y, &max); 388 389reenter: 390 cancelbutton = okbutton = 0; 391 while (layoutDialogLoop(ds_win, layout, &obj, &n, max, &cancelbutton, &cancel)) { 392 /* Prevent this from being irritating if user really means NO */ 393 if (filled < 3) { 394 /* Insert a default value for the netmask, 0xffffff00 is 395 * the most appropriate one (entire class C, or subnetted 396 * class A/B network). 397 */ 398 if (!netmask[0]) { 399 strcpy(netmask, "255.255.255.0"); 400 RefreshStringObj(layout[LAYOUT_NETMASK].obj); 401 ++filled; 402 } 403 if (!index(hostname, '.') && domainname[0]) { 404 strcat(hostname, "."); 405 strcat(hostname, domainname); 406 RefreshStringObj(layout[LAYOUT_HOSTNAME].obj); 407 ++filled; 408 } 409 else if (((tmp = index(hostname, '.')) != NULL) && !domainname[0]) { 410 SAFE_STRCPY(domainname, tmp + 1); 411 RefreshStringObj(layout[LAYOUT_DOMAINNAME].obj); 412 ++filled; 413 } 414 } 415 } 416 if (!cancel && !verifySettings()) 417 goto reenter; 418 419 /* Clear this crap off the screen */ 420 delwin(ds_win); 421 dialog_clear_norefresh(); 422 use_helpfile(NULL); 423 424 /* We actually need to inform the rest of sysinstall about this 425 data now if the user hasn't selected cancel. Save the stuff 426 out to the environment via the variable_set() mechanism */ 427 428netconfig: 429 if (!cancel) { 430 DevInfo *di; 431 char temp[512], ifn[255]; 432 char *ifaces; 433 char *pccard; 434 int ipv4_enable = FALSE; 435 436 if (hostname[0]) { 437 variable_set2(VAR_HOSTNAME, hostname, 1); 438 sethostname(hostname, strlen(hostname)); 439 } 440 if (domainname[0]) 441 variable_set2(VAR_DOMAINNAME, domainname, 0); 442 if (gateway[0]) 443 variable_set2(VAR_GATEWAY, gateway, use_dhcp ? 0 : 1); 444 if (nameserver[0]) 445 variable_set2(VAR_NAMESERVER, nameserver, 0); 446 if (ipaddr[0]) 447 variable_set2(VAR_IPADDR, ipaddr, 0); 448 if (ipv6addr[0]) 449 variable_set2(VAR_IPV6ADDR, ipv6addr, 0); 450 451 if (!devp->private) 452 devp->private = (DevInfo *)safe_malloc(sizeof(DevInfo)); 453 di = devp->private; 454 SAFE_STRCPY(di->ipaddr, ipaddr); 455 SAFE_STRCPY(di->netmask, netmask); 456 SAFE_STRCPY(di->extras, extras); 457 di->use_dhcp = use_dhcp; 458 di->use_rtsol = use_rtsol; 459 460 if (use_dhcp || ipaddr[0]) 461 ipv4_enable = TRUE; 462 if (ipv4_enable) { 463 sprintf(ifn, "%s%s", VAR_IFCONFIG, devp->name); 464 if (use_dhcp) 465 sprintf(temp, "DHCP"); 466 else 467 sprintf(temp, "inet %s %s netmask %s", 468 ipaddr, extras, netmask); 469 variable_set2(ifn, temp, 1); 470 } 471 pccard = variable_get("_pccard_install"); 472 if (pccard && strcmp(pccard, "YES") == 0 && ipv4_enable) { 473 variable_set2("pccard_ifconfig", temp, 1); 474 } 475 ifaces = variable_get(VAR_INTERFACES); 476 if (!ifaces) 477 variable_set2(VAR_INTERFACES, ifaces = "lo0", 1); 478 /* Only add it if it's not there already */ 479 if (strcmp(ifaces, "auto") && !strstr(ifaces, devp->name)) { 480 sprintf(ifn, "%s %s", devp->name, ifaces); 481 variable_set2(VAR_INTERFACES, ifn, 1); 482 } 483 if (use_rtsol) 484 variable_set2(VAR_IPV6_ENABLE, "YES", 1); 485 if (!use_dhcp) 486 configResolv(NULL); /* XXX this will do it on the MFS copy XXX */ 487 ret = DITEM_SUCCESS; 488 } 489 else 490 ret = DITEM_FAILURE; 491 restorescr(save); 492 return ret; 493} 494 495static Device *NetDev; 496 497static int 498netHook(dialogMenuItem *self) 499{ 500 Device **devs; 501 502 devs = deviceFindDescr(self->prompt, self->title, DEVICE_TYPE_NETWORK); 503 if (devs) { 504 if (DITEM_STATUS(tcpOpenDialog(devs[0])) != DITEM_FAILURE) 505 NetDev = devs[0]; 506 else 507 NetDev = NULL; 508 } 509 return devs ? DITEM_LEAVE_MENU : DITEM_FAILURE; 510} 511 512/* Get a network device */ 513Device * 514tcpDeviceSelect(void) 515{ 516 DMenu *menu; 517 Device **devs, *rval; 518 int cnt; 519 520 devs = deviceFind(NULL, DEVICE_TYPE_NETWORK); 521 cnt = deviceCount(devs); 522 rval = NULL; 523 524 if (!cnt) { 525 msgConfirm("No network devices available!"); 526 return NULL; 527 } 528 else if ((!RunningAsInit) && (variable_check("NETWORK_CONFIGURED=NO") != TRUE)) { 529 if (!msgYesNo("Running multi-user, assume that the network is already configured?")) 530 return devs[0]; 531 } 532 if (cnt == 1) { 533 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS)) 534 rval = devs[0]; 535 } 536 else if (variable_get(VAR_NONINTERACTIVE) && variable_get(VAR_NETWORK_DEVICE)) { 537 devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK); 538 cnt = deviceCount(devs); 539 if (cnt) { 540 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS)) 541 rval = devs[0]; 542 } 543 } 544 else { 545 int status; 546 547 menu = deviceCreateMenu(&MenuNetworkDevice, DEVICE_TYPE_NETWORK, netHook, NULL); 548 if (!menu) 549 msgFatal("Unable to create network device menu! Argh!"); 550 status = dmenuOpenSimple(menu, FALSE); 551 free(menu); 552 if (status) 553 rval = NetDev; 554 } 555 return rval; 556} 557 558/* Do it from a menu that doesn't care about status */ 559int 560tcpMenuSelect(dialogMenuItem *self) 561{ 562 Device *tmp; 563 WINDOW *save; 564 565 variable_set("NETWORK_CONFIGURED=NO",0); 566 tmp = tcpDeviceSelect(); 567 variable_unset("NETWORK_CONFIGURED"); 568 save = savescr(); 569 if (tmp && tmp->private && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name)) 570 if (!tmp->init(tmp)) 571 msgConfirm("Initialization of %s device failed.", tmp->name); 572 restorescr(save); 573 return DITEM_SUCCESS; 574} 575