Project

General

Profile

Download (11.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
require_once("globals.inc");
47
require_once("guiconfig.inc");
48

    
49
$curcfg = $config['system']['firmware'];
50

    
51
require_once("xmlrpc_client.inc");
52

    
53
/* Allow additional execution time 0 = no limit. */
54
ini_set('max_execution_time', '9999');
55
ini_set('max_input_time', '9999');
56

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

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

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

    
112
if($_POST['kerneltype']) {
113
	if($_POST['kerneltype'] == "single") 
114
		system("touch /boot/kernel/pfsense_kernel.txt");
115
	else 
116
		system("echo {$_POST['kerneltype']} > /boot/kernel/pfsense_kernel.txt");
117
}
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'], "Enable"))
126
		$mode = "enable";
127
	else if (stristr($_POST['Submit'], "Disable"))
128
		$mode = "disable";
129
	else if (stristr($_POST['Submit'], "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[] = "The uploaded image file is not for this platform ({$g['platform']}).";
149
				else if (!file_exists($_FILES['ulfile']['tmp_name'])) {
150
					/* probably out of memory for the MFS */
151
					$input_errors[] = "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 = "The digital signature on this image is invalid.";
163
					else if ($sigchk == 2)
164
						$sig_warning = "This image is not digitally signed.";
165
					else if (($sigchk == 3) || ($sigchk == 4))
166
						$sig_warning = "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[] = "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 = "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
								mwexec_bg("/etc/rc.firmware pfSenseupgrade {$g['upload_path']}/firmware.tgz");
189
                    } else {
190
                            $savemsg = "Firmware image missing or other error, please try again {$errortext}.";
191
                    }
192
            }
193
		}
194
	}
195
}
196

    
197
$pgtitle = array("Diagnostics","Firmware");
198
include("head.inc");
199

    
200
?>
201
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">	
202
<form action="system_firmware.php" method="post" enctype="multipart/form-data">
203
<?php
204
	/* Construct an upload_id for this session */
205
	$upload_id = "up". $_SESSION['Username'];
206
?>
207
<input type="hidden" name="UPLOAD_IDENTIFIER" value="<?php echo $upload_id;?>" /> 
208
<?php include("fbegin.inc"); ?>
209
<?php if ($input_errors) print_input_errors($input_errors); ?>
210
<?php if ($savemsg) print_info_box($savemsg); ?>
211
<?php if ($fwinfo <> "") print_info_box($fwinfo); ?>
212
<?php if ($sig_warning && !$input_errors): ?>
213
<?php
214
	$sig_warning = "<strong>" . $sig_warning . "</strong><br>This means that the image you uploaded " .
215
		"is not an official/supported image and may lead to unexpected behavior or security " .
216
		"compromises. Only install images that come from sources that you trust, and make sure ".
217
		"that the image has not been tampered with.<br><br>".
218
		"Do you want to install this image anyway (on your own risk)?";
219
print_info_box($sig_warning);
220
if(stristr($_FILES['ulfile']['name'],"nanobsd"))
221
	echo "<input type='hidden' name='isnano' id='isnano' value='yes'>\n";
222
?>
223
<input name="sig_override" type="submit" class="formbtn" id="sig_override" value=" Yes ">
224
<input name="sig_no" type="submit" class="formbtn" id="sig_no" value=" No ">
225
<?php else: ?>
226
<?php if (!is_subsystem_dirty('firmwarelock')): ?>
227
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
228
		<tr>
229
			<td>
230
<?php
231
	$tab_array = array();
232
	$tab_array[0] = array("Manual Update", true, "system_firmware.php");
233
	$tab_array[1] = array("Auto Update", false, "system_firmware_check.php");
234
	$tab_array[2] = array("Updater Settings", false, "system_firmware_settings.php");
235
	display_top_tabs($tab_array);
236
?>
237
			</td>
238
		</tr>
239
		<tr>
240
			<td>
241
				<div id="mainarea">
242
					<table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
243
                	<tr>
244
		 				<td colspan="2" class="listtopic">Invoke <?=$g['product_name']?> Manual Upgrade</td>
245
					</tr>
246
					<tr>
247
		  				<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
248
                  		<td width="78%" class="vtable">
249
						<p>
250
							Click &quot;Enable firmware
251
							upload&quot; below, then choose the image file (<?=$g['firmware_update_text'];?>)
252
							to be uploaded.
253
							<br>
254
							Click &quot;Upgrade firmware&quot;
255
							to start the upgrade process.
256
						</p>
257
						<?php if (!is_subsystem_dirty('rebootreq')): ?>
258
						<?php if (!is_subsystem_dirty('firmware')): ?>
259
							<input name="Submit" type="submit" class="formbtn" value="Enable firmware upload">
260
						<?php else: ?>
261
				  			<input name="Submit" type="submit" class="formbtn" value="Disable firmware upload">
262
							<br><br>
263
							<strong>Firmware image file: </strong>&nbsp;
264
							<input name="ulfile" type="file" class="formfld">
265
							<br><br>
266
							<?php
267
						  		if(!file_exists("/boot/kernel/pfsense_kernel.txt")) {
268
						  			if($g['platform'] == "pfSense") { 
269
										echo "Please select kernel type: ";
270
										echo "<select name='kerneltype'>";
271
										echo "<option value='SMP'>Multiprocessor kernel</option>";
272
										echo "<option value='single'>Uniprocessor kernel</option>";
273
										echo "<option value='wrap'>Embedded kernel</option>";
274
										echo "<option value='Developers'>Developers kernel</option>";
275
										echo "</select>";
276
										echo "<br><br>";
277
									}
278
								}
279
							?>
280
							<?php
281
								/*
282
								<input name="Submit" type="submit" class="formbtn" value="Upgrade firmware" onClick="window.open('upload_progress.php?upload_id=<?=$upload_id?>','UploadMeter','width=370,height=115', true); return true;">
283
								*/
284
							?>
285
							<input name="Submit" type="submit" class="formbtn" value="Upgrade firmware">
286
						<?php endif; else: ?>
287
							<strong>You must reboot the system before you can upgrade the firmware.</strong>
288
						<?php endif; ?>
289
					</td>
290
				</td>
291
			</tr>
292
			<tr>
293
				<td width="22%" valign="top">&nbsp;</td>
294
				<td width="78%"><span class="vexpl"><span class="red"><strong>Warning:<br>
295
				</strong></span>DO NOT abort the firmware upgrade once it
296
				has started. The firewall will reboot automatically after
297
				storing the new firmware. The configuration will be maintained.</span></td>
298
			</table>
299
		</div>
300
	</tr>
301
</table>
302

    
303
<?php endif; endif; ?>
304
<?php include("fend.inc"); ?>
305
</body>
306
</html>
(178-178/217)