#!/usr/bin/env python # mailman-smime.py - demo on how to deal with smime messages in the Mailman # framework. # # usage: this script should get installed in mailman/bin. On Debian # GNU/Linux systems this is /usr/lib/mailman/bin/ . It reads a raw S/MIME # message on stdin. # Copyright (C) 2005 Tilburg University http://www.uvt.nl/ # Author: Joost van Baal # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import paths from Mailman import mm_cfg import sys import email import base64 sys.path.append('/usr/lib/mailman/Mailman/Handlers') sys.path.append('/usr/lib/mailman/Mailman/') import Utils from Mailman import Message # import pyme from pyme import core, constants, errors import pyme.constants.validity # Initialize our context. c = pyme.core.Context() # We prefer ascii armored output c.set_armor(1) c.set_protocol(pyme.util.gpgme.GPGME_PROTOCOL_CMS) # fp = file("/home/joostvb/tmp/mailtje.mail") # msg = fp.read() # msg = sys.stdin.read() # msg = email.message_from_string(msg, Message.Message) msg = email.message_from_string(sys.stdin.read(), Message.Message) if msg.is_multipart(): for submsg in msg.get_payload(): if submsg.get_content_type()=="application/x-pkcs7-signature": cms = base64.decodestring(submsg.get_payload()) print "cms contains CMS part of message" else: if msg.get_content_type()=="application/x-pkcs7-mime": cms = base64.decodestring(msg.get_payload()) print "cms contains crypted CMS stuff" # Prepare for decryption: Set up input and output buffers cipher = pyme.core.Data(cms) plain = pyme.core.Data() # Do the decryption. try: c.op_decrypt(cipher, plain) plain.seek(0,0) print plain.read() except pyme.errors.GPGMEError, ex: print ex.getstring()