Skip to content

Commit 7c0effc

Browse files
committed
Real mailboxes infra: public mail ports, sender checks, fail2ban, webmail, hourly mail backup
1 parent f131f6a commit 7c0effc

45 files changed

Lines changed: 747 additions & 174 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/infra-boot.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ jobs:
2727
echo dummy > .vault_pass
2828
ansible-playbook -i inventory/ci.yml playbooks/render.yml -e "render_dir=$GITHUB_WORKSPACE/rendered"
2929
30+
- name: Rspamd from owner rule
31+
run: |
32+
sudo apt-get update -q
33+
sudo apt-get install -y -q luajit
34+
luajit infra/ansible/roles/hackorum/files/tests/from_owner_test.lua rendered/ci/hackorum/dms/rspamd-lua/from_owner.lua
35+
3036
- name: Build image
3137
run: docker build -t ghcr.io/hackorum-dev/hackorum:ci .
3238

@@ -47,6 +53,16 @@ jobs:
4753
curl -fs http://127.0.0.1:3000/up
4854
curl -fs http://127.0.0.1:3000/metrics | grep -q hackorum_solid_queue_ready_jobs
4955
56+
- name: Mail config render
57+
working-directory: rendered/ci/hackorum
58+
run: |
59+
cmp dms/config/postfix-accounts.system.cf dms/config/postfix-accounts.cf
60+
docker compose -f compose/compose.yml exec -T web bin/rails mail:render
61+
grep -q '^receiver@ci.hackorum.test|' dms/config/postfix-accounts.cf
62+
grep -q '^app@ci.hackorum.test|' dms/config/postfix-accounts.cf
63+
grep -qx 'receiver@ci.hackorum.test receiver@ci.hackorum.test' dms/config/sender-login.map
64+
test -f dms/config/dovecot-quotas.cf
65+
5066
- name: Logs on failure
5167
if: failure()
5268
working-directory: rendered/ci/hackorum/compose

‎app/models/mailbox_address.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class MailboxAddress < ApplicationRecord
1111
validates :local_part, format: { with: FORMAT }, allow_blank: true
1212
validate :no_consecutive_dots
1313
validate :name_available, on: :create
14+
validate :no_system_account_mixup, on: :create
1415

1516
after_commit { MailConfigRenderJob.perform_later }
1617
after_create :reserve_name
@@ -36,6 +37,12 @@ def name_available
3637
end
3738
end
3839

40+
def no_system_account_mixup
41+
return unless login? && local_part.present?
42+
return unless MailConfig::SYSTEM_ACCOUNTS.any? { |system| MailConfig.mixed_up?(local_part, system) }
43+
errors.add(:local_part, "conflicts with a system mail account")
44+
end
45+
3946
def owned_by_user?(reservation)
4047
reservation.owner_type == "User" && reservation.owner_id == mailbox&.user_id
4148
end

‎app/services/mail_config.rb‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,17 @@ def self.available? = dir.present?
44
def self.domain = ENV["MAIL_DOMAIN"]
55
def self.host = ENV["MAIL_HOST"]
66
def self.webmail_host = ENV["WEBMAIL_HOST"]
7+
8+
# accounts the mailserver gets from its own config, not from mailboxes
9+
SYSTEM_ACCOUNTS = %w[receiver app].freeze
10+
11+
# The mailserver looks accounts and quotas up with an unanchored grep ("." is a wildcard)
12+
# and takes the first line that matches, so a name can hit the line of a longer one.
13+
def self.tail_match?(pattern, name)
14+
name.match?(/#{Regexp.escape(pattern).gsub('\\.', '.')}\z/)
15+
end
16+
17+
def self.mixed_up?(one, other)
18+
one != other && (tail_match?(one, other) || tail_match?(other, one))
19+
end
720
end

‎app/services/mail_config/renderer.rb‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def build
3939
raise MissingFragment, "postfix-accounts.system.cf has no accounts" if system_accounts.empty?
4040

4141
taken = system_keys(accounts, virtual)
42-
mailboxes = enabled_mailboxes.reject { |m| collision?(taken, login(m)) }
42+
mailboxes = enabled_mailboxes.reject { |m| collision?(taken, login(m)) || mixed_up?(system_accounts, login(m)) }
4343
{
4444
"postfix-accounts.cf" => join(accounts, mailboxes.map { |m| "#{addr(login(m))}|#{m.password_hash}" }),
4545
"postfix-virtual.cf" => join(virtual, aliases(mailboxes, taken).sort.map { |a, l| "#{addr(a)} #{addr(l)}" }),
@@ -54,7 +54,7 @@ def build
5454
def enabled_mailboxes
5555
scope = Mailbox.enabled.joins(:user).where(users: { deleted_at: nil })
5656
scope = scope.where(users: { admin: true }) if Hackorum.staging?
57-
scope.includes(:addresses).filter_map { |m| m if login(m) }.sort_by { |m| login(m) }
57+
scope.includes(:addresses).filter_map { |m| m if login(m) }.sort_by { |m| first_match_order(login(m)) }
5858
end
5959

6060
def login(mailbox)
@@ -87,6 +87,17 @@ def collision?(taken, local)
8787
true
8888
end
8989

90+
# shorter first, more dots first: the first line a login's pattern matches is then its own
91+
def first_match_order(login) = [ login.length, -login.count("."), login ]
92+
93+
def mixed_up?(system_accounts, local)
94+
suffix = "@#{@domain}".downcase
95+
names = system_accounts.map(&:downcase).select { |a| a.end_with?(suffix) }.map { |a| a.delete_suffix(suffix) }
96+
return false unless names.any? { |name| MailConfig.mixed_up?(local, name) }
97+
Rails.logger.error("mail config: #{addr(local)} would be mixed up with a system account, skipped")
98+
true
99+
end
100+
90101
def addr(local) = "#{local}@#{@domain}"
91102

92103
def join(fragment, lines)

0 commit comments

Comments
 (0)