add webmail
it was far down on the roadmap, but implemented earlier, because it's
interesting, and to help prepare for a jmap implementation. for jmap we need to
implement more client-like functionality than with just imap. internal data
structures need to change. jmap has lots of other requirements, so it's already
a big project. by implementing a webmail now, some of the required data
structure changes become clear and can be made now, so the later jmap
implementation can do things similarly to the webmail code. the webmail
frontend and webmail are written together, making their interface/api much
smaller and simpler than jmap.
one of the internal changes is that we now keep track of per-mailbox
total/unread/unseen/deleted message counts and mailbox sizes. keeping this
data consistent after any change to the stored messages (through the code base)
is tricky, so mox now has a consistency check that verifies the counts are
correct, which runs only during tests, each time an internal account reference
is closed. we have a few more internal "changes" that are propagated for the
webmail frontend (that imap doesn't have a way to propagate on a connection),
like changes to the special-use flags on mailboxes, and used keywords in a
mailbox. more changes that will be required have revealed themselves while
implementing the webmail, and will be implemented next.
the webmail user interface is modeled after the mail clients i use or have
used: thunderbird, macos mail, mutt; and webmails i normally only use for
testing: gmail, proton, yahoo, outlook. a somewhat technical user is assumed,
but still the goal is to make this webmail client easy to use for everyone. the
user interface looks like most other mail clients: a list of mailboxes, a
search bar, a message list view, and message details. there is a top/bottom and
a left/right layout for the list/message view, default is automatic based on
screen size. the panes can be resized by the user. buttons for actions are just
text, not icons. clicking a button briefly shows the shortcut for the action in
the bottom right, helping with learning to operate quickly. any text that is
underdotted has a title attribute that causes more information to be displayed,
e.g. what a button does or a field is about. to highlight potential phishing
attempts, any text (anywhere in the webclient) that switches unicode "blocks"
(a rough approximation to (language) scripts) within a word is underlined
orange. multiple messages can be selected with familiar ui interaction:
clicking while holding control and/or shift keys. keyboard navigation works
with arrows/page up/down and home/end keys, and also with a few basic vi-like
keys for list/message navigation. we prefer showing the text instead of
html (with inlined images only) version of a message. html messages are shown
in an iframe served from an endpoint with CSP headers to prevent dangerous
resources (scripts, external images) from being loaded. the html is also
sanitized, with javascript removed. a user can choose to load external
resources (e.g. images for tracking purposes).
the frontend is just (strict) typescript, no external frameworks. all
incoming/outgoing data is typechecked, both the api request parameters and
response types, and the data coming in over SSE. the types and checking code
are generated with sherpats, which uses the api definitions generated by
sherpadoc based on the Go code. so types from the backend are automatically
propagated to the frontend. since there is no framework to automatically
propagate properties and rerender components, changes coming in over the SSE
connection are propagated explicitly with regular function calls. the ui is
separated into "views", each with a "root" dom element that is added to the
visible document. these views have additional functions for getting changes
propagated, often resulting in the view updating its (internal) ui state (dom).
we keep the frontend compilation simple, it's just a few typescript files that
get compiled (combined and types stripped) into a single js file, no additional
runtime code needed or complicated build processes used. the webmail is served
is served from a compressed, cachable html file that includes style and the
javascript, currently just over 225kb uncompressed, under 60kb compressed (not
minified, including comments). we include the generated js files in the
repository, to keep Go's easily buildable self-contained binaries.
authentication is basic http, as with the account and admin pages. most data
comes in over one long-term SSE connection to the backend. api requests signal
which mailbox/search/messages are requested over the SSE connection. fetching
individual messages, and making changes, are done through api calls. the
operations are similar to imap, so some code has been moved from package
imapserver to package store. the future jmap implementation will benefit from
these changes too. more functionality will probably be moved to the store
package in the future.
the quickstart enables webmail on the internal listener by default (for new
installs). users can enable it on the public listener if they want to. mox
localserve enables it too. to enable webmail on existing installs, add settings
like the following to the listeners in mox.conf, similar to AccountHTTP(S):
WebmailHTTP:
Enabled: true
WebmailHTTPS:
Enabled: true
special thanks to liesbeth, gerben, andrii for early user feedback.
there is plenty still to do, see the list at the top of webmail/webmail.ts.
feedback welcome as always.
2023-08-07 22:57:03 +03:00
|
|
|
package message
|
2023-01-30 16:27:06 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-13 19:35:53 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/mjl-/mox/dns"
|
2023-01-30 16:27:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// ../rfc/8601:577
|
|
|
|
|
|
|
|
// Authentication-Results header, see RFC 8601.
|
|
|
|
type AuthResults struct {
|
|
|
|
Hostname string
|
2024-03-13 19:35:53 +03:00
|
|
|
// Optional version of Authentication-Results header, assumed "1" when absent,
|
|
|
|
// which is common.
|
|
|
|
Version string
|
|
|
|
Comment string // If not empty, header comment without "()", added after Hostname.
|
|
|
|
Methods []AuthMethod // Can be empty, in case of "none".
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ../rfc/8601:598
|
|
|
|
|
|
|
|
// AuthMethod is a result for one authentication method.
|
|
|
|
//
|
|
|
|
// Example encoding in the header: "spf=pass smtp.mailfrom=example.net".
|
|
|
|
type AuthMethod struct {
|
|
|
|
// E.g. "dkim", "spf", "iprev", "auth".
|
|
|
|
Method string
|
2024-03-13 19:35:53 +03:00
|
|
|
Version string // For optional method version. "1" is implied when missing, which is common.
|
2023-01-30 16:27:06 +03:00
|
|
|
Result string // Each method has a set of known values, e.g. "pass", "temperror", etc.
|
|
|
|
Comment string // Optional, message header comment.
|
|
|
|
Reason string // Optional.
|
|
|
|
Props []AuthProp
|
|
|
|
}
|
|
|
|
|
|
|
|
// ../rfc/8601:606
|
|
|
|
|
|
|
|
// AuthProp describes properties for an authentication method.
|
|
|
|
// Each method has a set of known properties.
|
|
|
|
// Encoded in the header as "type.property=value", e.g. "smtp.mailfrom=example.net"
|
|
|
|
// for spf.
|
|
|
|
type AuthProp struct {
|
|
|
|
// Valid values maintained at https://www.iana.org/assignments/email-auth/email-auth.xhtml
|
|
|
|
Type string
|
|
|
|
Property string
|
|
|
|
Value string
|
|
|
|
// Whether value is address-like (localpart@domain, or domain). Or another value,
|
|
|
|
// which is subject to escaping.
|
|
|
|
IsAddrLike bool
|
implement dnssec-awareness throughout code, and dane for incoming/outgoing mail delivery
the vendored dns resolver code is a copy of the go stdlib dns resolver, with
awareness of the "authentic data" (i.e. dnssec secure) added, as well as support
for enhanced dns errors, and looking up tlsa records (for dane). ideally it
would be upstreamed, but the chances seem slim.
dnssec-awareness is added to all packages, e.g. spf, dkim, dmarc, iprev. their
dnssec status is added to the Received message headers for incoming email.
but the main reason to add dnssec was for implementing dane. with dane, the
verification of tls certificates can be done through certificates/public keys
published in dns (in the tlsa records). this only makes sense (is trustworthy)
if those dns records can be verified to be authentic.
mox now applies dane to delivering messages over smtp. mox already implemented
mta-sts for webpki/pkix-verification of certificates against the (large) pool
of CA's, and still enforces those policies when present. but it now also checks
for dane records, and will verify those if present. if dane and mta-sts are
both absent, the regular opportunistic tls with starttls is still done. and the
fallback to plaintext is also still done.
mox also makes it easy to setup dane for incoming deliveries, so other servers
can deliver with dane tls certificate verification. the quickstart now
generates private keys that are used when requesting certificates with acme.
the private keys are pre-generated because they must be static and known during
setup, because their public keys must be published in tlsa records in dns.
autocert would generate private keys on its own, so had to be forked to add the
option to provide the private key when requesting a new certificate. hopefully
upstream will accept the change and we can drop the fork.
with this change, using the quickstart to setup a new mox instance, the checks
at internet.nl result in a 100% score, provided the domain is dnssec-signed and
the network doesn't have any issues.
2023-10-10 13:09:35 +03:00
|
|
|
Comment string // If not empty, header comment without "()", added after Value.
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
|
|
|
|
add webmail
it was far down on the roadmap, but implemented earlier, because it's
interesting, and to help prepare for a jmap implementation. for jmap we need to
implement more client-like functionality than with just imap. internal data
structures need to change. jmap has lots of other requirements, so it's already
a big project. by implementing a webmail now, some of the required data
structure changes become clear and can be made now, so the later jmap
implementation can do things similarly to the webmail code. the webmail
frontend and webmail are written together, making their interface/api much
smaller and simpler than jmap.
one of the internal changes is that we now keep track of per-mailbox
total/unread/unseen/deleted message counts and mailbox sizes. keeping this
data consistent after any change to the stored messages (through the code base)
is tricky, so mox now has a consistency check that verifies the counts are
correct, which runs only during tests, each time an internal account reference
is closed. we have a few more internal "changes" that are propagated for the
webmail frontend (that imap doesn't have a way to propagate on a connection),
like changes to the special-use flags on mailboxes, and used keywords in a
mailbox. more changes that will be required have revealed themselves while
implementing the webmail, and will be implemented next.
the webmail user interface is modeled after the mail clients i use or have
used: thunderbird, macos mail, mutt; and webmails i normally only use for
testing: gmail, proton, yahoo, outlook. a somewhat technical user is assumed,
but still the goal is to make this webmail client easy to use for everyone. the
user interface looks like most other mail clients: a list of mailboxes, a
search bar, a message list view, and message details. there is a top/bottom and
a left/right layout for the list/message view, default is automatic based on
screen size. the panes can be resized by the user. buttons for actions are just
text, not icons. clicking a button briefly shows the shortcut for the action in
the bottom right, helping with learning to operate quickly. any text that is
underdotted has a title attribute that causes more information to be displayed,
e.g. what a button does or a field is about. to highlight potential phishing
attempts, any text (anywhere in the webclient) that switches unicode "blocks"
(a rough approximation to (language) scripts) within a word is underlined
orange. multiple messages can be selected with familiar ui interaction:
clicking while holding control and/or shift keys. keyboard navigation works
with arrows/page up/down and home/end keys, and also with a few basic vi-like
keys for list/message navigation. we prefer showing the text instead of
html (with inlined images only) version of a message. html messages are shown
in an iframe served from an endpoint with CSP headers to prevent dangerous
resources (scripts, external images) from being loaded. the html is also
sanitized, with javascript removed. a user can choose to load external
resources (e.g. images for tracking purposes).
the frontend is just (strict) typescript, no external frameworks. all
incoming/outgoing data is typechecked, both the api request parameters and
response types, and the data coming in over SSE. the types and checking code
are generated with sherpats, which uses the api definitions generated by
sherpadoc based on the Go code. so types from the backend are automatically
propagated to the frontend. since there is no framework to automatically
propagate properties and rerender components, changes coming in over the SSE
connection are propagated explicitly with regular function calls. the ui is
separated into "views", each with a "root" dom element that is added to the
visible document. these views have additional functions for getting changes
propagated, often resulting in the view updating its (internal) ui state (dom).
we keep the frontend compilation simple, it's just a few typescript files that
get compiled (combined and types stripped) into a single js file, no additional
runtime code needed or complicated build processes used. the webmail is served
is served from a compressed, cachable html file that includes style and the
javascript, currently just over 225kb uncompressed, under 60kb compressed (not
minified, including comments). we include the generated js files in the
repository, to keep Go's easily buildable self-contained binaries.
authentication is basic http, as with the account and admin pages. most data
comes in over one long-term SSE connection to the backend. api requests signal
which mailbox/search/messages are requested over the SSE connection. fetching
individual messages, and making changes, are done through api calls. the
operations are similar to imap, so some code has been moved from package
imapserver to package store. the future jmap implementation will benefit from
these changes too. more functionality will probably be moved to the store
package in the future.
the quickstart enables webmail on the internal listener by default (for new
installs). users can enable it on the public listener if they want to. mox
localserve enables it too. to enable webmail on existing installs, add settings
like the following to the listeners in mox.conf, similar to AccountHTTP(S):
WebmailHTTP:
Enabled: true
WebmailHTTPS:
Enabled: true
special thanks to liesbeth, gerben, andrii for early user feedback.
there is plenty still to do, see the list at the top of webmail/webmail.ts.
feedback welcome as always.
2023-08-07 22:57:03 +03:00
|
|
|
// MakeAuthProp is a convenient way to make an AuthProp.
|
|
|
|
func MakeAuthProp(typ, property, value string, isAddrLike bool, Comment string) AuthProp {
|
|
|
|
return AuthProp{typ, property, value, isAddrLike, Comment}
|
|
|
|
}
|
|
|
|
|
2023-01-30 16:27:06 +03:00
|
|
|
// todo future: we could store fields as dns.Domain, and when we encode as non-ascii also add the ascii version as a comment.
|
|
|
|
|
|
|
|
// Header returns an Authentication-Results header, possibly spanning multiple
|
|
|
|
// lines, always ending in crlf.
|
|
|
|
func (h AuthResults) Header() string {
|
|
|
|
// Escaping of values: ../rfc/8601:684 ../rfc/2045:661
|
|
|
|
|
|
|
|
optComment := func(s string) string {
|
|
|
|
if s != "" {
|
|
|
|
return " (" + s + ")"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
add webmail
it was far down on the roadmap, but implemented earlier, because it's
interesting, and to help prepare for a jmap implementation. for jmap we need to
implement more client-like functionality than with just imap. internal data
structures need to change. jmap has lots of other requirements, so it's already
a big project. by implementing a webmail now, some of the required data
structure changes become clear and can be made now, so the later jmap
implementation can do things similarly to the webmail code. the webmail
frontend and webmail are written together, making their interface/api much
smaller and simpler than jmap.
one of the internal changes is that we now keep track of per-mailbox
total/unread/unseen/deleted message counts and mailbox sizes. keeping this
data consistent after any change to the stored messages (through the code base)
is tricky, so mox now has a consistency check that verifies the counts are
correct, which runs only during tests, each time an internal account reference
is closed. we have a few more internal "changes" that are propagated for the
webmail frontend (that imap doesn't have a way to propagate on a connection),
like changes to the special-use flags on mailboxes, and used keywords in a
mailbox. more changes that will be required have revealed themselves while
implementing the webmail, and will be implemented next.
the webmail user interface is modeled after the mail clients i use or have
used: thunderbird, macos mail, mutt; and webmails i normally only use for
testing: gmail, proton, yahoo, outlook. a somewhat technical user is assumed,
but still the goal is to make this webmail client easy to use for everyone. the
user interface looks like most other mail clients: a list of mailboxes, a
search bar, a message list view, and message details. there is a top/bottom and
a left/right layout for the list/message view, default is automatic based on
screen size. the panes can be resized by the user. buttons for actions are just
text, not icons. clicking a button briefly shows the shortcut for the action in
the bottom right, helping with learning to operate quickly. any text that is
underdotted has a title attribute that causes more information to be displayed,
e.g. what a button does or a field is about. to highlight potential phishing
attempts, any text (anywhere in the webclient) that switches unicode "blocks"
(a rough approximation to (language) scripts) within a word is underlined
orange. multiple messages can be selected with familiar ui interaction:
clicking while holding control and/or shift keys. keyboard navigation works
with arrows/page up/down and home/end keys, and also with a few basic vi-like
keys for list/message navigation. we prefer showing the text instead of
html (with inlined images only) version of a message. html messages are shown
in an iframe served from an endpoint with CSP headers to prevent dangerous
resources (scripts, external images) from being loaded. the html is also
sanitized, with javascript removed. a user can choose to load external
resources (e.g. images for tracking purposes).
the frontend is just (strict) typescript, no external frameworks. all
incoming/outgoing data is typechecked, both the api request parameters and
response types, and the data coming in over SSE. the types and checking code
are generated with sherpats, which uses the api definitions generated by
sherpadoc based on the Go code. so types from the backend are automatically
propagated to the frontend. since there is no framework to automatically
propagate properties and rerender components, changes coming in over the SSE
connection are propagated explicitly with regular function calls. the ui is
separated into "views", each with a "root" dom element that is added to the
visible document. these views have additional functions for getting changes
propagated, often resulting in the view updating its (internal) ui state (dom).
we keep the frontend compilation simple, it's just a few typescript files that
get compiled (combined and types stripped) into a single js file, no additional
runtime code needed or complicated build processes used. the webmail is served
is served from a compressed, cachable html file that includes style and the
javascript, currently just over 225kb uncompressed, under 60kb compressed (not
minified, including comments). we include the generated js files in the
repository, to keep Go's easily buildable self-contained binaries.
authentication is basic http, as with the account and admin pages. most data
comes in over one long-term SSE connection to the backend. api requests signal
which mailbox/search/messages are requested over the SSE connection. fetching
individual messages, and making changes, are done through api calls. the
operations are similar to imap, so some code has been moved from package
imapserver to package store. the future jmap implementation will benefit from
these changes too. more functionality will probably be moved to the store
package in the future.
the quickstart enables webmail on the internal listener by default (for new
installs). users can enable it on the public listener if they want to. mox
localserve enables it too. to enable webmail on existing installs, add settings
like the following to the listeners in mox.conf, similar to AccountHTTP(S):
WebmailHTTP:
Enabled: true
WebmailHTTPS:
Enabled: true
special thanks to liesbeth, gerben, andrii for early user feedback.
there is plenty still to do, see the list at the top of webmail/webmail.ts.
feedback welcome as always.
2023-08-07 22:57:03 +03:00
|
|
|
w := &HeaderWriter{}
|
2024-03-13 19:35:53 +03:00
|
|
|
w.Add("", "Authentication-Results:"+optComment(h.Comment)+" "+value(h.Hostname, false)+";")
|
2023-01-30 16:27:06 +03:00
|
|
|
for i, m := range h.Methods {
|
2023-12-14 17:14:07 +03:00
|
|
|
w.Newline()
|
|
|
|
|
2023-01-30 16:27:06 +03:00
|
|
|
tokens := []string{}
|
|
|
|
addf := func(format string, args ...any) {
|
|
|
|
s := fmt.Sprintf(format, args...)
|
|
|
|
tokens = append(tokens, s)
|
|
|
|
}
|
|
|
|
addf("%s=%s", m.Method, m.Result)
|
|
|
|
if m.Comment != "" && (m.Reason != "" || len(m.Props) > 0) {
|
|
|
|
addf("(%s)", m.Comment)
|
|
|
|
}
|
|
|
|
if m.Reason != "" {
|
2024-03-13 19:35:53 +03:00
|
|
|
addf("reason=%s", value(m.Reason, false))
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
|
|
|
for _, p := range m.Props {
|
2024-03-13 19:35:53 +03:00
|
|
|
v := value(p.Value, p.IsAddrLike)
|
2023-01-30 16:27:06 +03:00
|
|
|
addf("%s.%s=%s%s", p.Type, p.Property, v, optComment(p.Comment))
|
|
|
|
}
|
|
|
|
for j, t := range tokens {
|
2023-12-14 17:14:07 +03:00
|
|
|
var sep string
|
|
|
|
if j > 0 {
|
|
|
|
sep = " "
|
|
|
|
}
|
2023-01-30 16:27:06 +03:00
|
|
|
if j == len(tokens)-1 && i < len(h.Methods)-1 {
|
|
|
|
t += ";"
|
|
|
|
}
|
2023-12-14 17:14:07 +03:00
|
|
|
w.Add(sep, t)
|
2023-01-30 16:27:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.String()
|
|
|
|
}
|
|
|
|
|
2024-03-13 19:35:53 +03:00
|
|
|
func value(s string, isAddrLike bool) string {
|
2023-01-30 16:27:06 +03:00
|
|
|
quote := s == ""
|
|
|
|
for _, c := range s {
|
|
|
|
// utf-8 does not have to be quoted. ../rfc/6532:242
|
2024-03-13 19:35:53 +03:00
|
|
|
// Characters outside of tokens do. ../rfc/2045:661
|
|
|
|
if c <= ' ' || c == 0x7f || (c == '@' && !isAddrLike) || strings.ContainsRune(`()<>,;:\\"/[]?= `, c) {
|
2023-01-30 16:27:06 +03:00
|
|
|
quote = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !quote {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
r := `"`
|
|
|
|
for _, c := range s {
|
|
|
|
if c == '"' || c == '\\' {
|
|
|
|
r += "\\"
|
|
|
|
}
|
|
|
|
r += string(c)
|
|
|
|
}
|
|
|
|
r += `"`
|
|
|
|
return r
|
|
|
|
}
|
2024-03-13 19:35:53 +03:00
|
|
|
|
|
|
|
// ParseAuthResults parses a Authentication-Results header value.
|
|
|
|
//
|
|
|
|
// Comments are not populated in the returned AuthResults.
|
|
|
|
// Both crlf and lf line-endings are accepted. The input string must end with
|
|
|
|
// either crlf or lf.
|
|
|
|
func ParseAuthResults(s string) (ar AuthResults, err error) {
|
|
|
|
// ../rfc/8601:577
|
|
|
|
lower := make([]byte, len(s))
|
|
|
|
for i, c := range []byte(s) {
|
|
|
|
if c >= 'A' && c <= 'Z' {
|
|
|
|
c += 'a' - 'A'
|
|
|
|
}
|
|
|
|
lower[i] = c
|
|
|
|
}
|
|
|
|
p := &parser{s: s, lower: string(lower)}
|
|
|
|
defer p.recover(&err)
|
|
|
|
|
|
|
|
p.cfws()
|
|
|
|
ar.Hostname = p.xvalue()
|
|
|
|
p.cfws()
|
|
|
|
ar.Version = p.digits()
|
|
|
|
p.cfws()
|
|
|
|
for {
|
|
|
|
p.xtake(";")
|
|
|
|
p.cfws()
|
|
|
|
// Yahoo has ";" at the end of the header value, incorrect.
|
|
|
|
if !Pedantic && p.end() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
method := p.xkeyword(false)
|
|
|
|
p.cfws()
|
|
|
|
if method == "none" {
|
|
|
|
if len(ar.Methods) == 0 {
|
|
|
|
p.xerrorf("missing results")
|
|
|
|
}
|
|
|
|
if !p.end() {
|
|
|
|
p.xerrorf(`data after "none" result`)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ar.Methods = append(ar.Methods, p.xresinfo(method))
|
|
|
|
p.cfws()
|
|
|
|
if p.end() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type parser struct {
|
|
|
|
s string
|
|
|
|
lower string // Like s, but with ascii characters lower-cased (utf-8 offsets preserved).
|
|
|
|
o int
|
|
|
|
}
|
|
|
|
|
|
|
|
type parseError struct{ err error }
|
|
|
|
|
|
|
|
func (p *parser) recover(err *error) {
|
|
|
|
x := recover()
|
|
|
|
if x == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
perr, ok := x.(parseError)
|
|
|
|
if ok {
|
|
|
|
*err = perr.err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
panic(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) xerrorf(format string, args ...any) {
|
|
|
|
panic(parseError{fmt.Errorf(format, args...)})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) end() bool {
|
|
|
|
return p.s[p.o:] == "\r\n" || p.s[p.o:] == "\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ../rfc/5322:599
|
|
|
|
func (p *parser) cfws() {
|
|
|
|
p.fws()
|
|
|
|
for p.prefix("(") {
|
|
|
|
p.xcomment()
|
|
|
|
}
|
|
|
|
p.fws()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) fws() {
|
|
|
|
for p.take(" ") || p.take("\t") {
|
|
|
|
}
|
|
|
|
opts := []string{"\n ", "\n\t", "\r\n ", "\r\n\t"}
|
|
|
|
for _, o := range opts {
|
|
|
|
if p.take(o) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for p.take(" ") || p.take("\t") {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) xcomment() {
|
|
|
|
p.xtake("(")
|
|
|
|
p.fws()
|
|
|
|
for !p.take(")") {
|
|
|
|
if p.empty() {
|
|
|
|
p.xerrorf("unexpected end in comment")
|
|
|
|
}
|
|
|
|
if p.prefix("(") {
|
|
|
|
p.xcomment()
|
|
|
|
p.fws()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.take(`\`)
|
|
|
|
if c := p.s[p.o]; c > ' ' && c < 0x7f {
|
|
|
|
p.o++
|
|
|
|
} else {
|
|
|
|
p.xerrorf("bad character %c in comment", c)
|
|
|
|
}
|
|
|
|
p.fws()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) prefix(s string) bool {
|
|
|
|
return strings.HasPrefix(p.lower[p.o:], s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) xvalue() string {
|
|
|
|
if p.prefix(`"`) {
|
|
|
|
return p.xquotedString()
|
|
|
|
}
|
|
|
|
return p.xtakefn1("value token", func(c rune, i int) bool {
|
|
|
|
// ../rfc/2045:661
|
|
|
|
// 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.
|
|
|
|
return c > ' ' && !strings.ContainsRune(`()<>@,;:\\"/[]?= `, c)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) xchar() rune {
|
|
|
|
// We are careful to track invalid utf-8 properly.
|
|
|
|
if p.empty() {
|
|
|
|
p.xerrorf("need another character")
|
|
|
|
}
|
|
|
|
var r rune
|
|
|
|
var o int
|
|
|
|
for i, c := range p.s[p.o:] {
|
|
|
|
if i > 0 {
|
|
|
|
o = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
r = c
|
|
|
|
}
|
|
|
|
if o == 0 {
|
|
|
|
p.o = len(p.s)
|
|
|
|
} else {
|
|
|
|
p.o += o
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) xquotedString() string {
|
|
|
|
p.xtake(`"`)
|
|
|
|
var s string
|
|
|
|
var esc bool
|
|
|
|
for {
|
|
|
|
c := p.xchar()
|
|
|
|
if esc {
|
|
|
|
if c >= ' ' && c < 0x7f {
|
|
|
|
s += string(c)
|
|
|
|
esc = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.xerrorf("bad escaped char %c in quoted string", c)
|
|
|
|
}
|
|
|
|
if c == '\\' {
|
|
|
|
esc = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c == '"' {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
if c >= ' ' && c != '\\' && c != '"' {
|
|
|
|
s += string(c)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.xerrorf("invalid quoted string, invalid character %c", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) digits() string {
|
|
|
|
o := p.o
|
|
|
|
for o < len(p.s) && p.s[o] >= '0' && p.s[o] <= '9' {
|
|
|
|
o++
|
|
|
|
}
|
|
|
|
p.o = o
|
|
|
|
return p.s[o:p.o]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) xdigits() string {
|
|
|
|
s := p.digits()
|
|
|
|
if s == "" {
|
|
|
|
p.xerrorf("expected digits, remaining %q", p.s[p.o:])
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) xtake(s string) {
|
|
|
|
if !p.prefix(s) {
|
|
|
|
p.xerrorf("expected %q, remaining %q", s, p.s[p.o:])
|
|
|
|
}
|
|
|
|
p.o += len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) empty() bool {
|
|
|
|
return p.o >= len(p.s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) take(s string) bool {
|
|
|
|
if p.prefix(s) {
|
|
|
|
p.o += len(s)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) xtakefn1(what string, fn func(c rune, i int) bool) string {
|
|
|
|
if p.empty() {
|
|
|
|
p.xerrorf("need at least one char for %s", what)
|
|
|
|
}
|
|
|
|
for i, c := range p.s[p.o:] {
|
|
|
|
if !fn(c, i) {
|
|
|
|
if i == 0 {
|
|
|
|
p.xerrorf("expected at least one char for %s, remaining %q", what, p.s[p.o:])
|
|
|
|
}
|
|
|
|
s := p.s[p.o : p.o+i]
|
|
|
|
p.o += i
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s := p.s[p.o:]
|
|
|
|
p.o = len(p.s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// ../rfc/5321:2287
|
|
|
|
func (p *parser) xkeyword(isResult bool) string {
|
|
|
|
s := strings.ToLower(p.xtakefn1("keyword", func(c rune, i int) bool {
|
|
|
|
// Yahoo sends results like "dkim=perm_fail".
|
|
|
|
return c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-' || isResult && !Pedantic && c == '_'
|
|
|
|
}))
|
|
|
|
if s == "-" {
|
|
|
|
p.xerrorf("missing keyword")
|
|
|
|
} else if strings.HasSuffix(s, "-") {
|
|
|
|
p.o--
|
|
|
|
s = s[:len(s)-1]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) xmethodspec(methodKeyword string) (string, string, string) {
|
|
|
|
p.cfws()
|
|
|
|
var methodDigits string
|
|
|
|
if p.take("/") {
|
|
|
|
methodDigits = p.xdigits()
|
|
|
|
p.cfws()
|
|
|
|
}
|
|
|
|
p.xtake("=")
|
|
|
|
p.cfws()
|
|
|
|
result := p.xkeyword(true)
|
|
|
|
return methodKeyword, methodDigits, result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *parser) xpropspec() (ap AuthProp) {
|
|
|
|
ap.Type = p.xkeyword(false)
|
|
|
|
p.cfws()
|
|
|
|
p.xtake(".")
|
|
|
|
p.cfws()
|
|
|
|
if p.take("mailfrom") {
|
|
|
|
ap.Property = "mailfrom"
|
|
|
|
} else if p.take("rcptto") {
|
|
|
|
ap.Property = "rcptto"
|
|
|
|
} else {
|
|
|
|
ap.Property = p.xkeyword(false)
|
|
|
|
}
|
|
|
|
p.cfws()
|
|
|
|
p.xtake("=")
|
|
|
|
ap.IsAddrLike, ap.Value = p.xpvalue()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// method keyword has been parsed, method-version not yet.
|
|
|
|
func (p *parser) xresinfo(methodKeyword string) (am AuthMethod) {
|
|
|
|
p.cfws()
|
|
|
|
am.Method, am.Version, am.Result = p.xmethodspec(methodKeyword)
|
|
|
|
p.cfws()
|
|
|
|
if p.take("reason") {
|
2024-06-28 11:39:46 +03:00
|
|
|
p.cfws()
|
|
|
|
p.xtake("=")
|
2024-03-13 19:35:53 +03:00
|
|
|
p.cfws()
|
|
|
|
am.Reason = p.xvalue()
|
|
|
|
}
|
|
|
|
p.cfws()
|
|
|
|
for !p.prefix(";") && !p.end() {
|
|
|
|
am.Props = append(am.Props, p.xpropspec())
|
|
|
|
p.cfws()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// todo: could keep track whether this is a localpart.
|
|
|
|
func (p *parser) xpvalue() (bool, string) {
|
|
|
|
p.cfws()
|
|
|
|
if p.take("@") {
|
|
|
|
// Bare domain.
|
|
|
|
dom, _ := p.xdomain()
|
|
|
|
return true, "@" + dom
|
|
|
|
}
|
|
|
|
s := p.xvalue()
|
|
|
|
if p.take("@") {
|
|
|
|
dom, _ := p.xdomain()
|
|
|
|
s += "@" + dom
|
|
|
|
return true, s
|
|
|
|
}
|
|
|
|
return false, s
|
|
|
|
}
|
|
|
|
|
|
|
|
// ../rfc/5321:2291
|
|
|
|
func (p *parser) xdomain() (string, dns.Domain) {
|
|
|
|
s := p.xsubdomain()
|
|
|
|
for p.take(".") {
|
|
|
|
s += "." + p.xsubdomain()
|
|
|
|
}
|
|
|
|
d, err := dns.ParseDomain(s)
|
|
|
|
if err != nil {
|
|
|
|
p.xerrorf("parsing domain name %q: %s", s, err)
|
|
|
|
}
|
|
|
|
if len(s) > 255 {
|
|
|
|
// ../rfc/5321:3491
|
|
|
|
p.xerrorf("domain longer than 255 octets")
|
|
|
|
}
|
|
|
|
return s, d
|
|
|
|
}
|
|
|
|
|
|
|
|
// ../rfc/5321:2303
|
|
|
|
// ../rfc/5321:2303 ../rfc/6531:411
|
|
|
|
func (p *parser) xsubdomain() string {
|
|
|
|
return p.xtakefn1("subdomain", func(c rune, i int) bool {
|
|
|
|
return c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || i > 0 && c == '-' || c > 0x7f
|
|
|
|
})
|
|
|
|
}
|