115 Commits

Author SHA1 Message Date
Alberto Bertogli
ac7f32c2ce smtpsrv: Implement a post-DATA hook
This patch implements a post-DATA hook, which is run after receiving the
data but before sending a reply.

It can be used to implement content filtering when receiving email, for
example for passing the email through an anti-spam or an anti-virus.
2016-10-21 22:18:53 +01:00
Alberto Bertogli
b8c0ac98f4 chasquid: Split Server and Conn
This patch moves chasquid's Server and Conn structures to their own
smtpsrv package, to make chasquid.go a bit more readable. It also helps
clarify the relation between Server and Conn.

There are no functional changes.

Note that git can still track the history across this commit (e.g. git
gui blame shows the right data).
2016-10-21 22:18:48 +01:00
Alberto Bertogli
c013c98283 domaininfo: New package to track domain (security) information
This patch introduces a new "domaininfo" package, which implements a
database with information about domains.  In particular, it tracks
incoming and outgoing security levels.

That information is used in incoming and outgoing SMTP to prevent
downgrades.
2016-10-21 22:15:09 +01:00
Alberto Bertogli
1d7a207e00 Minor code aesthetic improvements, based on vet+fmt+lint
This patch is the result of running go vet, go fmt -s and the linter,
and fixing some of the things they noted/suggested.

There shouldn't be any significant logic changes, it's mostly
readability improvements.
2016-10-21 22:13:39 +01:00
Alberto Bertogli
c172894317 chasquid: Update the monitoring HTML index
Add some links to the monitoring HTML index, to reflect the new
additions.

Also reorder to make it more practical, and default to the expanded view
for the tracer links.
2016-10-21 22:13:39 +01:00
Alberto Bertogli
dd35085a32 Remove obsolete TODOs 2016-10-21 22:13:39 +01:00
Alberto Bertogli
a7afa08e7c chasquid: Keep an AUTH event log
This patch adds an AUTH event log, to help troubleshoot issues with
authentication.
2016-10-21 22:13:39 +01:00
Alberto Bertogli
6747f0785e chasquid: Add an expvar to count incoming TLS status 2016-10-21 22:13:39 +01:00
Alberto Bertogli
eadd0ffc89 chasquid: Track and enforce the HELO/EHLO address
HELO and EHLO both take a mandatory parameter, which also should be used
in the Received header.

This patch tracks and enforces that parameter, and also updates the
Received header generation to use it.

https://tools.ietf.org/html/rfc5321#section-4.4
2016-10-21 22:13:39 +01:00
Alberto Bertogli
008cd98e39 chasquid: Break connections after 10 errors
If a connection has accumulated 10 errors, it's very likely that
something has gone significantly wrong, or they're just probing/abusing
the service.

This patch makes chasquid break the connection after 10 errors.
The number is arbitrary, we may adjust it later.
2016-10-21 22:13:39 +01:00
Alberto Bertogli
112e492c3a Normalize domains
We should ignore the domains' case, and treat them uniformly, specially when it
comes to local domains.

This patch extends the existing normalization (IDNA, keeping domains as
UTF8 internally) to include case conversion and NFC form for
consistency.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
ad25706d72 Normalize local usernames using PRECIS
This patch implements local username normalization using PRECIS
(https://tools.ietf.org/html/rfc7564,
https://tools.ietf.org/html/rfc7613)

It makes chasquid accept local email and authentication regardless of
the case. It covers both userdb and aliases.

Note that non-local usernames remain untouched.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
3eac221a7f chasquid: Make the "Received:" header RFC compliant
The Received header has some predefined structure and valid keywords,
this patch adjust how we create them to be compliant with that
structure.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
bc2b3b40a5 chasquid: Skip SPF if the connection has authenticated
Currently, we do SPF checks for all connections.

However, authenticated users will be sending email from different
locations, applying SPF to them will result in false positives.

So this patch makes chasquid skip SPF checking if the connection is
authenticated.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
5b0501e584 Tidy up MAIL FROM and RCPT TO address handling
This patch has small aesthetic changes to make the address handling in
MAIL FROM and RCPT TO a bit more readable.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
40153e352f chasquid: Detect email loops
This patch implements some measures against email loops, such as keeping
a limit on the lenght of an address, and rejecting email that has too
many Received headers.

It's not perfect (a server could be actively removing Received headers),
but it should cover the normal accidents and misconfigurations.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
c4e8b22fd0 Introduce expvar counters
This patch introduces expvar counters to chasquid and the queue
packages.

For now there's only a handful of counters, but they will be expanded in
future patches.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
08a5d19941 Add missing Close calls
The SMTP courier was not properly closing the connection, and chasquid's
closing of incoming connections was not ideal (it was closing the
underlying one, not necessarily the active one, like in the case of a jump
to TLS).

This patch fixes both by adding the missing calls to Close.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
dbe3843e12 chasquid: Redact AUTH parameters in debug logs 2016-10-10 00:51:05 +01:00
Alberto Bertogli
7403dbb8a3 chasquid: Minor fixes to MAIL FROM and RCPT TO handling
This patch tidies up the MAIL FROM and RCPT TO handling, in particular:

 - Preserve the case on received email. It could be outgoing and we
   should not change it.
 - Accept (but ignore) RCPT TO options, instead of failing.
 - Fix some error codes to make them follow the RFC.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
3e6dd12d06 Improve debugging and tracing information
This patch reviews various debug and informational messages, making more
uniform use of tracing, and extends the monitoring http server with
useful information like an index and a queue dump.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
2b801a84d1 chasquid: Add SPF checks
This patch makes chasquid perform SPF checks, and add the corresponding
Received-SPF header.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
6dda2fff4b chasquid: Introduce support for SMTPUTF8
This patch adds initial support for SMTPUTF8, which for now consists of just
advertising it.

We support most of it, but sending emails over SMTP requires further work, as
the SMTP courier does not support this yet (it's not in Go's standard
library). That will come in subsequent patches, along with IDNA handling.

https://tools.ietf.org/html/rfc6531.html
2016-10-10 00:51:04 +01:00
Alberto Bertogli
f767b83fe0 Implement basic IDNA support
This patch implements the first steps of support for IDNA (Internationalized
Domain Names for Applications).

Internally, we maintain the byte-agnostic representation, including
configuration.

We support receiving IDNA mail over SMTP, which we convert to unicode for
internal handling.

Local deliveries are still kept agnostic.

For sending over SMTP, we use IDNA for DNS resolution, but there are some
corner cases pending in the SMTP courier that are tied with SMTPUTF8 and will
be fixed in subsequent patches.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
280939c3ec chasquid: Don't enforce authentication at MAIL FROM
There are cases, like email bounces and forwarding, where a remote server may
use an address within our domain as "MAIL FROM".

The current test at MAIL FROM will block them, which can be quite an
inconvenience as those cases are not that rare.

It's a nice test but doesn't add much, as we don't really pass the validation
along, and we still do relay and user checks on RCPT TO.

So this patch removes that test.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
d97a4047c1 chasquid: Use human-readable TLS constants in addReceivedHeader
Make the "Received" header a bit more human-friendly by using the newly
introduced tlsconst package.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
e138f0dc05 chasquid: De-couple TLS certificates from domains
Having the certificates inside the domain directory may cause some confusion,
as it's possible they're not for the same name (they should be for the MX we
serve as, not the domain itself).

So it's not a problem if we have domains with no certificates (we could be
their MX with another name), and we could have more than one certificate per
"domain" (if we act as MXs with different names).

So this patch moves the certificates out of the domains into a new certs/
directory, where we do a one-level deep lookup for the files.

While at it, change the names of the files to "fullchain.pem" and
"privkey.pem", which match the names generated by the letsencrypt client, to
make it easier to set up.  There's no general convention for these names
anyway.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
04dd8b9534 Remove unreachable code, and don't leak contexts
This patch performs some minor cleanups for things detected by "go vet":

 - Remove one line of unreachable code.
 - Don't leak contexts until their deadline expires, cancel them.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
0995eac474 chasquid: Fail at RCPT TO time if a user does not exist
It's more convenient and in line with standard practice to fail RCPT TO if the
user does not exist.

This involves making the server and client aware of aliases, but it doesn't
end up being very convoluted, and simplifies other code.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
1433340ea6 chasquid: Periodically reload the user databases
This patch makes chasquid periodically reload the user databases, in line with
what we do for aliases.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
a5ea6c9ba0 aliases: Add files unconditionally, to allow for reloads
Today, if the aliases file does not exist when chasquid starts up, the entire
domain will be skipped from aliases resolution.

That's a bug, as it means we don't perform character and suffix replacements
for known domains, and is also an inconvenience as it forces us to reload the
daemon when adding a file for a known domain.

This patch fixes this by adding them unconditionally, even if the file does
not exist.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
1d3675a133 queue: Send delivery status notifications on failures
When we permanently failed to deliver to one or more recipients, send delivery
status notifications back to the sender.

To do this, we need to extend a couple of internal structures, to keep track
of the original destinations (so we can include them in the message, for
reference), and the hostname we're identifying ourselves as (this is arguable
but we're going with it for now, may change later).
2016-10-10 00:51:04 +01:00
Alberto Bertogli
0dc93d1ec6 chasquid: Remove overly verbose error about TLS config 2016-10-10 00:51:04 +01:00
Alberto Bertogli
711459b798 chasquid: Remove support for test keys
The test key flags have not been used in a while, so this patch removes
them, which simplifies the initialization code a little bit.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
3d06fb3a78 Turn chasquid-userdb into chasquid-util
This patch removes chasquid-userdb and adds a more generic and extensive
chasquid-util, that supports various operations on user databases as well as
aliases lookups.

The code is not very pretty but for now I took a more practical approach, the
tool is ancillary and can be tidied up later.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
667358d72e courier: Tidy up the Procmail courier
This patch tidies up the Procmail courier:
 - Move the configuration options to the courier instance, instead of using
   global variables.
 - Implement more useful string replacement options.
 - Use exec.CommandContext for running the command with a timeout.

As a consequence of the first item, the queue now takes the couriers via its
constructor.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
d05b8ef189 chasquid: Add "Received" email headers
This patch makes chasquid add "Received" email headers, after DATA completes
and before queueing.

We only use this for debugging purposes.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
c34c073c1c chasquid: Load and resolve aliases
This patch integrates the aliases resolution into the daemon and queue.

The queue now will resolve aliases at Put time.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
aacf8ffea7 queue: Implement persistency
This patch makes the queue read and write items to disk.

It uses protobuf for serialization. We serialize to text format to make
manual troubleshooting easier, as the performance difference is not very
relevant for us.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
e9d62e0467 chasquid: Do a Chdir into the configuration directory
In some cases, it's be useful to have references to directories relative
to the configuration itself.

So this patch makes chasquid do a Chdir into it, so we can assume it in
the rest of the code and config.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
394067bbd3 userdb: Use protocol buffers instead of our custom format
Protocol buffers are a more portable, practical and safe format for the user
database.
2016-10-10 00:51:03 +01:00
Alberto Bertogli
e2fdcb3705 Add checks to prevent unauthorized relaying and impersonation
This patch adds checks that verify:

 - The envelope from must match the authenticated user. This prevents
   impersonation at the envelope level (while still allowing bounces, of
   course).
 - If the destination is remote, then the user must have completed
   authentication. This prevents unauthorized relaying.

The patch ends up adjusting quite a few tests, as they were not written
considering these restrictions so they have to be changed accordingly.
2016-10-10 00:50:56 +01:00
Alberto Bertogli
941eb9315c Distinguish between SMTP and submission ports
We want to be able to distinguish between connections for SMTP and connections
for submission, so we can make different policy decisions.

To do that, we first make the configuration aware of the different kinds of
addresses. This is done in this patch in a backwards-incompatible way, but at
this point in time it is ok to do so.

Then, we extend systemd's socket passing library to support socket naming, so
we can tell the different sockets apart. This is done via the
LISTEN_FDNAMES/FileDescriptorName mechanism.

And finally we make the server and connection types aware of the socket mode.
2016-10-10 00:50:24 +01:00
Alberto Bertogli
aac2d3c061 Minor style and simplification cleanups
This patch does various minor style and simplification cleanups, fixing things
detected by tools such as go vet, gofmt -s, and golint.

There are no functional changes, this change is purely cosmetic, but will
enable us to run those tools more regularly now that their output is clean.
2016-09-12 04:06:56 +01:00
Alberto Bertogli
7eba9bb4f7 chasquid: Flush logs periodically and before exiting
This patch makes chasquid flush logs periodically, and also before exiting,
even if it gets killed via the usual signals (including Ctrl-C).

Both things help troubleshooting when there is not much traffic, or we kill
the server manually (including during automated tests).
2016-08-01 22:56:57 +01:00
Alberto Bertogli
ae1c246bde chasquid: Make "MAIL FROM" ignore extra parameters
MAIL FROM commands usually come in the form of:

  MAIL FROM:<from@from> BODY=8BITMIME

Note that there's extra parameters after the address, which for now we want to
ignore.

The current parser doesn't ignore them, and relies on mail.ParseAddress doing
so (that is, on mail.ParseAddress("<from> BODY=8BITMIME") working).
However, in go 1.7, the parser will get more strict and start to fail these
cases.

To fix this, we change the way we parse the line to use fmt.Sprintf, which is
much nicer than splitting by hand, and is more readable as well.
2016-07-22 01:44:45 +01:00
Alberto Bertogli
9d172a6ea0 Make the queue aware of local and remote couriers
The routing courier is a nice idea in theory, but at least for now, we want
the queue to be aware of when a destination is local so we can implement
differentiated logic.

This may change in the future, though, but at the moment it's not clear that
the abstractions will be worth it.

So this patch removes it, and makes the queue do the routing. There is no
difference in how the two are handled yet, those will come in subsequent
patches.
2016-07-22 01:44:45 +01:00
Alberto Bertogli
21e69aa42f Implement AUTH
This patch implements the AUTH SMTP command, using per-domain user databases.

Note that we don't really use or check the validation for anything, this is
just implementing the command itself.
2016-07-22 01:44:45 +01:00
Alberto Bertogli
ff103c18c3 courier: Let the users configure the mail delivery agent
This patch adds configuration options for the MDA binary and command line
arguments, and changes the (soon to be renamed) procmail courier to make use
of them.
2016-07-16 12:33:51 +01:00
Alberto Bertogli
7e7c8073c4 Implement VRFY and EXPN commands
We implement the VRFY and EXPN commands, but as no-ops. The RFC allows this,
and most implementations seem to do it this way too.

While at it, merge the tests for simple commands like these into one.
2016-07-16 12:33:50 +01:00