diff -r a82d7e028458 Doc/library/smtpd.rst --- a/Doc/library/smtpd.rst Mon Jun 16 19:26:56 2014 -0400 +++ b/Doc/library/smtpd.rst Wed Jun 18 00:59:58 2014 +0200 @@ -20,7 +20,8 @@ Additionally the SMTPChannel may be extended to implement very specific interaction behaviour with SMTP clients. -The code supports :RFC:`5321`, plus the :rfc:`1870` SIZE extension. +The code supports :RFC:`5321`, plus the :rfc:`1870` SIZE and :rfc:`6531` +SMTPUTF8 extensions. SMTPServer Objects @@ -28,7 +29,7 @@ .. class:: SMTPServer(localaddr, remoteaddr, data_size_limit=33554432,\ - map=None, decode_data=True) + map=None, enable_SMTPUTF8=False, decode_data=True) Create a new :class:`SMTPServer` object, which binds to local address *localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. It @@ -39,6 +40,12 @@ accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no limit. + *enable_SMTPUTF8* determins whether the ``SMTPUTF8`` extension (as defined + in :RFC:`6531`) should be enabled. A :exc:`ValueError` is raised if + *decode_data* is set to ``True`` at the same time. Make sure to implement + :meth:`process_smtputf8_message` to support ``SMTPUTF8``. + Default: ``False``. + A dictionary can be specified in *map* to avoid using a global socket map. *decode_data* specifies whether the data portion of the SMTP transaction @@ -60,6 +67,17 @@ argument will be a unicode string. If it is set to ``False``, it will be a bytes object. + .. method:: process_smtputf8_message(peer, mailfrom, rcpttos, data) + + Same as :meth:`process_message` but for messages which require + ``SMTPUTF8`` support. This method is called instead of + :meth:`process_message` if the client reqested ``SMTPUTF8`` support + after the server announced it. + + This function should return ``None``, for a normal ``250 Ok`` response; + otherwise it should return the desired response string in :RFC:`6531` + format. + .. attribute:: channel_class Override this in subclasses to use a custom :class:`SMTPChannel` for @@ -68,8 +86,12 @@ .. versionchanged:: 3.4 The *map* argument was added. - .. versionchanged:: 3.5 the *decode_data* argument was added, and *localaddr* - and *remoteaddr* may now contain IPv6 addresses. + .. versionchanged:: 3.5 + *localaddr* and *remoteaddr* may now contain IPv6 addresses. + + .. versionadded:: 3.5 + the :meth:`process_smtputf8_message` method and the *decode_data* and + *enable_SMTPUTF8* arguments. DebuggingServer Objects @@ -109,7 +131,7 @@ ------------------- .. class:: SMTPChannel(server, conn, addr, data_size_limit=33554432,\ - map=None, decode_data=True) + map=None, enable_SMTPUTF8=False, decode_data=True) Create a new :class:`SMTPChannel` object which manages the communication between the server and a single SMTP client. @@ -120,6 +142,11 @@ accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no limit. + *enable_SMTPUTF8* determins whether the ``SMTPUTF8`` extension (as defined + in :RFC:`6531`) should be enabled. A :exc:`ValueError` is raised if + *decode_data* is set to ``True`` at the same time. + Default: ``False``. + A dictionary can be specified in *map* to avoid using a global socket map. *decode_data* specifies whether the data portion of the SMTP transaction @@ -131,7 +158,7 @@ :attr:`SMTPServer.channel_class` of your :class:`SMTPServer`. .. versionchanged:: 3.5 - the *decode_data* argument was added. + the *decode_data* and *enable_SMTPUTF8* arguments were added. The :class:`SMTPChannel` has the following instance variables: diff -r a82d7e028458 Lib/smtpd.py --- a/Lib/smtpd.py Mon Jun 16 19:26:56 2014 -0400 +++ b/Lib/smtpd.py Wed Jun 18 00:59:58 2014 +0200 @@ -1,5 +1,5 @@ #! /usr/bin/env python3 -"""An RFC 5321 smtp proxy. +"""An RFC 5321 smtp proxy with optional RFC 1870 and RFC 6531 extensions. Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]] @@ -25,6 +25,10 @@ Restrict the total size of the incoming message to "limit" number of bytes via the RFC 1870 SIZE extension. Defaults to 33554432 bytes. + --smtputf8 + -u + Enable the SMTPUTF8 extension and behave as RFC 6531 smtp proxy. + --debug -d Turn on debugging prints. @@ -114,19 +118,29 @@ DATA = 1 command_size_limit = 512 - command_size_limits = collections.defaultdict(lambda x=command_size_limit: x) - command_size_limits.update({ - 'MAIL': command_size_limit + 26, - }) - max_command_size_limit = max(command_size_limits.values()) + command_size_limits = collections.defaultdict(lambda : 512) + + @property + def max_command_size_limit(self): + try: + return max(self.command_size_limits.values()) + except ValueError: + return self.command_size_limits.default_factory() def __init__(self, server, conn, addr, data_size_limit=DATA_SIZE_DEFAULT, - map=None, decode_data=None): + map=None, enable_SMTPUTF8=False, decode_data=None): asynchat.async_chat.__init__(self, conn, map=map) self.smtp_server = server self.conn = conn self.addr = addr self.data_size_limit = data_size_limit + self.enable_SMTPUTF8 = enable_SMTPUTF8 + if enable_SMTPUTF8: + if decode_data: + ValueError( + "decode_data and enable_SMTPUTF8 cannot be set to True at" + " the same time") + decode_data = False if decode_data is None: warn("The decode_data default of True will change to False in 3.6;" " specify an explicit value for this keyword", @@ -143,14 +157,8 @@ self._linesep = b'\r\n' self._dotsep = b'.' self._newline = b'\n' - self.received_lines = [] - self.smtp_state = self.COMMAND - self.seen_greeting = '' - self.mailfrom = None - self.rcpttos = [] - self.received_data = '' + self._set_initial_state() self.fqdn = socket.getfqdn() - self.num_bytes = 0 try: self.peer = conn.getpeername() except OSError as err: @@ -162,8 +170,27 @@ return print('Peer:', repr(self.peer), file=DEBUGSTREAM) self.push('220 %s %s' % (self.fqdn, __version__)) + + def _set_initial_state(self, + skip_greeting=False, + skip_received_data=False): + """Set all variables which may change by SMTP conversations to their + initial state. + """ + self.smtp_state = self.COMMAND + self.mailfrom = None + self.rcpttos = [] + self.require_SMTPUTF8 = False + self.num_bytes = 0 self.set_terminator(b'\r\n') - self.extended_smtp = False + if not skip_received_data: + self.received_data = '' + self.received_lines = [] + if not skip_greeting: + self.seen_greeting = '' + self.extended_smtp = False + self.command_size_limits.clear() + # properties for backwards-compatibility @property @@ -287,9 +314,10 @@ "set 'addr' instead", DeprecationWarning, 2) self.addr = value - # Overrides base class for convenience + # Overrides base class for convenience. def push(self, msg): - asynchat.async_chat.push(self, bytes(msg + '\r\n', 'ascii')) + asynchat.async_chat.push(self, bytes( + msg + '\r\n', 'utf-8' if self.require_SMTPUTF8 else 'ascii')) # Implementation of base class abstract method def collect_incoming_data(self, data): @@ -317,9 +345,9 @@ if not line: self.push('500 Error: bad syntax') return - method = None if not self._decode_data: line = str(line, 'utf-8') + method = None i = line.find(' ') if i < 0: command = line.upper() @@ -328,7 +356,7 @@ command = line[:i].upper() arg = line[i+1:].strip() max_sz = (self.command_size_limits[command] - if self.extended_smtp else self.command_size_limit) + if self.extended_smtp else self.command_size_limit) if sz > max_sz: self.push('500 Error: line too long') return @@ -356,15 +384,19 @@ else: data.append(text) self.received_data = self._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.num_bytes = 0 - self.set_terminator(b'\r\n') + processing_function = ( + self.smtp_server.process_smtputf8_message + if self.require_SMTPUTF8 + else self.smtp_server.process_message + ) + status = processing_function( + self.peer, + self.mailfrom, + self.rcpttos, + self.received_data + ) + self._set_initial_state( + skip_greeting=True, skip_received_data=True) if not status: self.push('250 OK') else: @@ -375,26 +407,34 @@ if not arg: self.push('501 Syntax: HELO hostname') return + # See issue #21783 for a discussion of this behavior. if self.seen_greeting: self.push('503 Duplicate HELO/EHLO') - else: - self.seen_greeting = arg - self.extended_smtp = False - self.push('250 %s' % self.fqdn) + return + self._set_initial_state() + 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 + # See issue #21783 for a discussion of this behavior. if self.seen_greeting: self.push('503 Duplicate HELO/EHLO') - else: - self.seen_greeting = arg - self.extended_smtp = True - self.push('250-%s' % self.fqdn) - if self.data_size_limit: - self.push('250-SIZE %s' % self.data_size_limit) - self.push('250 HELP') + return + self._set_initial_state() + self.seen_greeting = arg + self.extended_smtp = True + self.push('250-%s' % self.fqdn) + if self.data_size_limit: + self.push('250-SIZE %s' % self.data_size_limit) + self.command_size_limits['MAIL'] += 26 + if self.enable_SMTPUTF8: + self.push('250-8BITMIME') + self.push('250-SMTPUTF8') + self.command_size_limits['MAIL'] += 10 + self.push('250 HELP') def smtp_NOOP(self, arg): if arg: @@ -427,8 +467,8 @@ def _getparams(self, params): # Return any parameters that appear to be syntactically valid according # to RFC 1869, ignore all others. (Postel rule: accept what we can.) - params = [param.split('=', 1) for param in params.split() - if '=' in param] + params = [param.split('=', 1) if '=' in param else (param, True) for + param in params.split()] return {k: v for k, v in params if k.isalnum()} def smtp_HELP(self, arg): @@ -506,6 +546,15 @@ if params is None: self.push(syntaxerr) return + body = params.pop('BODY', '7BIT') + if self.enable_SMTPUTF8 and params.pop('SMTPUTF8', False): + if body != '8BITMIME': + self.push( + '501 Syntax: MAIL FROM:
[BODY=8BITMIME SMTPUTF8]' + ) + return + else: + self.require_SMTPUTF8 = True size = params.pop('SIZE', None) if size: if not size.isdigit(): @@ -567,10 +616,7 @@ self.push('501 Syntax: RSET') return # Resets the sender, recipients, and data, but not the greeting - self.mailfrom = None - self.rcpttos = [] - self.received_data = '' - self.smtp_state = self.COMMAND + self._set_initial_state(skip_greeting=True) self.push('250 OK') def smtp_DATA(self, arg): @@ -598,10 +644,17 @@ def __init__(self, localaddr, remoteaddr, data_size_limit=DATA_SIZE_DEFAULT, map=None, - decode_data=None): + enable_SMTPUTF8=False, decode_data=None): self._localaddr = localaddr self._remoteaddr = remoteaddr self.data_size_limit = data_size_limit + self.enable_SMTPUTF8 = enable_SMTPUTF8 + if enable_SMTPUTF8: + if decode_data: + raise ValueError("The decode_data and enable_SMTPUTF8" + " parameters cannot be set to True at the" + " same time.") + decode_data = False if decode_data is None: warn("The decode_data default of True will change to False in 3.6;" " specify an explicit value for this keyword", @@ -627,8 +680,14 @@ def handle_accepted(self, conn, addr): print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM) - channel = self.channel_class(self, conn, addr, self.data_size_limit, - self._map, self._decode_data) + channel = self.channel_class( + self, + conn, + addr, + self.data_size_limit, + self._map, + self.enable_SMTPUTF8, + self._decode_data) # API for "doing something useful with the message" def process_message(self, peer, mailfrom, rcpttos, data): @@ -650,28 +709,54 @@ removed. This function should return None, for a normal `250 Ok' response; - otherwise it returns the desired response string in RFC 821 format. + otherwise it should return the desired response string in RFC 821 + format. """ raise NotImplementedError + # API for processing messeges needing Unicode support (RFC 6531, RFC 6532). + def process_smtputf8_message(self, peer, mailfrom, rcpttos, data): + """Same as ``process_message`` but for messages which have sent the + SMTPUTF8 parameter with the MAIL command to a SMTPUTF8 aware server + (see the enable_SMTPUTF8 init parameter). + + This function should return None, for a normal `250 Ok' response; + otherwise it should return the desired response string in RFC 6531 + format. + + """ + raise NotImplementedError + class DebuggingServer(SMTPServer): - # Do something with the gathered message - def process_message(self, peer, mailfrom, rcpttos, data): + def _print_message_content(self, peer, data): inheaders = 1 lines = data.split('\n') - print('---------- MESSAGE FOLLOWS ----------') for line in lines: # headers first if inheaders and not line: print('X-Peer:', peer[0]) inheaders = 0 print(line) + + def process_message(self, peer, mailfrom, rcpttos, data): + print('---------- MESSAGE FOLLOWS ----------') + self._print_message_content(peer, data) print('------------ END MESSAGE ------------') + def process_smtputf8_message(self, *args, **kwargs): + print('----- SMTPUTF8 MESSAGE FOLLOWS ------') + self._print_message_content(peer, data) + print('------------ END MESSAGE ------------') + class PureProxy(SMTPServer): + def __init__(self, *args, **kwargs): + if 'enable_SMTPUTF8' in kwargs and kwargs['enable_SMTPUTF8']: + raise ValueError("SMTPUTF8 is not supported.") + super(PureProxy, self).__init__(*args, **kwargs) + def process_message(self, peer, mailfrom, rcpttos, data): lines = data.split('\n') # Look for the last header @@ -712,6 +797,11 @@ class MailmanProxy(PureProxy): + def __init__(self, *args, **kwargs): + if 'enable_SMTPUTF8' in kwargs and kwargs['enable_SMTPUTF8']: + raise ValueError("SMTPUTF8 is not supported.") + super(PureProxy, self).__init__(*args, **kwargs) + def process_message(self, peer, mailfrom, rcpttos, data): from io import StringIO from Mailman import Utils @@ -790,17 +880,19 @@ class Options: - setuid = 1 + setuid = True classname = 'PureProxy' size_limit = None + enable_SMTPUTF8 = False def parseargs(): global DEBUGSTREAM try: opts, args = getopt.getopt( - sys.argv[1:], 'nVhc:s:d', - ['class=', 'nosetuid', 'version', 'help', 'size=', 'debug']) + sys.argv[1:], 'nVhc:s:du', + ['class=', 'nosetuid', 'version', 'help', 'size=', 'debug', + 'smtputf8']) except getopt.error as e: usage(1, e) @@ -812,11 +904,13 @@ print(__version__) sys.exit(0) elif opt in ('-n', '--nosetuid'): - options.setuid = 0 + options.setuid = False elif opt in ('-c', '--class'): options.classname = arg elif opt in ('-d', '--debug'): DEBUGSTREAM = sys.stderr + elif opt in ('-u', '--smtputf8'): + options.enable_SMTPUTF8 = True elif opt in ('-s', '--size'): try: int_size = int(arg) @@ -871,7 +965,7 @@ class_ = getattr(mod, classname) proxy = class_((options.localhost, options.localport), (options.remotehost, options.remoteport), - options.size_limit) + options.size_limit, enable_SMTPUTF8=options.enable_SMTPUTF8) if options.setuid: try: import pwd diff -r a82d7e028458 Lib/test/test_smtpd.py --- a/Lib/test/test_smtpd.py Mon Jun 16 19:26:56 2014 -0400 +++ b/Lib/test/test_smtpd.py Wed Jun 18 00:59:58 2014 +0200 @@ -7,11 +7,10 @@ class DummyServer(smtpd.SMTPServer): - def __init__(self, localaddr, remoteaddr, decode_data=True): - smtpd.SMTPServer.__init__(self, localaddr, remoteaddr, - decode_data=decode_data) + def __init__(self, *args, **kwargs): + smtpd.SMTPServer.__init__(self, *args, **kwargs) self.messages = [] - if decode_data: + if self._decode_data: self.return_status = 'return status' else: self.return_status = b'return status' @@ -21,6 +20,9 @@ if data == self.return_status: return '250 Okish' + def process_smtputf8_message(self, *args, **kwargs): + return '250 SMTPUTF8 message okish' + class DummyDispatcherBroken(Exception): pass @@ -51,10 +53,35 @@ write_line(b'DATA') self.assertRaises(NotImplementedError, write_line, b'spam\r\n.\r\n') + def test_process_smtputf8_message_unimplemented(self): + server = smtpd.SMTPServer((support.HOST, 0), ('b', 0), + enable_SMTPUTF8=True) + conn, addr = server.accept() + channel = smtpd.SMTPChannel(server, conn, addr, enable_SMTPUTF8=True) + + def write_line(line): + channel.socket.queue_recv(line) + channel.handle_read() + + write_line(b'EHLO example') + write_line(b'MAIL From: