This repository has been archived by the owner on Jan 14, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key-report.py
193 lines (140 loc) · 5.6 KB
/
key-report.py
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
#! /usr/bin/env python
# -*- mode: python; mode: auto-fill; fill-column: 80 -*-
"""Key Report is a tool to report on when PGP keys expire.
Displays details about your keyring:
- Displays keys that have expired (error).
- Displays keys that are nearly expired (critical).
- Displays keys that are will expire soon (warning).
- Displays keys that are valid.
- Displays keys that never expire.
- Displays keys that have been revoked.
FIXME: 82: Localize date format if necessary.
TODO: 135: Write draft emails to files.
Copyright (C) 2013 Nick Daly <nick.m.daly@gmail.com>
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import argparse
from datetime import date, timedelta
from collections import defaultdict as DefaultDict
import subprocess
COMMANDS = { "list-keys": "gpg --list-keys --fixed-list-mode --with-colons" }
TRUSTWORTHY_STATES = ("expired", "unknown", "undefined", "marginal", "full",
"ultimate", )
def read_pgp():
"""Get list of keys from gpg."""
listkeys = subprocess.Popen(COMMANDS["list-keys"].split(),
stdout=subprocess.PIPE)
solution = listkeys.communicate()
return solution
def sort_keys(output):
"""Parse gpg lines."""
valid = DefaultDict(list)
invalid = DefaultDict(list)
unknown = DefaultDict(list)
today = date.today()
for line in output.splitlines():
# process lines that start with "pub" or "sub"
if not True in map(line.startswith, ("pub", "sub")):
continue
key, status, created, expires = parse(line)
if status in TRUSTWORTHY_STATES:
valid[expires].append(key)
else:
invalid[expires].append(key)
return valid, invalid, unknown
def parse(line):
"""Split PGP lines: returns key ID, status, creation date, and expiration.
>>> parse("pub:f:4096:1:0000000000000001:0:86400::q:::scESC:")
('0000000000000001', 'full', datetime.date(1969, 12, 31), datetime.date(1970, 1, 1))
"""
key = line.split(":")[4]
created = date.fromtimestamp(float(line.split(":")[5]))
try:
expires = date.fromtimestamp(float(line.split(":")[6]))
except ValueError:
expires = date.max
try:
status = { "o" : "unknown",
"i" : "invalid",
"r" : "revoked",
"e" : "expired",
"-" : "unknown",
"q" : "undefined",
"n" : "untrusted",
"m" : "marginal",
"f" : "full",
"u" : "ultimate",
}[line.split(":")[1]]
except KeyError:
status = "unknown"
return (key, status, created, expires)
def display_keys_dates(keys, status, critical, warning):
"""Displays valid keys and when they expire."""
today = date.today()
for adate, keys in sorted(keys.iteritems()):
if adate <= today:
date_goodness = "error"
elif adate - timedelta(days=critical) <= today:
date_goodness = "critical"
elif adate - timedelta(days=warning) <= today:
date_goodness = "warning"
else:
date_goodness = "valid"
for key in keys:
print date_goodness, status, key, adate
def draft_emails(keys, critical, warning):
"""Write out email drafts for folks to inform them of their expiration.
TODO Finish this some day.
"""
return
draft = """\
From: {0}
To: {1}
Subject: PGP Key Expiring
<#secure method=pgpmime mode=signencrypt>
Hi {2}, I just wanted to let you know that your PGP key is going to
expire within the next {3} days:
{4} {5}
You should publish another key (with a transitional signing statement)
or extend your current key's expiration date (if your key hasn't been
compromised).
Thanks for your time,
{6}
"""
print(draft.format(from_, to, to_name, expire_days,
key_id, expire_date, from_name))
def show_expiry(critical, warning):
"""Show expiry data."""
data = read_pgp()
valid, invalid, unknown = sort_keys(data[0])
print "Status", "Trustworthiness", "ID", "Expires"
for keys, status in ((valid, "valid"),
(invalid, "invalid"),
(unknown, "unknown")):
display_keys_dates(keys, status, critical, warning)
draft_emails(valid, critical, warning)
def test(*args, **kwargs):
import doctest
doctest.testmod()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Display key details.")
parser.add_argument("--warning", default=90, type=int,
help="Number of days before expiration to warn user.")
parser.add_argument("--critical", default=30, type=int,
help="Number of days before expiration to freak out.")
parser.add_argument("--test", action="store_const", const=True,
help="Run tests.")
args = parser.parse_args()
if args.test:
import doctest
doctest.testmod()
else:
show_expiry(args.critical, args.warning)