|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * MikoPBX - free phone system for small business |
| 5 | + * Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License along with this program. |
| 18 | + * If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace Modules\ModuleGetSsl\Lib; |
| 22 | + |
| 23 | +use MikoPBX\Common\Models\PbxSettings; |
| 24 | +use MikoPBX\Core\System\Directories; |
| 25 | +use MikoPBX\Core\System\Processes; |
| 26 | +use MikoPBX\Core\System\System; |
| 27 | +use MikoPBX\Core\System\Util; |
| 28 | +use Modules\ModuleGetSsl\Models\ModuleGetSsl; |
| 29 | + |
| 30 | +/** |
| 31 | + * Manages temporary port 80 opening for ACME HTTP-01 validation. |
| 32 | + * |
| 33 | + * Creates a dedicated nginx server block on port 80 serving only |
| 34 | + * /.well-known/acme-challenge/ and adds iptables rules when firewall is active. |
| 35 | + */ |
| 36 | +class AcmeHttpPort |
| 37 | +{ |
| 38 | + private const LOCK_FILE = '/var/run/custom_modules/ModuleGetSsl/acme_port80.lock'; |
| 39 | + private const NGINX_ACME_CONF = '/etc/nginx/mikopbx/modules_servers/ModuleGetSsl_acme80.conf'; |
| 40 | + private const MAX_OPEN_SECONDS = 300; |
| 41 | + |
| 42 | + private string $logFile; |
| 43 | + |
| 44 | + public function __construct() |
| 45 | + { |
| 46 | + $logDir = Directories::getDir(Directories::CORE_LOGS_DIR); |
| 47 | + $this->logFile = "$logDir/ModuleGetSsl/last-result.log"; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Opens port 80 for ACME HTTP-01 validation. |
| 52 | + * |
| 53 | + * Creates a dedicated nginx server block and adds firewall rules if needed. |
| 54 | + * |
| 55 | + * @return bool true on success or if port is already open |
| 56 | + */ |
| 57 | + public function openPort(): bool |
| 58 | + { |
| 59 | + if ($this->isAlreadyOpen()) { |
| 60 | + $this->log('Port 80 already open, skipping'); |
| 61 | + return true; |
| 62 | + } |
| 63 | + |
| 64 | + $lockDir = dirname(self::LOCK_FILE); |
| 65 | + Util::mwMkdir($lockDir); |
| 66 | + $lockData = json_encode(['pid' => getmypid(), 'time' => time()]); |
| 67 | + file_put_contents(self::LOCK_FILE, $lockData); |
| 68 | + |
| 69 | + $domainName = $this->getDomainName(); |
| 70 | + if (empty($domainName)) { |
| 71 | + unlink(self::LOCK_FILE); |
| 72 | + $this->log('Port 80 open skipped: domain name is empty'); |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + $this->createNginxConf($domainName); |
| 77 | + $this->reloadNginx(); |
| 78 | + |
| 79 | + $firewallManaged = $this->isFirewallManaged(); |
| 80 | + if ($firewallManaged) { |
| 81 | + $this->addFirewallRules(); |
| 82 | + $this->log("Port 80 opened for $domainName (nginx + iptables)"); |
| 83 | + } else { |
| 84 | + $this->log("Port 80 opened for $domainName (nginx only, firewall not managed)"); |
| 85 | + } |
| 86 | + |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Closes port 80 after ACME validation completes. |
| 92 | + * |
| 93 | + * Removes the nginx config, reloads nginx, removes firewall rules, and cleans up the lock file. |
| 94 | + */ |
| 95 | + public function closePort(): void |
| 96 | + { |
| 97 | + if (file_exists(self::NGINX_ACME_CONF)) { |
| 98 | + unlink(self::NGINX_ACME_CONF); |
| 99 | + } |
| 100 | + $this->reloadNginx(); |
| 101 | + $this->removeFirewallRules(); |
| 102 | + |
| 103 | + if (file_exists(self::LOCK_FILE)) { |
| 104 | + unlink(self::LOCK_FILE); |
| 105 | + } |
| 106 | + |
| 107 | + $this->log('Port 80 closed'); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Cleans up stale port 80 state from a previous crash or timeout. |
| 112 | + */ |
| 113 | + public static function cleanupStale(): void |
| 114 | + { |
| 115 | + if (!file_exists(self::LOCK_FILE)) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + $lockContent = file_get_contents(self::LOCK_FILE); |
| 120 | + $lockData = json_decode($lockContent, true); |
| 121 | + if (!is_array($lockData)) { |
| 122 | + // Corrupted lock file, clean up |
| 123 | + $instance = new self(); |
| 124 | + $instance->closePort(); |
| 125 | + Util::sysLogMsg(__CLASS__, 'Cleaned up corrupted ACME port 80 lock file'); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + $pid = $lockData['pid'] ?? 0; |
| 130 | + $lockTime = $lockData['time'] ?? 0; |
| 131 | + $elapsed = time() - $lockTime; |
| 132 | + $pidDead = ($pid > 0) ? !file_exists("/proc/$pid") : true; |
| 133 | + |
| 134 | + if ($elapsed > self::MAX_OPEN_SECONDS || $pidDead) { |
| 135 | + $instance = new self(); |
| 136 | + $instance->closePort(); |
| 137 | + Util::sysLogMsg( |
| 138 | + __CLASS__, |
| 139 | + "Cleaned up stale ACME port 80 (elapsed: {$elapsed}s, pid: $pid, dead: " . ($pidDead ? 'yes' : 'no') . ')' |
| 140 | + ); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Checks if port 80 is already open by this module. |
| 146 | + */ |
| 147 | + private function isAlreadyOpen(): bool |
| 148 | + { |
| 149 | + if (!file_exists(self::LOCK_FILE)) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + $lockContent = file_get_contents(self::LOCK_FILE); |
| 154 | + $lockData = json_decode($lockContent, true); |
| 155 | + if (!is_array($lockData)) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + $pid = $lockData['pid'] ?? 0; |
| 160 | + if ($pid > 0 && file_exists("/proc/$pid")) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Gets domain name from module settings. |
| 169 | + */ |
| 170 | + private function getDomainName(): string |
| 171 | + { |
| 172 | + $settings = ModuleGetSsl::findFirst(); |
| 173 | + if ($settings === null) { |
| 174 | + return ''; |
| 175 | + } |
| 176 | + return $settings->domainName ?? ''; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Creates a dedicated nginx server block for ACME validation on port 80. |
| 181 | + */ |
| 182 | + private function createNginxConf(string $domainName): void |
| 183 | + { |
| 184 | + $confDir = dirname(self::NGINX_ACME_CONF); |
| 185 | + Util::mwMkdir($confDir); |
| 186 | + |
| 187 | + $conf = <<<NGINX |
| 188 | +server { |
| 189 | + listen 80; |
| 190 | + listen [::]:80; |
| 191 | + server_name $domainName; |
| 192 | +
|
| 193 | + location /.well-known/acme-challenge/ { |
| 194 | + root /usr/www/sites; |
| 195 | + allow all; |
| 196 | + } |
| 197 | +
|
| 198 | + location / { |
| 199 | + return 444; |
| 200 | + } |
| 201 | +} |
| 202 | +NGINX; |
| 203 | + |
| 204 | + file_put_contents(self::NGINX_ACME_CONF, $conf); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Reloads nginx configuration. |
| 209 | + */ |
| 210 | + private function reloadNginx(): void |
| 211 | + { |
| 212 | + $nginxPath = Util::which('nginx'); |
| 213 | + Processes::mwExec("$nginxPath -s reload"); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Adds iptables rules to allow traffic on port 80. |
| 218 | + */ |
| 219 | + private function addFirewallRules(): void |
| 220 | + { |
| 221 | + if (!$this->isFirewallManaged()) { |
| 222 | + return; |
| 223 | + } |
| 224 | + |
| 225 | + $iptablesPath = Util::which('iptables'); |
| 226 | + Processes::mwExec("$iptablesPath -I INPUT -p tcp --dport 80 -j ACCEPT"); |
| 227 | + |
| 228 | + $ip6tablesPath = Util::which('ip6tables'); |
| 229 | + Processes::mwExec("$ip6tablesPath -I INPUT -p tcp --dport 80 -j ACCEPT"); |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Removes iptables rules for port 80. |
| 234 | + */ |
| 235 | + private function removeFirewallRules(): void |
| 236 | + { |
| 237 | + if (!$this->isFirewallManaged()) { |
| 238 | + return; |
| 239 | + } |
| 240 | + |
| 241 | + $iptablesPath = Util::which('iptables'); |
| 242 | + Processes::mwExec("$iptablesPath -D INPUT -p tcp --dport 80 -j ACCEPT"); |
| 243 | + |
| 244 | + $ip6tablesPath = Util::which('ip6tables'); |
| 245 | + Processes::mwExec("$ip6tablesPath -D INPUT -p tcp --dport 80 -j ACCEPT"); |
| 246 | + } |
| 247 | + |
| 248 | + /** |
| 249 | + * Checks whether firewall rules need to be managed. |
| 250 | + * |
| 251 | + * Returns true only when PBX firewall is enabled AND system can manage iptables. |
| 252 | + */ |
| 253 | + private function isFirewallManaged(): bool |
| 254 | + { |
| 255 | + $firewallEnabled = PbxSettings::getValueByKey(PbxSettings::PBX_FIREWALL_ENABLED); |
| 256 | + if ($firewallEnabled !== '1') { |
| 257 | + return false; |
| 258 | + } |
| 259 | + return System::canManageFirewall(); |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * Appends a timestamped message to the module log file. |
| 264 | + */ |
| 265 | + private function log(string $message): void |
| 266 | + { |
| 267 | + $timestamp = date('Y-m-d H:i:s'); |
| 268 | + file_put_contents($this->logFile, "[$timestamp] $message" . PHP_EOL, FILE_APPEND); |
| 269 | + } |
| 270 | +} |
0 commit comments