1
|
<?php
|
2
|
/* $Id$ */
|
3
|
/*
|
4
|
system_firmware.php
|
5
|
Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
|
6
|
Copyright (C) 2013-2015 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
|
}
|
69
|
$_gb = exec("/usr/bin/tar xzf $filename -C /tmp/ etc/platform");
|
70
|
unset($_gb);
|
71
|
if (!file_exists("/tmp/etc/platform")) {
|
72
|
return false;
|
73
|
}
|
74
|
$upgrade_is_for_platform = trim(file_get_contents("/tmp/etc/platform", " \n\t\r"));
|
75
|
if ($g['platform'] == $upgrade_is_for_platform) {
|
76
|
@unlink("/tmp/etc/platform");
|
77
|
return true;
|
78
|
}
|
79
|
return false;
|
80
|
}
|
81
|
|
82
|
function file_upload_error_message($error_code) {
|
83
|
switch ($error_code) {
|
84
|
case UPLOAD_ERR_INI_SIZE:
|
85
|
return gettext('The uploaded file exceeds the upload_max_filesize directive in php.ini');
|
86
|
case UPLOAD_ERR_FORM_SIZE:
|
87
|
return gettext('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
|
88
|
case UPLOAD_ERR_PARTIAL:
|
89
|
return gettext('The uploaded file was only partially uploaded');
|
90
|
case UPLOAD_ERR_NO_FILE:
|
91
|
return gettext('No file was uploaded');
|
92
|
case UPLOAD_ERR_NO_TMP_DIR:
|
93
|
return gettext('Missing a temporary folder');
|
94
|
case UPLOAD_ERR_CANT_WRITE:
|
95
|
return gettext('Failed to write file to disk');
|
96
|
case UPLOAD_ERR_EXTENSION:
|
97
|
return gettext('File upload stopped by extension');
|
98
|
default:
|
99
|
return gettext('Unknown upload error');
|
100
|
}
|
101
|
}
|
102
|
|
103
|
/* if upgrade in progress, alert user */
|
104
|
if (is_subsystem_dirty('firmwarelock')) {
|
105
|
$pgtitle = array(gettext("System"), gettext("Firmware"), gettext("Manual Update"));
|
106
|
include("head.inc");
|
107
|
include("fbegin.inc");
|
108
|
print_info_box(gettext("An upgrade is currently in progress. The firewall will reboot when the operation is complete.") . "<p><img src='/themes/{$g['theme']}/images/icons/icon_fw-update.gif' alt='update' /></p>");
|
109
|
include("foot.inc");
|
110
|
exit;
|
111
|
}
|
112
|
|
113
|
if ($_POST['backupbeforeupgrade']) {
|
114
|
touch("/tmp/perform_full_backup.txt");
|
115
|
}
|
116
|
|
117
|
/* Handle manual upgrade */
|
118
|
if ($_POST && !is_subsystem_dirty('firmwarelock')) {
|
119
|
|
120
|
unset($input_errors);
|
121
|
unset($sig_warning);
|
122
|
|
123
|
if (stristr($_POST['Submit'], gettext("Enable"))) {
|
124
|
$mode = "enable";
|
125
|
} else if (stristr($_POST['Submit'], gettext("Disable"))) {
|
126
|
$mode = "disable";
|
127
|
} else if (stristr($_POST['Submit'], gettext("Upgrade")) || $_POST['sig_override']) {
|
128
|
$mode = "upgrade";
|
129
|
} else if ($_POST['sig_no']) {
|
130
|
if (file_exists("{$g['upload_path']}/firmware.tgz")) {
|
131
|
unlink("{$g['upload_path']}/firmware.tgz");
|
132
|
}
|
133
|
}
|
134
|
if ($mode) {
|
135
|
if ($mode == "enable") {
|
136
|
conf_mount_rw();
|
137
|
mark_subsystem_dirty('firmware');
|
138
|
} else if ($mode == "disable") {
|
139
|
conf_mount_ro();
|
140
|
clear_subsystem_dirty('firmware');
|
141
|
} else if ($mode == "upgrade") {
|
142
|
if ($_FILES['ulfile']['error']) {
|
143
|
$errortext = "(" . file_upload_error_message($_FILES['ulfile']['error']) . ")";
|
144
|
}
|
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
|
|
169
|
if (!verify_gzip_file("{$g['upload_path']}/firmware.tgz")) {
|
170
|
$input_errors[] = gettext("The image file is corrupt.");
|
171
|
unlink("{$g['upload_path']}/firmware.tgz");
|
172
|
}
|
173
|
}
|
174
|
}
|
175
|
|
176
|
run_plugins("/usr/local/pkg/firmware_upgrade");
|
177
|
|
178
|
/* Check for input errors, firmware locks, warnings, then check for firmware if sig_override is set */
|
179
|
if (!$input_errors && !is_subsystem_dirty('firmwarelock') && (!$sig_warning || $_POST['sig_override'])) {
|
180
|
if (file_exists("{$g['upload_path']}/firmware.tgz")) {
|
181
|
/* fire up the update script in the background */
|
182
|
mark_subsystem_dirty('firmwarelock');
|
183
|
$savemsg = gettext("The firmware is now being updated. The firewall will reboot automatically.");
|
184
|
if (stristr($_FILES['ulfile']['name'], "nanobsd") or $_POST['isnano'] == "yes") {
|
185
|
mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade {$g['upload_path']}/firmware.tgz");
|
186
|
} else {
|
187
|
if ($g['platform'] == "nanobsd") {
|
188
|
$whichone = "pfSenseNanoBSDupgrade";
|
189
|
} else {
|
190
|
$whichone = "pfSenseupgrade";
|
191
|
}
|
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
|
|
203
|
$pgtitle = array(gettext("System"), gettext("Firmware"));
|
204
|
include("head.inc");
|
205
|
|
206
|
if ($input_errors)
|
207
|
print_input_errors($input_errors);
|
208
|
|
209
|
if ($savemsg)
|
210
|
print_info_box($savemsg);
|
211
|
|
212
|
if ($fwinfo != "")
|
213
|
print_info_box($fwinfo);
|
214
|
|
215
|
$tab_array = array();
|
216
|
$tab_array[] = array(gettext("Manual Update"), true, "system_firmware.php");
|
217
|
$tab_array[] = array(gettext("Auto Update"), false, "system_firmware_check.php");
|
218
|
$tab_array[] = array(gettext("Updater Settings"), false, "system_firmware_settings.php");
|
219
|
if($g['hidedownloadbackup'] == false)
|
220
|
$tab_array[] = array(gettext("Restore Full Backup"), false, "system_firmware_restorefullbackup.php");
|
221
|
|
222
|
display_top_tabs($tab_array);
|
223
|
|
224
|
// For a simple yes/no we can use an HTML form
|
225
|
if ($sig_warning && !$input_errors) {
|
226
|
$sig_warning = gettext("The image you uploaded " .
|
227
|
"is not an official/supported image and may lead to unexpected behavior or security " .
|
228
|
"compromises. Only install images that come from sources that you trust, and make sure ".
|
229
|
"that the image has not been tampered with.") . "<br /><br />".
|
230
|
gettext("Do you want to install this image anyway (on your own risk)?");
|
231
|
|
232
|
print_info_box($sig_warning);
|
233
|
?>
|
234
|
<form action="system_firmware.php" method="post" enctype="multipart/form-data">
|
235
|
<input name="sig_override" type="submit" class="btn btn-danger" id="sig_override" value=" <?=gettext("Yes");?> " />
|
236
|
<input name="sig_no" type="submit" class="btn btn-default" id="sig_no" value=" <?=gettext("No"); ?> " />
|
237
|
</form>
|
238
|
<?php
|
239
|
|
240
|
} else {
|
241
|
// This is where the work gets done so Forms.classes will be used from this point
|
242
|
if (!is_subsystem_dirty('firmwarelock')) {
|
243
|
require_once('classes/Form.class.php');
|
244
|
|
245
|
if (!is_subsystem_dirty('rebootreq')) {
|
246
|
// Provide a button to enable firmware upgrades. Upgrades should be disabled on initial page load
|
247
|
if (!is_subsystem_dirty('firmware') || !$_POST || $_POST['save']) {
|
248
|
$enablebtn = new Form_Button(
|
249
|
'Submit',
|
250
|
'Enable firmware upload'
|
251
|
);
|
252
|
|
253
|
$enablebtn->addClass('btn-warning');
|
254
|
$form = new Form($enablebtn);
|
255
|
$section = new Form_Section('Invoke ' . $g['product_name'] .' Manual Upgrade');
|
256
|
$section->addInput(new Form_StaticText('Enable', 'Click the "Enable firmware upload" button below to begin.'));
|
257
|
}
|
258
|
else {
|
259
|
// Upgrades are now enabled
|
260
|
$form = new Form('Disable firmware upload');
|
261
|
|
262
|
$form->setMultipartEncoding();
|
263
|
|
264
|
$section = new Form_Section('Perform ' . $g['product_name'] .' Manual Upgrade');
|
265
|
|
266
|
if (!session_id())
|
267
|
$upload_id = uniqid();
|
268
|
else
|
269
|
$upload_id = session_id();
|
270
|
|
271
|
$section->addInput(new Form_Input(
|
272
|
'UPLOAD_IDENTIFIER',
|
273
|
'',
|
274
|
'hidden',
|
275
|
$upload_id
|
276
|
));
|
277
|
|
278
|
if(stristr($_FILES['ulfile']['name'],"nanobsd")) {
|
279
|
$section->addInput(new Form_Input(
|
280
|
'isnano',
|
281
|
'',
|
282
|
'hidden',
|
283
|
'yes'
|
284
|
));
|
285
|
}
|
286
|
|
287
|
if ($g['platform'] == "nanobsd")
|
288
|
$type = "*.img.gz";
|
289
|
else
|
290
|
$type = "*.tgz";
|
291
|
|
292
|
$filepicker = new Form_Input(
|
293
|
'ulfile',
|
294
|
'File to upload (' . $type . ')',
|
295
|
'file',
|
296
|
''
|
297
|
);
|
298
|
|
299
|
$section->addInput($filepicker)->setHelp('Choose the file you wish to upload');
|
300
|
|
301
|
if ($g['hidebackupbeforeupgrade'] === false) {
|
302
|
$section->addInput(new Form_Checkbox(
|
303
|
'backupbeforeupgrade',
|
304
|
Backup,
|
305
|
'Perform a full backup prior to upgrade',
|
306
|
false
|
307
|
));
|
308
|
}
|
309
|
|
310
|
$section->addInput(new Form_Button(
|
311
|
'submit',
|
312
|
'Upgrade firmware'
|
313
|
))->addClass('btn-danger btn-sm')->setHelp('Click the "Upgrade firmware" button above to start the upgrade process');
|
314
|
}
|
315
|
|
316
|
$form->add($section);
|
317
|
print($form);
|
318
|
}
|
319
|
}
|
320
|
else {
|
321
|
print_info_box('<strong>' . gettext("You must reboot the system before you can upgrade the firmware.") . '</strong>');
|
322
|
}
|
323
|
|
324
|
if (is_subsystem_dirty('firmware') && !is_subsystem_dirty('firmwarelock')) {
|
325
|
print_info_box('<strong>' . gettext("DO NOT ") . '</strong>' . gettext('abort the firmware upgrade once it ' .
|
326
|
'has started. The firewall will reboot automatically after ' .
|
327
|
'storing the new firmware. The configuration will be maintained.'));
|
328
|
}
|
329
|
}
|
330
|
|
331
|
include("foot.inc"); ?>
|