]>
git.ipfire.org Git - 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 , $i );
118 $bfile = basename
( " $getfile " );
120 logger
( "DOWNLOAD STARTED: $getfile " ) unless ( $bfile =~ /^counter\?.*/ );
123 while (( $allok == 0 ) && $i < 5 ) {
126 if ( " $gethost " eq "" ) {
127 @server = selectmirror
();
130 $file = " $server [2]/ $getfile " ;
136 $proto = "HTTP" unless $proto ;
138 unless ( $bfile =~ /^counter\?.*/ ) {
139 logger
( "DOWNLOAD INFO: Host: $host ( $proto ) - File: $file " );
142 my $ua = LWP
:: UserAgent
-> new ;
143 $ua -> agent ( "Pakfire/ $Conf ::version" );
146 my %proxysettings =();
147 & General
:: readhash
( "${General::swroot}/proxy/advanced/settings" , \
%proxysettings );
149 if ( $proxysettings { 'UPSTREAM_PROXY' }) {
150 logger
( "DOWNLOAD INFO: Upstream proxy: \" $proxysettings {'UPSTREAM_PROXY'} \" " ) unless ( $bfile =~ /^counter\?.*/ );
151 if ( $proxysettings { 'UPSTREAM_USER' }) {
152 $ua -> proxy ( "http" , "http:// $proxysettings {'UPSTREAM_USER'}: $proxysettings {'UPSTREAM_PASSWORD'}@" . " $proxysettings {'UPSTREAM_PROXY'}/" );
153 logger
( "DOWNLOAD INFO: Logging in with: \" $proxysettings {'UPSTREAM_USER'} \" - \" $proxysettings {'UPSTREAM_PASSWORD'} \" " ) unless ( $bfile =~ /^counter\?.*/ );
155 $ua -> proxy ( "http" , "http:// $proxysettings {'UPSTREAM_PROXY'}/" );
160 my $url = "http:// $host / $file " ;
163 unless ( $bfile =~ /^counter\?.*/ ) {
164 my $result = $ua -> head ( $url );
165 my $remote_headers = $result -> headers ;
166 $total_size = $remote_headers -> content_length ;
167 logger
( "DOWNLOAD INFO: $file has size of $total_size bytes" );
169 $response = $ua -> get ( $url , ':content_cb' => \
& callback
);
172 $response = $ua -> get ( $url );
175 my $code = $response -> code ();
176 my $log = $response -> status_line ;
177 logger
( "DOWNLOAD INFO: HTTP-Status-Code: $code - $log " );
179 if ( $code eq "500" ) {
180 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." );
184 if ( $response -> is_success ) {
185 unless ( $bfile =~ /^counter\?.*/ ) {
186 if ( open ( FILE
, "> $Conf ::tmpdir/ $bfile " )) {
187 print FILE
$final_data ;
189 logger
( "DOWNLOAD INFO: File received. Start checking signature..." );
190 if ( system ( "gpg --verify \" $Conf ::tmpdir/ $bfile \" &>/dev/null" ) eq 0 ) {
191 logger
( "DOWNLOAD INFO: Signature of $bfile is fine." );
192 move
( " $Conf ::tmpdir/ $bfile " , " $Conf ::cachedir/ $bfile " );
194 message
( "DOWNLOAD ERROR: The downloaded file ( $file ) wasn't verified by IPFire.org. Sorry - Exiting..." );
197 logger
( "DOWNLOAD FINISHED: $file " );
201 logger
( "DOWNLOAD ERROR: Could not open $Conf ::cachedir/ $bfile for writing." );
207 logger
( "DOWNLOAD ERROR: $log " );
210 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." );
220 if ( - e
" $Conf ::dbdir/lists/server-list.db" ) {
221 my @stat = stat ( " $Conf ::dbdir/lists/server-list.db" );
223 $age = $time - $stat [ 9 ];
224 $force = "force" if ( " $age " >= "3600" );
225 logger
( "MIRROR INFO: server-list.db is $age seconds old. - DEBUG: $force " );
231 if ( " $force " eq "force" ) {
232 fetchfile
( " $Conf ::version/lists/server-list.db" , " $Conf ::mainserver" );
233 move
( " $Conf ::cachedir/server-list.db" , " $Conf ::dbdir/lists/server-list.db" );
243 if ( - e
" $Conf ::dbdir/lists/core-list.db" ) {
244 my @stat = stat ( " $Conf ::dbdir/lists/core-list.db" );
246 $age = $time - $stat [ 9 ];
247 $force = "force" if ( " $age " >= "3600" );
248 logger
( "CORE INFO: core-list.db is $age seconds old. - DEBUG: $force " );
254 if ( " $force " eq "force" ) {
255 fetchfile
( "lists/core-list.db" , "" );
256 move
( " $Conf ::cachedir/core-list.db" , " $Conf ::dbdir/lists/core-list.db" );
262 ### Check if there is a current server list and read it.
263 # If there is no list try to get one.
265 while (!( open ( FILE
, "< $Conf ::dbdir/lists/server-list.db" )) && ( $count lt 5 )) {
267 getmirrors
( "noforce" );
270 message
( "MIRROR ERROR: Could not find or download a server list" );
276 ### Count the number of the servers in the list
280 if ( " $_ " =~ /.*;.*;.*;/ ) {
285 logger
( "MIRROR INFO: $scount servers found in list" );
287 ### Choose a random server and test if it is online
288 # If the check fails try a new server.
289 # This will never give up.
292 while ( $found == 0 ) {
293 $server = int ( rand ( $scount ) + 1 );
295 my ( $line , $proto , $path , $host );
297 foreach $line ( @newlines ) {
299 if ( $servers eq $server ) {
300 @templine = split ( /\;/ , $line );
301 $proto = $templine [ 0 ];
302 $host = $templine [ 1 ];
303 $path = $templine [ 2 ];
304 if ( pinghost
( " $host " )) {
306 return ( $proto , $host , $path );
314 ### Update the database if the file is older than one day.
315 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
316 # Usage is always with an argument.
322 if ( - e
" $Conf ::dbdir/lists/packages_list.db" ) {
323 my @stat = stat ( " $Conf ::dbdir/lists/packages_list.db" );
325 $age = $time - $stat [ 9 ];
326 $force = "force" if ( " $age " >= "3600" );
327 logger
( "DB INFO: packages_list.db is $age seconds old. - DEBUG: $force " );
333 if ( " $force " eq "force" ) {
334 fetchfile
( "lists/packages_list.db" , "" );
335 move
( " $Conf ::cachedir/packages_list.db" , " $Conf ::dbdir/lists/packages_list.db" );
340 ### This subroutine lists the packages.
341 # You may also pass a filter: &Pakfire::dblist(filter)
342 # Usage is always with two arguments.
343 # filter may be: all, notinstalled, installed
351 my ( $name , $version , $release );
354 ### Make sure that the list is not outdated.
355 #dbgetlist("noforce");
357 open ( FILE
, "< $Conf ::dbdir/lists/packages_list.db" );
361 if ( " $filter " eq "upgrade" ) {
362 getcoredb
( "noforce" );
363 eval ( `grep "core_" $Conf ::dbdir/lists/core-list.db` );
364 if ( " $core_release " gt " $Conf ::core_mine" ) {
365 if ( " $forweb " eq "forweb" ) {
366 print "<option value= \" core \" >Core-Update -- $Conf ::version -- Release: $Conf ::core_mine -> $core_release </option> \n " ;
368 my $command = "Core-Update $Conf ::version \n Release: $Conf ::core_mine -> $core_release \n " ;
369 if ( " $Pakfire ::enable_colors" eq "1" ) {
370 print " $color {'lila'} $command $color {'normal'} \n " ;
377 opendir ( DIR
, " $Conf ::dbdir/meta" );
378 my @files = readdir ( DIR
);
380 foreach $file ( @files ) {
381 next if ( $file eq "." );
382 next if ( $file eq ".." );
383 open ( FILE
, "< $Conf ::dbdir/meta/ $file " );
386 foreach $line ( @meta ) {
387 @templine = split ( /\: / , $line );
388 if ( " $templine [0]" eq "Name" ) {
389 $name = $templine [ 1 ];
391 } elsif ( " $templine [0]" eq "ProgVersion" ) {
392 $version = $templine [ 1 ];
394 } elsif ( " $templine [0]" eq "Release" ) {
395 $release = $templine [ 1 ];
399 foreach $prog ( @db ) {
400 @templine = split ( /\;/ , $prog );
401 if (( " $name " eq " $templine [0]" ) && ( " $release " < " $templine [2]" )) {
402 push ( @updatepaks , $name );
403 if ( " $forweb " eq "forweb" ) {
404 print "<option value= \" $name \" >Update: $name -- Version: $version -> $templine [1] -- Release: $release -> $templine [2]</option> \n " ;
406 my $command = "Update: $name \n Version: $version -> $templine [1] \n Release: $release -> $templine [2] \n " ;
407 if ( " $Pakfire ::enable_colors" eq "1" ) {
408 print " $color {'lila'} $command $color {'normal'} \n " ;
422 foreach $line ( sort @db ) {
423 next unless ( $line =~ /.*;.*;.*;/ );
426 @templine = split ( /\;/ , $line );
427 if ( " $filter " eq "notinstalled" ) {
428 next if ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
429 } elsif ( " $filter " eq "installed" ) {
430 next unless ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
432 if ( " $forweb " eq "forweb" ) {
433 print "<option value= \" $templine [0] \" > $templine [0]- $templine [1]- $templine [2]</option> \n " ;
435 if ( " $Pakfire ::enable_colors" eq "1" ) {
436 if (& isinstalled
( " $templine [0]" )) {
437 $use_color = " $color {'red'}"
439 $use_color = " $color {'green'}"
442 print "${use_color}Name: $templine [0] \n ProgVersion: $templine [1] \n Release: $templine [2] $color {'normal'} \n\n " ;
445 print " $count packages total. \n " unless ( " $forweb " eq "forweb" );
454 message
( "PAKFIRE RESV: $pak : Resolving dependencies..." );
456 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
461 my ( @templine , @deps , @tempdeps , @all );
462 foreach $line ( @file ) {
463 @templine = split ( /\: / , $line );
464 if ( " $templine [0]" eq "Dependencies" ) {
465 @deps = split ( / / , $templine [ 1 ]);
471 my $return = & isinstalled
( $_ );
473 message
( "PAKFIRE RESV: $pak : Dependency is already installed: $_ " );
475 message
( "PAKFIRE RESV: $pak : Need to install dependency: $_ " );
482 foreach ( @tempdeps ) {
484 my @newdeps = resolvedeps
( " $_ " );
486 unless (( $_ eq " " ) || ( $_ eq "" )) {
487 my $return = & isinstalled
( $_ );
489 message
( "PAKFIRE RESV: $pak : Dependency is already installed: $_ " );
491 message
( "PAKFIRE RESV: $pak : Need to install dependency: $_ " );
507 logger
( "CLEANUP: $dir " );
509 if ( " $dir " eq "meta" ) {
510 $path = " $Conf ::dbdir/meta" ;
511 } elsif ( " $dir " eq "tmp" ) {
512 $path = " $Conf ::tmpdir" ;
516 my @files = readdir ( DIR
);
519 unless (( $_ eq "." ) || ( $_ eq ".." )) {
528 unless ( - e
" $Conf ::dbdir/meta/meta- $pak " ) {
529 fetchfile
( "meta/meta- $pak " , "" );
530 move
( " $Conf ::cachedir/meta- $pak " , " $Conf ::dbdir/meta/meta- $pak " );
533 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
537 open ( FILE
, "> $Conf ::dbdir/meta/meta- $pak " );
540 $string =~ s/\r\n/\n/g ;
552 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
558 foreach $line ( @file ) {
559 @templine = split ( /\: / , $line );
560 if ( " $templine [0]" eq "Size" ) {
573 my $file = getpak
( " $pak " , "noforce" );
575 logger
( "DECRYPT STARTED: $pak " );
576 my $return = system ( "cd $Conf ::tmpdir/ && gpg -d --batch --quiet --no-verbose --status-fd 2 --output - < $Conf ::cachedir/ $file 2>/dev/null | tar x" );
578 logger
( "DECRYPT FINISHED: $pak - Status: $return " );
579 if ( $return != 0 ) { exit 1 ; }
588 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
595 foreach $line ( @file ) {
596 @templine = split ( /\: / , $line );
597 if ( " $templine [0]" eq "File" ) {
599 $file = $templine [ 1 ];
604 message
( "No filename given in meta-file. Please phone the developers." );
608 unless ( " $force " eq "force" ) {
609 if ( - e
" $Conf ::cachedir/ $file " ) {
614 fetchfile
( "paks/ $file " , "" );
621 message
( "PAKFIRE INST: $pak : Decrypting..." );
624 message
( "PAKFIRE INST: $pak : Copying files and running post-installation scripts..." );
625 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./install.sh >> $Conf ::logdir/install- $pak .log 2>&1" );
627 if ( $pakfiresettings { 'UUID' } ne "off" ) {
628 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&ipak= $pak &return= $return " , " $Conf ::mainserver" );
631 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
633 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
634 message
( "PAKFIRE INST: $pak : Finished." );
637 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
644 getcoredb
( "noforce" );
645 eval ( `grep "core_" $Conf ::dbdir/lists/core-list.db` );
646 if ( " $core_release " gt " $Conf ::core_mine" ) {
647 message
( "CORE UPGR: Upgrading from release $Conf ::core_mine to $core_release " );
649 my @seq = `seq $Conf ::core_mine $core_release ` ;
652 foreach $release ( @seq ) {
654 getpak
( "core-upgrade- $release " );
657 foreach $release ( @seq ) {
659 upgradepak
( "core-upgrade- $release " );
662 system ( "echo $core_release > $Conf ::coredir/mine" );
665 message
( "CORE ERROR: No new upgrades available. You are on release $Conf ::core_mine." );
671 if ( open ( FILE
, "< $Conf ::dbdir/installed/meta- $pak " ) ) {
682 message
( "PAKFIRE UPGR: $pak : Decrypting..." );
685 message
( "PAKFIRE UPGR: $pak : Upgrading files and running post-upgrading scripts..." );
686 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./update.sh >> $Conf ::logdir/update- $pak .log 2>&1" );
688 if ( $pakfiresettings { 'UUID' } ne "off" ) {
689 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&upak= $pak &return= $return " , " $Conf ::mainserver" );
692 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
694 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
695 message
( "PAKFIRE UPGR: $pak : Finished." );
698 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
707 message
( "PAKFIRE REMV: $pak : Decrypting..." );
710 message
( "PAKFIRE REMV: $pak : Removing files and running post-removing scripts..." );
711 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./uninstall.sh >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
713 if ( $pakfiresettings { 'UUID' } ne "off" ) {
714 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&dpak= $pak &return= $return " , " $Conf ::mainserver" );
717 open ( FILE
, "< $Conf ::dbdir/rootfiles/ $pak " );
723 system ( "echo \" Removing: $line \" >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
724 system ( "cd / && rm -rf $line >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
726 unlink ( " $Conf ::dbdir/rootfiles/ $pak " );
727 unlink ( " $Conf ::dbdir/installed/meta- $pak " );
729 message
( "PAKFIRE REMV: $pak : Finished." );
732 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
740 #$size = $size / 1024;
743 if ( $size > 1023 * 1024 ) {
744 $size = ( $size / ( 1024 * 1024 ));
746 } elsif ( $size > 1023 ) {
747 $size = ( $size / 1024 );
752 $size = sprintf ( "%.2f" , $size );
753 my $string = " $size $unit " ;
758 unless ( - e
" $Conf ::dbdir/uuid" ) {
759 open ( FILE
, "</proc/sys/kernel/random/uuid" );
763 open ( FILE
, "> $Conf ::dbdir/uuid" );
772 if ( $pakfiresettings { 'UUID' } ne "off" ) {
773 unless ( " $Conf ::uuid" ) {
774 $Conf :: uuid
= `cat $Conf ::dbdir/uuid` ;
776 logger
( "Sending my uuid: $Conf ::uuid" );
777 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid" , " $Conf ::mainserver" );
778 system ( "rm -f $Conf ::tmpdir/counter* 2>/dev/null" );
783 logger
( "CRYPTO INFO: Checking GnuPG Database" );
784 my $ret = system ( "gpg --list-keys | grep -q $myid " );
785 unless ( " $ret " eq "0" ) {
786 message
( "CRYPTO WARN: The GnuPG isn't configured corectly. Trying now to fix this." );
787 message
( "CRYPTO WARN: It's normal to see this on first execution." );
788 my $command = "gpg --keyserver pgp.mit.edu --always-trust --status-fd 2" ;
789 system ( " $command --recv-key $myid >> $Conf ::logdir/gnupg-database.log 2>&1" );
790 system ( " $command --recv-key $trustid >> $Conf ::logdir/gnupg-database.log 2>&1" );
792 logger
( "CRYPTO INFO: Database is okay" );
797 my ( $data , $response , $protocol ) = @_ ;
798 $final_data .= $data ;
799 print progress_bar
( length ( $final_data ), $total_size , 30 , '=' );
803 my ( $got , $total , $width , $char ) = @_ ;
805 $width ||= 30 ; $char ||= '=' ;
806 my $len_bfile = length $bfile ;
807 if ( " $len_bfile " >= "17" ) {
808 $show_bfile = substr ( $bfile , 0 , 17 ). "..." ;
810 $show_bfile = $bfile ;
812 $progress = sprintf ( "%.2f%%" , 100 * $got /+ $total );
813 sprintf " $color {'lightgreen'}%-20s %7s |%-${width}s| %10s $color {'normal'} \r " , $show_bfile , $progress , $char x
(( $width - 1 )* $got / $total ). '>' , beautifysize
( $got );