mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-26 19:56:43 +00:00
chore(GODT-2799): Separate account states for SMTP Backend
Rather than accessing the Bridge user list, each user register their individual SMTP service with the server manager. Note that some dependencies on the user data are hidden behind the `UserInterface`. These will be removed in a future patch.
This commit is contained in:
@ -40,3 +40,11 @@ func (bridge *Bridge) setUserAgent(name, version string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type bridgeUserAgentUpdater struct {
|
||||
*Bridge
|
||||
}
|
||||
|
||||
func (b *bridgeUserAgentUpdater) SetUserAgent(name, version string) {
|
||||
b.setUserAgent(name, version)
|
||||
}
|
||||
|
||||
@ -50,6 +50,16 @@ func (bridge *Bridge) addIMAPUser(ctx context.Context, user *user.User) error {
|
||||
return bridge.serverManager.AddIMAPUser(ctx, user)
|
||||
}
|
||||
|
||||
// addSMTPUser connects the given user to gluon.
|
||||
func (bridge *Bridge) addSMTPUser(ctx context.Context, user *user.User) error {
|
||||
return bridge.serverManager.AddSMTPAccount(ctx, user.GetSMTPService())
|
||||
}
|
||||
|
||||
// removeSMTPUser connects the given user to gluon.
|
||||
func (bridge *Bridge) removeSMTPUser(ctx context.Context, user *user.User) error {
|
||||
return bridge.serverManager.RemoveSMTPAccount(ctx, user.GetSMTPService())
|
||||
}
|
||||
|
||||
// removeIMAPUser disconnects the given user from gluon, optionally also removing its files.
|
||||
func (bridge *Bridge) removeIMAPUser(ctx context.Context, user *user.User, withData bool) error {
|
||||
return bridge.serverManager.RemoveIMAPUser(ctx, user, withData)
|
||||
|
||||
@ -28,6 +28,7 @@ import (
|
||||
"github.com/ProtonMail/gluon/logging"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/events"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/safe"
|
||||
bridgesmtp "github.com/ProtonMail/proton-bridge/v3/internal/services/smtp"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/user"
|
||||
"github.com/ProtonMail/proton-bridge/v3/pkg/cpc"
|
||||
"github.com/emersion/go-smtp"
|
||||
@ -43,13 +44,15 @@ type ServerManager struct {
|
||||
|
||||
smtpServer *smtp.Server
|
||||
smtpListener net.Listener
|
||||
smtpAccounts *bridgesmtp.Accounts
|
||||
|
||||
loadedUserCount int
|
||||
}
|
||||
|
||||
func newServerManager() *ServerManager {
|
||||
return &ServerManager{
|
||||
requests: cpc.NewCPC(),
|
||||
requests: cpc.NewCPC(),
|
||||
smtpAccounts: bridgesmtp.NewAccounts(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,7 +62,7 @@ func (sm *ServerManager) Init(bridge *Bridge) error {
|
||||
return err
|
||||
}
|
||||
|
||||
smtpServer := createSMTPServer(bridge)
|
||||
smtpServer := createSMTPServer(bridge, sm.smtpAccounts)
|
||||
|
||||
sm.imapServer = imapServer
|
||||
sm.smtpServer = smtpServer
|
||||
@ -134,6 +137,18 @@ func (sm *ServerManager) RemoveGluonUser(ctx context.Context, gluonID string) er
|
||||
return err
|
||||
}
|
||||
|
||||
func (sm *ServerManager) AddSMTPAccount(ctx context.Context, service *bridgesmtp.Service) error {
|
||||
_, err := sm.requests.Send(ctx, &smRequestAddSMTPAccount{account: service})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (sm *ServerManager) RemoveSMTPAccount(ctx context.Context, service *bridgesmtp.Service) error {
|
||||
_, err := sm.requests.Send(ctx, &smRequestRemoveSMTPAccount{account: service})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (sm *ServerManager) run(ctx context.Context, bridge *Bridge) {
|
||||
eventCh, cancel := bridge.GetEvents()
|
||||
defer cancel()
|
||||
@ -206,6 +221,16 @@ func (sm *ServerManager) run(ctx context.Context, bridge *Bridge) {
|
||||
case *smRequestRemoveGluonUser:
|
||||
err := sm.handleRemoveGluonUser(ctx, r.userID)
|
||||
request.Reply(ctx, nil, err)
|
||||
|
||||
case *smRequestAddSMTPAccount:
|
||||
logrus.WithField("user", r.account.UserID()).Debug("Adding SMTP Account")
|
||||
sm.smtpAccounts.AddAccount(r.account)
|
||||
request.Reply(ctx, nil, nil)
|
||||
|
||||
case *smRequestRemoveSMTPAccount:
|
||||
logrus.WithField("user", r.account.UserID()).Debug("Removing SMTP Account")
|
||||
sm.smtpAccounts.RemoveAccount(r.account)
|
||||
request.Reply(ctx, nil, nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -386,8 +411,8 @@ func createIMAPServer(bridge *Bridge) (*gluon.Server, error) {
|
||||
)
|
||||
}
|
||||
|
||||
func createSMTPServer(bridge *Bridge) *smtp.Server {
|
||||
return newSMTPServer(bridge, bridge.tlsConfig, bridge.logSMTP)
|
||||
func createSMTPServer(bridge *Bridge, accounts *bridgesmtp.Accounts) *smtp.Server {
|
||||
return newSMTPServer(bridge, accounts, bridge.tlsConfig, bridge.logSMTP)
|
||||
}
|
||||
|
||||
func (sm *ServerManager) closeSMTPServer(bridge *Bridge) error {
|
||||
@ -473,7 +498,7 @@ func (sm *ServerManager) restartSMTP(bridge *Bridge) error {
|
||||
|
||||
bridge.publish(events.SMTPServerStopped{})
|
||||
|
||||
sm.smtpServer = newSMTPServer(bridge, bridge.tlsConfig, bridge.logSMTP)
|
||||
sm.smtpServer = newSMTPServer(bridge, sm.smtpAccounts, bridge.tlsConfig, bridge.logSMTP)
|
||||
|
||||
if sm.shouldStartServers() {
|
||||
return sm.serveSMTP(bridge)
|
||||
@ -694,3 +719,11 @@ type smRequestAddGluonUser struct {
|
||||
type smRequestRemoveGluonUser struct {
|
||||
userID string
|
||||
}
|
||||
|
||||
type smRequestAddSMTPAccount struct {
|
||||
account *bridgesmtp.Service
|
||||
}
|
||||
|
||||
type smRequestRemoveSMTPAccount struct {
|
||||
account *bridgesmtp.Service
|
||||
}
|
||||
|
||||
@ -23,6 +23,7 @@ import (
|
||||
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/constants"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/logging"
|
||||
bridgesmtp "github.com/ProtonMail/proton-bridge/v3/internal/services/smtp"
|
||||
"github.com/emersion/go-sasl"
|
||||
"github.com/emersion/go-smtp"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -32,10 +33,10 @@ func (bridge *Bridge) restartSMTP(ctx context.Context) error {
|
||||
return bridge.serverManager.RestartSMTP(ctx)
|
||||
}
|
||||
|
||||
func newSMTPServer(bridge *Bridge, tlsConfig *tls.Config, logSMTP bool) *smtp.Server {
|
||||
func newSMTPServer(bridge *Bridge, accounts *bridgesmtp.Accounts, tlsConfig *tls.Config, logSMTP bool) *smtp.Server {
|
||||
logrus.WithField("logSMTP", logSMTP).Info("Creating SMTP server")
|
||||
|
||||
smtpServer := smtp.NewServer(&smtpBackend{Bridge: bridge})
|
||||
smtpServer := smtp.NewServer(&smtpBackend{bridge: bridge, accounts: accounts})
|
||||
|
||||
smtpServer.TLSConfig = tlsConfig
|
||||
smtpServer.Domain = constants.Host
|
||||
|
||||
@ -19,22 +19,25 @@ package bridge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/safe"
|
||||
smtpservice "github.com/ProtonMail/proton-bridge/v3/internal/services/smtp"
|
||||
"github.com/ProtonMail/proton-bridge/v3/internal/useragent"
|
||||
"github.com/emersion/go-smtp"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type smtpBackend struct {
|
||||
*Bridge
|
||||
accounts *smtpservice.Accounts
|
||||
userAgent UserAgentUpdater
|
||||
}
|
||||
|
||||
type smtpSession struct {
|
||||
*Bridge
|
||||
accounts *smtpservice.Accounts
|
||||
userAgent UserAgentUpdater
|
||||
|
||||
userID string
|
||||
authID string
|
||||
@ -44,44 +47,31 @@ type smtpSession struct {
|
||||
}
|
||||
|
||||
func (be *smtpBackend) NewSession(*smtp.Conn) (smtp.Session, error) {
|
||||
return &smtpSession{Bridge: be.Bridge}, nil
|
||||
return &smtpSession{accounts: be.accounts, userAgent: be.userAgent}, nil
|
||||
}
|
||||
|
||||
func (s *smtpSession) AuthPlain(username, password string) error {
|
||||
return safe.RLockRet(func() error {
|
||||
for _, user := range s.users {
|
||||
addrID, err := user.CheckAuth(username, []byte(password))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
s.userID = user.ID()
|
||||
s.authID = addrID
|
||||
|
||||
if strings.Contains(s.Bridge.GetCurrentUserAgent(), useragent.DefaultUserAgent) {
|
||||
s.Bridge.setUserAgent(useragent.UnknownClient, useragent.DefaultVersion)
|
||||
}
|
||||
|
||||
user.SendConfigStatusSuccess(context.Background())
|
||||
|
||||
return nil
|
||||
userID, authID, err := s.accounts.CheckAuth(username, []byte(password))
|
||||
if err != nil {
|
||||
if !errors.Is(err, smtpservice.ErrNoSuchUser) {
|
||||
return fmt.Errorf("unknown error")
|
||||
}
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"username": username,
|
||||
"pkg": "smtp",
|
||||
}).Error("Incorrect login credentials.")
|
||||
err := fmt.Errorf("invalid username or password")
|
||||
for _, user := range s.users {
|
||||
for _, mail := range user.Emails() {
|
||||
if mail == username {
|
||||
user.ReportConfigStatusFailure(err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}, s.usersLock)
|
||||
|
||||
return fmt.Errorf("invalid username or password")
|
||||
}
|
||||
|
||||
s.userID = userID
|
||||
s.authID = authID
|
||||
|
||||
if strings.Contains(s.userAgent.GetUserAgent(), useragent.DefaultUserAgent) {
|
||||
s.userAgent.SetUserAgent(useragent.UnknownClient, useragent.DefaultVersion)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *smtpSession) Reset() {
|
||||
@ -108,14 +98,7 @@ func (s *smtpSession) Rcpt(to string) error {
|
||||
}
|
||||
|
||||
func (s *smtpSession) Data(r io.Reader) error {
|
||||
err := safe.RLockRet(func() error {
|
||||
user, ok := s.users[s.userID]
|
||||
if !ok {
|
||||
return ErrNoSuchUser
|
||||
}
|
||||
|
||||
return user.SendMail(s.authID, s.from, s.to, r)
|
||||
}, s.usersLock)
|
||||
err := s.accounts.SendMail(context.Background(), s.userID, s.authID, s.from, s.to, r)
|
||||
|
||||
if err != nil {
|
||||
logrus.WithField("pkg", "smtp").WithError(err).Error("Send mail failed.")
|
||||
|
||||
@ -43,6 +43,11 @@ type Identifier interface {
|
||||
GetClientString() string
|
||||
}
|
||||
|
||||
type UserAgentUpdater interface {
|
||||
Identifier
|
||||
SetUserAgent(name, version string)
|
||||
}
|
||||
|
||||
type ProxyController interface {
|
||||
AllowProxy()
|
||||
DisallowProxy()
|
||||
|
||||
@ -545,6 +545,10 @@ func (bridge *Bridge) addUserWithVault(
|
||||
return fmt.Errorf("failed to add IMAP user: %w", err)
|
||||
}
|
||||
|
||||
if err := bridge.addSMTPUser(ctx, user); err != nil {
|
||||
return fmt.Errorf("failed to add SMTP user: %w", err)
|
||||
}
|
||||
|
||||
// Handle events coming from the user before forwarding them to the bridge.
|
||||
// For example, if the user's addresses change, we need to update them in gluon.
|
||||
bridge.tasks.Once(func(ctx context.Context) {
|
||||
@ -613,6 +617,10 @@ func (bridge *Bridge) logoutUser(ctx context.Context, user *user.User, withAPI,
|
||||
logrus.WithError(err).Error("Failed to remove IMAP user")
|
||||
}
|
||||
|
||||
if err := bridge.removeSMTPUser(ctx, user); err != nil {
|
||||
logrus.WithError(err).Error("Failed to remove SMTP user")
|
||||
}
|
||||
|
||||
if err := user.Logout(ctx, withAPI); err != nil {
|
||||
logrus.WithError(err).Error("Failed to logout user")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user