| 1 | #! /bin/sh |
| 2 | ### |
| 3 | ### Delete a keeper set |
| 4 | ### |
| 5 | ### (c) 2012 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 | set -e |
| 27 | case "${KEYSLIB+t}" in t) ;; *) echo >&2 "$0: KEYSLIB unset"; exit 1 ;; esac |
| 28 | . "$KEYSLIB"/keyfunc.sh |
| 29 | |
| 30 | defhelp <<HELP |
| 31 | KEEPER |
| 32 | Delete the keeper set named KEEPER. |
| 33 | HELP |
| 34 | |
| 35 | ## Parse the command line. |
| 36 | case $# in 1) ;; *) usage_err ;; esac |
| 37 | keeper=$1 |
| 38 | checkword "keeper set label" "$keeper" |
| 39 | |
| 40 | ## Check that the set actually exists. |
| 41 | cd $KEYS/keeper |
| 42 | if [ ! -d $keeper ]; then |
| 43 | echo >&2 "$quis: unknown keeper set \`$keeper'" |
| 44 | exit 1 |
| 45 | fi |
| 46 | |
| 47 | ## Make sure that there aren't recovery keys which would be orphaned by |
| 48 | ## deleting this keeper set. Also, build a data structure of recovery keys |
| 49 | ## and their instances: `$recov' is a space-separated list of recovery key |
| 50 | ## labels, and for each such label R, `$ri_R' is a space-separated list of |
| 51 | ## its instances. |
| 52 | unset deps; recov=" " |
| 53 | if [ -d $KEYS/recov ]; then |
| 54 | cd $KEYS/recov |
| 55 | |
| 56 | ## Work through the available recovery keys. |
| 57 | for r in *; do |
| 58 | if ! expr >/dev/null "Q$r" : "Q$R_WORD"; then continue; fi |
| 59 | if [ ! -l $r/current ]; then continue; fi |
| 60 | |
| 61 | ## Add the key to our list. |
| 62 | recov="$recov$r " |
| 63 | |
| 64 | ## Now work through the instances. |
| 65 | ii="" |
| 66 | for ri in $r/*; do |
| 67 | i=${ri#*/} |
| 68 | case "$i" in *[!0-9]*) continue ;; esac |
| 69 | |
| 70 | ## Add the instance to our list. |
| 71 | ii="$ii $i" |
| 72 | |
| 73 | ## For each recovery key, make sure that: either it doesn't depend on |
| 74 | ## this keeper set, or it also depends on at least one other set. If |
| 75 | ## not, add it to the `deps' list. |
| 76 | this=nil others=nil |
| 77 | for kp in $ri/*.param; do |
| 78 | k=${kp##*/}; k=${k%.param} |
| 79 | case $k in $keeper) this=t ;; *) others=t ;; esac |
| 80 | done |
| 81 | case $this,$others in t,nil) deps="$deps $ri" ;; esac |
| 82 | done |
| 83 | |
| 84 | ## Record the list of instances. |
| 85 | eval "ri_$r=\$ii" |
| 86 | done |
| 87 | fi |
| 88 | |
| 89 | ## If we found any hard dependencies, report a failure. |
| 90 | case "${deps+t}" in |
| 91 | t) |
| 92 | echo >&2 "$quis: deleting keeper \`$keeper' would orphan recovery keys:" |
| 93 | for d in $deps; do echo 2>&1 " $d"; done |
| 94 | exit 1 |
| 95 | ;; |
| 96 | esac |
| 97 | |
| 98 | ## Disentangle the dependent recovery keys from this keeper set. |
| 99 | for r in $recov; do |
| 100 | |
| 101 | ## Remove the keeper data from the key's instances. |
| 102 | eval "ii=\$ri_$r" |
| 103 | for i in $ii; do rm -f $r/$i/$keeper.*; done |
| 104 | |
| 105 | ## Work through the current keepers, and remove our keeper's name from the |
| 106 | ## list. |
| 107 | changep=nil |
| 108 | while read k rest; do |
| 109 | case $k in $keeper) changep=t ;; *) echo "$k $rest" ;; esac |
| 110 | done <$r/keepers >$r/keepers.new |
| 111 | case $changep in |
| 112 | t) mv $r/keepers.new $r/keepers ;; |
| 113 | nil) rm $r/keepers.new ;; |
| 114 | esac |
| 115 | done |
| 116 | |
| 117 | ## Finally, actually delete the keeper keys. |
| 118 | cd $KEYS/keeper |
| 119 | rm -r $keeper |
| 120 | |
| 121 | ###----- That's all, folks -------------------------------------------------- |