summaryrefslogtreecommitdiff
path: root/src/leap/mx
diff options
context:
space:
mode:
authorMicah Anderson <micah@riseup.net>2013-08-13 15:49:55 -0400
committerMicah Anderson <micah@riseup.net>2013-08-13 15:49:55 -0400
commit327dac28be41ac5204a777025e212b78a268c9d4 (patch)
treedd62cac222f1311f9df26ca8f2e90133c43dd937 /src/leap/mx
parent54c3fe7a1240dbd2875c21fda9949fb8e91ecdef (diff)
parent5ae019af6e29697dd43700c614e06298d719ff96 (diff)
update to 0.3.0
Diffstat (limited to 'src/leap/mx')
-rw-r--r--src/leap/mx/__init__.py3
-rw-r--r--src/leap/mx/alias_resolver.py50
-rw-r--r--src/leap/mx/check_recipient_access.py9
-rw-r--r--src/leap/mx/mail_receiver.py16
4 files changed, 59 insertions, 19 deletions
diff --git a/src/leap/mx/__init__.py b/src/leap/mx/__init__.py
index 61c9a5c..9cbe2a3 100644
--- a/src/leap/mx/__init__.py
+++ b/src/leap/mx/__init__.py
@@ -17,4 +17,5 @@
"""
Module initialization file for leap.mx .
"""
-__version__ = "0.2.2"
+__version__ = "0.3.0"
+
diff --git a/src/leap/mx/alias_resolver.py b/src/leap/mx/alias_resolver.py
index 2074ee5..08ebb73 100644
--- a/src/leap/mx/alias_resolver.py
+++ b/src/leap/mx/alias_resolver.py
@@ -19,6 +19,8 @@
"""
Classes for resolving postfix aliases.
+Test this with postmap -v -q "foo" tcp:localhost:4242
+
TODO:
o Look into using twisted.protocols.postfix.policies classes for
controlling concurrent connections and throttling resource consumption.
@@ -29,39 +31,69 @@ try:
# from twisted.mail import alias
from twisted.protocols import postfix
from twisted.python import log
+ from twisted.internet import defer
except ImportError:
print "This software requires Twisted. Please see the README file"
print "for instructions on getting required dependencies."
+class LEAPPostFixTCPMapserver(postfix.PostfixTCPMapServer):
+ def _cbGot(self, value):
+ if value is None:
+ self.sendCode(500, postfix.quote("NOT FOUND SRY"))
+ else:
+ self.sendCode(200, postfix.quote(value))
+
+
class AliasResolverFactory(postfix.PostfixTCPMapDeferringDictServerFactory):
+
+ protocol = LEAPPostFixTCPMapserver
+
def __init__(self, couchdb, *args, **kwargs):
postfix.PostfixTCPMapDeferringDictServerFactory.__init__(
self, *args, **kwargs)
self._cdb = couchdb
def _to_str(self, result):
+ """
+ Properly encodes the result string if any.
+ """
if isinstance(result, unicode):
result = result.encode("utf8")
if result is None:
log.msg("Result not found")
return result
+ def spit_result(self, result):
+ """
+ Formats the return codes in a postfix friendly format.
+ """
+ if result is None:
+ return None
+ else:
+ return defer.succeed(result)
+
def get(self, key):
+ """
+ Looks up the passed key, but only up to the username id of the key.
+
+ At some point we will have to consider the domain part too.
+ """
try:
log.msg("Processing key: %s" % (key,))
if key.find("@") == -1:
- log.msg("Ignoring key since it's not an email address")
- return None
-
- key = key.split("@")[0]
- key = key.split("+")[0]
+ # No proper email address, but we need to continue processing
+ # the query so postmap is happy.
+ log.msg("Key it's not an email address")
+ else:
+ key = key.split("@")[0]
+ key = key.split("+")[0]
log.msg("Final key to query: %s" % (key,))
d = self._cdb.queryByLoginOrAlias(key)
+
d.addCallback(self._to_str)
+ d.addCallback(self.spit_result)
d.addErrback(log.err)
return d
- except:
- log.err()
-
- return None
+ except Exception as e:
+ log.err('exception in get: %r' % e)
diff --git a/src/leap/mx/check_recipient_access.py b/src/leap/mx/check_recipient_access.py
index 1b44504..0520c7c 100644
--- a/src/leap/mx/check_recipient_access.py
+++ b/src/leap/mx/check_recipient_access.py
@@ -18,6 +18,8 @@
"""
Classes for resolving postfix recipient access
+
+Test this with postmap -v -q "foo" tcp:localhost:2244
"""
from twisted.protocols import postfix
@@ -25,13 +27,14 @@ from twisted.protocols import postfix
from leap.mx.alias_resolver import AliasResolverFactory
-class CheckRecipientAccess(postfix.PostfixTCPMapServer):
+class LEAPPostFixTCPMapserverAccess(postfix.PostfixTCPMapServer):
def _cbGot(self, value):
if value is None:
- self.sendCode(500)
+ self.sendCode(500, postfix.quote("NOT FOUND SORRY"))
else:
+ # We do not send the value in this case
self.sendCode(200)
class CheckRecipientAccessFactory(AliasResolverFactory):
- protocol = CheckRecipientAccess
+ protocol = LEAPPostFixTCPMapserverAccess
diff --git a/src/leap/mx/mail_receiver.py b/src/leap/mx/mail_receiver.py
index b4e0d18..a830fa1 100644
--- a/src/leap/mx/mail_receiver.py
+++ b/src/leap/mx/mail_receiver.py
@@ -116,21 +116,25 @@ class MailReceiver(Service):
if pubkey is None or len(pubkey) == 0:
doc.content = {
+ "incoming": True,
"_enc_scheme": EncryptionSchemes.NONE,
"_enc_json": json.dumps(data)
}
return uuid, doc
- def _ascii_to_openpgp_cb(gpg):
+ openpgp_key = None
+ with openpgp.TempGPGWrapper(gpgbinary='/usr/bin/gpg') as gpg:
+ gpg.import_keys(pubkey)
key = gpg.list_keys().pop()
- return openpgp._build_key_from_gpg(address, key, pubkey)
-
- openpgp_key = openpgp._safe_call(_ascii_to_openpgp_cb, pubkey)
+ openpgp_key = openpgp._build_key_from_gpg(address, key, pubkey)
doc.content = {
+ "incoming": True,
"_enc_scheme": EncryptionSchemes.PUBKEY,
- "_enc_json": openpgp.encrypt_asym(json.dumps(data),
- openpgp_key)
+ "_enc_json": str(gpg.encrypt(
+ json.dumps(data),
+ openpgp_key.fingerprint,
+ symmetric=False))
}
return uuid, doc