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