chiark / gitweb /
keyfunc.sh.in: Fix the OpenSSL `dgst' rune.
[distorted-keys] / keyfunc.sh.in
index b55bd99d0c766e7908304fca2a79f964233358de..02bc10dd8af0d70e94ccb985cdf3906348e2997e 100644 (file)
@@ -129,6 +129,17 @@ parse_keylabel () {
   knub=$KEYS/nub/$kowner/$klabel
 }
 
+runas () {
+  user=$1 service=$2; shift 2
+  ## If the current (effective) user is not USER then reinvoke via `userv',
+  ## as the specified service, with the remaining arguments.
+
+  case $(id -un) in
+    "$user") ;;
+    *) exec userv "$user" "$service" "$@" ;;
+  esac
+}
+
 ###--------------------------------------------------------------------------
 ### Input validation functions.
 
@@ -229,6 +240,16 @@ EOF
   done
 }
 
+dumpprops () {
+  prefix=$1
+  ## Write the properties stored in the variables beginning with PREFIX.
+
+  set | sed -n "/^$prefix/{s/=.*\$//;p}" | sort | while read name; do
+    eval value=\$$name
+    echo "${name#$prefix}=$value"
+  done
+}
+
 defprops () {
   name=$1
   ## Define a properties table NAME.
@@ -297,8 +318,12 @@ nubid () {
   ## Compute a hash of the key nub in stdin, and write it to stdout in hex.
   ## The property `nubid_hash' is used.
 
-  { echo "distorted-keys nubid"; cat -; } |
-  openssl dgst -${kprop_nubid_hash-sha256}
+  ## Stupid dance because the output incompatibly grew a filename, in order
+  ## to demonstrate the same idiocy as GNU mumblesum.
+  set _ $({ echo "distorted-keys nubid"; cat -; } |
+    openssl dgst -${kprop_nubid_hash-sha256})
+  if [ $# -gt 2 ]; then shift; fi
+  echo $2
 }
 
 subst () {
@@ -371,7 +396,7 @@ read_profile () {
   case $uservp in
     t)
       checkword "profile user" "$user"
-      userv "$user" cryptop-profile "$label" >$tmp/profile
+      userv "$user" cryptop-profile "$label" >$tmp/profile </dev/null
       ;;
     nil)
       $bindir/extract-profile "$label" $ETC/profile.d/ >$tmp/profile
@@ -437,6 +462,7 @@ c_verify () { k_verify "$@"; }
 ## Stub implementations.
 notsupp () { op=$1; echo >&2 "$quis: operation \`$op' not supported"; }
 k_info () { :; }
+k_import () { :; }
 k_encrypt () { notsupp encrypt; }
 k_decrypt () { notsupp decrypt; }
 k_sign () { notsupp sign; }
@@ -543,6 +569,31 @@ c_sysverify () { c_sysop verify "$1" /dev/null; }
 ###--------------------------------------------------------------------------
 ### Recovery operations.
 
+sharethresh () {
+  pf=$1
+  ## Return the sharing threshold from the parameter file PARAM.
+
+  read param <"$pf"
+  case "$param" in
+    shamir-params:*) ;;
+    *)
+      echo >&2 "$quis: secret sharing parameter file damaged (wrong header)"
+      exit 1
+      ;;
+  esac
+  t=";${param#*:}"
+  case "$t" in
+    *";t="*) ;;
+    *)
+      echo >&2 "$quis: secret sharing parameter file damaged (missing t)"
+      exit 1
+      ;;
+  esac
+  t=${t#*;t=}
+  t=${t%%;*}
+  echo "$t"
+}
+
 stash () {
   recov=$1 label=$2
   ## Stash a copy of stdin encrypted under the recovery key RECOV, with a
@@ -587,7 +638,7 @@ recover () {
 
 defhelp () {
   read umsg
-  usage="usage: $quis${umsg+ }$umsg"
+  usage=$umsg
   help=$(cat)
   case "$KEYS_HELP" in t) help; exit ;; esac
 }
@@ -595,13 +646,17 @@ defhelp () {
 help () { showhelp; }
 showhelp () {
   cat <<EOF
-$usage
+Usage: $quis${usage:+ $usage}
 
 $help
 EOF
 }
 
-usage_err () { echo >&2 "$usage"; exit 1; }
+usage () {
+  : ${cmdargs=$usage}
+  echo "usage: $quis${cmdname:+ $cmdname}${cmdargs:+ $cmdargs}"
+}
+usage_err () { usage >&2; exit 1; }
 
 ###--------------------------------------------------------------------------
 ### Subcommand handling.
@@ -610,6 +665,20 @@ version () {
   echo "$PACKAGE version $VERSION"
 }
 
+unset cmdargs
+unset cmdname
+cmds=""
+defcmd () {
+  cmd=$1; shift; args=$*
+  help=$(cat)
+  eval help_$cmd=\$help
+  cmds="${cmds:+$cmds
+}$cmd $args"
+}
+
+defcmd help "[COMMAND ...]" <<EOF
+Show help about the COMMANDs, or about $quis if none are named.
+EOF
 cmd_help () {
   rc=0
   version
@@ -617,30 +686,50 @@ cmd_help () {
     0)
       cat <<EOF
 
-$usage
+Usage: $quis${usage:+ $usage}
 
 Options:
   -h           Show this help text.
   -v           Show the program version number.
 
-Commands installed:
+Commands provided:
 EOF
-      cd "$KEYSLIB"
-      for i in $prefix.*; do
-       if [ ! -x "$i" ]; then continue; fi
-       sed -n "/<<HELP/{n;s/^/ ${i#$prefix.} /;p;q;}" "$i"
-      done
+      while read cmd args; do echo "   $cmd${args:+ $args}"; done <<EOF
+$cmds
+EOF
+      case ${prefix+t} in
+       t)
+         cd $KEYSLIB
+         for i in $prefix.*; do
+           if [ ! -x "$i" ]; then continue; fi
+           sed -n "/<<HELP/{n;s/^/     ${i#$prefix.} /;p;q;}" "$i"
+         done
+         ;;
+      esac
       ;;
     *)
-      for i in "$@"; do
+      for cmd in "$@"; do
        echo
-       if [ ! -x "$KEYSLIB/$prefix.$i" ]; then
-         echo >&2 "$quis: unrecognized command \`$i'"
-         rc=1
-         continue
-       elif ! KEYS_HELP=t "$KEYSLIB/$prefix.$i"; then
-         rc=1
-       fi
+       foundp=nil
+       while read cmdname cmdargs; do
+         case $cmdname in "$cmd") foundp=t; break ;; esac
+       done <<EOF
+$cmds
+EOF
+       case $foundp in
+         t)
+           eval help=\$help_$cmdname; echo "$help"
+           ;;
+         nil)
+           if [ ! -x "$KEYSLIB/$prefix.$cmd" ]; then
+             echo >&2 "$quis: unrecognized command \`$cmd'"
+             rc=1
+             continue
+           elif ! KEYS_HELP=t "$KEYSLIB/$prefix.$cmd"; then
+             rc=1
+           fi
+           ;;
+       esac
       done
       ;;
   esac
@@ -648,16 +737,28 @@ EOF
 }
 
 dispatch () {
-  case $# in 0) echo >&2 "$usage"; exit 1 ;; esac
+  case $# in 0) usage_err ;; esac
   cmd=$1; shift
-  case "$cmd" in help) cmd_help "$@"; exit ;; esac
-  if [ ! -x "$KEYSLIB/$prefix.$cmd" ]; then
-    echo >&2 "$quis: unrecognized command \`$cmd'"
-    exit 1
-  fi
-
-  unset KEYS_HELP
-  exec "$KEYSLIB/$prefix.$cmd" "$@"
+  foundp=nil
+  while read cmdname cmdargs; do
+    case $cmdname in "$cmd") foundp=t; break ;; esac
+  done <<EOF
+$cmds
+EOF
+  case $foundp in
+    t)
+      OPTIND=1
+      cmd_$cmdname "$@"
+      ;;
+    nil)
+      if [ ! -x "$KEYSLIB/$prefix.$cmd" ]; then
+       echo >&2 "$quis: unrecognized command \`$cmd'"
+       exit 1
+      fi
+      unset KEYS_HELP
+      exec "$KEYSLIB/$prefix.$cmd" "$@"
+      ;;
+  esac
 }
 
 ###----- That's all, folks --------------------------------------------------