git » chasquid » commit 72477c0

smtpsrv: Get TLS information for direct connections

author Alberto Bertogli
2017-09-10 10:20:57 UTC
committer Alberto Bertogli
2017-09-10 10:20:57 UTC
parent 0972964722c3d525e116609f32fdaded4a9f41b5

smtpsrv: Get TLS information for direct connections

For direct TLS connections, such as submission-over-TLS, we currently
don't get the TLS information so it appears in the headers as "plain
text", which is misleading.

This patch fixes the problem by getting the connection information
early. Note it explicitly triggers the handshake, which would otherwise
happen transparently on the first read/write, so we can use the hostname
(if any) in our hello message.

internal/smtpsrv/conn.go +11 -0

diff --git a/internal/smtpsrv/conn.go b/internal/smtpsrv/conn.go
index c347cea..864e56b 100644
--- a/internal/smtpsrv/conn.go
+++ b/internal/smtpsrv/conn.go
@@ -158,6 +158,17 @@ func (c *Conn) Handle() {
 	defer c.tr.Finish()
 	c.tr.Debugf("Connected, mode: %s", c.mode)
 
+	if tc, ok := c.conn.(*tls.Conn); ok {
+		// For TLS connections, complete the handshake and get the state, so
+		// it can be used when we say hello below.
+		tc.Handshake()
+		cstate := tc.ConnectionState()
+		c.tlsConnState = &cstate
+		if name := c.tlsConnState.ServerName; name != "" {
+			c.hostname = name
+		}
+	}
+
 	c.tc.PrintfLine("220 %s ESMTP chasquid", c.hostname)
 
 	var cmd, params string