Project

General

Profile

Download (5.39 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php -f
2

    
3
<?php
4

    
5
require("globals.inc");
6
require("config.inc");
7
require("functions.inc");
8

    
9
echo "Starting the {$g['product_name']} console firmware update system";
10

    
11
require("functions.inc");
12
echo ".";
13

    
14
if(isset($config['system']['firmware']['alturl']['enable']))
15
	$updater_url = "{$config['system']['firmware']['alturl']['firmwareurl']}";
16
else
17
	$updater_url = $g['update_url'];
18

    
19
$nanosize = "";
20
if ($g['platform'] == "nanobsd") {
21
	if (file_exists("/etc/nano_use_vga.txt"))
22
		$nanosize = "-nanobsd-vga-";
23
	else
24
		$nanosize = "-nanobsd-";
25

    
26
	$nanosize .= strtolower(trim(file_get_contents("/etc/nanosize.txt")));
27
	$update_filename = "latest{$nanosize}.img.gz";
28
} else {
29
	$update_filename = "latest.tgz";
30
}
31
$autoupdateurl = "{$updater_url}/{$update_filename}";
32

    
33
$fp = fopen('php://stdin', 'r');
34

    
35
echo ".\n\n";
36

    
37
$shell_active = true;
38

    
39
echo "1) Update from a URL\n";
40
echo "2) Update from a local file\n";
41
echo "Q) Quit\n";
42

    
43
echo "\nPlease select an option to continue: ";
44

    
45
$pkg_interface = 'console';
46
$command = strtoupper(chop(fgets($fp)));
47

    
48
switch ($command) {
49
	case "q":
50
	case "quit":
51
		echo "\n";
52
		fclose($fp);
53
		die;
54
	break;
55
	case "1":
56
		echo "\nEnter the URL to the .tgz or .img.gz update file. \nType 'auto' to use {$autoupdateurl}\n> ";
57
		$url = chop(fgets($fp));
58
		if(!$url) { 
59
			fclose($fp);
60
			die;
61
		}
62
		if($url == "auto") {
63
			$url = $autoupdateurl;
64
		}
65
		$status = does_url_exist($url);
66
		if($status) {
67
			conf_mount_rw();
68
			mark_subsystem_dirty('firmware');
69
			unlink_if_exists("/root/firmware.tgz");
70
			echo "\nFetching file... ";
71
			download_file_with_progress_bar($url, '/root/firmware.tgz');
72
			if(!file_exists("/root/firmware.tgz")) {
73
				echo "Something went wrong during file transfer.  Exiting.\n\n";
74
				fclose($fp);
75
				clear_subsystem_dirty('firmware');
76
				die;
77
			}
78
			$status = does_url_exist("$url.sha256");
79
			if($status) { 
80
				echo "\nFetching sha256... ";
81
				download_file_with_progress_bar($url . ".sha256", '/root/firmware.tgz.sha256');
82
				echo "\n";
83
			} else {
84
				echo "\n\nWARNING.\n";
85
				echo "\nCould not locate a sha256 file.  We cannot verify the download once completed.\n\n";
86
				echo "Do you still want to proceed with the upgrade [n]? ";
87
				$answer = strtoupper(chop(fgets($fp)));
88
				if ($answer == "Y" or $answer == "YES") {
89
					echo "\nContinuing upgrade...";
90
				} else {
91
					echo "\nUpgrade cancelled.\n\n";
92
					die;
93
				}
94
			}
95
			if(file_exists("/root/firmware.tgz.sha256")) {
96
				$source_sha256 = trim(`cat /root/firmware.tgz.sha256 | awk '{ print \$4 }'`,"\r");
97
				$file_sha256 = trim(`sha256 /root/firmware.tgz | awk '{ print \$4 }'`,"\r");
98
				echo "URL sha256: $source_sha256\n";
99
				echo "Downloaded file sha256: $file_sha256\n";
100
				if($source_sha256 <> $file_sha256) {
101
					echo "\n\nsha256 checksum does not match.  Cancelling upgrade.\n\n";
102
					unlink_if_exists("/root/firmware.tgz.sha256");
103
					fclose($fp);
104
					clear_subsystem_dirty('firmware');
105
					die -1;
106
				}
107
				echo "\nsha256 checksum matches.\n";
108
				unlink_if_exists("/root/firmware.tgz.sha256");
109
			}
110
			if(strstr($url,"bdiff")) {
111
				echo "Binary DIFF upgrade file detected...\n";
112
				$type = "bdiff";
113
			} elseif(strstr($url,"nanobsd")) {
114
				echo "NanoBSD upgrade file detected...\n";
115
				$type = "nanobsd";
116
			} else {
117
				$type = "normal";
118
			}
119
			do_upgrade("/root/firmware.tgz", $type);
120
			clear_subsystem_dirty('firmware');
121
			exit;
122
		}
123
	case "2":
124
		echo "\nEnter the complete path to the .tgz or .img.gz update file: ";
125
		$path = chop(fgets($fp));
126
		if(!$path) {
127
			fclose($fp);
128
			die;
129
		}
130
		if(stristr($path,"bdiff"))
131
			$type = "bdiff";
132
		if(stristr($path,"nanobsd"))
133
			$type = "nanobsd";			
134
		if(file_exists($path)) {
135
			mark_subsystem_dirty('firmware');
136
			do_upgrade($path, $type);
137
			clear_subsystem_dirty('firmware');
138
		} else {
139
			echo "\nCould not find file.\n\n";
140
			fclose($fp);
141
			die -1;
142
		}
143
}
144

    
145
function do_upgrade($path, $type) {
146
	global $g, $fp;
147
	
148
	$sigchk = verify_digital_signature($path);
149
	if ($sigchk == 1)
150
		$sig_warning = "The digital signature on this image is invalid.";
151
	else if ($sigchk == 2)
152
		$sig_warning = "This image is not digitally signed.";
153
	else if (($sigchk == 3) || ($sigchk == 4))
154
		$sig_warning = "There has been an error verifying the signature on this image.";
155
	if($sig_warning) {
156
		$sig_warning = "\nWARNING! ACHTUNG! DANGER!\n\n{$sig_warning}\n\n" .
157
			"This means that the image you uploaded is not an official/supported image and\n" .
158
			"may lead to unexpected behavior or security compromises.\n\n" .
159
			"Only install images that come from sources that you trust, and make sure\n".
160
			"that the image has not been tampered with.\n\n".
161
			"Do you want to install this image anyway at your own risk [n]?";
162
		echo $sig_warning;
163
		$command = strtoupper(chop(fgets($fp)));
164
		if(strtoupper($command) == "Y" or strtoupper($command) == "Y" or strtoupper($command) == "YES") {
165
			echo "\nContinuing upgrade...";
166
		} else {
167
			echo "\nUpgrade cancelled.\n\n";
168
			die;
169
		}
170
	}
171
	mark_subsystem_dirty('firmwarelock');
172
	echo "\nOne moment please...\nInvoking firmware upgrade...";
173
	if($type == "bdiff")
174
		mwexec_bg("/etc/rc.firmware delta_update $path");
175
	elseif($type == "nanobsd")
176
		mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade $path");
177
	else
178
		mwexec_bg("/etc/rc.firmware pfSenseupgrade $path");
179
	sleep(10);
180
	while(is_subsystem_dirty('firmwarelock')) {
181
		sleep(1);
182
		echo ".";
183
	}
184
	sleep(10);
185
	echo "Done.  Rebooting...\n\n";
186
	clear_subsystem_dirty('firmwarelock');
187
}
188

    
189
exec("rm -f /root/*.sha256");
190
fclose($fp);
191

    
192
?>
(56-56/103)