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