]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/perl -w | |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | require "/opt/pakfire/etc/pakfire.conf"; | |
23 | require "/var/ipfire/general-functions.pl"; | |
24 | ||
25 | use File::Basename; | |
26 | use File::Copy; | |
27 | use LWP::UserAgent; | |
28 | use HTTP::Response; | |
29 | use HTTP::Headers; | |
30 | use HTTP::Message; | |
31 | use HTTP::Request; | |
32 | use Net::Ping; | |
33 | use URI; | |
34 | ||
35 | use Switch; | |
36 | ||
37 | package Pakfire; | |
38 | ||
39 | my @VALID_KEY_FINGERPRINTS = ( | |
40 | # 2018 | |
41 | "3ECA8AA4478208B924BB96206FEF7A8ED713594B", | |
42 | # 2007 | |
43 | "179740DC4D8C47DC63C099C74BDE364C64D96617", | |
44 | ); | |
45 | ||
46 | # A small color-hash :D | |
47 | our %color; | |
48 | $color{'normal'} = "\033[0m"; | |
49 | $color{'black'} = "\033[0;30m"; | |
50 | $color{'darkgrey'} = "\033[1;30m"; | |
51 | $color{'blue'} = "\033[0;34m"; | |
52 | $color{'lightblue'} = "\033[1;34m"; | |
53 | $color{'green'} = "\033[0;32m"; | |
54 | $color{'lightgreen'} = "\033[1;32m"; | |
55 | $color{'cyan'} = "\033[0;36m"; | |
56 | $color{'lightcyan'} = "\033[1;36m"; | |
57 | $color{'red'} = "\033[0;31m"; | |
58 | $color{'lightred'} = "\033[1;31m"; | |
59 | $color{'purple'} = "\033[0;35m"; | |
60 | $color{'lightpurple'} = "\033[1;35m"; | |
61 | $color{'brown'} = "\033[0;33m"; | |
62 | $color{'lightgrey'} = "\033[0;37m"; | |
63 | $color{'yellow'} = "\033[1;33m"; | |
64 | $color{'white'} = "\033[1;37m"; | |
65 | our $enable_colors = 1; | |
66 | ||
67 | my $final_data; | |
68 | my $total_size; | |
69 | my $bfile; | |
70 | ||
71 | my %pakfiresettings = (); | |
72 | &General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings); | |
73 | ||
74 | # Make version | |
75 | $Conf::version = &make_version(); | |
76 | ||
77 | # Pakfire lock file. | |
78 | our $lockfile = "/tmp/pakfire_lock"; | |
79 | ||
80 | sub message { | |
81 | my $message = shift; | |
82 | ||
83 | logger("$message"); | |
84 | if ( $enable_colors == 1 ) { | |
85 | if ("$message" =~ /ERROR/) { | |
86 | $message = "$color{'red'}$message$color{'normal'}"; | |
87 | } elsif ("$message" =~ /INFO/) { | |
88 | $message = "$color{'cyan'}$message$color{'normal'}"; | |
89 | } elsif ("$message" =~ /WARN/) { | |
90 | $message = "$color{'yellow'}$message$color{'normal'}"; | |
91 | } elsif ("$message" =~ /RESV/) { | |
92 | $message = "$color{'purple'}$message$color{'normal'}"; | |
93 | } elsif ("$message" =~ /INST/) { | |
94 | $message = "$color{'green'}$message$color{'normal'}"; | |
95 | } elsif ("$message" =~ /REMV/) { | |
96 | $message = "$color{'lightred'}$message$color{'normal'}"; | |
97 | } elsif ("$message" =~ /UPGR/) { | |
98 | $message = "$color{'lightblue'}$message$color{'normal'}"; | |
99 | } | |
100 | } | |
101 | print "$message\n"; | |
102 | ||
103 | } | |
104 | ||
105 | sub logger { | |
106 | my $log = shift; | |
107 | if ($log) { | |
108 | #system("echo \"`date`: $log\" >> /var/log/pakfire.log"); | |
109 | system("logger -t pakfire \"$log\""); | |
110 | } | |
111 | } | |
112 | ||
113 | sub usage { | |
114 | &Pakfire::message("Usage: pakfire <install|remove> [options] <pak(s)>"); | |
115 | &Pakfire::message(" <update> - Contacts the servers for new lists of paks."); | |
116 | &Pakfire::message(" <upgrade> - Installs the latest version of all paks."); | |
117 | &Pakfire::message(" <list> - Outputs a short list with all available paks."); | |
118 | &Pakfire::message(" <status> - Outputs a summary about available core upgrades, updates and a required reboot"); | |
119 | &Pakfire::message(""); | |
120 | &Pakfire::message(" Global options:"); | |
121 | &Pakfire::message(" --non-interactive --> Enables the non-interactive mode."); | |
122 | &Pakfire::message(" You won't see any question here."); | |
123 | &Pakfire::message(" -y --> Short for --non-interactive."); | |
124 | &Pakfire::message(" --no-colors --> Turns off the wonderful colors."); | |
125 | &Pakfire::message(""); | |
126 | exit 1; | |
127 | } | |
128 | ||
129 | sub fetchfile { | |
130 | my $getfile = shift; | |
131 | my $gethost = shift; | |
132 | my (@server, $host, $proto, $file, $i); | |
133 | my $allok = 0; | |
134 | ||
135 | use File::Basename; | |
136 | $bfile = basename("$getfile"); | |
137 | ||
138 | logger("DOWNLOAD STARTED: $getfile"); | |
139 | ||
140 | $i = 0; | |
141 | while (($allok == 0) && $i < 5) { | |
142 | $i++; | |
143 | ||
144 | if ("$gethost" eq "") { | |
145 | @server = selectmirror(); | |
146 | $proto = $server[0]; | |
147 | $host = $server[1]; | |
148 | $file = "$server[2]/$getfile"; | |
149 | } else { | |
150 | $host = $gethost; | |
151 | $file = $getfile; | |
152 | } | |
153 | ||
154 | $proto = "HTTPS" unless $proto; | |
155 | ||
156 | logger("DOWNLOAD INFO: Host: $host ($proto) - File: $file"); | |
157 | ||
158 | # Init LWP::UserAgent, request SSL hostname verification | |
159 | # and specify CA file. | |
160 | my $ua = LWP::UserAgent->new( | |
161 | ssl_opts => { | |
162 | SSL_ca_file => '/etc/ssl/cert.pem', | |
163 | verify_hostname => 1, | |
164 | } | |
165 | ); | |
166 | $ua->agent("Pakfire/$Conf::version"); | |
167 | $ua->timeout(20); | |
168 | ||
169 | my %proxysettings=(); | |
170 | &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings); | |
171 | ||
172 | if ($proxysettings{'UPSTREAM_PROXY'}) { | |
173 | logger("DOWNLOAD INFO: Upstream proxy: \"$proxysettings{'UPSTREAM_PROXY'}\""); | |
174 | if ($proxysettings{'UPSTREAM_USER'}) { | |
175 | $ua->proxy(["http", "https"], "http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$proxysettings{'UPSTREAM_PROXY'}/"); | |
176 | logger("DOWNLOAD INFO: Logging in with \"$proxysettings{'UPSTREAM_USER'}\" against \"$proxysettings{'UPSTREAM_PROXY'}\""); | |
177 | } else { | |
178 | $ua->proxy(["http", "https"], "http://$proxysettings{'UPSTREAM_PROXY'}/"); | |
179 | } | |
180 | } | |
181 | ||
182 | $final_data = undef; | |
183 | ||
184 | my $url; | |
185 | switch ($proto) { | |
186 | case "HTTP" { $url = "http://$host/$file"; } | |
187 | case "HTTPS" { $url = "https://$host/$file"; } | |
188 | else { | |
189 | # skip all lines with unknown protocols | |
190 | logger("DOWNLOAD WARNING: Skipping Host: $host due to unknown protocol ($proto) in mirror database"); | |
191 | next; | |
192 | } | |
193 | } | |
194 | ||
195 | my $result = $ua->head($url); | |
196 | my $remote_headers = $result->headers; | |
197 | $total_size = $remote_headers->content_length; | |
198 | logger("DOWNLOAD INFO: $file has size of $total_size bytes"); | |
199 | ||
200 | my $response = $ua->get($url, ':content_cb' => \&callback ); | |
201 | message(""); | |
202 | ||
203 | my $code = $response->code(); | |
204 | my $log = $response->status_line; | |
205 | logger("DOWNLOAD INFO: HTTP-Status-Code: $code - $log"); | |
206 | ||
207 | if ( $code eq "500" ) { | |
208 | message("Giving up: There was no chance to get the file \"$getfile\" from any available server.\nThere was an error on the way. Please fix it."); | |
209 | return 0; | |
210 | } | |
211 | ||
212 | if ($response->is_success) { | |
213 | if (open(FILE, ">$Conf::tmpdir/$bfile")) { | |
214 | print FILE $final_data; | |
215 | close(FILE); | |
216 | logger("DOWNLOAD INFO: File received. Start checking signature..."); | |
217 | if (&valid_signature("$Conf::tmpdir/$bfile")) { | |
218 | logger("DOWNLOAD INFO: Signature of $bfile is fine."); | |
219 | move("$Conf::tmpdir/$bfile","$Conf::cachedir/$bfile"); | |
220 | } else { | |
221 | message("DOWNLOAD ERROR: The downloaded file ($file) wasn't verified by IPFire.org. Sorry - Exiting..."); | |
222 | my $ntp = `ntpdate -q -t 10 pool.ntp.org 2>/dev/null | tail -1`; | |
223 | if ( $ntp !~ /time\ server(.*)offset(.*)/ ){message("TIME ERROR: Unable to get the nettime, this may lead to the verification error.");} | |
224 | else { $ntp =~ /time\ server(.*)offset(.*)/; message("TIME INFO: Time Server$1has$2 offset to localtime.");} | |
225 | exit 1; | |
226 | } | |
227 | logger("DOWNLOAD FINISHED: $file"); | |
228 | $allok = 1; | |
229 | return 1; | |
230 | } else { | |
231 | logger("DOWNLOAD ERROR: Could not open $Conf::tmpdir/$bfile for writing."); | |
232 | } | |
233 | } else { | |
234 | logger("DOWNLOAD ERROR: $log"); | |
235 | } | |
236 | } | |
237 | message("DOWNLOAD ERROR: There was no chance to get the file \"$getfile\" from any available server.\nMay be you should run \"pakfire update\" to get some new servers."); | |
238 | return 0; | |
239 | } | |
240 | ||
241 | sub getmirrors { | |
242 | my $force = shift; | |
243 | my $age; | |
244 | ||
245 | use File::Copy; | |
246 | ||
247 | if ( -e "$Conf::dbdir/lists/server-list.db" ) { | |
248 | my @stat = stat("$Conf::dbdir/lists/server-list.db"); | |
249 | my $time = time(); | |
250 | $age = $time - $stat[9]; | |
251 | $force = "force" if ("$age" >= "3600"); | |
252 | logger("MIRROR INFO: server-list.db is $age seconds old. - DEBUG: $force"); | |
253 | } else { | |
254 | # Force an update. | |
255 | $force = "force"; | |
256 | } | |
257 | ||
258 | if ("$force" eq "force") { | |
259 | if (fetchfile("$Conf::version/lists/server-list.db", "$Conf::mainserver")) { | |
260 | move("$Conf::cachedir/server-list.db", "$Conf::dbdir/lists/server-list.db"); | |
261 | } elsif (! -e "$Conf::dbdir/lists/server-list.db" ) { | |
262 | # if we end up with no server-list at all, return failure | |
263 | return 0; | |
264 | } | |
265 | } | |
266 | return 1; | |
267 | } | |
268 | ||
269 | sub getcoredb { | |
270 | my $force = shift; | |
271 | my $age; | |
272 | ||
273 | use File::Copy; | |
274 | ||
275 | if ( -e "$Conf::dbdir/lists/core-list.db" ) { | |
276 | my @stat = stat("$Conf::dbdir/lists/core-list.db"); | |
277 | my $time = time(); | |
278 | $age = $time - $stat[9]; | |
279 | $force = "force" if ("$age" >= "3600"); | |
280 | logger("CORE INFO: core-list.db is $age seconds old. - DEBUG: $force"); | |
281 | } else { | |
282 | # Force an update. | |
283 | $force = "force"; | |
284 | } | |
285 | ||
286 | if ("$force" eq "force") { | |
287 | if (fetchfile("lists/core-list.db", "")) { | |
288 | move("$Conf::cachedir/core-list.db", "$Conf::dbdir/lists/core-list.db"); | |
289 | } | |
290 | } | |
291 | } | |
292 | ||
293 | sub valid_signature($) { | |
294 | my $filename = shift; | |
295 | ||
296 | open(my $cmd, "gpg --verify --status-fd 1 \"$filename\" 2>/dev/null |"); | |
297 | while (<$cmd>) { | |
298 | # Process valid signature lines | |
299 | if (/VALIDSIG ([A-Z0-9]+)/) { | |
300 | # Check if we know the key | |
301 | foreach my $key (@VALID_KEY_FINGERPRINTS) { | |
302 | # Signature is valid | |
303 | return 1 if ($key eq $1); | |
304 | } | |
305 | } | |
306 | } | |
307 | close($cmd); | |
308 | ||
309 | # Signature is invalid | |
310 | return 0; | |
311 | } | |
312 | ||
313 | sub selectmirror { | |
314 | if (defined ${Conf::mirror}) { | |
315 | my $uri = URI->new("${Conf::mirror}"); | |
316 | ||
317 | # Only accept HTTPS mirrors | |
318 | if ($uri->scheme eq "https") { | |
319 | return ("HTTPS", $uri->host, $uri->path . "/" . ${Conf::version}); | |
320 | } else { | |
321 | message("MIRROR ERROR: Unsupported mirror: " . ${Conf::mirror}); | |
322 | } | |
323 | } | |
324 | ||
325 | ### Check if there is a current server list and read it. | |
326 | # If there is no list try to get one. | |
327 | unless (open(FILE, "<$Conf::dbdir/lists/server-list.db")) { | |
328 | unless (getmirrors("noforce")) { | |
329 | message("MIRROR ERROR: Could not find or download a server list"); | |
330 | exit 1; | |
331 | } | |
332 | } | |
333 | ||
334 | my @lines = <FILE>; | |
335 | close(FILE); | |
336 | ||
337 | ### Count the number of the servers in the list | |
338 | my $scount = 0; | |
339 | my @newlines; | |
340 | foreach (@lines) { | |
341 | if ("$_" =~ /.*;.*;.*;/ ) { | |
342 | push(@newlines,$_); | |
343 | $scount++; | |
344 | } | |
345 | } | |
346 | logger("MIRROR INFO: $scount servers found in list"); | |
347 | ||
348 | if ($scount eq 0) { | |
349 | logger("MIRROR INFO: Could not find any servers. Falling back to main server $Conf::mainserver"); | |
350 | return ("HTTPS", $Conf::mainserver, "/$Conf::version"); | |
351 | } | |
352 | ||
353 | ### Choose a random server and test if it is online | |
354 | # If the check fails try a new server. | |
355 | # This will never give up. | |
356 | my $servers = 0; | |
357 | while (1) { | |
358 | $server = int(rand($scount) + 1); | |
359 | $servers = 0; | |
360 | my ($line, $proto, $path, $host); | |
361 | my @templine; | |
362 | foreach $line (@newlines) { | |
363 | $servers++; | |
364 | if ($servers eq $server) { | |
365 | @templine = split(/\;/, $line); | |
366 | $proto = $templine[0]; | |
367 | $host = $templine[1]; | |
368 | $path = $templine[2]; | |
369 | ||
370 | return ($proto, $host, $path); | |
371 | } | |
372 | } | |
373 | } | |
374 | } | |
375 | ||
376 | sub dbgetlist { | |
377 | ### Update the database if the file is older than one day. | |
378 | # If you pass &Pakfire::dbgetlist(force) the list will be downloaded. | |
379 | # Usage is always with an argument. | |
380 | my $force = shift; | |
381 | my $age; | |
382 | ||
383 | use File::Copy; | |
384 | ||
385 | if ( -e "$Conf::dbdir/lists/packages_list.db" ) { | |
386 | my @stat = stat("$Conf::dbdir/lists/packages_list.db"); | |
387 | my $time = time(); | |
388 | $age = $time - $stat[9]; | |
389 | $force = "force" if ("$age" >= "3600"); | |
390 | logger("DB INFO: packages_list.db is $age seconds old. - DEBUG: $force"); | |
391 | } else { | |
392 | # Force an update. | |
393 | $force = "force"; | |
394 | } | |
395 | ||
396 | if ("$force" eq "force") { | |
397 | if (fetchfile("lists/packages_list.db", "")) { | |
398 | move("$Conf::cachedir/packages_list.db", "$Conf::dbdir/lists/packages_list.db"); | |
399 | } elsif ( -e "$Conf::dbdir/lists/packages_list.db" ) { | |
400 | # If we end up with no db file after download error there | |
401 | # is nothing more we can do here. | |
402 | return 0; | |
403 | } | |
404 | } | |
405 | ||
406 | # Update the meta database if new packages was in the package list | |
407 | my $file; | |
408 | my $line; | |
409 | my $prog; | |
410 | my %metadata; | |
411 | my @templine; | |
412 | ||
413 | my %paklist = &Pakfire::dblist("all"); | |
414 | ||
415 | opendir(DIR,"$Conf::dbdir/meta"); | |
416 | my @files = readdir(DIR); | |
417 | closedir(DIR); | |
418 | foreach $file (@files) { | |
419 | next if ( $file eq "." ); | |
420 | next if ( $file eq ".." ); | |
421 | next if ( $file eq "meta-" ); | |
422 | next if ( $file =~ /^old/ ); | |
423 | %metadata = parsemetafile("$Conf::dbdir/meta/$file"); | |
424 | ||
425 | if ((defined $paklist{"$metadata{'Name'}"}) && ( | |
426 | ("$paklist{\"$metadata{'Name'}\"}{'Release'}" ne "$metadata{'Release'}") || | |
427 | (defined $paklist{"$metadata{'Name'}"}{'AvailableRelease'})) | |
428 | ) { | |
429 | move("$Conf::dbdir/meta/meta-$metadata{'Name'}","$Conf::dbdir/meta/old_meta-$metadata{'Name'}"); | |
430 | getmetafile($metadata{'Name'}); | |
431 | } | |
432 | } | |
433 | } | |
434 | ||
435 | sub coredbinfo { | |
436 | ### This subroutine returns core db version information in a hash. | |
437 | # Usage is without arguments | |
438 | ||
439 | eval(`grep "core_" $Conf::dbdir/lists/core-list.db`); | |
440 | ||
441 | my %coredb = ( | |
442 | CoreVersion => $Conf::version, | |
443 | Release => $Conf::core_mine, | |
444 | ); | |
445 | ||
446 | $coredb{'AvailableRelease'} = $core_release if ("$Conf::core_mine" < "$core_release"); | |
447 | ||
448 | return %coredb; | |
449 | } | |
450 | ||
451 | sub dblist { | |
452 | ### This subroutine returns the packages from the packages_list db in a hash. | |
453 | # It uses the currently cached version of packages_list. To ensure latest | |
454 | # data, run Pakfire::dbgetlist first. | |
455 | # You may also pass a filter: &Pakfire::dblist(filter) | |
456 | # Usage is always with one argument. | |
457 | # filter may be: | |
458 | # - "all": list all known paks, | |
459 | # - "notinstalled": list only not installed paks, | |
460 | # - "installed": list only installed paks | |
461 | # - "upgrade": list only upgradable paks | |
462 | # | |
463 | # Returned hash format: | |
464 | # ( "<pak name>" => ( | |
465 | # "Installed" => "Yes" or "No" wether the pak is installed, | |
466 | # "ProgVersion" => Installed program version when "Installed" => "Yes" or | |
467 | # Available version when "Installed" => No, | |
468 | # "Release" => Installed pak release number when "Installed" => "Yes" or | |
469 | # Available pak release number when "Installed" => No, | |
470 | # "AvailableProgVersion" => Available program version. | |
471 | # Only defined if an upgrade to a higher version is available, | |
472 | # "AvailableRelease" => Available pak release version. | |
473 | # Only defined if an upgrade to a higher version is available | |
474 | # ), | |
475 | # ... | |
476 | # ) | |
477 | ||
478 | my $filter = shift; | |
479 | my %paklist = (); | |
480 | my $file; | |
481 | my $line; | |
482 | my %metadata; | |
483 | my @templine; | |
484 | ||
485 | open(FILE, "<$Conf::dbdir/lists/packages_list.db"); | |
486 | my @db = <FILE>; | |
487 | close(FILE); | |
488 | ||
489 | if ("$filter" ne "notinstalled") { | |
490 | opendir(DIR,"$Conf::dbdir/installed"); | |
491 | my @files = readdir(DIR); | |
492 | closedir(DIR); | |
493 | ||
494 | foreach $file (@files) { | |
495 | next if ( $file eq "." ); | |
496 | next if ( $file eq ".." ); | |
497 | next if ( $file =~ /^old/ ); | |
498 | %metadata = parsemetafile("$Conf::dbdir/installed/$file"); | |
499 | ||
500 | foreach $line (@db) { | |
501 | next unless ($line =~ /.*;.*;.*;/ ); | |
502 | @templine = split(/\;/,$line); | |
503 | if (("$metadata{'Name'}" eq "$templine[0]") && ("$metadata{'Release'}" < "$templine[2]")) { | |
504 | # Add all upgradable paks to list | |
505 | $paklist{"$metadata{'Name'}"} = { | |
506 | ProgVersion => $metadata{'ProgVersion'}, | |
507 | Release => $metadata{'Release'}, | |
508 | AvailableProgVersion => $templine[1], | |
509 | AvailableRelease => $templine[2], | |
510 | Installed => "yes" | |
511 | }; | |
512 | last; | |
513 | } elsif (("$metadata{'Name'}" eq "$templine[0]") && ("$filter" ne "upgrade")) { | |
514 | # Add installed paks without an upgrade available to list | |
515 | $paklist{"$metadata{'Name'}"} = { | |
516 | ProgVersion => $metadata{'ProgVersion'}, | |
517 | Release => $metadata{'Release'}, | |
518 | Installed => "yes" | |
519 | }; | |
520 | last; | |
521 | } | |
522 | } | |
523 | } | |
524 | } | |
525 | ||
526 | # Add all not installed paks to list | |
527 | if (("$filter" ne "upgrade") && ("$filter" ne "installed")) { | |
528 | foreach $line (@db) { | |
529 | next unless ($line =~ /.*;.*;.*;/ ); | |
530 | @templine = split(/\;/,$line); | |
531 | next if ((defined $paklist{"$templine[0]"}) || (&isinstalled($templine[0]) == 0)); | |
532 | ||
533 | $paklist{"$templine[0]"} = { | |
534 | ProgVersion => "$templine[1]", | |
535 | Release => "$templine[2]", | |
536 | Installed => "no" | |
537 | }; | |
538 | } | |
539 | } | |
540 | ||
541 | return %paklist; | |
542 | } | |
543 | ||
544 | sub resolvedeps_one { | |
545 | my $pak = shift; | |
546 | ||
547 | message("PAKFIRE RESV: $pak: Resolving dependencies..."); | |
548 | ||
549 | unless (getmetafile("$pak")) { | |
550 | message("PAKFIRE ERROR: Error retrieving dependency information on $pak. Unable to resolve dependencies."); | |
551 | exit 1; | |
552 | }; | |
553 | ||
554 | my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak"); | |
555 | my @all; | |
556 | my @deps = split(/ /, $metadata{'Dependencies'}); | |
557 | chomp (@deps); | |
558 | foreach (@deps) { | |
559 | if ($_) { | |
560 | my $return = &isinstalled($_); | |
561 | if ($return eq 0) { | |
562 | message("PAKFIRE RESV: $pak: Dependency is already installed: $_"); | |
563 | } else { | |
564 | message("PAKFIRE RESV: $pak: Need to install dependency: $_"); | |
565 | push(@all,$_); | |
566 | } | |
567 | } | |
568 | } | |
569 | ||
570 | return @all; | |
571 | } | |
572 | ||
573 | sub resolvedeps { | |
574 | my $pak = shift; | |
575 | my @all; | |
576 | ||
577 | # Resolve all not yet installed dependencies of $pak | |
578 | my @deps = &resolvedeps_one($pak); | |
579 | push(@all, @deps); | |
580 | ||
581 | # For each dependency, we check if more dependencies exist | |
582 | while (@deps) { | |
583 | my $dep = pop(@deps); | |
584 | ||
585 | my @subdeps = &resolvedeps_one($dep); | |
586 | foreach my $subdep (@subdeps) { | |
587 | # Skip the package we are currently resolving for | |
588 | next if ($pak eq $subdep); | |
589 | ||
590 | # If the package is not already to be installed, | |
591 | # we add it to the list (@all) and check if it has | |
592 | # more dependencies on its own. | |
593 | unless (grep {$_ eq $subdep} @all) { | |
594 | push(@deps, $subdep); | |
595 | push(@all, $subdep); | |
596 | } | |
597 | } | |
598 | } | |
599 | ||
600 | return @all; | |
601 | } | |
602 | ||
603 | sub resolvedeps_recursive { | |
604 | my @packages = @_; | |
605 | my @result = (); | |
606 | ||
607 | foreach my $pkg (@packages) { | |
608 | my @deps = &Pakfire::resolvedeps($pkg); | |
609 | ||
610 | foreach my $dep (@deps) { | |
611 | push(@result, $dep); | |
612 | } | |
613 | } | |
614 | ||
615 | # Sort the result array and remove dupes | |
616 | my %sort = map{ $_, 1 } @result; | |
617 | @result = keys %sort; | |
618 | ||
619 | return @result; | |
620 | } | |
621 | ||
622 | sub cleanup { | |
623 | my $dir = shift; | |
624 | my $path; | |
625 | ||
626 | logger("CLEANUP: $dir"); | |
627 | ||
628 | if ( "$dir" eq "meta" ) { | |
629 | $path = "$Conf::dbdir/meta"; | |
630 | } elsif ( "$dir" eq "tmp" ) { | |
631 | $path = "$Conf::tmpdir"; | |
632 | } | |
633 | chdir("$path"); | |
634 | opendir(DIR,"."); | |
635 | my @files = readdir(DIR); | |
636 | closedir(DIR); | |
637 | foreach (@files) { | |
638 | unless (($_ eq ".") || ($_ eq "..")) { | |
639 | system("rm -rf $_"); | |
640 | } | |
641 | } | |
642 | } | |
643 | ||
644 | sub getmetafile { | |
645 | my $pak = shift; | |
646 | ||
647 | # Try to download meta-file if we don't have one yet, or it is empty for some reason | |
648 | if ((! -e "$Conf::dbdir/meta/meta-$pak" ) || ( -z "$Conf::dbdir/meta/meta-$pak" )) { | |
649 | return 0 unless (fetchfile("meta/meta-$pak", "")); | |
650 | move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak"); | |
651 | } | |
652 | ||
653 | open(FILE, "<$Conf::dbdir/meta/meta-$pak"); | |
654 | my @line = <FILE>; | |
655 | close(FILE); | |
656 | ||
657 | open(FILE, ">$Conf::dbdir/meta/meta-$pak"); | |
658 | foreach (@line) { | |
659 | my $string = $_; | |
660 | $string =~ s/\r\n/\n/g; | |
661 | print FILE $string; | |
662 | } | |
663 | close(FILE); | |
664 | ||
665 | return 1; | |
666 | } | |
667 | ||
668 | sub getsize { | |
669 | my $pak = shift; | |
670 | ||
671 | getmetafile("$pak"); | |
672 | ||
673 | if (my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak")) { | |
674 | return $metadata{'Size'}; | |
675 | } | |
676 | return 0; | |
677 | } | |
678 | ||
679 | sub parsemetafile { | |
680 | ### This subroutine returns a hash with the contents of a meta- file | |
681 | # Pass path to metafile as argument: Pakfire::parsemetafile("$Conf::dbdir/meta/meta-$pak") | |
682 | # Usage is always with an argument. | |
683 | my $metafile = shift; | |
684 | ||
685 | my %metadata = (); | |
686 | ||
687 | my @templine; | |
688 | my @file; | |
689 | ||
690 | if (! -e $metafile ) { | |
691 | return 0; | |
692 | } | |
693 | ||
694 | open(FILE, "<$metafile"); | |
695 | @file = <FILE>; | |
696 | close(FILE); | |
697 | ||
698 | foreach (@file) { | |
699 | @templine = split(/\: /,$_); | |
700 | if ($templine[1]) { | |
701 | chomp($templine[1]); | |
702 | $metadata{"$templine[0]"} = $templine[1]; | |
703 | } | |
704 | } | |
705 | ||
706 | return %metadata; | |
707 | } | |
708 | ||
709 | sub decryptpak { | |
710 | my $pak = shift; | |
711 | ||
712 | cleanup("tmp"); | |
713 | ||
714 | my $file = getpak("$pak", "noforce"); | |
715 | ||
716 | logger("DECRYPT STARTED: $pak"); | |
717 | my $return = system("cd $Conf::tmpdir/ && gpg -d --batch --quiet --no-verbose --status-fd 2 --output - < $Conf::cachedir/$file 2>/dev/null | tar x"); | |
718 | $return %= 255; | |
719 | logger("DECRYPT FINISHED: $pak - Status: $return"); | |
720 | if ($return != 0) { exit 1; } | |
721 | } | |
722 | ||
723 | sub getpak { | |
724 | my $pak = shift; | |
725 | my $force = shift; | |
726 | ||
727 | unless (getmetafile("$pak")) { | |
728 | message("PAKFIRE ERROR: Unable to retrieve $pak metadata."); | |
729 | exit 1; | |
730 | } | |
731 | ||
732 | my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak"); | |
733 | my $file = $metadata{'File'}; | |
734 | ||
735 | unless ($file) { | |
736 | message("No filename given in meta-file."); | |
737 | exit 1; | |
738 | } | |
739 | ||
740 | unless ( "$force" eq "force" ) { | |
741 | if ( -e "$Conf::cachedir/$file" ) { | |
742 | return $file; | |
743 | } | |
744 | } | |
745 | ||
746 | unless (fetchfile("paks/$file", "")) { | |
747 | message("PAKFIRE ERROR: Unable to download $pak."); | |
748 | exit 1; | |
749 | } | |
750 | return $file; | |
751 | } | |
752 | ||
753 | sub setuppak { | |
754 | my $pak = shift; | |
755 | ||
756 | message("PAKFIRE INST: $pak: Decrypting..."); | |
757 | decryptpak("$pak"); | |
758 | ||
759 | message("PAKFIRE INST: $pak: Copying files and running post-installation scripts..."); | |
760 | my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1"); | |
761 | $return %= 255; | |
762 | if ($return == 0) { | |
763 | move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak"); | |
764 | cleanup("tmp"); | |
765 | copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/"); | |
766 | message("PAKFIRE INST: $pak: Finished."); | |
767 | message(""); | |
768 | } else { | |
769 | message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem."); | |
770 | exit $return; | |
771 | } | |
772 | return $return; | |
773 | } | |
774 | ||
775 | sub upgradecore { | |
776 | getcoredb("noforce"); | |
777 | eval(`grep "core_" $Conf::dbdir/lists/core-list.db`); | |
778 | if ("$core_release" > "$Conf::core_mine") { | |
779 | # Safety check for lazy testers: | |
780 | # Before we upgrade to the latest release, we re-install the previous release | |
781 | # to make sure that the tester has always been on the latest version. | |
782 | my $tree = &get_tree(); | |
783 | $Conf::core_mine-- if ($tree eq "testing" || $tree eq "unstable"); | |
784 | ||
785 | message("CORE UPGR: Upgrading from release $Conf::core_mine to $core_release"); | |
786 | ||
787 | my @seq = `seq $Conf::core_mine $core_release`; | |
788 | shift @seq; | |
789 | my $release; | |
790 | foreach $release (@seq) { | |
791 | chomp($release); | |
792 | getpak("core-upgrade-$release"); | |
793 | } | |
794 | ||
795 | foreach $release (@seq) { | |
796 | chomp($release); | |
797 | upgradepak("core-upgrade-$release"); | |
798 | } | |
799 | ||
800 | system("echo $core_release > $Conf::coredir/mine"); | |
801 | ||
802 | } else { | |
803 | message("CORE INFO: No new upgrades available. You are on release $Conf::core_mine."); | |
804 | } | |
805 | } | |
806 | ||
807 | sub isinstalled { | |
808 | my $pak = shift; | |
809 | if ( open(FILE,"<$Conf::dbdir/installed/meta-$pak") ) { | |
810 | close(FILE); | |
811 | return 0; | |
812 | } else { | |
813 | return 1; | |
814 | } | |
815 | } | |
816 | ||
817 | sub upgradepak { | |
818 | my $pak = shift; | |
819 | ||
820 | message("PAKFIRE UPGR: $pak: Decrypting..."); | |
821 | decryptpak("$pak"); | |
822 | ||
823 | message("PAKFIRE UPGR: $pak: Upgrading files and running post-upgrading scripts..."); | |
824 | my $return = system("cd $Conf::tmpdir && NAME=$pak ./update.sh >> $Conf::logdir/update-$pak.log 2>&1"); | |
825 | $return %= 255; | |
826 | if ($return == 0) { | |
827 | move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak"); | |
828 | cleanup("tmp"); | |
829 | copy("$Conf::dbdir/meta/meta-$pak", "$Conf::dbdir/installed/"); | |
830 | message("PAKFIRE UPGR: $pak: Finished."); | |
831 | message(""); | |
832 | } else { | |
833 | message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem."); | |
834 | exit $return; | |
835 | } | |
836 | return $return; | |
837 | } | |
838 | ||
839 | sub removepak { | |
840 | my $pak = shift; | |
841 | ||
842 | message("PAKFIRE REMV: $pak: Decrypting..."); | |
843 | decryptpak("$pak"); | |
844 | ||
845 | message("PAKFIRE REMV: $pak: Removing files and running post-removing scripts..."); | |
846 | my $return = system("cd $Conf::tmpdir && NAME=$pak ./uninstall.sh >> $Conf::logdir/uninstall-$pak.log 2>&1"); | |
847 | $return %= 255; | |
848 | if ($return == 0) { | |
849 | unlink("$Conf::dbdir/rootfiles/$pak"); | |
850 | unlink("$Conf::dbdir/installed/meta-$pak"); | |
851 | cleanup("tmp"); | |
852 | message("PAKFIRE REMV: $pak: Finished."); | |
853 | message(""); | |
854 | } else { | |
855 | message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem."); | |
856 | exit $return; | |
857 | } | |
858 | return $return; | |
859 | } | |
860 | ||
861 | sub beautifysize { | |
862 | my $size = shift; | |
863 | #$size = $size / 1024; | |
864 | my $unit; | |
865 | ||
866 | if ($size > 1023*1024) { | |
867 | $size = ($size / (1024*1024)); | |
868 | $unit = "MB"; | |
869 | } elsif ($size > 1023) { | |
870 | $size = ($size / 1024); | |
871 | $unit = "KB"; | |
872 | } else { | |
873 | $unit = "B"; | |
874 | } | |
875 | $size = sprintf("%.2f" , $size); | |
876 | my $string = "$size $unit"; | |
877 | return $string; | |
878 | } | |
879 | ||
880 | sub makeuuid { | |
881 | unless ( -e "$Conf::dbdir/uuid" ) { | |
882 | open(FILE, "</proc/sys/kernel/random/uuid"); | |
883 | my @line = <FILE>; | |
884 | close(FILE); | |
885 | ||
886 | open(FILE, ">$Conf::dbdir/uuid"); | |
887 | foreach (@line) { | |
888 | print FILE $_; | |
889 | } | |
890 | close(FILE); | |
891 | } | |
892 | } | |
893 | ||
894 | sub callback { | |
895 | my ($data, $response, $protocol) = @_; | |
896 | $final_data .= $data; | |
897 | print progress_bar( length($final_data), $total_size, 30, '=' ); | |
898 | } | |
899 | ||
900 | sub progress_bar { | |
901 | my ( $got, $total, $width, $char ) = @_; | |
902 | my $show_bfile; | |
903 | $width ||= 30; $char ||= '='; | |
904 | my $len_bfile = length $bfile; | |
905 | if ("$len_bfile" >= "17") { | |
906 | $show_bfile = substr($bfile,0,17)."..."; | |
907 | } else { | |
908 | $show_bfile = $bfile; | |
909 | } | |
910 | $progress = sprintf("%.2f%%", 100*$got/+$total); | |
911 | sprintf "$color{'lightgreen'}%-20s %7s |%-${width}s| %10s$color{'normal'}\r",$show_bfile, $progress, $char x (($width-1)*$got/$total). '>', beautifysize($got); | |
912 | } | |
913 | ||
914 | sub updates_available { | |
915 | # Get packets with updates available | |
916 | my %upgradepaks = &Pakfire::dblist("upgrade"); | |
917 | ||
918 | # Get the length of the returned hash | |
919 | my $updatecount = keys %upgradepaks; | |
920 | ||
921 | return "$updatecount"; | |
922 | } | |
923 | ||
924 | sub coreupdate_available { | |
925 | eval(`grep "core_" $Conf::dbdir/lists/core-list.db`); | |
926 | if ("$core_release" > "$Conf::core_mine") { | |
927 | return "yes ($core_release)"; | |
928 | } | |
929 | else { | |
930 | return "no"; | |
931 | } | |
932 | } | |
933 | ||
934 | sub reboot_required { | |
935 | if ( -e "/var/run/need_reboot" ) { | |
936 | return "yes"; | |
937 | } | |
938 | else { | |
939 | return "no"; | |
940 | } | |
941 | } | |
942 | ||
943 | sub status { | |
944 | # General info | |
945 | my $return = "Core-Version: $Conf::version\n"; | |
946 | $return .= "Core-Update-Level: $Conf::core_mine\n"; | |
947 | $return .= "Last update: " . &General::age("/opt/pakfire/db/core/mine") . " ago\n"; | |
948 | $return .= "Last core-list update: " . &General::age("/opt/pakfire/db/lists/core-list.db") . " ago\n"; | |
949 | $return .= "Last server-list update: " . &General::age("/opt/pakfire/db/lists/server-list.db") . " ago\n"; | |
950 | $return .= "Last packages-list update: " . &General::age("/opt/pakfire/db/lists/packages_list.db") . " ago\n"; | |
951 | ||
952 | # Get availability of core updates | |
953 | $return .= "Core-Update available: " . &Pakfire::coreupdate_available() . "\n"; | |
954 | ||
955 | # Get availability of package updates | |
956 | $return .= "Package-Updates available: " . &Pakfire::updates_available() . "\n"; | |
957 | ||
958 | # Test if reboot is required | |
959 | $return .= "Reboot required: " . &Pakfire::reboot_required() . "\n"; | |
960 | ||
961 | # Return status text | |
962 | print "$return"; | |
963 | exit 1; | |
964 | } | |
965 | ||
966 | sub get_arch() { | |
967 | # Append architecture | |
968 | my ($sysname, $nodename, $release, $version, $machine) = POSIX::uname(); | |
969 | ||
970 | # We only support armv6l for 32 bit arm | |
971 | if ($machine =~ m/armv[67]/) { | |
972 | return "armv6l"; | |
973 | } | |
974 | ||
975 | return $machine; | |
976 | } | |
977 | ||
978 | sub get_tree() { | |
979 | # Return stable if nothing is set | |
980 | return "stable" unless (defined $pakfiresettings{'TREE'}); | |
981 | ||
982 | return $pakfiresettings{'TREE'}; | |
983 | } | |
984 | ||
985 | sub make_version() { | |
986 | my $version = ""; | |
987 | ||
988 | # Open /etc/system-release | |
989 | open(RELEASE, "</etc/system-release"); | |
990 | my $release = <RELEASE>; | |
991 | close(RELEASE); | |
992 | ||
993 | # Add the main relase | |
994 | if ($release =~ m/IPFire ([\d\.]+)/) { | |
995 | $version .= $1; | |
996 | } | |
997 | ||
998 | # Append suffix for tree | |
999 | my $tree = &get_tree(); | |
1000 | if ($tree eq "testing") { | |
1001 | $version .= ".1"; | |
1002 | } elsif ($tree eq "unstable") { | |
1003 | $version .= ".2"; | |
1004 | } | |
1005 | ||
1006 | # Append architecture | |
1007 | $version .= "-" . &get_arch(); | |
1008 | ||
1009 | return $version; | |
1010 | } | |
1011 | ||
1012 | 1; |