mox/store/import.go
Mechiel Lukkien 28fae96a9b
make mox compile on windows, without "mox serve" but with working "mox localserve"
getting mox to compile required changing code in only a few places where
package "syscall" was used: for accessing file access times and for umask
handling. an open problem is how to start a process as an unprivileged user on
windows.  that's why "mox serve" isn't implemented yet. and just finding a way
to implement it now may not be good enough in the near future: we may want to
starting using a more complete privilege separation approach, with a process
handling sensitive tasks (handling private keys, authentication), where we may
want to pass file descriptors between processes. how would that work on
windows?

anyway, getting mox to compile for windows doesn't mean it works properly on
windows. the largest issue: mox would normally open a file, rename or remove
it, and finally close it. this happens during message delivery. that doesn't
work on windows, the rename/remove would fail because the file is still open.
so this commit swaps many "remove" and "close" calls. renames are a longer
story: message delivery had two ways to deliver: with "consuming" the
(temporary) message file (which would rename it to its final destination), and
without consuming (by hardlinking the file, falling back to copying). the last
delivery to a recipient of a message (and the only one in the common case of a
single recipient) would consume the message, and the earlier recipients would
not.  during delivery, the already open message file was used, to parse the
message.  we still want to use that open message file, and the caller now stays
responsible for closing it, but we no longer try to rename (consume) the file.
we always hardlink (or copy) during delivery (this works on windows), and the
caller is responsible for closing and removing (in that order) the original
temporary file. this does cost one syscall more. but it makes the delivery code
(responsibilities) a bit simpler.

there is one more obvious issue: the file system path separator. mox already
used the "filepath" package to join paths in many places, but not everywhere.
and it still used strings with slashes for local file access. with this commit,
the code now uses filepath.FromSlash for path strings with slashes, uses
"filepath" in a few more places where it previously didn't. also switches from
"filepath" to regular "path" package when handling mailbox names in a few
places, because those always use forward slashes, regardless of local file
system conventions.  windows can handle forward slashes when opening files, so
test code that passes path strings with forward slashes straight to go stdlib
file i/o functions are left unchanged to reduce code churn. the regular
non-test code, or test code that uses path strings in places other than
standard i/o functions, does have the paths converted for consistent paths
(otherwise we would end up with paths with mixed forward/backward slashes in
log messages).

windows cannot dup a listening socket. for "mox localserve", it isn't
important, and we can work around the issue. the current approach for "mox
serve" (forking a process and passing file descriptors of listening sockets on
"privileged" ports) won't work on windows. perhaps it isn't needed on windows,
and any user can listen on "privileged" ports? that would be welcome.

on windows, os.Open cannot open a directory, so we cannot call Sync on it after
message delivery. a cursory internet search indicates that directories cannot
be synced on windows. the story is probably much more nuanced than that, with
long deep technical details/discussions/disagreement/confusion, like on unix.
for "mox localserve" we can get away with making syncdir a no-op.
2023-10-14 10:54:07 +02:00

433 lines
11 KiB
Go

package store
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"golang.org/x/exp/maps"
"github.com/mjl-/mox/mlog"
)
// MsgSource is implemented by readers for mailbox file formats.
type MsgSource interface {
// Return next message, or io.EOF when there are no more.
Next() (*Message, *os.File, string, error)
}
// MboxReader reads messages from an mbox file, implementing MsgSource.
type MboxReader struct {
createTemp func(pattern string) (*os.File, error)
path string
line int
r *bufio.Reader
prevempty bool
nonfirst bool
log *mlog.Log
eof bool
fromLine string // "From "-line for this message.
header bool // Now in header section.
}
func NewMboxReader(createTemp func(pattern string) (*os.File, error), filename string, r io.Reader, log *mlog.Log) *MboxReader {
return &MboxReader{
createTemp: createTemp,
path: filename,
line: 1,
r: bufio.NewReader(r),
log: log,
}
}
// Position returns "<filename>:<lineno>" for the current position.
func (mr *MboxReader) Position() string {
return fmt.Sprintf("%s:%d", mr.path, mr.line)
}
// Next returns the next message read from the mbox file. The file is a temporary
// file and must be removed/consumed. The third return value is the position in the
// file.
func (mr *MboxReader) Next() (*Message, *os.File, string, error) {
if mr.eof {
return nil, nil, "", io.EOF
}
from := []byte("From ")
if !mr.nonfirst {
mr.header = true
// First read, we're at the beginning of the file.
line, err := mr.r.ReadBytes('\n')
if err == io.EOF {
return nil, nil, "", io.EOF
}
mr.line++
if !bytes.HasPrefix(line, from) {
return nil, nil, mr.Position(), fmt.Errorf(`first line does not start with "From "`)
}
mr.nonfirst = true
mr.fromLine = strings.TrimSpace(string(line))
}
f, err := mr.createTemp("mboxreader")
if err != nil {
return nil, nil, mr.Position(), err
}
defer func() {
if f != nil {
name := f.Name()
err := f.Close()
mr.log.Check(err, "closing temporary message file after mbox read error")
err = os.Remove(name)
mr.log.Check(err, "removing temporary message file after mbox read error", mlog.Field("path", name))
}
}()
fromLine := mr.fromLine
bf := bufio.NewWriter(f)
var flags Flags
keywords := map[string]bool{}
var size int64
for {
line, err := mr.r.ReadBytes('\n')
if err != nil && err != io.EOF {
return nil, nil, mr.Position(), fmt.Errorf("reading from mbox: %v", err)
}
if len(line) > 0 {
mr.line++
// We store data with crlf, adjust any imported messages with bare newlines.
if !bytes.HasSuffix(line, []byte("\r\n")) {
line = append(line[:len(line)-1], "\r\n"...)
}
if mr.header {
// See https://doc.dovecot.org/admin_manual/mailbox_formats/mbox/
if bytes.HasPrefix(line, []byte("Status:")) {
s := strings.TrimSpace(strings.SplitN(string(line), ":", 2)[1])
for _, c := range s {
switch c {
case 'R':
flags.Seen = true
}
}
} else if bytes.HasPrefix(line, []byte("X-Status:")) {
s := strings.TrimSpace(strings.SplitN(string(line), ":", 2)[1])
for _, c := range s {
switch c {
case 'A':
flags.Answered = true
case 'F':
flags.Flagged = true
case 'T':
flags.Draft = true
case 'D':
flags.Deleted = true
}
}
} else if bytes.HasPrefix(line, []byte("X-Keywords:")) {
s := strings.TrimSpace(strings.SplitN(string(line), ":", 2)[1])
for _, t := range strings.Split(s, ",") {
word := strings.ToLower(strings.TrimSpace(t))
switch word {
case "forwarded", "$forwarded":
flags.Forwarded = true
case "junk", "$junk":
flags.Junk = true
case "notjunk", "$notjunk", "nonjunk", "$nonjunk":
flags.Notjunk = true
case "phishing", "$phishing":
flags.Phishing = true
case "mdnsent", "$mdnsent":
flags.MDNSent = true
default:
if err := CheckKeyword(word); err == nil {
keywords[word] = true
}
}
}
}
}
if bytes.Equal(line, []byte("\r\n")) {
mr.header = false
}
// Next mail message starts at bare From word.
if mr.prevempty && bytes.HasPrefix(line, from) {
mr.fromLine = strings.TrimSpace(string(line))
mr.header = true
break
}
if bytes.HasPrefix(line, []byte(">")) && bytes.HasPrefix(bytes.TrimLeft(line, ">"), []byte("From ")) {
line = line[1:]
}
n, err := bf.Write(line)
if err != nil {
return nil, nil, mr.Position(), fmt.Errorf("writing message to file: %v", err)
}
size += int64(n)
mr.prevempty = bytes.Equal(line, []byte("\r\n"))
}
if err == io.EOF {
mr.eof = true
break
}
}
if err := bf.Flush(); err != nil {
return nil, nil, mr.Position(), fmt.Errorf("flush: %v", err)
}
m := &Message{Flags: flags, Keywords: maps.Keys(keywords), Size: size}
if t := strings.SplitN(fromLine, " ", 3); len(t) == 3 {
layouts := []string{time.ANSIC, time.UnixDate, time.RubyDate}
for _, l := range layouts {
t, err := time.Parse(l, t[2])
if err == nil {
m.Received = t
break
}
}
}
// Prevent cleanup by defer.
mf := f
f = nil
return m, mf, mr.Position(), nil
}
type MaildirReader struct {
createTemp func(pattern string) (*os.File, error)
newf, curf *os.File
f *os.File // File we are currently reading from. We first read newf, then curf.
dir string // Name of directory for f. Can be empty on first call.
entries []os.DirEntry
dovecotFlags []string // Lower-case flags/keywords.
log *mlog.Log
}
func NewMaildirReader(createTemp func(pattern string) (*os.File, error), newf, curf *os.File, log *mlog.Log) *MaildirReader {
mr := &MaildirReader{
createTemp: createTemp,
newf: newf,
curf: curf,
f: newf,
log: log,
}
// Best-effort parsing of dovecot keywords.
kf, err := os.Open(filepath.Join(filepath.Dir(newf.Name()), "dovecot-keywords"))
if err == nil {
mr.dovecotFlags, err = ParseDovecotKeywordsFlags(kf, log)
log.Check(err, "parsing dovecot keywords file")
err = kf.Close()
log.Check(err, "closing dovecot-keywords file")
}
return mr
}
func (mr *MaildirReader) Next() (*Message, *os.File, string, error) {
if mr.dir == "" {
mr.dir = mr.f.Name()
}
if len(mr.entries) == 0 {
var err error
mr.entries, err = mr.f.ReadDir(100)
if err != nil && err != io.EOF {
return nil, nil, "", err
}
if len(mr.entries) == 0 {
if mr.f == mr.curf {
return nil, nil, "", io.EOF
}
mr.f = mr.curf
mr.dir = ""
return mr.Next()
}
}
p := filepath.Join(mr.dir, mr.entries[0].Name())
mr.entries = mr.entries[1:]
sf, err := os.Open(p)
if err != nil {
return nil, nil, p, fmt.Errorf("open message in maildir: %s", err)
}
defer func() {
err := sf.Close()
mr.log.Check(err, "closing message file after error")
}()
f, err := mr.createTemp("maildirreader")
if err != nil {
return nil, nil, p, err
}
defer func() {
if f != nil {
name := f.Name()
err := f.Close()
mr.log.Check(err, "closing temporary message file after maildir read error")
err = os.Remove(name)
mr.log.Check(err, "removing temporary message file after maildir read error", mlog.Field("path", name))
}
}()
// Copy data, changing bare \n into \r\n.
r := bufio.NewReader(sf)
w := bufio.NewWriter(f)
var size int64
for {
line, err := r.ReadBytes('\n')
if err != nil && err != io.EOF {
return nil, nil, p, fmt.Errorf("reading message: %v", err)
}
if len(line) > 0 {
if !bytes.HasSuffix(line, []byte("\r\n")) {
line = append(line[:len(line)-1], "\r\n"...)
}
if n, err := w.Write(line); err != nil {
return nil, nil, p, fmt.Errorf("writing message: %v", err)
} else {
size += int64(n)
}
}
if err == io.EOF {
break
}
}
if err := w.Flush(); err != nil {
return nil, nil, p, fmt.Errorf("writing message: %v", err)
}
// Take received time from filename.
var received time.Time
t := strings.SplitN(filepath.Base(sf.Name()), ".", 2)
if v, err := strconv.ParseInt(t[0], 10, 64); err == nil {
received = time.Unix(v, 0)
}
// Parse flags. See https://cr.yp.to/proto/maildir.html.
flags := Flags{}
keywords := map[string]bool{}
t = strings.SplitN(filepath.Base(sf.Name()), ":2,", 2)
if len(t) == 2 {
for _, c := range t[1] {
switch c {
case 'P':
// Passed, doesn't map to a common IMAP flag.
case 'R':
flags.Answered = true
case 'S':
flags.Seen = true
case 'T':
flags.Deleted = true
case 'D':
flags.Draft = true
case 'F':
flags.Flagged = true
default:
if c >= 'a' && c <= 'z' {
index := int(c - 'a')
if index >= len(mr.dovecotFlags) {
continue
}
kw := mr.dovecotFlags[index]
switch kw {
case "$forwarded", "forwarded":
flags.Forwarded = true
case "$junk", "junk":
flags.Junk = true
case "$notjunk", "notjunk", "nonjunk":
flags.Notjunk = true
case "$mdnsent", "mdnsent":
flags.MDNSent = true
case "$phishing", "phishing":
flags.Phishing = true
default:
keywords[kw] = true
}
}
}
}
}
m := &Message{Received: received, Flags: flags, Keywords: maps.Keys(keywords), Size: size}
// Prevent cleanup by defer.
mf := f
f = nil
return m, mf, p, nil
}
// ParseDovecotKeywordsFlags attempts to parse a dovecot-keywords file. It only
// returns valid flags/keywords, as lower-case. If an error is encountered and
// returned, any keywords that were found are still returned. The returned list has
// both system/well-known flags and custom keywords.
func ParseDovecotKeywordsFlags(r io.Reader, log *mlog.Log) ([]string, error) {
/*
If the dovecot-keywords file is present, we parse its additional flags, see
https://doc.dovecot.org/admin_manual/mailbox_formats/maildir/
0 Old
1 Junk
2 NonJunk
3 $Forwarded
4 $Junk
*/
keywords := make([]string, 26)
end := 0
scanner := bufio.NewScanner(r)
var errs []string
for scanner.Scan() {
s := scanner.Text()
t := strings.SplitN(s, " ", 2)
if len(t) != 2 {
errs = append(errs, fmt.Sprintf("unexpected dovecot keyword line: %q", s))
continue
}
v, err := strconv.ParseInt(t[0], 10, 32)
if err != nil {
errs = append(errs, fmt.Sprintf("unexpected dovecot keyword index: %q", s))
continue
}
if v < 0 || v >= int64(len(keywords)) {
errs = append(errs, fmt.Sprintf("dovecot keyword index too big: %q", s))
continue
}
index := int(v)
if keywords[index] != "" {
errs = append(errs, fmt.Sprintf("duplicate dovecot keyword: %q", s))
continue
}
kw := strings.ToLower(t[1])
if !systemWellKnownFlags[kw] {
if err := CheckKeyword(kw); err != nil {
errs = append(errs, fmt.Sprintf("invalid keyword %q", kw))
continue
}
}
keywords[index] = kw
if index >= end {
end = index + 1
}
}
if err := scanner.Err(); err != nil {
errs = append(errs, fmt.Sprintf("reading dovecot keywords file: %v", err))
}
var err error
if len(errs) > 0 {
err = errors.New(strings.Join(errs, "; "))
}
return keywords[:end], err
}