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
|
#!/bin/bash
set -euo pipefail
if [[ "$#" -gt 0 && ("$1" == "-h" || "$1" == "--help") ]]; then
cat <<EOF
list: List the secrets this machine can read
Usage:
list each bucket this machine reads: its readers, its secrets
and anything wrong with it, then a summary
list --all every bucket, read here or not
list --keys each reader's age public key too
list --json the same as one JSON document, keys included, for scripts
list --names <bucket>/<name>, one a line
list -h|--help
Only names, never values; nothing is decrypted. Piped, plain "list" prints
bare names, one a line, sorted (SZECRET_FORCE_TTY=1, or =<columns>, gives the
terminal view instead). The old store counts as readable while the old key
is here.
Readers are named by the comment above their key in the bucket's recipients,
and admins by a "# <name>" line above theirs in SZECRET_ADMINS_COMMAND's
output; a key with neither is "unnamed". The problems shown come from file
names and headers alone: szecret check decrypts every file to be sure.
Colour follows the terminal, and NO_COLOR turns it off.
EOF
exit 0
fi
this_dir="$(cd "$(dirname "$(realpath "$0")")" && pwd)"
source "${this_dir}/common.sh"
all=""
names_only=""
json=""
keys=""
while [[ $# -gt 0 ]]; do
case "$1" in
--all) all=1 ;;
--names) names_only=1 ;;
--json) json=1 ;;
--keys) keys=1 ;;
*) die "usage: szecret list [--all] [--keys | --json | --names]" ;;
esac
shift
done
if [[ -n "${names_only}" && -n "${json}" ]]; then
die "--names and --json are two different outputs: pick one"
fi
secrets_in() {
local f
for f in "$1"/*.age; do
if [[ -e "${f}" ]]; then
basename "${f}" .age
fi
done | LC_ALL=C sort
}
# Each bucket shown, then the old store, as "<label>\t<dir>".
shown() {
local d
if [[ -n "${buckets}" ]]; then
for d in "${buckets}"/*/; do
d="${d%/}"
[[ -d "${d}" ]] || continue
if [[ -n "${all}" ]] || is_reader "${d}"; then
printf '%s\t%s\n' "$(basename "${d}")" "${d}"
fi
done
fi
if [[ -n "${store}" ]] && { [[ -n "${all}" ]] || [[ -n "${private_key}" && -r "${private_key}" ]]; }; then
printf '%s\t%s\n' "(old store)" "${store}"
fi
}
# Scripts read these two, so they stay as they were: --names, and plain list
# piped.
if [[ -n "${names_only}" ]]; then
shown | while IFS=$'\t' read -r label d; do
secrets_in "${d}" | sed "s|^|${label}/|"
done
exit 0
fi
if [[ -z "${json}" && ! -t 1 && -z "${SZECRET_FORCE_TTY:-}" ]]; then
shown | while IFS=$'\t' read -r label d; do
secrets_in "${d}"
done | LC_ALL=C sort
exit 0
fi
# --- The terminal view, and --json --------------------------------------------
#
# One awk reads every bucket at once: its recipients, its .szecret and the
# header of each .age file (up to the "---" line, never the payload). Before
# each bucket's files it reads a marker file, with the bucket's label and
# its secrets' names set as awk variables just before it.
LC_ALL=C
export LC_ALL
admins_known=1
admins_out="$(admins_output 2>/dev/null)" || admins_known=""
admins=""
admin_named=""
if [[ -n "${admins_known}" ]]; then
admins="$(admin_keys "${admins_out}" 2>/dev/null)" || admins_known=""
admin_named="$(admin_names "${admins_out}")"
fi
marker="$(mktemp "${TMPDIR:-/tmp}/szecret-list.XXXXXX")"
trap 'rm -f "${marker}"' EXIT
echo "bucket" > "${marker}"
args=()
# add_bucket <label> <dir> <old>: its marker, then each file awk reads.
add_bucket() {
local label="$1" d="$2" old="$3" f n names="" unread="" files=()
for f in "${d}"/*.age; do
[[ -e "${f}" ]] || continue
n="$(basename "${f}" .age)"
# awk takes the names as one space-separated assignment: a name put
# would refuse (spaces, backslashes, newlines) is named here instead.
if [[ ! "${n}" =~ ${re_secret} ]]; then
>&2 printf 'szecret list: skipped a file whose name szecret would refuse, in %s: %q\n' "${label}" "${n}"
continue
fi
names="${names} ${n}"
if [[ -r "${f}" && -f "${f}" ]]; then
files+=("${f}")
else
unread="${unread} ${n}"
fi
done
args+=("label=${label}" "old=${old}" "names=${names}" "unread=${unread}" "${marker}")
if [[ "${old}" == 0 ]]; then
[[ ! -r "${d}/recipients" ]] || args+=("${d}/recipients")
[[ ! -r "${d}/.szecret" ]] || args+=("${d}/.szecret")
fi
args+=(${files[@]+"${files[@]}"})
}
total=0
if [[ -n "${buckets}" ]]; then
for d in "${buckets}"/*/; do
d="${d%/}"
[[ -d "${d}" ]] || continue
add_bucket "$(basename "${d}")" "${d}" 0
total=$((total + 1))
done
fi
old_readable=0
if [[ -n "${store}" ]]; then
if [[ -n "${private_key}" && -r "${private_key}" ]]; then
old_readable=1
fi
add_bucket "(old store)" "${store}" 1
fi
width="${SZECRET_FORCE_TTY:-}"
if ! [[ "${width}" =~ ^[0-9]+$ && "${width}" -ge 20 ]]; then
width=""
if [[ -t 1 ]]; then
width="$(tput cols 2>/dev/null || true)"
fi
if ! [[ "${width}" =~ ^[0-9]+$ && "${width}" -ge 20 ]]; then
width="${COLUMNS:-80}"
fi
fi
[[ "${width}" =~ ^[0-9]+$ && "${width}" -ge 20 ]] || width=80
color=""
if [[ -t 1 && -z "${NO_COLOR:-}" && "${TERM:-dumb}" != dumb ]]; then
color=1
fi
identity_key=""
if [[ -r "${identity_pub}" ]]; then
identity_key="$(keys_in "${identity_pub}" | head -1)"
fi
has_identity=""
if [[ -n "${identity_key}" || ${old_readable} == 1 ]]; then
has_identity=1
fi
# read, not $(cat <<...): bash 3.2 misreads quotes in a heredoc inside $().
IFS= read -r -d '' prog <<'AWK' || true
function short(k) { return substr(k, 1, 12) "..." }
function jstr(s) { gsub(/\\/, "\\\\", s); gsub(/"/, "\\\"", s); gsub(/\t/, "\\t", s); return "\"" s "\"" }
function c(code, s) { return color ? "\033[" code "m" s "\033[0m" : s }
function bold(s) { return c("1", s) }
function dim(s) { return c("2", s) }
function warn(s) { return c("33", s) }
function plural(n, one, many) { return n " " (n == 1 ? one : many) }
function isort(arr, n, i, j, t) {
for (i = 2; i <= n; i++) {
t = arr[i]
for (j = i - 1; j > 0 && (tolower(arr[j]) > tolower(t) || (tolower(arr[j]) == tolower(t) && arr[j] > t)); j--) arr[j + 1] = arr[j]
arr[j + 1] = t
}
}
# Who key k is in bucket b: "name", plus admin-ness.
function is_admin(b, k) {
if (k in ADMIN) return 1
# With no admins command, rekey's record still says who was one.
return !admins_known && index(NAME[b, k], "admin age1") == 1
}
function who(b, k, n) {
if (k in ADMIN_NAME) return ADMIN_NAME[k]
n = NAME[b, k]
if (is_admin(b, k)) return "unnamed admin"
if (n == "" || n == "?" || index(n, "admin age1") == 1) return "unnamed reader"
return n
}
function who_key(b, k) { return keys ? who(b, k) " (" k ")" : who(b, k) }
function problem(b, msg, fix) {
NP[b]++
PMSG[b, NP[b]] = msg
PFIX[b, NP[b]] = fix
}
function start_bucket( n, i, a) {
nb++
LABEL[nb] = label
OLD[nb] = old
NS[nb] = split(names, a, " ")
for (i = 1; i <= NS[nb]; i++) { SEC[nb, i] = a[i]; HAVE[nb, a[i]] = 1 }
n = split(unread, a, " ")
for (i = 1; i <= n; i++) UNREAD[nb, a[i]] = 1
kind = ""
}
# finish(b): readability, readers, and every problem names and headers show.
function finish(b, i, k, n, a, l, plus, minus, s) {
l = LABEL[b]
if (OLD[b]) {
READABLE[b] = old_readable
NR_[b] = 0
} else {
n = split(TO_LINE[b], a, " ")
NTO[b] = 0
for (i = 1; i <= n; i++) if (a[i] != "") { TO[b, a[i]] = 1; NTO[b]++; TOK[b, NTO[b]] = a[i] }
READABLE[b] = 0
for (k in MINE) if ((b, k) in TO) READABLE[b] = 1
# The keys shown as its readers: what its files are encrypted to; a
# bucket never keyed shows those it will be.
NR_[b] = 0
if (KEYED[b]) {
for (i = 1; i <= NTO[b]; i++) RD[b, ++NR_[b]] = TOK[b, i]
} else {
for (i = 1; i <= NRK[b]; i++) RD[b, ++NR_[b]] = RK[b, i]
for (k in ADMIN) RD[b, ++NR_[b]] = k
}
if (!KEYED[b]) {
problem(b, "never keyed", "szecret rekey " l)
} else if (admins_known) {
plus = ""; minus = ""
for (i = 1; i <= NRK[b]; i++) WANT[b, RK[b, i]] = 1
for (k in ADMIN) WANT[b, k] = 1
for (i = 1; i <= NRK[b]; i++) if (!((b, RK[b, i]) in TO) && !SEEN[b, "+" RK[b, i]]++) plus = plus " +" who_key(b, RK[b, i])
for (k in ADMIN) if (!((b, k) in TO) && !SEEN[b, "+" k]++) plus = plus " +" who_key(b, k)
for (i = 1; i <= NTO[b]; i++) if (!((b, TOK[b, i]) in WANT)) minus = minus " -" who_key(b, TOK[b, i])
if (plus minus != "") problem(b, "readers changed since its last rekey:" plus minus, "szecret rekey " l)
}
}
for (i = 1; i <= NS[b]; i++) {
s = SEC[b, i]
if ((b, s) in UNREAD) problem(b, s ": can't read its file", "")
else if (!((b, s) in STANZAS)) problem(b, s ": not an age file", "")
else if (BADTYPE[b, s] != "") problem(b, s ": unexpected stanza type" BADTYPE[b, s], "szecret check")
else if (!OLD[b] && KEYED[b] && STANZAS[b, s] != NTO[b]) problem(b, s ": encrypted to " STANZAS[b, s] " keys, the bucket to " NTO[b], "szecret check")
}
}
BEGIN {
n = split(admin_list, a, " "); for (i = 1; i <= n; i++) ADMIN[a[i]] = 1
NADMIN = n
n = split(ENVIRON["SZECRET_LIST_ADMIN_NAMES"], a, "\n")
for (i = 1; i <= n; i++) { k = a[i]; sub(/ .*/, "", k); nm = a[i]; sub(/^[^ ]* /, "", nm); ADMIN_NAME[k] = nm }
n = split(mine, a, " "); for (i = 1; i <= n; i++) MINE[a[i]] = 1
OKTYPE["X25519"] = 1; OKTYPE["p256tag"] = 1; OKTYPE["piv-p256"] = 1
nb = 0
}
FNR == 1 {
if (FILENAME == marker) { start_bucket(); next }
if (FILENAME ~ /\/recipients$/) { kind = "rec"; pending = "" }
else if (FILENAME ~ /\/\.szecret$/) kind = "meta"
else {
kind = "age"
cur = FILENAME; sub(/.*\//, "", cur); sub(/\.age$/, "", cur)
if ($0 != "age-encryption.org/v1") nextfile
STANZAS[nb, cur] = 0
next
}
}
kind == "rec" {
if ($0 ~ /^[ \t]*#/) { pending = $0; sub(/^[ \t]*#[ \t]*/, "", pending); next }
line = $0; sub(/#.*/, "", line); gsub(/[ \t]/, "", line)
if (line == "") next
NRK[nb]++; RK[nb, NRK[nb]] = line
if (!((nb, line) in NAME)) NAME[nb, line] = pending
pending = ""
next
}
kind == "meta" {
if ($0 == "kind=bucket") KEYED[nb] = 1
else if (index($0, "encrypted-to=") == 1) TO_LINE[nb] = substr($0, 14)
else if (index($0, "name=") == 1) {
k = substr($0, 6); nm = k; sub(/ .*/, "", k); sub(/^[^ ]* ?/, "", nm)
if (!((nb, k) in NAME) || NAME[nb, k] == "") NAME[nb, k] = nm
}
next
}
kind == "age" {
if (index($0, "-> ") == 1) {
STANZAS[nb, cur]++
if (!($2 in OKTYPE)) BADTYPE[nb, cur] = BADTYPE[nb, cur] " " $2
next
}
if (index($0, "---") == 1) nextfile
}
# --- Output ---
function wrap_list(prefix, n, arr, indent, i, line, out, item, sep) {
line = prefix; out = ""
for (i = 1; i <= n; i++) {
item = arr[i] (i < n ? "," : "")
sep = (line == indent && i > 1 ? "" : " ")
if (line != indent && length(line sep item) > W) { out = out line "\n"; line = indent; sep = "" }
line = line sep item
}
return out line
}
# grid(b): the bucket's secrets in columns, filled downwards as ls does, as
# many as fit in W.
function grid(b, n, i, cols, rows, w, col, total, best, r, line, s, pad) {
n = NS[b]
for (i = 1; i <= n; i++) G[i] = SEC[b, i]
isort(G, n)
best = 1
for (cols = n; cols > 1; cols--) {
rows = int((n + cols - 1) / cols)
if (int((n + rows - 1) / rows) != cols) continue
total = 2
for (col = 0; col < cols; col++) {
w = 0
for (r = 1; r <= rows; r++) { i = col * rows + r; if (i <= n && length(G[i]) > w) w = length(G[i]) }
total += w + (col < cols - 1 ? 2 : 0)
}
if (total <= W) { best = cols; break }
}
cols = best; rows = int((n + cols - 1) / cols)
for (col = 0; col < cols; col++) {
CW[col] = 0
for (r = 1; r <= rows; r++) { i = col * rows + r; if (i <= n && length(G[i]) > CW[col]) CW[col] = length(G[i]) }
}
for (r = 1; r <= rows; r++) {
line = " "
for (col = 0; col < cols; col++) {
i = col * rows + r
if (i > n) break
s = G[i]
if (col < cols - 1 && col * rows + r + rows <= n) { pad = CW[col] - length(s) + 2; while (pad-- > 0) s = s " " }
line = line s
}
print line
}
}
function show_bucket(b, i, n, k, R, nadm, hosts, rl, readers, j) {
print bold(LABEL[b]) " " dim(NS[b] == 0 ? "empty" : plural(NS[b], "secret", "secrets"))
if (OLD[b]) {
print dim(" read with the old key")
} else if (keys) {
print dim(" read by")
for (i = 1; i <= NR_[b]; i++) R[i] = who(b, RD[b, i]) "\t" RD[b, i]
isort(R, NR_[b])
w = 0
for (i = 1; i <= NR_[b]; i++) { split(R[i], p, "\t"); if (length(p[1]) > w) w = length(p[1]) }
for (i = 1; i <= NR_[b]; i++) { split(R[i], p, "\t"); s = p[1]; while (length(s) < w) s = s " "; print dim(" " s " " p[2]) }
} else {
n = 0; nadm = 0
for (i = 1; i <= NR_[b]; i++) {
k = RD[b, i]
if (is_admin(b, k)) { ADM[++nadm] = who(b, k) } else { R[++n] = who(b, k) }
}
isort(R, n)
if (n == 0) R[++n] = "the admins only"
else if (nadm > 0) R[++n] = "the admins"
print dim(wrap_list(" read by", n, R, " "))
}
for (i = 1; i <= NP[b]; i++) {
print warn(" ! " PMSG[b, i])
if (PFIX[b, i] != "") print " " dim("fix:") " " PFIX[b, i]
}
if (NS[b] > 0) grid(b)
}
# machine(): this machine's name, as the buckets' readers name its key.
function machine( k, b, me) {
me = ""
for (k in MINE) for (b = 1; b <= nb; b++) if ((b, k) in NAME && NAME[b, k] != "" && NAME[b, k] != "?" && !is_admin(b, k)) me = NAME[b, k]
for (k in MINE) if (k in ADMIN_NAME) me = ADMIN_NAME[k]
return me
}
function human( b, nread, nsec, nall, nsecall, nother, probs, pl, me, i, A, n, an) {
nread = 0; nsec = 0; nall = 0; nsecall = 0; probs = 0; pl = ""
for (b = 1; b <= nb; b++) {
nall++; nsecall += NS[b]
if (READABLE[b]) { nread++; nsec += NS[b] }
if ((READABLE[b] || all) && NP[b] > 0) { probs += NP[b]; pl = pl (pl == "" ? "" : ", ") LABEL[b] }
}
me = machine()
if (nread == 0 && !all) {
if (nb == 0) {
print "There are no buckets yet."
return
}
print "This machine reads none of the " plural(nb, "bucket", "buckets") " here."
if (!has_identity) {
print ""
print "It has no identity yet. Make one with"
print " szecret enrol"
print "then add its key to cloud-setup's szecret/hosts and rekey."
} else {
print ""
print "Its key is in no bucket's readers: add it to cloud-setup's"
print "szecret/hosts, then rekey the buckets it should read."
if (identity_key != "") print dim(" " identity_key)
}
print ""
print dim("All the buckets, with what is in them: szecret list --all")
return
}
first = 1
for (b = 1; b <= nb; b++) if (READABLE[b]) { if (!first) print ""; first = 0; show_bucket(b) }
if (all) {
nother = nall - nread
if (nother > 0) {
if (!first) print ""
print bold("Not read here") " " dim(plural(nother, "bucket", "buckets"))
for (b = 1; b <= nb; b++) if (!READABLE[b]) { print ""; show_bucket(b) }
}
}
print ""
if (me == "") me = "This machine"
if (all) print me " reads " nread " of " plural(nall, "bucket", "buckets") ", " nsec " of " plural(nsecall, "secret", "secrets")
else print me " reads " plural(nread, "bucket", "buckets") ", " plural(nsec, "secret", "secrets")
if (admins_known) {
an = 0
for (k in ADMIN) A[++an] = (k in ADMIN_NAME) ? ADMIN_NAME[k] : "unnamed admin"
isort(A, an)
# A name twice (several unnamed admins) is said once, with a count.
n = 0
for (i = 1; i <= an; i++) U[A[i]]++
for (i = 1; i <= an; i++) if (!(A[i] in DONE)) { DONE[A[i]] = 1; AL[++n] = U[A[i]] > 1 ? U[A[i]] " " A[i] "s" : A[i] }
if (n > 0) print dim(wrap_list("The admins read every bucket:", n, AL, " "))
} else {
print warn("! the admins are unknown (SZECRET_ADMINS_COMMAND unset or failing), so pending rekeys aren't checked")
}
if (!all && nall > nread) print dim(plural(nall - nread, "bucket", "buckets") " not read here: szecret list --all")
if (probs > 0) print warn("! " plural(probs, "problem", "problems") " in " pl)
}
function json( b, i, k, first, firstr, s) {
printf "{\"buckets\":["
first = 1
for (b = 1; b <= nb; b++) {
if (!READABLE[b] && !all) continue
printf "%s{\"name\":%s,\"old_store\":%s,\"readable\":%s,\"keyed\":%s,", (first ? "" : ","), jstr(LABEL[b]), (OLD[b] ? "true" : "false"), (READABLE[b] ? "true" : "false"), (OLD[b] || KEYED[b] ? "true" : "false")
first = 0
printf "\"readers\":["
for (i = 1; i <= NR_[b]; i++) JR[i] = who(b, RD[b, i]) "\t" RD[b, i]
isort(JR, NR_[b])
for (i = 1; i <= NR_[b]; i++) {
k = JR[i]; sub(/.*\t/, "", k)
printf "%s{\"name\":%s,\"key\":%s,\"admin\":%s,\"this_machine\":%s}", (i > 1 ? "," : ""), jstr(who(b, k)), jstr(k), (is_admin(b, k) ? "true" : "false"), ((k in MINE) ? "true" : "false")
}
printf "],\"secrets\":["
for (i = 1; i <= NS[b]; i++) G[i] = SEC[b, i]
isort(G, NS[b])
for (i = 1; i <= NS[b]; i++) printf "%s%s", (i > 1 ? "," : ""), jstr(G[i])
printf "],\"problems\":["
for (i = 1; i <= NP[b]; i++) {
printf "%s{\"problem\":%s", (i > 1 ? "," : ""), jstr(PMSG[b, i])
if (PFIX[b, i] != "") printf ",\"fix\":%s", jstr(PFIX[b, i])
printf "}"
}
printf "]}"
}
printf "],\"machine\":%s,\"admins_known\":%s}\n", (machine() == "" ? "null" : jstr(machine())), (admins_known ? "true" : "false")
}
END {
for (b = 1; b <= nb; b++) finish(b)
if (mode == "json") json(); else human()
}
AWK
mine="$(my_keys | paste -sd ' ' -)"
SZECRET_LIST_ADMIN_NAMES="${admin_named}" awk -v marker="${marker}" -v mode="$([[ -n "${json}" ]] && echo json || echo human)" \
-v all="${all:-0}" -v keys="${keys:-0}" -v color="${color:-0}" -v W="${width}" \
-v admin_list="$(echo "${admins}" | paste -sd ' ' -)" \
-v admins_known="${admins_known:-0}" -v mine="${mine}" -v old_readable="${old_readable}" \
-v has_identity="${has_identity:-0}" -v identity_key="${identity_key}" \
"${prog}" ${args[@]+"${args[@]}"} </dev/null
|