rc.bootup 8.74 KB
Newer Older
1
#!/usr/local/bin/php
Ad Schellevis's avatar
Ad Schellevis committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<?php

/*
	Copyright (C) 2004-2009 Scott Ullrich <sullrich@pfsense.org>.
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
	Copyright (C) 2009 Erik Kristensen
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:

	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.

	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
function is_install_media()
{
	/*
	 * Despite unionfs underneath, / is still not writeable,
	 * making the following the perfect test for install media.
	 */

	$file = '/.probe.for.install.media';
	$fd = @fopen($file, 'w');
	if ($fd) {
		fclose($fd);
		unlink($file);
		return false;
	}

	return true;
}
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

function is_interface_mismatch()
{
	global $config;

	$do_assign = false;
	$i = 0;

	if (isset($config['interfaces'])) {
		foreach ($config['interfaces'] as $ifname => $ifcfg) {
			if (preg_match("/^enc|^cua|^tun|^tap|^l2tp|^pptp|^ppp|^ovpn|^gif|^gre|^lagg|^bridge|vlan|_wlan/i", $ifcfg['if'])) {
				/* Do not check these interfaces */
				$i++;
				continue;
			} elseif (does_interface_exist($ifcfg['if']) == false) {
				$do_assign = true;
			} else {
				$i++;
			}
		}
	}

	return $do_assign;
}



76 77 78 79
function rescue_detect_keypress()
{
	$timeout = 9;

Ad Schellevis's avatar
Ad Schellevis committed
80
	echo "\n";
81 82 83
	echo "(I)nstaller will be invoked by default.\n\n";
	echo "(C)ontinue with LiveCD bootup instead.\n\n";
	echo "(R)ecovery mode escapes to a root shell.\n\n";
Ad Schellevis's avatar
Ad Schellevis committed
84
	echo "Timeout before auto boot continues (seconds): {$timeout}";
85

Ad Schellevis's avatar
Ad Schellevis committed
86 87
	$key = null;
	exec("/bin/stty erase " . chr(8));
88
	while (!in_array($key, array("c", "C", "r","R", "i", "I"))) {
Ad Schellevis's avatar
Ad Schellevis committed
89 90 91 92 93 94 95
	        echo chr(8) . "{$timeout}";
	        `/bin/stty -icanon min 0 time 25`;
	        $key = trim(`KEY=\`dd count=1 2>/dev/null\`; echo \$KEY`);
	        `/bin/stty icanon`;
	        // Decrement our timeout value
	        $timeout--;
	        // If we have reached 0 exit and continue on
96 97 98
	        if ($timeout == 0) {
			break;
		}
Ad Schellevis's avatar
Ad Schellevis committed
99
	}
100 101 102

	echo "\n\n";

Ad Schellevis's avatar
Ad Schellevis committed
103
	if (in_array($key, array("r", "R"))) {
104
		echo "Recovery shell selected...\n\n";
Ad Schellevis's avatar
Ad Schellevis committed
105 106
		touch("/tmp/donotbootup");
		exit;
107 108
	} elseif (in_array($key, array("c", "C"))) {
		/* nothing */
Ad Schellevis's avatar
Ad Schellevis committed
109
	} else {
110 111 112 113 114 115
		echo "Installer mode selected...\n";
		passthru("/usr/local/etc/rc.installer");
		if (file_exists("/tmp/install_complete")) {
			passthru("/usr/local/etc/rc.reboot");
			exit;
		}
Ad Schellevis's avatar
Ad Schellevis committed
116 117 118 119 120 121 122
	}
}

echo " done.\n";

echo "Initializing...";
echo ".";
123
require_once("led.inc");
Ad Schellevis's avatar
Ad Schellevis committed
124 125 126 127 128 129 130
led_normalize();
echo ".";
if (led_count() >= 3) {
	led_kitt();
}

/* parse the configuration and include all functions used below */
131
require_once("config.inc");
Ad Schellevis's avatar
Ad Schellevis committed
132
echo ".";
133
require_once("config.console.inc");
Ad Schellevis's avatar
Ad Schellevis committed
134
echo ".";
135
require_once("auth.inc");
Ad Schellevis's avatar
Ad Schellevis committed
136
echo ".";
137
require_once("util.inc");
138
echo ".";
139
require_once("interfaces.inc");
140
echo ".";
141
require_once("services.inc");
142
echo ".";
143
require_once("pfsense-utils.inc");
144
echo ".";
145
require_once("system.inc");
146
echo ".";
147 148
require_once("unbound.inc");
echo ".";
149
require_once("vslb.inc");
Ad Schellevis's avatar
Ad Schellevis committed
150
echo ".";
151
require_once("filter.inc");
Ad Schellevis's avatar
Ad Schellevis committed
152
echo ".";
153
require_once("plugins.inc");
Ad Schellevis's avatar
Ad Schellevis committed
154
echo ".";
155 156
require_once("ipsec.inc");
echo ".";
157
require_once("openvpn.inc");
Ad Schellevis's avatar
Ad Schellevis committed
158
echo ".";
159
require_once("rrd.inc");
Ad Schellevis's avatar
Ad Schellevis committed
160 161 162 163 164 165
echo ".";
echo " done.\n";

/* start devd (dhclient now uses it) */
echo "Starting device manager (devd)...";
mute_kernel_msgs();
166 167
exec('/sbin/devd');
sleep(1);
Ad Schellevis's avatar
Ad Schellevis committed
168 169 170 171
set_device_perms();
unmute_kernel_msgs();
echo "done.\n";

172 173
/* Display live system's early boot options */
if (is_install_media()) {
174
	rescue_detect_keypress();
175 176
        // config may have changed (copied by installer)
        OPNsense\Core\Config::getInstance()->forceReload();
177
}
Ad Schellevis's avatar
Ad Schellevis committed
178 179

echo "Loading configuration...";
180 181
global $config;
$config = parse_config();
182
convert_config();
Ad Schellevis's avatar
Ad Schellevis committed
183 184
echo "done.\n";

185 186 187 188 189
/*
 *  Determine if we need to throw a interface exception
 *  and ask the user to reassign interfaces.  This will
 *  avoid a reboot and thats a good thing.
 */
190 191 192
if (is_interface_mismatch()) {
	echo PHP_EOL . gettext('Default interfaces not found -- Running interface assignment option.') . PHP_EOL;

193
	led_assigninterfaces();
194
	while (!set_networking_interfaces_ports(true));
195
	led_kitt();
Ad Schellevis's avatar
Ad Schellevis committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
}

/* read in /etc/sysctl.conf and set values if needed */
echo "Setting up extended sysctls...";
system_setup_sysctl();
echo "done.\n";

/* enable optional crypto modules */
load_crypto();

/* enable optional thermal sensor modules */
load_thermal_hardware();

/* set up our timezone */
system_timezone_configure();

212 213 214
/* set up firmware configuration */
system_firmware_configure();

Ad Schellevis's avatar
Ad Schellevis committed
215 216 217 218 219 220 221 222 223 224 225 226 227
/* set up our hostname */
system_hostname_configure();

/* make hosts file */
system_hosts_generate();

/* configure loopback interface */
interfaces_loopback_configure();

/* start syslogd */
system_syslogd_start();

echo "Starting Secure Shell Services...";
Ad Schellevis's avatar
Ad Schellevis committed
228
configd_run("sshd restart");
Ad Schellevis's avatar
Ad Schellevis committed
229 230 231 232 233 234 235 236
echo "done.\n";

/* setup polling */
echo "Setting up polling defaults...";
setup_polling();
echo "done.\n";

/* set up interfaces */
237
mute_kernel_msgs();
Ad Schellevis's avatar
Ad Schellevis committed
238
interfaces_configure();
239
unmute_kernel_msgs();
Ad Schellevis's avatar
Ad Schellevis committed
240 241 242 243 244 245 246 247 248 249 250 251

/* re-make hosts file after configuring interfaces */
system_hosts_generate();

/* start OpenVPN server & clients */
echo "Syncing OpenVPN settings...";
openvpn_resync_all();
echo "done.\n";

/* generate resolv.conf */
system_resolvconf_generate();

252
/* setup pf */
Ad Schellevis's avatar
Ad Schellevis committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
filter_configure_sync();

/* start pflog */
echo "Starting PFLOG...";
filter_pflog_start();
echo "done.\n";

/* reconfigure our gateway monitor */
echo "Setting up gateway monitors...";
setup_gateways_monitor();
echo "done.\n";

echo "Synchronizing user settings...";
local_sync_accounts();
echo "done.\n";

269
/* start web server */
270
echo 'Starting webConfigurator...'. (system_webgui_start() ? 'done.' : 'failed.') . PHP_EOL;
Ad Schellevis's avatar
Ad Schellevis committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296

/* configure cron service */
echo "Configuring CRON...";
configure_cron();
echo "done.\n";

/* set up static routes */
system_routing_configure();

/* enable routing */
system_routing_enable();

/* start dnsmasq service */
services_dnsmasq_configure();

/* start unbound service */
services_unbound_configure();

/* Do an initial time sync */
echo "Starting NTP time client...";
system_ntp_configure(false);
echo "done.\n";

/* start load balancer daemon */
relayd_configure();

297 298 299
/* configure console menu */
system_console_configure();

Ad Schellevis's avatar
Ad Schellevis committed
300 301 302 303 304 305 306 307 308 309 310 311 312
/* start DHCP service */
services_dhcpd_configure();

/* start dhcpleases dhpcp hosts leases program */
system_dhcpleases_configure();

/* start DHCP relay */
services_dhcrelay_configure();

/* start DHCP6 relay */
services_dhcrelay6_configure();

/* dyndns service updates */
313
configd_run("dyndns reload");
Ad Schellevis's avatar
Ad Schellevis committed
314 315 316 317

/* Run a filter configure now that most all services have started */
filter_configure_sync();

318 319
/* Run all registered plugins */
plugins_configure();
Ad Schellevis's avatar
Ad Schellevis committed
320 321

/* start IPsec tunnels */
322
$ipsec_dynamic_hosts = ipsec_configure();
Ad Schellevis's avatar
Ad Schellevis committed
323 324 325 326 327 328 329 330 331 332 333 334 335

/* start SNMP service */
services_snmpd_configure();

/* lock down console if necessary */
reload_ttys();

/* load graphing functions */
enable_rrd_graphing();

/* if we are operating at 1000 then increase timeouts.
   this was never accounted for after moving to 1000 hz */
$kern_hz = get_single_sysctl('kern.clockrate');
336 337 338 339 340
$kern_hz = substr($kern_hz, strpos($kern_hz, 'hz = ') + 5);
$kern_hz = substr($kern_hz, 0, strpos($kern_hz, ','));
if ($kern_hz == '1000') {
	set_single_sysctl('net.inet.tcp.rexmit_min' , '30');
}
Ad Schellevis's avatar
Ad Schellevis committed
341 342 343 344 345

/* start the igmpproxy daemon */
services_igmpproxy_configure();

/* start the upnp daemon if it is enabled */
346
upnp_configure();
Ad Schellevis's avatar
Ad Schellevis committed
347 348 349 350 351 352 353

/* If powerd is enabled, lets launch it */
activate_powerd();

/* Set preferred protocol */
prefer_ipv4_or_ipv6();

354 355 356 357 358
/*
 * Give syslogd a kick after everything else has been
 * initialized, otherwise it can occasionally fail to
 * route syslog messages properly on both IPv4 and IPv6.
 */
Ad Schellevis's avatar
Ad Schellevis committed
359 360 361 362
system_syslogd_start();

/* If there are ipsec dynamic hosts try again to reload the tunnels as rc.newipsecdns does */
if ($ipsec_dynamic_hosts) {
363
	ipsec_configure();
Ad Schellevis's avatar
Ad Schellevis committed
364 365 366
	filter_configure();
}

367 368 369
// generate configuration data for all installed templates
configd_run('template reload *');

Ad Schellevis's avatar
Ad Schellevis committed
370
led_normalize();