[Kolab-devel] wallace module gpgencrypt - dealing with attachments

Jan Kowalsky <[email protected]>
Newsgroups gmane.comp.kde.devel.kolab
Message-ID <559BA516.9060501__11190.7315128066$1436263735$gmane$org@datenkollektiv.net>
Hi all,

I took a look at this great idea of Jeroen to encrypt all incoming
messages through wallace with the module_gpgencrypt.

Since it didn't work with multipart messages yet I wrote some lines to
handle them (as a bloody python beginner).

Is there anybody still working on this module? Would it make sense to
write some documentation on it?

Best Regards
Jan

_______________________________________________
devel mailing list
[email protected]
https://lists.kolab.org/mailman/listinfo/devel
module_gpgencrypt.py.diff (text/x-diff, 3 KB)
--- /usr/share/pyshared/wallace/module_gpgencrypt.py.orig	2015-07-07 09:57:46.236680192 +0200
+++ /usr/share/pyshared/wallace/module_gpgencrypt.py	2015-07-07 11:26:13.426373068 +0200
@@ -28,6 +28,12 @@
 from email.utils import formataddr
 from email.utils import getaddresses
 
+import email.mime.application
+import email.mime.multipart
+import email.mime.text
+import email.encoders
+import gnupg
+
 import modules
 
 import pykolab
@@ -45,6 +51,40 @@
 def description():
     return """Encrypt messages to the recipient(s)."""
 
+def pgp_mime(msg, recepients):
+	gpg = gnupg.GPG(gnupghome='/var/lib/kolab/.gnupg', verbose=conf.debuglevel > 8)
+	gpg.encoding = 'utf-8'
+
+	msg = msg
+
+	msg_boundary = str(msg.get_boundary())
+	msg_content_type = str(msg.get_content_type()) 
+	payload = msg.get_payload()
+
+	content = "Content-Type: " + msg_content_type + ";" + "\n boundary=\"" + msg_boundary + "\"\n\n" + payload
+	encrypted_content = gpg.encrypt(content, recepients, always_trust=True) 
+	msg.set_type("multipart/encrypted")
+	msg.set_param("protocol","application/pgp-encrypted")
+	
+	msg_boundary_gpg = "--boundary-gpg-encryption-42"
+
+	msg_gpg_content = "This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156)\n\
+" + msg_boundary_gpg + "\n\
+Content-Type: application/pgp-encrypted\n\
+Content-Description: PGP/MIME version identification\n\
+\n\
+Version: 1\n\
+\n\
+" + msg_boundary_gpg + "\n\
+Content-Type: application/octet-stream; name=\"encrypted.asc\"\n\
+Content-Description: OpenPGP encrypted message\n\
+Content-Disposition: inline; filename=\"encrypted.asc\"\n\n"
+	
+	msg.set_boundary(msg_boundary_gpg)
+	msg.set_payload(msg_gpg_content + str(encrypted_content) + "\n" + msg_boundary_gpg)
+
+	return msg
+
 def execute(*args, **kw):
     if not os.path.isdir(mybasepath):
         os.makedirs(mybasepath)
@@ -211,16 +251,29 @@
                 encrypt_rcpts.append(key_local)
 
         payload = message.get_payload()
-        print "payload:", payload
+        #print "payload:", payload
         if len(encrypt_rcpts) < 1:
             return filepath
 
-        encrypted_data = gpg.encrypt(payload, encrypt_rcpts, always_trust=True)
-        encrypted_string = str(encrypted_data)
+    	if "multipart" in message.get_content_type():
+
+            	log.debug(_("Mime Message - we need to build multipart/encrypted structure"), level=8)
+		
+		msg = message
+		enc_mime_message = pgp_mime(msg, encrypt_rcpts)
+
+		message = enc_mime_message	
+
+	else:
+
+            	log.debug(_("No Mime Message - encypt plain"), level=8)
+
+		encrypted_data = gpg.encrypt(payload, encrypt_rcpts, always_trust=True)
+		encrypted_string = str(encrypted_data)
 
-        print "encrypted string:", encrypted_string
+		message.set_payload(encrypted_string)
 
-        message.set_payload(encrypted_string)
+	message.add_header('X-wallace-gpg-encrypted', 'true')
 
         (fp, new_filepath) = tempfile.mkstemp(dir="/var/spool/pykolab/wallace/gpgencrypt/ACCEPT")
         os.write(fp, message.as_string())
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.