diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 92e60433..1a381e7a 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -83,7 +83,7 @@ jobs: with: php-version: ${{ matrix.php-versions }} tools: composer - extensions: sqlite3, gd + extensions: sqlite3, gd, mailparse - name: Get composer cache directory if: ${{ !env.ACT }} diff --git a/VIRTUAL_VACATION/Contributions.txt b/VIRTUAL_VACATION/Contributions.txt index 4fe49d8b..8bee2514 100644 --- a/VIRTUAL_VACATION/Contributions.txt +++ b/VIRTUAL_VACATION/Contributions.txt @@ -109,3 +109,10 @@ Additional authors: PBP stern fixes Minor parameter bugfix in get_accountname Move noreply addresses to our for optional conf override + +2026-08-13 Rodrigo Cortés Cano (@TrapoSAMA) + Added the initial PHP CLI Virtual Vacation modernization with + PostfixAdmin configuration discovery, dependency and service + diagnostics, native configuration generation, an interactive SMTP + test, safe message inspection, complete Vacation processing and + delivery, documentation, and unit tests. diff --git a/VIRTUAL_VACATION/PHP_README.md b/VIRTUAL_VACATION/PHP_README.md new file mode 100644 index 00000000..f8a91cae --- /dev/null +++ b/VIRTUAL_VACATION/PHP_README.md @@ -0,0 +1,471 @@ +# PostfixAdmin Virtual Vacation for PHP CLI + +This is a PHP CLI implementation of +`VIRTUAL_VACATION/vacation.pl`. It preserves the original Postfix transport +model and credits its historical contributors in +`VIRTUAL_VACATION/Contributions.txt`. + +> **Current status:** complete Vacation transport plus setup, diagnostics, +> SMTP testing, and read-only message inspection. It reads Postfix pipe input, +> resolves aliases, enforces notification intervals, builds the automatic +> reply, and delivers it through SMTP or an optional `sendmail` executable. + +## 1. Install the single runtime file + +Production installation needs only: + +```text +vacation.php +``` + +Choose the path, owner, and group according to the local Postfix policy. For +example, a transport running as `vmail` with access through group `mail` could +use: + +```bash +install -d -o root -g mail -m 750 /opt/postfixadmin +install -o root -g mail -m 750 vacation.php /opt/postfixadmin/vacation.php +``` + +This is an example, not a required identity or path. The script does not +create service users, change ownership, or modify Postfix. + +## 2. Requirements + +- PHP CLI 8.2 or newer, matching the current PostfixAdmin `master` requirement +- PDO and the driver used by PostfixAdmin: `pdo_mysql`, `pdo_pgsql`, or + `pdo_sqlite` +- `mbstring` +- `mailparse` +- `openssl` only when SMTP TLS/SSL is enabled +- `proc_open` only when the optional `sendmail` transport is selected +- An existing PostfixAdmin installation and database +- An SMTP service, normally Postfix on `localhost:25`, or an absolute + `sendmail`-compatible executable path + +`mailparse` is a PECL extension and is not bundled with PHP. It requires +`mbstring` to be loaded first. Typical package names include: + +```bash +# RHEL, Rocky Linux, or AlmaLinux, depending on the selected repository +dnf install php-pecl-mailparse + +# Debian or Ubuntu +apt install php-mailparse +``` + +Package names can vary with versioned PHP streams. Verify the PHP CLI used by +Postfix rather than only the web server runtime: + +```bash +php --ini +php -m | grep -E 'PDO|pdo_mysql|pdo_pgsql|pdo_sqlite|mbstring|mailparse' +php --ri mailparse +``` + +The message inspection action uses `mailparse` to process the incoming stream +and applies the safety decisions used by `vacation.pl`. + +## 3. Locate PostfixAdmin + +The selected directory must contain both `config.inc.php` and +`config.local.php`. A common path is: + +```text +/var/www/html/postfixadmin +``` + +If `--postfixadmin-root` is omitted, `vacation.php` searches the current +directory, its parent directories, the script directory and its parents, and +common installation locations. `POSTFIXADMIN_ROOT` can also provide the path. + +When exactly one installation is found, it is selected automatically. When +none is found, the command fails and requests `--postfixadmin-root`. When more +than one is found, interactive setup asks the administrator to select one; +non-interactive checks require an explicit path. + +PHP loads PostfixAdmin's normal `config.inc.php` and `config.local.php` flow. +It does not parse PHP assignments as text, and it never displays the database +password. + +## 4. Check dependencies + +Run: + +```bash +php /opt/postfixadmin/vacation.php --check-dependencies \ + --postfixadmin-root /var/www/html/postfixadmin +``` + +This checks: + +- PHP version +- `mbstring` and `mailparse` +- PDO +- The PDO driver selected by PostfixAdmin + +It does not connect to the database, inspect tables, contact SMTP, or send a +message. + +If PostfixAdmin cannot be found, the database driver cannot be selected and is +reported as `NOT TESTED`: + +```text +Configuration: + PostfixAdmin config MISSING - no installation found; use --postfixadmin-root /path/to/postfixadmin + +Dependencies: + Database driver NOT TESTED - database type is unavailable because PostfixAdmin configuration was not loaded + +Result: FAILED +``` + +Status meanings: + +- `OK`: the individual check succeeded. +- `MISSING`: a required file or extension was not found. +- `NOT TESTED`: a prerequisite failed. +- `WARNING`: the item works but needs administrative attention. +- `FAILED`: the item was tested and failed. + +## 5. Create the PHP configuration + +Run: + +```bash +php /opt/postfixadmin/vacation.php --init-config \ + --postfixadmin-root /var/www/html/postfixadmin +``` + +The command: + +1. Loads effective PostfixAdmin configuration. +2. Shows the selected database while hiding its password. +3. Prompts for SMTP server, port, and HELO; Enter accepts each proposed value. +4. Creates `/etc/postfixadmin/vacation.ini` with mode `0640`. +5. Leaves owner and group selection to the administrator. + +Example generated file: + +```ini +[postfixadmin] +root = "/var/www/html/postfixadmin" + +[smtp] +server = "localhost" +port = 25 +helo = "mail.example.org" + +[logging] +syslog = true +level = info +``` + +Database settings, table names, prefixes, mappings, and `vacation_domain` are +inherited from PostfixAdmin and are not duplicated by default. + +The PHP configuration is deliberately separate from the Perl file: + +```text +/etc/postfixadmin/vacation.conf existing Perl configuration +/etc/postfixadmin/vacation.ini PHP configuration +``` + +Creating `vacation.ini` never modifies `vacation.conf`. + +## 6. General configuration reference + +The following example contains every accepted key. `[database]` and +`[vacation]` are optional overrides and should normally be omitted so that +PostfixAdmin remains the source of truth. + +```ini +[postfixadmin] +root = "/var/www/html/postfixadmin" + +[smtp] +server = "localhost" +port = 25 +helo = "mail.example.org" +security = "none" +timeout = 120 +local_address = "" +username = "" +password = "" +# Leave empty to use SMTP; otherwise use an absolute executable path. +sendmail = "" + +# Optional dedicated database account. Normally omit this section. +[database] +type = "mysqli" +host = "127.0.0.1" +port = 3306 +socket = "" +name = "postfix" +user = "postfix_vacation" +password = "change-me" + +# Optional override. Normally inherit this from PostfixAdmin. +[vacation] +domain = "autoreply.example.org" + +# Optional message-policy overrides. Normally inherit recipient_delimiter +# from PostfixAdmin and use the built-in no-reply protection. +[message] +recipient_delimiter = "+" +no_vacation_pattern = "" +custom_noreply_pattern = false +noreply_pattern = "bounce|do-not-reply|facebook|linkedin|list-|myspace|twitter" +default_noreply_pattern = "^(noreply|no-reply|do_not_reply|no_reply|postmaster|mailer-daemon|listserv|majordomo|owner-|request-|bounces-)|(-(owner|request|bounces)@)" + +[reply] +friendly_from = "" +account_name = false +replace_from = "<%From_Date>" +replace_until = "<%Until_Date>" +date_format = "Y-m-d" + +[logging] +syslog = true +level = info +file_enabled = false +file = "/var/log/vacation.log" +``` + +Defaults and behavior: + +- `smtp.server`: `localhost` +- `smtp.port`: `25` +- `smtp.helo`: detected machine FQDN when empty; prompted only if detection + also fails +- `smtp.security`: `none`, `ssl`, `starttls`, or `maybestarttls`; the default + is `none`. These preserve the existing `vacation.pl` SMTP modes. +- `smtp.timeout`: connection and response timeout in seconds; default `120` +- `smtp.local_address`: optional local source address +- `smtp.username` and `smtp.password`: optional SMTP authentication. The PHP + client supports the server-advertised `PLAIN` and `LOGIN` mechanisms. + `VACATION_SMTP_PASSWORD` can supply the password to the process without + storing it in the file and takes precedence over `smtp.password`. +- `smtp.sendmail`: absolute executable path used instead of SMTP when set. +- `message.recipient_delimiter`: optional override for the value inherited + from PostfixAdmin. +- `message.no_vacation_pattern`: optional regular-expression fragment for To + addresses that must never trigger a Vacation reply. +- `message.custom_noreply_pattern` and `message.noreply_pattern`: enable and + define an additional sender exclusion pattern. +- `message.default_noreply_pattern`: optional override for the built-in + protection against bounce, daemon, list, owner, and request addresses. Omit + it to retain the built-in expression; an explicitly empty value disables + that protection and is not recommended. +- `reply.friendly_from`: optional display name used in the From header. +- `reply.account_name`: use the mailbox `name` value instead of + `reply.friendly_from` when the database value is not empty. +- `reply.replace_from` and `reply.replace_until`: placeholders replaced in the + body with the Vacation start and end dates. +- `reply.date_format`: PHP date format for those replacements; default `Y-m-d`. +- `logging.syslog`: send transport events to syslog; default `true`. +- `logging.level`: `error`, `info`, or `debug`; default `info`. +- `logging.file_enabled` and `logging.file`: optional append-only file logging. + +Without `--config`, the script searches in this order: + +1. `/etc/mail/postfixadmin/vacation.ini` +2. `/etc/postfixadmin/vacation.ini` +3. `vacation.ini` in the current working directory + +Show the selected path with: + +```bash +php /opt/postfixadmin/vacation.php --show-config-path +``` + +## 7. Optional dedicated database account + +The `[database]` section can select a dedicated account instead of the main +PostfixAdmin credentials. Read access is sufficient for diagnostics, while the +production transport follows `vacation.pl` and requires: + +- `SELECT` on `vacation`, `alias`, `alias_domain`, and `mailbox`. +- `SELECT`, `INSERT`, `UPDATE`, and `DELETE` on + `vacation_notification`. +- No schema or privilege-management rights. + +Example for standard MySQL or MariaDB names: + +```sql +GRANT SELECT ON postfix.vacation TO 'postfix_vacation'@'localhost'; +GRANT SELECT ON postfix.alias TO 'postfix_vacation'@'localhost'; +GRANT SELECT ON postfix.alias_domain TO 'postfix_vacation'@'localhost'; +GRANT SELECT ON postfix.mailbox TO 'postfix_vacation'@'localhost'; +GRANT SELECT, INSERT, UPDATE, DELETE ON postfix.vacation_notification + TO 'postfix_vacation'@'localhost'; +``` + +Adapt the database name, account host, prefix, table mappings, and engine. The +script never creates accounts or grants privileges. + +## 8. Optionally import settings from Perl + +To read supported SMTP values from the existing Perl configuration: + +```bash +php /opt/postfixadmin/vacation.php --init-config \ + --postfixadmin-root /var/www/html/postfixadmin \ + --import-legacy /etc/postfixadmin/vacation.conf +``` + +The importer reads only simple assignments. In addition to `smtp_server`, +`smtp_server_port`, and `smtp_helo`, it preserves `smtp_ssl`, `smtp_timeout`, +`smtp_client`, `smtp_authid`, and `smtp_authpwd` when present. It does not +execute the Perl file and does not modify it. It also preserves simple +`recipient_delimiter`, `no_vacation_pattern`, `custom_noreply_pattern`, +`noreply_pattern`, and `default_noreply_pattern` assignments. Perl `smtp_ssl` +values map as follows: `0` to `none`, `1` to `ssl`, while `starttls` and +`maybestarttls` retain their names. It also preserves `sendmail_bin`, +`friendly_from`, `accountname_check`, `replace_from`, `replace_until`, and +`date_format`, together with `syslog`, `log_level`, `log_to_file`, and +`logfile`. Perl `strftime` date tokens are converted to their PHP equivalents +when the native configuration is generated. + +If `vacation.ini` already exists, setup stops without changing it. Use +`--force` only when replacing the PHP configuration is intentional. A file +recognized as Perl configuration is never overwritten, even with `--force`. + +## 9. Run the complete check + +```bash +php /opt/postfixadmin/vacation.php --check \ + --config /etc/postfixadmin/vacation.ini +``` + +The complete check validates PHP, extensions, effective PostfixAdmin +configuration, database connectivity, the five required tables, and the +configured SMTP or `sendmail` delivery path. It also displays the effective +Vacation domain inherited from PostfixAdmin or selected by the optional +`[vacation]` override. It does not send a message. + +## 10. Send a simple SMTP test + +```bash +php /opt/postfixadmin/vacation.php --test \ + --config /etc/postfixadmin/vacation.ini +``` + +SMTP server, port, and configured HELO come from `vacation.ini` and are +not prompted again. HELO is prompted only when it is neither configured nor +detectable. The test asks only for the envelope addresses: + +```text +MAIL FROM [noreply@example.org]: +RCPT TO: admin@example.org +Sending test message from noreply@example.org to admin@example.org using localhost:25... +Test message sent successfully. +``` + +Enter accepts the proposed `MAIL FROM`. Test values are not saved. The message +is predefined, and the test neither queries vacation records nor reads or +modifies `vacation_notification`. + +After SMTP accepts the message, queueing, routing, MX policy, retries, and final +delivery belong to the SMTP server. `vacation.php` does not implement MX policy. + +The normal default remains plain local SMTP on `localhost:25`. Optional +implicit TLS/SSL, required STARTTLS, opportunistic STARTTLS, source-address +binding, timeouts, and SMTP authentication preserve the corresponding +`vacation.pl` delivery choices. TLS certificate and hostname verification are +enabled. + +## 11. Inspect an incoming message without sending a reply + +The inspection action reads a message, evaluates the same header protections +that start at [line 746 of `vacation.pl`](https://github.com/postfixadmin/postfixadmin/blob/master/VIRTUAL_VACATION/vacation.pl#L746), +and reports whether it is eligible for later Vacation processing: + +```bash +php /opt/postfixadmin/vacation.php \ + --inspect-message /path/to/message.eml \ + --config /etc/postfixadmin/vacation.ini \ + -f sender@example.org -- user#example.org@autoreply.example.org +``` + +Use `--inspect-message -` to read the message from standard input. The action: + +- reads MIME headers with `mailparse` and does not inspect message-body content +- requires From, To, Message-ID, envelope sender, and envelope recipient +- restores the real recipient from the configured `vacation_domain` +- honors the configured recipient delimiter +- rejects spam, virus, bulk, list, automatic-response, and Vacation-loop headers +- rejects invalid, no-reply, self-addressed, and configured excluded addresses +- performs no database writes and sends no message + +An intentionally ignored message exits successfully, matching the safe pipe +behavior of `vacation.pl`, and prints the reason: + +```text +Message inspection: IGNORED - bulk or list Precedence header found +``` + +An eligible message reports the normalized envelope information but is not +sent or recorded: + +```text +Message inspection: ELIGIBLE - message is eligible for Vacation processing +``` + +This action validates parsing and safety decisions without invoking the normal +database and delivery stages. + +## 12. Production Vacation transport + +The normal pipe contract is compatible with `vacation.pl`: + +```bash +php /opt/postfixadmin/vacation.php \ + --config /etc/postfixadmin/vacation.ini \ + -f sender@example.org -- user#example.org@autoreply.example.org < message.eml +``` + +The production path: + +- parses and rejects unsafe automatic, list, spam, virus, loop, and no-reply mail +- restores the original recipient from `vacation_domain` +- resolves direct Vacation records, aliases, alias domains, and catch-all aliases +- requires an active record within `activefrom` and `activeuntil` +- atomically claims or renews `vacation_notification` and honors + `interval_time`, preventing concurrent messages from both sending a reply +- releases a newly claimed notification when generation or delivery fails so a + Postfix retry is not suppressed +- substitutes `$SUBJECT` and configured date placeholders +- applies the configured friendly name or mailbox account name +- builds the UTF-8 plain-text reply with the historical loop-prevention headers +- delivers through SMTP, TLS/SSL/STARTTLS and authentication, or `sendmail` + +Ignored messages and recipients without active Vacation exit successfully. +Exit statuses distinguish failures so Postfix does not retry permanent errors: + +- `64`: invalid command-line usage +- `65`: invalid message data that cannot be parsed +- `75`: temporary database, SMTP, sendmail, or I/O failure +- `78`: invalid configuration or a missing required runtime dependency + +The legacy `-t yes` transport option generates and prints the reply without +delivery. As in `vacation.pl`, it still performs the database and notification +checks, so use it only with a sender whose notification state may be updated. + +Running without an action or pipe arguments prints one usage line and exits +with status `69`. + +## 13. Postfix `master.cf` example + +After `--check` succeeds and the administrator has validated the transport on +the target installation, the PHP service can replace the Perl command: + +```text +vacation unix - n n - - pipe + flags=Rq user=vmail argv=/usr/bin/php /opt/postfixadmin/vacation.php -f ${sender} -- ${recipient} +``` + +This is an example, not an automatic configuration change. The administrator +chooses the service user, path, process limit, rollout, and rollback procedure. +Keep the Perl command available until the local PHP transport has been observed +successfully in production. diff --git a/VIRTUAL_VACATION/vacation.php b/VIRTUAL_VACATION/vacation.php new file mode 100755 index 00000000..5766aded --- /dev/null +++ b/VIRTUAL_VACATION/vacation.php @@ -0,0 +1,2502 @@ +#!/usr/bin/env php + $configuration + */ + public function inspectStream( + $stream, + string $envelopeSender, + string $envelopeRecipient, + array $configuration, + ): MessageInspectionResult { + if (!extension_loaded('mailparse')) { + throw new ConfigurationException('The mailparse extension is required to inspect a message stream'); + } + return $this->inspectHeaders( + $this->parseHeaders($stream), + $envelopeSender, + $envelopeRecipient, + $configuration, + ); + } + + /** + * @param array> $headers + * @param array $configuration + */ + public function inspectHeaders( + array $headers, + string $envelopeSender, + string $envelopeRecipient, + array $configuration = [], + ): MessageInspectionResult { + $headers = $this->normalizeHeaders($headers); + $rejection = $this->headerRejectionReason($headers); + if ($rejection !== null) { + return new MessageInspectionResult(false, $rejection); + } + + $fromRaw = $this->firstHeader($headers, 'from'); + $toRaw = $this->firstHeader($headers, 'to'); + $ccRaw = $this->firstHeader($headers, 'cc'); + $replyToRaw = $this->firstHeader($headers, 'reply-to'); + $subject = $this->firstHeader($headers, 'subject'); + $messageId = $this->firstHeader($headers, 'message-id'); + $vacationDomain = trim((string)($configuration['vacation_domain'] ?? '')); + $delimiter = (string)($configuration['recipient_delimiter'] ?? ''); + $envelopeSender = $this->normalizeEnvelopeAddress($envelopeSender); + $envelopeRecipient = $this->normalizeEnvelopeAddress( + $this->restoreVacationRecipient($envelopeRecipient, $vacationDomain, $delimiter) + ); + + if ($fromRaw === '' || $toRaw === '' || $messageId === '' + || $envelopeSender === '' || $envelopeRecipient === '' + ) { + return new MessageInspectionResult(false, 'a required message or envelope field is missing'); + } + + $noVacationPattern = trim((string)( + $configuration['message_no_vacation_pattern'] ?? self::DEFAULT_NO_VACATION_PATTERN + )); + if ($noVacationPattern !== '' && $this->matchesPattern($toRaw, $noVacationPattern)) { + return new MessageInspectionResult(false, 'the To header matches no_vacation_pattern'); + } + + $from = $this->extractAddresses($fromRaw); + $to = $this->extractAddresses($toRaw); + $cc = $this->extractAddresses($ccRaw); + $replyTo = $this->extractAddresses($replyToRaw); + if ($from === [] || $to === []) { + return new MessageInspectionResult(false, 'a required header contains no valid address'); + } + if ($replyToRaw !== '' && $replyTo === []) { + return new MessageInspectionResult(false, 'the Reply-To header contains no valid address'); + } + + $addressesToCheck = array_merge($from, $replyTo, [$envelopeSender, $envelopeRecipient]); + foreach ($addressesToCheck as $address) { + if ($this->isNoReplyAddress($address, $configuration)) { + return new MessageInspectionResult(false, "no-reply address detected: {$address}"); + } + } + if ($envelopeSender === $envelopeRecipient) { + return new MessageInspectionResult(false, 'envelope sender and recipient are the same'); + } + if (in_array($envelopeSender, array_merge($to, $cc), true)) { + return new MessageInspectionResult(false, 'the envelope sender is also a To or Cc recipient'); + } + + return new MessageInspectionResult( + true, + 'message is eligible for Vacation processing', + implode(', ', $from), + implode(', ', $to), + implode(', ', $cc), + implode(', ', $replyTo), + $subject, + $messageId, + $envelopeSender, + $envelopeRecipient, + ); + } + + public function restoreVacationRecipient(string $recipient, string $vacationDomain, string $delimiter = ''): string + { + $recipient = trim($recipient, "<> \t\n\r\0\x0B"); + $suffix = '@' . strtolower(trim($vacationDomain, '. ')); + if ($vacationDomain === '' || !str_ends_with(strtolower($recipient), $suffix)) { + return $recipient; + } + $encoded = substr($recipient, 0, -strlen($suffix)); + $restored = str_replace('#', '@', $encoded); + $at = strrpos($restored, '@'); + if ($at === false) { + return $restored; + } + $local = substr($restored, 0, $at); + $domain = substr($restored, $at + 1); + if ($delimiter !== '' && ($position = strpos($local, $delimiter)) !== false) { + $local = substr($local, 0, $position); + } + return "{$local}@{$domain}"; + } + + /** @param resource $stream @return array> */ + private function parseHeaders($stream): array + { + $message = call_user_func('mailparse_msg_create'); + try { + $headerData = ''; + while (!feof($stream)) { + $chunk = fread($stream, 8192); + if ($chunk === false) { + throw new RuntimeException('Could not read the message stream'); + } + $headerData .= $chunk; + $separator = $this->headerSeparator($headerData); + if ($separator !== null) { + $headerData = substr($headerData, 0, $separator); + break; + } + } + if ($headerData !== '' && call_user_func('mailparse_msg_parse', $message, $headerData) === false) { + throw new MessageDataException('mailparse could not parse the message headers'); + } + $partData = call_user_func('mailparse_msg_get_part_data', $message); + if (!isset($partData['headers']) || !is_array($partData['headers'])) { + throw new MessageDataException('mailparse did not return message headers'); + } + $headers = []; + foreach ($partData['headers'] as $name => $values) { + if (!is_string($name)) { + continue; + } + $values = is_array($values) ? $values : [$values]; + foreach ($values as $value) { + if (is_scalar($value)) { + $headers[$name][] = (string)$value; + } + } + } + return $headers; + } finally { + call_user_func('mailparse_msg_free', $message); + } + } + + private function headerSeparator(string $message): ?int + { + $positions = []; + foreach (["\r\n\r\n", "\n\n", "\r\r"] as $separator) { + $position = strpos($message, $separator); + if ($position !== false) { + $positions[] = $position + strlen($separator); + } + } + return $positions === [] ? null : min($positions); + } + + /** + * @param array> $headers + * @return array> + */ + private function normalizeHeaders(array $headers): array + { + $normalized = []; + foreach ($headers as $name => $values) { + $name = strtolower(trim($name)); + foreach (is_array($values) ? $values : [$values] as $value) { + $value = preg_replace('/\r?\n[ \t]+/', ' ', (string)$value) ?? (string)$value; + $value = preg_replace('/[\r\n]+/', ' ', $value) ?? $value; + $normalized[$name][] = trim($value); + } + } + return $normalized; + } + + /** @param array> $headers */ + private function headerRejectionReason(array $headers): ?string + { + $rules = [ + ['x-spam-flag', '/^\s*yes\b/i', 'X-Spam-Flag indicates spam'], + ['x-spam-status', '/^\s*yes\b/i', 'X-Spam-Status indicates spam'], + ['x-facebook-notify', '/.*/s', 'Facebook notification header found'], + ['x-amazon-mail-relay-type', '/^\s*notification\b/i', 'Amazon notification header found'], + ['precedence', '/^\s*(?:bulk|list|junk)\b/i', 'bulk or list Precedence header found'], + ['x-loop', '/^\s*postfix admin virtual vacation\b/i', 'Vacation loop header found'], + ['list-id', '/.*/s', 'List-Id header found'], + ['list-post', '/.*/s', 'List-Post header found'], + ['list-unsubscribe', '/.*/s', 'List-Unsubscribe header found'], + ['x-barracuda-spam-status', '/^\s*yes\b/i', 'Barracuda spam status header found'], + ['x-dspam-result', '/^\s*(?:spam|bl[ao]cklisted)\b/i', 'DSPAM rejection header found'], + ['x-virus-status', '/^\s*infected\b/i', 'virus status header found'], + ['x-antivirus-status', '/^\s*infected\b/i', 'antivirus status header found'], + ['x-avas-virus-status', '/^\s*infected\b/i', 'AVAS virus status header found'], + ['x-avas-spam-status', '/^\s*spam\b/i', 'AVAS spam status header found'], + ['x-spamtest-status', '/^\s*spam\b/i', 'SpamTest status header found'], + ['x-crm114-status', '/^\s*spam\b/i', 'CRM114 status header found'], + ['x-razor-status', '/^\s*spam\b/i', 'Razor status header found'], + ['x-pyzor-status', '/^\s*spam\b/i', 'Pyzor status header found'], + ['x-osbf-lua-score', '/^[0-9\.\/\-+]+\s+\[[\-S]\]/i', 'OSBF-Lua spam score header found'], + ['x-autogenerated', '/^\s*reply\b/i', 'automatic reply header found'], + ['x-auto-response-suppress', '/^\s*(?:oof|all)\b/i', 'automatic response suppression header found'], + ]; + foreach ($rules as [$name, $pattern, $reason]) { + foreach ($headers[$name] ?? [] as $value) { + if (preg_match($pattern, $value)) { + return $reason; + } + } + } + foreach ($headers['auto-submitted'] ?? [] as $value) { + if (!preg_match('/^\s*no\b/i', $value)) { + return 'Auto-Submitted indicates an automatically generated message'; + } + } + return null; + } + + /** @param array> $headers */ + private function firstHeader(array $headers, string $name): string + { + return $headers[$name][0] ?? ''; + } + + /** @return list */ + private function extractAddresses(string $value): array + { + if ($value === '') { + return []; + } + $addresses = []; + if (extension_loaded('mailparse')) { + $parsed = call_user_func('mailparse_rfc822_parse_addresses', $value); + foreach ($parsed as $entry) { + if (is_array($entry) && isset($entry['address'])) { + $addresses[] = (string)$entry['address']; + } + } + } else { + preg_match_all( + "/[A-Z0-9.!#$%&'*+\\/=\?^_`{|}~-]+@[A-Z0-9.-]+/i", + $value, + $matches, + ); + $addresses = $matches[0]; + } + $valid = []; + foreach ($addresses as $address) { + $address = strtolower(trim($address, "<> \t\n\r\0\x0B")); + if (filter_var($address, FILTER_VALIDATE_EMAIL) === $address) { + $valid[$address] = $address; + } + } + return array_values($valid); + } + + private function normalizeEnvelopeAddress(string $address): string + { + $address = strtolower(trim($address, "<> \t\n\r\0\x0B")); + return filter_var($address, FILTER_VALIDATE_EMAIL) === $address ? $address : ''; + } + + /** @param array $configuration */ + private function isNoReplyAddress(string $address, array $configuration): bool + { + $defaultPattern = (string)($configuration['message_default_noreply_pattern'] + ?? self::DEFAULT_NOREPLY_PATTERN); + if ($defaultPattern !== '' && $this->matchesPattern($address, $defaultPattern)) { + return true; + } + $customEnabled = filter_var( + $configuration['message_custom_noreply_pattern'] ?? false, + FILTER_VALIDATE_BOOL, + ); + $customPattern = trim((string)($configuration['message_noreply_pattern'] ?? '')); + return $customEnabled && $customPattern !== '' && $this->matchesPattern($address, $customPattern); + } + + private function matchesPattern(string $value, string $pattern): bool + { + $result = @preg_match('~' . str_replace('~', '\\~', $pattern) . '~i', $value); + if ($result === false) { + throw new ConfigurationException("Invalid configured message pattern: {$pattern}"); + } + return $result === 1; + } +} + +final class VacationRepository +{ + /** @var array */ + private array $tables; + + /** @param array $tables */ + public function __construct(private readonly PDO $database, array $tables, private readonly string $databaseType) + { + foreach (['vacation', 'vacation_notification', 'alias', 'alias_domain', 'mailbox'] as $key) { + $identifier = (string)($tables[$key] ?? $key); + if (!preg_match('/^[A-Za-z_][A-Za-z0-9_$]*$/', $identifier)) { + throw new ConfigurationException("Unsafe table identifier: {$identifier}"); + } + $this->tables[$key] = $this->quoteIdentifier($identifier); + } + } + + /** @return array|null */ + public function findActiveVacation(string $email, string $vacationDomain): ?array + { + return $this->resolveActiveVacation(strtolower($email), strtolower($vacationDomain), 0, []); + } + + /** @return array|null @param array $visited */ + private function resolveActiveVacation(string $email, string $vacationDomain, int $depth, array $visited): ?array + { + if ($depth >= 20 || isset($visited[$email])) { + throw new RuntimeException("Vacation alias resolution loop detected at {$email}"); + } + $visited[$email] = true; + $vacation = $this->activeVacation($email); + if ($vacation !== null) { + return $vacation; + } + + $encodedVacation = strtolower(str_replace('@', '#', $email) . '@' . $vacationDomain); + $statement = $this->database->prepare("SELECT goto FROM {$this->tables['alias']} WHERE address = ?"); + $statement->execute([$email]); + foreach ($statement->fetchAll(PDO::FETCH_COLUMN) as $targets) { + $aliases = array_values(array_filter(array_map('trim', explode(',', strtolower((string)$targets))))); + $hasVacationAlias = false; + foreach ($aliases as $alias) { + if ($alias === $encodedVacation) { + $hasVacationAlias = true; + break; + } + } + if (!$hasVacationAlias) { + continue; + } + foreach ($aliases as $alias) { + $vacation = $this->activeVacation($alias); + if ($vacation !== null) { + return $vacation; + } + } + } + + if (!str_contains($email, '@')) { + return null; + } + [$user, $domain] = explode('@', $email, 2); + $statement = $this->database->prepare( + "SELECT target_domain FROM {$this->tables['alias_domain']} WHERE alias_domain = ?" + ); + $statement->execute([$domain]); + $targetDomain = $statement->fetchColumn(); + if (is_string($targetDomain) && $targetDomain !== '') { + return $this->resolveActiveVacation( + "{$user}@" . strtolower($targetDomain), + $vacationDomain, + $depth + 1, + $visited, + ); + } + + $statement = $this->database->prepare("SELECT goto FROM {$this->tables['alias']} WHERE address = ?"); + $statement->execute(["@{$domain}"]); + $wildcard = $statement->fetchColumn(); + if (!is_string($wildcard) || $wildcard === '') { + return null; + } + $wildcard = strtolower(trim(explode(',', $wildcard, 2)[0])); + if (!str_contains($wildcard, '@')) { + return null; + } + [$wildcardUser, $wildcardDomain] = explode('@', $wildcard, 2); + $target = $wildcardUser !== '' ? $wildcard : "{$user}@{$wildcardDomain}"; + return $this->resolveActiveVacation($target, $vacationDomain, $depth + 1, $visited); + } + + /** @return array|null */ + private function activeVacation(string $email): ?array + { + $statement = $this->database->prepare( + "SELECT email, subject, body, activefrom, activeuntil, interval_time, active " + . "FROM {$this->tables['vacation']} WHERE email = ?" + ); + $statement->execute([$email]); + $row = $statement->fetch(PDO::FETCH_ASSOC); + if (!is_array($row) || !$this->databaseBoolean($row['active'] ?? false)) { + return null; + } + $now = time(); + $from = strtotime((string)($row['activefrom'] ?? '')); + $until = strtotime((string)($row['activeuntil'] ?? '')); + if ($from === false || $until === false || $from > $now || $until < $now) { + return null; + } + return $row; + } + + public function claimNotification(array $vacation, string $sender): bool + { + $email = strtolower((string)$vacation['email']); + $sender = strtolower($sender); + try { + $insert = $this->database->prepare( + "INSERT INTO {$this->tables['vacation_notification']} " + . '(on_vacation, notified) VALUES (?, ?)' + ); + $insert->execute([$email, $sender]); + return true; + } catch (PDOException $exception) { + if (!in_array((string)$exception->getCode(), ['23000', '23505'], true)) { + throw $exception; + } + } + + $interval = max(0, (int)($vacation['interval_time'] ?? 0)); + $timestampStatement = $this->database->query('SELECT CURRENT_TIMESTAMP'); + $databaseTimestamp = $timestampStatement === false ? false : $timestampStatement->fetchColumn(); + if (!is_string($databaseTimestamp) || $databaseTimestamp === '') { + throw new RuntimeException('The database did not return its current timestamp'); + } + $databaseNow = new DateTimeImmutable($databaseTimestamp, new DateTimeZone('UTC')); + $conditions = []; + $parameters = []; + $activeFrom = trim((string)($vacation['activefrom'] ?? '')); + if ($activeFrom !== '') { + $conditions[] = 'notified_at < ?'; + $parameters[] = $activeFrom; + } + if ($interval > 0) { + $conditions[] = 'notified_at < ?'; + $parameters[] = $databaseNow->modify("-{$interval} seconds")->format('Y-m-d H:i:s'); + } + if ($conditions === []) { + return false; + } + array_push($parameters, $email, $sender); + $update = $this->database->prepare( + "UPDATE {$this->tables['vacation_notification']} SET notified_at = CURRENT_TIMESTAMP " + . 'WHERE (' . implode(' OR ', $conditions) . ') AND on_vacation = ? AND notified = ?' + ); + $update->execute($parameters); + return $update->rowCount() === 1; + } + + public function accountName(string $email): string + { + $statement = $this->database->prepare("SELECT name FROM {$this->tables['mailbox']} WHERE username = ?"); + $statement->execute([$email]); + $name = $statement->fetchColumn(); + return is_string($name) ? trim($name) : ''; + } + + public function forgetNotification(string $vacationAddress, string $sender): void + { + $statement = $this->database->prepare( + "DELETE FROM {$this->tables['vacation_notification']} WHERE on_vacation = ? AND notified = ?" + ); + $statement->execute([strtolower($vacationAddress), strtolower($sender)]); + } + + private function databaseBoolean(mixed $value): bool + { + return in_array(strtolower((string)$value), ['1', 't', 'true', 'y', 'yes'], true); + } + + private function quoteIdentifier(string $identifier): string + { + return $this->databaseType === 'mysql' ? "`{$identifier}`" : '"' . $identifier . '"'; + } +} + +final class VacationReplyComposer +{ + /** @param array $vacation @param array $configuration */ + public function compose( + array $vacation, + MessageInspectionResult $incoming, + array $configuration, + string $accountName = '', + ): string { + $email = strtolower((string)$vacation['email']); + $subject = str_replace('$SUBJECT', $this->decodeHeader($incoming->subject), (string)$vacation['subject']); + $body = $this->replaceDates((string)$vacation['body'], $vacation, $configuration); + $friendly = trim((string)($configuration['reply_friendly_from'] ?? '')); + if (!empty($configuration['reply_account_name']) && $accountName !== '') { + $friendly = $accountName; + } + $from = $friendly === '' ? $email : $this->encodeHeader($friendly) . " <{$email}>"; + $headers = [ + 'To: ' . $incoming->envelopeSender, + 'From: ' . $from, + 'Subject: ' . $this->encodeHeader($subject), + 'Precedence: junk', + 'Content-Type: text/plain; charset=UTF-8', + 'Content-Transfer-Encoding: 8bit', + 'MIME-Version: 1.0', + 'X-Loop: Postfix Admin Virtual Vacation', + 'Auto-Submitted: auto-replied', + ]; + return implode("\r\n", $headers) . "\r\n\r\n" . $this->normalizeBody($body) . "\r\n"; + } + + /** @param array $vacation @param array $configuration */ + private function replaceDates(string $body, array $vacation, array $configuration): string + { + $format = (string)($configuration['reply_date_format'] ?? 'Y-m-d'); + $from = new DateTimeImmutable((string)$vacation['activefrom']); + $until = new DateTimeImmutable((string)$vacation['activeuntil']); + return str_ireplace( + [ + (string)($configuration['reply_replace_from'] ?? '<%From_Date>'), + (string)($configuration['reply_replace_until'] ?? '<%Until_Date>'), + ], + [$from->format($format), $until->format($format)], + $body, + ); + } + + private function decodeHeader(string $value): string + { + $decoded = mb_decode_mimeheader($value); + return $decoded !== '' ? $decoded : $value; + } + + private function encodeHeader(string $value): string + { + $value = str_replace(["\r", "\n"], '', $value); + return mb_encode_mimeheader($value, 'UTF-8', 'Q', "\r\n"); + } + + private function normalizeBody(string $body): string + { + return str_replace("\n", "\r\n", str_replace(["\r\n", "\r"], "\n", $body)); + } +} + +final class VacationCli +{ + public const VERSION = '1.0.0'; + + /** @var list */ + private const TABLE_KEYS = ['vacation', 'vacation_notification', 'alias', 'alias_domain', 'mailbox']; + + /** @var resource|null */ + private $input; + + /** @var resource|null */ + private $output; + + /** @var resource|null */ + private $errorOutput; + + /** + * @param resource|null $input + * @param resource|null $output + * @param resource|null $errorOutput + */ + public function __construct($input = null, $output = null, $errorOutput = null) + { + $this->input = $input ?? STDIN; + $this->output = $output ?? STDOUT; + $this->errorOutput = $errorOutput ?? STDERR; + } + + /** @param list $argv */ + public function run(array $argv): int + { + try { + $arguments = $this->parseArguments($argv); + + if ($arguments['version']) { + $this->write(self::VERSION . PHP_EOL); + return 0; + } + if ($arguments['init_config']) { + return $this->initConfig($arguments); + } + if ($arguments['check_dependencies']) { + return $this->runCheck($arguments, true); + } + if ($arguments['test']) { + return $this->runTest($arguments); + } + if ($arguments['inspect_message'] !== null) { + return $this->runInspectMessage($arguments); + } + if ($arguments['show_config_path']) { + return $this->showConfigPath($arguments); + } + if ($arguments['check']) { + return $this->runCheck($arguments, false); + } + + if ($arguments['envelope_sender'] !== null || $arguments['recipient'] !== null) { + return $this->runTransport($arguments); + } + + $this->writeError( + 'Use -f sender -- recipient for Vacation transport, or select --init-config, --check, --test, ' + . '--inspect-message, or --show-config-path.' . PHP_EOL + ); + return 69; + } catch (VacationCliException $exception) { + $this->writeError('ERROR: ' . $exception->getMessage() . PHP_EOL); + return $exception->exitStatus; + } catch (Throwable $exception) { + $this->writeError('ERROR: ' . $exception->getMessage() . PHP_EOL); + return 75; + } + } + + /** + * @param list $argv + * @return array + */ + public function parseArguments(array $argv): array + { + $arguments = [ + 'check' => false, + 'check_dependencies' => false, + 'init_config' => false, + 'test' => false, + 'show_config_path' => false, + 'inspect_message' => null, + 'version' => false, + 'config' => null, + 'import_legacy' => null, + 'postfixadmin_root' => null, + 'non_interactive' => false, + 'force' => false, + 'envelope_sender' => null, + 'recipient' => null, + 'transport_test' => false, + ]; + $actions = [ + '--check' => 'check', + '--check-dependencies' => 'check_dependencies', + '--init-config' => 'init_config', + '--test' => 'test', + '--show-config-path' => 'show_config_path', + ]; + $values = [ + '--config' => 'config', + '--import-legacy' => 'import_legacy', + '--postfixadmin-root' => 'postfixadmin_root', + '-f' => 'envelope_sender', + '-t' => 'transport_test', + ]; + $selectedActions = 0; + $positional = false; + + for ($index = 1, $count = count($argv); $index < $count; ++$index) { + $argument = $argv[$index]; + if ($argument === '--') { + $positional = true; + continue; + } + if (!$positional && isset($actions[$argument])) { + $arguments[$actions[$argument]] = true; + ++$selectedActions; + continue; + } + if (!$positional && $argument === '--inspect-message') { + if (!isset($argv[$index + 1])) { + throw new UsageException('Missing value for --inspect-message'); + } + $arguments['inspect_message'] = $argv[++$index]; + ++$selectedActions; + continue; + } + if (!$positional && str_starts_with($argument, '--inspect-message=')) { + $arguments['inspect_message'] = substr($argument, strlen('--inspect-message=')); + ++$selectedActions; + continue; + } + if (!$positional && $argument === '--non-interactive') { + $arguments['non_interactive'] = true; + continue; + } + if (!$positional && $argument === '--force') { + $arguments['force'] = true; + continue; + } + if (!$positional && $argument === '--version') { + $arguments['version'] = true; + continue; + } + if (!$positional && isset($values[$argument])) { + if (!isset($argv[$index + 1])) { + throw new UsageException("Missing value for {$argument}"); + } + $arguments[$values[$argument]] = $argv[++$index]; + continue; + } + if (!$positional && str_starts_with($argument, '--') && str_contains($argument, '=')) { + [$name, $value] = explode('=', $argument, 2); + if (isset($values[$name])) { + $arguments[$values[$name]] = $value; + continue; + } + } + if (!$positional && str_starts_with($argument, '-')) { + throw new UsageException("Unknown option: {$argument}"); + } + if ($arguments['recipient'] !== null) { + throw new UsageException("Unexpected argument: {$argument}"); + } + $arguments['recipient'] = $argument; + } + + if ($selectedActions > 1) { + throw new UsageException('Select only one action'); + } + return $arguments; + } + + /** @return list */ + public function defaultConfigPaths(): array + { + return [ + '/etc/mail/postfixadmin/vacation.ini', + '/etc/postfixadmin/vacation.ini', + getcwd() . DIRECTORY_SEPARATOR . 'vacation.ini', + ]; + } + + public function findVacationConfig(?string $explicit): ?string + { + $candidates = $explicit !== null ? [$explicit] : $this->defaultConfigPaths(); + foreach ($candidates as $candidate) { + $expanded = $this->expandHome($candidate); + if (is_file($expanded)) { + return realpath($expanded) ?: $expanded; + } + } + return null; + } + + /** @return list */ + public function discoverPostfixAdminRoots(?string $explicit = null): array + { + $candidates = []; + $environment = getenv('POSTFIXADMIN_ROOT'); + if ($explicit !== null && $explicit !== '') { + $candidates[] = $explicit; + } elseif (is_string($environment) && $environment !== '') { + $candidates[] = $environment; + } else { + foreach ([getcwd(), __DIR__] as $base) { + if ($base === false) { + continue; + } + do { + $candidates[] = $base; + $parent = dirname($base); + if ($parent === $base) { + break; + } + $base = $parent; + } while (true); + } + array_push( + $candidates, + '/var/www/html/postfixadmin', + '/var/www/postfixadmin', + '/usr/share/postfixadmin', + '/opt/postfixadmin', + ); + } + + $found = []; + foreach ($candidates as $candidate) { + $candidate = $this->expandHome($candidate); + $resolved = realpath($candidate); + if ($resolved === false) { + continue; + } + $key = strtolower(str_replace('\\', '/', $resolved)); + if (isset($found[$key])) { + continue; + } + if (is_file($resolved . '/config.inc.php') && is_file($resolved . '/config.local.php')) { + $found[$key] = $resolved; + } + } + return array_values($found); + } + + /** @param list $roots */ + public function choosePostfixAdminRoot(array $roots, bool $nonInteractive): string + { + if ($roots === []) { + throw new ConfigurationException('No PostfixAdmin installation with config.local.php was found'); + } + if (count($roots) === 1) { + return $roots[0]; + } + if ($nonInteractive) { + throw new ConfigurationException( + 'Multiple PostfixAdmin installations found: ' . implode(', ', $roots) + . '; use --postfixadmin-root' + ); + } + + $this->write('PostfixAdmin installations found:' . PHP_EOL); + foreach ($roots as $index => $root) { + $this->write(sprintf(' %d. %s%s', $index + 1, $root, PHP_EOL)); + } + do { + $selected = $this->prompt('Select installation', '1'); + if (ctype_digit($selected) && (int)$selected >= 1 && (int)$selected <= count($roots)) { + return $roots[(int)$selected - 1]; + } + $this->write('Invalid selection.' . PHP_EOL); + } while (true); + } + + /** @return array */ + public function loadPostfixAdminConfig(string $root): array + { + $configFile = $root . DIRECTORY_SEPARATOR . 'config.inc.php'; + $localFile = $root . DIRECTORY_SEPARATOR . 'config.local.php'; + if (!is_file($configFile) || !is_file($localFile)) { + throw new ConfigurationException("Invalid PostfixAdmin root: {$root}"); + } + + $previous = $GLOBALS['CONF'] ?? null; + $hadPrevious = array_key_exists('CONF', $GLOBALS); + try { + $configuration = $this->includePostfixAdminConfig($configFile); + } finally { + if ($hadPrevious) { + $GLOBALS['CONF'] = $previous; + } else { + unset($GLOBALS['CONF']); + } + } + if (!is_array($configuration)) { + throw new ConfigurationException('PostfixAdmin configuration did not produce $CONF'); + } + + $prefix = (string)($configuration['database_prefix'] ?? ''); + $mapping = is_array($configuration['database_tables'] ?? null) + ? $configuration['database_tables'] + : []; + $configuration['resolved_tables'] = []; + foreach (self::TABLE_KEYS as $key) { + $configuration['resolved_tables'][$key] = $prefix . (string)($mapping[$key] ?? $key); + } + return $configuration; + } + + private function includePostfixAdminConfig(string $configFile): mixed + { + global $CONF; + + $CONF = []; + require $configFile; + return $CONF; + } + + /** @return array{values: array, warnings: list} */ + public function loadVacationConfig(string $path): array + { + $parsed = parse_ini_file($path, true, INI_SCANNER_RAW); + if ($parsed === false) { + throw new ConfigurationException("Invalid INI configuration: {$path}"); + } + $warnings = []; + $values = [ + 'message_no_vacation_pattern' => VacationMessageInspector::DEFAULT_NO_VACATION_PATTERN, + ]; + if (isset($parsed['postfixadmin']) && is_array($parsed['postfixadmin'])) { + $values['postfixadmin_root'] = trim((string)($parsed['postfixadmin']['root'] ?? '')); + } else { + $warnings[] = 'missing [postfixadmin] section'; + } + if (isset($parsed['database']) && is_array($parsed['database'])) { + $mapping = [ + 'type' => 'database_type', + 'host' => 'database_host', + 'port' => 'database_port', + 'socket' => 'database_socket', + 'name' => 'database_name', + 'user' => 'database_user', + 'password' => 'database_password', + ]; + foreach ($mapping as $iniKey => $internalKey) { + if (array_key_exists($iniKey, $parsed['database'])) { + $values[$internalKey] = (string)$parsed['database'][$iniKey]; + } + } + } + if (isset($parsed['vacation']) + && is_array($parsed['vacation']) + && array_key_exists('domain', $parsed['vacation']) + ) { + $values['vacation_domain'] = (string)$parsed['vacation']['domain']; + } + if (isset($parsed['message']) && is_array($parsed['message'])) { + $mapping = [ + 'recipient_delimiter' => 'recipient_delimiter', + 'no_vacation_pattern' => 'message_no_vacation_pattern', + 'noreply_pattern' => 'message_noreply_pattern', + 'default_noreply_pattern' => 'message_default_noreply_pattern', + ]; + foreach ($mapping as $iniKey => $internalKey) { + if (array_key_exists($iniKey, $parsed['message'])) { + $values[$internalKey] = (string)$parsed['message'][$iniKey]; + } + } + if (array_key_exists('custom_noreply_pattern', $parsed['message'])) { + $values['message_custom_noreply_pattern'] = filter_var( + $parsed['message']['custom_noreply_pattern'], + FILTER_VALIDATE_BOOL, + FILTER_NULL_ON_FAILURE, + ) ?? false; + } + } + if (isset($parsed['smtp']) && is_array($parsed['smtp'])) { + $values['smtp_server'] = trim((string)($parsed['smtp']['server'] ?? 'localhost')); + $values['smtp_server_port'] = $this->validPort($parsed['smtp']['port'] ?? 25); + $values['smtp_helo'] = trim((string)($parsed['smtp']['helo'] ?? '')); + $values['smtp_security'] = $this->normalizeSmtpSecurity($parsed['smtp']['security'] ?? 'none'); + $values['smtp_timeout'] = $this->validTimeout($parsed['smtp']['timeout'] ?? 120); + $values['smtp_local_address'] = trim((string)($parsed['smtp']['local_address'] ?? '')); + $values['smtp_username'] = (string)($parsed['smtp']['username'] ?? ''); + $values['smtp_password'] = (string)($parsed['smtp']['password'] ?? ''); + $values['sendmail_path'] = trim((string)($parsed['smtp']['sendmail'] ?? '')); + $environmentPassword = getenv('VACATION_SMTP_PASSWORD'); + if (is_string($environmentPassword) && $environmentPassword !== '') { + $values['smtp_password'] = $environmentPassword; + } + } else { + $warnings[] = 'missing [smtp] section; localhost:25 defaults will be used'; + $values['smtp_server'] = 'localhost'; + $values['smtp_server_port'] = 25; + $values['smtp_helo'] = ''; + $values['smtp_security'] = 'none'; + $values['smtp_timeout'] = 120; + $values['smtp_local_address'] = ''; + $values['smtp_username'] = ''; + $values['smtp_password'] = ''; + $values['sendmail_path'] = ''; + } + if (isset($parsed['reply']) && is_array($parsed['reply'])) { + $values['reply_friendly_from'] = (string)($parsed['reply']['friendly_from'] ?? ''); + $values['reply_account_name'] = filter_var( + $parsed['reply']['account_name'] ?? false, + FILTER_VALIDATE_BOOL, + FILTER_NULL_ON_FAILURE, + ) ?? false; + $values['reply_replace_from'] = (string)($parsed['reply']['replace_from'] ?? '<%From_Date>'); + $values['reply_replace_until'] = (string)($parsed['reply']['replace_until'] ?? '<%Until_Date>'); + $values['reply_date_format'] = (string)($parsed['reply']['date_format'] ?? 'Y-m-d'); + } + if (isset($parsed['logging']) && is_array($parsed['logging'])) { + $values['log_syslog'] = filter_var( + $parsed['logging']['syslog'] ?? true, + FILTER_VALIDATE_BOOL, + FILTER_NULL_ON_FAILURE, + ) ?? true; + $values['log_level'] = $this->normalizeLogLevel($parsed['logging']['level'] ?? 'info'); + $values['log_file'] = trim((string)($parsed['logging']['file'] ?? '')); + $values['log_file_enabled'] = filter_var( + $parsed['logging']['file_enabled'] ?? false, + FILTER_VALIDATE_BOOL, + FILTER_NULL_ON_FAILURE, + ) ?? false; + } else { + $values['log_syslog'] = true; + $values['log_level'] = 'info'; + $values['log_file'] = ''; + $values['log_file_enabled'] = false; + } + return ['values' => $values, 'warnings' => $warnings]; + } + + /** @return array{values: array, warnings: list} */ + public function loadLegacyConfig(string $path): array + { + $contents = file($path, FILE_IGNORE_NEW_LINES); + if ($contents === false) { + throw new RuntimeException("Could not read legacy configuration: {$path}"); + } + $values = []; + $warnings = []; + foreach ($contents as $index => $line) { + if (!preg_match('/^\s*\$([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*?)\s*;\s*(?:#.*)?$/', $line, $matches)) { + continue; + } + try { + $values[$matches[1]] = $this->parsePerlValue($matches[2]); + } catch (RuntimeException $exception) { + $warnings[] = 'line ' . ($index + 1) . ': ' . $exception->getMessage(); + } + } + return ['values' => $values, 'warnings' => $warnings]; + } + + public function isLegacyConfig(string $path): bool + { + if (!is_file($path)) { + return false; + } + $handle = fopen($path, 'rb'); + if ($handle === false) { + return false; + } + try { + while (($line = fgets($handle)) !== false) { + if (preg_match('/^\s*\$[A-Za-z_][A-Za-z0-9_]*\s*=.*;/', $line)) { + return true; + } + } + } finally { + fclose($handle); + } + return false; + } + + /** + * @param array $smtpOptions + * @param array $messageOptions + * @param array $replyOptions + * @param array $loggingOptions + */ + public function renderConfig( + string $root, + string $server, + int $port, + string $helo, + array $smtpOptions = [], + array $messageOptions = [], + array $replyOptions = [], + array $loggingOptions = [], + ): string { + $lines = [ + '# PostfixAdmin Virtual Vacation PHP configuration', + '# Database settings and vacation_domain are inherited from PostfixAdmin.', + '', + '[postfixadmin]', + 'root = ' . $this->iniValue(str_replace('\\', '/', $root)), + '', + '[smtp]', + 'server = ' . $this->iniValue($server), + "port = {$port}", + 'helo = ' . $this->iniValue($helo), + ]; + $optional = [ + 'security' => $this->normalizeSmtpSecurity($smtpOptions['security'] ?? 'none'), + 'timeout' => $this->validTimeout($smtpOptions['timeout'] ?? 120), + 'local_address' => trim((string)($smtpOptions['local_address'] ?? '')), + 'username' => (string)($smtpOptions['username'] ?? ''), + 'password' => (string)($smtpOptions['password'] ?? ''), + 'sendmail' => trim((string)($smtpOptions['sendmail'] ?? '')), + ]; + if ($optional['security'] !== 'none') { + $lines[] = 'security = ' . $this->iniValue($optional['security']); + } + if ($optional['timeout'] !== 120) { + $lines[] = 'timeout = ' . $optional['timeout']; + } + foreach (['local_address', 'username', 'password', 'sendmail'] as $key) { + if ($optional[$key] !== '') { + $lines[] = $key . ' = ' . $this->iniValue((string)$optional[$key]); + } + } + $messageValues = [ + 'recipient_delimiter' => (string)($messageOptions['recipient_delimiter'] ?? ''), + 'no_vacation_pattern' => (string)($messageOptions['no_vacation_pattern'] ?? ''), + 'noreply_pattern' => (string)($messageOptions['noreply_pattern'] ?? ''), + 'default_noreply_pattern' => (string)($messageOptions['default_noreply_pattern'] ?? ''), + ]; + $customNoReply = filter_var( + $messageOptions['custom_noreply_pattern'] ?? false, + FILTER_VALIDATE_BOOL, + ); + if (array_filter($messageValues, static fn ($value) => $value !== '') !== [] || $customNoReply) { + array_push($lines, '', '[message]'); + foreach ($messageValues as $key => $value) { + if ($value !== '') { + $lines[] = $key . ' = ' . $this->iniValue($value); + } + } + if ($customNoReply) { + $lines[] = 'custom_noreply_pattern = true'; + } + } + $replyValues = [ + 'friendly_from' => (string)($replyOptions['friendly_from'] ?? ''), + 'replace_from' => (string)($replyOptions['replace_from'] ?? ''), + 'replace_until' => (string)($replyOptions['replace_until'] ?? ''), + 'date_format' => (string)($replyOptions['date_format'] ?? ''), + ]; + $accountName = filter_var($replyOptions['account_name'] ?? false, FILTER_VALIDATE_BOOL); + if (array_filter($replyValues, static fn ($value) => $value !== '') !== [] || $accountName) { + array_push($lines, '', '[reply]'); + foreach ($replyValues as $key => $value) { + if ($value !== '') { + $lines[] = $key . ' = ' . $this->iniValue($value); + } + } + if ($accountName) { + $lines[] = 'account_name = true'; + } + } + $logSyslog = filter_var($loggingOptions['syslog'] ?? true, FILTER_VALIDATE_BOOL); + $logFileEnabled = filter_var($loggingOptions['file_enabled'] ?? false, FILTER_VALIDATE_BOOL); + array_push($lines, '', '[logging]'); + $lines[] = 'syslog = ' . ($logSyslog ? 'true' : 'false'); + $lines[] = 'level = ' . $this->iniValue($this->normalizeLogLevel($loggingOptions['level'] ?? 'info')); + if ($logFileEnabled) { + $lines[] = 'file_enabled = true'; + $lines[] = 'file = ' . $this->iniValue((string)($loggingOptions['file'] ?? '/var/log/vacation.log')); + } + $lines[] = ''; + return implode(PHP_EOL, $lines); + } + + /** @param array $arguments */ + private function initConfig(array $arguments): int + { + $roots = $this->discoverPostfixAdminRoots($this->nullableString($arguments['postfixadmin_root'])); + $root = $this->choosePostfixAdminRoot($roots, (bool)$arguments['non_interactive']); + $source = $this->loadPostfixAdminConfig($root); + + $this->write("PostfixAdmin configuration: {$root}/config.local.php" . PHP_EOL); + $this->write('Database: ' . $this->normalizeDatabaseType($source['database_type'] ?? '') + . ' / ' . ($source['database_name'] ?? '') . PHP_EOL); + $this->write('Database host: ' . (($source['database_host'] ?? '') ?: 'local socket/default') . PHP_EOL); + $this->write('Database user: ' . ($source['database_user'] ?? '') . PHP_EOL); + $this->write('Database password: ' + . (($source['database_password'] ?? '') !== '' ? 'configured (hidden)' : 'empty') . PHP_EOL); + $this->write('Vacation domain: ' . ($source['vacation_domain'] ?? '') . PHP_EOL); + + $legacy = []; + $legacyPath = $this->nullableString($arguments['import_legacy']); + if ($legacyPath !== null) { + $legacyPath = $this->expandHome($legacyPath); + if (!is_file($legacyPath)) { + throw new RuntimeException("Legacy configuration not found: {$legacyPath}"); + } + $loaded = $this->loadLegacyConfig($legacyPath); + $legacy = $loaded['values']; + $this->write("Legacy configuration: {$legacyPath}" . PHP_EOL); + foreach ($loaded['warnings'] as $warning) { + $this->writeError("WARNING: {$warning}" . PHP_EOL); + } + } + + $nonInteractive = (bool)$arguments['non_interactive']; + $server = $this->promptValue('SMTP server', (string)($legacy['smtp_server'] ?? 'localhost'), $nonInteractive); + $port = $this->validPort( + $this->promptValue('SMTP port', (string)($legacy['smtp_server_port'] ?? 25), $nonInteractive) + ); + $defaultHelo = (string)($legacy['smtp_helo'] ?? $this->detectedFqdn() ?? 'localhost.localdomain'); + $helo = $this->promptValue('SMTP HELO', $defaultHelo, $nonInteractive); + $smtpOptions = [ + 'security' => $legacy['smtp_ssl'] ?? 'none', + 'timeout' => $legacy['smtp_timeout'] ?? 120, + 'local_address' => $legacy['smtp_client'] ?? '', + 'username' => $legacy['smtp_authid'] ?? '', + 'password' => $legacy['smtp_authpwd'] ?? '', + 'sendmail' => $legacy['sendmail_bin'] ?? '', + ]; + $messageOptions = [ + 'recipient_delimiter' => $legacy['recipient_delimiter'] ?? '', + 'no_vacation_pattern' => $legacy['no_vacation_pattern'] ?? '', + 'custom_noreply_pattern' => $legacy['custom_noreply_pattern'] ?? false, + 'noreply_pattern' => $legacy['noreply_pattern'] ?? '', + 'default_noreply_pattern' => $legacy['default_noreply_pattern'] ?? '', + ]; + $replyOptions = [ + 'friendly_from' => $legacy['friendly_from'] ?? '', + 'account_name' => $legacy['accountname_check'] ?? false, + 'replace_from' => $legacy['replace_from'] ?? '', + 'replace_until' => $legacy['replace_until'] ?? '', + 'date_format' => isset($legacy['date_format']) + ? $this->perlDateFormatToPhp((string)$legacy['date_format']) + : '', + ]; + $legacyLogLevel = (int)($legacy['log_level'] ?? 1); + $loggingOptions = [ + 'syslog' => $legacy['syslog'] ?? true, + 'level' => $legacyLogLevel >= 2 ? 'debug' : ($legacyLogLevel === 1 ? 'info' : 'error'), + 'file_enabled' => $legacy['log_to_file'] ?? false, + 'file' => $legacy['logfile'] ?? '/var/log/vacation.log', + ]; + $destination = $this->expandHome( + $this->nullableString($arguments['config']) ?? '/etc/postfixadmin/vacation.ini' + ); + if ($legacyPath !== null && $this->samePath($destination, $legacyPath)) { + throw new RuntimeException('The PHP configuration destination cannot be the legacy Perl configuration'); + } + if (is_file($destination) && $this->isLegacyConfig($destination)) { + throw new RuntimeException( + 'Refusing to overwrite a Perl vacation.conf; use a separate vacation.ini path' + ); + } + if (file_exists($destination) && !(bool)$arguments['force']) { + throw new RuntimeException("Configuration already exists: {$destination}; use --force to replace it"); + } + $directory = dirname($destination); + if (!is_dir($directory) && !mkdir($directory, 0750, true) && !is_dir($directory)) { + throw new RuntimeException("Could not create configuration directory: {$directory}"); + } + $this->writeConfigurationFile( + $destination, + $this->renderConfig( + $root, + $server, + $port, + $helo, + $smtpOptions, + $messageOptions, + $replyOptions, + $loggingOptions, + ), + ); + $this->write("Created: {$destination}" . PHP_EOL); + $this->write('Review the file owner and group for the service user configured in Postfix master.cf.' . PHP_EOL); + $this->write("Next: run vacation.php --check --config {$destination}" . PHP_EOL); + return 0; + } + + private function writeConfigurationFile(string $destination, string $contents): void + { + $temporary = tempnam(dirname($destination), '.vacation.ini.'); + if ($temporary === false) { + throw new RuntimeException("Could not create a temporary configuration file for {$destination}"); + } + try { + if (!chmod($temporary, 0600)) { + throw new RuntimeException("Could not protect temporary configuration file: {$temporary}"); + } + if (file_put_contents($temporary, $contents, LOCK_EX) === false) { + throw new RuntimeException("Could not write configuration: {$destination}"); + } + if (!chmod($temporary, 0640)) { + throw new RuntimeException("Could not set mode 0640 on configuration: {$destination}"); + } + clearstatcache(true, $temporary); + if (PHP_OS_FAMILY !== 'Windows') { + $mode = fileperms($temporary); + if ($mode === false || ($mode & 0777) !== 0640) { + throw new RuntimeException("Configuration mode is not 0640: {$destination}"); + } + } + if (!rename($temporary, $destination)) { + throw new RuntimeException("Could not install configuration atomically: {$destination}"); + } + $temporary = ''; + } finally { + if ($temporary !== '' && is_file($temporary)) { + unlink($temporary); + } + } + } + + /** + * @param array $configuration + * @param list $results + */ + public function checkDependencies(array $configuration, array &$results): bool + { + $ok = true; + $runtimeVersion = phpversion(); + if (version_compare($runtimeVersion, '8.2.0', '<')) { + $detail = $runtimeVersion . '; PHP 8.2 or newer is required'; + $results[] = new CheckResult('PHP', 'Version', 'FAILED', $detail); + $ok = false; + } else { + $results[] = new CheckResult('PHP', 'Version', 'OK', $runtimeVersion); + } + foreach (['mbstring' => 'required by mailparse', 'mailparse' => 'MIME message parser'] as $extension => $reason) { + if (extension_loaded($extension)) { + $results[] = new CheckResult('Dependencies', $extension, 'OK', $reason); + } else { + $results[] = new CheckResult('Dependencies', $extension, 'MISSING', $reason); + $ok = false; + } + } + if (trim((string)($configuration['sendmail_path'] ?? '')) !== '') { + if (function_exists('proc_open')) { + $results[] = new CheckResult('Dependencies', 'proc_open', 'OK', 'sendmail transport'); + } else { + $results[] = new CheckResult('Dependencies', 'proc_open', 'MISSING', 'sendmail transport'); + $ok = false; + } + } elseif (trim((string)($configuration['smtp_server'] ?? 'localhost')) === '') { + if (function_exists('dns_get_record')) { + $results[] = new CheckResult('Dependencies', 'dns_get_record', 'OK', 'dynamic MX delivery'); + } else { + $results[] = new CheckResult('Dependencies', 'dns_get_record', 'MISSING', 'dynamic MX delivery'); + $ok = false; + } + } + if (trim((string)($configuration['sendmail_path'] ?? '')) === '' + && ($configuration['smtp_security'] ?? 'none') !== 'none' + ) { + if (extension_loaded('openssl')) { + $results[] = new CheckResult('Dependencies', 'openssl', 'OK', 'SMTP TLS support'); + } else { + $results[] = new CheckResult('Dependencies', 'openssl', 'MISSING', 'SMTP TLS support'); + $ok = false; + } + } + if (!extension_loaded('pdo')) { + $results[] = new CheckResult('Dependencies', 'PDO', 'MISSING', 'database abstraction'); + return false; + } + $results[] = new CheckResult('Dependencies', 'PDO', 'OK', 'database abstraction'); + + $type = $this->normalizeDatabaseType($configuration['database_type'] ?? ''); + if ($type === '') { + $results[] = new CheckResult( + 'Dependencies', + 'Database driver', + 'NOT TESTED', + 'database type is unavailable because PostfixAdmin configuration was not loaded', + ); + return false; + } + $driver = ['mysql' => 'mysql', 'postgresql' => 'pgsql', 'sqlite' => 'sqlite'][$type] ?? null; + if ($driver === null) { + $results[] = new CheckResult('Dependencies', 'PDO driver', 'MISSING', "unsupported database type: {$type}"); + return false; + } + if (in_array($driver, PDO::getAvailableDrivers(), true)) { + $results[] = new CheckResult('Dependencies', "pdo_{$driver}", 'OK', "{$type} database driver"); + } else { + $results[] = new CheckResult('Dependencies', "pdo_{$driver}", 'MISSING', "{$type} database driver"); + $ok = false; + } + return $ok; + } + + /** @param array $configuration */ + public function connectDatabase(array $configuration): PDO + { + $type = $this->normalizeDatabaseType($configuration['database_type'] ?? ''); + $host = (string)($configuration['database_host'] ?? ''); + $port = (string)($configuration['database_port'] ?? ''); + $socket = (string)($configuration['database_socket'] ?? ''); + $name = (string)($configuration['database_name'] ?? ''); + $user = (string)($configuration['database_user'] ?? ''); + $password = (string)($configuration['database_password'] ?? ''); + $options = [ + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_TIMEOUT => 5, + ]; + + if ($type === 'mysql') { + $dsn = $socket !== '' ? "mysql:unix_socket={$socket}" : 'mysql:host=' . ($host ?: 'localhost'); + if ($port !== '') { + $dsn .= ";port={$port}"; + } + $dsn .= ";dbname={$name};charset=utf8mb4"; + if (filter_var($configuration['database_use_ssl'] ?? false, FILTER_VALIDATE_BOOL)) { + $options[PDO::MYSQL_ATTR_SSL_KEY] = (string)($configuration['database_ssl_key'] ?? ''); + $options[PDO::MYSQL_ATTR_SSL_CA] = (string)($configuration['database_ssl_ca'] ?? ''); + $options[PDO::MYSQL_ATTR_SSL_CAPATH] = (string)($configuration['database_ssl_ca_path'] ?? ''); + $options[PDO::MYSQL_ATTR_SSL_CERT] = (string)($configuration['database_ssl_cert'] ?? ''); + $options[PDO::MYSQL_ATTR_SSL_CIPHER] = (string)($configuration['database_ssl_cipher'] ?? ''); + $options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = filter_var( + $configuration['database_ssl_verify_server_cert'] ?? true, + FILTER_VALIDATE_BOOL, + ); + } + } elseif ($type === 'postgresql') { + $dsn = "pgsql:dbname={$name}"; + if ($host !== '') { + $dsn .= ";host={$host}"; + } + if ($port !== '') { + $dsn .= ";port={$port}"; + } + } elseif ($type === 'sqlite') { + $dsn = "sqlite:{$name}"; + $user = ''; + $password = ''; + } else { + throw new ConfigurationException('Unsupported database type: ' . ($type ?: '(empty)')); + } + + return new PDO($dsn, $user, $password, $options); + } + + /** @param array $configuration */ + private function databaseDependenciesAvailable(array $configuration): bool + { + if (!extension_loaded('pdo')) { + return false; + } + $type = $this->normalizeDatabaseType($configuration['database_type'] ?? ''); + $driver = ['mysql' => 'mysql', 'postgresql' => 'pgsql', 'sqlite' => 'sqlite'][$type] ?? null; + return $driver !== null && in_array($driver, PDO::getAvailableDrivers(), true); + } + + /** + * @param array $configuration + * @param list $results + */ + private function checkDatabase(array $configuration, bool $dependenciesOk, array &$results): void + { + if ($this->normalizeDatabaseType($configuration['database_type'] ?? '') === '') { + $results[] = new CheckResult( + 'Database', + 'Connection', + 'NOT TESTED', + 'PostfixAdmin database configuration was not loaded', + ); + return; + } + if (!$dependenciesOk) { + $results[] = new CheckResult('Database', 'Connection', 'NOT TESTED', 'the required PDO driver is missing'); + return; + } + try { + $database = $this->connectDatabase($configuration); + $results[] = new CheckResult('Database', 'Connection', 'OK'); + $tables = is_array($configuration['resolved_tables'] ?? null) + ? $configuration['resolved_tables'] + : array_combine(self::TABLE_KEYS, self::TABLE_KEYS); + foreach (self::TABLE_KEYS as $key) { + $table = (string)($tables[$key] ?? $key); + if (!preg_match('/^[A-Za-z_][A-Za-z0-9_$]*$/', $table)) { + $results[] = new CheckResult('Database', "Table {$key}", 'FAILED', "unsafe identifier: {$table}"); + continue; + } + $quoted = $this->quoteIdentifier($table, $this->normalizeDatabaseType($configuration['database_type'] ?? '')); + try { + $database->query("SELECT 1 FROM {$quoted} LIMIT 1"); + $results[] = new CheckResult('Database', "Table {$key}", 'OK', $table); + } catch (Throwable $exception) { + $results[] = new CheckResult('Database', "Table {$key}", 'FAILED', $exception->getMessage()); + } + } + } catch (Throwable $exception) { + $results[] = new CheckResult('Database', 'Connection', 'FAILED', $exception->getMessage()); + } + } + + /** @param list $results */ + private function checkSmtp(array $configuration, array &$results): void + { + $sendmail = trim((string)($configuration['sendmail_path'] ?? '')); + if ($sendmail !== '') { + $results[] = str_starts_with($sendmail, '/') && is_executable($sendmail) + ? new CheckResult('Delivery', 'sendmail', 'OK', $sendmail) + : new CheckResult('Delivery', 'sendmail', 'FAILED', 'an absolute executable path is required'); + return; + } + $server = (string)($configuration['smtp_server'] ?? 'localhost'); + if ($server === '') { + $results[] = new CheckResult('Delivery', 'SMTP', 'OK', 'MX lookup selected at delivery time'); + return; + } + $port = $this->validPort($configuration['smtp_server_port'] ?? 25); + $security = $this->normalizeSmtpSecurity($configuration['smtp_security'] ?? 'none'); + $helo = trim((string)($configuration['smtp_helo'] ?? '')) ?: $this->detectedFqdn(); + if ($helo === null || !$this->validHelo($helo)) { + $results[] = new CheckResult('Delivery', 'SMTP', 'FAILED', 'a valid configured or detected HELO is required'); + return; + } + try { + $socket = $this->smtpConnect($configuration, $helo); + try { + [$code, $response] = $this->smtpCommand($socket, 'NOOP'); + } finally { + $this->smtpQuit($socket); + } + if ($code >= 200 && $code < 400) { + $results[] = new CheckResult('Delivery', 'SMTP', 'OK', "{$server}:{$port} ({$security})"); + return; + } + $results[] = new CheckResult('Delivery', 'SMTP', 'FAILED', "NOOP returned {$code}: {$response}"); + } catch (Throwable $exception) { + $results[] = new CheckResult('Delivery', 'SMTP', 'FAILED', "{$server}:{$port}: {$exception->getMessage()}"); + } + } + + /** + * @param array $configuration + * @param list $results + */ + private function checkLogging(array $configuration, array &$results): void + { + if (empty($configuration['log_file_enabled'])) { + return; + } + $path = trim((string)($configuration['log_file'] ?? '')); + $writable = $path !== '' && (is_file($path) ? is_writable($path) : is_writable(dirname($path))); + $results[] = $writable + ? new CheckResult('Logging', 'File', 'OK', $path) + : new CheckResult('Logging', 'File', 'FAILED', $path !== '' ? $path : 'logging.file is empty'); + } + + /** @param array $arguments */ + private function runCheck(array $arguments, bool $dependenciesOnly): int + { + $results = []; + $vacationPath = $this->findVacationConfig($this->nullableString($arguments['config'])); + $configuration = []; + if ($vacationPath !== null) { + $loaded = $this->loadVacationConfig($vacationPath); + $configuration = $loaded['values']; + $results[] = new CheckResult('Configuration', 'Vacation config', 'OK', $vacationPath); + foreach ($loaded['warnings'] as $warning) { + $results[] = new CheckResult('Configuration', 'Parse warning', 'WARNING', $warning); + } + } elseif (!$dependenciesOnly) { + $results[] = new CheckResult('Configuration', 'Vacation config', 'MISSING', 'use --config or run --init-config'); + } + + $rootArgument = $this->nullableString($arguments['postfixadmin_root']) + ?? ($configuration['postfixadmin_root'] ?? null); + $roots = $this->discoverPostfixAdminRoots(is_string($rootArgument) ? $rootArgument : null); + if ($roots !== []) { + try { + $root = $this->choosePostfixAdminRoot($roots, true); + $postfixAdmin = $this->loadPostfixAdminConfig($root); + $results[] = new CheckResult('Configuration', 'PostfixAdmin config', 'OK', "{$root}/config.local.php"); + $configuration = array_replace($postfixAdmin, $configuration); + } catch (Throwable $exception) { + $results[] = new CheckResult('Configuration', 'PostfixAdmin config', 'FAILED', $exception->getMessage()); + } + } elseif ($rootArgument !== null) { + $results[] = new CheckResult('Configuration', 'PostfixAdmin config', 'MISSING', (string)$rootArgument); + } else { + $results[] = new CheckResult( + 'Configuration', + 'PostfixAdmin config', + 'MISSING', + 'no installation found; use --postfixadmin-root /path/to/postfixadmin', + ); + } + + if (!$dependenciesOnly) { + $vacationDomain = trim((string)($configuration['vacation_domain'] ?? '')); + $results[] = $vacationDomain !== '' + ? new CheckResult('Configuration', 'Vacation domain', 'OK', $vacationDomain) + : new CheckResult( + 'Configuration', + 'Vacation domain', + 'MISSING', + 'set vacation_domain in PostfixAdmin or [vacation] domain in vacation.ini', + ); + } + + $this->checkDependencies($configuration, $results); + if (!$dependenciesOnly && $vacationPath !== null) { + $this->checkDatabase($configuration, $this->databaseDependenciesAvailable($configuration), $results); + $this->checkSmtp($configuration, $results); + $this->checkLogging($configuration, $results); + } + + $this->write('PostfixAdmin Virtual Vacation - system check' . PHP_EOL . PHP_EOL); + $this->printResults($results); + $failed = false; + foreach ($results as $result) { + if (in_array($result->status, ['FAILED', 'MISSING'], true)) { + $failed = true; + break; + } + } + $this->write(PHP_EOL . 'Result: ' . ($failed ? 'FAILED' : 'OK') . PHP_EOL); + return $failed ? 1 : 0; + } + + /** @param array $arguments */ + private function runTest(array $arguments): int + { + $path = $this->findVacationConfig($this->nullableString($arguments['config'])); + if ($path === null) { + throw new ConfigurationException('No vacation.ini found; run --init-config first or use --config'); + } + $loaded = $this->loadVacationConfig($path); + foreach ($loaded['warnings'] as $warning) { + $this->writeError("WARNING: {$warning}" . PHP_EOL); + } + $configuration = $loaded['values']; + $server = (string)($configuration['smtp_server'] ?? 'localhost'); + if ($server === '') { + throw new ConfigurationException( + '--test requires a configured SMTP server; dynamic MX delivery is message-specific' + ); + } + $port = $this->validPort($configuration['smtp_server_port'] ?? 25); + $helo = $this->resolveSmtpHelo($configuration); + $sender = $this->prompt('MAIL FROM', $this->defaultTestSender($helo)); + if (!$this->validEmailAddress($sender)) { + throw new UsageException('Invalid test sender address'); + } + $recipient = $this->prompt('RCPT TO'); + if (!$this->validEmailAddress($recipient)) { + throw new UsageException('Invalid test recipient address'); + } + + $this->write("Sending test message from {$sender} to {$recipient} using {$server}:{$port}..." . PHP_EOL); + $socket = $this->smtpConnect($configuration, $helo); + try { + $this->smtpExpect($this->smtpCommand($socket, "MAIL FROM:<{$sender}>"), [250], 'MAIL FROM'); + $this->smtpExpect($this->smtpCommand($socket, "RCPT TO:<{$recipient}>"), [250, 251], 'RCPT TO'); + $this->smtpExpect($this->smtpCommand($socket, 'DATA'), [354], 'DATA'); + $message = $this->testMessage($sender, $recipient, $helo, $server, $port); + $message = preg_replace('/(?m)^\./', '..', $message) ?? $message; + $this->writeAll( + $socket, + str_replace("\n", "\r\n", str_replace("\r\n", "\n", $message)) . "\r\n.\r\n", + 'Could not write the test message to SMTP', + ); + $this->smtpExpect($this->smtpReadResponse($socket), [250], 'message body'); + } finally { + $this->smtpQuit($socket); + } + $this->write('Test message sent successfully.' . PHP_EOL); + return 0; + } + + /** @param array $arguments */ + private function runTransport(array $arguments): int + { + $envelopeSender = $this->nullableString($arguments['envelope_sender']); + $envelopeRecipient = $this->nullableString($arguments['recipient']); + if ($envelopeSender === null || $envelopeRecipient === null) { + throw new UsageException('Vacation transport requires -f sender -- recipient'); + } + $configuration = $this->runtimeConfiguration($arguments); + $claimedRepository = null; + $claimedVacation = ''; + $claimedSender = ''; + $processingCompleted = false; + try { + $inspection = (new VacationMessageInspector())->inspectStream( + $this->input, + $envelopeSender, + $envelopeRecipient, + $configuration, + ); + if (!$inspection->eligible) { + $this->log($configuration, 'debug', 'Message ignored: ' . $inspection->reason); + return 0; + } + + $database = $this->connectDatabase($configuration); + $tables = is_array($configuration['resolved_tables'] ?? null) + ? $configuration['resolved_tables'] + : array_combine(self::TABLE_KEYS, self::TABLE_KEYS); + $repository = new VacationRepository( + $database, + $tables, + $this->normalizeDatabaseType($configuration['database_type'] ?? ''), + ); + $vacation = $repository->findActiveVacation( + $inspection->envelopeRecipient, + (string)($configuration['vacation_domain'] ?? ''), + ); + if ($vacation === null) { + $this->log( + $configuration, + 'debug', + "No active Vacation record for {$inspection->envelopeRecipient}", + ); + return 0; + } + $vacationAddress = strtolower((string)$vacation['email']); + if (!$repository->claimNotification($vacation, $inspection->envelopeSender)) { + $this->log( + $configuration, + 'debug', + "Notification interval has not elapsed for {$vacationAddress} and {$inspection->envelopeSender}", + ); + return 0; + } + $claimedRepository = $repository; + $claimedVacation = $vacationAddress; + $claimedSender = $inspection->envelopeSender; + + $message = (new VacationReplyComposer())->compose( + $vacation, + $inspection, + $configuration, + $repository->accountName($vacationAddress), + ); + $testMode = in_array( + strtolower((string)($arguments['transport_test'] ?? '')), + ['1', 'true', 'yes'], + true, + ); + if ($testMode) { + $this->write($message); + $this->log($configuration, 'info', 'Transport test generated a Vacation reply without delivery'); + return 0; + } + + $sendmail = trim((string)($configuration['sendmail_path'] ?? '')); + if ($sendmail !== '') { + $this->deliverWithSendmail( + $sendmail, + $vacationAddress, + $inspection->envelopeSender, + $message, + ); + } else { + $deliveryConfiguration = $this->resolveDeliveryConfiguration($configuration, $vacationAddress); + $this->deliverWithSmtp( + $deliveryConfiguration, + $vacationAddress, + $inspection->envelopeSender, + $message, + ); + } + $processingCompleted = true; + $this->log( + $configuration, + 'info', + "Vacation response sent from {$vacationAddress} to {$inspection->envelopeSender}", + ); + return 0; + } catch (Throwable $exception) { + try { + if (!$processingCompleted && $claimedRepository instanceof VacationRepository) { + $claimedRepository->forgetNotification($claimedVacation, $claimedSender); + } + $this->log($configuration, 'error', $exception->getMessage()); + } catch (Throwable) { + // Preserve the original transport failure for Postfix. + } + throw $exception; + } + } + + /** @param array $arguments @return array */ + private function runtimeConfiguration(array $arguments): array + { + $path = $this->findVacationConfig($this->nullableString($arguments['config'])); + if ($path === null) { + throw new ConfigurationException('No vacation.ini found; run --init-config first or use --config'); + } + $loaded = $this->loadVacationConfig($path); + $configuration = $loaded['values']; + foreach ($loaded['warnings'] as $warning) { + $this->writeError("WARNING: {$warning}" . PHP_EOL); + } + $rootArgument = $this->nullableString($arguments['postfixadmin_root']) + ?? ($configuration['postfixadmin_root'] ?? null); + $roots = $this->discoverPostfixAdminRoots(is_string($rootArgument) ? $rootArgument : null); + if ($roots === []) { + throw new ConfigurationException('No PostfixAdmin installation found; use --postfixadmin-root'); + } + $postfixAdmin = $this->loadPostfixAdminConfig($this->choosePostfixAdminRoot($roots, true)); + $configuration = array_replace($postfixAdmin, $configuration); + if (trim((string)($configuration['vacation_domain'] ?? '')) === '') { + throw new ConfigurationException('vacation_domain is required for Vacation transport'); + } + return $configuration; + } + + /** @param array $configuration @return array */ + private function resolveDeliveryConfiguration(array $configuration, string $vacationAddress): array + { + $server = trim((string)($configuration['smtp_server'] ?? 'localhost')); + if ($server !== '') { + $configuration['smtp_server'] = $server; + if (strcasecmp($server, 'localhost') !== 0 + && strcasecmp(trim((string)($configuration['smtp_local_address'] ?? '')), 'localhost') === 0 + ) { + $configuration['smtp_local_address'] = ''; + } + return $configuration; + } + [, $domain] = explode('@', $vacationAddress, 2); + $records = dns_get_record($domain, DNS_MX); + if (!is_array($records) || $records === []) { + throw new RuntimeException("No MX record found for {$domain}"); + } + usort($records, static fn ($left, $right) => ($left['pri'] ?? 0) <=> ($right['pri'] ?? 0)); + $target = rtrim((string)($records[0]['target'] ?? ''), '.'); + if ($target === '') { + throw new RuntimeException("No usable MX target found for {$domain}"); + } + $configuration['smtp_server'] = $target; + if (strcasecmp(trim((string)($configuration['smtp_local_address'] ?? '')), 'localhost') === 0) { + $configuration['smtp_local_address'] = ''; + } + return $configuration; + } + + /** @param array $configuration */ + private function deliverWithSmtp( + array $configuration, + string $sender, + string $recipient, + string $message, + ): void { + $helo = $this->resolveSmtpHelo($configuration, false); + $socket = $this->smtpConnect($configuration, $helo); + try { + $this->smtpExpect($this->smtpCommand($socket, "MAIL FROM:<{$sender}>"), [250], 'MAIL FROM'); + $this->smtpExpect($this->smtpCommand($socket, "RCPT TO:<{$recipient}>"), [250, 251], 'RCPT TO'); + $this->smtpExpect($this->smtpCommand($socket, 'DATA'), [354], 'DATA'); + $message = preg_replace('/(?m)^\./', '..', $message) ?? $message; + $this->writeAll( + $socket, + rtrim($message, "\r\n") . "\r\n.\r\n", + 'Could not write the Vacation message to SMTP', + ); + $this->smtpExpect($this->smtpReadResponse($socket), [250], 'message body'); + } finally { + $this->smtpQuit($socket); + } + } + + private function deliverWithSendmail(string $path, string $sender, string $recipient, string $message): void + { + if (!str_starts_with($path, '/') || !is_executable($path)) { + throw new ConfigurationException('sendmail must be an absolute executable path'); + } + if (!function_exists('proc_open')) { + throw new ConfigurationException('The proc_open function is required for sendmail delivery'); + } + $process = proc_open( + [$path, '-f', $sender, $recipient], + [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], + $pipes, + ); + if (!is_resource($process)) { + throw new RuntimeException("Could not execute sendmail: {$path}"); + } + $this->writeAll($pipes[0], $message, 'Could not write the Vacation message to sendmail'); + fclose($pipes[0]); + $standardOutput = stream_get_contents($pipes[1]); + $standardError = stream_get_contents($pipes[2]); + fclose($pipes[1]); + fclose($pipes[2]); + $status = proc_close($process); + if ($status !== 0) { + $detail = trim((string)$standardError) ?: trim((string)$standardOutput); + throw new RuntimeException("sendmail exited with status {$status}" . ($detail !== '' ? ": {$detail}" : '')); + } + } + + /** @param array $configuration */ + private function log(array $configuration, string $level, string $message): void + { + $priorities = ['error' => LOG_ERR, 'info' => LOG_INFO, 'debug' => LOG_DEBUG]; + $configured = strtolower((string)($configuration['log_level'] ?? 'info')); + $rank = ['error' => 0, 'info' => 1, 'debug' => 2]; + if (($rank[$level] ?? 0) > ($rank[$configured] ?? 1)) { + return; + } + if (!empty($configuration['log_syslog'])) { + openlog('postfixadmin-vacation', LOG_PID, LOG_MAIL); + syslog($priorities[$level] ?? LOG_INFO, $message); + closelog(); + } + if (!empty($configuration['log_file_enabled'])) { + $path = trim((string)($configuration['log_file'] ?? '')); + if ($path === '') { + throw new ConfigurationException('File logging is enabled but logging.file is empty'); + } + $line = sprintf("%s %-5s %s%s", date(DATE_ATOM), strtoupper($level), $message, PHP_EOL); + if (file_put_contents($path, $line, FILE_APPEND | LOCK_EX) === false) { + throw new RuntimeException("Could not write Vacation log: {$path}"); + } + } + } + + /** @param array $arguments */ + private function runInspectMessage(array $arguments): int + { + $envelopeSender = $this->nullableString($arguments['envelope_sender']); + $envelopeRecipient = $this->nullableString($arguments['recipient']); + if ($envelopeSender === null || $envelopeRecipient === null) { + throw new UsageException('--inspect-message requires -f sender -- recipient'); + } + + $configuration = []; + $configPath = $this->findVacationConfig($this->nullableString($arguments['config'])); + if ($configPath !== null) { + $loaded = $this->loadVacationConfig($configPath); + $configuration = $loaded['values']; + foreach ($loaded['warnings'] as $warning) { + $this->writeError("WARNING: {$warning}" . PHP_EOL); + } + } + $rootArgument = $this->nullableString($arguments['postfixadmin_root']) + ?? ($configuration['postfixadmin_root'] ?? null); + $roots = $this->discoverPostfixAdminRoots(is_string($rootArgument) ? $rootArgument : null); + if ($roots === []) { + throw new ConfigurationException('No PostfixAdmin installation found; use --postfixadmin-root'); + } + $postfixAdmin = $this->loadPostfixAdminConfig($this->choosePostfixAdminRoot($roots, true)); + $configuration = array_replace($postfixAdmin, $configuration); + + $messagePath = $this->nullableString($arguments['inspect_message']); + if ($messagePath === null) { + throw new UsageException('--inspect-message requires a file path or - for standard input'); + } + $closeStream = false; + if ($messagePath === '-') { + $stream = $this->input; + } else { + $messagePath = $this->expandHome($messagePath); + $stream = fopen($messagePath, 'rb'); + if ($stream === false) { + throw new RuntimeException("Could not open message: {$messagePath}"); + } + $closeStream = true; + } + try { + $result = (new VacationMessageInspector())->inspectStream( + $stream, + $envelopeSender, + $envelopeRecipient, + $configuration, + ); + } finally { + if ($closeStream) { + fclose($stream); + } + } + + $status = $result->eligible ? 'ELIGIBLE' : 'IGNORED'; + $this->write("Message inspection: {$status} - {$result->reason}" . PHP_EOL); + if ($result->eligible) { + $this->write("Envelope sender: {$result->envelopeSender}" . PHP_EOL); + $this->write("Vacation recipient: {$result->envelopeRecipient}" . PHP_EOL); + $this->write("Message-ID: {$result->messageId}" . PHP_EOL); + } + return 0; + } + + /** @param array $arguments */ + private function showConfigPath(array $arguments): int + { + $path = $this->findVacationConfig($this->nullableString($arguments['config'])); + if ($path === null) { + $this->writeError('No vacation.ini found' . PHP_EOL); + return 1; + } + $this->write($path . PHP_EOL); + return 0; + } + + /** @param list $results */ + private function printResults(array $results): void + { + $section = null; + foreach ($results as $result) { + if ($result->section !== $section) { + if ($section !== null) { + $this->write(PHP_EOL); + } + $this->write($result->section . ':' . PHP_EOL); + $section = $result->section; + } + $detail = $result->detail !== '' ? ' - ' . $result->detail : ''; + $this->write(sprintf(' %-28s %s%s%s', $result->name, $result->status, $detail, PHP_EOL)); + } + } + + /** @param array $configuration @return resource */ + private function smtpConnect(array $configuration, string $helo) + { + $server = (string)($configuration['smtp_server'] ?? 'localhost'); + $port = $this->validPort($configuration['smtp_server_port'] ?? 25); + $timeout = $this->validTimeout($configuration['smtp_timeout'] ?? 120); + $security = $this->normalizeSmtpSecurity($configuration['smtp_security'] ?? 'none'); + if ($security !== 'none' && !extension_loaded('openssl')) { + throw new ConfigurationException('The openssl extension is required for SMTP TLS'); + } + $socketOptions = []; + $localAddress = trim((string)($configuration['smtp_local_address'] ?? '')); + if ($localAddress !== '') { + $socketOptions['bindto'] = filter_var($localAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) + ? "[{$localAddress}]:0" + : "{$localAddress}:0"; + } + $context = stream_context_create([ + 'socket' => $socketOptions, + 'ssl' => [ + 'peer_name' => $server, + 'verify_peer' => true, + 'verify_peer_name' => true, + 'allow_self_signed' => false, + ], + ]); + $host = filter_var($server, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? "[{$server}]" : $server; + $scheme = $security === 'ssl' ? 'tls' : 'tcp'; + $errorCode = 0; + $errorMessage = ''; + $socket = @stream_socket_client( + "{$scheme}://{$host}:{$port}", + $errorCode, + $errorMessage, + $timeout, + STREAM_CLIENT_CONNECT, + $context, + ); + if ($socket === false) { + throw new RuntimeException("SMTP connection failed ({$errorCode}): {$errorMessage}"); + } + try { + stream_set_timeout($socket, $timeout); + $this->smtpExpect($this->smtpReadResponse($socket), [220], 'greeting'); + $hello = $this->smtpHello($socket, $helo, $security !== 'starttls'); + if (in_array($security, ['starttls', 'maybestarttls'], true)) { + $supportsStartTls = preg_match('/^250[ -]STARTTLS\b/im', $hello[1]) === 1; + if (!$supportsStartTls && $security === 'starttls') { + throw new ConfigurationException('SMTP server does not advertise STARTTLS'); + } + if ($supportsStartTls) { + $this->smtpExpect($this->smtpCommand($socket, 'STARTTLS'), [220], 'STARTTLS'); + if (!stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { + throw new RuntimeException('SMTP TLS negotiation failed'); + } + $hello = $this->smtpHello($socket, $helo); + } + } + $username = (string)($configuration['smtp_username'] ?? ''); + if ($username !== '') { + $password = (string)($configuration['smtp_password'] ?? ''); + if ($password === '') { + throw new ConfigurationException('SMTP password is required when username is configured'); + } + $this->smtpAuthenticate($socket, $hello[1], $username, $password); + } + return $socket; + } catch (Throwable $exception) { + @fclose($socket); + throw $exception; + } + } + + /** @param resource $socket */ + private function smtpAuthenticate($socket, string $capabilities, string $username, string $password): void + { + if (preg_match('/^250[ -]AUTH\b[^\r\n]*\bPLAIN\b/im', $capabilities)) { + $credentials = base64_encode("\0{$username}\0{$password}"); + $this->smtpExpect( + $this->smtpCommand($socket, "AUTH PLAIN {$credentials}", 'AUTH PLAIN [redacted]'), + [235], + 'authentication', + ); + return; + } + if (preg_match('/^250[ -]AUTH\b[^\r\n]*\bLOGIN\b/im', $capabilities)) { + $this->smtpExpect($this->smtpCommand($socket, 'AUTH LOGIN'), [334], 'AUTH LOGIN'); + $this->smtpExpect( + $this->smtpCommand($socket, base64_encode($username), 'AUTH username [redacted]'), + [334], + 'AUTH username', + ); + $this->smtpExpect( + $this->smtpCommand($socket, base64_encode($password), 'AUTH password [redacted]'), + [235], + 'AUTH password', + ); + return; + } + throw new ConfigurationException( + 'SMTP authentication is configured but the server offers neither PLAIN nor LOGIN' + ); + } + + /** @param resource $socket @return array{0: int, 1: string} */ + private function smtpHello($socket, string $helo, bool $allowHeloFallback = true): array + { + $response = $this->smtpCommand($socket, "EHLO {$helo}"); + if ($response[0] >= 400 && $allowHeloFallback) { + $response = $this->smtpCommand($socket, "HELO {$helo}"); + } + $this->smtpExpect($response, [250], 'HELO'); + return $response; + } + + /** @param resource $socket @return array{0: int, 1: string} */ + private function smtpCommand($socket, string $command, ?string $displayCommand = null): array + { + $this->writeAll( + $socket, + $command . "\r\n", + 'Could not write SMTP command: ' . ($displayCommand ?? $command), + ); + return $this->smtpReadResponse($socket); + } + + /** @param resource $stream */ + private function writeAll($stream, string $data, string $error): void + { + $offset = 0; + $length = strlen($data); + while ($offset < $length) { + $written = fwrite($stream, substr($data, $offset)); + if ($written === false || $written === 0) { + throw new RuntimeException($error); + } + $offset += $written; + } + } + + /** @param resource $socket @return array{0: int, 1: string} */ + private function smtpReadResponse($socket): array + { + $lines = []; + $code = 0; + do { + $line = fgets($socket, 4096); + if ($line === false) { + $metadata = stream_get_meta_data($socket); + throw new RuntimeException(!empty($metadata['timed_out']) ? 'SMTP response timed out' : 'SMTP connection closed'); + } + $lines[] = rtrim($line, "\r\n"); + if (preg_match('/^(\d{3})([ -])/', $line, $matches)) { + $code = (int)$matches[1]; + $continued = $matches[2] === '-'; + } else { + $continued = false; + } + } while ($continued); + return [$code, implode("\n", $lines)]; + } + + /** @param array{0: int, 1: string} $response @param list $expected */ + private function smtpExpect(array $response, array $expected, string $operation): void + { + if (!in_array($response[0], $expected, true)) { + throw new RuntimeException("SMTP {$operation} failed: {$response[1]}"); + } + } + + /** @param resource $socket */ + private function smtpQuit($socket): void + { + if (!is_resource($socket)) { + return; + } + @fwrite($socket, "QUIT\r\n"); + @fclose($socket); + } + + private function testMessage(string $sender, string $recipient, string $helo, string $server, int $port): string + { + $date = new DateTimeImmutable('now', new DateTimeZone(date_default_timezone_get())); + $messageId = sprintf('<%s@%s>', bin2hex(random_bytes(16)), $helo); + return implode("\n", [ + "From: {$sender}", + "To: {$recipient}", + 'Subject: PostfixAdmin vacation.php test', + 'Date: ' . $date->format(DATE_RFC2822), + "Message-ID: {$messageId}", + 'Auto-Submitted: auto-generated', + 'X-PostfixAdmin-Vacation-Test: yes', + 'MIME-Version: 1.0', + 'Content-Type: text/plain; charset=UTF-8', + 'Content-Transfer-Encoding: 8bit', + '', + 'This is a test message sent by PostfixAdmin vacation.php.', + '', + "SMTP server: {$server}:{$port}", + "SMTP HELO: {$helo}", + ]); + } + + /** @param array $configuration */ + public function resolveSmtpHelo(array $configuration, bool $allowPrompt = true): string + { + $configured = trim((string)($configuration['smtp_helo'] ?? '')); + $helo = $configured !== '' ? $configured : $this->detectedFqdn(); + if ($helo === null && $allowPrompt) { + $helo = $this->prompt('SMTP HELO'); + } + if ($helo === null) { + throw new ConfigurationException( + 'A valid smtp.helo setting is required when no system FQDN can be detected' + ); + } + if (!$this->validHelo($helo)) { + throw new ConfigurationException('A valid SMTP HELO name is required'); + } + return $helo; + } + + public function detectedFqdn(): ?string + { + $hostname = gethostname(); + if (!is_string($hostname)) { + return null; + } + $candidates = [$hostname]; + $address = gethostbyname($hostname); + if ($address !== $hostname) { + $reverse = gethostbyaddr($address); + if (is_string($reverse)) { + array_unshift($candidates, $reverse); + } + } + foreach ($candidates as $candidate) { + $candidate = strtolower(trim($candidate, ". \t\n\r\0\x0B")); + if ($this->validHelo($candidate) && !in_array($candidate, ['localhost', 'localhost.localdomain'], true)) { + return $candidate; + } + } + return null; + } + + public function baseDomain(string $hostname): string + { + $labels = array_values(array_filter(explode('.', strtolower(trim($hostname, '.'))), static fn ($label) => $label !== '')); + if (count($labels) >= 3) { + array_shift($labels); + } + return $labels !== [] ? implode('.', $labels) : 'localhost'; + } + + public function defaultTestSender(string $helo): string + { + return 'noreply@' . $this->baseDomain($helo); + } + + public function validEmailAddress(string $address): bool + { + return !str_contains($address, "\r") + && !str_contains($address, "\n") + && filter_var($address, FILTER_VALIDATE_EMAIL) === $address; + } + + private function validHelo(string $helo): bool + { + return str_contains($helo, '.') && !preg_match('/\s/', $helo); + } + + private function validPort(mixed $value): int + { + $port = filter_var($value, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 65535]]); + if ($port === false) { + throw new ConfigurationException('SMTP port must be between 1 and 65535'); + } + return $port; + } + + private function validTimeout(mixed $value): int + { + $timeout = filter_var($value, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 3600]]); + if ($timeout === false) { + throw new ConfigurationException('SMTP timeout must be between 1 and 3600 seconds'); + } + return $timeout; + } + + private function normalizeSmtpSecurity(mixed $value): string + { + $value = strtolower(trim((string)$value)); + return match ($value) { + '', '0', 'false', 'none', 'plain' => 'none', + '1', 'true', 'ssl', 'tls' => 'ssl', + 'starttls' => 'starttls', + 'maybestarttls' => 'maybestarttls', + default => throw new ConfigurationException( + 'SMTP security must be none, ssl, starttls, or maybestarttls' + ), + }; + } + + private function normalizeLogLevel(mixed $value): string + { + $level = strtolower(trim((string)$value)); + if (!in_array($level, ['error', 'info', 'debug'], true)) { + throw new ConfigurationException('Logging level must be error, info, or debug'); + } + return $level; + } + + private function normalizeDatabaseType(mixed $value): string + { + return match (strtolower((string)$value)) { + 'mysql', 'mysqli', 'mariadb' => 'mysql', + 'pgsql', 'postgres', 'postgresql', 'pg' => 'postgresql', + 'sqlite', 'sqlite3' => 'sqlite', + default => strtolower((string)$value), + }; + } + + private function quoteIdentifier(string $identifier, string $databaseType): string + { + return $databaseType === 'mysql' ? "`{$identifier}`" : '"' . $identifier . '"'; + } + + private function parsePerlValue(string $value): bool|int|string + { + $value = trim($value); + if (preg_match("/^'(.*)'$/s", $value, $matches)) { + return str_replace(["\\'", "\\\\"], ["'", "\\"], $matches[1]); + } + if (preg_match('/^"(.*)"$/s', $value, $matches)) { + return stripcslashes($matches[1]); + } + if (preg_match('/^-?\d+$/', $value)) { + return (int)$value; + } + return match (strtolower($value)) { + 'true', 'yes' => true, + 'false', 'no' => false, + default => throw new RuntimeException("unsupported Perl value: {$value}"), + }; + } + + private function perlDateFormatToPhp(string $format): string + { + return strtr($format, [ + '%Y' => 'Y', + '%y' => 'y', + '%m' => 'm', + '%d' => 'd', + '%b' => 'M', + '%B' => 'F', + '%H' => 'H', + '%M' => 'i', + '%S' => 's', + '%%' => '%', + ]); + } + + private function iniValue(string $value): string + { + return '"' . addcslashes($value, "\\\"") . '"'; + } + + private function promptValue(string $label, string $default, bool $nonInteractive): string + { + return $nonInteractive ? $default : $this->prompt($label, $default); + } + + public function prompt(string $label, ?string $default = null): string + { + $suffix = $default !== null ? " [{$default}]" : ''; + $this->write("{$label}{$suffix}: "); + $line = fgets($this->input); + if ($line === false) { + throw new RuntimeException("No input received for {$label}"); + } + $value = trim($line); + if ($value === '' && $default !== null) { + return $default; + } + return $value; + } + + private function expandHome(string $path): string + { + if (!str_starts_with($path, '~/') && !str_starts_with($path, '~\\')) { + return $path; + } + $home = getenv(PHP_OS_FAMILY === 'Windows' ? 'USERPROFILE' : 'HOME'); + return is_string($home) && $home !== '' ? $home . substr($path, 1) : $path; + } + + private function samePath(string $left, string $right): bool + { + $leftReal = realpath($left); + $rightReal = realpath($right); + if ($leftReal !== false && $rightReal !== false) { + return strcasecmp($leftReal, $rightReal) === 0; + } + return strcasecmp(str_replace('\\', '/', $left), str_replace('\\', '/', $right)) === 0; + } + + private function nullableString(mixed $value): ?string + { + return is_string($value) && $value !== '' ? $value : null; + } + + private function write(string $message): void + { + fwrite($this->output, $message); + } + + private function writeError(string $message): void + { + fwrite($this->errorOutput, $message); + } +} + +if (realpath($_SERVER['SCRIPT_FILENAME'] ?? '') === __FILE__) { + exit((new VacationCli())->run($argv)); +} diff --git a/tests/VacationTest.php b/tests/VacationTest.php new file mode 100644 index 00000000..a43a40ef --- /dev/null +++ b/tests/VacationTest.php @@ -0,0 +1,818 @@ +assertSame(69, $cli->run(['vacation.php'])); + rewind($error); + $lines = preg_split('/\R/', trim((string)stream_get_contents($error))); + $this->assertSame(1, count($lines)); + } + + public function testArguments(): void + { + $cli = new VacationCli(); + $arguments = $cli->parseArguments([ + 'vacation.php', + '--check', + '--config=/etc/postfixadmin/vacation.ini', + '--postfixadmin-root', + '/var/www/postfixadmin', + ]); + $this->assertTrue($arguments['check']); + $this->assertSame('/etc/postfixadmin/vacation.ini', $arguments['config']); + $this->assertSame('/var/www/postfixadmin', $arguments['postfixadmin_root']); + $inspection = $cli->parseArguments([ + 'vacation.php', + '--inspect-message=message.eml', + '-f', + 'sender@example.org', + '--', + 'user#example.org@autoreply.example.org', + ]); + $this->assertSame('message.eml', $inspection['inspect_message']); + $this->assertSame('sender@example.org', $inspection['envelope_sender']); + $this->assertSame('user#example.org@autoreply.example.org', $inspection['recipient']); + $transport = $cli->parseArguments([ + 'vacation.php', + '-t', + 'yes', + '-f', + 'sender@example.org', + '--', + 'user#example.org@autoreply.example.org', + ]); + $this->assertSame('yes', $transport['transport_test']); + $this->assertSame('sender@example.org', $transport['envelope_sender']); + } + + public function testGeneratedConfiguration(): void + { + $cli = new VacationCli(); + $directory = $this->temporaryDirectory(); + try { + $path = $directory . '/vacation.ini'; + file_put_contents($path, $cli->renderConfig('/var/www/html/postfixadmin', 'localhost', 25, 'mail.example.org')); + $loaded = $cli->loadVacationConfig($path); + $this->assertSame([], $loaded['warnings']); + $this->assertSame('/var/www/html/postfixadmin', $loaded['values']['postfixadmin_root']); + $this->assertSame('localhost', $loaded['values']['smtp_server']); + $this->assertSame(25, $loaded['values']['smtp_server_port']); + $this->assertSame('mail.example.org', $loaded['values']['smtp_helo']); + $this->assertSame('none', $loaded['values']['smtp_security']); + $this->assertSame(120, $loaded['values']['smtp_timeout']); + $this->assertSame( + VacationMessageInspector::DEFAULT_NO_VACATION_PATTERN, + $loaded['values']['message_no_vacation_pattern'], + ); + $explicitlyEmpty = (string)file_get_contents($path) + . PHP_EOL . '[message]' . PHP_EOL . 'no_vacation_pattern = ""' . PHP_EOL; + file_put_contents($path, $explicitlyEmpty); + $this->assertSame('', $cli->loadVacationConfig($path)['values']['message_no_vacation_pattern']); + $this->assertFalse($cli->isLegacyConfig($path)); + $this->assertSame(realpath($path), $cli->findVacationConfig($path)); + putenv('VACATION_SMTP_PASSWORD=environment-secret'); + try { + $loaded = $cli->loadVacationConfig($path); + $this->assertSame('environment-secret', $loaded['values']['smtp_password']); + } finally { + putenv('VACATION_SMTP_PASSWORD'); + } + } finally { + $this->removeDirectory($directory); + } + } + + public function testLegacyImport(): void + { + $cli = new VacationCli(); + $directory = $this->temporaryDirectory(); + try { + $path = $directory . '/vacation.conf'; + file_put_contents($path, implode(PHP_EOL, [ + "\$smtp_server = 'localhost';", + '$smtp_server_port = 25;', + "\$smtp_helo = 'mail.example.org';", + "\$smtp_ssl = 'starttls';", + "\$smtp_timeout = '30';", + "\$smtp_client = '192.0.2.10';", + "\$smtp_authid = 'vacation';", + "\$smtp_authpwd = 'secret';", + "\$sendmail_bin = '/usr/sbin/sendmail';", + "\$recipient_delimiter = '+';", + "\$custom_noreply_pattern = 1;", + "\$noreply_pattern = 'social|notification';", + "\$friendly_from = 'Away';", + '$accountname_check = 1;', + "\$replace_from = '<%From_Date>';", + "\$replace_until = '<%Until_Date>';", + "\$date_format = '%d-%m-%Y';", + '$syslog = 0;', + '$log_level = 2;', + '$log_to_file = 1;', + "\$logfile = '/var/log/custom-vacation.log';", + "\$unsupported = \$ENV{'SECRET'};", + '', + ])); + $loaded = $cli->loadLegacyConfig($path); + $this->assertSame('localhost', $loaded['values']['smtp_server']); + $this->assertSame(25, $loaded['values']['smtp_server_port']); + $this->assertSame('mail.example.org', $loaded['values']['smtp_helo']); + $rendered = $cli->renderConfig('/var/www/html/postfixadmin', 'smtp.example.org', 587, 'mail.example.org', [ + 'security' => $loaded['values']['smtp_ssl'], + 'timeout' => $loaded['values']['smtp_timeout'], + 'local_address' => $loaded['values']['smtp_client'], + 'username' => $loaded['values']['smtp_authid'], + 'password' => $loaded['values']['smtp_authpwd'], + 'sendmail' => $loaded['values']['sendmail_bin'], + ], [ + 'recipient_delimiter' => $loaded['values']['recipient_delimiter'], + 'custom_noreply_pattern' => $loaded['values']['custom_noreply_pattern'], + 'noreply_pattern' => $loaded['values']['noreply_pattern'], + ], [ + 'friendly_from' => $loaded['values']['friendly_from'], + 'account_name' => $loaded['values']['accountname_check'], + 'replace_from' => $loaded['values']['replace_from'], + 'replace_until' => $loaded['values']['replace_until'], + 'date_format' => 'd-m-Y', + ], [ + 'syslog' => $loaded['values']['syslog'], + 'level' => 'debug', + 'file_enabled' => $loaded['values']['log_to_file'], + 'file' => $loaded['values']['logfile'], + ]); + file_put_contents($directory . '/vacation.ini', $rendered); + $phpConfiguration = $cli->loadVacationConfig($directory . '/vacation.ini'); + $this->assertSame('starttls', $phpConfiguration['values']['smtp_security']); + $this->assertSame(30, $phpConfiguration['values']['smtp_timeout']); + $this->assertSame('192.0.2.10', $phpConfiguration['values']['smtp_local_address']); + $this->assertSame('vacation', $phpConfiguration['values']['smtp_username']); + $this->assertSame('secret', $phpConfiguration['values']['smtp_password']); + $this->assertSame('/usr/sbin/sendmail', $phpConfiguration['values']['sendmail_path']); + $this->assertSame('+', $phpConfiguration['values']['recipient_delimiter']); + $this->assertTrue($phpConfiguration['values']['message_custom_noreply_pattern']); + $this->assertSame('social|notification', $phpConfiguration['values']['message_noreply_pattern']); + $this->assertSame('Away', $phpConfiguration['values']['reply_friendly_from']); + $this->assertTrue($phpConfiguration['values']['reply_account_name']); + $this->assertSame('d-m-Y', $phpConfiguration['values']['reply_date_format']); + $this->assertFalse($phpConfiguration['values']['log_syslog']); + $this->assertSame('debug', $phpConfiguration['values']['log_level']); + $this->assertTrue($phpConfiguration['values']['log_file_enabled']); + $this->assertSame('/var/log/custom-vacation.log', $phpConfiguration['values']['log_file']); + $this->assertSame(1, count($loaded['warnings'])); + $this->assertTrue($cli->isLegacyConfig($path)); + } finally { + $this->removeDirectory($directory); + } + } + + public function testPostfixAdminConfigurationLoading(): void + { + $cli = new VacationCli(); + $directory = $this->temporaryDirectory(); + try { + file_put_contents($directory . '/config.inc.php', <<<'PHP' + true, + 'database_type' => 'mysqli', + 'database_host' => 'localhost', + 'database_user' => 'postfix', + 'database_password' => 'default', + 'database_name' => 'postfix', + 'database_prefix' => 'pfa_', + 'database_tables' => ['vacation' => 'away'], + 'vacation_domain' => 'autoreply.example.org', +]; +require __DIR__ . '/config.local.php'; +PHP); + file_put_contents($directory . '/config.local.php', <<<'PHP' +loadPostfixAdminConfig($directory); + $this->assertSame('local-secret', $configuration['database_password']); + $this->assertSame('autoreply.example.org', $configuration['vacation_domain']); + $this->assertSame('pfa_away', $configuration['resolved_tables']['vacation']); + $this->assertSame('pfa_alias', $configuration['resolved_tables']['alias']); + $this->assertSame([realpath($directory)], $cli->discoverPostfixAdminRoots($directory)); + } finally { + $this->removeDirectory($directory); + } + } + + public function testAddressAndDomainHelpers(): void + { + $cli = new VacationCli(); + $this->assertSame('example.org', $cli->baseDomain('mail.example.org')); + $this->assertSame('noreply@example.org', $cli->defaultTestSender('mail.example.org')); + $this->assertTrue($cli->validEmailAddress('admin@example.org')); + $this->assertFalse($cli->validEmailAddress("admin@example.org\nBcc: victim@example.org")); + $this->assertFalse($cli->validEmailAddress('not-an-address')); + } + + public function testConfiguredHeloNeedsNoPrompt(): void + { + $input = fopen('php://memory', 'r+'); + $output = fopen('php://memory', 'r+'); + $error = fopen('php://memory', 'r+'); + $cli = new VacationCli($input, $output, $error); + $this->assertSame('configured.example.org', $cli->resolveSmtpHelo(['smtp_helo' => 'configured.example.org'])); + rewind($output); + $this->assertSame('', stream_get_contents($output)); + } + + public function testConfigurationIsWrittenAtomicallyWithRestrictedMode(): void + { + $directory = $this->temporaryDirectory(); + try { + $path = $directory . '/vacation.ini'; + $method = new ReflectionMethod(VacationCli::class, 'writeConfigurationFile'); + $method->invoke(new VacationCli(), $path, "[smtp]\npassword = secret\n"); + $this->assertSame("[smtp]\npassword = secret\n", file_get_contents($path)); + if (PHP_OS_FAMILY !== 'Windows') { + clearstatcache(true, $path); + $this->assertSame(0640, fileperms($path) & 0777); + } + $this->assertSame([], glob($directory . '/.vacation.ini.*') ?: []); + } finally { + $this->removeDirectory($directory); + } + } + + public function testCompleteWritesHandleShortStreamWrites(): void + { + $scheme = 'vacationshortwrite'; + if (in_array($scheme, stream_get_wrappers(), true)) { + stream_wrapper_unregister($scheme); + } + stream_wrapper_register($scheme, VacationShortWriteStream::class); + try { + $stream = fopen($scheme . '://output', 'w'); + $this->assertTrue(is_resource($stream)); + $method = new ReflectionMethod(VacationCli::class, 'writeAll'); + $method->invoke(new VacationCli(), $stream, 'complete payload', 'write failed'); + fclose($stream); + $this->assertSame('complete payload', VacationShortWriteStream::$contents); + } finally { + stream_wrapper_unregister($scheme); + } + } + + public function testRemoteSmtpClearsLegacyLoopbackBinding(): void + { + $method = new ReflectionMethod(VacationCli::class, 'resolveDeliveryConfiguration'); + $configuration = $method->invoke(new VacationCli(), [ + 'smtp_server' => 'smtp.example.org', + 'smtp_local_address' => 'localhost', + ], 'user@example.org'); + $this->assertSame('', $configuration['smtp_local_address']); + } + + public function testDependencyResultsAreExplicit(): void + { + $cli = new VacationCli(); + $results = []; + $cli->checkDependencies(['database_type' => 'sqlite'], $results); + $names = array_map(static fn ($result) => $result->name, $results); + $this->assertTrue(in_array('mbstring', $names, true)); + $this->assertTrue(in_array('mailparse', $names, true)); + $this->assertTrue(in_array('PDO', $names, true)); + $this->assertTrue(in_array('pdo_sqlite', $names, true)); + } + + public function testMessageInspectionRules(): void + { + $inspector = new VacationMessageInspector(); + $base = [ + 'From' => 'Sender ', + 'To' => "User ,\r\n another@example.org", + 'Subject' => 'Test message', + 'Message-ID' => '', + ]; + $configuration = [ + 'vacation_domain' => 'autoreply.example.org', + 'recipient_delimiter' => '+', + ]; + $eligible = $inspector->inspectHeaders( + $base + ['Auto-Submitted' => 'no'], + 'sender@example.org', + 'user+tag#example.org@autoreply.example.org', + $configuration, + ); + $this->assertTrue($eligible->eligible); + $this->assertSame('user@example.org', $eligible->envelopeRecipient); + $this->assertSame('sender@example.org', $eligible->from); + $this->assertSame('user@example.org, another@example.org', $eligible->to); + + $rejections = [ + ['X-Spam-Flag', 'YES'], + ['X-Spam-Status', 'Yes, score=20'], + ['X-Facebook-Notify', ''], + ['X-Amazon-Mail-Relay-Type', 'notification'], + ['Precedence', 'bulk'], + ['X-Loop', 'Postfix Admin Virtual Vacation'], + ['Auto-Submitted', 'auto-replied'], + ['List-Id', ''], + ['List-Post', ''], + ['List-Unsubscribe', ''], + ['X-Barracuda-Spam-Status', 'Yes'], + ['X-DSPAM-Result', 'Blacklisted'], + ['X-Virus-Status', 'infected'], + ['X-Antivirus-Status', 'infected'], + ['X-AVAS-Virus-Status', 'infected'], + ['X-AVAS-Spam-Status', 'spam'], + ['X-SpamTest-Status', 'spam'], + ['X-CRM114-Status', 'spam'], + ['X-Razor-Status', 'spam'], + ['X-Pyzor-Status', 'spam'], + ['X-OSBF-Lua-Score', '0.95 [S]'], + ['X-Autogenerated', 'reply'], + ['X-Auto-Response-Suppress', 'OOF'], + ]; + foreach ($rejections as [$header, $value]) { + $result = $inspector->inspectHeaders( + $base + [$header => $value], + 'sender@example.org', + 'user#example.org@autoreply.example.org', + $configuration, + ); + $this->assertFalse($result->eligible); + } + } + + public function testMessageAddressSafety(): void + { + $inspector = new VacationMessageInspector(); + $base = [ + 'From' => 'sender@example.org', + 'To' => 'user@example.org', + 'Message-ID' => '', + ]; + $this->assertFalse($inspector->inspectHeaders( + $base, + 'user@example.org', + 'user@example.org', + )->eligible); + $this->assertFalse($inspector->inspectHeaders( + array_replace($base, ['To' => 'sender@example.org']), + 'sender@example.org', + 'user@example.org', + )->eligible); + $this->assertFalse($inspector->inspectHeaders( + $base + ['Reply-To' => 'mailer-daemon@example.org'], + 'other@example.org', + 'user@example.org', + )->eligible); + $this->assertFalse($inspector->inspectHeaders( + $base, + 'other@example.org', + 'user@example.org', + ['message_no_vacation_pattern' => 'user@example\.org'], + )->eligible); + $this->assertFalse($inspector->inspectHeaders( + array_replace($base, ['To' => 'info@example.org']), + 'other@example.org', + 'user@example.org', + )->eligible); + $this->assertTrue($inspector->inspectHeaders( + array_replace($base, ['To' => 'info@example.org']), + 'other@example.org', + 'user@example.org', + ['message_no_vacation_pattern' => ''], + )->eligible); + $missingId = $base; + unset($missingId['Message-ID']); + $this->assertFalse($inspector->inspectHeaders( + $missingId, + 'other@example.org', + 'user@example.org', + )->eligible); + } + + public function testMessageInspectionStopsAfterHeaders(): void + { + if (!extension_loaded('mailparse')) { + $this->markTestSkipped('The mailparse extension is required'); + } + $stream = fopen('php://memory', 'r+'); + fwrite($stream, implode("\r\n", [ + 'From: sender@example.org', + 'To: user@example.org', + 'Message-ID: ', + '', + str_repeat('body attachment data', 2000), + ])); + rewind($stream); + $result = (new VacationMessageInspector())->inspectStream( + $stream, + 'sender@example.org', + 'user@example.org', + ['message_no_vacation_pattern' => ''], + ); + $this->assertTrue($result->eligible); + $this->assertFalse(feof($stream)); + fclose($stream); + } + + public function testHistoricalMessageFixtures(): void + { + $fixtureDirectory = dirname(__DIR__) . '/VIRTUAL_VACATION/tests'; + $inspector = new VacationMessageInspector(); + $configuration = ['vacation_domain' => 'autoreply.example.org']; + $cases = [ + ['test-email.txt', 'david1@example.org', true], + ['asterisk-email.txt', 'www-data@palepurple.net', true], + ['spam.txt', 'mary@ccr.org', false], + ['facebook.txt', 'notification+meynbxsa@facebookmail.com', false], + ['mailing-list.txt', 'fw-general-return@example.org', false], + ['mail-myself.txt', 'david@example.org', false], + ['teodor-smtp-envelope-headers.txt', 'david@example.org', false], + ]; + foreach ($cases as [$fixture, $sender, $expected]) { + $result = $inspector->inspectHeaders( + $this->headersFromMessage($fixtureDirectory . '/' . $fixture), + $sender, + 'david#example.org@autoreply.example.org', + $configuration, + ); + $this->assertSame($expected, $result->eligible); + } + } + + public function testReplyComposition(): void + { + $inspection = (new VacationMessageInspector())->inspectHeaders([ + 'From' => 'Sender ', + 'To' => 'user@example.org', + 'Subject' => 'Original subject', + 'Message-ID' => '', + ], 'sender@example.org', 'user@example.org'); + $message = (new VacationReplyComposer())->compose([ + 'email' => 'user@example.org', + 'subject' => 'Away: $SUBJECT', + 'body' => 'Away from <%From_Date> until <%Until_Date>.', + 'activefrom' => '2026-08-01 00:00:00', + 'activeuntil' => '2026-08-31 23:59:59', + ], $inspection, [ + 'reply_account_name' => true, + 'reply_date_format' => 'd-m-Y', + ], 'Example User'); + $this->assertTrue(str_contains($message, 'To: sender@example.org')); + $this->assertTrue(str_contains($message, 'From: Example User ')); + $this->assertTrue(str_contains($message, 'Subject: Away: Original subject')); + $this->assertTrue(str_contains($message, 'X-Loop: Postfix Admin Virtual Vacation')); + $this->assertTrue(str_contains($message, 'Auto-Submitted: auto-replied')); + $this->assertTrue(str_contains($message, 'Away from 01-08-2026 until 31-08-2026.')); + } + + public function testVacationRepository(): void + { + if (!in_array('sqlite', PDO::getAvailableDrivers(), true)) { + $this->markTestSkipped('The pdo_sqlite extension is required'); + } + $database = new PDO('sqlite::memory:'); + $database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $database->exec('CREATE TABLE vacation (' + . 'email TEXT PRIMARY KEY, subject TEXT, body TEXT, activefrom TEXT, activeuntil TEXT, ' + . 'interval_time INTEGER, active INTEGER)'); + $database->exec('CREATE TABLE vacation_notification (' + . 'on_vacation TEXT, notified TEXT, notified_at TEXT DEFAULT CURRENT_TIMESTAMP, ' + . 'PRIMARY KEY (on_vacation, notified))'); + $database->exec('CREATE TABLE alias (address TEXT, goto TEXT)'); + $database->exec('CREATE TABLE alias_domain (alias_domain TEXT, target_domain TEXT)'); + $database->exec('CREATE TABLE mailbox (username TEXT, name TEXT)'); + $insert = $database->prepare( + 'INSERT INTO vacation VALUES (?, ?, ?, ?, ?, ?, ?)' + ); + $activeFrom = date('Y-m-d H:i:s', time() - 86400); + $activeUntil = date('Y-m-d H:i:s', time() + 86400); + foreach (['user@example.org', 'bob@example.org', 'alice@example.org'] as $email) { + $insert->execute([ + $email, + 'Away', + 'Body', + $activeFrom, + $activeUntil, + 0, + 1, + ]); + } + $insert->execute([ + 'expired@example.org', + 'Away', + 'Body', + '2025-01-01 00:00:00', + '2025-12-31 23:59:59', + 0, + 1, + ]); + $insert->execute(['invalid-date@example.org', 'Away', 'Body', null, null, 0, 1]); + $database->exec("INSERT INTO alias VALUES " + . "('team@example.org', 'user@example.org,team#example.org@autoreply.example.org'), " + . "('@catch.example', '@example.org')"); + $database->exec("INSERT INTO alias_domain VALUES ('alias.example', 'example.org')"); + $database->exec("INSERT INTO mailbox VALUES ('user@example.org', 'Example User')"); + $repository = new VacationRepository($database, [], 'sqlite'); + $this->assertSame('user@example.org', $repository->findActiveVacation( + 'user@example.org', + 'autoreply.example.org', + )['email']); + $this->assertSame('user@example.org', $repository->findActiveVacation( + 'team@example.org', + 'autoreply.example.org', + )['email']); + $this->assertSame('bob@example.org', $repository->findActiveVacation( + 'bob@alias.example', + 'autoreply.example.org', + )['email']); + $this->assertSame('alice@example.org', $repository->findActiveVacation( + 'alice@catch.example', + 'autoreply.example.org', + )['email']); + $this->assertSame(null, $repository->findActiveVacation( + 'expired@example.org', + 'autoreply.example.org', + )); + $this->assertNull($repository->findActiveVacation( + 'invalid-date@example.org', + 'autoreply.example.org', + )); + $vacation = $repository->findActiveVacation('user@example.org', 'autoreply.example.org'); + $this->assertTrue($repository->claimNotification($vacation, 'sender@example.net')); + $this->assertFalse($repository->claimNotification($vacation, 'sender@example.net')); + $database->exec("UPDATE vacation SET interval_time = 60 WHERE email = 'user@example.org'"); + $database->exec("UPDATE vacation_notification SET notified_at = '2026-01-01 00:00:00'"); + $vacation = $repository->findActiveVacation('user@example.org', 'autoreply.example.org'); + $this->assertTrue($repository->claimNotification($vacation, 'sender@example.net')); + $this->assertFalse($repository->claimNotification($vacation, 'sender@example.net')); + $newActiveFrom = date('Y-m-d H:i:s', time() - 3600); + $oldNotification = date('Y-m-d H:i:s', time() - 7200); + $statement = $database->prepare( + "UPDATE vacation SET activefrom = ?, interval_time = 0 WHERE email = 'user@example.org'" + ); + $statement->execute([$newActiveFrom]); + $statement = $database->prepare('UPDATE vacation_notification SET notified_at = ?'); + $statement->execute([$oldNotification]); + $vacation = $repository->findActiveVacation('user@example.org', 'autoreply.example.org'); + $this->assertTrue($repository->claimNotification($vacation, 'sender@example.net')); + $this->assertSame('Example User', $repository->accountName('user@example.org')); + $repository->forgetNotification('user@example.org', 'sender@example.net'); + $this->assertSame(0, (int)$database->query('SELECT COUNT(*) FROM vacation_notification')->fetchColumn()); + } + + public function testTransportExitStatuses(): void + { + $output = fopen('php://memory', 'r+'); + $error = fopen('php://memory', 'r+'); + $cli = new VacationCli(null, $output, $error); + $this->assertSame(64, $cli->run(['vacation.php', '--unknown-option'])); + $this->assertSame(78, $cli->run([ + 'vacation.php', + '--config', + '/path/that/does/not/exist/vacation.ini', + '-f', + 'sender@example.net', + '--', + 'user@example.org', + ])); + } + + public function testCompleteTransport(): void + { + if (PHP_OS_FAMILY === 'Windows') { + $this->markTestSkipped('The process-based SMTP transport test requires a Unix-like platform'); + } + if (!extension_loaded('mailparse')) { + $this->markTestSkipped('The mailparse extension is required'); + } + if (!in_array('sqlite', PDO::getAvailableDrivers(), true)) { + $this->markTestSkipped('The pdo_sqlite extension is required'); + } + + $directory = $this->temporaryDirectory(); + try { + $databasePath = $directory . '/vacation.sqlite'; + $deliveredPath = $directory . '/delivered.eml'; + + file_put_contents($directory . '/config.inc.php', ' 'sqlite'," . PHP_EOL + . " 'database_name' => " . var_export($databasePath, true) . ',' . PHP_EOL + . " 'vacation_domain' => 'autoreply.example.org'," . PHP_EOL + . '];' . PHP_EOL + . "require __DIR__ . '/config.local.php';" . PHP_EOL); + file_put_contents($directory . '/config.local.php', "setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $database->exec('CREATE TABLE vacation (' + . 'email TEXT PRIMARY KEY, subject TEXT, body TEXT, activefrom TEXT, activeuntil TEXT, ' + . 'interval_time INTEGER, active INTEGER)'); + $database->exec('CREATE TABLE vacation_notification (' + . 'on_vacation TEXT, notified TEXT, notified_at TEXT DEFAULT CURRENT_TIMESTAMP, ' + . 'PRIMARY KEY (on_vacation, notified))'); + $database->exec('CREATE TABLE alias (address TEXT, goto TEXT)'); + $database->exec('CREATE TABLE alias_domain (alias_domain TEXT, target_domain TEXT)'); + $database->exec('CREATE TABLE mailbox (username TEXT, name TEXT)'); + $insert = $database->prepare('INSERT INTO vacation VALUES (?, ?, ?, ?, ?, ?, ?)'); + $insert->execute([ + 'user@example.org', + 'Away: $SUBJECT', + 'I am away.', + date('Y-m-d H:i:s', time() - 3600), + date('Y-m-d H:i:s', time() + 3600), + 3600, + 1, + ]); + $database->exec("INSERT INTO mailbox VALUES ('user@example.org', 'Example User')"); + + $configurationPath = $directory . '/vacation.ini'; + [$smtpProcess, $smtpPort] = $this->startSmtpServer($directory, $deliveredPath); + $this->writeTransportConfiguration($configurationPath, $directory, $smtpPort); + $message = implode("\r\n", [ + 'From: Sender ', + 'To: User ', + 'Subject: Integration test', + 'Message-ID: ', + '', + 'Message body.', + '', + ]); + + $this->assertSame(0, $this->runTransport($configurationPath, $message, 'sender@example.net')); + $this->assertSame(0, proc_close($smtpProcess)); + $delivered = file_get_contents($deliveredPath); + $this->assertTrue(is_string($delivered)); + $this->assertTrue(str_contains((string)$delivered, 'To: sender@example.net')); + $this->assertTrue(str_contains((string)$delivered, 'Subject: Away: Integration test')); + $this->assertSame(1, (int)$database->query('SELECT COUNT(*) FROM vacation_notification')->fetchColumn()); + + $deliveredSize = filesize($deliveredPath); + $this->assertSame(0, $this->runTransport($configurationPath, $message, 'sender@example.net')); + clearstatcache(true, $deliveredPath); + $this->assertSame($deliveredSize, filesize($deliveredPath)); + + $this->writeTransportConfiguration($configurationPath, $directory, $smtpPort); + $this->assertSame(75, $this->runTransport($configurationPath, $message, 'failure@example.net')); + $statement = $database->query( + "SELECT COUNT(*) FROM vacation_notification WHERE notified = 'failure@example.net'" + ); + $this->assertSame(0, (int)$statement->fetchColumn()); + } finally { + $this->removeDirectory($directory); + } + } + + /** @return array{0: resource, 1: int} */ + private function startSmtpServer(string $directory, string $deliveredPath): array + { + $readyPath = $directory . '/smtp-port'; + $process = proc_open( + [PHP_BINARY, __DIR__ . '/fixtures/VacationMockSmtp.php', $readyPath, $deliveredPath], + [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], + $pipes, + ); + if (!is_resource($process)) { + throw new RuntimeException('Could not start the mock SMTP server'); + } + fclose($pipes[0]); + fclose($pipes[1]); + fclose($pipes[2]); + for ($attempt = 0; $attempt < 200 && !is_file($readyPath); ++$attempt) { + usleep(10000); + } + $port = is_file($readyPath) ? filter_var(trim((string)file_get_contents($readyPath)), FILTER_VALIDATE_INT) : false; + if ($port === false) { + proc_terminate($process); + proc_close($process); + throw new RuntimeException('The mock SMTP server did not become ready'); + } + return [$process, $port]; + } + + private function writeTransportConfiguration(string $path, string $root, int $smtpPort): void + { + file_put_contents($path, implode(PHP_EOL, [ + '[postfixadmin]', + 'root = "' . addcslashes($root, '\\"') . '"', + '[smtp]', + 'server = "127.0.0.1"', + 'port = ' . $smtpPort, + 'helo = "vacation.test"', + '[logging]', + 'syslog = false', + '', + ])); + } + + private function runTransport(string $configurationPath, string $message, string $sender): int + { + $input = fopen('php://memory', 'r+'); + fwrite($input, $message); + rewind($input); + return (new VacationCli($input, fopen('php://memory', 'r+'), fopen('php://memory', 'r+')))->run([ + 'vacation.php', + '--config', + $configurationPath, + '-f', + $sender, + '--', + 'user#example.org@autoreply.example.org', + ]); + } + + /** @return array> */ + private function headersFromMessage(string $path): array + { + $lines = file($path, FILE_IGNORE_NEW_LINES); + if ($lines === false) { + throw new RuntimeException("Could not read fixture: {$path}"); + } + $headers = []; + $current = null; + foreach ($lines as $line) { + if ($line === '' || $line === "\r") { + break; + } + if (preg_match('/^[ \t]+(.*)$/', $line, $matches) && $current !== null) { + $last = count($headers[$current]) - 1; + $headers[$current][$last] .= ' ' . trim($matches[1]); + continue; + } + if (!str_contains($line, ':')) { + $current = null; + continue; + } + [$name, $value] = explode(':', $line, 2); + $current = strtolower(trim($name)); + $headers[$current][] = trim($value); + } + return $headers; + } + + private function temporaryDirectory(): string + { + $path = sys_get_temp_dir() . '/postfixadmin-vacation-php-' . bin2hex(random_bytes(8)); + if (!mkdir($path, 0700, true)) { + throw new RuntimeException("Could not create test directory: {$path}"); + } + return $path; + } + + private function removeDirectory(string $directory): void + { + foreach (scandir($directory) ?: [] as $entry) { + if ($entry === '.' || $entry === '..') { + continue; + } + $path = $directory . '/' . $entry; + if (is_dir($path)) { + $this->removeDirectory($path); + } else { + unlink($path); + } + } + rmdir($directory); + } +} diff --git a/tests/fixtures/VacationMockSmtp.php b/tests/fixtures/VacationMockSmtp.php new file mode 100644 index 00000000..d2da1d1e --- /dev/null +++ b/tests/fixtures/VacationMockSmtp.php @@ -0,0 +1,65 @@ +.\r\n"); + } elseif (strcasecmp($command, 'QUIT') === 0) { + fwrite($connection, "221 Bye\r\n"); + break; + } else { + fwrite($connection, "500 Unsupported command\r\n"); + } +} + +fclose($connection); +fclose($server);