2Package store implements storage for accounts, their mailboxes, IMAP
3subscriptions and messages, and broadcasts updates (e.g. mail delivery) to
4interested sessions (e.g. IMAP connections).
6Layout of storage for accounts:
8 <DataDir>/accounts/<name>/index.db
9 <DataDir>/accounts/<name>/msg/[a-zA-Z0-9_-]+/<id>
11Index.db holds tables for user information, mailboxes, and messages. Messages
12are stored in the msg/ subdirectory, each in their own file. The on-disk message
13does not contain headers generated during an incoming SMTP transaction, such as
14Received and Authentication-Results headers. Those are in the database to
15prevent having to rewrite incoming messages (e.g. Authentication-Result for DKIM
16signatures can only be determined after having read the message). Messages must
17be read through MsgReader, which transparently adds the prefix from the
22// todo: make up a function naming scheme that indicates whether caller should broadcast changes.
27 cryptorand "crypto/rand"
45 "golang.org/x/crypto/bcrypt"
46 "golang.org/x/exp/slices"
47 "golang.org/x/exp/slog"
48 "golang.org/x/text/unicode/norm"
50 "github.com/mjl-/bstore"
52 "github.com/mjl-/mox/config"
53 "github.com/mjl-/mox/dns"
54 "github.com/mjl-/mox/message"
55 "github.com/mjl-/mox/metrics"
56 "github.com/mjl-/mox/mlog"
57 "github.com/mjl-/mox/mox-"
58 "github.com/mjl-/mox/moxio"
59 "github.com/mjl-/mox/publicsuffix"
60 "github.com/mjl-/mox/scram"
61 "github.com/mjl-/mox/smtp"
64// If true, each time an account is closed its database file is checked for
65// consistency. If an inconsistency is found, panic is called. Set by default
66// because of all the packages with tests, the mox main function sets it to
68var CheckConsistencyOnClose = true
71 ErrUnknownMailbox = errors.New("no such mailbox")
72 ErrUnknownCredentials = errors.New("credentials not found")
73 ErrAccountUnknown = errors.New("no such account")
74 ErrOverQuota = errors.New("account over quota")
77var DefaultInitialMailboxes = config.InitialMailboxes{
78 SpecialUse: config.SpecialUseMailboxes{
93// CRAMMD5 holds HMAC ipad and opad hashes that are initialized with the first
94// block with (a derivation of) the key/password, so we don't store the password in plain
101// BinaryMarshal is used by bstore to store the ipad/opad hash states.
102func (c CRAMMD5) MarshalBinary() ([]byte, error) {
103 if c.Ipad == nil || c.Opad == nil {
107 ipad, err := c.Ipad.(encoding.BinaryMarshaler).MarshalBinary()
109 return nil, fmt.Errorf("marshal ipad: %v", err)
111 opad, err := c.Opad.(encoding.BinaryMarshaler).MarshalBinary()
113 return nil, fmt.Errorf("marshal opad: %v", err)
115 buf := make([]byte, 2+len(ipad)+len(opad))
116 ipadlen := uint16(len(ipad))
117 buf[0] = byte(ipadlen >> 8)
118 buf[1] = byte(ipadlen >> 0)
120 copy(buf[2+len(ipad):], opad)
124// BinaryUnmarshal is used by bstore to restore the ipad/opad hash states.
125func (c *CRAMMD5) UnmarshalBinary(buf []byte) error {
131 return fmt.Errorf("short buffer")
133 ipadlen := int(uint16(buf[0])<<8 | uint16(buf[1])<<0)
134 if len(buf) < 2+ipadlen {
135 return fmt.Errorf("buffer too short for ipadlen")
139 if err := ipad.(encoding.BinaryUnmarshaler).UnmarshalBinary(buf[2 : 2+ipadlen]); err != nil {
140 return fmt.Errorf("unmarshal ipad: %v", err)
142 if err := opad.(encoding.BinaryUnmarshaler).UnmarshalBinary(buf[2+ipadlen:]); err != nil {
143 return fmt.Errorf("unmarshal opad: %v", err)
145 *c = CRAMMD5{ipad, opad}
149// Password holds credentials in various forms, for logging in with SMTP/IMAP.
150type Password struct {
151 Hash string // bcrypt hash for IMAP LOGIN, SASL PLAIN and HTTP basic authentication.
152 CRAMMD5 CRAMMD5 // For SASL CRAM-MD5.
153 SCRAMSHA1 SCRAM // For SASL SCRAM-SHA-1.
154 SCRAMSHA256 SCRAM // For SASL SCRAM-SHA-256.
157// Subjectpass holds the secret key used to sign subjectpass tokens.
158type Subjectpass struct {
159 Email string // Our destination address (canonical, with catchall localpart stripped).
163// NextUIDValidity is a singleton record in the database with the next UIDValidity
164// to use for the next mailbox.
165type NextUIDValidity struct {
166 ID int // Just a single record with ID 1.
170// SyncState track ModSeqs.
171type SyncState struct {
172 ID int // Just a single record with ID 1.
174 // Last used, next assigned will be one higher. The first value we hand out is 2.
175 // That's because 0 (the default value for old existing messages, from before the
176 // Message.ModSeq field) is special in IMAP, so we return it as 1.
177 LastModSeq ModSeq `bstore:"nonzero"`
179 // Highest ModSeq of expunged record that we deleted. When a clients synchronizes
180 // and requests changes based on a modseq before this one, we don't have the
181 // history to provide information about deletions. We normally keep these expunged
182 // records around, but we may periodically truly delete them to reclaim storage
183 // space. Initially set to -1 because we don't want to match with any ModSeq in the
184 // database, which can be zero values.
185 HighestDeletedModSeq ModSeq
188// Mailbox is collection of messages, e.g. Inbox or Sent.
192 // "Inbox" is the name for the special IMAP "INBOX". Slash separated
194 Name string `bstore:"nonzero,unique"`
196 // If UIDs are invalidated, e.g. when renaming a mailbox to a previously existing
197 // name, UIDValidity must be changed. Used by IMAP for synchronization.
200 // UID likely to be assigned to next message. Used by IMAP to detect messages
201 // delivered to a mailbox.
206 // Keywords as used in messages. Storing a non-system keyword for a message
207 // automatically adds it to this list. Used in the IMAP FLAGS response. Only
208 // "atoms" are allowed (IMAP syntax), keywords are case-insensitive, only stored in
209 // lower case (for JMAP), sorted.
212 HaveCounts bool // Whether MailboxCounts have been initialized.
213 MailboxCounts // Statistics about messages, kept up to date whenever a change happens.
216// MailboxCounts tracks statistics about messages for a mailbox.
217type MailboxCounts struct {
218 Total int64 // Total number of messages, excluding \Deleted. For JMAP.
219 Deleted int64 // Number of messages with \Deleted flag. Used for IMAP message count that includes messages with \Deleted.
220 Unread int64 // Messages without \Seen, excluding those with \Deleted, for JMAP.
221 Unseen int64 // Messages without \Seen, including those with \Deleted, for IMAP.
222 Size int64 // Number of bytes for all messages.
225func (mc MailboxCounts) String() string {
226 return fmt.Sprintf("%d total, %d deleted, %d unread, %d unseen, size %d bytes", mc.Total, mc.Deleted, mc.Unread, mc.Unseen, mc.Size)
229// Add increases mailbox counts mc with those of delta.
230func (mc *MailboxCounts) Add(delta MailboxCounts) {
231 mc.Total += delta.Total
232 mc.Deleted += delta.Deleted
233 mc.Unread += delta.Unread
234 mc.Unseen += delta.Unseen
235 mc.Size += delta.Size
238// Add decreases mailbox counts mc with those of delta.
239func (mc *MailboxCounts) Sub(delta MailboxCounts) {
240 mc.Total -= delta.Total
241 mc.Deleted -= delta.Deleted
242 mc.Unread -= delta.Unread
243 mc.Unseen -= delta.Unseen
244 mc.Size -= delta.Size
247// SpecialUse identifies a specific role for a mailbox, used by clients to
248// understand where messages should go.
249type SpecialUse struct {
257// CalculateCounts calculates the full current counts for messages in the mailbox.
258func (mb *Mailbox) CalculateCounts(tx *bstore.Tx) (mc MailboxCounts, err error) {
259 q := bstore.QueryTx[Message](tx)
260 q.FilterNonzero(Message{MailboxID: mb.ID})
261 q.FilterEqual("Expunged", false)
262 err = q.ForEach(func(m Message) error {
263 mc.Add(m.MailboxCounts())
269// ChangeSpecialUse returns a change for special-use flags, for broadcasting to
271func (mb Mailbox) ChangeSpecialUse() ChangeMailboxSpecialUse {
272 return ChangeMailboxSpecialUse{mb.ID, mb.Name, mb.SpecialUse}
275// ChangeKeywords returns a change with new keywords for a mailbox (e.g. after
276// setting a new keyword on a message in the mailbox), for broadcasting to other
278func (mb Mailbox) ChangeKeywords() ChangeMailboxKeywords {
279 return ChangeMailboxKeywords{mb.ID, mb.Name, mb.Keywords}
282// KeywordsChanged returns whether the keywords in a mailbox have changed.
283func (mb Mailbox) KeywordsChanged(origmb Mailbox) bool {
284 if len(mb.Keywords) != len(origmb.Keywords) {
287 // Keywords are stored sorted.
288 for i, kw := range mb.Keywords {
289 if origmb.Keywords[i] != kw {
296// CountsChange returns a change with mailbox counts.
297func (mb Mailbox) ChangeCounts() ChangeMailboxCounts {
298 return ChangeMailboxCounts{mb.ID, mb.Name, mb.MailboxCounts}
301// Subscriptions are separate from existence of mailboxes.
302type Subscription struct {
306// Flags for a mail message.
320// FlagsAll is all flags set, for use as mask.
321var FlagsAll = Flags{true, true, true, true, true, true, true, true, true, true}
323// Validation of "message From" domain.
327 ValidationUnknown Validation = 0
328 ValidationStrict Validation = 1 // Like DMARC, with strict policies.
329 ValidationDMARC Validation = 2 // Actual DMARC policy.
330 ValidationRelaxed Validation = 3 // Like DMARC, with relaxed policies.
331 ValidationPass Validation = 4 // For SPF.
332 ValidationNeutral Validation = 5 // For SPF.
333 ValidationTemperror Validation = 6
334 ValidationPermerror Validation = 7
335 ValidationFail Validation = 8
336 ValidationSoftfail Validation = 9 // For SPF.
337 ValidationNone Validation = 10 // E.g. No records.
340// Message stored in database and per-message file on disk.
342// Contents are always the combined data from MsgPrefix and the on-disk file named
345// Messages always have a header section, even if empty. Incoming messages without
346// header section must get an empty header section added before inserting.
348 // ID, unchanged over lifetime, determines path to on-disk msg file.
349 // Set during deliver.
352 UID UID `bstore:"nonzero"` // UID, for IMAP. Set during deliver.
353 MailboxID int64 `bstore:"nonzero,unique MailboxID+UID,index MailboxID+Received,index MailboxID+ModSeq,ref Mailbox"`
355 // Modification sequence, for faster syncing with IMAP QRESYNC and JMAP.
356 // ModSeq is the last modification. CreateSeq is the Seq the message was inserted,
357 // always <= ModSeq. If Expunged is set, the message has been removed and should not
358 // be returned to the user. In this case, ModSeq is the Seq where the message is
359 // removed, and will never be changed again.
360 // We have an index on both ModSeq (for JMAP that synchronizes per account) and
361 // MailboxID+ModSeq (for IMAP that synchronizes per mailbox).
362 // The index on CreateSeq helps efficiently finding created messages for JMAP.
363 // The value of ModSeq is special for IMAP. Messages that existed before ModSeq was
364 // added have 0 as value. But modseq 0 in IMAP is special, so we return it as 1. If
365 // we get modseq 1 from a client, the IMAP server will translate it to 0. When we
366 // return modseq to clients, we turn 0 into 1.
367 ModSeq ModSeq `bstore:"index"`
368 CreateSeq ModSeq `bstore:"index"`
371 // If set, this message was delivered to a Rejects mailbox. When it is moved to a
372 // different mailbox, its MailboxOrigID is set to the destination mailbox and this
376 // If set, this is a forwarded message (through a ruleset with IsForward). This
377 // causes fields used during junk analysis to be moved to their Orig variants, and
378 // masked IP fields cleared, so they aren't used in junk classifications for
379 // incoming messages. This ensures the forwarded messages don't cause negative
380 // reputation for the forwarding mail server, which may also be sending regular
384 // MailboxOrigID is the mailbox the message was originally delivered to. Typically
385 // Inbox or Rejects, but can also be a mailbox configured in a Ruleset, or
386 // Postmaster, TLS/DMARC reporting addresses. MailboxOrigID is not changed when the
387 // message is moved to another mailbox, e.g. Archive/Trash/Junk. Used for
388 // per-mailbox reputation.
390 // MailboxDestinedID is normally 0, but when a message is delivered to the Rejects
391 // mailbox, it is set to the intended mailbox according to delivery rules,
392 // typically that of Inbox. When such a message is moved out of Rejects, the
393 // MailboxOrigID is corrected by setting it to MailboxDestinedID. This ensures the
394 // message is used for reputation calculation for future deliveries to that
397 // These are not bstore references to prevent having to update all messages in a
398 // mailbox when the original mailbox is removed. Use of these fields requires
399 // checking if the mailbox still exists.
401 MailboxDestinedID int64
403 Received time.Time `bstore:"default now,index"`
405 // Full IP address of remote SMTP server. Empty if not delivered over SMTP. The
406 // masked IPs are used to classify incoming messages. They are left empty for
407 // messages matching a ruleset for forwarded messages.
409 RemoteIPMasked1 string `bstore:"index RemoteIPMasked1+Received"` // For IPv4 /32, for IPv6 /64, for reputation.
410 RemoteIPMasked2 string `bstore:"index RemoteIPMasked2+Received"` // For IPv4 /26, for IPv6 /48.
411 RemoteIPMasked3 string `bstore:"index RemoteIPMasked3+Received"` // For IPv4 /21, for IPv6 /32.
413 // Only set if present and not an IP address. Unicode string. Empty for forwarded
415 EHLODomain string `bstore:"index EHLODomain+Received"`
416 MailFrom string // With localpart and domain. Can be empty.
417 MailFromLocalpart smtp.Localpart // SMTP "MAIL FROM", can be empty.
418 // Only set if it is a domain, not an IP. Unicode string. Empty for forwarded
419 // messages, but see OrigMailFromDomain.
420 MailFromDomain string `bstore:"index MailFromDomain+Received"`
421 RcptToLocalpart smtp.Localpart // SMTP "RCPT TO", can be empty.
422 RcptToDomain string // Unicode string.
424 // Parsed "From" message header, used for reputation along with domain validation.
425 MsgFromLocalpart smtp.Localpart
426 MsgFromDomain string `bstore:"index MsgFromDomain+Received"` // Unicode string.
427 MsgFromOrgDomain string `bstore:"index MsgFromOrgDomain+Received"` // Unicode string.
429 // Simplified statements of the Validation fields below, used for incoming messages
430 // to check reputation.
432 MailFromValidated bool
433 MsgFromValidated bool
435 EHLOValidation Validation // Validation can also take reverse IP lookup into account, not only SPF.
436 MailFromValidation Validation // Can have SPF-specific validations like ValidationSoftfail.
437 MsgFromValidation Validation // Desirable validations: Strict, DMARC, Relaxed. Will not be just Pass.
439 // Domains with verified DKIM signatures. Unicode string. For forwarded messages, a
440 // DKIM domain that matched a ruleset's verified domain is left out, but included
441 // in OrigDKIMDomains.
442 DKIMDomains []string `bstore:"index DKIMDomains+Received"`
444 // For forwarded messages,
445 OrigEHLODomain string
446 OrigDKIMDomains []string
448 // Canonicalized Message-Id, always lower-case and normalized quoting, without
449 // <>'s. Empty if missing. Used for matching message threads, and to prevent
450 // duplicate reject delivery.
451 MessageID string `bstore:"index"`
454 // For matching threads in case there is no References/In-Reply-To header. It is
455 // lower-cased, white-space collapsed, mailing list tags and re/fwd tags removed.
456 SubjectBase string `bstore:"index"`
459 // Hash of message. For rejects delivery in case there is no Message-ID, only set
460 // when delivered as reject.
463 // ID of message starting this thread.
464 ThreadID int64 `bstore:"index"`
465 // IDs of parent messages, from closest parent to the root message. Parent messages
466 // may be in a different mailbox, or may no longer exist. ThreadParentIDs must
467 // never contain the message id itself (a cycle), and parent messages must
468 // reference the same ancestors.
469 ThreadParentIDs []int64
470 // ThreadMissingLink is true if there is no match with a direct parent. E.g. first
471 // ID in ThreadParentIDs is not the direct ancestor (an intermediate message may
472 // have been deleted), or subject-based matching was done.
473 ThreadMissingLink bool
474 // If set, newly delivered child messages are automatically marked as read. This
475 // field is copied to new child messages. Changes are propagated to the webmail
478 // If set, this (sub)thread is collapsed in the webmail client, for threading mode
479 // "on" (mode "unread" ignores it). This field is copied to new child message.
480 // Changes are propagated to the webmail client.
483 // If received message was known to match a mailing list rule (with modified junk
487 ReceivedTLSVersion uint16 // 0 if unknown, 1 if plaintext/no TLS, otherwise TLS cipher suite.
488 ReceivedTLSCipherSuite uint16
489 ReceivedRequireTLS bool // Whether RequireTLS was known to be used for incoming delivery.
492 // For keywords other than system flags or the basic well-known $-flags. Only in
493 // "atom" syntax (IMAP), they are case-insensitive, always stored in lower-case
494 // (for JMAP), sorted.
495 Keywords []string `bstore:"index"`
497 TrainedJunk *bool // If nil, no training done yet. Otherwise, true is trained as junk, false trained as nonjunk.
498 MsgPrefix []byte // Typically holds received headers and/or header separator.
500 // ParsedBuf message structure. Currently saved as JSON of message.Part because bstore
501 // cannot yet store recursive types. Created when first needed, and saved in the
503 // todo: once replaced with non-json storage, remove date fixup in ../message/part.go.
507// MailboxCounts returns the delta to counts this message means for its
509func (m Message) MailboxCounts() (mc MailboxCounts) {
528func (m Message) ChangeAddUID() ChangeAddUID {
529 return ChangeAddUID{m.MailboxID, m.UID, m.ModSeq, m.Flags, m.Keywords}
532func (m Message) ChangeFlags(orig Flags) ChangeFlags {
533 mask := m.Flags.Changed(orig)
534 return ChangeFlags{MailboxID: m.MailboxID, UID: m.UID, ModSeq: m.ModSeq, Mask: mask, Flags: m.Flags, Keywords: m.Keywords}
537func (m Message) ChangeThread() ChangeThread {
538 return ChangeThread{[]int64{m.ID}, m.ThreadMuted, m.ThreadCollapsed}
541// ModSeq represents a modseq as stored in the database. ModSeq 0 in the
542// database is sent to the client as 1, because modseq 0 is special in IMAP.
543// ModSeq coming from the client are of type int64.
546func (ms ModSeq) Client() int64 {
553// ModSeqFromClient converts a modseq from a client to a modseq for internal
554// use, e.g. in a database query.
555// ModSeq 1 is turned into 0 (the Go zero value for ModSeq).
556func ModSeqFromClient(modseq int64) ModSeq {
560 return ModSeq(modseq)
563// PrepareExpunge clears fields that are no longer needed after an expunge, so
564// almost all fields. Does not change ModSeq, but does set Expunged.
565func (m *Message) PrepareExpunge() {
569 MailboxID: m.MailboxID,
570 CreateSeq: m.CreateSeq,
573 ThreadID: m.ThreadID,
577// PrepareThreading sets MessageID and SubjectBase (used in threading) based on the
579func (m *Message) PrepareThreading(log mlog.Log, part *message.Part) {
580 if part.Envelope == nil {
583 messageID, raw, err := message.MessageIDCanonical(part.Envelope.MessageID)
585 log.Debugx("parsing message-id, ignoring", err, slog.String("messageid", part.Envelope.MessageID))
587 log.Debug("could not parse message-id as address, continuing with raw value", slog.String("messageid", part.Envelope.MessageID))
589 m.MessageID = messageID
590 m.SubjectBase, _ = message.ThreadSubject(part.Envelope.Subject, false)
593// LoadPart returns a message.Part by reading from m.ParsedBuf.
594func (m Message) LoadPart(r io.ReaderAt) (message.Part, error) {
595 if m.ParsedBuf == nil {
596 return message.Part{}, fmt.Errorf("message not parsed")
599 err := json.Unmarshal(m.ParsedBuf, &p)
601 return p, fmt.Errorf("unmarshal message part")
607// NeedsTraining returns whether message needs a training update, based on
608// TrainedJunk (current training status) and new Junk/Notjunk flags.
609func (m Message) NeedsTraining() bool {
610 untrain := m.TrainedJunk != nil
611 untrainJunk := untrain && *m.TrainedJunk
612 train := m.Junk || m.Notjunk && !(m.Junk && m.Notjunk)
614 return untrain != train || untrain && train && untrainJunk != trainJunk
617// JunkFlagsForMailbox sets Junk and Notjunk flags based on mailbox name if configured. Often
618// used when delivering/moving/copying messages to a mailbox. Mail clients are not
619// very helpful with setting junk/notjunk flags. But clients can move/copy messages
620// to other mailboxes. So we set flags when clients move a message.
621func (m *Message) JunkFlagsForMailbox(mb Mailbox, conf config.Account) {
628 if !conf.AutomaticJunkFlags.Enabled {
632 lmailbox := strings.ToLower(mb.Name)
634 if conf.JunkMailbox != nil && conf.JunkMailbox.MatchString(lmailbox) {
637 } else if conf.NeutralMailbox != nil && conf.NeutralMailbox.MatchString(lmailbox) {
640 } else if conf.NotJunkMailbox != nil && conf.NotJunkMailbox.MatchString(lmailbox) {
643 } else if conf.JunkMailbox == nil && conf.NeutralMailbox != nil && conf.NotJunkMailbox != nil {
646 } else if conf.JunkMailbox != nil && conf.NeutralMailbox == nil && conf.NotJunkMailbox != nil {
649 } else if conf.JunkMailbox != nil && conf.NeutralMailbox != nil && conf.NotJunkMailbox == nil {
655// Recipient represents the recipient of a message. It is tracked to allow
656// first-time incoming replies from users this account has sent messages to. When a
657// mailbox is added to the Sent mailbox the message is parsed and recipients are
658// inserted as recipient. Recipients are never removed other than for removing the
659// message. On move/copy of a message, recipients aren't modified either. For IMAP,
660// this assumes a client simply appends messages to the Sent mailbox (as opposed to
661// copying messages from some place).
662type Recipient struct {
664 MessageID int64 `bstore:"nonzero,ref Message"` // Ref gives it its own index, useful for fast removal as well.
665 Localpart smtp.Localpart `bstore:"nonzero"`
666 Domain string `bstore:"nonzero,index Domain+Localpart"` // Unicode string.
667 OrgDomain string `bstore:"nonzero,index"` // Unicode string.
668 Sent time.Time `bstore:"nonzero"`
671// Outgoing is a message submitted for delivery from the queue. Used to enforce
672// maximum outgoing messages.
673type Outgoing struct {
675 Recipient string `bstore:"nonzero,index"` // Canonical international address with utf8 domain.
676 Submitted time.Time `bstore:"nonzero,default now"`
679// RecipientDomainTLS stores TLS capabilities of a recipient domain as encountered
680// during most recent connection (delivery attempt).
681type RecipientDomainTLS struct {
682 Domain string // Unicode.
683 Updated time.Time `bstore:"default now"`
684 STARTTLS bool // Supports STARTTLS.
685 RequireTLS bool // Supports RequireTLS SMTP extension.
688// DiskUsage tracks quota use.
689type DiskUsage struct {
690 ID int64 // Always one record with ID 1.
691 MessageSize int64 // Sum of all messages, for quota accounting.
694// SessionToken and CSRFToken are types to prevent mixing them up.
695// Base64 raw url encoded.
696type SessionToken string
699// LoginSession represents a login session. We keep a limited number of sessions
700// for a user, removing the oldest session when a new one is created.
701type LoginSession struct {
703 Created time.Time `bstore:"nonzero,default now"` // Of original login.
704 Expires time.Time `bstore:"nonzero"` // Extended each time it is used.
705 SessionTokenBinary [16]byte `bstore:"nonzero"` // Stored in cookie, like "webmailsession" or "webaccountsession".
706 CSRFTokenBinary [16]byte // For API requests, in "x-mox-csrf" header.
707 AccountName string `bstore:"nonzero"`
708 LoginAddress string `bstore:"nonzero"`
710 // Set when loading from database.
711 sessionToken SessionToken
715// Types stored in DB.
716var DBTypes = []any{NextUIDValidity{}, Message{}, Recipient{}, Mailbox{}, Subscription{}, Outgoing{}, Password{}, Subjectpass{}, SyncState{}, Upgrade{}, RecipientDomainTLS{}, DiskUsage{}, LoginSession{}}
718// Account holds the information about a user, includings mailboxes, messages, imap subscriptions.
720 Name string // Name, according to configuration.
721 Dir string // Directory where account files, including the database, bloom filter, and mail messages, are stored for this account.
722 DBPath string // Path to database with mailboxes, messages, etc.
723 DB *bstore.DB // Open database connection.
725 // Channel that is closed if/when account has/gets "threads" accounting (see
727 threadsCompleted chan struct{}
728 // If threads upgrade completed with error, this is set. Used for warning during
729 // delivery, or aborting when importing.
732 // Write lock must be held for account/mailbox modifications including message delivery.
733 // Read lock for reading mailboxes/messages.
734 // When making changes to mailboxes/messages, changes must be broadcasted before
735 // releasing the lock to ensure proper UID ordering.
738 nused int // Reference count, while >0, this account is alive and shared.
743 Threads byte // 0: None, 1: Adding MessageID's completed, 2: Adding ThreadID's completed.
746// InitialUIDValidity returns a UIDValidity used for initializing an account.
747// It can be replaced during tests with a predictable value.
748var InitialUIDValidity = func() uint32 {
749 return uint32(time.Now().Unix() >> 1) // A 2-second resolution will get us far enough beyond 2038.
752var openAccounts = struct {
753 names map[string]*Account
756 names: map[string]*Account{},
759func closeAccount(acc *Account) (rerr error) {
762 defer openAccounts.Unlock()
764 // threadsCompleted must be closed now because it increased nused.
765 rerr = acc.DB.Close()
767 delete(openAccounts.names, acc.Name)
772// OpenAccount opens an account by name.
774// No additional data path prefix or ".db" suffix should be added to the name.
775// A single shared account exists per name.
776func OpenAccount(log mlog.Log, name string) (*Account, error) {
778 defer openAccounts.Unlock()
779 if acc, ok := openAccounts.names[name]; ok {
784 if _, ok := mox.Conf.Account(name); !ok {
785 return nil, ErrAccountUnknown
788 acc, err := openAccount(log, name)
792 openAccounts.names[name] = acc
796// openAccount opens an existing account, or creates it if it is missing.
797func openAccount(log mlog.Log, name string) (a *Account, rerr error) {
798 dir := filepath.Join(mox.DataDirPath("accounts"), name)
799 return OpenAccountDB(log, dir, name)
802// OpenAccountDB opens an account database file and returns an initialized account
803// or error. Only exported for use by subcommands that verify the database file.
804// Almost all account opens must go through OpenAccount/OpenEmail/OpenEmailAuth.
805func OpenAccountDB(log mlog.Log, accountDir, accountName string) (a *Account, rerr error) {
806 dbpath := filepath.Join(accountDir, "index.db")
808 // Create account if it doesn't exist yet.
810 if _, err := os.Stat(dbpath); err != nil && os.IsNotExist(err) {
812 os.MkdirAll(accountDir, 0770)
815 db, err := bstore.Open(context.TODO(), dbpath, &bstore.Options{Timeout: 5 * time.Second, Perm: 0660}, DBTypes...)
835 threadsCompleted: make(chan struct{}),
839 if err := initAccount(db); err != nil {
840 return nil, fmt.Errorf("initializing account: %v", err)
842 close(acc.threadsCompleted)
846 // Ensure mailbox counts and total message size are set.
848 err = db.Write(context.TODO(), func(tx *bstore.Tx) error {
849 err := bstore.QueryTx[Mailbox](tx).FilterEqual("HaveCounts", false).ForEach(func(mb Mailbox) error {
852 log.Info("first calculation of mailbox counts for account", slog.String("account", accountName))
854 mc, err := mb.CalculateCounts(tx)
859 mb.MailboxCounts = mc
860 return tx.Update(&mb)
866 du := DiskUsage{ID: 1}
868 if err == nil || !errors.Is(err, bstore.ErrAbsent) {
871 // No DiskUsage record yet, calculate total size and insert.
872 err = bstore.QueryTx[Mailbox](tx).ForEach(func(mb Mailbox) error {
873 du.MessageSize += mb.Size
879 return tx.Insert(&du)
882 return nil, fmt.Errorf("calculating counts for mailbox: %v", err)
885 // Start adding threading if needed.
887 err = db.Write(context.TODO(), func(tx *bstore.Tx) error {
889 if err == bstore.ErrAbsent {
890 if err := tx.Insert(&up); err != nil {
891 return fmt.Errorf("inserting initial upgrade record: %v", err)
898 return nil, fmt.Errorf("checking message threading: %v", err)
901 close(acc.threadsCompleted)
905 // Increase account use before holding on to account in background.
906 // Caller holds the lock. The goroutine below decreases nused by calling
910 // Ensure all messages have a MessageID and SubjectBase, which are needed when
912 // Then assign messages to threads, in the same way we do during imports.
913 log.Info("upgrading account for threading, in background", slog.String("account", acc.Name))
916 err := closeAccount(acc)
917 log.Check(err, "closing use of account after upgrading account storage for threads", slog.String("account", a.Name))
921 x := recover() // Should not happen, but don't take program down if it does.
923 log.Error("upgradeThreads panic", slog.Any("err", x))
925 metrics.PanicInc(metrics.Upgradethreads)
926 acc.threadsErr = fmt.Errorf("panic during upgradeThreads: %v", x)
929 // Mark that upgrade has finished, possibly error is indicated in threadsErr.
930 close(acc.threadsCompleted)
933 err := upgradeThreads(mox.Shutdown, log, acc, &up)
936 log.Errorx("upgrading account for threading, aborted", err, slog.String("account", a.Name))
938 log.Info("upgrading account for threading, completed", slog.String("account", a.Name))
944// ThreadingWait blocks until the one-time account threading upgrade for the
945// account has completed, and returns an error if not successful.
947// To be used before starting an import of messages.
948func (a *Account) ThreadingWait(log mlog.Log) error {
950 case <-a.threadsCompleted:
954 log.Debug("waiting for account upgrade to complete")
960func initAccount(db *bstore.DB) error {
961 return db.Write(context.TODO(), func(tx *bstore.Tx) error {
962 uidvalidity := InitialUIDValidity()
964 if err := tx.Insert(&Upgrade{ID: 1, Threads: 2}); err != nil {
967 if err := tx.Insert(&DiskUsage{ID: 1}); err != nil {
971 if len(mox.Conf.Static.DefaultMailboxes) > 0 {
972 // Deprecated in favor of InitialMailboxes.
973 defaultMailboxes := mox.Conf.Static.DefaultMailboxes
974 mailboxes := []string{"Inbox"}
975 for _, name := range defaultMailboxes {
976 if strings.EqualFold(name, "Inbox") {
979 mailboxes = append(mailboxes, name)
981 for _, name := range mailboxes {
982 mb := Mailbox{Name: name, UIDValidity: uidvalidity, UIDNext: 1, HaveCounts: true}
983 if strings.HasPrefix(name, "Archive") {
985 } else if strings.HasPrefix(name, "Drafts") {
987 } else if strings.HasPrefix(name, "Junk") {
989 } else if strings.HasPrefix(name, "Sent") {
991 } else if strings.HasPrefix(name, "Trash") {
994 if err := tx.Insert(&mb); err != nil {
995 return fmt.Errorf("creating mailbox: %w", err)
997 if err := tx.Insert(&Subscription{name}); err != nil {
998 return fmt.Errorf("adding subscription: %w", err)
1002 mailboxes := mox.Conf.Static.InitialMailboxes
1003 var zerouse config.SpecialUseMailboxes
1004 if mailboxes.SpecialUse == zerouse && len(mailboxes.Regular) == 0 {
1005 mailboxes = DefaultInitialMailboxes
1008 add := func(name string, use SpecialUse) error {
1009 mb := Mailbox{Name: name, UIDValidity: uidvalidity, UIDNext: 1, SpecialUse: use, HaveCounts: true}
1010 if err := tx.Insert(&mb); err != nil {
1011 return fmt.Errorf("creating mailbox: %w", err)
1013 if err := tx.Insert(&Subscription{name}); err != nil {
1014 return fmt.Errorf("adding subscription: %w", err)
1018 addSpecialOpt := func(nameOpt string, use SpecialUse) error {
1022 return add(nameOpt, use)
1028 {"Inbox", SpecialUse{}},
1029 {mailboxes.SpecialUse.Archive, SpecialUse{Archive: true}},
1030 {mailboxes.SpecialUse.Draft, SpecialUse{Draft: true}},
1031 {mailboxes.SpecialUse.Junk, SpecialUse{Junk: true}},
1032 {mailboxes.SpecialUse.Sent, SpecialUse{Sent: true}},
1033 {mailboxes.SpecialUse.Trash, SpecialUse{Trash: true}},
1035 for _, e := range l {
1036 if err := addSpecialOpt(e.nameOpt, e.use); err != nil {
1040 for _, name := range mailboxes.Regular {
1041 if err := add(name, SpecialUse{}); err != nil {
1048 if err := tx.Insert(&NextUIDValidity{1, uidvalidity}); err != nil {
1049 return fmt.Errorf("inserting nextuidvalidity: %w", err)
1055// Close reduces the reference count, and closes the database connection when
1056// it was the last user.
1057func (a *Account) Close() error {
1058 if CheckConsistencyOnClose {
1059 xerr := a.CheckConsistency()
1060 err := closeAccount(a)
1066 return closeAccount(a)
1069// CheckConsistency checks the consistency of the database and returns a non-nil
1070// error for these cases:
1072// - Missing on-disk file for message.
1073// - Mismatch between message size and length of MsgPrefix and on-disk file.
1074// - Missing HaveCounts.
1075// - Incorrect mailbox counts.
1076// - Incorrect total message size.
1077// - Message with UID >= mailbox uid next.
1078// - Mailbox uidvalidity >= account uid validity.
1079// - ModSeq > 0, CreateSeq > 0, CreateSeq <= ModSeq.
1080// - All messages have a nonzero ThreadID, and no cycles in ThreadParentID, and parent messages the same ThreadParentIDs tail.
1081func (a *Account) CheckConsistency() error {
1082 var uidErrors []string // With a limit, could be many.
1083 var modseqErrors []string // With limit.
1084 var fileErrors []string // With limit.
1085 var threadidErrors []string // With limit.
1086 var threadParentErrors []string // With limit.
1087 var threadAncestorErrors []string // With limit.
1090 err := a.DB.Read(context.Background(), func(tx *bstore.Tx) error {
1091 nuv := NextUIDValidity{ID: 1}
1094 return fmt.Errorf("fetching next uid validity: %v", err)
1097 mailboxes := map[int64]Mailbox{}
1098 err = bstore.QueryTx[Mailbox](tx).ForEach(func(mb Mailbox) error {
1099 mailboxes[mb.ID] = mb
1101 if mb.UIDValidity >= nuv.Next {
1102 errmsg := fmt.Sprintf("mailbox %q (id %d) has uidvalidity %d >= account next uidvalidity %d", mb.Name, mb.ID, mb.UIDValidity, nuv.Next)
1103 errors = append(errors, errmsg)
1108 return fmt.Errorf("listing mailboxes: %v", err)
1111 counts := map[int64]MailboxCounts{}
1112 err = bstore.QueryTx[Message](tx).ForEach(func(m Message) error {
1113 mc := counts[m.MailboxID]
1114 mc.Add(m.MailboxCounts())
1115 counts[m.MailboxID] = mc
1117 mb := mailboxes[m.MailboxID]
1119 if (m.ModSeq == 0 || m.CreateSeq == 0 || m.CreateSeq > m.ModSeq) && len(modseqErrors) < 20 {
1120 modseqerr := fmt.Sprintf("message %d in mailbox %q (id %d) has invalid modseq %d or createseq %d, both must be > 0 and createseq <= modseq", m.ID, mb.Name, mb.ID, m.ModSeq, m.CreateSeq)
1121 modseqErrors = append(modseqErrors, modseqerr)
1123 if m.UID >= mb.UIDNext && len(uidErrors) < 20 {
1124 uiderr := fmt.Sprintf("message %d in mailbox %q (id %d) has uid %d >= mailbox uidnext %d", m.ID, mb.Name, mb.ID, m.UID, mb.UIDNext)
1125 uidErrors = append(uidErrors, uiderr)
1130 p := a.MessagePath(m.ID)
1131 st, err := os.Stat(p)
1133 existserr := fmt.Sprintf("message %d in mailbox %q (id %d) on-disk file %s: %v", m.ID, mb.Name, mb.ID, p, err)
1134 fileErrors = append(fileErrors, existserr)
1135 } else if len(fileErrors) < 20 && m.Size != int64(len(m.MsgPrefix))+st.Size() {
1136 sizeerr := fmt.Sprintf("message %d in mailbox %q (id %d) has size %d != len msgprefix %d + on-disk file size %d = %d", m.ID, mb.Name, mb.ID, m.Size, len(m.MsgPrefix), st.Size(), int64(len(m.MsgPrefix))+st.Size())
1137 fileErrors = append(fileErrors, sizeerr)
1140 if m.ThreadID <= 0 && len(threadidErrors) < 20 {
1141 err := fmt.Sprintf("message %d in mailbox %q (id %d) has threadid 0", m.ID, mb.Name, mb.ID)
1142 threadidErrors = append(threadidErrors, err)
1144 if slices.Contains(m.ThreadParentIDs, m.ID) && len(threadParentErrors) < 20 {
1145 err := fmt.Sprintf("message %d in mailbox %q (id %d) references itself in threadparentids", m.ID, mb.Name, mb.ID)
1146 threadParentErrors = append(threadParentErrors, err)
1148 for i, pid := range m.ThreadParentIDs {
1149 am := Message{ID: pid}
1150 if err := tx.Get(&am); err == bstore.ErrAbsent {
1152 } else if err != nil {
1153 return fmt.Errorf("get ancestor message: %v", err)
1154 } else if !slices.Equal(m.ThreadParentIDs[i+1:], am.ThreadParentIDs) && len(threadAncestorErrors) < 20 {
1155 err := fmt.Sprintf("message %d, thread %d has ancestor ids %v, and ancestor at index %d with id %d should have the same tail but has %v\n", m.ID, m.ThreadID, m.ThreadParentIDs, i, am.ID, am.ThreadParentIDs)
1156 threadAncestorErrors = append(threadAncestorErrors, err)
1164 return fmt.Errorf("reading messages: %v", err)
1168 for _, mb := range mailboxes {
1169 totalSize += mb.Size
1171 errmsg := fmt.Sprintf("mailbox %q (id %d) does not have counts, should be %#v", mb.Name, mb.ID, counts[mb.ID])
1172 errors = append(errors, errmsg)
1173 } else if mb.MailboxCounts != counts[mb.ID] {
1174 mbcounterr := fmt.Sprintf("mailbox %q (id %d) has wrong counts %s, should be %s", mb.Name, mb.ID, mb.MailboxCounts, counts[mb.ID])
1175 errors = append(errors, mbcounterr)
1179 du := DiskUsage{ID: 1}
1180 if err := tx.Get(&du); err != nil {
1181 return fmt.Errorf("get diskusage")
1183 if du.MessageSize != totalSize {
1184 errmsg := fmt.Sprintf("total message size in database is %d, sum of mailbox message sizes is %d", du.MessageSize, totalSize)
1185 errors = append(errors, errmsg)
1193 errors = append(errors, uidErrors...)
1194 errors = append(errors, modseqErrors...)
1195 errors = append(errors, fileErrors...)
1196 errors = append(errors, threadidErrors...)
1197 errors = append(errors, threadParentErrors...)
1198 errors = append(errors, threadAncestorErrors...)
1199 if len(errors) > 0 {
1200 return fmt.Errorf("%s", strings.Join(errors, "; "))
1205// Conf returns the configuration for this account if it still exists. During
1206// an SMTP session, a configuration update may drop an account.
1207func (a *Account) Conf() (config.Account, bool) {
1208 return mox.Conf.Account(a.Name)
1211// NextUIDValidity returns the next new/unique uidvalidity to use for this account.
1212func (a *Account) NextUIDValidity(tx *bstore.Tx) (uint32, error) {
1213 nuv := NextUIDValidity{ID: 1}
1214 if err := tx.Get(&nuv); err != nil {
1219 if err := tx.Update(&nuv); err != nil {
1225// NextModSeq returns the next modification sequence, which is global per account,
1227func (a *Account) NextModSeq(tx *bstore.Tx) (ModSeq, error) {
1228 v := SyncState{ID: 1}
1229 if err := tx.Get(&v); err == bstore.ErrAbsent {
1230 // We start assigning from modseq 2. Modseq 0 is not usable, so returned as 1, so
1232 // HighestDeletedModSeq is -1 so comparison against the default ModSeq zero value
1234 v = SyncState{1, 2, -1}
1235 return v.LastModSeq, tx.Insert(&v)
1236 } else if err != nil {
1240 return v.LastModSeq, tx.Update(&v)
1243func (a *Account) HighestDeletedModSeq(tx *bstore.Tx) (ModSeq, error) {
1244 v := SyncState{ID: 1}
1246 if err == bstore.ErrAbsent {
1249 return v.HighestDeletedModSeq, err
1252// WithWLock runs fn with account writelock held. Necessary for account/mailbox modification. For message delivery, a read lock is required.
1253func (a *Account) WithWLock(fn func()) {
1259// WithRLock runs fn with account read lock held. Needed for message delivery.
1260func (a *Account) WithRLock(fn func()) {
1266// DeliverMessage delivers a mail message to the account.
1268// The message, with msg.MsgPrefix and msgFile combined, must have a header
1269// section. The caller is responsible for adding a header separator to
1270// msg.MsgPrefix if missing from an incoming message.
1272// If the destination mailbox has the Sent special-use flag, the message is parsed
1273// for its recipients (to/cc/bcc). Their domains are added to Recipients for use in
1276// If sync is true, the message file and its directory are synced. Should be true
1277// for regular mail delivery, but can be false when importing many messages.
1279// If updateDiskUsage is true, the account total message size (for quota) is
1280// updated. Callers must check if a message can be added within quota before
1281// calling DeliverMessage.
1283// If CreateSeq/ModSeq is not set, it is assigned automatically.
1285// Must be called with account rlock or wlock.
1287// Caller must broadcast new message.
1289// Caller must update mailbox counts.
1290func (a *Account) DeliverMessage(log mlog.Log, tx *bstore.Tx, m *Message, msgFile *os.File, sync, notrain, nothreads, updateDiskUsage bool) error {
1292 return fmt.Errorf("cannot deliver expunged message")
1295 mb := Mailbox{ID: m.MailboxID}
1296 if err := tx.Get(&mb); err != nil {
1297 return fmt.Errorf("get mailbox: %w", err)
1301 if err := tx.Update(&mb); err != nil {
1302 return fmt.Errorf("updating mailbox nextuid: %w", err)
1305 if updateDiskUsage {
1306 du := DiskUsage{ID: 1}
1307 if err := tx.Get(&du); err != nil {
1308 return fmt.Errorf("get disk usage: %v", err)
1310 du.MessageSize += m.Size
1311 if err := tx.Update(&du); err != nil {
1312 return fmt.Errorf("update disk usage: %v", err)
1317 m.JunkFlagsForMailbox(mb, conf)
1319 mr := FileMsgReader(m.MsgPrefix, msgFile) // We don't close, it would close the msgFile.
1320 var part *message.Part
1321 if m.ParsedBuf == nil {
1322 p, err := message.EnsurePart(log.Logger, false, mr, m.Size)
1324 log.Infox("parsing delivered message", err, slog.String("parse", ""), slog.Int64("message", m.ID))
1325 // We continue, p is still valid.
1328 buf, err := json.Marshal(part)
1330 return fmt.Errorf("marshal parsed message: %w", err)
1335 if err := json.Unmarshal(m.ParsedBuf, &p); err != nil {
1336 log.Errorx("unmarshal parsed message, continuing", err, slog.String("parse", ""))
1342 // If we are delivering to the originally intended mailbox, no need to store the mailbox ID again.
1343 if m.MailboxDestinedID != 0 && m.MailboxDestinedID == m.MailboxOrigID {
1344 m.MailboxDestinedID = 0
1346 if m.CreateSeq == 0 || m.ModSeq == 0 {
1347 modseq, err := a.NextModSeq(tx)
1349 return fmt.Errorf("assigning next modseq: %w", err)
1351 m.CreateSeq = modseq
1355 if part != nil && m.MessageID == "" && m.SubjectBase == "" {
1356 m.PrepareThreading(log, part)
1359 // Assign to thread (if upgrade has completed).
1360 noThreadID := nothreads
1361 if m.ThreadID == 0 && !nothreads && part != nil {
1363 case <-a.threadsCompleted:
1364 if a.threadsErr != nil {
1365 log.Info("not assigning threads for new delivery, upgrading to threads failed")
1368 if err := assignThread(log, tx, m, part); err != nil {
1369 return fmt.Errorf("assigning thread: %w", err)
1373 // note: since we have a write transaction to get here, we can't wait for the
1374 // thread upgrade to finish.
1375 // If we don't assign a threadid the upgrade process will do it.
1376 log.Info("not assigning threads for new delivery, upgrading to threads in progress which will assign this message")
1381 if err := tx.Insert(m); err != nil {
1382 return fmt.Errorf("inserting message: %w", err)
1384 if !noThreadID && m.ThreadID == 0 {
1386 if err := tx.Update(m); err != nil {
1387 return fmt.Errorf("updating message for its own thread id: %w", err)
1391 // todo: perhaps we should match the recipients based on smtp submission and a matching message-id? we now miss the addresses in bcc's. for webmail, we could insert the recipients directly.
1392 if mb.Sent && part != nil && part.Envelope != nil {
1401 addrs := append(append(e.To, e.CC...), e.BCC...)
1402 for _, addr := range addrs {
1403 if addr.User == "" {
1404 // Would trigger error because Recipient.Localpart must be nonzero. todo: we could allow empty localpart in db, and filter by not using FilterNonzero.
1405 log.Info("to/cc/bcc address with empty localpart, not inserting as recipient", slog.Any("address", addr))
1408 d, err := dns.ParseDomain(addr.Host)
1410 log.Debugx("parsing domain in to/cc/bcc address", err, slog.Any("address", addr))
1415 Localpart: smtp.Localpart(addr.User),
1417 OrgDomain: publicsuffix.Lookup(context.TODO(), log.Logger, d).Name(),
1420 if err := tx.Insert(&mr); err != nil {
1421 return fmt.Errorf("inserting sent message recipients: %w", err)
1426 msgPath := a.MessagePath(m.ID)
1427 msgDir := filepath.Dir(msgPath)
1428 os.MkdirAll(msgDir, 0770)
1430 // Sync file data to disk.
1432 if err := msgFile.Sync(); err != nil {
1433 return fmt.Errorf("fsync message file: %w", err)
1437 if err := moxio.LinkOrCopy(log, msgPath, msgFile.Name(), &moxio.AtReader{R: msgFile}, true); err != nil {
1438 return fmt.Errorf("linking/copying message to new file: %w", err)
1442 if err := moxio.SyncDir(log, msgDir); err != nil {
1443 xerr := os.Remove(msgPath)
1444 log.Check(xerr, "removing message after syncdir error", slog.String("path", msgPath))
1445 return fmt.Errorf("sync directory: %w", err)
1449 if !notrain && m.NeedsTraining() {
1451 if err := a.RetrainMessages(context.TODO(), log, tx, l, false); err != nil {
1452 xerr := os.Remove(msgPath)
1453 log.Check(xerr, "removing message after syncdir error", slog.String("path", msgPath))
1454 return fmt.Errorf("training junkfilter: %w", err)
1462// SetPassword saves a new password for this account. This password is used for
1463// IMAP, SMTP (submission) sessions and the HTTP account web page.
1464func (a *Account) SetPassword(log mlog.Log, password string) error {
1465 hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
1467 return fmt.Errorf("generating password hash: %w", err)
1470 err = a.DB.Write(context.TODO(), func(tx *bstore.Tx) error {
1471 if _, err := bstore.QueryTx[Password](tx).Delete(); err != nil {
1472 return fmt.Errorf("deleting existing password: %v", err)
1475 pw.Hash = string(hash)
1477 // CRAM-MD5 calculates an HMAC-MD5, with the password as key, over a per-attempt
1478 // unique text that includes a timestamp. HMAC performs two hashes. Both times, the
1479 // first block is based on the key/password. We hash those first blocks now, and
1480 // store the hash state in the database. When we actually authenticate, we'll
1481 // complete the HMAC by hashing only the text. We cannot store crypto/hmac's hash,
1482 // because it does not expose its internal state and isn't a BinaryMarshaler.
1484 pw.CRAMMD5.Ipad = md5.New()
1485 pw.CRAMMD5.Opad = md5.New()
1486 key := []byte(password)
1491 ipad := make([]byte, md5.BlockSize)
1492 opad := make([]byte, md5.BlockSize)
1495 for i := range ipad {
1499 pw.CRAMMD5.Ipad.Write(ipad)
1500 pw.CRAMMD5.Opad.Write(opad)
1502 pw.SCRAMSHA1.Salt = scram.MakeRandom()
1503 pw.SCRAMSHA1.Iterations = 2 * 4096
1504 pw.SCRAMSHA1.SaltedPassword = scram.SaltPassword(sha1.New, password, pw.SCRAMSHA1.Salt, pw.SCRAMSHA1.Iterations)
1506 pw.SCRAMSHA256.Salt = scram.MakeRandom()
1507 pw.SCRAMSHA256.Iterations = 4096
1508 pw.SCRAMSHA256.SaltedPassword = scram.SaltPassword(sha256.New, password, pw.SCRAMSHA256.Salt, pw.SCRAMSHA256.Iterations)
1510 if err := tx.Insert(&pw); err != nil {
1511 return fmt.Errorf("inserting new password: %v", err)
1514 return sessionRemoveAll(context.TODO(), log, tx, a.Name)
1517 log.Info("new password set for account", slog.String("account", a.Name))
1522// Subjectpass returns the signing key for use with subjectpass for the given
1523// email address with canonical localpart.
1524func (a *Account) Subjectpass(email string) (key string, err error) {
1525 return key, a.DB.Write(context.TODO(), func(tx *bstore.Tx) error {
1526 v := Subjectpass{Email: email}
1532 if !errors.Is(err, bstore.ErrAbsent) {
1533 return fmt.Errorf("get subjectpass key from accounts database: %w", err)
1536 const chars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1537 buf := make([]byte, 16)
1538 if _, err := cryptorand.Read(buf); err != nil {
1541 for _, b := range buf {
1542 key += string(chars[int(b)%len(chars)])
1545 return tx.Insert(&v)
1549// Ensure mailbox is present in database, adding records for the mailbox and its
1550// parents if they aren't present.
1552// If subscribe is true, any mailboxes that were created will also be subscribed to.
1553// Caller must hold account wlock.
1554// Caller must propagate changes if any.
1555func (a *Account) MailboxEnsure(tx *bstore.Tx, name string, subscribe bool) (mb Mailbox, changes []Change, rerr error) {
1556 if norm.NFC.String(name) != name {
1557 return Mailbox{}, nil, fmt.Errorf("mailbox name not normalized")
1560 // Quick sanity check.
1561 if strings.EqualFold(name, "inbox") && name != "Inbox" {
1562 return Mailbox{}, nil, fmt.Errorf("bad casing for inbox")
1565 elems := strings.Split(name, "/")
1566 q := bstore.QueryTx[Mailbox](tx)
1567 q.FilterFn(func(mb Mailbox) bool {
1568 return mb.Name == elems[0] || strings.HasPrefix(mb.Name, elems[0]+"/")
1572 return Mailbox{}, nil, fmt.Errorf("list mailboxes: %v", err)
1575 mailboxes := map[string]Mailbox{}
1576 for _, xmb := range l {
1577 mailboxes[xmb.Name] = xmb
1581 for _, elem := range elems {
1587 mb, ok = mailboxes[p]
1591 uidval, err := a.NextUIDValidity(tx)
1593 return Mailbox{}, nil, fmt.Errorf("next uid validity: %v", err)
1597 UIDValidity: uidval,
1601 err = tx.Insert(&mb)
1603 return Mailbox{}, nil, fmt.Errorf("creating new mailbox: %v", err)
1608 if tx.Get(&Subscription{p}) != nil {
1609 err := tx.Insert(&Subscription{p})
1611 return Mailbox{}, nil, fmt.Errorf("subscribing to mailbox: %v", err)
1614 flags = []string{`\Subscribed`}
1616 changes = append(changes, ChangeAddMailbox{mb, flags})
1618 return mb, changes, nil
1621// MailboxExists checks if mailbox exists.
1622// Caller must hold account rlock.
1623func (a *Account) MailboxExists(tx *bstore.Tx, name string) (bool, error) {
1624 q := bstore.QueryTx[Mailbox](tx)
1625 q.FilterEqual("Name", name)
1629// MailboxFind finds a mailbox by name, returning a nil mailbox and nil error if mailbox does not exist.
1630func (a *Account) MailboxFind(tx *bstore.Tx, name string) (*Mailbox, error) {
1631 q := bstore.QueryTx[Mailbox](tx)
1632 q.FilterEqual("Name", name)
1634 if err == bstore.ErrAbsent {
1638 return nil, fmt.Errorf("looking up mailbox: %w", err)
1643// SubscriptionEnsure ensures a subscription for name exists. The mailbox does not
1644// have to exist. Any parents are not automatically subscribed.
1645// Changes are returned and must be broadcasted by the caller.
1646func (a *Account) SubscriptionEnsure(tx *bstore.Tx, name string) ([]Change, error) {
1647 if err := tx.Get(&Subscription{name}); err == nil {
1651 if err := tx.Insert(&Subscription{name}); err != nil {
1652 return nil, fmt.Errorf("inserting subscription: %w", err)
1655 q := bstore.QueryTx[Mailbox](tx)
1656 q.FilterEqual("Name", name)
1659 return []Change{ChangeAddSubscription{name, nil}}, nil
1660 } else if err != bstore.ErrAbsent {
1661 return nil, fmt.Errorf("looking up mailbox for subscription: %w", err)
1663 return []Change{ChangeAddSubscription{name, []string{`\NonExistent`}}}, nil
1666// MessageRuleset returns the first ruleset (if any) that message the message
1667// represented by msgPrefix and msgFile, with smtp and validation fields from m.
1668func MessageRuleset(log mlog.Log, dest config.Destination, m *Message, msgPrefix []byte, msgFile *os.File) *config.Ruleset {
1669 if len(dest.Rulesets) == 0 {
1673 mr := FileMsgReader(msgPrefix, msgFile) // We don't close, it would close the msgFile.
1674 p, err := message.Parse(log.Logger, false, mr)
1676 log.Errorx("parsing message for evaluating rulesets, continuing with headers", err, slog.String("parse", ""))
1677 // note: part is still set.
1679 // todo optimize: only parse header if needed for rulesets. and probably reuse an earlier parsing.
1680 header, err := p.Header()
1682 log.Errorx("parsing message headers for evaluating rulesets, delivering to default mailbox", err, slog.String("parse", ""))
1683 // todo: reject message?
1688 for _, rs := range dest.Rulesets {
1689 if rs.SMTPMailFromRegexpCompiled != nil {
1690 if !rs.SMTPMailFromRegexpCompiled.MatchString(m.MailFrom) {
1695 if !rs.VerifiedDNSDomain.IsZero() {
1696 d := rs.VerifiedDNSDomain.Name()
1698 matchDomain := func(s string) bool {
1699 return s == d || strings.HasSuffix(s, suffix)
1702 if m.EHLOValidated && matchDomain(m.EHLODomain) {
1705 if m.MailFromValidated && matchDomain(m.MailFromDomain) {
1708 for _, d := range m.DKIMDomains {
1720 for _, t := range rs.HeadersRegexpCompiled {
1721 for k, vl := range header {
1722 k = strings.ToLower(k)
1723 if !t[0].MatchString(k) {
1726 for _, v := range vl {
1727 v = strings.ToLower(strings.TrimSpace(v))
1728 if t[1].MatchString(v) {
1740// MessagePath returns the file system path of a message.
1741func (a *Account) MessagePath(messageID int64) string {
1742 return strings.Join(append([]string{a.Dir, "msg"}, messagePathElems(messageID)...), string(filepath.Separator))
1745// MessageReader opens a message for reading, transparently combining the
1746// message prefix with the original incoming message.
1747func (a *Account) MessageReader(m Message) *MsgReader {
1748 return &MsgReader{prefix: m.MsgPrefix, path: a.MessagePath(m.ID), size: m.Size}
1751// DeliverDestination delivers an email to dest, based on the configured rulesets.
1753// Returns ErrOverQuota when account would be over quota after adding message.
1755// Caller must hold account wlock (mailbox may be created).
1756// Message delivery, possible mailbox creation, and updated mailbox counts are
1758func (a *Account) DeliverDestination(log mlog.Log, dest config.Destination, m *Message, msgFile *os.File) error {
1760 rs := MessageRuleset(log, dest, m, m.MsgPrefix, msgFile)
1762 mailbox = rs.Mailbox
1763 } else if dest.Mailbox == "" {
1766 mailbox = dest.Mailbox
1768 return a.DeliverMailbox(log, mailbox, m, msgFile)
1771// DeliverMailbox delivers an email to the specified mailbox.
1773// Returns ErrOverQuota when account would be over quota after adding message.
1775// Caller must hold account wlock (mailbox may be created).
1776// Message delivery, possible mailbox creation, and updated mailbox counts are
1778func (a *Account) DeliverMailbox(log mlog.Log, mailbox string, m *Message, msgFile *os.File) error {
1779 var changes []Change
1780 err := a.DB.Write(context.TODO(), func(tx *bstore.Tx) error {
1781 if ok, _, err := a.CanAddMessageSize(tx, m.Size); err != nil {
1787 mb, chl, err := a.MailboxEnsure(tx, mailbox, true)
1789 return fmt.Errorf("ensuring mailbox: %w", err)
1792 m.MailboxOrigID = mb.ID
1794 // Update count early, DeliverMessage will update mb too and we don't want to fetch
1795 // it again before updating.
1796 mb.MailboxCounts.Add(m.MailboxCounts())
1797 if err := tx.Update(&mb); err != nil {
1798 return fmt.Errorf("updating mailbox for delivery: %w", err)
1801 if err := a.DeliverMessage(log, tx, m, msgFile, true, false, false, true); err != nil {
1805 changes = append(changes, chl...)
1806 changes = append(changes, m.ChangeAddUID(), mb.ChangeCounts())
1809 // todo: if rename succeeded but transaction failed, we should remove the file.
1814 BroadcastChanges(a, changes)
1818// TidyRejectsMailbox removes old reject emails, and returns whether there is space for a new delivery.
1820// Caller most hold account wlock.
1821// Changes are broadcasted.
1822func (a *Account) TidyRejectsMailbox(log mlog.Log, rejectsMailbox string) (hasSpace bool, rerr error) {
1823 var changes []Change
1825 var remove []Message
1827 for _, m := range remove {
1828 p := a.MessagePath(m.ID)
1830 log.Check(err, "removing rejects message file", slog.String("path", p))
1834 err := a.DB.Write(context.TODO(), func(tx *bstore.Tx) error {
1835 mb, err := a.MailboxFind(tx, rejectsMailbox)
1837 return fmt.Errorf("finding mailbox: %w", err)
1840 // No messages have been delivered yet.
1845 // Gather old messages to remove.
1846 old := time.Now().Add(-14 * 24 * time.Hour)
1847 qdel := bstore.QueryTx[Message](tx)
1848 qdel.FilterNonzero(Message{MailboxID: mb.ID})
1849 qdel.FilterEqual("Expunged", false)
1850 qdel.FilterLess("Received", old)
1851 remove, err = qdel.List()
1853 return fmt.Errorf("listing old messages: %w", err)
1856 changes, err = a.rejectsRemoveMessages(context.TODO(), log, tx, mb, remove)
1858 return fmt.Errorf("removing messages: %w", err)
1861 // We allow up to n messages.
1862 qcount := bstore.QueryTx[Message](tx)
1863 qcount.FilterNonzero(Message{MailboxID: mb.ID})
1864 qcount.FilterEqual("Expunged", false)
1866 n, err := qcount.Count()
1868 return fmt.Errorf("counting rejects: %w", err)
1875 remove = nil // Don't remove files on failure.
1879 BroadcastChanges(a, changes)
1881 return hasSpace, nil
1884func (a *Account) rejectsRemoveMessages(ctx context.Context, log mlog.Log, tx *bstore.Tx, mb *Mailbox, l []Message) ([]Change, error) {
1888 ids := make([]int64, len(l))
1889 anyids := make([]any, len(l))
1890 for i, m := range l {
1895 // Remove any message recipients. Should not happen, but a user can move messages
1896 // from a Sent mailbox to the rejects mailbox...
1897 qdmr := bstore.QueryTx[Recipient](tx)
1898 qdmr.FilterEqual("MessageID", anyids...)
1899 if _, err := qdmr.Delete(); err != nil {
1900 return nil, fmt.Errorf("deleting from message recipient: %w", err)
1903 // Assign new modseq.
1904 modseq, err := a.NextModSeq(tx)
1906 return nil, fmt.Errorf("assign next modseq: %w", err)
1909 // Expunge the messages.
1910 qx := bstore.QueryTx[Message](tx)
1912 var expunged []Message
1913 qx.Gather(&expunged)
1914 if _, err := qx.UpdateNonzero(Message{ModSeq: modseq, Expunged: true}); err != nil {
1915 return nil, fmt.Errorf("expunging messages: %w", err)
1919 for _, m := range expunged {
1920 m.Expunged = false // Was set by update, but would cause wrong count.
1921 mb.MailboxCounts.Sub(m.MailboxCounts())
1924 if err := tx.Update(mb); err != nil {
1925 return nil, fmt.Errorf("updating mailbox counts: %w", err)
1927 if err := a.AddMessageSize(log, tx, -totalSize); err != nil {
1928 return nil, fmt.Errorf("updating disk usage: %w", err)
1931 // Mark as neutral and train so junk filter gets untrained with these (junk) messages.
1932 for i := range expunged {
1933 expunged[i].Junk = false
1934 expunged[i].Notjunk = false
1936 if err := a.RetrainMessages(ctx, log, tx, expunged, true); err != nil {
1937 return nil, fmt.Errorf("retraining expunged messages: %w", err)
1940 changes := make([]Change, len(l), len(l)+1)
1941 for i, m := range l {
1942 changes[i] = ChangeRemoveUIDs{mb.ID, []UID{m.UID}, modseq}
1944 changes = append(changes, mb.ChangeCounts())
1948// RejectsRemove removes a message from the rejects mailbox if present.
1949// Caller most hold account wlock.
1950// Changes are broadcasted.
1951func (a *Account) RejectsRemove(log mlog.Log, rejectsMailbox, messageID string) error {
1952 var changes []Change
1954 var remove []Message
1956 for _, m := range remove {
1957 p := a.MessagePath(m.ID)
1959 log.Check(err, "removing rejects message file", slog.String("path", p))
1963 err := a.DB.Write(context.TODO(), func(tx *bstore.Tx) error {
1964 mb, err := a.MailboxFind(tx, rejectsMailbox)
1966 return fmt.Errorf("finding mailbox: %w", err)
1972 q := bstore.QueryTx[Message](tx)
1973 q.FilterNonzero(Message{MailboxID: mb.ID, MessageID: messageID})
1974 q.FilterEqual("Expunged", false)
1975 remove, err = q.List()
1977 return fmt.Errorf("listing messages to remove: %w", err)
1980 changes, err = a.rejectsRemoveMessages(context.TODO(), log, tx, mb, remove)
1982 return fmt.Errorf("removing messages: %w", err)
1988 remove = nil // Don't remove files on failure.
1992 BroadcastChanges(a, changes)
1997// AddMessageSize adjusts the DiskUsage.MessageSize by size.
1998func (a *Account) AddMessageSize(log mlog.Log, tx *bstore.Tx, size int64) error {
1999 du := DiskUsage{ID: 1}
2000 if err := tx.Get(&du); err != nil {
2001 return fmt.Errorf("get diskusage: %v", err)
2003 du.MessageSize += size
2004 if du.MessageSize < 0 {
2005 log.Error("negative total message size", slog.Int64("delta", size), slog.Int64("newtotalsize", du.MessageSize))
2007 if err := tx.Update(&du); err != nil {
2008 return fmt.Errorf("update total message size: %v", err)
2013// QuotaMessageSize returns the effective maximum total message size for an
2014// account. Returns 0 if there is no maximum.
2015func (a *Account) QuotaMessageSize() int64 {
2017 size := conf.QuotaMessageSize
2019 size = mox.Conf.Static.QuotaMessageSize
2027// CanAddMessageSize checks if a message of size bytes can be added, depending on
2028// total message size and configured quota for account.
2029func (a *Account) CanAddMessageSize(tx *bstore.Tx, size int64) (ok bool, maxSize int64, err error) {
2030 maxSize = a.QuotaMessageSize()
2035 du := DiskUsage{ID: 1}
2036 if err := tx.Get(&du); err != nil {
2037 return false, maxSize, fmt.Errorf("get diskusage: %v", err)
2039 return du.MessageSize+size <= maxSize, maxSize, nil
2042// We keep a cache of recent successful authentications, so we don't have to bcrypt successful calls each time.
2043var authCache = struct {
2045 success map[authKey]string
2047 success: map[authKey]string{},
2050type authKey struct {
2054// StartAuthCache starts a goroutine that regularly clears the auth cache.
2055func StartAuthCache() {
2056 go manageAuthCache()
2059func manageAuthCache() {
2062 authCache.success = map[authKey]string{}
2064 time.Sleep(15 * time.Minute)
2068// OpenEmailAuth opens an account given an email address and password.
2070// The email address may contain a catchall separator.
2071func OpenEmailAuth(log mlog.Log, email string, password string) (acc *Account, rerr error) {
2072 acc, _, rerr = OpenEmail(log, email)
2078 if rerr != nil && acc != nil {
2080 log.Check(err, "closing account after open auth failure")
2085 pw, err := bstore.QueryDB[Password](context.TODO(), acc.DB).Get()
2087 if err == bstore.ErrAbsent {
2088 return acc, ErrUnknownCredentials
2090 return acc, fmt.Errorf("looking up password: %v", err)
2093 ok := len(password) >= 8 && authCache.success[authKey{email, pw.Hash}] == password
2098 if err := bcrypt.CompareHashAndPassword([]byte(pw.Hash), []byte(password)); err != nil {
2099 rerr = ErrUnknownCredentials
2102 authCache.success[authKey{email, pw.Hash}] = password
2108// OpenEmail opens an account given an email address.
2110// The email address may contain a catchall separator.
2111func OpenEmail(log mlog.Log, email string) (*Account, config.Destination, error) {
2112 addr, err := smtp.ParseAddress(email)
2114 return nil, config.Destination{}, fmt.Errorf("%w: %v", ErrUnknownCredentials, err)
2116 accountName, _, dest, err := mox.FindAccount(addr.Localpart, addr.Domain, false)
2117 if err != nil && (errors.Is(err, mox.ErrAccountNotFound) || errors.Is(err, mox.ErrDomainNotFound)) {
2118 return nil, config.Destination{}, ErrUnknownCredentials
2119 } else if err != nil {
2120 return nil, config.Destination{}, fmt.Errorf("looking up address: %v", err)
2122 acc, err := OpenAccount(log, accountName)
2124 return nil, config.Destination{}, err
2126 return acc, dest, nil
2129// 64 characters, must be power of 2 for MessagePath
2130const msgDirChars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
2132// MessagePath returns the filename of the on-disk filename, relative to the
2133// containing directory such as <account>/msg or queue.
2134// Returns names like "AB/1".
2135func MessagePath(messageID int64) string {
2136 return strings.Join(messagePathElems(messageID), string(filepath.Separator))
2139// messagePathElems returns the elems, for a single join without intermediate
2140// string allocations.
2141func messagePathElems(messageID int64) []string {
2142 v := messageID >> 13 // 8k files per directory.
2145 dir += string(msgDirChars[int(v)&(len(msgDirChars)-1)])
2151 return []string{dir, strconv.FormatInt(messageID, 10)}
2154// Set returns a copy of f, with each flag that is true in mask set to the
2156func (f Flags) Set(mask, flags Flags) Flags {
2157 set := func(d *bool, m, v bool) {
2163 set(&r.Seen, mask.Seen, flags.Seen)
2164 set(&r.Answered, mask.Answered, flags.Answered)
2165 set(&r.Flagged, mask.Flagged, flags.Flagged)
2166 set(&r.Forwarded, mask.Forwarded, flags.Forwarded)
2167 set(&r.Junk, mask.Junk, flags.Junk)
2168 set(&r.Notjunk, mask.Notjunk, flags.Notjunk)
2169 set(&r.Deleted, mask.Deleted, flags.Deleted)
2170 set(&r.Draft, mask.Draft, flags.Draft)
2171 set(&r.Phishing, mask.Phishing, flags.Phishing)
2172 set(&r.MDNSent, mask.MDNSent, flags.MDNSent)
2176// Changed returns a mask of flags that have been between f and other.
2177func (f Flags) Changed(other Flags) (mask Flags) {
2178 mask.Seen = f.Seen != other.Seen
2179 mask.Answered = f.Answered != other.Answered
2180 mask.Flagged = f.Flagged != other.Flagged
2181 mask.Forwarded = f.Forwarded != other.Forwarded
2182 mask.Junk = f.Junk != other.Junk
2183 mask.Notjunk = f.Notjunk != other.Notjunk
2184 mask.Deleted = f.Deleted != other.Deleted
2185 mask.Draft = f.Draft != other.Draft
2186 mask.Phishing = f.Phishing != other.Phishing
2187 mask.MDNSent = f.MDNSent != other.MDNSent
2191var systemWellKnownFlags = map[string]bool{
2204// ParseFlagsKeywords parses a list of textual flags into system/known flags, and
2205// other keywords. Keywords are lower-cased and sorted and check for valid syntax.
2206func ParseFlagsKeywords(l []string) (flags Flags, keywords []string, rerr error) {
2207 fields := map[string]*bool{
2208 `\answered`: &flags.Answered,
2209 `\flagged`: &flags.Flagged,
2210 `\deleted`: &flags.Deleted,
2211 `\seen`: &flags.Seen,
2212 `\draft`: &flags.Draft,
2213 `$junk`: &flags.Junk,
2214 `$notjunk`: &flags.Notjunk,
2215 `$forwarded`: &flags.Forwarded,
2216 `$phishing`: &flags.Phishing,
2217 `$mdnsent`: &flags.MDNSent,
2219 seen := map[string]bool{}
2220 for _, f := range l {
2221 f = strings.ToLower(f)
2222 if field, ok := fields[f]; ok {
2226 return Flags{}, nil, fmt.Errorf("duplicate keyword %s", f)
2229 if err := CheckKeyword(f); err != nil {
2230 return Flags{}, nil, fmt.Errorf("invalid keyword %s", f)
2232 keywords = append(keywords, f)
2236 sort.Strings(keywords)
2237 return flags, keywords, nil
2240// RemoveKeywords removes keywords from l, returning whether any modifications were
2241// made, and a slice, a new slice in case of modifications. Keywords must have been
2242// validated earlier, e.g. through ParseFlagKeywords or CheckKeyword. Should only
2243// be used with valid keywords, not with system flags like \Seen.
2244func RemoveKeywords(l, remove []string) ([]string, bool) {
2247 for _, k := range remove {
2248 if i := slices.Index(l, k); i >= 0 {
2250 l = append([]string{}, l...)
2253 copy(l[i:], l[i+1:])
2261// MergeKeywords adds keywords from add into l, returning whether it added any
2262// keyword, and the slice with keywords, a new slice if modifications were made.
2263// Keywords are only added if they aren't already present. Should only be used with
2264// keywords, not with system flags like \Seen.
2265func MergeKeywords(l, add []string) ([]string, bool) {
2268 for _, k := range add {
2269 if !slices.Contains(l, k) {
2271 l = append([]string{}, l...)
2284// CheckKeyword returns an error if kw is not a valid keyword. Kw should
2285// already be in lower-case.
2286func CheckKeyword(kw string) error {
2288 return fmt.Errorf("keyword cannot be empty")
2290 if systemWellKnownFlags[kw] {
2291 return fmt.Errorf("cannot use well-known flag as keyword")
2293 for _, c := range kw {
2295 if c <= ' ' || c > 0x7e || c >= 'A' && c <= 'Z' || strings.ContainsRune(`(){%*"\]`, c) {
2296 return errors.New(`not a valid keyword, must be lower-case ascii without spaces and without any of these characters: (){%*"\]`)
2302// SendLimitReached checks whether sending a message to recipients would reach
2303// the limit of outgoing messages for the account. If so, the message should
2304// not be sent. If the returned numbers are >= 0, the limit was reached and the
2305// values are the configured limits.
2307// To limit damage to the internet and our reputation in case of account
2308// compromise, we limit the max number of messages sent in a 24 hour window, both
2309// total number of messages and number of first-time recipients.
2310func (a *Account) SendLimitReached(tx *bstore.Tx, recipients []smtp.Path) (msglimit, rcptlimit int, rerr error) {
2312 msgmax := conf.MaxOutgoingMessagesPerDay
2314 // For human senders, 1000 recipients in a day is quite a lot.
2317 rcptmax := conf.MaxFirstTimeRecipientsPerDay
2319 // Human senders may address a new human-sized list of people once in a while. In
2320 // case of a compromise, a spammer will probably try to send to many new addresses.
2324 rcpts := map[string]time.Time{}
2326 err := bstore.QueryTx[Outgoing](tx).FilterGreater("Submitted", time.Now().Add(-24*time.Hour)).ForEach(func(o Outgoing) error {
2328 if rcpts[o.Recipient].IsZero() || o.Submitted.Before(rcpts[o.Recipient]) {
2329 rcpts[o.Recipient] = o.Submitted
2334 return -1, -1, fmt.Errorf("querying message recipients in past 24h: %w", err)
2336 if n+len(recipients) > msgmax {
2337 return msgmax, -1, nil
2340 // Only check if max first-time recipients is reached if there are enough messages
2341 // to trigger the limit.
2342 if n+len(recipients) < rcptmax {
2346 isFirstTime := func(rcpt string, before time.Time) (bool, error) {
2347 exists, err := bstore.QueryTx[Outgoing](tx).FilterNonzero(Outgoing{Recipient: rcpt}).FilterLess("Submitted", before).Exists()
2353 for _, r := range recipients {
2354 if first, err := isFirstTime(r.XString(true), now); err != nil {
2355 return -1, -1, fmt.Errorf("checking whether recipient is first-time: %v", err)
2360 for r, t := range rcpts {
2361 if first, err := isFirstTime(r, t); err != nil {
2362 return -1, -1, fmt.Errorf("checking whether recipient is first-time: %v", err)
2367 if firsttime > rcptmax {
2368 return -1, rcptmax, nil
2373// MailboxCreate creates a new mailbox, including any missing parent mailboxes,
2374// the total list of created mailboxes is returned in created. On success, if
2375// exists is false and rerr nil, the changes must be broadcasted by the caller.
2377// Name must be in normalized form.
2378func (a *Account) MailboxCreate(tx *bstore.Tx, name string) (changes []Change, created []string, exists bool, rerr error) {
2379 elems := strings.Split(name, "/")
2381 for i, elem := range elems {
2386 exists, err := a.MailboxExists(tx, p)
2388 return nil, nil, false, fmt.Errorf("checking if mailbox exists")
2391 if i == len(elems)-1 {
2392 return nil, nil, true, fmt.Errorf("mailbox already exists")
2396 _, nchanges, err := a.MailboxEnsure(tx, p, true)
2398 return nil, nil, false, fmt.Errorf("ensuring mailbox exists")
2400 changes = append(changes, nchanges...)
2401 created = append(created, p)
2403 return changes, created, false, nil
2406// MailboxRename renames mailbox mbsrc to dst, and any missing parents for the
2407// destination, and any children of mbsrc and the destination.
2409// Names must be normalized and cannot be Inbox.
2410func (a *Account) MailboxRename(tx *bstore.Tx, mbsrc Mailbox, dst string) (changes []Change, isInbox, notExists, alreadyExists bool, rerr error) {
2411 if mbsrc.Name == "Inbox" || dst == "Inbox" {
2412 return nil, true, false, false, fmt.Errorf("inbox cannot be renamed")
2415 // We gather existing mailboxes that we need for deciding what to create/delete/update.
2416 q := bstore.QueryTx[Mailbox](tx)
2417 srcPrefix := mbsrc.Name + "/"
2418 dstRoot := strings.SplitN(dst, "/", 2)[0]
2419 dstRootPrefix := dstRoot + "/"
2420 q.FilterFn(func(mb Mailbox) bool {
2421 return mb.Name == mbsrc.Name || strings.HasPrefix(mb.Name, srcPrefix) || mb.Name == dstRoot || strings.HasPrefix(mb.Name, dstRootPrefix)
2423 q.SortAsc("Name") // We'll rename the parents before children.
2426 return nil, false, false, false, fmt.Errorf("listing relevant mailboxes: %v", err)
2429 mailboxes := map[string]Mailbox{}
2430 for _, mb := range l {
2431 mailboxes[mb.Name] = mb
2434 if _, ok := mailboxes[mbsrc.Name]; !ok {
2435 return nil, false, true, false, fmt.Errorf("mailbox does not exist")
2438 uidval, err := a.NextUIDValidity(tx)
2440 return nil, false, false, false, fmt.Errorf("next uid validity: %v", err)
2443 // Ensure parent mailboxes for the destination paths exist.
2445 dstElems := strings.Split(dst, "/")
2446 for i, elem := range dstElems[:len(dstElems)-1] {
2452 mb, ok := mailboxes[parent]
2460 UIDValidity: uidval,
2464 if err := tx.Insert(&mb); err != nil {
2465 return nil, false, false, false, fmt.Errorf("creating parent mailbox %q: %v", mb.Name, err)
2467 if err := tx.Get(&Subscription{Name: parent}); err != nil {
2468 if err := tx.Insert(&Subscription{Name: parent}); err != nil {
2469 return nil, false, false, false, fmt.Errorf("creating subscription for %q: %v", parent, err)
2472 changes = append(changes, ChangeAddMailbox{Mailbox: mb, Flags: []string{`\Subscribed`}})
2475 // Process src mailboxes, renaming them to dst.
2476 for _, srcmb := range l {
2477 if srcmb.Name != mbsrc.Name && !strings.HasPrefix(srcmb.Name, srcPrefix) {
2480 srcName := srcmb.Name
2481 dstName := dst + srcmb.Name[len(mbsrc.Name):]
2482 if _, ok := mailboxes[dstName]; ok {
2483 return nil, false, false, true, fmt.Errorf("destination mailbox %q already exists", dstName)
2486 srcmb.Name = dstName
2487 srcmb.UIDValidity = uidval
2488 if err := tx.Update(&srcmb); err != nil {
2489 return nil, false, false, false, fmt.Errorf("renaming mailbox: %v", err)
2492 var dstFlags []string
2493 if tx.Get(&Subscription{Name: dstName}) == nil {
2494 dstFlags = []string{`\Subscribed`}
2496 changes = append(changes, ChangeRenameMailbox{MailboxID: srcmb.ID, OldName: srcName, NewName: dstName, Flags: dstFlags})
2499 // If we renamed e.g. a/b to a/b/c/d, and a/b/c to a/b/c/d/c, we'll have to recreate a/b and a/b/c.
2500 srcElems := strings.Split(mbsrc.Name, "/")
2502 for i := 0; i < len(dstElems) && strings.HasPrefix(dst, xsrc+"/"); i++ {
2504 UIDValidity: uidval,
2509 if err := tx.Insert(&mb); err != nil {
2510 return nil, false, false, false, fmt.Errorf("creating mailbox at old path %q: %v", mb.Name, err)
2512 xsrc += "/" + dstElems[len(srcElems)+i]
2514 return changes, false, false, false, nil
2517// MailboxDelete deletes a mailbox by ID. If it has children, the return value
2518// indicates that and an error is returned.
2520// Caller should broadcast the changes and remove files for the removed message IDs.
2521func (a *Account) MailboxDelete(ctx context.Context, log mlog.Log, tx *bstore.Tx, mailbox Mailbox) (changes []Change, removeMessageIDs []int64, hasChildren bool, rerr error) {
2522 // Look for existence of child mailboxes. There is a lot of text in the IMAP RFCs about
2523 // NoInferior and NoSelect. We just require only leaf mailboxes are deleted.
2524 qmb := bstore.QueryTx[Mailbox](tx)
2525 mbprefix := mailbox.Name + "/"
2526 qmb.FilterFn(func(mb Mailbox) bool {
2527 return strings.HasPrefix(mb.Name, mbprefix)
2529 if childExists, err := qmb.Exists(); err != nil {
2530 return nil, nil, false, fmt.Errorf("checking if mailbox has child: %v", err)
2531 } else if childExists {
2532 return nil, nil, true, fmt.Errorf("mailbox has a child, only leaf mailboxes can be deleted")
2535 // todo jmap: instead of completely deleting a mailbox and its messages, we need to mark them all as expunged.
2537 qm := bstore.QueryTx[Message](tx)
2538 qm.FilterNonzero(Message{MailboxID: mailbox.ID})
2539 remove, err := qm.List()
2541 return nil, nil, false, fmt.Errorf("listing messages to remove: %v", err)
2544 if len(remove) > 0 {
2545 removeIDs := make([]any, len(remove))
2546 for i, m := range remove {
2549 qmr := bstore.QueryTx[Recipient](tx)
2550 qmr.FilterEqual("MessageID", removeIDs...)
2551 if _, err = qmr.Delete(); err != nil {
2552 return nil, nil, false, fmt.Errorf("removing message recipients for messages: %v", err)
2555 qm = bstore.QueryTx[Message](tx)
2556 qm.FilterNonzero(Message{MailboxID: mailbox.ID})
2557 if _, err := qm.Delete(); err != nil {
2558 return nil, nil, false, fmt.Errorf("removing messages: %v", err)
2562 for _, m := range remove {
2564 removeMessageIDs = append(removeMessageIDs, m.ID)
2568 if err := a.AddMessageSize(log, tx, -totalSize); err != nil {
2569 return nil, nil, false, fmt.Errorf("updating disk usage: %v", err)
2572 // Mark messages as not needing training. Then retrain them, so they are untrained if they were.
2575 for _, m := range remove {
2578 remove[o].Junk = false
2579 remove[o].Notjunk = false
2584 if err := a.RetrainMessages(ctx, log, tx, remove, true); err != nil {
2585 return nil, nil, false, fmt.Errorf("untraining deleted messages: %v", err)
2589 if err := tx.Delete(&Mailbox{ID: mailbox.ID}); err != nil {
2590 return nil, nil, false, fmt.Errorf("removing mailbox: %v", err)
2592 return []Change{ChangeRemoveMailbox{MailboxID: mailbox.ID, Name: mailbox.Name}}, removeMessageIDs, false, nil
2595// CheckMailboxName checks if name is valid, returning an INBOX-normalized name.
2596// I.e. it changes various casings of INBOX and INBOX/* to Inbox and Inbox/*.
2597// Name is invalid if it contains leading/trailing/double slashes, or when it isn't
2598// unicode-normalized, or when empty or has special characters.
2600// If name is the inbox, and allowInbox is false, this is indicated with the isInbox return parameter.
2601// For that case, and for other invalid names, an error is returned.
2602func CheckMailboxName(name string, allowInbox bool) (normalizedName string, isInbox bool, rerr error) {
2603 first := strings.SplitN(name, "/", 2)[0]
2604 if strings.EqualFold(first, "inbox") {
2605 if len(name) == len("inbox") && !allowInbox {
2606 return "", true, fmt.Errorf("special mailbox name Inbox not allowed")
2608 name = "Inbox" + name[len("Inbox"):]
2611 if norm.NFC.String(name) != name {
2612 return "", false, errors.New("non-unicode-normalized mailbox names not allowed")
2616 return "", false, errors.New("empty mailbox name")
2618 if strings.HasPrefix(name, "/") || strings.HasSuffix(name, "/") || strings.Contains(name, "//") {
2619 return "", false, errors.New("bad slashes in mailbox name")
2622 // "%" and "*" are difficult to use with the IMAP LIST command, but we allow mostly
2624 if strings.HasPrefix(name, "#") {
2625 return "", false, errors.New("mailbox name cannot start with hash due to conflict with imap namespaces")
2628 // "#" and "&" are special in IMAP mailbox names. "#" for namespaces, "&" for
2631 for _, c := range name {
2633 if c <= 0x1f || c >= 0x7f && c <= 0x9f || c == 0x2028 || c == 0x2029 {
2634 return "", false, errors.New("control characters not allowed in mailbox name")
2637 return name, false, nil