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