/
opt
/
alt
/
python34
/
lib64
/
python3.4
/
/opt/alt/python34/lib64/python3.4
mkdir
upload
Name
Size
Mode
Actions
asyncio/
-
0755
rm
collections/
-
0755
rm
concurrent/
-
0755
rm
config-3.4m/
-
0755
rm
ctypes/
-
0755
rm
curses/
-
0755
rm
dbm/
-
0755
rm
distutils/
-
0755
rm
email/
-
0755
rm
encodings/
-
0755
rm
ensurepip/
-
0755
rm
html/
-
0755
rm
http/
-
0755
rm
idlelib/
-
0755
rm
importlib/
-
0755
rm
json/
-
0755
rm
lib-dynload/
-
0755
rm
lib2to3/
-
0755
rm
logging/
-
0755
rm
multiprocessing/
-
0755
rm
plat-linux/
-
0755
rm
pydoc_data/
-
0755
rm
site-packages/
-
0755
rm
sqlite3/
-
0755
rm
test/
-
0755
rm
unittest/
-
0755
rm
urllib/
-
0755
rm
venv/
-
0755
rm
wsgiref/
-
0755
rm
xml/
-
0755
rm
xmlrpc/
-
0755
rm
__pycache__/
-
0755
rm
abc.py
8624
0644
edit
dl
rm
aifc.py
31578
0644
edit
dl
rm
antigravity.py
475
0644
edit
dl
rm
argparse.py
90027
0644
edit
dl
rm
ast.py
12034
0644
edit
dl
rm
asynchat.py
11825
0644
edit
dl
rm
asyncore.py
20998
0644
edit
dl
rm
base64.py
20180
0755
edit
dl
rm
bdb.py
23354
0644
edit
dl
rm
binhex.py
13928
0644
edit
dl
rm
bisect.py
2595
0644
edit
dl
rm
bz2.py
18860
0644
edit
dl
rm
calendar.py
22941
0644
edit
dl
rm
cgi.py
35941
0755
edit
dl
rm
cgitb.py
12041
0644
edit
dl
rm
chunk.py
5425
0644
edit
dl
rm
cmd.py
14860
0644
edit
dl
rm
code.py
10037
0644
edit
dl
rm
codecs.py
35910
0644
edit
dl
rm
codeop.py
5994
0644
edit
dl
rm
colorsys.py
4064
0644
edit
dl
rm
compileall.py
9618
0644
edit
dl
rm
configparser.py
49698
0644
edit
dl
rm
contextlib.py
11639
0644
edit
dl
rm
copy.py
9005
0644
edit
dl
rm
copyreg.py
6833
0644
edit
dl
rm
cProfile.py
5324
0755
edit
dl
rm
crypt.py
1879
0644
edit
dl
rm
csv.py
16185
0644
edit
dl
rm
datetime.py
75804
0644
edit
dl
rm
decimal.py
228688
0644
edit
dl
rm
difflib.py
81684
0644
edit
dl
rm
dis.py
17160
0644
edit
dl
rm
doctest.py
104492
0644
edit
dl
rm
dummy_threading.py
2815
0644
edit
dl
rm
enum.py
21538
0644
edit
dl
rm
filecmp.py
9830
0644
edit
dl
rm
fileinput.py
14865
0644
edit
dl
rm
fnmatch.py
3163
0644
edit
dl
rm
formatter.py
15173
0644
edit
dl
rm
fractions.py
23203
0644
edit
dl
rm
ftplib.py
38532
0644
edit
dl
rm
functools.py
28511
0644
edit
dl
rm
genericpath.py
3882
0644
edit
dl
rm
getopt.py
7489
0644
edit
dl
rm
getpass.py
6069
0644
edit
dl
rm
gettext.py
20767
0644
edit
dl
rm
glob.py
3461
0644
edit
dl
rm
gzip.py
24314
0644
edit
dl
rm
hashlib.py
9850
0644
edit
dl
rm
heapq.py
17997
0644
edit
dl
rm
hmac.py
5063
0644
edit
dl
rm
imaplib.py
50267
0644
edit
dl
rm
imghdr.py
3528
0644
edit
dl
rm
imp.py
9984
0644
edit
dl
rm
inspect.py
104640
0644
edit
dl
rm
io.py
3396
0644
edit
dl
rm
ipaddress.py
71598
0644
edit
dl
rm
keyword.py
2222
0755
edit
dl
rm
linecache.py
3953
0644
edit
dl
rm
locale.py
74530
0644
edit
dl
rm
lzma.py
19371
0644
edit
dl
rm
macpath.py
5619
0644
edit
dl
rm
macurl2path.py
2732
0644
edit
dl
rm
mailbox.py
78382
0644
edit
dl
rm
mailcap.py
7437
0644
edit
dl
rm
mimetypes.py
20781
0644
edit
dl
rm
modulefinder.py
23421
0644
edit
dl
rm
netrc.py
5748
0644
edit
dl
rm
nntplib.py
43082
0644
edit
dl
rm
ntpath.py
20477
0644
edit
dl
rm
nturl2path.py
2444
0644
edit
dl
rm
numbers.py
10243
0644
edit
dl
rm
opcode.py
5442
0644
edit
dl
rm
operator.py
9195
0644
edit
dl
rm
optparse.py
60346
0644
edit
dl
rm
os.py
33882
0644
edit
dl
rm
pathlib.py
42467
0644
edit
dl
rm
pdb.py
60992
0755
edit
dl
rm
pickle.py
55989
0644
edit
dl
rm
pickletools.py
91762
0644
edit
dl
rm
pipes.py
8916
0644
edit
dl
rm
pkgutil.py
21215
0644
edit
dl
rm
platform.py
46761
0755
edit
dl
rm
plistlib.py
31791
0644
edit
dl
rm
poplib.py
14319
0644
edit
dl
rm
posixpath.py
13448
0644
edit
dl
rm
pprint.py
14919
0644
edit
dl
rm
profile.py
22032
0755
edit
dl
rm
pstats.py
26316
0644
edit
dl
rm
pty.py
4763
0644
edit
dl
rm
pyclbr.py
13520
0644
edit
dl
rm
pydoc.py
103011
0755
edit
dl
rm
py_compile.py
7103
0644
edit
dl
rm
queue.py
8835
0644
edit
dl
rm
quopri.py
7265
0755
edit
dl
rm
random.py
26084
0644
edit
dl
rm
re.py
15604
0644
edit
dl
rm
reprlib.py
5110
0644
edit
dl
rm
rlcompleter.py
6069
0644
edit
dl
rm
runpy.py
10816
0644
edit
dl
rm
sched.py
6354
0644
edit
dl
rm
selectors.py
17097
0644
edit
dl
rm
shelve.py
8528
0644
edit
dl
rm
shlex.py
11548
0644
edit
dl
rm
shutil.py
39902
0644
edit
dl
rm
site.py
21553
0644
edit
dl
rm
smtpd.py
29991
0755
edit
dl
rm
smtplib.py
38971
0755
edit
dl
rm
sndhdr.py
6256
0644
edit
dl
rm
socket.py
19067
0644
edit
dl
rm
socketserver.py
24372
0644
edit
dl
rm
sre_compile.py
19903
0644
edit
dl
rm
sre_constants.py
7267
0644
edit
dl
rm
sre_parse.py
31429
0644
edit
dl
rm
ssl.py
34747
0644
edit
dl
rm
stat.py
4400
0644
edit
dl
rm
statistics.py
19556
0644
edit
dl
rm
string.py
11445
0644
edit
dl
rm
stringprep.py
12917
0644
edit
dl
rm
struct.py
257
0644
edit
dl
rm
subprocess.py
64549
0644
edit
dl
rm
sunau.py
18095
0644
edit
dl
rm
symbol.py
2053
0755
edit
dl
rm
symtable.py
7404
0644
edit
dl
rm
sysconfig.py
24632
0644
edit
dl
rm
tabnanny.py
11410
0755
edit
dl
rm
tarfile.py
91557
0755
edit
dl
rm
telnetlib.py
23074
0644
edit
dl
rm
tempfile.py
22525
0644
edit
dl
rm
textwrap.py
19282
0644
edit
dl
rm
this.py
1003
0644
edit
dl
rm
threading.py
48802
0644
edit
dl
rm
timeit.py
11972
0755
edit
dl
rm
token.py
3034
0644
edit
dl
rm
tokenize.py
25596
0644
edit
dl
rm
trace.py
31487
0755
edit
dl
rm
traceback.py
11167
0644
edit
dl
rm
tracemalloc.py
15651
0644
edit
dl
rm
tty.py
879
0644
edit
dl
rm
types.py
5411
0644
edit
dl
rm
uu.py
6766
0755
edit
dl
rm
uuid.py
23724
0644
edit
dl
rm
warnings.py
14303
0644
edit
dl
rm
wave.py
17682
0644
edit
dl
rm
weakref.py
19384
0644
edit
dl
rm
webbrowser.py
21432
0755
edit
dl
rm
xdrlib.py
5913
0644
edit
dl
rm
zipfile.py
68547
0644
edit
dl
rm
_bootlocale.py
1301
0644
edit
dl
rm
_collections_abc.py
19898
0644
edit
dl
rm
_compat_pickle.py
8318
0644
edit
dl
rm
_dummy_thread.py
4872
0644
edit
dl
rm
_markupbase.py
14598
0644
edit
dl
rm
_osx_support.py
19101
0644
edit
dl
rm
_pyio.py
73893
0644
edit
dl
rm
_sitebuiltins.py
3115
0644
edit
dl
rm
_strptime.py
22053
0644
edit
dl
rm
_sysconfigdata.py
24480
0644
edit
dl
rm
_threading_local.py
7410
0644
edit
dl
rm
_weakrefset.py
5705
0644
edit
dl
rm
__future__.py
4584
0644
edit
dl
rm
__phello__.foo.py
64
0644
edit
dl
rm
Edit:
/opt/alt/python34/lib64/python3.4/smtpd.py
(29991B)
#! /opt/alt/python34/bin/python3.4 """An RFC 5321 smtp proxy. Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]] 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 Use `classname' as the concrete SMTP proxy class. Uses `PureProxy' by default. --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. --debug -d Turn on debugging prints. --help -h Print this message and exit. Version: %(__version__)s 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. """ # Overview: # # This file implements the minimal SMTP protocol as defined in RFC 5321. It # has a hierarchy of classes which implement the backend functionality for the # smtpd. A number of classes are provided: # # SMTPServer - the base class for the backend. Raises NotImplementedError # 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. # # # Author: Barry Warsaw <barry@python.org> # # TODO: # # - support mailbox delivery # - alias files # - Handle more ESMTP extensions # - handle error codes from the backend smtpd import sys import os import errno import getopt import time import socket import asyncore import asynchat import collections from warnings import warn from email._header_value_parser import get_addr_spec, get_angle_addr __all__ = ["SMTPServer","DebuggingServer","PureProxy","MailmanProxy"] program = sys.argv[0] __version__ = 'Python SMTP proxy version 0.3' class Devnull: def write(self, msg): pass def flush(self): pass DEBUGSTREAM = Devnull() NEWLINE = '\n' EMPTYSTRING = '' COMMASPACE = ', ' DATA_SIZE_DEFAULT = 33554432 def usage(code, msg=''): print(__doc__ % globals(), file=sys.stderr) if msg: print(msg, file=sys.stderr) sys.exit(code) class SMTPChannel(asynchat.async_chat): COMMAND = 0 DATA = 1 command_size_limit = 512 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()) def __init__(self, server, conn, addr, data_size_limit=DATA_SIZE_DEFAULT, map=None): asynchat.async_chat.__init__(self, conn, map=map) self.smtp_server = server self.conn = conn self.addr = addr self.data_size_limit = data_size_limit self.received_lines = [] self.smtp_state = self.COMMAND self.seen_greeting = '' self.mailfrom = None self.rcpttos = [] self.received_data = '' self.fqdn = socket.getfqdn() self.num_bytes = 0 try: self.peer = conn.getpeername() except OSError as err: # 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 print('Peer:', repr(self.peer), file=DEBUGSTREAM) self.push('220 %s %s' % (self.fqdn, __version__)) self.set_terminator(b'\r\n') self.extended_smtp = False # properties for backwards-compatibility @property def __server(self): warn("Access to __server attribute on SMTPChannel is deprecated, " "use 'smtp_server' instead", DeprecationWarning, 2) return self.smtp_server @__server.setter def __server(self, value): warn("Setting __server attribute on SMTPChannel is deprecated, " "set 'smtp_server' instead", DeprecationWarning, 2) self.smtp_server = value @property def __line(self): warn("Access to __line attribute on SMTPChannel is deprecated, " "use 'received_lines' instead", DeprecationWarning, 2) return self.received_lines @__line.setter def __line(self, value): warn("Setting __line attribute on SMTPChannel is deprecated, " "set 'received_lines' instead", DeprecationWarning, 2) self.received_lines = value @property def __state(self): warn("Access to __state attribute on SMTPChannel is deprecated, " "use 'smtp_state' instead", DeprecationWarning, 2) return self.smtp_state @__state.setter def __state(self, value): warn("Setting __state attribute on SMTPChannel is deprecated, " "set 'smtp_state' instead", DeprecationWarning, 2) self.smtp_state = value @property def __greeting(self): warn("Access to __greeting attribute on SMTPChannel is deprecated, " "use 'seen_greeting' instead", DeprecationWarning, 2) return self.seen_greeting @__greeting.setter def __greeting(self, value): warn("Setting __greeting attribute on SMTPChannel is deprecated, " "set 'seen_greeting' instead", DeprecationWarning, 2) self.seen_greeting = value @property def __mailfrom(self): warn("Access to __mailfrom attribute on SMTPChannel is deprecated, " "use 'mailfrom' instead", DeprecationWarning, 2) return self.mailfrom @__mailfrom.setter def __mailfrom(self, value): warn("Setting __mailfrom attribute on SMTPChannel is deprecated, " "set 'mailfrom' instead", DeprecationWarning, 2) self.mailfrom = value @property def __rcpttos(self): warn("Access to __rcpttos attribute on SMTPChannel is deprecated, " "use 'rcpttos' instead", DeprecationWarning, 2) return self.rcpttos @__rcpttos.setter def __rcpttos(self, value): warn("Setting __rcpttos attribute on SMTPChannel is deprecated, " "set 'rcpttos' instead", DeprecationWarning, 2) self.rcpttos = value @property def __data(self): warn("Access to __data attribute on SMTPChannel is deprecated, " "use 'received_data' instead", DeprecationWarning, 2) return self.received_data @__data.setter def __data(self, value): warn("Setting __data attribute on SMTPChannel is deprecated, " "set 'received_data' instead", DeprecationWarning, 2) self.received_data = value @property def __fqdn(self): warn("Access to __fqdn attribute on SMTPChannel is deprecated, " "use 'fqdn' instead", DeprecationWarning, 2) return self.fqdn @__fqdn.setter def __fqdn(self, value): warn("Setting __fqdn attribute on SMTPChannel is deprecated, " "set 'fqdn' instead", DeprecationWarning, 2) self.fqdn = value @property def __peer(self): warn("Access to __peer attribute on SMTPChannel is deprecated, " "use 'peer' instead", DeprecationWarning, 2) return self.peer @__peer.setter def __peer(self, value): warn("Setting __peer attribute on SMTPChannel is deprecated, " "set 'peer' instead", DeprecationWarning, 2) self.peer = value @property def __conn(self): warn("Access to __conn attribute on SMTPChannel is deprecated, " "use 'conn' instead", DeprecationWarning, 2) return self.conn @__conn.setter def __conn(self, value): warn("Setting __conn attribute on SMTPChannel is deprecated, " "set 'conn' instead", DeprecationWarning, 2) self.conn = value @property def __addr(self): warn("Access to __addr attribute on SMTPChannel is deprecated, " "use 'addr' instead", DeprecationWarning, 2) return self.addr @__addr.setter def __addr(self, value): warn("Setting __addr attribute on SMTPChannel is deprecated, " "set 'addr' instead", DeprecationWarning, 2) self.addr = value # Overrides base class for convenience def push(self, msg): asynchat.async_chat.push(self, bytes(msg + '\r\n', 'ascii')) # Implementation of base class abstract method def collect_incoming_data(self, data): limit = None if self.smtp_state == self.COMMAND: limit = self.max_command_size_limit 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) self.received_lines.append(str(data, "utf-8")) # Implementation of base class abstract method def found_terminator(self): line = EMPTYSTRING.join(self.received_lines) print('Data:', repr(line), file=DEBUGSTREAM) self.received_lines = [] if self.smtp_state == self.COMMAND: sz, self.num_bytes = self.num_bytes, 0 if not line: self.push('500 Error: bad syntax') return method = None i = line.find(' ') if i < 0: command = line.upper() arg = None else: command = line[:i].upper() arg = line[i+1:].strip() 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 method = getattr(self, 'smtp_' + command, None) if not method: self.push('500 Error: command "%s" not recognized' % command) return method(arg) return else: if self.smtp_state != self.DATA: self.push('451 Internal confusion') self.num_bytes = 0 return if self.data_size_limit and self.num_bytes > self.data_size_limit: self.push('552 Error: Too much mail data') self.num_bytes = 0 return # Remove extraneous carriage returns and de-transparency according # to RFC 5321, Section 4.5.2. data = [] for text in line.split('\r\n'): if text and text[0] == '.': data.append(text[1:]) else: data.append(text) self.received_data = NEWLINE.join(data) status = self.smtp_server.process_message(self.peer, self.mailfrom, self.rcpttos, self.received_data) self.rcpttos = [] self.mailfrom = None self.smtp_state = self.COMMAND self.num_bytes = 0 self.set_terminator(b'\r\n') if not status: self.push('250 OK') else: self.push(status) # SMTP and ESMTP commands def smtp_HELO(self, arg): if not arg: self.push('501 Syntax: HELO hostname') return if self.seen_greeting: self.push('503 Duplicate HELO/EHLO') else: self.seen_greeting = arg self.extended_smtp = False self.push('250 %s' % self.fqdn) 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') def smtp_NOOP(self, arg): if arg: self.push('501 Syntax: NOOP') else: self.push('250 OK') def smtp_QUIT(self, arg): # args is ignored self.push('221 Bye') self.close_when_done() def _strip_command_keyword(self, keyword, arg): keylen = len(keyword) if arg[:keylen].upper() == keyword: 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>') def smtp_MAIL(self, arg): if not self.seen_greeting: self.push('503 Error: send HELO first'); return print('===> MAIL', arg, file=DEBUGSTREAM) 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 if self.mailfrom: self.push('503 Error: nested MAIL command') return 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 self.mailfrom = address print('sender:', self.mailfrom, file=DEBUGSTREAM) self.push('250 OK') def smtp_RCPT(self, arg): if not self.seen_greeting: self.push('503 Error: send HELO first'); return print('===> RCPT', arg, file=DEBUGSTREAM) if not self.mailfrom: self.push('503 Error: need MAIL command') return 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 params and len(params.keys()) > 0: self.push('555 RCPT TO parameters not recognized or not implemented') return self.rcpttos.append(address) print('recips:', self.rcpttos, file=DEBUGSTREAM) self.push('250 OK') def smtp_RSET(self, arg): if arg: self.push('501 Syntax: RSET') return # Resets the sender, recipients, and data, but not the greeting self.mailfrom = None self.rcpttos = [] self.received_data = '' self.smtp_state = self.COMMAND self.push('250 OK') def smtp_DATA(self, arg): if not self.seen_greeting: self.push('503 Error: send HELO first'); return if not self.rcpttos: self.push('503 Error: need RCPT command') return if arg: self.push('501 Syntax: DATA') return self.smtp_state = self.DATA self.set_terminator(b'\r\n.\r\n') self.push('354 End data with <CR><LF>.<CR><LF>') # Commands that have not been implemented def smtp_EXPN(self, arg): self.push('502 EXPN not implemented') class SMTPServer(asyncore.dispatcher): # SMTPChannel class to use for managing client connections channel_class = SMTPChannel def __init__(self, localaddr, remoteaddr, data_size_limit=DATA_SIZE_DEFAULT, map=None): self._localaddr = localaddr self._remoteaddr = remoteaddr self.data_size_limit = data_size_limit asyncore.dispatcher.__init__(self, map=map) try: self.create_socket(socket.AF_INET, socket.SOCK_STREAM) # 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) def handle_accepted(self, conn, addr): print('Incoming connection from %s' % repr(addr), file=DEBUGSTREAM) channel = self.channel_class(self, conn, addr, self.data_size_limit, self._map) # 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. """ raise NotImplementedError class DebuggingServer(SMTPServer): # Do something with the gathered message def process_message(self, peer, mailfrom, rcpttos, data): inheaders = 1 lines = data.split('\n') print('---------- MESSAGE FOLLOWS ----------') for line in lines: # headers first if inheaders and not line: print('X-Peer:', peer[0]) inheaders = 0 print(line) print('------------ END MESSAGE ------------') 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? print('we got some refusals:', refused, file=DEBUGSTREAM) 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() except smtplib.SMTPRecipientsRefused as e: print('got SMTPRecipientsRefused', file=DEBUGSTREAM) refused = e.recipients except (OSError, smtplib.SMTPException) as e: print('got', e.__class__, file=DEBUGSTREAM) # 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): from io import StringIO 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) # If there's any non-list destined recipients left, print('forwarding recips:', ' '.join(rcpttos), file=DEBUGSTREAM) if rcpttos: refused = self._deliver(mailfrom, rcpttos, data) # TBD: what to do with refused addresses? print('we got refusals:', refused, file=DEBUGSTREAM) # 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 # MTAs in existence seem to add these if the original message doesn't # have them. if not msg.get('from'): msg['From'] = mailfrom if not msg.get('date'): msg['Date'] = time.ctime(time.time()) for rcpt, listname, command in listnames: print('sending message to', rcpt, file=DEBUGSTREAM) 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' size_limit = None def parseargs(): global DEBUGSTREAM try: opts, args = getopt.getopt( sys.argv[1:], 'nVhc:s:d', ['class=', 'nosetuid', 'version', 'help', 'size=', 'debug']) except getopt.error as e: usage(1, e) options = Options() for opt, arg in opts: if opt in ('-h', '--help'): usage(0) elif opt in ('-V', '--version'): print(__version__) 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 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) # parse the rest of the arguments if len(args) < 1: localspec = 'localhost:8025' remotespec = 'localhost:25' elif len(args) < 2: localspec = args[0] remotespec = 'localhost:25' elif len(args) < 3: localspec = args[0] remotespec = args[1] else: usage(1, 'Invalid arguments: %s' % COMMASPACE.join(args)) # split into host/port pairs i = localspec.find(':') if i < 0: usage(1, 'Bad local spec: %s' % localspec) options.localhost = localspec[:i] try: options.localport = int(localspec[i+1:]) except ValueError: usage(1, 'Bad local port: %s' % localspec) i = remotespec.find(':') if i < 0: usage(1, 'Bad remote spec: %s' % remotespec) options.remotehost = remotespec[:i] try: options.remoteport = int(remotespec[i+1:]) except ValueError: usage(1, 'Bad remote port: %s' % remotespec) return options if __name__ == '__main__': options = parseargs() # Become nobody 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), (options.remotehost, options.remoteport), options.size_limit) if options.setuid: try: import pwd except ImportError: print('Cannot import module "pwd"; try running with -n option.', file=sys.stderr) sys.exit(1) nobody = pwd.getpwnam('nobody')[2] try: os.setuid(nobody) except PermissionError: print('Cannot setuid "nobody"; try running with -n option.', file=sys.stderr) sys.exit(1) try: asyncore.loop() except KeyboardInterrupt: pass
Save
cmd:
run