summaryrefslogtreecommitdiffstats
path: root/wallace/module_gpgencrypt.py
blob: e623fe140ab8364ed250f7552ee76f31a17a8d1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# -*- coding: utf-8 -*-
# Copyright 2010-2013 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# 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; version 3 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 Library 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 os
import tempfile
import time

from email import message_from_string
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.parser import Parser
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

from pykolab.translate import _

log = pykolab.getLogger('pykolab.wallace/gpgencrypt')
extra_log_params = {'qid': '-'}
log = pykolab.logger.LoggerAdapter(log, extra_log_params)
conf = pykolab.getConf()

mybasepath = '/var/spool/pykolab/wallace/gpgencrypt/'

def __init__():
    modules.register('gpgencrypt', execute, description=description())

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_preamble = "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_preamble + str(encrypted_content) + "\n" + msg_boundary_gpg)

    return msg

def execute(*args, **kw):
    global extra_log_params

    # TODO: Test for correct call.
    filepath = args[0]

    extra_log_params['qid'] = os.path.basename(filepath)

    if not os.path.isdir(mybasepath):
        os.makedirs(mybasepath)

    for stage in ['incoming', 'ACCEPT' ]:
        if not os.path.isdir(os.path.join(mybasepath, stage)):
            os.makedirs(os.path.join(mybasepath, stage))

    if kw.has_key('stage'):
        log.debug(_("Issuing callback after processing to stage %s") % (kw['stage']), level=8)
        log.debug(_("Testing cb_action_%s()") % (kw['stage']), level=8)
        if hasattr(modules, 'cb_action_%s' % (kw['stage'])):
            log.debug(_("Attempting to execute cb_action_%s()") % (kw['stage']), level=8)
            exec('modules.cb_action_%s(%r, %r)' % (kw['stage'],'gpgencrypt',filepath))

    log.debug(_("Executing module gpgencrypt for %r, %r") % (args, kw), level=8)

    new_filepath = os.path.join('/var/spool/pykolab/wallace/gpgencrypt/incoming', os.path.basename(filepath))

    if not filepath == new_filepath:
        log.debug("Renaming %r to %r" % (filepath, new_filepath))
        os.rename(filepath, new_filepath)
        filepath = new_filepath

    # parse message headers
    # @TODO: make sure we can use True as the 2nd argument here
    message = Parser().parse(open(filepath, 'r'), True)

    # Possible gpgencrypt answers are limited to ACCEPT only
    answers = [ 'ACCEPT' ]

# from Mail::GnuPG.is_encrypted
# 
#sub is_encrypted {
#  my ($self,$entity) = @_;
#  return 1
#    if (($entity->effective_type =~ m!multipart/encrypted!)
#    ||
#    ($entity->as_string =~ m!^-----BEGIN PGP MESSAGE-----!m));
#  return 0;
#}

    message_already_encrypted = False

    for part in message.walk():
        if part.get_content_type() in [ "application/pgp-encrypted" ]:
            message_already_encrypted = True
            log.debug(_("Message is already encrypted (app/pgp-enc content-type)"), level=8)

    if message.get_content_type() in [ "multipart/encrypted" ]:
        message_already_encrypted = True
        log.debug(_("Message already encrypted by main content-type header"), level=8)

    if message_already_encrypted:
        return filepath

    try:
        # What are recipient addresses to encrypt to (bitmask)?
        # 1 - organization key
        # 2 - envelope to
        # 4 - to
        # 8 - cc
        # 16 - resent-to
        # 32 - resent-cc
        encrypt_to_rcpts = conf.get('wallace', 'gpgencrypt_to_rcpts')
        if encrypt_to_rcpts == None:
            encrypt_to_rcpts = 14
        else:
            encrypt_to_rcpts = (int)(encrypt_to_rcpts)

        # Only encrypt to keys that are trusted
        strict_crypt = conf.get('wallace', 'gpgencrypt_strict')
        if strict_crypt == None:
            strict_crypt = False

        # Organization key ID
        if encrypt_to_rcpts & 1:
            encrypt_to_org = conf.get('wallace', 'gpgencrypt_to_org_key')
            if encrypt_to_org == None and encrypt_to_rcpts & 1:
                if strict_crypt:
                    log.error(_("Configured to encrypt to a key not configured, and strict policy enabled. Bailing out."))
                    modules.cb_action_REJECT('gpgencrypt',filepath)
                else:
                    log.error(_("Configured to encrypt to a key not configured, but continuing anyway (see 'gpgencrypt_strict')."))
        else:
            encrypt_to_org = []

        # Bounce the message if encryption fails?
        force_crypt = conf.get('wallace', 'gpgencrypt_force')
        if force_crypt == None:
            force_crypt = False

        # Retrieve keys from remote server(s) automatically?
        retrieve_keys = conf.get('wallace', 'gpgencrypt_retrieve_keys')
        if retrieve_keys == None:
            retrieve_keys = False

        if retrieve_keys:
            gpgserver = conf.get('wallace', 'gpgencrypt_server')
            if gpgserver == None:
                gpgserver = 'pgp.mit.edu'

        encrypt_to = []
        if encrypt_to_rcpts & 2:
            encrypt_to.extend(message.get_all('X-Kolab-To', []))

        if encrypt_to_rcpts & 4:
            encrypt_to.extend(message.get_all('to', []))

        if encrypt_to_rcpts & 8:
            encrypt_to.extend(message.get_all('cc', []))

        if encrypt_to_rcpts & 16:
            encrypt_to.extend(message.get_all('resent-to', []))

        if encrypt_to_rcpts & 32:
            encrypt_to.extend(message.get_all('resent-cc', []))

        recipients = [address for displayname,address in getaddresses(encrypt_to)]

        log.debug(_("Recipients: %r") % (recipients))

        # Split between recipients we can encrypt for/to, and ones we can not
        encrypt_rcpts = []
        nocrypt_rcpts = []


        gpg = gnupg.GPG(gnupghome='/var/lib/kolab/.gnupg', verbose=conf.debuglevel > 8)
        gpg.encoding = 'utf-8'

        local_keys = gpg.list_keys()
        log.debug(_("Current keys: %r") % (local_keys), level=8)

        for recipient in recipients:
            key_local = False

            log.debug(_("Retrieving key for recipient: %r") % (recipient))

            for key in local_keys:
                for address in [x for x in [address for displayname,address in getaddresses(key['uids'])] if x == recipient]:
                    log.debug(_("Found matching address %r") % (address))
                    key_local = key['keyid']

            if key_local == False:
                if retrieve_keys:
                    remote_keys = gpg.search_keys(recipient, gpgserver)
                    if len(remote_keys) == 1:
                        for address in [x for x in [address for displayname,address in getaddresses(remote_keys[0]['uids'])] if x == recipient]:
                            log.debug(_("Found matching address %r in remote keys") % (address))
                            gpg.recv_keys(gpgserver, remote_keys[0]['keyid'])
                            local_keys = gpg.list_keys()
                    else:
                        nocrypt_rcpts.append(recipient)

            for key in local_keys:
                for address in [x for x in [address for displayname,address in getaddresses(key['uids'])] if x == recipient]:
                    log.debug(_("Found matching address %r") % (address))
                    key_local = key['keyid']
            if not key_local == False:
                encrypt_rcpts.append(key_local)

        payload = message.get_payload()
        #print "payload:", payload
        if len(encrypt_rcpts) < 1:
            return filepath

        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)

            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())
        os.close(fp)
        os.unlink(filepath)

        exec('modules.cb_action_%s(%r, %r)' % ('ACCEPT','gpgencrypt', new_filepath))
    except Exception, errmsg:
        log.error(_("An error occurred: %r") % (errmsg))
        if conf.debuglevel > 8:
            import traceback
            traceback.print_exc()