blob: 6454eb450b73d9f6c20650ee271c416ca05f7dbc [file] [log] [blame]
giolekva75ee2712021-11-26 13:57:12 +04001package main
2
3import (
4 "bufio"
5 "crypto/tls"
giolekvaee3a57b2021-12-01 16:18:23 +04006 "flag"
giolekva75ee2712021-11-26 13:57:12 +04007 "fmt"
8 "os"
9
10 "github.com/emersion/go-sasl"
11 "github.com/emersion/go-smtp"
12)
13
giolekvaee3a57b2021-12-01 16:18:23 +040014var config = flag.String("config", "/etc/maddy/config/smtp-servers.conf", "Path to the configuration file with downstream SMTP server addresses per line.")
15
16func readConfig(path string) ([]string, error) {
17 inp, err := os.Open(path)
18 if err != nil {
19 return nil, err
20 }
21 defer inp.Close()
22 lines := bufio.NewScanner(inp)
23 ret := make([]string, 0)
24 for lines.Scan() {
25 ret = append(ret, lines.Text())
26 }
27 if err := lines.Err(); err != nil {
28 return nil, err
29 }
30 return ret, nil
giolekva75ee2712021-11-26 13:57:12 +040031}
32
33func auth(server, username, password string) (bool, error) {
34 c, err := smtp.Dial(server)
35 if err != nil {
36 return false, err
37 }
38 if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
39 return false, err
40 }
41 if err := c.Auth(sasl.NewPlainClient(username, username, password)); err != nil {
42 return false, err
43 }
44 return true, nil
45}
46
47func main() {
giolekvaee3a57b2021-12-01 16:18:23 +040048 flag.Parse()
giolekva75ee2712021-11-26 13:57:12 +040049 inp := bufio.NewReader(os.Stdin)
50 username, err := inp.ReadString('\n')
51 if err != nil {
52 fmt.Fprintln(os.Stderr, "Could not read username")
53 os.Exit(2)
54 }
55 username = username[:len(username)-1]
56 password, err := inp.ReadString('\n')
57 if err != nil {
58 fmt.Fprintln(os.Stderr, "Could not read password")
59 os.Exit(2)
60 }
61 password = password[:len(password)-1]
giolekvaee3a57b2021-12-01 16:18:23 +040062 smtpServers, err := readConfig(*config)
63 if err != nil {
64 fmt.Println(err.Error())
65 os.Exit(2)
66 }
giolekva75ee2712021-11-26 13:57:12 +040067 for _, s := range smtpServers {
68 if ok, _ := auth(s, username, password); ok {
69 os.Exit(0)
70 // } else if err != nil {
71 // fmt.Println(os.Stderr, err.Error())
72 // os.Exit(2)
73 }
74 }
75 os.Exit(1)
76}