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