Projet

Général

Profil

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

univnautes / etc / rc.initial.firmware_update @ 1c52509c

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
				sleep(15);
91
			}
92
			if(file_exists("/root/firmware.tgz.sha256")) {
93
				$source_sha256 = trim(`cat /root/firmware.tgz.sha256 | awk '{ print \$4 }'`,"\r");
94
				$file_sha256 = trim(`sha256 /root/firmware.tgz | awk '{ print \$4 }'`,"\r");
95
				echo "URL sha256: $source_sha256\n";
96
				echo "Downloaded file sha256: $file_sha256\n";
97
				if($source_sha256 <> $file_sha256) {
98
					echo "\n\nsha256 checksum does not match.  Cancelling upgrade.\n\n";
99
					unlink_if_exists("/root/firmware.tgz.sha256");
100
					fclose($fp);
101
					clear_subsystem_dirty('firmware');
102
					die -1;
103
				}
104
				echo "\nsha256 checksum matches.\n";
105
				unlink_if_exists("/root/firmware.tgz.sha256");
106
			}
107
			if(strstr($url,"bdiff")) {
108
				echo "Binary DIFF upgrade file detected...\n";
109
				$type = "bdiff";
110
			} elseif(strstr($url,"nanobsd")) {
111
				echo "NanoBSD upgrade file detected...\n";
112
				$type = "nanobsd";
113
			} else {
114
				$type = "normal";
115
			}
116
			do_upgrade("/root/firmware.tgz", $type);
117
			clear_subsystem_dirty('firmware');
118
			exit;
119
		}
120
	case "2":
121
		echo "\nEnter the complete path to the .tgz or .img.gz update file: ";
122
		$path = chop(fgets($fp));
123
		if(!$path) {
124
			fclose($fp);
125
			die;
126
		}
127
		if(stristr($path,"bdiff"))
128
			$type = "bdiff";
129
		if(stristr($path,"nanobsd"))
130
			$type = "nanobsd";			
131
		if(file_exists($path)) {
132
			mark_subsystem_dirty('firmware');
133
			do_upgrade($path, $type);
134
			clear_subsystem_dirty('firmware');
135
		} else {
136
			echo "\nCould not find file.\n\n";
137
			fclose($fp);
138
			die -1;
139
		}
140
}
141

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

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

    
189
?>
(57-57/104)