Skip to content

Commit 73c6d9f

Browse files
committed
Add smtp start_tls implementation
1 parent d636001 commit 73c6d9f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tls/datadog_checks/tls/tls_remote.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,24 @@ def _switch_starttls(self, sock):
155155
# Read Mysql welcome message
156156
data = self._read_n_bytes_from_socket(sock, bytes_to_read)
157157
sock.sendall(packet)
158+
elif protocol == "smtp":
159+
self.log.debug('Switching connection to encrypted for %s protocol', protocol)
160+
161+
# read & check server hello
162+
initial_banner = sock.recv(4096)
163+
if not initial_banner.startswith(b'220'):
164+
raise Exception('SMTP server did not greet correctly')
165+
166+
# send client hello
167+
sock.sendall(f'EHLO {self.agent_check._server_hostname}\r\n'.encode('ascii'))
168+
# drain EHLO response
169+
sock.recv(4096)
170+
171+
# upgrade connection
172+
sock.sendall(b'STARTTLS\r\n')
173+
data = sock.recv(1024)
174+
if not data.startswith(b'220'):
175+
raise Exception('SMTP endpoint does not support STARTTLS')
158176
else:
159177
raise Exception('Unsupported starttls protocol: ' + protocol)
160178

0 commit comments

Comments
 (0)