Project

General

Profile

Download (5.46 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

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

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

    
35
$fp = fopen('php://stdin', 'r');
36

    
37
echo ".\n\n";
38

    
39
$shell_active = true;
40

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

    
45
echo "\nPlease select an option to continue: ";
46

    
47
$pkg_interface = 'console';
48
$command = strtoupper(chop(fgets($fp)));
49

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

    
149
function do_upgrade($path, $type) {
150
	global $g, $fp;
151

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

    
195
exec("rm -f /root/*.sha256");
196
fclose($fp);
197

    
198
?>
(58-58/105)