Mercurial > projects > sievemgr
diff cmd/sievemgr/common.go @ 5:4dff4c3f0fbb
Introduce configuration file where account information is specified
Introduce a configuration file where account information must be specified
instead of passing it with each invocation on the command line. Each account
has a name by which it can be selected and one may be specified as the default
account. This is intended to improve usability for productive usage.
Enforce strict permissions since passwords may be specified for non-interactive
usage.
Remove command-line flags for passing account information.
author | Guido Berhoerster <guido+sievemgr@berhoerster.name> |
---|---|
date | Tue, 03 Nov 2020 23:44:45 +0100 |
parents | 0cd5a454dfb4 |
children | 29769b9e2f09 |
line wrap: on
line diff
--- a/cmd/sievemgr/common.go Tue Oct 27 19:17:56 2020 +0100 +++ b/cmd/sievemgr/common.go Tue Nov 03 23:44:45 2020 +0100 @@ -22,7 +22,6 @@ package main import ( - "bufio" "crypto/tls" "errors" "fmt" @@ -30,44 +29,36 @@ "io/ioutil" "net" "os" - "os/user" "runtime" "strings" "go.guido-berhoerster.org/managesieve" + "go.guido-berhoerster.org/sievemgr/cmd/sievemgr/internal/config" "golang.org/x/crypto/ssh/terminal" ) var errTooBig = errors.New("too big") -func parseHostPort(s string) (string, string, error) { - var host string - host, port, err := net.SplitHostPort(s) - if err != nil { - // error may be due to a missing port but there is no usable - // error value to test for, thus try again with a port added - var tmpErr error - host, _, tmpErr = net.SplitHostPort(s + ":4190") - if tmpErr != nil { - return "", "", err +func getAccount(conf *config.Configuration, name string) (*config.Account, error) { + if name == "" { + if conf.Default == nil { + return nil, fmt.Errorf("no default account configured") + } + return conf.Default, nil + } + for _, acct := range conf.Accounts { + if acct.Name == name { + return acct, nil } } - if port == "" { - // no port given, try to look up a SRV record for given domain - // and fall back to the domain and port 4190 - services, err := managesieve.LookupService(host) - if err != nil { - return "", "", err - } - host, port, err = net.SplitHostPort(services[0]) - if err != nil { - return "", "", err - } - } - return host, port, nil + return nil, fmt.Errorf("account %q does not exist", name) } -func readPassword() (string, error) { +func readPassword(acct *config.Account) error { + if acct.Password != "" { + return nil + } + var tty *os.File var fd int var w io.Writer @@ -78,7 +69,7 @@ var err error tty, err = os.OpenFile("/dev/tty", os.O_RDWR, 0666) if err != nil { - return "", err + return err } defer tty.Close() fd = int(tty.Fd()) @@ -89,74 +80,47 @@ rawPassword, err := terminal.ReadPassword(fd) io.WriteString(w, "\n") if err != nil { - return "", fmt.Errorf("failed to read password: %s", err) + return fmt.Errorf("failed to read password: %s", err) } password := string(rawPassword) if password == "" { - return "", fmt.Errorf("invalid password") - } - return password, nil -} - -func readPasswordFile(filename string) (string, error) { - f, err := os.Open(filename) - if err != nil { - return "", err + return fmt.Errorf("invalid password") } - defer f.Close() - scanner := bufio.NewScanner(f) - if !scanner.Scan() { - err := scanner.Err() - if err == nil { - err = fmt.Errorf("failed to read from %q: unexpected EOF", - filename) - } - return "", err - } - password := scanner.Text() - if password == "" { - return "", fmt.Errorf("invalid password") - } - return password, nil + acct.Password = password + return nil } -func usernamePassword(host, port, username, passwordFile string) (string, string, error) { - // fall back to the system username - if username == "" { - u, err := user.Current() - if err != nil { - return "", "", - fmt.Errorf("failed to obtain username: %s", err) - } - username = u.Username +func lookupHostPort(acct *config.Account) error { + if acct.Port != "" { + return nil } - - var password string - var err error - if passwordFile != "" { - password, err = readPasswordFile(passwordFilename) - } else { - password, err = readPassword() + // no port given, try to look up a SRV record for given domain + // and fall back to the domain and port 4190 + services, err := managesieve.LookupService(acct.Host) + if err != nil { + return fmt.Errorf("failed to look up service record: %s", err) } + host, port, err := net.SplitHostPort(services[0]) if err != nil { - return "", "", err + return fmt.Errorf("failed to parse service record: %s", err) } - - return username, password, nil + acct.Host = host + acct.Port = port + return nil } -func dialPlainAuth(hostport, username, password string) (*managesieve.Client, error) { - c, err := managesieve.Dial(hostport) +func dialPlainAuth(acct *config.Account) (*managesieve.Client, error) { + c, err := managesieve.Dial(net.JoinHostPort(acct.Host, acct.Port)) if err != nil { return nil, fmt.Errorf("failed to connect: %s", err) } - host, _, _ := net.SplitHostPort(hostport) // switch to a TLS connection except for localhost - if host != "localhost" && host != "127.0.0.1" && host != "::1" { + if acct.Host != "localhost" && acct.Host != "127.0.0.1" && + acct.Host != "::1" { tlsConf := &tls.Config{ - ServerName: host, - InsecureSkipVerify: skipCertVerify, + ServerName: acct.Host, + InsecureSkipVerify: acct.Insecure, } if err := c.StartTLS(tlsConf); err != nil { return nil, @@ -165,10 +129,10 @@ } } - auth := managesieve.PlainAuth("", username, password, host) + auth := managesieve.PlainAuth("", acct.User, acct.Password, acct.Host) if err := c.Authenticate(auth); err != nil { return nil, fmt.Errorf("failed to authenticate user %s: %s", - username, err) + acct.User, err) } return c, nil