# HG changeset patch # User Atul Varma # Date 1205890492 0 # Node ID e033c476f97a53e4f3a9ae5a7b2ccf9c0f9b900a Origination diff -r 000000000000 -r e033c476f97a smtpserver.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/smtpserver.py Wed Mar 19 01:34:52 2008 +0000 @@ -0,0 +1,44 @@ +import os +import sys +import smtpd +import smtplib +import asyncore +import grp +import pwd + +class MyServer( smtpd.SMTPServer ): + import smtpconfig as config + + def __init__( self ): + smtpd.SMTPServer.__init__( self, self.config.my_addr, None ) + + def bind( self, addr ): + retval = smtpd.SMTPServer.bind( self, addr ) + gid = grp.getgrnam( self.config.gid )[2] + uid = pwd.getpwnam( self.config.uid )[2] + os.setgid( gid ) + os.setuid( uid ) + print "Bound to %s and changed user to %s." % ( addr, + self.config.uid ) + return retval + + def process_message( self, peer, mailfrom, rcpttos, data ): + if peer[0] != "127.0.0.1": + print "not from localhost: %s" % peer + return + print "sending message from %s to %s" % (mailfrom, rcpttos) + server = smtplib.SMTP( self.config.server, self.config.port ) + server.set_debuglevel( 1 ) + server.login( self.config.username, self.config.password ) + server.sendmail( mailfrom, rcpttos, data ) + server.quit() + print "done." + +if __name__ == "__main__": + if os.getuid() != 0: + print "This program must be run as root." + sys.exit( -1 ) + server = MyServer() + print "Listening for incoming connections on port %s." % \ + server.config.my_addr[1] + asyncore.loop()