|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright (c) 2024, Daniel Jagszent |
| 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_letsencrypt_cleanup extends cronjob |
| 32 | +{ |
| 33 | + |
| 34 | + // job schedule |
| 35 | + protected $_schedule = '@weekly'; |
| 36 | + |
| 37 | + private $denylist = []; |
| 38 | + |
| 39 | + protected function onBeforeRun() |
| 40 | + { |
| 41 | + global $app, $conf; |
| 42 | + $app->uses('letsencrypt,ini_parser,getconf'); |
| 43 | + |
| 44 | + $server_db_record = $app->db->queryOneRecord("SELECT * FROM server WHERE server_id = ?", $conf['server_id']); |
| 45 | + if (! $server_db_record || ! $server_db_record['web_server']) { |
| 46 | + if ($conf['log_priority'] <= LOGLEVEL_DEBUG) { |
| 47 | + print 'Webserver not active, not running Let\'s Encrypt cleanup.'."\n"; |
| 48 | + } |
| 49 | + |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + $server_config = $app->getconf->get_server_config($conf['server_id'], 'server'); |
| 54 | + if (($server_config['migration_mode'] ?? 'n') != 'y') { |
| 55 | + if ($conf['log_priority'] <= LOGLEVEL_DEBUG) { |
| 56 | + print 'Migration mode active, not running Let\'s Encrypt cleanup.'."\n"; |
| 57 | + } |
| 58 | + |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + if (! $app->letsencrypt->get_acme_script() && ! $app->letsencrypt->get_certbot_script()) { |
| 63 | + if ($conf['log_priority'] <= LOGLEVEL_DEBUG) { |
| 64 | + print 'No Let\'s Encrypt client found, not running Let\'s Encrypt cleanup.'."\n"; |
| 65 | + } |
| 66 | + |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + $web_config = $app->getconf->get_server_config($conf['server_id'], 'web'); |
| 71 | + $le_auto_cleanup = $web_config['le_auto_cleanup'] ?? 'n'; |
| 72 | + if ($le_auto_cleanup == 'n') { |
| 73 | + return false; |
| 74 | + } |
| 75 | + $this->denylist = array_filter(array_map(function ($domain) use ($server_db_record) { |
| 76 | + $domain = trim($domain); |
| 77 | + if ($domain == '[server_name]') { |
| 78 | + return $server_db_record['server_name']; |
| 79 | + } |
| 80 | + |
| 81 | + return $domain; |
| 82 | + }, explode(',', $web_config['le_auto_cleanup_denylist'] ?? ''))); |
| 83 | + |
| 84 | + return parent::onBeforeRun(); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + public function onRunJob() |
| 89 | + { |
| 90 | + global $app, $conf; |
| 91 | + $used_serials = []; |
| 92 | + $active_ssl_records = $app->db->queryAllRecords( |
| 93 | + "SELECT * FROM web_domain WHERE active = 'y' AND ssl_letsencrypt = 'y' AND `ssl` = 'y' AND document_root IS NOT NULL AND server_id = ?", |
| 94 | + $conf['server_id'] |
| 95 | + ); |
| 96 | + foreach ($active_ssl_records as $active_ssl_record) { |
| 97 | + $cert_paths = $app->letsencrypt->get_website_certificate_paths(['new' => $active_ssl_record]); |
| 98 | + if (is_readable($cert_paths['crt'])) { |
| 99 | + $info = $app->letsencrypt->extract_x509($cert_paths['crt']); |
| 100 | + if ($info) { |
| 101 | + $used_serials[] = $info['serialNumber']; |
| 102 | + if ($conf['log_priority'] <= LOGLEVEL_DEBUG) { |
| 103 | + print 'mark serial number '.$info['serialNumber'].' as used from '.$cert_paths['crt']."\n"; |
| 104 | + } |
| 105 | + } else { |
| 106 | + if ($conf['log_priority'] <= LOGLEVEL_DEBUG) { |
| 107 | + print 'cannot extract x509 information from '.$cert_paths['crt']."\n"; |
| 108 | + } |
| 109 | + } |
| 110 | + } else { |
| 111 | + if ($conf['log_priority'] <= LOGLEVEL_DEBUG) { |
| 112 | + print $cert_paths['crt'].' is not readable'."\n"; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + $certificates = $app->letsencrypt->get_certificate_list(); |
| 118 | + foreach ($certificates as $certificate) { |
| 119 | + if (in_array($certificate['serialNumber'], $used_serials)) { |
| 120 | + if ($conf['log_priority'] <= LOGLEVEL_DEBUG) { |
| 121 | + print 'Skip '.$certificate['id'] . ' because it still gets used by ISPConfig' . "\n"; |
| 122 | + } |
| 123 | + continue; |
| 124 | + } |
| 125 | + foreach ($this->denylist as $domain) { |
| 126 | + if (in_array($domain, $certificate['domains'])) { |
| 127 | + if ($conf['log_priority'] <= LOGLEVEL_DEBUG) { |
| 128 | + print 'Skip '.$certificate['id'] . ' because it is on denylist' . "\n"; |
| 129 | + } |
| 130 | + continue 2; |
| 131 | + } |
| 132 | + } |
| 133 | + if ($app->letsencrypt->remove_certificate($certificate)) { |
| 134 | + print 'Removed unused certificate '.$certificate['id']."\n"; |
| 135 | + } else { |
| 136 | + print 'Error removing certificate '.$certificate['id']."\n"; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + parent::onRunJob(); |
| 141 | + } |
| 142 | +} |
0 commit comments