| 1 |
package authres |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"regexp" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/emersion/go-message/mail" |
| 9 |
"github.com/emersion/go-msgauth/authres" |
| 10 |
) |
| 11 |
|
| 12 |
const ( |
| 13 |
AuthHeader = "Authentication-Results" |
| 14 |
) |
| 15 |
|
| 16 |
type Method string |
| 17 |
|
| 18 |
const ( |
| 19 |
DKIM Method = "dkim" |
| 20 |
SPF Method = "spf" |
| 21 |
DMARC Method = "dmarc" |
| 22 |
) |
| 23 |
|
| 24 |
type Result string |
| 25 |
|
| 26 |
const ( |
| 27 |
ResultNone Result = "none" |
| 28 |
ResultPass Result = "pass" |
| 29 |
ResultFail Result = "fail" |
| 30 |
ResultNeutral Result = "neutral" |
| 31 |
ResultPolicy Result = "policy" |
| 32 |
) |
| 33 |
|
| 34 |
type Details struct { |
| 35 |
Results []Result |
| 36 |
Infos []string |
| 37 |
Reasons []string |
| 38 |
Err error |
| 39 |
} |
| 40 |
|
| 41 |
func (d *Details) add(r Result, info string, reason string) { |
| 42 |
d.Results = append(d.Results, r) |
| 43 |
d.Infos = append(d.Infos, info) |
| 44 |
d.Reasons = append(d.Reasons, reason) |
| 45 |
} |
| 46 |
|
| 47 |
type ParserFunc func(*mail.Header, []string) (*Details, error) |
| 48 |
|
| 49 |
func New(s string) ParserFunc { |
| 50 |
if i := strings.IndexRune(s, '+'); i > 0 { |
| 51 |
s = s[:i] |
| 52 |
} |
| 53 |
m := Method(strings.ToLower(s)) |
| 54 |
switch m { |
| 55 |
case DKIM, SPF, DMARC: |
| 56 |
return CreateParser(m) |
| 57 |
} |
| 58 |
return nil |
| 59 |
} |
| 60 |
|
| 61 |
func trust(s string, trusted []string) bool { |
| 62 |
for _, t := range trusted { |
| 63 |
if matched, _ := regexp.MatchString(t, s); matched || t == "*" { |
| 64 |
return true |
| 65 |
} |
| 66 |
} |
| 67 |
return false |
| 68 |
} |
| 69 |
|
| 70 |
var cleaner = regexp.MustCompile(`(\(.*);(.*\))`) |
| 71 |
|
| 72 |
func CreateParser(m Method) func(*mail.Header, []string) (*Details, error) { |
| 73 |
return func(header *mail.Header, trusted []string) (*Details, error) { |
| 74 |
details := &Details{} |
| 75 |
found := false |
| 76 |
|
| 77 |
hf := header.FieldsByKey(AuthHeader) |
| 78 |
for hf.Next() { |
| 79 |
headerText, err := hf.Text() |
| 80 |
if err != nil { |
| 81 |
return nil, err |
| 82 |
} |
| 83 |
|
| 84 |
identifier, results, err := authres.Parse(headerText) |
| 85 |
// TODO: refactor to use errors.Is |
| 86 |
switch { |
| 87 |
case err != nil && err.Error() == "msgauth: unsupported version": |
| 88 |
// Some MTA write their authres header without an identifier |
| 89 |
// which does not conform to RFC but still exists in the wild |
| 90 |
identifier, results, err = authres.Parse("unknown;" + headerText) |
| 91 |
if err != nil { |
| 92 |
return nil, err |
| 93 |
} |
| 94 |
case err != nil && err.Error() == "msgauth: malformed authentication method and value": |
| 95 |
// the go-msgauth parser doesn't like semi-colons in the comments |
| 96 |
// as a work-around we remove those |
| 97 |
cleanHeader := cleaner.ReplaceAllString(headerText, "${1}${2}") |
| 98 |
identifier, results, err = authres.Parse(cleanHeader) |
| 99 |
if err != nil { |
| 100 |
return nil, err |
| 101 |
} |
| 102 |
case err != nil: |
| 103 |
return nil, err |
| 104 |
} |
| 105 |
|
| 106 |
// implements recommendation from RFC 7601 Sec 7.1 to |
| 107 |
// have an explicit list of trustworthy hostnames |
| 108 |
// before displaying AuthRes results |
| 109 |
if !trust(identifier, trusted) { |
| 110 |
return nil, fmt.Errorf("%s is not trusted", identifier) |
| 111 |
} |
| 112 |
|
| 113 |
for _, result := range results { |
| 114 |
switch r := result.(type) { |
| 115 |
case *authres.DKIMResult: |
| 116 |
if m == DKIM { |
| 117 |
info := r.Identifier |
| 118 |
if info == "" && r.Domain != "" { |
| 119 |
info = r.Domain |
| 120 |
} |
| 121 |
details.add(Result(r.Value), info, r.Reason) |
| 122 |
found = true |
| 123 |
} |
| 124 |
case *authres.SPFResult: |
| 125 |
if m == SPF { |
| 126 |
info := r.From |
| 127 |
if info == "" && r.Helo != "" { |
| 128 |
info = r.Helo |
| 129 |
} |
| 130 |
details.add(Result(r.Value), info, r.Reason) |
| 131 |
found = true |
| 132 |
} |
| 133 |
case *authres.DMARCResult: |
| 134 |
if m == DMARC { |
| 135 |
details.add(Result(r.Value), r.From, r.Reason) |
| 136 |
found = true |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if !found { |
| 143 |
details.add(ResultNone, "", "") |
| 144 |
} |
| 145 |
return details, nil |
| 146 |
} |
| 147 |
} |