aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-01-16 11:43:38 +0800
committerRunxi Yu <me@runxiyu.org>2025-01-16 11:43:38 +0800
commitb408645806e8098b89e782a09e8ba7ef36c6fb6f (patch)
treeccc245e196a4df6ae8dce325cede4705656637d3
parentIMAP: Use clog.Debug instead of fmt.Printf (diff)
downloadmaild-b408645806e8098b89e782a09e8ba7ef36c6fb6f.tar.gz
maild-b408645806e8098b89e782a09e8ba7ef36c6fb6f.tar.zst
maild-b408645806e8098b89e782a09e8ba7ef36c6fb6f.zip
IMAP: Use TLS
-rw-r--r--config.go5
-rw-r--r--maild.scfg3
-rw-r--r--serve_imap.go27
3 files changed, 28 insertions, 7 deletions
diff --git a/config.go b/config.go
index 92bec3e..b805dbc 100644
--- a/config.go
+++ b/config.go
@@ -26,8 +26,9 @@ var config struct {
Addr string `scfg:"addr"`
} `scfg:"mx"`
IMAP struct {
- Net string `scfg:"net"`
- Addr string `scfg:"addr"`
+ Net string `scfg:"net"`
+ Addr string `scfg:"addr"`
+ Trans string `scfg:"trans"`
} `scfg:"imap"`
_tls_config *tls.Config
}
diff --git a/maild.scfg b/maild.scfg
index 8e909e9..646a5b3 100644
--- a/maild.scfg
+++ b/maild.scfg
@@ -17,6 +17,7 @@ mx {
imap {
net tcp
+ trans tls
addr :1993
- # TODO: Allow two listeners, one for TCP, one for TLS
+ # TODO: Add multiple listeners
}
diff --git a/serve_imap.go b/serve_imap.go
index 8b2c859..c55ed3e 100644
--- a/serve_imap.go
+++ b/serve_imap.go
@@ -3,9 +3,9 @@ package main
import (
"bufio"
"context"
- "fmt"
"crypto/tls"
"encoding/base64"
+ "fmt"
"io"
"net"
"strings"
@@ -62,7 +62,7 @@ func (session *imap_recv_session) handle(ctx context.Context) error {
break switch_cmd
}
argument := make([]byte, base64.StdEncoding.DecodedLen(len(argument_base64)))
- _, err := base64.StdEncoding.Decode(argument, []byte(argument_base64));
+ _, err := base64.StdEncoding.Decode(argument, []byte(argument_base64))
if err != nil {
_, _ = session.buf_conn.WriteString(tag + " TODO cannot decode base64\r\n")
_ = session.buf_conn.Flush()
@@ -86,12 +86,31 @@ func imap_new_session(ctx context.Context, net_conn net.Conn) error {
}
func serve_imap() {
- var imap_net, imap_addr string
+ var imap_net, imap_addr, imap_trans string
+ var tls_config *tls.Config
config_consistent_run(func() {
imap_net = config.IMAP.Net
imap_addr = config.IMAP.Addr
+ imap_trans = config.IMAP.Trans
+ tls_config = config._tls_config
})
- listener, err := net.Listen(imap_net, imap_addr)
+ var listener net.Listener
+ var err error
+ switch imap_trans {
+ case "tls", "":
+ listener, err = tls.Listen(imap_net, imap_addr, tls_config)
+ if err != nil {
+ clog.Fatal(1, "IMAP: Cannot listen TLS: "+err.Error())
+ }
+ case "plain":
+ listener, err = net.Listen(imap_net, imap_addr)
+ if err != nil {
+ clog.Fatal(1, "IMAP: Cannot listen plain: "+err.Error())
+ }
+ default:
+ clog.Fatal(1, "IMAP: Invalid transport for listening")
+ }
+
if err != nil {
clog.Fatal(1, "IMAP: Cannot listen: "+err.Error())
}