]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/pakfire.cgi
pakfire.cgi: Discard tac stderr output
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / pakfire.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23 use List::Util qw(any);
24 use URI;
25
26 # enable only the following on debugging purpose
27 #use warnings;
28 #use CGI::Carp 'fatalsToBrowser';
29
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";
34
35 my %cgiparams=();
36 my $errormessage = '';
37 my %color = ();
38 my %pakfiresettings = ();
39 my %mainsettings = ();
40
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;
45
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);
50
51 # Get CGI POST request data
52 $cgiparams{'ACTION'} = '';
53 $cgiparams{'FORCE'} = '';
54
55 $cgiparams{'INSPAKS'} = '';
56 $cgiparams{'DELPAKS'} = '';
57
58 &Header::getcgihash(\%cgiparams);
59
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;
64
65 my $mode = lc($query{'mode'} // '');
66 if(($mode eq $PM_DEFAULT) || ($mode eq $PM_LOGREAD)) {
67 $pagemode = $mode; # Limit to existing modes
68 }
69 }
70
71 ### Process AJAX/JSON request ###
72 if($cgiparams{'ACTION'} eq 'json-getstatus') {
73 # Send HTTP headers
74 _start_json_output();
75
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}'`;
79
80 # Test if the log contains an error message (fastest implementation, stops at first match)
81 my $failure = any{ index($_, 'ERROR') != -1 } @messages;
82
83 # Collect Pakfire status
84 my %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"
89 );
90
91 # Start JSON file
92 print "{\n";
93
94 foreach my $key (keys %status) {
95 my $value = $status{$key};
96 print qq{\t"$key": "$value",\n};
97 }
98
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;
104
105 print qq{\t\t"$line"};
106 print ",\n" unless $index < 1;
107 }
108 print "\n\t]\n";
109
110 # Finalize JSON file & stop
111 print "}";
112 exit;
113 }
114
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"};
136
137 # Check for valid input
138 if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
139 $errormessage .= $Lang::tr{'pakfire invalid tree'};
140 }
141
142 unless ($errormessage) {
143 &General::writehash("${General::swroot}/pakfire/settings", \%pakfiresettings);
144
145 # Update lists
146 &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
147 &_http_pagemode_redirect($PM_LOGREAD, 1);
148 }
149 }
150 }
151
152 ### Start pakfire page ###
153 &Header::showhttpheaders();
154
155 ###--- HTML HEAD ---###
156 my $extraHead = <<END
157 <style>
158 /* Main screen */
159 table#pfmain {
160 width: 100%;
161 border-style: hidden;
162 table-layout: fixed;
163 }
164
165 #pfmain td {
166 padding: 5px 20px 0;
167 text-align: center;
168 }
169 #pfmain tr:not(:last-child) > td {
170 padding-bottom: 1.5em;
171 }
172 #pfmain tr > td.heading {
173 padding: 0;
174 font-weight: bold;
175 background-color: $color{'color20'};
176 }
177
178 .pflist {
179 width: 100%;
180 text-align: left;
181 margin-bottom: 0.8em;
182 }
183
184 /* Pakfire log viewer */
185 section#pflog-header {
186 width: 100%;
187 display: flex;
188 text-align: left;
189 align-items: center;
190 column-gap: 20px;
191 }
192 #pflog-header > div:last-child {
193 margin-left: auto;
194 margin-right: 20px;
195 }
196 #pflog-header span {
197 line-height: 1.3em;
198 }
199 #pflog-header span:empty::before {
200 content: "\\200b"; /* zero width space */
201 }
202
203 pre#pflog-messages {
204 margin-top: 0.7em;
205 padding-top: 0.7em;
206 border-top: 0.5px solid $Header::bordercolour;
207
208 text-align: left;
209 min-height: 15em;
210 overflow-x: auto;
211 }
212 </style>
213
214 <script src="/include/pakfire.js"></script>
215 <script>
216 // Translations
217 pakfire.i18n.load({
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'}',
222
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>'
225 });
226
227 // AJAX auto refresh interval (in ms, default: 1000)
228 //pakfire.refreshInterval = 1000;
229
230 // Enable returning to main screen (delay in ms)
231 pakfire.setupPageReload(true, 3000);
232 </script>
233 END
234 ;
235 ###--- END HTML HEAD ---###
236
237 &Header::openpage($Lang::tr{'pakfire configuration'}, 1, $extraHead);
238 &Header::openbigbox('100%', 'left', '', $errormessage);
239
240 # Show error message
241 if ($errormessage) {
242 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
243 print "<font class='base'>$errormessage&nbsp;</font>\n";
244 &Header::closebox();
245 }
246
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");
250
251 print <<END
252 <section id="pflog-header">
253 <div><img src="/images/indicator.gif" alt="$Lang::tr{'active'}" title="$Lang::tr{'pagerefresh'}"></div>
254 <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>
258 </div>
259 <div><a href="$ENV{'SCRIPT_NAME'}"><img src="/images/view-refresh.png" alt="$Lang::tr{'refresh'}" title="$Lang::tr{'refresh'}"></a></div>
260 </section>
261
262 <!-- Pakfire log messages -->
263 <pre id="pflog-messages"></pre>
264 <script>
265 // Start automatic log refresh
266 pakfire.running = true;
267 </script>
268
269 END
270 ;
271
272 &Header::closebox();
273 &Header::closebigbox();
274 &Header::closepage();
275 exit;
276 }
277
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'});
282
283 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
284 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
285 print <<END;
286 <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'} @pkgs $Lang::tr{'pakfire possible dependency'}
287 <pre>
288 END
289 foreach (@output) {
290 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
291 print "$_\n";
292 }
293 print <<END;
294 </pre></td></tr>
295 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
296 <tr><td colspan='2'>&nbsp;</td></tr>
297 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
298 <input type='hidden' name='INSPAKS' value='$cgiparams{'INSPAKS'}' />
299 <input type='hidden' name='FORCE' value='on' />
300 <input type='hidden' name='ACTION' value='install' />
301 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/go-next.png' />
302 </form>
303 </td>
304 <td align='left'>
305 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
306 <input type='hidden' name='ACTION' value='' />
307 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
308 </form>
309 </td>
310 </tr>
311 </table>
312 END
313 &Header::closebox();
314 &Header::closebigbox();
315 &Header::closepage();
316 exit;
317
318 } elsif (($cgiparams{'ACTION'} eq 'remove') && ($pagemode eq $PM_DEFAULT)) {
319 &Header::openbox("100%", "center", $Lang::tr{'request'});
320
321 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
322 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
323 print <<END;
324 <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'} @pkgs $Lang::tr{'pakfire possible dependency'}
325 <pre>
326 END
327 foreach (@output) {
328 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
329 print "$_\n";
330 }
331 print <<END;
332 </pre></td></tr>
333 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
334 <tr><td colspan='2'>&nbsp;</td></tr>
335 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
336 <input type='hidden' name='DELPAKS' value='$cgiparams{'DELPAKS'}' />
337 <input type='hidden' name='FORCE' value='on' />
338 <input type='hidden' name='ACTION' value='remove' />
339 <input type='image' alt='$Lang::tr{'uninstall'}' title='$Lang::tr{'uninstall'}' src='/images/go-next.png' />
340 </form>
341 </td>
342 <td align='left'>
343 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
344 <input type='hidden' name='ACTION' value='' />
345 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
346 </form>
347 </td>
348 </tr>
349 </table>
350 END
351 &Header::closebox();
352 &Header::closebigbox();
353 &Header::closepage();
354 exit;
355 }
356
357 # Show Pakfire main page
358 my %selected=();
359 my %checked=();
360
361 $selected{"TREE"} = ();
362 $selected{"TREE"}{"stable"} = "";
363 $selected{"TREE"}{"testing"} = "";
364 $selected{"TREE"}{"unstable"} = "";
365 $selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
366
367 my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
368 chomp($core_release);
369 my $core_update_age = &General::age("/opt/pakfire/db/core/mine");
370 my $corelist_update_age = &General::age("/opt/pakfire/db/lists/core-list.db");
371 my $server_update_age = &General::age("/opt/pakfire/db/lists/server-list.db");
372 my $packages_update_age = &General::age("/opt/pakfire/db/lists/packages_list.db");
373
374 &Header::openbox("100%", "center", "Pakfire");
375
376 print <<END;
377 <table id="pfmain">
378 END
379 if ( -e "/var/run/need_reboot") {
380 print "\t\t<tr><td colspan='2'><a href='/cgi-bin/shutdown.cgi'>$Lang::tr{'needreboot'}!</a></td></tr>\n";
381 }
382 print <<END;
383 <tr><td class="heading">$Lang::tr{'pakfire system state'}:</td>
384 <td class="heading">$Lang::tr{'available updates'}:</td></tr>
385
386 <tr><td><strong>$Lang::tr{'pakfire core update level'}: $core_release</strong>
387 <hr>
388 <div class="pflist">
389 $Lang::tr{'pakfire last update'} $core_update_age $Lang::tr{'pakfire ago'}<br>
390 $Lang::tr{'pakfire last serverlist update'} $server_update_age $Lang::tr{'pakfire ago'}<br>
391 $Lang::tr{'pakfire last core list update'} $corelist_update_age $Lang::tr{'pakfire ago'}<br>
392 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
393 </div>
394 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
395 <input type='hidden' name='ACTION' value='update' />
396 <input type='submit' value='$Lang::tr{'calamaris refresh list'}' />
397 </form>
398 </td>
399 <td>
400 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
401 <select name="UPDPAKS" class="pflist" size="5" disabled>
402 END
403
404 &Pakfire::dblist("upgrade", "forweb");
405 print <<END;
406 </select>
407 <input type='hidden' name='ACTION' value='upgrade' />
408 <input type='image' alt='$Lang::tr{'upgrade'}' title='$Lang::tr{'upgrade'}' src='/images/document-save.png' />
409 </form>
410 </td>
411 </tr>
412 <tr><td class="heading">$Lang::tr{'pakfire available addons'}</td>
413 <td class="heading">$Lang::tr{'pakfire installed addons'}</td></tr>
414
415 <tr><td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire install description'}</p>
416 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
417 <select name="INSPAKS" class="pflist" size="10" multiple>
418 END
419
420 &Pakfire::dblist("notinstalled", "forweb");
421 print <<END;
422 </select>
423 <input type='hidden' name='ACTION' value='install' />
424 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/list-add.png' />
425 </form>
426 </td>
427 <td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire uninstall description'}</p>
428 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
429 <select name="DELPAKS" class="pflist" size="10" multiple>
430 END
431
432 &Pakfire::dblist("installed", "forweb");
433 print <<END;
434 </select>
435 <input type='hidden' name='ACTION' value='remove' />
436 <input type='image' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' src='/images/list-remove.png' />
437 </form>
438 </td>
439 </tr>
440 </table>
441 END
442
443 &Header::closebox();
444 &Header::openbox("100%", "center", "$Lang::tr{'settings'}");
445
446 print <<END;
447 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
448 <table width='95%'>
449 <tr>
450 <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
451 <td width="55%" align="left">
452 <select name="TREE">
453 <option value="stable" $selected{"TREE"}{"stable"}>$Lang::tr{'pakfire tree stable'}</option>
454 <option value="testing" $selected{"TREE"}{"testing"}>$Lang::tr{'pakfire tree testing'}</option>
455 <option value="unstable" $selected{"TREE"}{"unstable"}>$Lang::tr{'pakfire tree unstable'}</option>
456 </select>
457 </td>
458 </tr>
459 <tr>
460 <td colspan="2">&nbsp;</td>
461 </tr>
462 <tr>
463 <td colspan="2" align="center">
464 <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
465 </td>
466 </tr>
467 </table>
468 </form>
469 END
470
471 &Header::closebox();
472 &Header::closebigbox();
473 &Header::closepage();
474
475 ###--- Internal functions ---###
476
477 # Check if pakfire is already running (extend test here if necessary)
478 sub _is_pakfire_busy {
479 # Return immediately if lockfile is present
480 if(-e "$Pakfire::lockfile") {
481 return 1;
482 }
483
484 # Check if a PID of a running pakfire instance is found
485 # (The system backpipe command is safe, because no user input is computed.)
486 my $pakfire_pid = `pidof -s /usr/local/bin/pakfire`;
487 chomp($pakfire_pid);
488
489 if($pakfire_pid) {
490 return 1;
491 }
492
493 # Pakfire isn't running
494 return 0;
495 }
496
497 # Send HTTP headers
498 sub _start_json_output {
499 print "Cache-Control: no-cache, no-store\n";
500 print "Content-Type: application/json\n";
501 print "\n"; # End of HTTP headers
502 }
503
504 # Send HTTP 303 redirect headers to change page mode
505 # GET is always used to display the redirected page, which will remove already processed POST form data.
506 # Note: Custom headers must be sent before the HTML output is started by &Header::showhttpheaders().
507 # If switch_mode is set to true, the global page mode variable ("$pagemode") is also updated immediately.
508 sub _http_pagemode_redirect {
509 my ($mode, $switch_mode) = @_;
510 $mode //= $PM_DEFAULT;
511 $switch_mode //= 0;
512
513 # Send HTTP redirect with GET parameter
514 my $location = "https://$ENV{'SERVER_NAME'}:$ENV{'SERVER_PORT'}$ENV{'SCRIPT_NAME'}?mode=${mode}";
515 print "Status: 303 See Other\n";
516 print "Location: $location\n";
517
518 # Change global page mode
519 if($switch_mode) {
520 $pagemode = $mode;
521 }
522 }