Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Barry Warsaw | 406d46e | 2001-08-13 21:18:01 +0000 | [diff] [blame] | 2 | """An RFC 2821 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 | |
| 23 | --debug |
| 24 | -d |
| 25 | Turn on debugging prints. |
| 26 | |
| 27 | --help |
| 28 | -h |
| 29 | Print this message and exit. |
| 30 | |
| 31 | Version: %(__version__)s |
| 32 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 33 | If localhost is not given then `localhost' is used, and if localport is not |
| 34 | given then 8025 is used. If remotehost is not given then `localhost' is used, |
| 35 | and if remoteport is not given, then 25 is used. |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 36 | """ |
| 37 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 38 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 39 | # Overview: |
| 40 | # |
| 41 | # This file implements the minimal SMTP protocol as defined in RFC 821. It |
| 42 | # has a hierarchy of classes which implement the backend functionality for the |
| 43 | # smtpd. A number of classes are provided: |
| 44 | # |
Guido van Rossum | b8b45ea | 2001-04-15 13:06:04 +0000 | [diff] [blame] | 45 | # SMTPServer - the base class for the backend. Raises NotImplementedError |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 46 | # if you try to use it. |
| 47 | # |
| 48 | # DebuggingServer - simply prints each message it receives on stdout. |
| 49 | # |
| 50 | # PureProxy - Proxies all messages to a real smtpd which does final |
| 51 | # delivery. One known problem with this class is that it doesn't handle |
| 52 | # SMTP errors from the backend server at all. This should be fixed |
| 53 | # (contributions are welcome!). |
| 54 | # |
| 55 | # MailmanProxy - An experimental hack to work with GNU Mailman |
| 56 | # <www.list.org>. Using this server as your real incoming smtpd, your |
| 57 | # mailhost will automatically recognize and accept mail destined to Mailman |
| 58 | # lists when those lists are created. Every message not destined for a list |
| 59 | # gets forwarded to a real backend smtpd, as with PureProxy. Again, errors |
| 60 | # are not handled correctly yet. |
| 61 | # |
| 62 | # Please note that this script requires Python 2.0 |
| 63 | # |
Barry Warsaw | b102764 | 2004-07-12 23:10:08 +0000 | [diff] [blame] | 64 | # Author: Barry Warsaw <barry@python.org> |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 65 | # |
| 66 | # TODO: |
| 67 | # |
| 68 | # - support mailbox delivery |
| 69 | # - alias files |
| 70 | # - ESMTP |
| 71 | # - handle error codes from the backend smtpd |
| 72 | |
| 73 | import sys |
| 74 | import os |
| 75 | import errno |
| 76 | import getopt |
| 77 | import time |
| 78 | import socket |
| 79 | import asyncore |
| 80 | import asynchat |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 81 | from warnings import warn |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 82 | |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 83 | __all__ = ["SMTPServer","DebuggingServer","PureProxy","MailmanProxy"] |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 84 | |
| 85 | program = sys.argv[0] |
| 86 | __version__ = 'Python SMTP proxy version 0.2' |
| 87 | |
| 88 | |
| 89 | class Devnull: |
| 90 | def write(self, msg): pass |
| 91 | def flush(self): pass |
| 92 | |
| 93 | |
| 94 | DEBUGSTREAM = Devnull() |
| 95 | NEWLINE = '\n' |
| 96 | EMPTYSTRING = '' |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 97 | COMMASPACE = ', ' |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 98 | |
| 99 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 100 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 101 | def usage(code, msg=''): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 102 | print(__doc__ % globals(), file=sys.stderr) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 103 | if msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 104 | print(msg, file=sys.stderr) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 105 | sys.exit(code) |
| 106 | |
| 107 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 108 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 109 | class SMTPChannel(asynchat.async_chat): |
| 110 | COMMAND = 0 |
| 111 | DATA = 1 |
| 112 | |
| 113 | def __init__(self, server, conn, addr): |
| 114 | asynchat.async_chat.__init__(self, conn) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 115 | self.smtp_server = server |
| 116 | self.conn = conn |
| 117 | self.addr = addr |
| 118 | self.received_lines = [] |
| 119 | self.smtp_state = self.COMMAND |
| 120 | self.seen_greeting = '' |
| 121 | self.mailfrom = None |
| 122 | self.rcpttos = [] |
| 123 | self.received_data = '' |
| 124 | self.fqdn = socket.getfqdn() |
| 125 | self.peer = conn.getpeername() |
| 126 | print('Peer:', repr(self.peer), file=DEBUGSTREAM) |
| 127 | self.push('220 %s %s' % (self.fqdn, __version__)) |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 128 | self.set_terminator(b'\r\n') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 129 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 130 | # properties for backwards-compatibility |
| 131 | @property |
| 132 | def __server(self): |
| 133 | warn("Access to __server attribute on SMTPChannel is deprecated, " |
| 134 | "use 'smtp_server' instead", PendingDeprecationWarning, 2) |
| 135 | return self.smtp_server |
| 136 | @__server.setter |
| 137 | def __server(self, value): |
| 138 | warn("Setting __server attribute on SMTPChannel is deprecated, " |
| 139 | "set 'smtp_server' instead", PendingDeprecationWarning, 2) |
| 140 | self.smtp_server = value |
| 141 | |
| 142 | @property |
| 143 | def __line(self): |
| 144 | warn("Access to __line attribute on SMTPChannel is deprecated, " |
| 145 | "use 'received_lines' instead", PendingDeprecationWarning, 2) |
| 146 | return self.received_lines |
| 147 | @__line.setter |
| 148 | def __line(self, value): |
| 149 | warn("Setting __line attribute on SMTPChannel is deprecated, " |
| 150 | "set 'received_lines' instead", PendingDeprecationWarning, 2) |
| 151 | self.received_lines = value |
| 152 | |
| 153 | @property |
| 154 | def __state(self): |
| 155 | warn("Access to __state attribute on SMTPChannel is deprecated, " |
| 156 | "use 'smtp_state' instead", PendingDeprecationWarning, 2) |
| 157 | return self.smtp_state |
| 158 | @__state.setter |
| 159 | def __state(self, value): |
| 160 | warn("Setting __state attribute on SMTPChannel is deprecated, " |
| 161 | "set 'smtp_state' instead", PendingDeprecationWarning, 2) |
| 162 | self.smtp_state = value |
| 163 | |
| 164 | @property |
| 165 | def __greeting(self): |
| 166 | warn("Access to __greeting attribute on SMTPChannel is deprecated, " |
| 167 | "use 'seen_greeting' instead", PendingDeprecationWarning, 2) |
| 168 | return self.seen_greeting |
| 169 | @__greeting.setter |
| 170 | def __greeting(self, value): |
| 171 | warn("Setting __greeting attribute on SMTPChannel is deprecated, " |
| 172 | "set 'seen_greeting' instead", PendingDeprecationWarning, 2) |
| 173 | self.seen_greeting = value |
| 174 | |
| 175 | @property |
| 176 | def __mailfrom(self): |
| 177 | warn("Access to __mailfrom attribute on SMTPChannel is deprecated, " |
| 178 | "use 'mailfrom' instead", PendingDeprecationWarning, 2) |
| 179 | return self.mailfrom |
| 180 | @__mailfrom.setter |
| 181 | def __mailfrom(self, value): |
| 182 | warn("Setting __mailfrom attribute on SMTPChannel is deprecated, " |
| 183 | "set 'mailfrom' instead", PendingDeprecationWarning, 2) |
| 184 | self.mailfrom = value |
| 185 | |
| 186 | @property |
| 187 | def __rcpttos(self): |
| 188 | warn("Access to __rcpttos attribute on SMTPChannel is deprecated, " |
| 189 | "use 'rcpttos' instead", PendingDeprecationWarning, 2) |
| 190 | return self.rcpttos |
| 191 | @__rcpttos.setter |
| 192 | def __rcpttos(self, value): |
| 193 | warn("Setting __rcpttos attribute on SMTPChannel is deprecated, " |
| 194 | "set 'rcpttos' instead", PendingDeprecationWarning, 2) |
| 195 | self.rcpttos = value |
| 196 | |
| 197 | @property |
| 198 | def __data(self): |
| 199 | warn("Access to __data attribute on SMTPChannel is deprecated, " |
| 200 | "use 'received_data' instead", PendingDeprecationWarning, 2) |
| 201 | return self.received_data |
| 202 | @__data.setter |
| 203 | def __data(self, value): |
| 204 | warn("Setting __data attribute on SMTPChannel is deprecated, " |
| 205 | "set 'received_data' instead", PendingDeprecationWarning, 2) |
| 206 | self.received_data = value |
| 207 | |
| 208 | @property |
| 209 | def __fqdn(self): |
| 210 | warn("Access to __fqdn attribute on SMTPChannel is deprecated, " |
| 211 | "use 'fqdn' instead", PendingDeprecationWarning, 2) |
| 212 | return self.fqdn |
| 213 | @__fqdn.setter |
| 214 | def __fqdn(self, value): |
| 215 | warn("Setting __fqdn attribute on SMTPChannel is deprecated, " |
| 216 | "set 'fqdn' instead", PendingDeprecationWarning, 2) |
| 217 | self.fqdn = value |
| 218 | |
| 219 | @property |
| 220 | def __peer(self): |
| 221 | warn("Access to __peer attribute on SMTPChannel is deprecated, " |
| 222 | "use 'peer' instead", PendingDeprecationWarning, 2) |
| 223 | return self.peer |
| 224 | @__peer.setter |
| 225 | def __peer(self, value): |
| 226 | warn("Setting __peer attribute on SMTPChannel is deprecated, " |
| 227 | "set 'peer' instead", PendingDeprecationWarning, 2) |
| 228 | self.peer = value |
| 229 | |
| 230 | @property |
| 231 | def __conn(self): |
| 232 | warn("Access to __conn attribute on SMTPChannel is deprecated, " |
| 233 | "use 'conn' instead", PendingDeprecationWarning, 2) |
| 234 | return self.conn |
| 235 | @__conn.setter |
| 236 | def __conn(self, value): |
| 237 | warn("Setting __conn attribute on SMTPChannel is deprecated, " |
| 238 | "set 'conn' instead", PendingDeprecationWarning, 2) |
| 239 | self.conn = value |
| 240 | |
| 241 | @property |
| 242 | def __addr(self): |
| 243 | warn("Access to __addr attribute on SMTPChannel is deprecated, " |
| 244 | "use 'addr' instead", PendingDeprecationWarning, 2) |
| 245 | return self.addr |
| 246 | @__addr.setter |
| 247 | def __addr(self, value): |
| 248 | warn("Setting __addr attribute on SMTPChannel is deprecated, " |
| 249 | "set 'addr' instead", PendingDeprecationWarning, 2) |
| 250 | self.addr = value |
| 251 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 252 | # Overrides base class for convenience |
| 253 | def push(self, msg): |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 254 | asynchat.async_chat.push(self, bytes(msg + '\r\n', 'ascii')) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 255 | |
| 256 | # Implementation of base class abstract method |
| 257 | def collect_incoming_data(self, data): |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 258 | self.received_lines.append(str(data, "utf8")) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 259 | |
| 260 | # Implementation of base class abstract method |
| 261 | def found_terminator(self): |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 262 | line = EMPTYSTRING.join(self.received_lines) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 263 | print('Data:', repr(line), file=DEBUGSTREAM) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 264 | self.received_lines = [] |
| 265 | if self.smtp_state == self.COMMAND: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 266 | if not line: |
| 267 | self.push('500 Error: bad syntax') |
| 268 | return |
| 269 | method = None |
| 270 | i = line.find(' ') |
| 271 | if i < 0: |
| 272 | command = line.upper() |
| 273 | arg = None |
| 274 | else: |
| 275 | command = line[:i].upper() |
| 276 | arg = line[i+1:].strip() |
| 277 | method = getattr(self, 'smtp_' + command, None) |
| 278 | if not method: |
| 279 | self.push('502 Error: command "%s" not implemented' % command) |
| 280 | return |
| 281 | method(arg) |
| 282 | return |
| 283 | else: |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 284 | if self.smtp_state != self.DATA: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 285 | self.push('451 Internal confusion') |
| 286 | return |
| 287 | # Remove extraneous carriage returns and de-transparency according |
| 288 | # to RFC 821, Section 4.5.2. |
| 289 | data = [] |
| 290 | for text in line.split('\r\n'): |
| 291 | if text and text[0] == '.': |
| 292 | data.append(text[1:]) |
| 293 | else: |
| 294 | data.append(text) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 295 | self.received_data = NEWLINE.join(data) |
Georg Brandl | 17e3d69 | 2010-07-31 10:08:09 +0000 | [diff] [blame] | 296 | status = self.smtp_server.process_message(self.peer, |
| 297 | self.mailfrom, |
| 298 | self.rcpttos, |
| 299 | self.received_data) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 300 | self.rcpttos = [] |
| 301 | self.mailfrom = None |
| 302 | self.smtp_state = self.COMMAND |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 303 | self.set_terminator(b'\r\n') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 304 | if not status: |
| 305 | self.push('250 Ok') |
| 306 | else: |
| 307 | self.push(status) |
| 308 | |
| 309 | # SMTP and ESMTP commands |
| 310 | def smtp_HELO(self, arg): |
| 311 | if not arg: |
| 312 | self.push('501 Syntax: HELO hostname') |
| 313 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 314 | if self.seen_greeting: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 315 | self.push('503 Duplicate HELO/EHLO') |
| 316 | else: |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 317 | self.seen_greeting = arg |
| 318 | self.push('250 %s' % self.fqdn) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 319 | |
| 320 | def smtp_NOOP(self, arg): |
| 321 | if arg: |
| 322 | self.push('501 Syntax: NOOP') |
| 323 | else: |
| 324 | self.push('250 Ok') |
| 325 | |
| 326 | def smtp_QUIT(self, arg): |
| 327 | # args is ignored |
| 328 | self.push('221 Bye') |
| 329 | self.close_when_done() |
| 330 | |
| 331 | # factored |
| 332 | def __getaddr(self, keyword, arg): |
| 333 | address = None |
| 334 | keylen = len(keyword) |
| 335 | if arg[:keylen].upper() == keyword: |
| 336 | address = arg[keylen:].strip() |
Barry Warsaw | ebf5427 | 2001-11-04 03:04:25 +0000 | [diff] [blame] | 337 | if not address: |
| 338 | pass |
| 339 | elif address[0] == '<' and address[-1] == '>' and address != '<>': |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 340 | # Addresses can be in the form <person@dom.com> but watch out |
| 341 | # for null address, e.g. <> |
| 342 | address = address[1:-1] |
| 343 | return address |
| 344 | |
| 345 | def smtp_MAIL(self, arg): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 346 | print('===> MAIL', arg, file=DEBUGSTREAM) |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 347 | address = self.__getaddr('FROM:', arg) if arg else None |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 348 | if not address: |
| 349 | self.push('501 Syntax: MAIL FROM:<address>') |
| 350 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 351 | if self.mailfrom: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 352 | self.push('503 Error: nested MAIL command') |
| 353 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 354 | self.mailfrom = address |
| 355 | print('sender:', self.mailfrom, file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 356 | self.push('250 Ok') |
| 357 | |
| 358 | def smtp_RCPT(self, arg): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 359 | print('===> RCPT', arg, file=DEBUGSTREAM) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 360 | if not self.mailfrom: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 361 | self.push('503 Error: need MAIL command') |
| 362 | return |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 363 | address = self.__getaddr('TO:', arg) if arg else None |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 364 | if not address: |
| 365 | self.push('501 Syntax: RCPT TO: <address>') |
| 366 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 367 | self.rcpttos.append(address) |
| 368 | print('recips:', self.rcpttos, file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 369 | self.push('250 Ok') |
| 370 | |
| 371 | def smtp_RSET(self, arg): |
| 372 | if arg: |
| 373 | self.push('501 Syntax: RSET') |
| 374 | return |
| 375 | # Resets the sender, recipients, and data, but not the greeting |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 376 | self.mailfrom = None |
| 377 | self.rcpttos = [] |
| 378 | self.received_data = '' |
| 379 | self.smtp_state = self.COMMAND |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 380 | self.push('250 Ok') |
| 381 | |
| 382 | def smtp_DATA(self, arg): |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 383 | if not self.rcpttos: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 384 | self.push('503 Error: need RCPT command') |
| 385 | return |
| 386 | if arg: |
| 387 | self.push('501 Syntax: DATA') |
| 388 | return |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 389 | self.smtp_state = self.DATA |
Josiah Carlson | d74900e | 2008-07-07 04:15:08 +0000 | [diff] [blame] | 390 | self.set_terminator(b'\r\n.\r\n') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 391 | self.push('354 End data with <CR><LF>.<CR><LF>') |
| 392 | |
| 393 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 394 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 395 | class SMTPServer(asyncore.dispatcher): |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 396 | # SMTPChannel class to use for managing client connections |
| 397 | channel_class = SMTPChannel |
| 398 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 399 | def __init__(self, localaddr, remoteaddr): |
| 400 | self._localaddr = localaddr |
| 401 | self._remoteaddr = remoteaddr |
| 402 | asyncore.dispatcher.__init__(self) |
Giampaolo Rodolà | 610aa4f | 2010-06-30 17:47:39 +0000 | [diff] [blame] | 403 | try: |
| 404 | self.create_socket(socket.AF_INET, socket.SOCK_STREAM) |
| 405 | # try to re-use a server port if possible |
| 406 | self.set_reuse_addr() |
| 407 | self.bind(localaddr) |
| 408 | self.listen(5) |
| 409 | except: |
| 410 | self.close() |
| 411 | raise |
| 412 | else: |
| 413 | print('%s started at %s\n\tLocal addr: %s\n\tRemote addr:%s' % ( |
| 414 | self.__class__.__name__, time.ctime(time.time()), |
| 415 | localaddr, remoteaddr), file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 416 | |
| 417 | def handle_accept(self): |
| 418 | conn, addr = self.accept() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 419 | print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 420 | channel = self.channel_class(self, conn, addr) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 421 | |
| 422 | # API for "doing something useful with the message" |
| 423 | def process_message(self, peer, mailfrom, rcpttos, data): |
| 424 | """Override this abstract method to handle messages from the client. |
| 425 | |
| 426 | peer is a tuple containing (ipaddr, port) of the client that made the |
| 427 | socket connection to our smtp port. |
| 428 | |
| 429 | mailfrom is the raw address the client claims the message is coming |
| 430 | from. |
| 431 | |
| 432 | rcpttos is a list of raw addresses the client wishes to deliver the |
| 433 | message to. |
| 434 | |
| 435 | data is a string containing the entire full text of the message, |
| 436 | headers (if supplied) and all. It has been `de-transparencied' |
| 437 | according to RFC 821, Section 4.5.2. In other words, a line |
| 438 | containing a `.' followed by other text has had the leading dot |
| 439 | removed. |
| 440 | |
| 441 | This function should return None, for a normal `250 Ok' response; |
| 442 | otherwise it returns the desired response string in RFC 821 format. |
| 443 | |
| 444 | """ |
Guido van Rossum | b8b45ea | 2001-04-15 13:06:04 +0000 | [diff] [blame] | 445 | raise NotImplementedError |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 446 | |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 447 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 448 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 449 | class DebuggingServer(SMTPServer): |
| 450 | # Do something with the gathered message |
| 451 | def process_message(self, peer, mailfrom, rcpttos, data): |
| 452 | inheaders = 1 |
| 453 | lines = data.split('\n') |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 454 | print('---------- MESSAGE FOLLOWS ----------') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 455 | for line in lines: |
| 456 | # headers first |
| 457 | if inheaders and not line: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 458 | print('X-Peer:', peer[0]) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 459 | inheaders = 0 |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 460 | print(line) |
| 461 | print('------------ END MESSAGE ------------') |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 462 | |
| 463 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 464 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 465 | class PureProxy(SMTPServer): |
| 466 | def process_message(self, peer, mailfrom, rcpttos, data): |
| 467 | lines = data.split('\n') |
| 468 | # Look for the last header |
| 469 | i = 0 |
| 470 | for line in lines: |
| 471 | if not line: |
| 472 | break |
| 473 | i += 1 |
| 474 | lines.insert(i, 'X-Peer: %s' % peer[0]) |
| 475 | data = NEWLINE.join(lines) |
| 476 | refused = self._deliver(mailfrom, rcpttos, data) |
| 477 | # TBD: what to do with refused addresses? |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 478 | print('we got some refusals:', refused, file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 479 | |
| 480 | def _deliver(self, mailfrom, rcpttos, data): |
| 481 | import smtplib |
| 482 | refused = {} |
| 483 | try: |
| 484 | s = smtplib.SMTP() |
| 485 | s.connect(self._remoteaddr[0], self._remoteaddr[1]) |
| 486 | try: |
| 487 | refused = s.sendmail(mailfrom, rcpttos, data) |
| 488 | finally: |
| 489 | s.quit() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 490 | except smtplib.SMTPRecipientsRefused as e: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 491 | print('got SMTPRecipientsRefused', file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 492 | refused = e.recipients |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 493 | except (socket.error, smtplib.SMTPException) as e: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 494 | print('got', e.__class__, file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 495 | # All recipients were refused. If the exception had an associated |
| 496 | # error code, use it. Otherwise,fake it with a non-triggering |
| 497 | # exception code. |
| 498 | errcode = getattr(e, 'smtp_code', -1) |
| 499 | errmsg = getattr(e, 'smtp_error', 'ignore') |
| 500 | for r in rcpttos: |
| 501 | refused[r] = (errcode, errmsg) |
| 502 | return refused |
| 503 | |
| 504 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 505 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 506 | class MailmanProxy(PureProxy): |
| 507 | def process_message(self, peer, mailfrom, rcpttos, data): |
Guido van Rossum | 68937b4 | 2007-05-18 00:51:22 +0000 | [diff] [blame] | 508 | from io import StringIO |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 509 | from Mailman import Utils |
| 510 | from Mailman import Message |
| 511 | from Mailman import MailList |
| 512 | # If the message is to a Mailman mailing list, then we'll invoke the |
| 513 | # Mailman script directly, without going through the real smtpd. |
| 514 | # Otherwise we'll forward it to the local proxy for disposition. |
| 515 | listnames = [] |
| 516 | for rcpt in rcpttos: |
| 517 | local = rcpt.lower().split('@')[0] |
| 518 | # We allow the following variations on the theme |
| 519 | # listname |
| 520 | # listname-admin |
| 521 | # listname-owner |
| 522 | # listname-request |
| 523 | # listname-join |
| 524 | # listname-leave |
| 525 | parts = local.split('-') |
| 526 | if len(parts) > 2: |
| 527 | continue |
| 528 | listname = parts[0] |
| 529 | if len(parts) == 2: |
| 530 | command = parts[1] |
| 531 | else: |
| 532 | command = '' |
| 533 | if not Utils.list_exists(listname) or command not in ( |
| 534 | '', 'admin', 'owner', 'request', 'join', 'leave'): |
| 535 | continue |
| 536 | listnames.append((rcpt, listname, command)) |
| 537 | # Remove all list recipients from rcpttos and forward what we're not |
| 538 | # going to take care of ourselves. Linear removal should be fine |
| 539 | # since we don't expect a large number of recipients. |
| 540 | for rcpt, listname, command in listnames: |
| 541 | rcpttos.remove(rcpt) |
Tim Peters | 658cba6 | 2001-02-09 20:06:00 +0000 | [diff] [blame] | 542 | # If there's any non-list destined recipients left, |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 543 | print('forwarding recips:', ' '.join(rcpttos), file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 544 | if rcpttos: |
| 545 | refused = self._deliver(mailfrom, rcpttos, data) |
| 546 | # TBD: what to do with refused addresses? |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 547 | print('we got refusals:', refused, file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 548 | # Now deliver directly to the list commands |
| 549 | mlists = {} |
| 550 | s = StringIO(data) |
| 551 | msg = Message.Message(s) |
| 552 | # These headers are required for the proper execution of Mailman. All |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 553 | # 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] | 554 | # have them. |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 555 | if not msg.get('from'): |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 556 | msg['From'] = mailfrom |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 557 | if not msg.get('date'): |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 558 | msg['Date'] = time.ctime(time.time()) |
| 559 | for rcpt, listname, command in listnames: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 560 | print('sending message to', rcpt, file=DEBUGSTREAM) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 561 | mlist = mlists.get(listname) |
| 562 | if not mlist: |
| 563 | mlist = MailList.MailList(listname, lock=0) |
| 564 | mlists[listname] = mlist |
| 565 | # dispatch on the type of command |
| 566 | if command == '': |
| 567 | # post |
| 568 | msg.Enqueue(mlist, tolist=1) |
| 569 | elif command == 'admin': |
| 570 | msg.Enqueue(mlist, toadmin=1) |
| 571 | elif command == 'owner': |
| 572 | msg.Enqueue(mlist, toowner=1) |
| 573 | elif command == 'request': |
| 574 | msg.Enqueue(mlist, torequest=1) |
| 575 | elif command in ('join', 'leave'): |
| 576 | # TBD: this is a hack! |
| 577 | if command == 'join': |
| 578 | msg['Subject'] = 'subscribe' |
| 579 | else: |
| 580 | msg['Subject'] = 'unsubscribe' |
| 581 | msg.Enqueue(mlist, torequest=1) |
| 582 | |
| 583 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 584 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 585 | class Options: |
| 586 | setuid = 1 |
| 587 | classname = 'PureProxy' |
| 588 | |
| 589 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 590 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 591 | def parseargs(): |
| 592 | global DEBUGSTREAM |
| 593 | try: |
| 594 | opts, args = getopt.getopt( |
| 595 | sys.argv[1:], 'nVhc:d', |
| 596 | ['class=', 'nosetuid', 'version', 'help', 'debug']) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 597 | except getopt.error as e: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 598 | usage(1, e) |
| 599 | |
| 600 | options = Options() |
| 601 | for opt, arg in opts: |
| 602 | if opt in ('-h', '--help'): |
| 603 | usage(0) |
| 604 | elif opt in ('-V', '--version'): |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 605 | print(__version__, file=sys.stderr) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 606 | sys.exit(0) |
| 607 | elif opt in ('-n', '--nosetuid'): |
| 608 | options.setuid = 0 |
| 609 | elif opt in ('-c', '--class'): |
| 610 | options.classname = arg |
| 611 | elif opt in ('-d', '--debug'): |
| 612 | DEBUGSTREAM = sys.stderr |
| 613 | |
| 614 | # parse the rest of the arguments |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 615 | if len(args) < 1: |
| 616 | localspec = 'localhost:8025' |
| 617 | remotespec = 'localhost:25' |
| 618 | elif len(args) < 2: |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 619 | localspec = args[0] |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 620 | remotespec = 'localhost:25' |
Barry Warsaw | ebf5427 | 2001-11-04 03:04:25 +0000 | [diff] [blame] | 621 | elif len(args) < 3: |
| 622 | localspec = args[0] |
| 623 | remotespec = args[1] |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 624 | else: |
| 625 | usage(1, 'Invalid arguments: %s' % COMMASPACE.join(args)) |
| 626 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 627 | # split into host/port pairs |
| 628 | i = localspec.find(':') |
| 629 | if i < 0: |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 630 | usage(1, 'Bad local spec: %s' % localspec) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 631 | options.localhost = localspec[:i] |
| 632 | try: |
| 633 | options.localport = int(localspec[i+1:]) |
| 634 | except ValueError: |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 635 | usage(1, 'Bad local port: %s' % localspec) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 636 | i = remotespec.find(':') |
| 637 | if i < 0: |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 638 | usage(1, 'Bad remote spec: %s' % remotespec) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 639 | options.remotehost = remotespec[:i] |
| 640 | try: |
| 641 | options.remoteport = int(remotespec[i+1:]) |
| 642 | except ValueError: |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 643 | usage(1, 'Bad remote port: %s' % remotespec) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 644 | return options |
| 645 | |
| 646 | |
Barry Warsaw | 0e8427e | 2001-10-04 16:27:04 +0000 | [diff] [blame] | 647 | |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 648 | if __name__ == '__main__': |
| 649 | options = parseargs() |
| 650 | # Become nobody |
| 651 | if options.setuid: |
| 652 | try: |
| 653 | import pwd |
| 654 | except ImportError: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 655 | 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] | 656 | sys.exit(1) |
| 657 | nobody = pwd.getpwnam('nobody')[2] |
| 658 | try: |
| 659 | os.setuid(nobody) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 660 | except OSError as e: |
Guido van Rossum | 4ba3d65 | 2001-03-02 06:42:34 +0000 | [diff] [blame] | 661 | if e.errno != errno.EPERM: raise |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 662 | print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 663 | sys.exit(1) |
Skip Montanaro | 90e0153 | 2004-06-26 19:18:49 +0000 | [diff] [blame] | 664 | classname = options.classname |
| 665 | if "." in classname: |
| 666 | lastdot = classname.rfind(".") |
| 667 | mod = __import__(classname[:lastdot], globals(), locals(), [""]) |
| 668 | classname = classname[lastdot+1:] |
| 669 | else: |
| 670 | import __main__ as mod |
Skip Montanaro | 90e0153 | 2004-06-26 19:18:49 +0000 | [diff] [blame] | 671 | class_ = getattr(mod, classname) |
Barry Warsaw | 7e0d956 | 2001-01-31 22:51:35 +0000 | [diff] [blame] | 672 | proxy = class_((options.localhost, options.localport), |
| 673 | (options.remotehost, options.remoteport)) |
| 674 | try: |
| 675 | asyncore.loop() |
| 676 | except KeyboardInterrupt: |
| 677 | pass |