1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-17 14:37:02 +00:00
Commit Graph

206 Commits

Author SHA1 Message Date
Alberto Bertogli
e79586a014 Implement HAProxy protocol support
This patch implements support for incoming connections wrapped in the
HAProxy protocol v1.

This is useful when running chasquid behind a HAProxy server, as it
needs the original source IP to perform SPF checks.

This patch is a reimplementation of one originally provided by Denys
Vitali in pull request #15, except the logic for the protocol handling
is moved to a new package, and the smtpsrv.Conn handling of the source
IP is simplified.

It is marked as experimental for now, since we want to give it a bit
more exposure just in case the option/api needs adjustment.

Thanks a lot to Denys Vitali (@denysvitali in github) for sending the
original patch for this, and helping test it!
2020-11-13 20:49:42 +00:00
Alberto Bertogli
025cb2d96a courier: Rename Procmail to MDA
This patch renames courier.Procmail to courier.MDA, to make it more
obvious that the functionality is not tied to that particular MDA.

It's just for readability, there are no functional changes.
2020-09-17 02:47:42 +01:00
Alberto Bertogli
1cc7b9a864 smtpsrv: Pass EHLO/HELO domain to the post-data hook
Some utilities might want to access the EHLO/HELO domain in the
post-data hook (for example, to do additional SPF validations).

This patch implements that support, including sanitizing the EHLO domain
on the environment variable to reduce the risk of problems.
2020-09-17 01:29:49 +01:00
Alberto Bertogli
5bebb00af9 smtpsrv: Rename internal variable ehloAddress -> ehloDomain
The EHLO parameter is generally referred to as "domain", even though it
can take either a domain or an address.

For clarity, rename the variable and comments to match.

This is stylistic only, there are no functional changes.
2020-09-17 01:29:49 +01:00
Alberto Bertogli
1fcc4ffe0f queue: Remove dependency on external protobuf package
The queue protobuf definition currently uses the well-known timestamp
protobuf package.

This adds a build-time dependency on it, which is fairly harmless when
building from source (since the golang protobuf compiler includes it
already), but adds overhead for packaging on distributions.

Since this is the only external proto dependency we have, and the
protobuf message itself is trivial, this patch removes it an instead
embeds a compatible definition.

That way we remove the dependency and simplify packaging, with almost
negligible code overhead.

The change is fully backwards compatible and has no functional changes.
2020-09-12 10:56:17 +01:00
Alberto Bertogli
7fe42a368a monitoring: Add OpenMetrics exporter
This patch makes chasquid's monitoring server expose an OpenMetrics
metrics endpoint.

It adds a new package "expvarom" which implements an HTTP handler that
exports expvar variables in the OpenMetrics text format.

Then, the handler is registered by the monitoring server at /metrics
(where most things expect it to be).

The existing exported variables are also extended with descriptions,
which is optional, but improves the readability of the metrics.
2020-08-21 12:07:33 +01:00
Alberto Bertogli
35e19dc4a2 protoio: Use new protobuf API for text marshalling
This patch makes protoio use the new protobuf API for
marshalling/unmarshalling text protobufs, as well as extends the tests
to cover marshalling failures.

The protobuf text output is not stable/deterministic and some spaces are
added randomly, so some integration tests have to be adjusted to account
for it.
2020-06-30 11:14:52 +01:00
Alberto Bertogli
d9d56552f3 maillog: Support logging to stdout and stderr
This patch adds support for writing maillog to stdout and stderr, which
can be desirable in certain environments.

Thanks to Denys Vitali <denys@denv.it> who sent an alternative patch for
this functionality.
2020-05-24 02:26:18 +01:00
Alberto Bertogli
d83c1dc591 smtpsrv: Fix error code on transient authentication issues
When we can't authenticate due to a transient issue, for example if we
rely on Dovecot and it is not responding, we should use a differentiated
error code to avoid confusing users.

However, today we return the same error code as when the user enters the
wrong password, which could confuse users as their MUA might think their
credentials are no longer valid.

This patch fixes the issue by returning a differentiated error code in
that case, as per RFC 4954.

Thanks to Max Mazurov (fox.cpp@disroot.org) for reporting this problem.
2020-05-23 01:05:12 +01:00
ThinkChaos
db810084a0 Reopen logs on SIGHUP
This makes it possible to manage chasquid logs using logrotate.

Amended-by: Alberto Bertogli <albertito@blitiri.com.ar>
  Added tests, minor style and comment changes.
2020-05-22 20:34:42 +01:00
ThinkChaos
ade107f62e maillog: Use blitiri.com.ar/go/log for mail log
In preparation for supporting log rotation, this patch makes the maillog
package to use blitiri.com.ar/go/log instead of its own writer.

Some of the tests are made more strict, to better test the log format.

Amended-by: Alberto Bertogli <albertito@blitiri.com.ar>
  Fixed build, extended commit message, adjusted to the log options
  API, and added tests.
2020-05-22 20:09:19 +01:00
Alberto Bertogli
9fe790d7c6 aliases: Log the "alias-exists" hook output, for debugging
The output of the alias-exists hook is unused, so currently it's
discarded silently.

However, it can be very useful to debug issues when the hook is not
working as expected.

So this patch makes chasquid log the combined output (stdout and stderr)
to the execution trace.
2020-05-22 14:43:28 +01:00
Alberto Bertogli
4c28efcb20 config: Allow overrides from the command line
This patch allows the configuration values to be overridden from the
command-line, with a new -config_overrides flag.

There is a fairly specific use case for this, when editing the
configuration file is not feasible or convenient (e.g. running an
user-supplied configuration in a managed environment).
2020-05-17 00:10:06 +01:00
Alberto Bertogli
7909b479eb config: Tidy default handling and comparisons in tests
This patch tidies how defaults are handled in the config, using a new
logic to allow "overriding" one config (the default) with another (the
user supplied).

It also improves how the comparisons are done in the tests, using the
more convenient "github.com/google/go-cmp/cmp" package, which also
prints nice diffs on errors.

This is in preparation for a future path where the override mechanism
will be reused.
2020-05-16 23:48:09 +01:00
Alberto Bertogli
b1fe4f81f9 config: Improve logging of errors
Currently, the config package logs errors itself, in addition to
returning them.

That is confusing and results in some duplication of logging.

This patch makes config just return errors, and adjusts the callers
to log them properly.
2020-05-16 23:46:43 +01:00
Alberto Bertogli
50986a7b7e Update protobuf library to v2
There is a new protobuf library (and corresponding code generator) for
Go: google.golang.org/protobuf.

It is fairly compatible with the previous v1 API
(github.com/golang/protobuf), but there are some changes.

This patch adjusts the code and generated files to the new API.

The on-wire/on-disk format remains unchanged so this should be
transparent to the users.
2020-05-16 10:12:51 +01:00
Alberto Bertogli
7fa564f822 smtpsrv: Add comment on BuildNameToCertificate being deprecated
tls.Config.BuildNameToCertificate was deprecated in Go 1.14, and is no
longer necessary.

However, we support down to 1.11, so we will keep it for now.

This patch adds a TODO to remove it in the future once the minimum
supported version is 1.14; and adjust the CI linter accordingly.
2020-05-13 23:42:37 +01:00
Alberto Bertogli
13ee3ba482 courier: Use the hostname in SMTP HELO
The SMTP courier, which handles outgoing connections, uses the domain of
the envelope's from as the domain in the HELO/EHLO greeting.

This works fine in practice, but ideally the domain used in the greeting
should match the reverse DNS record. This used to be more relevant but
nowadays it is not really enforced; however, it sometimes comes up in
self checks, and might cause some confusion when troubleshooting.

So this patch makes it use the configured hostname instead, which is
under the users' control and more likely to be compliant. It also
simplifies the code.

The documentation of the hostname configuration option is also updated
to mention this behaviour.

Thanks to Jonas Seydel (thor77) for bringing this up.
2020-05-13 20:27:17 +01:00
Alberto Bertogli
7b0703eaa0 queue: Check that we can create the queue directory
When creating a new Queue instance, we os.MkdirAll the queue directory.

Currently we don't check if it fails, which will cause us to find out
about problems when the queue is first used, where it is more annoying
to troubleshoot.

This patch adjusts the code so that we check and propagate the error.
That way, problems with the queue directory will be more evident and
easier to handle.
2020-04-14 12:01:01 +01:00
Alberto Bertogli
25ebc4f2e2 Avoid unnecessary calls to fmt.Sprintf
The linter caught some unnecessary calls to fmt.Sprintf. This patch
removes them.

There are no functional changes.
2020-04-14 12:01:01 +01:00
Alberto Bertogli
d6b512166b Make it explicit when we are intentionally not checking errors
The linter complains that we're not checking for errors, but on some
cases it's on code paths were it is reasonable to do so (e.g. we're
closing the connection and it's a best-effort write).

This patch adjusts the code to make those cases explicit.
2020-04-14 12:01:01 +01:00
Alberto Bertogli
4802e2f3e4 smtpsrv: Check TLS Handshake result
When receiving a message on a TLS socket, we currently don't check the
Handshake result, so connections often fail in a way that is not easy to
troubleshoot.

This patch fixes that by checking the result and emitting a nicer error
message before closing the connection.
2020-04-14 12:01:01 +01:00
Alberto Bertogli
0fd3941cf0 courier: Don't call testing.T.Fatalf from a goroutine
Calling testing.T.Fatalf from a new goroutine is not supported; since
this should be quite exceptional, we just panic instead.
2020-04-14 12:01:01 +01:00
Alberto Bertogli
9fbd1fe786 tlsconst: Update list of ciphers
This patch updates the list of ciphers in tlsconst, using the latest
list from IANA.
2020-04-12 11:25:27 +01:00
Alberto Bertogli
29512f7e4f testlib: Add tests for testlib.WaitFor 2020-03-21 23:56:31 +00:00
Alberto Bertogli
fdae72f275 testlib: Add comments and unexport unnecessary structs
This patch contains some readability improvements to testlib: it
adds/reformats some comments for exported functions for consistency, and
unexports some structs that are not used outside the library.
2020-03-21 23:32:28 +00:00
Alberto Bertogli
44140220b9 test: Improve DATA handling in the smtpsrv fuzzer
The smtpsrv fuzzer doesn't handle DATA commands particularly well:
it will continue to read but will skip lines that have STARTTLS as
content, and only really care for the first line due to a bug.

This patch fixes the handling, and moves the logic to a separate
function for readability.
2020-03-21 23:27:19 +00:00
Alberto Bertogli
c8fbf2ecc9 smtpsrv: Don't consider client EOF an error
When the client closes the connection, which is very common, chasquid
logs it as an error ("exiting with error: EOF").

That can be confusing and mislead users, and also makes a lot of
traces be marked as errored, when nothing wrong occurred.

So this patch changes the log to not treat it as an error.
2020-03-21 16:58:56 +00:00
Alberto Bertogli
8fab350b59 courier: DNS temporary errors should cause temporary delivery failures
In the SMTP courier, when the MX lookup fails with a DNS temporary
error, we should propagate that up and cause delivery to fail with a
temporary error too.

That way the delivery can be attempted later by the queue.

However, the code today doesn't make the distinction and will treat any
temporary DNS error as a permanent delivery failure, which is a bug.

The bug was found by ludusrusso and reported in
https://github.com/albertito/chasquid/issues/4

This patch fixes the bug by propagating the error properly.
2020-03-12 22:07:22 +00:00
Alberto Bertogli
6641d858ad courier: Extend TODO for DNS error handling on Go >= 1.13
In https://github.com/albertito/chasquid/issues/4, ludusrusso comments
that the DNS lookup error handling in the SMTP courier could be improved
by using DNSError.IsNotFound.

That is true, but unfortunately it was only added in Go 1.13, and we are
currently trying to support Go 1.11 since that's what in Debian stable.

So this patch updates the TODO to note this, so that when we can use a
newer Go version, we improve this code.
2020-03-12 22:07:11 +00:00
Alberto Bertogli
4edcd79a25 smtpsrv: Keep reading DATA input even if it's too large
When the DATA input is too large, we should keep on reading through it
until we reach the end marker, otherwise there is a security problem:
the remaining data will be interpreted as SMTP commands, so for example
a forwarded message that is too long might end up executing SMTP
commands under an authenticated user.

This patch implements this behaviour, while being careful not to consume
extra memory to avoid opening up the possibility of a DoS.

Note the equivalent logic for single long lines is already implemented.
2019-12-04 01:46:54 +00:00
Alberto Bertogli
a12875162f smtpsrv: Test too many recipients
This patch adds a test to make sure we don't allow too many recipients.
2019-12-01 19:37:47 +00:00
Alberto Bertogli
99c4ad5ef7 smtpsrv: Disable reloads during tests
Reloading during tests will cause the testing aliases to be removed,
which makes test runs that extend beyond 30s to be flaky.

This patch fixes the bug by disabling reloads during these tests.
2019-12-01 19:09:58 +00:00
Alberto Bertogli
e8a6bf6188 smtpsrv: Make tests log maillog to stdout 2019-12-01 19:09:57 +00:00
Alberto Bertogli
a6a964ac3e test: Move testing couriers to testlib
The testing couriers are currently only used in the queue tests, but we
also want to use them in smtpsrv tests so we can make them more robusts
by checking the emails got delivered.

This patch moves the testing couriers to testlib, and makes both queue
and smtpsrv use them.
2019-12-01 19:09:12 +00:00
Alberto Bertogli
99df5e7b57 smtpsrv: Limit incoming line length and improve large message handling
Currently, there is no limit to incoming line length, so an evil client
could cause a memory exhaustion DoS by issuing very long lines.

This patch fixes the bug by limiting the size of the lines.

To do that, we replace the textproto.Conn with a pair of buffered reader
and writer, which simplify the code and allow for better and cleaner
control.

Thanks to Max Mazurov (fox.cpp@disroot.org) for finding and reporting
this issue.
2019-12-01 19:07:58 +00:00
Alberto Bertogli
d7006d0e16 smtp: Limit incoming line length
On the smtp client package, there is no limit to the length of the
server's replies, so an evil server could cause a memory exhaustion DoS
by issuing very long lines.

This patch fixes the bug by limiting the total size of received data.
Ideally this would be done per-line instead, but gets much more complex,
so this is a compromise.

The limit chosen is 2 MiB, which should be plenty for any the total size
of server-side replies, considering we only send a single message per
connection anyway.

This is similar to 06d808c (smtpsrv: Limit incoming line length), which
was found and reported by Max Mazurov (fox.cpp@disroot.org).
2019-12-01 17:25:25 +00:00
Alberto Bertogli
34339c4572 smtpsrv: Add fuzz testing
This patch adds a fuzz test for the smtpsrv package.

It brings up a server for test, and then fuzz the data sent over the
network.
2019-11-30 11:38:46 +00:00
Alberto Bertogli
933b979220 smtpsrv: Don't hard-code ports in tests
The smtpsrv tests hard-code ports, but this patch fixes that by making it
use the new testlib.GetFreePort function.
2019-11-30 11:38:46 +00:00
Alberto Bertogli
d0f65881c9 smtpsrv: Allow manual override of submission+TLS port in tests
The smtpsrv server tests allow manual override of testing ports via
flags, but the submission+TLS port was missing (accidental oversight).
2019-11-30 11:38:46 +00:00
Alberto Bertogli
ac2c5ab4db test: Add testlib.GetFreePort function
Some tests require picking ports, and today resort to hard-coding,
which is brittle.

This patch adds a testlib.GetFreePort function to help pick free ports.

It is not totally race-free, but is much better than hard-coding.
2019-11-30 11:38:46 +00:00
Alberto Bertogli
a47faf89a4 smtpsrv: Failures to enqueue are transient, not permanent
If we fail to put the message in the queue (e.g. because we're out of
storage space, or the aliases-resolve hook errored out), it should be
considered a transient failure.

Currently we return a permanent error, which is misleading, as we want
clients to retry in these situations.

So this patch changes the error returned accordingly.
2019-10-24 21:37:09 +01:00
Alberto Bertogli
0718749314 Update auto-generated code
This patch updates the auto-generated code to match the latest tooling
versions.

In particular, the protobufs are regenerated, and the new version no
longer supports unkeyed literals, so some minor changes are needed.

Other than that, the cipher list is extended with the latest ciphers.
2019-10-24 21:37:09 +01:00
Alberto Bertogli
f399fe3e84 aliases: Implement aliases hooks
This patch implements two new hooks: alias-resolve and alias-exists.

They are called during the aliases resolution process, to allow for more
complex integration with other systems, such as storing the aliases in a
database.

See the included documentation for more details.
2019-10-24 21:37:09 +01:00
Alberto Bertogli
dea6f73164 aliases: Treat empty pipe aliases as bad lines and skip them
A pipe alias must have a command, if it doesn't, we should treat the
line as bad and skip it like we do for others.
2019-10-22 22:14:26 +01:00
Alberto Bertogli
5f72f723db smtpsrv: Clean up post-data hook tracing output
This patch does some cleanups on the tracing output for the post-data
hook, to quote the output better and more consistently.
2019-10-22 21:45:54 +01:00
Alberto Bertogli
bb97991a24 docs: Add aliases documentation
The processing of aliases wasn't properly documented in an user-visible
way, so this patch adds a new section for it.
2019-10-19 00:45:18 +01:00
Alberto Bertogli
41d960590d smtpsrv: Use spf.CheckHostWithSender
The spf library has gained support for macros, but to process them
properly, a new function needs to be called with the full sender
address, spf.CheckHostWithSender.

This patch updates chasquid's calls to the new API.
2019-10-14 19:37:14 +01:00
Alberto Bertogli
3584441549 test: Use testlib.RemoveIfOk where appropriate
Some tests did not make use of testlib.RemoveIfOk, which resulted in
some duplication; this patch fixes that.

While at it, userdb tests have its own simpler variant, so add some
safety checks to it.
2019-09-10 01:09:44 +01:00
Alberto Bertogli
f63e5bf0b2 sts: Reject policies with max_age > 1y, as per RFC
The MTA-STS standard explicitly says the maximum max_age is 1 year.

This patch adds a check to the STS library to enforce this. Policies
with max_age > 1y will be treated as invalid.

See this email thread for some discussion on the topic:
https://mailarchive.ietf.org/arch/msg/uta/bnUjy9jxM_Va-lDXVtbB32zIkYI
2019-08-31 01:14:56 +01:00