caelum0x 173b60097d feat(mail): OCI relay pool, direct MTA (parked), inbound HTTPS ingest, Cloudflare inbound forwarder
- mail-oci: OCI Email Delivery client (signed REST), per-customer domain/DKIM/sender onboarding, suppression mirror, sending limits
- mail-mta: RelayPool transport with OCI primary, optional secondary, health/back-off/quota failover; direct MTA kept but off
- mail-inbound: DSN/ARF/DMARC/TLS-RPT parsing + HMAC-signed POST /inbound/raw ingest (no port 25)
- workers/inbound-forwarder: Cloudflare Email Routing worker forwarding raw mail over HTTPS

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-13 01:14:52 +03:00

124 lines
4.1 KiB
SQL

-- Inbound MX storage: raw messages, bounces, complaints, DMARC and TLS reports.
CREATE TABLE IF NOT EXISTS inb_messages (
id UUID PRIMARY KEY,
kind TEXT NOT NULL, -- message | bounce | complaint | dmarc_report | tlsrpt_report | unparsed
domain TEXT NOT NULL,
mail_from TEXT NOT NULL,
rcpt_to TEXT[] NOT NULL,
remote_ip INET NOT NULL,
helo TEXT NOT NULL,
tls BOOLEAN NOT NULL,
spf TEXT,
dkim JSONB,
dmarc TEXT,
arc TEXT,
auth_results TEXT,
from_header TEXT,
subject TEXT,
message_id TEXT,
size INTEGER NOT NULL,
raw BYTEA NOT NULL,
received_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS inb_messages_domain_idx ON inb_messages (domain, received_at DESC);
CREATE INDEX IF NOT EXISTS inb_messages_kind_idx ON inb_messages (kind, received_at DESC);
CREATE TABLE IF NOT EXISTS inb_bounces (
id BIGSERIAL PRIMARY KEY,
mail_id TEXT NOT NULL,
hard BOOLEAN NOT NULL,
recipient TEXT NOT NULL,
action TEXT NOT NULL,
status TEXT NOT NULL,
diagnostic TEXT,
remote_mta TEXT,
reporting_mta TEXT,
reporter_ip INET NOT NULL,
received_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS inb_bounces_mail_id_idx ON inb_bounces (mail_id);
CREATE TABLE IF NOT EXISTS inb_complaints (
id BIGSERIAL PRIMARY KEY,
mail_id TEXT,
feedback_type TEXT NOT NULL,
user_agent TEXT,
source_ip TEXT,
original_mail_from TEXT,
original_rcpt_to TEXT,
arrival_date TEXT,
reported_domain TEXT,
original_message_id TEXT,
reporter TEXT NOT NULL,
received_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS inb_complaints_mail_id_idx ON inb_complaints (mail_id);
CREATE TABLE IF NOT EXISTS inb_dmarc_reports (
id BIGSERIAL PRIMARY KEY,
org_name TEXT NOT NULL,
email TEXT,
report_id TEXT NOT NULL,
domain TEXT NOT NULL,
begin_at TIMESTAMPTZ NOT NULL,
end_at TIMESTAMPTZ NOT NULL,
policy_p TEXT,
policy_sp TEXT,
adkim TEXT,
aspf TEXT,
pct INTEGER,
total BIGINT NOT NULL,
pass_count BIGINT NOT NULL,
fail_count BIGINT NOT NULL,
raw_xml TEXT NOT NULL,
received_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (org_name, report_id)
);
CREATE TABLE IF NOT EXISTS inb_dmarc_records (
id BIGSERIAL PRIMARY KEY,
report_id BIGINT NOT NULL REFERENCES inb_dmarc_reports(id) ON DELETE CASCADE,
source_ip TEXT NOT NULL,
count BIGINT NOT NULL,
disposition TEXT NOT NULL,
dkim_eval TEXT NOT NULL,
spf_eval TEXT NOT NULL,
header_from TEXT NOT NULL,
envelope_from TEXT,
auth_dkim JSONB NOT NULL,
auth_spf JSONB NOT NULL
);
CREATE INDEX IF NOT EXISTS inb_dmarc_records_report_idx ON inb_dmarc_records (report_id);
-- Per-domain, per-day rollup of DMARC outcomes as reported by receivers.
CREATE TABLE IF NOT EXISTS inb_dmarc_domain_stats (
domain TEXT NOT NULL,
day DATE NOT NULL,
total BIGINT NOT NULL DEFAULT 0,
pass_count BIGINT NOT NULL DEFAULT 0,
fail_count BIGINT NOT NULL DEFAULT 0,
dkim_pass BIGINT NOT NULL DEFAULT 0,
spf_pass BIGINT NOT NULL DEFAULT 0,
reports INTEGER NOT NULL DEFAULT 0,
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (domain, day)
);
CREATE TABLE IF NOT EXISTS inb_tlsrpt_reports (
id BIGSERIAL PRIMARY KEY,
org_name TEXT NOT NULL,
report_id TEXT NOT NULL,
domain TEXT,
start_at TEXT NOT NULL,
end_at TEXT NOT NULL,
success_count BIGINT NOT NULL,
failure_count BIGINT NOT NULL,
report JSONB NOT NULL,
received_at TIMESTAMPTZ NOT NULL DEFAULT now()
);