Project

General

Profile

Download (8.68 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/*
4
	system_firmware.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

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

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

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

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

    
32
$d_isfwfile = 1; require("guiconfig.inc");
33

    
34
if($_GET['autoupgrade'] <> "") {
35
    exec_rc_script_async("/etc/rc.firmware_auto");
36
    $savemsg = "pfSense is now auto upgrading.  The firewall will automatically reboot if it succeeds.";
37
}
38

    
39
/* checks with m0n0.ch to see if a newer firmware version is available;
40
   returns any HTML message it gets from the server */
41
function check_firmware_version() {
42
	global $g;
43
	$post = "platform=" . rawurlencode($g['platform']) .
44
		"&version=" . rawurlencode(trim(file_get_contents("/etc/version")));
45

    
46
	$rfd = @fsockopen("pfSense", 80, $errno, $errstr, 3);
47
	if ($rfd) {
48
		$hdr = "POST /pfSense/checkversion.php HTTP/1.0\r\n";
49
		$hdr .= "Content-Type: application/x-www-form-urlencoded\r\n";
50
		$hdr .= "User-Agent: pfSense-webConfigurator/1.0\r\n";
51
		$hdr .= "Host: m0n0.ch\r\n";
52
		$hdr .= "Content-Length: " . strlen($post) . "\r\n\r\n";
53

    
54
		fwrite($rfd, $hdr);
55
		fwrite($rfd, $post);
56

    
57
		$inhdr = true;
58
		$resp = "";
59
		while (!feof($rfd)) {
60
			$line = fgets($rfd);
61
			if ($inhdr) {
62
				if (trim($line) == "")
63
					$inhdr = false;
64
			} else {
65
				$resp .= $line;
66
			}
67
		}
68

    
69
		fclose($rfd);
70

    
71
		return $resp;
72
	}
73

    
74
	return null;
75
}
76

    
77
if ($_POST && !file_exists($d_firmwarelock_path)) {
78

    
79
	unset($input_errors);
80
	unset($sig_warning);
81

    
82
	if (stristr($_POST['Submit'], "Enable"))
83
		$mode = "enable";
84
	else if (stristr($_POST['Submit'], "Disable"))
85
		$mode = "disable";
86
	else if (stristr($_POST['Submit'], "Upgrade") || $_POST['sig_override'])
87
		$mode = "upgrade";
88
	else if ($_POST['sig_no']) {
89
		if(file_exists("{$g['tmp_path']}/firmware.tgz"))
90
				unlink("{$g['tmp_path']}/firmware.tgz");
91
    }
92

    
93
	if ($mode) {
94
		if ($mode == "enable") {
95
			exec_rc_script("/etc/rc.firmware enable");
96
			touch($d_fwupenabled_path);
97
		} else if ($mode == "disable") {
98
			exec_rc_script("/etc/rc.firmware disable");
99
			if (file_exists($d_fwupenabled_path))
100
				unlink($d_fwupenabled_path);
101
		} else if ($mode == "upgrade") {
102
			if (is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
103
				/* verify firmware image(s) */
104
				if (!stristr($_FILES['ulfile']['name'], $g['platform']) && !$_POST['sig_override'])
105
					$input_errors[] = "The uploaded image file is not for this platfom ({$g['platform']}).";
106
				else if (!file_exists($_FILES['ulfile']['tmp_name'])) {
107
					/* probably out of memory for the MFS */
108
					$input_errors[] = "Image upload failed (out of memory?)";
109
					exec_rc_script("/etc/rc.firmware disable");
110
					if (file_exists($d_fwupenabled_path))
111
						unlink($d_fwupenabled_path);
112
				} else {
113
					/* move the image so PHP won't delete it */
114
					rename($_FILES['ulfile']['tmp_name'], "{$g['tmp_path']}/firmware.tgz");
115

    
116
					/* check digital signature */
117
					$sigchk = verify_digital_signature("{$g['tmp_path']}/firmware.tgz");
118

    
119
					if ($sigchk == 1)
120
						$sig_warning = "The digital signature on this image is invalid.";
121
					else if ($sigchk == 2)
122
						$sig_warning = "This image is not digitally signed.";
123
					else if (($sigchk == 3) || ($sigchk == 4))
124
						$sig_warning = "There has been an error verifying the signature on this image.";
125

    
126
					if (!verify_gzip_file("{$g['tmp_path']}/firmware.tgz")) {
127
						$input_errors[] = "The image file is corrupt.";
128
						unlink("{$g['tmp_path']}/firmware.tgz");
129
					}
130
				}
131
			}
132

    
133
			if (!$input_errors && !file_exists($d_firmwarelock_path) && (!$sig_warning || $_POST['sig_override'])) {
134
				/* fire up the update script in the background */
135
				touch($d_firmwarelock_path);
136
				exec_rc_script_async("/etc/rc.firmware pfSenseupgrade {$g['tmp_path']}/firmware.tgz");
137

    
138
				$savemsg = "The firmware is now being installed. The firewall will reboot automatically.";
139
			}
140
		}
141
	}
142
} else {
143
	if (!isset($config['system']['disablefirmwarecheck']))
144
		$fwinfo = check_firmware_version();
145
}
146
?>
147
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
148
<html>
149
<head>
150
<title><?=gentitle("System: Firmware");?></title>
151
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
152
<link href="gui.css" rel="stylesheet" type="text/css">
153
</head>
154

    
155
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
156
<?php include("fbegin.inc"); ?>
157
<p class="pgtitle">System: Firmware</p>
158
<?php if ($input_errors) print_input_errors($input_errors); ?>
159
<?php if ($savemsg) print_info_box($savemsg); ?>
160
<?php if ($fwinfo) echo $fwinfo; ?>
161
<?php if (!in_array($g['platform'], $fwupplatforms)): ?>
162
<p><strong>Firmware uploading is not supported on this platform.</strong></p>
163
<?php elseif ($sig_warning && !$input_errors): ?>
164
<form action="system_firmware.php" method="post">
165
<?php
166
$sig_warning = "<strong>" . $sig_warning . "</strong><br>This means that the image you uploaded " .
167
	"is not an official/supported image and may lead to unexpected behavior or security " .
168
	"compromises. Only install images that come from sources that you trust, and make sure ".
169
	"that the image has not been tampered with.<br><br>".
170
	"Do you want to install this image anyway (on your own risk)?";
171
print_info_box($sig_warning);
172
?>
173
<input name="sig_override" type="submit" class="formbtn" id="sig_override" value=" Yes ">
174
<input name="sig_no" type="submit" class="formbtn" id="sig_no" value=" No ">
175
</form>
176
<?php else: ?>
177
            <?php if (!file_exists($d_firmwarelock_path)): ?>
178
            <p>Click &quot;Enable firmware
179
              upload&quot; below, then choose the image file (<?=$g['platform'];?>-*.img)
180
			  to be uploaded.<br>Click &quot;Upgrade firmware&quot;
181
              to start the upgrade process.</p>
182
            <form action="system_firmware.php" method="post" enctype="multipart/form-data">
183
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
184
                <tr>
185
                  <td width="22%" valign="top">&nbsp;</td>
186
                  <td width="78%">
187
                    <?php if (!file_exists($d_sysrebootreqd_path)): ?>
188
                    <?php if (!file_exists($d_fwupenabled_path)): ?>
189
                    <input name="Submit" type="submit" class="formbtn" value="Enable firmware upload">
190
				  <?php else: ?>
191
				   <input name="Submit" type="submit" class="formbtn" value="Disable firmware upload">
192
                    <br><br>
193
					<strong>Firmware image file: </strong>&nbsp;<input name="ulfile" type="file" class="formfld">
194
                    <br><br>
195
                    <input name="Submit" type="submit" class="formbtn" value="Upgrade firmware">
196
				  <?php endif; else: ?>
197
				    <strong>You must reboot the system before you can upgrade the firmware.</strong>
198
				  <?php endif; ?>
199
                  </td>
200
                </tr>
201
                <tr>
202
                  <td width="22%" valign="top">&nbsp;</td>
203
                  <td width="78%"><span class="vexpl"><span class="red"><strong>Warning:<br>
204
                    </strong></span>DO NOT abort the firmware upgrade once it
205
                    has started. The firewall will reboot automatically after
206
                    storing the new firmware. The configuration will be maintained.</span></td>
207
                </tr>
208

    
209
		<tr>
210
		 <td colspan="2">&nbsp;</td>
211
		</tr>
212
		<tr>
213
		 <td colspan="2" class="listtopic">Invoke pfSense Auto Upgrade</td>
214
		</tr>
215
		<tr>
216
		  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
217
		  <td width="78%" class="vtable">
218
		  <p> Click this button to automatically upgrade pfSense in the background.  This may take a while.<br>
219
		  <br>
220
		  <a href="system_firmware.php?autoupgrade=true">Invoke Auto Upgrade</a>
221
		</tr>
222

    
223
              </table>
224
</form>
225
<?php endif; endif; ?>
226
<?php include("fend.inc"); ?>
227
</body>
228
</html>
(85-85/100)