mirror of
https://github.com/mjl-/mox.git
synced 2024-12-26 08:23:48 +03:00
8096441f67
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.
156 lines
4.3 KiB
Go
156 lines
4.3 KiB
Go
//go:build integration
|
|
|
|
// Run this using docker-compose.yml, see Makefile.
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
"github.com/mjl-/bstore"
|
|
|
|
"github.com/mjl-/mox/dns"
|
|
"github.com/mjl-/mox/mlog"
|
|
"github.com/mjl-/mox/mox-"
|
|
"github.com/mjl-/mox/sasl"
|
|
"github.com/mjl-/mox/smtpclient"
|
|
"github.com/mjl-/mox/store"
|
|
)
|
|
|
|
var ctxbg = context.Background()
|
|
|
|
func tcheck(t *testing.T, err error, msg string) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatalf("%s: %s", msg, err)
|
|
}
|
|
}
|
|
|
|
// Submit a message to mox, which sends it to postfix, which forwards back to mox.
|
|
// We check if we receive the message.
|
|
func TestDeliver(t *testing.T) {
|
|
mlog.Logfmt = true
|
|
|
|
// Remove state.
|
|
os.RemoveAll("testdata/integration/data")
|
|
os.MkdirAll("testdata/integration/data", 0750)
|
|
|
|
// Cleanup afterwards, these are owned by root, annoying to have around due to
|
|
// permission errors.
|
|
defer os.RemoveAll("testdata/integration/data")
|
|
|
|
// Load mox config.
|
|
mox.ConfigStaticPath = "testdata/integration/config/mox.conf"
|
|
filepath.Join(filepath.Dir(mox.ConfigStaticPath), "domains.conf")
|
|
if errs := mox.LoadConfig(ctxbg, true, false); len(errs) > 0 {
|
|
t.Fatalf("loading mox config: %v", errs)
|
|
}
|
|
|
|
// Create new accounts
|
|
createAccount := func(email, password string) {
|
|
t.Helper()
|
|
acc, _, err := store.OpenEmail(email)
|
|
tcheck(t, err, "open account")
|
|
err = acc.SetPassword(password)
|
|
tcheck(t, err, "setting password")
|
|
err = acc.Close()
|
|
tcheck(t, err, "closing account")
|
|
}
|
|
|
|
createAccount("moxtest1@mox1.example", "pass1234")
|
|
createAccount("moxtest2@mox2.example", "pass1234")
|
|
createAccount("moxtest3@mox3.example", "pass1234")
|
|
|
|
// Start mox.
|
|
const mtastsdbRefresher = false
|
|
const skipForkExec = true
|
|
err := start(mtastsdbRefresher, skipForkExec)
|
|
tcheck(t, err, "starting mox")
|
|
|
|
// todo: we should probably hook store.Comm to get updates.
|
|
latestMsgID := func(username string) int64 {
|
|
// We open the account index database created by mox for the test user. And we keep looking for the email we sent.
|
|
dbpath := fmt.Sprintf("testdata/integration/data/accounts/%s/index.db", username)
|
|
db, err := bstore.Open(ctxbg, dbpath, &bstore.Options{Timeout: 3 * time.Second}, store.DBTypes...)
|
|
if err != nil && errors.Is(err, bolt.ErrTimeout) {
|
|
log.Printf("db open timeout (normal delay for new sender with account and db file kept open)")
|
|
return 0
|
|
}
|
|
tcheck(t, err, "open test account database")
|
|
defer db.Close()
|
|
|
|
q := bstore.QueryDB[store.Mailbox](ctxbg, db)
|
|
q.FilterNonzero(store.Mailbox{Name: "Inbox"})
|
|
inbox, err := q.Get()
|
|
if err != nil {
|
|
log.Printf("inbox for finding latest message id: %v", err)
|
|
return 0
|
|
}
|
|
|
|
qm := bstore.QueryDB[store.Message](ctxbg, db)
|
|
qm.FilterNonzero(store.Message{MailboxID: inbox.ID})
|
|
qm.SortDesc("ID")
|
|
qm.Limit(1)
|
|
m, err := qm.Get()
|
|
if err != nil {
|
|
log.Printf("finding latest message id: %v", err)
|
|
return 0
|
|
}
|
|
return m.ID
|
|
}
|
|
|
|
waitForMsg := func(prevMsgID int64, username string) int64 {
|
|
t.Helper()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
msgID := latestMsgID(username)
|
|
if msgID > prevMsgID {
|
|
return msgID
|
|
}
|
|
time.Sleep(500 * time.Millisecond)
|
|
}
|
|
t.Fatalf("timeout waiting for message")
|
|
return 0 // not reached
|
|
}
|
|
|
|
deliver := func(username, desthost, mailfrom, password, rcptto string) {
|
|
t.Helper()
|
|
|
|
prevMsgID := latestMsgID(username)
|
|
|
|
conn, err := net.Dial("tcp", desthost+":587")
|
|
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")
|
|
auth := []sasl.Client{sasl.NewClientPlain(mailfrom, password)}
|
|
c, err := smtpclient.New(mox.Context, mlog.New("test"), conn, smtpclient.TLSOpportunistic, mox.Conf.Static.HostnameDomain, dns.Domain{ASCII: desthost}, auth)
|
|
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")
|
|
|
|
waitForMsg(prevMsgID, username)
|
|
}
|
|
|
|
deliver("moxtest1", "moxmail1.mox1.example", "moxtest1@mox1.example", "pass1234", "root@postfix.example")
|
|
deliver("moxtest3", "moxmail2.mox2.example", "moxtest2@mox2.example", "pass1234", "moxtest3@mox3.example")
|
|
}
|