diff -aur py3k.orig/Lib/smtpd.py py3k/Lib/smtpd.py --- py3k.orig/Lib/smtpd.py 2010-08-04 19:14:50.313169195 -0600 +++ py3k/Lib/smtpd.py 2010-08-04 23:44:43.300718940 -0600 @@ -1,5 +1,5 @@ #! /usr/bin/env python3 -"""An RFC 2821 smtp proxy. +"""An RFC 5321 smtp proxy. Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]] @@ -20,6 +20,11 @@ Use `classname' as the concrete SMTP proxy class. Uses `PureProxy' by default. + --size limit + -s limit + Restrict the total size of the incoming message to "limit" number of + bytes. Defaults to 0 (no limit). + --debug -d Turn on debugging prints. @@ -35,10 +40,9 @@ and if remoteport is not given, then 25 is used. """ - # Overview: # -# This file implements the minimal SMTP protocol as defined in RFC 821. It +# This file implements the minimal SMTP protocol as defined in RFC 5321. It # has a hierarchy of classes which implement the backend functionality for the # smtpd. A number of classes are provided: # @@ -59,15 +63,18 @@ # gets forwarded to a real backend smtpd, as with PureProxy. Again, errors # are not handled correctly yet. # -# Please note that this script requires Python 2.0 +# Please note that this script requires Python 3.0 # # Author: Barry Warsaw # +# Contributors: +# Alberto Trevino +# # TODO: # # - support mailbox delivery # - alias files -# - ESMTP +# - Handle more ESMTP extensions # - handle error codes from the backend smtpd import sys @@ -83,7 +90,7 @@ __all__ = ["SMTPServer","DebuggingServer","PureProxy","MailmanProxy"] program = sys.argv[0] -__version__ = 'Python SMTP proxy version 0.2' +__version__ = 'Python SMTP proxy version 0.3' class Devnull: @@ -97,7 +104,6 @@ COMMASPACE = ', ' - def usage(code, msg=''): print(__doc__ % globals(), file=sys.stderr) if msg: @@ -105,16 +111,16 @@ sys.exit(code) - class SMTPChannel(asynchat.async_chat): COMMAND = 0 DATA = 1 - def __init__(self, server, conn, addr): + def __init__(self, server, conn, addr, size = 0): asynchat.async_chat.__init__(self, conn) self.smtp_server = server self.conn = conn self.addr = addr + self.size = size self.received_lines = [] self.smtp_state = self.COMMAND self.seen_greeting = '' @@ -123,6 +129,7 @@ self.received_data = '' self.fqdn = socket.getfqdn() self.peer = conn.getpeername() + self.eightbitmime = False print('Peer:', repr(self.peer), file=DEBUGSTREAM) self.push('220 %s %s' % (self.fqdn, __version__)) self.set_terminator(b'\r\n') @@ -276,7 +283,7 @@ arg = line[i+1:].strip() method = getattr(self, 'smtp_' + command, None) if not method: - self.push('502 Error: command "%s" not implemented' % command) + self.push('500 Error: command "%s" not recognized' % command) return method(arg) return @@ -285,7 +292,7 @@ self.push('451 Internal confusion') return # Remove extraneous carriage returns and de-transparency according - # to RFC 821, Section 4.5.2. + # to RFC 5321, Section 4.5.2. data = [] for text in line.split('\r\n'): if text and text[0] == '.': @@ -293,18 +300,39 @@ else: data.append(text) self.received_data = NEWLINE.join(data) - status = self.smtp_server.process_message(self.peer, - self.mailfrom, - self.rcpttos, - self.received_data) - self.rcpttos = [] - self.mailfrom = None - self.smtp_state = self.COMMAND - self.set_terminator(b'\r\n') - if not status: - self.push('250 Ok') + + # Enforce data size limit + if self.size == 0 or len(self.received_data) <= self.size: + status = self.smtp_server.process_message(self.peer, + self.mailfrom, + self.rcpttos, + self.received_data) + self.rcpttos = [] + self.mailfrom = None + self.smtp_state = self.COMMAND + self.set_terminator(b'\r\n') + if not status: + self.push('250 OK') + else: + self.push(status) else: - self.push(status) + self.state = self.COMMAND + self.set_terminator(b'\r\n') + self.push('552 Too much mail data') + + # factored + def __getaddr(self, keyword, arg): + address = None + keylen = len(keyword) + if arg[:keylen].upper() == keyword: + address = arg[keylen:].strip() + if not address: + pass + elif address[0] == '<' and address[-1] == '>' and address != '<>': + # Addresses can be in the form but watch out + # for null address, e.g. <> + address = address[1:-1] + return address # SMTP and ESMTP commands def smtp_HELO(self, arg): @@ -317,30 +345,78 @@ self.seen_greeting = arg self.push('250 %s' % self.fqdn) + def smtp_EHLO(self, arg): + if not arg: + self.push('501 Syntax: EHLO hostname') + return + if self.seen_greeting: + self.push('503 Duplicate HELO/EHLO') + else: + self.seen_greeting = arg + self.push('250-%s' % self.fqdn) + self.push('250-8BITMIME') + if self.size > 0: + self.push('250-SIZE %s' % self.size) + self.push('250 HELP') + def smtp_NOOP(self, arg): if arg: self.push('501 Syntax: NOOP') else: - self.push('250 Ok') + self.push('250 OK') def smtp_QUIT(self, arg): # args is ignored self.push('221 Bye') self.close_when_done() - # factored - def __getaddr(self, keyword, arg): - address = None - keylen = len(keyword) - if arg[:keylen].upper() == keyword: - address = arg[keylen:].strip() - if not address: - pass - elif address[0] == '<' and address[-1] == '>' and address != '<>': - # Addresses can be in the form but watch out - # for null address, e.g. <> - address = address[1:-1] - return address + def smtp_8BITMIME(self, arg): + # There is nothing in this code that forces 7 bits, so it seems OK + # to simply accept this command; its value is saved for future use; + self.eightbitmime = True + if arg: + self.push("501 Syntax: 8BITMIME") + else: + self.push('250 OK') + + def smtp_HELP(self, arg): + if arg: + lc_arg = arg.upper() + if lc_arg == 'EHLO': + self.push('250 Syntax: EHLO hostname') + elif lc_arg == 'HELO': + self.push('250 Syntax: HELO hostname') + elif lc_arg == 'MAIL': + self.push('250 Syntax: MAIL FROM:
') + elif lc_arg == 'RCPT': + self.push('250 Syntax: RCPT TO:
') + elif lc_arg == 'DATA': + self.push('250 Syntax: DATA') + elif lc_arg == 'RSET': + self.push('250 Syntax: RSET') + elif lc_arg == 'NOOP': + self.push('250 Syntax: NOOP') + elif lc_arg == 'QUIT': + self.push('250 Syntax: QUIT') + elif lc_arg == 'VRFY': + self.push('250 Syntax: VRFY
') + else: + self.push('501 Supported commands: EHLO HELO MAIL RCPT ' + \ + 'DATA RSET NOOP QUIT VRFY') + else: + self.push('250 Supported commands: EHLO HELO MAIL RCPT DATA ' + \ + 'RSET NOOP QUIT VRFY') + + def smtp_VRFY(self, arg): + if arg: + address = self.__getaddr('', arg) if arg else None + if address: + self.push('252 Cannot VRFY user, but will accept message ' + \ + 'and attempt delivery') + else: + self.push('502 Could not VRFY %s' % arg) + else: + self.push('501 Syntax: VRFY
') def smtp_MAIL(self, arg): print('===> MAIL', arg, file=DEBUGSTREAM) @@ -353,7 +429,7 @@ return self.mailfrom = address print('sender:', self.mailfrom, file=DEBUGSTREAM) - self.push('250 Ok') + self.push('250 OK') def smtp_RCPT(self, arg): print('===> RCPT', arg, file=DEBUGSTREAM) @@ -366,7 +442,7 @@ return self.rcpttos.append(address) print('recips:', self.rcpttos, file=DEBUGSTREAM) - self.push('250 Ok') + self.push('250 OK') def smtp_RSET(self, arg): if arg: @@ -377,7 +453,7 @@ self.rcpttos = [] self.received_data = '' self.smtp_state = self.COMMAND - self.push('250 Ok') + self.push('250 OK') def smtp_DATA(self, arg): if not self.rcpttos: @@ -390,15 +466,19 @@ self.set_terminator(b'\r\n.\r\n') self.push('354 End data with .') + # Commands that have not been implemented + def smtp_EXPN(self, arg): + self.push('502 EXPN not implemented') + - class SMTPServer(asyncore.dispatcher): # SMTPChannel class to use for managing client connections channel_class = SMTPChannel - def __init__(self, localaddr, remoteaddr): + def __init__(self, localaddr, remoteaddr, size = 0): self._localaddr = localaddr self._remoteaddr = remoteaddr + self._size = size asyncore.dispatcher.__init__(self) try: self.create_socket(socket.AF_INET, socket.SOCK_STREAM) @@ -417,7 +497,7 @@ def handle_accept(self): conn, addr = self.accept() print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM) - channel = self.channel_class(self, conn, addr) + channel = self.channel_class(self, conn, addr, self._size) # API for "doing something useful with the message" def process_message(self, peer, mailfrom, rcpttos, data): @@ -438,14 +518,13 @@ containing a `.' followed by other text has had the leading dot removed. - This function should return None, for a normal `250 Ok' response; + This function should return None, for a normal `250 OK' response; otherwise it returns the desired response string in RFC 821 format. """ raise NotImplementedError - class DebuggingServer(SMTPServer): # Do something with the gathered message def process_message(self, peer, mailfrom, rcpttos, data): @@ -461,7 +540,6 @@ print('------------ END MESSAGE ------------') - class PureProxy(SMTPServer): def process_message(self, peer, mailfrom, rcpttos, data): lines = data.split('\n') @@ -502,7 +580,6 @@ return refused - class MailmanProxy(PureProxy): def process_message(self, peer, mailfrom, rcpttos, data): from io import StringIO @@ -581,19 +658,18 @@ msg.Enqueue(mlist, torequest=1) - class Options: setuid = 1 classname = 'PureProxy' + size = 0 - def parseargs(): global DEBUGSTREAM try: opts, args = getopt.getopt( - sys.argv[1:], 'nVhc:d', - ['class=', 'nosetuid', 'version', 'help', 'debug']) + sys.argv[1:], 'nVhc:ds:', + ['class=', 'nosetuid', 'version', 'help', 'debug', 'size=']) except getopt.error as e: usage(1, e) @@ -610,6 +686,13 @@ options.classname = arg elif opt in ('-d', '--debug'): DEBUGSTREAM = sys.stderr + elif opt in ('-s', '--size'): + try: + int_size = int(arg) + options.size = int_size + except: + print('Invalid size: ' + arg, file=sys.stderr) + sys.exit(1) # parse the rest of the arguments if len(args) < 1: @@ -644,33 +727,35 @@ return options - if __name__ == '__main__': options = parseargs() + classname = options.classname + if "." in classname: + lastdot = classname.rfind(".") + mod = __import__(classname[:lastdot], globals(), locals(), [""]) + classname = classname[lastdot+1:] + else: + import __main__ as mod + class_ = getattr(mod, classname) + proxy = class_((options.localhost, options.localport), + (options.remotehost, options.remoteport), + options.size) # Become nobody if options.setuid: try: import pwd except ImportError: - print('Cannot import module "pwd"; try running with -n option.', file=sys.stderr) + print('Cannot import module "pwd"; try running with -n option.', \ + file=sys.stderr) sys.exit(1) nobody = pwd.getpwnam('nobody')[2] try: os.setuid(nobody) except OSError as e: if e.errno != errno.EPERM: raise - print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr) + print('Cannot setuid "nobody"; try running with -n option.', \ + file=sys.stderr) sys.exit(1) - classname = options.classname - if "." in classname: - lastdot = classname.rfind(".") - mod = __import__(classname[:lastdot], globals(), locals(), [""]) - classname = classname[lastdot+1:] - else: - import __main__ as mod - class_ = getattr(mod, classname) - proxy = class_((options.localhost, options.localport), - (options.remotehost, options.remoteport)) try: asyncore.loop() except KeyboardInterrupt: diff -aur py3k.orig/Lib/test/test_smtpd.py py3k/Lib/test/test_smtpd.py --- py3k.orig/Lib/test/test_smtpd.py 2010-08-04 19:14:40.539028702 -0600 +++ py3k/Lib/test/test_smtpd.py 2010-08-04 23:40:54.931576107 -0600 @@ -74,10 +74,27 @@ self.assertEqual(self.channel.socket.last, b'500 Error: bad syntax\r\n') - def test_EHLO_not_implemented(self): + def test_EHLO(self): self.write_line(b'EHLO test.example') self.assertEqual(self.channel.socket.last, - b'502 Error: command "EHLO" not implemented\r\n') + b'250 HELP\r\n') + + def test_EHLO_bad_syntax(self): + self.write_line(b'EHLO') + self.assertEqual(self.channel.socket.last, + b'501 Syntax: EHLO hostname\r\n') + + def test_EHLO_duplicate(self): + self.write_line(b'EHLO test.example') + self.write_line(b'EHLO test.example') + self.assertEqual(self.channel.socket.last, + b'503 Duplicate HELO/EHLO\r\n') + + def test_EHLO_HELO_duplicate(self): + self.write_line(b'EHLO test.example') + self.write_line(b'HELO test.example') + self.assertEqual(self.channel.socket.last, + b'503 Duplicate HELO/EHLO\r\n') def test_HELO(self): name = smtpd.socket.getfqdn() @@ -96,15 +113,46 @@ self.assertEqual(self.channel.socket.last, b'503 Duplicate HELO/EHLO\r\n') + def test_HELO_EHLO_duplicate(self): + self.write_line(b'HELO test.example') + self.write_line(b'EHLO test.example') + self.assertEqual(self.channel.socket.last, + b'503 Duplicate HELO/EHLO\r\n') + + def test_HELP(self): + self.write_line(b'HELP') + self.assertEqual(self.channel.socket.last, + b'250 Supported commands: EHLO HELO MAIL RCPT ' + \ + b'DATA RSET NOOP QUIT VRFY\r\n') + + def test_HELP_command(self): + self.write_line(b'HELP MAIL') + self.assertEqual(self.channel.socket.last, + b'250 Syntax: MAIL FROM:
\r\n') + + def test_HELP_command_unknown(self): + self.write_line(b'HELP SPAM') + self.assertEqual(self.channel.socket.last, + b'501 Supported commands: EHLO HELO MAIL RCPT ' + \ + b'DATA RSET NOOP QUIT VRFY\r\n') + def test_NOOP(self): self.write_line(b'NOOP') - self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') def test_NOOP_bad_syntax(self): self.write_line(b'NOOP hi') self.assertEqual(self.channel.socket.last, b'501 Syntax: NOOP\r\n') + def test_8BITMIME(self): + self.write_line(b'8BITMIME') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + + def test_8BITMIME_syntax(self): + self.write_line(b'8BITMIME ON') + self.assertEqual(self.channel.socket.last, b'501 Syntax: 8BITMIME\r\n') + def test_QUIT(self): self.write_line(b'QUIT') self.assertEqual(self.channel.socket.last, b'221 Bye\r\n') @@ -136,7 +184,7 @@ def test_MAIL_chevrons(self): self.write_line(b'MAIL from:') - self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') def test_nested_MAIL(self): self.write_line(b'MAIL from:eggs@example') @@ -156,17 +204,33 @@ self.assertEqual(self.channel.socket.last, b'501 Syntax: RCPT TO:
\r\n') + def test_VRFY(self): + self.write_line(b'VRFY eggs@example') + self.assertEqual(self.channel.socket.last, + b'252 Cannot VRFY user, but will accept message and attempt ' + \ + b'delivery\r\n') + + def test_VRFY_syntax(self): + self.write_line(b'VRFY') + self.assertEqual(self.channel.socket.last, + b'501 Syntax: VRFY
\r\n') + + def test_EXPN_not_implemented(self): + self.write_line(b'EXPN') + self.assertEqual(self.channel.socket.last, + b'502 EXPN not implemented\r\n') + def test_data_dialog(self): self.write_line(b'MAIL From:eggs@example') - self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') self.write_line(b'RCPT To:spam@example') - self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') self.write_line(b'DATA') self.assertEqual(self.channel.socket.last, b'354 End data with .\r\n') self.write_line(b'data\r\nmore\r\n.') - self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') self.assertEqual(self.server.messages, [('peer', 'eggs@example', ['spam@example'], 'data\nmore')]) @@ -204,7 +268,7 @@ self.write_line(b'MAIL From:eggs@example') self.write_line(b'RCPT To:spam@example') self.write_line(b'RSET') - self.assertEqual(self.channel.socket.last, b'250 Ok\r\n') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') self.write_line(b'MAIL From:foo@example') self.write_line(b'RCPT To:eggs@example') self.write_line(b'DATA') @@ -216,6 +280,12 @@ self.write_line(b'RSET hi') self.assertEqual(self.channel.socket.last, b'501 Syntax: RSET\r\n') + def test_unknown_command(self): + self.write_line(b'UNKNOWN_CMD') + self.assertEqual(self.channel.socket.last, + b'500 Error: command "UNKNOWN_CMD" not ' + \ + b'recognized\r\n') + def test_attribute_deprecations(self): with support.check_warnings(('', PendingDeprecationWarning)): spam = self.channel._SMTPChannel__server @@ -262,8 +332,52 @@ with support.check_warnings(('', PendingDeprecationWarning)): self.channel._SMTPChannel__addr = 'spam' +class SMTPDChannelWithDataSizeLimitTest(TestCase): + def setUp(self): + smtpd.socket = asyncore.socket = mock_socket + self.debug = smtpd.DEBUGSTREAM = io.StringIO() + self.server = DummyServer('a', 'b') + conn, addr = self.server.accept() + # Set DATA size limit to 32 bytes for easy testing + self.channel = smtpd.SMTPChannel(self.server, conn, addr, 32) + + def tearDown(self): + asyncore.socket = smtpd.socket = socket + + def write_line(self, line): + self.channel.socket.queue_recv(line) + self.channel.handle_read() + + def test_data_limit_dialog(self): + self.write_line(b'MAIL From:eggs@example') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + self.write_line(b'RCPT To:spam@example') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + + self.write_line(b'DATA') + self.assertEqual(self.channel.socket.last, + b'354 End data with .\r\n') + self.write_line(b'data\r\nmore\r\n.') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + self.assertEqual(self.server.messages, + [('peer', 'eggs@example', ['spam@example'], 'data\nmore')]) + + def test_data_limit_dialog_too_much_data(self): + self.write_line(b'MAIL From:eggs@example') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + self.write_line(b'RCPT To:spam@example') + self.assertEqual(self.channel.socket.last, b'250 OK\r\n') + + self.write_line(b'DATA') + self.assertEqual(self.channel.socket.last, + b'354 End data with .\r\n') + self.write_line(b'This message is longer than 32 bytes\r\n.') + self.assertEqual(self.channel.socket.last, + b'552 Too much mail data\r\n') + def test_main(): - support.run_unittest(SMTPDServerTest, SMTPDChannelTest) + support.run_unittest(SMTPDServerTest, SMTPDChannelTest, + SMTPDChannelWithDataSizeLimitTest) if __name__ == "__main__": test_main() diff -aur py3k.orig/Lib/test/test_smtplib.py py3k/Lib/test/test_smtplib.py --- py3k.orig/Lib/test/test_smtplib.py 2010-08-04 19:14:36.312340384 -0600 +++ py3k/Lib/test/test_smtplib.py 2010-08-04 23:59:30.714797414 -0600 @@ -191,27 +191,28 @@ def testNOOP(self): smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) - expected = (250, b'Ok') + expected = (250, b'OK') self.assertEqual(smtp.noop(), expected) smtp.quit() def testRSET(self): smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) - expected = (250, b'Ok') + expected = (250, b'OK') self.assertEqual(smtp.rset(), expected) smtp.quit() def testNotImplemented(self): - # EHLO isn't implemented in DebuggingServer + # EXPN isn't implemented in DebuggingServer smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3) - expected = (502, b'Error: command "EHLO" not implemented') - self.assertEqual(smtp.ehlo(), expected) + expected = (502, b'EXPN not implemented') + smtp.putcmd('EXPN') + self.assertEqual(smtp.getreply(), expected) smtp.quit() def testVRFY(self): - # VRFY isn't implemented in DebuggingServer 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.verify('nobody@nowhere.com'), expected) smtp.quit() @@ -227,7 +228,8 @@ def testHELP(self): 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() def testSend(self):