]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/pakfire/lib/functions.pl
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
22 require "/opt/pakfire/etc/pakfire.conf" ;
23 require "/var/ipfire/general-functions.pl" ;
37 my $myid = "64D96617" ; # Our own gpg-key paks@ipfire.org
38 my $trustid = "65D0FD58" ; # gpg-key of CaCert
40 # A small color-hash :D
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" ;
59 our $enable_colors = 1 ;
65 my %pakfiresettings = ();
66 & General
:: readhash
( "${General::swroot}/pakfire/settings" , \
%pakfiresettings );
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'}" ;
96 system ( "echo \" `date`: $log \" >> /var/log/pakfire.log" );
97 #system("logger -t pakfire \"$log\"");
102 & Pakfire
:: message
( "Usage: pakfire <install|remove> [options] <pak(s)>" );
103 & Pakfire
:: message
( " <update> - Contacts the servers for new lists of paks." );
104 & Pakfire
:: message
( " <upgrade> - Installs the latest version of all paks." );
105 & Pakfire
:: message
( " <list> - Outputs a short list with all available paks." );
106 & Pakfire
:: message
( "" );
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
( "" );
119 $p = Net
:: Ping
-> new ();
120 if ( $p -> ping ( $host )) {
121 logger
( "PING INFO: $host is alive" );
124 logger
( "PING INFO: $host is unreachable" );
133 my ( @server , $host , $proto , $file , $i );
137 $bfile = basename
( " $getfile " );
139 logger
( "DOWNLOAD STARTED: $getfile " ) unless ( $bfile =~ /^counter\?.*/ );
142 while (( $allok == 0 ) && $i < 5 ) {
145 if ( " $gethost " eq "" ) {
146 @server = selectmirror
();
149 $file = " $server [2]/ $getfile " ;
155 $proto = "HTTP" unless $proto ;
157 unless ( $bfile =~ /^counter\?.*/ ) {
158 logger
( "DOWNLOAD INFO: Host: $host ( $proto ) - File: $file " );
161 my $ua = LWP
:: UserAgent
-> new ;
162 $ua -> agent ( "Pakfire/ $Conf ::version" );
165 my %proxysettings =();
166 & General
:: readhash
( "${General::swroot}/proxy/advanced/settings" , \
%proxysettings );
168 if ( $proxysettings { 'UPSTREAM_PROXY' }) {
169 logger
( "DOWNLOAD INFO: Upstream proxy: \" $proxysettings {'UPSTREAM_PROXY'} \" " ) unless ( $bfile =~ /^counter.py\?.*/ );
170 if ( $proxysettings { 'UPSTREAM_USER' }) {
171 $ua -> proxy ( "http" , "http:// $proxysettings {'UPSTREAM_USER'}: $proxysettings {'UPSTREAM_PASSWORD'}@" . " $proxysettings {'UPSTREAM_PROXY'}/" );
172 logger
( "DOWNLOAD INFO: Logging in with: \" $proxysettings {'UPSTREAM_USER'} \" - \" $proxysettings {'UPSTREAM_PASSWORD'} \" " ) unless ( $bfile =~ /^counter.py\?.*/ );
174 $ua -> proxy ( "http" , "http:// $proxysettings {'UPSTREAM_PROXY'}/" );
179 my $url = "http:// $host / $file " ;
182 unless ( $bfile =~ /^counter.py\?.*/ ) {
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" );
188 $response = $ua -> get ( $url , ':content_cb' => \
& callback
);
191 $response = $ua -> get ( $url );
194 my $code = $response -> code ();
195 my $log = $response -> status_line ;
196 logger
( "DOWNLOAD INFO: HTTP-Status-Code: $code - $log " );
198 if ( $code eq "500" ) {
199 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." );
203 if ( $response -> is_success ) {
204 unless ( $bfile =~ /^counter.py\?.*/ ) {
205 if ( open ( FILE
, "> $Conf ::tmpdir/ $bfile " )) {
206 print FILE
$final_data ;
208 logger
( "DOWNLOAD INFO: File received. Start checking signature..." );
209 if ( system ( "gpg --verify \" $Conf ::tmpdir/ $bfile \" &>/dev/null" ) eq 0 ) {
210 logger
( "DOWNLOAD INFO: Signature of $bfile is fine." );
211 move
( " $Conf ::tmpdir/ $bfile " , " $Conf ::cachedir/ $bfile " );
213 message
( "DOWNLOAD ERROR: The downloaded file ( $file ) wasn't verified by IPFire.org. Sorry - Exiting..." );
216 logger
( "DOWNLOAD FINISHED: $file " );
220 logger
( "DOWNLOAD ERROR: Could not open $Conf ::cachedir/ $bfile for writing." );
226 logger
( "DOWNLOAD ERROR: $log " );
229 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." );
239 if ( - e
" $Conf ::dbdir/lists/server-list.db" ) {
240 my @stat = stat ( " $Conf ::dbdir/lists/server-list.db" );
242 $age = $time - $stat [ 9 ];
243 $force = "force" if ( " $age " >= "3600" );
244 logger
( "MIRROR INFO: server-list.db is $age seconds old. - DEBUG: $force " );
250 if ( " $force " eq "force" ) {
251 fetchfile
( " $Conf ::version/lists/server-list.db" , " $Conf ::mainserver" );
252 move
( " $Conf ::cachedir/server-list.db" , " $Conf ::dbdir/lists/server-list.db" );
262 if ( - e
" $Conf ::dbdir/lists/core-list.db" ) {
263 my @stat = stat ( " $Conf ::dbdir/lists/core-list.db" );
265 $age = $time - $stat [ 9 ];
266 $force = "force" if ( " $age " >= "3600" );
267 logger
( "CORE INFO: core-list.db is $age seconds old. - DEBUG: $force " );
273 if ( " $force " eq "force" ) {
274 fetchfile
( "lists/core-list.db" , "" );
275 move
( " $Conf ::cachedir/core-list.db" , " $Conf ::dbdir/lists/core-list.db" );
281 ### Check if there is a current server list and read it.
282 # If there is no list try to get one.
284 while (!( open ( FILE
, "< $Conf ::dbdir/lists/server-list.db" )) && ( $count lt 20 )) {
286 getmirrors
( "noforce" );
289 message
( "MIRROR ERROR: Could not find or download a server list" );
295 ### Count the number of the servers in the list
299 if ( " $_ " =~ /.*;.*;.*;/ ) {
304 logger
( "MIRROR INFO: $scount servers found in list" );
306 ### Choose a random server and test if it is online
307 # If the check fails try a new server.
308 # This will never give up.
311 while ( $found == 0 ) {
312 $server = int ( rand ( $scount ) + 1 );
314 my ( $line , $proto , $path , $host );
316 foreach $line ( @newlines ) {
318 if ( $servers eq $server ) {
319 @templine = split ( /\;/ , $line );
320 $proto = $templine [ 0 ];
321 $host = $templine [ 1 ];
322 $path = $templine [ 2 ];
323 if ( pinghost
( " $host " )) {
325 return ( $proto , $host , $path );
333 ### Update the database if the file is older than one day.
334 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
335 # Usage is always with an argument.
341 if ( - e
" $Conf ::dbdir/lists/packages_list.db" ) {
342 my @stat = stat ( " $Conf ::dbdir/lists/packages_list.db" );
344 $age = $time - $stat [ 9 ];
345 $force = "force" if ( " $age " >= "3600" );
346 logger
( "DB INFO: packages_list.db is $age seconds old. - DEBUG: $force " );
352 if ( " $force " eq "force" ) {
353 fetchfile
( "lists/packages_list.db" , "" );
354 move
( " $Conf ::cachedir/packages_list.db" , " $Conf ::dbdir/lists/packages_list.db" );
359 ### This subroutine lists the packages.
360 # You may also pass a filter: &Pakfire::dblist(filter)
361 # Usage is always with two arguments.
362 # filter may be: all, notinstalled, installed
370 my ( $name , $version , $release );
373 ### Make sure that the list is not outdated.
374 #dbgetlist("noforce");
376 open ( FILE
, "< $Conf ::dbdir/lists/packages_list.db" );
380 if ( " $filter " eq "upgrade" ) {
381 getcoredb
( "noforce" );
382 eval ( `grep "core_" $Conf ::dbdir/lists/core-list.db` );
383 if ( " $core_release " gt " $Conf ::core_mine" ) {
384 if ( " $forweb " eq "forweb" ) {
385 print "<option value= \" core \" >Core-Update -- $Conf ::version -- Release: $Conf ::core_mine -> $core_release </option> \n " ;
387 my $command = "Core-Update $Conf ::version \n Release: $Conf ::core_mine -> $core_release \n " ;
388 if ( " $Pakfire ::enable_colors" eq "1" ) {
389 print " $color {'lila'} $command $color {'normal'} \n " ;
396 opendir ( DIR
, " $Conf ::dbdir/meta" );
397 my @files = readdir ( DIR
);
399 foreach $file ( @files ) {
400 next if ( $file eq "." );
401 next if ( $file eq ".." );
402 open ( FILE
, "< $Conf ::dbdir/meta/ $file " );
405 foreach $line ( @meta ) {
406 @templine = split ( /\: / , $line );
407 if ( " $templine [0]" eq "Name" ) {
408 $name = $templine [ 1 ];
410 } elsif ( " $templine [0]" eq "ProgVersion" ) {
411 $version = $templine [ 1 ];
413 } elsif ( " $templine [0]" eq "Release" ) {
414 $release = $templine [ 1 ];
418 foreach $prog ( @db ) {
419 @templine = split ( /\;/ , $prog );
420 if (( " $name " eq " $templine [0]" ) && ( " $release " < " $templine [2]" )) {
421 push ( @updatepaks , $name );
422 if ( " $forweb " eq "forweb" ) {
423 print "<option value= \" $name \" >Update: $name -- Version: $version -> $templine [1] -- Release: $release -> $templine [2]</option> \n " ;
425 my $command = "Update: $name \n Version: $version -> $templine [1] \n Release: $release -> $templine [2] \n " ;
426 if ( " $Pakfire ::enable_colors" eq "1" ) {
427 print " $color {'lila'} $command $color {'normal'} \n " ;
441 foreach $line ( sort @db ) {
442 next unless ( $line =~ /.*;.*;.*;/ );
445 @templine = split ( /\;/ , $line );
446 if ( " $filter " eq "notinstalled" ) {
447 next if ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
448 } elsif ( " $filter " eq "installed" ) {
449 next unless ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
451 if ( " $forweb " eq "forweb" ) {
452 print "<option value= \" $templine [0] \" > $templine [0]- $templine [1]- $templine [2]</option> \n " ;
454 if ( " $Pakfire ::enable_colors" eq "1" ) {
455 if (& isinstalled
( " $templine [0]" )) {
456 $use_color = " $color {'red'}"
458 $use_color = " $color {'green'}"
461 print "${use_color}Name: $templine [0] \n ProgVersion: $templine [1] \n Release: $templine [2] $color {'normal'} \n\n " ;
464 print " $count packages total. \n " unless ( " $forweb " eq "forweb" );
473 message
( "PAKFIRE RESV: $pak : Resolving dependencies..." );
475 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
480 my ( @templine , @deps , @tempdeps , @all );
481 foreach $line ( @file ) {
482 @templine = split ( /\: / , $line );
483 if ( " $templine [0]" eq "Dependencies" ) {
484 @deps = split ( / / , $templine [ 1 ]);
490 my $return = & isinstalled
( $_ );
492 message
( "PAKFIRE RESV: $pak : Dependency is already installed: $_ " );
494 message
( "PAKFIRE RESV: $pak : Need to install dependency: $_ " );
501 foreach ( @tempdeps ) {
503 my @newdeps = resolvedeps
( " $_ " );
505 unless (( $_ eq " " ) || ( $_ eq "" )) {
506 my $return = & isinstalled
( $_ );
508 message
( "PAKFIRE RESV: $pak : Dependency is already installed: $_ " );
510 message
( "PAKFIRE RESV: $pak : Need to install dependency: $_ " );
526 logger
( "CLEANUP: $dir " );
528 if ( " $dir " eq "meta" ) {
529 $path = " $Conf ::dbdir/meta" ;
530 } elsif ( " $dir " eq "tmp" ) {
531 $path = " $Conf ::tmpdir" ;
535 my @files = readdir ( DIR
);
538 unless (( $_ eq "." ) || ( $_ eq ".." )) {
547 unless ( - e
" $Conf ::dbdir/meta/meta- $pak " ) {
548 fetchfile
( "meta/meta- $pak " , "" );
549 move
( " $Conf ::cachedir/meta- $pak " , " $Conf ::dbdir/meta/meta- $pak " );
552 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
556 open ( FILE
, "> $Conf ::dbdir/meta/meta- $pak " );
559 $string =~ s/\r\n/\n/g ;
571 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
577 foreach $line ( @file ) {
578 @templine = split ( /\: / , $line );
579 if ( " $templine [0]" eq "Size" ) {
592 my $file = getpak
( " $pak " , "noforce" );
594 logger
( "DECRYPT STARTED: $pak " );
595 my $return = system ( "cd $Conf ::tmpdir/ && gpg -d --batch --quiet --no-verbose --status-fd 2 --output - < $Conf ::cachedir/ $file 2>/dev/null | tar x" );
597 logger
( "DECRYPT FINISHED: $pak - Status: $return " );
598 if ( $return != 0 ) { exit 1 ; }
607 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
614 foreach $line ( @file ) {
615 @templine = split ( /\: / , $line );
616 if ( " $templine [0]" eq "File" ) {
618 $file = $templine [ 1 ];
623 message
( "No filename given in meta-file. Please phone the developers." );
627 unless ( " $force " eq "force" ) {
628 if ( - e
" $Conf ::cachedir/ $file " ) {
633 fetchfile
( "paks/ $file " , "" );
640 message
( "PAKFIRE INST: $pak : Decrypting..." );
643 message
( "PAKFIRE INST: $pak : Copying files and running post-installation scripts..." );
644 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./install.sh >> $Conf ::logdir/install- $pak .log 2>&1" );
646 if ( $pakfiresettings { 'UUID' } ne "off" ) {
647 fetchfile
( "counter.py?ver= $Conf ::version&uuid= $Conf ::uuid&ipak= $pak &return= $return " , " $Conf ::mainserver" );
650 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
652 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
653 message
( "PAKFIRE INST: $pak : Finished." );
656 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
663 getcoredb
( "noforce" );
664 eval ( `grep "core_" $Conf ::dbdir/lists/core-list.db` );
665 if ( " $core_release " gt " $Conf ::core_mine" ) {
666 message
( "CORE UPGR: Upgrading from release $Conf ::core_mine to $core_release " );
668 my @seq = `seq $Conf ::core_mine $core_release ` ;
671 foreach $release ( @seq ) {
673 getpak
( "core-upgrade- $release " );
676 foreach $release ( @seq ) {
678 upgradepak
( "core-upgrade- $release " );
681 system ( "echo $core_release > $Conf ::coredir/mine" );
684 message
( "CORE ERROR: No new upgrades available. You are on release $Conf ::core_mine." );
690 if ( open ( FILE
, "< $Conf ::dbdir/installed/meta- $pak " ) ) {
701 message
( "PAKFIRE UPGR: $pak : Decrypting..." );
704 message
( "PAKFIRE UPGR: $pak : Upgrading files and running post-upgrading scripts..." );
705 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./update.sh >> $Conf ::logdir/update- $pak .log 2>&1" );
707 if ( $pakfiresettings { 'UUID' } ne "off" ) {
708 fetchfile
( "counter.py?ver= $Conf ::version&uuid= $Conf ::uuid&upak= $pak &return= $return " , " $Conf ::mainserver" );
711 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
713 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
714 message
( "PAKFIRE UPGR: $pak : Finished." );
717 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
726 message
( "PAKFIRE REMV: $pak : Decrypting..." );
729 message
( "PAKFIRE REMV: $pak : Removing files and running post-removing scripts..." );
730 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./uninstall.sh >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
732 if ( $pakfiresettings { 'UUID' } ne "off" ) {
733 fetchfile
( "counter.py?ver= $Conf ::version&uuid= $Conf ::uuid&dpak= $pak &return= $return " , " $Conf ::mainserver" );
736 open ( FILE
, "< $Conf ::dbdir/rootfiles/ $pak " );
742 system ( "echo \" Removing: $line \" >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
743 system ( "cd / && rm -rf $line >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
745 unlink ( " $Conf ::dbdir/rootfiles/ $pak " );
746 unlink ( " $Conf ::dbdir/installed/meta- $pak " );
748 message
( "PAKFIRE REMV: $pak : Finished." );
751 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
759 #$size = $size / 1024;
762 if ( $size > 1023 * 1024 ) {
763 $size = ( $size / ( 1024 * 1024 ));
765 } elsif ( $size > 1023 ) {
766 $size = ( $size / 1024 );
771 $size = sprintf ( "%.2f" , $size );
772 my $string = " $size $unit " ;
777 unless ( - e
" $Conf ::dbdir/uuid" ) {
778 open ( FILE
, "</proc/sys/kernel/random/uuid" );
782 open ( FILE
, "> $Conf ::dbdir/uuid" );
791 if ( $pakfiresettings { 'UUID' } ne "off" ) {
792 unless ( " $Conf ::uuid" ) {
793 $Conf :: uuid
= `cat $Conf ::dbdir/uuid` ;
795 logger
( "Sending my uuid: $Conf ::uuid" );
796 fetchfile
( "counter.py?ver= $Conf ::version&uuid= $Conf ::uuid" , " $Conf ::mainserver" );
797 system ( "rm -f $Conf ::tmpdir/counter* 2>/dev/null" );
802 logger
( "CRYPTO INFO: Checking GnuPG Database" );
803 my $ret = system ( "gpg --list-keys | grep -q $myid " );
804 unless ( " $ret " eq "0" ) {
805 message
( "CRYPTO WARN: The GnuPG isn't configured corectly. Trying now to fix this." );
806 message
( "CRYPTO WARN: It's normal to see this on first execution." );
807 my $command = "gpg --keyserver pgp.mit.edu --always-trust --status-fd 2" ;
808 system ( " $command --recv-key $myid >> $Conf ::logdir/gnupg-database.log 2>&1" );
809 system ( " $command --recv-key $trustid >> $Conf ::logdir/gnupg-database.log 2>&1" );
811 logger
( "CRYPTO INFO: Database is okay" );
816 my ( $data , $response , $protocol ) = @_ ;
817 $final_data .= $data ;
818 print progress_bar
( length ( $final_data ), $total_size , 30 , '=' );
822 my ( $got , $total , $width , $char ) = @_ ;
824 $width ||= 30 ; $char ||= '=' ;
825 my $len_bfile = length $bfile ;
826 if ( " $len_bfile " >= "17" ) {
827 $show_bfile = substr ( $bfile , 0 , 17 ). "..." ;
829 $show_bfile = $bfile ;
831 $progress = sprintf ( "%.2f%%" , 100 * $got /+ $total );
832 sprintf " $color {'lightgreen'}%-20s %7s |%-${width}s| %10s $color {'normal'} \r " , $show_bfile , $progress , $char x
(( $width - 1 )* $got / $total ). '>' , beautifysize
( $got );