|
| 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 | + |
0 commit comments