Project

General

Profile

Download (12.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	system_firmware.php
5
	Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
6
	All rights reserved.
7

    
8
	originally part of m0n0wall (http://m0n0.ch/wall)
9
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
10
	All rights reserved.
11

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

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

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

    
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33
/*
34
	pfSense_BUILDER_BINARIES:	/usr/bin/tar	
35
	pfSense_MODULE:	firmware
36
*/
37

    
38
##|+PRIV
39
##|*IDENT=page-system-firmware-manualupdate
40
##|*NAME=System: Firmware: Manual Update page
41
##|*DESCR=Allow access to the 'System: Firmware: Manual Update' page.
42
##|*MATCH=system_firmware.php*
43
##|-PRIV
44

    
45
$d_isfwfile = 1;
46
$nocsrf = true;
47

    
48
require_once("globals.inc");
49
require_once("functions.inc");
50
require_once("guiconfig.inc");
51

    
52
$curcfg = $config['system']['firmware'];
53

    
54
$kerneltypes = array(
55
	'SMP' => gettext("Standard Kernel"),
56
	'wrap' => gettext("Embedded Kernel"),
57
);
58

    
59
require_once("xmlrpc_client.inc");
60

    
61
/* Allow additional execution time 0 = no limit. */
62
ini_set('max_execution_time', '9999');
63
ini_set('max_input_time', '9999');
64

    
65
function file_is_for_platform($filename, $ul_name) {
66
	global $g;
67
	if($g['platform'] == "nanobsd") {
68
		if(stristr($ul_name, "nanobsd"))
69
			return true;
70
		else
71
			return false;		
72
	}
73
	exec("/usr/bin/tar xzf $filename -C /tmp/ etc/platform");
74
	if(!file_exists("/tmp/etc/platform")) 
75
		return false;
76
	$upgrade_is_for_platform = trim(file_get_contents("/tmp/etc/platform"));
77
	if($g['platform'] == $upgrade_is_for_platform) {
78
		unlink("/tmp/etc/platform");
79
		return true;
80
	}
81
	return false;
82
}
83

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

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

    
120
if($_POST['backupbeforeupgrade']) 
121
	touch("/tmp/perform_full_backup.txt");
122

    
123
if($_POST['kerneltype'] && in_array($_POST['kerneltype'], array_keys($kerneltypes))) {
124
	if($_POST['kerneltype'] == "single") 
125
		touch("/boot/kernel/pfsense_kernel.txt");
126
	else 
127
		file_put_contents("/boot/kernel/pfsense_kernel.txt", $_POST['kerneltype']);
128
}
129

    
130
/* Handle manual upgrade */
131
if ($_POST && !is_subsystem_dirty('firmwarelock')) {
132
	
133
	unset($input_errors);
134
	unset($sig_warning);
135

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

    
169
					/* check digital signature */
170
					$sigchk = verify_digital_signature("{$g['upload_path']}/firmware.tgz");
171

    
172
					if ($sigchk == 1)
173
						$sig_warning = gettext("The digital signature on this image is invalid.");
174
					else if ($sigchk == 2 && !isset($config['system']['firmware']['allowinvalidsig']))
175
						$sig_warning = gettext("This image is not digitally signed.");
176
					else if (($sigchk >= 3))
177
						$sig_warning = gettext("There has been an error verifying the signature on this image.");
178

    
179
					if (!verify_gzip_file("{$g['upload_path']}/firmware.tgz")) {
180
						$input_errors[] = gettext("The image file is corrupt.");
181
						unlink("{$g['upload_path']}/firmware.tgz");
182
					}
183
				}
184
			}
185

    
186
			run_plugins("/usr/local/pkg/firmware_upgrade");
187

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

    
208
$pgtitle = array(gettext("System"),gettext("Firmware"));
209
include("head.inc");
210

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

    
332
<?php endif; endif; ?>
333
<?php include("fend.inc"); ?>
334
</body>
335
</html>
(209-209/249)