Karta.MTASTS.Hosting (Karta v1.0.0)

MTA-STS policies that Karta serves for sending domains (Karta.MTASTS.HostedPolicy), so that a domain can have one without a web server of its own.

What the domain publishes (records/2): a CNAME record from mta-sts.<domain> to Karta's public host (Karta.Tracking.Hosts.target/0, the name tracking hosts point to), and the TXT record _mta-sts.<domain> with v=STSv1; id=<id>;. The id (id/1) is taken from the policy's text, so it changes whenever the policy changes, and comes back when a policy is set back to what it was (senders then fetch it again, which is what they must do).

Serving (served/1): https://mta-sts.<domain>/.well-known/mta-sts.txt on the tracking endpoint, the policy's text (text/1), while the policy is in use: its domain is verified and the CNAME record was found by the last check (check_host/2, every hour and on request; a failed lookup changes nothing). The reverse proxy gets a certificate for exactly those names (approved?/1, names_in_use/0), as for tracking hosts.

Safety. A policy in enforce mode makes senders that follow it refuse to deliver to any MX host not in it, or whose TLS fails, so save/3 checks first (readiness/3): every MX host of the domain must be in the policy and accept STARTTLS with a certificate valid for its name (Karta.MTASTS.Probe). A policy is removed only once it is in mode none (remove/1): senders keep a policy for max_age and would keep enforcing it without being able to fetch a newer one.

Saving, checking and removing a policy is announced on its domain's topic (:mta_sts, Karta.Changes).

Summary

Functions

True when the reverse proxy may get a certificate for name: a policy host in use.

A changeset for the domain's policy form: its policy, or a new one in testing mode for a week with mx (the domain's MX hosts, see mx_hosts/2).

Looks up the CNAME record at mta-sts.<domain> and records the result (as Karta.Tracking.Hosts.check/2 does for tracking hosts). Options: dns, target (instead of Karta.Tracking.Hosts.target/0) and now.

The domain's policy, or nil.

The policy's id: 20 hexadecimal digits of the SHA-256 digest of its text.

The names of the domain's MX hosts, by preference (no null MX), or {:error, text} when the lookup failed. dns: options for Karta.DNS.lookup/3; the cache is never used.

The policy hosts in use, by name: none without a target.

Whether senders that enforce a policy with these mx patterns can deliver to the domain: it has MX hosts, each is in the patterns and passes Karta.MTASTS.Probe.check/2. :ok, or {:error, problems}, a sentence each. Options: dns (for the MX lookup and the probes) and probe (options of Karta.MTASTS.Probe.check/2).

The records the domain publishes for its policy: the CNAME record of its policy host (when a target is configured) and the TXT record with the policy's id. Each %{type, name, value}.

Removes the domain's policy once it is in mode none ({:error, :not_withdrawn} before).

Creates or changes the domain's policy (attrs as for Karta.MTASTS.HostedPolicy.changeset/2), only for a verified domain. A policy in enforce mode is saved only when readiness/3 finds no problem: otherwise {:error, {:not_ready, problems}}. Options: dns and probe (for readiness/3).

The policy text Karta serves at host (mta-sts.<domain>, any case, with or without a final dot), or nil when no policy is in use there.

The policy's text, as served (CRLF line endings).

Functions

approved?(name)

@spec approved?(term()) :: boolean()

True when the reverse proxy may get a certificate for name: a policy host in use.

change(domain, policy, mx \\ [], attrs \\ %{})

A changeset for the domain's policy form: its policy, or a new one in testing mode for a week with mx (the domain's MX hosts, see mx_hosts/2).

check_host(policy, opts \\ [])

@spec check_host(Karta.MTASTS.HostedPolicy.t(), keyword()) ::
  {:ok, Karta.MTASTS.HostedPolicy.t()}
  | {:error, :not_configured | Ecto.Changeset.t()}

Looks up the CNAME record at mta-sts.<domain> and records the result (as Karta.Tracking.Hosts.check/2 does for tracking hosts). Options: dns, target (instead of Karta.Tracking.Hosts.target/0) and now.

get(domain)

The domain's policy, or nil.

id(policy)

The policy's id: 20 hexadecimal digits of the SHA-256 digest of its text.

mx_hosts(domain_name, dns \\ [])

@spec mx_hosts(String.t(), keyword()) :: {:ok, [String.t()]} | {:error, String.t()}

The names of the domain's MX hosts, by preference (no null MX), or {:error, text} when the lookup failed. dns: options for Karta.DNS.lookup/3; the cache is never used.

names_in_use()

@spec names_in_use() :: [String.t()]

The policy hosts in use, by name: none without a target.

readiness(domain_name, mx, opts \\ [])

@spec readiness(String.t(), [String.t()], keyword()) :: :ok | {:error, [String.t()]}

Whether senders that enforce a policy with these mx patterns can deliver to the domain: it has MX hosts, each is in the patterns and passes Karta.MTASTS.Probe.check/2. :ok, or {:error, problems}, a sentence each. Options: dns (for the MX lookup and the probes) and probe (options of Karta.MTASTS.Probe.check/2).

records(domain, policy)

@spec records(Karta.Domains.Domain.t(), Karta.MTASTS.HostedPolicy.t()) :: [
  %{type: String.t(), name: String.t(), value: String.t()}
]

The records the domain publishes for its policy: the CNAME record of its policy host (when a target is configured) and the TXT record with the policy's id. Each %{type, name, value}.

remove(policy)

@spec remove(Karta.MTASTS.HostedPolicy.t()) ::
  {:ok, Karta.MTASTS.HostedPolicy.t()}
  | {:error, :not_withdrawn | Ecto.Changeset.t()}

Removes the domain's policy once it is in mode none ({:error, :not_withdrawn} before).

save(domain, attrs, opts \\ [])

@spec save(Karta.Domains.Domain.t(), map(), keyword()) ::
  {:ok, Karta.MTASTS.HostedPolicy.t()}
  | {:error,
     Ecto.Changeset.t() | :domain_not_verified | {:not_ready, [String.t()]}}

Creates or changes the domain's policy (attrs as for Karta.MTASTS.HostedPolicy.changeset/2), only for a verified domain. A policy in enforce mode is saved only when readiness/3 finds no problem: otherwise {:error, {:not_ready, problems}}. Options: dns and probe (for readiness/3).

served(host)

@spec served(term()) :: String.t() | nil

The policy text Karta serves at host (mta-sts.<domain>, any case, with or without a final dot), or nil when no policy is in use there.

text(policy)

The policy's text, as served (CRLF line endings).