paul@6 | 1 | # -*- coding: iso-8859-1 -*- |
paul@6 | 2 | """ |
paul@6 | 3 | MoinMoin - MoinMessage library |
paul@6 | 4 | |
paul@6 | 5 | @copyright: 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@6 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@6 | 7 | """ |
paul@6 | 8 | |
paul@6 | 9 | from email import message_from_string |
paul@6 | 10 | from email.encoders import encode_noop |
paul@6 | 11 | from email.mime.multipart import MIMEMultipart |
paul@6 | 12 | from email.mime.application import MIMEApplication |
paul@6 | 13 | from email.mime.base import MIMEBase |
paul@6 | 14 | from email.mime.text import MIMEText |
paul@6 | 15 | from subprocess import Popen, PIPE |
paul@8 | 16 | from tempfile import mkstemp |
paul@12 | 17 | from urlparse import urlsplit |
paul@6 | 18 | import httplib |
paul@8 | 19 | import os |
paul@6 | 20 | |
paul@6 | 21 | class Message: |
paul@6 | 22 | |
paul@6 | 23 | "An update message." |
paul@6 | 24 | |
paul@6 | 25 | def __init__(self): |
paul@6 | 26 | self.updates = [] |
paul@6 | 27 | |
paul@6 | 28 | def add_update(self, alternatives): |
paul@13 | 29 | |
paul@13 | 30 | """ |
paul@13 | 31 | Add an update fragment to a message, providing alternative forms of the |
paul@13 | 32 | update content in the given 'alternatives': a list of MIME message |
paul@13 | 33 | parts, each encoding the content according to different MIME types. |
paul@13 | 34 | """ |
paul@13 | 35 | |
paul@6 | 36 | if len(alternatives) > 1: |
paul@6 | 37 | part = MIMEMultipart() |
paul@6 | 38 | for alternative in alternatives: |
paul@6 | 39 | part.attach(alternative) |
paul@6 | 40 | self.updates.append(part) |
paul@6 | 41 | else: |
paul@6 | 42 | self.updates.append(alternatives[0]) |
paul@6 | 43 | |
paul@6 | 44 | def get_payload(self): |
paul@13 | 45 | |
paul@13 | 46 | "Get the multipart payload for the message." |
paul@13 | 47 | |
paul@6 | 48 | if len(self.updates) == 1: |
paul@6 | 49 | message = self.updates[0] |
paul@6 | 50 | else: |
paul@6 | 51 | message = MIMEMultipart() |
paul@6 | 52 | message.add_header("Update-Type", "collection") |
paul@6 | 53 | for update in self.updates: |
paul@6 | 54 | message.attach(update) |
paul@6 | 55 | |
paul@6 | 56 | return message |
paul@6 | 57 | |
paul@6 | 58 | class MoinMessageError(Exception): |
paul@6 | 59 | pass |
paul@6 | 60 | |
paul@8 | 61 | class GPG: |
paul@8 | 62 | |
paul@8 | 63 | "A wrapper around the gpg command using a particular configuration." |
paul@6 | 64 | |
paul@8 | 65 | def __init__(self, homedir=None): |
paul@8 | 66 | self.conf_args = [] |
paul@6 | 67 | |
paul@8 | 68 | if homedir: |
paul@8 | 69 | self.conf_args += ["--homedir", homedir] |
paul@8 | 70 | |
paul@8 | 71 | self.errors = None |
paul@6 | 72 | |
paul@8 | 73 | def run(self, args, text=None): |
paul@6 | 74 | |
paul@8 | 75 | """ |
paul@8 | 76 | Invoke gpg with the given 'args', supplying the given 'text' to the |
paul@8 | 77 | command directly or, if 'text' is omitted, using a file provided as part |
paul@8 | 78 | of the 'args' if appropriate. |
paul@6 | 79 | |
paul@8 | 80 | Failure to complete the operation will result in a MoinMessageError |
paul@8 | 81 | being raised. |
paul@8 | 82 | """ |
paul@8 | 83 | |
paul@8 | 84 | cmd = Popen(["gpg"] + self.conf_args + list(args), stdin=PIPE, stdout=PIPE, stderr=PIPE) |
paul@6 | 85 | |
paul@11 | 86 | try: |
paul@11 | 87 | # Attempt to write input to the command and to read output from the |
paul@11 | 88 | # command. |
paul@11 | 89 | |
paul@11 | 90 | try: |
paul@11 | 91 | if text: |
paul@11 | 92 | cmd.stdin.write(text) |
paul@11 | 93 | cmd.stdin.close() |
paul@6 | 94 | |
paul@11 | 95 | text = cmd.stdout.read() |
paul@11 | 96 | |
paul@11 | 97 | # I/O errors can indicate the failure of the command. |
paul@8 | 98 | |
paul@11 | 99 | except IOError: |
paul@11 | 100 | pass |
paul@11 | 101 | |
paul@11 | 102 | self.errors = cmd.stderr.read() |
paul@8 | 103 | |
paul@8 | 104 | # Test for a zero result. |
paul@6 | 105 | |
paul@8 | 106 | if not cmd.wait(): |
paul@8 | 107 | return text |
paul@8 | 108 | else: |
paul@10 | 109 | raise MoinMessageError, self.errors |
paul@8 | 110 | |
paul@8 | 111 | finally: |
paul@8 | 112 | cmd.stdout.close() |
paul@8 | 113 | cmd.stderr.close() |
paul@6 | 114 | |
paul@8 | 115 | def verifyMessage(self, signature, content): |
paul@8 | 116 | |
paul@8 | 117 | "Using the given 'signature', verify the given message 'content'." |
paul@6 | 118 | |
paul@8 | 119 | # Write the detached signature and content to files. |
paul@8 | 120 | |
paul@8 | 121 | signature_fd, signature_filename = mkstemp() |
paul@8 | 122 | content_fd, content_filename = mkstemp() |
paul@6 | 123 | |
paul@8 | 124 | try: |
paul@8 | 125 | signature_fp = os.fdopen(signature_fd, "w") |
paul@8 | 126 | content_fp = os.fdopen(content_fd, "w") |
paul@8 | 127 | try: |
paul@8 | 128 | signature_fp.write(signature) |
paul@8 | 129 | content_fp.write(content) |
paul@8 | 130 | finally: |
paul@8 | 131 | signature_fp.close() |
paul@8 | 132 | content_fp.close() |
paul@6 | 133 | |
paul@8 | 134 | # Verify the message text. |
paul@6 | 135 | |
paul@10 | 136 | text = self.run(["--status-fd", "1", "--verify", signature_filename, content_filename]) |
paul@10 | 137 | |
paul@10 | 138 | # Return the details of the signing key. |
paul@10 | 139 | |
paul@11 | 140 | identity = None |
paul@11 | 141 | fingerprint = None |
paul@11 | 142 | |
paul@10 | 143 | for line in text.split("\n"): |
paul@10 | 144 | try: |
paul@11 | 145 | prefix, msgtype, digest, details = line.strip().split(" ", 3) |
paul@10 | 146 | except ValueError: |
paul@10 | 147 | continue |
paul@10 | 148 | |
paul@10 | 149 | # Return the fingerprint and identity details. |
paul@10 | 150 | |
paul@10 | 151 | if msgtype == "GOODSIG": |
paul@11 | 152 | identity = details |
paul@11 | 153 | elif msgtype == "VALIDSIG": |
paul@11 | 154 | fingerprint = digest |
paul@11 | 155 | |
paul@11 | 156 | if identity and fingerprint: |
paul@11 | 157 | return fingerprint, identity |
paul@10 | 158 | |
paul@10 | 159 | return None |
paul@6 | 160 | |
paul@8 | 161 | finally: |
paul@8 | 162 | os.remove(signature_filename) |
paul@8 | 163 | os.remove(content_filename) |
paul@8 | 164 | |
paul@8 | 165 | def signMessage(self, message, keyid): |
paul@6 | 166 | |
paul@8 | 167 | """ |
paul@8 | 168 | Return a signed version of 'message' using the given 'keyid'. |
paul@8 | 169 | """ |
paul@6 | 170 | |
paul@8 | 171 | text = message.as_string() |
paul@8 | 172 | signature = self.run(["--armor", "-u", keyid, "--detach-sig"], text) |
paul@8 | 173 | |
paul@8 | 174 | # Make the container for the message. |
paul@8 | 175 | |
paul@8 | 176 | signed_message = MIMEMultipart("signed", protocol="application/pgp-signature") |
paul@8 | 177 | signed_message.attach(message) |
paul@6 | 178 | |
paul@8 | 179 | signature_part = MIMEBase("application", "pgp-signature") |
paul@8 | 180 | signature_part.set_payload(signature) |
paul@8 | 181 | signed_message.attach(signature_part) |
paul@8 | 182 | |
paul@8 | 183 | return signed_message |
paul@8 | 184 | |
paul@8 | 185 | def decryptMessage(self, message): |
paul@6 | 186 | |
paul@8 | 187 | "Return a decrypted version of 'message'." |
paul@8 | 188 | |
paul@8 | 189 | return self.run(["--decrypt"], message) |
paul@6 | 190 | |
paul@8 | 191 | def encryptMessage(self, message, keyid): |
paul@6 | 192 | |
paul@8 | 193 | """ |
paul@8 | 194 | Return an encrypted version of 'message' using the given 'keyid'. |
paul@8 | 195 | """ |
paul@6 | 196 | |
paul@8 | 197 | text = message.as_string() |
paul@8 | 198 | encrypted = self.run(["--armor", "-r", keyid, "--encrypt", "--trust-model", "always"], text) |
paul@8 | 199 | |
paul@8 | 200 | # Make the container for the message. |
paul@8 | 201 | |
paul@8 | 202 | encrypted_message = MIMEMultipart("encrypted", protocol="application/pgp-encrypted") |
paul@8 | 203 | |
paul@8 | 204 | # For encrypted content, add the declaration and content. |
paul@6 | 205 | |
paul@8 | 206 | declaration = MIMEBase("application", "pgp-encrypted") |
paul@8 | 207 | declaration.set_payload("Version: 1") |
paul@8 | 208 | encrypted_message.attach(declaration) |
paul@6 | 209 | |
paul@8 | 210 | content = MIMEApplication(encrypted, "octet-stream", encode_noop) |
paul@8 | 211 | encrypted_message.attach(content) |
paul@6 | 212 | |
paul@8 | 213 | return encrypted_message |
paul@8 | 214 | |
paul@8 | 215 | # Communications functions. |
paul@6 | 216 | |
paul@12 | 217 | def sendMessage(message, url): |
paul@6 | 218 | |
paul@12 | 219 | "Send 'message' to the given 'url." |
paul@6 | 220 | |
paul@12 | 221 | scheme, host, port, path = parseURL(url) |
paul@6 | 222 | text = message.as_string() |
paul@6 | 223 | |
paul@12 | 224 | if scheme == "http": |
paul@12 | 225 | cls = httplib.HTTPConnection |
paul@12 | 226 | elif scheme == "https": |
paul@12 | 227 | cls = httplib.HTTPSConnection |
paul@12 | 228 | else: |
paul@12 | 229 | raise MoinMessageError, "Communications protocol not supported: %s" % scheme |
paul@12 | 230 | |
paul@12 | 231 | req = cls(host, port) |
paul@12 | 232 | req.request("PUT", path, text) |
paul@6 | 233 | resp = req.getresponse() |
paul@6 | 234 | return resp.read() |
paul@6 | 235 | |
paul@12 | 236 | def parseURL(url): |
paul@12 | 237 | |
paul@12 | 238 | "Return the scheme, host, port and path for the given 'url'." |
paul@12 | 239 | |
paul@12 | 240 | scheme, host_port, path, query, fragment = urlsplit(url) |
paul@12 | 241 | host_port = host_port.split(":") |
paul@12 | 242 | |
paul@12 | 243 | if query: |
paul@12 | 244 | path += "?" + query |
paul@12 | 245 | |
paul@12 | 246 | if len(host_port) > 1: |
paul@12 | 247 | host = host_port[0] |
paul@12 | 248 | port = int(host_port[1]) |
paul@12 | 249 | else: |
paul@12 | 250 | host = host_port[0] |
paul@12 | 251 | port = 80 |
paul@12 | 252 | |
paul@12 | 253 | return scheme, host, port, path |
paul@12 | 254 | |
paul@6 | 255 | # vim: tabstop=4 expandtab shiftwidth=4 |