| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
"""dummy SMTP server for use in tests""" |
| 4 |
|
| 5 |
|
| 6 |
import io |
| 7 |
import optparse |
| 8 |
import os |
| 9 |
import socket |
| 10 |
import ssl |
| 11 |
import sys |
| 12 |
|
| 13 |
from mercurial import ( |
| 14 |
pycompat, |
| 15 |
server, |
| 16 |
sslutil, |
| 17 |
ui as uimod, |
| 18 |
) |
| 19 |
|
| 20 |
if pycompat.iswindows: |
| 21 |
sys.stdout = io.TextIOWrapper( |
| 22 |
sys.stdout.buffer, |
| 23 |
sys.stdout.encoding, |
| 24 |
sys.stdout.errors, |
| 25 |
newline="\n", |
| 26 |
) |
| 27 |
|
| 28 |
if os.environ.get('HGIPV6', '0') == '1': |
| 29 |
family = socket.AF_INET6 |
| 30 |
else: |
| 31 |
family = socket.AF_INET |
| 32 |
|
| 33 |
|
| 34 |
def log(msg): |
| 35 |
sys.stdout.write(msg) |
| 36 |
sys.stdout.flush() |
| 37 |
|
| 38 |
|
| 39 |
def mocksmtpserversession(conn, addr): |
| 40 |
conn.send(b'220 smtp.example.com ESMTP\r\n') |
| 41 |
|
| 42 |
try: |
| 43 |
# Newer versions of OpenSSL raise on EOF |
| 44 |
line = conn.recv(1024) |
| 45 |
except ssl.SSLError: |
| 46 |
log('no hello: EOF\n') |
| 47 |
return |
| 48 |
|
| 49 |
if not line.lower().startswith(b'ehlo '): |
| 50 |
# Older versions of OpenSSl don't raise |
| 51 |
log('no hello: %s\n' % line) |
| 52 |
return |
| 53 |
|
| 54 |
conn.send(b'250 Hello\r\n') |
| 55 |
|
| 56 |
line = conn.recv(1024) |
| 57 |
if not line.lower().startswith(b'mail from:'): |
| 58 |
log('no mail from: %s\n' % line) |
| 59 |
return |
| 60 |
mailfrom = line[10:].decode().rstrip() |
| 61 |
if mailfrom.startswith('<') and mailfrom.endswith('>'): |
| 62 |
mailfrom = mailfrom[1:-1] |
| 63 |
|
| 64 |
conn.send(b'250 Ok\r\n') |
| 65 |
|
| 66 |
rcpttos = [] |
| 67 |
while True: |
| 68 |
line = conn.recv(1024) |
| 69 |
if not line.lower().startswith(b'rcpt to:'): |
| 70 |
break |
| 71 |
rcptto = line[8:].decode().rstrip() |
| 72 |
if rcptto.startswith('<') and rcptto.endswith('>'): |
| 73 |
rcptto = rcptto[1:-1] |
| 74 |
rcpttos.append(rcptto) |
| 75 |
|
| 76 |
conn.send(b'250 Ok\r\n') |
| 77 |
|
| 78 |
if not line.lower().strip() == b'data': |
| 79 |
log('no rcpt to or data: %s' % line) |
| 80 |
|
| 81 |
conn.send(b'354 Go ahead\r\n') |
| 82 |
|
| 83 |
data = b'' |
| 84 |
while True: |
| 85 |
line = conn.recv(1024) |
| 86 |
if not line: |
| 87 |
log('connection closed before end of data') |
| 88 |
break |
| 89 |
data += line |
| 90 |
if data.endswith(b'\r\n.\r\n'): |
| 91 |
data = data[:-5] |
| 92 |
break |
| 93 |
|
| 94 |
conn.send(b'250 Ok\r\n') |
| 95 |
|
| 96 |
log( |
| 97 |
'%s from=%s to=%s\n%s\n' |
| 98 |
% (addr[0], mailfrom, ', '.join(rcpttos), data.decode()) |
| 99 |
) |
| 100 |
|
| 101 |
|
| 102 |
def run(host, port, certificate): |
| 103 |
ui = uimod.ui.load() |
| 104 |
with socket.socket(family, socket.SOCK_STREAM) as s: |
| 105 |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 106 |
s.bind((host, port)) |
| 107 |
# log('listening at %s:%d\n' % (host, port)) |
| 108 |
s.listen(1) |
| 109 |
try: |
| 110 |
while True: |
| 111 |
conn, addr = s.accept() |
| 112 |
if certificate: |
| 113 |
try: |
| 114 |
conn = sslutil.wrapserversocket( |
| 115 |
conn, ui, certfile=certificate |
| 116 |
) |
| 117 |
except ssl.SSLError as e: |
| 118 |
log('%s ssl error: %s\n' % (addr[0], e)) |
| 119 |
conn.close() |
| 120 |
continue |
| 121 |
log("connection from %s:%s\n" % addr) |
| 122 |
mocksmtpserversession(conn, addr) |
| 123 |
conn.close() |
| 124 |
except KeyboardInterrupt: |
| 125 |
pass |
| 126 |
|
| 127 |
|
| 128 |
def _encodestrsonly(v): |
| 129 |
if isinstance(v, str): |
| 130 |
return v.encode('ascii') |
| 131 |
return v |
| 132 |
|
| 133 |
|
| 134 |
def bytesvars(obj): |
| 135 |
unidict = vars(obj) |
| 136 |
bd = {k.encode('ascii'): _encodestrsonly(v) for k, v in unidict.items()} |
| 137 |
if bd[b'daemon_postexec'] is not None: |
| 138 |
bd[b'daemon_postexec'] = [ |
| 139 |
_encodestrsonly(v) for v in bd[b'daemon_postexec'] |
| 140 |
] |
| 141 |
return bd |
| 142 |
|
| 143 |
|
| 144 |
def main(): |
| 145 |
op = optparse.OptionParser() |
| 146 |
op.add_option('-d', '--daemon', action='store_true') |
| 147 |
op.add_option('--daemon-postexec', action='append') |
| 148 |
op.add_option('-p', '--port', type=int, default=8025) |
| 149 |
op.add_option('-a', '--address', default='localhost') |
| 150 |
op.add_option('--pid-file', metavar='FILE') |
| 151 |
op.add_option('--tls', choices=['none', 'smtps'], default='none') |
| 152 |
op.add_option('--certificate', metavar='FILE') |
| 153 |
op.add_option('--logfile', metavar='FILE') |
| 154 |
|
| 155 |
opts, args = op.parse_args() |
| 156 |
if (opts.tls == 'smtps') != bool(opts.certificate): |
| 157 |
op.error('--certificate must be specified with --tls=smtps') |
| 158 |
|
| 159 |
server.runservice( |
| 160 |
bytesvars(opts), |
| 161 |
runfn=lambda: run(opts.address, opts.port, opts.certificate), |
| 162 |
runargs=[pycompat.sysexecutable, pycompat.fsencode(__file__)] |
| 163 |
+ pycompat.sysargv[1:], |
| 164 |
logfile=opts.logfile, |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
if __name__ == '__main__': |
| 169 |
main() |