Skip to content

Commit aaf963a

Browse files
committed
schedule jails for update when server jailkit settings are changed
1 parent 479b394 commit aaf963a

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

server/plugins-available/webserver_plugin.inc.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function onLoad() {
5656
global $app;
5757

5858
$app->plugins->registerAction('server_plugins_loaded', $this->plugin_name, 'check_phpini_changes');
59+
$app->plugins->registerEvent('server_update', $this->plugin_name, 'server_update');
5960
}
6061

6162
/**
@@ -164,6 +165,87 @@ public function check_phpini_changes() {
164165
unset($processed);
165166
}
166167

168+
169+
/*
170+
* Checks for changes to jailkit settings in server config and schedules affected jails to be updated.
171+
*/
172+
function server_update($event_name, $data) {
173+
global $app, $conf;
174+
175+
// load the server configuration options
176+
$app->uses('ini_parser,system');
177+
178+
$old = $app->ini_parser->parse_ini_string($data['old']['config']);
179+
$new = $app->ini_parser->parse_ini_string($data['new']['config']);
180+
if (is_array($old) && is_array($new) && isset($old['jailkit']) && isset($new['jailkit'])) {
181+
$old = $old['jailkit'];
182+
$new = $new['jailkit'];
183+
} else {
184+
$app->log('server_update: could not parse jailkit section of server config.', LOGLEVEL_WARN);
185+
return;
186+
}
187+
188+
$hardlink_mode_changed = (boolean)(($old['jailkit_hardlinks'] != $new['jailkit_hardlinks']) && $new['jailkit_hardlinks'] != 'allow');
189+
190+
if (($old['jailkit_chroot_app_sections'] != $new['jailkit_chroot_app_sections']) ||
191+
($old['jailkit_chroot_app_programs'] != $new['jailkit_chroot_app_programs']) ||
192+
($old['jailkit_chroot_cron_programs'] != $new['jailkit_chroot_cron_programs']) ||
193+
($hardlink_mode_changed && $new['jailkit_hardlinks'] != 'allow'))
194+
{
195+
$app->log('Jailkit config has changed, scheduling affected chroot jails to be updated.', LOGLEVEL_DEBUG);
196+
197+
$web_domains = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE type = 'vhost' AND server_id = ?", $conf['server_id']);
198+
199+
foreach ($web_domains as $web) {
200+
// we could check (php_fpm_chroot == y || jailkit shell user exists || jailkit cron exists),
201+
// but will just shortcut the db checks to see if jailkit was setup previously:
202+
if (!is_dir($web['document_root'].'/etc/jailkit')) {
203+
continue;
204+
}
205+
206+
if ($hardlink_mode_changed ||
207+
// chroot cron programs changed
208+
($old['jailkit_chroot_cron_programs'] != $new['jailkit_chroot_cron_programs']) ||
209+
// jailkit sections changed and website does not overwrite
210+
(($old['jailkit_chroot_app_sections'] != $new['jailkit_chroot_app_sections']) &&
211+
(!(isset($web['jailkit_chroot_app_sections']) && $web['jailkit_chroot_app_sections'] != '' ))) ||
212+
// jailkit apps changed and website does not overwrite
213+
(($old['jailkit_chroot_app_programs'] != $new['jailkit_chroot_app_programs']) &&
214+
(!(isset($web['jailkit_chroot_app_programs']) && $web['jailkit_chroot_app_programs'] != '' ))))
215+
{
216+
217+
$sections = $new['jailkit_chroot_app_sections'];
218+
if (isset($web['jailkit_chroot_app_sections']) && $web['jailkit_chroot_app_sections'] != '' ) {
219+
$sections = $web['jailkit_chroot_app_sections'];
220+
}
221+
222+
$programs = $new['jailkit_chroot_app_programs'];
223+
if (isset($web['jailkit_chroot_app_sections']) && $web['jailkit_chroot_app_sections'] != '' ) {
224+
$programs = $web['jailkit_chroot_app_sections'];
225+
}
226+
227+
if (isset($new['jailkit_hardlinks'])) {
228+
if ($new['jailkit_hardlinks'] == 'yes') {
229+
$options = array( 'hardlink', );
230+
} elseif ($new['jailkit_hardlinks'] == 'no') {
231+
$options = array();
232+
}
233+
} else {
234+
$options = array( 'allow_hardlink', );
235+
}
236+
237+
$options[] = 'force';
238+
239+
// we could add a server config setting to allow updating these immediately:
240+
// $app->system->update_jailkit_chroot($new['document_root'], $sections, $programs, $options);
241+
//
242+
// but to mitigate disk contention, will just queue "update needed"
243+
// for jailkit maintenance cronjob via last_jailkit_update timestamp
244+
$app->db->query("UPDATE `web_domain` SET `last_jailkit_update` = FROM_UNIXTIME(0) WHERE `document_root` = ?", $web['document_root']);
245+
}
246+
}
247+
}
248+
}
167249
}
168250

169251
?>

0 commit comments

Comments
 (0)