From mboxrd@z Thu Jan  1 00:00:00 1970
From: Robin Roevens <robin.roevens@disroot.org>
To: development@lists.ipfire.org
Subject: [PATCH v3 2/5] services.cgi: Fix status/actions on services with name
 != addon name
Date: Wed, 12 Oct 2022 00:01:54 +0200
Message-ID: <20221011220157.17385-3-robin.roevens@disroot.org>
In-Reply-To: <20221011220157.17385-1-robin.roevens@disroot.org>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="===============0345670996387426240=="
List-Id: <development.lists.ipfire.org>

--===============0345670996387426240==
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

* addonctrl's new functionality to control explicit addon services was
  implemented.
* Change 'Addon' column header to 'Addon Service' to be clear that
  it's not addons but services listed here.
* Services not matching the name of the addon now display the addon
  name between parentheses, so the user knows where the service comes
  from.
* When no valid runlevel symlink is found by addonctrl for a service,
  the 'enable on boot' checkbox is replaced by a small exclamation point
  with alt-text "No valid runlevel symlink was found for the initscript of
  this service." to inform user why a service can't be enabled.
* Added German and Dutch translation for above message.

Fixes: Bug#12935
Signed-off-by: Robin Roevens <robin.roevens(a)disroot.org>
---
 html/cgi-bin/services.cgi | 114 ++++++++++++++++----------------------
 langs/de/cgi-bin/de.pl    |   1 +
 langs/en/cgi-bin/en.pl    |   1 +
 langs/nl/cgi-bin/nl.pl    |   1 +
 4 files changed, 51 insertions(+), 66 deletions(-)

diff --git a/html/cgi-bin/services.cgi b/html/cgi-bin/services.cgi
index 29926ecc3..de946d755 100644
--- a/html/cgi-bin/services.cgi
+++ b/html/cgi-bin/services.cgi
@@ -20,7 +20,7 @@
 ############################################################################=
###
=20
 use strict;
-
+use feature "switch";
 # enable only the following on debugging purpose
 #use warnings;
 #use CGI::Carp 'fatalsToBrowser';
@@ -141,15 +141,22 @@ END
 	&Header::openbox('100%', 'left', "Addon - $Lang::tr{services}");
 	my $paramstr=3D$ENV{QUERY_STRING};
 	my @param=3Dsplit(/!/, $paramstr);
-	if ($param[1] ne ''){
-		&General::system("/usr/local/bin/addonctrl", "$param[0]", "$param[1]");
+	# Make sure action parameter is actually one of the allowed service actions
+	given ($param[1]) {
+		when ( ['start', 'stop', 'enable', 'disable'] ) {
+			# Make sure pak-name and service name don't contain any illegal character
+			if ( $param[0] !~ /[^a-zA-Z_0-9\-]/ &&
+			     $param[2] !~ /[^a-zA-Z_0-9\-]/ ) {
+				&General::system("/usr/local/bin/addonctrl", "$param[0]", "$param[1]", "=
$param[2]");
+			}
+		}
 	}
=20
 	print <<END
 <div align=3D'center'>
 <table width=3D'80%' cellspacing=3D'1' class=3D'tbl'>
 <tr>
-	<th align=3D'center'><b>Addon</b></th>
+	<th align=3D'left'><b>Addon $Lang::tr{service}</b></th>
 	<th align=3D'center'><b>Boot</b></th>
 	<th align=3D'center' colspan=3D2><b>$Lang::tr{'action'}</b></th>
 	<th align=3D'center'><b>$Lang::tr{'status'}</b></th>
@@ -170,33 +177,35 @@ END
 	foreach my $pak (keys %paklist) {
 		my %metadata =3D &Pakfire::getmetadata($pak, "installed");
 		=09
+		my $service;
+
 		if ("$metadata{'Services'}") {
-			foreach my $service (split(/ /, "$metadata{'Services'}")) {
-				push(@addon_services, $service);
-			}
-		}
-	}
+			foreach $service (split(/ /, "$metadata{'Services'}")) {
+				$lines++;
+				if ($lines % 2) {
+					print "<tr>";
+					$col=3D"bgcolor=3D'$color{'color22'}'";
+				} else {
+					print "<tr>";
+					$col=3D"bgcolor=3D'$color{'color20'}'";
+				}
=20
-	foreach (@addon_services) {
-		$lines++;
-		if ($lines % 2){
-			print "<tr>";
-			$col=3D"bgcolor=3D'$color{'color22'}'";
-		}else{
-			print "<tr>";
-			$col=3D"bgcolor=3D'$color{'color20'}'";
+				# Add addon name to displayname of service if servicename differs from a=
ddon
+				my $displayname =3D ($pak ne $service) ? "$service ($pak)" : $service;
+				print "<td align=3D'left' $col width=3D'31%'>$displayname</td> ";
+
+				my $status =3D isautorun($pak,$service,$col);
+				print "$status ";
+				print "<td align=3D'center' $col width=3D'8%'><a href=3D'services.cgi?$p=
ak!start!$service'><img alt=3D'$Lang::tr{'start'}' title=3D'$Lang::tr{'start'=
}' src=3D'/images/go-up.png' border=3D'0' /></a></td>";
+				print "<td align=3D'center' $col width=3D'8%'><a href=3D'services.cgi?$p=
ak!stop!$service'><img alt=3D'$Lang::tr{'stop'}' title=3D'$Lang::tr{'stop'}' =
src=3D'/images/go-down.png' border=3D'0' /></a></td> ";
+				my $status =3D isrunningaddon($pak,$service,$col);
+				$status =3D~ s/\=1B\[[0-1]\;[0-9]+m//g;
+
+				chomp($status);
+				print "$status";
+				print "</tr>";
+			}
 		}
-		print "<td align=3D'left' $col width=3D'31%'>$_</td> ";
-		my $status =3D isautorun($_,$col);
-		print "$status ";
-		print "<td align=3D'center' $col width=3D'8%'><a href=3D'services.cgi?$_!s=
tart'><img alt=3D'$Lang::tr{'start'}' title=3D'$Lang::tr{'start'}' src=3D'/im=
ages/go-up.png' border=3D'0' /></a></td>";
-		print "<td align=3D'center' $col width=3D'8%'><a href=3D'services.cgi?$_!s=
top'><img alt=3D'$Lang::tr{'stop'}' title=3D'$Lang::tr{'stop'}' src=3D'/image=
s/go-down.png' border=3D'0' /></a></td> ";
-		my $status =3D isrunningaddon($_,$col);
-		$status =3D~ s/\=1B\[[0-1]\;[0-9]+m//g;
-
-		chomp($status);
-		print "$status";
-		print "</tr>";
 	}
=20
 	print "</table></div>\n";
@@ -215,51 +224,24 @@ END
 }
=20
 sub isautorun (@) {
-	my ($cmd, $col) =3D @_;
-
-	# Init directory.
-	my $initdir =3D "/etc/rc.d/rc3.d/";
-
-	my $status =3D "<td align=3D'center' $col></td>";
+	my ($pak, $service, $col) =3D @_;
+	my @testcmd =3D &General::system_output("/usr/local/bin/addonctrl", "$pak",=
 "boot-status", "$service");
+	my $testcmd =3D @testcmd[0];
+	my $status =3D "<td align=3D'center' $col><img alt=3D'$Lang::tr{'service bo=
ot setting unavailable'}' title=3D'$Lang::tr{'service boot setting unavailabl=
e'}' src=3D'/images/dialog-warning.png' border=3D'0' width=3D'16' height=3D'1=
6' /></td>";
=20
-	# Check if autorun for the given cmd is enabled.
-	if ( &find_init("$cmd", "$initdir") ) {
+	# Check if autorun for the given service is enabled.
+	if ( $testcmd =3D~ /enabled\ on\ boot/ ) {
 		# Adjust status.
-		$status =3D "<td align=3D'center' $col><a href=3D'services.cgi?$_!disable'=
><img alt=3D'$Lang::tr{'deactivate'}' title=3D'$Lang::tr{'deactivate'}' src=
=3D'/images/on.gif' border=3D'0' width=3D'16' height=3D'16' /></a></td>";
-	} else {
+		$status =3D "<td align=3D'center' $col><a href=3D'services.cgi?$pak!disabl=
e!$service'><img alt=3D'$Lang::tr{'deactivate'}' title=3D'$Lang::tr{'deactiva=
te'}' src=3D'/images/on.gif' border=3D'0' width=3D'16' height=3D'16' /></a></=
td>";
+	} elsif ( $testcmd =3D~ /disabled\ on\ boot/ ) {
 		# Adjust status.
-		$status =3D "<td align=3D'center' $col><a href=3D'services.cgi?$_!enable'>=
<img alt=3D'$Lang::tr{'activate'}' title=3D'$Lang::tr{'activate'}' src=3D'/im=
ages/off.gif' border=3D'0' width=3D'16' height=3D'16' /></a></td>";
+		$status =3D "<td align=3D'center' $col><a href=3D'services.cgi?$pak!enable=
!$service'><img alt=3D'$Lang::tr{'activate'}' title=3D'$Lang::tr{'activate'}'=
 src=3D'/images/off.gif' border=3D'0' width=3D'16' height=3D'16' /></a></td>";
 	}
=20
 	# Return the status.
 	return $status;
 }
=20
-sub find_init (@) {
-	my ($cmd, $dir) =3D @_;
-
-	# Open given init directory.
-	opendir (INITDIR, "$dir") || die "Cannot opendir $dir: $!";
-
-	# Read-in init files from directory.
-	my @inits =3D readdir(INITDIR);
-
-	# Close directory handle.
-	closedir(INITDIR);
-
-	# Loop through the directory.
-	foreach my $init (@inits) {
-		# Check if the current processed file belongs to the given command.
-		if ($init =3D~ /S\d+\d+$cmd\z/) {
-			# Found, return "1" - True.
-			return "1";
-		}
-        }
-
-	# Nothing found, return nothing.
-	return;
-}
-
 sub isrunning (@) {
 	my ($cmd, $col) =3D @_;
 	my $status =3D "<td align=3D'center' bgcolor=3D'${Header::colourred}'><font=
 color=3D'white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan=3D'2' $co=
l></td>";
@@ -313,7 +295,7 @@ sub isrunning (@) {
 }
=20
 sub isrunningaddon (@) {
-	my ($cmd, $col) =3D @_;
+	my ($pak, $service, $col) =3D @_;
=20
 	my $status =3D "<td align=3D'center' bgcolor=3D'${Header::colourred}'><font=
 color=3D'white'><b>$Lang::tr{'stopped'}</b></font></td><td colspan=3D'2' $co=
l></td>";
 	my $pid =3D '';
@@ -321,7 +303,7 @@ sub isrunningaddon (@) {
 	my $exename;
 	my @memory;
=20
-	my @testcmd =3D &General::system_output("/usr/local/bin/addonctrl", "$cmd",=
 "status");
+	my @testcmd =3D &General::system_output("/usr/local/bin/addonctrl", "$pak",=
 "status", "$service");
 	my $testcmd =3D @testcmd[0];
=20
 	if ( $testcmd =3D~ /is\ running/ && $testcmd !~ /is\ not\ running/){
diff --git a/langs/de/cgi-bin/de.pl b/langs/de/cgi-bin/de.pl
index 798abcffc..db7d117b0 100644
--- a/langs/de/cgi-bin/de.pl
+++ b/langs/de/cgi-bin/de.pl
@@ -2251,6 +2251,7 @@
 'server string' =3D> 'Server String',
 'service' =3D> 'Dienst',
 'service added' =3D> 'Benutzerdefinierter Netzwerkdienst wurde hinzugef=C3=
=BCgt',
+'service boot setting unavailable' =3D> 'F=C3=BCr das Initscript dieses Dien=
stes wurde kein g=C3=BCltiger Runlevel-Symlink gefunden.',
 'service name' =3D> 'Name des Dienstes:',
 'service removed' =3D> 'Benutzerdefinierter Netzwerkdienst wurde entfernt',
 'service updated' =3D> 'Benutzerdefinierter Netzwerkdienst wurde aktualisier=
t',
diff --git a/langs/en/cgi-bin/en.pl b/langs/en/cgi-bin/en.pl
index f770e7cd9..60dca5be4 100644
--- a/langs/en/cgi-bin/en.pl
+++ b/langs/en/cgi-bin/en.pl
@@ -2306,6 +2306,7 @@
 'server string' =3D> 'Server String',
 'service' =3D> 'Service',
 'service added' =3D> 'Custom network service added',
+'service boot setting unavailable' =3D> 'No valid runlevel symlink was found=
 for the initscript of this service.',
 'service name' =3D> 'Service name:',
 'service removed' =3D> 'Custom network service removed',
 'service updated' =3D> 'Custom network service updated',
diff --git a/langs/nl/cgi-bin/nl.pl b/langs/nl/cgi-bin/nl.pl
index 49dabec99..4fd6955cc 100644
--- a/langs/nl/cgi-bin/nl.pl
+++ b/langs/nl/cgi-bin/nl.pl
@@ -1899,6 +1899,7 @@
 'server string' =3D> 'Server String',
 'service' =3D> 'Dienst',
 'service added' =3D> 'Aangepaste netwerkdienst toegevoegd',
+'service boot setting unavailable' =3D> 'Er werd voor het initscript van dez=
e service geen geldige runlevel symlink gevonden.',
 'service name' =3D> 'Dienstennaam:',
 'service removed' =3D> 'Aangepaste netwerkdienst verwijderd',
 'service updated' =3D> 'Aangepaste netwerkdienst bijgewerkt',
--=20
2.37.3


--=20
Dit bericht is gescanned op virussen en andere gevaarlijke
inhoud door MailScanner en lijkt schoon te zijn.


--===============0345670996387426240==--