1
|
#!/usr/local/bin/php -f
|
2
|
|
3
|
<?php
|
4
|
|
5
|
require("globals.inc");
|
6
|
require("config.inc");
|
7
|
require("functions.inc");
|
8
|
|
9
|
$g['booting'] = true;
|
10
|
|
11
|
echo "Starting the {$g['product_name']} console firmware update system";
|
12
|
|
13
|
require("functions.inc");
|
14
|
echo ".";
|
15
|
|
16
|
$g['booting'] = false;
|
17
|
|
18
|
if(isset($config['system']['firmware']['alturl']['enable']))
|
19
|
$updater_url = "{$config['system']['firmware']['alturl']['firmwareurl']}";
|
20
|
else
|
21
|
$updater_url = $g['update_url'];
|
22
|
|
23
|
$nanosize = "";
|
24
|
if ($g['platform'] == "nanobsd") {
|
25
|
if (file_exists("/etc/nano_use_vga.txt"))
|
26
|
$nanosize = "-nanobsd-vga-";
|
27
|
else
|
28
|
$nanosize = "-nanobsd-";
|
29
|
|
30
|
$nanosize .= strtolower(trim(file_get_contents("/etc/nanosize.txt")));
|
31
|
$update_filename = "latest{$nanosize}.img.gz";
|
32
|
} else {
|
33
|
$update_filename = "latest.tgz";
|
34
|
}
|
35
|
$autoupdateurl = "{$updater_url}/{$update_filename}";
|
36
|
|
37
|
$fp = fopen('php://stdin', 'r');
|
38
|
|
39
|
echo ".\n\n";
|
40
|
|
41
|
$shell_active = true;
|
42
|
|
43
|
echo "1) Update from a URL\n";
|
44
|
echo "2) Update from a local file\n";
|
45
|
echo "Q) Quit\n";
|
46
|
|
47
|
echo "\nPlease select an option to continue: ";
|
48
|
|
49
|
$command = strtoupper(chop(fgets($fp)));
|
50
|
|
51
|
switch ($command) {
|
52
|
case "q":
|
53
|
case "quit":
|
54
|
echo "\n";
|
55
|
fclose($fp);
|
56
|
die;
|
57
|
break;
|
58
|
case "1":
|
59
|
echo "\nEnter the URL to the .tgz or .img.gz update file. \nType 'auto' to use {$autoupdateurl}\n> ";
|
60
|
$url = chop(fgets($fp));
|
61
|
if(!$url) {
|
62
|
fclose($fp);
|
63
|
die;
|
64
|
}
|
65
|
if($url == "auto") {
|
66
|
$url = $autoupdateurl;
|
67
|
}
|
68
|
$status = does_url_exist($url);
|
69
|
if($status) {
|
70
|
conf_mount_rw();
|
71
|
mark_subsystem_dirty('firmware');
|
72
|
if(file_exists("/root/firmware.tgz"))
|
73
|
unlink("/root/firmware.tgz");
|
74
|
echo "\nFetching file size...\n";
|
75
|
$file_size = exec("fetch -s \"$url\"");
|
76
|
$file_size = trim($file_size, "\r");
|
77
|
echo "\nFile size: $file_size\n";
|
78
|
echo "\nFetching file...\n";
|
79
|
exec("fetch -1 -w15 -a -v -o /root/firmware.tgz \"$url\"");
|
80
|
if($file_size <> filesize("/root/firmware.tgz")) {
|
81
|
echo "\nFile size mismatch. Upgrade cancelled.\n\n";
|
82
|
fclose($fp);
|
83
|
die;
|
84
|
}
|
85
|
if(!file_exists("/root/firmware.tgz")) {
|
86
|
echo "Something went wrong during file transfer. Exiting.\n\n";
|
87
|
fclose($fp);
|
88
|
die;
|
89
|
}
|
90
|
$status = does_url_exist("$url.sha256");
|
91
|
if($status) {
|
92
|
echo "\nFetching sha256...\n";
|
93
|
exec("fetch -1 -w15 -a -v -o /root/firmware.tgz.sha256 \"$url.sha256\"");
|
94
|
} else {
|
95
|
echo "\n\nWARNING.\n";
|
96
|
echo "\nCould not locate a sha256 file. We cannot verify the download once completed.\n\n";
|
97
|
sleep(15);
|
98
|
}
|
99
|
if(file_exists("/root/firmware.tgz.sha256")) {
|
100
|
$source_sha256 = trim(`cat /root/firmware.tgz.sha256 | awk '{ print \$4 }'`,"\r");
|
101
|
$file_sha256 = trim(`sha256 /root/firmware.tgz | awk '{ print \$4 }'`,"\r");
|
102
|
echo "URL sha256: $source_sha256\n";
|
103
|
echo "Downloaded file sha256: $file_sha256\n";
|
104
|
if($source_sha256 <> $file_sha256) {
|
105
|
echo "\n\nsha256 checksum does not match. Cancelling upgrade.\n\n";
|
106
|
exec("rm -f /root/*.sha256");
|
107
|
fclose($fp);
|
108
|
die -1;
|
109
|
}
|
110
|
echo "\nsha256 checksum matches.\n";
|
111
|
exec("rm -f /root/*.sha256");
|
112
|
}
|
113
|
if(strstr($url,"bdiff")) {
|
114
|
echo "Binary DIFF upgrade file detected...\n";
|
115
|
$type = "bdiff";
|
116
|
} elseif(strstr($url,"nanobsd")) {
|
117
|
echo "NanoBSD upgrade file detected...\n";
|
118
|
$type = "nanobsd";
|
119
|
} else {
|
120
|
$type = "normal";
|
121
|
}
|
122
|
do_upgrade("/root/firmware.tgz", $type);
|
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
|
if(stristr($path,"nanobsd"))
|
135
|
$type = "nanobsd";
|
136
|
if(file_exists($path)) {
|
137
|
mark_subsystem_dirty('firmware');
|
138
|
do_upgrade($path, $type);
|
139
|
} else {
|
140
|
echo "\nCould not find file.\n\n";
|
141
|
fclose($fp);
|
142
|
die -1;
|
143
|
}
|
144
|
}
|
145
|
|
146
|
function do_upgrade($path, $type) {
|
147
|
global $g, $fp;
|
148
|
|
149
|
$sigchk = verify_digital_signature($path);
|
150
|
if ($sigchk == 1)
|
151
|
$sig_warning = "The digital signature on this image is invalid.";
|
152
|
else if ($sigchk == 2)
|
153
|
$sig_warning = "This image is not digitally signed.";
|
154
|
else if (($sigchk == 3) || ($sigchk == 4))
|
155
|
$sig_warning = "There has been an error verifying the signature on this image.";
|
156
|
if($sig_warning) {
|
157
|
$sig_warning = "\nWARNING! ACHTUNG! DANGER!\n\n{$sig_warning}\n\n" .
|
158
|
"This means that the image you uploaded is not an official/supported image and\n" .
|
159
|
"may lead to unexpected behavior or security compromises.\n\n" .
|
160
|
"Only install images that come from sources that you trust, and make sure\n".
|
161
|
"that the image has not been tampered with.\n\n".
|
162
|
"Do you want to install this image anyway at your own risk [n]?";
|
163
|
echo $sig_warning;
|
164
|
$command = strtoupper(chop(fgets($fp)));
|
165
|
if(strtoupper($command) == "Y" or strtoupper($command) == "Y" or strtoupper($command) == "YES") {
|
166
|
echo "\nContinuing upgrade...";
|
167
|
} else {
|
168
|
echo "\nUpgrade cancelled.\n\n";
|
169
|
die;
|
170
|
}
|
171
|
}
|
172
|
mark_subsystem_dirty('firmwarelock');
|
173
|
echo "\nOne moment please...\nInvoking firmware upgrade...";
|
174
|
if($type == "bdiff")
|
175
|
mwexec_bg("/etc/rc.firmware delta_update $path");
|
176
|
elseif($type == "nanobsd")
|
177
|
mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade $path");
|
178
|
else
|
179
|
mwexec_bg("/etc/rc.firmware pfSenseupgrade $path");
|
180
|
sleep(10);
|
181
|
while(is_subsystem_dirty('firmwarelock')) {
|
182
|
sleep(1);
|
183
|
echo ".";
|
184
|
}
|
185
|
sleep(10);
|
186
|
echo "Done. Rebooting...\n\n";
|
187
|
clear_subsystem_dirty('firmwarelock');
|
188
|
}
|
189
|
|
190
|
exec("rm -f /root/*.sha256");
|
191
|
fclose($fp);
|
192
|
|
193
|
?>
|