Project

General

Profile

Download (8.09 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	Copyright (C) 2008 Scott Ullrich <sullrich@gmail.com>
5
	All rights reserved.
6
	
7
	Based originally on system_firmware.php
8
	(C)2003-2004 Manuel Kasper
9
	All rights reserved.
10

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

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

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

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

    
33
require("guiconfig.inc");
34

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

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

    
45
?>
46

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

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

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

    
55
<p class="pgtitle"><?=$pgtitle?></font></p>
56

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

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

    
113
<?php
114

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

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

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

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

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

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

    
139
$sigchk = 0;
140

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

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

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

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

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

    
176
/*
177
	Helper functions
178
*/
179

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

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

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

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

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

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

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

    
244
?>
(140-140/173)