]>
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 , $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" );
235 logger
( "CORE: Trying to get a core list." );
237 if ( - e
" $Conf ::dbdir/lists/core-list.db" ) {
238 my @stat = stat ( " $Conf ::dbdir/lists/core-list.db" );
240 $age = $time - $stat [ 9 ];
246 if ( " $age " gt "3600" ) {
247 fetchfile
( "lists/core-list.db" , "" );
248 move
( " $Conf ::cachedir/core-list.db" , " $Conf ::dbdir/lists/core-list.db" );
254 ### Check if there is a current server list and read it.
255 # If there is no list try to get one.
257 while (!( open ( FILE
, "< $Conf ::dbdir/lists/server-list.db" )) && ( $count lt 5 )) {
262 message
( "MIRROR ERROR: Could not find or download a server list" );
268 ### Count the number of the servers in the list
272 if ( " $_ " =~ /.*;.*;.*;/ ) {
277 logger
( "MIRROR INFO: $scount servers found in list" );
279 ### Choose a random server and test if it is online
280 # If the check fails try a new server.
281 # This will never give up.
284 while ( $found == 0 ) {
285 $server = int ( rand ( $scount ) + 1 );
287 my ( $line , $proto , $path , $host );
289 foreach $line ( @newlines ) {
291 if ( $servers eq $server ) {
292 @templine = split ( /\;/ , $line );
293 $proto = $templine [ 0 ];
294 $host = $templine [ 1 ];
295 $path = $templine [ 2 ];
296 if ( pinghost
( " $host " )) {
298 return ( $proto , $host , $path );
306 ### Update the database if the file is older than one day.
307 # If you pass &Pakfire::dbgetlist(force) the list will be downloaded.
308 # Usage is always with an argument.
314 if ( - e
" $Conf ::dbdir/lists/packages_list.db" ) {
315 my @stat = stat ( " $Conf ::dbdir/lists/packages_list.db" );
317 $age = $time - $stat [ 9 ];
323 if (( " $age " gt "3600" ) || ( " $force " eq "force" )) {
324 fetchfile
( "lists/packages_list.db" , "" );
325 move
( " $Conf ::cachedir/packages_list.db" , " $Conf ::dbdir/lists/packages_list.db" );
330 ### This subroutine lists the packages.
331 # You may also pass a filter: &Pakfire::dblist(filter)
332 # Usage is always with two arguments.
333 # filter may be: all, notinstalled, installed
341 my ( $name , $version , $release );
344 ### Make sure that the list is not outdated.
345 dbgetlist
( "noforce" );
347 open ( FILE
, "< $Conf ::dbdir/lists/packages_list.db" );
351 if ( " $filter " eq "upgrade" ) {
352 opendir ( DIR
, " $Conf ::dbdir/meta" );
353 my @files = readdir ( DIR
);
355 foreach $file ( @files ) {
356 next if ( $file eq "." );
357 next if ( $file eq ".." );
358 open ( FILE
, "< $Conf ::dbdir/meta/ $file " );
361 foreach $line ( @meta ) {
362 @templine = split ( /\: / , $line );
363 if ( " $templine [0]" eq "Name" ) {
364 $name = $templine [ 1 ];
366 } elsif ( " $templine [0]" eq "ProgVersion" ) {
367 $version = $templine [ 1 ];
369 } elsif ( " $templine [0]" eq "Release" ) {
370 $release = $templine [ 1 ];
374 foreach $prog ( @db ) {
375 @templine = split ( /\;/ , $prog );
376 if (( " $name " eq " $templine [0]" ) && ( " $release " < " $templine [2]" )) {
377 push ( @updatepaks , $name );
378 if ( " $forweb " eq "forweb" ) {
379 print "<option value= \" $name \" >Update: $name -- Version: $version -> $templine [1] -- Release: $release -> $templine [2]</option> \n " ;
381 my $command = "Update: $name \n Version: $version -> $templine [1] \n Release: $release -> $templine [2] \n " ;
382 if ( " $Pakfire ::enable_colors" eq "1" ) {
383 print " $color {'lila'} $command $color {'normal'} \n " ;
397 foreach $line ( sort @db ) {
398 next unless ( $line =~ /.*;.*;.*;/ );
401 @templine = split ( /\;/ , $line );
402 if ( " $filter " eq "notinstalled" ) {
403 next if ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
404 } elsif ( " $filter " eq "installed" ) {
405 next unless ( - e
" $Conf ::dbdir/installed/meta- $templine [0]" );
407 if ( " $forweb " eq "forweb" ) {
408 print "<option value= \" $templine [0] \" > $templine [0]- $templine [1]- $templine [2]</option> \n " ;
410 if ( " $Pakfire ::enable_colors" eq "1" ) {
411 if (& isinstalled
( " $templine [0]" )) {
412 $use_color = " $color {'red'}"
414 $use_color = " $color {'green'}"
417 print "${use_color}Name: $templine [0] \n ProgVersion: $templine [1] \n Release: $templine [2] $color {'normal'} \n\n " ;
420 print " $count packages total. \n " unless ( " $forweb " eq "forweb" );
429 message
( "PAKFIRE RESV: $pak : Resolving dependencies..." );
431 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
436 my ( @templine , @deps , @tempdeps , @all );
437 foreach $line ( @file ) {
438 @templine = split ( /\: / , $line );
439 if ( " $templine [0]" eq "Dependencies" ) {
440 @deps = split ( / / , $templine [ 1 ]);
446 my $return = & isinstalled
( $_ );
448 message
( "PAKFIRE RESV: $pak : Dependency is already installed: $_ " );
450 message
( "PAKFIRE RESV: $pak : Need to install dependency: $_ " );
457 foreach ( @tempdeps ) {
459 my @newdeps = resolvedeps
( " $_ " );
461 unless (( $_ eq " " ) || ( $_ eq "" )) {
462 my $return = & isinstalled
( $_ );
464 message
( "PAKFIRE RESV: $pak : Dependency is already installed: $_ " );
466 message
( "PAKFIRE RESV: $pak : Need to install dependency: $_ " );
482 logger
( "CLEANUP: $dir " );
484 if ( " $dir " eq "meta" ) {
485 $path = " $Conf ::dbdir/meta" ;
486 } elsif ( " $dir " eq "tmp" ) {
487 $path = " $Conf ::tmpdir" ;
491 my @files = readdir ( DIR
);
494 unless (( $_ eq "." ) || ( $_ eq ".." )) {
503 unless ( - e
" $Conf ::dbdir/meta/meta- $pak " ) {
504 fetchfile
( "meta/meta- $pak " , "" );
505 move
( " $Conf ::cachedir/meta- $pak " , " $Conf ::dbdir/meta/meta- $pak " );
508 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
512 open ( FILE
, "> $Conf ::dbdir/meta/meta- $pak " );
515 $string =~ s/\r\n/\n/g ;
527 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
533 foreach $line ( @file ) {
534 @templine = split ( /\: / , $line );
535 if ( " $templine [0]" eq "Size" ) {
548 my $file = getpak
( " $pak " , "noforce" );
550 logger
( "DECRYPT STARTED: $pak " );
551 my $return = system ( "cd $Conf ::tmpdir/ && gpg -d --batch --quiet --no-verbose --status-fd 2 --output - < $Conf ::cachedir/ $file 2>/dev/null | tar x" );
553 logger
( "DECRYPT FINISHED: $pak - Status: $return " );
554 if ( $return != 0 ) { exit 1 ; }
563 open ( FILE
, "< $Conf ::dbdir/meta/meta- $pak " );
570 foreach $line ( @file ) {
571 @templine = split ( /\: / , $line );
572 if ( " $templine [0]" eq "File" ) {
574 $file = $templine [ 1 ];
579 message
( "No filename given in meta-file. Please phone the developers." );
583 unless ( " $force " eq "force" ) {
584 if ( - e
" $Conf ::cachedir/ $file " ) {
589 fetchfile
( "paks/ $file " , "" );
596 message
( "PAKFIRE INST: $pak : Decrypting..." );
599 message
( "PAKFIRE INST: $pak : Copying files and running post-installation scripts..." );
600 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./install.sh >> $Conf ::logdir/install- $pak .log 2>&1" );
602 if ( $pakfiresettings { 'UUID' } ne "off" ) {
603 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&ipak= $pak &return= $return " , " $Conf ::mainserver" );
606 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
608 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
609 message
( "PAKFIRE INST: $pak : Finished." );
612 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
620 eval ( `grep "core_" $Conf ::dbdir/lists/core-list.db` );
621 if ( " $core_release " gt " $Conf ::core_mine" ) {
622 message
( "CORE UPGR: Upgrading from release $Conf ::core_mine to $core_release " );
624 my @seq = `seq $Conf ::core_mine $core_release ` ;
627 foreach $release ( @seq ) {
629 getpak
( "core-upgrade- $release " );
632 foreach $release ( @seq ) {
634 upgradepak
( "core-upgrade- $release " );
637 system ( "echo $core_release > $Conf ::coredir/mine" );
640 message
( "CORE ERROR: No new upgrades available. You are on release $Conf ::core_mine." );
646 if ( open ( FILE
, "< $Conf ::dbdir/installed/meta- $pak " ) ) {
657 message
( "PAKFIRE UPGR: $pak : Decrypting..." );
660 message
( "PAKFIRE UPGR: $pak : Upgrading files and running post-upgrading scripts..." );
661 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./update.sh >> $Conf ::logdir/update- $pak .log 2>&1" );
663 if ( $pakfiresettings { 'UUID' } ne "off" ) {
664 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&upak= $pak &return= $return " , " $Conf ::mainserver" );
667 move
( " $Conf ::tmpdir/ROOTFILES" , " $Conf ::dbdir/rootfiles/ $pak " );
669 copy
( " $Conf ::dbdir/meta/meta- $pak " , " $Conf ::dbdir/installed/" );
670 message
( "PAKFIRE UPGR: $pak : Finished." );
673 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
682 message
( "PAKFIRE REMV: $pak : Decrypting..." );
685 message
( "PAKFIRE REMV: $pak : Removing files and running post-removing scripts..." );
686 my $return = system ( "cd $Conf ::tmpdir && NAME= $pak ./uninstall.sh >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
688 if ( $pakfiresettings { 'UUID' } ne "off" ) {
689 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid&dpak= $pak &return= $return " , " $Conf ::mainserver" );
692 open ( FILE
, "< $Conf ::dbdir/rootfiles/ $pak " );
698 system ( "echo \" Removing: $line \" >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
699 system ( "cd / && rm -rf $line >> $Conf ::logdir/uninstall- $pak .log 2>&1" );
701 unlink ( " $Conf ::dbdir/rootfiles/ $pak " );
702 unlink ( " $Conf ::dbdir/installed/meta- $pak " );
704 message
( "PAKFIRE REMV: $pak : Finished." );
707 message
( "PAKFIRE ERROR: Returncode: $return . Sorry. Please search our forum to find a solution for this problem." );
715 #$size = $size / 1024;
718 if ( $size > 1023 * 1024 ) {
719 $size = ( $size / ( 1024 * 1024 ));
721 } elsif ( $size > 1023 ) {
722 $size = ( $size / 1024 );
727 $size = sprintf ( "%.2f" , $size );
728 my $string = " $size $unit " ;
733 unless ( - e
" $Conf ::dbdir/uuid" ) {
734 open ( FILE
, "</proc/sys/kernel/random/uuid" );
738 open ( FILE
, "> $Conf ::dbdir/uuid" );
747 if ( $pakfiresettings { 'UUID' } ne "off" ) {
748 unless ( " $Conf ::uuid" ) {
749 $Conf :: uuid
= `cat $Conf ::dbdir/uuid` ;
751 logger
( "Sending my uuid: $Conf ::uuid" );
752 fetchfile
( "cgi-bin/counter?ver= $Conf ::version&uuid= $Conf ::uuid" , " $Conf ::mainserver" );
753 system ( "rm -f $Conf ::tmpdir/counter* 2>/dev/null" );
758 logger
( "CRYPTO INFO: Checking GnuPG Database" );
759 my $ret = system ( "gpg --list-keys | grep -q $myid " );
760 unless ( " $ret " eq "0" ) {
761 message
( "CRYPTO WARN: The GnuPG isn't configured corectly. Trying now to fix this." );
762 message
( "CRYPTO WARN: It's normal to see this on first execution." );
763 my $command = "gpg --keyserver pgp.mit.edu --always-trust --status-fs 2" ;
764 system ( " $command --recv-key $myid >> $Conf ::logdir/gnupg-database.log 2>&1" );
765 system ( " $command --recv-key $trustid >> $Conf ::logdir/gnupg-database.log 2>&1" );
767 logger
( "CRYPTO INFO: Database is okay" );
772 my ( $data , $response , $protocol ) = @_ ;
773 $final_data .= $data ;
774 print progress_bar
( length ( $final_data ), $total_size , 30 , '=' );
778 my ( $got , $total , $width , $char ) = @_ ;
780 $width ||= 30 ; $char ||= '=' ;
781 my $len_bfile = length $bfile ;
782 if ( " $len_bfile " >= "17" ) {
783 $show_bfile = substr ( $bfile , 0 , 17 ). "..." ;
785 $show_bfile = $bfile ;
787 $progress = sprintf ( "%.2f%%" , 100 * $got /+ $total );
788 sprintf " $color {'lightgreen'}%-20s %7s |%-${width}s| %10s $color {'normal'} \r " , $show_bfile , $progress , $char x
(( $width - 1 )* $got / $total ). '>' , beautifysize
( $got );