Projet

Général

Profil

Télécharger (5,43 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / etc / rc.initial.firmware_update @ master

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
$pkg_interface = 'console';
50
$command = strtoupper(chop(fgets($fp)));
51

    
52
switch ($command) {
53
	case "q":
54
	case "quit":
55
		echo "\n";
56
		fclose($fp);
57
		die;
58
	break;
59
	case "1":
60
		echo "\nEnter the URL to the .tgz or .img.gz update file. \nType 'auto' to use {$autoupdateurl}\n> ";
61
		$url = chop(fgets($fp));
62
		if(!$url) { 
63
			fclose($fp);
64
			die;
65
		}
66
		if($url == "auto") {
67
			$url = $autoupdateurl;
68
		}
69
		$status = does_url_exist($url);
70
		if($status) {
71
			conf_mount_rw();
72
			mark_subsystem_dirty('firmware');
73
			unlink_if_exists("/root/firmware.tgz");
74
			echo "\nFetching file... ";
75
			download_file_with_progress_bar($url, '/root/firmware.tgz');
76
			if(!file_exists("/root/firmware.tgz")) {
77
				echo "Something went wrong during file transfer.  Exiting.\n\n";
78
				fclose($fp);
79
				clear_subsystem_dirty('firmware');
80
				die;
81
			}
82
			$status = does_url_exist("$url.sha256");
83
			if($status) { 
84
				echo "\nFetching sha256... ";
85
				download_file_with_progress_bar($url . ".sha256", '/root/firmware.tgz.sha256');
86
				echo "\n";
87
			} else {
88
				echo "\n\nWARNING.\n";
89
				echo "\nCould not locate a sha256 file.  We cannot verify the download once completed.\n\n";
90
				echo "Do you still want to proceed with the upgrade [n]? ";
91
				$answer = strtoupper(chop(fgets($fp)));
92
				if ($answer == "Y" or $answer == "YES") {
93
					echo "\nContinuing upgrade...";
94
				} else {
95
					echo "\nUpgrade cancelled.\n\n";
96
					die;
97
				}
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
					unlink_if_exists("/root/firmware.tgz.sha256");
107
					fclose($fp);
108
					clear_subsystem_dirty('firmware');
109
					die -1;
110
				}
111
				echo "\nsha256 checksum matches.\n";
112
				unlink_if_exists("/root/firmware.tgz.sha256");
113
			}
114
			if(strstr($url,"bdiff")) {
115
				echo "Binary DIFF upgrade file detected...\n";
116
				$type = "bdiff";
117
			} elseif(strstr($url,"nanobsd")) {
118
				echo "NanoBSD upgrade file detected...\n";
119
				$type = "nanobsd";
120
			} else {
121
				$type = "normal";
122
			}
123
			do_upgrade("/root/firmware.tgz", $type);
124
			clear_subsystem_dirty('firmware');
125
			exit;
126
		}
127
	case "2":
128
		echo "\nEnter the complete path to the .tgz or .img.gz update file: ";
129
		$path = chop(fgets($fp));
130
		if(!$path) {
131
			fclose($fp);
132
			die;
133
		}
134
		if(stristr($path,"bdiff"))
135
			$type = "bdiff";
136
		if(stristr($path,"nanobsd"))
137
			$type = "nanobsd";			
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
	else if ($sigchk == 2)
156
		$sig_warning = "This image is not digitally signed.";
157
	else if (($sigchk == 3) || ($sigchk == 4))
158
		$sig_warning = "There has been an error verifying the signature on this image.";
159
	if($sig_warning) {
160
		$sig_warning = "\nWARNING! ACHTUNG! DANGER!\n\n{$sig_warning}\n\n" .
161
			"This means that the image you uploaded is not an official/supported image and\n" .
162
			"may lead to unexpected behavior or security compromises.\n\n" .
163
			"Only install images that come from sources that you trust, and make sure\n".
164
			"that the image has not been tampered with.\n\n".
165
			"Do you want to install this image anyway at your own risk [n]?";
166
		echo $sig_warning;
167
		$command = strtoupper(chop(fgets($fp)));
168
		if(strtoupper($command) == "Y" or strtoupper($command) == "Y" or strtoupper($command) == "YES") {
169
			echo "\nContinuing upgrade...";
170
		} else {
171
			echo "\nUpgrade cancelled.\n\n";
172
			die;
173
		}
174
	}
175
	mark_subsystem_dirty('firmwarelock');
176
	echo "\nOne moment please...\nInvoking firmware upgrade...";
177
	if($type == "bdiff")
178
		mwexec_bg("/etc/rc.firmware delta_update $path");
179
	elseif($type == "nanobsd")
180
		mwexec_bg("/etc/rc.firmware pfSenseNanoBSDupgrade $path");
181
	else
182
		mwexec_bg("/etc/rc.firmware pfSenseupgrade $path");
183
	sleep(10);
184
	while(is_subsystem_dirty('firmwarelock')) {
185
		sleep(1);
186
		echo ".";
187
	}
188
	sleep(10);
189
	echo "Done.  Rebooting...\n\n";
190
	clear_subsystem_dirty('firmwarelock');
191
}
192

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

    
196
?>
(56-56/103)