rc.bootup 10.2 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
function rescue_detect_keypress()
{
	$timeout = 9;

Ad Schellevis's avatar
Ad Schellevis committed
36
	echo "\n";
37 38 39
	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
40
	echo "Timeout before auto boot continues (seconds): {$timeout}";
41

Ad Schellevis's avatar
Ad Schellevis committed
42 43
	$key = null;
	exec("/bin/stty erase " . chr(8));
44
	while (!in_array($key, array("c", "C", "r","R", "i", "I"))) {
Ad Schellevis's avatar
Ad Schellevis committed
45 46 47 48 49 50 51
	        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
52 53 54
	        if ($timeout == 0) {
			break;
		}
Ad Schellevis's avatar
Ad Schellevis committed
55
	}
56 57 58

	echo "\n\n";

Ad Schellevis's avatar
Ad Schellevis committed
59
	if (in_array($key, array("r", "R"))) {
60
		echo "Recovery shell selected...\n\n";
Ad Schellevis's avatar
Ad Schellevis committed
61 62
		touch("/tmp/donotbootup");
		exit;
63 64
	} elseif (in_array($key, array("c", "C"))) {
		/* nothing */
Ad Schellevis's avatar
Ad Schellevis committed
65
	} else {
66 67 68 69 70 71
		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
72 73 74 75 76 77 78
	}
}

echo " done.\n";

echo "Initializing...";
echo ".";
79
require_once("globals.inc");
Ad Schellevis's avatar
Ad Schellevis committed
80
echo ".";
81
require_once("led.inc");
Ad Schellevis's avatar
Ad Schellevis committed
82 83 84 85 86 87 88 89 90 91
led_normalize();
echo ".";
if (led_count() >= 3) {
	led_kitt();
}

/* let the other functions know we're booting */
$pkg_interface = 'console';

/* parse the configuration and include all functions used below */
92
require_once("config.inc");
Ad Schellevis's avatar
Ad Schellevis committed
93
echo ".";
94
require_once("config.console.inc");
Ad Schellevis's avatar
Ad Schellevis committed
95
echo ".";
96
require_once("auth.inc");
Ad Schellevis's avatar
Ad Schellevis committed
97
echo ".";
98
require_once("functions.inc");
Ad Schellevis's avatar
Ad Schellevis committed
99
echo ".";
100
require_once("filter.inc");
Ad Schellevis's avatar
Ad Schellevis committed
101
echo ".";
102
require_once("shaper.inc");
Ad Schellevis's avatar
Ad Schellevis committed
103
echo ".";
104
require_once("ipsec.inc");
Ad Schellevis's avatar
Ad Schellevis committed
105
echo ".";
106
require_once("vpn.inc");
Ad Schellevis's avatar
Ad Schellevis committed
107
echo ".";
108
require_once("openvpn.inc");
Ad Schellevis's avatar
Ad Schellevis committed
109
echo ".";
110
require_once("captiveportal.inc");
Ad Schellevis's avatar
Ad Schellevis committed
111
echo ".";
112
require_once("rrd.inc");
Ad Schellevis's avatar
Ad Schellevis committed
113
echo ".";
114
require_once("pfsense-utils.inc");
Ad Schellevis's avatar
Ad Schellevis committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
echo ".";

/* get system memory amount */
$memory = get_memory();
$physmem = $memory[0];
$realmem = $memory[1];
echo " done.\n";

conf_mount_rw();

/* save dmesg output to file */
system_dmesg_save();

/* check whether config reset is desired (via hardware button on WRAP/ALIX) */
system_check_reset_button();

/* remove previous firmware upgrade if present */
132
if (file_exists("/root/firmware.tgz"))
Ad Schellevis's avatar
Ad Schellevis committed
133 134 135 136 137
	unlink("/root/firmware.tgz");

/* start devd (dhclient now uses it) */
echo "Starting device manager (devd)...";
mute_kernel_msgs();
138 139
exec('/sbin/devd');
sleep(1);
Ad Schellevis's avatar
Ad Schellevis committed
140 141 142 143
set_device_perms();
unmute_kernel_msgs();
echo "done.\n";

144 145
/* Display live system's early boot options */
if (is_install_media()) {
146 147
	rescue_detect_keypress();
}
Ad Schellevis's avatar
Ad Schellevis committed
148 149 150 151 152

echo "Loading configuration...";
parse_config_bootup();
echo "done.\n";

153 154 155 156 157 158 159 160
/*
 *  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.
 */
while(is_interface_mismatch() == true) {
	led_assigninterfaces();
	if (isset($config['revision'])) {
Franco Fichtner's avatar
Franco Fichtner committed
161 162 163
		if (file_exists('/tmp/missing_interfaces')) {
			echo "Warning: Configuration references interfaces that do not exist: " . file_get_contents('/tmp/missing_interfaces') . "\n";
		}
164 165 166 167 168 169 170
		echo "\nNetwork interface mismatch -- Running interface assignment option.\n";
	} else
		echo "\nDefault interfaces not found -- Running interface assignment option.\n";
	$ifaces = get_interface_list();
	if (is_array($ifaces)) {
		foreach($ifaces as $iface => $ifdata)
			interfaces_bring_up($iface);
Ad Schellevis's avatar
Ad Schellevis committed
171
	}
172 173
	set_networking_interfaces_ports();
	led_kitt();
Ad Schellevis's avatar
Ad Schellevis committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
}

/* convert config and clean backups */
echo "Updating configuration...";
convert_config();
echo "done.\n";

echo "Cleaning backup cache...";
cleanup_backupcache(true);
echo "done.\n";

/* 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();

/* 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...";
send_event("service reload sshd");
echo "done.\n";

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

/* setup interface microcode which improves tcp/ip speed */
echo "Setting up interfaces microcode...";
setup_microcode();
echo "done.\n";

/* set up interfaces */
226
mute_kernel_msgs();
Ad Schellevis's avatar
Ad Schellevis committed
227
interfaces_configure();
228
unmute_kernel_msgs();
Ad Schellevis's avatar
Ad Schellevis committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

/* 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();

/* setup altq + pf */
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";

if($realmem > 0 and $realmem < 65) {
	echo "System has less than 65 megabytes of ram {$realmem}.  Delaying webConfigurator startup.\n";
	/* start webConfigurator up on final pass */
261
	send_event('service restart webgui');
Ad Schellevis's avatar
Ad Schellevis committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
} else {
	/* start web server */
	system_webgui_start();
}

/* 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...";
/* At bootup this will just write the config, ntpd will launch from ntpdate_sync_once.sh */
system_ntp_configure(false);
mwexec_bg("/usr/local/sbin/ntpdate_sync_once.sh", true);
echo "done.\n";

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

294 295 296
/* configure console menu */
system_console_configure();

Ad Schellevis's avatar
Ad Schellevis committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
/* 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 */
send_event("service reload dyndnsall");

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

/* setup pppoe and pptp */
vpn_setup();

/* start the captive portal */
captiveportal_configure();

/* start Voucher support */
voucher_configure();

/* start IPsec tunnels */
$ipsec_dynamic_hosts = vpn_ipsec_configure();

/* start SNMP service */
services_snmpd_configure();

/* power down hard drive if needed/set */
system_set_harddisk_standby();

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

/* load graphing functions */
enable_rrd_graphing();

/* enable watchdog if supported */
enable_watchdog();

if($physmem < $g['minimum_ram_warning']) {
343
	require_once("notices.inc");
Ad Schellevis's avatar
Ad Schellevis committed
344 345 346 347 348 349 350 351 352 353 354 355
	file_notice("{$g['product_name']}MemoryRequirements", "{$g['product_name']} requires at least {$g['minimum_ram_warning_text']} of RAM.  Expect unusual performance.  This platform is not supported.", "Memory", "", 1);
	set_sysctl(array(
		"net.inet.tcp.recvspace" => "4096",
		"net.inet.tcp.sendspace" => "4096"
	));
}

/* 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');
$kern_hz = substr($kern_hz, strpos($kern_hz, "hz = ") + 5);
$kern_hz = substr($kern_hz, 0, strpos($kern_hz, ","));
356
if($kern_hz == "1000")
Ad Schellevis's avatar
Ad Schellevis committed
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	set_single_sysctl("net.inet.tcp.rexmit_min" , "30");

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

/* start the upnp daemon if it is enabled */
upnp_start();

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

/* Set preferred protocol */
prefer_ipv4_or_ipv6();

/* Remove the old shutdown binary if we kept it. */
if (file_exists("/sbin/shutdown.old"))
	@unlink("/sbin/shutdown.old");

/* Resync / Reinstall packages if need be */
if(file_exists('/conf/needs_package_sync')) {
	if($config['installedpackages'] <> '' && is_array($config['installedpackages']['package'])) {
		require_once("pkg-utils.inc");
		if($g['platform'] == "pfSense" || $g['platform'] == "nanobsd") {
			mark_subsystem_dirty('packagelock');
			pkg_reinstall_all();
			clear_subsystem_dirty('packagelock');
		}
	}
	@unlink('/conf/needs_package_sync');
}

/* 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 */
system_syslogd_start();


/* If there are ipsec dynamic hosts try again to reload the tunnels as rc.newipsecdns does */
if ($ipsec_dynamic_hosts) {
	vpn_ipsec_configure();
	filter_configure();
}

led_normalize();

conf_mount_ro();