Slightly improve debug logging for complex authentication pipelines

This commit is contained in:
fox.cpp 2025-02-01 19:24:53 +03:00
parent 1d044249c2
commit ef7fa210dc
No known key found for this signature in database
GPG key ID: 5B991F6215D2FCC0
5 changed files with 22 additions and 13 deletions

View file

@ -31,6 +31,11 @@ import (
// human-readable when values from multiple messages are lined up to each // human-readable when values from multiple messages are lined up to each
// other. // other.
type module interface {
Name() string
InstanceName() string
}
func marshalOrderedJSON(output *strings.Builder, m map[string]interface{}) error { func marshalOrderedJSON(output *strings.Builder, m map[string]interface{}) error {
order := make([]string, 0, len(m)) order := make([]string, 0, len(m))
for k := range m { for k := range m {
@ -62,6 +67,8 @@ func marshalOrderedJSON(output *strings.Builder, m map[string]interface{}) error
val = casted.FormatLog() val = casted.FormatLog()
case fmt.Stringer: case fmt.Stringer:
val = casted.String() val = casted.String()
case module:
val = casted.Name() + "/" + casted.InstanceName()
case error: case error:
val = casted.Error() val = casted.Error()
} }

View file

@ -105,12 +105,16 @@ func (s *SASLAuth) AuthPlain(username, password string) error {
var lastErr error var lastErr error
for _, p := range s.Plain { for _, p := range s.Plain {
username, err := s.usernameForAuth(context.TODO(), username) mappedUsername, err := s.usernameForAuth(context.TODO(), username)
if err != nil { if err != nil {
return err 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 { if lastErr == nil {
return nil return nil
} }
@ -139,12 +143,7 @@ func (s *SASLAuth) CreateSASL(mech string, remoteAddr net.Addr, successCb func(i
return ErrInvalidAuthCred return ErrInvalidAuthCred
} }
username, err := s.usernameForAuth(context.Background(), username) err := s.AuthPlain(username, password)
if err != nil {
return err
}
err = s.AuthPlain(username, password)
if err != nil { if err != nil {
s.Log.Error("authentication failed", err, "username", username, "src_ip", remoteAddr) s.Log.Error("authentication failed", err, "username", username, "src_ip", remoteAddr)
return ErrInvalidAuthCred return ErrInvalidAuthCred

View file

@ -79,6 +79,7 @@ func (endp *Endpoint) Init(cfg *config.Map) error {
endp.srv = dovecotsasl.NewServer() endp.srv = dovecotsasl.NewServer()
endp.srv.Log = stdlog.New(endp.log, "", 0) endp.srv.Log = stdlog.New(endp.log, "", 0)
endp.saslAuth.Log.Debug = endp.log.Debug
for _, mech := range endp.saslAuth.SASLMechanisms() { for _, mech := range endp.saslAuth.SASLMechanisms() {
endp.srv.AddMechanism(mech, mechInfo[mech], func(req *dovecotsasl.AuthReq) sasl.Server { endp.srv.AddMechanism(mech, mechInfo[mech], func(req *dovecotsasl.AuthReq) sasl.Server {

View file

@ -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)) addresses := make([]config.Endpoint, 0, len(endp.addrs))
for _, addr := range endp.addrs { for _, addr := range endp.addrs {
saddr, err := config.ParseEndpoint(addr) saddr, err := config.ParseEndpoint(addr)

View file

@ -110,15 +110,15 @@ func init() {
Value: filepath.Join(ConfigDirectory, "maddy.conf"), 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{ maddycli.AddSubcommand(&cli.Command{
Name: "run", Name: "run",
Usage: "Start the server", Usage: "Start the server",
Flags: []cli.Flag{ Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "enable debug logging early",
Destination: &log.DefaultLogger.Debug,
},
&cli.StringFlag{ &cli.StringFlag{
Name: "libexec", Name: "libexec",
Value: DefaultLibexecDirectory, Value: DefaultLibexecDirectory,