projects/sievemgr

changeset 18:d14d93d011d7

Show SASL mechanisms from before authentication in info output
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue Feb 02 23:57:37 2021 +0100 (7 months ago)
parents 2b799ca75d96
children 854167f55839
files cmd/sievemgr/info.go
line diff
     1.1 --- a/cmd/sievemgr/info.go	Tue Feb 02 19:04:34 2021 +0100
     1.2 +++ b/cmd/sievemgr/info.go	Tue Feb 02 23:57:37 2021 +0100
     1.3 @@ -22,8 +22,11 @@
     1.4  package main
     1.5  
     1.6  import (
     1.7 +	"crypto/tls"
     1.8  	"fmt"
     1.9  	"net"
    1.10 +
    1.11 +	"go.guido-berhoerster.org/managesieve"
    1.12  )
    1.13  
    1.14  func init() {
    1.15 @@ -53,9 +56,31 @@
    1.16  		return err
    1.17  	}
    1.18  
    1.19 -	c, err := dialPlainAuth(acct)
    1.20 +	// dialPlainAuth() is not used here so that SASL mechanisms can be
    1.21 +	// determined before authentication
    1.22 +	c, err := managesieve.Dial(net.JoinHostPort(acct.Host, acct.Port))
    1.23  	if err != nil {
    1.24 -		return err
    1.25 +		return fmt.Errorf("failed to connect: %s", err)
    1.26 +	}
    1.27 +
    1.28 +	// switch to a TLS connection except for localhost
    1.29 +	if acct.Host != "localhost" && acct.Host != "127.0.0.1" &&
    1.30 +		acct.Host != "::1" {
    1.31 +		tlsConf := &tls.Config{
    1.32 +			ServerName:         acct.Host,
    1.33 +			InsecureSkipVerify: acct.Insecure,
    1.34 +		}
    1.35 +		if err := c.StartTLS(tlsConf); err != nil {
    1.36 +			return fmt.Errorf("failed to start TLS connection: %s",
    1.37 +				err)
    1.38 +		}
    1.39 +	}
    1.40 +	saslMechs := c.SASLMechanisms()
    1.41 +
    1.42 +	auth := managesieve.PlainAuth("", acct.User, acct.Password, acct.Host)
    1.43 +	if err := c.Authenticate(auth); err != nil {
    1.44 +		return fmt.Errorf("failed to authenticate user %s: %s",
    1.45 +			acct.User, err)
    1.46  	}
    1.47  	defer c.Logout()
    1.48  
    1.49 @@ -68,7 +93,7 @@
    1.50  		fmt.Println("STARTTLS supported")
    1.51  	}
    1.52  	fmt.Printf("SASL mechanisms:\n")
    1.53 -	for _, m := range c.SASLMechanisms() {
    1.54 +	for _, m := range saslMechs {
    1.55  		fmt.Printf("  %s\n", m)
    1.56  	}
    1.57  	fmt.Printf("Extensions:\n")