| 
									
										
										
										
											2010-03-11 22:53:45 +00:00
										 |  |  | #! /usr/bin/env python3 | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  | """An RFC 5321 smtp proxy with optional RFC 1870 and RFC 6531 extensions.
 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-04 16:27:04 +00:00
										 |  |  | Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]] | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | Options: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     --nosetuid | 
					
						
							|  |  |  |     -n | 
					
						
							| 
									
										
										
										
											2024-05-22 12:35:18 -04:00
										 |  |  |         This program generally tries to setuid 'nobody', unless this flag is | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         set.  The setuid call will fail if this program is not run as root (in | 
					
						
							|  |  |  |         which case, use this flag). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     --version | 
					
						
							|  |  |  |     -V | 
					
						
							|  |  |  |         Print the version number and exit. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     --class classname | 
					
						
							|  |  |  |     -c classname | 
					
						
							| 
									
										
										
										
											2024-05-22 12:35:18 -04:00
										 |  |  |         Use 'classname' as the concrete SMTP proxy class.  Uses 'PureProxy' by | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         default. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |     --size limit | 
					
						
							|  |  |  |     -s limit | 
					
						
							|  |  |  |         Restrict the total size of the incoming message to "limit" number of | 
					
						
							|  |  |  |         bytes via the RFC 1870 SIZE extension.  Defaults to 33554432 bytes. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |     --smtputf8 | 
					
						
							|  |  |  |     -u | 
					
						
							|  |  |  |         Enable the SMTPUTF8 extension and behave as an RFC 6531 smtp proxy. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     --debug | 
					
						
							|  |  |  |     -d | 
					
						
							|  |  |  |         Turn on debugging prints. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     --help | 
					
						
							|  |  |  |     -h | 
					
						
							|  |  |  |         Print this message and exit. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Version: %(__version__)s | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-22 12:35:18 -04:00
										 |  |  | If localhost is not given then 'localhost' is used, and if localport is not | 
					
						
							|  |  |  | given then 8025 is used.  If remotehost is not given then 'localhost' is used, | 
					
						
							| 
									
										
										
										
											2001-10-04 16:27:04 +00:00
										 |  |  | and if remoteport is not given, then 25 is used. | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Overview: | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  | # This file implements the minimal SMTP protocol as defined in RFC 5321.  It | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | # has a hierarchy of classes which implement the backend functionality for the | 
					
						
							|  |  |  | # smtpd.  A number of classes are provided: | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2001-04-15 13:06:04 +00:00
										 |  |  | #   SMTPServer - the base class for the backend.  Raises NotImplementedError | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | #   if you try to use it. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #   DebuggingServer - simply prints each message it receives on stdout. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | #   PureProxy - Proxies all messages to a real smtpd which does final | 
					
						
							|  |  |  | #   delivery.  One known problem with this class is that it doesn't handle | 
					
						
							|  |  |  | #   SMTP errors from the backend server at all.  This should be fixed | 
					
						
							|  |  |  | #   (contributions are welcome!). | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2004-07-12 23:10:08 +00:00
										 |  |  | # Author: Barry Warsaw <barry@python.org> | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # TODO: | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # - support mailbox delivery | 
					
						
							|  |  |  | # - alias files | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  | # - Handle more ESMTP extensions | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | # - handle error codes from the backend smtpd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import errno | 
					
						
							|  |  |  | import getopt | 
					
						
							|  |  |  | import time | 
					
						
							|  |  |  | import socket | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  | import collections | 
					
						
							| 
									
										
										
										
											2022-11-08 18:48:58 +03:00
										 |  |  | from test.support import asyncore, asynchat | 
					
						
							| 
									
										
										
										
											2022-08-06 03:41:29 +03:00
										 |  |  | from warnings import warn | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  | from email._header_value_parser import get_addr_spec, get_angle_addr | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-06 02:03:11 +00:00
										 |  |  | __all__ = [ | 
					
						
							|  |  |  |     "SMTPChannel", "SMTPServer", "DebuggingServer", "PureProxy", | 
					
						
							|  |  |  | ] | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | program = sys.argv[0] | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  | __version__ = 'Python SMTP proxy version 0.3' | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Devnull: | 
					
						
							|  |  |  |     def write(self, msg): pass | 
					
						
							|  |  |  |     def flush(self): pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DEBUGSTREAM = Devnull() | 
					
						
							|  |  |  | NEWLINE = '\n' | 
					
						
							| 
									
										
										
										
											2001-10-04 16:27:04 +00:00
										 |  |  | COMMASPACE = ', ' | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  | DATA_SIZE_DEFAULT = 33554432 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def usage(code, msg=''): | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |     print(__doc__ % globals(), file=sys.stderr) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     if msg: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         print(msg, file=sys.stderr) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     sys.exit(code) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SMTPChannel(asynchat.async_chat): | 
					
						
							|  |  |  |     COMMAND = 0 | 
					
						
							|  |  |  |     DATA = 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-03 07:38:22 +00:00
										 |  |  |     command_size_limit = 512 | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |     command_size_limits = collections.defaultdict(lambda x=command_size_limit: x) | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def max_command_size_limit(self): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             return max(self.command_size_limits.values()) | 
					
						
							|  |  |  |         except ValueError: | 
					
						
							|  |  |  |             return self.command_size_limit | 
					
						
							| 
									
										
										
										
											2010-12-03 07:38:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-07 15:21:41 +01:00
										 |  |  |     def __init__(self, server, conn, addr, data_size_limit=DATA_SIZE_DEFAULT, | 
					
						
							| 
									
										
										
										
											2016-05-16 09:36:31 +03:00
										 |  |  |                  map=None, enable_SMTPUTF8=False, decode_data=False): | 
					
						
							| 
									
										
										
										
											2013-06-07 15:21:41 +01:00
										 |  |  |         asynchat.async_chat.__init__(self, conn, map=map) | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         self.smtp_server = server | 
					
						
							|  |  |  |         self.conn = conn | 
					
						
							|  |  |  |         self.addr = addr | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         self.data_size_limit = data_size_limit | 
					
						
							| 
									
										
										
										
											2016-05-29 23:50:56 +03:00
										 |  |  |         self.enable_SMTPUTF8 = enable_SMTPUTF8 | 
					
						
							|  |  |  |         self._decode_data = decode_data | 
					
						
							| 
									
										
										
										
											2016-05-16 09:36:31 +03:00
										 |  |  |         if enable_SMTPUTF8 and decode_data: | 
					
						
							|  |  |  |             raise ValueError("decode_data and enable_SMTPUTF8 cannot" | 
					
						
							|  |  |  |                              " be set to True at the same time") | 
					
						
							| 
									
										
										
										
											2014-06-11 11:18:08 -04:00
										 |  |  |         if decode_data: | 
					
						
							|  |  |  |             self._emptystring = '' | 
					
						
							|  |  |  |             self._linesep = '\r\n' | 
					
						
							|  |  |  |             self._dotsep = '.' | 
					
						
							|  |  |  |             self._newline = NEWLINE | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self._emptystring = b'' | 
					
						
							|  |  |  |             self._linesep = b'\r\n' | 
					
						
							| 
									
										
										
										
											2015-03-20 16:48:02 +02:00
										 |  |  |             self._dotsep = ord(b'.') | 
					
						
							| 
									
										
										
										
											2014-06-11 11:18:08 -04:00
										 |  |  |             self._newline = b'\n' | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         self._set_rset_state() | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         self.seen_greeting = '' | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         self.extended_smtp = False | 
					
						
							|  |  |  |         self.command_size_limits.clear() | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         self.fqdn = socket.getfqdn() | 
					
						
							| 
									
										
										
										
											2010-08-23 22:28:13 +00:00
										 |  |  |         try: | 
					
						
							|  |  |  |             self.peer = conn.getpeername() | 
					
						
							| 
									
										
										
										
											2012-12-18 23:10:48 +02:00
										 |  |  |         except OSError as err: | 
					
						
							| 
									
										
										
										
											2010-08-23 22:28:13 +00:00
										 |  |  |             # a race condition  may occur if the other end is closing | 
					
						
							|  |  |  |             # before we can get the peername | 
					
						
							|  |  |  |             self.close() | 
					
						
							| 
									
										
										
										
											2020-11-22 10:28:34 +02:00
										 |  |  |             if err.errno != errno.ENOTCONN: | 
					
						
							| 
									
										
										
										
											2010-08-23 22:28:13 +00:00
										 |  |  |                 raise | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         print('Peer:', repr(self.peer), file=DEBUGSTREAM) | 
					
						
							|  |  |  |         self.push('220 %s %s' % (self.fqdn, __version__)) | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _set_post_data_state(self): | 
					
						
							|  |  |  |         """Reset state variables to their post-DATA state.""" | 
					
						
							|  |  |  |         self.smtp_state = self.COMMAND | 
					
						
							|  |  |  |         self.mailfrom = None | 
					
						
							|  |  |  |         self.rcpttos = [] | 
					
						
							|  |  |  |         self.require_SMTPUTF8 = False | 
					
						
							|  |  |  |         self.num_bytes = 0 | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         self.set_terminator(b'\r\n') | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _set_rset_state(self): | 
					
						
							|  |  |  |         """Reset all state variables except the greeting.""" | 
					
						
							|  |  |  |         self._set_post_data_state() | 
					
						
							|  |  |  |         self.received_data = '' | 
					
						
							|  |  |  |         self.received_lines = [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-07 12:31:04 +01:00
										 |  |  |     # properties for backwards-compatibility | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __server(self): | 
					
						
							|  |  |  |         warn("Access to __server attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'smtp_server' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.smtp_server | 
					
						
							|  |  |  |     @__server.setter | 
					
						
							|  |  |  |     def __server(self, value): | 
					
						
							|  |  |  |         warn("Setting __server attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'smtp_server' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.smtp_server = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __line(self): | 
					
						
							|  |  |  |         warn("Access to __line attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'received_lines' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.received_lines | 
					
						
							|  |  |  |     @__line.setter | 
					
						
							|  |  |  |     def __line(self, value): | 
					
						
							|  |  |  |         warn("Setting __line attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'received_lines' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.received_lines = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __state(self): | 
					
						
							|  |  |  |         warn("Access to __state attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'smtp_state' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.smtp_state | 
					
						
							|  |  |  |     @__state.setter | 
					
						
							|  |  |  |     def __state(self, value): | 
					
						
							|  |  |  |         warn("Setting __state attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'smtp_state' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.smtp_state = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __greeting(self): | 
					
						
							|  |  |  |         warn("Access to __greeting attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'seen_greeting' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.seen_greeting | 
					
						
							|  |  |  |     @__greeting.setter | 
					
						
							|  |  |  |     def __greeting(self, value): | 
					
						
							|  |  |  |         warn("Setting __greeting attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'seen_greeting' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.seen_greeting = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __mailfrom(self): | 
					
						
							|  |  |  |         warn("Access to __mailfrom attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'mailfrom' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.mailfrom | 
					
						
							|  |  |  |     @__mailfrom.setter | 
					
						
							|  |  |  |     def __mailfrom(self, value): | 
					
						
							|  |  |  |         warn("Setting __mailfrom attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'mailfrom' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.mailfrom = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __rcpttos(self): | 
					
						
							|  |  |  |         warn("Access to __rcpttos attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'rcpttos' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.rcpttos | 
					
						
							|  |  |  |     @__rcpttos.setter | 
					
						
							|  |  |  |     def __rcpttos(self, value): | 
					
						
							|  |  |  |         warn("Setting __rcpttos attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'rcpttos' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.rcpttos = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __data(self): | 
					
						
							|  |  |  |         warn("Access to __data attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'received_data' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.received_data | 
					
						
							|  |  |  |     @__data.setter | 
					
						
							|  |  |  |     def __data(self, value): | 
					
						
							|  |  |  |         warn("Setting __data attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'received_data' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.received_data = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __fqdn(self): | 
					
						
							|  |  |  |         warn("Access to __fqdn attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'fqdn' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.fqdn | 
					
						
							|  |  |  |     @__fqdn.setter | 
					
						
							|  |  |  |     def __fqdn(self, value): | 
					
						
							|  |  |  |         warn("Setting __fqdn attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'fqdn' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.fqdn = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __peer(self): | 
					
						
							|  |  |  |         warn("Access to __peer attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'peer' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.peer | 
					
						
							|  |  |  |     @__peer.setter | 
					
						
							|  |  |  |     def __peer(self, value): | 
					
						
							|  |  |  |         warn("Setting __peer attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'peer' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.peer = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __conn(self): | 
					
						
							|  |  |  |         warn("Access to __conn attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'conn' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.conn | 
					
						
							|  |  |  |     @__conn.setter | 
					
						
							|  |  |  |     def __conn(self, value): | 
					
						
							|  |  |  |         warn("Setting __conn attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'conn' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.conn = value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @property | 
					
						
							|  |  |  |     def __addr(self): | 
					
						
							|  |  |  |         warn("Access to __addr attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "use 'addr' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         return self.addr | 
					
						
							|  |  |  |     @__addr.setter | 
					
						
							|  |  |  |     def __addr(self, value): | 
					
						
							|  |  |  |         warn("Setting __addr attribute on SMTPChannel is deprecated, " | 
					
						
							|  |  |  |             "set 'addr' instead", DeprecationWarning, 2) | 
					
						
							|  |  |  |         self.addr = value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |     # Overrides base class for convenience. | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     def push(self, msg): | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         asynchat.async_chat.push(self, bytes( | 
					
						
							|  |  |  |             msg + '\r\n', 'utf-8' if self.require_SMTPUTF8 else 'ascii')) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Implementation of base class abstract method | 
					
						
							|  |  |  |     def collect_incoming_data(self, data): | 
					
						
							| 
									
										
										
										
											2010-12-03 07:38:22 +00:00
										 |  |  |         limit = None | 
					
						
							|  |  |  |         if self.smtp_state == self.COMMAND: | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |             limit = self.max_command_size_limit | 
					
						
							| 
									
										
										
										
											2010-12-03 07:38:22 +00:00
										 |  |  |         elif self.smtp_state == self.DATA: | 
					
						
							|  |  |  |             limit = self.data_size_limit | 
					
						
							|  |  |  |         if limit and self.num_bytes > limit: | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         elif limit: | 
					
						
							|  |  |  |             self.num_bytes += len(data) | 
					
						
							| 
									
										
										
										
											2014-06-11 11:18:08 -04:00
										 |  |  |         if self._decode_data: | 
					
						
							|  |  |  |             self.received_lines.append(str(data, 'utf-8')) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.received_lines.append(data) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Implementation of base class abstract method | 
					
						
							|  |  |  |     def found_terminator(self): | 
					
						
							| 
									
										
										
										
											2014-06-11 11:18:08 -04:00
										 |  |  |         line = self._emptystring.join(self.received_lines) | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         print('Data:', repr(line), file=DEBUGSTREAM) | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         self.received_lines = [] | 
					
						
							|  |  |  |         if self.smtp_state == self.COMMAND: | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |             sz, self.num_bytes = self.num_bytes, 0 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             if not line: | 
					
						
							|  |  |  |                 self.push('500 Error: bad syntax') | 
					
						
							|  |  |  |                 return | 
					
						
							| 
									
										
										
										
											2014-06-11 11:18:08 -04:00
										 |  |  |             if not self._decode_data: | 
					
						
							|  |  |  |                 line = str(line, 'utf-8') | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             i = line.find(' ') | 
					
						
							|  |  |  |             if i < 0: | 
					
						
							|  |  |  |                 command = line.upper() | 
					
						
							|  |  |  |                 arg = None | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 command = line[:i].upper() | 
					
						
							|  |  |  |                 arg = line[i+1:].strip() | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |             max_sz = (self.command_size_limits[command] | 
					
						
							|  |  |  |                         if self.extended_smtp else self.command_size_limit) | 
					
						
							|  |  |  |             if sz > max_sz: | 
					
						
							|  |  |  |                 self.push('500 Error: line too long') | 
					
						
							|  |  |  |                 return | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             method = getattr(self, 'smtp_' + command, None) | 
					
						
							|  |  |  |             if not method: | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |                 self.push('500 Error: command "%s" not recognized' % command) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |                 return | 
					
						
							|  |  |  |             method(arg) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |             if self.smtp_state != self.DATA: | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |                 self.push('451 Internal confusion') | 
					
						
							| 
									
										
										
										
											2010-12-03 07:38:22 +00:00
										 |  |  |                 self.num_bytes = 0 | 
					
						
							|  |  |  |                 return | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |             if self.data_size_limit and self.num_bytes > self.data_size_limit: | 
					
						
							| 
									
										
										
										
											2010-12-03 07:38:22 +00:00
										 |  |  |                 self.push('552 Error: Too much mail data') | 
					
						
							|  |  |  |                 self.num_bytes = 0 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |                 return | 
					
						
							|  |  |  |             # Remove extraneous carriage returns and de-transparency according | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |             # to RFC 5321, Section 4.5.2. | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             data = [] | 
					
						
							| 
									
										
										
										
											2014-06-11 11:18:08 -04:00
										 |  |  |             for text in line.split(self._linesep): | 
					
						
							|  |  |  |                 if text and text[0] == self._dotsep: | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |                     data.append(text[1:]) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     data.append(text) | 
					
						
							| 
									
										
										
										
											2014-06-11 11:18:08 -04:00
										 |  |  |             self.received_data = self._newline.join(data) | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |             args = (self.peer, self.mailfrom, self.rcpttos, self.received_data) | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |             kwargs = {} | 
					
						
							|  |  |  |             if not self._decode_data: | 
					
						
							|  |  |  |                 kwargs = { | 
					
						
							|  |  |  |                     'mail_options': self.mail_options, | 
					
						
							|  |  |  |                     'rcpt_options': self.rcpt_options, | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             status = self.smtp_server.process_message(*args, **kwargs) | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |             self._set_post_data_state() | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             if not status: | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |                 self.push('250 OK') | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             else: | 
					
						
							|  |  |  |                 self.push(status) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # SMTP and ESMTP commands | 
					
						
							|  |  |  |     def smtp_HELO(self, arg): | 
					
						
							|  |  |  |         if not arg: | 
					
						
							|  |  |  |             self.push('501 Syntax: HELO hostname') | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         # See issue #21783 for a discussion of this behavior. | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         if self.seen_greeting: | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             self.push('503 Duplicate HELO/EHLO') | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |             return | 
					
						
							|  |  |  |         self._set_rset_state() | 
					
						
							|  |  |  |         self.seen_greeting = arg | 
					
						
							|  |  |  |         self.push('250 %s' % self.fqdn) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |     def smtp_EHLO(self, arg): | 
					
						
							|  |  |  |         if not arg: | 
					
						
							|  |  |  |             self.push('501 Syntax: EHLO hostname') | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         # See issue #21783 for a discussion of this behavior. | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         if self.seen_greeting: | 
					
						
							|  |  |  |             self.push('503 Duplicate HELO/EHLO') | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |             return | 
					
						
							|  |  |  |         self._set_rset_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 | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |         if not self._decode_data: | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |             self.push('250-8BITMIME') | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |         if self.enable_SMTPUTF8: | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |             self.push('250-SMTPUTF8') | 
					
						
							|  |  |  |             self.command_size_limits['MAIL'] += 10 | 
					
						
							|  |  |  |         self.push('250 HELP') | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     def smtp_NOOP(self, arg): | 
					
						
							|  |  |  |         if arg: | 
					
						
							|  |  |  |             self.push('501 Syntax: NOOP') | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |             self.push('250 OK') | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def smtp_QUIT(self, arg): | 
					
						
							|  |  |  |         # args is ignored | 
					
						
							|  |  |  |         self.push('221 Bye') | 
					
						
							|  |  |  |         self.close_when_done() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |     def _strip_command_keyword(self, keyword, arg): | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         keylen = len(keyword) | 
					
						
							|  |  |  |         if arg[:keylen].upper() == keyword: | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |             return arg[keylen:].strip() | 
					
						
							|  |  |  |         return '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _getaddr(self, arg): | 
					
						
							|  |  |  |         if not arg: | 
					
						
							|  |  |  |             return '', '' | 
					
						
							|  |  |  |         if arg.lstrip().startswith('<'): | 
					
						
							|  |  |  |             address, rest = get_angle_addr(arg) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             address, rest = get_addr_spec(arg) | 
					
						
							|  |  |  |         if not address: | 
					
						
							|  |  |  |             return address, rest | 
					
						
							|  |  |  |         return address.addr_spec, rest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def _getparams(self, params): | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |         # Return params as dictionary. Return None if not all parameters | 
					
						
							|  |  |  |         # appear to be syntactically valid according to RFC 1869. | 
					
						
							|  |  |  |         result = {} | 
					
						
							|  |  |  |         for param in params: | 
					
						
							|  |  |  |             param, eq, value = param.partition('=') | 
					
						
							|  |  |  |             if not param.isalnum() or eq and not value: | 
					
						
							|  |  |  |                 return None | 
					
						
							|  |  |  |             result[param] = value if eq else True | 
					
						
							|  |  |  |         return result | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def smtp_HELP(self, arg): | 
					
						
							|  |  |  |         if arg: | 
					
						
							| 
									
										
										
										
											2015-04-05 10:01:48 -04:00
										 |  |  |             extended = ' [SP <mail-parameters>]' | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |             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': | 
					
						
							|  |  |  |                 msg = '250 Syntax: MAIL FROM: <address>' | 
					
						
							|  |  |  |                 if self.extended_smtp: | 
					
						
							|  |  |  |                     msg += extended | 
					
						
							|  |  |  |                 self.push(msg) | 
					
						
							|  |  |  |             elif lc_arg == 'RCPT': | 
					
						
							|  |  |  |                 msg = '250 Syntax: RCPT TO: <address>' | 
					
						
							|  |  |  |                 if self.extended_smtp: | 
					
						
							|  |  |  |                     msg += extended | 
					
						
							|  |  |  |                 self.push(msg) | 
					
						
							|  |  |  |             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 <address>') | 
					
						
							|  |  |  |             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, params = self._getaddr(arg) | 
					
						
							|  |  |  |             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 <address>') | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def smtp_MAIL(self, arg): | 
					
						
							| 
									
										
										
										
											2012-03-20 16:16:29 -04:00
										 |  |  |         if not self.seen_greeting: | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |             self.push('503 Error: send HELO first') | 
					
						
							| 
									
										
										
										
											2012-03-20 16:16:29 -04:00
										 |  |  |             return | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         print('===> MAIL', arg, file=DEBUGSTREAM) | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         syntaxerr = '501 Syntax: MAIL FROM: <address>' | 
					
						
							|  |  |  |         if self.extended_smtp: | 
					
						
							|  |  |  |             syntaxerr += ' [SP <mail-parameters>]' | 
					
						
							|  |  |  |         if arg is None: | 
					
						
							|  |  |  |             self.push(syntaxerr) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         arg = self._strip_command_keyword('FROM:', arg) | 
					
						
							|  |  |  |         address, params = self._getaddr(arg) | 
					
						
							|  |  |  |         if not address: | 
					
						
							|  |  |  |             self.push(syntaxerr) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         if not self.extended_smtp and params: | 
					
						
							|  |  |  |             self.push(syntaxerr) | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         if self.mailfrom: | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             self.push('503 Error: nested MAIL command') | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |         self.mail_options = params.upper().split() | 
					
						
							|  |  |  |         params = self._getparams(self.mail_options) | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         if params is None: | 
					
						
							|  |  |  |             self.push(syntaxerr) | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |         if not self._decode_data: | 
					
						
							|  |  |  |             body = params.pop('BODY', '7BIT') | 
					
						
							|  |  |  |             if body not in ['7BIT', '8BITMIME']: | 
					
						
							|  |  |  |                 self.push('501 Error: BODY can only be one of 7BIT, 8BITMIME') | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |                 return | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |         if self.enable_SMTPUTF8: | 
					
						
							|  |  |  |             smtputf8 = params.pop('SMTPUTF8', False) | 
					
						
							|  |  |  |             if smtputf8 is True: | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |                 self.require_SMTPUTF8 = True | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |             elif smtputf8 is not False: | 
					
						
							|  |  |  |                 self.push('501 Error: SMTPUTF8 takes no arguments') | 
					
						
							|  |  |  |                 return | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         size = params.pop('SIZE', None) | 
					
						
							|  |  |  |         if size: | 
					
						
							|  |  |  |             if not size.isdigit(): | 
					
						
							|  |  |  |                 self.push(syntaxerr) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |             elif self.data_size_limit and int(size) > self.data_size_limit: | 
					
						
							|  |  |  |                 self.push('552 Error: message size exceeds fixed maximum message size') | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |         if len(params.keys()) > 0: | 
					
						
							|  |  |  |             self.push('555 MAIL FROM parameters not recognized or not implemented') | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         self.mailfrom = address | 
					
						
							|  |  |  |         print('sender:', self.mailfrom, file=DEBUGSTREAM) | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         self.push('250 OK') | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def smtp_RCPT(self, arg): | 
					
						
							| 
									
										
										
										
											2012-03-20 16:16:29 -04:00
										 |  |  |         if not self.seen_greeting: | 
					
						
							|  |  |  |             self.push('503 Error: send HELO first'); | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         print('===> RCPT', arg, file=DEBUGSTREAM) | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         if not self.mailfrom: | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             self.push('503 Error: need MAIL command') | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         syntaxerr = '501 Syntax: RCPT TO: <address>' | 
					
						
							|  |  |  |         if self.extended_smtp: | 
					
						
							|  |  |  |             syntaxerr += ' [SP <mail-parameters>]' | 
					
						
							|  |  |  |         if arg is None: | 
					
						
							|  |  |  |             self.push(syntaxerr) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         arg = self._strip_command_keyword('TO:', arg) | 
					
						
							|  |  |  |         address, params = self._getaddr(arg) | 
					
						
							|  |  |  |         if not address: | 
					
						
							|  |  |  |             self.push(syntaxerr) | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |         if not self.extended_smtp and params: | 
					
						
							|  |  |  |             self.push(syntaxerr) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         self.rcpt_options = params.upper().split() | 
					
						
							|  |  |  |         params = self._getparams(self.rcpt_options) | 
					
						
							|  |  |  |         if params is None: | 
					
						
							|  |  |  |             self.push(syntaxerr) | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         # XXX currently there are no options we recognize. | 
					
						
							|  |  |  |         if len(params.keys()) > 0: | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |             self.push('555 RCPT TO parameters not recognized or not implemented') | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         self.rcpttos.append(address) | 
					
						
							|  |  |  |         print('recips:', self.rcpttos, file=DEBUGSTREAM) | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         self.push('250 OK') | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def smtp_RSET(self, arg): | 
					
						
							|  |  |  |         if arg: | 
					
						
							|  |  |  |             self.push('501 Syntax: RSET') | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         self._set_rset_state() | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         self.push('250 OK') | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def smtp_DATA(self, arg): | 
					
						
							| 
									
										
										
										
											2012-03-20 16:16:29 -04:00
										 |  |  |         if not self.seen_greeting: | 
					
						
							|  |  |  |             self.push('503 Error: send HELO first'); | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         if not self.rcpttos: | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             self.push('503 Error: need RCPT command') | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         if arg: | 
					
						
							|  |  |  |             self.push('501 Syntax: DATA') | 
					
						
							|  |  |  |             return | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |         self.smtp_state = self.DATA | 
					
						
							| 
									
										
										
										
											2008-07-07 04:15:08 +00:00
										 |  |  |         self.set_terminator(b'\r\n.\r\n') | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         self.push('354 End data with <CR><LF>.<CR><LF>') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |     # Commands that have not been implemented | 
					
						
							|  |  |  |     def smtp_EXPN(self, arg): | 
					
						
							|  |  |  |         self.push('502 EXPN not implemented') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class SMTPServer(asyncore.dispatcher): | 
					
						
							| 
									
										
										
										
											2010-07-24 09:51:40 +00:00
										 |  |  |     # SMTPChannel class to use for managing client connections | 
					
						
							|  |  |  |     channel_class = SMTPChannel | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |     def __init__(self, localaddr, remoteaddr, | 
					
						
							| 
									
										
										
										
											2014-06-11 11:18:08 -04:00
										 |  |  |                  data_size_limit=DATA_SIZE_DEFAULT, map=None, | 
					
						
							| 
									
										
										
										
											2016-05-16 09:36:31 +03:00
										 |  |  |                  enable_SMTPUTF8=False, decode_data=False): | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         self._localaddr = localaddr | 
					
						
							|  |  |  |         self._remoteaddr = remoteaddr | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         self.data_size_limit = data_size_limit | 
					
						
							| 
									
										
										
										
											2016-05-29 23:50:56 +03:00
										 |  |  |         self.enable_SMTPUTF8 = enable_SMTPUTF8 | 
					
						
							|  |  |  |         self._decode_data = decode_data | 
					
						
							| 
									
										
										
										
											2016-05-16 09:36:31 +03:00
										 |  |  |         if enable_SMTPUTF8 and decode_data: | 
					
						
							|  |  |  |             raise ValueError("decode_data and enable_SMTPUTF8 cannot" | 
					
						
							|  |  |  |                              " be set to True at the same time") | 
					
						
							| 
									
										
										
										
											2013-06-07 15:21:41 +01:00
										 |  |  |         asyncore.dispatcher.__init__(self, map=map) | 
					
						
							| 
									
										
										
										
											2010-06-30 17:47:39 +00:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2014-06-11 15:17:50 -04:00
										 |  |  |             gai_results = socket.getaddrinfo(*localaddr, | 
					
						
							|  |  |  |                                              type=socket.SOCK_STREAM) | 
					
						
							| 
									
										
										
										
											2014-06-11 13:48:58 -04:00
										 |  |  |             self.create_socket(gai_results[0][0], gai_results[0][1]) | 
					
						
							| 
									
										
										
										
											2010-06-30 17:47:39 +00:00
										 |  |  |             # try to re-use a server port if possible | 
					
						
							|  |  |  |             self.set_reuse_addr() | 
					
						
							|  |  |  |             self.bind(localaddr) | 
					
						
							|  |  |  |             self.listen(5) | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             self.close() | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             print('%s started at %s\n\tLocal addr: %s\n\tRemote addr:%s' % ( | 
					
						
							|  |  |  |                 self.__class__.__name__, time.ctime(time.time()), | 
					
						
							|  |  |  |                 localaddr, remoteaddr), file=DEBUGSTREAM) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-04 21:08:36 +00:00
										 |  |  |     def handle_accepted(self, conn, addr): | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM) | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         channel = self.channel_class(self, | 
					
						
							|  |  |  |                                      conn, | 
					
						
							|  |  |  |                                      addr, | 
					
						
							|  |  |  |                                      self.data_size_limit, | 
					
						
							|  |  |  |                                      self._map, | 
					
						
							|  |  |  |                                      self.enable_SMTPUTF8, | 
					
						
							|  |  |  |                                      self._decode_data) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # API for "doing something useful with the message" | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |     def process_message(self, peer, mailfrom, rcpttos, data, **kwargs): | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         """Override this abstract method to handle messages from the client.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         peer is a tuple containing (ipaddr, port) of the client that made the | 
					
						
							|  |  |  |         socket connection to our smtp port. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         mailfrom is the raw address the client claims the message is coming | 
					
						
							|  |  |  |         from. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         rcpttos is a list of raw addresses the client wishes to deliver the | 
					
						
							|  |  |  |         message to. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         data is a string containing the entire full text of the message, | 
					
						
							| 
									
										
										
										
											2024-05-22 12:35:18 -04:00
										 |  |  |         headers (if supplied) and all.  It has been 'de-transparencied' | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         according to RFC 821, Section 4.5.2.  In other words, a line | 
					
						
							| 
									
										
										
										
											2024-05-22 12:35:18 -04:00
										 |  |  |         containing a '.' followed by other text has had the leading dot | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         removed. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 09:36:31 +03:00
										 |  |  |         kwargs is a dictionary containing additional information.  It is | 
					
						
							|  |  |  |         empty if decode_data=True was given as init parameter, otherwise | 
					
						
							|  |  |  |         it will contain the following keys: | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |             'mail_options': list of parameters to the mail command.  All | 
					
						
							|  |  |  |                             elements are uppercase strings.  Example: | 
					
						
							|  |  |  |                             ['BODY=8BITMIME', 'SMTPUTF8']. | 
					
						
							|  |  |  |             'rcpt_options': same, for the rcpt command. | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-22 12:35:18 -04:00
										 |  |  |         This function should return None for a normal '250 Ok' response; | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |         otherwise, it should return the desired response string in RFC 821 | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         format. | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         """
 | 
					
						
							| 
									
										
										
										
											2001-04-15 13:06:04 +00:00
										 |  |  |         raise NotImplementedError | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-09 20:06:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | class DebuggingServer(SMTPServer): | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _print_message_content(self, peer, data): | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         inheaders = 1 | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         lines = data.splitlines() | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         for line in lines: | 
					
						
							|  |  |  |             # headers first | 
					
						
							|  |  |  |             if inheaders and not line: | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |                 peerheader = 'X-Peer: ' + peer[0] | 
					
						
							|  |  |  |                 if not isinstance(data, str): | 
					
						
							|  |  |  |                     # decoded_data=false; make header match other binary output | 
					
						
							|  |  |  |                     peerheader = repr(peerheader.encode('utf-8')) | 
					
						
							|  |  |  |                 print(peerheader) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |                 inheaders = 0 | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |             if not isinstance(data, str): | 
					
						
							|  |  |  |                 # Avoid spurious 'str on bytes instance' warning. | 
					
						
							|  |  |  |                 line = repr(line) | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |             print(line) | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |     def process_message(self, peer, mailfrom, rcpttos, data, **kwargs): | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         print('---------- MESSAGE FOLLOWS ----------') | 
					
						
							| 
									
										
										
										
											2015-05-11 12:11:40 -04:00
										 |  |  |         if kwargs: | 
					
						
							|  |  |  |             if kwargs.get('mail_options'): | 
					
						
							|  |  |  |                 print('mail options: %s' % kwargs['mail_options']) | 
					
						
							|  |  |  |             if kwargs.get('rcpt_options'): | 
					
						
							|  |  |  |                 print('rcpt options: %s\n' % kwargs['rcpt_options']) | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         self._print_message_content(peer, data) | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         print('------------ END MESSAGE ------------') | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PureProxy(SMTPServer): | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |     def __init__(self, *args, **kwargs): | 
					
						
							|  |  |  |         if 'enable_SMTPUTF8' in kwargs and kwargs['enable_SMTPUTF8']: | 
					
						
							|  |  |  |             raise ValueError("PureProxy does not support SMTPUTF8.") | 
					
						
							|  |  |  |         super(PureProxy, self).__init__(*args, **kwargs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     def process_message(self, peer, mailfrom, rcpttos, data): | 
					
						
							|  |  |  |         lines = data.split('\n') | 
					
						
							|  |  |  |         # Look for the last header | 
					
						
							|  |  |  |         i = 0 | 
					
						
							|  |  |  |         for line in lines: | 
					
						
							|  |  |  |             if not line: | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             i += 1 | 
					
						
							|  |  |  |         lines.insert(i, 'X-Peer: %s' % peer[0]) | 
					
						
							|  |  |  |         data = NEWLINE.join(lines) | 
					
						
							|  |  |  |         refused = self._deliver(mailfrom, rcpttos, data) | 
					
						
							|  |  |  |         # TBD: what to do with refused addresses? | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |         print('we got some refusals:', refused, file=DEBUGSTREAM) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def _deliver(self, mailfrom, rcpttos, data): | 
					
						
							|  |  |  |         import smtplib | 
					
						
							|  |  |  |         refused = {} | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             s = smtplib.SMTP() | 
					
						
							|  |  |  |             s.connect(self._remoteaddr[0], self._remoteaddr[1]) | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 refused = s.sendmail(mailfrom, rcpttos, data) | 
					
						
							|  |  |  |             finally: | 
					
						
							|  |  |  |                 s.quit() | 
					
						
							| 
									
										
										
										
											2007-01-10 16:19:56 +00:00
										 |  |  |         except smtplib.SMTPRecipientsRefused as e: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |             print('got SMTPRecipientsRefused', file=DEBUGSTREAM) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             refused = e.recipients | 
					
						
							| 
									
										
										
										
											2012-12-18 23:10:48 +02:00
										 |  |  |         except (OSError, smtplib.SMTPException) as e: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |             print('got', e.__class__, file=DEBUGSTREAM) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             # All recipients were refused.  If the exception had an associated | 
					
						
							|  |  |  |             # error code, use it.  Otherwise,fake it with a non-triggering | 
					
						
							|  |  |  |             # exception code. | 
					
						
							|  |  |  |             errcode = getattr(e, 'smtp_code', -1) | 
					
						
							|  |  |  |             errmsg = getattr(e, 'smtp_error', 'ignore') | 
					
						
							|  |  |  |             for r in rcpttos: | 
					
						
							|  |  |  |                 refused[r] = (errcode, errmsg) | 
					
						
							|  |  |  |         return refused | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Options: | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |     setuid = True | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     classname = 'PureProxy' | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |     size_limit = None | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |     enable_SMTPUTF8 = False | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def parseargs(): | 
					
						
							|  |  |  |     global DEBUGSTREAM | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         opts, args = getopt.getopt( | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |             sys.argv[1:], 'nVhc:s:du', | 
					
						
							|  |  |  |             ['class=', 'nosetuid', 'version', 'help', 'size=', 'debug', | 
					
						
							|  |  |  |              'smtputf8']) | 
					
						
							| 
									
										
										
										
											2007-01-10 16:19:56 +00:00
										 |  |  |     except getopt.error as e: | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         usage(1, e) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     options = Options() | 
					
						
							|  |  |  |     for opt, arg in opts: | 
					
						
							|  |  |  |         if opt in ('-h', '--help'): | 
					
						
							|  |  |  |             usage(0) | 
					
						
							|  |  |  |         elif opt in ('-V', '--version'): | 
					
						
							| 
									
										
										
										
											2013-09-05 17:44:53 +03:00
										 |  |  |             print(__version__) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             sys.exit(0) | 
					
						
							|  |  |  |         elif opt in ('-n', '--nosetuid'): | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |             options.setuid = False | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         elif opt in ('-c', '--class'): | 
					
						
							|  |  |  |             options.classname = arg | 
					
						
							|  |  |  |         elif opt in ('-d', '--debug'): | 
					
						
							|  |  |  |             DEBUGSTREAM = sys.stderr | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |         elif opt in ('-u', '--smtputf8'): | 
					
						
							|  |  |  |             options.enable_SMTPUTF8 = True | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |         elif opt in ('-s', '--size'): | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 int_size = int(arg) | 
					
						
							|  |  |  |                 options.size_limit = int_size | 
					
						
							|  |  |  |             except: | 
					
						
							|  |  |  |                 print('Invalid size: ' + arg, file=sys.stderr) | 
					
						
							|  |  |  |                 sys.exit(1) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # parse the rest of the arguments | 
					
						
							| 
									
										
										
										
											2001-10-04 16:27:04 +00:00
										 |  |  |     if len(args) < 1: | 
					
						
							|  |  |  |         localspec = 'localhost:8025' | 
					
						
							|  |  |  |         remotespec = 'localhost:25' | 
					
						
							|  |  |  |     elif len(args) < 2: | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |         localspec = args[0] | 
					
						
							| 
									
										
										
										
											2001-10-04 16:27:04 +00:00
										 |  |  |         remotespec = 'localhost:25' | 
					
						
							| 
									
										
										
										
											2001-11-04 03:04:25 +00:00
										 |  |  |     elif len(args) < 3: | 
					
						
							|  |  |  |         localspec = args[0] | 
					
						
							|  |  |  |         remotespec = args[1] | 
					
						
							| 
									
										
										
										
											2001-10-04 16:27:04 +00:00
										 |  |  |     else: | 
					
						
							|  |  |  |         usage(1, 'Invalid arguments: %s' % COMMASPACE.join(args)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     # split into host/port pairs | 
					
						
							|  |  |  |     i = localspec.find(':') | 
					
						
							|  |  |  |     if i < 0: | 
					
						
							| 
									
										
										
										
											2001-10-04 16:27:04 +00:00
										 |  |  |         usage(1, 'Bad local spec: %s' % localspec) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     options.localhost = localspec[:i] | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         options.localport = int(localspec[i+1:]) | 
					
						
							|  |  |  |     except ValueError: | 
					
						
							| 
									
										
										
										
											2001-10-04 16:27:04 +00:00
										 |  |  |         usage(1, 'Bad local port: %s' % localspec) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     i = remotespec.find(':') | 
					
						
							|  |  |  |     if i < 0: | 
					
						
							| 
									
										
										
										
											2001-10-04 16:27:04 +00:00
										 |  |  |         usage(1, 'Bad remote spec: %s' % remotespec) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     options.remotehost = remotespec[:i] | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         options.remoteport = int(remotespec[i+1:]) | 
					
						
							|  |  |  |     except ValueError: | 
					
						
							| 
									
										
										
										
											2001-10-04 16:27:04 +00:00
										 |  |  |         usage(1, 'Bad remote port: %s' % remotespec) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     return options | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     options = parseargs() | 
					
						
							|  |  |  |     # Become nobody | 
					
						
							| 
									
										
										
										
											2011-10-20 23:03:43 +02:00
										 |  |  |     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), | 
					
						
							| 
									
										
										
										
											2012-05-26 14:33:59 -04:00
										 |  |  |                    (options.remotehost, options.remoteport), | 
					
						
							| 
									
										
										
										
											2014-08-09 16:40:49 -04:00
										 |  |  |                    options.size_limit, enable_SMTPUTF8=options.enable_SMTPUTF8) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |     if options.setuid: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             import pwd | 
					
						
							| 
									
										
										
										
											2013-07-04 17:43:24 -04:00
										 |  |  |         except ImportError: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |             print('Cannot import module "pwd"; try running with -n option.', file=sys.stderr) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             sys.exit(1) | 
					
						
							|  |  |  |         nobody = pwd.getpwnam('nobody')[2] | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             os.setuid(nobody) | 
					
						
							| 
									
										
										
										
											2013-02-12 15:14:17 +01:00
										 |  |  |         except PermissionError: | 
					
						
							| 
									
										
										
										
											2007-02-09 05:37:30 +00:00
										 |  |  |             print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr) | 
					
						
							| 
									
										
										
										
											2001-01-31 22:51:35 +00:00
										 |  |  |             sys.exit(1) | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         asyncore.loop() | 
					
						
							|  |  |  |     except KeyboardInterrupt: | 
					
						
							|  |  |  |         pass |