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