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