Project

General

Profile

Download (8.58 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[] = array("Manual Update", true, "system_firmware.php");
64
	if($g['platform'] <> "nanobsd") {
65
		$tab_array[] = array("Auto Update", false, "system_firmware_check.php");
66
		$tab_array[] = array("Updater Settings", false, "system_firmware_settings.php");
67
	}
68
	display_top_tabs($tab_array);
69
?>
70
		</td>
71
	</tr>
72
	<tr>
73
	  <td class="tabcont">
74
	      <table width="100%" border="0" cellpadding="6" cellspacing="0">
75
			  <tr>
76
			    <td class="tabcont">
77
					<table width="100%" border="0" cellpadding="6" cellspacing="0">
78
						<tr>
79
							<td>
80
								<center>
81
								<table height='15' width='420' border='0' colspacing='0' cellpadding='0' cellspacing='0'>
82

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

    
115
<?php
116

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

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

    
123
if($current_installed_pfsense_version <> $latest_version) 
124
	$needs_system_upgrade = true;
125

    
126
if($needs_system_upgrade == true) {
127
	update_status("Downloading updates ...");
128
	conf_mount_rw();
129
	$status = download_file_with_progress_bar("{$updater_url}/latest.tgz", "{$g['upload_path']}/latest.tgz");
130
	$status = download_file_with_progress_bar("{$updater_url}/latest.tgz.sha256", "{$g['upload_path']}/latest.tgz.sha256");
131
	conf_mount_ro();
132
	update_output_window("{$g['product_name']} download complete.");
133
}
134

    
135
/* launch external upgrade helper */
136
$external_upgrade_helper_text = "/etc/rc.firmware ";
137

    
138
if($g['platform'] == "nanobsd")
139
	$external_upgrade_helper_text .= "pfSenseNanoBSDupgrade ";
140
else
141
	$external_upgrade_helper_text .= "pfSenseupgrade ";
142

    
143
if($needs_system_upgrade == true)
144
	$external_upgrade_helper_text .= "{$g['upload_path']}/latest.tgz";
145

    
146
$downloaded_latest_tgz_sha256 = str_replace("\n", "", `sha256 {$g['upload_path']}/latest.tgz  | awk '{ print $4 }'`);
147
$upgrade_latest_tgz_sha256 = str_replace("\n", "", `cat {$g['upload_path']}/latest.tgz.sha256 | awk '{ print $4 }'`);
148

    
149
$sigchk = 0;
150

    
151
if(!isset($curcfg['alturl']['enable']))
152
	$sigchk = verify_digital_signature("{$g['upload_path']}/latest.tgz");
153

    
154
if ($sigchk == 1)
155
	$sig_warning = "The digital signature on this image is invalid.";
156
else if ($sigchk == 2)
157
	$sig_warning = "This image is not digitally signed.";
158
else if (($sigchk == 3) || ($sigchk == 4))
159
	$sig_warning = "There has been an error verifying the signature on this image.";
160

    
161
if (!verify_gzip_file("{$g['upload_path']}/latest.tgz")) {
162
	update_status("The image file is corrupt.");
163
	update_output_window("Update cannot continue");
164
	if (file_exists("{$g['upload_path']}/latest.tgz")) {
165
		conf_mount_rw();
166
		unlink("{$g['upload_path']}/latest.tgz");
167
		conf_mount_ro();
168
	}
169
	require("fend.inc");
170
	exit;
171
}
172

    
173
if ($sigchk) {
174
	update_status($sig_warning);
175
	update_output_window("Update cannot continue");
176
	if (file_exists("{$g['upload_path']}/latest.tgz")) {
177
		conf_mount_rw();
178
		unlink("{$g['upload_path']}/latest.tgz");
179
		conf_mount_ro();
180
	}
181
	require("fend.inc");
182
	exit;
183
}
184

    
185
if($downloaded_latest_tgz_sha256 <> $upgrade_latest_tgz_sha256) {
186
	update_status("Downloading complete but sha256 does not match.");
187
	update_output_window("Auto upgrade aborted.  \n\nDownloaded SHA256: $downloaded_latest_tgz_sha256 \n\nNeeded SHA256: $upgrade_latest_tgz_sha256");	
188
} else {
189
	update_output_window("{$g['product_name']} is now upgrading.\\n\\nThe firewall will reboot once the operation is completed.");
190
	echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';\n</script>";
191
	exec_rc_script_async("{$external_upgrade_helper_text}");
192
}
193

    
194
/*
195
	Helper functions
196
*/
197

    
198
function download_file_with_progress_bar($url_file, $destination_file) {
199
	global $ch, $fout, $file_size, $downloaded, $counter;
200
	$file_size  = 1;
201
	$downloaded = 1;
202
	/* open destination file */
203
	$fout = fopen($destination_file, "wb");
204

    
205
	/*
206
		Originally by Author: Keyvan Minoukadeh
207
		Modified by Scott Ullrich to return Content-Length size
208
	*/
209

    
210
	$ch = curl_init();
211
	curl_setopt($ch, CURLOPT_URL, $url_file);
212
	curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
213
	curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'read_body');
214
	curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
215

    
216
	curl_exec($ch);
217
	fclose($fout);
218
	return 1;
219

    
220
	if ($error = curl_error($ch)) {
221
	    return -1;
222
	}
223
}
224

    
225
function read_header($ch, $string) {
226
	global $file_size, $ch, $fout;
227
	$length = strlen($string);
228
	ereg("(Content-Length:) (.*)", $string, $regs);
229
	if($regs[2] <> "") {
230
		$file_size = intval($regs[2]);
231
	}
232
	return $length;
233
}
234

    
235
function read_body($ch, $string) {
236
	global $fout, $file_size, $downloaded, $counter, $version, $latest_version, $current_installed_pfsense_version;
237
	$length = strlen($string);
238
	$downloaded += intval($length);
239
	$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
240
	$downloadProgress = 100 - $downloadProgress;
241
	$a = $file_size;
242
	$b = $downloaded;
243
	$c = $downloadProgress;
244
	$text  = "  Auto Update Download Status\\n";
245
	$text .= "---------------------------------\\n";
246
	$text .= "  Latest Version  : {$latest_version}\\n";
247
	$text .= "  Current Version : {$current_installed_pfsense_version}\\n";
248
	$text .= "  File size       : {$a}\\n";
249
	$text .= "  Downloaded      : {$b}\\n";
250
	$text .= "  Percent         : {$c}%\\n";
251
	$text .= "---------------------------------\\n";
252
	$counter++;
253
	if($counter > 150) {
254
		update_output_window($text);
255
		update_progress_bar($downloadProgress);
256
		$counter = 0;
257
	}
258
	fwrite($fout, $string);
259
	return $length;
260
}
261

    
262
?>
(143-143/176)