8 "github.com/mjl-/mox/dns"
13func (e parseErr) Error() string {
17var _ error = parseErr("")
19// ParseRecord parses an MTA-STS record.
20func ParseRecord(txt string) (record *Record, ismtasts bool, err error) {
26 if xerr, ok := x.(parseErr); ok {
28 err = fmt.Errorf("%w: %s", ErrRecordSyntax, xerr)
34 // Parsing is mostly case-sensitive.
40 seen := map[string]struct{}{}
48 // Section 3.1 about the TXT record does not say anything about duplicate fields.
49 // But section 3.2 about (parsing) policies has a paragraph that starts
50 // requirements on both TXT and policy records. That paragraph ends with a note
51 // about handling duplicate fields. Let's assume that note also applies to TXT
63 record.Extensions = append(record.Extensions, Pair{k, v})
65 if !p.delim() || p.empty() {
70 p.xerrorf("leftover characters")
73 p.xerrorf("missing id")
78// ParsePolicy parses an MTA-STS policy.
79func ParsePolicy(s string) (policy *Policy, err error) {
85 if xerr, ok := x.(parseErr); ok {
87 err = fmt.Errorf("%w: %s", ErrPolicySyntax, xerr)
98 seen := map[string]struct{}{}
108 policy.Version = p.xtake("STSv1")
110 mode := Mode(p.xtakelist("testing", "enforce", "none"))
115 maxage := p.xmaxage()
117 policy.MaxAgeSeconds = maxage
120 policy.MX = append(policy.MX, p.xmx())
122 v := p.xpolicyvalue()
123 policy.Extensions = append(policy.Extensions, Pair{k, v})
126 if !p.eol() || p.empty() {
131 p.xerrorf("leftover characters")
133 required := []string{"version", "mode", "max_age"}
134 for _, req := range required {
135 if _, ok := seen[req]; !ok {
136 p.xerrorf("missing field %q", req)
139 if _, ok := seen["mx"]; !ok && policy.Mode != ModeNone {
141 p.xerrorf("missing mx given mode")
151func newParser(s string) *parser {
155func (p *parser) xerrorf(format string, args ...any) {
156 msg := fmt.Sprintf(format, args...)
158 msg += fmt.Sprintf(" (remain %q)", p.s[p.o:])
163func (p *parser) xtake(s string) string {
165 p.xerrorf("expected %q", s)
171func (p *parser) xdelim() {
173 p.xerrorf("expected semicolon")
177func (p *parser) xtaken(n int) string {
178 r := p.s[p.o : p.o+n]
183func (p *parser) xtakefn1(fn func(rune, int) bool) string {
184 for i, b := range p.s[p.o:] {
187 p.xerrorf("expected at least one char")
193 p.xerrorf("expected at least 1 char")
195 return p.xtaken(len(p.s) - p.o)
198func (p *parser) prefix(s string) bool {
199 return strings.HasPrefix(p.s[p.o:], s)
202// File name, the known values match this syntax.
204func (p *parser) xkey() string {
205 return p.xtakefn1(func(b rune, i int) bool {
206 return i < 32 && (b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= '0' && b <= '9' || (i > 0 && b == '_' || b == '-' || b == '.'))
211func (p *parser) xid() string {
212 return p.xtakefn1(func(b rune, i int) bool {
213 return i < 32 && (b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= '0' && b <= '9')
218func (p *parser) xvalue() string {
219 return p.xtakefn1(func(b rune, i int) bool {
220 return b > ' ' && b < 0x7f && b != '=' && b != ';'
225func (p *parser) delim() bool {
228 for o < e && (p.s[o] == ' ' || p.s[o] == '\t') {
231 if o >= e || p.s[o] != ';' {
235 for o < e && (p.s[o] == ' ' || p.s[o] == '\t') {
242func (p *parser) empty() bool {
243 return p.o >= len(p.s)
247func (p *parser) eol() bool {
248 return p.take("\n") || p.take("\r\n")
251func (p *parser) xtakelist(l ...string) string {
252 for _, s := range l {
254 return p.xtaken(len(s))
257 p.xerrorf("expected one of %s", strings.Join(l, ", "))
258 return "" // not reached
262func (p *parser) xmaxage() int {
263 digits := p.xtakefn1(func(b rune, i int) bool {
264 return b >= '0' && b <= '9' && i < 10
266 v, err := strconv.ParseInt(digits, 10, 32)
268 p.xerrorf("parsing int: %s", err)
273func (p *parser) take(s string) bool {
282func (p *parser) xmx() (mx STSMX) {
287 mx.Domain = p.xdomain()
292func (p *parser) xdomain() dns.Domain {
295 s += "." + p.xsubdomain()
297 d, err := dns.ParseDomain(s)
299 p.xerrorf("parsing domain %q: %s", s, err)
305func (p *parser) xsubdomain() string {
308 s := p.xtakefn1(func(c rune, i int) bool {
312 return c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || (i > 0 && c == '-') || c > 0x7f
315 p.xerrorf("domain must be specified in A labels, not U labels (unicode)")
321func (p *parser) xpolicyvalue() string {
323 for i, c := range p.s[p.o:] {
324 if c > ' ' && c < 0x7f || c >= 0x80 || (c == ' ' && i > 0) {
330 // Walk back on trailing spaces.
331 for e > p.o && p.s[e-1] == ' ' {
336 p.xerrorf("empty extension value")
342func (p *parser) wsp() {
344 for p.o < n && (p.s[p.o] == ' ' || p.s[p.o] == '\t') {