Project

General

Profile

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