Deleted Added
full compact
tcpip.c (155613) tcpip.c (156123)
1/*
1/*
2 * $FreeBSD: head/usr.sbin/sysinstall/tcpip.c 155613 2006-02-13 20:49:32Z ceri $
2 * $FreeBSD: head/usr.sbin/sysinstall/tcpip.c 156123 2006-02-28 21:49:33Z jhb $
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#include <paths.h>
47
48/* The help file for the TCP/IP setup screen */
49#define TCP_HELPFILE "tcp"
50
51/* These are nasty, but they make the layout structure a lot easier ... */
52
53static char hostname[HOSTNAME_FIELD_LEN], domainname[HOSTNAME_FIELD_LEN],
54 gateway[IPADDR_FIELD_LEN], nameserver[INET6_ADDRSTRLEN];
55static int okbutton, cancelbutton;
56static char ipaddr[IPADDR_FIELD_LEN], netmask[IPADDR_FIELD_LEN], extras[EXTRAS_FIELD_LEN];
57static char ipv6addr[INET6_ADDRSTRLEN];
58
59/* What the screen size is meant to be */
60#define TCP_DIALOG_Y 0
61#define TCP_DIALOG_X 8
62#define TCP_DIALOG_WIDTH COLS - 16
63#define TCP_DIALOG_HEIGHT LINES - 2
64
65static Layout layout[] = {
66#define LAYOUT_HOSTNAME 0
67 { 1, 2, 25, HOSTNAME_FIELD_LEN - 1,
68 "Host:", "Your fully-qualified hostname, e.g. foo.bar.com",
69 hostname, STRINGOBJ, NULL },
70#define LAYOUT_DOMAINNAME 1
71 { 1, 35, 20, HOSTNAME_FIELD_LEN - 1,
72 "Domain:",
73 "The name of the domain that your machine is in, e.g. bar.com",
74 domainname, STRINGOBJ, NULL },
75#define LAYOUT_GATEWAY 2
76 { 5, 2, 18, IPADDR_FIELD_LEN - 1,
77 "IPv4 Gateway:",
78 "IPv4 address of host forwarding packets to non-local destinations",
79 gateway, STRINGOBJ, NULL },
80#define LAYOUT_NAMESERVER 3
81 { 5, 35, 18, INET6_ADDRSTRLEN - 1,
82 "Name server:", "IPv4 or IPv6 address of your local DNS server",
83 nameserver, STRINGOBJ, NULL },
84#define LAYOUT_IPADDR 4
85 { 10, 10, 18, IPADDR_FIELD_LEN - 1,
86 "IPv4 Address:",
87 "The IPv4 address to be used for this interface",
88 ipaddr, STRINGOBJ, NULL },
89#define LAYOUT_NETMASK 5
90 { 10, 35, 18, IPADDR_FIELD_LEN - 1,
91 "Netmask:",
92 "The netmask for this interface, e.g. 0xffffff00 for a class C network",
93 netmask, STRINGOBJ, NULL },
94#define LAYOUT_EXTRAS 6
95 { 14, 10, 37, HOSTNAME_FIELD_LEN - 1,
96 "Extra options to ifconfig (usually empty):",
97 "Any interface-specific options to ifconfig you would like to add",
98 extras, STRINGOBJ, NULL },
99#define LAYOUT_OKBUTTON 7
100 { 19, 15, 0, 0,
101 "OK", "Select this if you are happy with these settings",
102 &okbutton, BUTTONOBJ, NULL },
103#define LAYOUT_CANCELBUTTON 8
104 { 19, 35, 0, 0,
105 "CANCEL", "Select this if you wish to cancel this screen",
106 &cancelbutton, BUTTONOBJ, NULL },
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#include <paths.h>
47
48/* The help file for the TCP/IP setup screen */
49#define TCP_HELPFILE "tcp"
50
51/* These are nasty, but they make the layout structure a lot easier ... */
52
53static char hostname[HOSTNAME_FIELD_LEN], domainname[HOSTNAME_FIELD_LEN],
54 gateway[IPADDR_FIELD_LEN], nameserver[INET6_ADDRSTRLEN];
55static int okbutton, cancelbutton;
56static char ipaddr[IPADDR_FIELD_LEN], netmask[IPADDR_FIELD_LEN], extras[EXTRAS_FIELD_LEN];
57static char ipv6addr[INET6_ADDRSTRLEN];
58
59/* What the screen size is meant to be */
60#define TCP_DIALOG_Y 0
61#define TCP_DIALOG_X 8
62#define TCP_DIALOG_WIDTH COLS - 16
63#define TCP_DIALOG_HEIGHT LINES - 2
64
65static Layout layout[] = {
66#define LAYOUT_HOSTNAME 0
67 { 1, 2, 25, HOSTNAME_FIELD_LEN - 1,
68 "Host:", "Your fully-qualified hostname, e.g. foo.bar.com",
69 hostname, STRINGOBJ, NULL },
70#define LAYOUT_DOMAINNAME 1
71 { 1, 35, 20, HOSTNAME_FIELD_LEN - 1,
72 "Domain:",
73 "The name of the domain that your machine is in, e.g. bar.com",
74 domainname, STRINGOBJ, NULL },
75#define LAYOUT_GATEWAY 2
76 { 5, 2, 18, IPADDR_FIELD_LEN - 1,
77 "IPv4 Gateway:",
78 "IPv4 address of host forwarding packets to non-local destinations",
79 gateway, STRINGOBJ, NULL },
80#define LAYOUT_NAMESERVER 3
81 { 5, 35, 18, INET6_ADDRSTRLEN - 1,
82 "Name server:", "IPv4 or IPv6 address of your local DNS server",
83 nameserver, STRINGOBJ, NULL },
84#define LAYOUT_IPADDR 4
85 { 10, 10, 18, IPADDR_FIELD_LEN - 1,
86 "IPv4 Address:",
87 "The IPv4 address to be used for this interface",
88 ipaddr, STRINGOBJ, NULL },
89#define LAYOUT_NETMASK 5
90 { 10, 35, 18, IPADDR_FIELD_LEN - 1,
91 "Netmask:",
92 "The netmask for this interface, e.g. 0xffffff00 for a class C network",
93 netmask, STRINGOBJ, NULL },
94#define LAYOUT_EXTRAS 6
95 { 14, 10, 37, HOSTNAME_FIELD_LEN - 1,
96 "Extra options to ifconfig (usually empty):",
97 "Any interface-specific options to ifconfig you would like to add",
98 extras, STRINGOBJ, NULL },
99#define LAYOUT_OKBUTTON 7
100 { 19, 15, 0, 0,
101 "OK", "Select this if you are happy with these settings",
102 &okbutton, BUTTONOBJ, NULL },
103#define LAYOUT_CANCELBUTTON 8
104 { 19, 35, 0, 0,
105 "CANCEL", "Select this if you wish to cancel this screen",
106 &cancelbutton, BUTTONOBJ, NULL },
107 { 0 },
107 LAYOUT_END,
108};
109
110#define _validByte(b) ((b) >= 0 && (b) <= 255)
111
112/* whine */
113static void
114feepout(char *msg)
115{
116 beep();
117 msgConfirm("%s", msg);
118}
119
120/* Verify IP address integrity */
121static int
122verifyIP(char *ip, unsigned long *mask, unsigned long *out)
123{
124 long a, b, c, d;
125 char *endptr;
126
127 unsigned long parsedip;
128 unsigned long max_addr = (255 << 24) | (255 << 16) | (255 << 8) | 255;
129
130 if (ip == NULL)
131 return 0;
132 a = strtol(ip, &endptr, 10);
133 if (*endptr++ != '.')
134 return 0;
135 b = strtol(endptr, &endptr, 10);
136 if (*endptr++ != '.')
137 return 0;
138 c = strtol(endptr, &endptr, 10);
139 if (*endptr++ != '.')
140 return 0;
141 d = strtol(endptr, &endptr, 10);
142 if (*endptr != '\0')
143 return 0;
144 if (!_validByte(a) || !_validByte(b) || !_validByte(c) || !_validByte(d))
145 return 0;
146 parsedip = (a << 24) | (b << 16) | (c << 8) | d;
147 if (out)
148 *out = parsedip;
149 /*
150 * The ip address must not be network or broadcast address.
151 */
152 if (mask && ((parsedip == (parsedip & *mask)) ||
153 (parsedip == ((parsedip & *mask) + max_addr - *mask))))
154 return 0;
155 return 1;
156}
157
158static int
159verifyIP6(char *ip)
160{
161 struct addrinfo hints, *res;
162
163 memset(&hints, 0, sizeof(hints));
164 hints.ai_family = AF_INET6;
165 hints.ai_socktype = SOCK_STREAM;
166 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
167 if (getaddrinfo(ip, NULL, &hints, &res) == 0) {
168 freeaddrinfo(res);
169 return 1;
170 }
171 return 0;
172}
173
174/* Verify IPv4 netmask as being well-formed as
175 a 0x or AAA.BBB.CCC.DDD mask */
176static int
177verifyNetmask(const char *netmask, unsigned long *out)
178{
179 unsigned long mask;
108};
109
110#define _validByte(b) ((b) >= 0 && (b) <= 255)
111
112/* whine */
113static void
114feepout(char *msg)
115{
116 beep();
117 msgConfirm("%s", msg);
118}
119
120/* Verify IP address integrity */
121static int
122verifyIP(char *ip, unsigned long *mask, unsigned long *out)
123{
124 long a, b, c, d;
125 char *endptr;
126
127 unsigned long parsedip;
128 unsigned long max_addr = (255 << 24) | (255 << 16) | (255 << 8) | 255;
129
130 if (ip == NULL)
131 return 0;
132 a = strtol(ip, &endptr, 10);
133 if (*endptr++ != '.')
134 return 0;
135 b = strtol(endptr, &endptr, 10);
136 if (*endptr++ != '.')
137 return 0;
138 c = strtol(endptr, &endptr, 10);
139 if (*endptr++ != '.')
140 return 0;
141 d = strtol(endptr, &endptr, 10);
142 if (*endptr != '\0')
143 return 0;
144 if (!_validByte(a) || !_validByte(b) || !_validByte(c) || !_validByte(d))
145 return 0;
146 parsedip = (a << 24) | (b << 16) | (c << 8) | d;
147 if (out)
148 *out = parsedip;
149 /*
150 * The ip address must not be network or broadcast address.
151 */
152 if (mask && ((parsedip == (parsedip & *mask)) ||
153 (parsedip == ((parsedip & *mask) + max_addr - *mask))))
154 return 0;
155 return 1;
156}
157
158static int
159verifyIP6(char *ip)
160{
161 struct addrinfo hints, *res;
162
163 memset(&hints, 0, sizeof(hints));
164 hints.ai_family = AF_INET6;
165 hints.ai_socktype = SOCK_STREAM;
166 hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
167 if (getaddrinfo(ip, NULL, &hints, &res) == 0) {
168 freeaddrinfo(res);
169 return 1;
170 }
171 return 0;
172}
173
174/* Verify IPv4 netmask as being well-formed as
175 a 0x or AAA.BBB.CCC.DDD mask */
176static int
177verifyNetmask(const char *netmask, unsigned long *out)
178{
179 unsigned long mask;
180 unsigned long tmp;
180 long tmp;
181 char *endptr;
182
183 if (netmask[0] == '0' && (netmask[1] == 'x' || netmask[1] == 'X')) {
184 /* Parse out hex mask */
185 mask = strtoul(netmask, &endptr, 0);
186 if (*endptr != '\0')
187 return 0;
188 } else {
189 /* Parse out quad decimal mask */
181 char *endptr;
182
183 if (netmask[0] == '0' && (netmask[1] == 'x' || netmask[1] == 'X')) {
184 /* Parse out hex mask */
185 mask = strtoul(netmask, &endptr, 0);
186 if (*endptr != '\0')
187 return 0;
188 } else {
189 /* Parse out quad decimal mask */
190 mask = strtoul(netmask, &endptr, 10);
191 if (!_validByte(mask) || *endptr++ != '.')
190 tmp = strtoul(netmask, &endptr, 10);
191 if (!_validByte(tmp) || *endptr++ != '.')
192 return 0;
192 return 0;
193 mask = tmp;
193 tmp = strtoul(endptr, &endptr, 10);
194 if (!_validByte(tmp) || *endptr++ != '.')
195 return 0;
196 mask = (mask << 8) + tmp;
197 tmp = strtoul(endptr, &endptr, 10);
198 if (!_validByte(tmp) || *endptr++ != '.')
199 return 0;
200 mask = (mask << 8) + tmp;
201 tmp = strtoul(endptr, &endptr, 10);
202 if (!_validByte(tmp) || *endptr++ != '\0')
203 return 0;
204 mask = (mask << 8) + tmp;
205 }
206 /* Verify that we have a continous netmask */
207 if ((((-mask & mask) - 1) | mask) != 0xffffffff)
208 return 0;
209 if (out)
210 *out = mask;
211 return 1;
212}
213
214static int
215verifyGW(char *gw, unsigned long *ip, unsigned long *mask)
216{
217 unsigned long parsedgw;
218
219 if (!verifyIP(gw, mask, &parsedgw))
220 return 0;
221 /* Gateway needs to be within the set of IPs reachable through the
222 interface */
223 if (ip && mask && ((parsedgw & *mask) != (*ip & *mask)))
224 return 0;
225 return 1;
226}
227
228/* Check for the settings on the screen - the per-interface stuff is
229 moved to the main handling code now to do it on the fly - sigh */
230static int
231verifySettings(void)
232{
233 unsigned long parsedip;
234 unsigned long parsednetmask;
235
236 if (!hostname[0])
237 feepout("Must specify a host name of some sort!");
238 else if (netmask[0] && !verifyNetmask(netmask, &parsednetmask))
239 feepout("Invalid netmask value");
240 else if (nameserver[0] && !verifyIP(nameserver, NULL, NULL) &&
241 !verifyIP6(nameserver))
242 feepout("Invalid name server IP address specified");
243 else if (ipaddr[0] && !verifyIP(ipaddr, &parsednetmask, &parsedip))
244 feepout("Invalid IPv4 address");
245 else if (gateway[0] && strcmp(gateway, "NO") &&
246 !verifyGW(gateway, ipaddr[0] ? &parsedip : NULL,
247 netmask[0] ? &parsednetmask : NULL))
248 feepout("Invalid gateway IPv4 address specified");
249 else
250 return 1;
251 return 0;
252}
253
254static void
255dhcpGetInfo(Device *devp)
256{
257 char leasefile[PATH_MAX];
258
259 snprintf(leasefile, sizeof(leasefile), "%sdhclient.leases.%s",
260 _PATH_VARDB, devp->name);
261 /* If it fails, do it the old-fashioned way */
262 if (dhcpParseLeases(leasefile, hostname, domainname,
263 nameserver, ipaddr, gateway, netmask) == -1) {
264 FILE *ifp;
265 char *cp, cmd[256], data[2048];
266 int i, j;
267
268 /* Bah, now we have to kludge getting the information from ifconfig */
269 snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name);
270 ifp = popen(cmd, "r");
271 if (ifp) {
272 j = fread(data, 1, sizeof(data), ifp);
273 fclose(ifp);
274 if (j < 0) /* paranoia */
275 j = 0;
276 data[j] = '\0';
277 if (isDebug())
278 msgDebug("DHCP configured interface returns %s\n", data);
279 /* XXX This is gross as it assumes a certain ordering to
280 ifconfig's output! XXX */
281 if ((cp = strstr(data, "inet ")) != NULL) {
282 i = 0;
283 cp += 5; /* move over keyword */
284 while (*cp != ' ')
285 ipaddr[i++] = *(cp++);
286 ipaddr[i] = '\0';
287 if (!strncmp(++cp, "netmask", 7)) {
288 i = 0;
289 cp += 8;
290 while (*cp != ' ')
291 netmask[i++] = *(cp++);
292 netmask[i] = '\0';
293 }
294 }
295 }
296 }
297
298 /* If we didn't get a name server value, hunt for it in resolv.conf */
299 if (!nameserver[0] && file_readable("/etc/resolv.conf"))
300 configEnvironmentResolv("/etc/resolv.conf");
301 if (hostname[0])
302 variable_set2(VAR_HOSTNAME, hostname, 0);
303}
304
305static void
306rtsolGetInfo(Device *devp)
307{
308 FILE *ifp;
309 char *cp, cmd[256], data[2048];
310 int i;
311
312 snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name);
313 if ((ifp = popen(cmd, "r")) == NULL)
314 return;
315 while (fgets(data, sizeof(data), ifp) != NULL) {
316 if (isDebug())
317 msgDebug("RTSOL configured interface returns %s\n", data);
318 if ((cp = strstr(data, "inet6 ")) != NULL) {
319 cp += 6; /* move over keyword */
320 if (strncmp(cp, "fe80:", 5)) {
321 i = 0;
322 while (*cp != ' ')
323 ipv6addr[i++] = *(cp++);
324 ipv6addr[i] = '\0';
325 }
326 }
327 }
328 fclose(ifp);
329}
330
331/* This is it - how to get TCP setup values */
332int
333tcpOpenDialog(Device *devp)
334{
335 WINDOW *ds_win, *save = NULL;
336 ComposeObj *obj = NULL;
337 int n = 0, filled = 0, cancel = FALSE;
338 int max, ret = DITEM_SUCCESS;
339 int use_dhcp = FALSE;
340 int use_rtsol = FALSE;
341 char *tmp;
342 char title[80];
343
344 save = savescr();
345 /* Initialise vars from previous device values */
346 if (devp->private) {
347 DevInfo *di = (DevInfo *)devp->private;
348
349 SAFE_STRCPY(ipaddr, di->ipaddr);
350 SAFE_STRCPY(netmask, di->netmask);
351 SAFE_STRCPY(extras, di->extras);
352 use_dhcp = di->use_dhcp;
353 use_rtsol = di->use_rtsol;
354 }
355 else { /* See if there are any defaults */
356 char *cp;
357 char *old_interactive = NULL;
358
359 /*
360 * This is a hack so that the dialogs below are interactive in a
361 * script if we have requested interactive behavior.
362 */
363 if (variable_get(VAR_NONINTERACTIVE) &&
364 variable_get(VAR_NETINTERACTIVE)) {
365 old_interactive = strdup(VAR_NONINTERACTIVE);
366 variable_unset(VAR_NONINTERACTIVE);
367 }
368
369
370 /*
371 * Try a RTSOL scan if such behavior is desired.
372 * If the variable was configured and is YES, do it.
373 * If it was configured to anything else, treat it as NO.
374 * Otherwise, ask the question interactively.
375 */
376 if (!variable_get(VAR_NO_INET6) &&
377 (!variable_cmp(VAR_TRY_RTSOL, "YES") ||
378 (variable_get(VAR_TRY_RTSOL)==0 && !msgNoYes("Do you want to try IPv6 configuration of the interface?")))) {
379 int i;
380 size_t len;
381
382 i = 0;
383 sysctlbyname("net.inet6.ip6.forwarding", NULL, 0, &i, sizeof(i));
384 i = 1;
385 sysctlbyname("net.inet6.ip6.accept_rtadv", NULL, 0, &i, sizeof(i));
386 vsystem("ifconfig %s up", devp->name);
387 len = sizeof(i);
388 sysctlbyname("net.inet6.ip6.dad_count", &i, &len, NULL, 0);
389 sleep(i + 1);
390 Mkdir("/var/run");
391 msgNotify("Scanning for RA servers...");
392 if (0 == vsystem("rtsol %s", devp->name)) {
393 len = sizeof(i);
394 sysctlbyname("net.inet6.ip6.dad_count", &i, &len, NULL, 0);
395 sleep(i + 1);
396 rtsolGetInfo(devp);
397 use_rtsol = TRUE;
398 } else
399 use_rtsol = FALSE;
400 }
401
402
403 /*
404 * First try a DHCP scan if such behavior is desired.
405 * If the variable was configured and is YES, do it.
406 * If it was configured to anything else, treat it as NO.
407 * Otherwise, ask the question interactively.
408 */
409 if (!variable_cmp(VAR_TRY_DHCP, "YES") ||
410 (variable_get(VAR_TRY_DHCP)==0 && !msgNoYes("Do you want to try DHCP configuration of the interface?"))) {
411 Mkdir("/var/db");
412 Mkdir("/var/run");
413 Mkdir("/tmp");
414 msgNotify("Scanning for DHCP servers...");
415 /* XXX clear any existing lease */
416 /* XXX limit protocol to N tries */
417 if (0 == vsystem("dhclient %s", devp->name)) {
418 dhcpGetInfo(devp);
419 use_dhcp = TRUE;
420 }
421 else
422 use_dhcp = FALSE;
423 }
424
425 /* Restore old VAR_NONINTERACTIVE if needed. */
426 if (old_interactive != NULL) {
427 variable_set2(VAR_NONINTERACTIVE, old_interactive, 0);
428 free(old_interactive);
429 }
430
431 /* Special hack so it doesn't show up oddly in the tcpip setup menu */
432 if (!strcmp(gateway, "NO"))
433 gateway[0] = '\0';
434
435 /* Get old IP address from variable space, if available */
436 if (!ipaddr[0]) {
437 if ((cp = variable_get(VAR_IPADDR)) != NULL)
438 SAFE_STRCPY(ipaddr, cp);
439 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_IPADDR))) != NULL)
440 SAFE_STRCPY(ipaddr, cp);
441 }
442
443 /* Get old netmask from variable space, if available */
444 if (!netmask[0]) {
445 if ((cp = variable_get(VAR_NETMASK)) != NULL)
446 SAFE_STRCPY(netmask, cp);
447 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_NETMASK))) != NULL)
448 SAFE_STRCPY(netmask, cp);
449 }
450
451 /* Get old extras string from variable space, if available */
452 if (!extras[0]) {
453 if ((cp = variable_get(VAR_EXTRAS)) != NULL)
454 SAFE_STRCPY(extras, cp);
455 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_EXTRAS))) != NULL)
456 SAFE_STRCPY(extras, cp);
457 }
458 }
459
460 /* Look up values already recorded with the system, or blank the string variables ready to accept some new data */
461 if (!hostname[0]) {
462 tmp = variable_get(VAR_HOSTNAME);
463 if (tmp)
464 SAFE_STRCPY(hostname, tmp);
465 }
466 if (!domainname[0]) {
467 tmp = variable_get(VAR_DOMAINNAME);
468 if (tmp)
469 SAFE_STRCPY(domainname, tmp);
470 }
471 if (!gateway[0]) {
472 tmp = variable_get(VAR_GATEWAY);
473 if (tmp && strcmp(tmp, "NO"))
474 SAFE_STRCPY(gateway, tmp);
475 }
476 if (!nameserver[0]) {
477 tmp = variable_get(VAR_NAMESERVER);
478 if (tmp)
479 SAFE_STRCPY(nameserver, tmp);
480 }
481
482 /* If non-interactive, jump straight over the dialog crap and into config section */
483 if (variable_get(VAR_NONINTERACTIVE) &&
484 !variable_get(VAR_NETINTERACTIVE)) {
485 if (!hostname[0])
486 msgConfirm("WARNING: hostname variable not set and is a non-optional\n"
487 "parameter. Please add this to your installation script\n"
488 "or set the netInteractive variable (see sysinstall man page)");
489 else
490 goto netconfig;
491 }
492
493 /* Now do all the screen I/O */
494 dialog_clear_norefresh();
495
496 /* Modify the help line for PLIP config */
497 if (!strncmp(devp->name, "plip", 4))
498 layout[LAYOUT_EXTRAS].help =
499 "For PLIP configuration, you must enter the peer's IP address here.";
500
501 /* We need a curses window */
502 tmp = " Network Configuration ";
503 if (ipv6addr[0])
504 tmp = string_concat(tmp, "(IPv6 ready) ");
505 if (!(ds_win = openLayoutDialog(TCP_HELPFILE, tmp,
506 TCP_DIALOG_X, TCP_DIALOG_Y, TCP_DIALOG_WIDTH, TCP_DIALOG_HEIGHT))) {
507 beep();
508 msgConfirm("Cannot open TCP/IP dialog window!!");
509 restorescr(save);
510 return DITEM_FAILURE;
511 }
512
513 /* Draw interface configuration box */
514 draw_box(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 8, TCP_DIALOG_HEIGHT - 13, TCP_DIALOG_WIDTH - 17,
515 dialog_attr, border_attr);
516 wattrset(ds_win, dialog_attr);
517 sprintf(title, " Configuration for Interface %s ", devp->name);
518 mvwaddstr(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 14, title);
519
520 /* Some more initialisation before we go into the main input loop */
521 obj = initLayoutDialog(ds_win, layout, TCP_DIALOG_X, TCP_DIALOG_Y, &max);
522
523reenter:
524 cancelbutton = okbutton = 0;
525 while (layoutDialogLoop(ds_win, layout, &obj, &n, max, &cancelbutton, &cancel)) {
526 /* Prevent this from being irritating if user really means NO */
527 if (filled < 3) {
528 /* Insert a default value for the netmask, 0xffffff00 is
529 * the most appropriate one (entire class C, or subnetted
530 * class A/B network).
531 */
532 if (!netmask[0]) {
533 strcpy(netmask, "255.255.255.0");
534 RefreshStringObj(layout[LAYOUT_NETMASK].obj);
535 ++filled;
536 }
537 if (!index(hostname, '.') && domainname[0]) {
538 strcat(hostname, ".");
539 strcat(hostname, domainname);
540 RefreshStringObj(layout[LAYOUT_HOSTNAME].obj);
541 ++filled;
542 }
543 else if (((tmp = index(hostname, '.')) != NULL) && !domainname[0]) {
544 SAFE_STRCPY(domainname, tmp + 1);
545 RefreshStringObj(layout[LAYOUT_DOMAINNAME].obj);
546 ++filled;
547 }
548 }
549 }
550 if (!cancel && !verifySettings())
551 goto reenter;
552
553 /* Clear this crap off the screen */
554 delwin(ds_win);
555 dialog_clear_norefresh();
556 use_helpfile(NULL);
557
558 /* We actually need to inform the rest of sysinstall about this
559 data now if the user hasn't selected cancel. Save the stuff
560 out to the environment via the variable_set() mechanism */
561
562netconfig:
563 if (!cancel) {
564 DevInfo *di;
565 char temp[512], ifn[255];
566#ifdef PCCARD_ARCH
567 char *pccard;
568#endif
569 int ipv4_enable = FALSE;
570
571 if (hostname[0]) {
572 variable_set2(VAR_HOSTNAME, hostname, 1);
573 sethostname(hostname, strlen(hostname));
574 }
575 if (domainname[0])
576 variable_set2(VAR_DOMAINNAME, domainname, 0);
577 if (gateway[0])
578 variable_set2(VAR_GATEWAY, gateway, use_dhcp ? 0 : 1);
579 if (nameserver[0])
580 variable_set2(VAR_NAMESERVER, nameserver, 0);
581 if (ipaddr[0])
582 variable_set2(VAR_IPADDR, ipaddr, 0);
583 if (ipv6addr[0])
584 variable_set2(VAR_IPV6ADDR, ipv6addr, 0);
585
586 if (!devp->private)
587 devp->private = (DevInfo *)safe_malloc(sizeof(DevInfo));
588 di = devp->private;
589 SAFE_STRCPY(di->ipaddr, ipaddr);
590 SAFE_STRCPY(di->netmask, netmask);
591 SAFE_STRCPY(di->extras, extras);
592 di->use_dhcp = use_dhcp;
593 di->use_rtsol = use_rtsol;
594
595 if (use_dhcp || ipaddr[0])
596 ipv4_enable = TRUE;
597 if (ipv4_enable) {
598 sprintf(ifn, "%s%s", VAR_IFCONFIG, devp->name);
599 if (use_dhcp) {
600 if (strlen(extras) > 0)
601 sprintf(temp, "DHCP %s", extras);
602 else
603 sprintf(temp, "DHCP");
604 } else
605 sprintf(temp, "inet %s %s netmask %s",
606 ipaddr, extras, netmask);
607 variable_set2(ifn, temp, 1);
608 }
609#ifdef PCCARD_ARCH
610 pccard = variable_get("_pccard_install");
611 if (pccard && strcmp(pccard, "YES") == 0 && ipv4_enable) {
612 variable_set2("pccard_ifconfig", temp, 1);
613 }
614#endif
615 if (use_rtsol)
616 variable_set2(VAR_IPV6_ENABLE, "YES", 1);
617 if (!use_dhcp)
618 configResolv(NULL); /* XXX this will do it on the MFS copy XXX */
619 ret = DITEM_SUCCESS;
620 }
621 else
622 ret = DITEM_FAILURE;
623 restorescr(save);
624 return ret;
625}
626
627static Device *NetDev;
628
629static int
630netHook(dialogMenuItem *self)
631{
632 Device **devs;
633
634 devs = deviceFindDescr(self->prompt, self->title, DEVICE_TYPE_NETWORK);
635 if (devs) {
636 if (DITEM_STATUS(tcpOpenDialog(devs[0])) != DITEM_FAILURE)
637 NetDev = devs[0];
638 else
639 NetDev = NULL;
640 }
641 return devs ? DITEM_LEAVE_MENU : DITEM_FAILURE;
642}
643
644/* Get a network device */
645Device *
646tcpDeviceSelect(void)
647{
648 DMenu *menu;
649 Device **devs, *rval;
650 int cnt;
651
652 devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK);
653 cnt = deviceCount(devs);
654 rval = NULL;
655
656 if (!cnt) {
657 msgConfirm("No network devices available!");
658 return NULL;
659 }
660 else if ((!RunningAsInit) && (variable_check("NETWORK_CONFIGURED=NO") != TRUE)) {
661 if (!msgYesNo("Running multi-user, assume that the network is already configured?"))
662 return devs[0];
663 }
664 if (cnt == 1) {
665 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS))
666 rval = devs[0];
667 }
668 else if (variable_get(VAR_NONINTERACTIVE) && variable_get(VAR_NETWORK_DEVICE)) {
669 devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK);
670 cnt = deviceCount(devs);
671 if (cnt) {
672 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS))
673 rval = devs[0];
674 }
675 }
676 else {
677 int status;
678
679 menu = deviceCreateMenu(&MenuNetworkDevice, DEVICE_TYPE_NETWORK, netHook, NULL);
680 if (!menu)
681 msgFatal("Unable to create network device menu! Argh!");
682 status = dmenuOpenSimple(menu, FALSE);
683 free(menu);
684 if (status)
685 rval = NetDev;
686 }
687 return rval;
688}
689
690/* Do it from a menu that doesn't care about status */
691int
692tcpMenuSelect(dialogMenuItem *self)
693{
694 Device *tmp;
695 WINDOW *save;
696
697 variable_set("NETWORK_CONFIGURED=NO",0);
698 tmp = tcpDeviceSelect();
699 variable_unset("NETWORK_CONFIGURED");
700 save = savescr();
701 if (tmp && tmp->private && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
702 if (!DEVICE_INIT(tmp))
703 msgConfirm("Initialization of %s device failed.", tmp->name);
704 restorescr(save);
705 return DITEM_SUCCESS;
706}
194 tmp = strtoul(endptr, &endptr, 10);
195 if (!_validByte(tmp) || *endptr++ != '.')
196 return 0;
197 mask = (mask << 8) + tmp;
198 tmp = strtoul(endptr, &endptr, 10);
199 if (!_validByte(tmp) || *endptr++ != '.')
200 return 0;
201 mask = (mask << 8) + tmp;
202 tmp = strtoul(endptr, &endptr, 10);
203 if (!_validByte(tmp) || *endptr++ != '\0')
204 return 0;
205 mask = (mask << 8) + tmp;
206 }
207 /* Verify that we have a continous netmask */
208 if ((((-mask & mask) - 1) | mask) != 0xffffffff)
209 return 0;
210 if (out)
211 *out = mask;
212 return 1;
213}
214
215static int
216verifyGW(char *gw, unsigned long *ip, unsigned long *mask)
217{
218 unsigned long parsedgw;
219
220 if (!verifyIP(gw, mask, &parsedgw))
221 return 0;
222 /* Gateway needs to be within the set of IPs reachable through the
223 interface */
224 if (ip && mask && ((parsedgw & *mask) != (*ip & *mask)))
225 return 0;
226 return 1;
227}
228
229/* Check for the settings on the screen - the per-interface stuff is
230 moved to the main handling code now to do it on the fly - sigh */
231static int
232verifySettings(void)
233{
234 unsigned long parsedip;
235 unsigned long parsednetmask;
236
237 if (!hostname[0])
238 feepout("Must specify a host name of some sort!");
239 else if (netmask[0] && !verifyNetmask(netmask, &parsednetmask))
240 feepout("Invalid netmask value");
241 else if (nameserver[0] && !verifyIP(nameserver, NULL, NULL) &&
242 !verifyIP6(nameserver))
243 feepout("Invalid name server IP address specified");
244 else if (ipaddr[0] && !verifyIP(ipaddr, &parsednetmask, &parsedip))
245 feepout("Invalid IPv4 address");
246 else if (gateway[0] && strcmp(gateway, "NO") &&
247 !verifyGW(gateway, ipaddr[0] ? &parsedip : NULL,
248 netmask[0] ? &parsednetmask : NULL))
249 feepout("Invalid gateway IPv4 address specified");
250 else
251 return 1;
252 return 0;
253}
254
255static void
256dhcpGetInfo(Device *devp)
257{
258 char leasefile[PATH_MAX];
259
260 snprintf(leasefile, sizeof(leasefile), "%sdhclient.leases.%s",
261 _PATH_VARDB, devp->name);
262 /* If it fails, do it the old-fashioned way */
263 if (dhcpParseLeases(leasefile, hostname, domainname,
264 nameserver, ipaddr, gateway, netmask) == -1) {
265 FILE *ifp;
266 char *cp, cmd[256], data[2048];
267 int i, j;
268
269 /* Bah, now we have to kludge getting the information from ifconfig */
270 snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name);
271 ifp = popen(cmd, "r");
272 if (ifp) {
273 j = fread(data, 1, sizeof(data), ifp);
274 fclose(ifp);
275 if (j < 0) /* paranoia */
276 j = 0;
277 data[j] = '\0';
278 if (isDebug())
279 msgDebug("DHCP configured interface returns %s\n", data);
280 /* XXX This is gross as it assumes a certain ordering to
281 ifconfig's output! XXX */
282 if ((cp = strstr(data, "inet ")) != NULL) {
283 i = 0;
284 cp += 5; /* move over keyword */
285 while (*cp != ' ')
286 ipaddr[i++] = *(cp++);
287 ipaddr[i] = '\0';
288 if (!strncmp(++cp, "netmask", 7)) {
289 i = 0;
290 cp += 8;
291 while (*cp != ' ')
292 netmask[i++] = *(cp++);
293 netmask[i] = '\0';
294 }
295 }
296 }
297 }
298
299 /* If we didn't get a name server value, hunt for it in resolv.conf */
300 if (!nameserver[0] && file_readable("/etc/resolv.conf"))
301 configEnvironmentResolv("/etc/resolv.conf");
302 if (hostname[0])
303 variable_set2(VAR_HOSTNAME, hostname, 0);
304}
305
306static void
307rtsolGetInfo(Device *devp)
308{
309 FILE *ifp;
310 char *cp, cmd[256], data[2048];
311 int i;
312
313 snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name);
314 if ((ifp = popen(cmd, "r")) == NULL)
315 return;
316 while (fgets(data, sizeof(data), ifp) != NULL) {
317 if (isDebug())
318 msgDebug("RTSOL configured interface returns %s\n", data);
319 if ((cp = strstr(data, "inet6 ")) != NULL) {
320 cp += 6; /* move over keyword */
321 if (strncmp(cp, "fe80:", 5)) {
322 i = 0;
323 while (*cp != ' ')
324 ipv6addr[i++] = *(cp++);
325 ipv6addr[i] = '\0';
326 }
327 }
328 }
329 fclose(ifp);
330}
331
332/* This is it - how to get TCP setup values */
333int
334tcpOpenDialog(Device *devp)
335{
336 WINDOW *ds_win, *save = NULL;
337 ComposeObj *obj = NULL;
338 int n = 0, filled = 0, cancel = FALSE;
339 int max, ret = DITEM_SUCCESS;
340 int use_dhcp = FALSE;
341 int use_rtsol = FALSE;
342 char *tmp;
343 char title[80];
344
345 save = savescr();
346 /* Initialise vars from previous device values */
347 if (devp->private) {
348 DevInfo *di = (DevInfo *)devp->private;
349
350 SAFE_STRCPY(ipaddr, di->ipaddr);
351 SAFE_STRCPY(netmask, di->netmask);
352 SAFE_STRCPY(extras, di->extras);
353 use_dhcp = di->use_dhcp;
354 use_rtsol = di->use_rtsol;
355 }
356 else { /* See if there are any defaults */
357 char *cp;
358 char *old_interactive = NULL;
359
360 /*
361 * This is a hack so that the dialogs below are interactive in a
362 * script if we have requested interactive behavior.
363 */
364 if (variable_get(VAR_NONINTERACTIVE) &&
365 variable_get(VAR_NETINTERACTIVE)) {
366 old_interactive = strdup(VAR_NONINTERACTIVE);
367 variable_unset(VAR_NONINTERACTIVE);
368 }
369
370
371 /*
372 * Try a RTSOL scan if such behavior is desired.
373 * If the variable was configured and is YES, do it.
374 * If it was configured to anything else, treat it as NO.
375 * Otherwise, ask the question interactively.
376 */
377 if (!variable_get(VAR_NO_INET6) &&
378 (!variable_cmp(VAR_TRY_RTSOL, "YES") ||
379 (variable_get(VAR_TRY_RTSOL)==0 && !msgNoYes("Do you want to try IPv6 configuration of the interface?")))) {
380 int i;
381 size_t len;
382
383 i = 0;
384 sysctlbyname("net.inet6.ip6.forwarding", NULL, 0, &i, sizeof(i));
385 i = 1;
386 sysctlbyname("net.inet6.ip6.accept_rtadv", NULL, 0, &i, sizeof(i));
387 vsystem("ifconfig %s up", devp->name);
388 len = sizeof(i);
389 sysctlbyname("net.inet6.ip6.dad_count", &i, &len, NULL, 0);
390 sleep(i + 1);
391 Mkdir("/var/run");
392 msgNotify("Scanning for RA servers...");
393 if (0 == vsystem("rtsol %s", devp->name)) {
394 len = sizeof(i);
395 sysctlbyname("net.inet6.ip6.dad_count", &i, &len, NULL, 0);
396 sleep(i + 1);
397 rtsolGetInfo(devp);
398 use_rtsol = TRUE;
399 } else
400 use_rtsol = FALSE;
401 }
402
403
404 /*
405 * First try a DHCP scan if such behavior is desired.
406 * If the variable was configured and is YES, do it.
407 * If it was configured to anything else, treat it as NO.
408 * Otherwise, ask the question interactively.
409 */
410 if (!variable_cmp(VAR_TRY_DHCP, "YES") ||
411 (variable_get(VAR_TRY_DHCP)==0 && !msgNoYes("Do you want to try DHCP configuration of the interface?"))) {
412 Mkdir("/var/db");
413 Mkdir("/var/run");
414 Mkdir("/tmp");
415 msgNotify("Scanning for DHCP servers...");
416 /* XXX clear any existing lease */
417 /* XXX limit protocol to N tries */
418 if (0 == vsystem("dhclient %s", devp->name)) {
419 dhcpGetInfo(devp);
420 use_dhcp = TRUE;
421 }
422 else
423 use_dhcp = FALSE;
424 }
425
426 /* Restore old VAR_NONINTERACTIVE if needed. */
427 if (old_interactive != NULL) {
428 variable_set2(VAR_NONINTERACTIVE, old_interactive, 0);
429 free(old_interactive);
430 }
431
432 /* Special hack so it doesn't show up oddly in the tcpip setup menu */
433 if (!strcmp(gateway, "NO"))
434 gateway[0] = '\0';
435
436 /* Get old IP address from variable space, if available */
437 if (!ipaddr[0]) {
438 if ((cp = variable_get(VAR_IPADDR)) != NULL)
439 SAFE_STRCPY(ipaddr, cp);
440 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_IPADDR))) != NULL)
441 SAFE_STRCPY(ipaddr, cp);
442 }
443
444 /* Get old netmask from variable space, if available */
445 if (!netmask[0]) {
446 if ((cp = variable_get(VAR_NETMASK)) != NULL)
447 SAFE_STRCPY(netmask, cp);
448 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_NETMASK))) != NULL)
449 SAFE_STRCPY(netmask, cp);
450 }
451
452 /* Get old extras string from variable space, if available */
453 if (!extras[0]) {
454 if ((cp = variable_get(VAR_EXTRAS)) != NULL)
455 SAFE_STRCPY(extras, cp);
456 else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_EXTRAS))) != NULL)
457 SAFE_STRCPY(extras, cp);
458 }
459 }
460
461 /* Look up values already recorded with the system, or blank the string variables ready to accept some new data */
462 if (!hostname[0]) {
463 tmp = variable_get(VAR_HOSTNAME);
464 if (tmp)
465 SAFE_STRCPY(hostname, tmp);
466 }
467 if (!domainname[0]) {
468 tmp = variable_get(VAR_DOMAINNAME);
469 if (tmp)
470 SAFE_STRCPY(domainname, tmp);
471 }
472 if (!gateway[0]) {
473 tmp = variable_get(VAR_GATEWAY);
474 if (tmp && strcmp(tmp, "NO"))
475 SAFE_STRCPY(gateway, tmp);
476 }
477 if (!nameserver[0]) {
478 tmp = variable_get(VAR_NAMESERVER);
479 if (tmp)
480 SAFE_STRCPY(nameserver, tmp);
481 }
482
483 /* If non-interactive, jump straight over the dialog crap and into config section */
484 if (variable_get(VAR_NONINTERACTIVE) &&
485 !variable_get(VAR_NETINTERACTIVE)) {
486 if (!hostname[0])
487 msgConfirm("WARNING: hostname variable not set and is a non-optional\n"
488 "parameter. Please add this to your installation script\n"
489 "or set the netInteractive variable (see sysinstall man page)");
490 else
491 goto netconfig;
492 }
493
494 /* Now do all the screen I/O */
495 dialog_clear_norefresh();
496
497 /* Modify the help line for PLIP config */
498 if (!strncmp(devp->name, "plip", 4))
499 layout[LAYOUT_EXTRAS].help =
500 "For PLIP configuration, you must enter the peer's IP address here.";
501
502 /* We need a curses window */
503 tmp = " Network Configuration ";
504 if (ipv6addr[0])
505 tmp = string_concat(tmp, "(IPv6 ready) ");
506 if (!(ds_win = openLayoutDialog(TCP_HELPFILE, tmp,
507 TCP_DIALOG_X, TCP_DIALOG_Y, TCP_DIALOG_WIDTH, TCP_DIALOG_HEIGHT))) {
508 beep();
509 msgConfirm("Cannot open TCP/IP dialog window!!");
510 restorescr(save);
511 return DITEM_FAILURE;
512 }
513
514 /* Draw interface configuration box */
515 draw_box(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 8, TCP_DIALOG_HEIGHT - 13, TCP_DIALOG_WIDTH - 17,
516 dialog_attr, border_attr);
517 wattrset(ds_win, dialog_attr);
518 sprintf(title, " Configuration for Interface %s ", devp->name);
519 mvwaddstr(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 14, title);
520
521 /* Some more initialisation before we go into the main input loop */
522 obj = initLayoutDialog(ds_win, layout, TCP_DIALOG_X, TCP_DIALOG_Y, &max);
523
524reenter:
525 cancelbutton = okbutton = 0;
526 while (layoutDialogLoop(ds_win, layout, &obj, &n, max, &cancelbutton, &cancel)) {
527 /* Prevent this from being irritating if user really means NO */
528 if (filled < 3) {
529 /* Insert a default value for the netmask, 0xffffff00 is
530 * the most appropriate one (entire class C, or subnetted
531 * class A/B network).
532 */
533 if (!netmask[0]) {
534 strcpy(netmask, "255.255.255.0");
535 RefreshStringObj(layout[LAYOUT_NETMASK].obj);
536 ++filled;
537 }
538 if (!index(hostname, '.') && domainname[0]) {
539 strcat(hostname, ".");
540 strcat(hostname, domainname);
541 RefreshStringObj(layout[LAYOUT_HOSTNAME].obj);
542 ++filled;
543 }
544 else if (((tmp = index(hostname, '.')) != NULL) && !domainname[0]) {
545 SAFE_STRCPY(domainname, tmp + 1);
546 RefreshStringObj(layout[LAYOUT_DOMAINNAME].obj);
547 ++filled;
548 }
549 }
550 }
551 if (!cancel && !verifySettings())
552 goto reenter;
553
554 /* Clear this crap off the screen */
555 delwin(ds_win);
556 dialog_clear_norefresh();
557 use_helpfile(NULL);
558
559 /* We actually need to inform the rest of sysinstall about this
560 data now if the user hasn't selected cancel. Save the stuff
561 out to the environment via the variable_set() mechanism */
562
563netconfig:
564 if (!cancel) {
565 DevInfo *di;
566 char temp[512], ifn[255];
567#ifdef PCCARD_ARCH
568 char *pccard;
569#endif
570 int ipv4_enable = FALSE;
571
572 if (hostname[0]) {
573 variable_set2(VAR_HOSTNAME, hostname, 1);
574 sethostname(hostname, strlen(hostname));
575 }
576 if (domainname[0])
577 variable_set2(VAR_DOMAINNAME, domainname, 0);
578 if (gateway[0])
579 variable_set2(VAR_GATEWAY, gateway, use_dhcp ? 0 : 1);
580 if (nameserver[0])
581 variable_set2(VAR_NAMESERVER, nameserver, 0);
582 if (ipaddr[0])
583 variable_set2(VAR_IPADDR, ipaddr, 0);
584 if (ipv6addr[0])
585 variable_set2(VAR_IPV6ADDR, ipv6addr, 0);
586
587 if (!devp->private)
588 devp->private = (DevInfo *)safe_malloc(sizeof(DevInfo));
589 di = devp->private;
590 SAFE_STRCPY(di->ipaddr, ipaddr);
591 SAFE_STRCPY(di->netmask, netmask);
592 SAFE_STRCPY(di->extras, extras);
593 di->use_dhcp = use_dhcp;
594 di->use_rtsol = use_rtsol;
595
596 if (use_dhcp || ipaddr[0])
597 ipv4_enable = TRUE;
598 if (ipv4_enable) {
599 sprintf(ifn, "%s%s", VAR_IFCONFIG, devp->name);
600 if (use_dhcp) {
601 if (strlen(extras) > 0)
602 sprintf(temp, "DHCP %s", extras);
603 else
604 sprintf(temp, "DHCP");
605 } else
606 sprintf(temp, "inet %s %s netmask %s",
607 ipaddr, extras, netmask);
608 variable_set2(ifn, temp, 1);
609 }
610#ifdef PCCARD_ARCH
611 pccard = variable_get("_pccard_install");
612 if (pccard && strcmp(pccard, "YES") == 0 && ipv4_enable) {
613 variable_set2("pccard_ifconfig", temp, 1);
614 }
615#endif
616 if (use_rtsol)
617 variable_set2(VAR_IPV6_ENABLE, "YES", 1);
618 if (!use_dhcp)
619 configResolv(NULL); /* XXX this will do it on the MFS copy XXX */
620 ret = DITEM_SUCCESS;
621 }
622 else
623 ret = DITEM_FAILURE;
624 restorescr(save);
625 return ret;
626}
627
628static Device *NetDev;
629
630static int
631netHook(dialogMenuItem *self)
632{
633 Device **devs;
634
635 devs = deviceFindDescr(self->prompt, self->title, DEVICE_TYPE_NETWORK);
636 if (devs) {
637 if (DITEM_STATUS(tcpOpenDialog(devs[0])) != DITEM_FAILURE)
638 NetDev = devs[0];
639 else
640 NetDev = NULL;
641 }
642 return devs ? DITEM_LEAVE_MENU : DITEM_FAILURE;
643}
644
645/* Get a network device */
646Device *
647tcpDeviceSelect(void)
648{
649 DMenu *menu;
650 Device **devs, *rval;
651 int cnt;
652
653 devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK);
654 cnt = deviceCount(devs);
655 rval = NULL;
656
657 if (!cnt) {
658 msgConfirm("No network devices available!");
659 return NULL;
660 }
661 else if ((!RunningAsInit) && (variable_check("NETWORK_CONFIGURED=NO") != TRUE)) {
662 if (!msgYesNo("Running multi-user, assume that the network is already configured?"))
663 return devs[0];
664 }
665 if (cnt == 1) {
666 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS))
667 rval = devs[0];
668 }
669 else if (variable_get(VAR_NONINTERACTIVE) && variable_get(VAR_NETWORK_DEVICE)) {
670 devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK);
671 cnt = deviceCount(devs);
672 if (cnt) {
673 if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS))
674 rval = devs[0];
675 }
676 }
677 else {
678 int status;
679
680 menu = deviceCreateMenu(&MenuNetworkDevice, DEVICE_TYPE_NETWORK, netHook, NULL);
681 if (!menu)
682 msgFatal("Unable to create network device menu! Argh!");
683 status = dmenuOpenSimple(menu, FALSE);
684 free(menu);
685 if (status)
686 rval = NetDev;
687 }
688 return rval;
689}
690
691/* Do it from a menu that doesn't care about status */
692int
693tcpMenuSelect(dialogMenuItem *self)
694{
695 Device *tmp;
696 WINDOW *save;
697
698 variable_set("NETWORK_CONFIGURED=NO",0);
699 tmp = tcpDeviceSelect();
700 variable_unset("NETWORK_CONFIGURED");
701 save = savescr();
702 if (tmp && tmp->private && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
703 if (!DEVICE_INIT(tmp))
704 msgConfirm("Initialization of %s device failed.", tmp->name);
705 restorescr(save);
706 return DITEM_SUCCESS;
707}