]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/pakfire/lib/functions.pl
pakfire: Replace dbgetlist duplicate code
[people/pmueller/ipfire-2.x.git] / src / pakfire / lib / functions.pl
CommitLineData
1bd42c89 1#!/usr/bin/perl -w
70df8302
MT
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
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
c6d055a8 413 my %paklist = &Pakfire::dblist("all");
ff9545f8
AF
414
415 opendir(DIR,"$Conf::dbdir/meta");
416 my @files = readdir(DIR);
417 closedir(DIR);
418 foreach $file (@files) {
419 next if ( $file eq "." );
420 next if ( $file eq ".." );
1af34aa8 421 next if ( $file eq "meta-" );
ff9545f8 422 next if ( $file =~ /^old/ );
847df41d
RR
423 %metadata = parsemetafile("$Conf::dbdir/meta/$file");
424
c6d055a8
RR
425 if ((defined $paklist{"$metadata{'Name'}"}) && (
426 ("$paklist{\"$metadata{'Name'}\"}{'Release'}" ne "$metadata{'Release'}") ||
427 (defined $paklist{"$metadata{'Name'}"}{'AvailableRelease'}))
428 ) {
429 move("$Conf::dbdir/meta/meta-$metadata{'Name'}","$Conf::dbdir/meta/old_meta-$metadata{'Name'}");
430 getmetafile($metadata{'Name'});
ff9545f8
AF
431 }
432 }
1bd42c89
MT
433}
434
0bd5b131
RR
435sub coredbinfo {
436 ### This subroutine returns core db version information in a hash.
437 # Usage is without arguments
438
439 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
440
441 my %coredb = (
442 CoreVersion => $Conf::version,
443 Release => $Conf::core_mine,
444 );
445
446 $coredb{'AvailableRelease'} = $core_release if ("$Conf::core_mine" < "$core_release");
447
448 return %coredb;
449}
450
1bd42c89 451sub dblist {
0bd5b131
RR
452 ### This subroutine returns the packages from the packages_list db in a hash.
453 # It uses the currently cached version of packages_list. To ensure latest
454 # data, run Pakfire::dbgetlist first.
455 # You may also pass a filter: &Pakfire::dblist(filter)
456 # Usage is always with one argument.
457 # filter may be:
458 # - "all": list all known paks,
459 # - "notinstalled": list only not installed paks,
460 # - "installed": list only installed paks
461 # - "upgrade": list only upgradable paks
462 #
463 # Returned hash format:
464 # ( "<pak name>" => (
465 # "Installed" => "Yes" or "No" wether the pak is installed,
466 # "ProgVersion" => Installed program version when "Installed" => "Yes" or
467 # Available version when "Installed" => No,
468 # "Release" => Installed pak release number when "Installed" => "Yes" or
469 # Available pak release number when "Installed" => No,
470 # "AvailableProgVersion" => Available program version.
471 # Only defined if an upgrade to a higher version is available,
472 # "AvailableRelease" => Available pak release version.
473 # Only defined if an upgrade to a higher version is available
474 # ),
475 # ...
476 # )
477
1bd42c89 478 my $filter = shift;
0bd5b131 479 my %paklist = ();
4b122800
MT
480 my $file;
481 my $line;
847df41d 482 my %metadata;
4b122800 483 my @templine;
0bd5b131 484
1bd42c89
MT
485 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
486 my @db = <FILE>;
487 close(FILE);
4b122800 488
0bd5b131 489 if ("$filter" ne "notinstalled") {
5e932bd5 490 opendir(DIR,"$Conf::dbdir/installed");
4b122800
MT
491 my @files = readdir(DIR);
492 closedir(DIR);
0bd5b131 493
4b122800
MT
494 foreach $file (@files) {
495 next if ( $file eq "." );
496 next if ( $file eq ".." );
3f01107b 497 next if ( $file =~ /^old/ );
847df41d
RR
498 %metadata = parsemetafile("$Conf::dbdir/installed/$file");
499
0bd5b131
RR
500 foreach $line (@db) {
501 next unless ($line =~ /.*;.*;.*;/ );
502 @templine = split(/\;/,$line);
503 if (("$metadata{'Name'}" eq "$templine[0]") && ("$metadata{'Release'}" < "$templine[2]")) {
504 # Add all upgradable paks to list
505 $paklist{"$metadata{'Name'}"} = {
506 ProgVersion => $metadata{'ProgVersion'},
507 Release => $metadata{'Release'},
508 AvailableProgVersion => $templine[1],
509 AvailableRelease => $templine[2],
510 Installed => "yes"
511 };
512 last;
513 } elsif (("$metadata{'Name'}" eq "$templine[0]") && ("$filter" ne "upgrade")) {
514 # Add installed paks without an upgrade available to list
515 $paklist{"$metadata{'Name'}"} = {
516 ProgVersion => $metadata{'ProgVersion'},
517 Release => $metadata{'Release'},
518 Installed => "yes"
519 };
520 last;
4b122800
MT
521 }
522 }
5b2a12ff 523 }
0bd5b131
RR
524 }
525
526 # Add all not installed paks to list
527 if (("$filter" ne "upgrade") && ("$filter" ne "installed")) {
528 foreach $line (@db) {
06209efc 529 next unless ($line =~ /.*;.*;.*;/ );
4b122800 530 @templine = split(/\;/,$line);
0bd5b131
RR
531 next if ((defined $paklist{"$templine[0]"}) || (&isinstalled($templine[0]) == 0));
532
533 $paklist{"$templine[0]"} = {
534 ProgVersion => "$templine[1]",
535 Release => "$templine[2]",
536 Installed => "no"
537 };
1bd42c89
MT
538 }
539 }
0bd5b131
RR
540
541 return %paklist;
1bd42c89
MT
542}
543
621dcd86 544sub resolvedeps_one {
1bd42c89 545 my $pak = shift;
8ce72945 546
35f38a8b 547 message("PAKFIRE RESV: $pak: Resolving dependencies...");
66c36198 548
8ce72945
RR
549 unless (getmetafile("$pak")) {
550 message("PAKFIRE ERROR: Error retrieving dependency information on $pak. Unable to resolve dependencies.");
551 exit 1;
552 };
553
847df41d
RR
554 my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak");
555 my @all;
556 my @deps = split(/ /, $metadata{'Dependencies'});
1bd42c89
MT
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 565 push(@all,$_);
66c36198 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;
66c36198 625
35f38a8b 626 logger("CLEANUP: $dir");
66c36198 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;
8ce72945
RR
646
647 # Try to download meta-file if we don't have one yet, or it is empty for some reason
648 if ((! -e "$Conf::dbdir/meta/meta-$pak" ) || ( -z "$Conf::dbdir/meta/meta-$pak" )) {
649 return 0 unless (fetchfile("meta/meta-$pak", ""));
1bd42c89
MT
650 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
651 }
66c36198 652
1bd42c89
MT
653 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
654 my @line = <FILE>;
655 close(FILE);
66c36198 656
1bd42c89
MT
657 open(FILE, ">$Conf::dbdir/meta/meta-$pak");
658 foreach (@line) {
659 my $string = $_;
660 $string =~ s/\r\n/\n/g;
661 print FILE $string;
662 }
663 close(FILE);
8ce72945 664
1bd42c89
MT
665 return 1;
666}
667
668sub getsize {
669 my $pak = shift;
66c36198 670
1bd42c89 671 getmetafile("$pak");
66c36198 672
847df41d
RR
673 if (my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak")) {
674 return $metadata{'Size'};
675 }
676 return 0;
677}
678
679sub parsemetafile {
680 ### This subroutine returns a hash with the contents of a meta- file
681 # Pass path to metafile as argument: Pakfire::parsemetafile("$Conf::dbdir/meta/meta-$pak")
682 # Usage is always with an argument.
683 my $metafile = shift;
684
685 my %metadata = ();
686
687 my @templine;
688 my @file;
689
690 if (! -e $metafile ) {
691 return 0;
692 }
693
694 open(FILE, "<$metafile");
695 @file = <FILE>;
1bd42c89 696 close(FILE);
66c36198 697
847df41d
RR
698 foreach (@file) {
699 @templine = split(/\: /,$_);
700 if ($templine[1]) {
1bd42c89 701 chomp($templine[1]);
847df41d 702 $metadata{"$templine[0]"} = $templine[1];
1bd42c89
MT
703 }
704 }
847df41d
RR
705
706 return %metadata;
1bd42c89
MT
707}
708
709sub decryptpak {
710 my $pak = shift;
66c36198 711
1bd42c89 712 cleanup("tmp");
66c36198 713
1bd42c89 714 my $file = getpak("$pak", "noforce");
66c36198 715
a6d327a7 716 logger("DECRYPT STARTED: $pak");
35f38a8b 717 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 718 $return %= 255;
a6d327a7 719 logger("DECRYPT FINISHED: $pak - Status: $return");
cde0e116 720 if ($return != 0) { exit 1; }
1bd42c89
MT
721}
722
723sub getpak {
724 my $pak = shift;
725 my $force = shift;
726
8ce72945
RR
727 unless (getmetafile("$pak")) {
728 message("PAKFIRE ERROR: Unable to retrieve $pak metadata.");
729 exit 1;
730 }
731
847df41d
RR
732 my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak");
733 my $file = $metadata{'File'};
66c36198 734
1bd42c89 735 unless ($file) {
1af34aa8 736 message("No filename given in meta-file.");
1bd42c89
MT
737 exit 1;
738 }
66c36198 739
1bd42c89
MT
740 unless ( "$force" eq "force" ) {
741 if ( -e "$Conf::cachedir/$file" ) {
1bd42c89
MT
742 return $file;
743 }
744 }
8ce72945
RR
745
746 unless (fetchfile("paks/$file", "")) {
747 message("PAKFIRE ERROR: Unable to download $pak.");
748 exit 1;
749 }
1bd42c89
MT
750 return $file;
751}
752
753sub setuppak {
754 my $pak = shift;
66c36198 755
35f38a8b 756 message("PAKFIRE INST: $pak: Decrypting...");
1bd42c89 757 decryptpak("$pak");
66c36198 758
35f38a8b 759 message("PAKFIRE INST: $pak: Copying files and running post-installation scripts...");
99e6df8e 760 my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1");
cde0e116 761 $return %= 255;
1bd42c89
MT
762 if ($return == 0) {
763 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
764 cleanup("tmp");
4d504812 765 copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/");
35f38a8b
MT
766 message("PAKFIRE INST: $pak: Finished.");
767 message("");
1bd42c89 768 } else {
35f38a8b 769 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
770 exit $return;
771 }
a08c3a2e 772 return $return;
1bd42c89
MT
773}
774
2aa6d448 775sub upgradecore {
377560fb 776 getcoredb("noforce");
2aa6d448 777 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
518f894b 778 if ("$core_release" > "$Conf::core_mine") {
712425ff
AF
779 # Safety check for lazy testers:
780 # Before we upgrade to the latest release, we re-install the previous release
781 # to make sure that the tester has always been on the latest version.
782 my $tree = &get_tree();
783 $Conf::core_mine-- if ($tree eq "testing" || $tree eq "unstable");
784
2aa6d448 785 message("CORE UPGR: Upgrading from release $Conf::core_mine to $core_release");
66c36198 786
2aa6d448
MT
787 my @seq = `seq $Conf::core_mine $core_release`;
788 shift @seq;
789 my $release;
790 foreach $release (@seq) {
791 chomp($release);
792 getpak("core-upgrade-$release");
793 }
66c36198 794
2aa6d448
MT
795 foreach $release (@seq) {
796 chomp($release);
797 upgradepak("core-upgrade-$release");
798 }
66c36198 799
2aa6d448 800 system("echo $core_release > $Conf::coredir/mine");
66c36198 801
2aa6d448 802 } else {
950d00b2 803 message("CORE INFO: No new upgrades available. You are on release $Conf::core_mine.");
2aa6d448
MT
804 }
805}
806
186e3d2c
MT
807sub isinstalled {
808 my $pak = shift;
809 if ( open(FILE,"<$Conf::dbdir/installed/meta-$pak") ) {
810 close(FILE);
811 return 0;
812 } else {
813 return 1;
814 }
815}
816
99e6df8e 817sub upgradepak {
1bd42c89
MT
818 my $pak = shift;
819
35f38a8b 820 message("PAKFIRE UPGR: $pak: Decrypting...");
1bd42c89
MT
821 decryptpak("$pak");
822
35f38a8b 823 message("PAKFIRE UPGR: $pak: Upgrading files and running post-upgrading scripts...");
99e6df8e
MT
824 my $return = system("cd $Conf::tmpdir && NAME=$pak ./update.sh >> $Conf::logdir/update-$pak.log 2>&1");
825 $return %= 255;
1bd42c89
MT
826 if ($return == 0) {
827 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
828 cleanup("tmp");
4e4b54c5 829 copy("$Conf::dbdir/meta/meta-$pak", "$Conf::dbdir/installed/");
35f38a8b
MT
830 message("PAKFIRE UPGR: $pak: Finished.");
831 message("");
1bd42c89 832 } else {
35f38a8b 833 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
834 exit $return;
835 }
8e58bd37 836 return $return;
1bd42c89
MT
837}
838
839sub removepak {
840 my $pak = shift;
841
35f38a8b 842 message("PAKFIRE REMV: $pak: Decrypting...");
1bd42c89
MT
843 decryptpak("$pak");
844
35f38a8b 845 message("PAKFIRE REMV: $pak: Removing files and running post-removing scripts...");
99e6df8e
MT
846 my $return = system("cd $Conf::tmpdir && NAME=$pak ./uninstall.sh >> $Conf::logdir/uninstall-$pak.log 2>&1");
847 $return %= 255;
1bd42c89 848 if ($return == 0) {
1bd42c89 849 unlink("$Conf::dbdir/rootfiles/$pak");
a6d327a7 850 unlink("$Conf::dbdir/installed/meta-$pak");
1bd42c89 851 cleanup("tmp");
35f38a8b
MT
852 message("PAKFIRE REMV: $pak: Finished.");
853 message("");
1bd42c89 854 } else {
35f38a8b 855 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
856 exit $return;
857 }
8e58bd37 858 return $return;
1bd42c89
MT
859}
860
861sub beautifysize {
862 my $size = shift;
35f38a8b 863 #$size = $size / 1024;
a08c3a2e 864 my $unit;
66c36198 865
35f38a8b
MT
866 if ($size > 1023*1024) {
867 $size = ($size / (1024*1024));
a08c3a2e 868 $unit = "MB";
35f38a8b
MT
869 } elsif ($size > 1023) {
870 $size = ($size / 1024);
a08c3a2e 871 $unit = "KB";
35f38a8b
MT
872 } else {
873 $unit = "B";
1bd42c89 874 }
a08c3a2e
MT
875 $size = sprintf("%.2f" , $size);
876 my $string = "$size $unit";
877 return $string;
1bd42c89
MT
878}
879
8e58bd37
MT
880sub makeuuid {
881 unless ( -e "$Conf::dbdir/uuid" ) {
8e58bd37
MT
882 open(FILE, "</proc/sys/kernel/random/uuid");
883 my @line = <FILE>;
884 close(FILE);
66c36198 885
8e58bd37
MT
886 open(FILE, ">$Conf::dbdir/uuid");
887 foreach (@line) {
888 print FILE $_;
889 }
890 close(FILE);
891 }
892}
893
a6d327a7
MT
894sub callback {
895 my ($data, $response, $protocol) = @_;
896 $final_data .= $data;
35f38a8b 897 print progress_bar( length($final_data), $total_size, 30, '=' );
a6d327a7
MT
898}
899
900sub progress_bar {
901 my ( $got, $total, $width, $char ) = @_;
902 my $show_bfile;
35f38a8b 903 $width ||= 30; $char ||= '=';
a6d327a7 904 my $len_bfile = length $bfile;
35f38a8b
MT
905 if ("$len_bfile" >= "17") {
906 $show_bfile = substr($bfile,0,17)."...";
a6d327a7
MT
907 } else {
908 $show_bfile = $bfile;
66c36198 909 }
35f38a8b
MT
910 $progress = sprintf("%.2f%%", 100*$got/+$total);
911 sprintf "$color{'lightgreen'}%-20s %7s |%-${width}s| %10s$color{'normal'}\r",$show_bfile, $progress, $char x (($width-1)*$got/$total). '>', beautifysize($got);
a6d327a7
MT
912}
913
090af02e
AK
914sub updates_available {
915 # Get packets with updates available
0bd5b131 916 my %upgradepaks = &Pakfire::dblist("upgrade");
090af02e 917
0bd5b131
RR
918 # Get the length of the returned hash
919 my $updatecount = keys %upgradepaks;
090af02e
AK
920
921 return "$updatecount";
922}
923
924sub coreupdate_available {
925 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
926 if ("$core_release" > "$Conf::core_mine") {
927 return "yes ($core_release)";
928 }
929 else {
930 return "no";
931 }
932}
933
934sub reboot_required {
935 if ( -e "/var/run/need_reboot" ) {
936 return "yes";
937 }
938 else {
939 return "no";
940 }
941}
942
943sub status {
944 # General info
8c072c5c
AK
945 my $return = "Core-Version: $Conf::version\n";
946 $return .= "Core-Update-Level: $Conf::core_mine\n";
090af02e
AK
947 $return .= "Last update: " . &General::age("/opt/pakfire/db/core/mine") . " ago\n";
948 $return .= "Last core-list update: " . &General::age("/opt/pakfire/db/lists/core-list.db") . " ago\n";
949 $return .= "Last server-list update: " . &General::age("/opt/pakfire/db/lists/server-list.db") . " ago\n";
950 $return .= "Last packages-list update: " . &General::age("/opt/pakfire/db/lists/packages_list.db") . " ago\n";
951
952 # Get availability of core updates
953 $return .= "Core-Update available: " . &Pakfire::coreupdate_available() . "\n";
954
955 # Get availability of package updates
956 $return .= "Package-Updates available: " . &Pakfire::updates_available() . "\n";
957
958 # Test if reboot is required
959 $return .= "Reboot required: " . &Pakfire::reboot_required() . "\n";
960
961 # Return status text
962 print "$return";
963 exit 1;
964}
965
e6f4991b
MT
966sub get_arch() {
967 # Append architecture
968 my ($sysname, $nodename, $release, $version, $machine) = POSIX::uname();
969
b16ac3ba
AF
970 # We only support armv6l for 32 bit arm
971 if ($machine =~ m/armv[67]/) {
972 return "armv6l";
e6f4991b
MT
973 }
974
975 return $machine;
976}
977
152378c6
MT
978sub get_tree() {
979 # Return stable if nothing is set
980 return "stable" unless (defined $pakfiresettings{'TREE'});
981
982 return $pakfiresettings{'TREE'};
983}
984
e6f4991b
MT
985sub make_version() {
986 my $version = "";
987
988 # Open /etc/system-release
989 open(RELEASE, "</etc/system-release");
990 my $release = <RELEASE>;
991 close(RELEASE);
992
993 # Add the main relase
994 if ($release =~ m/IPFire ([\d\.]+)/) {
995 $version .= $1;
996 }
997
152378c6
MT
998 # Append suffix for tree
999 my $tree = &get_tree();
1000 if ($tree eq "testing") {
1001 $version .= ".1";
1002 } elsif ($tree eq "unstable") {
1003 $version .= ".2";
1004 }
1005
e6f4991b 1006 # Append architecture
6cf219c4 1007 $version .= "-" . &get_arch();
e6f4991b
MT
1008
1009 return $version;
1010}
1011
1bd42c89 10121;