Skip to content

Commit ef7fa21

Browse files
committed
Slightly improve debug logging for complex authentication pipelines
1 parent 1d04424 commit ef7fa21

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

framework/log/orderedjson.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ import (
3131
// human-readable when values from multiple messages are lined up to each
3232
// other.
3333

34+
type module interface {
35+
Name() string
36+
InstanceName() string
37+
}
38+
3439
func marshalOrderedJSON(output *strings.Builder, m map[string]interface{}) error {
3540
order := make([]string, 0, len(m))
3641
for k := range m {
@@ -62,6 +67,8 @@ func marshalOrderedJSON(output *strings.Builder, m map[string]interface{}) error
6267
val = casted.FormatLog()
6368
case fmt.Stringer:
6469
val = casted.String()
70+
case module:
71+
val = casted.Name() + "/" + casted.InstanceName()
6572
case error:
6673
val = casted.Error()
6774
}

internal/auth/sasl.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,16 @@ func (s *SASLAuth) AuthPlain(username, password string) error {
105105

106106
var lastErr error
107107
for _, p := range s.Plain {
108-
username, err := s.usernameForAuth(context.TODO(), username)
108+
mappedUsername, err := s.usernameForAuth(context.TODO(), username)
109109
if err != nil {
110110
return err
111111
}
112112

113-
lastErr = p.AuthPlain(username, password)
113+
s.Log.DebugMsg("attempting authentication",
114+
"mapped_username", mappedUsername, "original_username", username,
115+
"module", p)
116+
117+
lastErr = p.AuthPlain(mappedUsername, password)
114118
if lastErr == nil {
115119
return nil
116120
}
@@ -139,12 +143,7 @@ func (s *SASLAuth) CreateSASL(mech string, remoteAddr net.Addr, successCb func(i
139143
return ErrInvalidAuthCred
140144
}
141145

142-
username, err := s.usernameForAuth(context.Background(), username)
143-
if err != nil {
144-
return err
145-
}
146-
147-
err = s.AuthPlain(username, password)
146+
err := s.AuthPlain(username, password)
148147
if err != nil {
149148
s.Log.Error("authentication failed", err, "username", username, "src_ip", remoteAddr)
150149
return ErrInvalidAuthCred

internal/endpoint/dovecot_sasld/dovecot_sasl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (endp *Endpoint) Init(cfg *config.Map) error {
7979

8080
endp.srv = dovecotsasl.NewServer()
8181
endp.srv.Log = stdlog.New(endp.log, "", 0)
82+
endp.saslAuth.Log.Debug = endp.log.Debug
8283

8384
for _, mech := range endp.saslAuth.SASLMechanisms() {
8485
endp.srv.AddMechanism(mech, mechInfo[mech], func(req *dovecotsasl.AuthReq) sasl.Server {

internal/endpoint/imap/imap.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ func (endp *Endpoint) Init(cfg *config.Map) error {
112112
}
113113
}
114114

115+
endp.saslAuth.Log.Debug = endp.Log.Debug
116+
115117
addresses := make([]config.Endpoint, 0, len(endp.addrs))
116118
for _, addr := range endp.addrs {
117119
saddr, err := config.ParseEndpoint(addr)

maddy.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ func init() {
110110
Value: filepath.Join(ConfigDirectory, "maddy.conf"),
111111
},
112112
)
113+
maddycli.AddGlobalFlag(&cli.BoolFlag{
114+
Name: "debug",
115+
Usage: "enable debug logging early",
116+
Destination: &log.DefaultLogger.Debug,
117+
})
113118
maddycli.AddSubcommand(&cli.Command{
114119
Name: "run",
115120
Usage: "Start the server",
116121
Flags: []cli.Flag{
117-
&cli.BoolFlag{
118-
Name: "debug",
119-
Usage: "enable debug logging early",
120-
Destination: &log.DefaultLogger.Debug,
121-
},
122122
&cli.StringFlag{
123123
Name: "libexec",
124124
Value: DefaultLibexecDirectory,

0 commit comments

Comments
 (0)