]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/pakfire.cgi
pakfire.cgi: Show error and log messages earlier
[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
25 # enable only the following on debugging purpose
26 #use warnings;
27 #use CGI::Carp 'fatalsToBrowser';
28
29 require '/var/ipfire/general-functions.pl';
30 require "${General::swroot}/lang.pl";
31 require "${General::swroot}/header.pl";
32 require "/opt/pakfire/lib/functions.pl";
33
34 my %cgiparams=();
35 my $errormessage = '';
36 my %color = ();
37 my %pakfiresettings = ();
38 my %mainsettings = ();
39
40 # Load general settings
41 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
42 &General::readhash("${General::swroot}/pakfire/settings", \%pakfiresettings);
43 &General::readhash("/srv/web/ipfire/html/themes/ipfire/include/colors.txt", \%color);
44
45 # Get CGI request data
46 $cgiparams{'ACTION'} = '';
47 $cgiparams{'FORCE'} = '';
48
49 $cgiparams{'INSPAKS'} = '';
50 $cgiparams{'DELPAKS'} = '';
51
52 &Header::getcgihash(\%cgiparams);
53
54 ### Process AJAX/JSON request ###
55 if($cgiparams{'ACTION'} eq 'json-getstatus') {
56 # Send HTTP headers
57 _start_json_output();
58
59 # Read /var/log/messages backwards until a "Pakfire started" header is found,
60 # to capture all messages of the last (i.e. current) Pakfire run
61 my @messages = `tac /var/log/messages | sed -n '/pakfire:/{p;/Pakfire.*started/q}'`;
62
63 # Test if the log contains an error message (fastest implementation, stops at first match)
64 my $failure = any{ index($_, 'ERROR') != -1 } @messages;
65
66 # Collect Pakfire status
67 my %status = (
68 'running' => &_is_pakfire_busy() || "0",
69 'running_since' => &General::age("$Pakfire::lockfile") || "0s",
70 'reboot' => (-e "/var/run/need_reboot") || "0",
71 'failure' => $failure || "0"
72 );
73
74 # Start JSON file
75 print "{\n";
76
77 foreach my $key (keys %status) {
78 my $value = $status{$key};
79 print qq{\t"$key": "$value",\n};
80 }
81
82 # Print sanitized messages in reverse order to undo previous "tac"
83 print qq{\t"messages": [\n};
84 for my $index (reverse (0 .. $#messages)) {
85 my $line = $messages[$index];
86 $line =~ s/[[:cntrl:]<>&\\]+//g;
87
88 print qq{\t\t"$line"};
89 print ",\n" unless $index < 1;
90 }
91 print "\n\t]\n";
92
93 # Finalize JSON file & stop
94 print "}";
95 exit;
96 }
97
98 ### Process Pakfire install/update commands ###
99 if(($cgiparams{'ACTION'} ne '') && (! &_is_pakfire_busy())) {
100 if(($cgiparams{'ACTION'} eq 'install') && ($cgiparams{'FORCE'} eq 'on')) {
101 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
102 &General::system_background("/usr/local/bin/pakfire", "install", "--non-interactive", "--no-colors", @pkgs);
103 } elsif(($cgiparams{'ACTION'} eq 'remove') && ($cgiparams{'FORCE'} eq 'on')) {
104 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
105 &General::system_background("/usr/local/bin/pakfire", "remove", "--non-interactive", "--no-colors", @pkgs);
106 } elsif($cgiparams{'ACTION'} eq 'update') {
107 &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
108 } elsif($cgiparams{'ACTION'} eq 'upgrade') {
109 &General::system_background("/usr/local/bin/pakfire", "upgrade", "-y", "--no-colors");
110 } elsif($cgiparams{'ACTION'} eq $Lang::tr{'save'}) {
111 $pakfiresettings{"TREE"} = $cgiparams{"TREE"};
112
113 # Check for valid input
114 if ($pakfiresettings{"TREE"} !~ m/^(stable|testing|unstable)$/) {
115 $errormessage .= $Lang::tr{'pakfire invalid tree'};
116 }
117
118 unless ($errormessage) {
119 &General::writehash("${General::swroot}/pakfire/settings", \%pakfiresettings);
120
121 # Update lists
122 &General::system_background("/usr/local/bin/pakfire", "update", "--force", "--no-colors");
123 }
124 }
125 }
126
127 ### Start pakfire page ###
128 &Header::showhttpheaders();
129
130 ###--- HTML HEAD ---###
131 my $extraHead = <<END
132 <style>
133 /* Main screen */
134 table#pfmain {
135 width: 100%;
136 border-style: hidden;
137 table-layout: fixed;
138 }
139
140 #pfmain td {
141 padding: 5px 20px 0;
142 text-align: center;
143 }
144 #pfmain tr:not(:last-child) > td {
145 padding-bottom: 1.5em;
146 }
147 #pfmain tr > td.heading {
148 padding: 0;
149 font-weight: bold;
150 background-color: $color{'color20'};
151 }
152
153 .pflist {
154 width: 100%;
155 text-align: left;
156 margin-bottom: 0.8em;
157 }
158
159 /* Pakfire log viewer */
160 section#pflog-header {
161 width: 100%;
162 display: flex;
163 text-align: left;
164 align-items: center;
165 column-gap: 20px;
166 }
167 #pflog-header > div:last-child {
168 margin-left: auto;
169 margin-right: 20px;
170 }
171 #pflog-header span {
172 line-height: 1.3em;
173 }
174 #pflog-header span:empty::before {
175 content: "\\200b"; /* zero width space */
176 }
177
178 pre#pflog-messages {
179 margin-top: 0.7em;
180 padding-top: 0.7em;
181 border-top: 0.5px solid $Header::bordercolour;
182
183 text-align: left;
184 min-height: 15em;
185 overflow-x: auto;
186 }
187 </style>
188
189 <script src="/include/pakfire.js"></script>
190 <script>
191 // Translations
192 pakfire.i18n.load({
193 'working': '$Lang::tr{'pakfire working'}',
194 'finished': '$Lang::tr{'pakfire finished'}',
195 'finished error': '$Lang::tr{'pakfire finished error'}',
196 'since': '$Lang::tr{'since'}',
197
198 'link_return': '<a href="$ENV{'SCRIPT_NAME'}">$Lang::tr{'pakfire return'}</a>',
199 'link_reboot': '<a href="/cgi-bin/shutdown.cgi">$Lang::tr{'needreboot'}</a>'
200 });
201
202 // AJAX auto refresh interval (in ms, default: 1000)
203 //pakfire.refreshInterval = 1000;
204
205 // Enable returning to main screen (delay in ms)
206 pakfire.setupPageReload(true, 3000);
207 </script>
208 END
209 ;
210 ###--- END HTML HEAD ---###
211
212 &Header::openpage($Lang::tr{'pakfire configuration'}, 1, $extraHead);
213 &Header::openbigbox('100%', 'left', '', $errormessage);
214
215 # Show error message
216 if ($errormessage) {
217 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
218 print "<font class='base'>$errormessage&nbsp;</font>\n";
219 &Header::closebox();
220 }
221
222 # Show log output while Pakfire is running
223 if(&_is_pakfire_busy()) {
224 &Header::openbox("100%", "center", "Pakfire");
225
226 print <<END
227 <section id="pflog-header">
228 <div><img src="/images/indicator.gif" alt="$Lang::tr{'active'}" title="$Lang::tr{'pagerefresh'}"></div>
229 <div>
230 <span id="pflog-status">$Lang::tr{'pakfire working'}</span><br>
231 <span id="pflog-time"></span><br>
232 <span id="pflog-action"></span>
233 </div>
234 <div><a href="$ENV{'SCRIPT_NAME'}"><img src="/images/view-refresh.png" alt="$Lang::tr{'refresh'}" title="$Lang::tr{'refresh'}"></a></div>
235 </section>
236
237 <!-- Pakfire log messages -->
238 <pre id="pflog-messages"></pre>
239 <script>
240 // Start automatic log refresh
241 pakfire.running = true;
242 </script>
243
244 END
245 ;
246
247 &Header::closebox();
248 &Header::closebigbox();
249 &Header::closepage();
250 exit;
251 }
252
253 # Show Pakfire install/remove dependencies and confirm form
254 if (($cgiparams{'ACTION'} eq 'install') && (! &_is_pakfire_busy())) {
255 &Header::openbox("100%", "center", $Lang::tr{'request'});
256
257 my @pkgs = split(/\|/, $cgiparams{'INSPAKS'});
258 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
259 print <<END;
260 <table><tr><td colspan='2'>$Lang::tr{'pakfire install package'} @pkgs $Lang::tr{'pakfire possible dependency'}
261 <pre>
262 END
263 foreach (@output) {
264 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
265 print "$_\n";
266 }
267 print <<END;
268 </pre></td></tr>
269 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
270 <tr><td colspan='2'>&nbsp;</td></tr>
271 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
272 <input type='hidden' name='INSPAKS' value='$cgiparams{'INSPAKS'}' />
273 <input type='hidden' name='FORCE' value='on' />
274 <input type='hidden' name='ACTION' value='install' />
275 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/go-next.png' />
276 </form>
277 </td>
278 <td align='left'>
279 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
280 <input type='hidden' name='ACTION' value='' />
281 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
282 </form>
283 </td>
284 </tr>
285 </table>
286 END
287 &Header::closebox();
288 &Header::closebigbox();
289 &Header::closepage();
290 exit;
291
292 } elsif (($cgiparams{'ACTION'} eq 'remove') && (! &_is_pakfire_busy())) {
293 &Header::openbox("100%", "center", $Lang::tr{'request'});
294
295 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
296 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
297 print <<END;
298 <table><tr><td colspan='2'>$Lang::tr{'pakfire uninstall package'} @pkgs $Lang::tr{'pakfire possible dependency'}
299 <pre>
300 END
301 foreach (@output) {
302 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
303 print "$_\n";
304 }
305 print <<END;
306 </pre></td></tr>
307 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
308 <tr><td colspan='2'>&nbsp;</td></tr>
309 <tr><td align='right'><form method='post' action='$ENV{'SCRIPT_NAME'}'>
310 <input type='hidden' name='DELPAKS' value='$cgiparams{'DELPAKS'}' />
311 <input type='hidden' name='FORCE' value='on' />
312 <input type='hidden' name='ACTION' value='remove' />
313 <input type='image' alt='$Lang::tr{'uninstall'}' title='$Lang::tr{'uninstall'}' src='/images/go-next.png' />
314 </form>
315 </td>
316 <td align='left'>
317 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
318 <input type='hidden' name='ACTION' value='' />
319 <input type='image' alt='$Lang::tr{'abort'}' title='$Lang::tr{'abort'}' src='/images/dialog-error.png' />
320 </form>
321 </td>
322 </tr>
323 </table>
324 END
325 &Header::closebox();
326 &Header::closebigbox();
327 &Header::closepage();
328 exit;
329 }
330
331 # Show Pakfire main page
332 my %selected=();
333 my %checked=();
334
335 $selected{"TREE"} = ();
336 $selected{"TREE"}{"stable"} = "";
337 $selected{"TREE"}{"testing"} = "";
338 $selected{"TREE"}{"unstable"} = "";
339 $selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
340
341 my $core_release = `cat /opt/pakfire/db/core/mine 2>/dev/null`;
342 chomp($core_release);
343 my $core_update_age = &General::age("/opt/pakfire/db/core/mine");
344 my $corelist_update_age = &General::age("/opt/pakfire/db/lists/core-list.db");
345 my $server_update_age = &General::age("/opt/pakfire/db/lists/server-list.db");
346 my $packages_update_age = &General::age("/opt/pakfire/db/lists/packages_list.db");
347
348 &Header::openbox("100%", "center", "Pakfire");
349
350 print <<END;
351 <table id="pfmain">
352 END
353 if ( -e "/var/run/need_reboot") {
354 print "\t\t<tr><td colspan='2'><a href='/cgi-bin/shutdown.cgi'>$Lang::tr{'needreboot'}!</a></td></tr>\n";
355 }
356 print <<END;
357 <tr><td class="heading">$Lang::tr{'pakfire system state'}:</td>
358 <td class="heading">$Lang::tr{'available updates'}:</td></tr>
359
360 <tr><td><strong>$Lang::tr{'pakfire core update level'}: $core_release</strong>
361 <hr>
362 <div class="pflist">
363 $Lang::tr{'pakfire last update'} $core_update_age $Lang::tr{'pakfire ago'}<br>
364 $Lang::tr{'pakfire last serverlist update'} $server_update_age $Lang::tr{'pakfire ago'}<br>
365 $Lang::tr{'pakfire last core list update'} $corelist_update_age $Lang::tr{'pakfire ago'}<br>
366 $Lang::tr{'pakfire last package update'} $packages_update_age $Lang::tr{'pakfire ago'}
367 </div>
368 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
369 <input type='hidden' name='ACTION' value='update' />
370 <input type='submit' value='$Lang::tr{'calamaris refresh list'}' />
371 </form>
372 </td>
373 <td>
374 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
375 <select name="UPDPAKS" class="pflist" size="5" disabled>
376 END
377
378 &Pakfire::dblist("upgrade", "forweb");
379 print <<END;
380 </select>
381 <input type='hidden' name='ACTION' value='upgrade' />
382 <input type='image' alt='$Lang::tr{'upgrade'}' title='$Lang::tr{'upgrade'}' src='/images/document-save.png' />
383 </form>
384 </td>
385 </tr>
386 <tr><td class="heading">$Lang::tr{'pakfire available addons'}</td>
387 <td class="heading">$Lang::tr{'pakfire installed addons'}</td></tr>
388
389 <tr><td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire install description'}</p>
390 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
391 <select name="INSPAKS" class="pflist" size="10" multiple>
392 END
393
394 &Pakfire::dblist("notinstalled", "forweb");
395 print <<END;
396 </select>
397 <input type='hidden' name='ACTION' value='install' />
398 <input type='image' alt='$Lang::tr{'install'}' title='$Lang::tr{'install'}' src='/images/list-add.png' />
399 </form>
400 </td>
401 <td style="padding:5px 10px 20px 20px" align="center"><p>$Lang::tr{'pakfire uninstall description'}</p>
402 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
403 <select name="DELPAKS" class="pflist" size="10" multiple>
404 END
405
406 &Pakfire::dblist("installed", "forweb");
407 print <<END;
408 </select>
409 <input type='hidden' name='ACTION' value='remove' />
410 <input type='image' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' src='/images/list-remove.png' />
411 </form>
412 </td>
413 </tr>
414 </table>
415 END
416
417 &Header::closebox();
418 &Header::openbox("100%", "center", "$Lang::tr{'settings'}");
419
420 print <<END;
421 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
422 <table width='95%'>
423 <tr>
424 <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
425 <td width="55%" align="left">
426 <select name="TREE">
427 <option value="stable" $selected{"TREE"}{"stable"}>$Lang::tr{'pakfire tree stable'}</option>
428 <option value="testing" $selected{"TREE"}{"testing"}>$Lang::tr{'pakfire tree testing'}</option>
429 <option value="unstable" $selected{"TREE"}{"unstable"}>$Lang::tr{'pakfire tree unstable'}</option>
430 </select>
431 </td>
432 </tr>
433 <tr>
434 <td colspan="2">&nbsp;</td>
435 </tr>
436 <tr>
437 <td colspan="2" align="center">
438 <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
439 </td>
440 </tr>
441 </table>
442 </form>
443 END
444
445 &Header::closebox();
446 &Header::closebigbox();
447 &Header::closepage();
448
449 ###--- Internal functions ---###
450
451 # Check if pakfire is already running (extend test here if necessary)
452 sub _is_pakfire_busy {
453 # Return immediately if lockfile is present
454 if(-e "$Pakfire::lockfile") {
455 return 1;
456 }
457
458 # Check if a PID of a running pakfire instance is found
459 # (The system backpipe command is safe, because no user input is computed.)
460 my $pakfire_pid = `pidof -s /usr/local/bin/pakfire`;
461 chomp($pakfire_pid);
462
463 if($pakfire_pid) {
464 return 1;
465 }
466
467 # Pakfire isn't running
468 return 0;
469 }
470
471 # Send HTTP headers
472 sub _start_json_output {
473 print "Cache-Control: no-cache, no-store\n";
474 print "Content-Type: application/json\n";
475 print "\n"; # End of HTTP headers
476 }