comparison cmd/sievemgr/info.go @ 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 4dff4c3f0fbb
children fc5e6970a0d5
comparison
equal deleted inserted replaced
17:2b799ca75d96 18:d14d93d011d7
20 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 21
22 package main 22 package main
23 23
24 import ( 24 import (
25 "crypto/tls"
25 "fmt" 26 "fmt"
26 "net" 27 "net"
28
29 "go.guido-berhoerster.org/managesieve"
27 ) 30 )
28 31
29 func init() { 32 func init() {
30 cmdInfo.Flag.StringVar(&acctName, "a", "", "Select the account") 33 cmdInfo.Flag.StringVar(&acctName, "a", "", "Select the account")
31 } 34 }
51 54
52 if err := readPassword(acct); err != nil { 55 if err := readPassword(acct); err != nil {
53 return err 56 return err
54 } 57 }
55 58
56 c, err := dialPlainAuth(acct) 59 // dialPlainAuth() is not used here so that SASL mechanisms can be
60 // determined before authentication
61 c, err := managesieve.Dial(net.JoinHostPort(acct.Host, acct.Port))
57 if err != nil { 62 if err != nil {
58 return err 63 return fmt.Errorf("failed to connect: %s", err)
64 }
65
66 // switch to a TLS connection except for localhost
67 if acct.Host != "localhost" && acct.Host != "127.0.0.1" &&
68 acct.Host != "::1" {
69 tlsConf := &tls.Config{
70 ServerName: acct.Host,
71 InsecureSkipVerify: acct.Insecure,
72 }
73 if err := c.StartTLS(tlsConf); err != nil {
74 return fmt.Errorf("failed to start TLS connection: %s",
75 err)
76 }
77 }
78 saslMechs := c.SASLMechanisms()
79
80 auth := managesieve.PlainAuth("", acct.User, acct.Password, acct.Host)
81 if err := c.Authenticate(auth); err != nil {
82 return fmt.Errorf("failed to authenticate user %s: %s",
83 acct.User, err)
59 } 84 }
60 defer c.Logout() 85 defer c.Logout()
61 86
62 fmt.Printf("%s (%s)\n", net.JoinHostPort(acct.Host, acct.Port), 87 fmt.Printf("%s (%s)\n", net.JoinHostPort(acct.Host, acct.Port),
63 c.Implementation()) 88 c.Implementation())
66 } 91 }
67 if c.SupportsTLS() { 92 if c.SupportsTLS() {
68 fmt.Println("STARTTLS supported") 93 fmt.Println("STARTTLS supported")
69 } 94 }
70 fmt.Printf("SASL mechanisms:\n") 95 fmt.Printf("SASL mechanisms:\n")
71 for _, m := range c.SASLMechanisms() { 96 for _, m := range saslMechs {
72 fmt.Printf(" %s\n", m) 97 fmt.Printf(" %s\n", m)
73 } 98 }
74 fmt.Printf("Extensions:\n") 99 fmt.Printf("Extensions:\n")
75 for _, e := range c.Extensions() { 100 for _, e := range c.Extensions() {
76 fmt.Printf(" %s\n", e) 101 fmt.Printf(" %s\n", e)