Skip to content

Commit bd01b0f

Browse files
committed
WIP: jail cleanup and updates
1 parent 0974c52 commit bd01b0f

File tree

3 files changed

+91
-25
lines changed

3 files changed

+91
-25
lines changed

server/lib/classes/system.inc.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,7 +2243,7 @@ public function create_jailkit_user($username, $home_dir, $user_home_dir, $shell
22432243

22442244
public function create_jailkit_chroot($home_dir, $app_sections = array(), $options = array()) {
22452245
if(!is_dir($home_dir)) {
2246-
$app->log("create_jailkit_chroot: jail directory does not exist: $homedir", LOGLEVEL_WARN);
2246+
$app->log("create_jailkit_chroot: jail directory does not exist: $home_dir", LOGLEVEL_WARN);
22472247
return false;
22482248
}
22492249
if(empty($app_sections)) {
@@ -2293,7 +2293,7 @@ public function create_jailkit_chroot($home_dir, $app_sections = array(), $optio
22932293

22942294
public function create_jailkit_programs($home_dir, $programs = array(), $options = array()) {
22952295
if(!is_dir($home_dir)) {
2296-
$app->log("create_jailkit_programs: jail directory does not exist: $homedir", LOGLEVEL_WARN);
2296+
$app->log("create_jailkit_programs: jail directory does not exist: $home_dir", LOGLEVEL_WARN);
22972297
return false;
22982298
}
22992299
if(empty($programs)) {
@@ -2337,7 +2337,7 @@ public function create_jailkit_programs($home_dir, $programs = array(), $options
23372337
}
23382338
}
23392339
if (count($bad_paths) > 0) {
2340-
$app->log("Prohibited path not added to jail $homedir: " . implode(", ", $bad_paths), LOGLEVEL_WARN);
2340+
$app->log("Prohibited path not added to jail $home_dir: " . implode(", ", $bad_paths), LOGLEVEL_WARN);
23412341
} else {
23422342
$program_args .= ' ' . escapeshellarg($prog);
23432343
}
@@ -2353,7 +2353,7 @@ public function create_jailkit_programs($home_dir, $programs = array(), $options
23532353

23542354
public function update_jailkit_chroot($home_dir, $sections = array(), $programs = array(), $options = array()) {
23552355
if(!is_dir($home_dir)) {
2356-
$app->log("update_jailkit_chroot: jail directory does not exist: $homedir", LOGLEVEL_WARN);
2356+
$app->log("update_jailkit_chroot: jail directory does not exist: $home_dir", LOGLEVEL_WARN);
23572357
return false;
23582358
}
23592359

@@ -2407,11 +2407,8 @@ public function update_jailkit_chroot($home_dir, $sections = array(), $programs
24072407
// remove dangling symlinks
24082408
$app->log("TODO: search for and remove dangling symlinks", LOGLEVEL_DEBUG);
24092409

2410-
// search for and remove hardlinked
2410+
// save list of hardlinked files
24112411
if (!in_array($opts, 'hardlink') && !in_array($options, 'allow_hardlink')) {
2412-
$app->log("TODO: search for and remove hardlinked files", LOGLEVEL_DEBUG);
2413-
// search for and save list of files
2414-
24152412
$find_multiple_links = function ( $path ) use ( &$find_multiple_links ) {
24162413
$found = array();
24172414
if (is_dir($path)) {
@@ -2498,9 +2495,9 @@ public function update_jailkit_chroot($home_dir, $sections = array(), $programs
24982495
return true;
24992496
}
25002497

2501-
public function delete_jailkit_chroot($home_dir, $options = array()) {
2498+
public function delete_jailkit_chroot($home_dir) {
25022499
if(!is_dir($home_dir)) {
2503-
$app->log("delete_jailkit_chroot: jail directory does not exist: $homedir", LOGLEVEL_DEBUG);
2500+
$app->log("delete_jailkit_chroot: jail directory does not exist: $home_dir", LOGLEVEL_DEBUG);
25042501
return false;
25052502
}
25062503

@@ -2528,7 +2525,7 @@ public function delete_jailkit_chroot($home_dir, $options = array()) {
25282525

25292526
}
25302527

2531-
$app->log("delete_jailkit_chroot: removed from jail $homedir: $removed", LOGLEVEL_DEBUG);
2528+
$app->log("delete_jailkit_chroot: removed from jail $home_dir: $removed", LOGLEVEL_DEBUG);
25322529

25332530
// handle etc and home special
25342531
$home = rtrim($home_dir, '/') . '/home';

server/plugins-available/cron_jailkit_plugin.inc.php

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function insert($event_name, $data) {
7676
}
7777

7878
//* get data from web
79-
$parent_domain = $app->db->queryOneRecord("SELECT `domain_id`, `system_user`, `system_group`, `document_root`, `domain` FROM `web_domain` WHERE `domain_id` = ?", $data["new"]["parent_domain_id"]);
79+
$parent_domain = $app->db->queryOneRecord("SELECT * FROM `web_domain` WHERE `domain_id` = ?", $data["new"]["parent_domain_id"]);
8080
if(!$parent_domain["domain_id"]) {
8181
$app->log("Parent domain not found", LOGLEVEL_WARN);
8282
return 0;
@@ -107,6 +107,11 @@ function insert($event_name, $data) {
107107
$this->data = $data;
108108
$this->app = $app;
109109
$this->jailkit_config = $app->getconf->get_server_config($conf["server_id"], 'jailkit');
110+
foreach (array('jailkit_chroot_app_sections', 'jailkit_chroot_app_programs', 'jailkit_do_not_remove_paths') as $section) {
111+
if (isset($parent_domain[$section]) && $parent_domain[$section] != '' ) {
112+
$this->jailkit_config[$section] = $parent_domain[$section];
113+
}
114+
}
110115

111116
$this->_update_website_security_level();
112117

@@ -141,7 +146,7 @@ function update($event_name, $data) {
141146
return 0;
142147
}
143148
//* get data from web
144-
$parent_domain = $app->db->queryOneRecord("SELECT `domain_id`, `system_user`, `system_group`, `document_root`, `domain` FROM `web_domain` WHERE `domain_id` = ?", $data["new"]["parent_domain_id"]);
149+
$parent_domain = $app->db->queryOneRecord("SELECT * FROM `web_domain` WHERE `domain_id` = ?", $data["new"]["parent_domain_id"]);
145150
if(!$parent_domain["domain_id"]) {
146151
$app->log("Parent domain not found", LOGLEVEL_WARN);
147152
return 0;
@@ -167,20 +172,15 @@ function update($event_name, $data) {
167172
{
168173
$app->log("Jailkit Plugin (Cron) -> setting up jail", LOGLEVEL_DEBUG);
169174
// load the server configuration options
170-
/*
171-
$app->uses("getconf");
172-
$this->data = $data;
173-
$this->app = $app;
174-
$this->jailkit_config = $app->getconf->get_server_config($conf["server_id"], 'jailkit');
175-
$this->parent_domain = $parent_domain;
176-
177-
$this->_setup_jailkit_chroot();
178-
$this->_add_jailkit_user();
179-
*/
180175
$app->uses("getconf");
181176
$this->data = $data;
182177
$this->app = $app;
183178
$this->jailkit_config = $app->getconf->get_server_config($conf["server_id"], 'jailkit');
179+
foreach (array('jailkit_chroot_app_sections', 'jailkit_chroot_app_programs', 'jailkit_do_not_remove_paths') as $section) {
180+
if (isset($parent_domain[$section]) && $parent_domain[$section] != '' ) {
181+
$this->jailkit_config[$section] = $parent_domain[$section];
182+
}
183+
}
184184

185185
$this->_update_website_security_level();
186186

@@ -205,8 +205,25 @@ function update($event_name, $data) {
205205
function delete($event_name, $data) {
206206
global $app, $conf;
207207

208-
//* nothing to do here!
208+
if($data["old"]["parent_domain_id"] == '') {
209+
$app->log("Parent domain not set", LOGLEVEL_WARN);
210+
return 0;
211+
}
212+
213+
$app->uses('system');
214+
215+
if ($data['old']['type'] == "chrooted")
216+
{
217+
$parent_domain = $app->db->queryOneRecord("SELECT * FROM `web_domain` WHERE `domain_id` = ?", $data['old']['parent_domain_id']);
218+
219+
// should copy some _delete_homedir() functionality from shelluser_jailkit_plugin ?
209220
221+
if (isset($parent_domain['delete_unused_jailkit']) && $parent_domain['delete_unused_jailkit']) {
222+
$app->system->web_folder_protection($parent_domain['document_root'], false);
223+
$this->_delete_jailkit_if_unused($parent_domain['domain_id']);
224+
$app->system->web_folder_protection($parent_domain['document_root'], true);
225+
}
226+
}
210227
}
211228

212229
function _setup_jailkit_chroot()
@@ -247,6 +264,8 @@ function _setup_jailkit_chroot()
247264

248265
$app->system->file_put_contents($motd, $tpl->grab());
249266

267+
} else {
268+
$app->system->update_jailkit_chroot($this->data['new']['dir']);
250269
}
251270
$this->_add_jailkit_programs();
252271
}
@@ -311,5 +330,29 @@ function _update_website_security_level() {
311330
}
312331
}
313332

333+
private function _delete_jailkit_if_unused($parent_domain_id) {
334+
global $app, $conf;
335+
336+
// get jail directory
337+
$parent_domain = $app->db->queryOneRecord("SELECT * FROM `web_domain` WHERE `domain_id` = ? OR `parent_domain_id` = ? AND `document_root` IS NOT NULL", $parent_domain_id, $parent_domain_id);
338+
if (!is_dir($parent_domain['document_root'])) {
339+
return;
340+
}
341+
342+
// check for any shell_user using this jail
343+
$inuse = $app->db->queryOneRecord('SELECT shell_user_id FROM `shell_user` WHERE `parent_domain_id` = ? AND `chroot` = ?', $parent_domain_id, 'jailkit');
344+
if($inuse) {
345+
return;
346+
}
347+
348+
// check for any cron job using this jail
349+
$inuse = $app->db->queryOneRecord('SELECT id FROM `cron` WHERE `parent_domain_id` = ? AND `type` = ?', $parent_domain_id, 'chrooted');
350+
if($inuse) {
351+
return;
352+
}
353+
354+
$app->system->delete_jailkit_chroot($parent_domain['document_root']);
355+
}
356+
314357
} // end class
315358

server/plugins-available/shelluser_jailkit_plugin.inc.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ function delete($event_name, $data) {
273273
}
274274

275275
if (isset($web['delete_unused_jailkit']) && $web['delete_unused_jailkit']) {
276-
$app->system->delete_jailkit_if_unused($web['domain_id']);
276+
$this->_delete_jailkit_if_unused($web['domain_id']);
277277
}
278278

279279
$app->system->web_folder_protection($web['document_root'], true);
@@ -328,6 +328,8 @@ function _setup_jailkit_chroot()
328328

329329
$app->system->file_put_contents($motd, $tpl->grab());
330330

331+
} else {
332+
$app->system->update_jailkit_chroot($this->data['new']['dir']);
331333
}
332334
}
333335

@@ -562,6 +564,30 @@ private function _delete_homedir($homedir,$userid,$parent_domain_id) {
562564

563565
}
564566

567+
private function _delete_jailkit_if_unused($parent_domain_id) {
568+
global $app, $conf;
569+
570+
// get jail directory
571+
$parent_domain = $app->db->queryOneRecord("SELECT * FROM `web_domain` WHERE `domain_id` = ? OR `parent_domain_id` = ? AND `document_root` IS NOT NULL", $parent_domain_id, $parent_domain_id);
572+
if (!is_dir($parent_domain['document_root'])) {
573+
return;
574+
}
575+
576+
// check for any shell_user using this jail
577+
$inuse = $app->db->queryOneRecord('SELECT shell_user_id FROM `shell_user` WHERE `parent_domain_id` = ? AND `chroot` = ?', $parent_domain_id, 'jailkit');
578+
if($inuse) {
579+
return;
580+
}
581+
582+
// check for any cron job using this jail
583+
$inuse = $app->db->queryOneRecord('SELECT id FROM `cron` WHERE `parent_domain_id` = ? AND `type` = ?', $parent_domain_id, 'chrooted');
584+
if($inuse) {
585+
return;
586+
}
587+
588+
$app->system->delete_jailkit_chroot($parent_domain['document_root']);
589+
}
590+
565591
} // end class
566592

567593
?>

0 commit comments

Comments
 (0)