]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/pakfire/lib/functions.pl
Fix update.sh of some packages
[people/pmueller/ipfire-2.x.git] / src / pakfire / lib / functions.pl
CommitLineData
1bd42c89 1#!/usr/bin/perl -w
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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###############################################################################
1bd42c89
MT
21
22require "/opt/pakfire/etc/pakfire.conf";
4b122800 23require "/var/ipfire/general-functions.pl";
1bd42c89
MT
24
25use File::Basename;
26use File::Copy;
27use LWP::UserAgent;
4d504812 28use HTTP::Response;
a6d327a7
MT
29use HTTP::Headers;
30use HTTP::Message;
31use HTTP::Request;
1bd42c89
MT
32use Net::Ping;
33
34package Pakfire;
35
750c1528
MT
36# GPG Keys
37my $myid = "64D96617"; # Our own gpg-key paks@ipfire.org
38my $trustid = "65D0FD58"; # gpg-key of CaCert
39
35f38a8b
MT
40# A small color-hash :D
41my %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";
750c1528 59our $enable_colors = 1;
35f38a8b 60
a6d327a7
MT
61my $final_data;
62my $total_size;
63my $bfile;
64
4b122800
MT
65my %pakfiresettings = ();
66&General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
67
1bd42c89
MT
68sub message {
69 my $message = shift;
35f38a8b 70
1bd42c89 71 logger("$message");
750c1528
MT
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 }
35f38a8b
MT
88 }
89 print "$message\n";
90
1bd42c89
MT
91}
92
93sub logger {
94 my $log = shift;
9ced24a8 95 if ($log) {
c506cad0
CS
96 #system("echo \"`date`: $log\" >> /var/log/pakfire.log");
97 system("logger -t pakfire \"$log\"");
9ced24a8 98 }
1bd42c89
MT
99}
100
5b2a12ff 101sub usage {
750c1528 102 &Pakfire::message("Usage: pakfire <install|remove> [options] <pak(s)>");
5b2a12ff 103 &Pakfire::message(" <update> - Contacts the servers for new lists of paks.");
99e6df8e 104 &Pakfire::message(" <upgrade> - Installs the latest version of all paks.");
5b2a12ff 105 &Pakfire::message(" <list> - Outputs a short list with all available paks.");
99e6df8e 106 &Pakfire::message("");
750c1528
MT
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("");
5b2a12ff
MT
113 exit 1;
114}
115
1bd42c89
MT
116sub pinghost {
117 my $host = shift;
118
119 $p = Net::Ping->new();
120 if ($p->ping($host)) {
28593a79 121 logger("PING INFO: $host is alive");
1bd42c89
MT
122 return 1;
123 } else {
a6d327a7 124 logger("PING INFO: $host is unreachable");
1bd42c89
MT
125 return 0;
126 }
127 $p->close();
128}
129
130sub fetchfile {
4d504812
MT
131 my $getfile = shift;
132 my $gethost = shift;
377560fb
MT
133 my (@server, $host, $proto, $file, $i);
134 my $allok = 0;
1bd42c89
MT
135
136 use File::Basename;
4d504812 137 $bfile = basename("$getfile");
377560fb
MT
138
139 logger("DOWNLOAD STARTED: $getfile") unless ($bfile =~ /^counter\?.*/);
1bd42c89 140
4d504812
MT
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";
1bd42c89 150 } else {
4d504812 151 $host = $gethost;
afabe9f7 152 $file = $getfile;
1bd42c89 153 }
4d504812
MT
154
155 $proto = "HTTP" unless $proto;
156
a6d327a7
MT
157 unless ($bfile =~ /^counter\?.*/) {
158 logger("DOWNLOAD INFO: Host: $host ($proto) - File: $file");
a6d327a7 159 }
1bd42c89 160
4d504812
MT
161 my $ua = LWP::UserAgent->new;
162 $ua->agent("Pakfire/$Conf::version");
3d3b68c5 163 $ua->timeout(20);
4b122800
MT
164
165 my %proxysettings=();
166 &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings);
167
99e6df8e 168 if ($proxysettings{'UPSTREAM_PROXY'}) {
4c7fa778 169 logger("DOWNLOAD INFO: Upstream proxy: \"$proxysettings{'UPSTREAM_PROXY'}\"") unless ($bfile =~ /^counter.py\?.*/);
4b122800 170 if ($proxysettings{'UPSTREAM_USER'}) {
99e6df8e 171 $ua->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$proxysettings{'UPSTREAM_PROXY'}/");
4c7fa778 172 logger("DOWNLOAD INFO: Logging in with: \"$proxysettings{'UPSTREAM_USER'}\" - \"$proxysettings{'UPSTREAM_PASSWORD'}\"") unless ($bfile =~ /^counter.py\?.*/);
4b122800 173 } else {
99e6df8e 174 $ua->proxy("http","http://$proxysettings{'UPSTREAM_PROXY'}/");
4b122800
MT
175 }
176 }
a6d327a7
MT
177
178 $final_data = undef;
179 my $url = "http://$host/$file";
180 my $response;
181
4c7fa778 182 unless ($bfile =~ /^counter.py\?.*/) {
a6d327a7
MT
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 }
4d504812 193
4b122800
MT
194 my $code = $response->code();
195 my $log = $response->status_line;
a6d327a7 196 logger("DOWNLOAD INFO: HTTP-Status-Code: $code - $log");
4b122800
MT
197
198 if ( $code eq "500" ) {
e44b26cf 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.");
4b122800
MT
200 return 1;
201 }
202
4d504812 203 if ($response->is_success) {
4c7fa778 204 unless ($bfile =~ /^counter.py\?.*/) {
35f38a8b
MT
205 if (open(FILE, ">$Conf::tmpdir/$bfile")) {
206 print FILE $final_data;
207 close(FILE);
a6d327a7 208 logger("DOWNLOAD INFO: File received. Start checking signature...");
06209efc 209 if (system("gpg --verify \"$Conf::tmpdir/$bfile\" &>/dev/null") eq 0) {
a6d327a7 210 logger("DOWNLOAD INFO: Signature of $bfile is fine.");
06209efc
MT
211 move("$Conf::tmpdir/$bfile","$Conf::cachedir/$bfile");
212 } else {
a6d327a7 213 message("DOWNLOAD ERROR: The downloaded file ($file) wasn't verified by IPFire.org. Sorry - Exiting...");
28593a79
CS
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.");}
06209efc
MT
217 exit 1;
218 }
35f38a8b
MT
219 logger("DOWNLOAD FINISHED: $file");
220 $allok = 1;
221 return 0;
222 } else {
223 logger("DOWNLOAD ERROR: Could not open $Conf::cachedir/$bfile for writing.");
186e3d2c 224 }
4d504812 225 } else {
35f38a8b 226 return 0;
4d504812
MT
227 }
228 } else {
a6d327a7 229 logger("DOWNLOAD ERROR: $log");
4d504812 230 }
1bd42c89 231 }
a6d327a7 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.");
4d504812 233 return 1;
1bd42c89
MT
234}
235
236sub getmirrors {
377560fb
MT
237 my $force = shift;
238 my $age;
239
1bd42c89 240 use File::Copy;
1bd42c89 241
2aa6d448
MT
242 if ( -e "$Conf::dbdir/lists/server-list.db" ) {
243 my @stat = stat("$Conf::dbdir/lists/server-list.db");
e3670217
MT
244 my $time = time();
245 $age = $time - $stat[9];
377560fb
MT
246 $force = "force" if ("$age" >= "3600");
247 logger("MIRROR INFO: server-list.db is $age seconds old. - DEBUG: $force");
e3670217
MT
248 } else {
249 # Force an update.
377560fb 250 $force = "force";
e3670217
MT
251 }
252
377560fb 253 if ("$force" eq "force") {
e3670217
MT
254 fetchfile("$Conf::version/lists/server-list.db", "$Conf::mainserver");
255 move("$Conf::cachedir/server-list.db", "$Conf::dbdir/lists/server-list.db");
256 }
1bd42c89
MT
257}
258
2aa6d448 259sub getcoredb {
377560fb
MT
260 my $force = shift;
261 my $age;
262
2aa6d448 263 use File::Copy;
2aa6d448
MT
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];
377560fb
MT
269 $force = "force" if ("$age" >= "3600");
270 logger("CORE INFO: core-list.db is $age seconds old. - DEBUG: $force");
2aa6d448
MT
271 } else {
272 # Force an update.
377560fb 273 $force = "force";
2aa6d448
MT
274 }
275
377560fb 276 if ("$force" eq "force") {
2aa6d448
MT
277 fetchfile("lists/core-list.db", "");
278 move("$Conf::cachedir/core-list.db", "$Conf::dbdir/lists/core-list.db");
279 }
280}
281
282
1bd42c89
MT
283sub 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;
3d3b68c5 287 while (!(open(FILE, "<$Conf::dbdir/lists/server-list.db")) && ($count lt 5)) {
1bd42c89 288 $count++;
377560fb 289 getmirrors("noforce");
1bd42c89 290 }
3d3b68c5 291 if ($count == 5) {
a6d327a7 292 message("MIRROR ERROR: Could not find or download a server list");
1bd42c89
MT
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;
e44b26cf 300 my @newlines;
1bd42c89 301 foreach (@lines) {
e44b26cf
MT
302 if ("$_" =~ /.*;.*;.*;/ ) {
303 push(@newlines,$_);
304 $scount++;
305 }
1bd42c89 306 }
a6d327a7 307 logger("MIRROR INFO: $scount servers found in list");
1bd42c89
MT
308
309 ### Choose a random server and test if it is online
310 # If the check fails try a new server.
311 # This will never give up.
312 my $found = 0;
313 my $servers = 0;
314 while ($found == 0) {
315 $server = int(rand($scount) + 1);
316 $servers = 0;
317 my ($line, $proto, $path, $host);
318 my @templine;
e44b26cf 319 foreach $line (@newlines) {
1bd42c89
MT
320 $servers++;
321 if ($servers eq $server) {
322 @templine = split(/\;/, $line);
323 $proto = $templine[0];
324 $host = $templine[1];
325 $path = $templine[2];
a08c3a2e 326 if (pinghost("$host")) {
1bd42c89
MT
327 $found = 1;
328 return ($proto, $host, $path);
329 }
330 }
331 }
4d504812 332 }
1bd42c89
MT
333}
334
335sub dbgetlist {
336 ### Update the database if the file is older than one day.
337 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
338 # Usage is always with an argument.
339 my $force = shift;
340 my $age;
341
342 use File::Copy;
343
344 if ( -e "$Conf::dbdir/lists/packages_list.db" ) {
345 my @stat = stat("$Conf::dbdir/lists/packages_list.db");
346 my $time = time();
347 $age = $time - $stat[9];
377560fb
MT
348 $force = "force" if ("$age" >= "3600");
349 logger("DB INFO: packages_list.db is $age seconds old. - DEBUG: $force");
1bd42c89
MT
350 } else {
351 # Force an update.
377560fb 352 $force = "force";
1bd42c89
MT
353 }
354
377560fb 355 if ("$force" eq "force") {
1bd42c89
MT
356 fetchfile("lists/packages_list.db", "");
357 move("$Conf::cachedir/packages_list.db", "$Conf::dbdir/lists/packages_list.db");
358 }
359}
360
361sub dblist {
362 ### This subroutine lists the packages.
363 # You may also pass a filter: &Pakfire::dblist(filter)
364 # Usage is always with two arguments.
365 # filter may be: all, notinstalled, installed
366 my $filter = shift;
367 my $forweb = shift;
4b122800 368 my @meta;
99e6df8e 369 my @updatepaks;
4b122800
MT
370 my $file;
371 my $line;
372 my $prog;
373 my ($name, $version, $release);
374 my @templine;
1bd42c89
MT
375
376 ### Make sure that the list is not outdated.
377560fb 377 #dbgetlist("noforce");
1bd42c89
MT
378
379 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
380 my @db = <FILE>;
381 close(FILE);
4b122800
MT
382
383 if ("$filter" eq "upgrade") {
d1d1cb42 384 if ("$forweb" ne "forweb") {getcoredb("noforce");}
8a8a50ad 385 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
518f894b 386 if ("$core_release" > "$Conf::core_mine") {
8a8a50ad
MT
387 if ("$forweb" eq "forweb") {
388 print "<option value=\"core\">Core-Update -- $Conf::version -- Release: $Conf::core_mine -> $core_release</option>\n";
389 } else {
390 my $command = "Core-Update $Conf::version\nRelease: $Conf::core_mine -> $core_release\n";
391 if ("$Pakfire::enable_colors" eq "1") {
392 print "$color{'lila'}$command$color{'normal'}\n";
393 } else {
394 print "$command\n";
395 }
396 }
397 }
398
4b122800
MT
399 opendir(DIR,"$Conf::dbdir/meta");
400 my @files = readdir(DIR);
401 closedir(DIR);
402 foreach $file (@files) {
403 next if ( $file eq "." );
404 next if ( $file eq ".." );
405 open(FILE, "<$Conf::dbdir/meta/$file");
406 @meta = <FILE>;
407 close(FILE);
408 foreach $line (@meta) {
409 @templine = split(/\: /,$line);
410 if ("$templine[0]" eq "Name") {
411 $name = $templine[1];
412 chomp($name);
413 } elsif ("$templine[0]" eq "ProgVersion") {
414 $version = $templine[1];
415 chomp($version);
416 } elsif ("$templine[0]" eq "Release") {
417 $release = $templine[1];
418 chomp($release);
419 }
420 }
421 foreach $prog (@db) {
422 @templine = split(/\;/,$prog);
423 if (("$name" eq "$templine[0]") && ("$release" < "$templine[2]" )) {
99e6df8e 424 push(@updatepaks,$name);
4b122800
MT
425 if ("$forweb" eq "forweb") {
426 print "<option value=\"$name\">Update: $name -- Version: $version -> $templine[1] -- Release: $release -> $templine[2]</option>\n";
427 } else {
e3670217
MT
428 my $command = "Update: $name\nVersion: $version -> $templine[1]\nRelease: $release -> $templine[2]\n";
429 if ("$Pakfire::enable_colors" eq "1") {
430 print "$color{'lila'}$command$color{'normal'}\n";
431 } else {
432 print "$command\n";
433 }
4b122800
MT
434 }
435 }
436 }
5b2a12ff 437 }
99e6df8e 438 return @updatepaks;
4b122800
MT
439 } else {
440 my $line;
e3670217 441 my $use_color;
4b122800 442 my @templine;
e3670217 443 my $count;
4b122800 444 foreach $line (sort @db) {
06209efc 445 next unless ($line =~ /.*;.*;.*;/ );
e3670217
MT
446 $use_color = "";
447 $count++;
4b122800
MT
448 @templine = split(/\;/,$line);
449 if ("$filter" eq "notinstalled") {
450 next if ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
451 } elsif ("$filter" eq "installed") {
452 next unless ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
453 }
454 if ("$forweb" eq "forweb") {
455 print "<option value=\"$templine[0]\">$templine[0]-$templine[1]-$templine[2]</option>\n";
456 } else {
e3670217
MT
457 if ("$Pakfire::enable_colors" eq "1") {
458 if (&isinstalled("$templine[0]")) {
459 $use_color = "$color{'red'}"
460 } else {
461 $use_color = "$color{'green'}"
462 }
463 }
464 print "${use_color}Name: $templine[0]\nProgVersion: $templine[1]\nRelease: $templine[2]$color{'normal'}\n\n";
4b122800 465 }
1bd42c89 466 }
e3670217 467 print "$count packages total.\n" unless ("$forweb" eq "forweb");
1bd42c89
MT
468 }
469}
470
471sub resolvedeps {
472 my $pak = shift;
473
474 getmetafile("$pak");
475
35f38a8b 476 message("PAKFIRE RESV: $pak: Resolving dependencies...");
1bd42c89
MT
477
478 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
479 my @file = <FILE>;
480 close(FILE);
481
482 my $line;
186e3d2c 483 my (@templine, @deps, @tempdeps, @all);
1bd42c89
MT
484 foreach $line (@file) {
485 @templine = split(/\: /,$line);
486 if ("$templine[0]" eq "Dependencies") {
487 @deps = split(/ /, $templine[1]);
488 }
489 }
490 chomp (@deps);
491 foreach (@deps) {
492 if ($_) {
186e3d2c
MT
493 my $return = &isinstalled($_);
494 if ($return eq 0) {
35f38a8b 495 message("PAKFIRE RESV: $pak: Dependency is already installed: $_");
186e3d2c 496 } else {
35f38a8b 497 message("PAKFIRE RESV: $pak: Need to install dependency: $_");
186e3d2c
MT
498 push(@tempdeps,$_);
499 push(@all,$_);
500 }
1bd42c89
MT
501 }
502 }
e44b26cf 503
1bd42c89
MT
504 foreach (@tempdeps) {
505 if ($_) {
506 my @newdeps = resolvedeps("$_");
507 foreach(@newdeps) {
508 unless (($_ eq " ") || ($_ eq "")) {
186e3d2c
MT
509 my $return = &isinstalled($_);
510 if ($return eq 0) {
35f38a8b 511 message("PAKFIRE RESV: $pak: Dependency is already installed: $_");
186e3d2c 512 } else {
35f38a8b 513 message("PAKFIRE RESV: $pak: Need to install dependency: $_");
186e3d2c
MT
514 push(@all,$_);
515 }
1bd42c89
MT
516 }
517 }
518 }
519 }
35f38a8b 520 message("");
186e3d2c
MT
521 chomp (@all);
522 return @all;
1bd42c89
MT
523}
524
525sub cleanup {
526 my $dir = shift;
527 my $path;
528
35f38a8b
MT
529 logger("CLEANUP: $dir");
530
1bd42c89
MT
531 if ( "$dir" eq "meta" ) {
532 $path = "$Conf::dbdir/meta";
533 } elsif ( "$dir" eq "tmp" ) {
534 $path = "$Conf::tmpdir";
535 }
536 chdir("$path");
537 opendir(DIR,".");
538 my @files = readdir(DIR);
539 closedir(DIR);
540 foreach (@files) {
541 unless (($_ eq ".") || ($_ eq "..")) {
542 system("rm -rf $_");
543 }
544 }
545}
546
547sub getmetafile {
548 my $pak = shift;
549
28593a79
CS
550 unless ( -e "$Conf::dbdir/meta/meta-$pak" ) {
551 fetchfile("meta/meta-$pak", "");
552 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
553 }
554
555 if ( -z "$Conf::dbdir/meta/meta-$pak" ) {
1bd42c89
MT
556 fetchfile("meta/meta-$pak", "");
557 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
558 }
559
560 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
561 my @line = <FILE>;
562 close(FILE);
563
564 open(FILE, ">$Conf::dbdir/meta/meta-$pak");
565 foreach (@line) {
566 my $string = $_;
567 $string =~ s/\r\n/\n/g;
568 print FILE $string;
569 }
570 close(FILE);
571 return 1;
572}
573
574sub getsize {
575 my $pak = shift;
576
577 getmetafile("$pak");
578
579 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
580 my @file = <FILE>;
581 close(FILE);
582
583 my $line;
584 my @templine;
585 foreach $line (@file) {
586 @templine = split(/\: /,$line);
587 if ("$templine[0]" eq "Size") {
588 chomp($templine[1]);
589 return $templine[1];
590 }
591 }
4b122800 592 return 0;
1bd42c89
MT
593}
594
595sub decryptpak {
596 my $pak = shift;
597
598 cleanup("tmp");
599
600 my $file = getpak("$pak", "noforce");
601
a6d327a7 602 logger("DECRYPT STARTED: $pak");
35f38a8b 603 my $return = system("cd $Conf::tmpdir/ && gpg -d --batch --quiet --no-verbose --status-fd 2 --output - < $Conf::cachedir/$file 2>/dev/null | tar x");
99e6df8e 604 $return %= 255;
a6d327a7 605 logger("DECRYPT FINISHED: $pak - Status: $return");
cde0e116 606 if ($return != 0) { exit 1; }
1bd42c89
MT
607}
608
609sub getpak {
610 my $pak = shift;
611 my $force = shift;
612
613 getmetafile("$pak");
614
615 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
616 my @file = <FILE>;
617 close(FILE);
618
619 my $line;
620 my $file;
621 my @templine;
622 foreach $line (@file) {
623 @templine = split(/\: /,$line);
624 if ("$templine[0]" eq "File") {
625 chomp($templine[1]);
626 $file = $templine[1];
627 }
628 }
629
630 unless ($file) {
631 message("No filename given in meta-file. Please phone the developers.");
632 exit 1;
633 }
634
1bd42c89
MT
635 unless ( "$force" eq "force" ) {
636 if ( -e "$Conf::cachedir/$file" ) {
1bd42c89
MT
637 return $file;
638 }
639 }
640
641 fetchfile("paks/$file", "");
642 return $file;
643}
644
645sub setuppak {
646 my $pak = shift;
647
35f38a8b 648 message("PAKFIRE INST: $pak: Decrypting...");
1bd42c89
MT
649 decryptpak("$pak");
650
35f38a8b 651 message("PAKFIRE INST: $pak: Copying files and running post-installation scripts...");
99e6df8e 652 my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1");
cde0e116 653 $return %= 255;
e44b26cf 654 if ($pakfiresettings{'UUID'} ne "off") {
4c7fa778 655 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&ipak=$pak&return=$return", "$Conf::mainserver");
e44b26cf 656 }
1bd42c89
MT
657 if ($return == 0) {
658 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
659 cleanup("tmp");
4d504812 660 copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/");
35f38a8b
MT
661 message("PAKFIRE INST: $pak: Finished.");
662 message("");
1bd42c89 663 } else {
35f38a8b 664 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
665 exit $return;
666 }
a08c3a2e 667 return $return;
1bd42c89
MT
668}
669
2aa6d448 670sub upgradecore {
377560fb 671 getcoredb("noforce");
2aa6d448 672 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
518f894b 673 if ("$core_release" > "$Conf::core_mine") {
2aa6d448
MT
674 message("CORE UPGR: Upgrading from release $Conf::core_mine to $core_release");
675
676 my @seq = `seq $Conf::core_mine $core_release`;
677 shift @seq;
678 my $release;
679 foreach $release (@seq) {
680 chomp($release);
681 getpak("core-upgrade-$release");
682 }
683
684 foreach $release (@seq) {
685 chomp($release);
686 upgradepak("core-upgrade-$release");
687 }
688
689 system("echo $core_release > $Conf::coredir/mine");
690
691 } else {
692 message("CORE ERROR: No new upgrades available. You are on release $Conf::core_mine.");
693 }
694}
695
186e3d2c
MT
696sub isinstalled {
697 my $pak = shift;
698 if ( open(FILE,"<$Conf::dbdir/installed/meta-$pak") ) {
699 close(FILE);
700 return 0;
701 } else {
702 return 1;
703 }
704}
705
99e6df8e 706sub upgradepak {
1bd42c89
MT
707 my $pak = shift;
708
35f38a8b 709 message("PAKFIRE UPGR: $pak: Decrypting...");
1bd42c89
MT
710 decryptpak("$pak");
711
35f38a8b 712 message("PAKFIRE UPGR: $pak: Upgrading files and running post-upgrading scripts...");
99e6df8e
MT
713 my $return = system("cd $Conf::tmpdir && NAME=$pak ./update.sh >> $Conf::logdir/update-$pak.log 2>&1");
714 $return %= 255;
e44b26cf 715 if ($pakfiresettings{'UUID'} ne "off") {
4c7fa778 716 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&upak=$pak&return=$return", "$Conf::mainserver");
e44b26cf 717 }
1bd42c89
MT
718 if ($return == 0) {
719 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
720 cleanup("tmp");
4e4b54c5 721 copy("$Conf::dbdir/meta/meta-$pak", "$Conf::dbdir/installed/");
35f38a8b
MT
722 message("PAKFIRE UPGR: $pak: Finished.");
723 message("");
1bd42c89 724 } else {
35f38a8b 725 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
726 exit $return;
727 }
8e58bd37 728 return $return;
1bd42c89
MT
729}
730
731sub removepak {
732 my $pak = shift;
733
35f38a8b 734 message("PAKFIRE REMV: $pak: Decrypting...");
1bd42c89
MT
735 decryptpak("$pak");
736
35f38a8b 737 message("PAKFIRE REMV: $pak: Removing files and running post-removing scripts...");
99e6df8e
MT
738 my $return = system("cd $Conf::tmpdir && NAME=$pak ./uninstall.sh >> $Conf::logdir/uninstall-$pak.log 2>&1");
739 $return %= 255;
e44b26cf 740 if ($pakfiresettings{'UUID'} ne "off") {
4c7fa778 741 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&dpak=$pak&return=$return", "$Conf::mainserver");
e44b26cf 742 }
1bd42c89 743 if ($return == 0) {
1bd42c89 744 unlink("$Conf::dbdir/rootfiles/$pak");
a6d327a7 745 unlink("$Conf::dbdir/installed/meta-$pak");
1bd42c89 746 cleanup("tmp");
35f38a8b
MT
747 message("PAKFIRE REMV: $pak: Finished.");
748 message("");
1bd42c89 749 } else {
35f38a8b 750 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
751 exit $return;
752 }
8e58bd37 753 return $return;
1bd42c89
MT
754}
755
756sub beautifysize {
757 my $size = shift;
35f38a8b 758 #$size = $size / 1024;
a08c3a2e 759 my $unit;
1bd42c89 760
35f38a8b
MT
761 if ($size > 1023*1024) {
762 $size = ($size / (1024*1024));
a08c3a2e 763 $unit = "MB";
35f38a8b
MT
764 } elsif ($size > 1023) {
765 $size = ($size / 1024);
a08c3a2e 766 $unit = "KB";
35f38a8b
MT
767 } else {
768 $unit = "B";
1bd42c89 769 }
a08c3a2e
MT
770 $size = sprintf("%.2f" , $size);
771 my $string = "$size $unit";
772 return $string;
1bd42c89
MT
773}
774
8e58bd37
MT
775sub makeuuid {
776 unless ( -e "$Conf::dbdir/uuid" ) {
8e58bd37
MT
777 open(FILE, "</proc/sys/kernel/random/uuid");
778 my @line = <FILE>;
779 close(FILE);
780
781 open(FILE, ">$Conf::dbdir/uuid");
782 foreach (@line) {
783 print FILE $_;
784 }
785 close(FILE);
786 }
787}
788
789sub senduuid {
99e6df8e 790 if ($pakfiresettings{'UUID'} ne "off") {
4b122800
MT
791 unless("$Conf::uuid") {
792 $Conf::uuid = `cat $Conf::dbdir/uuid`;
793 }
794 logger("Sending my uuid: $Conf::uuid");
4c7fa778 795 fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid", "$Conf::mainserver");
06209efc 796 system("rm -f $Conf::tmpdir/counter* 2>/dev/null");
8e58bd37 797 }
8e58bd37 798}
1bd42c89 799
cde0e116 800sub checkcryptodb {
a6d327a7 801 logger("CRYPTO INFO: Checking GnuPG Database");
cde0e116
MT
802 my $ret = system("gpg --list-keys | grep -q $myid");
803 unless ( "$ret" eq "0" ) {
a6d327a7
MT
804 message("CRYPTO WARN: The GnuPG isn't configured corectly. Trying now to fix this.");
805 message("CRYPTO WARN: It's normal to see this on first execution.");
a4c9c660 806 my $command = "gpg --keyserver pgp.mit.edu --always-trust --status-fd 2";
ca385da6
MT
807 system("$command --recv-key $myid >> $Conf::logdir/gnupg-database.log 2>&1");
808 system("$command --recv-key $trustid >> $Conf::logdir/gnupg-database.log 2>&1");
a6d327a7
MT
809 } else {
810 logger("CRYPTO INFO: Database is okay");
cde0e116
MT
811 }
812}
813
a6d327a7
MT
814sub callback {
815 my ($data, $response, $protocol) = @_;
816 $final_data .= $data;
35f38a8b 817 print progress_bar( length($final_data), $total_size, 30, '=' );
a6d327a7
MT
818}
819
820sub progress_bar {
821 my ( $got, $total, $width, $char ) = @_;
822 my $show_bfile;
35f38a8b 823 $width ||= 30; $char ||= '=';
a6d327a7 824 my $len_bfile = length $bfile;
35f38a8b
MT
825 if ("$len_bfile" >= "17") {
826 $show_bfile = substr($bfile,0,17)."...";
a6d327a7
MT
827 } else {
828 $show_bfile = $bfile;
35f38a8b
MT
829 }
830 $progress = sprintf("%.2f%%", 100*$got/+$total);
831 sprintf "$color{'lightgreen'}%-20s %7s |%-${width}s| %10s$color{'normal'}\r",$show_bfile, $progress, $char x (($width-1)*$got/$total). '>', beautifysize($got);
a6d327a7
MT
832}
833
1bd42c89 8341;