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