Einige Root-Files noch erweitert/gekuerzt.
[people/pmueller/ipfire-2.x.git] / src / pakfire / lib / functions.pl
CommitLineData
1bd42c89
MT
1#!/usr/bin/perl -w
2
3require "/opt/pakfire/etc/pakfire.conf";
4b122800 4require "/var/ipfire/general-functions.pl";
1bd42c89
MT
5
6use File::Basename;
7use File::Copy;
8use LWP::UserAgent;
4d504812 9use HTTP::Response;
a6d327a7
MT
10use HTTP::Headers;
11use HTTP::Message;
12use HTTP::Request;
1bd42c89
MT
13use Net::Ping;
14
15package Pakfire;
16
750c1528
MT
17# GPG Keys
18my $myid = "64D96617"; # Our own gpg-key paks@ipfire.org
19my $trustid = "65D0FD58"; # gpg-key of CaCert
20
35f38a8b
MT
21# A small color-hash :D
22my %color;
23 $color{'normal'} = "\033[0m";
24 $color{'black'} = "\033[0;30m";
25 $color{'darkgrey'} = "\033[1;30m";
26 $color{'blue'} = "\033[0;34m";
27 $color{'lightblue'} = "\033[1;34m";
28 $color{'green'} = "\033[0;32m";
29 $color{'lightgreen'} = "\033[1;32m";
30 $color{'cyan'} = "\033[0;36m";
31 $color{'lightcyan'} = "\033[1;36m";
32 $color{'red'} = "\033[0;31m";
33 $color{'lightred'} = "\033[1;31m";
34 $color{'purple'} = "\033[0;35m";
35 $color{'lightpurple'} = "\033[1;35m";
36 $color{'brown'} = "\033[0;33m";
37 $color{'lightgrey'} = "\033[0;37m";
38 $color{'yellow'} = "\033[1;33m";
39 $color{'white'} = "\033[1;37m";
750c1528 40our $enable_colors = 1;
35f38a8b 41
a6d327a7
MT
42my $final_data;
43my $total_size;
44my $bfile;
45
4b122800
MT
46my %pakfiresettings = ();
47&General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
48
1bd42c89
MT
49sub message {
50 my $message = shift;
35f38a8b 51
1bd42c89 52 logger("$message");
750c1528
MT
53 if ( $enable_colors == 1 ) {
54 if ("$message" =~ /ERROR/) {
55 $message = "$color{'red'}$message$color{'normal'}";
56 } elsif ("$message" =~ /INFO/) {
57 $message = "$color{'cyan'}$message$color{'normal'}";
58 } elsif ("$message" =~ /WARN/) {
59 $message = "$color{'yellow'}$message$color{'normal'}";
60 } elsif ("$message" =~ /RESV/) {
61 $message = "$color{'purple'}$message$color{'normal'}";
62 } elsif ("$message" =~ /INST/) {
63 $message = "$color{'green'}$message$color{'normal'}";
64 } elsif ("$message" =~ /REMV/) {
65 $message = "$color{'lightred'}$message$color{'normal'}";
66 } elsif ("$message" =~ /UPGR/) {
67 $message = "$color{'lightblue'}$message$color{'normal'}";
68 }
35f38a8b
MT
69 }
70 print "$message\n";
71
1bd42c89
MT
72}
73
74sub logger {
75 my $log = shift;
9ced24a8
MT
76 if ($log) {
77 system("echo \"`date`: $log\" >> /var/log/pakfire.log");
78 #system("logger -t pakfire \"$log\"");
79 }
1bd42c89
MT
80}
81
5b2a12ff 82sub usage {
750c1528 83 &Pakfire::message("Usage: pakfire <install|remove> [options] <pak(s)>");
5b2a12ff 84 &Pakfire::message(" <update> - Contacts the servers for new lists of paks.");
99e6df8e 85 &Pakfire::message(" <upgrade> - Installs the latest version of all paks.");
5b2a12ff 86 &Pakfire::message(" <list> - Outputs a short list with all available paks.");
99e6df8e 87 &Pakfire::message("");
750c1528
MT
88 &Pakfire::message(" Global options:");
89 &Pakfire::message(" --non-interactive --> Enables the non-interactive mode.");
90 &Pakfire::message(" You won't see any question here.");
91 &Pakfire::message(" -y --> Short for --non-interactive.");
92 &Pakfire::message(" --no-colors --> Turns off the wonderful colors.");
93 &Pakfire::message("");
5b2a12ff
MT
94 exit 1;
95}
96
1bd42c89
MT
97sub pinghost {
98 my $host = shift;
99
100 $p = Net::Ping->new();
101 if ($p->ping($host)) {
a6d327a7 102 logger("PING INFO: $host is alive");
1bd42c89
MT
103 return 1;
104 } else {
a6d327a7 105 logger("PING INFO: $host is unreachable");
1bd42c89
MT
106 return 0;
107 }
108 $p->close();
109}
110
111sub fetchfile {
4d504812
MT
112 my $getfile = shift;
113 my $gethost = shift;
114 my (@server, $host, $proto, $file, $allok, $i);
1bd42c89 115
a6d327a7 116 logger("DOWNLOAD STARTED: $getfile") unless ($bfile =~ /^counter\?.*/);
1bd42c89 117 use File::Basename;
4d504812 118 $bfile = basename("$getfile");
1bd42c89 119
4d504812
MT
120 $i = 0;
121 while (($allok == 0) && $i < 5) {
122 $i++;
123
124 if ("$gethost" eq "") {
125 @server = selectmirror();
126 $proto = $server[0];
127 $host = $server[1];
128 $file = "$server[2]/$getfile";
1bd42c89 129 } else {
4d504812 130 $host = $gethost;
afabe9f7 131 $file = $getfile;
1bd42c89 132 }
4d504812
MT
133
134 $proto = "HTTP" unless $proto;
135
a6d327a7
MT
136 unless ($bfile =~ /^counter\?.*/) {
137 logger("DOWNLOAD INFO: Host: $host ($proto) - File: $file");
a6d327a7 138 }
1bd42c89 139
4d504812
MT
140 my $ua = LWP::UserAgent->new;
141 $ua->agent("Pakfire/$Conf::version");
afabe9f7 142 $ua->timeout(5);
4b122800
MT
143
144 my %proxysettings=();
145 &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings);
146
99e6df8e 147 if ($proxysettings{'UPSTREAM_PROXY'}) {
a6d327a7 148 logger("DOWNLOAD INFO: Upstream proxy: \"$proxysettings{'UPSTREAM_PROXY'}\"") unless ($bfile =~ /^counter\?.*/);
4b122800 149 if ($proxysettings{'UPSTREAM_USER'}) {
99e6df8e 150 $ua->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$proxysettings{'UPSTREAM_PROXY'}/");
a6d327a7 151 logger("DOWNLOAD INFO: Logging in with: \"$proxysettings{'UPSTREAM_USER'}\" - \"$proxysettings{'UPSTREAM_PASSWORD'}\"") unless ($bfile =~ /^counter\?.*/);
4b122800 152 } else {
99e6df8e 153 $ua->proxy("http","http://$proxysettings{'UPSTREAM_PROXY'}/");
4b122800
MT
154 }
155 }
a6d327a7
MT
156
157 $final_data = undef;
158 my $url = "http://$host/$file";
159 my $response;
160
161 unless ($bfile =~ /^counter\?.*/) {
162 my $result = $ua->head($url);
163 my $remote_headers = $result->headers;
164 $total_size = $remote_headers->content_length;
165 logger("DOWNLOAD INFO: $file has size of $total_size bytes");
166
167 $response = $ua->get($url, ':content_cb' => \&callback );
168 message("");
169 } else {
170 $response = $ua->get($url);
171 }
4d504812 172
4b122800
MT
173 my $code = $response->code();
174 my $log = $response->status_line;
a6d327a7 175 logger("DOWNLOAD INFO: HTTP-Status-Code: $code - $log");
4b122800
MT
176
177 if ( $code eq "500" ) {
e44b26cf 178 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
179 return 1;
180 }
181
4d504812 182 if ($response->is_success) {
35f38a8b
MT
183 unless ($bfile =~ /^counter\?.*/) {
184 if (open(FILE, ">$Conf::tmpdir/$bfile")) {
185 print FILE $final_data;
186 close(FILE);
a6d327a7 187 logger("DOWNLOAD INFO: File received. Start checking signature...");
06209efc 188 if (system("gpg --verify \"$Conf::tmpdir/$bfile\" &>/dev/null") eq 0) {
a6d327a7 189 logger("DOWNLOAD INFO: Signature of $bfile is fine.");
06209efc
MT
190 move("$Conf::tmpdir/$bfile","$Conf::cachedir/$bfile");
191 } else {
a6d327a7 192 message("DOWNLOAD ERROR: The downloaded file ($file) wasn't verified by IPFire.org. Sorry - Exiting...");
06209efc
MT
193 exit 1;
194 }
35f38a8b
MT
195 logger("DOWNLOAD FINISHED: $file");
196 $allok = 1;
197 return 0;
198 } else {
199 logger("DOWNLOAD ERROR: Could not open $Conf::cachedir/$bfile for writing.");
186e3d2c 200 }
4d504812 201 } else {
35f38a8b 202 return 0;
4d504812
MT
203 }
204 } else {
a6d327a7 205 logger("DOWNLOAD ERROR: $log");
4d504812 206 }
1bd42c89 207 }
a6d327a7 208 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 209 return 1;
1bd42c89
MT
210}
211
212sub getmirrors {
213 use File::Copy;
214
a6d327a7 215 logger("MIRROR: Trying to get a mirror list.");
1bd42c89 216
e3670217
MT
217 if ( -e "$Conf::dbdir/lists/server_list.db" ) {
218 my @stat = stat("$Conf::dbdir/lists/server_list.db");
219 my $time = time();
220 $age = $time - $stat[9];
221 } else {
222 # Force an update.
223 $age = "86401";
224 }
225
226 if ("$age" gt "86400") {
227 fetchfile("$Conf::version/lists/server-list.db", "$Conf::mainserver");
228 move("$Conf::cachedir/server-list.db", "$Conf::dbdir/lists/server-list.db");
229 }
1bd42c89
MT
230}
231
232sub selectmirror {
233 ### Check if there is a current server list and read it.
234 # If there is no list try to get one.
235 my $count = 0;
06209efc 236 while (!(open(FILE, "<$Conf::dbdir/lists/server-list.db")) && ($count lt 5)) {
1bd42c89
MT
237 $count++;
238 getmirrors();
239 }
240 if ($count == 5) {
a6d327a7 241 message("MIRROR ERROR: Could not find or download a server list");
1bd42c89
MT
242 exit 1;
243 }
244 my @lines = <FILE>;
245 close(FILE);
246
247 ### Count the number of the servers in the list
248 my $scount = 0;
e44b26cf 249 my @newlines;
1bd42c89 250 foreach (@lines) {
e44b26cf
MT
251 if ("$_" =~ /.*;.*;.*;/ ) {
252 push(@newlines,$_);
253 $scount++;
254 }
1bd42c89 255 }
a6d327a7 256 logger("MIRROR INFO: $scount servers found in list");
1bd42c89
MT
257
258 ### Choose a random server and test if it is online
259 # If the check fails try a new server.
260 # This will never give up.
261 my $found = 0;
262 my $servers = 0;
263 while ($found == 0) {
264 $server = int(rand($scount) + 1);
265 $servers = 0;
266 my ($line, $proto, $path, $host);
267 my @templine;
e44b26cf 268 foreach $line (@newlines) {
1bd42c89
MT
269 $servers++;
270 if ($servers eq $server) {
271 @templine = split(/\;/, $line);
272 $proto = $templine[0];
273 $host = $templine[1];
274 $path = $templine[2];
a08c3a2e 275 if (pinghost("$host")) {
1bd42c89
MT
276 $found = 1;
277 return ($proto, $host, $path);
278 }
279 }
280 }
4d504812 281 }
1bd42c89
MT
282}
283
284sub dbgetlist {
285 ### Update the database if the file is older than one day.
286 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
287 # Usage is always with an argument.
288 my $force = shift;
289 my $age;
290
291 use File::Copy;
292
293 if ( -e "$Conf::dbdir/lists/packages_list.db" ) {
294 my @stat = stat("$Conf::dbdir/lists/packages_list.db");
295 my $time = time();
296 $age = $time - $stat[9];
297 } else {
298 # Force an update.
299 $age = "86401";
300 }
301
35f38a8b 302 if (("$age" gt "86400") || ("$force" eq "force")) {
1bd42c89
MT
303 fetchfile("lists/packages_list.db", "");
304 move("$Conf::cachedir/packages_list.db", "$Conf::dbdir/lists/packages_list.db");
305 }
306}
307
308sub dblist {
309 ### This subroutine lists the packages.
310 # You may also pass a filter: &Pakfire::dblist(filter)
311 # Usage is always with two arguments.
312 # filter may be: all, notinstalled, installed
313 my $filter = shift;
314 my $forweb = shift;
4b122800 315 my @meta;
99e6df8e 316 my @updatepaks;
4b122800
MT
317 my $file;
318 my $line;
319 my $prog;
320 my ($name, $version, $release);
321 my @templine;
1bd42c89
MT
322
323 ### Make sure that the list is not outdated.
324 dbgetlist("noforce");
325
326 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
327 my @db = <FILE>;
328 close(FILE);
4b122800
MT
329
330 if ("$filter" eq "upgrade") {
331 opendir(DIR,"$Conf::dbdir/meta");
332 my @files = readdir(DIR);
333 closedir(DIR);
334 foreach $file (@files) {
335 next if ( $file eq "." );
336 next if ( $file eq ".." );
337 open(FILE, "<$Conf::dbdir/meta/$file");
338 @meta = <FILE>;
339 close(FILE);
340 foreach $line (@meta) {
341 @templine = split(/\: /,$line);
342 if ("$templine[0]" eq "Name") {
343 $name = $templine[1];
344 chomp($name);
345 } elsif ("$templine[0]" eq "ProgVersion") {
346 $version = $templine[1];
347 chomp($version);
348 } elsif ("$templine[0]" eq "Release") {
349 $release = $templine[1];
350 chomp($release);
351 }
352 }
353 foreach $prog (@db) {
354 @templine = split(/\;/,$prog);
355 if (("$name" eq "$templine[0]") && ("$release" < "$templine[2]" )) {
99e6df8e 356 push(@updatepaks,$name);
4b122800
MT
357 if ("$forweb" eq "forweb") {
358 print "<option value=\"$name\">Update: $name -- Version: $version -> $templine[1] -- Release: $release -> $templine[2]</option>\n";
359 } else {
e3670217
MT
360 my $command = "Update: $name\nVersion: $version -> $templine[1]\nRelease: $release -> $templine[2]\n";
361 if ("$Pakfire::enable_colors" eq "1") {
362 print "$color{'lila'}$command$color{'normal'}\n";
363 } else {
364 print "$command\n";
365 }
4b122800
MT
366 }
367 }
368 }
5b2a12ff 369 }
99e6df8e 370 return @updatepaks;
4b122800
MT
371 } else {
372 my $line;
e3670217 373 my $use_color;
4b122800 374 my @templine;
e3670217 375 my $count;
4b122800 376 foreach $line (sort @db) {
06209efc 377 next unless ($line =~ /.*;.*;.*;/ );
e3670217
MT
378 $use_color = "";
379 $count++;
4b122800
MT
380 @templine = split(/\;/,$line);
381 if ("$filter" eq "notinstalled") {
382 next if ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
383 } elsif ("$filter" eq "installed") {
384 next unless ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
385 }
386 if ("$forweb" eq "forweb") {
387 print "<option value=\"$templine[0]\">$templine[0]-$templine[1]-$templine[2]</option>\n";
388 } else {
e3670217
MT
389 if ("$Pakfire::enable_colors" eq "1") {
390 if (&isinstalled("$templine[0]")) {
391 $use_color = "$color{'red'}"
392 } else {
393 $use_color = "$color{'green'}"
394 }
395 }
396 print "${use_color}Name: $templine[0]\nProgVersion: $templine[1]\nRelease: $templine[2]$color{'normal'}\n\n";
4b122800 397 }
1bd42c89 398 }
e3670217 399 print "$count packages total.\n" unless ("$forweb" eq "forweb");
1bd42c89
MT
400 }
401}
402
403sub resolvedeps {
404 my $pak = shift;
405
406 getmetafile("$pak");
407
35f38a8b 408 message("PAKFIRE RESV: $pak: Resolving dependencies...");
1bd42c89
MT
409
410 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
411 my @file = <FILE>;
412 close(FILE);
413
414 my $line;
186e3d2c 415 my (@templine, @deps, @tempdeps, @all);
1bd42c89
MT
416 foreach $line (@file) {
417 @templine = split(/\: /,$line);
418 if ("$templine[0]" eq "Dependencies") {
419 @deps = split(/ /, $templine[1]);
420 }
421 }
422 chomp (@deps);
423 foreach (@deps) {
424 if ($_) {
186e3d2c
MT
425 my $return = &isinstalled($_);
426 if ($return eq 0) {
35f38a8b 427 message("PAKFIRE RESV: $pak: Dependency is already installed: $_");
186e3d2c 428 } else {
35f38a8b 429 message("PAKFIRE RESV: $pak: Need to install dependency: $_");
186e3d2c
MT
430 push(@tempdeps,$_);
431 push(@all,$_);
432 }
1bd42c89
MT
433 }
434 }
e44b26cf 435
1bd42c89
MT
436 foreach (@tempdeps) {
437 if ($_) {
438 my @newdeps = resolvedeps("$_");
439 foreach(@newdeps) {
440 unless (($_ eq " ") || ($_ eq "")) {
186e3d2c
MT
441 my $return = &isinstalled($_);
442 if ($return eq 0) {
35f38a8b 443 message("PAKFIRE RESV: $pak: Dependency is already installed: $_");
186e3d2c 444 } else {
35f38a8b 445 message("PAKFIRE RESV: $pak: Need to install dependency: $_");
186e3d2c
MT
446 push(@all,$_);
447 }
1bd42c89
MT
448 }
449 }
450 }
451 }
35f38a8b 452 message("");
186e3d2c
MT
453 chomp (@all);
454 return @all;
1bd42c89
MT
455}
456
457sub cleanup {
458 my $dir = shift;
459 my $path;
460
35f38a8b
MT
461 logger("CLEANUP: $dir");
462
1bd42c89
MT
463 if ( "$dir" eq "meta" ) {
464 $path = "$Conf::dbdir/meta";
465 } elsif ( "$dir" eq "tmp" ) {
466 $path = "$Conf::tmpdir";
467 }
468 chdir("$path");
469 opendir(DIR,".");
470 my @files = readdir(DIR);
471 closedir(DIR);
472 foreach (@files) {
473 unless (($_ eq ".") || ($_ eq "..")) {
474 system("rm -rf $_");
475 }
476 }
477}
478
479sub getmetafile {
480 my $pak = shift;
481
1bd42c89
MT
482 unless ( -e "$Conf::dbdir/meta/meta-$pak") {
483 fetchfile("meta/meta-$pak", "");
484 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
485 }
486
487 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
488 my @line = <FILE>;
489 close(FILE);
490
491 open(FILE, ">$Conf::dbdir/meta/meta-$pak");
492 foreach (@line) {
493 my $string = $_;
494 $string =~ s/\r\n/\n/g;
495 print FILE $string;
496 }
497 close(FILE);
498 return 1;
499}
500
501sub getsize {
502 my $pak = shift;
503
504 getmetafile("$pak");
505
506 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
507 my @file = <FILE>;
508 close(FILE);
509
510 my $line;
511 my @templine;
512 foreach $line (@file) {
513 @templine = split(/\: /,$line);
514 if ("$templine[0]" eq "Size") {
515 chomp($templine[1]);
516 return $templine[1];
517 }
518 }
4b122800 519 return 0;
1bd42c89
MT
520}
521
522sub decryptpak {
523 my $pak = shift;
524
525 cleanup("tmp");
526
527 my $file = getpak("$pak", "noforce");
528
a6d327a7 529 logger("DECRYPT STARTED: $pak");
35f38a8b 530 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 531 $return %= 255;
a6d327a7 532 logger("DECRYPT FINISHED: $pak - Status: $return");
cde0e116 533 if ($return != 0) { exit 1; }
1bd42c89
MT
534}
535
536sub getpak {
537 my $pak = shift;
538 my $force = shift;
539
540 getmetafile("$pak");
541
542 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
543 my @file = <FILE>;
544 close(FILE);
545
546 my $line;
547 my $file;
548 my @templine;
549 foreach $line (@file) {
550 @templine = split(/\: /,$line);
551 if ("$templine[0]" eq "File") {
552 chomp($templine[1]);
553 $file = $templine[1];
554 }
555 }
556
557 unless ($file) {
558 message("No filename given in meta-file. Please phone the developers.");
559 exit 1;
560 }
561
1bd42c89
MT
562 unless ( "$force" eq "force" ) {
563 if ( -e "$Conf::cachedir/$file" ) {
1bd42c89
MT
564 return $file;
565 }
566 }
567
568 fetchfile("paks/$file", "");
569 return $file;
570}
571
572sub setuppak {
573 my $pak = shift;
574
35f38a8b 575 message("PAKFIRE INST: $pak: Decrypting...");
1bd42c89
MT
576 decryptpak("$pak");
577
35f38a8b 578 message("PAKFIRE INST: $pak: Copying files and running post-installation scripts...");
99e6df8e 579 my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1");
cde0e116 580 $return %= 255;
e44b26cf
MT
581 if ($pakfiresettings{'UUID'} ne "off") {
582 fetchfile("cgi-bin/counter?ver=$Conf::version&uuid=$Conf::uuid&ipak=$pak&return=$return", "$Conf::mainserver");
583 }
1bd42c89
MT
584 if ($return == 0) {
585 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
586 cleanup("tmp");
4d504812 587 copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/");
35f38a8b
MT
588 message("PAKFIRE INST: $pak: Finished.");
589 message("");
1bd42c89 590 } else {
35f38a8b 591 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
592 exit $return;
593 }
a08c3a2e 594 return $return;
1bd42c89
MT
595}
596
186e3d2c
MT
597sub isinstalled {
598 my $pak = shift;
599 if ( open(FILE,"<$Conf::dbdir/installed/meta-$pak") ) {
600 close(FILE);
601 return 0;
602 } else {
603 return 1;
604 }
605}
606
99e6df8e 607sub upgradepak {
1bd42c89
MT
608 my $pak = shift;
609
35f38a8b 610 message("PAKFIRE UPGR: $pak: Decrypting...");
1bd42c89
MT
611 decryptpak("$pak");
612
35f38a8b 613 message("PAKFIRE UPGR: $pak: Upgrading files and running post-upgrading scripts...");
99e6df8e
MT
614 my $return = system("cd $Conf::tmpdir && NAME=$pak ./update.sh >> $Conf::logdir/update-$pak.log 2>&1");
615 $return %= 255;
e44b26cf
MT
616 if ($pakfiresettings{'UUID'} ne "off") {
617 fetchfile("cgi-bin/counter?ver=$Conf::version&uuid=$Conf::uuid&upak=$pak&return=$return", "$Conf::mainserver");
618 }
1bd42c89
MT
619 if ($return == 0) {
620 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
621 cleanup("tmp");
99e6df8e 622 copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/");
35f38a8b
MT
623 message("PAKFIRE UPGR: $pak: Finished.");
624 message("");
1bd42c89 625 } else {
35f38a8b 626 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
627 exit $return;
628 }
8e58bd37 629 return $return;
1bd42c89
MT
630}
631
632sub removepak {
633 my $pak = shift;
634
35f38a8b 635 message("PAKFIRE REMV: $pak: Decrypting...");
1bd42c89
MT
636 decryptpak("$pak");
637
35f38a8b 638 message("PAKFIRE REMV: $pak: Removing files and running post-removing scripts...");
99e6df8e
MT
639 my $return = system("cd $Conf::tmpdir && NAME=$pak ./uninstall.sh >> $Conf::logdir/uninstall-$pak.log 2>&1");
640 $return %= 255;
e44b26cf
MT
641 if ($pakfiresettings{'UUID'} ne "off") {
642 fetchfile("cgi-bin/counter?ver=$Conf::version&uuid=$Conf::uuid&dpak=$pak&return=$return", "$Conf::mainserver");
643 }
1bd42c89
MT
644 if ($return == 0) {
645 open(FILE, "<$Conf::dbdir/rootfiles/$pak");
646 my @file = <FILE>;
647 close(FILE);
648 foreach (@file) {
649 my $line = $_;
650 chomp($line);
651 system("echo \"Removing: $line\" >> $Conf::logdir/uninstall-$pak.log 2>&1");
652 system("cd / && rm -rf $line >> $Conf::logdir/uninstall-$pak.log 2>&1");
653 }
654 unlink("$Conf::dbdir/rootfiles/$pak");
a6d327a7 655 unlink("$Conf::dbdir/installed/meta-$pak");
1bd42c89 656 cleanup("tmp");
35f38a8b
MT
657 message("PAKFIRE REMV: $pak: Finished.");
658 message("");
1bd42c89 659 } else {
35f38a8b 660 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
661 exit $return;
662 }
8e58bd37 663 return $return;
1bd42c89
MT
664}
665
666sub beautifysize {
667 my $size = shift;
35f38a8b 668 #$size = $size / 1024;
a08c3a2e 669 my $unit;
1bd42c89 670
35f38a8b
MT
671 if ($size > 1023*1024) {
672 $size = ($size / (1024*1024));
a08c3a2e 673 $unit = "MB";
35f38a8b
MT
674 } elsif ($size > 1023) {
675 $size = ($size / 1024);
a08c3a2e 676 $unit = "KB";
35f38a8b
MT
677 } else {
678 $unit = "B";
1bd42c89 679 }
a08c3a2e
MT
680 $size = sprintf("%.2f" , $size);
681 my $string = "$size $unit";
682 return $string;
1bd42c89
MT
683}
684
8e58bd37
MT
685sub makeuuid {
686 unless ( -e "$Conf::dbdir/uuid" ) {
8e58bd37
MT
687 open(FILE, "</proc/sys/kernel/random/uuid");
688 my @line = <FILE>;
689 close(FILE);
690
691 open(FILE, ">$Conf::dbdir/uuid");
692 foreach (@line) {
693 print FILE $_;
694 }
695 close(FILE);
696 }
697}
698
699sub senduuid {
99e6df8e 700 if ($pakfiresettings{'UUID'} ne "off") {
4b122800
MT
701 unless("$Conf::uuid") {
702 $Conf::uuid = `cat $Conf::dbdir/uuid`;
703 }
704 logger("Sending my uuid: $Conf::uuid");
705 fetchfile("cgi-bin/counter?ver=$Conf::version&uuid=$Conf::uuid", "$Conf::mainserver");
06209efc 706 system("rm -f $Conf::tmpdir/counter* 2>/dev/null");
8e58bd37 707 }
8e58bd37 708}
1bd42c89 709
cde0e116 710sub checkcryptodb {
a6d327a7 711 logger("CRYPTO INFO: Checking GnuPG Database");
cde0e116
MT
712 my $ret = system("gpg --list-keys | grep -q $myid");
713 unless ( "$ret" eq "0" ) {
a6d327a7
MT
714 message("CRYPTO WARN: The GnuPG isn't configured corectly. Trying now to fix this.");
715 message("CRYPTO WARN: It's normal to see this on first execution.");
750c1528
MT
716 system("gpg --keyserver wwwkeys.de.pgp.net --always-trust --status-fd 2 --recv-key $myid >> $Conf::logdir/gnupg-database.log 2>&1");
717 system("gpg --keyserver wwwkeys.de.pgp.net --always-trust --status-fd 2 --recv-key $trustid >> $Conf::logdir/gnupg-database.log 2>&1");
a6d327a7
MT
718 } else {
719 logger("CRYPTO INFO: Database is okay");
cde0e116
MT
720 }
721}
722
a6d327a7
MT
723sub callback {
724 my ($data, $response, $protocol) = @_;
725 $final_data .= $data;
35f38a8b 726 print progress_bar( length($final_data), $total_size, 30, '=' );
a6d327a7
MT
727}
728
729sub progress_bar {
730 my ( $got, $total, $width, $char ) = @_;
731 my $show_bfile;
35f38a8b 732 $width ||= 30; $char ||= '=';
a6d327a7 733 my $len_bfile = length $bfile;
35f38a8b
MT
734 if ("$len_bfile" >= "17") {
735 $show_bfile = substr($bfile,0,17)."...";
a6d327a7
MT
736 } else {
737 $show_bfile = $bfile;
35f38a8b
MT
738 }
739 $progress = sprintf("%.2f%%", 100*$got/+$total);
740 sprintf "$color{'lightgreen'}%-20s %7s |%-${width}s| %10s$color{'normal'}\r",$show_bfile, $progress, $char x (($width-1)*$got/$total). '>', beautifysize($got);
a6d327a7
MT
741}
742
1bd42c89 7431;