|
| 1 | +package dkim |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + |
| 8 | + "github.com/emersion/go-message/textproto" |
| 9 | + "github.com/emersion/go-msgauth/authres" |
| 10 | + "github.com/emersion/go-msgauth/dkim" |
| 11 | + "github.com/emersion/go-smtp" |
| 12 | + "github.com/foxcpp/maddy/buffer" |
| 13 | + "github.com/foxcpp/maddy/check" |
| 14 | + "github.com/foxcpp/maddy/config" |
| 15 | + "github.com/foxcpp/maddy/log" |
| 16 | + "github.com/foxcpp/maddy/module" |
| 17 | + "github.com/foxcpp/maddy/target" |
| 18 | +) |
| 19 | + |
| 20 | +type Check struct { |
| 21 | + instName string |
| 22 | + log log.Logger |
| 23 | + |
| 24 | + brokenSigAction check.FailAction |
| 25 | + noSigAction check.FailAction |
| 26 | +} |
| 27 | + |
| 28 | +func New(_, instName string, _ []string) (module.Module, error) { |
| 29 | + return &Check{ |
| 30 | + instName: instName, |
| 31 | + log: log.Logger{Name: "verify_dkim"}, |
| 32 | + }, nil |
| 33 | +} |
| 34 | + |
| 35 | +func (c *Check) Init(cfg *config.Map) error { |
| 36 | + cfg.Bool("debug", true, false, &c.log.Debug) |
| 37 | + cfg.Custom("broken_sig_action", false, false, |
| 38 | + func() (interface{}, error) { |
| 39 | + return check.FailAction{}, nil |
| 40 | + }, check.FailActionDirective, &c.brokenSigAction) |
| 41 | + cfg.Custom("no_sig_action", false, false, |
| 42 | + func() (interface{}, error) { |
| 43 | + return check.FailAction{}, nil |
| 44 | + }, check.FailActionDirective, &c.noSigAction) |
| 45 | + _, err := cfg.Process() |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (c *Check) Name() string { |
| 53 | + return "verify_dkim" |
| 54 | +} |
| 55 | + |
| 56 | +func (c *Check) InstanceName() string { |
| 57 | + return c.instName |
| 58 | +} |
| 59 | + |
| 60 | +type dkimCheckState struct { |
| 61 | + c *Check |
| 62 | + msgMeta *module.MsgMetadata |
| 63 | + log log.Logger |
| 64 | +} |
| 65 | + |
| 66 | +func (d dkimCheckState) CheckConnection(ctx context.Context) module.CheckResult { |
| 67 | + return module.CheckResult{} |
| 68 | +} |
| 69 | + |
| 70 | +func (d dkimCheckState) CheckSender(ctx context.Context, mailFrom string) module.CheckResult { |
| 71 | + return module.CheckResult{} |
| 72 | +} |
| 73 | + |
| 74 | +func (d dkimCheckState) CheckRcpt(ctx context.Context, rcptTo string) module.CheckResult { |
| 75 | + return module.CheckResult{} |
| 76 | +} |
| 77 | + |
| 78 | +func (d dkimCheckState) CheckBody(ctx context.Context, header textproto.Header, body buffer.Buffer) module.CheckResult { |
| 79 | + if !header.Has("DKIM-Signature") { |
| 80 | + return d.c.noSigAction.Apply(module.CheckResult{ |
| 81 | + RejectErr: &smtp.SMTPError{ |
| 82 | + Code: 550, |
| 83 | + EnhancedCode: smtp.EnhancedCode{5, 7, 20}, |
| 84 | + Message: "No DKIM signatures present", |
| 85 | + }, |
| 86 | + AuthResult: []authres.Result{ |
| 87 | + &authres.DKIMResult{ |
| 88 | + Value: authres.ResultNone, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + // TODO: Optimize that so we can avoid serializing header once more. |
| 95 | + // https://github.com/emersion/go-msgauth/issues/10. |
| 96 | + b := bytes.Buffer{} |
| 97 | + textproto.WriteHeader(&b, header) |
| 98 | + bodyRdr, err := body.Open() |
| 99 | + if err != nil { |
| 100 | + return module.CheckResult{RejectErr: err} |
| 101 | + } |
| 102 | + |
| 103 | + verifications, err := dkim.Verify(io.MultiReader(&b, bodyRdr)) |
| 104 | + if err != nil { |
| 105 | + return module.CheckResult{RejectErr: err} |
| 106 | + } |
| 107 | + |
| 108 | + brokenSigs := false |
| 109 | + |
| 110 | + res := module.CheckResult{AuthResult: make([]authres.Result, 0, len(verifications))} |
| 111 | + for _, verif := range verifications { |
| 112 | + var val authres.ResultValue |
| 113 | + if verif.Err == nil { |
| 114 | + val = authres.ResultPass |
| 115 | + } else { |
| 116 | + if dkim.IsPermFail(err) { |
| 117 | + brokenSigs = true |
| 118 | + } |
| 119 | + val = authres.ResultFail |
| 120 | + } |
| 121 | + |
| 122 | + res.AuthResult = append(res.AuthResult, &authres.DKIMResult{ |
| 123 | + Value: val, |
| 124 | + Domain: verif.Domain, |
| 125 | + Identifier: verif.Identifier, |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + if brokenSigs { |
| 130 | + d.log.Println("broken signatures present") |
| 131 | + return d.c.brokenSigAction.Apply(res) |
| 132 | + } |
| 133 | + return res |
| 134 | +} |
| 135 | + |
| 136 | +func (d dkimCheckState) Close() error { |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +func (c *Check) CheckStateForMsg(msgMeta *module.MsgMetadata) (module.CheckState, error) { |
| 141 | + return dkimCheckState{ |
| 142 | + c: c, |
| 143 | + msgMeta: msgMeta, |
| 144 | + log: target.DeliveryLogger(c.log, msgMeta), |
| 145 | + }, nil |
| 146 | +} |
| 147 | + |
| 148 | +func init() { |
| 149 | + module.Register("verify_dkim", New) |
| 150 | + module.RegisterInstance(&Check{ |
| 151 | + instName: "verify_dkim", |
| 152 | + log: log.Logger{Name: "verify_dkim"}, |
| 153 | + }, &config.Map{Block: &config.Node{}}) |
| 154 | +} |
0 commit comments