-
Notifications
You must be signed in to change notification settings - Fork 10
/
pgpg
executable file
·376 lines (315 loc) · 12 KB
/
pgpg
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env python3
# pgpg (part of ossobv/vcutil) // wdoekes/2023 // Public Domain
#
# Parallel GPG encryption/decryption, wrapping gpg1(1). This is useful
# when a bunch of files are to be individually encrypted/decrypted.
#
# This works around the inability of parallel work by the gpg-agent(1)
# -- which gpg1 *prefers* and gpg2 *requires* -- by bypassing the agent
# and having gpg1 do the work directly.
#
# Beware:
#
# To decrypt, pgpg will make a temporary password-less secret key,
# stored in /dev/shm.
#
# Usage:
#
# pgpg --key KEYID[,...] --encrypt|--decrypt [FILES..] < NUL-delimited-FILES
#
# If FILES are not passed on the command line, the files are expected as
# NUL-delimited on stdin. When you're dealing with many files, prefer
# that option. If xargs has to invokes pgpg multiple times, you'll have to
# provide credentials multiple times.
#
# Encryption example:
#
# find ~/src/linux -type f -name '*.c' -print0 |
# pgpg --key KEYID[,KEYID,...] --encrypt
#
# Decryption examples:
#
# find ~/src/linux -type f -name '*.c.gpg' -print0 |
# pgpg --key KEYID --decrypt
#
# Rationale/caveats:
#
# - The gpg-agent that gpg2 and gpg1 use, does not handle multiple
# encryption/decryption jobs at the same time well. (Observed on
# Ubuntu/Jammy with gpg(-agent) 2.2.27-3ubuntu2.1.)
# - The gnupg1 version (1.4.23-1.1build1) will do encryption/decryption
# without the agent. That way we can leverage the multiple cores we
# have at our disposal on modern machines.
# - It does so by asking you to decrypt your secret key once, and using
# that for the batch decryption.
# - If your key is stored on a card, this trick will not work.
#
# Other notes:
#
# - If you enable 'auto-expand-secmem' in gpg-agent.conf you have
# multiple gpg2 instances do gpg decryption. But it will still be
# dramatically slower than using this with gpg1.
#
# Todo:
#
# - Maybe make secret-key-gathering dynamic (when needed) so a run where
# all files are already done, needs no keys.
# - Maybe check for gpg1 before starting. (apt install gnupg1)
#
import sys
from subprocess import DEVNULL, Popen, check_call
from multiprocessing import cpu_count
from os import (
environ, listdir, lstat, rename, rmdir, unlink,
wait, waitstatus_to_exitcode)
from os.path import basename, dirname, exists, join
from stat import S_ISREG
from tempfile import mkdtemp
MAX_JOBS = cpu_count()
class Job:
__slots__ = ('process', 'source', 'temp', 'dest')
def __init__(self, process, source, temp, dest):
self.process = process
self.source = source
self.temp = temp
self.dest = dest
def finalize(self, status):
self.process.communicate()
self.process.wait()
assert self.process.returncode == 0, (
'we ate the real status using wait()', self.process.returncode)
status = self.process.returncode = waitstatus_to_exitcode(status)
if status != 0:
try:
unlink(self.temp)
except FileNotFoundError:
pass
except Exception as e:
print(
f'pgpg: unlinking {self.temp} failed: {e}',
file=sys.stderr)
return False
try:
rename(self.temp, self.dest)
except Exception as e:
print(
f'pgpg: rename {self.temp} -> {self.dest} failed: {e}',
file=sys.stderr)
return False
return True
def __repr__(self):
return (
f'<Job({self.process.returncode}, {self.source!r}, '
f'{self.temp!r}, {self.dest!r})>')
def secret_key_setup(keyid):
assert all(i in (
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz'
'0123456789') for i in keyid), ('expected only [A-Za-z0-9]', keyid)
tempdir = mkdtemp(prefix='pgpg.', dir='/dev/shm') # assert shell safe
try:
print()
print('==vv========================================================')
print(f'Importing secret key into TEMPORARY {tempdir}')
print('==^^========================================================')
print()
check_call(
f'gpg --export-secret-key {keyid} | '
f'GNUPGHOME={tempdir} gpg1 --import',
shell=True)
print()
print('==vv========================================================')
print('Now we need to drop the passphrase from the TEMPORARY secret')
print()
print('Type the following:')
print('> passwd')
print('(enter your passphrase)')
print('(then set the new passphrase to empty on this temporary copy)')
print('> save')
print('==^^========================================================')
print()
check_call(
f'GNUPGHOME={tempdir} gpg1 --no-greeting --edit-key {keyid}',
shell=True)
print()
print('==vv========================================================')
print(f'Secret key is now in TEMPORARY {tempdir}')
print('It will be cleaned up upon exit')
print('==^^========================================================')
print()
except Exception:
secret_key_cleanup(tempdir)
raise
return tempdir
def secret_key_cleanup(gnupghome):
for file in sorted(listdir(gnupghome)):
assert file not in ('.', '..'), file
path = join(gnupghome, file)
try:
unlink(path)
except IsADirectoryError:
secret_key_cleanup(path)
except Exception as e:
print(f'pgpg: could not clean {path!r}: {e}')
try:
rmdir(gnupghome)
except Exception as e:
print(f'pgpg: could not clean {path!r}: {e}')
def decrypt(source, dest, gnupghome):
"""
Right now, we must:
- use gpg1 (gpg2 will still use the agent and be slow)
- GNUPGHOME_TMP=$(mktemp -d -p /dev/shm)
- gpg --export-secret-keys X | GNUPGHOME=$GNUPGHOME_TMP gpg1 --import
- GNUPGHOME=$GNUPGHOME_TMP gpg1 --edit-key X
passwd -> enter old password
-> change to empty password and confirm
save -> write key
- run this, using the the GNUPGHOME in env
Major speedup increase by:
- not needing to decrypt the secret key
- running parallel while bypassing the gpg-agent
"""
# We must use gpg1 here because gpg2 refuses to *not* use the gpg-agent.
# Do not use --lock-never. That breaks stuff, causing intermittent
# "gpg: note: random_seed file is empty" warnings.
cmd = ['gpg1', '--batch', '--no-tty', '--no-use-agent', '--quiet']
# It's important we specify --yes here. Without it, any existing
# (partial?) temp file will not be clobbered, but the gpg1 exit code will
# be success (0). Ignoring that would open the possibility for file
# corruption if a .pgpgtmp file from a previous run was laying around.
cmd.extend(['--yes'])
cmd.extend(['-o', dest])
cmd.extend(['--decrypt', source])
return Popen(cmd, stdin=DEVNULL, env={
'PATH': environ['PATH'],
'GNUPGHOME': gnupghome,
})
def encrypt(source, dest, recipients):
# It doesn't matter whether we use gpg1 or gpg2 here. Either is fast
# enough and neither benefits from decrypted keys.
cmd = ['gpg', '--batch', '--no-tty']
for recipient in recipients:
cmd.extend(['-r', recipient])
cmd.extend(['-o', dest])
cmd.extend(['--encrypt', source])
# We do not wipe the environment here. The gpg agent is not in the
# way when encrypting.
return Popen(cmd, stdin=DEVNULL, env=environ)
def parallel_decrypt(files, gnupghome):
def del_gpg(filename):
assert filename.endswith('.gpg'), filename
return filename[0:-4]
def decrypt_with_gnupghome(source, dest):
return decrypt(source, dest, gnupghome)
return _parallel_action(files, del_gpg, decrypt_with_gnupghome)
def parallel_encrypt(files, recipients):
def add_gpg(filename):
assert not filename.endswith('.gpg'), filename
return f'{filename}.gpg'
def encrypt_for_recipients(source, dest):
return encrypt(source, dest, recipients)
return _parallel_action(files, add_gpg, encrypt_for_recipients)
def wait_for_jobs_and_bail(failing_job, other_jobs):
print(
f'pgpg: job {failing_job} failed; '
f'waiting for {len(other_jobs)} to finish', file=sys.stderr)
try:
unlink(failing_job.temp)
except Exception:
pass
while other_jobs:
pid, status = wait()
job = [i for i in other_jobs if i.process.pid == pid][0]
other_jobs.remove(job)
if job.finalize(status):
print(f'pgpg: done with {job}', file=sys.stderr)
else:
print(f'pgpg: job {job} also failed', file=sys.stderr)
exit(failing_job.process.returncode)
def _parallel_action(sources, make_destname, make_dest):
def is_valid(source):
try:
st = lstat(source)
except FileNotFoundError:
print(f'pgpg: {source!r} not found', file=sys.stderr)
return False
except Exception as e:
print(f'pgpg: {source!r} got {e}', file=sys.stderr)
return False
if not S_ISREG(st.st_mode):
print(f'pgpg: {source!r} is not a regular file', file=sys.stderr)
return False
dest = make_destname(source)
if exists(dest):
print(f'pgpg: {dest!r} already exists', file=sys.stderr)
return False
return True
def make_job(source):
dest = make_destname(source)
if '/' in dest:
temp = f'{dirname(dest)}/.{basename(dest)}.pgpgtmp'
else:
temp = f'.{dest}.pgpgtmp'
process = make_dest(source, temp)
return Job(process=process, source=source, temp=temp, dest=dest)
njobs = MAX_JOBS
idx = 0
jobs = []
# Skip over invalid/done files
while idx < len(sources) and not is_valid(sources[idx]):
idx += 1
# Start creating njobs jobs
while idx < len(sources) and len(jobs) < njobs:
jobs.append(make_job(sources[idx]))
idx += 1
# While skipping invalid/done
while idx < len(sources) and not is_valid(sources[idx]):
idx += 1
# While there are still files left, keep exactly njobs jobs running
while idx < len(sources):
pid, status = wait()
job = [i for i in jobs if i.process.pid == pid][0]
jobs.remove(job)
if not job.finalize(status):
wait_for_jobs_and_bail(job, jobs)
jobs.append(make_job(sources[idx]))
assert len(jobs) == njobs
idx += 1
# While skipping invalid/done
while idx < len(sources) and not is_valid(sources[idx]):
idx += 1
# No files are left; wait for all jobs to complete
while jobs:
pid, status = wait()
job = [i for i in jobs if i.process.pid == pid][0]
jobs.remove(job)
if not job.finalize(status):
wait_for_jobs_and_bail(job, jobs)
def get_files(files):
"""
Return the list as passed as argument, or read stdin and split by 0.
"""
if not files:
files = [i for i in sys.stdin.read().split('\0') if i]
return files
def main():
if sys.argv[1:2] == ['--key'] and sys.argv[3:4] == ['--encrypt']:
recipients, files = sys.argv[2].split(','), get_files(sys.argv[4:])
parallel_encrypt(files, recipients=recipients)
elif sys.argv[1:2] == ['--key'] and sys.argv[3:4] == ['--decrypt']:
keys = sys.argv[2].split(',')
assert len(keys) == 1, 'expected one secret key to decrypt with'
keyid, files = keys[0], get_files(sys.argv[4:])
gnupghome = secret_key_setup(keyid)
try:
parallel_decrypt(files, gnupghome=gnupghome)
finally:
secret_key_cleanup(gnupghome)
else:
print(
'usage: pgpg --key KEYID[,...] --encrypt|--decrypt [FILES..]',
file=sys.stderr)
print('Providing NUL-delimited filenames on stdin is preferred.')
if __name__ == '__main__':
main()