From 7e4cace1e286e98d2d4d6b90f72671c16a30bbee Mon Sep 17 00:00:00 2001 From: Thomas Gleixner Date: Wed, 11 Mar 2020 11:57:42 +0100 Subject: [PATCH] remail/mail: Handle dwmw2s magic evo plugin dwmw2's evolution plugin which seems to workaround ms-exchange oddities has another interesting format not handled by remail yet: multipart/mixed with: - empty text/plain section - application/pgp-encrypted - application/octed-stream Check for this with at least some sanity checks and morph it into a regular multipart/encrypted message so decrypt can handle it without adding more mess to that part. Signed-off-by: Thomas Gleixner --- remail/mail.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/remail/mail.py b/remail/mail.py index 92ee364..b14a2f3 100644 --- a/remail/mail.py +++ b/remail/mail.py @@ -402,6 +402,56 @@ def msg_sanitize_outlook(msg): # code. continue +def msg_handle_multimix(msg): + ''' + Magic format used by dwmw2's devolution plugin to prevent + exchange from wreckaging mail. It's kinda valid, but sigh... + + Multipart mixed message with: + - empty text/plain + - application/pgp-encrypted + - application/octed-stream + + Make it look like a sane PGP application/encrypted mail + ''' + ct = msg.get_content_type() + if ct != 'multipart/mixed': + return + + fnames = ['msg.asc', 'encrypted.asc'] + gpgpl = 0 + otherpl = [] + + payloads = msg.get_payload() + for payload in payloads: + try: + ct = payload.get_content_type() + print(ct) + if ct == 'application/pgp-encrypted': + gpgpl += 1 + continue + elif ct == 'application/octet-stream': + fname = payload.get_filename(None) + if gpgpl == 1 and fname in fnames: + gpgpl += 1 + continue + # None of the above. Mark it as other + otherpl.append(payload) + except: + # Ignore fails here. This is all best effort + # guesswork. + pass + + if gpgpl != 2: + return + + # Remove the irrelevant payload parts + for pl in otherpl: + payloads.remove(pl) + # Fixup the message type so decrypt knows what to do with it + msg.set_type('multipart/encrypted') + msg.set_param('protocol', 'application/pgp-encrypted') + def decode_base64(msg): # # Decode base64 encoded text/plain sections @@ -436,6 +486,9 @@ def msg_sanitize_incoming(msg): # Strip html multipart first msg_strip_html(msg) + # Handle multipart/mixed + msg_handle_multimix(msg) + # Sanitize outlook crappola msg_sanitize_outlook(msg)