1 /*
2  * The new sysinstall program.
3  *
4  * This is probably the last attempt in the `sysinstall' line, the next
5  * generation being slated to essentially a complete rewrite.
6  *
7  * $FreeBSD: stable/9/usr.sbin/sysinstall/network.c 210175 2010-07-16 20:42:20Z brucec $
8  *
9  * Copyright (c) 1995
10  *	Jordan Hubbard.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer,
17  *    verbatim and that no modifications are made prior to this
18  *    point in the file.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  *
24  * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  */
37 
38 /* These routines deal with getting things off of network media */
39 
40 #include "sysinstall.h"
41 #include <signal.h>
42 #include <termios.h>
43 #include <sys/fcntl.h>
44 #include <sys/ioctl.h>
45 #include <sys/stat.h>
46 
47 static Boolean	networkInitialized;
48 
49 Boolean
mediaInitNetwork(Device * dev)50 mediaInitNetwork(Device *dev)
51 {
52     int i;
53     char *rp;
54     char *cp, ifconfig[255];
55     WINDOW *w;
56 
57     if (!RunningAsInit || networkInitialized)
58 	return TRUE;
59 
60     if (isDebug())
61 	msgDebug("Init routine called for network device %s.\n", dev->name);
62 
63     if (!file_readable("/etc/resolv.conf")) {
64 	if (DITEM_STATUS(configResolv(NULL)) == DITEM_FAILURE) {
65 	    msgConfirm("Can't seem to write out /etc/resolv.conf.  Net cannot be used.");
66 	    return FALSE;
67 	}
68     }
69 
70     w = savescr();
71     dialog_clear_norefresh();
72 
73     snprintf(ifconfig, 255, "%s%s", VAR_IFCONFIG, dev->name);
74     cp = variable_get(ifconfig);
75     if (cp) {
76 	/*
77 	 * If this interface isn't a DHCP one, bring it up.
78 	 * If it is, then it's already up.
79 	 */
80 	if (strstr(cp, "DHCP") == NULL) {
81 	    msgDebug("Not a DHCP interface.\n");
82 	    i = vsystem("ifconfig %s %s", dev->name, cp);
83 	    if (i) {
84 		msgConfirm("Unable to configure the %s interface!\n"
85 			   "This installation method cannot be used.",
86 			   dev->name);
87 		return FALSE;
88 	    }
89 	    rp = variable_get(VAR_GATEWAY);
90 	    if (!rp || *rp == '0') {
91 		msgConfirm("No gateway has been set. You will be unable to access hosts\n"
92 			   "not on your local network");
93 	    }
94 	    else {
95 		/*
96 		 * Explicitly flush all routes to get back to a known sane
97 		 * state. We don't need to check this exit code because if
98 		 * anything fails it will show up in the route add below.
99 		 */
100 		system("route -n flush");
101 		msgDebug("Adding default route to %s.\n", rp);
102 		if (vsystem("route -n add default %s", rp) != 0) {
103 		    msgConfirm("Failed to add a default route; please check "
104 			       "your network configuration");
105 		    return FALSE;
106 		}
107 	    }
108 	} else {
109 	    msgDebug("A DHCP interface.  Should already be up.\n");
110 	}
111     } else if ((cp = variable_get(VAR_IPV6ADDR)) == NULL || *cp == '\0') {
112 	msgConfirm("The %s device is not configured.  You will need to do so\n"
113 		   "in the Networking configuration menu before proceeding.", dev->name);
114 	return FALSE;
115     }
116 
117     if (isDebug())
118 	msgDebug("Network initialized successfully.\n");
119     networkInitialized = TRUE;
120     return TRUE;
121 }
122 
123 void
mediaShutdownNetwork(Device * dev)124 mediaShutdownNetwork(Device *dev)
125 {
126     char *cp;
127 
128     if (!RunningAsInit || !networkInitialized)
129 	return;
130 
131 	msgDebug("Shutdown called for network device %s\n", dev->name);
132 	int i;
133 	char ifconfig[255];
134 
135 	snprintf(ifconfig, 255, "%s%s", VAR_IFCONFIG, dev->name);
136 	cp = variable_get(ifconfig);
137 	if (!cp)
138 	    return;
139 	msgDebug("ifconfig %s down\n", dev->name);
140 	i = vsystem("ifconfig %s down", dev->name);
141 	if (i)
142 	    msgConfirm("Warning: Unable to down the %s interface properly", dev->name);
143 	cp = variable_get(VAR_GATEWAY);
144 	if (cp) {
145 	    msgDebug("Deleting default route.\n");
146 	    vsystem("route -n delete default");
147 	}
148 }
149 
150