Karta. Domains
(Karta v1.0.0)
The domains organisations send from, and their DKIM keys.
What a verified domain permits. An organisation's servers may send mail whose author (From, and Sender if present) and envelope sender are at one of its verified domains, and only verified domains have their mail signed. Subdomains are separate domains. A name is verified for at most one organisation.
Proving ownership in DNS. An organisation verifies a domain by
publishing a TXT record with the domain's token (ownership_record/1:
_karta.<domain>, karta-verification=<token>); check_ownership/2
looks it up, without the DNS cache, and verifies the domain when it
finds it. Sending depends on verification, so a record that disappears
later does not stop mail at once: a domain verified by its record loses
verification only after checks have not found it for 7 days
(ownership_grace/0), and a lookup that fails changes nothing. An
administrator of the instance can still verify a domain without the
record (verify_domain/1); such a verification does not depend on it.
DKIM keys are generated here (generate_dkim_key/2), never taken
from input, and stored encrypted. A key signs only once it is activated
(activate_dkim_key/2), which needs a verified domain; activating a key
moves the domain's previous active key of the same algorithm to
:retiring. An active key is rotated (rotate_dkim_key/1) through a
pending key that Karta.Domains.Rotation activates once its record is
in DNS, and the key it replaces is retired a week later.
Every lookup is scoped by organisation (or by server, through its
organisation). Adding, verifying, unverifying or deleting a domain is
announced on its organisation's topic (:domains, Karta.Changes), and
every change of a domain, a check of its ownership record included, on
its own (:domain), as is every change of its keys (:dkim_keys).
Summary
Functions
Makes a key the active key of its algorithm at now; the previous one
becomes :retiring (its retiring_at set to now). Publish the key's
record before activating it (Karta.Domains.Rotation.activate/2 checks
that it is in DNS). Needs a verified domain.
The domain of an address in ASCII form; :error for address literals and non-domains.
Checks a message's author: every mailbox in From and the Sender must be at
a verified domain of the organisation. The addresses are parsed
(Karta.Mail.Authorisation), never matched as text, so a comment or a
display name that looks like an address cannot pass for one.
Looks up the domain's ownership record (ownership_record/1) without the
DNS cache and records the result (see Karta.Domains.Domain)
Adds an unverified domain to an organisation.
Deletes a domain and its keys.
The ids of the domains checked again every hour (Karta.Domains.Check):
every verified domain, and unverified domains added in the last 7 days
(so a record published soon after is found), those checked longest ago
first.
Generates a key for the domain, :pending until activated. attrs:
algorithm (rsa or ed25519), bits (RSA: 2048, 3072, 4096) and
selector (optional).
A domain of the organisation, or nil.
A domain by id, whatever its organisation (for jobs), or nil.
The domain's keys, newest first.
The organisation's domains, by name.
How long a domain verified by its record keeps verification once the record is gone.
The TXT record that proves the organisation controls the domain:
{"_karta.example.com", "karta-verification=<token>"}.
Retires a key: it no longer signs, and its record can be removed.
Starts a rotation of an active key: generates a pending key of the same
algorithm (and size) with a new selector, which replaces the active key
when it is activated (Karta.Domains.Rotation does that once the new
key's record has been in DNS for a while). One rotation of a key at a
time; retiring the pending key cancels it.
True when reverse_path (an SMTP envelope sender, or :null) may be used
by the organisation: the null path, or an address at a verified domain.
The keys that sign mail from domain (ASCII form) sent through the
server: the active keys of that domain, when it is a verified domain of
the server's organisation. Ed25519 first, then RSA.
Withdraws verification: the domain no longer sends or signs.
The organisation's verified domain named name (its ASCII form), or
nil.
The ASCII names of the organisation's verified domains.
Marks a domain verified by an administrator of the instance, who has
checked ownership by other means (the record of ownership_record/1 is
the usual way). Fails when another organisation has verified the name.
Functions
@spec activate_dkim_key(Karta.Domains.DKIMKey.t(), DateTime.t()) :: {:ok, Karta.Domains.DKIMKey.t()} | {:error, :domain_not_verified | :retired}
Makes a key the active key of its algorithm at now; the previous one
becomes :retiring (its retiring_at set to now). Publish the key's
record before activating it (Karta.Domains.Rotation.activate/2 checks
that it is in DNS). Needs a verified domain.
The domain of an address in ASCII form; :error for address literals and non-domains.
@spec check_from(Ecto.UUID.t(), [Karta.Mail.Header.t()]) :: :ok | {:error, Karta.Mail.Authorisation.reason()}
Checks a message's author: every mailbox in From and the Sender must be at
a verified domain of the organisation. The addresses are parsed
(Karta.Mail.Authorisation), never matched as text, so a comment or a
display name that looks like an address cannot pass for one.
@spec check_ownership(Karta.Domains.Domain.t(), keyword()) :: {:ok, Karta.Domains.Domain.t()}
Looks up the domain's ownership record (ownership_record/1) without the
DNS cache and records the result (see Karta.Domains.Domain):
- found on a domain that is not verified: the domain is verified by
DNS, unless another organisation has verified the name (
:taken); - found on a domain verified by its record: it stays verified;
- not found on a domain verified by its record: it stays verified until
checks have not found the record for
ownership_grace/0, then verification is withdrawn; - a lookup that fails, and anything about a domain an administrator verified, changes no verification.
Options: dns (options for Karta.DNS.lookup/3) and now.
@spec create_domain(Karta.Accounts.Organisation.t(), map()) :: {:ok, Karta.Domains.Domain.t()} | {:error, Ecto.Changeset.t()}
Adds an unverified domain to an organisation.
@spec delete_domain(Karta.Domains.Domain.t()) :: {:ok, Karta.Domains.Domain.t()} | {:error, Ecto.Changeset.t()}
Deletes a domain and its keys.
@spec due_for_check(DateTime.t()) :: [Ecto.UUID.t()]
The ids of the domains checked again every hour (Karta.Domains.Check):
every verified domain, and unverified domains added in the last 7 days
(so a record published soon after is found), those checked longest ago
first.
@spec generate_dkim_key(Karta.Domains.Domain.t(), map()) :: {:ok, Karta.Domains.DKIMKey.t()} | {:error, Ecto.Changeset.t()}
Generates a key for the domain, :pending until activated. attrs:
algorithm (rsa or ed25519), bits (RSA: 2048, 3072, 4096) and
selector (optional).
@spec get_domain(Karta.Accounts.Organisation.t(), term()) :: Karta.Domains.Domain.t() | nil
A domain of the organisation, or nil.
@spec get_domain_by_id(Ecto.UUID.t()) :: Karta.Domains.Domain.t() | nil
A domain by id, whatever its organisation (for jobs), or nil.
@spec list_dkim_keys(Karta.Domains.Domain.t()) :: [Karta.Domains.DKIMKey.t()]
The domain's keys, newest first.
@spec list_domains(Karta.Accounts.Organisation.t()) :: [Karta.Domains.Domain.t()]
The organisation's domains, by name.
@spec ownership_grace() :: Duration.t()
How long a domain verified by its record keeps verification once the record is gone.
@spec ownership_record(Karta.Domains.Domain.t()) :: {String.t(), String.t()}
The TXT record that proves the organisation controls the domain:
{"_karta.example.com", "karta-verification=<token>"}.
@spec retire_dkim_key(Karta.Domains.DKIMKey.t()) :: {:ok, Karta.Domains.DKIMKey.t()} | {:error, Ecto.Changeset.t()}
Retires a key: it no longer signs, and its record can be removed.
@spec rotate_dkim_key(Karta.Domains.DKIMKey.t()) :: {:ok, Karta.Domains.DKIMKey.t()} | {:error, :not_active | :rotating}
Starts a rotation of an active key: generates a pending key of the same
algorithm (and size) with a new selector, which replaces the active key
when it is activated (Karta.Domains.Rotation does that once the new
key's record has been in DNS for a while). One rotation of a key at a
time; retiring the pending key cancels it.
@spec sender_allowed?(Ecto.UUID.t(), :null | String.t()) :: boolean()
True when reverse_path (an SMTP envelope sender, or :null) may be used
by the organisation: the null path, or an address at a verified domain.
@spec signing_keys(Ecto.UUID.t(), String.t()) :: [Karta.Domains.DKIMKey.t()]
The keys that sign mail from domain (ASCII form) sent through the
server: the active keys of that domain, when it is a verified domain of
the server's organisation. Ed25519 first, then RSA.
@spec unverify_domain(Karta.Domains.Domain.t()) :: {:ok, Karta.Domains.Domain.t()} | {:error, Ecto.Changeset.t()}
Withdraws verification: the domain no longer sends or signs.
@spec verified_domain(Ecto.UUID.t(), String.t()) :: Karta.Domains.Domain.t() | nil
The organisation's verified domain named name (its ASCII form), or
nil.
@spec verified_names(Ecto.UUID.t()) :: MapSet.t(String.t())
The ASCII names of the organisation's verified domains.
@spec verify_domain(Karta.Domains.Domain.t()) :: {:ok, Karta.Domains.Domain.t()} | {:error, Ecto.Changeset.t()}
Marks a domain verified by an administrator of the instance, who has
checked ownership by other means (the record of ownership_record/1 is
the usual way). Fails when another organisation has verified the name.