cpython: 241c33b6cb95 (original) (raw)
--- a/Lib/smtpd.py +++ b/Lib/smtpd.py @@ -374,6 +374,10 @@ class SMTPChannel(asynchat.async_chat): return address def smtp_MAIL(self, arg):
if not self.seen_greeting:[](#l1.7)
self.push('503 Error: send HELO first');[](#l1.8)
return[](#l1.9)
+ print('===> MAIL', arg, file=DEBUGSTREAM) address = self.__getaddr('FROM:', arg) if arg else None if not address: @@ -387,6 +391,10 @@ class SMTPChannel(asynchat.async_chat): self.push('250 Ok') def smtp_RCPT(self, arg):
if not self.seen_greeting:[](#l1.18)
self.push('503 Error: send HELO first');[](#l1.19)
return[](#l1.20)
+ print('===> RCPT', arg, file=DEBUGSTREAM) if not self.mailfrom: self.push('503 Error: need MAIL command') @@ -411,6 +419,10 @@ class SMTPChannel(asynchat.async_chat): self.push('250 Ok') def smtp_DATA(self, arg):
if not self.seen_greeting:[](#l1.29)
self.push('503 Error: send HELO first');[](#l1.30)
return[](#l1.31)
+ if not self.rcpttos: self.push('503 Error: need RCPT command') return
--- a/Lib/test/test_smtpd.py +++ b/Lib/test/test_smtpd.py @@ -39,6 +39,7 @@ class SMTPDServerTest(TestCase): channel.socket.queue_recv(line) channel.handle_read()
write_line(b'HELO test.example')[](#l2.7) write_line(b'MAIL From:eggs@example')[](#l2.8) write_line(b'RCPT To:spam@example')[](#l2.9) write_line(b'DATA')[](#l2.10)
@@ -104,6 +105,11 @@ class SMTPDChannelTest(TestCase): self.write_line(b'NOOP') self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')
- def test_HELO_NOOP(self):
self.write_line(b'HELO example')[](#l2.16)
self.write_line(b'NOOP')[](#l2.17)
self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')[](#l2.18)
+ def test_NOOP_bad_syntax(self): self.write_line(b'NOOP hi') self.assertEqual(self.channel.socket.last, @@ -113,17 +119,23 @@ class SMTPDChannelTest(TestCase): self.write_line(b'QUIT') self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')
- def test_HELO_QUIT(self):
self.write_line(b'HELO example')[](#l2.28)
self.write_line(b'QUIT')[](#l2.29)
self.assertEqual(self.channel.socket.last, b'221 Bye\r\n')[](#l2.30)
+ def test_QUIT_arg_ignored(self): self.write_line(b'QUIT bye bye') self.assertEqual(self.channel.socket.last, b'221 Bye\r\n') def test_bad_state(self): self.channel.smtp_state = 'BAD STATE'
self.write_line(b'HELO')[](#l2.38)
self.write_line(b'HELO example')[](#l2.39) self.assertEqual(self.channel.socket.last,[](#l2.40) b'451 Internal confusion\r\n')[](#l2.41)
def test_command_too_long(self):
self.write_line(b'HELO example')[](#l2.44) self.write_line(b'MAIL from ' +[](#l2.45) b'a' * self.channel.command_size_limit +[](#l2.46) b'@example')[](#l2.47)
@@ -133,6 +145,7 @@ class SMTPDChannelTest(TestCase): def test_data_too_long(self): # Small hack. Setting limit to 2K octets here will save us some time. self.channel.data_size_limit = 2048
self.write_line(b'HELO example')[](#l2.52) self.write_line(b'MAIL From:eggs@example')[](#l2.53) self.write_line(b'RCPT To:spam@example')[](#l2.54) self.write_line(b'DATA')[](#l2.55)
@@ -142,43 +155,61 @@ class SMTPDChannelTest(TestCase): b'552 Error: Too much mail data\r\n') def test_need_MAIL(self):
self.write_line(b'HELO example')[](#l2.60) self.write_line(b'RCPT to:spam@example')[](#l2.61) self.assertEqual(self.channel.socket.last,[](#l2.62) b'503 Error: need MAIL command\r\n')[](#l2.63)
self.write_line(b'HELO example')[](#l2.66) self.write_line(b'MAIL from eggs@example')[](#l2.67) self.assertEqual(self.channel.socket.last,[](#l2.68) b'501 Syntax: MAIL FROM:<address>\r\n')[](#l2.69)
def test_MAIL_missing_from(self):
self.write_line(b'HELO example')[](#l2.72) self.write_line(b'MAIL from:')[](#l2.73) self.assertEqual(self.channel.socket.last,[](#l2.74) b'501 Syntax: MAIL FROM:<address>\r\n')[](#l2.75)
self.write_line(b'HELO example')[](#l2.78) self.write_line(b'MAIL from:<eggs@example>')[](#l2.79) self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')[](#l2.80)
self.write_line(b'HELO example')[](#l2.83) self.write_line(b'MAIL from:eggs@example')[](#l2.84) self.write_line(b'MAIL from:spam@example')[](#l2.85) self.assertEqual(self.channel.socket.last,[](#l2.86) b'503 Error: nested MAIL command\r\n')[](#l2.87)
- def test_no_HELO_MAIL(self):
self.write_line(b'MAIL from:<foo@example.com>')[](#l2.90)
self.assertEqual(self.channel.socket.last,[](#l2.91)
b'503 Error: send HELO first\r\n')[](#l2.92)
self.write_line(b'HELO example')[](#l2.95) self.write_line(b'MAIL From:eggs@example')[](#l2.96) self.write_line(b'DATA')[](#l2.97) self.assertEqual(self.channel.socket.last,[](#l2.98) b'503 Error: need RCPT command\r\n')[](#l2.99)
self.write_line(b'HELO example')[](#l2.102) self.write_line(b'MAIL From:eggs@example')[](#l2.103) self.write_line(b'RCPT to eggs@example')[](#l2.104) self.assertEqual(self.channel.socket.last,[](#l2.105) b'501 Syntax: RCPT TO: <address>\r\n')[](#l2.106)
- def test_no_HELO_RCPT(self):
self.write_line(b'RCPT to eggs@example')[](#l2.109)
self.assertEqual(self.channel.socket.last,[](#l2.110)
b'503 Error: send HELO first\r\n')[](#l2.111)
self.write_line(b'HELO example')[](#l2.114) self.write_line(b'MAIL From:eggs@example')[](#l2.115) self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')[](#l2.116) self.write_line(b'RCPT To:spam@example')[](#l2.117)
@@ -193,12 +224,19 @@ class SMTPDChannelTest(TestCase): [('peer', 'eggs@example', ['spam@example'], 'data\nmore')]) def test_DATA_syntax(self):
self.write_line(b'HELO example')[](#l2.122) self.write_line(b'MAIL From:eggs@example')[](#l2.123) self.write_line(b'RCPT To:spam@example')[](#l2.124) self.write_line(b'DATA spam')[](#l2.125) self.assertEqual(self.channel.socket.last, b'501 Syntax: DATA\r\n')[](#l2.126)
- def test_no_HELO_DATA(self):
self.write_line(b'DATA spam')[](#l2.129)
self.assertEqual(self.channel.socket.last,[](#l2.130)
b'503 Error: send HELO first\r\n')[](#l2.131)
+ def test_data_transparency_section_4_5_2(self):
self.write_line(b'HELO example')[](#l2.134) self.write_line(b'MAIL From:eggs@example')[](#l2.135) self.write_line(b'RCPT To:spam@example')[](#l2.136) self.write_line(b'DATA')[](#l2.137)
@@ -206,6 +244,7 @@ class SMTPDChannelTest(TestCase): self.assertEqual(self.channel.received_data, '.') def test_multiple_RCPT(self):
self.write_line(b'HELO example')[](#l2.142) self.write_line(b'MAIL From:eggs@example')[](#l2.143) self.write_line(b'RCPT To:spam@example')[](#l2.144) self.write_line(b'RCPT To:ham@example')[](#l2.145)
@@ -216,6 +255,7 @@ class SMTPDChannelTest(TestCase): def test_manual_status(self): # checks that the Channel is able to return a custom status message
self.write_line(b'HELO example')[](#l2.150) self.write_line(b'MAIL From:eggs@example')[](#l2.151) self.write_line(b'RCPT To:spam@example')[](#l2.152) self.write_line(b'DATA')[](#l2.153)
@@ -223,6 +263,7 @@ class SMTPDChannelTest(TestCase): self.assertEqual(self.channel.socket.last, b'250 Okish\r\n') def test_RSET(self):
self.write_line(b'HELO example')[](#l2.158) self.write_line(b'MAIL From:eggs@example')[](#l2.159) self.write_line(b'RCPT To:spam@example')[](#l2.160) self.write_line(b'RSET')[](#l2.161)
@@ -234,6 +275,11 @@ class SMTPDChannelTest(TestCase): self.assertEqual(self.server.messages, [('peer', 'foo@example', ['eggs@example'], 'data')])
- def test_HELO_RSET(self):
self.write_line(b'HELO example')[](#l2.167)
self.write_line(b'RSET')[](#l2.168)
self.assertEqual(self.channel.socket.last, b'250 Ok\r\n')[](#l2.169)
+ def test_RSET_syntax(self): self.write_line(b'RSET hi') self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n')
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -531,6 +531,7 @@ Magnus Kessler Lawrence Kesteloot Vivek Khera Mads Kiilerich +Jason Killen Taek Joo Kim W. Trevor King Paul Kippes
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -30,6 +30,9 @@ Core and Builtins Library ------- +- Issue #14269: SMTPD now conforms to the RFC and requires a HELO command