--- smtpd_orig.py 2010-05-10 22:53:42.926320614 -0600 +++ smtpd.py 2010-05-15 08:07:59.912315054 -0600 @@ -1,5 +1,5 @@ #! /usr/bin/env python -"""An RFC 2821 smtp proxy. +"""An RFC 5321 smtp proxy. Usage: %(program)s [options] [localhost:localport [remotehost:remoteport]] @@ -35,10 +35,9 @@ and if remoteport is not given, then 25 is used. """ - # Overview: # -# This file implements the minimal SMTP protocol as defined in RFC 821. It +# 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: # @@ -59,15 +58,18 @@ # gets forwarded to a real backend smtpd, as with PureProxy. Again, errors # are not handled correctly yet. # -# Please note that this script requires Python 2.0 +# Please note that this script requires Python 3.0 # # Author: Barry Warsaw # +# Contributors: +# Alberto Trevino +# # TODO: # # - support mailbox delivery # - alias files -# - ESMTP +# - Handle more ESMTP extensions # - handle error codes from the backend smtpd import sys @@ -82,7 +84,7 @@ __all__ = ["SMTPServer","DebuggingServer","PureProxy","MailmanProxy"] program = sys.argv[0] -__version__ = 'Python SMTP proxy version 0.2' +__version__ = 'Python SMTP proxy version 0.21' class Devnull: @@ -96,7 +98,6 @@ COMMASPACE = ', ' - def usage(code, msg=''): print(__doc__ % globals(), file=sys.stderr) if msg: @@ -104,7 +105,6 @@ sys.exit(code) - class SMTPChannel(asynchat.async_chat): COMMAND = 0 DATA = 1 @@ -122,6 +122,8 @@ self.__data = '' self.__fqdn = socket.getfqdn() self.__peer = conn.getpeername() + self.__8bitmime = False + self.__size = 33554432 # 32 MiB print('Peer:', repr(self.__peer), file=DEBUGSTREAM) self.push('220 %s %s' % (self.__fqdn, __version__)) self.set_terminator(b'\r\n') @@ -153,7 +155,7 @@ arg = line[i+1:].strip() method = getattr(self, 'smtp_' + command, None) if not method: - self.push('502 Error: command "%s" not implemented' % command) + self.push('500 Error: command "%s" not recognized' % command) return method(arg) return @@ -162,7 +164,7 @@ self.push('451 Internal confusion') return # Remove extraneous carriage returns and de-transparency according - # to RFC 821, Section 4.5.2. + # to RFC 5321, Section 4.5.2. data = [] for text in line.split('\r\n'): if text and text[0] == '.': @@ -170,18 +172,39 @@ else: data.append(text) self.__data = NEWLINE.join(data) - status = self.__server.process_message(self.__peer, - self.__mailfrom, - self.__rcpttos, - self.__data) - self.__rcpttos = [] - self.__mailfrom = None - self.__state = self.COMMAND - self.set_terminator(b'\r\n') - if not status: - self.push('250 Ok') + + # Enforce data size limit + if len(self.__data) <= self.__size: + status = self.__server.process_message(self.__peer, + self.__mailfrom, + self.__rcpttos, + self.__data) + self.__rcpttos = [] + self.__mailfrom = None + self.__state = self.COMMAND + self.set_terminator(b'\r\n') + if not status: + self.push('250 Ok') + else: + self.push(status) else: - self.push(status) + self.__state = self.COMMAND + self.set_terminator(b'\r\n') + self.push('552 Too much mail data') + + # factored + def __getaddr(self, keyword, arg): + address = None + keylen = len(keyword) + if arg[:keylen].upper() == keyword: + address = arg[keylen:].strip() + if not address: + pass + elif address[0] == '<' and address[-1] == '>' and address != '<>': + # Addresses can be in the form but watch out + # for null address, e.g. <> + address = address[1:-1] + return address # SMTP and ESMTP commands def smtp_HELO(self, arg): @@ -193,7 +216,20 @@ else: self.__greeting = arg self.push('250 %s' % self.__fqdn) - + + def smtp_EHLO(self, arg): + if not arg: + self.push('501 Syntax: EHLO hostname') + return + if self.__greeting: + self.push('503 Duplicate HELO/EHLO') + else: + self.__greeting = arg + self.push('250-%s' % self.__fqdn) + self.push('250-8BITMIME') + self.push('250-SIZE %s' % self.__size) + self.push('250 HELP') + def smtp_NOOP(self, arg): if arg: self.push('501 Syntax: NOOP') @@ -204,20 +240,24 @@ # args is ignored self.push('221 Bye') self.close_when_done() - - # factored - def __getaddr(self, keyword, arg): - address = None - keylen = len(keyword) - if arg[:keylen].upper() == keyword: - address = arg[keylen:].strip() - if not address: - pass - elif address[0] == '<' and address[-1] == '>' and address != '<>': - # Addresses can be in the form but watch out - # for null address, e.g. <> - address = address[1:-1] - return address + + def smtp_8BITMIME(self, arg): + # There is nothing in this code that forces 7 bits, so it seems OK + # to simply accept this command; its value is saved for future use; + # args is ignored + self.__8bitmime = True + self.push('250 Ok') + + def smtp_HELP(self, arg): + ## TODO: implement command help + self.push('SUPPORTED COMMANDS: EHLO HELO MAIL RCPT DATA RSET NOOP QUIT VRFY') + + def smtp_VRFY(self, arg): + address = self.__getaddr('', arg) if arg else None + if address: + self.push('252 Cannot VRFY user, but will accept message and attempt delivery') + else: + self.push('502 Could not VRFY %s' % arg) def smtp_MAIL(self, arg): print('===> MAIL', arg, file=DEBUGSTREAM) @@ -266,9 +306,12 @@ self.__state = self.DATA self.set_terminator(b'\r\n.\r\n') self.push('354 End data with .') + + # Commands that have not been implemented + def smtp_EXPN(self, arg): + self.push('502 EXPN not implemented') - class SMTPServer(asyncore.dispatcher): def __init__(self, localaddr, remoteaddr): self._localaddr = localaddr @@ -314,7 +357,6 @@ raise NotImplementedError - class DebuggingServer(SMTPServer): # Do something with the gathered message def process_message(self, peer, mailfrom, rcpttos, data): @@ -330,7 +372,6 @@ print('------------ END MESSAGE ------------') - class PureProxy(SMTPServer): def process_message(self, peer, mailfrom, rcpttos, data): lines = data.split('\n') @@ -371,7 +412,6 @@ return refused - class MailmanProxy(PureProxy): def process_message(self, peer, mailfrom, rcpttos, data): from io import StringIO @@ -450,13 +490,11 @@ msg.Enqueue(mlist, torequest=1) - class Options: setuid = 1 classname = 'PureProxy' - def parseargs(): global DEBUGSTREAM try: @@ -513,7 +551,6 @@ return options - if __name__ == '__main__': options = parseargs() # Become nobody