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