Commit d1a30c93 authored by R David Murray's avatar R David Murray

#8739: upgrade smtpd to RFC 5321 and 1870.

smtpd now handles EHLO and has infrastructure for extended smtp command mode.
The SIZE extension is also implemented.  In order to support parameters on
MAIL FROM, the RFC 5322 parser from the email package is used to parse the
address "token".

Logging subclasses things and overrides __init__, so it was necessary to
update those __init__ functions in the logging tests to make the logging tests
pass.

The original suggestion and patch were by Alberto Trevino.  Juhana Jauhiainen
added the --size argument and SIZE parameter support.  Michele Orrù improved
the patch and added more tests.  Dan Boswell conditionalized various bits of
code on whether or not we are in HELO or EHLO mode, as well as some other
improvements and tests.  I finalized the patch and added the address parsing.
parent 032eed3c
...@@ -20,17 +20,24 @@ specific mail-sending strategies. ...@@ -20,17 +20,24 @@ specific mail-sending strategies.
Additionally the SMTPChannel may be extended to implement very specific Additionally the SMTPChannel may be extended to implement very specific
interaction behaviour with SMTP clients. interaction behaviour with SMTP clients.
The code supports :RFC:`5321`, plus the :rfc:`1870` SIZE extension.
SMTPServer Objects SMTPServer Objects
------------------ ------------------
.. class:: SMTPServer(localaddr, remoteaddr) .. class:: SMTPServer(localaddr, remoteaddr, data_size_limit=33554432)
Create a new :class:`SMTPServer` object, which binds to local address Create a new :class:`SMTPServer` object, which binds to local address
*localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. It *localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. It
inherits from :class:`asyncore.dispatcher`, and so will insert itself into inherits from :class:`asyncore.dispatcher`, and so will insert itself into
:mod:`asyncore`'s event loop on instantiation. :mod:`asyncore`'s event loop on instantiation.
*data_size_limit* specifies the maximum number of bytes that will be
accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no
limit.
.. method:: process_message(peer, mailfrom, rcpttos, data) .. method:: process_message(peer, mailfrom, rcpttos, data)
Raise :exc:`NotImplementedError` exception. Override this in subclasses to Raise :exc:`NotImplementedError` exception. Override this in subclasses to
...@@ -155,11 +162,15 @@ SMTPChannel Objects ...@@ -155,11 +162,15 @@ SMTPChannel Objects
Command Action taken Command Action taken
======== =================================================================== ======== ===================================================================
HELO Accepts the greeting from the client and stores it in HELO Accepts the greeting from the client and stores it in
:attr:`seen_greeting`. :attr:`seen_greeting`. Sets server to base command mode.
EHLO Accepts the greeting from the client and stores it in
:attr:`seen_greeting`. Sets server to extended command mode.
NOOP Takes no action. NOOP Takes no action.
QUIT Closes the connection cleanly. QUIT Closes the connection cleanly.
MAIL Accepts the "MAIL FROM:" syntax and stores the supplied address as MAIL Accepts the "MAIL FROM:" syntax and stores the supplied address as
:attr:`mailfrom`. :attr:`mailfrom`. In extended command mode, accepts the
:rfc:`1870` SIZE attribute and responds appropriately based on the
value of ``data_size_limit``.
RCPT Accepts the "RCPT TO:" syntax and stores the supplied addresses in RCPT Accepts the "RCPT TO:" syntax and stores the supplied addresses in
the :attr:`rcpttos` list. the :attr:`rcpttos` list.
RSET Resets the :attr:`mailfrom`, :attr:`rcpttos`, and RSET Resets the :attr:`mailfrom`, :attr:`rcpttos`, and
...@@ -167,4 +178,7 @@ SMTPChannel Objects ...@@ -167,4 +178,7 @@ SMTPChannel Objects
DATA Sets the internal state to :attr:`DATA` and stores remaining lines DATA Sets the internal state to :attr:`DATA` and stores remaining lines
from the client in :attr:`received_data` until the terminator from the client in :attr:`received_data` until the terminator
"\r\n.\r\n" is received. "\r\n.\r\n" is received.
HELP Returns minimal information on command syntax
VRFY Returns code 252 (the server doesn't know if the address is valid)
EXPN Reports that the command is not implemented.
======== =================================================================== ======== ===================================================================
This diff is collapsed.
...@@ -663,6 +663,7 @@ if threading: ...@@ -663,6 +663,7 @@ if threading:
self.smtp_server = server self.smtp_server = server
self.conn = conn self.conn = conn
self.addr = addr self.addr = addr
self.data_size_limit = None
self.received_lines = [] self.received_lines = []
self.smtp_state = self.COMMAND self.smtp_state = self.COMMAND
self.seen_greeting = '' self.seen_greeting = ''
...@@ -682,6 +683,7 @@ if threading: ...@@ -682,6 +683,7 @@ if threading:
return return
self.push('220 %s %s' % (self.fqdn, smtpd.__version__)) self.push('220 %s %s' % (self.fqdn, smtpd.__version__))
self.set_terminator(b'\r\n') self.set_terminator(b'\r\n')
self.extended_smtp = False
class TestSMTPServer(smtpd.SMTPServer): class TestSMTPServer(smtpd.SMTPServer):
...@@ -709,6 +711,7 @@ if threading: ...@@ -709,6 +711,7 @@ if threading:
def __init__(self, addr, handler, poll_interval, sockmap): def __init__(self, addr, handler, poll_interval, sockmap):
self._localaddr = addr self._localaddr = addr
self._remoteaddr = None self._remoteaddr = None
self.data_size_limit = None
self.sockmap = sockmap self.sockmap = sockmap
asyncore.dispatcher.__init__(self, map=sockmap) asyncore.dispatcher.__init__(self, map=sockmap)
try: try:
......
This diff is collapsed.
...@@ -229,13 +229,13 @@ class DebuggingServerTests(unittest.TestCase): ...@@ -229,13 +229,13 @@ class DebuggingServerTests(unittest.TestCase):
def testNOOP(self): def testNOOP(self):
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
expected = (250, b'Ok') expected = (250, b'OK')
self.assertEqual(smtp.noop(), expected) self.assertEqual(smtp.noop(), expected)
smtp.quit() smtp.quit()
def testRSET(self): def testRSET(self):
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
expected = (250, b'Ok') expected = (250, b'OK')
self.assertEqual(smtp.rset(), expected) self.assertEqual(smtp.rset(), expected)
smtp.quit() smtp.quit()
...@@ -246,10 +246,18 @@ class DebuggingServerTests(unittest.TestCase): ...@@ -246,10 +246,18 @@ class DebuggingServerTests(unittest.TestCase):
self.assertEqual(smtp.ehlo(), expected) self.assertEqual(smtp.ehlo(), expected)
smtp.quit() smtp.quit()
def testNotImplemented(self):
# EXPN isn't implemented in DebuggingServer
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
expected = (502, b'EXPN not implemented')
smtp.putcmd('EXPN')
self.assertEqual(smtp.getreply(), expected)
smtp.quit()
def testVRFY(self): def testVRFY(self):
# VRFY isn't implemented in DebuggingServer
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
expected = (502, b'Error: command "VRFY" not implemented') expected = (252, b'Cannot VRFY user, but will accept message ' + \
b'and attempt delivery')
self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected) self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected)
self.assertEqual(smtp.verify('nobody@nowhere.com'), expected) self.assertEqual(smtp.verify('nobody@nowhere.com'), expected)
smtp.quit() smtp.quit()
...@@ -265,7 +273,8 @@ class DebuggingServerTests(unittest.TestCase): ...@@ -265,7 +273,8 @@ class DebuggingServerTests(unittest.TestCase):
def testHELP(self): def testHELP(self):
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
self.assertEqual(smtp.help(), b'Error: command "HELP" not implemented') self.assertEqual(smtp.help(), b'Supported commands: EHLO HELO MAIL ' + \
b'RCPT DATA RSET NOOP QUIT VRFY')
smtp.quit() smtp.quit()
def testSend(self): def testSend(self):
......
...@@ -112,6 +112,7 @@ Gregory Bond ...@@ -112,6 +112,7 @@ Gregory Bond
Matias Bordese Matias Bordese
Jurjen Bos Jurjen Bos
Peter Bosch Peter Bosch
Dan Boswell
Eric Bouck Eric Bouck
Thierry Bousch Thierry Bousch
Sebastian Boving Sebastian Boving
...@@ -494,6 +495,7 @@ Geert Jansen ...@@ -494,6 +495,7 @@ Geert Jansen
Jack Jansen Jack Jansen
Bill Janssen Bill Janssen
Thomas Jarosch Thomas Jarosch
Juhana Jauhiainen
Zbigniew Jędrzejewski-Szmek Zbigniew Jędrzejewski-Szmek
Julien Jehannet Julien Jehannet
Drew Jenkins Drew Jenkins
...@@ -1039,6 +1041,7 @@ Sandro Tosi ...@@ -1039,6 +1041,7 @@ Sandro Tosi
Richard Townsend Richard Townsend
David Townshend David Townshend
Laurence Tratt Laurence Tratt
Alberto Trevino
Matthias Troffaes Matthias Troffaes
John Tromp John Tromp
Jason Trowbridge Jason Trowbridge
......
...@@ -46,6 +46,9 @@ Core and Builtins ...@@ -46,6 +46,9 @@ Core and Builtins
Library Library
------- -------
- Issue #8739: Updated smtpd to support RFC 5321, and added support for the
RFC 1870 SIZE extension.
- Issue #665194: Added a localtime function to email.utils to provide an - Issue #665194: Added a localtime function to email.utils to provide an
aware local datetime for use in setting Date headers. aware local datetime for use in setting Date headers.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment