package imap import ( "crypto/tls" "fmt" "net" "os" "strings" "time" "git.sr.ht/~rjarry/aerc/lib" "git.sr.ht/~rjarry/aerc/lib/auth" "git.sr.ht/~rjarry/aerc/lib/log" "github.com/emersion/go-imap" "github.com/emersion/go-imap/client" ) // connect establishes a new tcp connection to the imap server, logs in and // selects the default inbox. If no error is returned, the imap client will be // in the imap.SelectedState. func (w *IMAPWorker) connect() (*client.Client, error) { var ( conn *net.TCPConn err error c *client.Client ) protocol, mech, err := auth.ParseScheme(w.config.url) if err != nil { return nil, err } addr := w.config.url.Host if !strings.Contains(addr, ":") { addr += ":" + protocol } conn, err = newTCPConn(addr, w.config.connection_timeout) if conn == nil || err != nil { return nil, err } if w.config.connection_timeout > 0 { end := time.Now().Add(w.config.connection_timeout) err = conn.SetDeadline(end) if err != nil { return nil, err } } if w.config.keepalive_period > 0 { err = w.setKeepaliveParameters(conn) if err != nil { return nil, err } } serverName, _, _ := net.SplitHostPort(addr) tlsConfig := &tls.Config{ServerName: serverName} protocol, insecure := strings.CutSuffix(protocol, "+insecure") switch protocol { case "imap": c, err = client.New(conn) if err != nil { return nil, err } if !insecure { if err = c.StartTLS(tlsConfig); err != nil { return nil, err } } case "imaps": if insecure { tlsConfig.InsecureSkipVerify = true } tlsConn := tls.Client(conn, tlsConfig) c, err = client.New(tlsConn) if err != nil { return nil, err } default: return nil, fmt.Errorf("Unknown IMAP scheme %s", protocol) } c.ErrorLog = log.ErrorLogger() if w.config.url.User != nil && mech == "" { if plain, err := c.SupportAuth("PLAIN"); err != nil { return nil, err } else if plain { mech = "plain" } else if nologin, err := c.SupportAuth("LOGINDISABLED"); err != nil { return nil, err } else if !nologin { mech = "login" } else { return nil, fmt.Errorf("No auth method supported") } } switch mech { case "login": if w.config.url.User != nil { username := w.config.url.User.Username() password, _ := w.config.url.User.Password() if err := c.Login(username, password); err != nil { return nil, err } } default: saslClient, err := auth.NewSaslClient(mech, w.config.url, w.config.name) if err != nil { return nil, err } if saslClient != nil { if ok, err := c.SupportAuth(strings.ToUpper(mech)); err != nil { return nil, err } else if !ok { return nil, fmt.Errorf("%s auth not supported", mech) } err = c.Authenticate(saslClient) if err != nil { return nil, err } } } if _, err := c.Select(imap.InboxName, false); err != nil { return nil, err } info := make(chan *imap.MailboxInfo, 1) if err := c.List("", "", info); err != nil { return nil, fmt.Errorf("failed to retrieve delimiter: %w", err) } if mailboxinfo := <-info; mailboxinfo != nil { w.delimiter = mailboxinfo.Delimiter } if w.delimiter == "" { // just in case some implementation does not follow standards w.delimiter = "/" } if len(w.config.debugLogPath) > 0 { logName := fmt.Sprintf("%s/imap_debug_%s.log", w.config.debugLogPath, w.config.name, ) logFile, err := os.OpenFile(logName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o644) if err != nil { w.worker.Tracef("Not logging IMAP debug logs: %s", err) } else { c.SetDebug(logFile) } } return c, nil } // newTCPConn establishes a new tcp connection. Timeout will ensure that the // function does not hang when there is no connection. If there is a timeout, // but a valid connection is eventually returned, ensure that it is properly // closed. func newTCPConn(addr string, timeout time.Duration) (*net.TCPConn, error) { errTCPTimeout := fmt.Errorf("tcp connection timeout") type tcpConn struct { conn *net.TCPConn err error } done := make(chan tcpConn) go func() { defer log.PanicHandler() newConn, err := net.Dial("tcp", addr) if err != nil { done <- tcpConn{nil, err} return } done <- tcpConn{newConn.(*net.TCPConn), nil} }() select { case <-time.After(timeout): go func() { defer log.PanicHandler() if tcpResult := <-done; tcpResult.conn != nil { tcpResult.conn.Close() } }() return nil, errTCPTimeout case tcpResult := <-done: if tcpResult.conn == nil || tcpResult.err != nil { return nil, tcpResult.err } return tcpResult.conn, nil } } // Set additional keepalive parameters. // Uses new interfaces introduced in Go1.11, which let us get connection's file // descriptor, without blocking, and therefore without uncontrolled spawning of // threads (not goroutines, actual threads). func (w *IMAPWorker) setKeepaliveParameters(conn *net.TCPConn) error { err := conn.SetKeepAlive(true) if err != nil { return err } // Idle time before sending a keepalive probe err = conn.SetKeepAlivePeriod(w.config.keepalive_period) if err != nil { return err } rawConn, e := conn.SyscallConn() if e != nil { return e } err = rawConn.Control(func(fdPtr uintptr) { fd := int(fdPtr) // Max number of probes before failure err := lib.SetTcpKeepaliveProbes(fd, w.config.keepalive_probes) if err != nil { w.worker.Errorf("cannot set tcp keepalive probes: %v", err) } // Wait time after an unsuccessful probe err = lib.SetTcpKeepaliveInterval(fd, w.config.keepalive_interval) if err != nil { w.worker.Errorf("cannot set tcp keepalive interval: %v", err) } }) return err }