From 83414d5f4487c254d56ad87d7e250348dec0a0fb Mon Sep 17 00:00:00 2001 From: Daniel Winzen Date: Sun, 18 Oct 2026 10:50:09 +0200 Subject: [PATCH] Add .onion identity --- README.md | 2 ++ common_config.php | 27 +++++++++++++++----- etc/rspamd/rspamd.local.lua | 51 +++++++++++++++++++++++++++++++++++++ setup.php | 17 ++++++++++++- www/admin.php | 8 +++++- www/register.php | 8 +++++- 6 files changed, 104 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7b2d4f5..998561a 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,8 @@ Postfix updates a mailbox's `last_login` timestamp when an authenticated SMTP se Mailbox owners can add identity addresses on their mailbox domain from account settings; administrators can associate addresses on domains they manage from the mailbox editor. Identity addresses deliver only to their owner, are hidden from the regular alias list, and are authorized as sender addresses only for that mailbox's SMTP login. +Registering a mailbox on `CLEARNET_SERVER` automatically creates its matching `local_part@ONION_SERVER` identity. The database upgrade backfills this identity for existing clearnet mailboxes. + Create a password used for your TURN server and replace all `YOUR_SECRET` in `/etc/prosody/prosody.cfg.lua` with it. Install [acme.sh](https://github.com/acmesh-official/acme.sh) or [certbot](https://certbot.eff.org/) to obtain a free letsencrypt SSL certificate, then update the path to this new certificate in the following files: diff --git a/common_config.php b/common_config.php index e80b7eb..600a0d4 100644 --- a/common_config.php +++ b/common_config.php @@ -9,7 +9,7 @@ const DBHOST = 'localhost'; // Database host const DBUSER = 'postfix'; // Database user const DBPASS = 'YOUR_PASSWORD'; // Database password const DBNAME = 'postfix'; // Database -const DBVERSION = 12; // Database schema version +const DBVERSION = 13; // Database schema version const INACTIVITY_PERIOD_MONTHS = [1, 6, 12, 24]; // Allowed inactivity periods in months - don't forget to update get_inactivity_period_options on changes here and if an option is removed, you should also update accounts in the database mailbox table const DEFAULT_INACTIVITY_PERIOD = 12; // Default inactivity period in months const ALLOW_ACCOUNT_NEVER_DELETE = false; // Whether to allow users to set their inactivity period to "never delete" @@ -518,11 +518,11 @@ function check_email_valid( string $email, string &$msg = '' ): bool function create_mailbox_identity( PDO $db, string $identityAddress, string $mailboxUsername, string &$msg = '', bool $allowOtherDomains = false ): bool { $identityAddress = mb_strtolower( trim( $identityAddress ) ); - $stmt = $db->prepare( 'SELECT username, domain FROM mailbox WHERE username = ? AND active = 1;' ); + $stmt = $db->prepare( 'SELECT username, domain, active FROM mailbox WHERE username = ?;' ); $stmt->execute( [ $mailboxUsername ] ); $mailbox = $stmt->fetch( PDO::FETCH_ASSOC ); - if ( ! $mailbox ) { - $msg .= ''; + if ( ! $mailbox || (int) $mailbox[ 'active' ] < 0 ) { + $msg .= ''; return false; } $mailboxUsername = $mailbox[ 'username' ]; @@ -550,11 +550,26 @@ function create_mailbox_identity( PDO $db, string $identityAddress, string $mail $msg .= ''; return false; } - $stmt = $db->prepare( 'INSERT INTO alias (address, goto, domain, created, modified, active, enforce_tls_in, identity) VALUES (?, ?, ?, NOW(), NOW(), 1, 1, ?);' ); - $stmt->execute( [ $identityAddress, $mailboxUsername, $identityDomain, $mailboxUsername ] ); + $stmt = $db->prepare( 'INSERT INTO alias (address, goto, domain, created, modified, active, enforce_tls_in, identity) VALUES (?, ?, ?, NOW(), NOW(), ?, 1, ?);' ); + $stmt->execute( [ $identityAddress, $mailboxUsername, $identityDomain, ( (int) $mailbox[ 'active' ] === 1 ? 1 : 0 ), $mailboxUsername ] ); return true; } +function create_clearnet_onion_identity( PDO $db, string $mailboxUsername, string &$msg = '' ): bool +{ + $stmt = $db->prepare( 'SELECT local_part, domain FROM mailbox WHERE username = ?;' ); + $stmt->execute( [ $mailboxUsername ] ); + $mailbox = $stmt->fetch( PDO::FETCH_ASSOC ); + if ( ! $mailbox ) { + $msg .= ''; + return false; + } + if ( $mailbox[ 'domain' ] !== CLEARNET_SERVER ) { + return true; + } + return create_mailbox_identity( $db, $mailbox[ 'local_part' ] . '@' . ONION_SERVER, $mailboxUsername, $msg, true ); +} + function parse_sender_address_list( string $value ): ?array { $validator = new EmailValidator(); diff --git a/etc/rspamd/rspamd.local.lua b/etc/rspamd/rspamd.local.lua index f40a0e5..2d0d8cf 100644 --- a/etc/rspamd/rspamd.local.lua +++ b/etc/rspamd/rspamd.local.lua @@ -56,3 +56,54 @@ rspamd_config:register_symbol{ } rspamd_config:register_dependency('DKIM_SIGNED', 'GOOGLE_FEEDBACK_ID') + +rspamd_config:register_symbol{ + name = 'ONION_SENDER_RECIPIENT_POLICY', + type = 'prefilter', + priority = lua_util.symbols_priorities.top, + flags = 'empty,nostat', + score = 0.0, + callback = function(task) + local function is_onion_address(address) + local domain = address and address.domain + if not domain then + return false + end + domain = domain:lower():gsub('%.$', '') + return domain:match('%.onion$') ~= nil + end + + local onion_sender = false + for _, address_type in ipairs({ 'smtp', 'mime' }) do + local senders = task:get_from(address_type) or {} + for _, sender in ipairs(senders) do + if is_onion_address(sender) then + onion_sender = true + break + end + end + if onion_sender then + break + end + end + + if not onion_sender then + return + end + + local recipients = task:get_recipients('smtp') or {} + if #recipients == 0 then + return + end + for _, recipient in ipairs(recipients) do + if not is_onion_address(recipient) then + task:set_pre_result( + 'reject', + 'Messages from .onion senders may only be sent to .onion recipients', + 'ONION_SENDER_RECIPIENT_POLICY' + ) + return + end + end + end, +} diff --git a/setup.php b/setup.php index a594a7a..e61385d 100644 --- a/setup.php +++ b/setup.php @@ -26,6 +26,7 @@ try{ $createTableStatements = [ 'admin' => "CREATE TABLE IF NOT EXISTS `admin` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `superadmin` tinyint(1) NOT NULL DEFAULT 0, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `modified` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '{MD5-CRYPT}', `totp_secret` varchar(64) DEFAULT NULL, PRIMARY KEY (`username`), KEY `active` (`active`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'domain' => "CREATE TABLE IF NOT EXISTS `domain` (`domain` varchar(255) NOT NULL, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `modified` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `dkim_key` text NOT NULL DEFAULT '', `dkim_selector` varchar(20) NOT NULL DEFAULT '', PRIMARY KEY (`domain`), KEY `active` (`active`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", + 'mailbox' => "CREATE TABLE IF NOT EXISTS `mailbox` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL DEFAULT '', `quota` bigint(20) NOT NULL DEFAULT 0, `local_part` varchar(255) NOT NULL DEFAULT '', `domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT current_timestamp(), `modified` datetime NOT NULL DEFAULT current_timestamp(), `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '', `openpgpkey_wkd` char(32) NOT NULL DEFAULT '', `pgp_key` text DEFAULT NULL, `pgp_verified` tinyint(1) NOT NULL DEFAULT 0, `tfa` tinyint(1) NOT NULL DEFAULT 0, `totp_secret` varchar(64) DEFAULT NULL, `last_login` bigint(20) unsigned DEFAULT NULL, `inactivity_period` tinyint unsigned NOT NULL DEFAULT 12, `spam_reject_score` tinyint unsigned NOT NULL DEFAULT 15, `blocked_senders` text NOT NULL DEFAULT '', `whitelisted_senders` text NOT NULL DEFAULT '', `feedback_id` char(15) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, `trusted` tinyint(1) NOT NULL DEFAULT 0, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), UNIQUE KEY `feedback_id` (`feedback_id`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), KEY `inactivity_period` (`inactivity_period`), KEY `spam_reject_score` (`spam_reject_score`), KEY `created` (`created`), KEY `trusted` (`trusted`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'alias' => "CREATE TABLE IF NOT EXISTS `alias` (`address` varchar(255) NOT NULL, `goto` text NOT NULL, `domain` varchar(255) NOT NULL, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `modified` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `identity` varchar(255) DEFAULT NULL, PRIMARY KEY (`address`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `identity` (`identity`), CONSTRAINT `alias_ibfk_1` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE, CONSTRAINT `alias_identity_ibfk_1` FOREIGN KEY (`identity`) REFERENCES `mailbox` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'alias_domain' => "CREATE TABLE IF NOT EXISTS `alias_domain` (`alias_domain` varchar(255) NOT NULL DEFAULT '', `target_domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `modified` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`alias_domain`), KEY `active` (`active`), KEY `target_domain` (`target_domain`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'captcha' => "CREATE TABLE IF NOT EXISTS `captcha` (`id` bigint(20) NOT NULL AUTO_INCREMENT, `time` int(11) NOT NULL, `code` char(5) NOT NULL, PRIMARY KEY (`id`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;", @@ -36,7 +37,6 @@ $createTableStatements = [ 'dmarc_report_record_reason' => "CREATE TABLE IF NOT EXISTS `dmarc_report_record_reason` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `record_id` bigint(20) unsigned NOT NULL, `type` varchar(255) NOT NULL, `comment` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `record_id` (`record_id`), CONSTRAINT `dmarc_report_record_reason_ibfk_1` FOREIGN KEY (`record_id`) REFERENCES `dmarc_report_records` (`id`) ON DELETE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'dmarc_report_record_spf_result' => "CREATE TABLE IF NOT EXISTS `dmarc_report_record_spf_result` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `record_id` bigint(20) unsigned NOT NULL, `domain` varchar(255) NOT NULL, `result` varchar(255) NOT NULL, `scope` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `record_id` (`record_id`), CONSTRAINT `dmarc_report_record_spf_result_ibfk_2` FOREIGN KEY (`record_id`) REFERENCES `dmarc_report_records` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;", 'domain_admins' => "CREATE TABLE IF NOT EXISTS `domain_admins` (`username` varchar(255) NOT NULL, `domain` varchar(255) NOT NULL, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `id` bigint(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), KEY `username` (`username`), KEY `active` (`active`), KEY `domain` (`domain`), CONSTRAINT `domain_admins_ibfk_1` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `domain_admins_ibfk_2` FOREIGN KEY (`username`) REFERENCES `admin` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", - 'mailbox' => "CREATE TABLE IF NOT EXISTS `mailbox` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL DEFAULT '', `quota` bigint(20) NOT NULL DEFAULT 0, `local_part` varchar(255) NOT NULL DEFAULT '', `domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT current_timestamp(), `modified` datetime NOT NULL DEFAULT current_timestamp(), `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '', `openpgpkey_wkd` char(32) NOT NULL DEFAULT '', `pgp_key` text DEFAULT NULL, `pgp_verified` tinyint(1) NOT NULL DEFAULT 0, `tfa` tinyint(1) NOT NULL DEFAULT 0, `totp_secret` varchar(64) DEFAULT NULL, `last_login` bigint(20) unsigned DEFAULT NULL, `inactivity_period` tinyint unsigned NOT NULL DEFAULT 12, `spam_reject_score` tinyint unsigned NOT NULL DEFAULT 15, `blocked_senders` text NOT NULL DEFAULT '', `whitelisted_senders` text NOT NULL DEFAULT '', `feedback_id` char(15) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL, `trusted` tinyint(1) NOT NULL DEFAULT 0, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), UNIQUE KEY `feedback_id` (`feedback_id`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), KEY `inactivity_period` (`inactivity_period`), KEY `spam_reject_score` (`spam_reject_score`), KEY `created` (`created`), KEY `trusted` (`trusted`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'monitored_mailboxes' => "CREATE TABLE IF NOT EXISTS `monitored_mailboxes` (`username` varchar(255) NOT NULL, `last_login` int(11) NOT NULL, PRIMARY KEY (`username`), KEY `lastlogin` (`last_login`), CONSTRAINT `monitored_mailboxes_ibfk_1` FOREIGN KEY (`username`) REFERENCES `mailbox` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'tls_report' => "CREATE TABLE IF NOT EXISTS `tls_report` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `report_id` varchar(255) NOT NULL, `organization` varchar(255) NOT NULL, `contact` varchar(255) NOT NULL, `start` datetime NOT NULL, `end` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `report_id_organization` (`report_id`,`organization`), KEY `start_end` (`start`,`end`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'tls_report_policy' => "CREATE TABLE IF NOT EXISTS `tls_report_policy` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `report_id` bigint(20) unsigned NOT NULL, `policy_type` varchar(255) NOT NULL, `policy_string` text NOT NULL, `policy_domain` varchar(255) NOT NULL, `mx_host` text NOT NULL, `success` int(11) NOT NULL, `failure` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `report_id` (`report_id`), KEY `policy_domain` (`policy_domain`), CONSTRAINT `tls_report_policy_ibfk_1` FOREIGN KEY (`report_id`) REFERENCES `tls_report` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `tls_report_policy_ibfk_3` FOREIGN KEY (`policy_domain`) REFERENCES `domain` (`domain`) ON DELETE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", @@ -152,6 +152,21 @@ try{ $db->exec( 'ALTER TABLE `alias` ADD CONSTRAINT `alias_identity_ibfk_1` FOREIGN KEY (`identity`) REFERENCES `mailbox` (`username`) ON DELETE CASCADE ON UPDATE CASCADE;' ); } } + if ( $version < 13 ) { + $backfill_onion_identities = $db->prepare( + "INSERT INTO `alias` (`address`, `goto`, `domain`, `created`, `modified`, `active`, `enforce_tls_in`, `identity`) + SELECT CONCAT(m.`local_part`, '@', ?), m.`username`, ?, NOW(), NOW(), IF(m.`active` = 1, 1, 0), m.`enforce_tls_in`, m.`username` + FROM `mailbox` AS m + INNER JOIN `domain` AS d ON d.`domain` = ? AND d.`active` = 1 + WHERE m.`domain` = ? + AND NOT EXISTS ( + SELECT 1 FROM `alias` AS existing_identity + WHERE existing_identity.`address` = CONCAT(m.`local_part`, '@', ?) + AND existing_identity.`identity` = m.`username` + );" + ); + $backfill_onion_identities->execute( [ ONION_SERVER, ONION_SERVER, ONION_SERVER, CLEARNET_SERVER, ONION_SERVER ] ); + } $db->beginTransaction(); $stmt=$db->prepare("UPDATE `settings` SET `value`=? WHERE `setting`='version';"); $stmt->execute([DBVERSION]); diff --git a/www/admin.php b/www/admin.php index 6cbbeaa..8096223 100644 --- a/www/admin.php +++ b/www/admin.php @@ -447,11 +447,17 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { $alias_goto .= validate_email_list( $additional, $msg ); } $alias_goto = rtrim( $alias_goto, ',' ); + $db->beginTransaction(); $stmt = $db->prepare( 'INSERT INTO alias (address, goto, domain, created, modified, enforce_tls_in, active) VALUES (?, ?, ?, NOW(), NOW(), ?, ?);' ); $stmt->execute( [ $email, $alias_goto, $domain, ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ) ] ); $stmt = $db->prepare( 'INSERT INTO mailbox (username, password, quota, local_part, domain, created, modified, password_hash_type, openpgpkey_wkd, feedback_id, enforce_tls_in, enforce_tls_out, active) VALUES(?, ?, ?, ?, ?, NOW(), NOW(), ?, ?, ?, ?, ?, ?);' ); $stmt->execute( [ $email, $hash, DEFAULT_QUOTA, $user, $domain, '{ARGON2ID}', z_base32_encode( hash( 'sha1', mb_strtolower( $user ), true ) ), generate_feedback_id( $db ), ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'enforce_tls_out' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ) ] ); - $msg .= ''; + if ( ! create_clearnet_onion_identity( $db, $email, $msg ) ) { + $db->rollBack(); + } else { + $db->commit(); + $msg .= ''; + } } } } elseif ( $_POST[ 'action' ] === 'save_password_mailbox' && ! empty( $_POST[ 'user' ] ) ) { diff --git a/www/register.php b/www/register.php index ec11f74..ec11842 100644 --- a/www/register.php +++ b/www/register.php @@ -67,11 +67,17 @@ if ( isset( $_POST[ 'user' ] ) ) { } if ( $ok ) { $hash = password_hash( $_POST[ 'pwd' ], PASSWORD_ARGON2ID ); + $db->beginTransaction(); $stmt = $db->prepare( 'INSERT INTO alias (address, goto, domain, created, modified) VALUES (?, ?, ?, NOW(), NOW());' ); $stmt->execute( [ "$user@$domain", "$user@$domain", $domain ] ); $stmt = $db->prepare( 'INSERT INTO mailbox (username, password, quota, local_part, domain, created, modified, password_hash_type, openpgpkey_wkd, feedback_id) VALUES(?, ?, ?, ?, ?, NOW(), NOW(), ?, ?, ?);' ); $stmt->execute( [ "$user@$domain", $hash, DEFAULT_QUOTA, $user, $domain, '{ARGON2ID}', z_base32_encode( hash( 'sha1', mb_strtolower( $user ), true ) ), generate_feedback_id( $db ) ] ); - $msg .= ''; + if ( ! create_clearnet_onion_identity( $db, "$user@$domain", $msg ) ) { + $db->rollBack(); + } else { + $db->commit(); + $msg .= ''; + } } } }