Project

General

Profile

Download (8 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 m0n0.ch 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("m0n0.ch", 80, $errno, $errstr, 3);
42
	if ($rfd) {
43
		$hdr = "POST /wall/checkversion.php HTTP/1.0\r\n";
44
		$hdr .= "Content-Type: application/x-www-form-urlencoded\r\n";
45
		$hdr .= "User-Agent: m0n0wall-webGUI/1.0\r\n";
46
		$hdr .= "Host: m0n0.ch\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
		return $resp;
67
	}
68

    
69
	return null;
70
}
71

    
72
if ($_POST && !file_exists($d_firmwarelock_path)) {
73

    
74
	unset($input_errors);
75
	unset($sig_warning);
76

    
77
	if (stristr($_POST['Submit'], "Enable"))
78
		$mode = "enable";
79
	else if (stristr($_POST['Submit'], "Disable"))
80
		$mode = "disable";
81
	else if (stristr($_POST['Submit'], "Upgrade") || $_POST['sig_override'])
82
		$mode = "upgrade";
83
	else if ($_POST['sig_no'])
84
		unlink("{$g['ftmp_path']}/firmware.img");
85

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

    
109
					/* check digital signature */
110
					$sigchk = verify_digital_signature("{$g['ftmp_path']}/firmware.img");
111

    
112
					if ($sigchk == 1)
113
						$sig_warning = "The digital signature on this image is invalid.";
114
					else if ($sigchk == 2)
115
						$sig_warning = "This image is not digitally signed.";
116
					else if (($sigchk == 3) || ($sigchk == 4))
117
						$sig_warning = "There has been an error verifying the signature on this image.";
118

    
119
					if (!verify_gzip_file("{$g['ftmp_path']}/firmware.img")) {
120
						$input_errors[] = "The image file is corrupt.";
121
						unlink("{$g['ftmp_path']}/firmware.img");
122
					}
123
				}
124
			}
125

    
126
			if (!$input_errors && !file_exists($d_firmwarelock_path) && (!$sig_warning || $_POST['sig_override'])) {
127
				/* fire up the update script in the background */
128
				touch($d_firmwarelock_path);
129
				exec_rc_script_async("/etc/rc.firmware pfSenseupgrade {$g['ftmp_path']}/firmware.img");
130

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

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