1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#!/usr/bin/env python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
# Copyright (C) 2019 Bryce W. Harrington
# Copyright (C) 2022 Matthias Andree
#
# Released under GNU GPLv2 or later, read the file 'LICENSE.GPLv2+' for
# more information.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Author: Bryce Harrington <bryce@canonical.com>
import os
import sys
import socket
from tempfile import mkstemp
from pathlib import Path
DEBUGGING = True
DEFAULT_HOST = ''
DEFAULT_PORT = 11110
NEWLINE = b"\r\n"
MESSAGE_CONTENT = b'''
From: test@example.com\r
Subject: Test message\r
\r
This is a body for testing\r
\r
'''
def dbg(msg):
"""Prints information to stdout if debugging is enabled"""
if DEBUGGING:
sys.stdout.write("{}\n".format(msg))
def command_user(data, msg):
return b'+OK user accepted'
def command_pass(data, msg):
return b'+OK pass accepted'
def command_stat(data, msg):
return b'+OK 1 %i' %(msg.size)
def command_list(data, msg):
if data == b'LIST':
return b'+OK 1 messages (%i octets)'%(msg.size) \
+ NEWLINE \
+ b'1 %i'%(msg.size) \
+ NEWLINE \
+ b'.'
else:
cmd, num = data.split()
return b'+OK 1 (%i octects)'%(msg.size)
def command_last(data, msg):
return b'+OK 0'
def command_top(data, msg):
cmd, num, lines = data.split()
assert num == b'1', "unknown message number: {num}".format(num)
bottom = NEWLINE.join(msg.bottom[:int(lines)])
text = msg.top + NEWLINE + NEWLINE + bottom
dbg(text)
return b'+OK top of message follows%s' % (NEWLINE + text + NEWLINE + b'.')
def command_retr(data, msg):
return b'+OK %i octets'%(msg.size) \
+ NEWLINE \
+ msg.data \
+ NEWLINE \
+ b'.'
def command_dele(data, msg):
return b'+OK 1 %i'%(msg.size)
def command_noop(data, msg):
return b'+OK 1 %i'%(msg.size)
def command_quit(data, msg):
return b'+OK mock pop3 server signing off'
COMMANDS = {
b'USER' : command_user,
b'PASS' : command_pass,
b'STAT' : command_stat,
b'LIST' : command_list,
b'LAST' : command_last,
b'TOP' : command_top,
b'RETR' : command_retr,
b'DELE' : command_dele,
b'NOOP' : command_noop,
b'QUIT' : command_quit,
}
class Mailbox:
"""Encapsulates a mailbox containing a single email message"""
def __init__(self, message_filename):
with open(message_filename, "rb") as msg:
data = msg.read()
self.data = data
self.size = len(data)
self.top, rest = data.split(NEWLINE + NEWLINE, 1)
self.bottom = rest.split(NEWLINE)
class Mailserver:
def __init__(self, conn, mbox):
self._conn = conn
self._mbox = mbox
self._chunk_size = 4096
def close(self):
dbg(" - Server exiting")
self._conn.close()
def send(self, data):
dbg(" - Server sending")
self._conn.sendall(data + NEWLINE)
def receive(self):
dbg(" - Server receiving")
data = []
while True:
chunk = self._conn.recv(self._chunk_size)
if NEWLINE in chunk:
data.append(chunk[:chunk.index(NEWLINE)])
break
data.append(chunk)
return b"".join(data)
def process(self):
data = self.receive()
dbg(" - Received: '{}'".format(data))
command = data.split(None, 1)[0].upper()
if command in COMMANDS.keys():
response = COMMANDS[command](data, self._mbox)
dbg(" - Response: {}".format(response))
try:
self.send(response)
if command == b'QUIT':
return False
except BrokenPipeError:
dbg(" - Client terminated connection")
return False
else:
self.send(b"-ERR unrecognized command")
return True
def serve(messages_filename, host=DEFAULT_HOST, port=DEFAULT_PORT):
dbg("Serving for {} on {}".format(host, port))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
mailbox = Mailbox(messages_filename)
try:
sock.listen(1)
dbg("* mock-pop3-server ready on {}:{}".format(host, port))
while True:
sock.listen(1)
startmarker = os.environ.get("STARTMARKER")
if startmarker:
try:
Path(startmarker).touch()
except FileNotFoundError:
# work directory has gone away, so we have not
# started in time.
os.exit(1)
conn, address = sock.accept()
dbg("* Connection from {}".format(address))
server = Mailserver(conn, mailbox)
server.send(b"+OK mock-pop3-server pop3 server ready")
try:
dbg("* Processing commands for connection...")
while server.process():
dbg("* Finished command")
pass
finally:
dbg("* Closing remote connection")
server.close()
except KeyboardInterrupt:
dbg("* mock-pop3-server interrupted")
return 1
except SystemExit:
dbg("* mock-pop3-server exiting")
return 1
finally:
sock.shutdown(socket.SHUT_RDWR)
sock.close()
return 0
if __name__ == "__main__":
fd, message_filename = mkstemp(prefix='message-', suffix='.txt')
dbg("Creating message file '{}'".format(message_filename))
with open(message_filename, 'wb') as f:
f.write(MESSAGE_CONTENT.lstrip())
try:
exit(serve(message_filename, host=DEFAULT_HOST, port=DEFAULT_PORT))
except KeyError:
sys.stderr.write("Exiting\n")
sys.exit(1)
|