Skip to content

Commit f43d23f

Browse files
committed
cronjob for jailkit maintenance
1 parent fd5a162 commit f43d23f

File tree

4 files changed

+142
-5
lines changed

4 files changed

+142
-5
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
ALTER TABLE `web_domain` ADD `jailkit_chroot_app_sections` mediumtext NULL DEFAULT NULL;
22
ALTER TABLE `web_domain` ADD `jailkit_chroot_app_programs` mediumtext NULL DEFAULT NULL;
33
ALTER TABLE `web_domain` ADD `delete_unused_jailkit` enum('n','y') NOT NULL DEFAULT 'n';
4-
ALTER TABLE `web_domain` ADD `last_jailkit_update` date NULL DEFAULT NULL;
4+
ALTER TABLE `web_domain` ADD `last_jailkit_update` date NOT NULL DEFAULT FROM_UNIXTIME(0);
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2020, Jesse Norell <jesse@kci.net>
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
class cronjob_jailkit_maintenance extends cronjob {
32+
33+
// job schedule
34+
protected $_schedule = '*/5 * * * *';
35+
protected $_run_at_new = true;
36+
37+
//private $_tools = null;
38+
39+
/* this function is optional if it contains no custom code */
40+
public function onPrepare() {
41+
global $app;
42+
43+
parent::onPrepare();
44+
}
45+
46+
/* this function is optional if it contains no custom code */
47+
public function onBeforeRun() {
48+
global $app;
49+
50+
return parent::onBeforeRun();
51+
}
52+
53+
public function onRunJob() {
54+
global $app, $conf;
55+
56+
$app->uses('system,getconf');
57+
58+
$server_config = $app->getconf->get_server_config($conf['server_id'], 'server');
59+
if(isset($server_config['migration_mode']) && $server_config['migration_mode'] == 'y') {
60+
$app->log('Migration mode active, not running Jailkit updates.', LOGLEVEL_DEBUG);
61+
}
62+
63+
$update_options = array( 'allow_hardlink', );
64+
65+
$jailkit_config = $app->getconf->get_server_config($conf['server_id'], 'jailkit');
66+
if (isset($this->jailkit_config) && isset($this->jailkit_config['jailkit_hardlinks'])) {
67+
if ($this->jailkit_config['jailkit_hardlinks'] == 'yes') {
68+
$update_options = array( 'hardlink', );
69+
} elseif ($this->jailkit_config['jailkit_hardlinks'] == 'no') {
70+
unset($update_options['allow_hardlink']);
71+
}
72+
}
73+
74+
// limit the number of jails we update at one time according to time of day
75+
$num_jails_to_update = (date('H') < 6) ? 25 : 3;
76+
77+
$sql = "SELECT domain_id, domain, document_root, jailkit_chroot_app_sections, jailkit_chroot_app_programs, delete_unused_jailkit FROM web_domain WHERE type = 'vhost' AND last_jailkit_update < (NOW() - INTERVAL 24 HOUR) AND server_id = ? ORDER by last_jailkit_update LIMIT ?";
78+
$records = $app->db->queryAllRecords($sql, $conf['server_id'], $num_jails_to_update);
79+
80+
foreach($records as $rec) {
81+
if (!is_dir($rec['document_root']) || !is_dir($rec['document_root'].'/etc/jailkit')) {
82+
return;
83+
}
84+
85+
$app->log('Beginning jailkit maintenance for domain '.$rec['domain'].' at '.$rec['document_root'], LOGLEVEL_DEBUG);
86+
87+
// check for any shell_user using this jail
88+
$shell_user_inuse = $app->db->queryOneRecord('SELECT shell_user_id FROM `shell_user` WHERE `parent_domain_id` = ? AND `chroot` = ? AND `server_id` = ?', $rec['domain_id'], 'jailkit', $conf['server_id']);
89+
90+
// check for any cron job using this jail
91+
$cron_inuse = $app->db->queryOneRecord('SELECT id FROM `cron` WHERE `parent_domain_id` = ? AND `type` = ? AND `server_id` = ?', $rec['domain_id'], 'chrooted', $conf['server_id']);
92+
93+
if ($shell_user_inuse || $cron_inuse || $rec['delete_unused_jailkit'] != 'y') {
94+
$sections = $jailkit_config['jailkit_chroot_app_sections'];
95+
if (isset($web['jailkit_chroot_app_sections']) && $web['jailkit_chroot_app_sections'] != '') {
96+
$sections = $web['jailkit_chroot_app_sections'];
97+
}
98+
$programs = $jailkit_config['jailkit_chroot_app_programs'];
99+
if (isset($web['jailkit_chroot_app_programs']) && $web['jailkit_chroot_app_programs'] != '') {
100+
$programs = $web['jailkit_chroot_app_programs'];
101+
}
102+
$app->system->update_jailkit_chroot($rec['document_root'], $sections, $programs, $update_options);
103+
} else {
104+
if ($rec['delete_unused_jailkit'] == 'y') {
105+
$app->log('Removing unused jail: '.$rec['document_root'], LOGLEVEL_DEBUG);
106+
$app->system->delete_jailkit_chroot($rec['document_root']);
107+
}
108+
}
109+
110+
// might need to update master db here? checking....
111+
$app->db->query("UPDATE `web_domain` SET `last_jailkit_update` = NOW() WHERE `document_root` = ?", $rec['document_root']);
112+
}
113+
114+
parent::onRunJob();
115+
}
116+
117+
/* this function is optional if it contains no custom code */
118+
public function onAfterRun() {
119+
global $app;
120+
121+
parent::onAfterRun();
122+
}
123+
124+
}
125+

server/plugins-available/cron_jailkit_plugin.inc.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ 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) {
110+
foreach (array('jailkit_chroot_app_sections', 'jailkit_chroot_app_programs') as $section) {
111111
if (isset($parent_domain[$section]) && $parent_domain[$section] != '' ) {
112112
$this->jailkit_config[$section] = $parent_domain[$section];
113113
}
@@ -176,7 +176,7 @@ function update($event_name, $data) {
176176
$this->data = $data;
177177
$this->app = $app;
178178
$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) {
179+
foreach (array('jailkit_chroot_app_sections', 'jailkit_chroot_app_programs') as $section) {
180180
if (isset($parent_domain[$section]) && $parent_domain[$section] != '' ) {
181181
$this->jailkit_config[$section] = $parent_domain[$section];
182182
}
@@ -279,6 +279,9 @@ function _setup_jailkit_chroot()
279279
$app->system->update_jailkit_chroot($this->data['new']['dir'], $options);
280280
}
281281
$this->_add_jailkit_programs();
282+
283+
// might need to update master db here? checking....
284+
$app->db->query("UPDATE `web_domain` SET `last_jailkit_update` = NOW() WHERE `document_root` = ?", $this->data['new']['dir']);
282285
}
283286

284287
function _add_jailkit_programs()
@@ -363,6 +366,9 @@ private function _delete_jailkit_if_unused($parent_domain_id) {
363366
}
364367

365368
$app->system->delete_jailkit_chroot($parent_domain['document_root']);
369+
370+
// might need to update master db here? checking....
371+
$app->db->query("UPDATE `web_domain` SET `last_jailkit_update` = NOW() WHERE `document_root` = ?", $parent_domain['document_root']);
366372
}
367373

368374
} // end class

server/plugins-available/shelluser_jailkit_plugin.inc.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function insert($event_name, $data) {
109109
$this->data = $data;
110110
$this->app = $app;
111111
$this->jailkit_config = $app->getconf->get_server_config($conf["server_id"], 'jailkit');
112-
foreach (array('jailkit_chroot_app_sections', 'jailkit_chroot_app_programs', 'jailkit_do_not_remove_paths') as $section) {
112+
foreach (array('jailkit_chroot_app_sections', 'jailkit_chroot_app_programs') as $section) {
113113
if (isset($web[$section]) && $web[$section] != '' ) {
114114
$this->jailkit_config[$section] = $web[$section];
115115
}
@@ -192,7 +192,7 @@ function update($event_name, $data) {
192192
$this->data = $data;
193193
$this->app = $app;
194194
$this->jailkit_config = $app->getconf->get_server_config($conf["server_id"], 'jailkit');
195-
foreach (array('jailkit_chroot_app_sections', 'jailkit_chroot_app_programs', 'jailkit_do_not_remove_paths') as $section) {
195+
foreach (array('jailkit_chroot_app_sections', 'jailkit_chroot_app_programs') as $section) {
196196
if (isset($web[$section]) && $web[$section] != '' ) {
197197
$this->jailkit_config[$section] = $web[$section];
198198
}
@@ -342,6 +342,9 @@ function _setup_jailkit_chroot()
342342

343343
$app->system->update_jailkit_chroot($this->data['new']['dir'], $options);
344344
}
345+
346+
// might need to update master db here? checking....
347+
$app->db->query("UPDATE `web_domain` SET `last_jailkit_update` = NOW() WHERE `document_root` = ?", $this->data['new']['dir']);
345348
}
346349

347350
function _add_jailkit_programs()
@@ -597,6 +600,9 @@ private function _delete_jailkit_if_unused($parent_domain_id) {
597600
}
598601

599602
$app->system->delete_jailkit_chroot($parent_domain['document_root']);
603+
604+
// might need to update master db here? checking....
605+
$app->db->query("UPDATE `web_domain` SET `last_jailkit_update` = NOW() WHERE `document_root` = ?", $parent_domain['document_root']);
600606
}
601607

602608
} // end class

0 commit comments

Comments
 (0)