Skip to content

Commit

Permalink
Refactor mail notifications, add support for SSL servers, add default…
Browse files Browse the repository at this point in the history
… mail config
  • Loading branch information
evilaliv3 committed Jan 9, 2016
1 parent ad72456 commit a50fcdb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
18 changes: 2 additions & 16 deletions tor2web/t2w.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
from tor2web.utils.daemon import Daemon, set_pdeathsig, set_proctitle
from tor2web.utils.hostsmap import HostsMap
from tor2web.utils.lists import LimitedSizeDict, List, TorExitNodeList
from tor2web.utils.mail import sendmail, sendexceptionmail
from tor2web.utils.mail import sendmail, MailException
from tor2web.utils.misc import listenTCPonExistingFD, listenSSLonExistingFD, re_sub, verify_onion
from tor2web.utils.socks import SOCKSError, SOCKS5ClientEndpoint, TLSWrapClientEndpoint
from tor2web.utils.ssl import T2WSSLContextFactory, HTTPSVerifyingContextFactory
Expand Down Expand Up @@ -810,8 +810,6 @@ def process(self):
defer.returnValue(self.contentFinish(content))

elif staticpath == "notification" and config.smtpmailto_notifications != '':
# if config.smtp_mail is configured we accept notifications

# ################################################################
# Here we need to parse POST data in x-www-form-urlencoded format
# ################################################################
Expand Down Expand Up @@ -843,13 +841,7 @@ def process(self):
message = StringIO(''.join(tmp))

try:
sendmail(config.smtpuser,
config.smtppass,
config.smtpmail,
config.smtpmailto_notifications,
message,
config.smtpdomain,
config.smtpport)
sendmail(config, message)
except Exception:
pass

Expand Down Expand Up @@ -1300,9 +1292,6 @@ def daemon_main(self):
subprocess = spawnT2W(self, self.childFDs, self.fds_https, self.fds_http)
self.subprocesses.append(subprocess.pid)

def MailException(etype, value, tb):
sendexceptionmail(self.config, etype, value, tb)

if self.config.smtpmailto_exceptions:
# if self.config.smtp_mail is configured we change the excepthook
sys.excepthook = MailException
Expand Down Expand Up @@ -1364,9 +1353,6 @@ def start_worker():
fd=fd,
factory=factory))

def MailException(etype, value, tb):
sendexceptionmail(config, etype, value, tb)

if config.smtpmailto_exceptions:
# if config.smtp_mail is configured we change the excepthook
sys.excepthook = MailException
Expand Down
14 changes: 6 additions & 8 deletions tor2web/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ def __init__(self):
self.__dict__['disable_tor_redirection'] = False
self.__dict__['disable_gettor'] = False
self.__dict__['avoid_rewriting_visible_data'] = False
self.__dict__['smtp_user'] = ''
self.__dict__['smtp_pass'] = ''
self.__dict__['smtp_mail'] = ''
self.__dict__['smtpuser'] = 'hey_you_should_change_me'
self.__dict__['smtppass'] = 'yes_you_really_should_change_me'
self.__dict__['smtpmail'] = '[email protected]'
self.__dict__['smtpmailto_exceptions'] = '[email protected]'
self.__dict__['smtpmailto_notifications'] = '[email protected]'
self.__dict__['smtpdomain'] = ''
self.__dict__['smtpport'] = 587
self.__dict__['smtpdomain'] = 'demo.globaleaks.org'
self.__dict__['smtpport'] = 9267
self.__dict__['smtpsecurity'] = 'TLS'
self.__dict__['exit_node_list_refresh'] = 600
self.__dict__['automatic_blocklist_updates_source'] = ''
self.__dict__['automatic_blocklist_updates_refresh'] = 600
Expand Down Expand Up @@ -143,7 +144,6 @@ def load(self):
exit(1)

try:

self._parser.read([self._file])

for name in self._parser.options(self._section):
Expand Down Expand Up @@ -199,7 +199,6 @@ def splitlist(self, line):

def parse(self, name):
try:

value = self._parser.get(self._section, name)

# strip any boundry whitespace just in case
Expand Down Expand Up @@ -231,7 +230,6 @@ def __setattr__(self, name, value):
return

try:

# XXX: Automagically discover variable type
self._parser.set(self._section, name, value)

Expand Down
29 changes: 20 additions & 9 deletions tor2web/utils/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from tor2web import __version__


def sendmail(authenticationUsername, authenticationSecret, fromAddress, toAddress, messageFile, smtpHost, smtpPort=25):
def sendmail(config, messageFile):
"""
Sends an email using SSLv3 over SMTP
Expand All @@ -47,18 +47,26 @@ def sendmail(authenticationUsername, authenticationSecret, fromAddress, toAddres
resultDeferred = defer.Deferred()

senderFactory = ESMTPSenderFactory(
authenticationUsername,
authenticationSecret,
fromAddress,
toAddress,
config.smtpuser.encode('utf-8'),
config.smtppass.encode('utf-8'),
config.smtpmail,
config.smtpmailto_exceptions,
messageFile,
resultDeferred,
contextFactory=contextFactory)
contextFactory=contextFactory,
requireAuthentication=True,
requireTransportSecurity=(config.smtpsecurity != 'SSL'),
retries=0,
timeout=15)

reactor.connectTCP(smtpHost, smtpPort, senderFactory)
if config.security == "SSL":
senderFactory = tls.TLSMemoryBIOFactory(contextFactory, True, senderFactory)

reactor.connectTCP(config.smtpdomain, config.smtpport, senderFactory)

return resultDeferred


def sendexceptionmail(config, etype, value, tb):
"""
Formats traceback and exception data and emails the error
Expand All @@ -67,7 +75,6 @@ def sendexceptionmail(config, etype, value, tb):
@param value: Exception string value
@param tb: Traceback string data
"""

exc_type = re.sub("(<(type|class ')|'exceptions.|'>|__main__.)", "", str(etype))

tmp = ["From: Tor2web Node %s.%s <%s>\n" % (config.nodename, config.basehost, config.smtpmail),
Expand All @@ -86,4 +93,8 @@ def sendexceptionmail(config, etype, value, tb):
info_string = ''.join(tmp)
message = StringIO(info_string)

sendmail(config.smtpuser, config.smtppass, config.smtpmail, config.smtpmailto_exceptions, message, config.smtpdomain, config.smtpport)
sendmail(config, message)


def MailException(etype, value, tb):
sendexceptionmail(config, etype, value, tb)
1 change: 1 addition & 0 deletions tor2web/utils/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from pyasn1.type import univ, constraint, char, namedtype, tag
from pyasn1.codec.der.decoder import decode
from twisted.internet import ssl
from twisted.protocols import tls


certificateAuthorityMap = {}
Expand Down

0 comments on commit a50fcdb

Please sign in to comment.