Project

General

Profile

Download (12 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	system_firmware.php
5
	Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
6
        Copyright (C) 2013-2014 Electric Sheep Fencing, LP
7
	All rights reserved.
8

    
9
	originally part of m0n0wall (http://m0n0.ch/wall)
10
	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
*/
34
/*
35
	pfSense_BUILDER_BINARIES:	/usr/bin/tar
36
	pfSense_MODULE:	firmware
37
*/
38

    
39
##|+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
$d_isfwfile = 1;
47
$nocsrf = true;
48

    
49
require_once("globals.inc");
50
require_once("functions.inc");
51
require_once("guiconfig.inc");
52
require_once("xmlrpc_client.inc");
53

    
54
$curcfg = $config['system']['firmware'];
55

    
56
/* Allow additional execution time 0 = no limit. */
57
ini_set('max_execution_time', '9999');
58
ini_set('max_input_time', '9999');
59

    
60
function file_is_for_platform($filename, $ul_name) {
61
	global $g;
62
	if($g['platform'] == "nanobsd") {
63
		if(stristr($ul_name, "nanobsd"))
64
			return true;
65
		else
66
			return false;
67
	}
68
	$_gb = exec("/usr/bin/tar xzf $filename -C /tmp/ etc/platform");
69
	unset($_gb);
70
	if(!file_exists("/tmp/etc/platform"))
71
		return false;
72
	$upgrade_is_for_platform = trim(file_get_contents("/tmp/etc/platform", " \n\t\r"));
73
	if ($g['platform'] == $upgrade_is_for_platform) {
74
		@unlink("/tmp/etc/platform");
75
		return true;
76
	}
77
	return false;
78
}
79

    
80
function file_upload_error_message($error_code) {
81
	switch ($error_code) {
82
	case UPLOAD_ERR_INI_SIZE:
83
		return gettext('The uploaded file exceeds the upload_max_filesize directive in php.ini');
84
	case UPLOAD_ERR_FORM_SIZE:
85
		return gettext('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
86
	case UPLOAD_ERR_PARTIAL:
87
		return gettext('The uploaded file was only partially uploaded');
88
	case UPLOAD_ERR_NO_FILE:
89
		return gettext('No file was uploaded');
90
	case UPLOAD_ERR_NO_TMP_DIR:
91
		return gettext('Missing a temporary folder');
92
	case UPLOAD_ERR_CANT_WRITE:
93
		return gettext('Failed to write file to disk');
94
	case UPLOAD_ERR_EXTENSION:
95
		return gettext('File upload stopped by extension');
96
	default:
97
		return gettext('Unknown upload error');
98
	}
99
}
100

    
101
/* if upgrade in progress, alert user */
102
if(is_subsystem_dirty('firmwarelock')) {
103
	$pgtitle = array(gettext("System"),gettext("Firmware"),gettext("Manual Update"));
104
	include("head.inc");
105
	echo "<body link=\"#0000CC\" vlink=\"#0000CC\" alink=\"#0000CC\">\n";
106
	include("fbegin.inc");
107
	echo "<div>\n";
108
	print_info_box(gettext("An upgrade is currently in progress.<p>The firewall will reboot when the operation is complete.") . "</p><p><img src='/themes/{$g['theme']}/images/icons/icon_fw-update.gif' alt='update' /></p>");
109
	echo "</div>\n";
110
	include("fend.inc");
111
	echo "</body>";
112
	echo "</html>";
113
	exit;
114
}
115

    
116
if($_POST['backupbeforeupgrade'])
117
	touch("/tmp/perform_full_backup.txt");
118

    
119
/* Handle manual upgrade */
120
if ($_POST && !is_subsystem_dirty('firmwarelock')) {
121

    
122
	unset($input_errors);
123
	unset($sig_warning);
124

    
125
	if (stristr($_POST['Submit'], gettext("Enable")))
126
		$mode = "enable";
127
	else if (stristr($_POST['Submit'], gettext("Disable")))
128
		$mode = "disable";
129
	else if (stristr($_POST['Submit'], gettext("Upgrade")) || $_POST['sig_override'])
130
		$mode = "upgrade";
131
	else if ($_POST['sig_no']) {
132
		if(file_exists("{$g['upload_path']}/firmware.tgz"))
133
				unlink("{$g['upload_path']}/firmware.tgz");
134
	}
135
	if ($mode) {
136
		if ($mode == "enable") {
137
			conf_mount_rw();
138
			mark_subsystem_dirty('firmware');
139
		} else if ($mode == "disable") {
140
			conf_mount_ro();
141
			clear_subsystem_dirty('firmware');
142
		} else if ($mode == "upgrade") {
143
			if ($_FILES['ulfile']['error'])
144
				$errortext = "(" . file_upload_error_message($_FILES['ulfile']['error']) . ")";
145
			if (is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
146
				/* verify firmware image(s) */
147
				if (file_is_for_platform($_FILES['ulfile']['tmp_name'], $_FILES['ulfile']['name']) == false && !$_POST['sig_override'])
148
					$input_errors[] = gettext("The uploaded image file is not for this platform.");
149
				else if (!file_exists($_FILES['ulfile']['tmp_name'])) {
150
					/* probably out of memory for the MFS */
151
					$input_errors[] = gettext("Image upload failed (out of memory?)");
152
					mwexec("/etc/rc.firmware disable");
153
					clear_subsystem_dirty('firmware');
154
				} else {
155
					/* move the image so PHP won't delete it */
156
					rename($_FILES['ulfile']['tmp_name'], "{$g['upload_path']}/firmware.tgz");
157

    
158
					/* check digital signature */
159
					$sigchk = verify_digital_signature("{$g['upload_path']}/firmware.tgz");
160

    
161
					if ($sigchk == 1)
162
						$sig_warning = gettext("The digital signature on this image is invalid.");
163
					else if ($sigchk == 2 && !isset($config['system']['firmware']['allowinvalidsig']))
164
						$sig_warning = gettext("This image is not digitally signed.");
165
					else if (($sigchk >= 3))
166
						$sig_warning = gettext("There has been an error verifying the signature on this image.");
167

    
168
					if (!verify_gzip_file("{$g['upload_path']}/firmware.tgz")) {
169
						$input_errors[] = gettext("The image file is corrupt.");
170
						unlink("{$g['upload_path']}/firmware.tgz");
171
					}
172
				}
173
			}
174

    
175
			run_plugins("/usr/local/pkg/firmware_upgrade");
176

    
177
			/* Check for input errors, firmware locks, warnings, then check for firmware if sig_override is set */
178
			if (!$input_errors && !is_subsystem_dirty('firmwarelock') && (!$sig_warning || $_POST['sig_override'])) {
179
				if (file_exists("{$g['upload_path']}/firmware.tgz")) {
180
					/* fire up the update script in the background */
181
					mark_subsystem_dirty('firmwarelock');
182
					$savemsg = gettext("The firmware is now being updated. The firewall will reboot automatically.");
183
					if (stristr($_FILES['ulfile']['name'],"nanobsd") or $_POST['isnano'] == "yes")
184
						mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade {$g['upload_path']}/firmware.tgz");
185
					else if(stristr($_FILES['ulfile']['name'],"bdiff"))
186
						mwexec_bg("/etc/rc.firmware delta_update {$g['upload_path']}/firmware.tgz");
187
					else  {
188
						if($g['platform'] == "nanobsd")
189
							$whichone = "pfSenseNanoBSDupgrade";
190
						else
191
							$whichone = "pfSenseupgrade";
192
						mwexec_bg("/etc/rc.firmware {$whichone} {$g['upload_path']}/firmware.tgz");
193
						unset($whichone);
194
					}
195
				} else
196
					$savemsg = sprintf(gettext("Firmware image missing or other error, please try again %s."),$errortext);
197
			}
198
		}
199
	}
200
}
201

    
202
$pgtitle = array(gettext("System"),gettext("Firmware"));
203
include("head.inc");
204

    
205
?>
206
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
207
<form action="system_firmware.php" method="post" enctype="multipart/form-data">
208
<?php
209
	/* Construct an upload_id for this session */
210
	if (!session_id())
211
		$upload_id = uniqid();
212
	else
213
		$upload_id = session_id();
214
?>
215
<input type="hidden" name="UPLOAD_IDENTIFIER" value="<?php echo $upload_id;?>" />
216
<?php include("fbegin.inc"); ?>
217
<?php if ($input_errors) print_input_errors($input_errors); ?>
218
<?php if ($savemsg) print_info_box($savemsg); ?>
219
<?php if ($fwinfo <> "") print_info_box($fwinfo); ?>
220
<?php if ($sig_warning && !$input_errors): ?>
221
<?php
222
	$sig_warning = "<strong>" . $sig_warning . "</strong><br />" . gettext("This means that the image you uploaded " .
223
		"is not an official/supported image and may lead to unexpected behavior or security " .
224
		"compromises. Only install images that come from sources that you trust, and make sure ".
225
		"that the image has not been tampered with.") . "<br /><br />".
226
		gettext("Do you want to install this image anyway (on your own risk)?");
227
print_info_box($sig_warning);
228
if(stristr($_FILES['ulfile']['name'],"nanobsd"))
229
	echo "<input type='hidden' name='isnano' id='isnano' value='yes' />\n";
230
?>
231
<input name="sig_override" type="submit" class="formbtn" id="sig_override" value=" <?=gettext("Yes");?> " />
232
<input name="sig_no" type="submit" class="formbtn" id="sig_no" value=" <?=gettext("No"); ?> " />
233
<?php else: ?>
234
<?php if (!is_subsystem_dirty('firmwarelock')): ?>
235
	<table width="100%" border="0" cellpadding="0" cellspacing="0" summary="firmware">
236
		<tr>
237
			<td>
238
<?php
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
	display_top_tabs($tab_array);
246
?>
247
			</td>
248
		</tr>
249
		<tr>
250
			<td>
251
				<div id="mainarea">
252
					<table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
253
					<tr>
254
						<td colspan="2" class="listtopic"><?=gettext("Invoke") ." ". $g['product_name'] ." ".  gettext("Manual Upgrade"); ?></td>
255
					</tr>
256
					<tr>
257
						<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
258
						<td width="78%" class="vtable">
259
						<?php if (!is_subsystem_dirty('rebootreq')): ?>
260
						<?php if (!is_subsystem_dirty('firmware')): ?>
261
						<input name="Submit" type="submit" class="formbtn" value="<?=gettext("Enable firmware upload");?>" />
262
						<br />
263
							<?php printf(gettext('Click "Enable firmware upload" to begin.'),$g['firmware_update_text']);?>
264
						<br />
265
						<?php else: ?>
266
							<input name="Submit" type="submit" class="formbtn" value="<?=gettext("Disable firmware upload");?>" />
267
					</td>
268
					</tr>
269
					<tr>
270
						<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
271
						<td width="78%" class="vtable">
272
							<?php
273
								if ($g['platform'] == "nanobsd")
274
									$type = "*.img.gz";
275
								else
276
									$type = "*.tgz";
277
							?>
278
							<strong><?=gettext("Firmware image file ($type):");?> </strong>
279
							<input name="ulfile" type="file" class="formfld" />
280
							<br />
281
							<?php if ($g['hidebackupbeforeupgrade'] === false): ?>
282
							<input type="checkbox" name='backupbeforeupgrade' id='backupbeforeupgrade' /> <?=gettext("Perform full backup prior to upgrade");?>
283
							<br />
284
							<?php endif; ?>
285
							<input name="Submit" type="submit" class="formbtn" value="<?=gettext("Upgrade firmware");?>" />
286
							<?=gettext('Click "Upgrade firmware" to start the upgrade process.');?>
287
						<?php endif; else: ?>
288
							<strong><?=gettext("You must reboot the system before you can upgrade the firmware.");?></strong>
289
						<?php endif; ?>
290
				</td>
291
			</tr>
292
			<tr>
293
				<td width="22%" valign="top">&nbsp;</td>
294
				<td width="78%">
295
						<?php if (is_subsystem_dirty('firmware')): ?>
296
					<span class="vexpl">
297
						<span class="red">
298
							<strong>
299
								<?=gettext("Warning:");?><br />
300
							</strong>
301
						</span>
302
						<?=gettext("DO NOT abort the firmware upgrade once it " .
303
								"has started. The firewall will reboot automatically after " .
304
								"storing the new firmware. The configuration will be maintained.");?>
305
					</span>
306
						<?php endif; ?>
307
				</td></tr>
308
			</table>
309
		</div>
310
	</td></tr>
311
</table>
312

    
313
<?php endif; endif; ?>
314
<?php include("fend.inc"); ?>
315
</form>
316
</body>
317
</html>
(215-215/256)