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