@@@ keys.retire-recov
[distorted-keys] / keyfunc.sh.in
CommitLineData
53263601
MW
1### -*-sh-*-
2###
3### Common key management functions.
4###
5### (c) 2011 Mark Wooding
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
599c8f75
MW
10### This file is part of the distorted.org.uk key management suite.
11###
12### distorted-keys is free software; you can redistribute it and/or modify
53263601
MW
13### it under the terms of the GNU General Public License as published by
14### the Free Software Foundation; either version 2 of the License, or
15### (at your option) any later version.
16###
599c8f75 17### distorted-keys is distributed in the hope that it will be useful,
53263601
MW
18### but WITHOUT ANY WARRANTY; without even the implied warranty of
19### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20### GNU General Public License for more details.
21###
22### You should have received a copy of the GNU General Public License
599c8f75 23### along with distorted-keys; if not, write to the Free Software Foundation,
53263601
MW
24### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26quis=${0##*/}
27
28###--------------------------------------------------------------------------
29### Configuration variables.
30
e787e19c 31## Automatically configured pathnames.
53263601 32PACKAGE="@PACKAGE@" VERSION="@VERSION@"
53263601
MW
33bindir="@bindir@"
34
e787e19c 35## Read user configuration.
c47f2aba 36if [ -f $ETC/keys.conf ]; then . $ETC/keys.conf; fi
599c8f75 37
e787e19c 38## Maybe turn on debugging.
599c8f75
MW
39case "${KEYS_DEBUG+t}" in t) set -x ;; esac
40
ec208149
MW
41## Fake up caller credentials if not called via userv.
42case "${USERV_USER+t}" in
43 t) ;;
44 *) USERV_USER=${LOGNAME-${USER-$(id -un)}} USERV_UID=$(id -u) ;;
45esac
46case "${USERV_GROUP+t}" in
47 t) ;;
48 *) USERV_GROUP=$(id -Gn) USERV_GID=$(id -gn) ;;
49esac
50
53263601
MW
51###--------------------------------------------------------------------------
52### Cleanup handling.
53
54cleanups=""
c47f2aba
MW
55cleanup () { cleanups=${cleanups+$cleanups }$1; }
56runcleanups () { for i in $cleanups; do $i; done; }
57trap 'rc=$?; runcleanups; exit $rc' EXIT
58trap 'trap "" EXIT; runcleanups; exit 127' INT TERM
53263601
MW
59
60###--------------------------------------------------------------------------
61### Utility functions.
62
c47f2aba
MW
63reqsafe () {
64 ## Fail unless a safe directory is set.
65
66 err="$quis: (CONFIGURATION ERROR)"
67 case ${SAFE+t} in
68 t) ;;
69 *) echo >&2 "$err: no SAFE directory"; exit 1 ;;
70 esac
71 if [ ! -d "$SAFE" ]; then
72 echo >&2 "$err: SAFE path \`$SAFE' isn't a directory"
73 exit 1
74 fi
75 case "$SAFE" in
76 [!/]* | *[][[:space:]*?]*)
77 echo >&2 "$err: SAFE path \`$SAFE' contains bad characters"
78 exit 1
79 ;;
80 esac
81 ls -ld "$SAFE" | {
82 me=$(id -un)
83 read perm _ user stuff
84 case "$perm:$user" in
85 d???------:"$me") ;;
86 *)
87 echo >&2 "$err: SAFE path \`$SAFE' has bad owner or permissions"
88 exit 1
89 ;;
90 esac
91 }
92}
93
53263601
MW
94## Temporary directory.
95unset tmp
c47f2aba
MW
96rmtmp () { case ${tmp+t} in t) cd /; rm -rf $tmp ;; esac; }
97cleanup rmtmp
53263601 98mktmp () {
c47f2aba 99 ## Make a temporary directory and store its name in `tmp'.
53263601 100
c47f2aba
MW
101 case "${tmp+t}" in t) return ;; esac
102 reqsafe
103 tmp="$SAFE/keys.tmp.$$"
53263601
MW
104 rm -rf "$tmp"
105 mkdir -m700 "$tmp"
53263601
MW
106}
107
c47f2aba
MW
108reqtmp () {
109 ## Fail unless a temporary directory is set.
53263601 110
c47f2aba
MW
111 case ${tmp+t} in
112 t) ;;
113 *) echo >&2 "$quis (INTERNAL): no tmp directory set"; exit 127 ;;
53263601
MW
114 esac
115}
116
c47f2aba
MW
117parse_keylabel () {
118 key=$1
119 ## Parse the key label string KEY. Set `kdir' to the base path to use for
120 ## the key's storage, and `kowner' to the key owner's name.
121
122 case "$key" in
123 *:*) kowner=${key%%:*} klabel=${key#*:} ;;
124 *) kowner=$USERV_USER klabel=$key ;;
53263601 125 esac
c47f2aba
MW
126 checkword "key owner name" "$kowner"
127 checklabel "key" "$klabel"
128 kdir=$KEYS/store/$kowner/$klabel
129 knub=$KEYS/nub/$kowner/$klabel
53263601
MW
130}
131
4c8c4065
MW
132runas () {
133 user=$1 service=$2; shift 2
134 ## If the current (effective) user is not USER then reinvoke via `userv',
135 ## as the specified service, with the remaining arguments.
136
137 case $(id -un) in
138 "$user") ;;
139 *) exec userv "$user" "$service" "$@" ;;
140 esac
141}
142
c47f2aba
MW
143###--------------------------------------------------------------------------
144### Input validation functions.
145
146nl="
147"
148check () {
149 ckwhat=$1 ckpat=$2 thing=$3
150 ## Verify that THING matches the (anchored, basic) regular expression
151 ## CKPAT. Since matching newlines is hard to do portably, also check that
152 ## THING doesn't contain any. If the checks fail, report an error and
153 ## exit.
154
155 validp=t
599c8f75 156 case "$thing" in
ec3628d8
MW
157 *"$nl"*)
158 validp=nil
159 ;;
160 *)
161 if ! expr >/dev/null "Q$thing" : "\(Q$ckpat\)\$"; then
162 validp=nil
163 fi
164 ;;
c47f2aba
MW
165 esac
166 case $validp in
167 nil) echo >&2 "$quis: bad $ckwhat \`$thing'"; exit 1 ;;
599c8f75
MW
168 esac
169}
170
c47f2aba
MW
171## Regular expressions for validating input.
172R_IDENTCHARS="A-Za-z0-9_"
09c56d3e
MW
173R_GOODPUNCT="!%@+="
174R_WORDCHARS="-$R_IDENTCHARS$R_GOODPUNCT"
c47f2aba
MW
175R_IDENT="[$R_IDENTCHARS][$R_IDENTCHARS]*"
176R_WORD="[$R_WORDCHARS][$R_WORDCHARS]*"
09c56d3e 177R_ACLCHARS="][$R_IDENTCHARS$R_GOODPUNCT*?:.#"
c47f2aba 178R_WORDSEQ="[$R_WORDCHARS[:space:]][$R_WORDCHARS[:space:]]*"
09c56d3e 179R_ACL="[$R_ACLCHARS[:space:]-][$R_ACLCHARS[:space:]-]*"
c47f2aba
MW
180R_NUMERIC='\(\([1-9][0-9]*\)\{0,1\}0\{0,1\}\)'
181R_LABEL="\($R_WORD\(/$R_WORD\)*\)"
182R_LINE=".*"
183
184## Various validation functions.
185checknumber () { check "$1" "$R_NUMERIC" "$2"; }
186checkident () { check "$1" "$R_IDENT" "$2"; }
187checkword () { check "$1" "$R_WORD" "$2"; }
188checklabel () { check "$1 label" "$R_LABEL" "$2"; }
189
5fb66a7a
MW
190## Boolean canonification.
191boolify () {
192 var=$1 what=$2
193
194 eval v=\$$var
195 case $v in
196 1 | y | yes | on | t | true) v=t ;;
197 0 | n | no | off | f | false | nil) v=nil ;;
198 *) echo >&2 "$quis: bad boolean $what \`$v'"; exit 1 ;;
199 esac
200 eval $var=\$v
201}
202
53263601 203###--------------------------------------------------------------------------
c47f2aba
MW
204### Key storage and properties.
205
206getsysprofile () {
207 profile=$1
208 ## Write the named system PROFILE to standard output.
209
b65e1f93 210 $bindir/extract-profile "$profile" $ETC/profile.d/
c47f2aba
MW
211}
212
213setprops () {
214 what=$1 prefix=$2; shift 2
215 ## Set variables based on the NAME=VALUE assignments in the arguments. The
216 ## value for property NAME is stored in the shell variable PREFIX_NAME.
217
218 for assg in "$@"; do
219 goodp=t
220 case "$assg" in
221 *\=*) name=${assg%%=*} value=${assg#*=} ;;
222 *) goodp=nil ;;
223 esac
224 case "$goodp,$name" in t,*[!0-9A-Za-z_]*=*) goodp=nil ;; esac
225 case "$goodp" in
226 nil) echo >&2 "$quis: bad $what assignment \`$assg'"; exit 1 ;;
227 esac
228 eval "$prefix$name=\$value"
229 done
230}
53263601 231
c47f2aba
MW
232checkprops () {
233 whatprop=$1 prefix=$2; shift 2
234 ## Check that property variables are set in accordance with the remaining
235 ## TABLE arguments. Each row of TABLE has the form
53263601 236 ##
c47f2aba
MW
237 ## NAME OMIT PAT
238 ##
239 ## A table row is satisfied if there is a variable PREFIXNAME whose value
240 ## matces the (basic) regular expression PAT, or if the variable is unset
241 ## and OMIT is `t'.
242
243 for table in "$@"; do
244 case "$table" in ?*) ;; *) continue ;; esac
245 while read -r name omit pat; do
246 eval foundp=\${$prefix$name+t}
247 case "$foundp,$omit" in
248 ,t) continue ;;
249 ,nil)
250 echo >&2 "$quis: missing $whatprop \`$name' required"
251 exit 1
252 ;;
253 esac
254 eval value=\$$prefix$name
255 check "value for $whatprop \`$name'" "$pat" "$value"
256 done <<EOF
257$table
258EOF
259 done
260}
53263601 261
f9431707
MW
262dumpprops () {
263 prefix=$1
264 ## Write the properties stored in the variables beginning with PREFIX.
265
266 set | sed -n "/^$prefix/{s/=.*\$//;p}" | sort | while read name; do
267 eval value=\$$name
268 echo "${name#$prefix}=$value"
269 done
270}
271
c47f2aba
MW
272defprops () {
273 name=$1
274 ## Define a properties table NAME.
275
276 table=$(cat)
277 eval $name=\$table
278}
279
280defprops g_props <<EOF
281type nil $R_IDENT
282recovery t $R_WORDSEQ
283random t $R_WORD
2a877b7f
MW
284nub_hash t $R_WORD
285nubid_hash t $R_WORD
286nub_random_bytes t $R_NUMERIC
09c56d3e
MW
287acl_encrypt t $R_ACL
288acl_decrypt t $R_ACL
289acl_sign t $R_ACL
290acl_verify t $R_ACL
291acl_info t $R_ACL
c47f2aba
MW
292EOF
293
294readprops () {
295 file=$1
296 ## Read a profile from a file. This doesn't check the form of the
297 ## filename, so it's not suitable for unchecked input. Properties are set
298 ## using `setprops' with prefix `kprop_'.
299
300 ## Parse the settings from the file.
301 exec 3<"$file"
302 while read line; do
303 case "$line" in "" | \#*) continue ;; esac
304 setprops "property" kprop_ "$line"
305 done <&3
306 exec 3>&-
307 checkprops "property" kprop_ "$g_props"
308
309 ## Fetch the key-type handling library.
310 if [ ! -f $KEYSLIB/ktype.$kprop_type ]; then
311 echo >&2 "$quis: unknown key type \`$kprop_type'"
312 exit 1
313 fi
314 . $KEYSLIB/ktype.$kprop_type
315 checkprops "property" kprop_ "$k_props"
316}
317
318readmeta () {
319 kdir=$1
320 ## Read key metadata from KDIR.
321
322 { read profile; } <"$kdir"/meta
323}
324
325makenub () {
326 ## Generate a key nub in the default way, and write it to standard output.
2a877b7f
MW
327 ## The properties `random', `nub_random_bytes' and `nub_hash' are referred
328 ## to.
c47f2aba
MW
329
330 dd 2>/dev/null \
2a877b7f
MW
331 if=/dev/${kprop_random-random} bs=1 count=${kprop_nub_random_bytes-64} |
332 openssl dgst -${kprop_nub_hash-sha256} -binary |
c47f2aba
MW
333 openssl base64
334}
335
336nubid () {
337 ## Compute a hash of the key nub in stdin, and write it to stdout in hex.
2a877b7f 338 ## The property `nubid_hash' is used.
c47f2aba 339
21a21fff
MW
340 ## Stupid dance because the output incompatibly grew a filename, in order
341 ## to demonstrate the same idiocy as GNU mumblesum.
342 set _ $({ echo "distorted-keys nubid"; cat -; } |
343 openssl dgst -${kprop_nubid_hash-sha256})
37ba6d05 344 if [ $# -gt 2 ]; then shift; fi
21a21fff 345 echo $2
c47f2aba
MW
346}
347
348subst () {
349 what=$1 templ=$2 prefix=$3 pat=$4
350 ## Substitute option values into the template TEMPL. Each occurrence of
351 ## %{VAR} is replaced by the value of the variable PREFIXVAR. Finally, an
352 ## error is reported unless the final value matches the regular expression
353 ## PAT.
354
355 out=""
356 rest=$templ
357 while :; do
358
359 ## If there are no more markers to substitute, then finish.
360 case "$rest" in *"%{"*"}"*) ;; *) out=$out$rest; break ;; esac
361
362 ## Split the template into three parts.
363 left=${rest%%\%\{*} right=${rest#*\%\{}
364 var=${right%%\}*} rest=${right#*\}}
365 case "$var" in
366 *-*) default=${var#*-} var=${var%%-*} defaultp=t ;;
367 *) defaultp=nil ;;
368 esac
369
370 ## Find the variable value.
371 checkident "template variable name" "$var"
372 eval foundp=\${$prefix$var+t}
373 case $foundp,$defaultp in
374 t,*) eval value=\$$prefix$var ;;
375 ,t) value=$default ;;
376 *)
377 echo >&2 "$quis: option \`$var' unset, used in template \`$templ'"
378 exit 1
379 ;;
380 esac
381
382 ## Do the substitution.
383 out=$out$left$value
384 done
385
386 ## Check the final result.
387 check "$what" "$pat" "$out"
388
389 ## Done.
390 echo "$out"
391}
392
393read_profile () {
e9cf7079 394 owner=$1 profile=$2
c47f2aba 395 ## Read property settings from a profile. The PROFILE name has the form
e9cf7079
MW
396 ## [USER:]LABEL; USER defaults to OWNER. Properties are set using
397 ## `setprops' with prefix `kprop_'.
c47f2aba
MW
398
399 reqtmp
400 case "$profile" in
401 :*)
402 label=${profile#:} uservp=nil
403 ;;
53263601 404 *)
e9cf7079 405 user=$kowner label=$profile uservp=t
c47f2aba
MW
406 ;;
407 *:*)
408 user=${profile%%:*} label=${profile#*:} uservp=t
409 ;;
410 esac
411 checkword "profile label" "$label"
412
413 ## Fetch the profile settings from the user.
414 reqtmp
415 case $uservp in
416 t)
417 checkword "profile user" "$user"
21a21fff 418 userv "$user" cryptop-profile "$label" >$tmp/profile </dev/null
c47f2aba
MW
419 ;;
420 nil)
b65e1f93 421 $bindir/extract-profile "$label" $ETC/profile.d/ >$tmp/profile
53263601
MW
422 ;;
423 esac
424
c47f2aba
MW
425 ## Read the file.
426 readprops $tmp/profile
53263601
MW
427}
428
c47f2aba
MW
429###--------------------------------------------------------------------------
430### General crypto operations.
431
432c_genkey () {
433 profile=$1 kdir=$2 knub=$3 hook=$4; shift 4
434 ## Generate a key, and associate it with the named PROFILE (which is
435 ## assumed already to have been read!); store the main data in KDIR, and
436 ## the nub separately in the file KNUB; run HOOK after generation, passing
437 ## it the working key directory and nub file. Remaining arguments are
438 ## options to the key type.
439
440 ## Set options and check them.
f93aa266 441 kopt_owner=$kowner kopt_label=$klabel
c47f2aba
MW
442 setprops "option" kopt_ "$@"
443 checkprops "option" kopt_ "$k_genopts"
444
445 ## Create directory structure and start writing metadata.
446 rm -rf "$kdir.new"
447 mkdir -m755 -p "$kdir.new"
89f8efd7 448 case "$knub" in */*) mkdir -m755 -p "${knub%/*}" ;; esac
c47f2aba
MW
449 cat >"$kdir.new/meta" <<EOF
450$profile
451EOF
53263601 452
c47f2aba 453 ## Generate the key.
28bb1ec3 454 (umask 077; makenub >"$knub.new")
c47f2aba
MW
455 k_generate "$kdir.new" "$knub.new"
456 $hook "$kdir.new" "$knub.new"
457
458 ## Hash the nub.
459 nubid <"$knub.new" >"$kdir.new/nubid"
460
461 ## Juggle everything into place. Doing this atomically is very difficult,
462 ## and requires more machinery than I can really justify here. If
463 ## something goes wrong halfway, it should always be possible to fix it,
464 ## either by backing out (if $kdir.new still exists) or pressing on
465 ## forwards (if not).
466 rm -rf "$kdir.old"
467 if [ -e "$kdir" ]; then mv "$kdir" "$kdir.old"; fi
468 mv "$kdir.new" "$kdir"
469 mv "$knub.new" "$knub"
470 rm -rf "$kdir.old"
53263601
MW
471}
472
c47f2aba
MW
473c_encrypt () { k_encrypt "$@"; }
474c_decrypt () {
475 if k_decrypt "$@" >$tmp/plain; then cat $tmp/plain
476 else return $?
477 fi
478}
479c_sign () { k_sign "$@"; }
480c_verify () { k_verify "$@"; }
481
482## Stub implementations.
483notsupp () { op=$1; echo >&2 "$quis: operation \`$op' not supported"; }
484k_info () { :; }
fff6c653 485k_import () { :; }
c47f2aba
MW
486k_encrypt () { notsupp encrypt; }
487k_decrypt () { notsupp decrypt; }
488k_sign () { notsupp sign; }
489k_verify () { notsupp verify; }
490
491prepare () {
492 key=$1 op=$2
493 ## Prepare for a crypto operation OP, using the KEY. This validates the
5cff41ea
MW
494 ## key label, reads the profile, and checks the access-control list. If OP
495 ## is `-' then allow the operation unconditionally.
c47f2aba
MW
496
497 ## Find the key properties.
498 parse_keylabel "$key"
499 if [ ! -d $kdir ]; then echo >&2 "$quis: unknown key \`$key'"; exit 1; fi
500 readmeta $kdir
e9cf7079 501 read_profile $kowner "$profile"
c47f2aba
MW
502
503 ## Check whether we're allowed to do this thing. This is annoyingly
504 ## fiddly.
5cff41ea 505 case $op in -) return ;; esac
c47f2aba
MW
506 eval acl=\${kprop_acl_$op-!owner}
507 verdict=forbid
508 while :; do
509
510 ## Remove leading whitespace.
511 while :; do
512 case "$acl" in
513 [[:space:]]*) acl=${acl#?} ;;
514 *) break ;;
515 esac
516 done
517
518 ## If there's nothing left, leave.
519 case "$acl" in ?*) ;; *) break ;; esac
520
521 ## Split off the leading word.
522 case "$acl" in
523 *[[:space:]]*) word=${acl%%[[:space:]]*} acl=${acl#*[[:space:]]} ;;
524 *) word=$acl acl="" ;;
525 esac
526
527 ## See what sense it has if it matches.
528 case "$word" in
529 -*) sense=forbid rest=${word#-} ;;
530 *) sense=allow rest=$word ;;
531 esac
532
533 ## See whether the calling user matches.
534 case "$rest" in
535 !owner) pat=$kowner list=$USERV_USER ;;
536 !*) echo >&2 "$quis: unknown ACL token \`$word'" ;;
537 %*) pat=${rest#%} list="$USERV_GROUP $USERV_GID" ;;
538 *) pat=$rest list="$USERV_USER $USERV_UID" ;;
539 esac
540 matchp=nil
541 for i in $list; do case "$i" in $pat) matchp=t; break ;; esac; done
542 case $matchp in t) verdict=$sense; break ;; esac
543 done
544
545 case $verdict in
68023101 546 forbid) echo >&2 "$quis: $op access to key \`$key' forbidden"; exit 1 ;;
c47f2aba 547 esac
53263601
MW
548}
549
c47f2aba
MW
550###--------------------------------------------------------------------------
551### Crypto operations for infrastructure purposes.
552
553c_sysprofile () {
554 profile=$1
555 ## Select the profile in FILE for future crypto operations.
53263601 556
c47f2aba
MW
557 unset $(set | sed -n '/^kprop_/s/=.*$//p')
558 reqtmp
559 getsysprofile "$profile" >$tmp/profile
560 readprops $tmp/profile
53263601
MW
561}
562
c47f2aba
MW
563c_gensyskey () {
564 profile=$1 kdir=$2 knub=$3; shift 3
565 ## Generate a system key using PROFILE; store the data in KDIR and the nub
566 ## in KNUB. Remaining arguments are options.
53263601 567
c47f2aba
MW
568 c_sysprofile "$profile"
569 c_genkey "$profile" "$kdir" "$knub" : "$@"
53263601
MW
570}
571
c47f2aba
MW
572c_sysprepare () {
573 kdir=$1
574 readmeta "$kdir"
575 c_sysprofile "$profile"
576}
599c8f75 577
c47f2aba
MW
578c_sysop () {
579 op=$1 kdir=$2; shift 1
580 c_sysprepare "$kdir"
581 c_$op "$@"
599c8f75
MW
582}
583
c47f2aba
MW
584c_sysencrypt () { c_sysop encrypt "$1" /dev/null; }
585c_sysdecrypt () { c_sysop decrypt "$1" "$2"; }
586c_syssign () { c_sysop sign "$1" "$2"; }
587c_sysverify () { c_sysop verify "$1" /dev/null; }
588
589###--------------------------------------------------------------------------
590### Recovery operations.
591
2661d8aa
MW
592sharethresh () {
593 pf=$1
594 ## Return the sharing threshold from the parameter file PARAM.
595
596 read param <"$pf"
597 case "$param" in
598 shamir-params:*) ;;
599 *)
600 echo >&2 "$quis: secret sharing parameter file damaged (wrong header)"
601 exit 1
602 ;;
603 esac
604 t=";${param#*:}"
605 case "$t" in
606 *";t="*) ;;
607 *)
608 echo >&2 "$quis: secret sharing parameter file damaged (missing t)"
609 exit 1
610 ;;
611 esac
612 t=${t#*;t=}
613 t=${t%%;*}
614 echo "$t"
615}
616
c47f2aba
MW
617stash () {
618 recov=$1 label=$2
619 ## Stash a copy of stdin encrypted under the recovery key RECOV, with a
620 ## given LABEL.
621 checkword "recovery key label" "$recov"
622 checklabel "secret" "$label"
623
624 rdir=$KEYS/recov/$recov/current
625 if [ ! -d $rdir/store ]; then
626 echo >&2 "$quis: unknown recovery key \`$recov'"
627 exit 1
628 fi
629 case $label in */*) mkdir -m755 -p $rdir/${label%/*} ;; esac
630 (c_sysencrypt $rdir/store >$rdir/$label.new)
631 mv $rdir/$label.new $rdir/$label.recov
632}
599c8f75 633
c47f2aba 634recover () {
ae0eb898 635 recov=$1 inst=$2 label=$3
c47f2aba
MW
636 ## Recover a stashed secret, protected by RECOV and stored as LABEL, and
637 ## write it to stdout.
638 checkword "recovery key label" "$recov"
ae0eb898 639 checkword "recovery instance" "$inst"
c47f2aba
MW
640 checklabel "secret" "$label"
641
ae0eb898 642 rdir=$KEYS/recov/$recov/$inst
c47f2aba 643 if [ ! -f $rdir/$label.recov ]; then
1581d63e 644 echo >&2 "$quis: recovery key \`$recov/$inst' has no blob for \`$label'"
c47f2aba
MW
645 exit 1
646 fi
647 reqsafe
ae0eb898
MW
648 tag=$recov.$inst
649 nub=$SAFE/keys.reveal/$tag/nub
c47f2aba 650 if [ ! -f $nub ]; then
1581d63e 651 echo >&2 "$quis: recovery key \`$recov/$inst' not revealed"
c47f2aba
MW
652 exit 1;
653 fi
654 mktmp
655 c_sysdecrypt $rdir/store $nub <$rdir/$label.recov
599c8f75
MW
656}
657
53263601
MW
658###--------------------------------------------------------------------------
659### Help text.
660
c47f2aba
MW
661defhelp () {
662 read umsg
865fd3f4 663 usage=$umsg
c47f2aba
MW
664 help=$(cat)
665 case "$KEYS_HELP" in t) help; exit ;; esac
53263601
MW
666}
667
53263601
MW
668help () { showhelp; }
669showhelp () {
670 cat <<EOF
865fd3f4 671Usage: $quis${usage:+ $usage}
53263601
MW
672
673$help
674EOF
675}
676
865fd3f4
MW
677usage () {
678 : ${cmdargs=$usage}
679 echo "usage: $quis${cmdname:+ $cmdname}${cmdargs:+ $cmdargs}"
680}
681usage_err () { usage >&2; exit 1; }
c47f2aba
MW
682
683###--------------------------------------------------------------------------
684### Subcommand handling.
685
686version () {
1d0c984e 687 echo "$quis, $PACKAGE version $VERSION"
c47f2aba
MW
688}
689
865fd3f4
MW
690unset cmdargs
691unset cmdname
692cmds=""
693defcmd () {
694 cmd=$1; shift; args=$*
695 help=$(cat)
696 eval help_$cmd=\$help
697 cmds="${cmds:+$cmds
698}$cmd $args"
699}
700
701defcmd help "[COMMAND ...]" <<EOF
702Show help about the COMMANDs, or about $quis if none are named.
703EOF
c47f2aba
MW
704cmd_help () {
705 rc=0
706 version
707 case $# in
708 0)
709 cat <<EOF
710
865fd3f4 711Usage: $quis${usage:+ $usage}
c47f2aba
MW
712
713Options:
714 -h Show this help text.
715 -v Show the program version number.
716
865fd3f4 717Commands provided:
c47f2aba 718EOF
865fd3f4
MW
719 while read cmd args; do echo " $cmd${args:+ $args}"; done <<EOF
720$cmds
721EOF
722 case ${prefix+t} in
723 t)
724 cd $KEYSLIB
725 for i in $prefix.*; do
726 if [ ! -x "$i" ]; then continue; fi
727 sed -n "/<<HELP/{n;s/^/ ${i#$prefix.} /;p;q;}" "$i"
728 done
729 ;;
730 esac
c47f2aba
MW
731 ;;
732 *)
865fd3f4 733 for cmd in "$@"; do
c47f2aba 734 echo
865fd3f4
MW
735 foundp=nil
736 while read cmdname cmdargs; do
737 case $cmdname in "$cmd") foundp=t; break ;; esac
738 done <<EOF
739$cmds
740EOF
741 case $foundp in
742 t)
0bc47568 743 usage; echo
865fd3f4
MW
744 eval help=\$help_$cmdname; echo "$help"
745 ;;
746 nil)
747 if [ ! -x "$KEYSLIB/$prefix.$cmd" ]; then
748 echo >&2 "$quis: unrecognized command \`$cmd'"
749 rc=1
750 continue
751 elif ! KEYS_HELP=t "$KEYSLIB/$prefix.$cmd"; then
752 rc=1
753 fi
754 ;;
755 esac
c47f2aba
MW
756 done
757 ;;
758 esac
759 return $rc
760}
761
762dispatch () {
865fd3f4 763 case $# in 0) usage_err ;; esac
c47f2aba 764 cmd=$1; shift
865fd3f4
MW
765 foundp=nil
766 while read cmdname cmdargs; do
767 case $cmdname in "$cmd") foundp=t; break ;; esac
768 done <<EOF
769$cmds
770EOF
771 case $foundp in
772 t)
773 OPTIND=1
774 cmd_$cmdname "$@"
775 ;;
776 nil)
777 if [ ! -x "$KEYSLIB/$prefix.$cmd" ]; then
778 echo >&2 "$quis: unrecognized command \`$cmd'"
779 exit 1
780 fi
781 unset KEYS_HELP
782 exec "$KEYSLIB/$prefix.$cmd" "$@"
783 ;;
784 esac
c47f2aba
MW
785}
786
53263601 787###----- That's all, folks --------------------------------------------------