Merge from vendor branch DIFFUTILS:
[dragonfly.git] / release / sysinstall / tcpip.c
1 /*
2  * $FreeBSD: src/release/sysinstall/tcpip.c,v 1.103.2.22 2003/03/22 23:03:22 mbr Exp $
3  * $DragonFly: src/release/sysinstall/Attic/tcpip.c,v 1.2 2003/06/17 04:27:21 dillon Exp $
4  *
5  * Copyright (c) 1995
6  *      Gary J Palmer. All rights reserved.
7  * Copyright (c) 1996
8  *      Jordan K. Hubbard. All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer,
15  *    verbatim and that no modifications are made prior to this
16  *    point in the file.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33
34 /*
35  * All kinds of hacking also performed by jkh on this code.  Don't
36  * blame Gary for every bogosity you see here.. :-)
37  *
38  * -jkh
39  */
40
41 #include "sysinstall.h"
42 #include <sys/param.h>
43 #include <sys/sysctl.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netdb.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
53 static char     hostname[HOSTNAME_FIELD_LEN], domainname[HOSTNAME_FIELD_LEN],
54                 gateway[IPADDR_FIELD_LEN], nameserver[INET6_ADDRSTRLEN];
55 static int      okbutton, cancelbutton;
56 static char     ipaddr[IPADDR_FIELD_LEN], netmask[IPADDR_FIELD_LEN], extras[EXTRAS_FIELD_LEN];
57 static 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
65 static 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:",
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     { NULL },
108 };
109
110 #define _validByte(b) ((b) >= 0 && (b) <= 255)
111
112 /* whine */
113 static void
114 feepout(char *msg)
115 {
116     beep();
117     msgConfirm("%s", msg);
118 }
119
120 /* Verify IP address integrity */
121 static int
122 verifyIP(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
158 static int
159 verifyIP6(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 */
176 static int
177 verifyNetmask(const char *netmask, unsigned long *out)
178 {
179     unsigned long mask;
180     unsigned 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 */
190         mask = strtoul(netmask, &endptr, 10);
191         if (!_validByte(mask) || *endptr++ != '.')
192             return 0;
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
214 static int
215 verifyGW(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 */
230 static int
231 verifySettings(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
254 static void
255 dhcpGetInfo(Device *devp)
256 {
257     /* If it fails, do it the old-fashioned way */
258     if (dhcpParseLeases("/var/db/dhclient.leases", hostname, domainname,
259                          nameserver, ipaddr, gateway, netmask) == -1) {
260         FILE *ifp;
261         char *cp, cmd[256], data[2048];
262         int i, j;
263
264         /* Bah, now we have to kludge getting the information from ifconfig */
265         snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name);
266         ifp = popen(cmd, "r");
267         if (ifp) {
268             j = fread(data, 1, sizeof(data), ifp);
269             fclose(ifp);
270             if (j < 0)  /* paranoia */
271                 j = 0;
272             data[j] = '\0';
273             if (isDebug())
274                 msgDebug("DHCP configured interface returns %s\n", data);
275             /* XXX This is gross as it assumes a certain ordering to
276                ifconfig's output! XXX */
277             if ((cp = strstr(data, "inet ")) != NULL) {
278                 i = 0;
279                 cp += 5;        /* move over keyword */
280                 while (*cp != ' ')
281                     ipaddr[i++] = *(cp++);
282                 ipaddr[i] = '\0';
283                 if (!strncmp(++cp, "netmask", 7)) {
284                     i = 0;
285                     cp += 8;
286                     while (*cp != ' ')
287                         netmask[i++] = *(cp++);
288                     netmask[i] = '\0';
289                 }
290             }
291         }
292     }
293
294     /* If we didn't get a name server value, hunt for it in resolv.conf */
295     if (!nameserver[0] && file_readable("/etc/resolv.conf"))
296         configEnvironmentResolv("/etc/resolv.conf");
297     if (hostname[0])
298         variable_set2(VAR_HOSTNAME, hostname, 0);
299 }
300
301 static void
302 rtsolGetInfo(Device *devp)
303 {
304     FILE *ifp;
305     char *cp, cmd[256], data[2048];
306     int i;
307
308     snprintf(cmd, sizeof cmd, "ifconfig %s", devp->name);
309     if ((ifp = popen(cmd, "r")) == NULL)
310         return;
311     while (fgets(data, sizeof(data), ifp) != NULL) {
312         if (isDebug())
313             msgDebug("RTSOL configured interface returns %s", data);
314         if ((cp = strstr(data, "inet6 ")) != NULL) {
315             cp += 6;    /* move over keyword */
316             if (strncmp(cp, "fe80:", 5)) {
317                 i = 0;
318                 while (*cp != ' ')
319                     ipv6addr[i++] = *(cp++);
320                 ipv6addr[i] = '\0';
321             }
322         }
323     }
324     fclose(ifp);
325 }
326
327 /* This is it - how to get TCP setup values */
328 int
329 tcpOpenDialog(Device *devp)
330 {
331     WINDOW              *ds_win, *save = NULL;
332     ComposeObj          *obj = NULL;
333     int                 n = 0, filled = 0, cancel = FALSE;
334     int                 max, ret = DITEM_SUCCESS;
335     int                 use_dhcp = FALSE;
336     int                 use_rtsol = FALSE;
337     char                *tmp;
338     char                title[80];
339
340     save = savescr();
341     /* Initialise vars from previous device values */
342     if (devp->private) {
343         DevInfo *di = (DevInfo *)devp->private;
344
345         SAFE_STRCPY(ipaddr, di->ipaddr);
346         SAFE_STRCPY(netmask, di->netmask);
347         SAFE_STRCPY(extras, di->extras);
348         use_dhcp = di->use_dhcp;
349         use_rtsol = di->use_rtsol;
350     }
351     else { /* See if there are any defaults */
352         char *cp;
353         char *old_interactive = NULL;
354
355         /*
356          * This is a hack so that the dialogs below are interactive in a
357          * script if we have requested interactive behavior.
358          */
359         if (variable_get(VAR_NONINTERACTIVE) &&
360           variable_get(VAR_NETINTERACTIVE)) {
361             old_interactive = strdup(VAR_NONINTERACTIVE);
362             variable_unset(VAR_NONINTERACTIVE);
363         }
364
365
366         /*
367          * Try a RTSOL scan if such behavior is desired.
368          * If the variable was configured and is YES, do it.
369          * If it was configured to anything else, treat it as NO.
370          * Otherwise, ask the question interactively.
371          */
372         if (!variable_cmp(VAR_TRY_RTSOL, "YES") || 
373             (variable_get(VAR_TRY_RTSOL)==0 && !msgNoYes("Do you want to try IPv6 configuration of the interface?"))) {
374             int i;
375             size_t len;
376
377             i = 0;
378             sysctlbyname("net.inet6.ip6.forwarding", NULL, 0, &i, sizeof(i));
379             i = 1;
380             sysctlbyname("net.inet6.ip6.accept_rtadv", NULL, 0, &i, sizeof(i));
381             vsystem("ifconfig %s up", devp->name);
382             len = sizeof(i);
383             sysctlbyname("net.inet6.ip6.dad_count", &i, &len, NULL, 0);
384             sleep(i + 1);
385             Mkdir("/var/run");
386             msgNotify("Scanning for RA servers...");
387             if (0 == vsystem("rtsol %s", devp->name)) {
388                 len = sizeof(i);
389                 sysctlbyname("net.inet6.ip6.dad_count", &i, &len, NULL, 0);
390                 sleep(i + 1);
391                 rtsolGetInfo(devp);
392                 use_rtsol = TRUE;
393             } else
394                 use_rtsol = FALSE;
395         }
396
397
398         /*
399          * First try a DHCP scan if such behavior is desired.
400          * If the variable was configured and is YES, do it.
401          * If it was configured to anything else, treat it as NO.
402          * Otherwise, ask the question interactively.
403          */
404         if (!variable_cmp(VAR_TRY_DHCP, "YES") || 
405             (variable_get(VAR_TRY_DHCP)==0 && !msgNoYes("Do you want to try DHCP configuration of the interface?"))) {
406             Mkdir("/var/db");
407             Mkdir("/var/run");
408             Mkdir("/tmp");
409             vsystem("dhclient -r %s", devp->name);
410             msgNotify("Scanning for DHCP servers...");
411             if (0 == vsystem("dhclient -1 %s", devp->name)) {
412                 dhcpGetInfo(devp);
413                 use_dhcp = TRUE;
414             }
415             else
416                 use_dhcp = FALSE;
417         }
418
419         /* Restore old VAR_NONINTERACTIVE if needed. */
420         if (old_interactive != NULL) {
421             variable_set2(VAR_NONINTERACTIVE, old_interactive, 0);
422             free(old_interactive);
423         }
424
425         /* Special hack so it doesn't show up oddly in the tcpip setup menu */
426         if (!strcmp(gateway, "NO"))
427             gateway[0] = '\0';
428
429         /* Get old IP address from variable space, if available */
430         if (!ipaddr[0]) {
431             if ((cp = variable_get(VAR_IPADDR)) != NULL)
432                 SAFE_STRCPY(ipaddr, cp);
433             else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_IPADDR))) != NULL)
434                 SAFE_STRCPY(ipaddr, cp);
435         }
436
437         /* Get old netmask from variable space, if available */
438         if (!netmask[0]) {
439             if ((cp = variable_get(VAR_NETMASK)) != NULL)
440                 SAFE_STRCPY(netmask, cp);
441             else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_NETMASK))) != NULL)
442                 SAFE_STRCPY(netmask, cp);
443         }
444
445         /* Get old extras string from variable space, if available */
446         if (!extras[0]) {
447             if ((cp = variable_get(VAR_EXTRAS)) != NULL)
448                 SAFE_STRCPY(extras, cp);
449             else if ((cp = variable_get(string_concat3(devp->name, "_", VAR_EXTRAS))) != NULL)
450                 SAFE_STRCPY(extras, cp);
451         }
452     }
453
454     /* Look up values already recorded with the system, or blank the string variables ready to accept some new data */
455     if (!hostname[0]) {
456         tmp = variable_get(VAR_HOSTNAME);
457         if (tmp)
458             SAFE_STRCPY(hostname, tmp);
459     }
460     if (!domainname[0]) {
461         tmp = variable_get(VAR_DOMAINNAME);
462         if (tmp)
463             SAFE_STRCPY(domainname, tmp);
464     }
465     if (!gateway[0]) {
466         tmp = variable_get(VAR_GATEWAY);
467         if (tmp && strcmp(tmp, "NO"))
468             SAFE_STRCPY(gateway, tmp);
469     }
470     if (!nameserver[0]) {
471         tmp = variable_get(VAR_NAMESERVER);
472         if (tmp)
473             SAFE_STRCPY(nameserver, tmp);
474     }
475
476     /* If non-interactive, jump straight over the dialog crap and into config section */
477     if (variable_get(VAR_NONINTERACTIVE) &&
478         !variable_get(VAR_NETINTERACTIVE)) {
479         if (!hostname[0])
480             msgConfirm("WARNING: hostname variable not set and is a non-optional\n"
481                        "parameter.  Please add this to your installation script\n"
482                        "or set the netInteractive variable (see sysinstall man page)");
483         else
484             goto netconfig;
485     }
486
487     /* Now do all the screen I/O */
488     dialog_clear_norefresh();
489
490     /* Modify the help line for PLIP config */
491     if (!strncmp(devp->name, "lp", 2))
492         layout[LAYOUT_EXTRAS].help = 
493          "For PLIP configuration, you must enter the peer's IP address here.";
494
495     /* We need a curses window */
496     tmp = " Network Configuration ";
497     if (ipv6addr[0])
498         tmp = string_concat(tmp, "(IPv6 ready) ");
499     if (!(ds_win = openLayoutDialog(TCP_HELPFILE, tmp,
500                                     TCP_DIALOG_X, TCP_DIALOG_Y, TCP_DIALOG_WIDTH, TCP_DIALOG_HEIGHT))) {
501         beep();
502         msgConfirm("Cannot open TCP/IP dialog window!!");
503         restorescr(save);
504         return DITEM_FAILURE;
505     }
506
507     /* Draw interface configuration box */
508     draw_box(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 8, TCP_DIALOG_HEIGHT - 13, TCP_DIALOG_WIDTH - 17,
509              dialog_attr, border_attr);
510     wattrset(ds_win, dialog_attr);
511     sprintf(title, " Configuration for Interface %s ", devp->name);
512     mvwaddstr(ds_win, TCP_DIALOG_Y + 9, TCP_DIALOG_X + 14, title);
513
514     /* Some more initialisation before we go into the main input loop */
515     obj = initLayoutDialog(ds_win, layout, TCP_DIALOG_X, TCP_DIALOG_Y, &max);
516
517 reenter:
518     cancelbutton = okbutton = 0;
519     while (layoutDialogLoop(ds_win, layout, &obj, &n, max, &cancelbutton, &cancel)) {
520         /* Prevent this from being irritating if user really means NO */
521         if (filled < 3) {
522             /* Insert a default value for the netmask, 0xffffff00 is
523              * the most appropriate one (entire class C, or subnetted
524              * class A/B network).
525              */
526             if (!netmask[0]) {
527                 strcpy(netmask, "255.255.255.0");
528                 RefreshStringObj(layout[LAYOUT_NETMASK].obj);
529                 ++filled;
530             }
531             if (!index(hostname, '.') && domainname[0]) {
532                 strcat(hostname, ".");
533                 strcat(hostname, domainname);
534                 RefreshStringObj(layout[LAYOUT_HOSTNAME].obj);
535                 ++filled;
536             }
537             else if (((tmp = index(hostname, '.')) != NULL) && !domainname[0]) {
538                 SAFE_STRCPY(domainname, tmp + 1);
539                 RefreshStringObj(layout[LAYOUT_DOMAINNAME].obj);
540                 ++filled;
541             }
542         }
543     }
544     if (!cancel && !verifySettings())
545         goto reenter;
546
547     /* Clear this crap off the screen */
548     delwin(ds_win);
549     dialog_clear_norefresh();
550     use_helpfile(NULL);
551
552     /* We actually need to inform the rest of sysinstall about this
553        data now if the user hasn't selected cancel.  Save the stuff
554        out to the environment via the variable_set() mechanism */
555
556 netconfig:
557     if (!cancel) {
558         DevInfo *di;
559         char temp[512], ifn[255];
560         char *pccard;
561         int ipv4_enable = FALSE;
562
563         if (hostname[0]) {
564             variable_set2(VAR_HOSTNAME, hostname, 1);
565             sethostname(hostname, strlen(hostname));
566         }
567         if (domainname[0])
568             variable_set2(VAR_DOMAINNAME, domainname, 0);
569         if (gateway[0])
570             variable_set2(VAR_GATEWAY, gateway, use_dhcp ? 0 : 1);
571         if (nameserver[0])
572             variable_set2(VAR_NAMESERVER, nameserver, 0);
573         if (ipaddr[0])
574             variable_set2(VAR_IPADDR, ipaddr, 0);
575         if (ipv6addr[0])
576             variable_set2(VAR_IPV6ADDR, ipv6addr, 0);
577
578         if (!devp->private)
579             devp->private = (DevInfo *)safe_malloc(sizeof(DevInfo));
580         di = devp->private;
581         SAFE_STRCPY(di->ipaddr, ipaddr);
582         SAFE_STRCPY(di->netmask, netmask);
583         SAFE_STRCPY(di->extras, extras);
584         di->use_dhcp = use_dhcp;
585         di->use_rtsol = use_rtsol;
586
587         if (use_dhcp || ipaddr[0])
588             ipv4_enable = TRUE;
589         if (ipv4_enable) {
590             sprintf(ifn, "%s%s", VAR_IFCONFIG, devp->name);
591             if (use_dhcp)
592                 sprintf(temp, "DHCP");
593             else
594                 sprintf(temp, "inet %s %s netmask %s",
595                         ipaddr, extras, netmask);
596             variable_set2(ifn, temp, 1);
597         }
598 #ifdef PCCARD_ARCH
599         pccard = variable_get("_pccard_install");
600         if (pccard && strcmp(pccard, "YES") == 0 && ipv4_enable) {
601             variable_set2("pccard_ifconfig", temp, 1);
602         }
603 #endif
604         if (use_rtsol)
605             variable_set2(VAR_IPV6_ENABLE, "YES", 1);
606         if (!use_dhcp)
607             configResolv(NULL); /* XXX this will do it on the MFS copy XXX */
608         ret = DITEM_SUCCESS;
609     }
610     else
611         ret = DITEM_FAILURE;
612     restorescr(save);
613     return ret;
614 }
615
616 static Device *NetDev;
617
618 static int
619 netHook(dialogMenuItem *self)
620 {
621     Device **devs;
622
623     devs = deviceFindDescr(self->prompt, self->title, DEVICE_TYPE_NETWORK);
624     if (devs) {
625         if (DITEM_STATUS(tcpOpenDialog(devs[0])) != DITEM_FAILURE)
626             NetDev = devs[0];
627         else
628             NetDev = NULL;
629     }
630     return devs ? DITEM_LEAVE_MENU : DITEM_FAILURE;
631 }
632
633 /* Get a network device */
634 Device *
635 tcpDeviceSelect(void)
636 {
637     DMenu *menu;
638     Device **devs, *rval;
639     int cnt;
640
641     devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK);
642     cnt = deviceCount(devs);
643     rval = NULL;
644
645     if (!cnt) {
646         msgConfirm("No network devices available!");
647         return NULL;
648     }
649     else if ((!RunningAsInit) && (variable_check("NETWORK_CONFIGURED=NO") != TRUE)) {
650         if (!msgYesNo("Running multi-user, assume that the network is already configured?"))
651             return devs[0];
652     }
653     if (cnt == 1) {
654         if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS))
655             rval = devs[0];
656     }
657     else if (variable_get(VAR_NONINTERACTIVE) && variable_get(VAR_NETWORK_DEVICE)) {
658         devs = deviceFind(variable_get(VAR_NETWORK_DEVICE), DEVICE_TYPE_NETWORK);
659         cnt = deviceCount(devs);
660         if (cnt) {
661             if (DITEM_STATUS(tcpOpenDialog(devs[0]) == DITEM_SUCCESS))
662                 rval = devs[0];
663         }
664     }
665     else {
666         int status;
667
668         menu = deviceCreateMenu(&MenuNetworkDevice, DEVICE_TYPE_NETWORK, netHook, NULL);
669         if (!menu)
670             msgFatal("Unable to create network device menu!  Argh!");
671         status = dmenuOpenSimple(menu, FALSE);
672         free(menu);
673         if (status)
674             rval = NetDev;
675     }
676     return rval;
677 }
678
679 /* Do it from a menu that doesn't care about status */
680 int
681 tcpMenuSelect(dialogMenuItem *self)
682 {
683     Device *tmp;
684     WINDOW *save;
685
686     variable_set("NETWORK_CONFIGURED=NO",0);
687     tmp = tcpDeviceSelect();
688     variable_unset("NETWORK_CONFIGURED");
689     save = savescr();
690     if (tmp && tmp->private && !((DevInfo *)tmp->private)->use_dhcp && !msgYesNo("Would you like to bring the %s interface up right now?", tmp->name))
691         if (!DEVICE_INIT(tmp))
692             msgConfirm("Initialization of %s device failed.", tmp->name);
693     restorescr(save);
694     return DITEM_SUCCESS;
695 }