Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 2 | """An RFC 5321 smtp proxy with optional RFC 1870 and RFC 6531 extensions. |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 3 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 4 | Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]] |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 5 | |
| 6 | Options: |
| 7 | |
| 8 | --nosetuid |
| 9 | -n |
| 10 | This program generally tries to setuid `nobody', unless this flag is |
| 11 | set. The setuid call will fail if this program is not run as root (in |
| 12 | which case, use this flag). |
| 13 | |
| 14 | --version |
| 15 | -V |
| 16 | Print the version number and exit. |
| 17 | |
| 18 | --class classname |
| 19 | -c classname |
Barry Warsaw | f267b62 | 2004-10-09 21:44:13 +0000 | [diff] [blame] | 20 | Use `classname' as the concrete SMTP proxy class. Uses `PureProxy' by |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 21 | default. |
| 22 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 23 | --size limit |
| 24 | -s limit |
| 25 | Restrict the total size of the incoming message to "limit" number of |
| 26 | bytes via the RFC 1870 SIZE extension. Defaults to 33554432 bytes. |
| 27 | |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 28 | --smtputf8 |
| 29 | -u |
| 30 | Enable the SMTPUTF8 extension and behave as an RFC 6531 smtp proxy. |
| 31 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 32 | --debug |
| 33 | -d |
| 34 | Turn on debugging prints. |
| 35 | |
| 36 | --help |
| 37 | -h |
| 38 | Print this message and exit. |
| 39 | |
| 40 | Version: %(__version__)s |
| 41 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 42 | If localhost is not given then `localhost' is used, and if localport is not |
| 43 | given then 8025 is used. If remotehost is not given then `localhost' is used, |
| 44 | and if remoteport is not given, then 25 is used. |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 45 | """ |
| 46 | |
| 47 | # Overview: |
| 48 | # |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 49 | # This file implements the minimal SMTP protocol as defined in RFC 5321. It |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 50 | # has a hierarchy of classes which implement the backend functionality for the |
| 51 | # smtpd. A number of classes are provided: |
| 52 | # |
Guido van Rossum | b8b45ea | 2001-04-15 13:06:04 +0000 | [diff] [blame] | 53 | # SMTPServer - the base class for the backend. Raises NotImplementedError |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 54 | # if you try to use it. |
| 55 | # |
| 56 | # DebuggingServer - simply prints each message it receives on stdout. |
| 57 | # |
| 58 | # PureProxy - Proxies all messages to a real smtpd which does final |
| 59 | # delivery. One known problem with this class is that it doesn't handle |
| 60 | # SMTP errors from the backend server at all. This should be fixed |
| 61 | # (contributions are welcome!). |
| 62 | # |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 63 | # |
Barry Warsaw | b102764 | 2004-07-12 23:10:08 +0000 | [diff] [blame] | 64 | # Author: Barry Warsaw <barry@python.org> |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 65 | # |
| 66 | # TODO: |
| 67 | # |
| 68 | # - support mailbox delivery |
| 69 | # - alias files |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 70 | # - Handle more ESMTP extensions |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 71 | # - handle error codes from the backend smtpd |
| 72 | |
| 73 | import sys |
| 74 | import os |
| 75 | import errno |
| 76 | import getopt |
| 77 | import time |
| 78 | import socket |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 79 | import collections |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 80 | from warnings import warn |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 81 | from email._header_value_parser import get_addr_spec, get_angle_addr |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 82 | |
Martin Panter | 380ef01 | 2016-06-06 02:03:11 +0000 | [diff] [blame] | 83 | __all__ = [ |
| 84 | "SMTPChannel", "SMTPServer", "DebuggingServer", "PureProxy", |
Martin Panter | 380ef01 | 2016-06-06 02:03:11 +0000 | [diff] [blame] | 85 | ] |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 86 | |
Barry Warsaw | 8488b85 | 2021-06-24 12:37:26 -0700 | [diff] [blame] | 87 | warn( |
| 88 | 'The smtpd module is deprecated and unmaintained. Please see aiosmtpd ' |
| 89 | '(https://aiosmtpd.readthedocs.io/) for the recommended replacement.', |
| 90 | DeprecationWarning, |
| 91 | stacklevel=2) |
| 92 | |
| 93 | |
| 94 | # These are imported after the above warning so that users get the correct |
| 95 | # deprecation warning. |
| 96 | import asyncore |
| 97 | import asynchat |
| 98 | |
| 99 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 100 | program = sys.argv[0] |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 101 | __version__ = 'Python SMTP proxy version 0.3' |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 102 | |
| 103 | |
| 104 | class Devnull: |
| 105 | def write(self, msg): pass |
| 106 | def flush(self): pass |
| 107 | |
| 108 | |
| 109 | DEBUGSTREAM = Devnull() |
| 110 | NEWLINE = '\n' |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 111 | COMMASPACE = ', ' |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 112 | DATA_SIZE_DEFAULT = 33554432 |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 113 | |
| 114 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 115 | def usage(code, msg=''): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 116 | print(__doc__ % globals(), file=sys.stderr) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 117 | if msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 118 | print(msg, file=sys.stderr) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 119 | sys.exit(code) |
| 120 | |
| 121 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 122 | class SMTPChannel(asynchat.async_chat): |
| 123 | COMMAND = 0 |
| 124 | DATA = 1 |
| 125 | |
Georg Brandl | 1e5c5f8 | 2010-12-03 07:38:22 +0000 | [diff] [blame] | 126 | command_size_limit = 512 |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 127 | command_size_limits = collections.defaultdict(lambda x=command_size_limit: x) |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 128 | |
| 129 | @property |
| 130 | def max_command_size_limit(self): |
| 131 | try: |
| 132 | return max(self.command_size_limits.values()) |
| 133 | except ValueError: |
| 134 | return self.command_size_limit |
Georg Brandl | 1e5c5f8 | 2010-12-03 07:38:22 +0000 | [diff] [blame] | 135 | |
Vinay Sajip | 30298b4 | 2013-06-07 15:21:41 +0100 | [diff] [blame] | 136 | def __init__(self, server, conn, addr, data_size_limit=DATA_SIZE_DEFAULT, |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame] | 137 | map=None, enable_SMTPUTF8=False, decode_data=False): |
Vinay Sajip | 30298b4 | 2013-06-07 15:21:41 +0100 | [diff] [blame] | 138 | asynchat.async_chat.__init__(self, conn, map=map) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 139 | self.smtp_server = server |
| 140 | self.conn = conn |
| 141 | self.addr = addr |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 142 | self.data_size_limit = data_size_limit |
Serhiy Storchaka | eb6cd74 | 2016-05-29 23:50:56 +0300 | [diff] [blame] | 143 | self.enable_SMTPUTF8 = enable_SMTPUTF8 |
| 144 | self._decode_data = decode_data |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame] | 145 | if enable_SMTPUTF8 and decode_data: |
| 146 | raise ValueError("decode_data and enable_SMTPUTF8 cannot" |
| 147 | " be set to True at the same time") |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 148 | if decode_data: |
| 149 | self._emptystring = '' |
| 150 | self._linesep = '\r\n' |
| 151 | self._dotsep = '.' |
| 152 | self._newline = NEWLINE |
| 153 | else: |
| 154 | self._emptystring = b'' |
| 155 | self._linesep = b'\r\n' |
Serhiy Storchaka | ee4c0b9 | 2015-03-20 16:48:02 +0200 | [diff] [blame] | 156 | self._dotsep = ord(b'.') |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 157 | self._newline = b'\n' |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 158 | self._set_rset_state() |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 159 | self.seen_greeting = '' |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 160 | self.extended_smtp = False |
| 161 | self.command_size_limits.clear() |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 162 | self.fqdn = socket.getfqdn() |
Giampaolo Rodolà | 9cf5ef4 | 2010-08-23 22:28:13 +0000 | [diff] [blame] | 163 | try: |
| 164 | self.peer = conn.getpeername() |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 165 | except OSError as err: |
Giampaolo Rodolà | 9cf5ef4 | 2010-08-23 22:28:13 +0000 | [diff] [blame] | 166 | # a race condition may occur if the other end is closing |
| 167 | # before we can get the peername |
| 168 | self.close() |
Serhiy Storchaka | c4d45ee | 2020-11-22 10:28:34 +0200 | [diff] [blame] | 169 | if err.errno != errno.ENOTCONN: |
Giampaolo Rodolà | 9cf5ef4 | 2010-08-23 22:28:13 +0000 | [diff] [blame] | 170 | raise |
| 171 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 172 | print('Peer:', repr(self.peer), file=DEBUGSTREAM) |
| 173 | self.push('220 %s %s' % (self.fqdn, __version__)) |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 174 | |
| 175 | def _set_post_data_state(self): |
| 176 | """Reset state variables to their post-DATA state.""" |
| 177 | self.smtp_state = self.COMMAND |
| 178 | self.mailfrom = None |
| 179 | self.rcpttos = [] |
| 180 | self.require_SMTPUTF8 = False |
| 181 | self.num_bytes = 0 |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 182 | self.set_terminator(b'\r\n') |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 183 | |
| 184 | def _set_rset_state(self): |
| 185 | """Reset all state variables except the greeting.""" |
| 186 | self._set_post_data_state() |
| 187 | self.received_data = '' |
| 188 | self.received_lines = [] |
| 189 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 190 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 191 | # properties for backwards-compatibility |
| 192 | @property |
| 193 | def __server(self): |
| 194 | warn("Access to __server attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 195 | "use 'smtp_server' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 196 | return self.smtp_server |
| 197 | @__server.setter |
| 198 | def __server(self, value): |
| 199 | warn("Setting __server attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 200 | "set 'smtp_server' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 201 | self.smtp_server = value |
| 202 | |
| 203 | @property |
| 204 | def __line(self): |
| 205 | warn("Access to __line attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 206 | "use 'received_lines' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 207 | return self.received_lines |
| 208 | @__line.setter |
| 209 | def __line(self, value): |
| 210 | warn("Setting __line attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 211 | "set 'received_lines' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 212 | self.received_lines = value |
| 213 | |
| 214 | @property |
| 215 | def __state(self): |
| 216 | warn("Access to __state attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 217 | "use 'smtp_state' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 218 | return self.smtp_state |
| 219 | @__state.setter |
| 220 | def __state(self, value): |
| 221 | warn("Setting __state attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 222 | "set 'smtp_state' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 223 | self.smtp_state = value |
| 224 | |
| 225 | @property |
| 226 | def __greeting(self): |
| 227 | warn("Access to __greeting attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 228 | "use 'seen_greeting' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 229 | return self.seen_greeting |
| 230 | @__greeting.setter |
| 231 | def __greeting(self, value): |
| 232 | warn("Setting __greeting attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 233 | "set 'seen_greeting' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 234 | self.seen_greeting = value |
| 235 | |
| 236 | @property |
| 237 | def __mailfrom(self): |
| 238 | warn("Access to __mailfrom attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 239 | "use 'mailfrom' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 240 | return self.mailfrom |
| 241 | @__mailfrom.setter |
| 242 | def __mailfrom(self, value): |
| 243 | warn("Setting __mailfrom attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 244 | "set 'mailfrom' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 245 | self.mailfrom = value |
| 246 | |
| 247 | @property |
| 248 | def __rcpttos(self): |
| 249 | warn("Access to __rcpttos attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 250 | "use 'rcpttos' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 251 | return self.rcpttos |
| 252 | @__rcpttos.setter |
| 253 | def __rcpttos(self, value): |
| 254 | warn("Setting __rcpttos attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 255 | "set 'rcpttos' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 256 | self.rcpttos = value |
| 257 | |
| 258 | @property |
| 259 | def __data(self): |
| 260 | warn("Access to __data attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 261 | "use 'received_data' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 262 | return self.received_data |
| 263 | @__data.setter |
| 264 | def __data(self, value): |
| 265 | warn("Setting __data attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 266 | "set 'received_data' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 267 | self.received_data = value |
| 268 | |
| 269 | @property |
| 270 | def __fqdn(self): |
| 271 | warn("Access to __fqdn attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 272 | "use 'fqdn' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 273 | return self.fqdn |
| 274 | @__fqdn.setter |
| 275 | def __fqdn(self, value): |
| 276 | warn("Setting __fqdn attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 277 | "set 'fqdn' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 278 | self.fqdn = value |
| 279 | |
| 280 | @property |
| 281 | def __peer(self): |
| 282 | warn("Access to __peer attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 283 | "use 'peer' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 284 | return self.peer |
| 285 | @__peer.setter |
| 286 | def __peer(self, value): |
| 287 | warn("Setting __peer attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 288 | "set 'peer' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 289 | self.peer = value |
| 290 | |
| 291 | @property |
| 292 | def __conn(self): |
| 293 | warn("Access to __conn attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 294 | "use 'conn' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 295 | return self.conn |
| 296 | @__conn.setter |
| 297 | def __conn(self, value): |
| 298 | warn("Setting __conn attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 299 | "set 'conn' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 300 | self.conn = value |
| 301 | |
| 302 | @property |
| 303 | def __addr(self): |
| 304 | warn("Access to __addr attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 305 | "use 'addr' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 306 | return self.addr |
| 307 | @__addr.setter |
| 308 | def __addr(self, value): |
| 309 | warn("Setting __addr attribute on SMTPChannel is deprecated, " |
Florent Xicluna | 6731775 | 2011-12-10 11:07:42 +0100 | [diff] [blame] | 310 | "set 'addr' instead", DeprecationWarning, 2) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 311 | self.addr = value |
| 312 | |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 313 | # Overrides base class for convenience. |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 314 | def push(self, msg): |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 315 | asynchat.async_chat.push(self, bytes( |
| 316 | msg + '\r\n', 'utf-8' if self.require_SMTPUTF8 else 'ascii')) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 317 | |
| 318 | # Implementation of base class abstract method |
| 319 | def collect_incoming_data(self, data): |
Georg Brandl | 1e5c5f8 | 2010-12-03 07:38:22 +0000 | [diff] [blame] | 320 | limit = None |
| 321 | if self.smtp_state == self.COMMAND: |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 322 | limit = self.max_command_size_limit |
Georg Brandl | 1e5c5f8 | 2010-12-03 07:38:22 +0000 | [diff] [blame] | 323 | elif self.smtp_state == self.DATA: |
| 324 | limit = self.data_size_limit |
| 325 | if limit and self.num_bytes > limit: |
| 326 | return |
| 327 | elif limit: |
| 328 | self.num_bytes += len(data) |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 329 | if self._decode_data: |
| 330 | self.received_lines.append(str(data, 'utf-8')) |
| 331 | else: |
| 332 | self.received_lines.append(data) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 333 | |
| 334 | # Implementation of base class abstract method |
| 335 | def found_terminator(self): |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 336 | line = self._emptystring.join(self.received_lines) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 337 | print('Data:', repr(line), file=DEBUGSTREAM) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 338 | self.received_lines = [] |
| 339 | if self.smtp_state == self.COMMAND: |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 340 | sz, self.num_bytes = self.num_bytes, 0 |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 341 | if not line: |
| 342 | self.push('500 Error: bad syntax') |
| 343 | return |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 344 | if not self._decode_data: |
| 345 | line = str(line, 'utf-8') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 346 | i = line.find(' ') |
| 347 | if i < 0: |
| 348 | command = line.upper() |
| 349 | arg = None |
| 350 | else: |
| 351 | command = line[:i].upper() |
| 352 | arg = line[i+1:].strip() |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 353 | max_sz = (self.command_size_limits[command] |
| 354 | if self.extended_smtp else self.command_size_limit) |
| 355 | if sz > max_sz: |
| 356 | self.push('500 Error: line too long') |
| 357 | return |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 358 | method = getattr(self, 'smtp_' + command, None) |
| 359 | if not method: |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 360 | self.push('500 Error: command "%s" not recognized' % command) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 361 | return |
| 362 | method(arg) |
| 363 | return |
| 364 | else: |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 365 | if self.smtp_state != self.DATA: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 366 | self.push('451 Internal confusion') |
Georg Brandl | 1e5c5f8 | 2010-12-03 07:38:22 +0000 | [diff] [blame] | 367 | self.num_bytes = 0 |
| 368 | return |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 369 | if self.data_size_limit and self.num_bytes > self.data_size_limit: |
Georg Brandl | 1e5c5f8 | 2010-12-03 07:38:22 +0000 | [diff] [blame] | 370 | self.push('552 Error: Too much mail data') |
| 371 | self.num_bytes = 0 |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 372 | return |
| 373 | # Remove extraneous carriage returns and de-transparency according |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 374 | # to RFC 5321, Section 4.5.2. |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 375 | data = [] |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 376 | for text in line.split(self._linesep): |
| 377 | if text and text[0] == self._dotsep: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 378 | data.append(text[1:]) |
| 379 | else: |
| 380 | data.append(text) |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 381 | self.received_data = self._newline.join(data) |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 382 | args = (self.peer, self.mailfrom, self.rcpttos, self.received_data) |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 383 | kwargs = {} |
| 384 | if not self._decode_data: |
| 385 | kwargs = { |
| 386 | 'mail_options': self.mail_options, |
| 387 | 'rcpt_options': self.rcpt_options, |
| 388 | } |
| 389 | status = self.smtp_server.process_message(*args, **kwargs) |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 390 | self._set_post_data_state() |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 391 | if not status: |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 392 | self.push('250 OK') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 393 | else: |
| 394 | self.push(status) |
| 395 | |
| 396 | # SMTP and ESMTP commands |
| 397 | def smtp_HELO(self, arg): |
| 398 | if not arg: |
| 399 | self.push('501 Syntax: HELO hostname') |
| 400 | return |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 401 | # See issue #21783 for a discussion of this behavior. |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 402 | if self.seen_greeting: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 403 | self.push('503 Duplicate HELO/EHLO') |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 404 | return |
| 405 | self._set_rset_state() |
| 406 | self.seen_greeting = arg |
| 407 | self.push('250 %s' % self.fqdn) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 408 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 409 | def smtp_EHLO(self, arg): |
| 410 | if not arg: |
| 411 | self.push('501 Syntax: EHLO hostname') |
| 412 | return |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 413 | # See issue #21783 for a discussion of this behavior. |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 414 | if self.seen_greeting: |
| 415 | self.push('503 Duplicate HELO/EHLO') |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 416 | return |
| 417 | self._set_rset_state() |
| 418 | self.seen_greeting = arg |
| 419 | self.extended_smtp = True |
| 420 | self.push('250-%s' % self.fqdn) |
| 421 | if self.data_size_limit: |
| 422 | self.push('250-SIZE %s' % self.data_size_limit) |
| 423 | self.command_size_limits['MAIL'] += 26 |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 424 | if not self._decode_data: |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 425 | self.push('250-8BITMIME') |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 426 | if self.enable_SMTPUTF8: |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 427 | self.push('250-SMTPUTF8') |
| 428 | self.command_size_limits['MAIL'] += 10 |
| 429 | self.push('250 HELP') |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 430 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 431 | def smtp_NOOP(self, arg): |
| 432 | if arg: |
| 433 | self.push('501 Syntax: NOOP') |
| 434 | else: |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 435 | self.push('250 OK') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 436 | |
| 437 | def smtp_QUIT(self, arg): |
| 438 | # args is ignored |
| 439 | self.push('221 Bye') |
| 440 | self.close_when_done() |
| 441 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 442 | def _strip_command_keyword(self, keyword, arg): |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 443 | keylen = len(keyword) |
| 444 | if arg[:keylen].upper() == keyword: |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 445 | return arg[keylen:].strip() |
| 446 | return '' |
| 447 | |
| 448 | def _getaddr(self, arg): |
| 449 | if not arg: |
| 450 | return '', '' |
| 451 | if arg.lstrip().startswith('<'): |
| 452 | address, rest = get_angle_addr(arg) |
| 453 | else: |
| 454 | address, rest = get_addr_spec(arg) |
| 455 | if not address: |
| 456 | return address, rest |
| 457 | return address.addr_spec, rest |
| 458 | |
| 459 | def _getparams(self, params): |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 460 | # Return params as dictionary. Return None if not all parameters |
| 461 | # appear to be syntactically valid according to RFC 1869. |
| 462 | result = {} |
| 463 | for param in params: |
| 464 | param, eq, value = param.partition('=') |
| 465 | if not param.isalnum() or eq and not value: |
| 466 | return None |
| 467 | result[param] = value if eq else True |
| 468 | return result |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 469 | |
| 470 | def smtp_HELP(self, arg): |
| 471 | if arg: |
Benjamin Peterson | 0c80331 | 2015-04-05 10:01:48 -0400 | [diff] [blame] | 472 | extended = ' [SP <mail-parameters>]' |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 473 | lc_arg = arg.upper() |
| 474 | if lc_arg == 'EHLO': |
| 475 | self.push('250 Syntax: EHLO hostname') |
| 476 | elif lc_arg == 'HELO': |
| 477 | self.push('250 Syntax: HELO hostname') |
| 478 | elif lc_arg == 'MAIL': |
| 479 | msg = '250 Syntax: MAIL FROM: <address>' |
| 480 | if self.extended_smtp: |
| 481 | msg += extended |
| 482 | self.push(msg) |
| 483 | elif lc_arg == 'RCPT': |
| 484 | msg = '250 Syntax: RCPT TO: <address>' |
| 485 | if self.extended_smtp: |
| 486 | msg += extended |
| 487 | self.push(msg) |
| 488 | elif lc_arg == 'DATA': |
| 489 | self.push('250 Syntax: DATA') |
| 490 | elif lc_arg == 'RSET': |
| 491 | self.push('250 Syntax: RSET') |
| 492 | elif lc_arg == 'NOOP': |
| 493 | self.push('250 Syntax: NOOP') |
| 494 | elif lc_arg == 'QUIT': |
| 495 | self.push('250 Syntax: QUIT') |
| 496 | elif lc_arg == 'VRFY': |
| 497 | self.push('250 Syntax: VRFY <address>') |
| 498 | else: |
| 499 | self.push('501 Supported commands: EHLO HELO MAIL RCPT ' |
| 500 | 'DATA RSET NOOP QUIT VRFY') |
| 501 | else: |
| 502 | self.push('250 Supported commands: EHLO HELO MAIL RCPT DATA ' |
| 503 | 'RSET NOOP QUIT VRFY') |
| 504 | |
| 505 | def smtp_VRFY(self, arg): |
| 506 | if arg: |
| 507 | address, params = self._getaddr(arg) |
| 508 | if address: |
| 509 | self.push('252 Cannot VRFY user, but will accept message ' |
| 510 | 'and attempt delivery') |
| 511 | else: |
| 512 | self.push('502 Could not VRFY %s' % arg) |
| 513 | else: |
| 514 | self.push('501 Syntax: VRFY <address>') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 515 | |
| 516 | def smtp_MAIL(self, arg): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 517 | if not self.seen_greeting: |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 518 | self.push('503 Error: send HELO first') |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 519 | return |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 520 | print('===> MAIL', arg, file=DEBUGSTREAM) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 521 | syntaxerr = '501 Syntax: MAIL FROM: <address>' |
| 522 | if self.extended_smtp: |
| 523 | syntaxerr += ' [SP <mail-parameters>]' |
| 524 | if arg is None: |
| 525 | self.push(syntaxerr) |
| 526 | return |
| 527 | arg = self._strip_command_keyword('FROM:', arg) |
| 528 | address, params = self._getaddr(arg) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 529 | if not address: |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 530 | self.push(syntaxerr) |
| 531 | return |
| 532 | if not self.extended_smtp and params: |
| 533 | self.push(syntaxerr) |
| 534 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 535 | if self.mailfrom: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 536 | self.push('503 Error: nested MAIL command') |
| 537 | return |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 538 | self.mail_options = params.upper().split() |
| 539 | params = self._getparams(self.mail_options) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 540 | if params is None: |
| 541 | self.push(syntaxerr) |
| 542 | return |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 543 | if not self._decode_data: |
| 544 | body = params.pop('BODY', '7BIT') |
| 545 | if body not in ['7BIT', '8BITMIME']: |
| 546 | self.push('501 Error: BODY can only be one of 7BIT, 8BITMIME') |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 547 | return |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 548 | if self.enable_SMTPUTF8: |
| 549 | smtputf8 = params.pop('SMTPUTF8', False) |
| 550 | if smtputf8 is True: |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 551 | self.require_SMTPUTF8 = True |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 552 | elif smtputf8 is not False: |
| 553 | self.push('501 Error: SMTPUTF8 takes no arguments') |
| 554 | return |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 555 | size = params.pop('SIZE', None) |
| 556 | if size: |
| 557 | if not size.isdigit(): |
| 558 | self.push(syntaxerr) |
| 559 | return |
| 560 | elif self.data_size_limit and int(size) > self.data_size_limit: |
| 561 | self.push('552 Error: message size exceeds fixed maximum message size') |
| 562 | return |
| 563 | if len(params.keys()) > 0: |
| 564 | self.push('555 MAIL FROM parameters not recognized or not implemented') |
| 565 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 566 | self.mailfrom = address |
| 567 | print('sender:', self.mailfrom, file=DEBUGSTREAM) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 568 | self.push('250 OK') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 569 | |
| 570 | def smtp_RCPT(self, arg): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 571 | if not self.seen_greeting: |
| 572 | self.push('503 Error: send HELO first'); |
| 573 | return |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 574 | print('===> RCPT', arg, file=DEBUGSTREAM) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 575 | if not self.mailfrom: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 576 | self.push('503 Error: need MAIL command') |
| 577 | return |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 578 | syntaxerr = '501 Syntax: RCPT TO: <address>' |
| 579 | if self.extended_smtp: |
| 580 | syntaxerr += ' [SP <mail-parameters>]' |
| 581 | if arg is None: |
| 582 | self.push(syntaxerr) |
| 583 | return |
| 584 | arg = self._strip_command_keyword('TO:', arg) |
| 585 | address, params = self._getaddr(arg) |
| 586 | if not address: |
| 587 | self.push(syntaxerr) |
| 588 | return |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 589 | if not self.extended_smtp and params: |
| 590 | self.push(syntaxerr) |
| 591 | return |
| 592 | self.rcpt_options = params.upper().split() |
| 593 | params = self._getparams(self.rcpt_options) |
| 594 | if params is None: |
| 595 | self.push(syntaxerr) |
| 596 | return |
| 597 | # XXX currently there are no options we recognize. |
| 598 | if len(params.keys()) > 0: |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 599 | self.push('555 RCPT TO parameters not recognized or not implemented') |
| 600 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 601 | self.rcpttos.append(address) |
| 602 | print('recips:', self.rcpttos, file=DEBUGSTREAM) |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 603 | self.push('250 OK') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 604 | |
| 605 | def smtp_RSET(self, arg): |
| 606 | if arg: |
| 607 | self.push('501 Syntax: RSET') |
| 608 | return |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 609 | self._set_rset_state() |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 610 | self.push('250 OK') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 611 | |
| 612 | def smtp_DATA(self, arg): |
R David Murray | 669b755 | 2012-03-20 16:16:29 -0400 | [diff] [blame] | 613 | if not self.seen_greeting: |
| 614 | self.push('503 Error: send HELO first'); |
| 615 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 616 | if not self.rcpttos: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 617 | self.push('503 Error: need RCPT command') |
| 618 | return |
| 619 | if arg: |
| 620 | self.push('501 Syntax: DATA') |
| 621 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 622 | self.smtp_state = self.DATA |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 623 | self.set_terminator(b'\r\n.\r\n') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 624 | self.push('354 End data with <CR><LF>.<CR><LF>') |
| 625 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 626 | # Commands that have not been implemented |
| 627 | def smtp_EXPN(self, arg): |
| 628 | self.push('502 EXPN not implemented') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 629 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 630 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 631 | class SMTPServer(asyncore.dispatcher): |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 632 | # SMTPChannel class to use for managing client connections |
| 633 | channel_class = SMTPChannel |
| 634 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 635 | def __init__(self, localaddr, remoteaddr, |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 636 | data_size_limit=DATA_SIZE_DEFAULT, map=None, |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame] | 637 | enable_SMTPUTF8=False, decode_data=False): |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 638 | self._localaddr = localaddr |
| 639 | self._remoteaddr = remoteaddr |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 640 | self.data_size_limit = data_size_limit |
Serhiy Storchaka | eb6cd74 | 2016-05-29 23:50:56 +0300 | [diff] [blame] | 641 | self.enable_SMTPUTF8 = enable_SMTPUTF8 |
| 642 | self._decode_data = decode_data |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame] | 643 | if enable_SMTPUTF8 and decode_data: |
| 644 | raise ValueError("decode_data and enable_SMTPUTF8 cannot" |
| 645 | " be set to True at the same time") |
Vinay Sajip | 30298b4 | 2013-06-07 15:21:41 +0100 | [diff] [blame] | 646 | asyncore.dispatcher.__init__(self, map=map) |
Giampaolo Rodolà | 610aa4f | 2010-06-30 17:47:39 +0000 | [diff] [blame] | 647 | try: |
R David Murray | 012a83a | 2014-06-11 15:17:50 -0400 | [diff] [blame] | 648 | gai_results = socket.getaddrinfo(*localaddr, |
| 649 | type=socket.SOCK_STREAM) |
R David Murray | 6fe56a3 | 2014-06-11 13:48:58 -0400 | [diff] [blame] | 650 | self.create_socket(gai_results[0][0], gai_results[0][1]) |
Giampaolo Rodolà | 610aa4f | 2010-06-30 17:47:39 +0000 | [diff] [blame] | 651 | # try to re-use a server port if possible |
| 652 | self.set_reuse_addr() |
| 653 | self.bind(localaddr) |
| 654 | self.listen(5) |
| 655 | except: |
| 656 | self.close() |
| 657 | raise |
| 658 | else: |
| 659 | print('%s started at %s\n\tLocal addr: %s\n\tRemote addr:%s' % ( |
| 660 | self.__class__.__name__, time.ctime(time.time()), |
| 661 | localaddr, remoteaddr), file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 662 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 663 | def handle_accepted(self, conn, addr): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 664 | print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM) |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 665 | channel = self.channel_class(self, |
| 666 | conn, |
| 667 | addr, |
| 668 | self.data_size_limit, |
| 669 | self._map, |
| 670 | self.enable_SMTPUTF8, |
| 671 | self._decode_data) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 672 | |
| 673 | # API for "doing something useful with the message" |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 674 | def process_message(self, peer, mailfrom, rcpttos, data, **kwargs): |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 675 | """Override this abstract method to handle messages from the client. |
| 676 | |
| 677 | peer is a tuple containing (ipaddr, port) of the client that made the |
| 678 | socket connection to our smtp port. |
| 679 | |
| 680 | mailfrom is the raw address the client claims the message is coming |
| 681 | from. |
| 682 | |
| 683 | rcpttos is a list of raw addresses the client wishes to deliver the |
| 684 | message to. |
| 685 | |
| 686 | data is a string containing the entire full text of the message, |
| 687 | headers (if supplied) and all. It has been `de-transparencied' |
| 688 | according to RFC 821, Section 4.5.2. In other words, a line |
| 689 | containing a `.' followed by other text has had the leading dot |
| 690 | removed. |
| 691 | |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame] | 692 | kwargs is a dictionary containing additional information. It is |
| 693 | empty if decode_data=True was given as init parameter, otherwise |
| 694 | it will contain the following keys: |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 695 | 'mail_options': list of parameters to the mail command. All |
| 696 | elements are uppercase strings. Example: |
| 697 | ['BODY=8BITMIME', 'SMTPUTF8']. |
| 698 | 'rcpt_options': same, for the rcpt command. |
| 699 | |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 700 | This function should return None for a normal `250 Ok' response; |
| 701 | otherwise, it should return the desired response string in RFC 821 |
| 702 | format. |
| 703 | |
| 704 | """ |
| 705 | raise NotImplementedError |
| 706 | |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 707 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 708 | class DebuggingServer(SMTPServer): |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 709 | |
| 710 | def _print_message_content(self, peer, data): |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 711 | inheaders = 1 |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 712 | lines = data.splitlines() |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 713 | for line in lines: |
| 714 | # headers first |
| 715 | if inheaders and not line: |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 716 | peerheader = 'X-Peer: ' + peer[0] |
| 717 | if not isinstance(data, str): |
| 718 | # decoded_data=false; make header match other binary output |
| 719 | peerheader = repr(peerheader.encode('utf-8')) |
| 720 | print(peerheader) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 721 | inheaders = 0 |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 722 | if not isinstance(data, str): |
| 723 | # Avoid spurious 'str on bytes instance' warning. |
| 724 | line = repr(line) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 725 | print(line) |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 726 | |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 727 | def process_message(self, peer, mailfrom, rcpttos, data, **kwargs): |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 728 | print('---------- MESSAGE FOLLOWS ----------') |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 729 | if kwargs: |
| 730 | if kwargs.get('mail_options'): |
| 731 | print('mail options: %s' % kwargs['mail_options']) |
| 732 | if kwargs.get('rcpt_options'): |
| 733 | print('rcpt options: %s\n' % kwargs['rcpt_options']) |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 734 | self._print_message_content(peer, data) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 735 | print('------------ END MESSAGE ------------') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 736 | |
| 737 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 738 | class PureProxy(SMTPServer): |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 739 | def __init__(self, *args, **kwargs): |
| 740 | if 'enable_SMTPUTF8' in kwargs and kwargs['enable_SMTPUTF8']: |
| 741 | raise ValueError("PureProxy does not support SMTPUTF8.") |
| 742 | super(PureProxy, self).__init__(*args, **kwargs) |
| 743 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 744 | def process_message(self, peer, mailfrom, rcpttos, data): |
| 745 | lines = data.split('\n') |
| 746 | # Look for the last header |
| 747 | i = 0 |
| 748 | for line in lines: |
| 749 | if not line: |
| 750 | break |
| 751 | i += 1 |
| 752 | lines.insert(i, 'X-Peer: %s' % peer[0]) |
| 753 | data = NEWLINE.join(lines) |
| 754 | refused = self._deliver(mailfrom, rcpttos, data) |
| 755 | # TBD: what to do with refused addresses? |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 756 | print('we got some refusals:', refused, file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 757 | |
| 758 | def _deliver(self, mailfrom, rcpttos, data): |
| 759 | import smtplib |
| 760 | refused = {} |
| 761 | try: |
| 762 | s = smtplib.SMTP() |
| 763 | s.connect(self._remoteaddr[0], self._remoteaddr[1]) |
| 764 | try: |
| 765 | refused = s.sendmail(mailfrom, rcpttos, data) |
| 766 | finally: |
| 767 | s.quit() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 768 | except smtplib.SMTPRecipientsRefused as e: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 769 | print('got SMTPRecipientsRefused', file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 770 | refused = e.recipients |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 771 | except (OSError, smtplib.SMTPException) as e: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 772 | print('got', e.__class__, file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 773 | # All recipients were refused. If the exception had an associated |
| 774 | # error code, use it. Otherwise,fake it with a non-triggering |
| 775 | # exception code. |
| 776 | errcode = getattr(e, 'smtp_code', -1) |
| 777 | errmsg = getattr(e, 'smtp_error', 'ignore') |
| 778 | for r in rcpttos: |
| 779 | refused[r] = (errcode, errmsg) |
| 780 | return refused |
| 781 | |
| 782 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 783 | class Options: |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 784 | setuid = True |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 785 | classname = 'PureProxy' |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 786 | size_limit = None |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 787 | enable_SMTPUTF8 = False |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 788 | |
| 789 | |
| 790 | def parseargs(): |
| 791 | global DEBUGSTREAM |
| 792 | try: |
| 793 | opts, args = getopt.getopt( |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 794 | sys.argv[1:], 'nVhc:s:du', |
| 795 | ['class=', 'nosetuid', 'version', 'help', 'size=', 'debug', |
| 796 | 'smtputf8']) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 797 | except getopt.error as e: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 798 | usage(1, e) |
| 799 | |
| 800 | options = Options() |
| 801 | for opt, arg in opts: |
| 802 | if opt in ('-h', '--help'): |
| 803 | usage(0) |
| 804 | elif opt in ('-V', '--version'): |
Serhiy Storchaka | c56894d | 2013-09-05 17:44:53 +0300 | [diff] [blame] | 805 | print(__version__) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 806 | sys.exit(0) |
| 807 | elif opt in ('-n', '--nosetuid'): |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 808 | options.setuid = False |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 809 | elif opt in ('-c', '--class'): |
| 810 | options.classname = arg |
| 811 | elif opt in ('-d', '--debug'): |
| 812 | DEBUGSTREAM = sys.stderr |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 813 | elif opt in ('-u', '--smtputf8'): |
| 814 | options.enable_SMTPUTF8 = True |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 815 | elif opt in ('-s', '--size'): |
| 816 | try: |
| 817 | int_size = int(arg) |
| 818 | options.size_limit = int_size |
| 819 | except: |
| 820 | print('Invalid size: ' + arg, file=sys.stderr) |
| 821 | sys.exit(1) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 822 | |
| 823 | # parse the rest of the arguments |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 824 | if len(args) < 1: |
| 825 | localspec = 'localhost:8025' |
| 826 | remotespec = 'localhost:25' |
| 827 | elif len(args) < 2: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 828 | localspec = args[0] |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 829 | remotespec = 'localhost:25' |
Barry Warsaw | ebf5427 | 2001-11-04 03:04:25 +0000 | [diff] [blame] | 830 | elif len(args) < 3: |
| 831 | localspec = args[0] |
| 832 | remotespec = args[1] |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 833 | else: |
| 834 | usage(1, 'Invalid arguments: %s' % COMMASPACE.join(args)) |
| 835 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 836 | # split into host/port pairs |
| 837 | i = localspec.find(':') |
| 838 | if i < 0: |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 839 | usage(1, 'Bad local spec: %s' % localspec) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 840 | options.localhost = localspec[:i] |
| 841 | try: |
| 842 | options.localport = int(localspec[i+1:]) |
| 843 | except ValueError: |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 844 | usage(1, 'Bad local port: %s' % localspec) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 845 | i = remotespec.find(':') |
| 846 | if i < 0: |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 847 | usage(1, 'Bad remote spec: %s' % remotespec) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 848 | options.remotehost = remotespec[:i] |
| 849 | try: |
| 850 | options.remoteport = int(remotespec[i+1:]) |
| 851 | except ValueError: |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 852 | usage(1, 'Bad remote port: %s' % remotespec) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 853 | return options |
| 854 | |
| 855 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 856 | if __name__ == '__main__': |
| 857 | options = parseargs() |
| 858 | # Become nobody |
Florent Xicluna | 711f87c | 2011-10-20 23:03:43 +0200 | [diff] [blame] | 859 | classname = options.classname |
| 860 | if "." in classname: |
| 861 | lastdot = classname.rfind(".") |
| 862 | mod = __import__(classname[:lastdot], globals(), locals(), [""]) |
| 863 | classname = classname[lastdot+1:] |
| 864 | else: |
| 865 | import __main__ as mod |
| 866 | class_ = getattr(mod, classname) |
| 867 | proxy = class_((options.localhost, options.localport), |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 868 | (options.remotehost, options.remoteport), |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 869 | options.size_limit, enable_SMTPUTF8=options.enable_SMTPUTF8) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 870 | if options.setuid: |
| 871 | try: |
| 872 | import pwd |
Brett Cannon | cd171c8 | 2013-07-04 17:43:24 -0400 | [diff] [blame] | 873 | except ImportError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 874 | print('Cannot import module "pwd"; try running with -n option.', file=sys.stderr) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 875 | sys.exit(1) |
| 876 | nobody = pwd.getpwnam('nobody')[2] |
| 877 | try: |
| 878 | os.setuid(nobody) |
Giampaolo Rodola' | 0166a28 | 2013-02-12 15:14:17 +0100 | [diff] [blame] | 879 | except PermissionError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 880 | print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 881 | sys.exit(1) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 882 | try: |
| 883 | asyncore.loop() |
| 884 | except KeyboardInterrupt: |
| 885 | pass |