Skip to content

Commit

Permalink
fix: handle disabled shell_exec
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb authored and backportbot[bot] committed May 6, 2024
1 parent 22a8b35 commit f647bbc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
16 changes: 14 additions & 2 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,23 @@ public function getThermalZones(): array {
return [];
}

/**
* Execute a command with shell_exec.
*
* The command will be escaped with escapeshellcmd.
*
* @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response.
*/
protected function executeCommand(string $command): string {
$output = @shell_exec(escapeshellcmd($command));
if ($output === null || $output === '' || $output === false) {
if (function_exists('shell_exec') === false) {
throw new RuntimeException('shell_exec unavailable');
}

$output = shell_exec(escapeshellcmd($command));
if ($output === false || $output === null || $output === '') {
throw new RuntimeException('No output for command: "' . $command . '"');
}

return $output;
}

Expand Down
37 changes: 29 additions & 8 deletions lib/OperatingSystems/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ public function getCpuName(): string {
}

public function getTime(): string {
return (string)shell_exec('date');
try {
return $this->executeCommand('date');
} catch (RuntimeException $e) {
return '';
}
}

public function getUptime(): int {
Expand All @@ -139,12 +143,17 @@ public function getUptime(): int {
}

public function getNetworkInfo(): array {
$result = [];
$result['hostname'] = \gethostname();
$dns = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2');
$result['dns'] = $dns;
$gw = shell_exec('ip route | awk \'/default/ { print $3 }\'');
$result['gateway'] = $gw;
$result = [
'hostname' => \gethostname(),
'dns' => '',
'gateway' => '',
];

if (function_exists('shell_exec')) {
$result['dns'] = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2');
$result['gateway'] = shell_exec('ip route | awk \'/default/ { print $3 }\'');
}

return $result;
}

Expand Down Expand Up @@ -261,11 +270,23 @@ protected function readContent(string $filename): string {
return trim($data);
}

/**
* Execute a command with shell_exec.
*
* The command will be escaped with escapeshellcmd.
*
* @throws RuntimeException if shell_exec is unavailable, the command failed or an empty response.
*/
protected function executeCommand(string $command): string {
$output = @shell_exec(escapeshellcmd($command));
if (function_exists('shell_exec') === false) {
throw new RuntimeException('shell_exec unavailable');
}

$output = shell_exec(escapeshellcmd($command));
if ($output === false || $output === null || $output === '') {
throw new RuntimeException('No output for command: "' . $command . '"');
}

return $output;
}

Expand Down

0 comments on commit f647bbc

Please sign in to comment.