2010-03-11 18:53:45 -04:00
|
|
|
#! /usr/bin/env python3
|
2012-05-26 15:33:59 -03:00
|
|
|
"""An RFC 5321 smtp proxy.
|
2001-01-31 18:51:35 -04:00
|
|
|
|
2001-10-04 13:27:04 -03:00
|
|
|
Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]]
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
--nosetuid
|
|
|
|
-n
|
|
|
|
This program generally tries to setuid `nobody', unless this flag is
|
|
|
|
set. The setuid call will fail if this program is not run as root (in
|
|
|
|
which case, use this flag).
|
|
|
|
|
|
|
|
--version
|
|
|
|
-V
|
|
|
|
Print the version number and exit.
|
|
|
|
|
|
|
|
--class classname
|
|
|
|
-c classname
|
2004-10-09 18:44:13 -03:00
|
|
|
Use `classname' as the concrete SMTP proxy class. Uses `PureProxy' by
|
2001-01-31 18:51:35 -04:00
|
|
|
default.
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
--size limit
|
|
|
|
-s limit
|
|
|
|
Restrict the total size of the incoming message to "limit" number of
|
|
|
|
bytes via the RFC 1870 SIZE extension. Defaults to 33554432 bytes.
|
|
|
|
|
2001-01-31 18:51:35 -04:00
|
|
|
--debug
|
|
|
|
-d
|
|
|
|
Turn on debugging prints.
|
|
|
|
|
|
|
|
--help
|
|
|
|
-h
|
|
|
|
Print this message and exit.
|
|
|
|
|
|
|
|
Version: %(__version__)s
|
|
|
|
|
2001-10-04 13:27:04 -03:00
|
|
|
If localhost is not given then `localhost' is used, and if localport is not
|
|
|
|
given then 8025 is used. If remotehost is not given then `localhost' is used,
|
|
|
|
and if remoteport is not given, then 25 is used.
|
2001-01-31 18:51:35 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Overview:
|
|
|
|
#
|
2012-05-26 15:33:59 -03:00
|
|
|
# This file implements the minimal SMTP protocol as defined in RFC 5321. It
|
2001-01-31 18:51:35 -04:00
|
|
|
# has a hierarchy of classes which implement the backend functionality for the
|
|
|
|
# smtpd. A number of classes are provided:
|
|
|
|
#
|
2001-04-15 10:06:04 -03:00
|
|
|
# SMTPServer - the base class for the backend. Raises NotImplementedError
|
2001-01-31 18:51:35 -04:00
|
|
|
# if you try to use it.
|
|
|
|
#
|
|
|
|
# DebuggingServer - simply prints each message it receives on stdout.
|
|
|
|
#
|
|
|
|
# PureProxy - Proxies all messages to a real smtpd which does final
|
|
|
|
# delivery. One known problem with this class is that it doesn't handle
|
|
|
|
# SMTP errors from the backend server at all. This should be fixed
|
|
|
|
# (contributions are welcome!).
|
|
|
|
#
|
|
|
|
# MailmanProxy - An experimental hack to work with GNU Mailman
|
|
|
|
# <www.list.org>. Using this server as your real incoming smtpd, your
|
|
|
|
# mailhost will automatically recognize and accept mail destined to Mailman
|
|
|
|
# lists when those lists are created. Every message not destined for a list
|
|
|
|
# gets forwarded to a real backend smtpd, as with PureProxy. Again, errors
|
|
|
|
# are not handled correctly yet.
|
|
|
|
#
|
|
|
|
#
|
2004-07-12 20:10:08 -03:00
|
|
|
# Author: Barry Warsaw <barry@python.org>
|
2001-01-31 18:51:35 -04:00
|
|
|
#
|
|
|
|
# TODO:
|
|
|
|
#
|
|
|
|
# - support mailbox delivery
|
|
|
|
# - alias files
|
2012-05-26 15:33:59 -03:00
|
|
|
# - Handle more ESMTP extensions
|
2001-01-31 18:51:35 -04:00
|
|
|
# - handle error codes from the backend smtpd
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import errno
|
|
|
|
import getopt
|
|
|
|
import time
|
|
|
|
import socket
|
|
|
|
import asyncore
|
|
|
|
import asynchat
|
2012-05-26 15:33:59 -03:00
|
|
|
import collections
|
2010-07-24 06:51:40 -03:00
|
|
|
from warnings import warn
|
2012-05-26 15:33:59 -03:00
|
|
|
from email._header_value_parser import get_addr_spec, get_angle_addr
|
2001-01-31 18:51:35 -04:00
|
|
|
|
2001-02-15 18:15:14 -04:00
|
|
|
__all__ = ["SMTPServer","DebuggingServer","PureProxy","MailmanProxy"]
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
program = sys.argv[0]
|
2012-05-26 15:33:59 -03:00
|
|
|
__version__ = 'Python SMTP proxy version 0.3'
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Devnull:
|
|
|
|
def write(self, msg): pass
|
|
|
|
def flush(self): pass
|
|
|
|
|
|
|
|
|
|
|
|
DEBUGSTREAM = Devnull()
|
|
|
|
NEWLINE = '\n'
|
2001-10-04 13:27:04 -03:00
|
|
|
COMMASPACE = ', '
|
2012-05-26 15:33:59 -03:00
|
|
|
DATA_SIZE_DEFAULT = 33554432
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
def usage(code, msg=''):
|
2007-02-09 01:37:30 -04:00
|
|
|
print(__doc__ % globals(), file=sys.stderr)
|
2001-01-31 18:51:35 -04:00
|
|
|
if msg:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(msg, file=sys.stderr)
|
2001-01-31 18:51:35 -04:00
|
|
|
sys.exit(code)
|
|
|
|
|
|
|
|
|
|
|
|
class SMTPChannel(asynchat.async_chat):
|
|
|
|
COMMAND = 0
|
|
|
|
DATA = 1
|
|
|
|
|
2010-12-03 03:38:22 -04:00
|
|
|
command_size_limit = 512
|
2012-05-26 15:33:59 -03:00
|
|
|
command_size_limits = collections.defaultdict(lambda x=command_size_limit: x)
|
|
|
|
command_size_limits.update({
|
|
|
|
'MAIL': command_size_limit + 26,
|
|
|
|
})
|
|
|
|
max_command_size_limit = max(command_size_limits.values())
|
2010-12-03 03:38:22 -04:00
|
|
|
|
2013-06-07 11:21:41 -03:00
|
|
|
def __init__(self, server, conn, addr, data_size_limit=DATA_SIZE_DEFAULT,
|
2014-06-11 12:18:08 -03:00
|
|
|
map=None, decode_data=None):
|
2013-06-07 11:21:41 -03:00
|
|
|
asynchat.async_chat.__init__(self, conn, map=map)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.smtp_server = server
|
|
|
|
self.conn = conn
|
|
|
|
self.addr = addr
|
2012-05-26 15:33:59 -03:00
|
|
|
self.data_size_limit = data_size_limit
|
2014-06-11 12:18:08 -03:00
|
|
|
if decode_data is None:
|
|
|
|
warn("The decode_data default of True will change to False in 3.6;"
|
|
|
|
" specify an explicit value for this keyword",
|
|
|
|
DeprecationWarning, 2)
|
|
|
|
decode_data = True
|
|
|
|
self._decode_data = decode_data
|
|
|
|
if decode_data:
|
|
|
|
self._emptystring = ''
|
|
|
|
self._linesep = '\r\n'
|
|
|
|
self._dotsep = '.'
|
|
|
|
self._newline = NEWLINE
|
|
|
|
else:
|
|
|
|
self._emptystring = b''
|
|
|
|
self._linesep = b'\r\n'
|
|
|
|
self._dotsep = b'.'
|
|
|
|
self._newline = b'\n'
|
2010-07-24 06:51:40 -03:00
|
|
|
self.received_lines = []
|
|
|
|
self.smtp_state = self.COMMAND
|
|
|
|
self.seen_greeting = ''
|
|
|
|
self.mailfrom = None
|
|
|
|
self.rcpttos = []
|
|
|
|
self.received_data = ''
|
|
|
|
self.fqdn = socket.getfqdn()
|
2010-12-03 03:38:22 -04:00
|
|
|
self.num_bytes = 0
|
2010-08-23 19:28:13 -03:00
|
|
|
try:
|
|
|
|
self.peer = conn.getpeername()
|
2012-12-18 17:10:48 -04:00
|
|
|
except OSError as err:
|
2010-08-23 19:28:13 -03:00
|
|
|
# a race condition may occur if the other end is closing
|
|
|
|
# before we can get the peername
|
|
|
|
self.close()
|
|
|
|
if err.args[0] != errno.ENOTCONN:
|
|
|
|
raise
|
|
|
|
return
|
2010-07-24 06:51:40 -03:00
|
|
|
print('Peer:', repr(self.peer), file=DEBUGSTREAM)
|
|
|
|
self.push('220 %s %s' % (self.fqdn, __version__))
|
2008-07-07 01:15:08 -03:00
|
|
|
self.set_terminator(b'\r\n')
|
2012-05-26 15:33:59 -03:00
|
|
|
self.extended_smtp = False
|
2001-01-31 18:51:35 -04:00
|
|
|
|
2010-07-24 06:51:40 -03:00
|
|
|
# properties for backwards-compatibility
|
|
|
|
@property
|
|
|
|
def __server(self):
|
|
|
|
warn("Access to __server attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'smtp_server' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.smtp_server
|
|
|
|
@__server.setter
|
|
|
|
def __server(self, value):
|
|
|
|
warn("Setting __server attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'smtp_server' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.smtp_server = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __line(self):
|
|
|
|
warn("Access to __line attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'received_lines' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.received_lines
|
|
|
|
@__line.setter
|
|
|
|
def __line(self, value):
|
|
|
|
warn("Setting __line attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'received_lines' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.received_lines = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __state(self):
|
|
|
|
warn("Access to __state attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'smtp_state' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.smtp_state
|
|
|
|
@__state.setter
|
|
|
|
def __state(self, value):
|
|
|
|
warn("Setting __state attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'smtp_state' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.smtp_state = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __greeting(self):
|
|
|
|
warn("Access to __greeting attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'seen_greeting' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.seen_greeting
|
|
|
|
@__greeting.setter
|
|
|
|
def __greeting(self, value):
|
|
|
|
warn("Setting __greeting attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'seen_greeting' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.seen_greeting = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __mailfrom(self):
|
|
|
|
warn("Access to __mailfrom attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'mailfrom' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.mailfrom
|
|
|
|
@__mailfrom.setter
|
|
|
|
def __mailfrom(self, value):
|
|
|
|
warn("Setting __mailfrom attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'mailfrom' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.mailfrom = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __rcpttos(self):
|
|
|
|
warn("Access to __rcpttos attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'rcpttos' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.rcpttos
|
|
|
|
@__rcpttos.setter
|
|
|
|
def __rcpttos(self, value):
|
|
|
|
warn("Setting __rcpttos attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'rcpttos' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.rcpttos = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __data(self):
|
|
|
|
warn("Access to __data attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'received_data' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.received_data
|
|
|
|
@__data.setter
|
|
|
|
def __data(self, value):
|
|
|
|
warn("Setting __data attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'received_data' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.received_data = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __fqdn(self):
|
|
|
|
warn("Access to __fqdn attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'fqdn' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.fqdn
|
|
|
|
@__fqdn.setter
|
|
|
|
def __fqdn(self, value):
|
|
|
|
warn("Setting __fqdn attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'fqdn' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.fqdn = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __peer(self):
|
|
|
|
warn("Access to __peer attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'peer' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.peer
|
|
|
|
@__peer.setter
|
|
|
|
def __peer(self, value):
|
|
|
|
warn("Setting __peer attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'peer' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.peer = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __conn(self):
|
|
|
|
warn("Access to __conn attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'conn' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.conn
|
|
|
|
@__conn.setter
|
|
|
|
def __conn(self, value):
|
|
|
|
warn("Setting __conn attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'conn' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.conn = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def __addr(self):
|
|
|
|
warn("Access to __addr attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"use 'addr' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
return self.addr
|
|
|
|
@__addr.setter
|
|
|
|
def __addr(self, value):
|
|
|
|
warn("Setting __addr attribute on SMTPChannel is deprecated, "
|
2011-12-10 06:07:42 -04:00
|
|
|
"set 'addr' instead", DeprecationWarning, 2)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.addr = value
|
|
|
|
|
2001-01-31 18:51:35 -04:00
|
|
|
# Overrides base class for convenience
|
|
|
|
def push(self, msg):
|
2008-07-07 01:15:08 -03:00
|
|
|
asynchat.async_chat.push(self, bytes(msg + '\r\n', 'ascii'))
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
# Implementation of base class abstract method
|
|
|
|
def collect_incoming_data(self, data):
|
2010-12-03 03:38:22 -04:00
|
|
|
limit = None
|
|
|
|
if self.smtp_state == self.COMMAND:
|
2012-05-26 15:33:59 -03:00
|
|
|
limit = self.max_command_size_limit
|
2010-12-03 03:38:22 -04:00
|
|
|
elif self.smtp_state == self.DATA:
|
|
|
|
limit = self.data_size_limit
|
|
|
|
if limit and self.num_bytes > limit:
|
|
|
|
return
|
|
|
|
elif limit:
|
|
|
|
self.num_bytes += len(data)
|
2014-06-11 12:18:08 -03:00
|
|
|
if self._decode_data:
|
|
|
|
self.received_lines.append(str(data, 'utf-8'))
|
|
|
|
else:
|
|
|
|
self.received_lines.append(data)
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
# Implementation of base class abstract method
|
|
|
|
def found_terminator(self):
|
2014-06-11 12:18:08 -03:00
|
|
|
line = self._emptystring.join(self.received_lines)
|
2007-02-09 01:37:30 -04:00
|
|
|
print('Data:', repr(line), file=DEBUGSTREAM)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.received_lines = []
|
|
|
|
if self.smtp_state == self.COMMAND:
|
2012-05-26 15:33:59 -03:00
|
|
|
sz, self.num_bytes = self.num_bytes, 0
|
2001-01-31 18:51:35 -04:00
|
|
|
if not line:
|
|
|
|
self.push('500 Error: bad syntax')
|
|
|
|
return
|
|
|
|
method = None
|
2014-06-11 12:18:08 -03:00
|
|
|
if not self._decode_data:
|
|
|
|
line = str(line, 'utf-8')
|
2001-01-31 18:51:35 -04:00
|
|
|
i = line.find(' ')
|
|
|
|
if i < 0:
|
|
|
|
command = line.upper()
|
|
|
|
arg = None
|
|
|
|
else:
|
|
|
|
command = line[:i].upper()
|
|
|
|
arg = line[i+1:].strip()
|
2012-05-26 15:33:59 -03:00
|
|
|
max_sz = (self.command_size_limits[command]
|
|
|
|
if self.extended_smtp else self.command_size_limit)
|
|
|
|
if sz > max_sz:
|
|
|
|
self.push('500 Error: line too long')
|
|
|
|
return
|
2001-01-31 18:51:35 -04:00
|
|
|
method = getattr(self, 'smtp_' + command, None)
|
|
|
|
if not method:
|
2012-05-26 15:33:59 -03:00
|
|
|
self.push('500 Error: command "%s" not recognized' % command)
|
2001-01-31 18:51:35 -04:00
|
|
|
return
|
|
|
|
method(arg)
|
|
|
|
return
|
|
|
|
else:
|
2010-07-24 06:51:40 -03:00
|
|
|
if self.smtp_state != self.DATA:
|
2001-01-31 18:51:35 -04:00
|
|
|
self.push('451 Internal confusion')
|
2010-12-03 03:38:22 -04:00
|
|
|
self.num_bytes = 0
|
|
|
|
return
|
2012-05-26 15:33:59 -03:00
|
|
|
if self.data_size_limit and self.num_bytes > self.data_size_limit:
|
2010-12-03 03:38:22 -04:00
|
|
|
self.push('552 Error: Too much mail data')
|
|
|
|
self.num_bytes = 0
|
2001-01-31 18:51:35 -04:00
|
|
|
return
|
|
|
|
# Remove extraneous carriage returns and de-transparency according
|
2012-05-26 15:33:59 -03:00
|
|
|
# to RFC 5321, Section 4.5.2.
|
2001-01-31 18:51:35 -04:00
|
|
|
data = []
|
2014-06-11 12:18:08 -03:00
|
|
|
for text in line.split(self._linesep):
|
|
|
|
if text and text[0] == self._dotsep:
|
2001-01-31 18:51:35 -04:00
|
|
|
data.append(text[1:])
|
|
|
|
else:
|
|
|
|
data.append(text)
|
2014-06-11 12:18:08 -03:00
|
|
|
self.received_data = self._newline.join(data)
|
2010-07-31 07:08:09 -03:00
|
|
|
status = self.smtp_server.process_message(self.peer,
|
|
|
|
self.mailfrom,
|
|
|
|
self.rcpttos,
|
|
|
|
self.received_data)
|
2010-07-24 06:51:40 -03:00
|
|
|
self.rcpttos = []
|
|
|
|
self.mailfrom = None
|
|
|
|
self.smtp_state = self.COMMAND
|
2010-12-03 03:38:22 -04:00
|
|
|
self.num_bytes = 0
|
2008-07-07 01:15:08 -03:00
|
|
|
self.set_terminator(b'\r\n')
|
2001-01-31 18:51:35 -04:00
|
|
|
if not status:
|
2012-05-26 15:33:59 -03:00
|
|
|
self.push('250 OK')
|
2001-01-31 18:51:35 -04:00
|
|
|
else:
|
|
|
|
self.push(status)
|
|
|
|
|
|
|
|
# SMTP and ESMTP commands
|
|
|
|
def smtp_HELO(self, arg):
|
|
|
|
if not arg:
|
|
|
|
self.push('501 Syntax: HELO hostname')
|
|
|
|
return
|
2010-07-24 06:51:40 -03:00
|
|
|
if self.seen_greeting:
|
2001-01-31 18:51:35 -04:00
|
|
|
self.push('503 Duplicate HELO/EHLO')
|
|
|
|
else:
|
2010-07-24 06:51:40 -03:00
|
|
|
self.seen_greeting = arg
|
2012-05-26 15:33:59 -03:00
|
|
|
self.extended_smtp = False
|
2010-07-24 06:51:40 -03:00
|
|
|
self.push('250 %s' % self.fqdn)
|
2001-01-31 18:51:35 -04:00
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def smtp_EHLO(self, arg):
|
|
|
|
if not arg:
|
|
|
|
self.push('501 Syntax: EHLO hostname')
|
|
|
|
return
|
|
|
|
if self.seen_greeting:
|
|
|
|
self.push('503 Duplicate HELO/EHLO')
|
|
|
|
else:
|
|
|
|
self.seen_greeting = arg
|
|
|
|
self.extended_smtp = True
|
|
|
|
self.push('250-%s' % self.fqdn)
|
|
|
|
if self.data_size_limit:
|
|
|
|
self.push('250-SIZE %s' % self.data_size_limit)
|
|
|
|
self.push('250 HELP')
|
|
|
|
|
2001-01-31 18:51:35 -04:00
|
|
|
def smtp_NOOP(self, arg):
|
|
|
|
if arg:
|
|
|
|
self.push('501 Syntax: NOOP')
|
|
|
|
else:
|
2012-05-26 15:33:59 -03:00
|
|
|
self.push('250 OK')
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
def smtp_QUIT(self, arg):
|
|
|
|
# args is ignored
|
|
|
|
self.push('221 Bye')
|
|
|
|
self.close_when_done()
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def _strip_command_keyword(self, keyword, arg):
|
2001-01-31 18:51:35 -04:00
|
|
|
keylen = len(keyword)
|
|
|
|
if arg[:keylen].upper() == keyword:
|
2012-05-26 15:33:59 -03:00
|
|
|
return arg[keylen:].strip()
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def _getaddr(self, arg):
|
|
|
|
if not arg:
|
|
|
|
return '', ''
|
|
|
|
if arg.lstrip().startswith('<'):
|
|
|
|
address, rest = get_angle_addr(arg)
|
|
|
|
else:
|
|
|
|
address, rest = get_addr_spec(arg)
|
|
|
|
if not address:
|
|
|
|
return address, rest
|
|
|
|
return address.addr_spec, rest
|
|
|
|
|
|
|
|
def _getparams(self, params):
|
|
|
|
# Return any parameters that appear to be syntactically valid according
|
|
|
|
# to RFC 1869, ignore all others. (Postel rule: accept what we can.)
|
|
|
|
params = [param.split('=', 1) for param in params.split()
|
|
|
|
if '=' in param]
|
|
|
|
return {k: v for k, v in params if k.isalnum()}
|
|
|
|
|
|
|
|
def smtp_HELP(self, arg):
|
|
|
|
if arg:
|
|
|
|
extended = ' [SP <mail parameters]'
|
|
|
|
lc_arg = arg.upper()
|
|
|
|
if lc_arg == 'EHLO':
|
|
|
|
self.push('250 Syntax: EHLO hostname')
|
|
|
|
elif lc_arg == 'HELO':
|
|
|
|
self.push('250 Syntax: HELO hostname')
|
|
|
|
elif lc_arg == 'MAIL':
|
|
|
|
msg = '250 Syntax: MAIL FROM: <address>'
|
|
|
|
if self.extended_smtp:
|
|
|
|
msg += extended
|
|
|
|
self.push(msg)
|
|
|
|
elif lc_arg == 'RCPT':
|
|
|
|
msg = '250 Syntax: RCPT TO: <address>'
|
|
|
|
if self.extended_smtp:
|
|
|
|
msg += extended
|
|
|
|
self.push(msg)
|
|
|
|
elif lc_arg == 'DATA':
|
|
|
|
self.push('250 Syntax: DATA')
|
|
|
|
elif lc_arg == 'RSET':
|
|
|
|
self.push('250 Syntax: RSET')
|
|
|
|
elif lc_arg == 'NOOP':
|
|
|
|
self.push('250 Syntax: NOOP')
|
|
|
|
elif lc_arg == 'QUIT':
|
|
|
|
self.push('250 Syntax: QUIT')
|
|
|
|
elif lc_arg == 'VRFY':
|
|
|
|
self.push('250 Syntax: VRFY <address>')
|
|
|
|
else:
|
|
|
|
self.push('501 Supported commands: EHLO HELO MAIL RCPT '
|
|
|
|
'DATA RSET NOOP QUIT VRFY')
|
|
|
|
else:
|
|
|
|
self.push('250 Supported commands: EHLO HELO MAIL RCPT DATA '
|
|
|
|
'RSET NOOP QUIT VRFY')
|
|
|
|
|
|
|
|
def smtp_VRFY(self, arg):
|
|
|
|
if arg:
|
|
|
|
address, params = self._getaddr(arg)
|
|
|
|
if address:
|
|
|
|
self.push('252 Cannot VRFY user, but will accept message '
|
|
|
|
'and attempt delivery')
|
|
|
|
else:
|
|
|
|
self.push('502 Could not VRFY %s' % arg)
|
|
|
|
else:
|
|
|
|
self.push('501 Syntax: VRFY <address>')
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
def smtp_MAIL(self, arg):
|
2012-03-20 17:16:29 -03:00
|
|
|
if not self.seen_greeting:
|
|
|
|
self.push('503 Error: send HELO first');
|
|
|
|
return
|
2007-02-09 01:37:30 -04:00
|
|
|
print('===> MAIL', arg, file=DEBUGSTREAM)
|
2012-05-26 15:33:59 -03:00
|
|
|
syntaxerr = '501 Syntax: MAIL FROM: <address>'
|
|
|
|
if self.extended_smtp:
|
|
|
|
syntaxerr += ' [SP <mail-parameters>]'
|
|
|
|
if arg is None:
|
|
|
|
self.push(syntaxerr)
|
|
|
|
return
|
|
|
|
arg = self._strip_command_keyword('FROM:', arg)
|
|
|
|
address, params = self._getaddr(arg)
|
|
|
|
if not address:
|
|
|
|
self.push(syntaxerr)
|
|
|
|
return
|
|
|
|
if not self.extended_smtp and params:
|
|
|
|
self.push(syntaxerr)
|
|
|
|
return
|
2001-01-31 18:51:35 -04:00
|
|
|
if not address:
|
2012-05-26 15:33:59 -03:00
|
|
|
self.push(syntaxerr)
|
2001-01-31 18:51:35 -04:00
|
|
|
return
|
2010-07-24 06:51:40 -03:00
|
|
|
if self.mailfrom:
|
2001-01-31 18:51:35 -04:00
|
|
|
self.push('503 Error: nested MAIL command')
|
|
|
|
return
|
2012-05-26 15:33:59 -03:00
|
|
|
params = self._getparams(params.upper())
|
|
|
|
if params is None:
|
|
|
|
self.push(syntaxerr)
|
|
|
|
return
|
|
|
|
size = params.pop('SIZE', None)
|
|
|
|
if size:
|
|
|
|
if not size.isdigit():
|
|
|
|
self.push(syntaxerr)
|
|
|
|
return
|
|
|
|
elif self.data_size_limit and int(size) > self.data_size_limit:
|
|
|
|
self.push('552 Error: message size exceeds fixed maximum message size')
|
|
|
|
return
|
|
|
|
if len(params.keys()) > 0:
|
|
|
|
self.push('555 MAIL FROM parameters not recognized or not implemented')
|
|
|
|
return
|
2010-07-24 06:51:40 -03:00
|
|
|
self.mailfrom = address
|
|
|
|
print('sender:', self.mailfrom, file=DEBUGSTREAM)
|
2012-05-26 15:33:59 -03:00
|
|
|
self.push('250 OK')
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
def smtp_RCPT(self, arg):
|
2012-03-20 17:16:29 -03:00
|
|
|
if not self.seen_greeting:
|
|
|
|
self.push('503 Error: send HELO first');
|
|
|
|
return
|
2007-02-09 01:37:30 -04:00
|
|
|
print('===> RCPT', arg, file=DEBUGSTREAM)
|
2010-07-24 06:51:40 -03:00
|
|
|
if not self.mailfrom:
|
2001-01-31 18:51:35 -04:00
|
|
|
self.push('503 Error: need MAIL command')
|
|
|
|
return
|
2012-05-26 15:33:59 -03:00
|
|
|
syntaxerr = '501 Syntax: RCPT TO: <address>'
|
|
|
|
if self.extended_smtp:
|
|
|
|
syntaxerr += ' [SP <mail-parameters>]'
|
|
|
|
if arg is None:
|
|
|
|
self.push(syntaxerr)
|
|
|
|
return
|
|
|
|
arg = self._strip_command_keyword('TO:', arg)
|
|
|
|
address, params = self._getaddr(arg)
|
|
|
|
if not address:
|
|
|
|
self.push(syntaxerr)
|
|
|
|
return
|
|
|
|
if params:
|
|
|
|
if self.extended_smtp:
|
|
|
|
params = self._getparams(params.upper())
|
|
|
|
if params is None:
|
|
|
|
self.push(syntaxerr)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
self.push(syntaxerr)
|
|
|
|
return
|
|
|
|
if not address:
|
|
|
|
self.push(syntaxerr)
|
|
|
|
return
|
|
|
|
if params and len(params.keys()) > 0:
|
|
|
|
self.push('555 RCPT TO parameters not recognized or not implemented')
|
|
|
|
return
|
2001-01-31 18:51:35 -04:00
|
|
|
if not address:
|
|
|
|
self.push('501 Syntax: RCPT TO: <address>')
|
|
|
|
return
|
2010-07-24 06:51:40 -03:00
|
|
|
self.rcpttos.append(address)
|
|
|
|
print('recips:', self.rcpttos, file=DEBUGSTREAM)
|
2012-05-26 15:33:59 -03:00
|
|
|
self.push('250 OK')
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
def smtp_RSET(self, arg):
|
|
|
|
if arg:
|
|
|
|
self.push('501 Syntax: RSET')
|
|
|
|
return
|
|
|
|
# Resets the sender, recipients, and data, but not the greeting
|
2010-07-24 06:51:40 -03:00
|
|
|
self.mailfrom = None
|
|
|
|
self.rcpttos = []
|
|
|
|
self.received_data = ''
|
|
|
|
self.smtp_state = self.COMMAND
|
2012-05-26 15:33:59 -03:00
|
|
|
self.push('250 OK')
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
def smtp_DATA(self, arg):
|
2012-03-20 17:16:29 -03:00
|
|
|
if not self.seen_greeting:
|
|
|
|
self.push('503 Error: send HELO first');
|
|
|
|
return
|
2010-07-24 06:51:40 -03:00
|
|
|
if not self.rcpttos:
|
2001-01-31 18:51:35 -04:00
|
|
|
self.push('503 Error: need RCPT command')
|
|
|
|
return
|
|
|
|
if arg:
|
|
|
|
self.push('501 Syntax: DATA')
|
|
|
|
return
|
2010-07-24 06:51:40 -03:00
|
|
|
self.smtp_state = self.DATA
|
2008-07-07 01:15:08 -03:00
|
|
|
self.set_terminator(b'\r\n.\r\n')
|
2001-01-31 18:51:35 -04:00
|
|
|
self.push('354 End data with <CR><LF>.<CR><LF>')
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
# Commands that have not been implemented
|
|
|
|
def smtp_EXPN(self, arg):
|
|
|
|
self.push('502 EXPN not implemented')
|
|
|
|
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
class SMTPServer(asyncore.dispatcher):
|
2010-07-24 06:51:40 -03:00
|
|
|
# SMTPChannel class to use for managing client connections
|
|
|
|
channel_class = SMTPChannel
|
|
|
|
|
2012-05-26 15:33:59 -03:00
|
|
|
def __init__(self, localaddr, remoteaddr,
|
2014-06-11 12:18:08 -03:00
|
|
|
data_size_limit=DATA_SIZE_DEFAULT, map=None,
|
|
|
|
decode_data=None):
|
2001-01-31 18:51:35 -04:00
|
|
|
self._localaddr = localaddr
|
|
|
|
self._remoteaddr = remoteaddr
|
2012-05-26 15:33:59 -03:00
|
|
|
self.data_size_limit = data_size_limit
|
2014-06-11 12:18:08 -03:00
|
|
|
if decode_data is None:
|
|
|
|
warn("The decode_data default of True will change to False in 3.6;"
|
|
|
|
" specify an explicit value for this keyword",
|
|
|
|
DeprecationWarning, 2)
|
|
|
|
decode_data = True
|
|
|
|
self._decode_data = decode_data
|
2013-06-07 11:21:41 -03:00
|
|
|
asyncore.dispatcher.__init__(self, map=map)
|
2010-06-30 14:47:39 -03:00
|
|
|
try:
|
2014-06-11 16:17:50 -03:00
|
|
|
gai_results = socket.getaddrinfo(*localaddr,
|
|
|
|
type=socket.SOCK_STREAM)
|
2014-06-11 14:48:58 -03:00
|
|
|
self.create_socket(gai_results[0][0], gai_results[0][1])
|
2010-06-30 14:47:39 -03:00
|
|
|
# try to re-use a server port if possible
|
|
|
|
self.set_reuse_addr()
|
|
|
|
self.bind(localaddr)
|
|
|
|
self.listen(5)
|
|
|
|
except:
|
|
|
|
self.close()
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
print('%s started at %s\n\tLocal addr: %s\n\tRemote addr:%s' % (
|
|
|
|
self.__class__.__name__, time.ctime(time.time()),
|
|
|
|
localaddr, remoteaddr), file=DEBUGSTREAM)
|
2001-01-31 18:51:35 -04:00
|
|
|
|
2010-10-04 18:08:36 -03:00
|
|
|
def handle_accepted(self, conn, addr):
|
2007-02-09 01:37:30 -04:00
|
|
|
print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM)
|
2013-06-07 11:21:41 -03:00
|
|
|
channel = self.channel_class(self, conn, addr, self.data_size_limit,
|
2014-06-11 12:18:08 -03:00
|
|
|
self._map, self._decode_data)
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
# API for "doing something useful with the message"
|
|
|
|
def process_message(self, peer, mailfrom, rcpttos, data):
|
|
|
|
"""Override this abstract method to handle messages from the client.
|
|
|
|
|
|
|
|
peer is a tuple containing (ipaddr, port) of the client that made the
|
|
|
|
socket connection to our smtp port.
|
|
|
|
|
|
|
|
mailfrom is the raw address the client claims the message is coming
|
|
|
|
from.
|
|
|
|
|
|
|
|
rcpttos is a list of raw addresses the client wishes to deliver the
|
|
|
|
message to.
|
|
|
|
|
|
|
|
data is a string containing the entire full text of the message,
|
|
|
|
headers (if supplied) and all. It has been `de-transparencied'
|
|
|
|
according to RFC 821, Section 4.5.2. In other words, a line
|
|
|
|
containing a `.' followed by other text has had the leading dot
|
|
|
|
removed.
|
|
|
|
|
|
|
|
This function should return None, for a normal `250 Ok' response;
|
|
|
|
otherwise it returns the desired response string in RFC 821 format.
|
|
|
|
|
|
|
|
"""
|
2001-04-15 10:06:04 -03:00
|
|
|
raise NotImplementedError
|
2001-01-31 18:51:35 -04:00
|
|
|
|
2001-02-09 16:06:00 -04:00
|
|
|
|
2001-01-31 18:51:35 -04:00
|
|
|
class DebuggingServer(SMTPServer):
|
|
|
|
# Do something with the gathered message
|
|
|
|
def process_message(self, peer, mailfrom, rcpttos, data):
|
|
|
|
inheaders = 1
|
|
|
|
lines = data.split('\n')
|
2007-02-09 01:37:30 -04:00
|
|
|
print('---------- MESSAGE FOLLOWS ----------')
|
2001-01-31 18:51:35 -04:00
|
|
|
for line in lines:
|
|
|
|
# headers first
|
|
|
|
if inheaders and not line:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('X-Peer:', peer[0])
|
2001-01-31 18:51:35 -04:00
|
|
|
inheaders = 0
|
2007-02-09 01:37:30 -04:00
|
|
|
print(line)
|
|
|
|
print('------------ END MESSAGE ------------')
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PureProxy(SMTPServer):
|
|
|
|
def process_message(self, peer, mailfrom, rcpttos, data):
|
|
|
|
lines = data.split('\n')
|
|
|
|
# Look for the last header
|
|
|
|
i = 0
|
|
|
|
for line in lines:
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
i += 1
|
|
|
|
lines.insert(i, 'X-Peer: %s' % peer[0])
|
|
|
|
data = NEWLINE.join(lines)
|
|
|
|
refused = self._deliver(mailfrom, rcpttos, data)
|
|
|
|
# TBD: what to do with refused addresses?
|
2007-02-09 01:37:30 -04:00
|
|
|
print('we got some refusals:', refused, file=DEBUGSTREAM)
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
def _deliver(self, mailfrom, rcpttos, data):
|
|
|
|
import smtplib
|
|
|
|
refused = {}
|
|
|
|
try:
|
|
|
|
s = smtplib.SMTP()
|
|
|
|
s.connect(self._remoteaddr[0], self._remoteaddr[1])
|
|
|
|
try:
|
|
|
|
refused = s.sendmail(mailfrom, rcpttos, data)
|
|
|
|
finally:
|
|
|
|
s.quit()
|
2007-01-10 12:19:56 -04:00
|
|
|
except smtplib.SMTPRecipientsRefused as e:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('got SMTPRecipientsRefused', file=DEBUGSTREAM)
|
2001-01-31 18:51:35 -04:00
|
|
|
refused = e.recipients
|
2012-12-18 17:10:48 -04:00
|
|
|
except (OSError, smtplib.SMTPException) as e:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('got', e.__class__, file=DEBUGSTREAM)
|
2001-01-31 18:51:35 -04:00
|
|
|
# All recipients were refused. If the exception had an associated
|
|
|
|
# error code, use it. Otherwise,fake it with a non-triggering
|
|
|
|
# exception code.
|
|
|
|
errcode = getattr(e, 'smtp_code', -1)
|
|
|
|
errmsg = getattr(e, 'smtp_error', 'ignore')
|
|
|
|
for r in rcpttos:
|
|
|
|
refused[r] = (errcode, errmsg)
|
|
|
|
return refused
|
|
|
|
|
|
|
|
|
|
|
|
class MailmanProxy(PureProxy):
|
|
|
|
def process_message(self, peer, mailfrom, rcpttos, data):
|
2007-05-17 21:51:22 -03:00
|
|
|
from io import StringIO
|
2001-01-31 18:51:35 -04:00
|
|
|
from Mailman import Utils
|
|
|
|
from Mailman import Message
|
|
|
|
from Mailman import MailList
|
|
|
|
# If the message is to a Mailman mailing list, then we'll invoke the
|
|
|
|
# Mailman script directly, without going through the real smtpd.
|
|
|
|
# Otherwise we'll forward it to the local proxy for disposition.
|
|
|
|
listnames = []
|
|
|
|
for rcpt in rcpttos:
|
|
|
|
local = rcpt.lower().split('@')[0]
|
|
|
|
# We allow the following variations on the theme
|
|
|
|
# listname
|
|
|
|
# listname-admin
|
|
|
|
# listname-owner
|
|
|
|
# listname-request
|
|
|
|
# listname-join
|
|
|
|
# listname-leave
|
|
|
|
parts = local.split('-')
|
|
|
|
if len(parts) > 2:
|
|
|
|
continue
|
|
|
|
listname = parts[0]
|
|
|
|
if len(parts) == 2:
|
|
|
|
command = parts[1]
|
|
|
|
else:
|
|
|
|
command = ''
|
|
|
|
if not Utils.list_exists(listname) or command not in (
|
|
|
|
'', 'admin', 'owner', 'request', 'join', 'leave'):
|
|
|
|
continue
|
|
|
|
listnames.append((rcpt, listname, command))
|
|
|
|
# Remove all list recipients from rcpttos and forward what we're not
|
|
|
|
# going to take care of ourselves. Linear removal should be fine
|
|
|
|
# since we don't expect a large number of recipients.
|
|
|
|
for rcpt, listname, command in listnames:
|
|
|
|
rcpttos.remove(rcpt)
|
2001-02-09 16:06:00 -04:00
|
|
|
# If there's any non-list destined recipients left,
|
2007-02-09 01:37:30 -04:00
|
|
|
print('forwarding recips:', ' '.join(rcpttos), file=DEBUGSTREAM)
|
2001-01-31 18:51:35 -04:00
|
|
|
if rcpttos:
|
|
|
|
refused = self._deliver(mailfrom, rcpttos, data)
|
|
|
|
# TBD: what to do with refused addresses?
|
2007-02-09 01:37:30 -04:00
|
|
|
print('we got refusals:', refused, file=DEBUGSTREAM)
|
2001-01-31 18:51:35 -04:00
|
|
|
# Now deliver directly to the list commands
|
|
|
|
mlists = {}
|
|
|
|
s = StringIO(data)
|
|
|
|
msg = Message.Message(s)
|
|
|
|
# These headers are required for the proper execution of Mailman. All
|
2009-02-21 16:59:32 -04:00
|
|
|
# MTAs in existence seem to add these if the original message doesn't
|
2001-01-31 18:51:35 -04:00
|
|
|
# have them.
|
2008-06-12 01:06:45 -03:00
|
|
|
if not msg.get('from'):
|
2001-01-31 18:51:35 -04:00
|
|
|
msg['From'] = mailfrom
|
2008-06-12 01:06:45 -03:00
|
|
|
if not msg.get('date'):
|
2001-01-31 18:51:35 -04:00
|
|
|
msg['Date'] = time.ctime(time.time())
|
|
|
|
for rcpt, listname, command in listnames:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('sending message to', rcpt, file=DEBUGSTREAM)
|
2001-01-31 18:51:35 -04:00
|
|
|
mlist = mlists.get(listname)
|
|
|
|
if not mlist:
|
|
|
|
mlist = MailList.MailList(listname, lock=0)
|
|
|
|
mlists[listname] = mlist
|
|
|
|
# dispatch on the type of command
|
|
|
|
if command == '':
|
|
|
|
# post
|
|
|
|
msg.Enqueue(mlist, tolist=1)
|
|
|
|
elif command == 'admin':
|
|
|
|
msg.Enqueue(mlist, toadmin=1)
|
|
|
|
elif command == 'owner':
|
|
|
|
msg.Enqueue(mlist, toowner=1)
|
|
|
|
elif command == 'request':
|
|
|
|
msg.Enqueue(mlist, torequest=1)
|
|
|
|
elif command in ('join', 'leave'):
|
|
|
|
# TBD: this is a hack!
|
|
|
|
if command == 'join':
|
|
|
|
msg['Subject'] = 'subscribe'
|
|
|
|
else:
|
|
|
|
msg['Subject'] = 'unsubscribe'
|
|
|
|
msg.Enqueue(mlist, torequest=1)
|
|
|
|
|
|
|
|
|
|
|
|
class Options:
|
|
|
|
setuid = 1
|
|
|
|
classname = 'PureProxy'
|
2012-05-26 15:33:59 -03:00
|
|
|
size_limit = None
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
def parseargs():
|
|
|
|
global DEBUGSTREAM
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(
|
2012-05-26 15:33:59 -03:00
|
|
|
sys.argv[1:], 'nVhc:s:d',
|
|
|
|
['class=', 'nosetuid', 'version', 'help', 'size=', 'debug'])
|
2007-01-10 12:19:56 -04:00
|
|
|
except getopt.error as e:
|
2001-01-31 18:51:35 -04:00
|
|
|
usage(1, e)
|
|
|
|
|
|
|
|
options = Options()
|
|
|
|
for opt, arg in opts:
|
|
|
|
if opt in ('-h', '--help'):
|
|
|
|
usage(0)
|
|
|
|
elif opt in ('-V', '--version'):
|
2013-09-05 11:44:53 -03:00
|
|
|
print(__version__)
|
2001-01-31 18:51:35 -04:00
|
|
|
sys.exit(0)
|
|
|
|
elif opt in ('-n', '--nosetuid'):
|
|
|
|
options.setuid = 0
|
|
|
|
elif opt in ('-c', '--class'):
|
|
|
|
options.classname = arg
|
|
|
|
elif opt in ('-d', '--debug'):
|
|
|
|
DEBUGSTREAM = sys.stderr
|
2012-05-26 15:33:59 -03:00
|
|
|
elif opt in ('-s', '--size'):
|
|
|
|
try:
|
|
|
|
int_size = int(arg)
|
|
|
|
options.size_limit = int_size
|
|
|
|
except:
|
|
|
|
print('Invalid size: ' + arg, file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2001-01-31 18:51:35 -04:00
|
|
|
|
|
|
|
# parse the rest of the arguments
|
2001-10-04 13:27:04 -03:00
|
|
|
if len(args) < 1:
|
|
|
|
localspec = 'localhost:8025'
|
|
|
|
remotespec = 'localhost:25'
|
|
|
|
elif len(args) < 2:
|
2001-01-31 18:51:35 -04:00
|
|
|
localspec = args[0]
|
2001-10-04 13:27:04 -03:00
|
|
|
remotespec = 'localhost:25'
|
2001-11-03 23:04:25 -04:00
|
|
|
elif len(args) < 3:
|
|
|
|
localspec = args[0]
|
|
|
|
remotespec = args[1]
|
2001-10-04 13:27:04 -03:00
|
|
|
else:
|
|
|
|
usage(1, 'Invalid arguments: %s' % COMMASPACE.join(args))
|
|
|
|
|
2001-01-31 18:51:35 -04:00
|
|
|
# split into host/port pairs
|
|
|
|
i = localspec.find(':')
|
|
|
|
if i < 0:
|
2001-10-04 13:27:04 -03:00
|
|
|
usage(1, 'Bad local spec: %s' % localspec)
|
2001-01-31 18:51:35 -04:00
|
|
|
options.localhost = localspec[:i]
|
|
|
|
try:
|
|
|
|
options.localport = int(localspec[i+1:])
|
|
|
|
except ValueError:
|
2001-10-04 13:27:04 -03:00
|
|
|
usage(1, 'Bad local port: %s' % localspec)
|
2001-01-31 18:51:35 -04:00
|
|
|
i = remotespec.find(':')
|
|
|
|
if i < 0:
|
2001-10-04 13:27:04 -03:00
|
|
|
usage(1, 'Bad remote spec: %s' % remotespec)
|
2001-01-31 18:51:35 -04:00
|
|
|
options.remotehost = remotespec[:i]
|
|
|
|
try:
|
|
|
|
options.remoteport = int(remotespec[i+1:])
|
|
|
|
except ValueError:
|
2001-10-04 13:27:04 -03:00
|
|
|
usage(1, 'Bad remote port: %s' % remotespec)
|
2001-01-31 18:51:35 -04:00
|
|
|
return options
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
options = parseargs()
|
|
|
|
# Become nobody
|
2011-10-20 18:03:43 -03:00
|
|
|
classname = options.classname
|
|
|
|
if "." in classname:
|
|
|
|
lastdot = classname.rfind(".")
|
|
|
|
mod = __import__(classname[:lastdot], globals(), locals(), [""])
|
|
|
|
classname = classname[lastdot+1:]
|
|
|
|
else:
|
|
|
|
import __main__ as mod
|
|
|
|
class_ = getattr(mod, classname)
|
|
|
|
proxy = class_((options.localhost, options.localport),
|
2012-05-26 15:33:59 -03:00
|
|
|
(options.remotehost, options.remoteport),
|
|
|
|
options.size_limit)
|
2001-01-31 18:51:35 -04:00
|
|
|
if options.setuid:
|
|
|
|
try:
|
|
|
|
import pwd
|
2013-07-04 18:43:24 -03:00
|
|
|
except ImportError:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('Cannot import module "pwd"; try running with -n option.', file=sys.stderr)
|
2001-01-31 18:51:35 -04:00
|
|
|
sys.exit(1)
|
|
|
|
nobody = pwd.getpwnam('nobody')[2]
|
|
|
|
try:
|
|
|
|
os.setuid(nobody)
|
2013-02-12 10:14:17 -04:00
|
|
|
except PermissionError:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr)
|
2001-01-31 18:51:35 -04:00
|
|
|
sys.exit(1)
|
|
|
|
try:
|
|
|
|
asyncore.loop()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|