Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7318e52b94 | ||
|
|
f2a43a948a | ||
|
|
de41f90943 | ||
|
|
f36515f233 | ||
|
|
7423d4941d | ||
|
|
44182ddd45 | ||
|
|
6812dd0b8d | ||
|
|
9956b94649 | ||
|
|
4547c85237 |
No files matched your search
@@ -1,29 +0,0 @@
|
||||
from socketserver import StreamRequestHandler
|
||||
|
||||
ERR_CODE = {
|
||||
220 : '',
|
||||
500 : 'Syntax error, command unrecognized',
|
||||
}
|
||||
|
||||
class SmtpProcessor(object):
|
||||
def __init__(self, src_addr, rfile,wfile):
|
||||
self.src_addr = src_addr
|
||||
self.rfile = rfile
|
||||
self.wfile = wfile
|
||||
self.unrec_commands_count = 0
|
||||
|
||||
class SmtpHandler(StreamRequestHandler):
|
||||
|
||||
def handle(self):
|
||||
# self.rfile is a file-like object created by the handler;
|
||||
# we can now use e.g. readline() instead of raw recv() calls
|
||||
self.data = self.rfile.readline().strip()
|
||||
print("{} wrote:".format(self.client_address[0]))
|
||||
print(self.data)
|
||||
# Likewise, self.wfile is a file-like object used to write back
|
||||
# to the client
|
||||
self.wfile.write(self.data.upper())
|
||||
|
||||
#######################################################
|
||||
processor = SmtpProcessor(self.client_address, self.rfile, self.wfile)
|
||||
|
||||
@@ -1,38 +1,59 @@
|
||||
import socketserver, subprocess, sys
|
||||
from threading import Thread
|
||||
from pprint import pprint
|
||||
import json
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
my_unix_command = ['bc']
|
||||
HOST = 'localhost'
|
||||
PORT = 2000
|
||||
from twisted.cred.checkers import ICredentialsChecker
|
||||
from twisted.cred import credentials, error
|
||||
from twisted.python import failure
|
||||
from twisted.internet import reactor, defer
|
||||
from twisted.internet.task import LoopingCall
|
||||
|
||||
class SingleTCPHandler(socketserver.StreamRequestHandler):
|
||||
from zope.interface import implementer
|
||||
|
||||
def handle(self):
|
||||
# self.rfile is a file-like object created by the handler;
|
||||
# we can now use e.g. readline() instead of raw recv() calls
|
||||
self.data = self.rfile.readline().strip()
|
||||
print("{} wrote:".format(self.client_address[0]))
|
||||
print(self.data)
|
||||
# Likewise, self.wfile is a file-like object used to write back
|
||||
# to the client
|
||||
self.wfile.write(self.data.upper())
|
||||
from mech_smtp import SerpentSMTPFactory, smtp_portal
|
||||
from mech_imap import SerpentIMAPFactory, imap_portal
|
||||
from serpent.usrpwd import dbs
|
||||
from serpent.queue import squeue
|
||||
from serpent.config import conf
|
||||
|
||||
@implementer(ICredentialsChecker)
|
||||
class CredChecker(object):
|
||||
'''Класс проверки данных авторизации.
|
||||
Параметром в конструктор передаётся список (list()) объектов баз пользователей.'''
|
||||
credentialInterfaces = (credentials.IUsernamePassword,
|
||||
credentials.IUsernameHashedPassword)
|
||||
|
||||
def __init__(self, dbs):
|
||||
self.dbs = dbs
|
||||
|
||||
def _cbPasswordMatch(self, matched, username):
|
||||
if matched:
|
||||
return username
|
||||
else:
|
||||
return failure.Failure(error.UnauthorizedLogin())
|
||||
|
||||
def requestAvatarId(self, credentials):
|
||||
found_user = False
|
||||
for db in self.dbs:
|
||||
found_user = db.user_exist(credentials.username)
|
||||
if found_user:
|
||||
pwdfunc = db.check_pw
|
||||
break
|
||||
if found_user:
|
||||
return defer.maybeDeferred(
|
||||
pwdfunc, [credentials.username, credentials.password]).addCallback(
|
||||
self._cbPasswordMatch, str(credentials.username))
|
||||
else:
|
||||
return defer.fail(error.UnauthorizedLogin())
|
||||
|
||||
class SimpleServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||
# Ctrl-C will cleanly kill all spawned threads
|
||||
daemon_threads = True
|
||||
# much faster rebinding
|
||||
allow_reuse_address = True
|
||||
checker = CredChecker(dbs)
|
||||
smtp_portal.registerChecker(checker)
|
||||
smtp_factory = SerpentSMTPFactory(smtp_portal)
|
||||
imap_portal.registerChecker(checker)
|
||||
imap_factory = SerpentIMAPFactory(imap_portal)
|
||||
|
||||
reactor.listenTCP(2500, smtp_factory)
|
||||
reactor.listenTCP(1430, imap_factory)
|
||||
|
||||
def __init__(self, server_address, RequestHandlerClass):
|
||||
socketserver.TCPServer.__init__(self, server_address, RequestHandlerClass)
|
||||
qtask = LoopingCall(squeue.run)
|
||||
qtask.start(conf.smtp_queue_check_period)
|
||||
|
||||
if __name__ == "__main__":
|
||||
server = SimpleServer((HOST, PORT), SingleTCPHandler)
|
||||
# terminate with Ctrl-C
|
||||
try:
|
||||
server.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
sys.exit(0)
|
||||
reactor.run()
|
||||
+303
@@ -0,0 +1,303 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
|
||||
from zope.interface import implementer
|
||||
|
||||
from twisted.cred import portal
|
||||
from twisted.internet import protocol, ssl
|
||||
from twisted.mail import imap4
|
||||
|
||||
from serpent.config import conf
|
||||
from serpent.imap.mailbox import ExtendedMaildir
|
||||
from serpent.misc import IMAP_HDELIM, IMAP_MBOX_REG, IMAP_ACC_CONN_NUM
|
||||
from shutil import rmtree, move
|
||||
|
||||
@implementer(imap4.IAccount)
|
||||
class IMAPUserAccount(object):
|
||||
def __init__(self, mdir):
|
||||
if not os.path.exists(mdir):
|
||||
os.makedirs(mdir)
|
||||
self.dir = mdir
|
||||
if self.dir in IMAP_MBOX_REG.keys():
|
||||
IMAP_MBOX_REG[self.dir][IMAP_ACC_CONN_NUM] += 1
|
||||
else:
|
||||
IMAP_MBOX_REG[self.dir] = {}
|
||||
IMAP_MBOX_REG[self.dir][IMAP_ACC_CONN_NUM] = 0
|
||||
for m in conf.imap_auto_mbox:
|
||||
if m not in IMAP_MBOX_REG[self.dir].keys():
|
||||
if isinstance(m, str):
|
||||
m = m.encode('imap4-utf-7')
|
||||
IMAP_MBOX_REG[self.dir][m] = ExtendedMaildir(os.path.join(self.dir, m))
|
||||
IMAP_MBOX_REG[self.dir][m]._start_monitor()
|
||||
self.subscribe(m)
|
||||
|
||||
def _getMailbox(self, path):
|
||||
if isinstance(path, str):
|
||||
path = path.encode('imap4-utf-7')
|
||||
fullPath = os.path.join(self.dir, path)
|
||||
mbox = ExtendedMaildir(fullPath)
|
||||
mbox._start_monitor()
|
||||
return mbox
|
||||
|
||||
def listMailboxes(self, ref, wildcard):
|
||||
for box in os.listdir(self.dir):
|
||||
yield box.decode('imap4-utf-7'), self.create(box)
|
||||
|
||||
def select(self, path, rw=False):
|
||||
if isinstance(path, str):
|
||||
path = path.encode('imap4-utf-7')
|
||||
if path in IMAP_MBOX_REG[self.dir].keys():
|
||||
return IMAP_MBOX_REG[self.dir][path]
|
||||
else:
|
||||
if path in os.listdir(self.dir):
|
||||
return self.create(path)
|
||||
else:
|
||||
raise imap4.NoSuchMailbox(path)
|
||||
|
||||
def addMailbox(self, name, mbox = None):
|
||||
if mbox:
|
||||
raise NotImplementedError
|
||||
return self.create(name)
|
||||
|
||||
def create(self, pathspec):
|
||||
if isinstance(pathspec, str):
|
||||
pathspec = pathspec.encode('imap4-utf-7')
|
||||
if pathspec not in IMAP_MBOX_REG[self.dir].keys():
|
||||
paths = filter(None, pathspec.split(IMAP_HDELIM))
|
||||
for accum in range(1, len(paths)):
|
||||
subpath = IMAP_HDELIM.join(paths[:accum])
|
||||
if subpath not in IMAP_MBOX_REG[self.dir].keys():
|
||||
try:
|
||||
IMAP_MBOX_REG[self.dir][subpath] = self._getMailbox(IMAP_HDELIM.join(paths[:accum]))
|
||||
except imap4.MailboxCollision:
|
||||
pass
|
||||
IMAP_MBOX_REG[self.dir][pathspec] = self._getMailbox(pathspec)
|
||||
return IMAP_MBOX_REG[self.dir][pathspec]
|
||||
|
||||
def delete(self, pathspec):
|
||||
if isinstance(pathspec, str):
|
||||
pathspec = pathspec.encode('imap4-utf-7')
|
||||
if pathspec in conf.imap_auto_mbox:
|
||||
raise imap4.MailboxException(pathspec)
|
||||
if pathspec not in IMAP_MBOX_REG[self.dir].keys():
|
||||
raise imap4.MailboxException("No such mailbox")
|
||||
inferiors = self._inferiorNames(pathspec)
|
||||
if r'\Noselect' in IMAP_MBOX_REG[self.dir][pathspec].getFlags():
|
||||
# Check for hierarchically inferior mailboxes with this one
|
||||
# as part of their root.
|
||||
for inferior in inferiors:
|
||||
if inferior != pathspec:
|
||||
raise imap4.MailboxException("Hierarchically inferior mailboxes exist and \\Noselect is set")
|
||||
for inferior in inferiors:
|
||||
mdir = IMAP_MBOX_REG[self.dir][inferior].path
|
||||
IMAP_MBOX_REG[self.dir][inferior].destroy()
|
||||
del IMAP_MBOX_REG[self.dir][inferior]
|
||||
rmtree(mdir)
|
||||
return True
|
||||
|
||||
def rename(self, oldname, newname):
|
||||
if oldname in conf.imap_auto_mbox:
|
||||
raise imap4.MailboxException(oldname)
|
||||
if isinstance(oldname, str):
|
||||
oldname = oldname.encode('imap4-utf-7')
|
||||
if isinstance(newname, str):
|
||||
newname = newname.encode('imap4-utf-7')
|
||||
if oldname not in IMAP_MBOX_REG[self.dir].keys():
|
||||
raise imap4.NoSuchMailbox(oldname)
|
||||
inferiors = [(o, o.replace(oldname, newname, 1)) for o in self._inferiorNames(oldname)]
|
||||
for (old, new) in inferiors:
|
||||
if new in IMAP_MBOX_REG[self.dir].keys():
|
||||
raise imap4.MailboxCollision(new)
|
||||
for (old, new) in inferiors:
|
||||
move(os.path.join(self.dir, old), os.path.join(self.dir, new))
|
||||
IMAP_MBOX_REG[self.dir][new] = IMAP_MBOX_REG[self.dir][old]
|
||||
IMAP_MBOX_REG[self.dir][new].path = os.path.join(self.dir, new)
|
||||
IMAP_MBOX_REG[self.dir][new].path_msg_info = os.path.join(self.dir, conf.imap_msg_info)
|
||||
IMAP_MBOX_REG[self.dir][new].path_mbox_info = os.path.join(self.dir, conf.imap_mbox_info)
|
||||
del IMAP_MBOX_REG[self.dir][old]
|
||||
return True
|
||||
|
||||
def subscribe(self, name):
|
||||
if isinstance(name, str):
|
||||
name = name.encode('imap4-utf-7')
|
||||
if name in IMAP_MBOX_REG[self.dir].keys():
|
||||
IMAP_MBOX_REG[self.dir][name].subscribe()
|
||||
|
||||
def unsubscribe(self, name):
|
||||
if name in conf.imap_auto_mbox:
|
||||
raise imap4.MailboxException(name)
|
||||
if isinstance(name, str):
|
||||
name = name.encode('imap4-utf-7')
|
||||
if name in IMAP_MBOX_REG[self.dir].keys():
|
||||
IMAP_MBOX_REG[self.dir][name].unsubscribe()
|
||||
|
||||
def isSubscribed(self, name):
|
||||
if isinstance(name, str):
|
||||
name = name.encode('imap4-utf-7')
|
||||
return IMAP_MBOX_REG[self.dir][name].is_subscribed()
|
||||
|
||||
def _inferiorNames(self, name):
|
||||
name_l = name.split(IMAP_HDELIM)
|
||||
inferiors = []
|
||||
for infname in IMAP_MBOX_REG[self.dir].keys():
|
||||
if name_l == infname.split(IMAP_HDELIM)[:len(name_l)]:
|
||||
inferiors.append(infname)
|
||||
return inferiors
|
||||
|
||||
@implementer(portal.IRealm)
|
||||
class SerpentIMAPRealm(object):
|
||||
def requestAvatar(self, avatarId, mind, *interfaces):
|
||||
if imap4.IAccount not in interfaces:
|
||||
raise NotImplementedError(
|
||||
"This realm only supports the imap4.IAccount interface.")
|
||||
mdir = os.path.join(conf.app_dir, conf.maildir_user_path % avatarId)
|
||||
avatar = IMAPUserAccount(mdir)
|
||||
return imap4.IAccount, avatar, lambda: None
|
||||
|
||||
###############################################################################
|
||||
|
||||
class IMAPServerProtocol(imap4.IMAP4Server):
|
||||
def lineReceived(self, line):
|
||||
if isinstance(line, str):
|
||||
line = line.encode('utf-8')
|
||||
print("CLIENT:", line)
|
||||
imap4.IMAP4Server.lineReceived(self, line)
|
||||
|
||||
def sendLine(self, line):
|
||||
imap4.IMAP4Server.sendLine(self, line)
|
||||
if isinstance(line, str):
|
||||
line = line.encode('utf-8')
|
||||
print("SERVER:", line)
|
||||
|
||||
def connectionLost(self, reason):
|
||||
self.setTimeout(None)
|
||||
if self.account and self.account.dir in IMAP_MBOX_REG.keys():
|
||||
IMAP_MBOX_REG[self.account.dir][IMAP_ACC_CONN_NUM] -= 1
|
||||
if IMAP_MBOX_REG[self.account.dir][IMAP_ACC_CONN_NUM] <= 0:
|
||||
for m in IMAP_MBOX_REG[self.account.dir].keys():
|
||||
if m == IMAP_ACC_CONN_NUM:
|
||||
continue
|
||||
IMAP_MBOX_REG[self.account.dir][m].close()
|
||||
del IMAP_MBOX_REG[self.account.dir][m]
|
||||
del IMAP_MBOX_REG[self.account.dir]
|
||||
self.account = None
|
||||
|
||||
def _parseMbox(self, name):
|
||||
if isinstance(name, str):
|
||||
return name
|
||||
try:
|
||||
return name.decode('imap4-utf-7')
|
||||
except:
|
||||
#log.err()
|
||||
raise imap4.IllegalMailboxEncoding(name)
|
||||
|
||||
def _cbCopySelectedMailbox(self, mbox, tag, messages, mailbox, uid):
|
||||
if not isinstance(mbox, IMAPMailbox):
|
||||
self.sendNegativeResponse(tag, 'No such mailbox: ' + mailbox)
|
||||
else:
|
||||
imap4.maybeDeferred(self.mbox.fetch, messages, uid
|
||||
).addCallback(self.__cbCopy, tag, mbox
|
||||
).addCallback(self.__cbCopied, tag, mbox
|
||||
).addErrback(self.__ebCopy, tag
|
||||
)
|
||||
|
||||
def __cbCopy(self, messages, tag, mbox):
|
||||
# XXX - This should handle failures with a rollback or something
|
||||
addedDeferreds = []
|
||||
fastCopyMbox = imap4.IMessageCopier(mbox, None)
|
||||
for (_id, msg) in messages:
|
||||
if fastCopyMbox is not None:
|
||||
d = imap4.maybeDeferred(fastCopyMbox.copy, msg)
|
||||
addedDeferreds.append(d)
|
||||
continue
|
||||
# XXX - The following should be an implementation of IMessageCopier.copy
|
||||
# on an IMailbox->IMessageCopier adapter.
|
||||
flags = msg.getFlags()
|
||||
date = msg.getInternalDate()
|
||||
body = imap4.IMessageFile(msg, None)
|
||||
if body is not None:
|
||||
bodyFile = body.open()
|
||||
d = imap4.maybeDeferred(mbox.addMessage, bodyFile, flags, date)
|
||||
else:
|
||||
def rewind(f):
|
||||
f.seek(0)
|
||||
return f
|
||||
_buffer = imap4.tempfile.TemporaryFile()
|
||||
d = imap4.MessageProducer(msg, _buffer, self._scheduler
|
||||
).beginProducing(None
|
||||
).addCallback(lambda _, b=_buffer, f=flags, d=date: mbox.addMessage(rewind(b), f, d)
|
||||
)
|
||||
addedDeferreds.append(d)
|
||||
return imap4.defer.DeferredList(addedDeferreds)
|
||||
def __cbCopied(self, deferredIds, tag, mbox):
|
||||
ids = []
|
||||
failures = []
|
||||
for (status, result) in deferredIds:
|
||||
if status:
|
||||
ids.append(result)
|
||||
else:
|
||||
failures.append(result.value)
|
||||
if failures:
|
||||
self.sendNegativeResponse(tag, '[ALERT] Some messages were not copied')
|
||||
else:
|
||||
self.sendPositiveResponse(tag, 'COPY completed')
|
||||
def __ebCopy(self, failure, tag):
|
||||
self.sendBadResponse(tag, 'COPY failed:' + str(failure.value))
|
||||
#log.err(failure)
|
||||
|
||||
def _cbAppendGotMailbox(self, mbox, tag, flags, date, message):
|
||||
if not isinstance(mbox, IMAPMailbox):
|
||||
self.sendNegativeResponse(tag, '[TRYCREATE] No such mailbox')
|
||||
return
|
||||
d = mbox.addMessage(message, flags, date)
|
||||
d.addCallback(self.__cbAppend, tag, mbox)
|
||||
d.addErrback(self.__ebAppend, tag)
|
||||
|
||||
def __cbAppend(self, result, tag, mbox):
|
||||
self.sendUntaggedResponse('%d EXISTS' % mbox.getMessageCount())
|
||||
self.sendPositiveResponse(tag, 'APPEND complete')
|
||||
def __ebAppend(self, failure, tag):
|
||||
self.sendBadResponse(tag, 'APPEND failed: ' + str(failure.value))
|
||||
|
||||
def _cbStatusGotMailbox(self, mbox, tag, mailbox, names):
|
||||
if isinstance(mbox, IMAPMailbox):
|
||||
|
||||
imap4.maybeDeferred(mbox.requestStatus, names).addCallbacks(
|
||||
self.__cbStatus, self.__ebStatus,
|
||||
(tag, mailbox), None, (tag, mailbox), None
|
||||
)
|
||||
else:
|
||||
self.sendNegativeResponse(tag, "Could not open mailbox")
|
||||
def _ebStatusGotMailbox(self, failure, tag):
|
||||
self.sendBadResponse(tag, "Server error encountered while opening mailbox.")
|
||||
#log.err(failure)
|
||||
|
||||
def __cbStatus(self, status, tag, box):
|
||||
line = ' '.join(['%s %s' % x for x in status.iteritems()])
|
||||
if isinstance(box, str):
|
||||
box = box.encode('imap4-utf-7')
|
||||
self.sendUntaggedResponse('STATUS %s (%s)' % (box, line))
|
||||
self.sendPositiveResponse(tag, 'STATUS complete')
|
||||
def __ebStatus(self, failure, tag, box):
|
||||
self.sendBadResponse(tag, 'STATUS %s failed: %s' % (box, str(failure.value)))
|
||||
|
||||
################################################################################
|
||||
|
||||
class SerpentIMAPFactory(protocol.Factory):
|
||||
def __init__(self, portal):
|
||||
self.portal = portal
|
||||
|
||||
def buildProtocol(self, addr):
|
||||
contextFactory = None
|
||||
if conf.tls:
|
||||
tls_data = open(conf.tls_pem, 'rb').read()
|
||||
cert = ssl.PrivateCertificate.loadPEM(tls_data)
|
||||
contextFactory = cert.options()
|
||||
p = IMAPServerProtocol(contextFactory = contextFactory)
|
||||
if conf.tls:
|
||||
p.canStartTLS = True
|
||||
p.IDENT = '%s ready' % conf.SRVNAME
|
||||
p.portal = self.portal
|
||||
return p
|
||||
imap_portal = portal.Portal(SerpentIMAPRealm())
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from serpent.config import conf
|
||||
from serpent import rules
|
||||
from serpent.queue import squeue
|
||||
|
||||
|
||||
from email.header import Header
|
||||
from zope.interface import implementer
|
||||
|
||||
from twisted.internet import defer, ssl
|
||||
from twisted.mail import smtp
|
||||
from twisted.mail.imap4 import LOGINCredentials, PLAINCredentials
|
||||
|
||||
from twisted.cred.portal import IRealm, Portal
|
||||
|
||||
|
||||
|
||||
@implementer(smtp.IMessageDelivery)
|
||||
class SmtpMessageDelivery:
|
||||
def __init__(self, avatarId = None):
|
||||
self.avatarId = avatarId
|
||||
|
||||
def receivedHeader(self, helo, origin, recipients):
|
||||
header = conf.smtp_header.format(
|
||||
sender_ip = helo[1],
|
||||
sender_host = helo[0],
|
||||
srv_host = conf.smtp_hostname,
|
||||
srv_info = conf.srv_version,
|
||||
sender = conf.smtp_email_delim.join([origin.local, origin.domain]),
|
||||
id = self.messageid,
|
||||
rcpt = conf.smtp_email_delim.join([recipients[0].dest.local, recipients[0].dest.domain]),
|
||||
date = smtp.rfc822date()
|
||||
)
|
||||
return 'Received: %s' % Header(header)
|
||||
|
||||
def validateFrom(self, helo, origin): # Надо воткнуть всякие проверки хоста по HELO
|
||||
try:
|
||||
rules.validateFrom(self, [origin.local, origin.domain])
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
return origin
|
||||
|
||||
def validateTo(self, user):
|
||||
self.messageid = smtp.messageid().split('@')[0].strip('<')
|
||||
try:
|
||||
rules.validateTo(self, user)
|
||||
except:
|
||||
raise
|
||||
else:
|
||||
msg = {
|
||||
'from': [user.orig.local, user.orig.domain],
|
||||
'rcpt': [user.dest.local, user.dest.domain],
|
||||
'transaction_id': self.messageid,
|
||||
'id': smtp.messageid().split('@')[0].strip('<')
|
||||
}
|
||||
return lambda: SmtpMessage(msg)
|
||||
|
||||
@implementer(smtp.IMessage)
|
||||
class SmtpMessage:
|
||||
def __init__(self, msg):
|
||||
self.lines = []
|
||||
self.size = 0
|
||||
self.msg = msg
|
||||
|
||||
def lineReceived(self, line):
|
||||
self.lines.append(line)
|
||||
|
||||
def eomReceived(self):
|
||||
self.lines.append('')
|
||||
self.msg['message'] = "\n".join(self.lines)
|
||||
self.lines = None
|
||||
return defer.succeed(squeue.add(self.msg))
|
||||
|
||||
def connectionLost(self):
|
||||
# There was an error, throw away the stored lines
|
||||
self.lines = None
|
||||
|
||||
class SerpentESMTP(smtp.ESMTP):
|
||||
def ext_AUTH(self, rest):
|
||||
if self.canStartTLS and not self.startedTLS:
|
||||
self.sendCode(538, 'Unencrypted auth denied')
|
||||
return
|
||||
if self.authenticated:
|
||||
self.sendCode(503, 'Already authenticated')
|
||||
return
|
||||
parts = rest.split(None, 1)
|
||||
chal = self.challengers.get(parts[0].upper(), lambda: None)()
|
||||
if not chal:
|
||||
self.sendCode(504, 'Unrecognized authentication type')
|
||||
return
|
||||
self.mode = smtp.AUTH
|
||||
self.challenger = chal
|
||||
if len(parts) > 1:
|
||||
chal.getChallenge() # Discard it, apparently the client does not
|
||||
# care about it.
|
||||
rest = parts[1]
|
||||
else:
|
||||
rest = None
|
||||
self.state_AUTH(rest)
|
||||
|
||||
class SerpentSMTPFactory(smtp.SMTPFactory):
|
||||
protocol = SerpentESMTP
|
||||
|
||||
def __init__(self, *a, **kw):
|
||||
smtp.SMTPFactory.__init__(self, *a, **kw)
|
||||
self.delivery = SmtpMessageDelivery()
|
||||
|
||||
def buildProtocol(self, addr):
|
||||
contextFactory = None
|
||||
if conf.tls:
|
||||
tls_data = open(conf.tls_pem, 'rb').read()
|
||||
cert = ssl.PrivateCertificate.loadPEM(tls_data)
|
||||
contextFactory = cert.options()
|
||||
p = smtp.SMTPFactory.buildProtocol(self, addr)
|
||||
p.ctx = contextFactory
|
||||
if conf.tls:
|
||||
p.canStartTLS = True
|
||||
p.host = conf.smtp_hostname
|
||||
p.delivery = self.delivery
|
||||
p.challengers = {"LOGIN": LOGINCredentials, "PLAIN": PLAINCredentials}
|
||||
return p
|
||||
|
||||
|
||||
@implementer(IRealm)
|
||||
class SmtpRealm:
|
||||
def requestAvatar(self, avatarId, mind, *interfaces):
|
||||
if smtp.IMessageDelivery in interfaces:
|
||||
return smtp.IMessageDelivery, SmtpMessageDelivery(avatarId), lambda: None
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
|
||||
smtp_portal = Portal(SmtpRealm())
|
||||
@@ -1,11 +0,0 @@
|
||||
import socketserver
|
||||
from .. import handler.smtp as h_smtp
|
||||
|
||||
class SmtpServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
||||
# Ctrl-C will cleanly kill all spawned threads
|
||||
daemon_threads = True
|
||||
# much faster rebinding
|
||||
allow_reuse_address = True
|
||||
|
||||
def __init__(self, server_address, RequestHandlerClass):
|
||||
socketserver.TCPServer.__init__(self, server_address, RequestHandlerClass)
|
||||
File renamed without changes.
@@ -0,0 +1,36 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
class Config(object):
|
||||
pass
|
||||
|
||||
conf = Config()
|
||||
conf.VERSION = '0.1.0'
|
||||
conf.SRVNAME = 'Serpent'
|
||||
conf.srv_version = '%s %s' % (conf.SRVNAME, conf.VERSION)
|
||||
conf.local_domains = ['dom.lan'] # Список доменов, для которых будет приниматься почта
|
||||
conf.tls = True
|
||||
conf.tls_pem = './serpent.pem'
|
||||
conf.smtp_open_relay = False # Разрешить ли пересылку откуда угодно куда угодно
|
||||
conf.smtp_email_delim = '@'
|
||||
conf.smtp_header = '''from [{sender_ip}] (helo={sender_host})
|
||||
by {srv_host} with ESMTP ({srv_info})
|
||||
(envelope-from <{sender}>)
|
||||
id {id}
|
||||
for {rcpt}; {date}
|
||||
'''
|
||||
conf.smtp_hostname = 'mail.dom.lan'
|
||||
conf.app_dir = '/home/inpos/tmp/serpent'
|
||||
conf.smtp_queue_dir = 'smtp_queue'
|
||||
conf.smtp_message_size = 40 # Размер в МБ
|
||||
conf.smtp_queue_check_period = 30 # Период запуска обработки очереди в минутах
|
||||
conf.smtp_queue_message_ttl = 3 * 24 * 60 # Время жизни сообщения в очереди в минутах
|
||||
conf.maildir_user_path = 'mailstore/%s/'
|
||||
conf.smtp_email_tls_required = True
|
||||
|
||||
conf.imap_SENT = 'Sent'
|
||||
conf.imap_TRASH = 'Trash'
|
||||
conf.imap_subscribed = '.subscribed'
|
||||
conf.imap_msg_info = 'msg_info.db'
|
||||
conf.imap_mbox_info = 'mbox_info.db'
|
||||
conf.imap_auto_mbox = ['INBOX', 'Sent', 'Trash']
|
||||
conf.imap_expunge_on_close = True
|
||||
conf.imap_check_new_interval = 10.0 # Период проверки новых сообщений в ящике
|
||||
@@ -0,0 +1,76 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import pickle
|
||||
from glob import iglob
|
||||
from serpent.config import conf
|
||||
|
||||
class SmtpFileStore(object):
|
||||
def __init__(self, dpath):
|
||||
self.path = dpath
|
||||
if not os.path.exists(dpath):
|
||||
os.makedirs(dpath)
|
||||
def read(self, fid):
|
||||
try:
|
||||
with open(os.path.join(self.path, fid), 'rb') as f, open(os.path.join(self.path, fid + '.i'), 'rb') as i:
|
||||
data = pickle.load(i)
|
||||
try:
|
||||
data['message'] = f.read()
|
||||
except:
|
||||
raise
|
||||
return data
|
||||
except:
|
||||
#return False
|
||||
raise
|
||||
def write(self, data):
|
||||
fid = data['id']
|
||||
try:
|
||||
with open(os.path.join(self.path, fid), 'wb') as f, open(os.path.join(self.path, fid + '.i'), 'wb') as i:
|
||||
m = data['message']
|
||||
data['message'] = ''
|
||||
try:
|
||||
pickle.dump(data, i, 2)
|
||||
f.write(m)
|
||||
except:
|
||||
raise
|
||||
return True
|
||||
except:
|
||||
#return False
|
||||
raise
|
||||
def getinfo(self, fid):
|
||||
try:
|
||||
with open(os.path.join(self.path, fid + '.i'), 'rb') as i:
|
||||
data = pickle.load(i)
|
||||
return data
|
||||
except:
|
||||
return False
|
||||
def setinfo(self, data):
|
||||
try:
|
||||
with open(os.path.join(self.path, data['id'] + '.i'), 'wb') as i:
|
||||
pickle.dump(data, i, 2)
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
def list(self):
|
||||
return [i.split('/')[-1].rstrip('\.i') for i in iglob(self.path + '*.i')]
|
||||
def delete(self, fid):
|
||||
os.remove(os.path.join(self.path, fid + '.i'))
|
||||
os.remove(os.path.join(self.path, fid))
|
||||
|
||||
class MailDirStore(object):
|
||||
def __init__(self):
|
||||
import mailbox
|
||||
self.mbox = mailbox
|
||||
def deliver(self, user, message):
|
||||
mdir = os.path.join(conf.app_dir, conf.maildir_user_path % user)
|
||||
if not os.path.exists(mdir):
|
||||
os.makedirs(mdir)
|
||||
inbox = os.path.join(mdir, 'INBOX')
|
||||
mailbox = self.mbox.Maildir(inbox)
|
||||
msg = self.mbox.MaildirMessage(message)
|
||||
try:
|
||||
mailbox.add(msg, [])
|
||||
return True
|
||||
except:
|
||||
raise
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from twisted.mail import smtp
|
||||
### Исключения
|
||||
class SMTPAuthReqError(smtp.SMTPServerError):
|
||||
'''Класс исключения. Сообщает о необходимости авторизации.'''
|
||||
def __init__(self):
|
||||
smtp.SMTPServerError.__init__(self, 550, 'Authentication required!')
|
||||
|
||||
class SMTPNotOpenRelay(smtp.SMTPServerError):
|
||||
def __init__(self):
|
||||
smtp.SMTPServerError.__init__(self, 550, 'Not Open Relay!')
|
||||
###
|
||||
|
||||
SMTPBadRcpt = smtp.SMTPBadRcpt
|
||||
SMTPBadSender = smtp.SMTPBadSender
|
||||
File renamed without changes.
@@ -0,0 +1,37 @@
|
||||
from mailbox import Maildir
|
||||
import os
|
||||
|
||||
class ExtendedMaildir(Maildir):
|
||||
def set_flags(self, key, flags):
|
||||
sflags = sorted(flags)
|
||||
if sflags == self.get_flags(key): return True
|
||||
subpath = self._lookup(key)
|
||||
info = '2,' + ''.join(sflags)
|
||||
oldpath = os.path.join(self._path, subpath)
|
||||
newsubdir = os.path.split(subpath)[0]
|
||||
newname = key + self.colon + info
|
||||
if 'S' in sflags and newsubdir == 'new':
|
||||
newsubdir = 'cur'
|
||||
if 'S' not in sflags and newsubdir == 'cur':
|
||||
newsubdir = 'new'
|
||||
newpath = os.path.join(self._path, newsubdir, newname)
|
||||
if hasattr(os, 'link'):
|
||||
os.link(oldpath, newpath)
|
||||
os.remove(oldpath)
|
||||
else:
|
||||
os.rename(oldpath, newpath)
|
||||
self._toc[key] = os.path.join(newsubdir, newname)
|
||||
def get_flags(self, key):
|
||||
subpath = self._lookup(key)
|
||||
_, name = os.path.split(subpath)
|
||||
info = name.split(self.colon)[-1]
|
||||
if info.startswith('2,'):
|
||||
return info[2:]
|
||||
else:
|
||||
return ''
|
||||
def add_flag(self, key, flag):
|
||||
self.set_flags(key, ''.join(set(self.get_flags(key)) | set(flag)))
|
||||
def remove_flag(self, key, flag):
|
||||
if flag not in self.get_flags(key): return True
|
||||
if self.get_flags(key):
|
||||
self.set_flags(key, ''.join(set(self.get_flags(key)) - set(flag)))
|
||||
@@ -0,0 +1,15 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
MSG_ACTIVE = 0
|
||||
MSG_FROZEN = 1
|
||||
|
||||
IMAP_FLAGS = {
|
||||
'S': '\\Seen',
|
||||
'F': '\\Flagged',
|
||||
'P': '\\Passed',
|
||||
'R': '\\Replied',
|
||||
'T': '\\Trashed',
|
||||
'D': '\\Draft'
|
||||
}
|
||||
IMAP_HDELIM = '.'
|
||||
IMAP_ACC_CONN_NUM = '...ConnectionNumber...'
|
||||
IMAP_MBOX_REG = {}
|
||||
@@ -0,0 +1,23 @@
|
||||
######### Запуск приложения под twistd
|
||||
|
||||
from twisted.application import internet, service
|
||||
from somemodule import EchoFactory
|
||||
|
||||
port = 7001
|
||||
factory = EchoFactory()
|
||||
|
||||
# this is the important bit
|
||||
application = service.Application("echo") # create the Application
|
||||
echoService = internet.TCPServer(port, factory) # create the service
|
||||
# add the service to the application
|
||||
echoService.setServiceParent(application)
|
||||
|
||||
#############################################
|
||||
def startService(self):
|
||||
service.Service.startService(self)
|
||||
|
||||
def stopService(self):
|
||||
service.Service.stopService(self)
|
||||
if self._call:
|
||||
self._call.cancel()
|
||||
self._call = None
|
||||
@@ -0,0 +1,116 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from serpent.config import conf
|
||||
from serpent import dataio, misc, rules
|
||||
from datetime import datetime, timedelta
|
||||
from os import path
|
||||
from DNS import dnslookup
|
||||
from operator import itemgetter
|
||||
from smtplib import SMTP
|
||||
|
||||
class SmtpQueue(object):
|
||||
def __init__(self, store, local_delivery):
|
||||
self.stor = store
|
||||
self.local_delivery = local_delivery
|
||||
|
||||
def add(self, data):
|
||||
'''Ставит письмо в очередь'''
|
||||
data['add_time'] = datetime.utcnow()
|
||||
data['state'] = misc.MSG_ACTIVE
|
||||
w = self.stor.write(data)
|
||||
if not w:
|
||||
return False
|
||||
return self.__process_(data['id'])
|
||||
|
||||
def run(self):
|
||||
'''Запускает обработку очереди'''
|
||||
now = datetime.utcnow()
|
||||
check_delta = timedelta(minutes = conf.smtp_queue_check_period)
|
||||
expire_delta = timedelta(minutes = conf.smtp_queue_message_ttl)
|
||||
for mid in self.__list_messages_():
|
||||
info = self.stor.getinfo(mid)
|
||||
if (now - info['add_time']) >= expire_delta:
|
||||
self.stor.delete(mid)
|
||||
continue
|
||||
if (now - info['add_time']) >= check_delta:
|
||||
continue
|
||||
self.__process_(mid)
|
||||
return True
|
||||
|
||||
def __local_deliver_(self, mid):
|
||||
message = self.stor.read(mid)
|
||||
user = rules.username_by_email(message['rcpt'])
|
||||
return self.local_delivery.deliver(user, message['message'])
|
||||
|
||||
def __send_email_(self, mid):
|
||||
info = self.stor.getinfo(mid)
|
||||
try:
|
||||
mail_servers = dnslookup(info['rcpt'][1], 'mx')
|
||||
except:
|
||||
return False
|
||||
mail_servers = sorted(mail_servers, key=itemgetter(0))
|
||||
for _, mx in mail_servers:
|
||||
s = SMTP(local_hostname = conf.smtp_hostname)
|
||||
try:
|
||||
ret_code, _ = s.connect(mx, 25)
|
||||
except:
|
||||
s.quit()
|
||||
continue
|
||||
if ret_code != 220:
|
||||
s.quit()
|
||||
continue
|
||||
try:
|
||||
s.starttls()
|
||||
except:
|
||||
if conf.smtp_email_tls_required:
|
||||
s.quit()
|
||||
continue
|
||||
from_addr = conf.smtp_email_delim.join(info['from'])
|
||||
to_addr = conf.smtp_email_delim.join(info['rcpt'])
|
||||
message = self.stor.read(mid)
|
||||
try:
|
||||
s.sendmail(from_addr, [to_addr], message['message'])
|
||||
except:
|
||||
s.quit()
|
||||
continue
|
||||
s.quit()
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def __freeze_(self, mid):
|
||||
info = self.stor.getinfo(mid)
|
||||
if info:
|
||||
if info['state'] != misc.MSG_FROZEN :
|
||||
info['state'] = misc.MSG_FROZEN
|
||||
if self.stor.setinfo(info):
|
||||
return True
|
||||
else:
|
||||
return True
|
||||
return False
|
||||
|
||||
def __process_(self, mid):
|
||||
info = self.stor.getinfo(mid)
|
||||
if info:
|
||||
if info['rcpt'][1] in conf.local_domains:
|
||||
if self.__local_deliver_(mid):
|
||||
self.__remove_message_(mid)
|
||||
return True
|
||||
else:
|
||||
return self.__freeze_(mid)
|
||||
else:
|
||||
if self.__send_email_(mid):
|
||||
self.__remove_message_(mid)
|
||||
return True
|
||||
else:
|
||||
return self.__freeze_(mid)
|
||||
return False
|
||||
|
||||
def __list_messages_(self):
|
||||
return self.stor.list()
|
||||
|
||||
def __remove_message_(self, mid):
|
||||
self.stor.delete(mid)
|
||||
|
||||
mailstore = dataio.MailDirStore()
|
||||
squeue = SmtpQueue(dataio.SmtpFileStore(path.join(conf.app_dir, conf.smtp_queue_dir)),
|
||||
mailstore)
|
||||
@@ -0,0 +1,36 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from serpent.config import conf
|
||||
from serpent.usrpwd import dbs
|
||||
from serpent import errors
|
||||
'''Здесь находятся функции для проверки различных вещей.'''
|
||||
|
||||
def validateFrom(obj, email):
|
||||
if not conf.smtp_open_relay:
|
||||
if email[1] in conf.local_domains:
|
||||
if not obj.avatarId:
|
||||
raise errors.SMTPAuthReqError()
|
||||
elif obj.avatarId != username_by_email(email):
|
||||
raise errors.SMTPBadSender(conf.smtp_email_delim.join(email))
|
||||
elif obj.avatarId:
|
||||
raise errors.SMTPBadSender(conf.smtp_email_delim.join(email))
|
||||
return True
|
||||
|
||||
def validateTo(obj, user):
|
||||
local = user.dest.local
|
||||
domain = user.dest.domain
|
||||
for u, f in user.protocol._to:
|
||||
if local == u.dest.local and domain == u.dest.domain:
|
||||
del user.protocol._to[user.protocol._to.index((u,f))]
|
||||
if domain in conf.local_domains and not username_by_email([local, domain]):
|
||||
raise errors.SMTPBadRcpt(conf.smtp_email_delim.join([local, domain]))
|
||||
if domain not in conf.local_domains and not obj.avatarId and not conf.smtp_open_relay:
|
||||
raise errors.SMTPNotOpenRelay()
|
||||
return True # Адрес найден в базах пользователей
|
||||
|
||||
def username_by_email(email):
|
||||
result = None
|
||||
for db in dbs:
|
||||
result = db.username_by_email(email)
|
||||
if result:
|
||||
break
|
||||
return result
|
||||
@@ -0,0 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
userdb = {
|
||||
'user1': ['password1', 'local1', 'dom.lan'],
|
||||
'user2': ['password2', 'local2', 'dom.lan']
|
||||
}
|
||||
|
||||
class DictUDB(object):
|
||||
def __init__(self, userdb = userdb):
|
||||
self. userdb = userdb
|
||||
def username_by_email(self, email = None):
|
||||
if not email:
|
||||
return None
|
||||
for usr in self.userdb.keys():
|
||||
if email[0] == self.userdb[usr][1] and email[1] == self.userdb[usr][2]:
|
||||
return usr
|
||||
return False
|
||||
def check_pw(self, creds):
|
||||
usr = creds[0]
|
||||
pwd = creds[1]
|
||||
return usr in self.userdb.keys() and pwd == self.userdb[usr][0]
|
||||
def user_exist(self, username):
|
||||
return username in self.userdb.keys()
|
||||
dbs = [DictUDB()]
|
||||
Reference in new issue
Block a user