]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/pakfire/lib/functions.pl
3 require "/opt/pakfire/etc/pakfire.conf" ;
4 require "/var/ipfire/general-functions.pl" ;
18 my $myid = "64D96617" ; # Our own gpg-key paks@ipfire.org
19 my $trustid = "65D0FD58" ; # gpg-key of CaCert
21 # A small color-hash :D
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" ;
40 our $enable_colors = 1 ;
46 my %pakfiresettings = ();
47 & General
:: readhash
( "${General::swroot}/pakfire/settings" , \
%pakfiresettings );
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'}" ;
77 system ( "echo \" `date`: $log \" >> /var/log/pakfire.log" );
78 #system("logger -t pakfire \"$log\"");
83 & Pakfire
:: message
( "Usage: pakfire <install|remove> [options] <pak(s)>" );
84 & Pakfire
:: message
( " <update> - Contacts the servers for new lists of paks." );
85 & Pakfire
:: message
( " <upgrade> - Installs the latest version of all paks." );
86 & Pakfire
:: message
( " <list> - Outputs a short list with all available paks." );
87 & Pakfire
:: message
( "" );
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
( "" );
100 $p = Net
:: Ping
-> new ();
101 if ( $p -> ping ( $host )) {
102 logger
( "PING INFO: $host is alive" );
105 logger
( "PING INFO: $host is unreachable" );
114 my ( @server , $host , $proto , $file , $allok , $i );
116 logger
( "DOWNLOAD STARTED: $getfile " ) unless ( $bfile =~ /^counter\?.*/ );
118 $bfile = basename
( " $getfile " );
121 while (( $allok == 0 ) && $i < 5 ) {
124 if ( " $gethost " eq "" ) {
125 @server = selectmirror
();
128 $file = " $server [2]/ $getfile " ;
134 $proto = "HTTP" unless $proto ;
136 unless ( $bfile =~ /^counter\?.*/ ) {
137 logger
( "DOWNLOAD INFO: Host: $host ( $proto ) - File: $file " );
140 my $ua = LWP
:: UserAgent
-> new ;
141 $ua -> agent ( "Pakfire/ $Conf ::version" );
144 my %proxysettings =();
145 & General
:: readhash
( "${General::swroot}/proxy/advanced/settings" , \
%proxysettings );
147 if ( $proxysettings { 'UPSTREAM_PROXY' }) {
148 logger
( "DOWNLOAD INFO: Upstream proxy: \" $proxysettings {'UPSTREAM_PROXY'} \" " ) unless ( $bfile =~ /^counter\?.*/ );
149 if ( $proxysettings { 'UPSTREAM_USER' }) {
150 $ua -> proxy ( "http" , "http:// $proxysettings {'UPSTREAM_USER'}: $proxysettings {'UPSTREAM_PASSWORD'}@" . " $proxysettings {'UPSTREAM_PROXY'}/" );
151 logger
( "DOWNLOAD INFO: Logging in with: \" $proxysettings {'UPSTREAM_USER'} \" - \" $proxysettings {'UPSTREAM_PASSWORD'} \" " ) unless ( $bfile =~ /^counter\?.*/ );
153 $ua -> proxy ( "http" , "http:// $proxysettings {'UPSTREAM_PROXY'}/" );
158 my $url = "http:// $host / $file " ;
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" );
167 $response = $ua -> get ( $url , ':content_cb' => \
& callback
);
170 $response = $ua -> get ( $url );
173 my $code = $response -> code ();
174 my $log = $response -> status_line ;
175 logger
( "DOWNLOAD INFO: HTTP-Status-Code: $code - $log " );
177 if ( $code eq "500" ) {
178 message
( "Giving up: There was no chance to get the file \" $getfile \" from any available server. \n There was an error on the way. Please fix it." );
182 if ( $response -> is_success ) {
183 unless ( $bfile =~ /^counter\?.*/ ) {
184 if ( open ( FILE
, "> $Conf ::tmpdir/ $bfile " )) {
185 print FILE
$final_data ;
187 logger
( "DOWNLOAD INFO: File received. Start checking signature..." );
188 if ( system ( "gpg --verify \" $Conf ::tmpdir/ $bfile \" &>/dev/null" ) eq 0 ) {
189 logger
( "DOWNLOAD INFO: Signature of $bfile is fine." );
190 move
( " $Conf ::tmpdir/ $bfile " , " $Conf ::cachedir/ $bfile " );
192 message
( "DOWNLOAD ERROR: The downloaded file ( $file ) wasn't verified by IPFire.org. Sorry - Exiting..." );
195 logger
( "DOWNLOAD FINISHED: $file " );
199 logger
( "DOWNLOAD ERROR: Could not open $Conf ::cachedir/ $bfile for writing." );
205 logger
( "DOWNLOAD ERROR: $log " );
208 message
( "DOWNLOAD ERROR: There was no chance to get the file \" $getfile \" from any available server. \n May be you should run \" pakfire update \" to get some new servers." );
215 logger
( "MIRROR: Trying to get a mirror list." );
217 if ( - e
" $Conf ::dbdir/lists/server_list.db" ) {
218 my @stat = stat ( " $Conf ::dbdir/lists/server_list.db" );
220 $age = $time - $stat [ 9 ];
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" );
233 ### Check if there is a current server list and read it.
234 # If there is no list try to get one.
236 while (!( open ( FILE
, "< $Conf ::dbdir/lists/server-list.db" )) && ( $count lt 5 )) {
241 message
( "MIRROR ERROR: Could not find or download a server list" );
247 ### Count the number of the servers in the list
251 if ( " $_ " =~ /.*;.*;.*;/ ) {
256 logger
( "MIRROR INFO: $scount servers found in list" );
258 ### Choose a random server and test if it is online
259 # If the check fails try a new server.
260 # This will never give up.
263 while ( $found == 0 ) {
264 $server = int ( rand ( $scount ) + 1 );
266 my ( $line , $proto , $path , $host );
268 foreach $line ( @newlines ) {
270 if ( $servers eq $server ) {
271 @templine = split ( /\;/ , $line );
272 $proto = $templine [ 0 ];
273 $host = $templine [ 1 ];
274 $path = $templine [ 2 ];
275 if ( pinghost
( " $host " )) {
277 return ( $proto , $host , $path );
285 ### Update the database if the file is older than one day.
286 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
287 # Usage is always with an argument.
293 if ( - e
" $Conf ::dbdir/lists/packages_list.db" ) {
294 my @stat = stat ( " $Conf ::dbdir/lists/packages_list.db" );
296 $age = $time - $stat [ 9 ];
302 if (( " $age " gt "86400" ) || ( " $force " eq "force" )) {
303 fetchfile
( "lists/packages_list.db" , "" );
304 move
( " $Conf ::cachedir/packages_list.db" , " $Conf ::dbdir/lists/packages_list.db" );
309 ### This subroutine lists the packages.
310 # You may also pass a filter: &Pakfire::dblist(filter)
311 # Usage is always with two arguments.
312 # filter may be: all, notinstalled, installed
320 my ( $name , $version , $release );
323 ### Make sure that the list is not outdated.
324 dbgetlist
( "noforce" );
326 open ( FILE
, "< $Conf ::dbdir/lists/packages_list.db" );
330 if ( " $filter " eq "upgrade" ) {
331 opendir ( DIR
, " $Conf ::dbdir/meta" );
332 my @files = readdir ( DIR
);
334 foreach $file ( @files ) {
335 next if ( $file eq "." );
336 next if ( $file eq ".." );
337 open ( FILE
, "< $Conf ::dbdir/meta/ $file " );
340 foreach $line ( @meta ) {
341 @templine = split ( /\: / , $line );
342 if ( " $templine [0]" eq "Name" ) {
343 $name = $templine [ 1 ];
345 } elsif ( " $templine [0]" eq "ProgVersion" ) {
346 $version = $templine [ 1 ];
348 } elsif ( " $templine [0]" eq "Release" ) {
349 $release = $templine [ 1 ];
353 foreach $prog ( @db ) {
354 @templine = split ( /\;/ , $prog );
355 if (( " $name " eq " $templine [0]" ) && ( " $release " < " $templine [2]" )) {
356 push ( @updatepaks , $name );
357 if ( " $forweb " eq "forweb" ) {
358 print "<option value= \" $name \" >Update: $name -- Version: $version -> $templine [1] -- Release: $release -> $templine [2]</option> \n " ;
360 my $command = "Update: $name \n Version: $version -> $templine [1] \n Release: $release -> $templine [2] \n " ;
361 if ( " $Pakfire ::enable_colors" eq "1" ) {
362 print " $color {'lila'} $command $color {'normal'} \n " ;
376 foreach $line ( sort @db ) {
377 next unless ( $line =~ /.*;.*;.*;/ );
380 @templine = split ( /\;/ , $line );
381 if ( " $filter " eq "notinstalled" ) {
382 next if ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
383 } elsif ( " $filter " eq "installed" ) {
384 next unless ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
386 if ( " $forweb " eq "forweb" ) {
387 print "<option value= \" $templine [0] \" > $templine [0]- $templine [1]- $templine [2]</option> \n " ;
389 if ( " $Pakfire ::enable_colors" eq "1" ) {
390 if (& isinstalled
( " $templine [0]" )) {
391 $use_color = " $color {'red'}"
393 $use_color = " $color {'green'}"
396 print "${use_color}Name: $templine [0] \n ProgVersion: $templine [1] \n Release: $templine [2] $color {'normal'} \n\n " ;
399 print " $count packages total. \n " unless ( " $forweb " eq "forweb" );
408 message
( "PAKFIRE RESV: $pak : Resolving dependencies..." );
410 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
415 my ( @templine , @deps , @tempdeps , @all );
416 foreach $line ( @file ) {
417 @templine = split ( /\: / , $line );
418 if ( " $templine [0]" eq "Dependencies" ) {
419 @deps = split ( / / , $templine [ 1 ]);
425 my $return = & isinstalled
( $_ );
427 message
( "PAKFIRE RESV: $pak : Dependency is already installed: $_ " );
429 message
( "PAKFIRE RESV: $pak : Need to install dependency: $_ " );
436 foreach ( @tempdeps ) {
438 my @newdeps = resolvedeps
( " $_ " );
440 unless (( $_ eq " " ) || ( $_ eq "" )) {
441 my $return = & isinstalled
( $_ );
443 message
( "PAKFIRE RESV: $pak : Dependency is already installed: $_ " );
445 message
( "PAKFIRE RESV: $pak : Need to install dependency: $_ " );
461 logger
( "CLEANUP: $dir " );
463 if ( " $dir " eq "meta" ) {
464 $path = " $Conf ::dbdir/meta" ;
465 } elsif ( " $dir " eq "tmp" ) {
466 $path = " $Conf ::tmpdir" ;
470 my @files = readdir ( DIR
);
473 unless (( $_ eq "." ) || ( $_ eq ".." )) {
482 unless ( - e
" $Conf ::dbdir/meta/meta- $pak " ) {
483 fetchfile
( "meta/meta- $pak " , "" );
484 move
( " $Conf ::cachedir/meta- $pak " , " $Conf ::dbdir/meta/meta- $pak " );
487 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
491 open ( FILE
, "> $Conf ::dbdir/meta/meta- $pak " );
494 $string =~ s/\r\n/\n/g ;
506 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
512 foreach $line ( @file ) {
513 @templine = split ( /\: / , $line );
514 if ( " $templine [0]" eq "Size" ) {
527 my $file = getpak
( " $pak " , "noforce" );
529 logger
( "DECRYPT STARTED: $pak " );
530 my $return = system ( "cd $Conf ::tmpdir/ && gpg -d --batch --quiet --no-verbose --status-fd 2 --output - < $Conf ::cachedir/ $file 2>/dev/null | tar x" );
532 logger
( "DECRYPT FINISHED: $pak - Status: $return " );
533 if ( $return != 0 ) { exit 1 ; }
542 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
549 foreach $line ( @file ) {
550 @templine = split ( /\: / , $line );
551 if ( " $templine [0]" eq "File" ) {
553 $file = $templine [ 1 ];
558 message
( "No filename given in meta-file. Please phone the developers." );
562 unless ( " $force " eq "force" ) {
563 if ( - e
" $Conf ::cachedir/ $file " ) {
568 fetchfile
( "paks/ $file " , "" );
575 message
( "PAKFIRE INST: $pak : Decrypting..." );
578 message
( "PAKFIRE INST: $pak : Copying files and running post-installation scripts..." );
579 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./install.sh >> $Conf ::logdir/install- $pak .log 2>&1" );
581 if ( $pakfiresettings { 'UUID' } ne "off" ) {
582 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&ipak= $pak &return= $return " , " $Conf ::mainserver" );
585 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
587 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
588 message
( "PAKFIRE INST: $pak : Finished." );
591 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
599 if ( open ( FILE
, "< $Conf ::dbdir/installed/meta- $pak " ) ) {
610 message
( "PAKFIRE UPGR: $pak : Decrypting..." );
613 message
( "PAKFIRE UPGR: $pak : Upgrading files and running post-upgrading scripts..." );
614 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./update.sh >> $Conf ::logdir/update- $pak .log 2>&1" );
616 if ( $pakfiresettings { 'UUID' } ne "off" ) {
617 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&upak= $pak &return= $return " , " $Conf ::mainserver" );
620 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
622 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
623 message
( "PAKFIRE UPGR: $pak : Finished." );
626 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
635 message
( "PAKFIRE REMV: $pak : Decrypting..." );
638 message
( "PAKFIRE REMV: $pak : Removing files and running post-removing scripts..." );
639 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./uninstall.sh >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
641 if ( $pakfiresettings { 'UUID' } ne "off" ) {
642 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&dpak= $pak &return= $return " , " $Conf ::mainserver" );
645 open ( FILE
, "< $Conf ::dbdir/rootfiles/ $pak " );
651 system ( "echo \" Removing: $line \" >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
652 system ( "cd / && rm -rf $line >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
654 unlink ( " $Conf ::dbdir/rootfiles/ $pak " );
655 unlink ( " $Conf ::dbdir/installed/meta- $pak " );
657 message
( "PAKFIRE REMV: $pak : Finished." );
660 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
668 #$size = $size / 1024;
671 if ( $size > 1023 * 1024 ) {
672 $size = ( $size / ( 1024 * 1024 ));
674 } elsif ( $size > 1023 ) {
675 $size = ( $size / 1024 );
680 $size = sprintf ( "%.2f" , $size );
681 my $string = " $size $unit " ;
686 unless ( - e
" $Conf ::dbdir/uuid" ) {
687 open ( FILE
, "</proc/sys/kernel/random/uuid" );
691 open ( FILE
, "> $Conf ::dbdir/uuid" );
700 if ( $pakfiresettings { 'UUID' } ne "off" ) {
701 unless ( " $Conf ::uuid" ) {
702 $Conf :: uuid
= `cat $Conf ::dbdir/uuid` ;
704 logger
( "Sending my uuid: $Conf ::uuid" );
705 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid" , " $Conf ::mainserver" );
706 system ( "rm -f $Conf ::tmpdir/counter* 2>/dev/null" );
711 logger
( "CRYPTO INFO: Checking GnuPG Database" );
712 my $ret = system ( "gpg --list-keys | grep -q $myid " );
713 unless ( " $ret " eq "0" ) {
714 message
( "CRYPTO WARN: The GnuPG isn't configured corectly. Trying now to fix this." );
715 message
( "CRYPTO WARN: It's normal to see this on first execution." );
716 system ( "gpg --keyserver wwwkeys.de.pgp.net --always-trust --status-fd 2 --recv-key $myid >> $Conf ::logdir/gnupg-database.log 2>&1" );
717 system ( "gpg --keyserver wwwkeys.de.pgp.net --always-trust --status-fd 2 --recv-key $trustid >> $Conf ::logdir/gnupg-database.log 2>&1" );
719 logger
( "CRYPTO INFO: Database is okay" );
724 my ( $data , $response , $protocol ) = @_ ;
725 $final_data .= $data ;
726 print progress_bar
( length ( $final_data ), $total_size , 30 , '=' );
730 my ( $got , $total , $width , $char ) = @_ ;
732 $width ||= 30 ; $char ||= '=' ;
733 my $len_bfile = length $bfile ;
734 if ( " $len_bfile " >= "17" ) {
735 $show_bfile = substr ( $bfile , 0 , 17 ). "..." ;
737 $show_bfile = $bfile ;
739 $progress = sprintf ( "%.2f%%" , 100 * $got /+ $total );
740 sprintf " $color {'lightgreen'}%-20s %7s |%-${width}s| %10s $color {'normal'} \r " , $show_bfile , $progress , $char x
(( $width - 1 )* $got / $total ). '>' , beautifysize
( $got );