-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostmortem
More file actions
executable file
·552 lines (476 loc) · 18.2 KB
/
postmortem
File metadata and controls
executable file
·552 lines (476 loc) · 18.2 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/usr/bin/env python3
# Description {{{1
"""Postmortem
Generate an account summary that includes complete account information,
including secrets, for selected accounts. This summary should allow the
recipients to access your accounts. The summaries are intended to be given to
the recipients after you die.
Usage:
postmortem [options] [<recipients>...]
Options:
-s, --send send encrypted packet to recipient
-r, --redact redact secrets
Choose from: {available}. If no recipients are specified, then summaries will
be generated for all recipients.
A description of how to configure and use this program can be found at
github.com/KenKundert/postmortem.
You can unpack an encrypted output file using::
gpg -d -o - name.tgz.gpg | tar xzf -
"""
# License {{{1
# Copyright (C) 2016-2023 Kenneth S. Kundert
#
# 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, either version 3 of the License, 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
# Imports {{{1
from appdirs import user_config_dir
from avendesora import PasswordGenerator, PasswordError
from docopt import docopt
from inform import (
Error, conjoin, cull, display, fatal, full_stop, indent, join, os_error,
terminate, warn, is_str, is_mapping, is_collection, is_str
)
import nestedtext as nt
from shlib import Run, to_path, mkdir, cp, rm, set_prefs as shlib_set_prefs
from textwrap import dedent
from voluptuous import Schema, Invalid, Extra, Required, REMOVE_EXTRA
import arrow
import gnupg
# Settings {{{1
# constants {{{2
prog_name = 'postmortem'
config_filename = 'config.nt'
shlib_set_prefs(use_inform=True)
__version__ = '0.8.0'
__released__ = '2021-08-10'
now = arrow.now()
# avendesora_header {{{3
avendesora_header = dedent("""
# Exported Avendesora Accounts
# {date}
#
# To adopt these accounts, you should copy this file into your existing
# Avendesora directory: ~/.config/avendesora (you can rename this file
# but keep the extension the same). Then add the name of this file to the
# accounts_files list in the ~/.config/avendesora/accounts_files file. You
# may then find that the account names and aliases from this file conflict
# with your current accounts, in which case you can change the names of the
# accounts and aliases in this file.
#
# Exported accounts should only be used for archival purposes. Any generated
# secrets these accounts once contained have all be converted to obscured
# secrets. If, at some point, it becomes necessary to change a secret, the
# secret should be converted back to a generated secret to preserve its
# unpredictability.
# Imports {fold}1
from avendesora import (
# Basics
Account, Hidden, Question, Script, WriteFile, OTP,
# Account Discovery
RecognizeAll, RecognizeAny, RecognizeTitle, RecognizeURL, RecognizeCWD,
RecognizeHost, RecognizeUser, RecognizeEnvVar, RecognizeNetwork,
RecognizeFile
)
# Accounts {fold}1
""").lstrip().format(fold='{''{''{', date=now.format('D MMMM YYYY'))
# avendesora_footer {{{3
avendesora_footer = join(
'\n# vim:',
'filetype=python',
'sw=4',
'sts=4',
'et',
'ai',
'ff=unix',
'fileencoding=utf-8',
'foldmethod=marker',
':'
)
# unpack_script {{{3
unpack_script = dedent('''
#!/bin/bash
if [ $# -le 0 ]; then
echo 'usage:'
echo ' unpack <archive>.gpg ...'
exit
fi
echo
while [ $# -gt 0 ]
do
src="$1"
tarfile="${src%.*}"
archive="${tarfile%.*}"
echo "=== $src --> $archive ==="
gpg -d -o - "$src" | tar xvzf -
echo
shift
done
''').lstrip()
# readme_contents {{{3
outer_readme_contents = dedent('''
Run unpack to decrypt and unpack your archive using::
./unpack <yourname>-<date>.tgz.gpg
It will create a directory <yourname>-<date> that contains your information.
This directory contains sensitive information, so keep it secure.
I recommend that you just copy out the information you need, then delete it.
''').lstrip()
inner_readme_contents = dedent('''
The accounts.gpg file contains information about those of my accounts that
are relevant to you. All passwords and other secrets are expanded as of the
time this file was created. Time-based one-time password codes are included
but will be out-of-date and useless.
The avendesora_accounts.gpg file contains the same information, but in
a form that can be imported into Avendesora. To do so, follow the
instructions at the top of the file. Once imported, the time-based one-time
passwords will be generated correctly by Avendesora.
I recommend that you attempt to access the GPG files contained in this
package to assure that you will be able to when you need them.
''').lstrip()
# defaults {{{2
# these can be overridden in the settings file: ~/.config/postmortem
my_gpg_ids = ''
sign_with = None
salutation = ''
cc = []
recipients = dict()
avendesora_value_fieldname = 'estimated_value'
avendesora_recipients_fieldname = 'postmortem_recipients'
avendesora_gpg_passphrase_account = None
avendesora_gpg_passphrase_field = None
unpack_script_name = 'unpack'
readme_name = 'README'
# utility functions {{{2
# expand_settings() {{{3
def expand_settings(value):
# allows macro values to be defined as a top-level setting.
# allows macro reference to be found anywhere.
if is_str(value):
value = value.strip()
if value[:1] == '@':
value = settings[normalize(value[1:])]
return value
if is_mapping(value):
return {k:expand_settings(v) for k, v in value.items()}
if is_collection(value):
return [expand_settings(v) for v in value]
raise NotImplementedError(value)
# functions for setting validation and transformation {{{3
def to_str(arg):
if isinstance(arg, str):
return arg
raise Invalid('expected text.')
def to_ident(arg):
arg = to_str(arg)
if arg.isidentifier():
return arg
raise Invalid('expected simple identifier')
def to_int(arg):
try:
return int(arg)
except ValueError:
raise Invalid('expected integer.')
def to_list(arg):
if isinstance(arg, str):
return arg.split()
if isinstance(arg, dict):
raise Invalid('expected list.')
return arg
def to_set(arg):
return set(to_list(arg))
def to_paths(arg):
return [to_path(p).expanduser() for p in to_list(arg)]
def to_email(arg):
user, _, host = arg.partition('@')
if '.' in host:
return arg
raise Invalid('expected email address.')
def to_emails(arg):
return [to_email(e) for e in to_list(arg)]
def to_gpg_id(arg):
try:
return to_email(arg) # gpg ID may be an email addres
except Invalid:
try:
int(arg, base=16) # if not an email, it must be a hex key
assert len(arg) >= 8 # at least 8 characters long
return arg
except (ValueError, AssertionError):
raise Invalid('expected GPG id.')
def to_gpg_ids(arg):
return [to_gpg_id(i) for i in to_list(arg)]
def normalize(key, *args):
return '_'.join(key.strip().lower().split()) if type(key) == str else key
# schema {{{2
schema = Schema(
{
Required('my_gpg_ids'): to_gpg_ids,
'sign_with': to_gpg_id,
Required('avendesora_gpg_passphrase_account'): to_str,
'avendesora_gpg_passphrase_field': to_str,
'name_template': to_str,
'cc': to_list,
'salutation': to_str,
Required('recipients'): {
Extra: {
Required('categories'): to_set,
Required('email'): to_emails,
'gpg_id': to_gpg_id,
'attach': to_paths,
'networth': to_str,
'salutation': to_str,
'accounts': to_int,
}
},
},
extra = REMOVE_EXTRA
)
voluptuous_error_msg_mapings = {
'extra keys not allowed': 'unknown key',
}
try:
# Read settings {{{1
config_filepath = to_path(user_config_dir(prog_name), config_filename)
if config_filepath.exists():
# load from file
settings = nt.load(
config_filepath, keymap=(locmap:={}), normalize_key=normalize
)
# expand references
settings = expand_settings(settings)
# check settings and transform to desired types
settings = schema(settings)
# convert keys to identifiers
settings = {'_'.join(k.split()): v for k, v in settings.items()}
# add settings to local variables
locals().update(settings)
else:
warn('no configuration file found.')
# Read command line and process options {{{1
cmdline = docopt(__doc__.format(available=conjoin(recipients)))
who = cmdline['<recipients>']
if not who:
who = recipients
send = cmdline['--send']
redact = cmdline['--redact']
# Scan accounts and gather information for recipients {{{1
pw = PasswordGenerator()
accounts = {}
avendesora_accounts = {}
for account in pw.all_accounts():
account_name = account.get_name()
class_name = account.__name__
description = account.get_scalar('desc', None, None)
# summarize account values {{{2
data = account.get_composite('_' + avendesora_value_fieldname)
if not data:
data = account.get_composite(avendesora_value_fieldname)
postmortem_recipients = account.get_scalar(avendesora_recipients_fieldname, default=None)
if data and not postmortem_recipients:
warn('no recipients.', culprit= account.get_name())
continue
if not postmortem_recipients:
continue
postmortem_recipients = set(postmortem_recipients.split())
# gather information for recipients {{{2
for name, recipient in recipients.items():
if recipient.get('categories') & postmortem_recipients:
# create textual version of account
# output title
title = ' - '.join(cull([class_name, description]))
lines = [title, len(title)*'=']
# output avendesora names
aliases = account.get_composite('aliases')
names = [account_name] + (aliases if aliases else [])
lines.append('avendesora names: ' + ', '.join(names))
# output user fields
for field, keys in account.get_fields():
if field in [avendesora_recipients_fieldname, 'desc', 'NAME']:
continue
if keys == [None]:
v = account.get_value(field)
if redact and v.is_secret:
lines += v.render('{n}: <redacted>').split('\n')
else:
lines += v.render('{n}: {v}').split('\n')
else:
lines.append(field + ':')
for k, v in account.get_values(field):
if redact and v.is_secret:
lines += v.render('{k}: <redacted>').split('\n')
else:
lines += indent(
v.render(('{k}) {d}: {v}', '{k}: {v}'))
).split('\n')
# add textual version of account to accounts
if name not in accounts:
accounts[name] = []
accounts[name].append('\n'.join(lines))
# add code version of account to avendesora_accounts
if not redact:
if name not in avendesora_accounts:
avendesora_accounts[name] = []
avendesora_accounts[name].append(account.export())
# generate encrypted files that contain accounts for each recipient {{{1
gpg = gnupg.GPG()
gpg.encoding = 'utf-8'
if sign_with and avendesora_gpg_passphrase_account:
gpg_account = pw.get_account(avendesora_gpg_passphrase_account)
if avendesora_gpg_passphrase_field:
gpg_passphrase = account.get_value(avendesora_gpg_passphrase_field)
else:
gpg_passphrase = account.get_passcode()
gpg_passphrase = str(gpg_passphrase.value)
else:
gpg_passphrase = None
for name, recipient in recipients.items():
if who and name not in who:
continue
dir_path = to_path(name_template.format(name=name, now=now))
rm(dir_path)
mkdir(dir_path)
idents = (
recipient.get('gpg_id', recipient.get('email')) + my_gpg_ids
)
# copy in attachments
for each in recipient.get('attach', []):
try:
cp(each, dir_path)
except OSError as e:
warn(os_error(e))
# save readme
if readme_name:
readme = to_path(dir_path/readme_name)
readme.write_text(inner_readme_contents)
readme.chmod(0o600)
# generate networth report
profile = recipient.get('networth')
if profile:
cmd = 'networth'.split()
if is_str(profile):
cmd.append(profile)
nw = Run(cmd, 'sOeW')
file_path = dir_path / 'networth'
file_path.write_text(nw.stdout)
# generates accounts.gpg
if name in accounts:
content = accounts[name]
num_accounts = len(content)
expected_num = recipient.get('accounts')
if expected_num is not None and num_accounts != expected_num:
warn(
f'expected {expected_num} accounts, {num_accounts} found.',
culprit=name
)
encrypted = gpg.encrypt('\n\n\n'.join(content), idents, armor='always')
if not encrypted.ok:
raise Error(
'unable to encrypt:', encrypted.stderr, culprit=name
)
try:
file_path = dir_path / 'accounts.gpg'
file_path.write_text(str(encrypted))
display(f'contains {num_accounts} accounts.', culprit=name)
except OSError as e:
raise Error(os_error(e))
else:
warn('no accounts found.', culprit=name)
# generates avendesora_accounts.gpg
if name in avendesora_accounts:
content = (
[avendesora_header] +
avendesora_accounts[name] +
[avendesora_footer]
)
num_accounts = len(avendesora_accounts[name])
encrypted = gpg.encrypt('\n\n'.join(content), idents, armor='always')
if not encrypted.ok:
raise Error(
'unable to encrypt:', encrypted.stderr, culprit=name
)
if not redact:
try:
file_path = dir_path / 'avendesora_accounts.gpg'
file_path.write_text(str(encrypted))
except OSError as e:
raise Error(os_error(e))
# tar up directory
Run(['chmod', '-R', 'go-rwx', dir_path], 'soeW')
tar_path = dir_path.with_suffix('.tgz')
Run(['tar', 'zcf', tar_path, dir_path], 'soeW')
tar_path.chmod(0o600)
# sign and encrypt tarfile
tar_contents = tar_path.read_bytes()
encrypted = gpg.encrypt(
tar_contents, idents, sign=sign_with, passphrase=gpg_passphrase,
armor='always'
)
if not encrypted.ok:
raise Error(
'unable to encrypt:', encrypted.stderr, culprit=name
)
encrypted_tar_path = tar_path.with_suffix('.tgz.gpg')
encrypted_tar_path.write_text(str(encrypted))
encrypted_tar_path.chmod(0o600)
# remove intermediate files & dirs
rm(dir_path, tar_path)
# save unpack script
if unpack_script_name:
unpack = to_path(unpack_script_name)
unpack.write_text(unpack_script)
unpack.chmod(0o700)
# save readme
if readme_name:
readme = to_path(readme_name)
readme.write_text(outer_readme_contents)
readme.chmod(0o600)
# send packet
if send and recipient.get('email'):
the_salutation = recipient.get('salutation', salutation)
msg = dedent(f'''
{name.title()},
Attached is a packet of information that should be useful
if something were to happen to me. Please keep this information
secure and use it responsibly. It is encrypted with your GPG
key.
You can unpack this packet using:
gpg -d -o - {encrypted_tar_path!s} | tar xzf -
See https://github.com/KenKundert/postmortem for information on
how to import my accounts into your Avendesora accounts.
{the_salutation}
''')
copy_to = 2*len(cc)*['-c']
copy_to[1::2] = cc
cmd = [
'mail',
'-s', 'postmortem packet',
'-a', str(encrypted_tar_path),
] + copy_to + recipient.get('email')
Run(cmd, 'soeW', stdin=msg)
# process exceptions {{{1
except KeyboardInterrupt:
terminate('Killed by user.')
except nt.NestedTextError as e:
e.terminate()
except Invalid as e:
msg = voluptuous_error_msg_mapings.get(e.msg, e.msg)
kind = 'key' if 'key' in msg else 'value'
loc = locmap[tuple(e.path)]
fatal(
f"{'.'.join(str(p) for p in e.path)}: {full_stop(msg)}",
codicil = loc.as_line(kind)
)
except (PasswordError, Error) as e:
e.terminate()
except OSError as e:
fatal(os_error(e))
# vim: set sw=4 sts=4 et: