Project

General

Profile

Download (8.19 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	system_firmware_auto.php
5
	Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
6
	Copyright (C) 2005 Scott Ullrich
7

    
8
        Based originally on system_firmware.php
9
        (C)2003-2004 Manuel Kasper
10
	All rights reserved.
11

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

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

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

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

    
34
require("guiconfig.inc");
35

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

    
38
if(isset($curcfg['alturl']['enable']))
39
	$updater_url = "{$config['system']['firmware']['alturl']['firmwareurl']}";
40
else
41
	$updater_url = $g['update_url'];
42
	
43
$pgtitle = array("Diagnostics","Firmware","Auto Update");
44
include("head.inc");
45

    
46
?>
47

    
48
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
49
<link href="gui.css" rel="stylesheet" type="text/css">
50
</head>
51

    
52
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
53

    
54
<?php include("fbegin.inc"); ?>
55

    
56
<form action="system_firmware_auto.php" method="post">
57
<table width="100%" border="0" cellpadding="6" cellspacing="0">
58
	<tr>
59
		<td>
60
<?php
61
	$tab_array = array();
62
	$tab_array[0] = array("Manual Update", true, "system_firmware.php");
63
	$tab_array[1] = array("Auto Update", false, "system_firmware_check.php");
64
	$tab_array[2] = array("Updater Settings", false, "system_firmware_settings.php");
65
	display_top_tabs($tab_array);
66
?>
67
		</td>
68
	</tr>
69
	<tr>
70
	  <td class="tabcont">
71
	      <table width="100%" border="0" cellpadding="6" cellspacing="0">
72
			  <tr>
73
			    <td class="tabcont">
74
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
75
						<tr>
76
							<td>
77
								<center>
78
								<table height='15' width='420' border='0' colspacing='0' cellpadding='0' cellspacing='0'>
79

    
80
								<tr>
81
									<td background="./themes/the_wall/images/misc/bar_left.gif" height='15' width='5'>
82
									</td>
83
									<td>
84
									<table id="progholder" name="progholder" height='15' width='410' border='0' colspacing='0' cellpadding='0' cellspacing='0'>
85
										<td background="./themes/the_wall/images/misc/bar_gray.gif" valign="top" align="left">
86
											<img src='./themes/the_wall/images/misc/bar_blue.gif' width='0' height='15' name='progressbar' id='progressbar'>
87
										</td>
88
									</table>
89
								</td>
90
								<td background="./themes/the_wall/images/misc/bar_right.gif" height='15' width='5'>
91
								</td>
92
							</tr>
93
						</table>
94
						<br>
95
						<!-- status box -->
96
						<textarea cols="60" rows="1" name="status" id="status" wrap="hard">Beginning package installation.</textarea>
97
						<!-- command output box -->
98
						<textarea cols="60" rows="25" name="output" id="output" wrap="hard"></textarea>
99
					</center>
100
					</td>
101
				</tr>
102
	      </table>
103
		</table>
104
	  </td>
105
	</tr>
106
</table>
107
</form>
108
<?php include("fend.inc"); ?>
109
</body>
110
</html>
111

    
112
<?php
113

    
114
update_status("Downloading current version information...");
115
$latest_version = download_file_with_progress_bar("{$updater_url}/version", "/tmp/{$g['product_name']}_version");
116

    
117
$current_installed_pfsense_version = str_replace("\n", "", file_get_contents("/etc/version"));
118
$latest_version = str_replace("\n", "", file_get_contents("/tmp/{$g['product_name']}_version"));
119

    
120
if($current_installed_pfsense_version <> $latest_version) 
121
	$needs_system_upgrade = true;
122

    
123
if($needs_system_upgrade == true) {
124
	update_status("Downloading updates ...");
125
	$status = download_file_with_progress_bar("{$updater_url}/latest.tgz", "/tmp/latest.tgz");	
126
	$status = download_file_with_progress_bar("{$updater_url}/latest.tgz.sha256", "/tmp/latest.tgz.sha256");
127
	update_output_window("{$g['product_name']} download complete.");
128
}
129

    
130
/* launch external upgrade helper */
131
$external_upgrade_helper_text = "/etc/rc.firmware pfSenseupgrade ";
132
if($needs_system_upgrade == true)
133
	$external_upgrade_helper_text .= "/tmp/latest.tgz";
134

    
135
$downloaded_latest_tgz_sha256 = str_replace("\n", "", `sha256 /tmp/latest.tgz  | awk '{ print $4 }'`);
136
$upgrade_latest_tgz_sha256 = str_replace("\n", "", `cat /tmp/latest.tgz.sha256 | awk '{ print $4 }'`);
137

    
138
$sigchk = 0;
139

    
140
if(!isset($curcfg['alturl']['enable']))
141
	$sigchk = verify_digital_signature("/tmp/latest.tgz");
142

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

    
150
if (!verify_gzip_file("/tmp/latest.tgz")) {
151
	update_status("The image file is corrupt.");
152
	update_output_window("Update cannot continue");
153
	unlink("{$g['upload_path']}/latest.tgz");
154
	require("fend.inc");
155
	exit;
156
}
157

    
158
if ($sigchk) {
159
	update_status($sig_warning);
160
	update_output_window("Update cannot continue");
161
	unlink("{$g['upload_path']}/latest.tgz");
162
	require("fend.inc");
163
	exit;
164
}
165

    
166
if($downloaded_latest_tgz_sha256 <> $upgrade_latest_tgz_sha256) {
167
	update_status("Downloading complete but sha256 does not match.");
168
	update_output_window("Auto upgrade aborted.  \n\nDownloaded SHA256: $downloaded_latest_tgz_sha256 \n\nNeeded SHA256: $upgrade_latest_tgz_sha256");	
169
} else {
170
	update_output_window("{$g['product_name']} is now upgrading.\\n\\nThe firewall will reboot once the operation is completed.");
171
	echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';\n</script>";
172
	exec_rc_script_async("{$external_upgrade_helper_text}");
173
}
174

    
175
/*
176
	Helper functions
177
*/
178

    
179
function download_file_with_progress_bar($url_file, $destination_file) {
180
	global $ch, $fout, $file_size, $downloaded, $counter;
181
	$file_size  = 1;
182
	$downloaded = 1;
183
	/* open destination file */
184
	$fout = fopen($destination_file, "wb");
185

    
186
	/*
187
		Originally by Author: Keyvan Minoukadeh
188
		Modified by Scott Ullrich to return Content-Length size
189
	*/
190

    
191
	$ch = curl_init();
192
	curl_setopt($ch, CURLOPT_URL, $url_file);
193
	curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
194
	curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
195
	curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
196

    
197
	curl_exec($ch);
198
	fclose($fout);
199
	return 1;
200

    
201
	if ($error = curl_error($ch)) {
202
	    return -1;
203
	}
204
}
205

    
206
function read_header($ch, $string) {
207
	global $file_size, $ch, $fout;
208
	$length = strlen($string);
209
	ereg("(Content-Length:) (.*)", $string, $regs);
210
	if($regs[2] <> "") {
211
		$file_size = intval($regs[2]);
212
	}
213
	return $length;
214
}
215

    
216
function read_body($ch, $string) {
217
	global $fout, $file_size, $downloaded, $counter, $version, $latest_version, $current_installed_pfsense_version;
218
	$length = strlen($string);
219
	$downloaded += intval($length);
220
	$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
221
	$downloadProgress = 100 - $downloadProgress;
222
	$a = $file_size;
223
	$b = $downloaded;
224
	$c = $downloadProgress;
225
	$text  = "  Auto Update Download Status\\n";
226
	$text .= "---------------------------------\\n";
227
	$text .= "  Latest Version  : {$latest_version}\\n";
228
	$text .= "  Current Version : {$current_installed_pfsense_version}\\n";
229
	$text .= "  File size       : {$a}\\n";
230
	$text .= "  Downloaded      : {$b}\\n";
231
	$text .= "  Percent         : {$c}%\\n";
232
	$text .= "---------------------------------\\n";
233
	$counter++;
234
	if($counter > 150) {
235
		update_output_window($text);
236
		update_progress_bar($downloadProgress);
237
		$counter = 0;
238
	}
239
	fwrite($fout, $string);
240
	echo "<script language='javascript'>'>document.progressbar.style.width=\"$c%\";</script>\n";
241
	return $length;
242
}
243

    
244
?>
(150-150/196)