]>
Commit | Line | Data |
---|---|---|
1bd42c89 | 1 | #!/usr/bin/perl -w |
70df8302 MT |
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 | ############################################################################### | |
1bd42c89 MT |
21 | |
22 | require "/opt/pakfire/etc/pakfire.conf"; | |
4b122800 | 23 | require "/var/ipfire/general-functions.pl"; |
1bd42c89 MT |
24 | |
25 | use File::Basename; | |
26 | use File::Copy; | |
27 | use LWP::UserAgent; | |
4d504812 | 28 | use HTTP::Response; |
a6d327a7 MT |
29 | use HTTP::Headers; |
30 | use HTTP::Message; | |
31 | use HTTP::Request; | |
1bd42c89 MT |
32 | use Net::Ping; |
33 | ||
34 | package Pakfire; | |
35 | ||
750c1528 MT |
36 | # GPG Keys |
37 | my $myid = "64D96617"; # Our own gpg-key paks@ipfire.org | |
38 | my $trustid = "65D0FD58"; # gpg-key of CaCert | |
39 | ||
35f38a8b MT |
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"; | |
750c1528 | 59 | our $enable_colors = 1; |
35f38a8b | 60 | |
a6d327a7 MT |
61 | my $final_data; |
62 | my $total_size; | |
63 | my $bfile; | |
64 | ||
4b122800 MT |
65 | my %pakfiresettings = (); |
66 | &General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings); | |
67 | ||
1bd42c89 MT |
68 | sub message { |
69 | my $message = shift; | |
35f38a8b | 70 | |
1bd42c89 | 71 | logger("$message"); |
750c1528 MT |
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 | } | |
35f38a8b MT |
88 | } |
89 | print "$message\n"; | |
90 | ||
1bd42c89 MT |
91 | } |
92 | ||
93 | sub logger { | |
94 | my $log = shift; | |
9ced24a8 | 95 | if ($log) { |
c506cad0 CS |
96 | #system("echo \"`date`: $log\" >> /var/log/pakfire.log"); |
97 | system("logger -t pakfire \"$log\""); | |
9ced24a8 | 98 | } |
1bd42c89 MT |
99 | } |
100 | ||
5b2a12ff | 101 | sub usage { |
750c1528 | 102 | &Pakfire::message("Usage: pakfire <install|remove> [options] <pak(s)>"); |
5b2a12ff | 103 | &Pakfire::message(" <update> - Contacts the servers for new lists of paks."); |
99e6df8e | 104 | &Pakfire::message(" <upgrade> - Installs the latest version of all paks."); |
5b2a12ff | 105 | &Pakfire::message(" <list> - Outputs a short list with all available paks."); |
99e6df8e | 106 | &Pakfire::message(""); |
750c1528 MT |
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(""); | |
5b2a12ff MT |
113 | exit 1; |
114 | } | |
115 | ||
1bd42c89 MT |
116 | sub pinghost { |
117 | my $host = shift; | |
118 | ||
119 | $p = Net::Ping->new(); | |
120 | if ($p->ping($host)) { | |
28593a79 | 121 | logger("PING INFO: $host is alive"); |
1bd42c89 MT |
122 | return 1; |
123 | } else { | |
a6d327a7 | 124 | logger("PING INFO: $host is unreachable"); |
1bd42c89 MT |
125 | return 0; |
126 | } | |
127 | $p->close(); | |
128 | } | |
129 | ||
130 | sub fetchfile { | |
4d504812 MT |
131 | my $getfile = shift; |
132 | my $gethost = shift; | |
377560fb MT |
133 | my (@server, $host, $proto, $file, $i); |
134 | my $allok = 0; | |
1bd42c89 MT |
135 | |
136 | use File::Basename; | |
4d504812 | 137 | $bfile = basename("$getfile"); |
377560fb MT |
138 | |
139 | logger("DOWNLOAD STARTED: $getfile") unless ($bfile =~ /^counter\?.*/); | |
1bd42c89 | 140 | |
4d504812 MT |
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"; | |
1bd42c89 | 150 | } else { |
4d504812 | 151 | $host = $gethost; |
afabe9f7 | 152 | $file = $getfile; |
1bd42c89 | 153 | } |
4d504812 MT |
154 | |
155 | $proto = "HTTP" unless $proto; | |
156 | ||
a6d327a7 MT |
157 | unless ($bfile =~ /^counter\?.*/) { |
158 | logger("DOWNLOAD INFO: Host: $host ($proto) - File: $file"); | |
a6d327a7 | 159 | } |
1bd42c89 | 160 | |
4d504812 MT |
161 | my $ua = LWP::UserAgent->new; |
162 | $ua->agent("Pakfire/$Conf::version"); | |
3d3b68c5 | 163 | $ua->timeout(20); |
4b122800 MT |
164 | |
165 | my %proxysettings=(); | |
166 | &General::readhash("${General::swroot}/proxy/advanced/settings", \%proxysettings); | |
167 | ||
99e6df8e | 168 | if ($proxysettings{'UPSTREAM_PROXY'}) { |
4c7fa778 | 169 | logger("DOWNLOAD INFO: Upstream proxy: \"$proxysettings{'UPSTREAM_PROXY'}\"") unless ($bfile =~ /^counter.py\?.*/); |
4b122800 | 170 | if ($proxysettings{'UPSTREAM_USER'}) { |
99e6df8e | 171 | $ua->proxy("http","http://$proxysettings{'UPSTREAM_USER'}:$proxysettings{'UPSTREAM_PASSWORD'}@"."$proxysettings{'UPSTREAM_PROXY'}/"); |
4c7fa778 | 172 | logger("DOWNLOAD INFO: Logging in with: \"$proxysettings{'UPSTREAM_USER'}\" - \"$proxysettings{'UPSTREAM_PASSWORD'}\"") unless ($bfile =~ /^counter.py\?.*/); |
4b122800 | 173 | } else { |
99e6df8e | 174 | $ua->proxy("http","http://$proxysettings{'UPSTREAM_PROXY'}/"); |
4b122800 MT |
175 | } |
176 | } | |
a6d327a7 MT |
177 | |
178 | $final_data = undef; | |
179 | my $url = "http://$host/$file"; | |
180 | my $response; | |
181 | ||
4c7fa778 | 182 | unless ($bfile =~ /^counter.py\?.*/) { |
a6d327a7 MT |
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 | } | |
4d504812 | 193 | |
4b122800 MT |
194 | my $code = $response->code(); |
195 | my $log = $response->status_line; | |
a6d327a7 | 196 | logger("DOWNLOAD INFO: HTTP-Status-Code: $code - $log"); |
4b122800 MT |
197 | |
198 | if ( $code eq "500" ) { | |
e44b26cf | 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."); |
4b122800 MT |
200 | return 1; |
201 | } | |
202 | ||
4d504812 | 203 | if ($response->is_success) { |
4c7fa778 | 204 | unless ($bfile =~ /^counter.py\?.*/) { |
35f38a8b MT |
205 | if (open(FILE, ">$Conf::tmpdir/$bfile")) { |
206 | print FILE $final_data; | |
207 | close(FILE); | |
a6d327a7 | 208 | logger("DOWNLOAD INFO: File received. Start checking signature..."); |
06209efc | 209 | if (system("gpg --verify \"$Conf::tmpdir/$bfile\" &>/dev/null") eq 0) { |
a6d327a7 | 210 | logger("DOWNLOAD INFO: Signature of $bfile is fine."); |
06209efc MT |
211 | move("$Conf::tmpdir/$bfile","$Conf::cachedir/$bfile"); |
212 | } else { | |
a6d327a7 | 213 | message("DOWNLOAD ERROR: The downloaded file ($file) wasn't verified by IPFire.org. Sorry - Exiting..."); |
28593a79 CS |
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.");} | |
06209efc MT |
217 | exit 1; |
218 | } | |
35f38a8b MT |
219 | logger("DOWNLOAD FINISHED: $file"); |
220 | $allok = 1; | |
221 | return 0; | |
222 | } else { | |
223 | logger("DOWNLOAD ERROR: Could not open $Conf::cachedir/$bfile for writing."); | |
186e3d2c | 224 | } |
4d504812 | 225 | } else { |
35f38a8b | 226 | return 0; |
4d504812 MT |
227 | } |
228 | } else { | |
a6d327a7 | 229 | logger("DOWNLOAD ERROR: $log"); |
4d504812 | 230 | } |
1bd42c89 | 231 | } |
a6d327a7 | 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."); |
4d504812 | 233 | return 1; |
1bd42c89 MT |
234 | } |
235 | ||
236 | sub getmirrors { | |
377560fb MT |
237 | my $force = shift; |
238 | my $age; | |
239 | ||
1bd42c89 | 240 | use File::Copy; |
1bd42c89 | 241 | |
2aa6d448 MT |
242 | if ( -e "$Conf::dbdir/lists/server-list.db" ) { |
243 | my @stat = stat("$Conf::dbdir/lists/server-list.db"); | |
e3670217 MT |
244 | my $time = time(); |
245 | $age = $time - $stat[9]; | |
377560fb MT |
246 | $force = "force" if ("$age" >= "3600"); |
247 | logger("MIRROR INFO: server-list.db is $age seconds old. - DEBUG: $force"); | |
e3670217 MT |
248 | } else { |
249 | # Force an update. | |
377560fb | 250 | $force = "force"; |
e3670217 MT |
251 | } |
252 | ||
377560fb | 253 | if ("$force" eq "force") { |
e3670217 MT |
254 | fetchfile("$Conf::version/lists/server-list.db", "$Conf::mainserver"); |
255 | move("$Conf::cachedir/server-list.db", "$Conf::dbdir/lists/server-list.db"); | |
256 | } | |
1bd42c89 MT |
257 | } |
258 | ||
2aa6d448 | 259 | sub getcoredb { |
377560fb MT |
260 | my $force = shift; |
261 | my $age; | |
262 | ||
2aa6d448 | 263 | use File::Copy; |
2aa6d448 MT |
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]; | |
377560fb MT |
269 | $force = "force" if ("$age" >= "3600"); |
270 | logger("CORE INFO: core-list.db is $age seconds old. - DEBUG: $force"); | |
2aa6d448 MT |
271 | } else { |
272 | # Force an update. | |
377560fb | 273 | $force = "force"; |
2aa6d448 MT |
274 | } |
275 | ||
377560fb | 276 | if ("$force" eq "force") { |
2aa6d448 MT |
277 | fetchfile("lists/core-list.db", ""); |
278 | move("$Conf::cachedir/core-list.db", "$Conf::dbdir/lists/core-list.db"); | |
279 | } | |
280 | } | |
281 | ||
282 | ||
1bd42c89 MT |
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; | |
3d3b68c5 | 287 | while (!(open(FILE, "<$Conf::dbdir/lists/server-list.db")) && ($count lt 5)) { |
1bd42c89 | 288 | $count++; |
377560fb | 289 | getmirrors("noforce"); |
1bd42c89 | 290 | } |
3d3b68c5 | 291 | if ($count == 5) { |
a6d327a7 | 292 | message("MIRROR ERROR: Could not find or download a server list"); |
1bd42c89 MT |
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; | |
e44b26cf | 300 | my @newlines; |
1bd42c89 | 301 | foreach (@lines) { |
e44b26cf MT |
302 | if ("$_" =~ /.*;.*;.*;/ ) { |
303 | push(@newlines,$_); | |
304 | $scount++; | |
305 | } | |
1bd42c89 | 306 | } |
a6d327a7 | 307 | logger("MIRROR INFO: $scount servers found in list"); |
1bd42c89 MT |
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; | |
e4e4b3f9 | 314 | my $pingdelay = 1; |
1bd42c89 MT |
315 | while ($found == 0) { |
316 | $server = int(rand($scount) + 1); | |
317 | $servers = 0; | |
318 | my ($line, $proto, $path, $host); | |
319 | my @templine; | |
e44b26cf | 320 | foreach $line (@newlines) { |
1bd42c89 MT |
321 | $servers++; |
322 | if ($servers eq $server) { | |
323 | @templine = split(/\;/, $line); | |
324 | $proto = $templine[0]; | |
325 | $host = $templine[1]; | |
326 | $path = $templine[2]; | |
a08c3a2e | 327 | if (pinghost("$host")) { |
1bd42c89 MT |
328 | $found = 1; |
329 | return ($proto, $host, $path); | |
330 | } | |
e4e4b3f9 AF |
331 | if ($found == 0) { |
332 | sleep($pingdelay); | |
333 | $pingdelay=$pingdelay*2; | |
334 | if ($pingdelay>1200) { | |
335 | $pingdelay=1200; | |
336 | } | |
337 | } | |
1bd42c89 MT |
338 | } |
339 | } | |
4d504812 | 340 | } |
1bd42c89 MT |
341 | } |
342 | ||
343 | sub dbgetlist { | |
344 | ### Update the database if the file is older than one day. | |
345 | # If you pass &Pakfire::dbgetlist(force) the list will be downloaded. | |
346 | # Usage is always with an argument. | |
347 | my $force = shift; | |
348 | my $age; | |
349 | ||
350 | use File::Copy; | |
351 | ||
352 | if ( -e "$Conf::dbdir/lists/packages_list.db" ) { | |
353 | my @stat = stat("$Conf::dbdir/lists/packages_list.db"); | |
354 | my $time = time(); | |
355 | $age = $time - $stat[9]; | |
377560fb MT |
356 | $force = "force" if ("$age" >= "3600"); |
357 | logger("DB INFO: packages_list.db is $age seconds old. - DEBUG: $force"); | |
1bd42c89 MT |
358 | } else { |
359 | # Force an update. | |
377560fb | 360 | $force = "force"; |
1bd42c89 MT |
361 | } |
362 | ||
377560fb | 363 | if ("$force" eq "force") { |
1bd42c89 MT |
364 | fetchfile("lists/packages_list.db", ""); |
365 | move("$Conf::cachedir/packages_list.db", "$Conf::dbdir/lists/packages_list.db"); | |
366 | } | |
367 | } | |
368 | ||
369 | sub dblist { | |
370 | ### This subroutine lists the packages. | |
371 | # You may also pass a filter: &Pakfire::dblist(filter) | |
372 | # Usage is always with two arguments. | |
373 | # filter may be: all, notinstalled, installed | |
374 | my $filter = shift; | |
375 | my $forweb = shift; | |
4b122800 | 376 | my @meta; |
99e6df8e | 377 | my @updatepaks; |
4b122800 MT |
378 | my $file; |
379 | my $line; | |
380 | my $prog; | |
381 | my ($name, $version, $release); | |
382 | my @templine; | |
1bd42c89 MT |
383 | |
384 | ### Make sure that the list is not outdated. | |
377560fb | 385 | #dbgetlist("noforce"); |
1bd42c89 MT |
386 | |
387 | open(FILE, "<$Conf::dbdir/lists/packages_list.db"); | |
388 | my @db = <FILE>; | |
389 | close(FILE); | |
4b122800 MT |
390 | |
391 | if ("$filter" eq "upgrade") { | |
d1d1cb42 | 392 | if ("$forweb" ne "forweb") {getcoredb("noforce");} |
8a8a50ad | 393 | eval(`grep "core_" $Conf::dbdir/lists/core-list.db`); |
518f894b | 394 | if ("$core_release" > "$Conf::core_mine") { |
8a8a50ad MT |
395 | if ("$forweb" eq "forweb") { |
396 | print "<option value=\"core\">Core-Update -- $Conf::version -- Release: $Conf::core_mine -> $core_release</option>\n"; | |
397 | } else { | |
398 | my $command = "Core-Update $Conf::version\nRelease: $Conf::core_mine -> $core_release\n"; | |
399 | if ("$Pakfire::enable_colors" eq "1") { | |
400 | print "$color{'lila'}$command$color{'normal'}\n"; | |
401 | } else { | |
402 | print "$command\n"; | |
403 | } | |
404 | } | |
405 | } | |
406 | ||
4b122800 MT |
407 | opendir(DIR,"$Conf::dbdir/meta"); |
408 | my @files = readdir(DIR); | |
409 | closedir(DIR); | |
410 | foreach $file (@files) { | |
411 | next if ( $file eq "." ); | |
412 | next if ( $file eq ".." ); | |
413 | open(FILE, "<$Conf::dbdir/meta/$file"); | |
414 | @meta = <FILE>; | |
415 | close(FILE); | |
416 | foreach $line (@meta) { | |
417 | @templine = split(/\: /,$line); | |
418 | if ("$templine[0]" eq "Name") { | |
419 | $name = $templine[1]; | |
420 | chomp($name); | |
421 | } elsif ("$templine[0]" eq "ProgVersion") { | |
422 | $version = $templine[1]; | |
423 | chomp($version); | |
424 | } elsif ("$templine[0]" eq "Release") { | |
425 | $release = $templine[1]; | |
426 | chomp($release); | |
427 | } | |
428 | } | |
429 | foreach $prog (@db) { | |
430 | @templine = split(/\;/,$prog); | |
431 | if (("$name" eq "$templine[0]") && ("$release" < "$templine[2]" )) { | |
99e6df8e | 432 | push(@updatepaks,$name); |
4b122800 MT |
433 | if ("$forweb" eq "forweb") { |
434 | print "<option value=\"$name\">Update: $name -- Version: $version -> $templine[1] -- Release: $release -> $templine[2]</option>\n"; | |
435 | } else { | |
e3670217 MT |
436 | my $command = "Update: $name\nVersion: $version -> $templine[1]\nRelease: $release -> $templine[2]\n"; |
437 | if ("$Pakfire::enable_colors" eq "1") { | |
438 | print "$color{'lila'}$command$color{'normal'}\n"; | |
439 | } else { | |
440 | print "$command\n"; | |
441 | } | |
4b122800 MT |
442 | } |
443 | } | |
444 | } | |
5b2a12ff | 445 | } |
99e6df8e | 446 | return @updatepaks; |
4b122800 MT |
447 | } else { |
448 | my $line; | |
e3670217 | 449 | my $use_color; |
4b122800 | 450 | my @templine; |
e3670217 | 451 | my $count; |
4b122800 | 452 | foreach $line (sort @db) { |
06209efc | 453 | next unless ($line =~ /.*;.*;.*;/ ); |
e3670217 MT |
454 | $use_color = ""; |
455 | $count++; | |
4b122800 MT |
456 | @templine = split(/\;/,$line); |
457 | if ("$filter" eq "notinstalled") { | |
458 | next if ( -e "$Conf::dbdir/installed/meta-$templine[0]" ); | |
459 | } elsif ("$filter" eq "installed") { | |
460 | next unless ( -e "$Conf::dbdir/installed/meta-$templine[0]" ); | |
461 | } | |
462 | if ("$forweb" eq "forweb") { | |
463 | print "<option value=\"$templine[0]\">$templine[0]-$templine[1]-$templine[2]</option>\n"; | |
464 | } else { | |
e3670217 MT |
465 | if ("$Pakfire::enable_colors" eq "1") { |
466 | if (&isinstalled("$templine[0]")) { | |
467 | $use_color = "$color{'red'}" | |
468 | } else { | |
469 | $use_color = "$color{'green'}" | |
470 | } | |
471 | } | |
472 | print "${use_color}Name: $templine[0]\nProgVersion: $templine[1]\nRelease: $templine[2]$color{'normal'}\n\n"; | |
4b122800 | 473 | } |
1bd42c89 | 474 | } |
e3670217 | 475 | print "$count packages total.\n" unless ("$forweb" eq "forweb"); |
1bd42c89 MT |
476 | } |
477 | } | |
478 | ||
479 | sub resolvedeps { | |
480 | my $pak = shift; | |
481 | ||
482 | getmetafile("$pak"); | |
483 | ||
35f38a8b | 484 | message("PAKFIRE RESV: $pak: Resolving dependencies..."); |
1bd42c89 MT |
485 | |
486 | open(FILE, "<$Conf::dbdir/meta/meta-$pak"); | |
487 | my @file = <FILE>; | |
488 | close(FILE); | |
489 | ||
490 | my $line; | |
186e3d2c | 491 | my (@templine, @deps, @tempdeps, @all); |
1bd42c89 MT |
492 | foreach $line (@file) { |
493 | @templine = split(/\: /,$line); | |
494 | if ("$templine[0]" eq "Dependencies") { | |
495 | @deps = split(/ /, $templine[1]); | |
496 | } | |
497 | } | |
498 | chomp (@deps); | |
499 | foreach (@deps) { | |
500 | if ($_) { | |
186e3d2c MT |
501 | my $return = &isinstalled($_); |
502 | if ($return eq 0) { | |
35f38a8b | 503 | message("PAKFIRE RESV: $pak: Dependency is already installed: $_"); |
186e3d2c | 504 | } else { |
35f38a8b | 505 | message("PAKFIRE RESV: $pak: Need to install dependency: $_"); |
186e3d2c MT |
506 | push(@tempdeps,$_); |
507 | push(@all,$_); | |
508 | } | |
1bd42c89 MT |
509 | } |
510 | } | |
e44b26cf | 511 | |
1bd42c89 MT |
512 | foreach (@tempdeps) { |
513 | if ($_) { | |
514 | my @newdeps = resolvedeps("$_"); | |
515 | foreach(@newdeps) { | |
516 | unless (($_ eq " ") || ($_ eq "")) { | |
186e3d2c MT |
517 | my $return = &isinstalled($_); |
518 | if ($return eq 0) { | |
35f38a8b | 519 | message("PAKFIRE RESV: $pak: Dependency is already installed: $_"); |
186e3d2c | 520 | } else { |
35f38a8b | 521 | message("PAKFIRE RESV: $pak: Need to install dependency: $_"); |
186e3d2c MT |
522 | push(@all,$_); |
523 | } | |
1bd42c89 MT |
524 | } |
525 | } | |
526 | } | |
527 | } | |
35f38a8b | 528 | message(""); |
186e3d2c MT |
529 | chomp (@all); |
530 | return @all; | |
1bd42c89 MT |
531 | } |
532 | ||
533 | sub cleanup { | |
534 | my $dir = shift; | |
535 | my $path; | |
536 | ||
35f38a8b MT |
537 | logger("CLEANUP: $dir"); |
538 | ||
1bd42c89 MT |
539 | if ( "$dir" eq "meta" ) { |
540 | $path = "$Conf::dbdir/meta"; | |
541 | } elsif ( "$dir" eq "tmp" ) { | |
542 | $path = "$Conf::tmpdir"; | |
543 | } | |
544 | chdir("$path"); | |
545 | opendir(DIR,"."); | |
546 | my @files = readdir(DIR); | |
547 | closedir(DIR); | |
548 | foreach (@files) { | |
549 | unless (($_ eq ".") || ($_ eq "..")) { | |
550 | system("rm -rf $_"); | |
551 | } | |
552 | } | |
553 | } | |
554 | ||
555 | sub getmetafile { | |
556 | my $pak = shift; | |
557 | ||
28593a79 CS |
558 | unless ( -e "$Conf::dbdir/meta/meta-$pak" ) { |
559 | fetchfile("meta/meta-$pak", ""); | |
560 | move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak"); | |
561 | } | |
562 | ||
563 | if ( -z "$Conf::dbdir/meta/meta-$pak" ) { | |
1bd42c89 MT |
564 | fetchfile("meta/meta-$pak", ""); |
565 | move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak"); | |
566 | } | |
567 | ||
568 | open(FILE, "<$Conf::dbdir/meta/meta-$pak"); | |
569 | my @line = <FILE>; | |
570 | close(FILE); | |
571 | ||
572 | open(FILE, ">$Conf::dbdir/meta/meta-$pak"); | |
573 | foreach (@line) { | |
574 | my $string = $_; | |
575 | $string =~ s/\r\n/\n/g; | |
576 | print FILE $string; | |
577 | } | |
578 | close(FILE); | |
579 | return 1; | |
580 | } | |
581 | ||
582 | sub getsize { | |
583 | my $pak = shift; | |
584 | ||
585 | getmetafile("$pak"); | |
586 | ||
587 | open(FILE, "<$Conf::dbdir/meta/meta-$pak"); | |
588 | my @file = <FILE>; | |
589 | close(FILE); | |
590 | ||
591 | my $line; | |
592 | my @templine; | |
593 | foreach $line (@file) { | |
594 | @templine = split(/\: /,$line); | |
595 | if ("$templine[0]" eq "Size") { | |
596 | chomp($templine[1]); | |
597 | return $templine[1]; | |
598 | } | |
599 | } | |
4b122800 | 600 | return 0; |
1bd42c89 MT |
601 | } |
602 | ||
603 | sub decryptpak { | |
604 | my $pak = shift; | |
605 | ||
606 | cleanup("tmp"); | |
607 | ||
608 | my $file = getpak("$pak", "noforce"); | |
609 | ||
a6d327a7 | 610 | logger("DECRYPT STARTED: $pak"); |
35f38a8b | 611 | 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 | 612 | $return %= 255; |
a6d327a7 | 613 | logger("DECRYPT FINISHED: $pak - Status: $return"); |
cde0e116 | 614 | if ($return != 0) { exit 1; } |
1bd42c89 MT |
615 | } |
616 | ||
617 | sub getpak { | |
618 | my $pak = shift; | |
619 | my $force = shift; | |
620 | ||
621 | getmetafile("$pak"); | |
622 | ||
623 | open(FILE, "<$Conf::dbdir/meta/meta-$pak"); | |
624 | my @file = <FILE>; | |
625 | close(FILE); | |
626 | ||
627 | my $line; | |
628 | my $file; | |
629 | my @templine; | |
630 | foreach $line (@file) { | |
631 | @templine = split(/\: /,$line); | |
632 | if ("$templine[0]" eq "File") { | |
633 | chomp($templine[1]); | |
634 | $file = $templine[1]; | |
635 | } | |
636 | } | |
637 | ||
638 | unless ($file) { | |
639 | message("No filename given in meta-file. Please phone the developers."); | |
640 | exit 1; | |
641 | } | |
642 | ||
1bd42c89 MT |
643 | unless ( "$force" eq "force" ) { |
644 | if ( -e "$Conf::cachedir/$file" ) { | |
1bd42c89 MT |
645 | return $file; |
646 | } | |
647 | } | |
648 | ||
649 | fetchfile("paks/$file", ""); | |
650 | return $file; | |
651 | } | |
652 | ||
653 | sub setuppak { | |
654 | my $pak = shift; | |
655 | ||
35f38a8b | 656 | message("PAKFIRE INST: $pak: Decrypting..."); |
1bd42c89 MT |
657 | decryptpak("$pak"); |
658 | ||
35f38a8b | 659 | message("PAKFIRE INST: $pak: Copying files and running post-installation scripts..."); |
99e6df8e | 660 | my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1"); |
cde0e116 | 661 | $return %= 255; |
e44b26cf | 662 | if ($pakfiresettings{'UUID'} ne "off") { |
4c7fa778 | 663 | fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&ipak=$pak&return=$return", "$Conf::mainserver"); |
e44b26cf | 664 | } |
1bd42c89 MT |
665 | if ($return == 0) { |
666 | move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak"); | |
667 | cleanup("tmp"); | |
4d504812 | 668 | copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/"); |
35f38a8b MT |
669 | message("PAKFIRE INST: $pak: Finished."); |
670 | message(""); | |
1bd42c89 | 671 | } else { |
35f38a8b | 672 | message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem."); |
1bd42c89 MT |
673 | exit $return; |
674 | } | |
a08c3a2e | 675 | return $return; |
1bd42c89 MT |
676 | } |
677 | ||
2aa6d448 | 678 | sub upgradecore { |
377560fb | 679 | getcoredb("noforce"); |
2aa6d448 | 680 | eval(`grep "core_" $Conf::dbdir/lists/core-list.db`); |
518f894b | 681 | if ("$core_release" > "$Conf::core_mine") { |
2aa6d448 MT |
682 | message("CORE UPGR: Upgrading from release $Conf::core_mine to $core_release"); |
683 | ||
684 | my @seq = `seq $Conf::core_mine $core_release`; | |
685 | shift @seq; | |
686 | my $release; | |
687 | foreach $release (@seq) { | |
688 | chomp($release); | |
689 | getpak("core-upgrade-$release"); | |
690 | } | |
691 | ||
692 | foreach $release (@seq) { | |
693 | chomp($release); | |
694 | upgradepak("core-upgrade-$release"); | |
695 | } | |
696 | ||
697 | system("echo $core_release > $Conf::coredir/mine"); | |
698 | ||
699 | } else { | |
700 | message("CORE ERROR: No new upgrades available. You are on release $Conf::core_mine."); | |
701 | } | |
702 | } | |
703 | ||
186e3d2c MT |
704 | sub isinstalled { |
705 | my $pak = shift; | |
706 | if ( open(FILE,"<$Conf::dbdir/installed/meta-$pak") ) { | |
707 | close(FILE); | |
708 | return 0; | |
709 | } else { | |
710 | return 1; | |
711 | } | |
712 | } | |
713 | ||
99e6df8e | 714 | sub upgradepak { |
1bd42c89 MT |
715 | my $pak = shift; |
716 | ||
35f38a8b | 717 | message("PAKFIRE UPGR: $pak: Decrypting..."); |
1bd42c89 MT |
718 | decryptpak("$pak"); |
719 | ||
35f38a8b | 720 | message("PAKFIRE UPGR: $pak: Upgrading files and running post-upgrading scripts..."); |
99e6df8e MT |
721 | my $return = system("cd $Conf::tmpdir && NAME=$pak ./update.sh >> $Conf::logdir/update-$pak.log 2>&1"); |
722 | $return %= 255; | |
e44b26cf | 723 | if ($pakfiresettings{'UUID'} ne "off") { |
4c7fa778 | 724 | fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&upak=$pak&return=$return", "$Conf::mainserver"); |
e44b26cf | 725 | } |
1bd42c89 MT |
726 | if ($return == 0) { |
727 | move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak"); | |
728 | cleanup("tmp"); | |
4e4b54c5 | 729 | copy("$Conf::dbdir/meta/meta-$pak", "$Conf::dbdir/installed/"); |
35f38a8b MT |
730 | message("PAKFIRE UPGR: $pak: Finished."); |
731 | message(""); | |
1bd42c89 | 732 | } else { |
35f38a8b | 733 | message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem."); |
1bd42c89 MT |
734 | exit $return; |
735 | } | |
8e58bd37 | 736 | return $return; |
1bd42c89 MT |
737 | } |
738 | ||
739 | sub removepak { | |
740 | my $pak = shift; | |
741 | ||
35f38a8b | 742 | message("PAKFIRE REMV: $pak: Decrypting..."); |
1bd42c89 MT |
743 | decryptpak("$pak"); |
744 | ||
35f38a8b | 745 | message("PAKFIRE REMV: $pak: Removing files and running post-removing scripts..."); |
99e6df8e MT |
746 | my $return = system("cd $Conf::tmpdir && NAME=$pak ./uninstall.sh >> $Conf::logdir/uninstall-$pak.log 2>&1"); |
747 | $return %= 255; | |
e44b26cf | 748 | if ($pakfiresettings{'UUID'} ne "off") { |
4c7fa778 | 749 | fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&dpak=$pak&return=$return", "$Conf::mainserver"); |
e44b26cf | 750 | } |
1bd42c89 | 751 | if ($return == 0) { |
1bd42c89 | 752 | unlink("$Conf::dbdir/rootfiles/$pak"); |
a6d327a7 | 753 | unlink("$Conf::dbdir/installed/meta-$pak"); |
1bd42c89 | 754 | cleanup("tmp"); |
35f38a8b MT |
755 | message("PAKFIRE REMV: $pak: Finished."); |
756 | message(""); | |
1bd42c89 | 757 | } else { |
35f38a8b | 758 | message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem."); |
1bd42c89 MT |
759 | exit $return; |
760 | } | |
8e58bd37 | 761 | return $return; |
1bd42c89 MT |
762 | } |
763 | ||
764 | sub beautifysize { | |
765 | my $size = shift; | |
35f38a8b | 766 | #$size = $size / 1024; |
a08c3a2e | 767 | my $unit; |
1bd42c89 | 768 | |
35f38a8b MT |
769 | if ($size > 1023*1024) { |
770 | $size = ($size / (1024*1024)); | |
a08c3a2e | 771 | $unit = "MB"; |
35f38a8b MT |
772 | } elsif ($size > 1023) { |
773 | $size = ($size / 1024); | |
a08c3a2e | 774 | $unit = "KB"; |
35f38a8b MT |
775 | } else { |
776 | $unit = "B"; | |
1bd42c89 | 777 | } |
a08c3a2e MT |
778 | $size = sprintf("%.2f" , $size); |
779 | my $string = "$size $unit"; | |
780 | return $string; | |
1bd42c89 MT |
781 | } |
782 | ||
8e58bd37 MT |
783 | sub makeuuid { |
784 | unless ( -e "$Conf::dbdir/uuid" ) { | |
8e58bd37 MT |
785 | open(FILE, "</proc/sys/kernel/random/uuid"); |
786 | my @line = <FILE>; | |
787 | close(FILE); | |
788 | ||
789 | open(FILE, ">$Conf::dbdir/uuid"); | |
790 | foreach (@line) { | |
791 | print FILE $_; | |
792 | } | |
793 | close(FILE); | |
794 | } | |
795 | } | |
796 | ||
797 | sub senduuid { | |
99e6df8e | 798 | if ($pakfiresettings{'UUID'} ne "off") { |
4b122800 MT |
799 | unless("$Conf::uuid") { |
800 | $Conf::uuid = `cat $Conf::dbdir/uuid`; | |
801 | } | |
802 | logger("Sending my uuid: $Conf::uuid"); | |
4c7fa778 | 803 | fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid", "$Conf::mainserver"); |
06209efc | 804 | system("rm -f $Conf::tmpdir/counter* 2>/dev/null"); |
8e58bd37 | 805 | } |
8e58bd37 | 806 | } |
1bd42c89 | 807 | |
cde0e116 | 808 | sub checkcryptodb { |
a6d327a7 | 809 | logger("CRYPTO INFO: Checking GnuPG Database"); |
cde0e116 MT |
810 | my $ret = system("gpg --list-keys | grep -q $myid"); |
811 | unless ( "$ret" eq "0" ) { | |
a6d327a7 MT |
812 | message("CRYPTO WARN: The GnuPG isn't configured corectly. Trying now to fix this."); |
813 | message("CRYPTO WARN: It's normal to see this on first execution."); | |
a4c9c660 | 814 | my $command = "gpg --keyserver pgp.mit.edu --always-trust --status-fd 2"; |
ca385da6 MT |
815 | system("$command --recv-key $myid >> $Conf::logdir/gnupg-database.log 2>&1"); |
816 | system("$command --recv-key $trustid >> $Conf::logdir/gnupg-database.log 2>&1"); | |
a6d327a7 MT |
817 | } else { |
818 | logger("CRYPTO INFO: Database is okay"); | |
cde0e116 MT |
819 | } |
820 | } | |
821 | ||
a6d327a7 MT |
822 | sub callback { |
823 | my ($data, $response, $protocol) = @_; | |
824 | $final_data .= $data; | |
35f38a8b | 825 | print progress_bar( length($final_data), $total_size, 30, '=' ); |
a6d327a7 MT |
826 | } |
827 | ||
828 | sub progress_bar { | |
829 | my ( $got, $total, $width, $char ) = @_; | |
830 | my $show_bfile; | |
35f38a8b | 831 | $width ||= 30; $char ||= '='; |
a6d327a7 | 832 | my $len_bfile = length $bfile; |
35f38a8b MT |
833 | if ("$len_bfile" >= "17") { |
834 | $show_bfile = substr($bfile,0,17)."..."; | |
a6d327a7 MT |
835 | } else { |
836 | $show_bfile = $bfile; | |
35f38a8b MT |
837 | } |
838 | $progress = sprintf("%.2f%%", 100*$got/+$total); | |
839 | sprintf "$color{'lightgreen'}%-20s %7s |%-${width}s| %10s$color{'normal'}\r",$show_bfile, $progress, $char x (($width-1)*$got/$total). '>', beautifysize($got); | |
a6d327a7 MT |
840 | } |
841 | ||
1bd42c89 | 842 | 1; |