5 * Gary J Palmer. All rights reserved.
7 * Jordan K. Hubbard. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
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.
34 * All kinds of hacking also performed by jkh on this code. Don't
35 * blame Gary for every bogosity you see here.. :-)
40 #include "sysinstall.h"
41 #include <sys/param.h>
44 /* The help file for the TCP/IP setup screen */
45 #define TCP_HELPFILE "tcp"
47 /* These are nasty, but they make the layout structure a lot easier ... */
49 static char hostname[HOSTNAME_FIELD_LEN], domainname[HOSTNAME_FIELD_LEN],
50 gateway[IPADDR_FIELD_LEN], nameserver[IPADDR_FIELD_LEN];
51 static int okbutton, cancelbutton;
52 static char ipaddr[IPADDR_FIELD_LEN], netmask[IPADDR_FIELD_LEN], extras[EXTRAS_FIELD_LEN];
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
60 static 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,
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,
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,
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,
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
96 "OK", "Select this if you are happy with these settings",
97 &okbutton, BUTTONOBJ, NULL },
98 #define LAYOUT_CANCELBUTTON 8
100 "CANCEL", "Select this if you wish to cancel this screen",
101 &cancelbutton, BUTTONOBJ, NULL },
105 #define _validByte(b) ((b) >= 0 && (b) <= 255)
115 /* Very basic IP address integrity check - could be drastically improved */
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))
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 */
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");
150 dhcpGetInfo(Device *devp)
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) {
156 char *cp, cmd[256], data[2048];
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");
163 j = fread(data, 1, sizeof(data), ifp);
165 if (j < 0) /* paranoia */
169 msgDebug("DHCP configured interface returns %s\n", data);
170 /* XXX This is gross as it assumes a certain ordering to
171 ifconfig's output! XXX */
172 if ((cp = strstr(data, "inet")) != NULL) {
174 cp += 5; /* move over keyword */
176 ipaddr[i++] = *(cp++);
178 if (!strncmp(++cp, "netmask", 7)) {
182 netmask[i++] = *(cp++);
189 /* If we didn't get a name server value, hunt for it in resolv.conf */
190 if (!nameserver[0] && file_readable("/etc/resolv.conf"))
191 configEnvironmentResolv("/etc/resolv.conf");
193 variable_set2(VAR_HOSTNAME, hostname, 0);
196 /* This is it - how to get TCP setup values */
198 tcpOpenDialog(Device *devp)
200 WINDOW *ds_win, *save = NULL;
201 ComposeObj *obj = NULL;
202 int n = 0, filled = 0, cancel = FALSE;
203 int max, ret = DITEM_SUCCESS;
204 int use_dhcp = FALSE;
208 /* Initialise vars from previous device values */
210 DevInfo *di = (DevInfo *)devp->private;
212 SAFE_STRCPY(ipaddr, di->ipaddr);
213 SAFE_STRCPY(netmask, di->netmask);
214 SAFE_STRCPY(extras, di->extras);
215 use_dhcp = di->use_dhcp;
217 else { /* See if there are any defaults */
220 /* First try a DHCP scan if such behavior is desired */
221 if (!variable_cmp(VAR_TRY_DHCP, "YES") || !msgYesNo("Do you want to try DHCP configuration of the interface?")) {
227 msgNotify("Scanning for DHCP servers...");
228 for (k = 1; k < 4; k++) {
229 if (0 == vsystem("dhclient -1 %s", devp->name)) {
234 msgNotify("Scanning for DHCP servers... Retry: %d", k);
238 /* Get old IP address from variable space, if available */
240 if ((cp = variable_get(VAR_IPADDR)) != NULL)
241 SAFE_STRCPY(ipaddr, cp);
242 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_IPADDR))) != NULL)
243 SAFE_STRCPY(ipaddr, cp);
246 /* Get old netmask from variable space, if available */
248 if ((cp = variable_get(VAR_NETMASK)) != NULL)
249 SAFE_STRCPY(netmask, cp);
250 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_NETMASK))) != NULL)
251 SAFE_STRCPY(netmask, cp);
254 /* Get old extras string from variable space, if available */
256 if ((cp = variable_get(VAR_EXTRAS)) != NULL)
257 SAFE_STRCPY(extras, cp);
258 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_EXTRAS))) != NULL)
259 SAFE_STRCPY(extras, cp);
263 /* Look up values already recorded with the system, or blank the string variables ready to accept some new data */
265 tmp = variable_get(VAR_HOSTNAME);
267 SAFE_STRCPY(hostname, tmp);
269 if (!domainname[0]) {
270 tmp = variable_get(VAR_DOMAINNAME);
272 SAFE_STRCPY(domainname, tmp);
275 tmp = variable_get(VAR_GATEWAY);
277 SAFE_STRCPY(gateway, tmp);
279 if (!nameserver[0]) {
280 tmp = variable_get(VAR_NAMESERVER);
282 SAFE_STRCPY(nameserver, tmp);
286 /* If non-interactive, jump straight over the dialog crap and into config section */
287 if (variable_get(VAR_NONINTERACTIVE) &&
288 !variable_get(VAR_NETINTERACTIVE)) {
290 msgConfirm("WARNING: hostname variable not set and is a non-optional\n"
291 "parameter. Please add this to your installation script\n"
292 "or set the netInteractive variable (see sysinstall man page)");
297 /* Now do all the screen I/O */
298 dialog_clear_norefresh();
300 /* We need a curses window */
301 if (!(ds_win = openLayoutDialog(TCP_HELPFILE, " Network Configuration ",
302 TCP_DIALOG_X, TCP_DIALOG_Y, TCP_DIALOG_WIDTH, TCP_DIALOG_HEIGHT))) {
304 msgConfirm("Cannot open TCP/IP dialog window!!");
306 return DITEM_FAILURE;
309 /* Draw interface configuration box */
310 draw_box(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 8, TCP_DIALOG_HEIGHT - 13, TCP_DIALOG_WIDTH - 17,
311 dialog_attr, border_attr);
312 wattrset(ds_win, dialog_attr);
313 sprintf(title, " Configuration for Interface %s ", devp->name);
314 mvwaddstr(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 14, title);
316 /* Some more initialisation before we go into the main input loop */
317 obj = initLayoutDialog(ds_win, layout, TCP_DIALOG_X, TCP_DIALOG_Y, &max);
320 cancelbutton = okbutton = 0;
321 while (layoutDialogLoop(ds_win, layout, &obj, &n, max, &cancelbutton, &cancel)) {
322 /* Prevent this from being irritating if user really means NO */
324 /* Insert a default value for the netmask, 0xffffff00 is
325 * the most appropriate one (entire class C, or subnetted
326 * class A/B network).
329 strcpy(netmask, "255.255.255.0");
330 RefreshStringObj(layout[LAYOUT_NETMASK].obj);
333 if (!index(hostname, '.') && domainname[0]) {
334 strcat(hostname, ".");
335 strcat(hostname, domainname);
336 RefreshStringObj(layout[LAYOUT_HOSTNAME].obj);
339 else if (((tmp = index(hostname, '.')) != NULL) && !domainname[0]) {
340 SAFE_STRCPY(domainname, tmp + 1);
341 RefreshStringObj(layout[LAYOUT_DOMAINNAME].obj);
346 if (!cancel && !verifySettings())
349 /* Clear this crap off the screen */
351 dialog_clear_norefresh();
354 /* We actually need to inform the rest of sysinstall about this
355 data now if the user hasn't selected cancel. Save the stuff
356 out to the environment via the variable_set() mechanism */
361 char temp[512], ifn[255];
365 variable_set2(VAR_HOSTNAME, hostname, use_dhcp ? 0 : 1);
366 sethostname(hostname, strlen(hostname));
369 variable_set2(VAR_DOMAINNAME, domainname, 0);
371 variable_set2(VAR_GATEWAY, gateway, use_dhcp ? 0 : 1);
373 variable_set2(VAR_NAMESERVER, nameserver, 0);
375 variable_set2(VAR_IPADDR, ipaddr, 0);
378 devp->private = (DevInfo *)safe_malloc(sizeof(DevInfo));
380 SAFE_STRCPY(di->ipaddr, ipaddr);
381 SAFE_STRCPY(di->netmask, netmask);
382 SAFE_STRCPY(di->extras, extras);
383 di->use_dhcp = use_dhcp;
385 sprintf(ifn, "%s%s", VAR_IFCONFIG, devp->name);
387 sprintf(temp, "DHCP");
389 sprintf(temp, "inet %s %s netmask %s", ipaddr, extras, netmask);
390 variable_set2(ifn, temp, 1);
391 ifaces = variable_get(VAR_INTERFACES);
393 variable_set2(VAR_INTERFACES, ifaces = "lo0", 1);
394 /* Only add it if it's not there already */
395 if (!strstr(ifaces, devp->name)) {
396 sprintf(ifn, "%s %s", devp->name, ifaces);
397 variable_set2(VAR_INTERFACES, ifn, 1);
400 configResolv(NULL); /* XXX this will do it on the MFS copy XXX */
409 static Device *NetDev;
412 netHook(dialogMenuItem *self)
416 devs = deviceFindDescr(self->prompt, self->title, DEVICE_TYPE_NETWORK);
418 if (DITEM_STATUS(tcpOpenDialog(devs[0])) != DITEM_FAILURE)
423 return devs ? DITEM_LEAVE_MENU : DITEM_FAILURE;
426 /* Get a network device */
428 tcpDeviceSelect(void)
431 Device **devs, *rval;
434 devs = deviceFind(NULL, DEVICE_TYPE_NETWORK);
435 cnt = deviceCount(devs);
439 msgConfirm("No network devices available!");
442 else if (!RunningAsInit) {
443 if (!msgYesNo("Running multi-user, assume that the network is already configured?"))
447 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS))
450 else if (variable_get(VAR_NONINTERACTIVE) && variable_get(VAR_NETWORK_DEVICE)) {
451 devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK);
452 cnt = deviceCount(devs);
454 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS))
461 menu = deviceCreateMenu(&MenuNetworkDevice, DEVICE_TYPE_NETWORK, netHook, NULL);
463 msgFatal("Unable to create network device menu! Argh!");
464 status = dmenuOpenSimple(menu, FALSE);
472 /* Do it from a menu that doesn't care about status */
474 tcpMenuSelect(dialogMenuItem *self)
478 tmp = tcpDeviceSelect();
479 if (tmp && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
481 msgConfirm("Initialization of %s device failed.", tmp->name);
482 return DITEM_SUCCESS | DITEM_RESTORE;