]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/pakfire.cgi
3e8dc54604604b4ee63c3229bfd3c560cbfcc368
[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 style="width: 100%"><tr><td colspan='2'><p>$Lang::tr{'pakfire install package'} <strong>@{pkgs}</strong><br>
287 $Lang::tr{'pakfire possible dependency'}</p>
288 <pre>
289 END
290 foreach (@output) {
291 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
292 print "$_\n";
293 }
294 print <<END;
295 </pre></td></tr>
296 <tr><td colspan='2'>$Lang::tr{'pakfire accept all'}</td></tr>
297 <tr><td colspan='2'>&nbsp;</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' />
303 </form>
304 </td>
305 <td align='left'>
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' />
309 </form>
310 </td>
311 </tr>
312 </table>
313 END
314 &Header::closebox();
315 &Header::closebigbox();
316 &Header::closepage();
317 exit;
318
319 } elsif (($cgiparams{'ACTION'} eq 'remove') && ($pagemode eq $PM_DEFAULT)) {
320 &Header::openbox("100%", "center", $Lang::tr{'request'});
321
322 my @pkgs = split(/\|/, $cgiparams{'DELPAKS'});
323 my @output = &General::system_output("/usr/local/bin/pakfire", "resolvedeps", "--no-colors", @pkgs);
324 print <<END;
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>
327 <pre>
328 END
329 foreach (@output) {
330 $_ =~ s/\\e\[[0-1]\;[0-9]+m//g;
331 print "$_\n";
332 }
333 print <<END;
334 </pre></td></tr>
335 <tr><td colspan='2'>$Lang::tr{'pakfire uninstall all'}</td></tr>
336 <tr><td colspan='2'>&nbsp;</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' />
342 </form>
343 </td>
344 <td align='left'>
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' />
348 </form>
349 </td>
350 </tr>
351 </table>
352 END
353 &Header::closebox();
354 &Header::closebigbox();
355 &Header::closepage();
356 exit;
357 }
358
359 # Show Pakfire main page
360 my %selected=();
361 my %checked=();
362
363 $selected{"TREE"} = ();
364 $selected{"TREE"}{"stable"} = "";
365 $selected{"TREE"}{"testing"} = "";
366 $selected{"TREE"}{"unstable"} = "";
367 $selected{"TREE"}{$pakfiresettings{"TREE"}} = "selected";
368
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");
375
376 &Header::openbox("100%", "center", "Pakfire");
377
378 print <<END;
379 <table id="pfmain">
380 END
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";
383 }
384 print <<END;
385 <tr><td class="heading">$Lang::tr{'pakfire system state'}:</td>
386 <td class="heading">$Lang::tr{'available updates'}:</td></tr>
387
388 <tr><td><strong>$Lang::tr{'pakfire core update level'}: $core_release</strong>
389 <hr>
390 <div class="pflist">
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'}
395 </div>
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'}' />
399 </form>
400 </td>
401 <td>
402 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
403 <select name="UPDPAKS" class="pflist" size="5" disabled>
404 END
405
406 &Pakfire::dblist("upgrade", "forweb");
407 print <<END;
408 </select>
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' />
411 </form>
412 </td>
413 </tr>
414 <tr><td class="heading">$Lang::tr{'pakfire available addons'}</td>
415 <td class="heading">$Lang::tr{'pakfire installed addons'}</td></tr>
416
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>
420 END
421
422 &Pakfire::dblist("notinstalled", "forweb");
423 print <<END;
424 </select>
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' />
427 </form>
428 </td>
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>
432 END
433
434 &Pakfire::dblist("installed", "forweb");
435 print <<END;
436 </select>
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' />
439 </form>
440 </td>
441 </tr>
442 </table>
443 END
444
445 &Header::closebox();
446 &Header::openbox("100%", "center", "$Lang::tr{'settings'}");
447
448 print <<END;
449 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
450 <table width='95%'>
451 <tr>
452 <td align='left' width='45%'>$Lang::tr{'pakfire tree'}</td>
453 <td width="55%" align="left">
454 <select name="TREE">
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>
458 </select>
459 </td>
460 </tr>
461 <tr>
462 <td colspan="2">&nbsp;</td>
463 </tr>
464 <tr>
465 <td colspan="2" align="center">
466 <input type="submit" name="ACTION" value="$Lang::tr{'save'}" />
467 </td>
468 </tr>
469 </table>
470 </form>
471 END
472
473 &Header::closebox();
474 &Header::closebigbox();
475 &Header::closepage();
476
477 ###--- Internal functions ---###
478
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") {
483 return 1;
484 }
485
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`;
489 chomp($pakfire_pid);
490
491 if($pakfire_pid) {
492 return 1;
493 }
494
495 # Pakfire isn't running
496 return 0;
497 }
498
499 # Send HTTP headers
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
504 }
505
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;
513 $switch_mode //= 0;
514
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";
519
520 # Change global page mode
521 if($switch_mode) {
522 $pagemode = $mode;
523 }
524 }