| 1 |
package imap |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/tls" |
| 5 |
"fmt" |
| 6 |
"net" |
| 7 |
"os" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
|
| 11 |
"git.sr.ht/~rjarry/aerc/lib" |
| 12 |
"git.sr.ht/~rjarry/aerc/lib/auth" |
| 13 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 14 |
"github.com/emersion/go-imap" |
| 15 |
"github.com/emersion/go-imap/client" |
| 16 |
) |
| 17 |
|
| 18 |
// connect establishes a new tcp connection to the imap server, logs in and |
| 19 |
// selects the default inbox. If no error is returned, the imap client will be |
| 20 |
// in the imap.SelectedState. |
| 21 |
func (w *IMAPWorker) connect() (*client.Client, error) { |
| 22 |
var ( |
| 23 |
conn *net.TCPConn |
| 24 |
err error |
| 25 |
c *client.Client |
| 26 |
) |
| 27 |
|
| 28 |
protocol, mech, err := auth.ParseScheme(w.config.url) |
| 29 |
if err != nil { |
| 30 |
return nil, err |
| 31 |
} |
| 32 |
|
| 33 |
addr := w.config.url.Host |
| 34 |
if !strings.Contains(addr, ":") { |
| 35 |
addr += ":" + protocol |
| 36 |
} |
| 37 |
|
| 38 |
conn, err = newTCPConn(addr, w.config.connection_timeout) |
| 39 |
if conn == nil || err != nil { |
| 40 |
return nil, err |
| 41 |
} |
| 42 |
|
| 43 |
if w.config.connection_timeout > 0 { |
| 44 |
end := time.Now().Add(w.config.connection_timeout) |
| 45 |
err = conn.SetDeadline(end) |
| 46 |
if err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if w.config.keepalive_period > 0 { |
| 52 |
err = w.setKeepaliveParameters(conn) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
serverName, _, _ := net.SplitHostPort(addr) |
| 59 |
tlsConfig := &tls.Config{ServerName: serverName} |
| 60 |
|
| 61 |
protocol, insecure := strings.CutSuffix(protocol, "+insecure") |
| 62 |
|
| 63 |
switch protocol { |
| 64 |
case "imap": |
| 65 |
c, err = client.New(conn) |
| 66 |
if err != nil { |
| 67 |
return nil, err |
| 68 |
} |
| 69 |
if !insecure { |
| 70 |
if err = c.StartTLS(tlsConfig); err != nil { |
| 71 |
return nil, err |
| 72 |
} |
| 73 |
} |
| 74 |
case "imaps": |
| 75 |
if insecure { |
| 76 |
tlsConfig.InsecureSkipVerify = true |
| 77 |
} |
| 78 |
tlsConn := tls.Client(conn, tlsConfig) |
| 79 |
c, err = client.New(tlsConn) |
| 80 |
if err != nil { |
| 81 |
return nil, err |
| 82 |
} |
| 83 |
default: |
| 84 |
return nil, fmt.Errorf("Unknown IMAP scheme %s", protocol) |
| 85 |
} |
| 86 |
|
| 87 |
c.ErrorLog = log.ErrorLogger() |
| 88 |
|
| 89 |
if w.config.url.User != nil && mech == "" { |
| 90 |
if plain, err := c.SupportAuth("PLAIN"); err != nil { |
| 91 |
return nil, err |
| 92 |
} else if plain { |
| 93 |
mech = "plain" |
| 94 |
} else if nologin, err := c.SupportAuth("LOGINDISABLED"); err != nil { |
| 95 |
return nil, err |
| 96 |
} else if !nologin { |
| 97 |
mech = "login" |
| 98 |
} else { |
| 99 |
return nil, fmt.Errorf("No auth method supported") |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
switch mech { |
| 104 |
case "login": |
| 105 |
if w.config.url.User != nil { |
| 106 |
username := w.config.url.User.Username() |
| 107 |
password, _ := w.config.url.User.Password() |
| 108 |
|
| 109 |
if err := c.Login(username, password); err != nil { |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
} |
| 113 |
default: |
| 114 |
saslClient, err := auth.NewSaslClient(mech, w.config.url, w.config.name) |
| 115 |
if err != nil { |
| 116 |
return nil, err |
| 117 |
} |
| 118 |
if saslClient != nil { |
| 119 |
if ok, err := c.SupportAuth(strings.ToUpper(mech)); err != nil { |
| 120 |
return nil, err |
| 121 |
} else if !ok { |
| 122 |
return nil, fmt.Errorf("%s auth not supported", mech) |
| 123 |
} |
| 124 |
|
| 125 |
err = c.Authenticate(saslClient) |
| 126 |
if err != nil { |
| 127 |
return nil, err |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if _, err := c.Select(imap.InboxName, false); err != nil { |
| 133 |
return nil, err |
| 134 |
} |
| 135 |
|
| 136 |
info := make(chan *imap.MailboxInfo, 1) |
| 137 |
if err := c.List("", "", info); err != nil { |
| 138 |
return nil, fmt.Errorf("failed to retrieve delimiter: %w", err) |
| 139 |
} |
| 140 |
if mailboxinfo := <-info; mailboxinfo != nil { |
| 141 |
w.delimiter = mailboxinfo.Delimiter |
| 142 |
} |
| 143 |
if w.delimiter == "" { |
| 144 |
// just in case some implementation does not follow standards |
| 145 |
w.delimiter = "/" |
| 146 |
} |
| 147 |
if len(w.config.debugLogPath) > 0 { |
| 148 |
logName := fmt.Sprintf("%s/imap_debug_%s.log", |
| 149 |
w.config.debugLogPath, |
| 150 |
w.config.name, |
| 151 |
) |
| 152 |
logFile, err := os.OpenFile(logName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644) |
| 153 |
if err != nil { |
| 154 |
w.worker.Tracef("Not logging IMAP debug logs: %s", err) |
| 155 |
} else { |
| 156 |
c.SetDebug(logFile) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return c, nil |
| 161 |
} |
| 162 |
|
| 163 |
// newTCPConn establishes a new tcp connection. Timeout will ensure that the |
| 164 |
// function does not hang when there is no connection. If there is a timeout, |
| 165 |
// but a valid connection is eventually returned, ensure that it is properly |
| 166 |
// closed. |
| 167 |
func newTCPConn(addr string, timeout time.Duration) (*net.TCPConn, error) { |
| 168 |
errTCPTimeout := fmt.Errorf("tcp connection timeout") |
| 169 |
|
| 170 |
type tcpConn struct { |
| 171 |
conn *net.TCPConn |
| 172 |
err error |
| 173 |
} |
| 174 |
|
| 175 |
done := make(chan tcpConn) |
| 176 |
go func() { |
| 177 |
defer log.PanicHandler() |
| 178 |
|
| 179 |
newConn, err := net.Dial("tcp", addr) |
| 180 |
if err != nil { |
| 181 |
done <- tcpConn{nil, err} |
| 182 |
return |
| 183 |
} |
| 184 |
done <- tcpConn{newConn.(*net.TCPConn), nil} |
| 185 |
}() |
| 186 |
|
| 187 |
select { |
| 188 |
case <-time.After(timeout): |
| 189 |
go func() { |
| 190 |
defer log.PanicHandler() |
| 191 |
if tcpResult := <-done; tcpResult.conn != nil { |
| 192 |
tcpResult.conn.Close() |
| 193 |
} |
| 194 |
}() |
| 195 |
return nil, errTCPTimeout |
| 196 |
case tcpResult := <-done: |
| 197 |
if tcpResult.conn == nil || tcpResult.err != nil { |
| 198 |
return nil, tcpResult.err |
| 199 |
} |
| 200 |
return tcpResult.conn, nil |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
// Set additional keepalive parameters. |
| 205 |
// Uses new interfaces introduced in Go1.11, which let us get connection's file |
| 206 |
// descriptor, without blocking, and therefore without uncontrolled spawning of |
| 207 |
// threads (not goroutines, actual threads). |
| 208 |
func (w *IMAPWorker) setKeepaliveParameters(conn *net.TCPConn) error { |
| 209 |
err := conn.SetKeepAlive(true) |
| 210 |
if err != nil { |
| 211 |
return err |
| 212 |
} |
| 213 |
// Idle time before sending a keepalive probe |
| 214 |
err = conn.SetKeepAlivePeriod(w.config.keepalive_period) |
| 215 |
if err != nil { |
| 216 |
return err |
| 217 |
} |
| 218 |
rawConn, e := conn.SyscallConn() |
| 219 |
if e != nil { |
| 220 |
return e |
| 221 |
} |
| 222 |
err = rawConn.Control(func(fdPtr uintptr) { |
| 223 |
fd := int(fdPtr) |
| 224 |
// Max number of probes before failure |
| 225 |
err := lib.SetTcpKeepaliveProbes(fd, w.config.keepalive_probes) |
| 226 |
if err != nil { |
| 227 |
w.worker.Errorf("cannot set tcp keepalive probes: %v", err) |
| 228 |
} |
| 229 |
// Wait time after an unsuccessful probe |
| 230 |
err = lib.SetTcpKeepaliveInterval(fd, w.config.keepalive_interval) |
| 231 |
if err != nil { |
| 232 |
w.worker.Errorf("cannot set tcp keepalive interval: %v", err) |
| 233 |
} |
| 234 |
}) |
| 235 |
return err |
| 236 |
} |