Project

General

Profile

Download (11.2 KB) Statistics
| Branch: | Tag: | Revision:
1 4668f9f7 Scott Ullrich
<?php
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4 580182e2 Colin Smith
	system_firmware.php
5 1cecfbf7 Scott Ullrich
	Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
6 ce77a9c4 Phil Davis
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
7 1cecfbf7 Scott Ullrich
	All rights reserved.
8 580182e2 Colin Smith
9 1cecfbf7 Scott Ullrich
	originally part of m0n0wall (http://m0n0.ch/wall)
10 580182e2 Colin Smith
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
	All rights reserved.
12
13
	Redistribution and use in source and binary forms, with or without
14
	modification, are permitted provided that the following conditions are met:
15
16
	1. Redistributions of source code must retain the above copyright notice,
17
	   this list of conditions and the following disclaimer.
18
19
	2. Redistributions in binary form must reproduce the above copyright
20
	   notice, this list of conditions and the following disclaimer in the
21
	   documentation and/or other materials provided with the distribution.
22
23
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
	POSSIBILITY OF SUCH DAMAGE.
33 5b237745 Scott Ullrich
*/
34 1d333258 Scott Ullrich
/*
35 4b805dbc Renato Botelho
	pfSense_BUILDER_BINARIES:	/usr/bin/tar
36 5387537e sbeaver
	pfSense_MODULE: firmware
37 1d333258 Scott Ullrich
*/
38 5b237745 Scott Ullrich
39 6b07c15a Matthew Grooms
##|+PRIV
40
##|*IDENT=page-system-firmware-manualupdate
41
##|*NAME=System: Firmware: Manual Update page
42
##|*DESCR=Allow access to the 'System: Firmware: Manual Update' page.
43
##|*MATCH=system_firmware.php*
44
##|-PRIV
45
46 3958d63b Colin Smith
$d_isfwfile = 1;
47 7385a6b4 Scott Ullrich
$nocsrf = true;
48
49 f0394a03 Scott Ullrich
require_once("globals.inc");
50 c1605b35 Scott Ullrich
require_once("functions.inc");
51 6605faea Scott Ullrich
require_once("guiconfig.inc");
52 f03ed350 Ermal
require_once("xmlrpc_client.inc");
53 da55e467 Scott Ullrich
54
$curcfg = $config['system']['firmware'];
55
56 47d11b79 Mark Crane
/* Allow additional execution time 0 = no limit. */
57 8999038a Scott Ullrich
ini_set('max_execution_time', '9999');
58
ini_set('max_input_time', '9999');
59 0045dfd1 Scott Ullrich
60 d2d86ca3 Scott Ullrich
function file_is_for_platform($filename, $ul_name) {
61 f0394a03 Scott Ullrich
	global $g;
62 0e88de0c Phil Davis
	if ($g['platform'] == "nanobsd") {
63
		if (stristr($ul_name, "nanobsd")) {
64 7475fc0b Scott Ullrich
			return true;
65 0e88de0c Phil Davis
		} else {
66 4b805dbc Renato Botelho
			return false;
67 0e88de0c Phil Davis
		}
68 7475fc0b Scott Ullrich
	}
69 e6d5af4b Ermal
	$_gb = exec("/usr/bin/tar xzf $filename -C /tmp/ etc/platform");
70
	unset($_gb);
71 0e88de0c Phil Davis
	if (!file_exists("/tmp/etc/platform")) {
72 fe38f1da Scott Ullrich
		return false;
73 0e88de0c Phil Davis
	}
74 e6d5af4b Ermal
	$upgrade_is_for_platform = trim(file_get_contents("/tmp/etc/platform", " \n\t\r"));
75
	if ($g['platform'] == $upgrade_is_for_platform) {
76
		@unlink("/tmp/etc/platform");
77 f0394a03 Scott Ullrich
		return true;
78
	}
79
	return false;
80
}
81
82 cdd3238d Scott Ullrich
function file_upload_error_message($error_code) {
83 4b805dbc Renato Botelho
	switch ($error_code) {
84 0e88de0c Phil Davis
		case UPLOAD_ERR_INI_SIZE:
85
			return gettext('The uploaded file exceeds the upload_max_filesize directive in php.ini');
86
		case UPLOAD_ERR_FORM_SIZE:
87
			return gettext('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
88
		case UPLOAD_ERR_PARTIAL:
89
			return gettext('The uploaded file was only partially uploaded');
90
		case UPLOAD_ERR_NO_FILE:
91
			return gettext('No file was uploaded');
92
		case UPLOAD_ERR_NO_TMP_DIR:
93
			return gettext('Missing a temporary folder');
94
		case UPLOAD_ERR_CANT_WRITE:
95
			return gettext('Failed to write file to disk');
96
		case UPLOAD_ERR_EXTENSION:
97
			return gettext('File upload stopped by extension');
98
		default:
99
			return gettext('Unknown upload error');
100 4b805dbc Renato Botelho
	}
101 cdd3238d Scott Ullrich
}
102
103 8b7c81d7 Scott Ullrich
/* if upgrade in progress, alert user */
104 0e88de0c Phil Davis
if (is_subsystem_dirty('firmwarelock')) {
105
	$pgtitle = array(gettext("System"), gettext("Firmware"), gettext("Manual Update"));
106 8b7c81d7 Scott Ullrich
	include("head.inc");
107
	include("fbegin.inc");
108 5387537e sbeaver
	print_info_box(gettext("An upgrade is currently in progress. The firewall will reboot when the operation is complete.") . "<p><img src='/themes/{$g['theme']}/images/icons/icon_fw-update.gif' alt='update' /></p>");
109
	include("foot.inc");
110 8b7c81d7 Scott Ullrich
	exit;
111
}
112
113 0e88de0c Phil Davis
if ($_POST['backupbeforeupgrade']) {
114 b2a67b55 Scott Ullrich
	touch("/tmp/perform_full_backup.txt");
115 0e88de0c Phil Davis
}
116 b2a67b55 Scott Ullrich
117 aa08f46b Bill Marquette
/* Handle manual upgrade */
118 a368a026 Ermal Lu?i
if ($_POST && !is_subsystem_dirty('firmwarelock')) {
119 4b805dbc Renato Botelho
120 580182e2 Colin Smith
	unset($input_errors);
121
	unset($sig_warning);
122
123 0e88de0c Phil Davis
	if (stristr($_POST['Submit'], gettext("Enable"))) {
124 580182e2 Colin Smith
		$mode = "enable";
125 0e88de0c Phil Davis
	} else if (stristr($_POST['Submit'], gettext("Disable"))) {
126 580182e2 Colin Smith
		$mode = "disable";
127 0e88de0c Phil Davis
	} else if (stristr($_POST['Submit'], gettext("Upgrade")) || $_POST['sig_override']) {
128 580182e2 Colin Smith
		$mode = "upgrade";
129 0e88de0c Phil Davis
	} else if ($_POST['sig_no']) {
130
		if (file_exists("{$g['upload_path']}/firmware.tgz")) {
131
			unlink("{$g['upload_path']}/firmware.tgz");
132
		}
133 580182e2 Colin Smith
	}
134
	if ($mode) {
135
		if ($mode == "enable") {
136 b6f67235 Scott Ullrich
			conf_mount_rw();
137 a368a026 Ermal Lu?i
			mark_subsystem_dirty('firmware');
138 580182e2 Colin Smith
		} else if ($mode == "disable") {
139 b6f67235 Scott Ullrich
			conf_mount_ro();
140 a368a026 Ermal Lu?i
			clear_subsystem_dirty('firmware');
141 580182e2 Colin Smith
		} else if ($mode == "upgrade") {
142 0e88de0c Phil Davis
			if ($_FILES['ulfile']['error']) {
143 4b805dbc Renato Botelho
				$errortext = "(" . file_upload_error_message($_FILES['ulfile']['error']) . ")";
144 0e88de0c Phil Davis
			}
145 580182e2 Colin Smith
			if (is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
146
				/* verify firmware image(s) */
147 0e88de0c Phil Davis
				if (file_is_for_platform($_FILES['ulfile']['tmp_name'], $_FILES['ulfile']['name']) == false && !$_POST['sig_override']) {
148 33a2693c Chris Buechler
					$input_errors[] = gettext("The uploaded image file is not for this platform.");
149 0e88de0c Phil Davis
				} else if (!file_exists($_FILES['ulfile']['tmp_name'])) {
150 580182e2 Colin Smith
					/* probably out of memory for the MFS */
151 1b4f376d Vinicius Coque
					$input_errors[] = gettext("Image upload failed (out of memory?)");
152 0d64af59 Ermal Lu?i
					mwexec("/etc/rc.firmware disable");
153 a368a026 Ermal Lu?i
					clear_subsystem_dirty('firmware');
154 580182e2 Colin Smith
				} else {
155
					/* move the image so PHP won't delete it */
156 1ef7b568 Scott Ullrich
					rename($_FILES['ulfile']['tmp_name'], "{$g['upload_path']}/firmware.tgz");
157 580182e2 Colin Smith
158
					/* check digital signature */
159 1ef7b568 Scott Ullrich
					$sigchk = verify_digital_signature("{$g['upload_path']}/firmware.tgz");
160 580182e2 Colin Smith
161 0e88de0c Phil Davis
					if ($sigchk == 1) {
162 1b4f376d Vinicius Coque
						$sig_warning = gettext("The digital signature on this image is invalid.");
163 0e88de0c Phil Davis
					} else if ($sigchk == 2 && !isset($config['system']['firmware']['allowinvalidsig'])) {
164 1b4f376d Vinicius Coque
						$sig_warning = gettext("This image is not digitally signed.");
165 0e88de0c Phil Davis
					} else if (($sigchk >= 3)) {
166 1b4f376d Vinicius Coque
						$sig_warning = gettext("There has been an error verifying the signature on this image.");
167 0e88de0c Phil Davis
					}
168 580182e2 Colin Smith
169 709f48f0 Scott Ullrich
					if (!verify_gzip_file("{$g['upload_path']}/firmware.tgz")) {
170 1b4f376d Vinicius Coque
						$input_errors[] = gettext("The image file is corrupt.");
171 1ef7b568 Scott Ullrich
						unlink("{$g['upload_path']}/firmware.tgz");
172 580182e2 Colin Smith
					}
173
				}
174
			}
175
176 1834f481 Scott Ullrich
			run_plugins("/usr/local/pkg/firmware_upgrade");
177
178 e6d5af4b Ermal
			/* Check for input errors, firmware locks, warnings, then check for firmware if sig_override is set */
179
			if (!$input_errors && !is_subsystem_dirty('firmwarelock') && (!$sig_warning || $_POST['sig_override'])) {
180
				if (file_exists("{$g['upload_path']}/firmware.tgz")) {
181
					/* fire up the update script in the background */
182
					mark_subsystem_dirty('firmwarelock');
183
					$savemsg = gettext("The firmware is now being updated. The firewall will reboot automatically.");
184 0e88de0c Phil Davis
					if (stristr($_FILES['ulfile']['name'], "nanobsd") or $_POST['isnano'] == "yes") {
185 e6d5af4b Ermal
						mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade {$g['upload_path']}/firmware.tgz");
186 6c07db48 Phil Davis
					} else {
187 0e88de0c Phil Davis
						if ($g['platform'] == "nanobsd") {
188 e6d5af4b Ermal
							$whichone = "pfSenseNanoBSDupgrade";
189 0e88de0c Phil Davis
						} else {
190 e6d5af4b Ermal
							$whichone = "pfSenseupgrade";
191 0e88de0c Phil Davis
						}
192 e6d5af4b Ermal
						mwexec_bg("/etc/rc.firmware {$whichone} {$g['upload_path']}/firmware.tgz");
193
						unset($whichone);
194
					}
195 0e88de0c Phil Davis
				} else {
196
					$savemsg = sprintf(gettext("Firmware image missing or other error, please try again %s."), $errortext);
197
				}
198 e6d5af4b Ermal
			}
199 580182e2 Colin Smith
		}
200
	}
201 5b237745 Scott Ullrich
}
202 e2fa4962 Scott Ullrich
203 0e88de0c Phil Davis
$pgtitle = array(gettext("System"), gettext("Firmware"));
204 52380979 Scott Ullrich
include("head.inc");
205
206 5387537e sbeaver
if ($input_errors)
207
	print_input_errors($input_errors);
208
209
if ($savemsg)
210
	print_info_box($savemsg);
211
212 1be02f50 sbeaver
if ($fwinfo != "")
213 5387537e sbeaver
	print_info_box($fwinfo);
214
215
$tab_array = array();
216
$tab_array[] = array(gettext("Manual Update"), true, "system_firmware.php");
217
$tab_array[] = array(gettext("Auto Update"), false, "system_firmware_check.php");
218
$tab_array[] = array(gettext("Updater Settings"), false, "system_firmware_settings.php");
219
if($g['hidedownloadbackup'] == false)
220
	$tab_array[] = array(gettext("Restore Full Backup"), false, "system_firmware_restorefullbackup.php");
221
222
display_top_tabs($tab_array);
223
224
// For a simple yes/no we can use an HTML form
225
if ($sig_warning && !$input_errors) {
226
	$sig_warning = gettext("The image you uploaded " .
227 77a89fb6 Scott Ullrich
		"is not an official/supported image and may lead to unexpected behavior or security " .
228
		"compromises. Only install images that come from sources that you trust, and make sure ".
229 8cd558b6 ayvis
		"that the image has not been tampered with.") . "<br /><br />".
230 1b4f376d Vinicius Coque
		gettext("Do you want to install this image anyway (on your own risk)?");
231 5387537e sbeaver
232
	print_info_box($sig_warning);
233 5b237745 Scott Ullrich
?>
234 5387537e sbeaver
	<form action="system_firmware.php" method="post" enctype="multipart/form-data">
235 38e06c66 Sjon Hortensius
		<input name="sig_override" type="submit" class="btn btn-danger" id="sig_override" value=" <?=gettext("Yes");?> " />
236
		<input name="sig_no" type="submit" class="btn btn-default" id="sig_no" value=" <?=gettext("No"); ?> " />
237
	</form>
238 4820d297 Scott Ullrich
<?php
239 5387537e sbeaver
240
} else {
241
	// This is where the work gets done so Forms.classes will be used from this point
242
	if (!is_subsystem_dirty('firmwarelock')) {
243 ad2879b8 PiBa-NL
		require_once('classes/Form.class.php');
244 5387537e sbeaver
245
		if (!is_subsystem_dirty('rebootreq')) {
246 45e630d7 sbeaver
			// Provide a button to enable firmware upgrades. Upgrades should be disabled on initial page load
247 a4af095c Renato Botelho
			if (!is_subsystem_dirty('firmware') || !$_POST || $_POST['save']) {
248 1be02f50 sbeaver
				$enablebtn = new Form_Button(
249
					'Submit',
250
					'Enable firmware upload'
251
					);
252
253 5387537e sbeaver
				$enablebtn->addClass('btn-warning');
254
				$form = new Form($enablebtn);
255
				$section = new Form_Section('Invoke ' . $g['product_name'] .' Manual Upgrade');
256
				$section->addInput(new Form_StaticText('Enable', 'Click the "Enable firmware upload" button below to begin.'));
257
			}
258
			else {
259 1be02f50 sbeaver
				// Upgrades are now enabled
260 38e06c66 Sjon Hortensius
				$form = new Form('Disable firmware upload');
261 5387537e sbeaver
262
				$form->setMultipartEncoding();
263
264
				$section = new Form_Section('Perform ' . $g['product_name'] .' Manual Upgrade');
265
266
				if (!session_id())
267
					$upload_id = uniqid();
268
				else
269
					$upload_id = session_id();
270
271
				$section->addInput(new Form_Input(
272
					'UPLOAD_IDENTIFIER',
273
					'',
274
					'hidden',
275 45e630d7 sbeaver
					$upload_id
276 5387537e sbeaver
					));
277
278
				if(stristr($_FILES['ulfile']['name'],"nanobsd")) {
279
					$section->addInput(new Form_Input(
280
						'isnano',
281
						'',
282
						'hidden',
283 45e630d7 sbeaver
						'yes'
284 5387537e sbeaver
						));
285
				}
286
287
				if ($g['platform'] == "nanobsd")
288
					$type = "*.img.gz";
289
				else
290
					$type = "*.tgz";
291
292
				$filepicker = new Form_Input(
293
					'ulfile',
294
					'File to upload (' . $type . ')',
295
					'file',
296 45e630d7 sbeaver
					''
297 5387537e sbeaver
				);
298
299
				$section->addInput($filepicker)->setHelp('Choose the file you wish to upload');
300
301
				if ($g['hidebackupbeforeupgrade'] === false) {
302
					$section->addInput(new Form_Checkbox(
303
						'backupbeforeupgrade',
304
						Backup,
305 9b2bd81e Stephen Beaver
						'Perform a full backup prior to upgrade',
306 5387537e sbeaver
						false
307
					));
308
				}
309
310
				$section->addInput(new Form_Button(
311
					'submit',
312
					'Upgrade firmware'
313 9b2bd81e Stephen Beaver
				))->addClass('btn-danger btn-sm')->setHelp('Click the "Upgrade firmware" button above to start the upgrade process');
314 5387537e sbeaver
			}
315
316
			$form->add($section);
317
			print($form);
318
		}
319
	}
320
	else {
321
		print_info_box('<strong>' . gettext("You must reboot the system before you can upgrade the firmware.") . '</strong>');
322
	}
323
324
	if (is_subsystem_dirty('firmware') && !is_subsystem_dirty('firmwarelock')) {
325
		print_info_box('<strong>' . gettext("DO NOT ") . '</strong>' . gettext('abort the firmware upgrade once it ' .
326
			'has started. The firewall will reboot automatically after ' .
327
			'storing the new firmware. The configuration will be maintained.'));
328
	}
329
}
330
331
include("foot.inc"); ?>