]> git.decadent.org.uk Git - dak.git/blob - daklib/gpg.py
Merge branch 'master' of ssh://ftp-master.debian.org/srv/ftp.debian.org/git/dak
[dak.git] / daklib / gpg.py
1 """Utilities for signed files
2
3 @contact: Debian FTP Master <ftpmaster@debian.org>
4 @copyright: 2011  Ansgar Burchardt <ansgar@debian.org>
5 @license: GNU General Public License version 2 or later
6 """
7
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22 import errno
23 import fcntl
24 import os
25 import select
26
27 try:
28     _MAXFD = os.sysconf("SC_OPEN_MAX")
29 except:
30     _MAXFD = 256
31
32 class GpgException(Exception):
33     pass
34
35 class _Pipe(object):
36     """context manager for pipes
37
38     Note: When the pipe is closed by other means than the close_r and close_w
39     methods, you have to set self.r (self.w) to None.
40     """
41     def __enter__(self):
42         (self.r, self.w) = os.pipe()
43         return self
44     def __exit__(self, type, value, traceback):
45         self.close_w()
46         self.close_r()
47         return False
48     def close_r(self):
49         """close reading side of the pipe"""
50         if self.r:
51             os.close(self.r)
52             self.r = None
53     def close_w(self):
54         """close writing part of the pipe"""
55         if self.w:
56             os.close(self.w)
57             self.w = None
58
59 class SignedFile(object):
60     """handle files signed with PGP
61
62     The following attributes are available:
63       contents            - string with the content (after removing PGP armor)
64       valid               - Boolean indicating a valid signature was found
65       fingerprint         - fingerprint of the key used for signing
66       primary_fingerprint - fingerprint of the primary key associated to the key used for signing
67     """
68     def __init__(self, data, keyrings, require_signature=True, gpg="/usr/bin/gpg"):
69         """
70         @param data: string containing the message
71         @param keyrings: seqeuence of keyrings
72         @param require_signature: if True (the default), will raise an exception if no valid signature was found
73         @param gpg: location of the gpg binary
74         """
75         self.gpg = gpg
76         self.keyrings = keyrings
77
78         self.valid = False
79         self.fingerprint = None
80         self.primary_fingerprint = None
81
82         self._verify(data, require_signature)
83
84     def _verify(self, data, require_signature):
85         with _Pipe() as stdin:
86          with _Pipe() as contents:
87           with _Pipe() as status:
88            with _Pipe() as stderr:
89             pid = os.fork()
90             if pid == 0:
91                 self._exec_gpg(stdin.r, contents.w, stderr.w, status.w)
92             else:
93                 stdin.close_r()
94                 contents.close_w()
95                 stderr.close_w()
96                 status.close_w()
97
98                 read = self._do_io([contents.r, stderr.r, status.r], {stdin.w: data})
99                 stdin.w = None # was closed by _do_io
100
101                 (pid_, exit_code, usage_) = os.wait4(pid, 0)
102
103                 self.contents = read[contents.r]
104                 self.status   = read[status.r]
105                 self.stderr   = read[stderr.r]
106
107                 if self.status == "":
108                     raise GpgException("No status output from GPG. (GPG exited with status code %s)\n%s" % (exit_code, self.stderr))
109
110                 for line in self.status.splitlines():
111                     self._parse_status(line)
112
113                 if require_signature and not self.valid:
114                     raise GpgException("No valid signature found. (GPG exited with status code %s)\n%s" % (exit_code, self.stderr))
115
116     def _do_io(self, read, write):
117         read_lines = dict( (fd, []) for fd in read )
118         write_pos = dict( (fd, 0) for fd in write )
119
120         read_set = list(read)
121         write_set = write.keys()
122         while len(read_set) > 0 or len(write_set) > 0:
123             r, w, x_ = select.select(read_set, write_set, ())
124             for fd in r:
125                 data = os.read(fd, 4096)
126                 if data == "":
127                     read_set.remove(fd)
128                 read_lines[fd].append(data)
129             for fd in w:
130                 data = write[fd][write_pos[fd]:]
131                 if data == "":
132                     os.close(fd)
133                     write_set.remove(fd)
134                 else:
135                     bytes_written = os.write(fd, data)
136                     write_pos[fd] += bytes_written
137
138         return dict( (fd, "".join(read_lines[fd])) for fd in read_lines.keys() )
139
140     def _parse_status(self, line):
141         fields = line.split()
142         if fields[0] != "[GNUPG:]":
143             raise GpgException("Unexpected output on status-fd: %s" % line)
144
145         # VALIDSIG    <fingerprint in hex> <sig_creation_date> <sig-timestamp>
146         #             <expire-timestamp> <sig-version> <reserved> <pubkey-algo>
147         #             <hash-algo> <sig-class> <primary-key-fpr>
148         if fields[1] == "VALIDSIG":
149             self.valid = True
150             self.fingerprint = fields[2]
151             self.primary_fingerprint = fields[11]
152
153         if fields[1] == "BADARMOR":
154             raise GpgException("Bad armor.")
155
156         if fields[1] == "NODATA":
157             raise GpgException("No data.")
158
159         if fields[1] == "DECRYPTION_FAILED":
160             raise GpgException("Decryption failed.")
161
162         if fields[1] == "ERROR":
163             raise GpgException("Other error: %s %s" % (fields[2], fields[3]))
164
165     def _exec_gpg(self, stdin, stdout, stderr, statusfd):
166         try:
167             if stdin != 0:
168                 os.dup2(stdin, 0)
169             if stdout != 1:
170                 os.dup2(stdout, 1)
171             if stderr != 2:
172                 os.dup2(stderr, 2)
173             if statusfd != 3:
174                 os.dup2(statusfd, 3)
175             for fd in range(4):
176                 old = fcntl.fcntl(fd, fcntl.F_GETFD)
177                 fcntl.fcntl(fd, fcntl.F_SETFD, old & ~fcntl.FD_CLOEXEC)
178             os.closerange(4, _MAXFD)
179
180             args = [self.gpg, "--status-fd=3", "--no-default-keyring"]
181             for k in self.keyrings:
182                 args.append("--keyring=%s" % k)
183             args.extend(["--decrypt", "-"])
184
185             os.execvp(self.gpg, args)
186         finally:
187             os._exit(1)
188
189 # vim: set sw=4 et: