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