Skip to content

Instantly share code, notes, and snippets.

@ltvolks
Created April 6, 2010 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ltvolks/357396 to your computer and use it in GitHub Desktop.
Save ltvolks/357396 to your computer and use it in GitHub Desktop.
# Assumes user/pass of admin/aaa, md5 hashed in the server password file
def logincram(user='admin', password='47bce5c74f589f4867dbd57e9ca9f808', port=2025):
s = smtplib.SMTP(host='localhost', port=port)
s.ehlo()
s.esmtp_features["auth"] = 'CRAM-MD5'
s.debuglevel = 5
s.login(user, password)
def loginplain(user='admin', password='aaa', port=2025):
s = smtplib.SMTP(host='localhost', port=port)
s.ehlo()
s.esmtp_features["auth"] = 'LOGIN'
s.debuglevel = 5
s.login(user, password)
import os, md5
from twisted.mail import mail, relaymanager, imap4
from twisted.cred import checkers, credentials
from twisted.python import log
from twisted.application import internet
from twisted.application import service
from twisted.internet import defer
SMTP_PORT = 2025
QUEUE_PATH = './users_home/insertionQueue'
if not os.path.exists(QUEUE_PATH):
os.makedirs(QUEUE_PATH)
def _hash(name, clearpsw, hashedpsw):
return md5.md5(clearpsw).hexdigest()
class MyFilePasswordDB(checkers.FilePasswordDB):
def __init__(self, filename, delim=':', usernameField=0, passwordField=1,
caseSensitive=True, hash=None, cache=False):
checkers.FilePasswordDB.__init__(self, filename, delim, usernameField, passwordField,
caseSensitive, hash, cache)
self.credentialInterfaces = (
credentials.IUsernamePassword,
credentials.IUsernameHashedPassword
)
def requestAvatarId(self, c):
try:
u, p = self.getUser(c.username)
except KeyError:
return defer.fail(error.UnauthorizedLogin())
else:
up = credentials.IUsernamePassword(c, None)
if self.hash and credentials.IUsernameHashedPassword(c, None) is None:
if up is not None:
h = self.hash(up.username, up.password, p)
if h == p:
return defer.succeed(u)
return defer.fail(error.UnauthorizedLogin())
else:
return defer.maybeDeferred(c.checkPassword, p
).addCallback(self._cbPasswordMatch, u)
smtpusers = MyFilePasswordDB('smtppasswords.txt', caseSensitive=True, hash=_hash, cache=True)
mailservice = mail.MailService()
mailservice.setQueue(relaymanager.Queue(QUEUE_PATH))
mailservice.smtpPortal.registerChecker(smtpusers)
smtpserver = mailservice.getESMTPFactory()
# Add PLAIN auth challenger
smtpserver.challengers['PLAIN'] = imap4.PLAINCredentials
application = service.Application("Console SMTP Server")
internet.TCPServer(SMTP_PORT, smtpserver).setServiceParent(application)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment