]>
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 | ||
91bc7814 | 119 | $p = Net::Ping->new("icmp"); |
1bd42c89 | 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 { | |
4a979780 | 223 | logger("DOWNLOAD ERROR: Could not open $Conf::tmpdir/$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"); |
63efc01c MT |
308 | |
309 | if ($scount eq 0) { | |
310 | logger("MIRROR INFO: Could not find any servers. Falling back to main server $Conf::mainserver"); | |
311 | return ("HTTP", $Conf::mainserver, "/$Conf::version"); | |
312 | } | |
313 | ||
1bd42c89 MT |
314 | ### Choose a random server and test if it is online |
315 | # If the check fails try a new server. | |
316 | # This will never give up. | |
317 | my $found = 0; | |
318 | my $servers = 0; | |
e4e4b3f9 | 319 | my $pingdelay = 1; |
1bd42c89 MT |
320 | while ($found == 0) { |
321 | $server = int(rand($scount) + 1); | |
322 | $servers = 0; | |
323 | my ($line, $proto, $path, $host); | |
324 | my @templine; | |
e44b26cf | 325 | foreach $line (@newlines) { |
1bd42c89 MT |
326 | $servers++; |
327 | if ($servers eq $server) { | |
328 | @templine = split(/\;/, $line); | |
329 | $proto = $templine[0]; | |
330 | $host = $templine[1]; | |
331 | $path = $templine[2]; | |
d59b02f1 CS |
332 | if ($pakfiresettings{'HEALTHCHECK'} eq "off") { |
333 | logger("PING INFO: Healthcheck is disabled"); | |
334 | $found = 1; | |
335 | return ($proto, $host, $path); | |
984e2723 | 336 | } |
d59b02f1 | 337 | elsif (pinghost("$host")) { |
1bd42c89 MT |
338 | $found = 1; |
339 | return ($proto, $host, $path); | |
340 | } | |
e4e4b3f9 AF |
341 | if ($found == 0) { |
342 | sleep($pingdelay); | |
343 | $pingdelay=$pingdelay*2; | |
344 | if ($pingdelay>1200) { | |
345 | $pingdelay=1200; | |
346 | } | |
347 | } | |
1bd42c89 MT |
348 | } |
349 | } | |
4d504812 | 350 | } |
1bd42c89 MT |
351 | } |
352 | ||
353 | sub dbgetlist { | |
354 | ### Update the database if the file is older than one day. | |
355 | # If you pass &Pakfire::dbgetlist(force) the list will be downloaded. | |
356 | # Usage is always with an argument. | |
357 | my $force = shift; | |
358 | my $age; | |
359 | ||
360 | use File::Copy; | |
361 | ||
362 | if ( -e "$Conf::dbdir/lists/packages_list.db" ) { | |
363 | my @stat = stat("$Conf::dbdir/lists/packages_list.db"); | |
364 | my $time = time(); | |
365 | $age = $time - $stat[9]; | |
377560fb MT |
366 | $force = "force" if ("$age" >= "3600"); |
367 | logger("DB INFO: packages_list.db is $age seconds old. - DEBUG: $force"); | |
1bd42c89 MT |
368 | } else { |
369 | # Force an update. | |
377560fb | 370 | $force = "force"; |
1bd42c89 MT |
371 | } |
372 | ||
377560fb | 373 | if ("$force" eq "force") { |
1bd42c89 MT |
374 | fetchfile("lists/packages_list.db", ""); |
375 | move("$Conf::cachedir/packages_list.db", "$Conf::dbdir/lists/packages_list.db"); | |
376 | } | |
ff9545f8 AF |
377 | |
378 | # Update the meta database if new packages was in the package list | |
379 | my @meta; | |
380 | my $file; | |
381 | my $line; | |
382 | my $prog; | |
383 | my ($name, $version, $release); | |
384 | my @templine; | |
385 | ||
386 | open(FILE, "<$Conf::dbdir/lists/packages_list.db"); | |
387 | my @db = <FILE>; | |
388 | close(FILE); | |
389 | ||
390 | opendir(DIR,"$Conf::dbdir/meta"); | |
391 | my @files = readdir(DIR); | |
392 | closedir(DIR); | |
393 | foreach $file (@files) { | |
394 | next if ( $file eq "." ); | |
395 | next if ( $file eq ".." ); | |
396 | next if ( $file =~ /^old/ ); | |
397 | open(FILE, "<$Conf::dbdir/meta/$file"); | |
398 | @meta = <FILE>; | |
399 | close(FILE); | |
400 | foreach $line (@meta) { | |
401 | @templine = split(/\: /,$line); | |
402 | if ("$templine[0]" eq "Name") { | |
403 | $name = $templine[1]; | |
404 | chomp($name); | |
405 | } elsif ("$templine[0]" eq "ProgVersion") { | |
406 | $version = $templine[1]; | |
407 | chomp($version); | |
408 | } elsif ("$templine[0]" eq "Release") { | |
409 | $release = $templine[1]; | |
410 | chomp($release); | |
411 | } | |
412 | } | |
413 | foreach $prog (@db) { | |
414 | @templine = split(/\;/,$prog); | |
415 | if (("$name" eq "$templine[0]") && ("$release" ne "$templine[2]")) { | |
416 | move("$Conf::dbdir/meta/meta-$name","$Conf::dbdir/meta/old_meta-$name"); | |
417 | fetchfile("meta/meta-$name", ""); | |
418 | move("$Conf::cachedir/meta-$name", "$Conf::dbdir/meta/meta-$name"); | |
419 | } | |
420 | } | |
421 | } | |
1bd42c89 MT |
422 | } |
423 | ||
424 | sub dblist { | |
425 | ### This subroutine lists the packages. | |
426 | # You may also pass a filter: &Pakfire::dblist(filter) | |
427 | # Usage is always with two arguments. | |
428 | # filter may be: all, notinstalled, installed | |
429 | my $filter = shift; | |
430 | my $forweb = shift; | |
4b122800 | 431 | my @meta; |
99e6df8e | 432 | my @updatepaks; |
4b122800 MT |
433 | my $file; |
434 | my $line; | |
435 | my $prog; | |
436 | my ($name, $version, $release); | |
437 | my @templine; | |
1bd42c89 MT |
438 | |
439 | ### Make sure that the list is not outdated. | |
377560fb | 440 | #dbgetlist("noforce"); |
1bd42c89 MT |
441 | |
442 | open(FILE, "<$Conf::dbdir/lists/packages_list.db"); | |
443 | my @db = <FILE>; | |
444 | close(FILE); | |
4b122800 MT |
445 | |
446 | if ("$filter" eq "upgrade") { | |
4638fd92 | 447 | if ("$forweb" ne "forweb" && "$forweb" ne "notice" ) {getcoredb("noforce");} |
8a8a50ad | 448 | eval(`grep "core_" $Conf::dbdir/lists/core-list.db`); |
518f894b | 449 | if ("$core_release" > "$Conf::core_mine") { |
8a8a50ad MT |
450 | if ("$forweb" eq "forweb") { |
451 | print "<option value=\"core\">Core-Update -- $Conf::version -- Release: $Conf::core_mine -> $core_release</option>\n"; | |
2b38ab5c JPT |
452 | } |
453 | elsif ("$forweb" eq "notice") { | |
462a5e86 | 454 | print "<br /><br /><br /><a href='pakfire.cgi'>$Lang::tr{'core notice 1'} $Conf::core_mine $Lang::tr{'core notice 2'} $core_release $Lang::tr{'core notice 3'}</a>"; |
8a8a50ad MT |
455 | } else { |
456 | my $command = "Core-Update $Conf::version\nRelease: $Conf::core_mine -> $core_release\n"; | |
457 | if ("$Pakfire::enable_colors" eq "1") { | |
458 | print "$color{'lila'}$command$color{'normal'}\n"; | |
459 | } else { | |
460 | print "$command\n"; | |
461 | } | |
462 | } | |
463 | } | |
464 | ||
5e932bd5 | 465 | opendir(DIR,"$Conf::dbdir/installed"); |
4b122800 MT |
466 | my @files = readdir(DIR); |
467 | closedir(DIR); | |
468 | foreach $file (@files) { | |
469 | next if ( $file eq "." ); | |
470 | next if ( $file eq ".." ); | |
3f01107b | 471 | next if ( $file =~ /^old/ ); |
5e932bd5 | 472 | open(FILE, "<$Conf::dbdir/installed/$file"); |
4b122800 MT |
473 | @meta = <FILE>; |
474 | close(FILE); | |
475 | foreach $line (@meta) { | |
476 | @templine = split(/\: /,$line); | |
477 | if ("$templine[0]" eq "Name") { | |
478 | $name = $templine[1]; | |
479 | chomp($name); | |
480 | } elsif ("$templine[0]" eq "ProgVersion") { | |
481 | $version = $templine[1]; | |
482 | chomp($version); | |
483 | } elsif ("$templine[0]" eq "Release") { | |
484 | $release = $templine[1]; | |
485 | chomp($release); | |
486 | } | |
487 | } | |
488 | foreach $prog (@db) { | |
489 | @templine = split(/\;/,$prog); | |
462a5e86 | 490 | if (("$name" eq "$templine[0]") && ("$release" < "$templine[2]" && "$forweb" ne "notice")) { |
99e6df8e | 491 | push(@updatepaks,$name); |
4b122800 MT |
492 | if ("$forweb" eq "forweb") { |
493 | print "<option value=\"$name\">Update: $name -- Version: $version -> $templine[1] -- Release: $release -> $templine[2]</option>\n"; | |
494 | } else { | |
e3670217 MT |
495 | my $command = "Update: $name\nVersion: $version -> $templine[1]\nRelease: $release -> $templine[2]\n"; |
496 | if ("$Pakfire::enable_colors" eq "1") { | |
497 | print "$color{'lila'}$command$color{'normal'}\n"; | |
498 | } else { | |
499 | print "$command\n"; | |
500 | } | |
4b122800 MT |
501 | } |
502 | } | |
503 | } | |
5b2a12ff | 504 | } |
99e6df8e | 505 | return @updatepaks; |
4b122800 MT |
506 | } else { |
507 | my $line; | |
e3670217 | 508 | my $use_color; |
4b122800 | 509 | my @templine; |
e3670217 | 510 | my $count; |
4b122800 | 511 | foreach $line (sort @db) { |
06209efc | 512 | next unless ($line =~ /.*;.*;.*;/ ); |
e3670217 MT |
513 | $use_color = ""; |
514 | $count++; | |
4b122800 MT |
515 | @templine = split(/\;/,$line); |
516 | if ("$filter" eq "notinstalled") { | |
517 | next if ( -e "$Conf::dbdir/installed/meta-$templine[0]" ); | |
518 | } elsif ("$filter" eq "installed") { | |
519 | next unless ( -e "$Conf::dbdir/installed/meta-$templine[0]" ); | |
520 | } | |
521 | if ("$forweb" eq "forweb") { | |
522 | print "<option value=\"$templine[0]\">$templine[0]-$templine[1]-$templine[2]</option>\n"; | |
523 | } else { | |
e3670217 MT |
524 | if ("$Pakfire::enable_colors" eq "1") { |
525 | if (&isinstalled("$templine[0]")) { | |
526 | $use_color = "$color{'red'}" | |
527 | } else { | |
528 | $use_color = "$color{'green'}" | |
529 | } | |
530 | } | |
531 | print "${use_color}Name: $templine[0]\nProgVersion: $templine[1]\nRelease: $templine[2]$color{'normal'}\n\n"; | |
4b122800 | 532 | } |
1bd42c89 | 533 | } |
e3670217 | 534 | print "$count packages total.\n" unless ("$forweb" eq "forweb"); |
1bd42c89 MT |
535 | } |
536 | } | |
537 | ||
538 | sub resolvedeps { | |
539 | my $pak = shift; | |
540 | ||
541 | getmetafile("$pak"); | |
542 | ||
35f38a8b | 543 | message("PAKFIRE RESV: $pak: Resolving dependencies..."); |
1bd42c89 MT |
544 | |
545 | open(FILE, "<$Conf::dbdir/meta/meta-$pak"); | |
546 | my @file = <FILE>; | |
547 | close(FILE); | |
548 | ||
549 | my $line; | |
186e3d2c | 550 | my (@templine, @deps, @tempdeps, @all); |
1bd42c89 MT |
551 | foreach $line (@file) { |
552 | @templine = split(/\: /,$line); | |
553 | if ("$templine[0]" eq "Dependencies") { | |
554 | @deps = split(/ /, $templine[1]); | |
555 | } | |
556 | } | |
557 | chomp (@deps); | |
558 | foreach (@deps) { | |
559 | if ($_) { | |
186e3d2c MT |
560 | my $return = &isinstalled($_); |
561 | if ($return eq 0) { | |
35f38a8b | 562 | message("PAKFIRE RESV: $pak: Dependency is already installed: $_"); |
186e3d2c | 563 | } else { |
35f38a8b | 564 | message("PAKFIRE RESV: $pak: Need to install dependency: $_"); |
186e3d2c MT |
565 | push(@tempdeps,$_); |
566 | push(@all,$_); | |
567 | } | |
1bd42c89 MT |
568 | } |
569 | } | |
e44b26cf | 570 | |
1bd42c89 MT |
571 | foreach (@tempdeps) { |
572 | if ($_) { | |
573 | my @newdeps = resolvedeps("$_"); | |
574 | foreach(@newdeps) { | |
575 | unless (($_ eq " ") || ($_ eq "")) { | |
186e3d2c MT |
576 | my $return = &isinstalled($_); |
577 | if ($return eq 0) { | |
35f38a8b | 578 | message("PAKFIRE RESV: $pak: Dependency is already installed: $_"); |
186e3d2c | 579 | } else { |
35f38a8b | 580 | message("PAKFIRE RESV: $pak: Need to install dependency: $_"); |
186e3d2c MT |
581 | push(@all,$_); |
582 | } | |
1bd42c89 MT |
583 | } |
584 | } | |
585 | } | |
586 | } | |
35f38a8b | 587 | message(""); |
186e3d2c MT |
588 | chomp (@all); |
589 | return @all; | |
1bd42c89 MT |
590 | } |
591 | ||
592 | sub cleanup { | |
593 | my $dir = shift; | |
594 | my $path; | |
595 | ||
35f38a8b MT |
596 | logger("CLEANUP: $dir"); |
597 | ||
1bd42c89 MT |
598 | if ( "$dir" eq "meta" ) { |
599 | $path = "$Conf::dbdir/meta"; | |
600 | } elsif ( "$dir" eq "tmp" ) { | |
601 | $path = "$Conf::tmpdir"; | |
602 | } | |
603 | chdir("$path"); | |
604 | opendir(DIR,"."); | |
605 | my @files = readdir(DIR); | |
606 | closedir(DIR); | |
607 | foreach (@files) { | |
608 | unless (($_ eq ".") || ($_ eq "..")) { | |
609 | system("rm -rf $_"); | |
610 | } | |
611 | } | |
612 | } | |
613 | ||
614 | sub getmetafile { | |
615 | my $pak = shift; | |
616 | ||
28593a79 CS |
617 | unless ( -e "$Conf::dbdir/meta/meta-$pak" ) { |
618 | fetchfile("meta/meta-$pak", ""); | |
619 | move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak"); | |
620 | } | |
621 | ||
622 | if ( -z "$Conf::dbdir/meta/meta-$pak" ) { | |
1bd42c89 MT |
623 | fetchfile("meta/meta-$pak", ""); |
624 | move("$Conf::cachedir/meta-$pak", "$Conf::dbdir/meta/meta-$pak"); | |
625 | } | |
626 | ||
627 | open(FILE, "<$Conf::dbdir/meta/meta-$pak"); | |
628 | my @line = <FILE>; | |
629 | close(FILE); | |
630 | ||
631 | open(FILE, ">$Conf::dbdir/meta/meta-$pak"); | |
632 | foreach (@line) { | |
633 | my $string = $_; | |
634 | $string =~ s/\r\n/\n/g; | |
635 | print FILE $string; | |
636 | } | |
637 | close(FILE); | |
638 | return 1; | |
639 | } | |
640 | ||
641 | sub getsize { | |
642 | my $pak = shift; | |
643 | ||
644 | getmetafile("$pak"); | |
645 | ||
646 | open(FILE, "<$Conf::dbdir/meta/meta-$pak"); | |
647 | my @file = <FILE>; | |
648 | close(FILE); | |
649 | ||
650 | my $line; | |
651 | my @templine; | |
652 | foreach $line (@file) { | |
653 | @templine = split(/\: /,$line); | |
654 | if ("$templine[0]" eq "Size") { | |
655 | chomp($templine[1]); | |
656 | return $templine[1]; | |
657 | } | |
658 | } | |
4b122800 | 659 | return 0; |
1bd42c89 MT |
660 | } |
661 | ||
662 | sub decryptpak { | |
663 | my $pak = shift; | |
664 | ||
665 | cleanup("tmp"); | |
666 | ||
667 | my $file = getpak("$pak", "noforce"); | |
668 | ||
a6d327a7 | 669 | logger("DECRYPT STARTED: $pak"); |
35f38a8b | 670 | 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 | 671 | $return %= 255; |
a6d327a7 | 672 | logger("DECRYPT FINISHED: $pak - Status: $return"); |
cde0e116 | 673 | if ($return != 0) { exit 1; } |
1bd42c89 MT |
674 | } |
675 | ||
676 | sub getpak { | |
677 | my $pak = shift; | |
678 | my $force = shift; | |
679 | ||
680 | getmetafile("$pak"); | |
681 | ||
682 | open(FILE, "<$Conf::dbdir/meta/meta-$pak"); | |
683 | my @file = <FILE>; | |
684 | close(FILE); | |
685 | ||
686 | my $line; | |
687 | my $file; | |
688 | my @templine; | |
689 | foreach $line (@file) { | |
690 | @templine = split(/\: /,$line); | |
691 | if ("$templine[0]" eq "File") { | |
692 | chomp($templine[1]); | |
693 | $file = $templine[1]; | |
694 | } | |
695 | } | |
696 | ||
697 | unless ($file) { | |
698 | message("No filename given in meta-file. Please phone the developers."); | |
699 | exit 1; | |
700 | } | |
701 | ||
1bd42c89 MT |
702 | unless ( "$force" eq "force" ) { |
703 | if ( -e "$Conf::cachedir/$file" ) { | |
1bd42c89 MT |
704 | return $file; |
705 | } | |
706 | } | |
707 | ||
708 | fetchfile("paks/$file", ""); | |
709 | return $file; | |
710 | } | |
711 | ||
712 | sub setuppak { | |
713 | my $pak = shift; | |
714 | ||
35f38a8b | 715 | message("PAKFIRE INST: $pak: Decrypting..."); |
1bd42c89 MT |
716 | decryptpak("$pak"); |
717 | ||
35f38a8b | 718 | message("PAKFIRE INST: $pak: Copying files and running post-installation scripts..."); |
99e6df8e | 719 | my $return = system("cd $Conf::tmpdir && NAME=$pak ./install.sh >> $Conf::logdir/install-$pak.log 2>&1"); |
cde0e116 | 720 | $return %= 255; |
e44b26cf | 721 | if ($pakfiresettings{'UUID'} ne "off") { |
4c7fa778 | 722 | fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&ipak=$pak&return=$return", "$Conf::mainserver"); |
e44b26cf | 723 | } |
1bd42c89 MT |
724 | if ($return == 0) { |
725 | move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak"); | |
726 | cleanup("tmp"); | |
4d504812 | 727 | copy("$Conf::dbdir/meta/meta-$pak","$Conf::dbdir/installed/"); |
35f38a8b MT |
728 | message("PAKFIRE INST: $pak: Finished."); |
729 | message(""); | |
1bd42c89 | 730 | } else { |
35f38a8b | 731 | message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem."); |
1bd42c89 MT |
732 | exit $return; |
733 | } | |
a08c3a2e | 734 | return $return; |
1bd42c89 MT |
735 | } |
736 | ||
2aa6d448 | 737 | sub upgradecore { |
377560fb | 738 | getcoredb("noforce"); |
2aa6d448 | 739 | eval(`grep "core_" $Conf::dbdir/lists/core-list.db`); |
518f894b | 740 | if ("$core_release" > "$Conf::core_mine") { |
2aa6d448 MT |
741 | message("CORE UPGR: Upgrading from release $Conf::core_mine to $core_release"); |
742 | ||
743 | my @seq = `seq $Conf::core_mine $core_release`; | |
744 | shift @seq; | |
745 | my $release; | |
746 | foreach $release (@seq) { | |
747 | chomp($release); | |
748 | getpak("core-upgrade-$release"); | |
749 | } | |
750 | ||
751 | foreach $release (@seq) { | |
752 | chomp($release); | |
753 | upgradepak("core-upgrade-$release"); | |
754 | } | |
755 | ||
756 | system("echo $core_release > $Conf::coredir/mine"); | |
757 | ||
758 | } else { | |
759 | message("CORE ERROR: No new upgrades available. You are on release $Conf::core_mine."); | |
760 | } | |
761 | } | |
762 | ||
186e3d2c MT |
763 | sub isinstalled { |
764 | my $pak = shift; | |
765 | if ( open(FILE,"<$Conf::dbdir/installed/meta-$pak") ) { | |
766 | close(FILE); | |
767 | return 0; | |
768 | } else { | |
769 | return 1; | |
770 | } | |
771 | } | |
772 | ||
99e6df8e | 773 | sub upgradepak { |
1bd42c89 MT |
774 | my $pak = shift; |
775 | ||
35f38a8b | 776 | message("PAKFIRE UPGR: $pak: Decrypting..."); |
1bd42c89 MT |
777 | decryptpak("$pak"); |
778 | ||
35f38a8b | 779 | message("PAKFIRE UPGR: $pak: Upgrading files and running post-upgrading scripts..."); |
99e6df8e MT |
780 | my $return = system("cd $Conf::tmpdir && NAME=$pak ./update.sh >> $Conf::logdir/update-$pak.log 2>&1"); |
781 | $return %= 255; | |
e44b26cf | 782 | if ($pakfiresettings{'UUID'} ne "off") { |
4c7fa778 | 783 | fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&upak=$pak&return=$return", "$Conf::mainserver"); |
e44b26cf | 784 | } |
1bd42c89 MT |
785 | if ($return == 0) { |
786 | move("$Conf::tmpdir/ROOTFILES", "$Conf::dbdir/rootfiles/$pak"); | |
787 | cleanup("tmp"); | |
4e4b54c5 | 788 | copy("$Conf::dbdir/meta/meta-$pak", "$Conf::dbdir/installed/"); |
35f38a8b MT |
789 | message("PAKFIRE UPGR: $pak: Finished."); |
790 | message(""); | |
1bd42c89 | 791 | } else { |
35f38a8b | 792 | message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem."); |
1bd42c89 MT |
793 | exit $return; |
794 | } | |
8e58bd37 | 795 | return $return; |
1bd42c89 MT |
796 | } |
797 | ||
798 | sub removepak { | |
799 | my $pak = shift; | |
800 | ||
35f38a8b | 801 | message("PAKFIRE REMV: $pak: Decrypting..."); |
1bd42c89 MT |
802 | decryptpak("$pak"); |
803 | ||
35f38a8b | 804 | message("PAKFIRE REMV: $pak: Removing files and running post-removing scripts..."); |
99e6df8e MT |
805 | my $return = system("cd $Conf::tmpdir && NAME=$pak ./uninstall.sh >> $Conf::logdir/uninstall-$pak.log 2>&1"); |
806 | $return %= 255; | |
e44b26cf | 807 | if ($pakfiresettings{'UUID'} ne "off") { |
4c7fa778 | 808 | fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid&dpak=$pak&return=$return", "$Conf::mainserver"); |
e44b26cf | 809 | } |
1bd42c89 | 810 | if ($return == 0) { |
1bd42c89 | 811 | unlink("$Conf::dbdir/rootfiles/$pak"); |
a6d327a7 | 812 | unlink("$Conf::dbdir/installed/meta-$pak"); |
1bd42c89 | 813 | cleanup("tmp"); |
35f38a8b MT |
814 | message("PAKFIRE REMV: $pak: Finished."); |
815 | message(""); | |
1bd42c89 | 816 | } else { |
35f38a8b | 817 | message("PAKFIRE ERROR: Returncode: $return. Sorry. Please search our forum to find a solution for this problem."); |
1bd42c89 MT |
818 | exit $return; |
819 | } | |
8e58bd37 | 820 | return $return; |
1bd42c89 MT |
821 | } |
822 | ||
823 | sub beautifysize { | |
824 | my $size = shift; | |
35f38a8b | 825 | #$size = $size / 1024; |
a08c3a2e | 826 | my $unit; |
1bd42c89 | 827 | |
35f38a8b MT |
828 | if ($size > 1023*1024) { |
829 | $size = ($size / (1024*1024)); | |
a08c3a2e | 830 | $unit = "MB"; |
35f38a8b MT |
831 | } elsif ($size > 1023) { |
832 | $size = ($size / 1024); | |
a08c3a2e | 833 | $unit = "KB"; |
35f38a8b MT |
834 | } else { |
835 | $unit = "B"; | |
1bd42c89 | 836 | } |
a08c3a2e MT |
837 | $size = sprintf("%.2f" , $size); |
838 | my $string = "$size $unit"; | |
839 | return $string; | |
1bd42c89 MT |
840 | } |
841 | ||
8e58bd37 MT |
842 | sub makeuuid { |
843 | unless ( -e "$Conf::dbdir/uuid" ) { | |
8e58bd37 MT |
844 | open(FILE, "</proc/sys/kernel/random/uuid"); |
845 | my @line = <FILE>; | |
846 | close(FILE); | |
847 | ||
848 | open(FILE, ">$Conf::dbdir/uuid"); | |
849 | foreach (@line) { | |
850 | print FILE $_; | |
851 | } | |
852 | close(FILE); | |
853 | } | |
854 | } | |
855 | ||
856 | sub senduuid { | |
99e6df8e | 857 | if ($pakfiresettings{'UUID'} ne "off") { |
4b122800 MT |
858 | unless("$Conf::uuid") { |
859 | $Conf::uuid = `cat $Conf::dbdir/uuid`; | |
860 | } | |
861 | logger("Sending my uuid: $Conf::uuid"); | |
4c7fa778 | 862 | fetchfile("counter.py?ver=$Conf::version&uuid=$Conf::uuid", "$Conf::mainserver"); |
06209efc | 863 | system("rm -f $Conf::tmpdir/counter* 2>/dev/null"); |
8e58bd37 | 864 | } |
8e58bd37 | 865 | } |
1bd42c89 | 866 | |
cde0e116 | 867 | sub checkcryptodb { |
a6d327a7 | 868 | logger("CRYPTO INFO: Checking GnuPG Database"); |
cde0e116 MT |
869 | my $ret = system("gpg --list-keys | grep -q $myid"); |
870 | unless ( "$ret" eq "0" ) { | |
a6d327a7 MT |
871 | message("CRYPTO WARN: The GnuPG isn't configured corectly. Trying now to fix this."); |
872 | message("CRYPTO WARN: It's normal to see this on first execution."); | |
ccbe39e9 | 873 | my $command = "gpg --keyserver pgp.ipfire.org --always-trust --status-fd 2"; |
ca385da6 MT |
874 | system("$command --recv-key $myid >> $Conf::logdir/gnupg-database.log 2>&1"); |
875 | system("$command --recv-key $trustid >> $Conf::logdir/gnupg-database.log 2>&1"); | |
a6d327a7 MT |
876 | } else { |
877 | logger("CRYPTO INFO: Database is okay"); | |
cde0e116 MT |
878 | } |
879 | } | |
880 | ||
a6d327a7 MT |
881 | sub callback { |
882 | my ($data, $response, $protocol) = @_; | |
883 | $final_data .= $data; | |
35f38a8b | 884 | print progress_bar( length($final_data), $total_size, 30, '=' ); |
a6d327a7 MT |
885 | } |
886 | ||
887 | sub progress_bar { | |
888 | my ( $got, $total, $width, $char ) = @_; | |
889 | my $show_bfile; | |
35f38a8b | 890 | $width ||= 30; $char ||= '='; |
a6d327a7 | 891 | my $len_bfile = length $bfile; |
35f38a8b MT |
892 | if ("$len_bfile" >= "17") { |
893 | $show_bfile = substr($bfile,0,17)."..."; | |
a6d327a7 MT |
894 | } else { |
895 | $show_bfile = $bfile; | |
35f38a8b MT |
896 | } |
897 | $progress = sprintf("%.2f%%", 100*$got/+$total); | |
898 | sprintf "$color{'lightgreen'}%-20s %7s |%-${width}s| %10s$color{'normal'}\r",$show_bfile, $progress, $char x (($width-1)*$got/$total). '>', beautifysize($got); | |
a6d327a7 MT |
899 | } |
900 | ||
1bd42c89 | 901 | 1; |