2021-07-18 23:03:28 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021 Neil Alexander
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-07 18:15:07 +01:00
|
|
|
package smtpserver
|
|
|
|
|
|
|
|
|
|
import (
|
2026-02-14 02:34:09 +05:00
|
|
|
"crypto/ed25519"
|
2021-07-07 18:15:07 +01:00
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
|
|
|
|
|
"github.com/emersion/go-smtp"
|
2026-02-14 02:34:09 +05:00
|
|
|
"github.com/neilalexander/yggmail/internal/account"
|
|
|
|
|
"github.com/neilalexander/yggmail/internal/remote"
|
|
|
|
|
"github.com/neilalexander/yggmail/internal/storage"
|
2021-07-08 23:12:20 +01:00
|
|
|
"github.com/neilalexander/yggmail/internal/utils"
|
2026-02-14 02:34:09 +05:00
|
|
|
"github.com/neilalexander/yggmail/internal/utils/e2ee"
|
2021-07-07 18:15:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SessionLocal struct {
|
|
|
|
|
backend *Backend
|
2026-02-14 02:34:09 +05:00
|
|
|
storage storage.Storage
|
2021-07-07 18:15:07 +01:00
|
|
|
state *smtp.ConnectionState
|
2026-02-14 02:34:09 +05:00
|
|
|
session *account.Session
|
2021-07-07 18:15:07 +01:00
|
|
|
from string
|
|
|
|
|
rcpt []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SessionLocal) Mail(from string, opts smtp.MailOptions) error {
|
2021-07-09 19:54:15 +01:00
|
|
|
s.rcpt = s.rcpt[:0]
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
_, aPk, err := utils.ParseAddress(from)
|
2021-07-07 18:15:07 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("parseAddress: %w", err)
|
|
|
|
|
}
|
2026-02-14 02:34:09 +05:00
|
|
|
if !aPk.Equal(s.session.Publickey()) {
|
|
|
|
|
return fmt.Errorf("not allowed to send outgoing mail as account: %s", utils.EncodeString(aPk))
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.from = from
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SessionLocal) Rcpt(to string) error {
|
|
|
|
|
s.rcpt = append(s.rcpt, to)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *SessionLocal) Data(r io.Reader) error {
|
2026-02-14 02:34:09 +05:00
|
|
|
payload, size, err := remote.ReadRawData(io.NopCloser(r))
|
2021-07-07 18:15:07 +01:00
|
|
|
if err != nil {
|
2026-02-14 02:34:09 +05:00
|
|
|
return err
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
var rcpt []ed25519.PublicKey
|
|
|
|
|
for _, r := range s.rcpt {
|
|
|
|
|
_, aPk, err := utils.ParseAddress(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.backend.Log.Println("Data: Parse address: cant parse: ", r)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
rcpt = append(rcpt, aPk)
|
|
|
|
|
}
|
|
|
|
|
payload, err = e2ee.EncryptFilter(payload.(io.ReadSeekCloser), s.from, rcpt, s.session.Sign, s.session.Encrypt)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.backend.Log.Printf("\nSMTP DATA: error encrypt: %w", err)
|
|
|
|
|
return err
|
2025-12-02 23:02:22 +00:00
|
|
|
}
|
2021-07-07 18:15:07 +01:00
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
payload, size, err = remote.ReadRawData(payload)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
if err := s.backend.Queues.QueueFor(s.storage, s.session.Username(), s.from, s.rcpt, payload, size); err != nil {
|
2021-07-09 23:43:09 +01:00
|
|
|
return fmt.Errorf("s.backend.Queues.QueueFor: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
s.backend.Log.Println("SMTP L: Queued mail for", s.rcpt)
|
2021-07-09 23:43:09 +01:00
|
|
|
|
2021-07-07 18:15:07 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-09 19:57:56 +01:00
|
|
|
func (s *SessionLocal) Reset() {
|
|
|
|
|
s.rcpt = s.rcpt[:0]
|
|
|
|
|
s.from = ""
|
|
|
|
|
}
|
2021-07-07 18:15:07 +01:00
|
|
|
|
|
|
|
|
func (s *SessionLocal) Logout() error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|