validate GPG keys by fingerprint
[ipfire-2.x.git] / src / pakfire / lib / functions.pl
1 #!/usr/bin/perl -w
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2015 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
34 package Pakfire;
35
36 # GPG Keys
37 my $myid = "179740DC4D8C47DC63C099C74BDE364C64D96617"; # Our own gpg-key paks@ipfire.org
38 my $trustid = "A31D4F81EF4EBD07B456FA04D2BB0D0165D0FD58"; # gpg-key of CaCert
39
40 # A small color-hash :D
41 my %color;
42 $color{'normal'} = "\033[0m";
43 $color{'black'} = "\033[0;30m";
44 $color{'darkgrey'} = "\033[1;30m";
45 $color{'blue'} = "\033[0;34m";
46 $color{'lightblue'} = "\033[1;34m";
47 $color{'green'} = "\033[0;32m";
48 $color{'lightgreen'} = "\033[1;32m";
49 $color{'cyan'} = "\033[0;36m";
50 $color{'lightcyan'} = "\033[1;36m";
51 $color{'red'} = "\033[0;31m";
52 $color{'lightred'} = "\033[1;31m";
53 $color{'purple'} = "\033[0;35m";
54 $color{'lightpurple'} = "\033[1;35m";
55 $color{'brown'} = "\033[0;33m";
56 $color{'lightgrey'} = "\033[0;37m";
57 $color{'yellow'} = "\033[1;33m";
58 $color{'white'} = "\033[1;37m";
59 our $enable_colors = 1;
60
61 my $final_data;
62 my $total_size;
63 my $bfile;
64
65 my %pakfiresettings = ();
66 &General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
67
68 sub message {
69 my $message = shift;
70
71 logger("$message");
72 if ( $enable_colors == 1 ) {
73 if ("$message" =~ /ERROR/) {
74 $message = "$color{'red'}$message$color{'normal'}";
75 } elsif ("$message" =~ /INFO/) {
76 $message = "$color{'cyan'}$message$color{'normal'}";
77 } elsif ("$message" =~ /WARN/) {
78 $message = "$color{'yellow'}$message$color{'normal'}";
79 } elsif ("$message" =~ /RESV/) {
80 $message = "$color{'purple'}$message$color{'normal'}";
81 } elsif ("$message" =~ /INST/) {
82 $message = "$color{'green'}$message$color{'normal'}";
83 } elsif ("$message" =~ /REMV/) {
84 $message = "$color{'lightred'}$message$color{'normal'}";
85 } elsif ("$message" =~ /UPGR/) {
86 $message = "$color{'lightblue'}$message$color{'normal'}";
87 }
88 }
89 print "$message\n";
90
91 }
92
93 sub logger {
94 my $log = shift;
95 if ($log) {
96 #system("echo \"`date`: $log\" >> /var/log/pakfire.log");
97 system("logger -t pakfire \"$log\"");
98 }
99 }
100
101 sub usage {
102 &Pakfire::message("Usage: pakfire <install|remove> [options] <pak(s)>");
103 &Pakfire::message(" <update> - Contacts the servers for new lists of paks.");
104 &Pakfire::message(" <upgrade> - Installs the latest version of all paks.");
105 &Pakfire::message(" <list> - Outputs a short list with all available paks.");
106 &Pakfire::message("");
107 &Pakfire::message(" Global options:");
108 &Pakfire::message(" --non-interactive --> Enables the non-interactive mode.");
109 &Pakfire::message(" You won't see any question here.");
110 &Pakfire::message(" -y --> Short for --non-interactive.");
111 &Pakfire::message(" --no-colors --> Turns off the wonderful colors.");
112 &Pakfire::message("");
113 exit 1;
114 }
115
116 sub pinghost {
117 my $host = shift;
118
119 $p = Net::Ping->new("icmp");
120 if ($p->ping($host)) {
121 logger("PING INFO: $host is alive");
122 return 1;
123 } else {
124 logger("PING INFO: $host is unreachable");
125 return 0;
126 }
127 $p->close();
128 }
129
130 sub fetchfile {
131 my $getfile = shift;
132 my $gethost = shift;
133 my (@server, $host, $proto, $file, $i);
134 my $allok = 0;
135
136 use File::Basename;
137 $bfile = basename("$getfile");
138
139 logger("DOWNLOAD STARTED: $getfile") unless ($bfile =~ /^counter\?.*/);
140
141 $i = 0;
142 while (($allok == 0) && $i < 5) {
143 $i++;
144
145 if ("$gethost" eq "") {
146 @server = selectmirror();
147 $proto = $server[0];
148 $host = $server[1];
149 $file = "$server[2]/$getfile";
150 } else {
151 $host = $gethost;
152 $file = $getfile;
153 }
154
155 $proto = "HTTP" unless $proto;
156
157 unless ($bfile =~ /^counter\?.*/) {
158 logger("DOWNLOAD INFO: Host: $host ($proto) - File: $file");
159 }
160
161 my $ua = LWP::UserAgent->new;
162 $ua->agent("Pakfire/$Conf::version");
163 $ua->timeout(20);
164
165 my %proxysettings=();
166 &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings);
167
168 if ($proxysettings{'UPSTREAM_PROXY'}) {
169 logger("DOWNLOAD INFO: Upstream proxy: \"$proxysettings{'UPSTREAM_PROXY'}\"") unless ($bfile =~ /^counter.py\?.*/);
170 if ($proxysettings{'UPSTREAM_USER'}) {
171 $ua->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$proxysettings{'UPSTREAM_PROXY'}/");
172 logger("DOWNLOAD INFO: Logging in with: \"$proxysettings{'UPSTREAM_USER'}\" - \"$proxysettings{'UPSTREAM_PASSWORD'}\"") unless ($bfile =~ /^counter.py\?.*/);
173 } else {
174 $ua->proxy("http","http://$proxysettings{'UPSTREAM_PROXY'}/");
175 }
176 }
177
178 $final_data = undef;
179 my $url = "http://$host/$file";
180 my $response;
181
182 unless ($bfile =~ /^counter.py\?.*/) {
183 my $result = $ua->head($url);
184 my $remote_headers = $result->headers;
185 $total_size = $remote_headers->content_length;
186 logger("DOWNLOAD INFO: $file has size of $total_size bytes");
187
188 $response = $ua->get($url, ':content_cb' => \&callback );
189 message("");
190 } else {
191 $response = $ua->get($url);
192 }
193
194 my $code = $response->code();
195 my $log = $response->status_line;
196 logger("DOWNLOAD INFO: HTTP-Status-Code: $code - $log");
197
198 if ( $code eq "500" ) {
199 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.");
200 return 1;
201 }
202
203 if ($response->is_success) {
204 unless ($bfile =~ /^counter.py\?.*/) {
205 if (open(FILE, ">$Conf::tmpdir/$bfile")) {
206 print FILE $final_data;
207 close(FILE);
208 logger("DOWNLOAD INFO: File received. Start checking signature...");
209 if (system("gpg --verify \"$Conf::tmpdir/$bfile\" &>/dev/null") eq 0) {
210 logger("DOWNLOAD INFO: Signature of $bfile is fine.");
211 move("$Conf::tmpdir/$bfile","$Conf::cachedir/$bfile");
212 } else {
213 message("DOWNLOAD ERROR: The downloaded file ($file) wasn't verified by IPFire.org. Sorry - Exiting...");
214 my $ntp = `ntpdate -q -t 10 pool.ntp.org 2>/dev/null | tail -1`;
215 if ( $ntp !~ /time\ server(.*)offset(.*)/ ){message("TIME ERROR: Unable to get the nettime, this may lead to the verification error.");}
216 else { $ntp =~ /time\ server(.*)offset(.*)/; message("TIME INFO: Time Server$1has$2 offset to localtime.");}
217 exit 1;
218 }
219 logger("DOWNLOAD FINISHED: $file");
220 $allok = 1;
221 return 0;
222 } else {
223 logger("DOWNLOAD ERROR: Could not open $Conf::tmpdir/$bfile for writing.");
224 }
225 } else {
226 return 0;
227 }
228 } else {
229 logger("DOWNLOAD ERROR: $log");
230 }
231 }
232 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.");
233 return 1;
234 }
235
236 sub getmirrors {
237 my $force = shift;
238 my $age;
239
240 use File::Copy;
241
242 if ( -e "$Conf::dbdir/lists/server-list.db" ) {
243 my @stat = stat("$Conf::dbdir/lists/server-list.db");
244 my $time = time();
245 $age = $time - $stat[9];
246 $force = "force" if ("$age" >= "3600");
247 logger("MIRROR INFO: server-list.db is $age seconds old. - DEBUG: $force");
248 } else {
249 # Force an update.
250 $force = "force";
251 }
252
253 if ("$force" eq "force") {
254 fetchfile("$Conf::version/lists/server-list.db", "$Conf::mainserver");
255 move("$Conf::cachedir/server-list.db", "$Conf::dbdir/lists/server-list.db");
256 }
257 }
258
259 sub getcoredb {
260 my $force = shift;
261 my $age;
262
263 use File::Copy;
264
265 if ( -e "$Conf::dbdir/lists/core-list.db" ) {
266 my @stat = stat("$Conf::dbdir/lists/core-list.db");
267 my $time = time();
268 $age = $time - $stat[9];
269 $force = "force" if ("$age" >= "3600");
270 logger("CORE INFO: core-list.db is $age seconds old. - DEBUG: $force");
271 } else {
272 # Force an update.
273 $force = "force";
274 }
275
276 if ("$force" eq "force") {
277 fetchfile("lists/core-list.db", "");
278 move("$Conf::cachedir/core-list.db", "$Conf::dbdir/lists/core-list.db");
279 }
280 }
281
282
283 sub selectmirror {
284 ### Check if there is a current server list and read it.
285 # If there is no list try to get one.
286 my $count = 0;
287 while (!(open(FILE, "<$Conf::dbdir/lists/server-list.db")) && ($count lt 5)) {
288 $count++;
289 getmirrors("noforce");
290 }
291 if ($count == 5) {
292 message("MIRROR ERROR: Could not find or download a server list");
293 exit 1;
294 }
295 my @lines = <FILE>;
296 close(FILE);
297
298 ### Count the number of the servers in the list
299 my $scount = 0;
300 my @newlines;
301 foreach (@lines) {
302 if ("$_" =~ /.*;.*;.*;/ ) {
303 push(@newlines,$_);
304 $scount++;
305 }
306 }
307 logger("MIRROR INFO: $scount servers found in list");
308
309 if ($scount eq 0) {
310 logger("MIRROR INFO: Could not find any servers. Falling back to main server $Conf::mainserver");
311 return ("HTTP", $Conf::mainserver, "/$Conf::version");
312 }
313
314 ### Choose a random server and test if it is online
315 # If the check fails try a new server.
316 # This will never give up.
317 my $found = 0;
318 my $servers = 0;
319 my $pingdelay = 1;
320 while ($found == 0) {
321 $server = int(rand($scount) + 1);
322 $servers = 0;
323 my ($line, $proto, $path, $host);
324 my @templine;
325 foreach $line (@newlines) {
326 $servers++;
327 if ($servers eq $server) {
328 @templine = split(/\;/, $line);
329 $proto = $templine[0];
330 $host = $templine[1];
331 $path = $templine[2];
332 if ($pakfiresettings{'HEALTHCHECK'} eq "off") {
333 logger("PING INFO: Healthcheck is disabled");
334 $found = 1;
335 return ($proto, $host, $path);
336 }
337 elsif (pinghost("$host")) {
338 $found = 1;
339 return ($proto, $host, $path);
340 }
341 if ($found == 0) {
342 sleep($pingdelay);
343 $pingdelay=$pingdelay*2;
344 if ($pingdelay>1200) {
345 $pingdelay=1200;
346 }
347 }
348 }
349 }
350 }
351 }
352
353 sub dbgetlist {
354 ### Update the database if the file is older than one day.
355 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
356 # Usage is always with an argument.
357 my $force = shift;
358 my $age;
359
360 use File::Copy;
361
362 if ( -e "$Conf::dbdir/lists/packages_list.db" ) {
363 my @stat = stat("$Conf::dbdir/lists/packages_list.db");
364 my $time = time();
365 $age = $time - $stat[9];
366 $force = "force" if ("$age" >= "3600");
367 logger("DB INFO: packages_list.db is $age seconds old. - DEBUG: $force");
368 } else {
369 # Force an update.
370 $force = "force";
371 }
372
373 if ("$force" eq "force") {
374 fetchfile("lists/packages_list.db", "");
375 move("$Conf::cachedir/packages_list.db", "$Conf::dbdir/lists/packages_list.db");
376 }
377
378 # Update the meta database if new packages was in the package list
379 my @meta;
380 my $file;
381 my $line;
382 my $prog;
383 my ($name, $version, $release);
384 my @templine;
385
386 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
387 my @db = <FILE>;
388 close(FILE);
389
390 opendir(DIR,"$Conf::dbdir/meta");
391 my @files = readdir(DIR);
392 closedir(DIR);
393 foreach $file (@files) {
394 next if ( $file eq "." );
395 next if ( $file eq ".." );
396 next if ( $file eq "meta-" );
397 next if ( $file =~ /^old/ );
398 open(FILE, "<$Conf::dbdir/meta/$file");
399 @meta = <FILE>;
400 close(FILE);
401 foreach $line (@meta) {
402 @templine = split(/\: /,$line);
403 if ("$templine[0]" eq "Name") {
404 $name = $templine[1];
405 chomp($name);
406 } elsif ("$templine[0]" eq "ProgVersion") {
407 $version = $templine[1];
408 chomp($version);
409 } elsif ("$templine[0]" eq "Release") {
410 $release = $templine[1];
411 chomp($release);
412 }
413 }
414 foreach $prog (@db) {
415 @templine = split(/\;/,$prog);
416 if (("$name" eq "$templine[0]") && ("$release" ne "$templine[2]")) {
417 move("$Conf::dbdir/meta/meta-$name","$Conf::dbdir/meta/old_meta-$name");
418 fetchfile("meta/meta-$name", "");
419 move("$Conf::cachedir/meta-$name", "$Conf::dbdir/meta/meta-$name");
420 }
421 }
422 }
423 }
424
425 sub dblist {
426 ### This subroutine lists the packages.
427 # You may also pass a filter: &Pakfire::dblist(filter)
428 # Usage is always with two arguments.
429 # filter may be: all, notinstalled, installed
430 my $filter = shift;
431 my $forweb = shift;
432 my @meta;
433 my @updatepaks;
434 my $file;
435 my $line;
436 my $prog;
437 my ($name, $version, $release);
438 my @templine;
439
440 ### Make sure that the list is not outdated.
441 #dbgetlist("noforce");
442
443 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
444 my @db = <FILE>;
445 close(FILE);
446
447 if ("$filter" eq "upgrade") {
448 if ("$forweb" ne "forweb" && "$forweb" ne "notice" ) {getcoredb("noforce");}
449 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
450 if ("$core_release" > "$Conf::core_mine") {
451 if ("$forweb" eq "forweb") {
452 print "<option value=\"core\">Core-Update -- $Conf::version -- Release: $Conf::core_mine -> $core_release</option>\n";
453 }
454 elsif ("$forweb" eq "notice") {
455 print "<br /><br /><br /><a href='pakfire.cgi'>$Lang::tr{'core notice 1'} $Conf::core_mine $Lang::tr{'core notice 2'} $core_release $Lang::tr{'core notice 3'}</a>";
456 } else {
457 my $command = "Core-Update $Conf::version\nRelease: $Conf::core_mine -> $core_release\n";
458 if ("$Pakfire::enable_colors" eq "1") {
459 print "$color{'lila'}$command$color{'normal'}\n";
460 } else {
461 print "$command\n";
462 }
463 }
464 }
465
466 opendir(DIR,"$Conf::dbdir/installed");
467 my @files = readdir(DIR);
468 closedir(DIR);
469 foreach $file (@files) {
470 next if ( $file eq "." );
471 next if ( $file eq ".." );
472 next if ( $file =~ /^old/ );
473 open(FILE, "<$Conf::dbdir/installed/$file");
474 @meta = <FILE>;
475 close(FILE);
476 foreach $line (@meta) {
477 @templine = split(/\: /,$line);
478 if ("$templine[0]" eq "Name") {
479 $name = $templine[1];
480 chomp($name);
481 } elsif ("$templine[0]" eq "ProgVersion") {
482 $version = $templine[1];
483 chomp($version);
484 } elsif ("$templine[0]" eq "Release") {
485 $release = $templine[1];
486 chomp($release);
487 }
488 }
489 foreach $prog (@db) {
490 @templine = split(/\;/,$prog);
491 if (("$name" eq "$templine[0]") && ("$release" < "$templine[2]" && "$forweb" ne "notice")) {
492 push(@updatepaks,$name);
493 if ("$forweb" eq "forweb") {
494 print "<option value=\"$name\">Update: $name -- Version: $version -> $templine[1] -- Release: $release -> $templine[2]</option>\n";
495 } else {
496 my $command = "Update: $name\nVersion: $version -> $templine[1]\nRelease: $release -> $templine[2]\n";
497 if ("$Pakfire::enable_colors" eq "1") {
498 print "$color{'lila'}$command$color{'normal'}\n";
499 } else {
500 print "$command\n";
501 }
502 }
503 }
504 }
505 }
506 return @updatepaks;
507 } else {
508 my $line;
509 my $use_color;
510 my @templine;
511 my $count;
512 foreach $line (sort @db) {
513 next unless ($line =~ /.*;.*;.*;/ );
514 $use_color = "";
515 $count++;
516 @templine = split(/\;/,$line);
517 if ("$filter" eq "notinstalled") {
518 next if ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
519 } elsif ("$filter" eq "installed") {
520 next unless ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
521 }
522 if ("$forweb" eq "forweb")
523 {
524 if ("$filter" eq "notinstalled") {
525 print "<option value=\"$templine[0]\">$templine[0]-$templine[1]-$templine[2]</option>\n";
526 } else {
527 print "<option value=\"$templine[0]\">$templine[0]</option>\n";
528 }
529 } else {
530 if ("$Pakfire::enable_colors" eq "1") {
531 if (&isinstalled("$templine[0]")) {
532 $use_color = "$color{'red'}"
533 } else {
534 $use_color = "$color{'green'}"
535 }
536 }
537 print "${use_color}Name: $templine[0]\nProgVersion: $templine[1]\nRelease: $templine[2]$color{'normal'}\n\n";
538 }
539 }
540 print "$count packages total.\n" unless ("$forweb" eq "forweb");
541 }
542 }
543
544 sub resolvedeps_one {
545 my $pak = shift;
546
547 getmetafile("$pak");
548
549 message("PAKFIRE RESV: $pak: Resolving dependencies...");
550
551 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
552 my @file = <FILE>;
553 close(FILE);
554
555 my $line;
556 my (@templine, @deps, @all);
557 foreach $line (@file) {
558 @templine = split(/\: /,$line);
559 if ("$templine[0]" eq "Dependencies") {
560 @deps = split(/ /, $templine[1]);
561 }
562 }
563 chomp (@deps);
564 foreach (@deps) {
565 if ($_) {
566 my $return = &isinstalled($_);
567 if ($return eq 0) {
568 message("PAKFIRE RESV: $pak: Dependency is already installed: $_");
569 } else {
570 message("PAKFIRE RESV: $pak: Need to install dependency: $_");
571 push(@all,$_);
572 }
573 }
574 }
575
576 return @all;
577 }
578
579 sub resolvedeps {
580 my $pak = shift;
581 my @all;
582
583 # Resolve all not yet installed dependencies of $pak
584 my @deps = &resolvedeps_one($pak);
585 push(@all, @deps);
586
587 # For each dependency, we check if more dependencies exist
588 while (@deps) {
589 my $dep = pop(@deps);
590
591 my @subdeps = &resolvedeps_one($dep);
592 foreach my $subdep (@subdeps) {
593 # Skip the package we are currently resolving for
594 next if ($pak eq $subdep);
595
596 # If the package is not already to be installed,
597 # we add it to the list (@all) and check if it has
598 # more dependencies on its own.
599 unless (grep {$_ eq $subdep} @all) {
600 push(@deps, $subdep);
601 push(@all, $subdep);
602 }
603 }
604 }
605
606 return @all;
607 }
608
609 sub resolvedeps_recursive {
610 my @packages = @_;
611 my @result = ();
612
613 foreach my $pkg (@packages) {
614 my @deps = &Pakfire::resolvedeps($pkg);
615
616 foreach my $dep (@deps) {
617 push(@result, $dep);
618 }
619 }
620
621 # Sort the result array and remove dupes
622 my %sort = map{ $_, 1 } @result;
623 @result = keys %sort;
624
625 return @result;
626 }
627
628 sub cleanup {
629 my $dir = shift;
630 my $path;
631
632 logger("CLEANUP: $dir");
633
634 if ( "$dir" eq "meta" ) {
635 $path = "$Conf::dbdir/meta";
636 } elsif ( "$dir" eq "tmp" ) {
637 $path = "$Conf::tmpdir";
638 }
639 chdir("$path");
640 opendir(DIR,".");
641 my @files = readdir(DIR);
642 closedir(DIR);
643 foreach (@files) {
644 unless (($_ eq ".") || ($_ eq "..")) {
645 system("rm -rf $_");
646 }
647 }
648 }
649
650 sub getmetafile {
651 my $pak = shift;
652
653 unless ( -e "$Conf::dbdir/meta/meta-$pak" ) {
654 fetchfile("meta/meta-$pak", "");
655 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
656 }
657
658 if ( -z "$Conf::dbdir/meta/meta-$pak" ) {
659 fetchfile("meta/meta-$pak", "");
660 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
661 }
662
663 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
664 my @line = <FILE>;
665 close(FILE);
666
667 open(FILE, ">$Conf::dbdir/meta/meta-$pak");
668 foreach (@line) {
669 my $string = $_;
670 $string =~ s/\r\n/\n/g;
671 print FILE $string;
672 }
673 close(FILE);
674 return 1;
675 }
676
677 sub getsize {
678 my $pak = shift;
679
680 getmetafile("$pak");
681
682 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
683 my @file = <FILE>;
684 close(FILE);
685
686 my $line;
687 my @templine;
688 foreach $line (@file) {
689 @templine = split(/\: /,$line);
690 if ("$templine[0]" eq "Size") {
691 chomp($templine[1]);
692 return $templine[1];
693 }
694 }
695 return 0;
696 }
697
698 sub decryptpak {
699 my $pak = shift;
700
701 cleanup("tmp");
702
703 my $file = getpak("$pak", "noforce");
704
705 logger("DECRYPT STARTED: $pak");
706 my $return = system("cd $Conf::tmpdir/ && gpg -d --batch --quiet --no-verbose --status-fd 2 --output - < $Conf::cachedir/$file 2>/dev/null | tar x");
707 $return %= 255;
708 logger("DECRYPT FINISHED: $pak - Status: $return");
709 if ($return != 0) { exit 1; }
710 }
711
712 sub getpak {
713 my $pak = shift;
714 my $force = shift;
715
716 getmetafile("$pak");
717
718 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
719 my @file = <FILE>;
720 close(FILE);
721
722 my $line;
723 my $file;
724 my @templine;
725 foreach $line (@file) {
726 @templine = split(/\: /,$line);
727 if ("$templine[0]" eq "File") {
728 chomp($templine[1]);
729 $file = $templine[1];
730 }
731 }
732
733 unless ($file) {
734 message("No filename given in meta-file.");
735 exit 1;
736 }
737
738 unless ( "$force" eq "force" ) {
739 if ( -e "$Conf::cachedir/$file" ) {
740 return $file;
741 }
742 }
743
744 fetchfile("paks/$file", "");
745 return $file;
746 }
747
748 sub setuppak {
749 my $pak = shift;
750
751 message("PAKFIRE INST: $pak: Decrypting...");
752 decryptpak("$pak");
753
754 message("PAKFIRE INST: $pak: Copying files and running post-installation scripts...");
755 my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1");
756 $return %= 255;
757 if ($pakfiresettings{'UUID'} ne "off") {
758 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&ipak=$pak&return=$return", "$Conf::mainserver");
759 }
760 if ($return == 0) {
761 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
762 cleanup("tmp");
763 copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/");
764 message("PAKFIRE INST: $pak: Finished.");
765 message("");
766 } else {
767 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
768 exit $return;
769 }
770 return $return;
771 }
772
773 sub upgradecore {
774 getcoredb("noforce");
775 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
776 if ("$core_release" > "$Conf::core_mine") {
777 message("CORE UPGR: Upgrading from release $Conf::core_mine to $core_release");
778
779 my @seq = `seq $Conf::core_mine $core_release`;
780 shift @seq;
781 my $release;
782 foreach $release (@seq) {
783 chomp($release);
784 getpak("core-upgrade-$release");
785 }
786
787 foreach $release (@seq) {
788 chomp($release);
789 upgradepak("core-upgrade-$release");
790 }
791
792 system("echo $core_release > $Conf::coredir/mine");
793
794 } else {
795 message("CORE ERROR: No new upgrades available. You are on release $Conf::core_mine.");
796 }
797 }
798
799 sub isinstalled {
800 my $pak = shift;
801 if ( open(FILE,"<$Conf::dbdir/installed/meta-$pak") ) {
802 close(FILE);
803 return 0;
804 } else {
805 return 1;
806 }
807 }
808
809 sub upgradepak {
810 my $pak = shift;
811
812 message("PAKFIRE UPGR: $pak: Decrypting...");
813 decryptpak("$pak");
814
815 message("PAKFIRE UPGR: $pak: Upgrading files and running post-upgrading scripts...");
816 my $return = system("cd $Conf::tmpdir && NAME=$pak ./update.sh >> $Conf::logdir/update-$pak.log 2>&1");
817 $return %= 255;
818 if ($pakfiresettings{'UUID'} ne "off") {
819 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&upak=$pak&return=$return", "$Conf::mainserver");
820 }
821 if ($return == 0) {
822 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
823 cleanup("tmp");
824 copy("$Conf::dbdir/meta/meta-$pak", "$Conf::dbdir/installed/");
825 message("PAKFIRE UPGR: $pak: Finished.");
826 message("");
827 } else {
828 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
829 exit $return;
830 }
831 return $return;
832 }
833
834 sub removepak {
835 my $pak = shift;
836
837 message("PAKFIRE REMV: $pak: Decrypting...");
838 decryptpak("$pak");
839
840 message("PAKFIRE REMV: $pak: Removing files and running post-removing scripts...");
841 my $return = system("cd $Conf::tmpdir && NAME=$pak ./uninstall.sh >> $Conf::logdir/uninstall-$pak.log 2>&1");
842 $return %= 255;
843 if ($pakfiresettings{'UUID'} ne "off") {
844 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&dpak=$pak&return=$return", "$Conf::mainserver");
845 }
846 if ($return == 0) {
847 unlink("$Conf::dbdir/rootfiles/$pak");
848 unlink("$Conf::dbdir/installed/meta-$pak");
849 cleanup("tmp");
850 message("PAKFIRE REMV: $pak: Finished.");
851 message("");
852 } else {
853 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
854 exit $return;
855 }
856 return $return;
857 }
858
859 sub beautifysize {
860 my $size = shift;
861 #$size = $size / 1024;
862 my $unit;
863
864 if ($size > 1023*1024) {
865 $size = ($size / (1024*1024));
866 $unit = "MB";
867 } elsif ($size > 1023) {
868 $size = ($size / 1024);
869 $unit = "KB";
870 } else {
871 $unit = "B";
872 }
873 $size = sprintf("%.2f" , $size);
874 my $string = "$size $unit";
875 return $string;
876 }
877
878 sub makeuuid {
879 unless ( -e "$Conf::dbdir/uuid" ) {
880 open(FILE, "</proc/sys/kernel/random/uuid");
881 my @line = <FILE>;
882 close(FILE);
883
884 open(FILE, ">$Conf::dbdir/uuid");
885 foreach (@line) {
886 print FILE $_;
887 }
888 close(FILE);
889 }
890 }
891
892 sub senduuid {
893 if ($pakfiresettings{'UUID'} ne "off") {
894 unless("$Conf::uuid") {
895 $Conf::uuid = `cat $Conf::dbdir/uuid`;
896 }
897 logger("Sending my uuid: $Conf::uuid");
898 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid", "$Conf::mainserver");
899 system("rm -f $Conf::tmpdir/counter* 2>/dev/null");
900 }
901 }
902
903 sub checkcryptodb {
904 logger("CRYPTO INFO: Checking GnuPG Database");
905 my $ret = system("gpg --list-keys | grep -q $myid");
906 unless ( "$ret" eq "0" ) {
907 message("CRYPTO WARN: The GnuPG isn't configured corectly. Trying now to fix this.");
908 message("CRYPTO WARN: It's normal to see this on first execution.");
909 message("CRYPTO WARN: If this message is being shown repeatedly, check if time and date are set correctly, and if IPFire can connect via port 11371 TCP.");
910 my $command = "gpg --keyserver pgp.ipfire.org --always-trust --status-fd 2";
911 system("$command --recv-key $myid >> $Conf::logdir/gnupg-database.log 2>&1");
912 system("$command --recv-key $trustid >> $Conf::logdir/gnupg-database.log 2>&1");
913 } else {
914 logger("CRYPTO INFO: Database is okay");
915 }
916 }
917
918 sub callback {
919 my ($data, $response, $protocol) = @_;
920 $final_data .= $data;
921 print progress_bar( length($final_data), $total_size, 30, '=' );
922 }
923
924 sub progress_bar {
925 my ( $got, $total, $width, $char ) = @_;
926 my $show_bfile;
927 $width ||= 30; $char ||= '=';
928 my $len_bfile = length $bfile;
929 if ("$len_bfile" >= "17") {
930 $show_bfile = substr($bfile,0,17)."...";
931 } else {
932 $show_bfile = $bfile;
933 }
934 $progress = sprintf("%.2f%%", 100*$got/+$total);
935 sprintf "$color{'lightgreen'}%-20s %7s |%-${width}s| %10s$color{'normal'}\r",$show_bfile, $progress, $char x (($width-1)*$got/$total). '>', beautifysize($got);
936 }
937
938 1;