Commit | Line | Data |
---|---|---|
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 | ||
26 | quis=${0##*/} | |
27 | ||
28 | ###-------------------------------------------------------------------------- | |
29 | ### Configuration variables. | |
30 | ||
e787e19c | 31 | ## Automatically configured pathnames. |
53263601 | 32 | PACKAGE="@PACKAGE@" VERSION="@VERSION@" |
53263601 MW |
33 | bindir="@bindir@" |
34 | ||
e787e19c | 35 | ## Read user configuration. |
c47f2aba | 36 | if [ -f $ETC/keys.conf ]; then . $ETC/keys.conf; fi |
599c8f75 | 37 | |
e787e19c | 38 | ## Maybe turn on debugging. |
599c8f75 MW |
39 | case "${KEYS_DEBUG+t}" in t) set -x ;; esac |
40 | ||
ec208149 MW |
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 | ||
53263601 MW |
51 | ###-------------------------------------------------------------------------- |
52 | ### Cleanup handling. | |
53 | ||
54 | cleanups="" | |
c47f2aba MW |
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 | |
53263601 MW |
59 | |
60 | ###-------------------------------------------------------------------------- | |
61 | ### Utility functions. | |
62 | ||
c47f2aba MW |
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 | ||
53263601 MW |
94 | ## Temporary directory. |
95 | unset tmp | |
c47f2aba MW |
96 | rmtmp () { case ${tmp+t} in t) cd /; rm -rf $tmp ;; esac; } |
97 | cleanup rmtmp | |
53263601 | 98 | mktmp () { |
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 |
108 | reqtmp () { |
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 |
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 ;; | |
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 |
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 | ||
c47f2aba MW |
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 | |
599c8f75 | 156 | case "$thing" in |
c47f2aba | 157 | *"$nl"*) validp=nil ;; |
11c7b588 | 158 | *) if ! expr >/dev/null "Q$thing" : "Q$ckpat\$"; then validp=nil; fi ;; |
c47f2aba MW |
159 | esac |
160 | case $validp in | |
161 | nil) echo >&2 "$quis: bad $ckwhat \`$thing'"; exit 1 ;; | |
599c8f75 MW |
162 | esac |
163 | } | |
164 | ||
c47f2aba MW |
165 | ## Regular expressions for validating input. |
166 | R_IDENTCHARS="A-Za-z0-9_" | |
09c56d3e MW |
167 | R_GOODPUNCT="!%@+=" |
168 | R_WORDCHARS="-$R_IDENTCHARS$R_GOODPUNCT" | |
c47f2aba MW |
169 | R_IDENT="[$R_IDENTCHARS][$R_IDENTCHARS]*" |
170 | R_WORD="[$R_WORDCHARS][$R_WORDCHARS]*" | |
09c56d3e | 171 | R_ACLCHARS="][$R_IDENTCHARS$R_GOODPUNCT*?:.#" |
c47f2aba | 172 | R_WORDSEQ="[$R_WORDCHARS[:space:]][$R_WORDCHARS[:space:]]*" |
09c56d3e | 173 | R_ACL="[$R_ACLCHARS[:space:]-][$R_ACLCHARS[:space:]-]*" |
c47f2aba MW |
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 | ||
53263601 | 184 | ###-------------------------------------------------------------------------- |
c47f2aba MW |
185 | ### Key storage and properties. |
186 | ||
187 | getsysprofile () { | |
188 | profile=$1 | |
189 | ## Write the named system PROFILE to standard output. | |
190 | ||
b65e1f93 | 191 | $bindir/extract-profile "$profile" $ETC/profile.d/ |
c47f2aba MW |
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 | } | |
53263601 | 212 | |
c47f2aba MW |
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 | |
53263601 | 217 | ## |
c47f2aba MW |
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 | } | |
53263601 | 242 | |
f9431707 MW |
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 | ||
c47f2aba MW |
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 | |
2a877b7f MW |
265 | nub_hash t $R_WORD |
266 | nubid_hash t $R_WORD | |
267 | nub_random_bytes t $R_NUMERIC | |
09c56d3e MW |
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 | |
c47f2aba MW |
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. | |
2a877b7f MW |
308 | ## The properties `random', `nub_random_bytes' and `nub_hash' are referred |
309 | ## to. | |
c47f2aba MW |
310 | |
311 | dd 2>/dev/null \ | |
2a877b7f MW |
312 | if=/dev/${kprop_random-random} bs=1 count=${kprop_nub_random_bytes-64} | |
313 | openssl dgst -${kprop_nub_hash-sha256} -binary | | |
c47f2aba MW |
314 | openssl base64 |
315 | } | |
316 | ||
317 | nubid () { | |
318 | ## Compute a hash of the key nub in stdin, and write it to stdout in hex. | |
2a877b7f | 319 | ## The property `nubid_hash' is used. |
c47f2aba | 320 | |
21a21fff MW |
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}) | |
37ba6d05 | 325 | if [ $# -gt 2 ]; then shift; fi |
21a21fff | 326 | echo $2 |
c47f2aba MW |
327 | } |
328 | ||
329 | subst () { | |
330 | what=$1 templ=$2 prefix=$3 pat=$4 | |
331 | ## Substitute option values into the template TEMPL. Each occurrence of | |
332 | ## %{VAR} is replaced by the value of the variable PREFIXVAR. Finally, an | |
333 | ## error is reported unless the final value matches the regular expression | |
334 | ## PAT. | |
335 | ||
336 | out="" | |
337 | rest=$templ | |
338 | while :; do | |
339 | ||
340 | ## If there are no more markers to substitute, then finish. | |
341 | case "$rest" in *"%{"*"}"*) ;; *) out=$out$rest; break ;; esac | |
342 | ||
343 | ## Split the template into three parts. | |
344 | left=${rest%%\%\{*} right=${rest#*\%\{} | |
345 | var=${right%%\}*} rest=${right#*\}} | |
346 | case "$var" in | |
347 | *-*) default=${var#*-} var=${var%%-*} defaultp=t ;; | |
348 | *) defaultp=nil ;; | |
349 | esac | |
350 | ||
351 | ## Find the variable value. | |
352 | checkident "template variable name" "$var" | |
353 | eval foundp=\${$prefix$var+t} | |
354 | case $foundp,$defaultp in | |
355 | t,*) eval value=\$$prefix$var ;; | |
356 | ,t) value=$default ;; | |
357 | *) | |
358 | echo >&2 "$quis: option \`$var' unset, used in template \`$templ'" | |
359 | exit 1 | |
360 | ;; | |
361 | esac | |
362 | ||
363 | ## Do the substitution. | |
364 | out=$out$left$value | |
365 | done | |
366 | ||
367 | ## Check the final result. | |
368 | check "$what" "$pat" "$out" | |
369 | ||
370 | ## Done. | |
371 | echo "$out" | |
372 | } | |
373 | ||
374 | read_profile () { | |
e9cf7079 | 375 | owner=$1 profile=$2 |
c47f2aba | 376 | ## Read property settings from a profile. The PROFILE name has the form |
e9cf7079 MW |
377 | ## [USER:]LABEL; USER defaults to OWNER. Properties are set using |
378 | ## `setprops' with prefix `kprop_'. | |
c47f2aba MW |
379 | |
380 | reqtmp | |
381 | case "$profile" in | |
382 | :*) | |
383 | label=${profile#:} uservp=nil | |
384 | ;; | |
53263601 | 385 | *) |
e9cf7079 | 386 | user=$kowner label=$profile uservp=t |
c47f2aba MW |
387 | ;; |
388 | *:*) | |
389 | user=${profile%%:*} label=${profile#*:} uservp=t | |
390 | ;; | |
391 | esac | |
392 | checkword "profile label" "$label" | |
393 | ||
394 | ## Fetch the profile settings from the user. | |
395 | reqtmp | |
396 | case $uservp in | |
397 | t) | |
398 | checkword "profile user" "$user" | |
21a21fff | 399 | userv "$user" cryptop-profile "$label" >$tmp/profile </dev/null |
c47f2aba MW |
400 | ;; |
401 | nil) | |
b65e1f93 | 402 | $bindir/extract-profile "$label" $ETC/profile.d/ >$tmp/profile |
53263601 MW |
403 | ;; |
404 | esac | |
405 | ||
c47f2aba MW |
406 | ## Read the file. |
407 | readprops $tmp/profile | |
53263601 MW |
408 | } |
409 | ||
c47f2aba MW |
410 | ###-------------------------------------------------------------------------- |
411 | ### General crypto operations. | |
412 | ||
413 | c_genkey () { | |
414 | profile=$1 kdir=$2 knub=$3 hook=$4; shift 4 | |
415 | ## Generate a key, and associate it with the named PROFILE (which is | |
416 | ## assumed already to have been read!); store the main data in KDIR, and | |
417 | ## the nub separately in the file KNUB; run HOOK after generation, passing | |
418 | ## it the working key directory and nub file. Remaining arguments are | |
419 | ## options to the key type. | |
420 | ||
421 | ## Set options and check them. | |
422 | setprops "option" kopt_ "$@" | |
423 | checkprops "option" kopt_ "$k_genopts" | |
424 | ||
425 | ## Create directory structure and start writing metadata. | |
426 | rm -rf "$kdir.new" | |
427 | mkdir -m755 -p "$kdir.new" | |
428 | case "$knub" in */*) mkdir -m700 -p "${knub%/*}" ;; esac | |
429 | cat >"$kdir.new/meta" <<EOF | |
430 | $profile | |
431 | EOF | |
53263601 | 432 | |
c47f2aba MW |
433 | ## Generate the key. |
434 | umask=$(umask); umask 077; >"$knub.new"; umask $umask | |
435 | k_generate "$kdir.new" "$knub.new" | |
436 | $hook "$kdir.new" "$knub.new" | |
437 | ||
438 | ## Hash the nub. | |
439 | nubid <"$knub.new" >"$kdir.new/nubid" | |
440 | ||
441 | ## Juggle everything into place. Doing this atomically is very difficult, | |
442 | ## and requires more machinery than I can really justify here. If | |
443 | ## something goes wrong halfway, it should always be possible to fix it, | |
444 | ## either by backing out (if $kdir.new still exists) or pressing on | |
445 | ## forwards (if not). | |
446 | rm -rf "$kdir.old" | |
447 | if [ -e "$kdir" ]; then mv "$kdir" "$kdir.old"; fi | |
448 | mv "$kdir.new" "$kdir" | |
449 | mv "$knub.new" "$knub" | |
450 | rm -rf "$kdir.old" | |
53263601 MW |
451 | } |
452 | ||
c47f2aba MW |
453 | c_encrypt () { k_encrypt "$@"; } |
454 | c_decrypt () { | |
455 | if k_decrypt "$@" >$tmp/plain; then cat $tmp/plain | |
456 | else return $? | |
457 | fi | |
458 | } | |
459 | c_sign () { k_sign "$@"; } | |
460 | c_verify () { k_verify "$@"; } | |
461 | ||
462 | ## Stub implementations. | |
463 | notsupp () { op=$1; echo >&2 "$quis: operation \`$op' not supported"; } | |
464 | k_info () { :; } | |
fff6c653 | 465 | k_import () { :; } |
c47f2aba MW |
466 | k_encrypt () { notsupp encrypt; } |
467 | k_decrypt () { notsupp decrypt; } | |
468 | k_sign () { notsupp sign; } | |
469 | k_verify () { notsupp verify; } | |
470 | ||
471 | prepare () { | |
472 | key=$1 op=$2 | |
473 | ## Prepare for a crypto operation OP, using the KEY. This validates the | |
5cff41ea MW |
474 | ## key label, reads the profile, and checks the access-control list. If OP |
475 | ## is `-' then allow the operation unconditionally. | |
c47f2aba MW |
476 | |
477 | ## Find the key properties. | |
478 | parse_keylabel "$key" | |
479 | if [ ! -d $kdir ]; then echo >&2 "$quis: unknown key \`$key'"; exit 1; fi | |
480 | readmeta $kdir | |
e9cf7079 | 481 | read_profile $kowner "$profile" |
c47f2aba MW |
482 | |
483 | ## Check whether we're allowed to do this thing. This is annoyingly | |
484 | ## fiddly. | |
5cff41ea | 485 | case $op in -) return ;; esac |
c47f2aba MW |
486 | eval acl=\${kprop_acl_$op-!owner} |
487 | verdict=forbid | |
488 | while :; do | |
489 | ||
490 | ## Remove leading whitespace. | |
491 | while :; do | |
492 | case "$acl" in | |
493 | [[:space:]]*) acl=${acl#?} ;; | |
494 | *) break ;; | |
495 | esac | |
496 | done | |
497 | ||
498 | ## If there's nothing left, leave. | |
499 | case "$acl" in ?*) ;; *) break ;; esac | |
500 | ||
501 | ## Split off the leading word. | |
502 | case "$acl" in | |
503 | *[[:space:]]*) word=${acl%%[[:space:]]*} acl=${acl#*[[:space:]]} ;; | |
504 | *) word=$acl acl="" ;; | |
505 | esac | |
506 | ||
507 | ## See what sense it has if it matches. | |
508 | case "$word" in | |
509 | -*) sense=forbid rest=${word#-} ;; | |
510 | *) sense=allow rest=$word ;; | |
511 | esac | |
512 | ||
513 | ## See whether the calling user matches. | |
514 | case "$rest" in | |
515 | !owner) pat=$kowner list=$USERV_USER ;; | |
516 | !*) echo >&2 "$quis: unknown ACL token \`$word'" ;; | |
517 | %*) pat=${rest#%} list="$USERV_GROUP $USERV_GID" ;; | |
518 | *) pat=$rest list="$USERV_USER $USERV_UID" ;; | |
519 | esac | |
520 | matchp=nil | |
521 | for i in $list; do case "$i" in $pat) matchp=t; break ;; esac; done | |
522 | case $matchp in t) verdict=$sense; break ;; esac | |
523 | done | |
524 | ||
525 | case $verdict in | |
68023101 | 526 | forbid) echo >&2 "$quis: $op access to key \`$key' forbidden"; exit 1 ;; |
c47f2aba | 527 | esac |
53263601 MW |
528 | } |
529 | ||
c47f2aba MW |
530 | ###-------------------------------------------------------------------------- |
531 | ### Crypto operations for infrastructure purposes. | |
532 | ||
533 | c_sysprofile () { | |
534 | profile=$1 | |
535 | ## Select the profile in FILE for future crypto operations. | |
53263601 | 536 | |
c47f2aba MW |
537 | unset $(set | sed -n '/^kprop_/s/=.*$//p') |
538 | reqtmp | |
539 | getsysprofile "$profile" >$tmp/profile | |
540 | readprops $tmp/profile | |
53263601 MW |
541 | } |
542 | ||
c47f2aba MW |
543 | c_gensyskey () { |
544 | profile=$1 kdir=$2 knub=$3; shift 3 | |
545 | ## Generate a system key using PROFILE; store the data in KDIR and the nub | |
546 | ## in KNUB. Remaining arguments are options. | |
53263601 | 547 | |
c47f2aba MW |
548 | c_sysprofile "$profile" |
549 | c_genkey "$profile" "$kdir" "$knub" : "$@" | |
53263601 MW |
550 | } |
551 | ||
c47f2aba MW |
552 | c_sysprepare () { |
553 | kdir=$1 | |
554 | readmeta "$kdir" | |
555 | c_sysprofile "$profile" | |
556 | } | |
599c8f75 | 557 | |
c47f2aba MW |
558 | c_sysop () { |
559 | op=$1 kdir=$2; shift 1 | |
560 | c_sysprepare "$kdir" | |
561 | c_$op "$@" | |
599c8f75 MW |
562 | } |
563 | ||
c47f2aba MW |
564 | c_sysencrypt () { c_sysop encrypt "$1" /dev/null; } |
565 | c_sysdecrypt () { c_sysop decrypt "$1" "$2"; } | |
566 | c_syssign () { c_sysop sign "$1" "$2"; } | |
567 | c_sysverify () { c_sysop verify "$1" /dev/null; } | |
568 | ||
569 | ###-------------------------------------------------------------------------- | |
570 | ### Recovery operations. | |
571 | ||
2661d8aa MW |
572 | sharethresh () { |
573 | pf=$1 | |
574 | ## Return the sharing threshold from the parameter file PARAM. | |
575 | ||
576 | read param <"$pf" | |
577 | case "$param" in | |
578 | shamir-params:*) ;; | |
579 | *) | |
580 | echo >&2 "$quis: secret sharing parameter file damaged (wrong header)" | |
581 | exit 1 | |
582 | ;; | |
583 | esac | |
584 | t=";${param#*:}" | |
585 | case "$t" in | |
586 | *";t="*) ;; | |
587 | *) | |
588 | echo >&2 "$quis: secret sharing parameter file damaged (missing t)" | |
589 | exit 1 | |
590 | ;; | |
591 | esac | |
592 | t=${t#*;t=} | |
593 | t=${t%%;*} | |
594 | echo "$t" | |
595 | } | |
596 | ||
c47f2aba MW |
597 | stash () { |
598 | recov=$1 label=$2 | |
599 | ## Stash a copy of stdin encrypted under the recovery key RECOV, with a | |
600 | ## given LABEL. | |
601 | checkword "recovery key label" "$recov" | |
602 | checklabel "secret" "$label" | |
603 | ||
604 | rdir=$KEYS/recov/$recov/current | |
605 | if [ ! -d $rdir/store ]; then | |
606 | echo >&2 "$quis: unknown recovery key \`$recov'" | |
607 | exit 1 | |
608 | fi | |
609 | case $label in */*) mkdir -m755 -p $rdir/${label%/*} ;; esac | |
610 | (c_sysencrypt $rdir/store >$rdir/$label.new) | |
611 | mv $rdir/$label.new $rdir/$label.recov | |
612 | } | |
599c8f75 | 613 | |
c47f2aba | 614 | recover () { |
ae0eb898 | 615 | recov=$1 inst=$2 label=$3 |
c47f2aba MW |
616 | ## Recover a stashed secret, protected by RECOV and stored as LABEL, and |
617 | ## write it to stdout. | |
618 | checkword "recovery key label" "$recov" | |
ae0eb898 | 619 | checkword "recovery instance" "$inst" |
c47f2aba MW |
620 | checklabel "secret" "$label" |
621 | ||
ae0eb898 | 622 | rdir=$KEYS/recov/$recov/$inst |
c47f2aba | 623 | if [ ! -f $rdir/$label.recov ]; then |
ae0eb898 | 624 | echo >&2 "$quis: no blob for \`$label' under recovery key \`$recov/$inst'" |
c47f2aba MW |
625 | exit 1 |
626 | fi | |
627 | reqsafe | |
ae0eb898 MW |
628 | tag=$recov.$inst |
629 | nub=$SAFE/keys.reveal/$tag/nub | |
c47f2aba | 630 | if [ ! -f $nub ]; then |
ae0eb898 | 631 | echo >&2 "$quis: current recovery key \`$recov/$inst' not revealed" |
c47f2aba MW |
632 | exit 1; |
633 | fi | |
634 | mktmp | |
635 | c_sysdecrypt $rdir/store $nub <$rdir/$label.recov | |
599c8f75 MW |
636 | } |
637 | ||
53263601 MW |
638 | ###-------------------------------------------------------------------------- |
639 | ### Help text. | |
640 | ||
c47f2aba MW |
641 | defhelp () { |
642 | read umsg | |
865fd3f4 | 643 | usage=$umsg |
c47f2aba MW |
644 | help=$(cat) |
645 | case "$KEYS_HELP" in t) help; exit ;; esac | |
53263601 MW |
646 | } |
647 | ||
53263601 MW |
648 | help () { showhelp; } |
649 | showhelp () { | |
650 | cat <<EOF | |
865fd3f4 | 651 | Usage: $quis${usage:+ $usage} |
53263601 MW |
652 | |
653 | $help | |
654 | EOF | |
655 | } | |
656 | ||
865fd3f4 MW |
657 | usage () { |
658 | : ${cmdargs=$usage} | |
659 | echo "usage: $quis${cmdname:+ $cmdname}${cmdargs:+ $cmdargs}" | |
660 | } | |
661 | usage_err () { usage >&2; exit 1; } | |
c47f2aba MW |
662 | |
663 | ###-------------------------------------------------------------------------- | |
664 | ### Subcommand handling. | |
665 | ||
666 | version () { | |
667 | echo "$PACKAGE version $VERSION" | |
668 | } | |
669 | ||
865fd3f4 MW |
670 | unset cmdargs |
671 | unset cmdname | |
672 | cmds="" | |
673 | defcmd () { | |
674 | cmd=$1; shift; args=$* | |
675 | help=$(cat) | |
676 | eval help_$cmd=\$help | |
677 | cmds="${cmds:+$cmds | |
678 | }$cmd $args" | |
679 | } | |
680 | ||
681 | defcmd help "[COMMAND ...]" <<EOF | |
682 | Show help about the COMMANDs, or about $quis if none are named. | |
683 | EOF | |
c47f2aba MW |
684 | cmd_help () { |
685 | rc=0 | |
686 | version | |
687 | case $# in | |
688 | 0) | |
689 | cat <<EOF | |
690 | ||
865fd3f4 | 691 | Usage: $quis${usage:+ $usage} |
c47f2aba MW |
692 | |
693 | Options: | |
694 | -h Show this help text. | |
695 | -v Show the program version number. | |
696 | ||
865fd3f4 | 697 | Commands provided: |
c47f2aba | 698 | EOF |
865fd3f4 MW |
699 | while read cmd args; do echo " $cmd${args:+ $args}"; done <<EOF |
700 | $cmds | |
701 | EOF | |
702 | case ${prefix+t} in | |
703 | t) | |
704 | cd $KEYSLIB | |
705 | for i in $prefix.*; do | |
706 | if [ ! -x "$i" ]; then continue; fi | |
707 | sed -n "/<<HELP/{n;s/^/ ${i#$prefix.} /;p;q;}" "$i" | |
708 | done | |
709 | ;; | |
710 | esac | |
c47f2aba MW |
711 | ;; |
712 | *) | |
865fd3f4 | 713 | for cmd in "$@"; do |
c47f2aba | 714 | echo |
865fd3f4 MW |
715 | foundp=nil |
716 | while read cmdname cmdargs; do | |
717 | case $cmdname in "$cmd") foundp=t; break ;; esac | |
718 | done <<EOF | |
719 | $cmds | |
720 | EOF | |
721 | case $foundp in | |
722 | t) | |
723 | eval help=\$help_$cmdname; echo "$help" | |
724 | ;; | |
725 | nil) | |
726 | if [ ! -x "$KEYSLIB/$prefix.$cmd" ]; then | |
727 | echo >&2 "$quis: unrecognized command \`$cmd'" | |
728 | rc=1 | |
729 | continue | |
730 | elif ! KEYS_HELP=t "$KEYSLIB/$prefix.$cmd"; then | |
731 | rc=1 | |
732 | fi | |
733 | ;; | |
734 | esac | |
c47f2aba MW |
735 | done |
736 | ;; | |
737 | esac | |
738 | return $rc | |
739 | } | |
740 | ||
741 | dispatch () { | |
865fd3f4 | 742 | case $# in 0) usage_err ;; esac |
c47f2aba | 743 | cmd=$1; shift |
865fd3f4 MW |
744 | foundp=nil |
745 | while read cmdname cmdargs; do | |
746 | case $cmdname in "$cmd") foundp=t; break ;; esac | |
747 | done <<EOF | |
748 | $cmds | |
749 | EOF | |
750 | case $foundp in | |
751 | t) | |
752 | OPTIND=1 | |
753 | cmd_$cmdname "$@" | |
754 | ;; | |
755 | nil) | |
756 | if [ ! -x "$KEYSLIB/$prefix.$cmd" ]; then | |
757 | echo >&2 "$quis: unrecognized command \`$cmd'" | |
758 | exit 1 | |
759 | fi | |
760 | unset KEYS_HELP | |
761 | exec "$KEYSLIB/$prefix.$cmd" "$@" | |
762 | ;; | |
763 | esac | |
c47f2aba MW |
764 | } |
765 | ||
53263601 | 766 | ###----- That's all, folks -------------------------------------------------- |