285 Commits

Author SHA1 Message Date
Alberto Bertogli
54cce0c2bf test: Reduce the loop detection threshold from 50 down to 5
The loop test can be quite slow, specially on computers without
cryptography-friendly instructions.

This patch introduces a new flag for testing, so that we can bring the
threshold down to 5. The test is just as useful but now runs in a few
seconds, as opposed to a few minutes.
2016-11-01 23:56:04 +00:00
Alberto Bertogli
8cbc4f9ca6 queue: Use a PRNG to generate IDs
The queue IDs are internal to chasquid, and we don't need them to be
cryptographically secure, so this patch changes the ID generation to use
the PRNG.

This also helps avoid entropy issues.
2016-11-01 23:56:04 +00:00
Alberto Bertogli
1bc111f783 Improve the readability of some log messages
This patch contains a few changes to logging messages, to improve log
readability.

While at it, an obsolete TODO in the SMTP courier is removed.
2016-11-01 23:56:04 +00:00
Alberto Bertogli
60a7932bd3 log: Replace glog with a new logging module
glog works fine and has great features, but it does not play along well
with systemd or standard log rotators (as it does the rotation itself).

So this patch replaces glog with a new logging module "log", which by
default logs to stderr, in a systemd-friendly manner.

Logging to files or syslog is still supported.
2016-11-01 23:56:04 +00:00
Alberto Bertogli
6640c09b9e smtpsrv: Always pass the environment variables to the hook
This patch makes the hooks always have a complete set of environment
varuables, set to 0/1 or whatever is appropriate, to make it easier to
write the checks for them.
2016-10-22 18:55:09 +01:00
Alberto Bertogli
14c48153aa smtpsrv: If the hook exits with code 20, it's a permanent failure
It is can be convenient for hooks to indicate that an error is
permanent; for example if the anti-virus found something.

This patch makes it so that if the hook exits with code 20, then it's
considered permanent. Otherwise it is considered transient, to help
prevent accidental errors cause final delivery issues.
2016-10-22 18:55:02 +01:00
Alberto Bertogli
38b4e76090 spf: Support the "ptr" mechanism
The "ptr" mechanism should not be used, but unfortunately many domains
still do.

This patch extends our SPF implementation to support checking it.

https://tools.ietf.org/html/rfc7208#section-5.5
2016-10-22 00:07:07 +01:00
Alberto Bertogli
c00648b4ae queue: Calculate next delay based on creation time
Calculating the next delay based on the previous delay causes daemon
restarts to start from scratch, as we don't persist it.

This can cause a few server restarts to generate many unnecessary sends.

This patch changes the next delay calculation to use the creation time
instead, and also adds a <=1m random perturbation to avoid all queued
emails to be retried at the exact same time after a restart.
2016-10-21 22:20:49 +01:00
Alberto Bertogli
8e9e4eddc5 queue: Rewrite the aliases test to remove races
TestAliases is unfortunately racy, and cause occasional failures.

This patch rewrites it to be more end-to-end, similar to TestBasic,
which should remove the races while keeping the main objectives of the
test.
2016-10-21 22:20:49 +01:00
Alberto Bertogli
d660f88f67 queue: Send DSN for messages that time out in the queue
The queue currently only considers failed recipients when deciding
whether to send a DSN or not. This is a bug, as recipients that time out
are not taken into account.

This patch fixes that issue by including both failed and pending
recipients in the DSN.

It also adds more comprehensive tests for this case, both in the queue
and in the dsn generation code.
2016-10-21 22:20:49 +01:00
Alberto Bertogli
febe96697a maillog: Introduce a special-purpose mail logging package
The default INFO logs are more oriented towards debugging and can be
a bit too verbose when looking for high-level information.

This patch introduces a new "maillog" package, used to log messages of
particular relevance to mail transmission at a higher level.
2016-10-21 22:20:47 +01:00
Alberto Bertogli
cda11e03a5 safeio: Preserve file ownership
This patch makes safeio preserve file ownership. This is specially
useful when using command-line utilities as root, but the files they
change are owned by a different user.
2016-10-21 22:18:53 +01:00
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
5faffbbfe3 courier: Simplify procmail's execution logic
The way the procmail courier runs the command is unnecessary convoluted,
this patch simplifies it by using the corresponding standard tools.
2016-10-21 22:18:53 +01:00
Alberto Bertogli
88868d7d8e smtpsrv: Sanitize unknown commands
Unknown commands can fill the logs, traces and expvars with a lot of
noise; this patch sanitizes them a bit down to 6 bytes, as a compromise
to maintain some information for troubleshooting.
2016-10-21 22:18:53 +01:00
Alberto Bertogli
f5e466eac5 smtpsrv: Include the mode in the Received header
Including the mode (SMTP/submission) in the Received header can be
useful for troubleshooting transport issues.
2016-10-21 22:18:53 +01:00
Alberto Bertogli
c2d0d5f705 smtpsrv: Only allow authenticated email on submission
The submission port is expected to be used only by authenticated
clients, so this patch makes chasquid enforce this, which also helps
to reduce spam.

https://www.rfc-editor.org/rfc/rfc6409.txt
2016-10-21 22:18:53 +01:00
Alberto Bertogli
fe146f00e5 queue: Always use the main domain for sending DSNs
Today, we pick the domain used to send the DSN from based on what we
presented to the client at EHLO time, which itself may be based on the
TLS negotiation (which is not necessarily trusted).

This is complex, not necessarily correct, and involves passing the
domain around through the queue and persisting it in the items.

So this patch simplifies that handling by always using the main domain
as specified by the configuration.
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
a5e6e197a6 trace: Quote errors before logging them
Errors can contain newlines, in particular this is common in messages
returned by remote SMTP servers.

This patch quotes them before logging, they don't interfere with the
log. Note the tracer itself can handle this just fine.
2016-10-21 22:13:39 +01:00
Alberto Bertogli
09d3c73f6c queue: Simplify sending loop
This patch simplifies the sending loop code:

 - Move the recipient sending function from a closure to a method.
 - Simplify the status update logic: we now update and write
   unconditionally (as we should have been doing).
 - Create a function for counting recipients in a given status.

It also adds a test for the removal of completed items from the queue,
which was not covered before and came up during development.
2016-10-21 22:13:39 +01:00
Alberto Bertogli
dd35085a32 Remove obsolete TODOs 2016-10-21 22:13:39 +01:00
Alberto Bertogli
aa0486b54e protoio: Add a generic protobuf store
This patch adds a generic protobuf store, where one can put and retreive
protobufs, indexed by a string.

This will be used in subsequent patches.
2016-10-10 01:32:55 +01:00
Alberto Bertogli
cf36003e3a trace: Add an EventLog
This patch adds an EventLog wrapper to the trace module, which will be
used in the future to track long-lived objects.
2016-10-10 00:51:05 +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
220b5d20ff trace: Set max events to 30 by default
The default for max events is 10, which is a bit short for a normal SMTP
exchange. Expand it to 30 which should be large enough to keep most of
the traces.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
2b46f300ca trace: Include debug messages in the traces
We don't want to write debug messages to the log, but having them in
traces is always useful.

The traces are volatile and self-cleaning so the additional volume
should not cause any problems, and helps troubleshooting.

While at it, fix the depth constant when logging.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
3e55b0d742 courier/smtp: Reorder EHLO domain logic
The logic that picks the domain we use for EHLO does not need to live
within the TLS retry cycle, and makes it harder to understand.

This patch moves it out of the way, to improve readability.
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
641406cede queue: Fix race in tests
The test courier has a racy map access, and this started to manifest
after some recent changes. This patch fixes the race by implementing the
corresponding locks.
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
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
3d3b771b80 internal/spf: Add an SPF package
This patch adds a package for evaluating SPF, as defined by RFC 7208
(https://tools.ietf.org/html/rfc7208).

It doesn't implement 100% of the RFC, but it coves enough to handle the
most common cases, and will fail open on the others.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
498bb29585 config: Set defaults for suffix_separators and drop_characters
This patch sets common and practical defaults for the suffix_separators ("+")
and drop_character options (".").
2016-10-10 00:51:05 +01:00
Alberto Bertogli
55b03c8cf0 queue: Use a local envelope-from when forwarding
If there's an alias to forward email to a non-local domain, using the original
From is problematic, as we may not be an authorized sender for it.

Some MTAs (like Exim) will do it anyway, others (like gmail) will construct a
special address based on the original address.

This patch implements the latter approach, which is safer and allows the
receiver to properly enforce SPF.

We construct a (hopefully) reasonable From based on the local user, and
embedding the original From (but transformed for IDNA, as the receiver may not
support SMTPUTF8).
2016-10-10 00:51:05 +01:00
Alberto Bertogli
afd79dfd8d courier: Always have a domain for EHLO/HELO
In the SMTP courier, we should always include a domain when saying hello, as
many MTAs will be pick about it.

An empty domain can happen if the envelope-from is <>. In that case, we fall
back to our hostname.
2016-10-10 00:51:05 +01:00
Alberto Bertogli
0e41821779 smtp: Distinguish permanent errors
This patch makes the SMTP courier distinguish permanent errors, so the queue
can decide whether to retry or not.

It's based on the SMTP replies: 5xy is considerent permanent, anything else is
transient (including errors other than protocol issues).
2016-10-10 00:51:04 +01:00
Alberto Bertogli
7cbe6a5076 courier: Support SMTPUTF8 in the SMTP courier
This patch adds SMTPUTF8 to the SMTP courier.

It introduces a new internal/smtp package that extends Go's net/smtp with
SMTPUTF8 (in a very narrow way, not for general use), and then makes the
courier use it.

Also use an IDNA-safe version when saying hello, otherwise servers could
complain if the hostname is not ASCII, and at that stage we don't know if they
support it or not.
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
7fa40397c5 courier: SMTP falls back to A when MX does not exist
Make the SMTP courier fall back to the A record when MX does not exist, as per
standard behaviour.

This is not implemented nicely, because Go's API does not give a clear signal
if the answer was that there are no MX records or something else happens.
For now, we implement it with a heuristic that should work pretty reliably,
but it's definitely not very nice.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
fdaca0bedf courier: Fix small typo in smtp.go's trace 2016-10-10 00:51:04 +01:00
Alberto Bertogli
2f2d1f2dbd courier: Use the senders' domain when saying EHLO
Some servers, like postfix, will pay close attention to the domain we say as a
part of the EHLO.

By default, Go's smtp package will use "localhost", causing it to complain.

This patch fixes that by using the envelope-from's domain.
It's not clear if that's better than using what we are resolving to, but
that's much more involved so we're going to do this for now.
2016-10-10 00:51:04 +01:00
Alberto Bertogli
16d9d45e06 internal/tlsconst: Add a package with TLS constants
There are a couple of places where it's handy to print TLS constants in
human-readable form.

To do so, we need functions that take TLS constants (usually in uint16 form)
and give us friendly strings.

Go's crypto/tls package does not provide us with this, so this patch
introduces a new module with that purpose.
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
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