Skip to content

Commit 1714a5f

Browse files
committed
Fallback to other PHP cli binaries if the selected one doesn't exist in the jail
1 parent 7960a79 commit 1714a5f

File tree

4 files changed

+64
-11
lines changed

4 files changed

+64
-11
lines changed

server/lib/classes/system.inc.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ function copy($file1, $file2) {
936936
} else {
937937
return false;
938938
}
939-
939+
940940
}
941941

942942
function move($file1, $file2) {
@@ -3024,4 +3024,35 @@ private function check_run_as_user($username) {
30243024
return false;
30253025
}
30263026
}
3027+
3028+
public function get_newest_php_bin($bin_directory) {
3029+
3030+
if(empty($bin_directory)) {
3031+
$bin_directory = '/usr/bin';
3032+
}
3033+
3034+
$php_binaries = [];
3035+
3036+
if($handle = opendir($bin_directory)) {
3037+
while(false !== ($entry = readdir($handle))) {
3038+
$full_path = $bin_directory . '/' . $entry;
3039+
// Check if the filename matches a pattern for commonly available PHP CLI binaries
3040+
// and ensure they are not symbolic links
3041+
if(preg_match('/^php(\d{1,2}\.?\d{1,2})?$/', $entry) && !is_link($full_path) && is_file($full_path)) {
3042+
$php_binaries[] = $entry;
3043+
}
3044+
}
3045+
closedir($handle);
3046+
}
3047+
// Find and return the newest/highest version PHP binary
3048+
$newest_php_bin = null;
3049+
foreach($php_binaries as $php_bin) {
3050+
if($newest_php_bin === null || version_compare($php_bin, $newest_php_bin) > 0) {
3051+
$newest_php_bin = $php_bin;
3052+
}
3053+
}
3054+
3055+
return $newest_php_bin ? $bin_directory . '/' . $newest_php_bin : null;
3056+
}
3057+
30273058
}

server/plugins-available/cron_jailkit_plugin.inc.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,10 @@ function _setup_php_jailkit() {
382382
$tpl->setVar('use_php_path', false);
383383
$tpl->setVar('use_php_alias', false);
384384

385-
$php_bin_dir = dirname($this->parent_domain['php_cli_binary']);
386385

387386
if(($this->parent_domain['server_php_id'] > 0) && !empty($this->parent_domain['php_cli_binary'])) {
387+
$php_bin_dir = dirname($this->parent_domain['php_cli_binary']);
388+
388389
if(preg_match('/^(\/usr\/(s)?bin|\/(s)?bin)/', $php_bin_dir)) {
389390
$tpl->setVar('use_php_path', false);
390391
$tpl->setVar('use_php_alias', true);
@@ -399,9 +400,19 @@ function _setup_php_jailkit() {
399400
$app->log("The PHP cli binary " . $this->parent_domain['php_cli_binary'] . " is not available in the jail of the web " . $this->parent_domain['domain'] . " / cronjob_id: " . $this->data['new']['id'] . ". Check your Jailkit setup!", LOGLEVEL_DEBUG);
400401
$tpl->setVar('use_php_path', false);
401402
$tpl->setVar('use_php_alias', false);
402-
if(is_link($this->parent_domain['document_root'] . '/etc/alternatives/php'))
403-
{
404-
unlink($this->parent_domain['document_root'] . '/etc/alternatives/php');
403+
404+
if(!empty($app->system->get_newest_php_bin($this->parent_domain['document_root'] . $php_bin_dir))) {
405+
if(is_link($this->parent_domain['document_root'] . '/etc/alternatives/php'))
406+
{
407+
unlink($this->parent_domain['document_root'] . '/etc/alternatives/php');
408+
}
409+
410+
$fallback_php = $app->system->get_newest_php_bin($this->parent_domain['document_root'] . $php_bin_dir);
411+
$fallback_php_bin = str_replace($this->parent_domain['document_root'], '', $fallback_php);
412+
413+
symlink($fallback_php_bin, $this->parent_domain['document_root'] . '/etc/alternatives/php');
414+
415+
$app->log("Found " . $fallback_php_bin . " as a fallback in the jail of ". $this->parent_domain['domain'], LOGLEVEL_DEBUG);
405416
}
406417
} else {
407418
if($app->system->get_os_type() == "debian" || $app->system->get_os_type() == "ubuntu") {

server/plugins-available/shelluser_base_plugin.inc.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,9 @@ function _add_user_bashrc() {
618618
$tpl->newTemplate("bashrc_user_generic.master");
619619
}
620620

621-
$php_bin_dir = dirname($this->web['php_cli_binary']);
622-
623621
if(($this->web['server_php_id'] > 0) && !empty($this->web['php_cli_binary'])) {
622+
$php_bin_dir = dirname($this->web['php_cli_binary']);
623+
624624
if(preg_match('/^(\/usr\/(s)?bin|\/(s)?bin)/', $php_bin_dir)) {
625625
$tpl->setVar('use_php_path', false);
626626
$tpl->setVar('use_php_alias', true);

server/plugins-available/shelluser_jailkit_plugin.inc.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,10 @@ function _setup_php_jailkit() {
719719
$tpl->setVar('use_php_path', false);
720720
$tpl->setVar('use_php_alias', false);
721721

722-
$php_bin_dir = dirname($this->web['php_cli_binary']);
723722

724723
if(($this->web['server_php_id'] > 0) && !empty($this->web['php_cli_binary'])) {
724+
$php_bin_dir = dirname($this->web['php_cli_binary']);
725+
725726
if(preg_match('/^(\/usr\/(s)?bin|\/(s)?bin)/', $php_bin_dir)) {
726727
$tpl->setVar('use_php_path', false);
727728
$tpl->setVar('use_php_alias', true);
@@ -736,9 +737,19 @@ function _setup_php_jailkit() {
736737
$app->log("The PHP cli binary " . $this->web['php_cli_binary'] . " is not available in the jail of the web " . $this->web['domain'] . " / SSH/SFTP user: " . $this->data['new']['username'] . ". Check your Jailkit setup!", LOGLEVEL_DEBUG);
737738
$tpl->setVar('use_php_path', false);
738739
$tpl->setVar('use_php_alias', false);
739-
if(is_link($this->web['document_root'] . '/etc/alternatives/php'))
740-
{
741-
unlink($this->web['document_root'] . '/etc/alternatives/php');
740+
741+
if(!empty($app->system->get_newest_php_bin($this->web['document_root'] . $php_bin_dir))) {
742+
if(is_link($this->web['document_root'] . '/etc/alternatives/php') || is_file($this->web['document_root'] . '/etc/alternatives/php'))
743+
{
744+
unlink($this->web['document_root'] . '/etc/alternatives/php');
745+
}
746+
747+
$fallback_php = $app->system->get_newest_php_bin($this->web['document_root'] . $php_bin_dir);
748+
$fallback_php_bin = str_replace($this->web['document_root'], '', $fallback_php);
749+
750+
symlink($fallback_php_bin, $this->web['document_root'] . '/etc/alternatives/php');
751+
752+
$app->log("Found " . $fallback_php_bin . " as a fallback in the jail of ". $this->web['domain'], LOGLEVEL_DEBUG);
742753
}
743754
} else {
744755
if($app->system->get_os_type() == "debian" || $app->system->get_os_type() == "ubuntu") {

0 commit comments

Comments
 (0)