Project

General

Profile

Download (13 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
require_once("xmlrpc_client.inc");
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
	exec("/usr/bin/tar xzf $filename -C /tmp/ etc/platform");
69
	if(!file_exists("/tmp/etc/platform")) 
70
		return false;
71
	$upgrade_is_for_platform = trim(file_get_contents("/tmp/etc/platform"));
72
	if($g['platform'] == $upgrade_is_for_platform) {
73
		unlink("/tmp/etc/platform");
74
		return true;
75
	}
76
	return false;
77
}
78

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

    
100
/* if upgrade in progress, alert user */
101
if(is_subsystem_dirty('firmwarelock')) {
102
	$pgtitle = array(gettext("System"),gettext("Firmware"),gettext("Manual Update"));
103
	include("head.inc");
104
	echo "<body link=\"#0000CC\" vlink=\"#0000CC\" alink=\"#0000CC\">\n";
105
	include("fbegin.inc");
106
	echo "<div>\n";
107
	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'>");
108
	echo "</div>\n";
109
	include("fend.inc");
110
	echo "</body>";
111
	echo "</html>";
112
	exit;
113
}
114

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

    
118
if($_POST['kerneltype']) {
119
	if($_POST['kerneltype'] == "single") 
120
		system("touch /boot/kernel/pfsense_kernel.txt");
121
	else 
122
		system("echo {$_POST['kerneltype']} > /boot/kernel/pfsense_kernel.txt");
123
}
124

    
125
/* Handle manual upgrade */
126
if ($_POST && !is_subsystem_dirty('firmwarelock')) {
127
	
128
	unset($input_errors);
129
	unset($sig_warning);
130

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

    
164
					/* check digital signature */
165
					$sigchk = verify_digital_signature("{$g['upload_path']}/firmware.tgz");
166

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

    
174
					if (!verify_gzip_file("{$g['upload_path']}/firmware.tgz")) {
175
						$input_errors[] = gettext("The image file is corrupt.");
176
						unlink("{$g['upload_path']}/firmware.tgz");
177
					}
178
				}
179
			}
180

    
181
			run_plugins("/usr/local/pkg/firmware_upgrade");
182

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

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

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

    
330
<?php endif; endif; ?>
331
<?php include("fend.inc"); ?>
332
</body>
333
</html>
(199-199/239)