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