]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/pakfire/lib/functions.pl
Early spring clean: Remove trailing whitespaces, and correct licence headers
[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 #
847df41d 5# Copyright (C) 2007-2021 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
MT
46# A small color-hash :D
47my %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.");
4b122800
MT
209 return 1;
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;
35f38a8b 229 return 0;
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.");
4d504812 238 return 1;
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") {
e3670217
MT
259 fetchfile("$Conf::version/lists/server-list.db", "$Conf::mainserver");
260 move("$Conf::cachedir/server-list.db", "$Conf::dbdir/lists/server-list.db");
261 }
1bd42c89
MT
262}
263
2aa6d448 264sub getcoredb {
377560fb
MT
265 my $force = shift;
266 my $age;
66c36198 267
2aa6d448 268 use File::Copy;
66c36198 269
2aa6d448
MT
270 if ( -e "$Conf::dbdir/lists/core-list.db" ) {
271 my @stat = stat("$Conf::dbdir/lists/core-list.db");
272 my $time = time();
273 $age = $time - $stat[9];
377560fb
MT
274 $force = "force" if ("$age" >= "3600");
275 logger("CORE INFO: core-list.db is $age seconds old. - DEBUG: $force");
2aa6d448
MT
276 } else {
277 # Force an update.
377560fb 278 $force = "force";
2aa6d448 279 }
66c36198 280
377560fb 281 if ("$force" eq "force") {
2aa6d448
MT
282 fetchfile("lists/core-list.db", "");
283 move("$Conf::cachedir/core-list.db", "$Conf::dbdir/lists/core-list.db");
284 }
285}
286
3e29608f
MT
287sub valid_signature($) {
288 my $filename = shift;
289
290 open(my $cmd, "gpg --verify --status-fd 1 \"$filename\" 2>/dev/null |");
291 while (<$cmd>) {
292 # Process valid signature lines
293 if (/VALIDSIG ([A-Z0-9]+)/) {
294 # Check if we know the key
295 foreach my $key (@VALID_KEY_FINGERPRINTS) {
296 # Signature is valid
297 return 1 if ($key eq $1);
298 }
299 }
300 }
301 close($cmd);
302
303 # Signature is invalid
304 return 0;
305}
2aa6d448 306
1bd42c89 307sub selectmirror {
ec18a1ec
MT
308 if (defined ${Conf::mirror}) {
309 my $uri = URI->new("${Conf::mirror}");
310
311 # Only accept HTTPS mirrors
312 if ($uri->scheme eq "https") {
313 return ("HTTPS", $uri->host, $uri->path . "/" . ${Conf::version});
314 } else {
315 message("MIRROR ERROR: Unsupported mirror: " . ${Conf::mirror});
316 }
317 }
318
1bd42c89
MT
319 ### Check if there is a current server list and read it.
320 # If there is no list try to get one.
321 my $count = 0;
3d3b68c5 322 while (!(open(FILE, "<$Conf::dbdir/lists/server-list.db")) && ($count lt 5)) {
1bd42c89 323 $count++;
377560fb 324 getmirrors("noforce");
1bd42c89 325 }
3d3b68c5 326 if ($count == 5) {
a6d327a7 327 message("MIRROR ERROR: Could not find or download a server list");
1bd42c89
MT
328 exit 1;
329 }
330 my @lines = <FILE>;
331 close(FILE);
332
333 ### Count the number of the servers in the list
334 my $scount = 0;
e44b26cf 335 my @newlines;
1bd42c89 336 foreach (@lines) {
e44b26cf
MT
337 if ("$_" =~ /.*;.*;.*;/ ) {
338 push(@newlines,$_);
339 $scount++;
340 }
1bd42c89 341 }
a6d327a7 342 logger("MIRROR INFO: $scount servers found in list");
63efc01c
MT
343
344 if ($scount eq 0) {
345 logger("MIRROR INFO: Could not find any servers. Falling back to main server $Conf::mainserver");
c846ed16 346 return ("HTTPS", $Conf::mainserver, "/$Conf::version");
63efc01c
MT
347 }
348
1bd42c89
MT
349 ### Choose a random server and test if it is online
350 # If the check fails try a new server.
351 # This will never give up.
1bd42c89 352 my $servers = 0;
e32591e7 353 while (1) {
1bd42c89
MT
354 $server = int(rand($scount) + 1);
355 $servers = 0;
356 my ($line, $proto, $path, $host);
357 my @templine;
e44b26cf 358 foreach $line (@newlines) {
1bd42c89
MT
359 $servers++;
360 if ($servers eq $server) {
361 @templine = split(/\;/, $line);
362 $proto = $templine[0];
363 $host = $templine[1];
364 $path = $templine[2];
e32591e7
MT
365
366 return ($proto, $host, $path);
1bd42c89
MT
367 }
368 }
4d504812 369 }
1bd42c89
MT
370}
371
372sub dbgetlist {
373 ### Update the database if the file is older than one day.
374 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
375 # Usage is always with an argument.
376 my $force = shift;
377 my $age;
66c36198 378
1bd42c89 379 use File::Copy;
66c36198 380
1bd42c89
MT
381 if ( -e "$Conf::dbdir/lists/packages_list.db" ) {
382 my @stat = stat("$Conf::dbdir/lists/packages_list.db");
383 my $time = time();
384 $age = $time - $stat[9];
377560fb
MT
385 $force = "force" if ("$age" >= "3600");
386 logger("DB INFO: packages_list.db is $age seconds old. - DEBUG: $force");
1bd42c89
MT
387 } else {
388 # Force an update.
377560fb 389 $force = "force";
1bd42c89 390 }
66c36198 391
377560fb 392 if ("$force" eq "force") {
1bd42c89
MT
393 fetchfile("lists/packages_list.db", "");
394 move("$Conf::cachedir/packages_list.db", "$Conf::dbdir/lists/packages_list.db");
395 }
ff9545f8
AF
396
397 # Update the meta database if new packages was in the package list
ff9545f8
AF
398 my $file;
399 my $line;
400 my $prog;
847df41d 401 my %metadata;
ff9545f8
AF
402 my @templine;
403
404 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
405 my @db = <FILE>;
406 close(FILE);
407
408 opendir(DIR,"$Conf::dbdir/meta");
409 my @files = readdir(DIR);
410 closedir(DIR);
411 foreach $file (@files) {
412 next if ( $file eq "." );
413 next if ( $file eq ".." );
1af34aa8 414 next if ( $file eq "meta-" );
ff9545f8 415 next if ( $file =~ /^old/ );
847df41d
RR
416 %metadata = parsemetafile("$Conf::dbdir/meta/$file");
417
ff9545f8
AF
418 foreach $prog (@db) {
419 @templine = split(/\;/,$prog);
847df41d
RR
420 if (("$metadata{'Name'}" eq "$templine[0]") && ("$metadata{'Release'}" ne "$templine[2]")) {
421 move("$Conf::dbdir/meta/meta-$metadata{'Name'}","$Conf::dbdir/meta/old_meta-$metadata{'Name'}");
422 fetchfile("meta/meta-$metadata{'Name'}", "");
423 move("$Conf::cachedir/meta-$metadata{'Name'}", "$Conf::dbdir/meta/meta-$metadata{'Name'}");
ff9545f8
AF
424 }
425 }
426 }
1bd42c89
MT
427}
428
429sub dblist {
430 ### This subroutine lists the packages.
66c36198 431 # You may also pass a filter: &Pakfire::dblist(filter)
1bd42c89
MT
432 # Usage is always with two arguments.
433 # filter may be: all, notinstalled, installed
434 my $filter = shift;
435 my $forweb = shift;
99e6df8e 436 my @updatepaks;
4b122800
MT
437 my $file;
438 my $line;
439 my $prog;
847df41d 440 my %metadata;
4b122800 441 my @templine;
66c36198
PM
442
443 ### Make sure that the list is not outdated.
377560fb 444 #dbgetlist("noforce");
1bd42c89
MT
445
446 open(FILE, "<$Conf::dbdir/lists/packages_list.db");
447 my @db = <FILE>;
448 close(FILE);
4b122800
MT
449
450 if ("$filter" eq "upgrade") {
4638fd92 451 if ("$forweb" ne "forweb" && "$forweb" ne "notice" ) {getcoredb("noforce");}
8a8a50ad 452 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
518f894b 453 if ("$core_release" > "$Conf::core_mine") {
8a8a50ad
MT
454 if ("$forweb" eq "forweb") {
455 print "<option value=\"core\">Core-Update -- $Conf::version -- Release: $Conf::core_mine -> $core_release</option>\n";
2b38ab5c
JPT
456 }
457 elsif ("$forweb" eq "notice") {
462a5e86 458 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
459 } else {
460 my $command = "Core-Update $Conf::version\nRelease: $Conf::core_mine -> $core_release\n";
461 if ("$Pakfire::enable_colors" eq "1") {
462 print "$color{'lila'}$command$color{'normal'}\n";
463 } else {
464 print "$command\n";
465 }
466 }
467 }
66c36198 468
5e932bd5 469 opendir(DIR,"$Conf::dbdir/installed");
4b122800
MT
470 my @files = readdir(DIR);
471 closedir(DIR);
472 foreach $file (@files) {
473 next if ( $file eq "." );
474 next if ( $file eq ".." );
3f01107b 475 next if ( $file =~ /^old/ );
847df41d
RR
476 %metadata = parsemetafile("$Conf::dbdir/installed/$file");
477
4b122800
MT
478 foreach $prog (@db) {
479 @templine = split(/\;/,$prog);
847df41d
RR
480 if (("$metadata{'Name'}" eq "$templine[0]") && ("$metadata{'Release'}" < "$templine[2]" && "$forweb" ne "notice")) {
481 push(@updatepaks,$metadata{'Name'});
4b122800 482 if ("$forweb" eq "forweb") {
847df41d 483 print "<option value=\"$metadata{'Name'}\">Update: $metadata{'Name'} -- Version: $metadata{'ProgVersion'} -> $templine[1] -- Release: $metadata{'Release'} -> $templine[2]</option>\n";
4b122800 484 } else {
847df41d 485 my $command = "Update: $metadata{'Name'}\nVersion: $metadata{'ProgVersion'} -> $templine[1]\nRelease: $metadata{'Release'} -> $templine[2]\n";
e3670217
MT
486 if ("$Pakfire::enable_colors" eq "1") {
487 print "$color{'lila'}$command$color{'normal'}\n";
488 } else {
489 print "$command\n";
490 }
4b122800
MT
491 }
492 }
493 }
5b2a12ff 494 }
99e6df8e 495 return @updatepaks;
4b122800
MT
496 } else {
497 my $line;
e3670217 498 my $use_color;
4b122800 499 my @templine;
e3670217 500 my $count;
4b122800 501 foreach $line (sort @db) {
06209efc 502 next unless ($line =~ /.*;.*;.*;/ );
e3670217 503 $use_color = "";
4b122800
MT
504 @templine = split(/\;/,$line);
505 if ("$filter" eq "notinstalled") {
506 next if ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
507 } elsif ("$filter" eq "installed") {
508 next unless ( -e "$Conf::dbdir/installed/meta-$templine[0]" );
509 }
a06a552c 510 $count++;
4e17785f
AF
511 if ("$forweb" eq "forweb")
512 {
513 if ("$filter" eq "notinstalled") {
514 print "<option value=\"$templine[0]\">$templine[0]-$templine[1]-$templine[2]</option>\n";
515 } else {
516 print "<option value=\"$templine[0]\">$templine[0]</option>\n";
517 }
4b122800 518 } else {
e3670217
MT
519 if ("$Pakfire::enable_colors" eq "1") {
520 if (&isinstalled("$templine[0]")) {
66c36198 521 $use_color = "$color{'red'}"
e3670217
MT
522 } else {
523 $use_color = "$color{'green'}"
524 }
525 }
526 print "${use_color}Name: $templine[0]\nProgVersion: $templine[1]\nRelease: $templine[2]$color{'normal'}\n\n";
4b122800 527 }
1bd42c89 528 }
e3670217 529 print "$count packages total.\n" unless ("$forweb" eq "forweb");
1bd42c89
MT
530 }
531}
532
621dcd86 533sub resolvedeps_one {
1bd42c89 534 my $pak = shift;
66c36198 535
1bd42c89 536 getmetafile("$pak");
66c36198 537
35f38a8b 538 message("PAKFIRE RESV: $pak: Resolving dependencies...");
66c36198 539
847df41d
RR
540 my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak");
541 my @all;
542 my @deps = split(/ /, $metadata{'Dependencies'});
1bd42c89
MT
543 chomp (@deps);
544 foreach (@deps) {
545 if ($_) {
186e3d2c
MT
546 my $return = &isinstalled($_);
547 if ($return eq 0) {
35f38a8b 548 message("PAKFIRE RESV: $pak: Dependency is already installed: $_");
186e3d2c 549 } else {
35f38a8b 550 message("PAKFIRE RESV: $pak: Need to install dependency: $_");
186e3d2c 551 push(@all,$_);
66c36198 552 }
1bd42c89
MT
553 }
554 }
e44b26cf 555
621dcd86
MT
556 return @all;
557}
558
559sub resolvedeps {
560 my $pak = shift;
561 my @all;
562
563 # Resolve all not yet installed dependencies of $pak
564 my @deps = &resolvedeps_one($pak);
565 push(@all, @deps);
566
567 # For each dependency, we check if more dependencies exist
568 while (@deps) {
569 my $dep = pop(@deps);
570
571 my @subdeps = &resolvedeps_one($dep);
572 foreach my $subdep (@subdeps) {
573 # Skip the package we are currently resolving for
574 next if ($pak eq $subdep);
575
576 # If the package is not already to be installed,
577 # we add it to the list (@all) and check if it has
578 # more dependencies on its own.
579 unless (grep {$_ eq $subdep} @all) {
580 push(@deps, $subdep);
581 push(@all, $subdep);
1bd42c89
MT
582 }
583 }
584 }
621dcd86 585
186e3d2c 586 return @all;
1bd42c89
MT
587}
588
9f1f68f1 589sub resolvedeps_recursive {
031becc0 590 my @packages = @_;
9f1f68f1
MT
591 my @result = ();
592
593 foreach my $pkg (@packages) {
594 my @deps = &Pakfire::resolvedeps($pkg);
595
596 foreach my $dep (@deps) {
597 push(@result, $dep);
598 }
599 }
600
601 # Sort the result array and remove dupes
602 my %sort = map{ $_, 1 } @result;
603 @result = keys %sort;
604
605 return @result;
606}
607
1bd42c89
MT
608sub cleanup {
609 my $dir = shift;
610 my $path;
66c36198 611
35f38a8b 612 logger("CLEANUP: $dir");
66c36198 613
1bd42c89
MT
614 if ( "$dir" eq "meta" ) {
615 $path = "$Conf::dbdir/meta";
616 } elsif ( "$dir" eq "tmp" ) {
617 $path = "$Conf::tmpdir";
618 }
619 chdir("$path");
620 opendir(DIR,".");
621 my @files = readdir(DIR);
622 closedir(DIR);
623 foreach (@files) {
624 unless (($_ eq ".") || ($_ eq "..")) {
625 system("rm -rf $_");
626 }
627 }
628}
629
630sub getmetafile {
631 my $pak = shift;
66c36198 632
28593a79
CS
633 unless ( -e "$Conf::dbdir/meta/meta-$pak" ) {
634 fetchfile("meta/meta-$pak", "");
635 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
636 }
66c36198 637
28593a79 638 if ( -z "$Conf::dbdir/meta/meta-$pak" ) {
1bd42c89
MT
639 fetchfile("meta/meta-$pak", "");
640 move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak");
641 }
66c36198 642
1bd42c89
MT
643 open(FILE, "<$Conf::dbdir/meta/meta-$pak");
644 my @line = <FILE>;
645 close(FILE);
66c36198 646
1bd42c89
MT
647 open(FILE, ">$Conf::dbdir/meta/meta-$pak");
648 foreach (@line) {
649 my $string = $_;
650 $string =~ s/\r\n/\n/g;
651 print FILE $string;
652 }
653 close(FILE);
654 return 1;
655}
656
657sub getsize {
658 my $pak = shift;
66c36198 659
1bd42c89 660 getmetafile("$pak");
66c36198 661
847df41d
RR
662 if (my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak")) {
663 return $metadata{'Size'};
664 }
665 return 0;
666}
667
668sub parsemetafile {
669 ### This subroutine returns a hash with the contents of a meta- file
670 # Pass path to metafile as argument: Pakfire::parsemetafile("$Conf::dbdir/meta/meta-$pak")
671 # Usage is always with an argument.
672 my $metafile = shift;
673
674 my %metadata = ();
675
676 my @templine;
677 my @file;
678
679 if (! -e $metafile ) {
680 return 0;
681 }
682
683 open(FILE, "<$metafile");
684 @file = <FILE>;
1bd42c89 685 close(FILE);
66c36198 686
847df41d
RR
687 foreach (@file) {
688 @templine = split(/\: /,$_);
689 if ($templine[1]) {
1bd42c89 690 chomp($templine[1]);
847df41d 691 $metadata{"$templine[0]"} = $templine[1];
1bd42c89
MT
692 }
693 }
847df41d
RR
694
695 return %metadata;
1bd42c89
MT
696}
697
698sub decryptpak {
699 my $pak = shift;
66c36198 700
1bd42c89 701 cleanup("tmp");
66c36198 702
1bd42c89 703 my $file = getpak("$pak", "noforce");
66c36198 704
a6d327a7 705 logger("DECRYPT STARTED: $pak");
35f38a8b 706 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 707 $return %= 255;
a6d327a7 708 logger("DECRYPT FINISHED: $pak - Status: $return");
cde0e116 709 if ($return != 0) { exit 1; }
1bd42c89
MT
710}
711
712sub getpak {
713 my $pak = shift;
714 my $force = shift;
715
716 getmetafile("$pak");
66c36198 717
847df41d
RR
718 my %metadata = parsemetafile("$Conf::dbdir/meta/meta-$pak");
719 my $file = $metadata{'File'};
66c36198 720
1bd42c89 721 unless ($file) {
1af34aa8 722 message("No filename given in meta-file.");
1bd42c89
MT
723 exit 1;
724 }
66c36198 725
1bd42c89
MT
726 unless ( "$force" eq "force" ) {
727 if ( -e "$Conf::cachedir/$file" ) {
1bd42c89
MT
728 return $file;
729 }
730 }
66c36198 731
1bd42c89
MT
732 fetchfile("paks/$file", "");
733 return $file;
734}
735
736sub setuppak {
737 my $pak = shift;
66c36198 738
35f38a8b 739 message("PAKFIRE INST: $pak: Decrypting...");
1bd42c89 740 decryptpak("$pak");
66c36198 741
35f38a8b 742 message("PAKFIRE INST: $pak: Copying files and running post-installation scripts...");
99e6df8e 743 my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1");
cde0e116 744 $return %= 255;
1bd42c89
MT
745 if ($return == 0) {
746 move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak");
747 cleanup("tmp");
4d504812 748 copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/");
35f38a8b
MT
749 message("PAKFIRE INST: $pak: Finished.");
750 message("");
1bd42c89 751 } else {
35f38a8b 752 message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem.");
1bd42c89
MT
753 exit $return;
754 }
a08c3a2e 755 return $return;
1bd42c89
MT
756}
757
2aa6d448 758sub upgradecore {
377560fb 759 getcoredb("noforce");
2aa6d448 760 eval(`grep "core_" $Conf::dbdir/lists/core-list.db`);
518f894b 761 if ("$core_release" > "$Conf::core_mine") {
712425ff
AF
762 # Safety check for lazy testers:
763 # Before we upgrade to the latest release, we re-install the previous release
764 # to make sure that the tester has always been on the latest version.
765 my $tree = &get_tree();
766 $Conf::core_mine-- if ($tree eq "testing" || $tree eq "unstable");
767
2aa6d448 768 message("CORE UPGR: Upgrading from release $Conf::core_mine to $core_release");
66c36198 769
2aa6d448
MT
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 }
66c36198 777
2aa6d448
MT
778 foreach $release (@seq) {
779 chomp($release);
780 upgradepak("core-upgrade-$release");
781 }
66c36198 782
2aa6d448 783 system("echo $core_release > $Conf::coredir/mine");
66c36198 784
2aa6d448
MT
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;
66c36198 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);
66c36198 868
8e58bd37
MT
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;
66c36198 892 }
35f38a8b
MT
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
e6f4991b
MT
949sub get_arch() {
950 # Append architecture
951 my ($sysname, $nodename, $release, $version, $machine) = POSIX::uname();
952
b16ac3ba
AF
953 # We only support armv6l for 32 bit arm
954 if ($machine =~ m/armv[67]/) {
955 return "armv6l";
e6f4991b
MT
956 }
957
958 return $machine;
959}
960
152378c6
MT
961sub get_tree() {
962 # Return stable if nothing is set
963 return "stable" unless (defined $pakfiresettings{'TREE'});
964
965 return $pakfiresettings{'TREE'};
966}
967
e6f4991b
MT
968sub make_version() {
969 my $version = "";
970
971 # Open /etc/system-release
972 open(RELEASE, "</etc/system-release");
973 my $release = <RELEASE>;
974 close(RELEASE);
975
976 # Add the main relase
977 if ($release =~ m/IPFire ([\d\.]+)/) {
978 $version .= $1;
979 }
980
152378c6
MT
981 # Append suffix for tree
982 my $tree = &get_tree();
983 if ($tree eq "testing") {
984 $version .= ".1";
985 } elsif ($tree eq "unstable") {
986 $version .= ".2";
987 }
988
e6f4991b 989 # Append architecture
6cf219c4 990 $version .= "-" . &get_arch();
e6f4991b
MT
991
992 return $version;
993}
994
1bd42c89 9951;