Project

General

Profile

Download (5.21 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php-cgi -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 (!isset($g['enableserial_force'])) {
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, "nanobsd")) {
113
				echo "NanoBSD upgrade file detected...\n";
114
				$type = "nanobsd";
115
			} else {
116
				$type = "normal";
117
			}
118
			do_upgrade("/root/firmware.tgz", $type);
119
			clear_subsystem_dirty('firmware');
120
			exit;
121
		}
122
	case "2":
123
		echo "\nEnter the complete path to the .tgz or .img.gz update file: ";
124
		$path = chop(fgets($fp));
125
		if (!$path) {
126
			fclose($fp);
127
			die;
128
		}
129
		if (stristr($path, "nanobsd")) {
130
			$type = "nanobsd";
131
		}
132
		if (file_exists($path)) {
133
			mark_subsystem_dirty('firmware');
134
			do_upgrade($path, $type);
135
			clear_subsystem_dirty('firmware');
136
		} else {
137
			echo "\nCould not find file.\n\n";
138
			fclose($fp);
139
			die -1;
140
		}
141
}
142

    
143
function do_upgrade($path, $type) {
144
	global $g, $fp;
145

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

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

    
190
?>
(58-58/105)