18791Sjkh/*
28791Sjkh * The new sysinstall program.
38791Sjkh *
48791Sjkh * This is probably the last attempt in the `sysinstall' line, the next
58791Sjkh * generation being slated to essentially a complete rewrite.
68791Sjkh *
750479Speter * $FreeBSD$
88791Sjkh *
98791Sjkh * Copyright (c) 1995
108791Sjkh *	Jordan Hubbard.  All rights reserved.
118791Sjkh *
128791Sjkh * Redistribution and use in source and binary forms, with or without
138791Sjkh * modification, are permitted provided that the following conditions
148791Sjkh * are met:
158791Sjkh * 1. Redistributions of source code must retain the above copyright
168881Srgrimes *    notice, this list of conditions and the following disclaimer,
178881Srgrimes *    verbatim and that no modifications are made prior to this
188791Sjkh *    point in the file.
1920231Sjkh *
208791Sjkh * 2. Redistributions in binary form must reproduce the above copyright
218791Sjkh *    notice, this list of conditions and the following disclaimer in the
228791Sjkh *    documentation and/or other materials provided with the distribution.
238791Sjkh *
248791Sjkh * THIS SOFTWARE IS PROVIDED BY JORDAN HUBBARD ``AS IS'' AND
258791Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
268791Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
278791Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL JORDAN HUBBARD OR HIS PETS BE LIABLE
288791Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
298791Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
308791Sjkh * OR SERVICES; LOSS OF USE, DATA, LIFE OR PROFITS; OR BUSINESS INTERRUPTION)
318791Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
328791Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
338791Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
348791Sjkh * SUCH DAMAGE.
358791Sjkh *
368791Sjkh */
378791Sjkh
388803Sjkh/* These routines deal with getting things off of network media */
398791Sjkh
408791Sjkh#include "sysinstall.h"
4112661Speter#include <signal.h>
4250917Sjkh#include <termios.h>
438810Sjkh#include <sys/fcntl.h>
4412661Speter#include <sys/ioctl.h>
458837Sjkh#include <sys/stat.h>
468791Sjkh
479202Srgrimesstatic Boolean	networkInitialized;
488791Sjkh
498791SjkhBoolean
508791SjkhmediaInitNetwork(Device *dev)
518791Sjkh{
528791Sjkh    int i;
538791Sjkh    char *rp;
5420233Sjkh    char *cp, ifconfig[255];
5554587Sjkh    WINDOW *w;
5654587Sjkh
5712661Speter    if (!RunningAsInit || networkInitialized)
588791Sjkh	return TRUE;
598791Sjkh
6041162Sjkh    if (isDebug())
6141162Sjkh	msgDebug("Init routine called for network device %s.\n", dev->name);
6212661Speter
6341162Sjkh    if (!file_readable("/etc/resolv.conf")) {
6441162Sjkh	if (DITEM_STATUS(configResolv(NULL)) == DITEM_FAILURE) {
6541162Sjkh	    msgConfirm("Can't seem to write out /etc/resolv.conf.  Net cannot be used.");
6641162Sjkh	    return FALSE;
6741162Sjkh	}
6841162Sjkh    }
6941162Sjkh
7054722Sjkh    w = savescr();
7154722Sjkh    dialog_clear_norefresh();
7254722Sjkh
7312661Speter    snprintf(ifconfig, 255, "%s%s", VAR_IFCONFIG, dev->name);
7412661Speter    cp = variable_get(ifconfig);
7563118Sume    if (cp) {
76155473Sdelphij	/*
77155473Sdelphij	 * If this interface isn't a DHCP one, bring it up.
78155473Sdelphij	 * If it is, then it's already up.
79155473Sdelphij	 */
80155473Sdelphij	if (strstr(cp, "DHCP") == NULL) {
81155473Sdelphij	    msgDebug("Not a DHCP interface.\n");
8263118Sume	    i = vsystem("ifconfig %s %s", dev->name, cp);
8363118Sume	    if (i) {
8463118Sume		msgConfirm("Unable to configure the %s interface!\n"
8563118Sume			   "This installation method cannot be used.",
8663118Sume			   dev->name);
8763118Sume		return FALSE;
8863118Sume	    }
8963118Sume	    rp = variable_get(VAR_GATEWAY);
9063118Sume	    if (!rp || *rp == '0') {
91208407Srandi		msgConfirm("No gateway has been set. You will be unable to access hosts\n"
9263118Sume			   "not on your local network");
9363118Sume	    }
9463118Sume	    else {
95208407Srandi		/*
96208407Srandi		 * Explicitly flush all routes to get back to a known sane
97208407Srandi		 * state. We don't need to check this exit code because if
98208407Srandi		 * anything fails it will show up in the route add below.
99208407Srandi		 */
100208407Srandi		system("route -n flush");
101106279Skuriyama		msgDebug("Adding default route to %s.\n", rp);
102208407Srandi		if (vsystem("route -n add default %s", rp) != 0) {
103208407Srandi		    msgConfirm("Failed to add a default route; please check "
104208407Srandi			       "your network configuration");
105208407Srandi		    return FALSE;
106208407Srandi		}
10763118Sume	    }
108155473Sdelphij	} else {
109155473Sdelphij	    msgDebug("A DHCP interface.  Should already be up.\n");
11063118Sume	}
11163118Sume    } else if ((cp = variable_get(VAR_IPV6ADDR)) == NULL || *cp == '\0') {
11212661Speter	msgConfirm("The %s device is not configured.  You will need to do so\n"
11316330Sjkh		   "in the Networking configuration menu before proceeding.", dev->name);
11410882Speter	return FALSE;
1158791Sjkh    }
1168791Sjkh
11720208Sjkh    if (isDebug())
11820208Sjkh	msgDebug("Network initialized successfully.\n");
1198791Sjkh    networkInitialized = TRUE;
1208791Sjkh    return TRUE;
1218791Sjkh}
1228791Sjkh
1238791Sjkhvoid
1248791SjkhmediaShutdownNetwork(Device *dev)
1258791Sjkh{
1268791Sjkh    char *cp;
1278791Sjkh
12812661Speter    if (!RunningAsInit || !networkInitialized)
1298791Sjkh	return;
1308791Sjkh
131210175Sbrucec	msgDebug("Shutdown called for network device %s\n", dev->name);
1328791Sjkh	int i;
13312661Speter	char ifconfig[255];
1348791Sjkh
13512661Speter	snprintf(ifconfig, 255, "%s%s", VAR_IFCONFIG, dev->name);
13612661Speter	cp = variable_get(ifconfig);
1378791Sjkh	if (!cp)
1388791Sjkh	    return;
139106279Skuriyama	msgDebug("ifconfig %s down\n", dev->name);
1408791Sjkh	i = vsystem("ifconfig %s down", dev->name);
14115355Sjkh	if (i)
1428791Sjkh	    msgConfirm("Warning: Unable to down the %s interface properly", dev->name);
14312661Speter	cp = variable_get(VAR_GATEWAY);
14412661Speter	if (cp) {
145106279Skuriyama	    msgDebug("Deleting default route.\n");
14639868Smsmith	    vsystem("route -n delete default");
14712661Speter	}
1488810Sjkh}
1498791Sjkh
150