2023-01-30 16:27:06 +03:00
|
|
|
//go:build integration
|
|
|
|
|
implement dnssec-awareness throughout code, and dane for incoming/outgoing mail delivery
the vendored dns resolver code is a copy of the go stdlib dns resolver, with
awareness of the "authentic data" (i.e. dnssec secure) added, as well as support
for enhanced dns errors, and looking up tlsa records (for dane). ideally it
would be upstreamed, but the chances seem slim.
dnssec-awareness is added to all packages, e.g. spf, dkim, dmarc, iprev. their
dnssec status is added to the Received message headers for incoming email.
but the main reason to add dnssec was for implementing dane. with dane, the
verification of tls certificates can be done through certificates/public keys
published in dns (in the tlsa records). this only makes sense (is trustworthy)
if those dns records can be verified to be authentic.
mox now applies dane to delivering messages over smtp. mox already implemented
mta-sts for webpki/pkix-verification of certificates against the (large) pool
of CA's, and still enforces those policies when present. but it now also checks
for dane records, and will verify those if present. if dane and mta-sts are
both absent, the regular opportunistic tls with starttls is still done. and the
fallback to plaintext is also still done.
mox also makes it easy to setup dane for incoming deliveries, so other servers
can deliver with dane tls certificate verification. the quickstart now
generates private keys that are used when requesting certificates with acme.
the private keys are pre-generated because they must be static and known during
setup, because their public keys must be published in tlsa records in dns.
autocert would generate private keys on its own, so had to be forked to add the
option to provide the private key when requesting a new certificate. hopefully
upstream will accept the change and we can drop the fork.
with this change, using the quickstart to setup a new mox instance, the checks
at internet.nl result in a 100% score, provided the domain is dnssec-signed and
the network doesn't have any issues.
2023-10-10 13:09:35 +03:00
|
|
|
// todo: set up a test for dane, mta-sts, etc.
|
|
|
|
|
2023-01-30 16:27:06 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-24 00:32:02 +03:00
|
|
|
"crypto/tls"
|
2023-01-30 16:27:06 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
2023-07-01 19:48:29 +03:00
|
|
|
"os/exec"
|
2023-01-30 16:27:06 +03:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
new feature: when delivering messages from the queue, make it possible to use a "transport"
the default transport is still just "direct delivery", where we connect to the
destination domain's MX servers.
other transports are:
- regular smtp without authentication, this is relaying to a smarthost.
- submission with authentication, e.g. to a third party email sending service.
- direct delivery, but with with connections going through a socks proxy. this
can be helpful if your ip is blocked, you need to get email out, and you have
another IP that isn't blocked.
keep in mind that for all of the above, appropriate SPF/DKIM settings have to
be configured. the "dnscheck" for a domain does a check for any SOCKS IP in the
SPF record. SPF for smtp/submission (ranges? includes?) and any DKIM
requirements cannot really be checked.
which transport is used can be configured through routes. routes can be set on
an account, a domain, or globally. the routes are evaluated in that order, with
the first match selecting the transport. these routes are evaluated for each
delivery attempt. common selection criteria are recipient domain and sender
domain, but also which delivery attempt this is. you could configured mox to
attempt sending through a 3rd party from the 4th attempt onwards.
routes and transports are optional. if no route matches, or an empty/zero
transport is selected, normal direct delivery is done.
we could already "submit" emails with 3rd party accounts with "sendmail". but
we now support more SASL authentication mechanisms with SMTP (not only PLAIN,
but also SCRAM-SHA-256, SCRAM-SHA-1 and CRAM-MD5), which sendmail now also
supports. sendmail will use the most secure mechanism supported by the server,
or the explicitly configured mechanism.
for issue #36 by dmikushin. also based on earlier discussion on hackernews.
2023-06-16 19:38:28 +03:00
|
|
|
"github.com/mjl-/mox/dns"
|
2023-07-01 15:24:28 +03:00
|
|
|
"github.com/mjl-/mox/imapclient"
|
2023-01-30 16:27:06 +03:00
|
|
|
"github.com/mjl-/mox/mlog"
|
|
|
|
"github.com/mjl-/mox/mox-"
|
new feature: when delivering messages from the queue, make it possible to use a "transport"
the default transport is still just "direct delivery", where we connect to the
destination domain's MX servers.
other transports are:
- regular smtp without authentication, this is relaying to a smarthost.
- submission with authentication, e.g. to a third party email sending service.
- direct delivery, but with with connections going through a socks proxy. this
can be helpful if your ip is blocked, you need to get email out, and you have
another IP that isn't blocked.
keep in mind that for all of the above, appropriate SPF/DKIM settings have to
be configured. the "dnscheck" for a domain does a check for any SOCKS IP in the
SPF record. SPF for smtp/submission (ranges? includes?) and any DKIM
requirements cannot really be checked.
which transport is used can be configured through routes. routes can be set on
an account, a domain, or globally. the routes are evaluated in that order, with
the first match selecting the transport. these routes are evaluated for each
delivery attempt. common selection criteria are recipient domain and sender
domain, but also which delivery attempt this is. you could configured mox to
attempt sending through a 3rd party from the 4th attempt onwards.
routes and transports are optional. if no route matches, or an empty/zero
transport is selected, normal direct delivery is done.
we could already "submit" emails with 3rd party accounts with "sendmail". but
we now support more SASL authentication mechanisms with SMTP (not only PLAIN,
but also SCRAM-SHA-256, SCRAM-SHA-1 and CRAM-MD5), which sendmail now also
supports. sendmail will use the most secure mechanism supported by the server,
or the explicitly configured mechanism.
for issue #36 by dmikushin. also based on earlier discussion on hackernews.
2023-06-16 19:38:28 +03:00
|
|
|
"github.com/mjl-/mox/sasl"
|
2023-01-30 16:27:06 +03:00
|
|
|
"github.com/mjl-/mox/smtpclient"
|
|
|
|
)
|
|
|
|
|
2023-07-02 14:53:34 +03:00
|
|
|
func tcheck(t *testing.T, err error, errmsg string) {
|
2023-01-30 16:27:06 +03:00
|
|
|
if err != nil {
|
2023-07-24 00:32:02 +03:00
|
|
|
t.Helper()
|
2023-07-02 14:53:34 +03:00
|
|
|
t.Fatalf("%s: %s", errmsg, err)
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeliver(t *testing.T) {
|
2023-07-24 00:32:02 +03:00
|
|
|
xlog := mlog.New("integration")
|
2023-01-30 16:27:06 +03:00
|
|
|
mlog.Logfmt = true
|
|
|
|
|
2023-07-24 00:32:02 +03:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
tcheck(t, err, "hostname")
|
|
|
|
ourHostname, err := dns.ParseDomain(hostname)
|
|
|
|
tcheck(t, err, "parse hostname")
|
2023-01-30 16:27:06 +03:00
|
|
|
|
2023-07-01 15:24:28 +03:00
|
|
|
// Single update from IMAP IDLE.
|
|
|
|
type idleResponse struct {
|
|
|
|
untagged imapclient.Untagged
|
|
|
|
err error
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
|
|
|
|
2023-07-24 00:32:02 +03:00
|
|
|
// Deliver submits a message over submissions, and checks with imap idle if the
|
|
|
|
// message is received by the destination mail server.
|
|
|
|
deliver := func(checkTime bool, dialtls bool, imaphost, imapuser, imappassword string, send func()) {
|
2023-01-30 16:27:06 +03:00
|
|
|
t.Helper()
|
|
|
|
|
2023-07-24 00:32:02 +03:00
|
|
|
// Connect to IMAP, execute IDLE command, which will return on deliver message.
|
|
|
|
// TLS certificates work because the container has the CA certificates configured.
|
|
|
|
var imapconn net.Conn
|
|
|
|
var err error
|
|
|
|
if dialtls {
|
|
|
|
imapconn, err = tls.Dial("tcp", imaphost, nil)
|
|
|
|
} else {
|
|
|
|
imapconn, err = net.Dial("tcp", imaphost)
|
|
|
|
}
|
|
|
|
tcheck(t, err, "dial imap")
|
2023-07-01 15:24:28 +03:00
|
|
|
defer imapconn.Close()
|
2023-07-24 00:32:02 +03:00
|
|
|
|
|
|
|
imapc, err := imapclient.New(imapconn, false)
|
2023-07-01 15:24:28 +03:00
|
|
|
tcheck(t, err, "new imapclient")
|
2023-07-24 00:32:02 +03:00
|
|
|
|
|
|
|
_, _, err = imapc.Login(imapuser, imappassword)
|
|
|
|
tcheck(t, err, "imap login")
|
|
|
|
|
|
|
|
_, _, err = imapc.Select("Inbox")
|
2023-07-01 15:24:28 +03:00
|
|
|
tcheck(t, err, "imap select inbox")
|
|
|
|
|
2023-07-24 00:32:02 +03:00
|
|
|
err = imapc.Commandf("", "idle")
|
|
|
|
tcheck(t, err, "write imap idle command")
|
2023-07-01 15:24:28 +03:00
|
|
|
|
2023-07-24 00:32:02 +03:00
|
|
|
_, _, _, err = imapc.ReadContinuation()
|
2023-07-01 15:24:28 +03:00
|
|
|
tcheck(t, err, "read imap continuation")
|
|
|
|
|
|
|
|
idle := make(chan idleResponse)
|
|
|
|
go func() {
|
|
|
|
for {
|
2023-07-24 00:32:02 +03:00
|
|
|
untagged, err := imapc.ReadUntagged()
|
2023-07-01 15:24:28 +03:00
|
|
|
idle <- idleResponse{untagged, err}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
2023-07-01 15:24:28 +03:00
|
|
|
}()
|
|
|
|
defer func() {
|
2023-07-24 00:32:02 +03:00
|
|
|
err := imapc.Writelinef("done")
|
2023-07-01 15:24:28 +03:00
|
|
|
tcheck(t, err, "aborting idle")
|
|
|
|
}()
|
2023-01-30 16:27:06 +03:00
|
|
|
|
2023-07-01 19:48:29 +03:00
|
|
|
t0 := time.Now()
|
2023-07-24 00:32:02 +03:00
|
|
|
send()
|
2023-07-01 19:48:29 +03:00
|
|
|
|
|
|
|
// Wait for notification of delivery.
|
|
|
|
select {
|
|
|
|
case resp := <-idle:
|
|
|
|
tcheck(t, resp.err, "idle notification")
|
|
|
|
_, ok := resp.untagged.(imapclient.UntaggedExists)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("got idle %#v, expected untagged exists", resp.untagged)
|
|
|
|
}
|
|
|
|
if d := time.Since(t0); checkTime && d < 1*time.Second {
|
|
|
|
t.Fatalf("delivery took %v, but should have taken at least 1 second, the first-time sender delay", d)
|
|
|
|
}
|
2023-07-24 00:32:02 +03:00
|
|
|
case <-time.After(30 * time.Second):
|
2023-07-01 19:48:29 +03:00
|
|
|
t.Fatalf("timeout after 5s waiting for IMAP IDLE notification of new message, should take about 1 second")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-24 00:32:02 +03:00
|
|
|
submit := func(dialtls bool, mailfrom, password, desthost, rcptto string) {
|
|
|
|
var conn net.Conn
|
|
|
|
var err error
|
|
|
|
if dialtls {
|
|
|
|
conn, err = tls.Dial("tcp", desthost, nil)
|
|
|
|
} else {
|
|
|
|
conn, err = net.Dial("tcp", desthost)
|
|
|
|
}
|
2023-01-30 16:27:06 +03:00
|
|
|
tcheck(t, err, "dial submission")
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
msg := fmt.Sprintf(`From: <%s>
|
|
|
|
To: <%s>
|
|
|
|
Subject: test message
|
|
|
|
|
|
|
|
This is the message.
|
|
|
|
`, mailfrom, rcptto)
|
|
|
|
msg = strings.ReplaceAll(msg, "\n", "\r\n")
|
new feature: when delivering messages from the queue, make it possible to use a "transport"
the default transport is still just "direct delivery", where we connect to the
destination domain's MX servers.
other transports are:
- regular smtp without authentication, this is relaying to a smarthost.
- submission with authentication, e.g. to a third party email sending service.
- direct delivery, but with with connections going through a socks proxy. this
can be helpful if your ip is blocked, you need to get email out, and you have
another IP that isn't blocked.
keep in mind that for all of the above, appropriate SPF/DKIM settings have to
be configured. the "dnscheck" for a domain does a check for any SOCKS IP in the
SPF record. SPF for smtp/submission (ranges? includes?) and any DKIM
requirements cannot really be checked.
which transport is used can be configured through routes. routes can be set on
an account, a domain, or globally. the routes are evaluated in that order, with
the first match selecting the transport. these routes are evaluated for each
delivery attempt. common selection criteria are recipient domain and sender
domain, but also which delivery attempt this is. you could configured mox to
attempt sending through a 3rd party from the 4th attempt onwards.
routes and transports are optional. if no route matches, or an empty/zero
transport is selected, normal direct delivery is done.
we could already "submit" emails with 3rd party accounts with "sendmail". but
we now support more SASL authentication mechanisms with SMTP (not only PLAIN,
but also SCRAM-SHA-256, SCRAM-SHA-1 and CRAM-MD5), which sendmail now also
supports. sendmail will use the most secure mechanism supported by the server,
or the explicitly configured mechanism.
for issue #36 by dmikushin. also based on earlier discussion on hackernews.
2023-06-16 19:38:28 +03:00
|
|
|
auth := []sasl.Client{sasl.NewClientPlain(mailfrom, password)}
|
implement dnssec-awareness throughout code, and dane for incoming/outgoing mail delivery
the vendored dns resolver code is a copy of the go stdlib dns resolver, with
awareness of the "authentic data" (i.e. dnssec secure) added, as well as support
for enhanced dns errors, and looking up tlsa records (for dane). ideally it
would be upstreamed, but the chances seem slim.
dnssec-awareness is added to all packages, e.g. spf, dkim, dmarc, iprev. their
dnssec status is added to the Received message headers for incoming email.
but the main reason to add dnssec was for implementing dane. with dane, the
verification of tls certificates can be done through certificates/public keys
published in dns (in the tlsa records). this only makes sense (is trustworthy)
if those dns records can be verified to be authentic.
mox now applies dane to delivering messages over smtp. mox already implemented
mta-sts for webpki/pkix-verification of certificates against the (large) pool
of CA's, and still enforces those policies when present. but it now also checks
for dane records, and will verify those if present. if dane and mta-sts are
both absent, the regular opportunistic tls with starttls is still done. and the
fallback to plaintext is also still done.
mox also makes it easy to setup dane for incoming deliveries, so other servers
can deliver with dane tls certificate verification. the quickstart now
generates private keys that are used when requesting certificates with acme.
the private keys are pre-generated because they must be static and known during
setup, because their public keys must be published in tlsa records in dns.
autocert would generate private keys on its own, so had to be forked to add the
option to provide the private key when requesting a new certificate. hopefully
upstream will accept the change and we can drop the fork.
with this change, using the quickstart to setup a new mox instance, the checks
at internet.nl result in a 100% score, provided the domain is dnssec-signed and
the network doesn't have any issues.
2023-10-10 13:09:35 +03:00
|
|
|
c, err := smtpclient.New(mox.Context, xlog, conn, smtpclient.TLSSkip, ourHostname, dns.Domain{ASCII: desthost}, auth, nil, nil, nil)
|
2023-01-30 16:27:06 +03:00
|
|
|
tcheck(t, err, "smtp hello")
|
|
|
|
err = c.Deliver(mox.Context, mailfrom, rcptto, int64(len(msg)), strings.NewReader(msg), false, false)
|
|
|
|
tcheck(t, err, "deliver with smtp")
|
|
|
|
err = c.Close()
|
|
|
|
tcheck(t, err, "close smtpclient")
|
|
|
|
}
|
|
|
|
|
2023-07-24 00:32:02 +03:00
|
|
|
// Make sure moxacmepebble has a TLS certificate.
|
|
|
|
conn, err := tls.Dial("tcp", "moxacmepebble.mox1.example:465", nil)
|
|
|
|
tcheck(t, err, "dial submission")
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
xlog.Print("submitting email to moxacmepebble, waiting for imap notification at moxmail2")
|
|
|
|
t0 := time.Now()
|
|
|
|
deliver(true, true, "moxmail2.mox2.example:993", "moxtest2@mox2.example", "accountpass4321", func() {
|
|
|
|
submit(true, "moxtest1@mox1.example", "accountpass1234", "moxacmepebble.mox1.example:465", "moxtest2@mox2.example")
|
2023-07-01 19:48:29 +03:00
|
|
|
})
|
2023-07-24 00:32:02 +03:00
|
|
|
xlog.Print("success", mlog.Field("duration", time.Since(t0)))
|
|
|
|
|
|
|
|
xlog.Print("submitting email to moxmail2, waiting for imap notification at moxacmepebble")
|
|
|
|
t0 = time.Now()
|
|
|
|
deliver(true, true, "moxacmepebble.mox1.example:993", "moxtest1@mox1.example", "accountpass1234", func() {
|
|
|
|
submit(true, "moxtest2@mox2.example", "accountpass4321", "moxmail2.mox2.example:465", "moxtest1@mox1.example")
|
2023-07-01 19:48:29 +03:00
|
|
|
})
|
2023-07-24 00:32:02 +03:00
|
|
|
xlog.Print("success", mlog.Field("duration", time.Since(t0)))
|
2023-07-01 19:48:29 +03:00
|
|
|
|
2023-07-24 00:32:02 +03:00
|
|
|
xlog.Print("submitting email to postfix, waiting for imap notification at moxacmepebble")
|
|
|
|
t0 = time.Now()
|
|
|
|
deliver(true, true, "moxacmepebble.mox1.example:993", "moxtest1@mox1.example", "accountpass1234", func() {
|
|
|
|
submit(true, "moxtest1@mox1.example", "accountpass1234", "moxacmepebble.mox1.example:465", "root@postfix.example")
|
2023-07-01 19:48:29 +03:00
|
|
|
})
|
2023-07-24 00:32:02 +03:00
|
|
|
xlog.Print("success", mlog.Field("duration", time.Since(t0)))
|
2023-07-01 19:48:29 +03:00
|
|
|
|
2023-07-24 00:32:02 +03:00
|
|
|
xlog.Print("submitting email to localserve")
|
|
|
|
t0 = time.Now()
|
|
|
|
deliver(false, false, "localserve.mox1.example:1143", "mox@localhost", "moxmoxmox", func() {
|
|
|
|
submit(false, "mox@localhost", "moxmoxmox", "localserve.mox1.example:1587", "moxtest1@mox1.example")
|
|
|
|
})
|
|
|
|
xlog.Print("success", mlog.Field("duration", time.Since(t0)))
|
|
|
|
|
|
|
|
xlog.Print("submitting email to localserve")
|
|
|
|
t0 = time.Now()
|
|
|
|
deliver(false, false, "localserve.mox1.example:1143", "mox@localhost", "moxmoxmox", func() {
|
2023-07-01 19:48:29 +03:00
|
|
|
cmd := exec.Command("go", "run", ".", "sendmail", "mox@localhost")
|
|
|
|
const msg = `Subject: test
|
|
|
|
|
|
|
|
a message.
|
|
|
|
`
|
|
|
|
cmd.Stdin = strings.NewReader(msg)
|
|
|
|
var out strings.Builder
|
|
|
|
cmd.Stdout = &out
|
|
|
|
err := cmd.Run()
|
2023-07-24 00:32:02 +03:00
|
|
|
xlog.Print("sendmail", mlog.Field("output", out.String()))
|
2023-07-01 19:48:29 +03:00
|
|
|
tcheck(t, err, "sendmail")
|
|
|
|
})
|
2023-07-24 00:32:02 +03:00
|
|
|
xlog.Print("success", mlog.Field("duration", time.Since(t0)))
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|