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