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