system_firmware.php 11.3 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
	Copyright (C) 2014-2015 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
	All rights reserved.

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

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

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

	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	POSSIBILITY OF SUCH DAMAGE.
*/

$d_isfwfile = 1;
$nocsrf = true;

require_once("globals.inc");
35
require_once("functions.inc");
Ad Schellevis's avatar
Ad Schellevis committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
require_once("guiconfig.inc");
require_once("xmlrpc_client.inc");

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

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

function file_is_for_platform($filename, $ul_name) {
	global $g;
	if($g['platform'] == "nanobsd") {
		if(stristr($ul_name, "nanobsd"))
			return true;
		else
			return false;
	}
53
	$_gb = exec("/usr/bin/tar xzf $filename -C /tmp/ usr/local/etc/platform");
Ad Schellevis's avatar
Ad Schellevis committed
54
	unset($_gb);
55
	if(!file_exists("/tmp/usr/local/etc/platform"))
Ad Schellevis's avatar
Ad Schellevis committed
56
		return false;
57
	$upgrade_is_for_platform = trim(file_get_contents("/tmp/usr/local/etc/platform", " \n\t\r"));
Ad Schellevis's avatar
Ad Schellevis committed
58
	if ($g['platform'] == $upgrade_is_for_platform) {
59
		@unlink("/tmp/usr/local/etc/platform");
Ad Schellevis's avatar
Ad Schellevis committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
		return true;
	}
	return false;
}

function file_upload_error_message($error_code) {
	switch ($error_code) {
	case UPLOAD_ERR_INI_SIZE:
		return gettext('The uploaded file exceeds the upload_max_filesize directive in php.ini');
	case UPLOAD_ERR_FORM_SIZE:
		return gettext('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
	case UPLOAD_ERR_PARTIAL:
		return gettext('The uploaded file was only partially uploaded');
	case UPLOAD_ERR_NO_FILE:
		return gettext('No file was uploaded');
	case UPLOAD_ERR_NO_TMP_DIR:
		return gettext('Missing a temporary folder');
	case UPLOAD_ERR_CANT_WRITE:
		return gettext('Failed to write file to disk');
	case UPLOAD_ERR_EXTENSION:
		return gettext('File upload stopped by extension');
	default:
		return gettext('Unknown upload error');
	}
}

/* if upgrade in progress, alert user */
if(is_subsystem_dirty('firmwarelock')) {
	$pgtitle = array(gettext("System"),gettext("Firmware"),gettext("Manual Update"));
	include("head.inc");
	echo "<body link=\"#0000CC\" vlink=\"#0000CC\" alink=\"#0000CC\">\n";
	include("fbegin.inc");
	echo "<div>\n";
	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>");
	echo "</div>\n";
	include("fend.inc");
	echo "</body>";
	echo "</html>";
	exit;
}

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

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

	unset($input_errors);
	unset($sig_warning);

	if (stristr($_POST['Submit'], gettext("Enable")))
		$mode = "enable";
	else if (stristr($_POST['Submit'], gettext("Disable")))
		$mode = "disable";
	else if (stristr($_POST['Submit'], gettext("Upgrade")) || $_POST['sig_override'])
		$mode = "upgrade";
	else if ($_POST['sig_no']) {
		if(file_exists("{$g['upload_path']}/firmware.tgz"))
				unlink("{$g['upload_path']}/firmware.tgz");
	}
	if ($mode) {
		if ($mode == "enable") {
			conf_mount_rw();
			mark_subsystem_dirty('firmware');
		} else if ($mode == "disable") {
			conf_mount_ro();
			clear_subsystem_dirty('firmware');
		} else if ($mode == "upgrade") {
			if ($_FILES['ulfile']['error'])
				$errortext = "(" . file_upload_error_message($_FILES['ulfile']['error']) . ")";
			if (is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
				/* verify firmware image(s) */
				if (file_is_for_platform($_FILES['ulfile']['tmp_name'], $_FILES['ulfile']['name']) == false && !$_POST['sig_override'])
					$input_errors[] = gettext("The uploaded image file is not for this platform.");
				else if (!file_exists($_FILES['ulfile']['tmp_name'])) {
					/* probably out of memory for the MFS */
					$input_errors[] = gettext("Image upload failed (out of memory?)");
					clear_subsystem_dirty('firmware');
				} else {
					/* move the image so PHP won't delete it */
					rename($_FILES['ulfile']['tmp_name'], "{$g['upload_path']}/firmware.tgz");

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

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

			/* Check for input errors, firmware locks, warnings, then check for firmware if sig_override is set */
			if (!$input_errors && !is_subsystem_dirty('firmwarelock') && (!$sig_warning || $_POST['sig_override'])) {
				if (file_exists("{$g['upload_path']}/firmware.tgz")) {
					/* fire up the update script in the background */
					mark_subsystem_dirty('firmwarelock');
					$savemsg = gettext("The firmware is now being updated. The firewall will reboot automatically.");
157 158
					mwexec_bg("/usr/local/etc/rc.firmware pfSenseupgrade {$g['upload_path']}/firmware.tgz");
				} else {
Ad Schellevis's avatar
Ad Schellevis committed
159
					$savemsg = sprintf(gettext("Firmware image missing or other error, please try again %s."),$errortext);
160
				}
Ad Schellevis's avatar
Ad Schellevis committed
161 162 163 164 165 166 167 168 169
			}
		}
	}
}

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

?>
170 171
<body>

Ad Schellevis's avatar
Ad Schellevis committed
172 173
<?php include("fbegin.inc"); ?>

174 175 176
<!-- row -->
<section class="page-content-main">
	<div class="container-fluid">
177

178 179 180 181
        <div class="row">
             <?php if ($input_errors) print_input_errors($input_errors); ?>
             <?php if ($savemsg) print_info_box($savemsg); ?>
             <?php if ($fwinfo <> "") print_info_box($fwinfo); ?>
182

183
            <section class="col-xs-12">
184 185 186

                <? include('system_firmware_tabs.php'); ?>

187 188 189
                <div class="content-box tab-content">

                    <form action="system_firmware.php" method="post" enctype="multipart/form-data">
190

191
                        <?php
192 193 194
				/* Construct an upload_id for this session */
				if (!session_id()) $upload_id = uniqid();
				else $upload_id = session_id();
195 196
                        ?>
                        <input type="hidden" name="UPLOAD_IDENTIFIER" value="<?php echo $upload_id;?>" />
197

198 199 200
                        <?php if ($sig_warning && !$input_errors): ?>

                            <?php
201 202 203 204 205 206
				$sig_warning = "<strong>" . $sig_warning . "</strong><br />" . gettext("This means that the image you uploaded " .
					"is not an official/supported image and may lead to unexpected behavior or security " .
					"compromises. Only install images that come from sources that you trust, and make sure ".
					"that the image has not been tampered with.") . "<br /><br />".
					gettext("Do you want to install this image anyway (on your own risk)?");

207
                                print_info_box($sig_warning);
208

209
                                if(stristr($_FILES['ulfile']['name'],"nanobsd"))
210
					echo "<input type='hidden' name='isnano' id='isnano' value='yes' />\n";
211
                            ?>
212

213 214 215 216
                        <input name="sig_override" type="submit" class="btn btn-primary" id="sig_override" value=" <?=gettext("Yes");?> " />
                        <input name="sig_no" type="submit" class="btn btn-default" id="sig_no" value=" <?=gettext("No"); ?> " />

                        <?php else: ?>
217

218
                            <?php if (!is_subsystem_dirty('firmwarelock')): ?>
219

220
                                <table class="table table-striped" width="100%" border="0" cellpadding="6" cellspacing="0" summary="main area">
221

222 223
                                    <thead>
                                        <tr>
224
							<th colspan="2" class="listtopic"><?=gettext("Invoke") ." ". $g['product_name'] ." ".  gettext("Manual Upgrade"); ?></th>
225 226
                                        </tr>
                                    </thead>
227

228
                                    <tbody>
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
						<tr>
							<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
							<td width="78%" class="vtable">
							<?php if (!is_subsystem_dirty('rebootreq')): ?>
							<?php if (!is_subsystem_dirty('firmware')): ?>
							<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Enable firmware upload");?>" />
							<br />
								<?php printf(gettext('Click "Enable firmware upload" to begin.'),$g['firmware_update_text']);?>
							<br />
							<?php else: ?>
								<input name="Submit" type="submit" class="btn btn-default" value="<?=gettext("Disable firmware upload");?>" />
						    </td>
						</tr>
						<tr>
							<td width="22%" valign="baseline" class="vncell">&nbsp;</td>
							<td width="78%" class="vtable">
								<?php
									if ($g['platform'] == "nanobsd")
										$type = "*.img.gz";
									else
										$type = "*.tgz";
								?>
								<strong><?=gettext("Firmware image file ($type):");?> </strong>
								<input name="ulfile" type="file" class="formfld" />
								<br />
								<input type="checkbox" name='backupbeforeupgrade' id='backupbeforeupgrade' /> <?=gettext("Perform full backup prior to upgrade");?>
								<br />
								<input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Upgrade firmware");?>" />
								<?=gettext('Click "Upgrade firmware" to start the upgrade process.');?>
							<?php endif; else: ?>
								<strong><?=gettext("You must reboot the system before you can upgrade the firmware.");?></strong>
							<?php endif; ?>
					        </td>
						    </tr>
263
                                        <tr>
264
							    <td width="22%" valign="top">&nbsp;</td>
265
                                            <td width="78%">
266 267
									<?php if (is_subsystem_dirty('firmware')): ?>

268
                                                <span class="vexpl">
269 270 271 272 273
									<span class="red">
										<strong>
											<?=gettext("Warning:");?><br />
										</strong>
									</span>
274
                                                    <?=gettext("DO NOT abort the firmware upgrade once it " .
275 276
											"has started. The firewall will reboot automatically after " .
											"storing the new firmware. The configuration will be maintained.");?>
277
                                                </span>
278 279 280 281

									<?php endif; ?>
							    </td>
							</tr>
282 283 284
                                    </tbody>
                                </table>
                            <?php endif;?>
285

286
                        <?php endif;?>
287

288
                    </form>
289

290 291
                </div>
            </section>
292
        </div>
293 294 295
	</div>
</section>
<?php include("foot.inc"); ?>