Project

General

Profile

Download (9.12 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
    $http_auth_username = "";
36
    $http_auth_password = "";
37
    if($config['system']['proxy_auth_username'])
38
	$http_auth_username = $config['system']['proxy_auth_username'];
39
    if($config['system']['proxy_auth_password'])
40
	$http_auth_password = $config['system']['proxy_auth_password'];
41
    exec_rc_script_async("/etc/rc.firmware_auto {$http_auth_username} {$http_auth_password}");
42
    $savemsg = "pfSense is now auto upgrading.  The firewall will automatically reboot if it succeeds.";
43
}
44

    
45
/* checks with pfSense to see if a newer firmware version is available;
46
   returns any HTML message it gets from the server */
47
function check_firmware_version() {
48
	global $g;
49
	$post = "platform=" . rawurlencode($g['platform']) .
50
		"&version=" . rawurlencode(trim(file_get_contents("/etc/version")));
51

    
52
	$rfd = @fsockopen("www.pfSense.com", 80, $errno, $errstr, 3);
53
	if ($rfd) {
54
		$hdr = "POST /pfSense/checkversion.php HTTP/1.0\r\n";
55
		$hdr .= "Content-Type: application/x-www-form-urlencoded\r\n";
56
		$hdr .= "User-Agent: pfSense-webConfigurator/1.0\r\n";
57
		$hdr .= "Host: www.pfSense.com\r\n";
58
		$hdr .= "Content-Length: " . strlen($post) . "\r\n\r\n";
59

    
60
		fwrite($rfd, $hdr);
61
		fwrite($rfd, $post);
62

    
63
		$inhdr = true;
64
		$resp = "";
65
		while (!feof($rfd)) {
66
			$line = fgets($rfd);
67
			if ($inhdr) {
68
				if (trim($line) == "")
69
					$inhdr = false;
70
			} else {
71
				$resp .= $line;
72
			}
73
		}
74

    
75
		fclose($rfd);
76

    
77
		if($_GET['autoupgrade'] <> "")
78
		    return;
79

    
80
		return $resp;
81
	}
82

    
83
	return null;
84
}
85

    
86
if ($_POST && !file_exists($d_firmwarelock_path)) {
87

    
88
	unset($input_errors);
89
	unset($sig_warning);
90

    
91
	if (stristr($_POST['Submit'], "Enable"))
92
		$mode = "enable";
93
	else if (stristr($_POST['Submit'], "Disable"))
94
		$mode = "disable";
95
	else if (stristr($_POST['Submit'], "Upgrade") || $_POST['sig_override'])
96
		$mode = "upgrade";
97
	else if ($_POST['sig_no']) {
98
		if(file_exists("{$g['tmp_path']}/firmware.tgz"))
99
				unlink("{$g['tmp_path']}/firmware.tgz");
100
    }
101

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

    
125
					/* check digital signature */
126
					$sigchk = verify_digital_signature("{$g['tmp_path']}/firmware.tgz");
127

    
128
					if ($sigchk == 1)
129
						$sig_warning = "The digital signature on this image is invalid.";
130
					else if ($sigchk == 2)
131
						$sig_warning = "This image is not digitally signed.";
132
					else if (($sigchk == 3) || ($sigchk == 4))
133
						$sig_warning = "There has been an error verifying the signature on this image.";
134

    
135
					if (!verify_gzip_file("{$g['tmp_path']}/firmware.tgz")) {
136
						$input_errors[] = "The image file is corrupt.";
137
						unlink("{$g['tmp_path']}/firmware.tgz");
138
					}
139
				}
140
			}
141

    
142
			if (!$input_errors && !file_exists($d_firmwarelock_path) && (!$sig_warning || $_POST['sig_override'])) {
143
				/* fire up the update script in the background */
144
				touch($d_firmwarelock_path);
145
				exec_rc_script_async("/etc/rc.firmware pfSenseupgrade {$g['tmp_path']}/firmware.tgz");
146

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

    
164
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
165
<?php include("fbegin.inc"); ?>
166
<p class="pgtitle">System: Firmware</p>
167
<?php if ($input_errors) print_input_errors($input_errors); ?>
168
<?php if ($savemsg) print_info_box($savemsg); ?>
169
<?php if ($fwinfo <> "") print_info_box($fwinfo); ?>
170
<?php if (!in_array($g['platform'], $fwupplatforms)): ?>
171
<p><strong>Firmware uploading is not supported on this platform.</strong></p>
172
<?php elseif ($sig_warning && !$input_errors): ?>
173
<form action="system_firmware.php" method="post">
174
<?php
175
$sig_warning = "<strong>" . $sig_warning . "</strong><br>This means that the image you uploaded " .
176
	"is not an official/supported image and may lead to unexpected behavior or security " .
177
	"compromises. Only install images that come from sources that you trust, and make sure ".
178
	"that the image has not been tampered with.<br><br>".
179
	"Do you want to install this image anyway (on your own risk)?";
180
print_info_box($sig_warning);
181
?>
182
<input name="sig_override" type="submit" class="formbtn" id="sig_override" value=" Yes ">
183
<input name="sig_no" type="submit" class="formbtn" id="sig_no" value=" No ">
184
</form>
185
<?php else: ?>
186
            <?php if (!file_exists($d_firmwarelock_path)): ?>
187
<form action="system_firmware.php" method="post" enctype="multipart/form-data">
188
<?php if($savemsg == ""): ?>
189
            <p>Click &quot;Enable firmware
190
              upload&quot; below, then choose the image file (<?=$g['platform'];?>-*.img)
191
			  to be uploaded.<br>Click &quot;Upgrade firmware&quot;
192
              to start the upgrade process.</p>
193

    
194
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
195
                <tr>
196
                  <td width="22%" valign="top">&nbsp;</td>
197
                  <td width="78%">
198
                    <?php if (!file_exists($d_sysrebootreqd_path)): ?>
199
                    <?php if (!file_exists($d_fwupenabled_path)): ?>
200
                    <input name="Submit" type="submit" class="formbtn" value="Enable firmware upload">
201
				  <?php else: ?>
202
				   <input name="Submit" type="submit" class="formbtn" value="Disable firmware upload">
203
                    <br><br>
204
					<strong>Firmware image file: </strong>&nbsp;<input name="ulfile" type="file" class="formfld">
205
                    <br><br>
206
                    <input name="Submit" type="submit" class="formbtn" value="Upgrade firmware">
207
				  <?php endif; else: ?>
208
				    <strong>You must reboot the system before you can upgrade the firmware.</strong>
209
				  <?php endif; ?>
210
                  </td>
211
                </tr>
212
                <tr>
213
                  <td width="22%" valign="top">&nbsp;</td>
214
                  <td width="78%"><span class="vexpl"><span class="red"><strong>Warning:<br>
215
                    </strong></span>DO NOT abort the firmware upgrade once it
216
                    has started. The firewall will reboot automatically after
217
                    storing the new firmware. The configuration will be maintained.</span></td>
218
                </tr>
219

    
220
		<tr>
221
		 <td colspan="2">&nbsp;</td>
222
		</tr>
223
		<tr>
224
		 <td colspan="2" class="listtopic">Invoke pfSense Auto Upgrade</td>
225
		</tr>
226
		<tr>
227
		  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
228
		  <td width="78%" class="vtable">
229
		  <p> Click this button to automatically upgrade pfSense in the background.  This may take a while.<br>
230
		  <br>
231
		  <a href="system_firmware.php?autoupgrade=true">Invoke Auto Upgrade</a>
232
		</tr>
233
              </table>
234
<?php endif ?>
235

    
236
</form>
237
<?php endif; endif; ?>
238
<?php include("fend.inc"); ?>
239
</body>
240
</html>
(85-85/100)