]>
git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/pakfire.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
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 ###############################################################################
23 use List
::Util
qw(any);
26 # enable only the following on debugging purpose
28 #use CGI::Carp 'fatalsToBrowser';
30 require '/var/ipfire/general-functions.pl';
31 require "${General::swroot}/lang.pl";
32 require "${General::swroot}/header.pl";
33 require "/opt/pakfire/lib/functions.pl";
36 my $errormessage = '';
38 my %pakfiresettings = ();
39 my %mainsettings = ();
41 # The page mode is used to explictly switch between user interface functions:
42 my $PM_DEFAULT = 'default'; # Default user interface with command processing
43 my $PM_LOGREAD = 'logread'; # Log messages viewer (ignores all commands)
44 my $pagemode = $PM_DEFAULT;
46 # Load general settings
47 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
48 &General
::readhash
("${General::swroot}/pakfire/settings", \
%pakfiresettings);
49 &General
::readhash
("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \
%color);
51 # Get CGI POST request data
52 $cgiparams{'ACTION'} = '';
53 $cgiparams{'FORCE'} = '';
55 $cgiparams{'INSPAKS'} = '';
56 $cgiparams{'DELPAKS'} = '';
58 &Header
::getcgihash
(\
%cgiparams);
60 # Get CGI GET request data (if available)
61 if($ENV{'QUERY_STRING'}) {
62 my $uri = URI
->new($ENV{'REQUEST_URI'});
63 my %query = $uri->query_form;
65 my $mode = lc($query{'mode'} // '');
66 if(($mode eq $PM_DEFAULT) || ($mode eq $PM_LOGREAD)) {
67 $pagemode = $mode; # Limit to existing modes
71 ### Process AJAX/JSON request ###
72 if($cgiparams{'ACTION'} eq 'json-getstatus') {
76 # Read /var/log/messages backwards until a "Pakfire started" header is found,
77 # to capture all messages of the last (i.e. current) Pakfire run
78 my @messages = `tac /var/log/messages 2>/dev/null | sed -n '/pakfire:/{p;/Pakfire.*started/q}'`;
80 # Test if the log contains an error message (fastest implementation, stops at first match)
81 my $failure = any
{ index($_, 'ERROR') != -1 } @messages;
83 # Collect Pakfire status
85 'running' => &_is_pakfire_busy
() || "0",
86 'running_since' => &General
::age
("$Pakfire::lockfile") || "0s",
87 'reboot' => (-e
"/var/run/need_reboot") || "0",
88 'failure' => $failure || "0"
94 foreach my $key (keys %status) {
95 my $value = $status{$key};
96 print qq{\t"$key": "$value",\n};
99 # Print sanitized messages in reverse order to undo previous "tac"
100 print qq{\t"messages": [\n};
101 for my $index (reverse (0 .. $#messages)) {
102 my $line = $messages[$index];
103 $line =~ s/[[:cntrl:]<>&\\]+//g;
105 print qq{\t\t"$line"};
106 print ",\n" unless $index < 1;
110 # Finalize JSON file & stop
115 ### Process Pakfire install/update commands ###
116 if(($cgiparams{'ACTION'} ne '') && ($pagemode eq $PM_DEFAULT)) {
117 if(&_is_pakfire_busy
()) {
118 $errormessage = $Lang::tr
{'pakfire already busy'};
119 $pagemode = $PM_LOGREAD; # Running Pakfire instance found, switch to log viewer mode
120 } elsif(($cgiparams{'ACTION'} eq 'install') && ($cgiparams{'FORCE'} eq 'on')) {
121 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
122 &General
::system_background
("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
123 &_http_pagemode_redirect
($PM_LOGREAD, 1);
124 } elsif(($cgiparams{'ACTION'} eq 'remove') && ($cgiparams{'FORCE'} eq 'on')) {
125 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
126 &General
::system_background
("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
127 &_http_pagemode_redirect
($PM_LOGREAD, 1);
128 } elsif($cgiparams{'ACTION'} eq 'update') {
129 &General
::system_background
("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
130 &_http_pagemode_redirect
($PM_LOGREAD, 1);
131 } elsif($cgiparams{'ACTION'} eq 'upgrade') {
132 &General
::system_background
("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
133 &_http_pagemode_redirect
($PM_LOGREAD, 1);
134 } elsif($cgiparams{'ACTION'} eq $Lang::tr
{'save'}) {
135 $pakfiresettings{"TREE"} = $cgiparams{"TREE"};
137 # Check for valid input
138 if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
139 $errormessage .= $Lang::tr
{'pakfire invalid tree'};
142 unless ($errormessage) {
143 &General
::writehash
("${General::swroot}/pakfire/settings", \
%pakfiresettings);
146 &General
::system_background
("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
147 &_http_pagemode_redirect
($PM_LOGREAD, 1);
152 ### Start pakfire page ###
153 &Header
::showhttpheaders
();
155 ###--- HTML HEAD ---###
156 my $extraHead = <<END
161 border-style: hidden;
169 #pfmain tr:not(:last-child) > td {
170 padding-bottom: 1.5em;
172 #pfmain tr > td.heading {
175 background-color: $color{'color20'};
181 margin-bottom: 0.8em;
184 /* Pakfire log viewer */
185 section#pflog-header {
192 #pflog-header > div:last-child {
199 #pflog-header span:empty::before {
200 content: "\\200b"; /* zero width space */
206 border-top: 0.5px solid $Header::bordercolour;
214 <script src="/include/pakfire.js"></script>
218 'working': '$Lang::tr{'pakfire working'}',
219 'finished': '$Lang::tr{'pakfire finished'}',
220 'finished error': '$Lang::tr{'pakfire finished error'}',
221 'since': '$Lang::tr{'since'}',
223 'link_return': '<a href="$ENV{'SCRIPT_NAME'}">$Lang::tr{'pakfire return'}</a>',
224 'link_reboot': '<a href="/cgi-bin/shutdown.cgi">$Lang::tr{'needreboot'}</a>'
227 // AJAX auto refresh interval (in ms, default: 1000)
228 //pakfire.refreshInterval = 1000;
230 // Enable returning to main screen (delay in ms)
231 pakfire.setupPageReload(true, 3000);
235 ###--- END HTML HEAD ---###
237 &Header
::openpage
($Lang::tr
{'pakfire configuration'}, 1, $extraHead);
238 &Header
::openbigbox
('100%', 'left', '', $errormessage);
242 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
243 print "<font class='base'>$errormessage </font>\n";
247 # Show only log output while Pakfire is running and stop afterwards
248 if(($pagemode eq $PM_LOGREAD) || (&_is_pakfire_busy
())) {
249 &Header
::openbox
("100%", "center", "Pakfire");
252 <section id="pflog-header">
253 <div><img src="/images/indicator.gif" alt="$Lang::tr{'active'}" title="$Lang::tr{'pagerefresh'}"></div>
255 <span id="pflog-status">$Lang::tr{'pakfire working'}</span><br>
256 <span id="pflog-time"></span><br>
257 <span id="pflog-action"></span>
259 <div><a href="$ENV{'SCRIPT_NAME'}"><img src="/images/view-refresh.png" alt="$Lang::tr{'refresh'}" title="$Lang::tr{'refresh'}"></a></div>
262 <!-- Pakfire log messages -->
263 <pre id="pflog-messages"></pre>
265 // Start automatic log refresh
266 pakfire.running = true;
273 &Header
::closebigbox
();
274 &Header
::closepage
();
278 # Show Pakfire install/remove dependencies and confirm form
279 # (_is_pakfire_busy status was checked before and can be omitted)
280 if (($cgiparams{'ACTION'} eq 'install') && ($pagemode eq $PM_DEFAULT)) {
281 &Header
::openbox
("100%", "center", $Lang::tr
{'request'});
283 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
284 my @output = &General
::system_output
("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
286 <table style="width: 100%"><tr><td colspan='2'><p>$Lang::tr{'pakfire install package'} <strong>@{pkgs}</strong><br>
287 $Lang::tr{'pakfire possible dependency'}</p>
291 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
296 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
297 <tr><td colspan='2'> </td></tr>
298 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
299 <input type='hidden' name='INSPAKS' value='$cgiparams{'INSPAKS'}' />
300 <input type='hidden' name='FORCE' value='on' />
301 <input type='hidden' name='ACTION' value='install' />
302 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/go-next.png' />
306 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
307 <input type='hidden' name='ACTION' value='' />
308 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
315 &Header
::closebigbox
();
316 &Header
::closepage
();
319 } elsif (($cgiparams{'ACTION'} eq 'remove') && ($pagemode eq $PM_DEFAULT)) {
320 &Header
::openbox
("100%", "center", $Lang::tr
{'request'});
322 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
323 my @output = &General
::system_output
("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
325 <table style="width: 100%"><tr><td colspan='2'><p>$Lang::tr{'pakfire uninstall package'} <strong>@{pkgs}</strong><br>
326 $Lang::tr{'pakfire possible dependency'}</p>
330 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
335 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
336 <tr><td colspan='2'> </td></tr>
337 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
338 <input type='hidden' name='DELPAKS' value='$cgiparams{'DELPAKS'}' />
339 <input type='hidden' name='FORCE' value='on' />
340 <input type='hidden' name='ACTION' value='remove' />
341 <input type='image' alt='$Lang::tr{'uninstall'}' title='$Lang::tr{'uninstall'}' src='/images/go-next.png' />
345 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
346 <input type='hidden' name='ACTION' value='' />
347 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
354 &Header
::closebigbox
();
355 &Header
::closepage
();
359 # Show Pakfire main page
363 $selected{"TREE"} = ();
364 $selected{"TREE"}{"stable"} = "";
365 $selected{"TREE"}{"testing"} = "";
366 $selected{"TREE"}{"unstable"} = "";
367 $selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
369 my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
370 chomp($core_release);
371 my $core_update_age = &General
::age
("/opt/pakfire/db/core/mine");
372 my $corelist_update_age = &General
::age
("/opt/pakfire/db/lists/core-list.db");
373 my $server_update_age = &General
::age
("/opt/pakfire/db/lists/server-list.db");
374 my $packages_update_age = &General
::age
("/opt/pakfire/db/lists/packages_list.db");
376 &Header
::openbox
("100%", "center", "Pakfire");
381 if ( -e
"/var/run/need_reboot") {
382 print "\t\t<tr><td colspan='2'><a href='/cgi-bin/shutdown.cgi'>$Lang::tr{'needreboot'}!</a></td></tr>\n";
385 <tr><td class="heading">$Lang::tr{'pakfire system state'}:</td>
386 <td class="heading">$Lang::tr{'available updates'}:</td></tr>
388 <tr><td><strong>$Lang::tr{'pakfire core update level'}: $core_release</strong>
391 $Lang::tr{'pakfire last update'} $core_update_age $Lang::tr{'pakfire ago'}<br>
392 $Lang::tr{'pakfire last serverlist update'} $server_update_age $Lang::tr{'pakfire ago'}<br>
393 $Lang::tr{'pakfire last core list update'} $corelist_update_age $Lang::tr{'pakfire ago'}<br>
394 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
396 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
397 <input type='hidden' name='ACTION' value='update' />
398 <input type='submit' value='$Lang::tr{'calamaris refresh list'}' />
402 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
403 <select name="UPDPAKS" class="pflist" size="5" disabled>
406 &Pakfire
::dblist
("upgrade", "forweb");
409 <input type='hidden' name='ACTION' value='upgrade' />
410 <input type='image' alt='$Lang::tr{'upgrade'}' title='$Lang::tr{'upgrade'}' src='/images/document-save.png' />
414 <tr><td class="heading">$Lang::tr{'pakfire available addons'}</td>
415 <td class="heading">$Lang::tr{'pakfire installed addons'}</td></tr>
417 <tr><td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire install description'}</p>
418 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
419 <select name="INSPAKS" class="pflist" size="10" multiple>
422 &Pakfire
::dblist
("notinstalled", "forweb");
425 <input type='hidden' name='ACTION' value='install' />
426 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/list-add.png' />
429 <td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire uninstall description'}</p>
430 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
431 <select name="DELPAKS" class="pflist" size="10" multiple>
434 &Pakfire
::dblist
("installed", "forweb");
437 <input type='hidden' name='ACTION' value='remove' />
438 <input type='image' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' src='/images/list-remove.png' />
446 &Header
::openbox
("100%", "center", "$Lang::tr{'settings'}");
449 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
452 <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
453 <td width="55%" align="left">
455 <option value="stable" $selected{"TREE"}{"stable"}>$Lang::tr{'pakfire tree stable'}</option>
456 <option value="testing" $selected{"TREE"}{"testing"}>$Lang::tr{'pakfire tree testing'}</option>
457 <option value="unstable" $selected{"TREE"}{"unstable"}>$Lang::tr{'pakfire tree unstable'}</option>
462 <td colspan="2"> </td>
465 <td colspan="2" align="center">
466 <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
474 &Header
::closebigbox
();
475 &Header
::closepage
();
477 ###--- Internal functions ---###
479 # Check if pakfire is already running (extend test here if necessary)
480 sub _is_pakfire_busy
{
481 # Return immediately if lockfile is present
482 if(-e
"$Pakfire::lockfile") {
486 # Check if a PID of a running pakfire instance is found
487 # (The system backpipe command is safe, because no user input is computed.)
488 my $pakfire_pid = `pidof -s /usr/local/bin/pakfire`;
495 # Pakfire isn't running
500 sub _start_json_output
{
501 print "Cache-Control: no-cache, no-store\n";
502 print "Content-Type: application/json\n";
503 print "\n"; # End of HTTP headers
506 # Send HTTP 303 redirect headers to change page mode
507 # GET is always used to display the redirected page, which will remove already processed POST form data.
508 # Note: Custom headers must be sent before the HTML output is started by &Header::showhttpheaders().
509 # If switch_mode is set to true, the global page mode variable ("$pagemode") is also updated immediately.
510 sub _http_pagemode_redirect
{
511 my ($mode, $switch_mode) = @_;
512 $mode //= $PM_DEFAULT;
515 # Send HTTP redirect with GET parameter
516 my $location = "https://$ENV{'SERVER_NAME'}:$ENV{'SERVER_PORT'}$ENV{'SCRIPT_NAME'}?mode=${mode}";
517 print "Status: 303 See Other\n";
518 print "Location: $location\n";
520 # Change global page mode