Skip to content

Commit

Permalink
Slightly improve debug logging for complex authentication pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
foxcpp committed Feb 1, 2025
1 parent 1d04424 commit ef7fa21
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
7 changes: 7 additions & 0 deletions framework/log/orderedjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import (
// human-readable when values from multiple messages are lined up to each
// other.

type module interface {
Name() string
InstanceName() string
}

func marshalOrderedJSON(output *strings.Builder, m map[string]interface{}) error {
order := make([]string, 0, len(m))
for k := range m {
Expand Down Expand Up @@ -62,6 +67,8 @@ func marshalOrderedJSON(output *strings.Builder, m map[string]interface{}) error
val = casted.FormatLog()
case fmt.Stringer:
val = casted.String()
case module:
val = casted.Name() + "/" + casted.InstanceName()
case error:
val = casted.Error()
}
Expand Down
15 changes: 7 additions & 8 deletions internal/auth/sasl.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,16 @@ func (s *SASLAuth) AuthPlain(username, password string) error {

var lastErr error
for _, p := range s.Plain {
username, err := s.usernameForAuth(context.TODO(), username)
mappedUsername, err := s.usernameForAuth(context.TODO(), username)
if err != nil {
return err
}

lastErr = p.AuthPlain(username, password)
s.Log.DebugMsg("attempting authentication",
"mapped_username", mappedUsername, "original_username", username,
"module", p)

lastErr = p.AuthPlain(mappedUsername, password)
if lastErr == nil {
return nil
}
Expand Down Expand Up @@ -139,12 +143,7 @@ func (s *SASLAuth) CreateSASL(mech string, remoteAddr net.Addr, successCb func(i
return ErrInvalidAuthCred
}

username, err := s.usernameForAuth(context.Background(), username)
if err != nil {
return err
}

err = s.AuthPlain(username, password)
err := s.AuthPlain(username, password)
if err != nil {
s.Log.Error("authentication failed", err, "username", username, "src_ip", remoteAddr)
return ErrInvalidAuthCred
Expand Down
1 change: 1 addition & 0 deletions internal/endpoint/dovecot_sasld/dovecot_sasl.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (endp *Endpoint) Init(cfg *config.Map) error {

endp.srv = dovecotsasl.NewServer()
endp.srv.Log = stdlog.New(endp.log, "", 0)
endp.saslAuth.Log.Debug = endp.log.Debug

for _, mech := range endp.saslAuth.SASLMechanisms() {
endp.srv.AddMechanism(mech, mechInfo[mech], func(req *dovecotsasl.AuthReq) sasl.Server {
Expand Down
2 changes: 2 additions & 0 deletions internal/endpoint/imap/imap.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ func (endp *Endpoint) Init(cfg *config.Map) error {
}
}

endp.saslAuth.Log.Debug = endp.Log.Debug

addresses := make([]config.Endpoint, 0, len(endp.addrs))
for _, addr := range endp.addrs {
saddr, err := config.ParseEndpoint(addr)
Expand Down
10 changes: 5 additions & 5 deletions maddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ func init() {
Value: filepath.Join(ConfigDirectory, "maddy.conf"),
},
)
maddycli.AddGlobalFlag(&cli.BoolFlag{
Name: "debug",
Usage: "enable debug logging early",
Destination: &log.DefaultLogger.Debug,
})
maddycli.AddSubcommand(&cli.Command{
Name: "run",
Usage: "Start the server",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "enable debug logging early",
Destination: &log.DefaultLogger.Debug,
},
&cli.StringFlag{
Name: "libexec",
Value: DefaultLibexecDirectory,
Expand Down

0 comments on commit ef7fa21

Please sign in to comment.