636 Commits

Author SHA1 Message Date
Alberto Bertogli
cf2b8ade65 etc: Add a skeleton configuration structure
This patch adds an "etc" directory, with a basic configuration structure
that can be used for an initial installation.
2016-11-01 23:56:04 +00:00
Alberto Bertogli
3865dd0ea4 smtp-check: Check SPF and be verbose about errors 2016-11-01 23:56:04 +00:00
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
60ed30e95a Add a Makefile
This patch introduces a Makefile to make it easier to build with version
information, and run all the tests together.

It's just for convenience, plain go commands continue to work just fine.
2016-11-01 23:55:56 +00:00
Alberto Bertogli
c87c5ec1bc chasquid: Include build and version information
This patch adds the possibility of setting build and version
information, which will be exported and displayed in the monitoring http
server.
2016-10-26 22:25:12 +01:00
Alberto Bertogli
67fe5b50a5 hooks: Add greylisting to the post-data hook
While greylisting has severe drawbacks, it's reasonably common.
This patch extends the default hook for greylisting, using "greylistd".

Completing authentication or passing SPF will make it skip the greylist
check, to avoid some of the most common issues with it.
2016-10-26 22:25:12 +01: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
6f048027a7 test: Readability cleanup
This patch makes a few small changes to the tests for readability, such
as changing the arguments to the add_user function.
2016-10-21 22:20:49 +01:00
Alberto Bertogli
f38ae47164 chasquid-util: Make the commands more user-friendly
This patch changes chasquid-util's subcommands and parameters to
(hopefully) make them more user friendly and intuitive by default.

The changes include defaulting the configuration to /etc/chasquid, and
using full addresses as usernames.

It also adds some shell tests to cover most of the functionality.
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
a9ff0379a5 test: Add missing directory to t-03-queue_persistency 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
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
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
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
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
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
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