tcpip.c revision 48894
1/* 2 * $Id: tcpip.c,v 1.83 1999/07/19 10:06:18 jkh Exp $ 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 <netdb.h> 43 44/* The help file for the TCP/IP setup screen */ 45#define TCP_HELPFILE "tcp" 46 47/* These are nasty, but they make the layout structure a lot easier ... */ 48 49static char hostname[HOSTNAME_FIELD_LEN], domainname[HOSTNAME_FIELD_LEN], 50 gateway[IPADDR_FIELD_LEN], nameserver[IPADDR_FIELD_LEN]; 51static int okbutton, cancelbutton; 52static char ipaddr[IPADDR_FIELD_LEN], netmask[IPADDR_FIELD_LEN], extras[EXTRAS_FIELD_LEN]; 53 54/* What the screen size is meant to be */ 55#define TCP_DIALOG_Y 0 56#define TCP_DIALOG_X 8 57#define TCP_DIALOG_WIDTH COLS - 16 58#define TCP_DIALOG_HEIGHT LINES - 2 59 60static Layout layout[] = { 61#define LAYOUT_HOSTNAME 0 62 { 1, 2, 25, HOSTNAME_FIELD_LEN - 1, 63 "Host:", "Your fully-qualified hostname, e.g. foo.bar.com", 64 hostname, STRINGOBJ, NULL }, 65#define LAYOUT_DOMAINNAME 1 66 { 1, 35, 20, HOSTNAME_FIELD_LEN - 1, 67 "Domain:", 68 "The name of the domain that your machine is in, e.g. bar.com", 69 domainname, STRINGOBJ, NULL }, 70#define LAYOUT_GATEWAY 2 71 { 5, 2, 18, IPADDR_FIELD_LEN - 1, 72 "Gateway:", 73 "IP address of host forwarding packets to non-local destinations", 74 gateway, STRINGOBJ, NULL }, 75#define LAYOUT_NAMESERVER 3 76 { 5, 35, 18, IPADDR_FIELD_LEN - 1, 77 "Name server:", "IP address of your local DNS server", 78 nameserver, STRINGOBJ, NULL }, 79#define LAYOUT_IPADDR 4 80 { 10, 10, 18, IPADDR_FIELD_LEN - 1, 81 "IP Address:", 82 "The IP address to be used for this interface", 83 ipaddr, STRINGOBJ, NULL }, 84#define LAYOUT_NETMASK 5 85 { 10, 35, 18, IPADDR_FIELD_LEN - 1, 86 "Netmask:", 87 "The netmask for this interface, e.g. 0xffffff00 for a class C network", 88 netmask, STRINGOBJ, NULL }, 89#define LAYOUT_EXTRAS 6 90 { 14, 10, 37, HOSTNAME_FIELD_LEN - 1, 91 "Extra options to ifconfig:", 92 "Any interface-specific options to ifconfig you would like to add", 93 extras, STRINGOBJ, NULL }, 94#define LAYOUT_OKBUTTON 7 95 { 19, 15, 0, 0, 96 "OK", "Select this if you are happy with these settings", 97 &okbutton, BUTTONOBJ, NULL }, 98#define LAYOUT_CANCELBUTTON 8 99 { 19, 35, 0, 0, 100 "CANCEL", "Select this if you wish to cancel this screen", 101 &cancelbutton, BUTTONOBJ, NULL }, 102 { NULL }, 103}; 104 105#define _validByte(b) ((b) >= 0 && (b) <= 255) 106 107/* whine */ 108static void 109feepout(char *msg) 110{ 111 beep(); 112 msgConfirm(msg); 113} 114 115/* Very basic IP address integrity check - could be drastically improved */ 116static int 117verifyIP(char *ip) 118{ 119 int a, b, c, d; 120 121 if (ip && sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d) == 4 && 122 _validByte(a) && _validByte(b) && _validByte(c) && 123 _validByte(d) && (d != 255)) 124 return 1; 125 else 126 return 0; 127} 128 129/* Check for the settings on the screen - the per-interface stuff is 130 moved to the main handling code now to do it on the fly - sigh */ 131static int 132verifySettings(void) 133{ 134 if (!hostname[0]) 135 feepout("Must specify a host name of some sort!"); 136 else if (gateway[0] && strcmp(gateway, "NO") && !verifyIP(gateway)) 137 feepout("Invalid gateway IP address specified"); 138 else if (nameserver[0] && !verifyIP(nameserver)) 139 feepout("Invalid name server IP address specified"); 140 else if (netmask[0] && (netmask[0] < '0' && netmask[0] > '3')) 141 feepout("Invalid netmask value"); 142 else if (ipaddr[0] && !verifyIP(ipaddr)) 143 feepout("Invalid IP address"); 144 else 145 return 1; 146 return 0; 147} 148 149static void 150dhcpGetInfo(Device *devp) 151{ 152 /* If it fails, do it the old-fashioned way */ 153 if (dhcpParseLeases("/var/db/dhclient.leases", hostname, domainname, 154 nameserver, ipaddr, gateway, netmask) == -1) { 155 FILE *ifp; 156 char *cp, cmd[256], data[2048]; 157 int i = 0, j = 0; 158 159 /* Bah, now we have to kludge getting the information from ifconfig */ 160 snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name); 161 ifp = popen(cmd, "r"); 162 if (ifp) { 163 while ((j = fread(data + i, 1, 512, ifp)) > 0) 164 i += j; 165 fclose(ifp); 166 data[i] = 0; 167 if (isDebug()) 168 msgDebug("DHCP configured interface returns %s\n", data); 169 /* XXX This is gross as it assumes a certain ordering to 170 ifconfig's output! XXX */ 171 if ((cp = strstr(data, "inet")) != NULL) { 172 i = 0; 173 cp += 5; /* move over keyword */ 174 while (*cp != ' ') 175 ipaddr[i++] = *(cp++); 176 ipaddr[i] = '\0'; 177 if (!strncmp(++cp, "netmask", 7)) { 178 i = 0; 179 cp += 8; 180 while (*cp != ' ') 181 netmask[i++] = *(cp++); 182 netmask[i] = '\0'; 183 } 184 } 185 } 186 } 187 188 /* If we didn't get a name server value, hunt for it in resolv.conf */ 189 if (!nameserver[0] && file_readable("/etc/resolv.conf")) 190 configEnvironmentResolv("/etc/resolv.conf"); 191 192 /* See if we have a hostname and can derive one if not */ 193 if (!hostname[0] && ipaddr[0]) { 194 } 195 variable_set2(VAR_HOSTNAME, hostname, 1); 196} 197 198/* This is it - how to get TCP setup values */ 199int 200tcpOpenDialog(Device *devp) 201{ 202 WINDOW *ds_win, *save = NULL; 203 ComposeObj *obj = NULL; 204 int n = 0, filled = 0, cancel = FALSE; 205 int max, ret = DITEM_SUCCESS; 206 int use_dhcp = FALSE; 207 char *tmp; 208 char title[80]; 209 210 /* Initialise vars from previous device values */ 211 if (devp->private) { 212 DevInfo *di = (DevInfo *)devp->private; 213 214 SAFE_STRCPY(ipaddr, di->ipaddr); 215 SAFE_STRCPY(netmask, di->netmask); 216 SAFE_STRCPY(extras, di->extras); 217 use_dhcp = di->use_dhcp; 218 } 219 else { /* See if there are any defaults */ 220 char *cp; 221 222 /* First try a DHCP scan if such behavior is desired */ 223 if (!variable_cmp(VAR_TRY_DHCP, "YES") || !msgYesNo("Do you want to try DHCP configuration of the interface?")) { 224 Mkdir("/var/db"); 225 Mkdir("/var/run"); 226 Mkdir("/tmp"); 227 msgNotify("Scanning for DHCP servers..."); 228 if (!vsystem("dhclient %s", devp->name)) { 229 dhcpGetInfo(devp); 230 use_dhcp = TRUE; 231 } 232 } 233 234 /* Get old IP address from variable space, if available */ 235 if (!ipaddr[0]) { 236 if ((cp = variable_get(VAR_IPADDR)) != NULL) 237 SAFE_STRCPY(ipaddr, cp); 238 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_IPADDR))) != NULL) 239 SAFE_STRCPY(ipaddr, cp); 240 } 241 242 /* Get old netmask from variable space, if available */ 243 if (!netmask[0]) { 244 if ((cp = variable_get(VAR_NETMASK)) != NULL) 245 SAFE_STRCPY(netmask, cp); 246 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_NETMASK))) != NULL) 247 SAFE_STRCPY(netmask, cp); 248 } 249 250 /* Get old extras string from variable space, if available */ 251 if (!extras[0]) { 252 if ((cp = variable_get(VAR_EXTRAS)) != NULL) 253 SAFE_STRCPY(extras, cp); 254 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_EXTRAS))) != NULL) 255 SAFE_STRCPY(extras, cp); 256 } 257 } 258 259 /* Look up values already recorded with the system, or blank the string variables ready to accept some new data */ 260 if (!hostname[0]) { 261 tmp = variable_get(VAR_HOSTNAME); 262 if (tmp) 263 SAFE_STRCPY(hostname, tmp); 264 } 265 if (!domainname[0]) { 266 tmp = variable_get(VAR_DOMAINNAME); 267 if (tmp) 268 SAFE_STRCPY(domainname, tmp); 269 } 270 if (!gateway[0]) { 271 tmp = variable_get(VAR_GATEWAY); 272 if (tmp) 273 SAFE_STRCPY(gateway, tmp); 274 } 275 if (!nameserver[0]) { 276 tmp = variable_get(VAR_NAMESERVER); 277 if (tmp) 278 SAFE_STRCPY(nameserver, tmp); 279 } 280 281 save = savescr(); 282 /* If non-interactive, jump straight over the dialog crap and into config section */ 283 if (variable_get(VAR_NONINTERACTIVE) && 284 !variable_get(VAR_NETINTERACTIVE)) { 285 if (!hostname[0]) 286 msgConfirm("WARNING: hostname variable not set and is a non-optional\n" 287 "parameter. Please add this to your installation script\n" 288 "or set the netInteractive variable (see sysinstall man page)"); 289 else 290 goto netconfig; 291 } 292 293 /* Now do all the screen I/O */ 294 dialog_clear_norefresh(); 295 296 /* We need a curses window */ 297 if (!(ds_win = openLayoutDialog(TCP_HELPFILE, " Network Configuration ", 298 TCP_DIALOG_X, TCP_DIALOG_Y, TCP_DIALOG_WIDTH, TCP_DIALOG_HEIGHT))) { 299 beep(); 300 msgConfirm("Cannot open TCP/IP dialog window!!"); 301 restorescr(save); 302 return DITEM_FAILURE; 303 } 304 305 /* Draw interface configuration box */ 306 draw_box(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 8, TCP_DIALOG_HEIGHT - 13, TCP_DIALOG_WIDTH - 17, 307 dialog_attr, border_attr); 308 wattrset(ds_win, dialog_attr); 309 sprintf(title, " Configuration for Interface %s ", devp->name); 310 mvwaddstr(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 14, title); 311 312 /* Some more initialisation before we go into the main input loop */ 313 obj = initLayoutDialog(ds_win, layout, TCP_DIALOG_X, TCP_DIALOG_Y, &max); 314 315reenter: 316 cancelbutton = okbutton = 0; 317 while (layoutDialogLoop(ds_win, layout, &obj, &n, max, &cancelbutton, &cancel)) { 318 /* Prevent this from being irritating if user really means NO */ 319 if (filled < 3) { 320 /* Insert a default value for the netmask, 0xffffff00 is 321 * the most appropriate one (entire class C, or subnetted 322 * class A/B network). 323 */ 324 if (!netmask[0]) { 325 strcpy(netmask, "255.255.255.0"); 326 RefreshStringObj(layout[LAYOUT_NETMASK].obj); 327 ++filled; 328 } 329 if (!index(hostname, '.') && domainname[0]) { 330 strcat(hostname, "."); 331 strcat(hostname, domainname); 332 RefreshStringObj(layout[LAYOUT_HOSTNAME].obj); 333 ++filled; 334 } 335 else if (((tmp = index(hostname, '.')) != NULL) && !domainname[0]) { 336 SAFE_STRCPY(domainname, tmp + 1); 337 RefreshStringObj(layout[LAYOUT_DOMAINNAME].obj); 338 ++filled; 339 } 340 } 341 } 342 if (!cancel && !verifySettings()) 343 goto reenter; 344 345 /* Clear this crap off the screen */ 346 delwin(ds_win); 347 dialog_clear_norefresh(); 348 use_helpfile(NULL); 349 350 /* We actually need to inform the rest of sysinstall about this 351 data now if the user hasn't selected cancel. Save the stuff 352 out to the environment via the variable_set() mechanism */ 353 354netconfig: 355 if (!cancel) { 356 DevInfo *di; 357 char temp[512], ifn[255]; 358 char *ifaces; 359 360 if (hostname[0]) { 361 variable_set2(VAR_HOSTNAME, hostname, 1); 362 sethostname(hostname, strlen(hostname)); 363 } 364 if (domainname[0]) 365 variable_set2(VAR_DOMAINNAME, domainname, 0); 366 if (gateway[0]) 367 variable_set2(VAR_GATEWAY, gateway, 1); 368 if (nameserver[0]) 369 variable_set2(VAR_NAMESERVER, nameserver, 0); 370 if (ipaddr[0]) 371 variable_set2(VAR_IPADDR, ipaddr, 0); 372 373 if (!devp->private) 374 devp->private = (DevInfo *)safe_malloc(sizeof(DevInfo)); 375 di = devp->private; 376 SAFE_STRCPY(di->ipaddr, ipaddr); 377 SAFE_STRCPY(di->netmask, netmask); 378 SAFE_STRCPY(di->extras, extras); 379 di->use_dhcp = use_dhcp; 380 381 sprintf(ifn, "%s%s", VAR_IFCONFIG, devp->name); 382 if (use_dhcp) 383 sprintf(temp, "DHCP"); 384 else 385 sprintf(temp, "inet %s %s netmask %s", ipaddr, extras, netmask); 386 variable_set2(ifn, temp, 1); 387 ifaces = variable_get(VAR_INTERFACES); 388 if (!ifaces) 389 variable_set2(VAR_INTERFACES, ifaces = "lo0", 1); 390 /* Only add it if it's not there already */ 391 if (!strstr(ifaces, devp->name)) { 392 sprintf(ifn, "%s %s", devp->name, ifaces); 393 variable_set2(VAR_INTERFACES, ifn, 1); 394 } 395 if (!use_dhcp) 396 configResolv(NULL); /* XXX this will do it on the MFS copy XXX */ 397 ret = DITEM_SUCCESS; 398 } 399 else 400 ret = DITEM_FAILURE; 401 restorescr(save); 402 return ret; 403} 404 405static Device *NetDev; 406 407static int 408netHook(dialogMenuItem *self) 409{ 410 Device **devs; 411 412 devs = deviceFindDescr(self->prompt, self->title, DEVICE_TYPE_NETWORK); 413 if (devs) { 414 if (DITEM_STATUS(tcpOpenDialog(devs[0])) != DITEM_FAILURE) 415 NetDev = devs[0]; 416 else 417 NetDev = NULL; 418 } 419 return devs ? DITEM_LEAVE_MENU : DITEM_FAILURE; 420} 421 422/* Get a network device */ 423Device * 424tcpDeviceSelect(void) 425{ 426 DMenu *menu; 427 Device **devs, *rval; 428 int cnt; 429 430 devs = deviceFind(NULL, DEVICE_TYPE_NETWORK); 431 cnt = deviceCount(devs); 432 rval = NULL; 433 434 if (!cnt) { 435 msgConfirm("No network devices available!"); 436 return NULL; 437 } 438 else if (!RunningAsInit) { 439 if (!msgYesNo("Running multi-user, assume that the network is already configured?")) 440 return devs[0]; 441 } 442 if (cnt == 1) { 443 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS)) 444 rval = devs[0]; 445 } 446 else if (variable_get(VAR_NONINTERACTIVE) && variable_get(VAR_NETWORK_DEVICE)) { 447 devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK); 448 cnt = deviceCount(devs); 449 if (cnt) { 450 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS)) 451 rval = devs[0]; 452 } 453 } 454 else { 455 int status; 456 457 menu = deviceCreateMenu(&MenuNetworkDevice, DEVICE_TYPE_NETWORK, netHook, NULL); 458 if (!menu) 459 msgFatal("Unable to create network device menu! Argh!"); 460 status = dmenuOpenSimple(menu, FALSE); 461 free(menu); 462 if (status) 463 rval = NetDev; 464 } 465 return rval; 466} 467 468/* Do it from a menu that doesn't care about status */ 469int 470tcpMenuSelect(dialogMenuItem *self) 471{ 472 Device *tmp; 473 474 tmp = tcpDeviceSelect(); 475 if (tmp && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name)) 476 if (!tmp->init(tmp)) 477 msgConfirm("Initialization of %s device failed.", tmp->name); 478 return DITEM_SUCCESS | DITEM_RESTORE; 479} 480