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