Project

General

Profile

Download (9.89 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
/* checks with pfSense to see if a newer firmware version is available;
35
   returns any HTML message it gets from the server */
36
function check_firmware_version() {
37
	global $g;
38
	$post = "platform=" . rawurlencode($g['platform']) .
39
		"&version=" . rawurlencode(trim(file_get_contents("/etc/version")));
40

    
41
	$rfd = @fsockopen("www.pfSense.com", 80, $errno, $errstr, 3);
42
	if ($rfd) {
43
		$hdr = "POST /pfSense/checkversion.php HTTP/1.0\r\n";
44
		$hdr .= "Content-Type: application/x-www-form-urlencoded\r\n";
45
		$hdr .= "User-Agent: pfSense-webConfigurator/1.0\r\n";
46
		$hdr .= "Host: www.pfSense.com\r\n";
47
		$hdr .= "Content-Length: " . strlen($post) . "\r\n\r\n";
48

    
49
		fwrite($rfd, $hdr);
50
		fwrite($rfd, $post);
51

    
52
		$inhdr = true;
53
		$resp = "";
54
		while (!feof($rfd)) {
55
			$line = fgets($rfd);
56
			if ($inhdr) {
57
				if (trim($line) == "")
58
					$inhdr = false;
59
			} else {
60
				$resp .= $line;
61
			}
62
		}
63

    
64
		fclose($rfd);
65

    
66
		if($_GET['autoupgrade'] <> "")
67
		    return;
68

    
69
		return $resp;
70
	}
71

    
72
	return null;
73
}
74

    
75
/* Handle auto upgrade */
76
if($_POST) {
77
	if (stristr($_POST['autoupgrade'], "Auto")) {
78
		$http_auth_username = "";
79
		$http_auth_password = "";
80
		if($config['system']['proxy_auth_username'])
81
			$http_auth_username = $config['system']['proxy_auth_username'];
82
		if($config['system']['proxy_auth_password'])
83
			$http_auth_password = $config['system']['proxy_auth_password'];
84

    
85
		/* custom firmware option */
86
		if (isset($config['system']['altfirmwareurl'])) {
87
			$firmwareurl=$config['system']['firmwareurl'];
88
			$firmwarename=$config['system']['firmwarename'];
89
		} else {
90
			$firmwareurl=$g['firmwarebaseurl'];
91
			$firmwarename=$g['firmwarefilename'];
92
		}
93

    
94
		exec_rc_script_async("/etc/rc.firmware_auto {$firmwareurl} {$firmwarename} {$http_auth_username} {$http_auth_password}");
95
		$savemsg = "pfSense is now auto upgrading.  The firewall will automatically reboot if it succeeds.";
96
	}
97
}
98

    
99
/* Handle manual upgrade */
100
if ($_POST && !file_exists($d_firmwarelock_path)) {
101

    
102
	unset($input_errors);
103
	unset($sig_warning);
104

    
105
	if (stristr($_POST['Submit'], "Enable"))
106
		$mode = "enable";
107
	else if (stristr($_POST['Submit'], "Disable"))
108
		$mode = "disable";
109
	else if (stristr($_POST['Submit'], "Upgrade") || $_POST['sig_override'])
110
		$mode = "upgrade";
111
	else if ($_POST['sig_no']) {
112
		if(file_exists("{$g['tmp_path']}/firmware.tgz"))
113
				unlink("{$g['tmp_path']}/firmware.tgz");
114
    }
115

    
116
	if ($mode) {
117
		if ($mode == "enable") {
118
			exec_rc_script("/etc/rc.firmware enable");
119
			touch($d_fwupenabled_path);
120
		} else if ($mode == "disable") {
121
			exec_rc_script("/etc/rc.firmware disable");
122
			if (file_exists($d_fwupenabled_path))
123
				unlink($d_fwupenabled_path);
124
		} else if ($mode == "upgrade") {
125
			if (is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
126
				/* verify firmware image(s) */
127
				if (!stristr($_FILES['ulfile']['name'], $g['platform']) && !$_POST['sig_override'])
128
					$input_errors[] = "The uploaded image file is not for this platfom ({$g['platform']}).";
129
				else if (!file_exists($_FILES['ulfile']['tmp_name'])) {
130
					/* probably out of memory for the MFS */
131
					$input_errors[] = "Image upload failed (out of memory?)";
132
					exec_rc_script("/etc/rc.firmware disable");
133
					if (file_exists($d_fwupenabled_path))
134
						unlink($d_fwupenabled_path);
135
				} else {
136
					/* move the image so PHP won't delete it */
137
					rename($_FILES['ulfile']['tmp_name'], "{$g['tmp_path']}/firmware.tgz");
138

    
139
					/* check digital signature */
140
					$sigchk = verify_digital_signature("{$g['tmp_path']}/firmware.tgz");
141

    
142
					if ($sigchk == 1)
143
						$sig_warning = "The digital signature on this image is invalid.";
144
					else if ($sigchk == 2)
145
						$sig_warning = "This image is not digitally signed.";
146
					else if (($sigchk == 3) || ($sigchk == 4))
147
						$sig_warning = "There has been an error verifying the signature on this image.";
148

    
149
					if (!verify_gzip_file("{$g['tmp_path']}/firmware.tgz")) {
150
						$input_errors[] = "The image file is corrupt.";
151
						unlink("{$g['tmp_path']}/firmware.tgz");
152
					}
153
				}
154
			}
155

    
156
			if (!$input_errors && !file_exists($d_firmwarelock_path) && (!$sig_warning || $_POST['sig_override'])) {
157
				/* fire up the update script in the background */
158
				touch($d_firmwarelock_path);
159
				exec_rc_script_async("/etc/rc.firmware pfSenseupgrade {$g['tmp_path']}/firmware.tgz");
160

    
161
				$savemsg = "The firmware is now being installed. The firewall will reboot automatically.";
162
			}
163
		}
164
	}
165
} else {
166
	/* Only check firmware version if we're setup to go against pfsense.org  and user wants us to */
167
	if (!isset($config['system']['disablefirmwarecheck']))
168
		if(!isset($config['system']['altfirmwareurl']))
169
			$fwinfo = check_firmware_version();
170
		else
171
			$fwinfo = "Using alternate firmware URL, cannot determine if {$config['system']['firmwareurl']}{$config['system']['firmwarename']} is newer than current.";
172
}
173
?>
174
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
175
<html>
176
<head>
177
<title><?=gentitle("System: Firmware");?></title>
178
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
179
<link href="gui.css" rel="stylesheet" type="text/css">
180
</head>
181

    
182
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
183
<?php include("fbegin.inc"); ?>
184
<p class="pgtitle">System: Firmware</p>
185
<?php if ($input_errors) print_input_errors($input_errors); ?>
186
<?php if ($savemsg) print_info_box($savemsg); ?>
187
<?php if ($fwinfo <> "") print_info_box($fwinfo); ?>
188
<?php if (!in_array($g['platform'], $fwupplatforms)): ?>
189
<p><strong>Firmware uploading is not supported on this platform.</strong></p>
190
<?php elseif ($sig_warning && !$input_errors): ?>
191
<form action="system_firmware.php" method="post">
192
<?php
193
$sig_warning = "<strong>" . $sig_warning . "</strong><br>This means that the image you uploaded " .
194
	"is not an official/supported image and may lead to unexpected behavior or security " .
195
	"compromises. Only install images that come from sources that you trust, and make sure ".
196
	"that the image has not been tampered with.<br><br>".
197
	"Do you want to install this image anyway (on your own risk)?";
198
print_info_box($sig_warning);
199
?>
200
<input name="sig_override" type="submit" class="formbtn" id="sig_override" value=" Yes ">
201
<input name="sig_no" type="submit" class="formbtn" id="sig_no" value=" No ">
202
</form>
203
<?php else: ?>
204
            <?php if (!file_exists($d_firmwarelock_path)): ?>
205
<form action="system_firmware.php" method="post" enctype="multipart/form-data">
206
<?php if($savemsg == ""): ?>
207

    
208
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
209
		<tr>
210
		 <td colspan="2" class="listtopic">Invoke pfSense Auto Upgrade</td>
211
		</tr>
212
		<tr>
213
		  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
214
		  <td width="78%" class="vtable">
215
		  <p> Click this button to automatically upgrade pfSense in the background.  This may take a while.<br>
216
		  <br>
217
		  <input name="autoupgrade" type="submit" class="formbtn" value="Invoke Auto Upgrade">
218
		</tr>
219
		<tr><td>&nbsp;</td></tr>
220
                <tr>
221
		 <td colspan="2" class="listtopic">Invoke pfSense Manual Upgrade</td>
222
		</tr>
223
		  <td width="22%" valign="baseline" class="vncell">&nbsp;</td>
224
                  <td width="78%">
225
            <p>Click &quot;Enable firmware
226
              upload&quot; below, then choose the image file (<?=$g['platform'];?>-*.tgz)
227
			  to be uploaded.<br>Click &quot;Upgrade firmware&quot;
228
              to start the upgrade process.</p>
229
                    <?php if (!file_exists($d_sysrebootreqd_path)): ?>
230
                    <?php if (!file_exists($d_fwupenabled_path)): ?>
231
                    <input name="Submit" type="submit" class="formbtn" value="Enable firmware upload">
232
				  <?php else: ?>
233
				   <input name="Submit" type="submit" class="formbtn" value="Disable firmware upload">
234
                    <br><br>
235
					<strong>Firmware image file: </strong>&nbsp;<input name="ulfile" type="file" class="formfld">
236
                    <br><br>
237
                    <input name="Submit" type="submit" class="formbtn" value="Upgrade firmware">
238
				  <?php endif; else: ?>
239
				    <strong>You must reboot the system before you can upgrade the firmware.</strong>
240
				  <?php endif; ?>
241
                  </td>
242
                </tr>
243
                <tr>
244
                  <td width="22%" valign="top">&nbsp;</td>
245
                  <td width="78%"><span class="vexpl"><span class="red"><strong>Warning:<br>
246
                    </strong></span>DO NOT abort the firmware upgrade once it
247
                    has started. The firewall will reboot automatically after
248
                    storing the new firmware. The configuration will be maintained.</span></td>
249
                </tr>
250

    
251
              </table>
252
<?php endif ?>
253

    
254
</form>
255
<?php endif; endif; ?>
256
<?php include("fend.inc"); ?>
257
</body>
258
</html>
(91-91/106)