mirror of
https://github.com/neilalexander/yggmail.git
synced 2026-04-26 11:56:34 +03:00
Single user refactor
This commit is contained in:
@@ -32,15 +32,14 @@ func (b *Backend) Login(state *smtp.ConnectionState, username, password string)
|
||||
switch b.Mode {
|
||||
case BackendModeInternal:
|
||||
// If our username is email-like, then take just the localpart
|
||||
if localpart, host, err := utils.ParseAddress(username); err == nil {
|
||||
if host != base62.EncodeToString(b.Config.PublicKey) {
|
||||
if pk, err := utils.ParseAddress(username); err == nil {
|
||||
if !pk.Equal(b.Config.PublicKey) {
|
||||
return nil, fmt.Errorf("failed to authenticate: wrong domain in username")
|
||||
}
|
||||
username = localpart
|
||||
}
|
||||
|
||||
username = base62.EncodeToString(b.Config.PublicKey)
|
||||
// The connection came from our local listener
|
||||
if authed, err := b.Storage.TryAuthenticate(username, password); err != nil {
|
||||
if authed, err := b.Storage.ConfigTryPassword(password); err != nil {
|
||||
b.Log.Printf("Failed to authenticate SMTP user %q due to error: %s", username, err)
|
||||
return nil, fmt.Errorf("failed to authenticate: %w", err)
|
||||
} else if !authed {
|
||||
|
||||
@@ -21,12 +21,12 @@ type SessionLocal struct {
|
||||
}
|
||||
|
||||
func (s *SessionLocal) Mail(from string, opts smtp.MailOptions) error {
|
||||
_, host, err := utils.ParseAddress(from)
|
||||
pk, err := utils.ParseAddress(from)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parseAddress: %w", err)
|
||||
}
|
||||
|
||||
if host != base62.EncodeToString(s.backend.Config.PublicKey) {
|
||||
if !pk.Equal(s.backend.Config.PublicKey) {
|
||||
return fmt.Errorf("not allowed to send outgoing mail as %s", from)
|
||||
}
|
||||
|
||||
@@ -56,22 +56,23 @@ func (s *SessionLocal) Data(r io.Reader) error {
|
||||
servers := make(map[string]struct{})
|
||||
|
||||
for _, rcpt := range s.rcpt {
|
||||
localpart, host, err := utils.ParseAddress(rcpt)
|
||||
pk, err := utils.ParseAddress(rcpt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parseAddress: %w", err)
|
||||
}
|
||||
host := base62.EncodeToString(pk)
|
||||
|
||||
if _, ok := servers[host]; ok {
|
||||
continue
|
||||
}
|
||||
servers[host] = struct{}{}
|
||||
|
||||
if host == base62.EncodeToString(s.backend.Config.PublicKey) {
|
||||
if pk.Equal(s.backend.Config.PublicKey) {
|
||||
var b bytes.Buffer
|
||||
if err := m.WriteTo(&b); err != nil {
|
||||
return fmt.Errorf("m.WriteTo: %w", err)
|
||||
}
|
||||
if _, err := s.backend.Storage.MailCreate(localpart, "INBOX", b.Bytes()); err != nil {
|
||||
if _, err := s.backend.Storage.MailCreate("INBOX", b.Bytes()); err != nil {
|
||||
return fmt.Errorf("s.backend.Storage.StoreMessageFor: %w", err)
|
||||
}
|
||||
continue
|
||||
|
||||
@@ -23,7 +23,7 @@ type SessionRemote struct {
|
||||
}
|
||||
|
||||
func (s *SessionRemote) Mail(from string, opts smtp.MailOptions) error {
|
||||
_, host, err := utils.ParseAddress(from)
|
||||
pk, err := utils.ParseAddress(from)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mail.ParseAddress: %w", err)
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func (s *SessionRemote) Mail(from string, opts smtp.MailOptions) error {
|
||||
return fmt.Errorf("hex.DecodeString: %w", err)
|
||||
}
|
||||
|
||||
if remote := base62.EncodeToString(pks); host != remote {
|
||||
if remote := base62.EncodeToString(pks); base62.EncodeToString(pk) != remote {
|
||||
return fmt.Errorf("not allowed to send incoming mail as %s", from)
|
||||
}
|
||||
|
||||
@@ -42,16 +42,15 @@ func (s *SessionRemote) Mail(from string, opts smtp.MailOptions) error {
|
||||
}
|
||||
|
||||
func (s *SessionRemote) Rcpt(to string) error {
|
||||
user, host, err := utils.ParseAddress(to)
|
||||
pk, err := utils.ParseAddress(to)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mail.ParseAddress: %w", err)
|
||||
}
|
||||
|
||||
if local := base62.EncodeToString(s.backend.Config.PublicKey); host != local {
|
||||
return fmt.Errorf("not allowed to send mail to %q", host)
|
||||
if !pk.Equal(s.backend.Config.PublicKey) {
|
||||
return fmt.Errorf("unexpected recipient for wrong domain")
|
||||
}
|
||||
|
||||
s.localparts = append(s.localparts, user)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -73,12 +72,11 @@ func (s *SessionRemote) Data(r io.Reader) error {
|
||||
return fmt.Errorf("m.WriteTo: %w", err)
|
||||
}
|
||||
|
||||
for _, localpart := range s.localparts {
|
||||
if _, err := s.backend.Storage.MailCreate(localpart, "INBOX", b.Bytes()); err != nil {
|
||||
return fmt.Errorf("s.backend.Storage.StoreMessageFor: %w", err)
|
||||
}
|
||||
s.backend.Log.Printf("Stored new mail for local user %q from %s", localpart, s.from)
|
||||
if _, err := s.backend.Storage.MailCreate("INBOX", b.Bytes()); err != nil {
|
||||
return fmt.Errorf("s.backend.Storage.StoreMessageFor: %w", err)
|
||||
}
|
||||
s.backend.Log.Printf("Stored new mail from %s", s.from)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user