]> TLD Linux GIT Repositories - tld-builder.git/blob - TLD_Builder/gpg.py
- more python 3.x fixes
[tld-builder.git] / TLD_Builder / gpg.py
1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
3 import log
4 import subprocess
5 import re
6 import sys
7 if sys.version_info[0] == 2:
8     import StringIO
9 else:
10     from io import StringIO
11
12 import util
13 import os
14 import pipeutil
15
16 def get_keys(buf):
17     """Extract keys from gpg message
18
19     """
20
21     if not os.path.isfile('/usr/bin/gpg'):
22         log.error("missing gnupg binary: /usr/bin/gpg")
23         raise OSError('Missing gnupg binary')
24
25     d_stdout = None
26     d_stderr = None
27     cmd = ['/usr/bin/gpg', '--batch', '--no-tty', '--decrypt']
28     gpg_run = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
29     try:
30         d_stdout, d_stderr = gpg_run.communicate(buf.decode().encode('utf-8'))
31     except OSError as e:
32         log.error("gnupg run, does gpg binary exist? : %s" % e)
33         raise
34
35     rx = re.compile("^gpg:.*using\s[DR]SA\skey\s(?:ID\s)?(\w+)")
36     keys = []
37
38     for l in d_stderr.decode().split('\n'):
39         m = rx.match(l)
40         if m:
41             keys.append(m.group(1))
42
43     return keys
44
45 def verify_sig(buf):
46     """Check signature.
47
48     Given email as file-like object, return (signer-emails, signed-body).
49     where signer-emails is lists of strings, and signed-body is StringIO
50     object.
51     """
52
53     if not os.path.isfile('/usr/bin/gpg'):
54         log.error("missing gnupg binary: /usr/bin/gpg")
55         raise OSError('Missing gnupg binary')
56
57     d_stdout = None
58     d_stderr = None
59     cmd = ['/usr/bin/gpg', '--batch', '--no-tty', '--decrypt']
60     gpg_run = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
61     try:
62         d_stdout, d_stderr = gpg_run.communicate(buf.decode().encode('utf-8'))
63     except OSError as e:
64         log.error("gnupg run failed, does gpg binary exist? : %s" % e)
65         raise
66
67     rx = re.compile("^gpg: (Good signature from|                aka) .*<([^>]+)>")
68     emails = []
69     for l in d_stderr.decode().split('\n'):
70         m = rx.match(l)
71         if m:
72             emails.append(m.group(2))
73     if not emails:
74         log.error("gnupg signature check failed: %s" % d_stderr)
75     return (emails, d_stdout)
76
77 def sign(buf):
78     if not os.path.isfile('/usr/bin/gpg'):
79         log.error("missing gnupg binary: /usr/bin/gpg")
80         raise OSError('Missing gnupg binary')
81
82     d_stdout = None
83     d_stderr = None
84     cmd = ['/usr/bin/gpg', '--batch', '--no-tty', '--clearsign']
85     # TODO: check for gpg return code!
86     gpg_run = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
87     try:
88         d_stdout, d_stderr = gpg_run.communicate(buf.decode().encode('utf-8'))
89     except OSError as e:
90         log.error("gnupg signing failed, does gpg binary exist? : %s" % e)
91         raise
92
93     if len(d_stderr):
94         log.error("gpg: %s" % d_stderr.decode())
95
96     return d_stdout