Skip to content

Commit c70634b

Browse files
fix: smtp login
1 parent a64d2ea commit c70634b

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

internal/app/app.go

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ func New(
117117

118118
var ErrorLogin = fmt.Errorf("login invalid")
119119

120+
func (a App) AuthSMTPAnonymous() bool {
121+
return a.config.AuthSMTP.Anonymous
122+
}
123+
120124
// AuthHTTPAnonymous checks if anonymous access is allowed.
121125
func (a App) AuthHTTPAnonymous() bool {
122126
return a.config.AuthHTTP.Anonymous

internal/core/app.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,32 @@ type App interface {
2323
AttachmentOrphanDelete(ctx context.Context, tracer trace.Tracer) error
2424
AuthHTTPAnonymous() bool
2525
AuthHTTPLogin(ctx context.Context, username, password string) (models.User, error)
26+
AuthSMTPAnonymous() bool
2627
AuthSMTPLogin(ctx context.Context, username, password string) error
2728
EndpointList(ctx context.Context) ([]models.Endpoint, error)
2829
EndpointTest(ctx context.Context, id int64) error
30+
EnvelopeCount(ctx context.Context) (int, error)
2931
EnvelopeCreate(ctx context.Context, msg models.DTOMessageCreate, datts []models.DTOAttachmentCreate) (int64, error)
3032
EnvelopeDelete(ctx context.Context, id int64) error
3133
EnvelopeDrop(ctx context.Context) error
3234
EnvelopeGet(ctx context.Context, id int64) (models.Envelope, error)
3335
EnvelopeList(ctx context.Context, page pagination.Page, req models.DTOEnvelopeListRequest) (models.DTOEnvelopeListResult, error)
3436
EnvelopeSend(ctx context.Context, envelopeID int64, endpointID int64) error
35-
EnvelopeCount(ctx context.Context) (int, error)
37+
MailmanDequeue(ctx context.Context) (*models.Envelope, error)
38+
MailmanEnqueue(ctx context.Context, envelopeID int64) error
3639
MessageHTMLGet(ctx context.Context, id int64) (string, error)
3740
RetentionPolicyGet(ctx context.Context) models.ConfigRetentionPolicy
3841
RetentionPolicyRun(ctx context.Context, trace trace.Tracer) error
3942
RuleCreate(ctx context.Context, req models.DTORuleCreate) (int64, error)
4043
RuleDelete(ctx context.Context, id int64) error
44+
RuleEndpointsGet(ctx context.Context, id int64) (models.RuleEndpoints, error)
4145
RuleEndpointsList(ctx context.Context) ([]models.RuleEndpoints, error)
4246
RuleExpressionCheck(ctx context.Context, expression string) error
4347
RuleGet(ctx context.Context, id int64) (models.Rule, error)
44-
RuleEndpointsGet(ctx context.Context, id int64) (models.RuleEndpoints, error)
4548
RuleList(ctx context.Context) ([]models.Rule, error)
4649
RuleUpdate(ctx context.Context, req models.DTORuleUpdate) error
4750
StorageGet(ctx context.Context) (models.Storage, error)
4851
TraceDrop(ctx context.Context) error
4952
TraceList(ctx context.Context, page pagination.Page, req models.DTOTraceListRequest) (models.DTOTraceListResult, error)
5053
Tracer(source string) trace.Tracer
51-
MailmanEnqueue(ctx context.Context, envelopeID int64) error
52-
MailmanDequeue(ctx context.Context) (*models.Envelope, error)
5354
}

smtp/backend.go

+32-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package smtp
33
import (
44
"bytes"
55
"context"
6+
"fmt"
67
"io"
78
"net/mail"
89

@@ -34,8 +35,11 @@ func (b *Backend) NewSession(state *smtp.Conn) (smtp.Session, error) {
3435
tracer := b.app.Tracer(trace.SourceSMTP).Sticky(trace.WithAddress(address))
3536
log := b.log.With().Str("address", address).Logger()
3637

38+
// log.Debug().Msg("NewSession")
39+
3740
return &session{
3841
app: b.app,
42+
auth: b.app.AuthSMTPAnonymous(),
3943
log: log,
4044
tracer: tracer,
4145
address: address,
@@ -44,6 +48,7 @@ func (b *Backend) NewSession(state *smtp.Conn) (smtp.Session, error) {
4448

4549
// A Session is returned after EHLO.
4650
type session struct {
51+
auth bool
4752
app core.App
4853
log zerolog.Logger
4954
tracer trace.Tracer
@@ -53,22 +58,44 @@ type session struct {
5358
}
5459

5560
func (s *session) AuthPlain(username, password string) error {
56-
return s.app.AuthSMTPLogin(context.Background(), username, password)
61+
// s.log.Debug().Str("username", username).Str("password", password).Msg("AuthPlain")
62+
err := s.app.AuthSMTPLogin(context.Background(), username, password)
63+
if err != nil {
64+
return smtp.ErrAuthFailed
65+
}
66+
67+
s.auth = true
68+
fmt.Println("1.1", s.auth)
69+
70+
return nil
5771
}
5872

5973
func (s *session) Mail(from string, opts *smtp.MailOptions) error {
74+
// s.log.Debug().Str("from", from).Msg("Mail")
75+
if !s.auth {
76+
return smtp.ErrAuthRequired
77+
}
6078
// log.Println("Mail from:", from)
6179
s.from = from
6280
return nil
6381
}
6482

6583
func (s *session) Rcpt(to string, opts *smtp.RcptOptions) error {
84+
// s.log.Debug().Str("to", to).Msg("Rcpt")
85+
if !s.auth {
86+
return smtp.ErrAuthRequired
87+
}
6688
// log.Println("Rcpt to:", to)
6789
s.to = to
6890
return nil
6991
}
7092

7193
func (s *session) Data(r io.Reader) error {
94+
// s.log.Debug().Msg("Data")
95+
if !s.auth {
96+
return smtp.ErrAuthRequired
97+
}
98+
7299
e, err := enmime.ReadEnvelope(r)
73100
if err != nil {
74101
log.Error().Err(err).Msg("Failed to read envelope")
@@ -127,8 +154,11 @@ func (s *session) Data(r io.Reader) error {
127154
return nil
128155
}
129156

130-
func (s *session) Reset() {}
157+
func (s *session) Reset() {
158+
// s.log.Debug().Msg("Reset")
159+
}
131160

132161
func (s *session) Logout() error {
162+
// s.log.Debug().Msg("Logout")
133163
return nil
134164
}

smtp/utils.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func enableMechLogin(be smtp.Backend, s *smtp.Server) {
1010
// Adapted from https://github.com/emersion/go-smtp/issues/41#issuecomment-493601465
1111
s.EnableAuth(sasl.Login, func(conn *smtp.Conn) sasl.Server {
1212
return sasl.NewLoginServer(func(username, password string) error {
13-
session, err := be.NewSession(conn)
14-
if err != nil {
15-
return err
13+
sess := conn.Session()
14+
if sess == nil {
15+
panic("No session when AUTH is called")
1616
}
1717

18-
return session.AuthPlain(username, password)
18+
return sess.AuthPlain(username, password)
1919
})
2020
})
2121
}

0 commit comments

Comments
 (0)