1package smtpserver
2
3import (
4 "context"
5 "fmt"
6 "time"
7
8 "github.com/mjl-/mox/dsn"
9 "github.com/mjl-/mox/mlog"
10 "github.com/mjl-/mox/mox-"
11 "github.com/mjl-/mox/queue"
12 "github.com/mjl-/mox/smtp"
13 "github.com/mjl-/mox/store"
14)
15
16// compose dsn message and add it to the queue for delivery to rcptTo.
17func queueDSN(ctx context.Context, log mlog.Log, c *conn, rcptTo smtp.Path, m dsn.Message, requireTLS bool) error {
18 buf, err := m.Compose(c.log, false)
19 if err != nil {
20 return err
21 }
22 bufDKIM, err := mox.DKIMSign(ctx, c.log, m.From, false, buf)
23 log.Check(err, "dkim signing dsn")
24 buf = append([]byte(bufDKIM), buf...)
25
26 var bufUTF8 []byte
27 if c.smtputf8 {
28 bufUTF8, err = m.Compose(c.log, true)
29 if err != nil {
30 c.log.Errorx("composing dsn with utf-8 for incoming delivery for unknown user, continuing with ascii-only dsn", err)
31 } else {
32 bufUTF8DKIM, err := mox.DKIMSign(ctx, log, m.From, true, bufUTF8)
33 log.Check(err, "dkim signing dsn with utf8")
34 bufUTF8 = append([]byte(bufUTF8DKIM), bufUTF8...)
35 }
36 }
37
38 f, err := store.CreateMessageTemp(c.log, "smtp-dsn")
39 if err != nil {
40 return fmt.Errorf("creating temp file: %w", err)
41 }
42 defer store.CloseRemoveTempFile(c.log, f, "smtpserver dsn message")
43
44 if _, err := f.Write([]byte(buf)); err != nil {
45 return fmt.Errorf("writing dsn file: %w", err)
46 }
47
48 // Queue DSN with null reverse path so failures to deliver will eventually drop the
49 // message instead of causing delivery loops.
50 // ../rfc/3464:433
51 const has8bit = false
52 const smtputf8 = false
53 var reqTLS *bool
54 if requireTLS {
55 reqTLS = &requireTLS
56 }
57 qm := queue.MakeMsg(smtp.Path{}, rcptTo, has8bit, smtputf8, int64(len(buf)), m.MessageID, nil, reqTLS, time.Now(), m.Subject)
58 qm.DSNUTF8 = bufUTF8
59 if err := queue.Add(ctx, c.log, mox.Conf.Static.Postmaster.Account, f, qm); err != nil {
60 return err
61 }
62 return nil
63}
64