diff --git a/common_config.php b/common_config.php index e94a09a..3049fb2 100644 --- a/common_config.php +++ b/common_config.php @@ -9,10 +9,13 @@ const DBHOST = 'localhost'; // Database host const DBUSER = 'postfix'; // Database user const DBPASS = 'YOUR_PASSWORD'; // Database password const DBNAME = 'postfix'; // Database -const DBVERSION = 3; // Database schema version +const DBVERSION = 4; // 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" const PERSISTENT = true; // persistent database connection const CAPTCHA_DIFFICULTY = 1; // captcha difficulty from 0 to 4 -const RESERVED_USERNAMES = ['about', 'abuse', 'admin', 'administrator', 'billing', 'contact', 'daemon', 'ftp', 'help', 'hostmaster', 'info', 'legal', 'list', 'list-request', 'lists', 'maildaemon', 'mailerdaemon', 'mailer-daemon', 'marketing', 'media', 'news', 'newsletter', 'nobody', 'noc', 'noreply', 'no-reply', 'notification', 'notifications', 'notify', 'offer', 'offers', 'office', 'official', 'order', 'orders', 'phish', 'phishing', 'postmaster', 'root', 'sale', 'sales', 'security', 'service', 'services', 'shop', 'shopping', 'spam', 'staff', 'support', 'survey', 'system', 'team', 'teams', 'unsbubscribe', 'uucp', 'usenet', 'user', 'username', 'users', 'web', 'webmail', 'webmaster', 'webmasters', 'welcome', 'www']; // list of reserved usernames that can mot be used on public registration +const RESERVED_USERNAMES = ['about', 'abuse', 'admin', 'administrator', 'billing', 'contact', 'daemon', 'ftp', 'help', 'hostmaster', 'info', 'legal', 'list', 'list-request', 'lists', 'maildaemon', 'mailerdaemon', 'mailer-daemon', 'marketing', 'media', 'news', 'newsletter', 'nobody', 'noc', 'noreply', 'no-reply', 'notification', 'notifications', 'notify', 'offer', 'offers', 'office', 'official', 'order', 'orders', 'phish', 'phishing', 'postmaster', 'root', 'sale', 'sales', 'security', 'service', 'services', 'shop', 'shopping', 'spam', 'staff', 'support', 'survey', 'system', 'team', 'teams', 'unsubscribe', 'uucp', 'usenet', 'user', 'username', 'users', 'web', 'webmail', 'webmaster', 'webmasters', 'welcome', 'www']; // list of reserved usernames that can not be used on public registration const CANONICAL_URL = 'https://mail.danwin1210.de/'; // our preferred URL prefix for search engines const PRIVACY_POLICY_URL = '/privacy.php'; // URL to privacy policy const WEB_XMPP_URL = 'https://danwin1210.de:5281/conversejs'; // URL to Web-XMPP @@ -84,6 +87,16 @@ textdomain('mail-hosting'); require_once( __DIR__ . '/vendor/autoload.php' ); +function get_inactivity_period_options(): array +{ + return [ + 1 => _('1 month'), + 6 => _('6 months'), + 12 => _('1 year'), + 24 => _('2 years'), + ]; +} + function get_db_instance(): PDO { static $db = null; diff --git a/cron.php b/cron.php index f2c7348..3c8347a 100644 --- a/cron.php +++ b/cron.php @@ -12,10 +12,12 @@ try { } setlocale( LC_CTYPE, 'C.UTF-8' ); // make sure to use UTF-8 locale. Non UTF-8 locales can cause serious issues when handling UTF-8 file names -// mark accounts deletable that haven't been used in an entire year -$expire_time = strtotime( '-1 year' ); -$stmt = $db->prepare( 'UPDATE mailbox SET active = -2 WHERE active IN (0, 1) AND modified < ? AND (last_login < ? OR last_login IS NULL);' ); -$stmt->execute( [ date( 'Y-m-d H:i:s', $expire_time ), $expire_time ] ); +// mark accounts deletable after their selected period of inactivity +foreach ( INACTIVITY_PERIOD_MONTHS as $months ) { + $expire_time = strtotime( "-$months months" ); + $stmt = $db->prepare( 'UPDATE mailbox SET active = -2 WHERE active IN (0, 1) AND inactivity_period = ? AND inactivity_period > 0 AND modified < ? AND (last_login < ? OR last_login IS NULL);' ); + $stmt->execute( [ $months, date( 'Y-m-d H:i:s', $expire_time ), $expire_time ] ); +} // delete all associated data when deleting/disabling accounts $stmt = $db->query( 'SELECT username, local_part, domain, active FROM mailbox WHERE active IN (-1, -2);' ); diff --git a/setup.php b/setup.php index 0241fc6..d226770 100644 --- a/setup.php +++ b/setup.php @@ -36,7 +36,7 @@ $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, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) 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, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), 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;", @@ -46,12 +46,12 @@ $createTableStatements = [ try{ $stmt=$db->query("SELECT value FROM settings WHERE setting='version';"); - $version=$stmt->fetch(PDO::FETCH_NUM)[0]; + $version=(int) $stmt->fetch(PDO::FETCH_NUM)[0]; try { foreach($createTableStatements as $tableName => $statement){ $db->exec($statement); } - if ( (int) $version < 3 ) { + if ( $version < 3 ) { foreach ( [ 'admin', 'mailbox' ] as $tableName ) { $stmt = $db->query( "SHOW COLUMNS FROM `$tableName` LIKE 'totp_secret';" ); if ( ! $stmt->fetch() ) { @@ -59,6 +59,12 @@ try{ } } } + if ( $version < 4 ) { + $stmt = $db->query( "SHOW COLUMNS FROM `mailbox` LIKE 'inactivity_period';" ); + if ( ! $stmt->fetch() ) { + $db->exec( 'ALTER TABLE `mailbox` ADD COLUMN `inactivity_period` tinyint unsigned NOT NULL DEFAULT 12 AFTER `last_login`;' ); + } + } $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 2ddc748..0749372 100644 --- a/www/admin.php +++ b/www/admin.php @@ -334,20 +334,29 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { $ok = check_domain_access( $_POST[ 'user' ], $msg ); } if ( $ok ) { - $alias_goto = ''; - if ( isset( $_POST[ 'alias_keep_copy' ] ) ) { - $alias_goto .= $_POST[ 'user' ] . ','; + $inactivity_period = filter_var( $_POST[ 'inactivity_period' ] ?? null, FILTER_VALIDATE_INT ); + $valid_inactivity_period = $inactivity_period !== false && ( + in_array( $inactivity_period, INACTIVITY_PERIOD_MONTHS, true ) + || ( ALLOW_ACCOUNT_NEVER_DELETE && $inactivity_period === 0 ) + ); + if ( ! $valid_inactivity_period ) { + $msg .= '