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