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