changeset 18:d14d93d011d7

Show SASL mechanisms from before authentication in info output
author Guido Berhoerster <guido+sievemgr@berhoerster.name>
date Tue, 02 Feb 2021 23:57:37 +0100
parents 2b799ca75d96
children 854167f55839
files cmd/sievemgr/info.go
diffstat 1 files changed, 28 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/cmd/sievemgr/info.go	Tue Feb 02 19:04:34 2021 +0100
+++ b/cmd/sievemgr/info.go	Tue Feb 02 23:57:37 2021 +0100
@@ -22,8 +22,11 @@
 package main
 
 import (
+	"crypto/tls"
 	"fmt"
 	"net"
+
+	"go.guido-berhoerster.org/managesieve"
 )
 
 func init() {
@@ -53,9 +56,31 @@
 		return err
 	}
 
-	c, err := dialPlainAuth(acct)
+	// dialPlainAuth() is not used here so that SASL mechanisms can be
+	// determined before authentication
+	c, err := managesieve.Dial(net.JoinHostPort(acct.Host, acct.Port))
 	if err != nil {
-		return err
+		return fmt.Errorf("failed to connect: %s", err)
+	}
+
+	// switch to a TLS connection except for localhost
+	if acct.Host != "localhost" && acct.Host != "127.0.0.1" &&
+		acct.Host != "::1" {
+		tlsConf := &tls.Config{
+			ServerName:         acct.Host,
+			InsecureSkipVerify: acct.Insecure,
+		}
+		if err := c.StartTLS(tlsConf); err != nil {
+			return fmt.Errorf("failed to start TLS connection: %s",
+				err)
+		}
+	}
+	saslMechs := c.SASLMechanisms()
+
+	auth := managesieve.PlainAuth("", acct.User, acct.Password, acct.Host)
+	if err := c.Authenticate(auth); err != nil {
+		return fmt.Errorf("failed to authenticate user %s: %s",
+			acct.User, err)
 	}
 	defer c.Logout()
 
@@ -68,7 +93,7 @@
 		fmt.Println("STARTTLS supported")
 	}
 	fmt.Printf("SASL mechanisms:\n")
-	for _, m := range c.SASLMechanisms() {
+	for _, m := range saslMechs {
 		fmt.Printf("  %s\n", m)
 	}
 	fmt.Printf("Extensions:\n")