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