Skip to content

Commit 1fd5805

Browse files
committed
Updated PHP Shell/Jailkit code
1 parent d89c171 commit 1fd5805

File tree

9 files changed

+157
-108
lines changed

9 files changed

+157
-108
lines changed

install/tpl/jk_init_el.ini.master

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# jk_init.ini: jailkit initialization config
22

33
# Includes paths to handle Enterprise Linux systems like RHEL and its derivatives AlmaLinux, Rocky Linux et cetera
4-
# if other paths are needed please create an issue with the details or a merge request at:
4+
# if other paths are needed please create an issue with the details or even a merge request at:
55
# https://git.ispconfig.org/ispconfig/ispconfig3
66

77
[uidbasics]
@@ -202,67 +202,67 @@ includesections = env, logbasics, netbasics, mysqlutils, webutils, imagemagick
202202

203203
[php5_4]
204204
comment = PHP 5.4
205-
paths = /opt/remi/php54/root/bin/php, /opt/remi/php54/root/bin/phar, /opt/remi/php54/root/usr/lib64/, /opt/remi/php54/root/usr/share/
205+
paths = /opt/remi/php54/root/bin/php, /usr/bin/php54, /opt/remi/php54/root/bin/phar, /opt/remi/php54/root/usr/lib64/, /opt/remi/php54/root/usr/share/
206206
includesections = php_common
207207

208208
[php5_5]
209209
comment = PHP 5.5
210-
paths = /opt/remi/php55/root/bin/php, /opt/remi/php55/root/bin/phar, /opt/remi/php55/root/usr/lib64/, /opt/remi/php55/root/usr/share/
210+
paths = /opt/remi/php55/root/bin/php, /usr/bin/php55, /opt/remi/php55/root/bin/phar, /opt/remi/php55/root/usr/lib64/, /opt/remi/php55/root/usr/share/
211211
includesections = php_common
212212

213213
[php5_6]
214214
comment = PHP 5.6
215-
paths = /opt/remi/php56/root/bin/php, /opt/remi/php56/root/bin/phar, /opt/remi/php56/root/usr/lib64/, /opt/remi/php56/root/usr/share/
215+
paths = /opt/remi/php56/root/bin/php, /usr/bin/php56, /opt/remi/php56/root/bin/phar, /opt/remi/php56/root/usr/lib64/, /opt/remi/php56/root/usr/share/
216216
includesections = php_common
217217

218218
[php7_0]
219219
comment = PHP 7.0
220-
paths = /opt/remi/php70/root/bin/php, /opt/remi/php70/root/bin/phar, /opt/remi/php70/root/usr/lib64/, /opt/remi/php70/root/usr/share/
220+
paths = /opt/remi/php70/root/bin/php, /usr/bin/php70, /opt/remi/php70/root/bin/phar, /opt/remi/php70/root/usr/lib64/, /opt/remi/php70/root/usr/share/
221221
includesections = php_common
222222

223223
[php7_1]
224224
comment = PHP 7.1
225-
paths = /opt/remi/php71/root/bin/php, /opt/remi/php71/root/bin/phar, /opt/remi/php71/root/usr/lib64/, /opt/remi/php71/root/usr/share/
225+
paths = /opt/remi/php71/root/bin/php, /usr/bin/php71, /opt/remi/php71/root/bin/phar, /opt/remi/php71/root/usr/lib64/, /opt/remi/php71/root/usr/share/
226226
includesections = php_common
227227

228228
[php7_2]
229229
comment = PHP 7.2
230-
paths = /opt/remi/php72/root/bin/php, /opt/remi/php72/root/bin/phar, /opt/remi/php72/root/usr/lib64/, /opt/remi/php72/root/usr/share/
230+
paths = /opt/remi/php72/root/bin/php, /usr/bin/php72, /opt/remi/php72/root/bin/phar, /opt/remi/php72/root/usr/lib64/, /opt/remi/php72/root/usr/share/
231231
includesections = php_common
232232

233233
[php7_3]
234234
comment = PHP 7.3
235-
paths = /opt/remi/php73/root/bin/php, /opt/remi/php73/root/bin/phar, /opt/remi/php73/root/usr/lib64/, /opt/remi/php73/root/usr/share/
235+
paths = /opt/remi/php73/root/bin/php, /usr/bin/php73, /opt/remi/php73/root/bin/phar, /opt/remi/php73/root/usr/lib64/, /opt/remi/php73/root/usr/share/
236236
includesections = php_common
237237

238238
[php7_4]
239239
comment = PHP 7.4
240-
paths = /opt/remi/php74/root/bin/php, /opt/remi/php74/root/bin/phar, /opt/remi/php74/root/usr/lib64/, /opt/remi/php74/root/usr/share/
240+
paths = /opt/remi/php74/root/bin/php, /usr/bin/php74, /opt/remi/php74/root/bin/phar, /opt/remi/php74/root/usr/lib64/, /opt/remi/php74/root/usr/share/
241241
includesections = php_common
242242

243243
[php8_0]
244244
comment = PHP 8.0
245-
paths = /opt/remi/php80/root/bin/php, /opt/remi/php80/root/bin/phar, /opt/remi/php80/root/usr/lib64/, /opt/remi/php80/root/usr/share/
245+
paths = /opt/remi/php80/root/bin/php, /usr/bin/php80, /opt/remi/php80/root/bin/phar, /opt/remi/php80/root/usr/lib64/, /opt/remi/php80/root/usr/share/
246246
includesections = php_common
247247

248248
[php8_1]
249249
comment = PHP 8.1
250-
paths = /opt/remi/php81/root/bin/php, /opt/remi/php81/root/bin/phar, /opt/remi/php81/root/usr/lib64/, /opt/remi/php81/root/usr/share/
250+
paths = /opt/remi/php81/root/bin/php, /usr/bin/php81, /opt/remi/php81/root/bin/phar, /opt/remi/php81/root/usr/lib64/, /opt/remi/php81/root/usr/share/
251251
includesections = php_common
252252

253253
[php8_2]
254254
comment = PHP 8.2
255-
paths = /opt/remi/php82/root/bin/php, /opt/remi/php82/root/bin/phar, /opt/remi/php82/root/usr/lib64/, /opt/remi/php82/root/usr/share/
255+
paths = /opt/remi/php82/root/bin/php, /usr/bin/php82, /opt/remi/php82/root/bin/phar, /opt/remi/php82/root/usr/lib64/, /opt/remi/php82/root/usr/share/
256256
includesections = php_common
257257

258258
[php8_3]
259259
comment = PHP 8.3
260-
paths = /opt/remi/php83/root/bin/php, /opt/remi/php83/root/bin/phar, /opt/remi/php83/root/usr/lib64/, /opt/remi/php83/root/usr/share/
260+
paths = /opt/remi/php83/root/bin/php, /usr/bin/php83, /opt/remi/php83/root/bin/phar, /opt/remi/php83/root/usr/lib64/, /opt/remi/php83/root/usr/share/
261261
includesections = php_common
262262

263263
[php8_4]
264264
comment = PHP 8.4
265-
paths = /opt/remi/php84/root/bin/php, /opt/remi/php84/root/bin/phar, /opt/remi/php84/root/usr/lib64/, /opt/remi/php84/root/usr/share/
265+
paths = /opt/remi/php84/root/bin/php, /usr/bin/php84, /opt/remi/php84/root/bin/phar, /opt/remi/php84/root/usr/lib64/, /opt/remi/php84/root/usr/share/
266266
includesections = php_common
267267

268268
[imagemagick]

server/conf/bashrc_user_deb.master

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,10 @@ fi
9292
#alias la='ls -A'
9393
#alias l='ls -CF'
9494

95-
<tmpl_if name='jailkit_chroot' op='==' value='n'>
9695
<tmpl_if name='use_php_path'>
9796
# Overwrite the PHP cli binaries by using $PATH:
9897
export PATH=<tmpl_var name='php_bin_dir'>:$PATH
9998
</tmpl_if>
100-
</tmpl_if>
10199

102100
# Alias definitions.
103101
# You may want to put all your additions into a separate file like

server/conf/bashrc_user_generic.master

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
2323
then
2424
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
2525
fi
26-
export PATH
2726

27+
export PATH
2828

2929
# Source custom bashrc files
3030
if [ -d ~/.bashrc.d ]

server/conf/bashrc_user_redhat.master

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
3434
then
3535
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
3636
fi
37-
export PATH
3837

39-
# Uncomment the following line if you don't like systemctl's auto-paging feature:
40-
# export SYSTEMD_PAGER=
38+
export PATH
4139

4240
# Source custom bashrc files
4341
if [ -d ~/.bashrc.d ]

server/lib/classes/cron.d/600-jailkit_maintenance.inc.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,15 @@ public function onRunJob() {
8888
}
8989

9090
$shelluser_list = $app->db->queryAllRecords("SELECT * FROM shell_user WHERE parent_domain_id = ? and chroot = 'jailkit' and active = 'y'", $rec['domain_id']);
91-
$cronjob_list = $app->db->queryAllRecords("SELECT * FROM cron WHERE parent_domain_id = ? and type = 'chrooted' and active = 'y'", $rec['domain_id']);
9291

93-
if(is_array($cronjob_list) && !empty($cronjob_list) || is_array($shelluser_list) && !empty($shelluser_list)) {
92+
if(is_array($shelluser_list) && !empty($shelluser_list)) {
9493
$options['jk_php_maintenance_check'] = "yes";
94+
$options['homedir_usernames'] = array();
95+
96+
foreach($shelluser_list as $shelluser) {
97+
$options['homedir_usernames'][] = $shelluser['username'];
98+
}
99+
95100
} else {
96101
$options['jk_php_maintenance_check'] = "no";
97102

server/lib/classes/system.inc.php

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,7 +2555,7 @@ public function create_jailkit_chroot($home_dir, $app_sections = array(), $optio
25552555
} elseif(is_string($app_sections)) {
25562556
$app_sections = preg_split('/[\s,]+/', $app_sections);
25572557
}
2558-
if(! is_array($options)) {
2558+
if(!is_array($options)) {
25592559
$options = (is_string($options) ? preg_split('/[\s,]+/', $options) : array());
25602560
}
25612561

@@ -2588,8 +2588,15 @@ public function create_jailkit_chroot($home_dir, $app_sections = array(), $optio
25882588

25892589
// Initialize the chroot into the specified directory with the specified applications
25902590
$cmd = 'jk_init' . $program_args;
2591+
$app->log("Executing command: $cmd", LOGLEVEL_DEBUG);
25912592
$this->exec_safe($cmd, $home_dir);
25922593

2594+
// Check for errors in the command execution
2595+
if ($this->last_exec_retcode() != 0) {
2596+
$app->log("Error executing jk_init command: " . implode("\n", $this->last_exec_out()), LOGLEVEL_ERROR);
2597+
return false;
2598+
}
2599+
25932600
// Create the tmp and /var/run directories
25942601
if(!is_dir($home_dir . '/tmp')) {
25952602
$this->mkdirpath($home_dir . '/tmp', 0770);
@@ -2694,6 +2701,8 @@ public function update_jailkit_chroot($home_dir, $sections = array(), $programs
26942701
global $app;
26952702

26962703
$app->log("update_jailkit_chroot called for $home_dir with options ".print_r($options, true), LOGLEVEL_DEBUG);
2704+
$app->log("update_jailkit_chroot called for $home_dir with sections ".print_r($sections, true), LOGLEVEL_DEBUG);
2705+
26972706
$app->uses('ini_parser');
26982707

26992708
// Disallow operating on root directory
@@ -2738,7 +2747,7 @@ public function update_jailkit_chroot($home_dir, $sections = array(), $programs
27382747
$jk_cp_args .= ' -f';
27392748
break;
27402749
default:
2741-
if (preg_match('@^skip[ =]/?(.+)$@', $opt, $matches) ) {
2750+
if (is_string($opt) && preg_match('@^skip[ =]/?(.+)$@', $opt, $matches) ) {
27422751
if (in_array($matches[1], $jailkit_directories)) {
27432752
$app->log("update_jailkit_chroot: skipping update of jailkit directory $home_dir/".$matches[1]
27442753
. "; if this is in use as a web folder, it is insecure and should be fixed.", LOGLEVEL_WARN);
@@ -2877,37 +2886,48 @@ public function update_jailkit_chroot($home_dir, $sections = array(), $programs
28772886
$this->chmod($home_dir . '/var/tmp', 0770, true);
28782887
}
28792888

2880-
$os_type = $app->system->get_os_type();
2881-
if (isset($os_type['type'])) {
2882-
$used_os_type = $os_type['type'];
2883-
} else {
2884-
$used_os_type = 'unknown';
2885-
}
2889+
// If update_jailkit_chroot was called from cronjob 600-jailkit.inc.php, we need to check if the PHP cli binary is available in the jail
2890+
if(isset($options['jk_php_maintenance_check']) && $options['jk_php_maintenance_check'] == 'yes') {
2891+
$os_type = $app->system->get_os_type();
2892+
$used_os_type = isset($os_type['type']) ? $os_type['type'] : 'unknown';
2893+
2894+
if(is_array($options['homedir_usernames']) && !empty($options['homedir_usernames'])) {
2895+
foreach($options['homedir_usernames'] as $homedir_username) {
28862896

2887-
if($options['jk_php_maintenance_check'] == 'yes') {
2888-
$alternatives_php = $home_dir . '/etc/alternatives/php';
2897+
if($used_os_type == "debian" || $used_os_type == "ubuntu") {
2898+
$php_binary = $home_dir . '/etc/alternatives/php';
2899+
} elseif ($used_os_type == "redhat") {
2900+
$php_binary = $home_dir . '/home/' . $homedir_username . '/.local/bin/php';
2901+
} else {
2902+
$php_binary = $home_dir . '/home/' . $homedir_username . '/.local/bin/php';
2903+
}
28892904

2890-
if(!empty($options['php_cli_binary'])) {
2891-
$php_bin_dir = dirname($options['php_cli_binary']);
2892-
if(!file_exists($home_dir . '/' . $options['php_cli_binary'])) {
2893-
$app->log("update_jailkit_chroot: The PHP cli binary " . $options['php_cli_binary'] . " is not available in the jail of the web " . $options['domain'], LOGLEVEL_DEBUG);
2905+
if(!empty($options['php_cli_binary'])) {
2906+
$php_bin_dir = dirname($options['php_cli_binary']);
2907+
if(!file_exists($home_dir . '/' . $options['php_cli_binary'])) {
2908+
$app->log("update_jailkit_chroot: The PHP cli binary " . $options['php_cli_binary'] . " is not available in the jail of the web " . $options['domain'], LOGLEVEL_DEBUG);
28942909

2895-
$fallback_php = $app->system->get_newest_php_bin($home_dir . $php_bin_dir);
2896-
$fallback_php_bin = str_replace($home_dir, '', $fallback_php);
2910+
$fallback_php = $app->system->get_newest_php_bin($home_dir . $php_bin_dir);
2911+
$fallback_php_bin = str_replace($home_dir, '', $fallback_php);
28972912

2898-
if(!empty($fallback_php) && file_exists($fallback_php_bin)) {
2899-
if(is_link($alternatives_php) || is_file($alternatives_php) || !file_exists($alternatives_php)) {
2900-
unlink($alternatives_php);
2901-
symlink($fallback_php_bin, $alternatives_php);
2902-
$app->log("update_jailkit_chroot: Found " . $fallback_php_bin . " as a fallback for alternatives/php in the jail of " . $options['domain'], LOGLEVEL_DEBUG);
2903-
}
2904-
}
2905-
} else {
2906-
if($used_os_type == "debian" || $$used_os_type == "ubuntu") {
2907-
$app->log("update_jailkit_chroot: setting alternatives/php to " . $options['php_cli_binary'], LOGLEVEL_DEBUG);
2908-
if(is_link($alternatives_php) || is_file($alternatives_php) || !file_exists($alternatives_php)) {
2909-
unlink($alternatives_php);
2910-
symlink($options['php_cli_binary'], $alternatives_php);
2913+
if(!empty($fallback_php) && file_exists($fallback_php_bin)) {
2914+
if(is_link($php_binary) || is_file($php_binary) || !file_exists($php_binary)) {
2915+
unlink($php_binary);
2916+
symlink($fallback_php_bin, $php_binary);
2917+
$app->log("update_jailkit_chroot: Found " . $fallback_php_bin . " as a fallback for PHP in the jail of " . $options['domain'], LOGLEVEL_DEBUG);
2918+
}
2919+
}
2920+
} else {
2921+
$app->log("update_jailkit_chroot: setting PHP to " . $options['php_cli_binary'], LOGLEVEL_DEBUG);
2922+
if(is_link($php_binary) || is_file($php_binary) || !file_exists($php_binary)) {
2923+
unlink($php_binary);
2924+
symlink($options['php_cli_binary'], $php_binary);
2925+
if($used_os_type == "debian" || $$used_os_type == "ubuntu") {
2926+
if(file_exists($home_dir . '/home/' . $homedir_username . '/.local/bin/php')) {
2927+
unlink($home_dir . '/home/' . $homedir_username . '/.local/bin/php');
2928+
}
2929+
}
2930+
}
29112931
}
29122932
}
29132933
}
@@ -3096,8 +3116,7 @@ public function get_newest_php_bin($bin_directory) {
30963116
while(false !== ($entry = readdir($handle))) {
30973117
$full_path = $bin_directory . '/' . $entry;
30983118
// Check if the filename matches a pattern for commonly available PHP CLI binaries
3099-
// and ensure they are not symbolic links
3100-
if(preg_match('/^php(\d{1,2}\.?\d{1,2})?$/', $entry) && !is_link($full_path) && is_file($full_path)) {
3119+
if(preg_match('/^php(\d{1,2}\.?\d{1,2})?$/', $entry) && file_exists($full_path)) {
31013120
$php_binaries[] = $entry;
31023121
}
31033122
}

server/plugins-available/cron_jailkit_plugin.inc.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ function insert($event_name, $data) {
8383
LEFT JOIN server_php ON web_domain.server_php_id = server_php.server_php_id
8484
WHERE web_domain.domain_id = ?", $data["new"]["parent_domain_id"]);
8585

86+
$this->parent_domain = $parent_domain;
87+
8688
if(!$parent_domain["domain_id"]) {
8789
$app->log("Parent domain not found", LOGLEVEL_WARN);
8890
return 0;
@@ -135,7 +137,7 @@ function insert($event_name, $data) {
135137

136138
$this->_add_jailkit_user();
137139

138-
$this->_setup_php_jailkit();
140+
//$this->_setup_php_jailkit();
139141

140142
$command .= 'usermod -U ? 2>/dev/null';
141143
$app->system->exec_safe($command, $parent_domain["system_user"]);
@@ -168,6 +170,8 @@ function update($event_name, $data) {
168170
LEFT JOIN server_php ON web_domain.server_php_id = server_php.server_php_id
169171
WHERE web_domain.domain_id = ?", $data["new"]["parent_domain_id"]);
170172

173+
$this->parent_domain = $parent_domain;
174+
171175
if(!$parent_domain["domain_id"]) {
172176
$app->log("Parent domain not found", LOGLEVEL_WARN);
173177
return 0;
@@ -191,14 +195,14 @@ function update($event_name, $data) {
191195
$app->uses("getconf");
192196
$this->data = $data;
193197
$this->jailkit_config = $app->getconf->get_server_config($conf["server_id"], 'jailkit');
194-
foreach (array('jailkit_chroot_app_sections', 'jailkit_chroot_app_programs') as $section) {
198+
foreach(array('jailkit_chroot_app_sections', 'jailkit_chroot_app_programs') as $section) {
195199
// Replace and don't inherit the server's Jailkit config
196-
if (isset($parent_domain[$section]) && $parent_domain[$section] != '' ) {
200+
if(isset($parent_domain[$section]) && $parent_domain[$section] != '' ) {
197201
$this->jailkit_config[$section] = $parent_domain[$section];
198202
}
199203
// Add selected PHP version to the jailkit chroot
200-
if ($section == 'jailkit_chroot_app_sections') {
201-
if (isset($parent_domain['php_jk_section']) && $parent_domain['php_jk_section'] != '' ) {
204+
if($section == 'jailkit_chroot_app_sections') {
205+
if(isset($parent_domain['php_jk_section']) && $parent_domain['php_jk_section'] != '' ) {
202206
$this->jailkit_config['jailkit_chroot_app_sections'] = $this->jailkit_config['jailkit_chroot_app_sections'] . ' ' . $parent_domain['php_jk_section'];
203207
$jk_temp_config = preg_split('/[\s,]+/', $this->jailkit_config['jailkit_chroot_app_sections']);
204208

@@ -217,7 +221,7 @@ function update($event_name, $data) {
217221

218222
$this->_add_jailkit_user();
219223

220-
$this->_setup_php_jailkit();
224+
//$this->_setup_php_jailkit();
221225

222226
$this->_update_website_security_level();
223227

0 commit comments

Comments
 (0)