summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrebs <drebs@leap.se>2013-01-28 18:03:35 -0200
committerdrebs <drebs@leap.se>2013-01-28 18:03:35 -0200
commitca6a1e9faf7ceb2c6833cfb7049672e996be8a38 (patch)
tree715cc2962b8476db165f96d3b61448140b2b8da3
parentbadc4aedb69ec94abd7d66347d94ac817c73fff6 (diff)
parentc9ed6f3d7f368e3dc4a3027517c7d63be2176296 (diff)
Merge branch 'feature/smtp-relay' into develop
-rw-r--r--src/leap/email/smtp/smtprelay.py30
-rw-r--r--src/leap/email/smtp/tests/__init__.py4
-rw-r--r--src/leap/email/smtp/tests/test_smtprelay.py61
3 files changed, 50 insertions, 45 deletions
diff --git a/src/leap/email/smtp/smtprelay.py b/src/leap/email/smtp/smtprelay.py
index f44aeb6f..7a647717 100644
--- a/src/leap/email/smtp/smtprelay.py
+++ b/src/leap/email/smtp/smtprelay.py
@@ -39,11 +39,11 @@ class SMTPDelivery(object):
self._gpg = gpg
else:
self._gpg = GPGWrapper()
-
+
def receivedHeader(self, helo, origin, recipients):
myHostname, clientIP = helo
headerValue = "by %s from %s with ESMTP ; %s" % (
- myHostname, clientIP, smtp.rfc822date( ))
+ myHostname, clientIP, smtp.rfc822date())
# email.Header.Header used for automatic wrapping of long lines
return "Received: %s" % Header(headerValue)
@@ -76,7 +76,7 @@ class EncryptedMessage():
implements(smtp.IMessage)
SMTP_HOSTNAME = "mail.riseup.net"
- SMTP_PORT = 25
+ SMTP_PORT = 25
def __init__(self, user, gpg=None):
self.user = user
@@ -94,7 +94,7 @@ class EncryptedMessage():
def eomReceived(self):
"""Encrypt and send message."""
log.msg("Message data complete.")
- self.lines.append('') # add a trailing newline
+ self.lines.append('') # add a trailing newline
self.parseMessage()
try:
self.encrypt()
@@ -148,16 +148,20 @@ class EncryptedMessage():
log.msg("Encrypting to %s" % fp)
self.cyphertext = str(self._gpg.encrypt('\n'.join(self.body), [fp],
always_trust=always_trust))
-
+
# this will be replaced by some other mechanism of obtaining credentials
# for SMTP server.
def getSMTPInfo(self):
- f = open('/media/smtp-info.txt', 'r')
- self.smtp_host = f.readline().rstrip()
- self.smtp_port = f.readline().rstrip()
- self.smtp_username = f.readline().rstrip()
- self.smtp_password = f.readline().rstrip()
- f.close()
+ #f = open('/media/smtp-info.txt', 'r')
+ #self.smtp_host = f.readline().rstrip()
+ #self.smtp_port = f.readline().rstrip()
+ #self.smtp_username = f.readline().rstrip()
+ #self.smtp_password = f.readline().rstrip()
+ #f.close()
+ self.smtp_host = ''
+ self.smtp_port = ''
+ self.smtp_username = ''
+ self.smtp_password = ''
class GPGWrapper():
@@ -166,8 +170,8 @@ class GPGWrapper():
replaced by a more general class used throughout the project.
"""
- GNUPG_HOME = "~/.config/leap/gnupg"
- GNUPG_BINARY = "/usr/bin/gpg" # this has to be changed based on OS
+ GNUPG_HOME = "~/.config/leap/gnupg"
+ GNUPG_BINARY = "/usr/bin/gpg" # TODO: change this based on OS
def __init__(self, gpghome=GNUPG_HOME, gpgbinary=GNUPG_BINARY):
self.gpg = gnupg.GPG(gnupghome=gpghome, gpgbinary=gpgbinary)
diff --git a/src/leap/email/smtp/tests/__init__.py b/src/leap/email/smtp/tests/__init__.py
index ef5fd062..022968e1 100644
--- a/src/leap/email/smtp/tests/__init__.py
+++ b/src/leap/email/smtp/tests/__init__.py
@@ -5,6 +5,7 @@ from leap.email.smtp.smtprelay import GPGWrapper
from twisted.trial import unittest
from leap.testing.basetest import BaseLeapTest
+
class OpenPGPTestCase(unittest.TestCase, BaseLeapTest):
def setUp(self):
@@ -24,7 +25,7 @@ class OpenPGPTestCase(unittest.TestCase, BaseLeapTest):
os.mkdir(self.gnupg_home)
self.email = 'leap@leap.se'
self._gpg = GPGWrapper(gpghome=self.gnupg_home)
-
+
self.assertEqual(self._gpg.import_keys(PUBLIC_KEY).summary(),
'1 imported', "error importing public key")
self.assertEqual(self._gpg.import_keys(PRIVATE_KEY).summary(),
@@ -48,7 +49,6 @@ class OpenPGPTestCase(unittest.TestCase, BaseLeapTest):
self.assertNotEqual(text, encrypted, "failed encrypting text")
decrypted = str(self._gpg.decrypt(encrypted))
self.assertEqual(text, decrypted, "failed decrypting text")
-
# Key material for testing
diff --git a/src/leap/email/smtp/tests/test_smtprelay.py b/src/leap/email/smtp/tests/test_smtprelay.py
index 54aa6dea..109e253b 100644
--- a/src/leap/email/smtp/tests/test_smtprelay.py
+++ b/src/leap/email/smtp/tests/test_smtprelay.py
@@ -11,47 +11,50 @@ from twisted.mail.smtp import User
# some regexps
-IP_REGEX = "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])";
-HOSTNAME_REGEX = "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])";
+IP_REGEX = "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}" + \
+ "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
+HOSTNAME_REGEX = "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*" + \
+ "([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])"
IP_OR_HOST_REGEX = '(' + IP_REGEX + '|' + HOSTNAME_REGEX + ')'
-class TestSmtpRelay(tests.OpenPGPTestCase):
-
- EMAIL_DATA = [ 'HELO relay.leap.se',
- 'MAIL FROM: <user@leap.se>',
- 'RCPT TO: <leap@leap.se>',
- 'DATA',
- 'From: User <user@leap.se>',
- 'To: Leap <leap@leap.se>',
- 'Date: ' + datetime.now().strftime('%c'),
- 'Subject: test message',
- '',
- 'This is a secret message.',
- 'Yours,',
- 'A.',
- '',
- '.',
- 'QUIT' ]
+class TestSmtpRelay(tests.OpenPGPTestCase):
+ EMAIL_DATA = ['HELO relay.leap.se',
+ 'MAIL FROM: <user@leap.se>',
+ 'RCPT TO: <leap@leap.se>',
+ 'DATA',
+ 'From: User <user@leap.se>',
+ 'To: Leap <leap@leap.se>',
+ 'Date: ' + datetime.now().strftime('%c'),
+ 'Subject: test message',
+ '',
+ 'This is a secret message.',
+ 'Yours,',
+ 'A.',
+ '',
+ '.',
+ 'QUIT']
def assertMatch(self, string, pattern, msg=None):
if not re.match(pattern, string):
msg = self._formatMessage(msg, '"%s" does not match pattern "%s".'
- % (string, pattern))
+ % (string, pattern))
raise self.failureException(msg)
-
def test_relay_accepts_valid_email(self):
"""
Test if SMTP server responds correctly for valid interaction.
"""
- SMTP_ANSWERS = [ '220 ' + IP_OR_HOST_REGEX + ' NO UCE NO UBE NO RELAY PROBES',
- '250 ' + IP_OR_HOST_REGEX + ' Hello ' + IP_OR_HOST_REGEX + ', nice to meet you',
- '250 Sender address accepted',
- '250 Recipient address accepted',
- '354 Continue' ]
- proto = SMTPFactory(self._gpg).buildProtocol(('127.0.0.1',0))
+
+ SMTP_ANSWERS = ['220 ' + IP_OR_HOST_REGEX +
+ ' NO UCE NO UBE NO RELAY PROBES',
+ '250 ' + IP_OR_HOST_REGEX + ' Hello ' +
+ IP_OR_HOST_REGEX + ', nice to meet you',
+ '250 Sender address accepted',
+ '250 Recipient address accepted',
+ '354 Continue']
+ proto = SMTPFactory(self._gpg).buildProtocol(('127.0.0.1', 0))
transport = proto_helpers.StringTransport()
proto.makeConnection(transport)
for i, line in enumerate(self.EMAIL_DATA):
@@ -60,9 +63,8 @@ class TestSmtpRelay(tests.OpenPGPTestCase):
'\r\n'.join(SMTP_ANSWERS[0:i+1]))
proto.setTimeout(None)
-
def test_message_encrypt(self):
- proto = SMTPFactory(self._gpg).buildProtocol(('127.0.0.1',0))
+ proto = SMTPFactory(self._gpg).buildProtocol(('127.0.0.1', 0))
user = User('leap@leap.se', 'relay.leap.se', proto, 'leap@leap.se')
m = EncryptedMessage(user, self._gpg)
for line in self.EMAIL_DATA[4:12]:
@@ -71,4 +73,3 @@ class TestSmtpRelay(tests.OpenPGPTestCase):
m.encrypt()
decrypted = str(self._gpg.decrypt(m.cyphertext))
self.assertEqual('\n'.join(self.EMAIL_DATA[9:12]), decrypted)
-