1package message
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/mjl-/mox/dns"
8)
9
10// ../rfc/8601:577
11
12// Authentication-Results header, see RFC 8601.
13type AuthResults struct {
14 Hostname string
15 // Optional version of Authentication-Results header, assumed "1" when absent,
16 // which is common.
17 Version string
18 Comment string // If not empty, header comment without "()", added after Hostname.
19 Methods []AuthMethod // Can be empty, in case of "none".
20}
21
22// ../rfc/8601:598
23
24// AuthMethod is a result for one authentication method.
25//
26// Example encoding in the header: "spf=pass smtp.mailfrom=example.net".
27type AuthMethod struct {
28 // E.g. "dkim", "spf", "iprev", "auth".
29 Method string
30 Version string // For optional method version. "1" is implied when missing, which is common.
31 Result string // Each method has a set of known values, e.g. "pass", "temperror", etc.
32 Comment string // Optional, message header comment.
33 Reason string // Optional.
34 Props []AuthProp
35}
36
37// ../rfc/8601:606
38
39// AuthProp describes properties for an authentication method.
40// Each method has a set of known properties.
41// Encoded in the header as "type.property=value", e.g. "smtp.mailfrom=example.net"
42// for spf.
43type AuthProp struct {
44 // Valid values maintained at https://www.iana.org/assignments/email-auth/email-auth.xhtml
45 Type string
46 Property string
47 Value string
48 // Whether value is address-like (localpart@domain, or domain). Or another value,
49 // which is subject to escaping.
50 IsAddrLike bool
51 Comment string // If not empty, header comment without "()", added after Value.
52}
53
54// MakeAuthProp is a convenient way to make an AuthProp.
55func MakeAuthProp(typ, property, value string, isAddrLike bool, Comment string) AuthProp {
56 return AuthProp{typ, property, value, isAddrLike, Comment}
57}
58
59// todo future: we could store fields as dns.Domain, and when we encode as non-ascii also add the ascii version as a comment.
60
61// Header returns an Authentication-Results header, possibly spanning multiple
62// lines, always ending in crlf.
63func (h AuthResults) Header() string {
64 // Escaping of values: ../rfc/8601:684 ../rfc/2045:661
65
66 optComment := func(s string) string {
67 if s != "" {
68 return " (" + s + ")"
69 }
70 return s
71 }
72
73 w := &HeaderWriter{}
74 w.Add("", "Authentication-Results:"+optComment(h.Comment)+" "+value(h.Hostname, false)+";")
75 for i, m := range h.Methods {
76 w.Newline()
77
78 tokens := []string{}
79 addf := func(format string, args ...any) {
80 s := fmt.Sprintf(format, args...)
81 tokens = append(tokens, s)
82 }
83 addf("%s=%s", m.Method, m.Result)
84 if m.Comment != "" && (m.Reason != "" || len(m.Props) > 0) {
85 addf("(%s)", m.Comment)
86 }
87 if m.Reason != "" {
88 addf("reason=%s", value(m.Reason, false))
89 }
90 for _, p := range m.Props {
91 v := value(p.Value, p.IsAddrLike)
92 addf("%s.%s=%s%s", p.Type, p.Property, v, optComment(p.Comment))
93 }
94 for j, t := range tokens {
95 var sep string
96 if j > 0 {
97 sep = " "
98 }
99 if j == len(tokens)-1 && i < len(h.Methods)-1 {
100 t += ";"
101 }
102 w.Add(sep, t)
103 }
104 }
105 return w.String()
106}
107
108func value(s string, isAddrLike bool) string {
109 quote := s == ""
110 for _, c := range s {
111 // utf-8 does not have to be quoted. ../rfc/6532:242
112 // Characters outside of tokens do. ../rfc/2045:661
113 if c <= ' ' || c == 0x7f || (c == '@' && !isAddrLike) || strings.ContainsRune(`()<>,;:\\"/[]?= `, c) {
114 quote = true
115 break
116 }
117 }
118 if !quote {
119 return s
120 }
121 r := `"`
122 for _, c := range s {
123 if c == '"' || c == '\\' {
124 r += "\\"
125 }
126 r += string(c)
127 }
128 r += `"`
129 return r
130}
131
132// ParseAuthResults parses a Authentication-Results header value.
133//
134// Comments are not populated in the returned AuthResults.
135// Both crlf and lf line-endings are accepted. The input string must end with
136// either crlf or lf.
137func ParseAuthResults(s string) (ar AuthResults, err error) {
138 // ../rfc/8601:577
139 lower := make([]byte, len(s))
140 for i, c := range []byte(s) {
141 if c >= 'A' && c <= 'Z' {
142 c += 'a' - 'A'
143 }
144 lower[i] = c
145 }
146 p := &parser{s: s, lower: string(lower)}
147 defer p.recover(&err)
148
149 p.cfws()
150 ar.Hostname = p.xvalue()
151 p.cfws()
152 ar.Version = p.digits()
153 p.cfws()
154 for {
155 p.xtake(";")
156 p.cfws()
157 // Yahoo has ";" at the end of the header value, incorrect.
158 if !Pedantic && p.end() {
159 break
160 }
161 method := p.xkeyword(false)
162 p.cfws()
163 if method == "none" {
164 if len(ar.Methods) == 0 {
165 p.xerrorf("missing results")
166 }
167 if !p.end() {
168 p.xerrorf(`data after "none" result`)
169 }
170 return
171 }
172 ar.Methods = append(ar.Methods, p.xresinfo(method))
173 p.cfws()
174 if p.end() {
175 break
176 }
177 }
178 return
179}
180
181type parser struct {
182 s string
183 lower string // Like s, but with ascii characters lower-cased (utf-8 offsets preserved).
184 o int
185}
186
187type parseError struct{ err error }
188
189func (p *parser) recover(err *error) {
190 x := recover()
191 if x == nil {
192 return
193 }
194 perr, ok := x.(parseError)
195 if ok {
196 *err = perr.err
197 return
198 }
199 panic(x)
200}
201
202func (p *parser) xerrorf(format string, args ...any) {
203 panic(parseError{fmt.Errorf(format, args...)})
204}
205
206func (p *parser) end() bool {
207 return p.s[p.o:] == "\r\n" || p.s[p.o:] == "\n"
208}
209
210// ../rfc/5322:599
211func (p *parser) cfws() {
212 p.fws()
213 for p.prefix("(") {
214 p.xcomment()
215 }
216 p.fws()
217}
218
219func (p *parser) fws() {
220 for p.take(" ") || p.take("\t") {
221 }
222 opts := []string{"\n ", "\n\t", "\r\n ", "\r\n\t"}
223 for _, o := range opts {
224 if p.take(o) {
225 break
226 }
227 }
228 for p.take(" ") || p.take("\t") {
229 }
230}
231
232func (p *parser) xcomment() {
233 p.xtake("(")
234 p.fws()
235 for !p.take(")") {
236 if p.empty() {
237 p.xerrorf("unexpected end in comment")
238 }
239 if p.prefix("(") {
240 p.xcomment()
241 p.fws()
242 continue
243 }
244 p.take(`\`)
245 if c := p.s[p.o]; c > ' ' && c < 0x7f {
246 p.o++
247 } else {
248 p.xerrorf("bad character %c in comment", c)
249 }
250 p.fws()
251 }
252}
253
254func (p *parser) prefix(s string) bool {
255 return strings.HasPrefix(p.lower[p.o:], s)
256}
257
258func (p *parser) xvalue() string {
259 if p.prefix(`"`) {
260 return p.xquotedString()
261 }
262 return p.xtakefn1("value token", func(c rune, i int) bool {
263 // ../rfc/2045:661
264 // todo: token cannot contain utf-8? not updated in ../rfc/6532. however, we also use it for the localpart & domain parsing, so we'll allow it.
265 return c > ' ' && !strings.ContainsRune(`()<>@,;:\\"/[]?= `, c)
266 })
267}
268
269func (p *parser) xchar() rune {
270 // We are careful to track invalid utf-8 properly.
271 if p.empty() {
272 p.xerrorf("need another character")
273 }
274 var r rune
275 var o int
276 for i, c := range p.s[p.o:] {
277 if i > 0 {
278 o = i
279 break
280 }
281 r = c
282 }
283 if o == 0 {
284 p.o = len(p.s)
285 } else {
286 p.o += o
287 }
288 return r
289}
290
291func (p *parser) xquotedString() string {
292 p.xtake(`"`)
293 var s string
294 var esc bool
295 for {
296 c := p.xchar()
297 if esc {
298 if c >= ' ' && c < 0x7f {
299 s += string(c)
300 esc = false
301 continue
302 }
303 p.xerrorf("bad escaped char %c in quoted string", c)
304 }
305 if c == '\\' {
306 esc = true
307 continue
308 }
309 if c == '"' {
310 return s
311 }
312 if c >= ' ' && c != '\\' && c != '"' {
313 s += string(c)
314 continue
315 }
316 p.xerrorf("invalid quoted string, invalid character %c", c)
317 }
318}
319
320func (p *parser) digits() string {
321 o := p.o
322 for o < len(p.s) && p.s[o] >= '0' && p.s[o] <= '9' {
323 o++
324 }
325 p.o = o
326 return p.s[o:p.o]
327}
328
329func (p *parser) xdigits() string {
330 s := p.digits()
331 if s == "" {
332 p.xerrorf("expected digits, remaining %q", p.s[p.o:])
333 }
334 return s
335}
336
337func (p *parser) xtake(s string) {
338 if !p.prefix(s) {
339 p.xerrorf("expected %q, remaining %q", s, p.s[p.o:])
340 }
341 p.o += len(s)
342}
343
344func (p *parser) empty() bool {
345 return p.o >= len(p.s)
346}
347
348func (p *parser) take(s string) bool {
349 if p.prefix(s) {
350 p.o += len(s)
351 return true
352 }
353 return false
354}
355
356func (p *parser) xtakefn1(what string, fn func(c rune, i int) bool) string {
357 if p.empty() {
358 p.xerrorf("need at least one char for %s", what)
359 }
360 for i, c := range p.s[p.o:] {
361 if !fn(c, i) {
362 if i == 0 {
363 p.xerrorf("expected at least one char for %s, remaining %q", what, p.s[p.o:])
364 }
365 s := p.s[p.o : p.o+i]
366 p.o += i
367 return s
368 }
369 }
370 s := p.s[p.o:]
371 p.o = len(p.s)
372 return s
373}
374
375// ../rfc/5321:2287
376func (p *parser) xkeyword(isResult bool) string {
377 s := strings.ToLower(p.xtakefn1("keyword", func(c rune, i int) bool {
378 // Yahoo sends results like "dkim=perm_fail".
379 return c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-' || isResult && !Pedantic && c == '_'
380 }))
381 if s == "-" {
382 p.xerrorf("missing keyword")
383 } else if strings.HasSuffix(s, "-") {
384 p.o--
385 s = s[:len(s)-1]
386 }
387 return s
388}
389
390func (p *parser) xmethodspec(methodKeyword string) (string, string, string) {
391 p.cfws()
392 var methodDigits string
393 if p.take("/") {
394 methodDigits = p.xdigits()
395 p.cfws()
396 }
397 p.xtake("=")
398 p.cfws()
399 result := p.xkeyword(true)
400 return methodKeyword, methodDigits, result
401}
402
403func (p *parser) xpropspec() (ap AuthProp) {
404 ap.Type = p.xkeyword(false)
405 p.cfws()
406 p.xtake(".")
407 p.cfws()
408 if p.take("mailfrom") {
409 ap.Property = "mailfrom"
410 } else if p.take("rcptto") {
411 ap.Property = "rcptto"
412 } else {
413 ap.Property = p.xkeyword(false)
414 }
415 p.cfws()
416 p.xtake("=")
417 ap.IsAddrLike, ap.Value = p.xpvalue()
418 return
419}
420
421// method keyword has been parsed, method-version not yet.
422func (p *parser) xresinfo(methodKeyword string) (am AuthMethod) {
423 p.cfws()
424 am.Method, am.Version, am.Result = p.xmethodspec(methodKeyword)
425 p.cfws()
426 if p.take("reason") {
427 p.cfws()
428 am.Reason = p.xvalue()
429 }
430 p.cfws()
431 for !p.prefix(";") && !p.end() {
432 am.Props = append(am.Props, p.xpropspec())
433 p.cfws()
434 }
435 return
436}
437
438// todo: could keep track whether this is a localpart.
439func (p *parser) xpvalue() (bool, string) {
440 p.cfws()
441 if p.take("@") {
442 // Bare domain.
443 dom, _ := p.xdomain()
444 return true, "@" + dom
445 }
446 s := p.xvalue()
447 if p.take("@") {
448 dom, _ := p.xdomain()
449 s += "@" + dom
450 return true, s
451 }
452 return false, s
453}
454
455// ../rfc/5321:2291
456func (p *parser) xdomain() (string, dns.Domain) {
457 s := p.xsubdomain()
458 for p.take(".") {
459 s += "." + p.xsubdomain()
460 }
461 d, err := dns.ParseDomain(s)
462 if err != nil {
463 p.xerrorf("parsing domain name %q: %s", s, err)
464 }
465 if len(s) > 255 {
466 // ../rfc/5321:3491
467 p.xerrorf("domain longer than 255 octets")
468 }
469 return s, d
470}
471
472// ../rfc/5321:2303
473// ../rfc/5321:2303 ../rfc/6531:411
474func (p *parser) xsubdomain() string {
475 return p.xtakefn1("subdomain", func(c rune, i int) bool {
476 return c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || i > 0 && c == '-' || c > 0x7f
477 })
478}
479