|  | @@ -0,0 +1,119 @@
 | 
	
		
			
			|  | 1 | +#!/bin/bash
 | 
	
		
			
			|  | 2 | +#
 | 
	
		
			
			|  | 3 | +# .---.                  .              .
 | 
	
		
			
			|  | 4 | +# |                      |              |
 | 
	
		
			
			|  | 5 | +# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
 | 
	
		
			
			|  | 6 | +# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
 | 
	
		
			
			|  | 7 | +# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
 | 
	
		
			
			|  | 8 | +#
 | 
	
		
			
			|  | 9 | +#                    Freedom in the Cloud
 | 
	
		
			
			|  | 10 | +#
 | 
	
		
			
			|  | 11 | +# A script which recovers a user's gpg key from a number of fragments
 | 
	
		
			
			|  | 12 | +
 | 
	
		
			
			|  | 13 | +# License
 | 
	
		
			
			|  | 14 | +# =======
 | 
	
		
			
			|  | 15 | +#
 | 
	
		
			
			|  | 16 | +# Copyright (C) 2015 Bob Mottram <bob@robotics.uk.to>
 | 
	
		
			
			|  | 17 | +#
 | 
	
		
			
			|  | 18 | +# This program is free software: you can redistribute it and/or modify
 | 
	
		
			
			|  | 19 | +# it under the terms of the GNU General Public License as published by
 | 
	
		
			
			|  | 20 | +# the Free Software Foundation, either version 3 of the License, or
 | 
	
		
			
			|  | 21 | +# (at your option) any later version.
 | 
	
		
			
			|  | 22 | +#
 | 
	
		
			
			|  | 23 | +# This program is distributed in the hope that it will be useful,
 | 
	
		
			
			|  | 24 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
	
		
			
			|  | 25 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | 
	
		
			
			|  | 26 | +# GNU General Public License for more details.
 | 
	
		
			
			|  | 27 | +#
 | 
	
		
			
			|  | 28 | +# You should have received a copy of the GNU General Public License
 | 
	
		
			
			|  | 29 | +# along with this program. If not, see <http://www.gnu.org/licenses/>.
 | 
	
		
			
			|  | 30 | +
 | 
	
		
			
			|  | 31 | +function show_help {
 | 
	
		
			
			|  | 32 | +    echo ''
 | 
	
		
			
			|  | 33 | +    echo 'freedombone-recoverkey -u [username]'
 | 
	
		
			
			|  | 34 | +    echo ''
 | 
	
		
			
			|  | 35 | +    exit 0
 | 
	
		
			
			|  | 36 | +}
 | 
	
		
			
			|  | 37 | +
 | 
	
		
			
			|  | 38 | +while [[ $# > 1 ]]
 | 
	
		
			
			|  | 39 | +do
 | 
	
		
			
			|  | 40 | +key="$1"
 | 
	
		
			
			|  | 41 | +
 | 
	
		
			
			|  | 42 | +case $key in
 | 
	
		
			
			|  | 43 | +    -h|--help)
 | 
	
		
			
			|  | 44 | +    show_help
 | 
	
		
			
			|  | 45 | +    ;;
 | 
	
		
			
			|  | 46 | +    -u|--user)
 | 
	
		
			
			|  | 47 | +    shift
 | 
	
		
			
			|  | 48 | +    MY_USERNAME="$1"
 | 
	
		
			
			|  | 49 | +    ;;
 | 
	
		
			
			|  | 50 | +    *)
 | 
	
		
			
			|  | 51 | +    # unknown option
 | 
	
		
			
			|  | 52 | +    ;;
 | 
	
		
			
			|  | 53 | +esac
 | 
	
		
			
			|  | 54 | +shift
 | 
	
		
			
			|  | 55 | +done
 | 
	
		
			
			|  | 56 | +
 | 
	
		
			
			|  | 57 | +if [ ! $MY_USERNAME ]; then
 | 
	
		
			
			|  | 58 | +    show_help
 | 
	
		
			
			|  | 59 | +fi
 | 
	
		
			
			|  | 60 | +if [ ! -d /home/$MY_USERNAME ]; then
 | 
	
		
			
			|  | 61 | +    echo "User $MY_USERNAME does not exist on the system"
 | 
	
		
			
			|  | 62 | +    exit 7270
 | 
	
		
			
			|  | 63 | +fi
 | 
	
		
			
			|  | 64 | +
 | 
	
		
			
			|  | 65 | +if [ ! $MY_USERNAME ]; then
 | 
	
		
			
			|  | 66 | +    echo 'No username given'
 | 
	
		
			
			|  | 67 | +    exit 3578
 | 
	
		
			
			|  | 68 | +fi
 | 
	
		
			
			|  | 69 | +if [ ! -d /home/$MY_USERNAME ]; then
 | 
	
		
			
			|  | 70 | +    echo "User $MY_USERNAME does not exist on the system"
 | 
	
		
			
			|  | 71 | +    exit 7270
 | 
	
		
			
			|  | 72 | +fi
 | 
	
		
			
			|  | 73 | +FRAGMENTS_DIR=/home/$MY_USERNAME/.gnupg_fragments
 | 
	
		
			
			|  | 74 | +if [ ! -d $FRAGMENTS_DIR ]; then
 | 
	
		
			
			|  | 75 | +    echo 'No fragments have been recovered, so the key cannot be recovered'
 | 
	
		
			
			|  | 76 | +    exit 7483
 | 
	
		
			
			|  | 77 | +fi
 | 
	
		
			
			|  | 78 | +
 | 
	
		
			
			|  | 79 | +# join the fragments
 | 
	
		
			
			|  | 80 | +if [ ! -d /home/$MY_USERNAME/.tempgnupg ]; then
 | 
	
		
			
			|  | 81 | +    mkdir /home/$MY_USERNAME/.tempgnupg
 | 
	
		
			
			|  | 82 | +fi
 | 
	
		
			
			|  | 83 | +KEYS_FILE=/home/$MY_USERNAME/.tempgnupg/tempfile.asc
 | 
	
		
			
			|  | 84 | +cat $FRAGMENTS_DIR/data* > $KEYS_FILE.gpg
 | 
	
		
			
			|  | 85 | +if [ ! "$?" = "0" ]; then
 | 
	
		
			
			|  | 86 | +    echo 'Unable to find key fragments'
 | 
	
		
			
			|  | 87 | +    exit 8727
 | 
	
		
			
			|  | 88 | +fi
 | 
	
		
			
			|  | 89 | +
 | 
	
		
			
			|  | 90 | +# decrypt the file
 | 
	
		
			
			|  | 91 | +cd /home/$MY_USERNAME/.tempgnupg
 | 
	
		
			
			|  | 92 | +gpg -d $KEYS_FILE.gpg -o $KEYS_FILE
 | 
	
		
			
			|  | 93 | +if [ ! "$?" = "0" ]; then
 | 
	
		
			
			|  | 94 | +    echo 'Unable to decrypt data. This may mean that not enough fragments are available'
 | 
	
		
			
			|  | 95 | +    exit 6283
 | 
	
		
			
			|  | 96 | +fi
 | 
	
		
			
			|  | 97 | +shred -zu $KEYS_FILE.gpg
 | 
	
		
			
			|  | 98 | +if [ ! -f $KEYS_FILE ]; then
 | 
	
		
			
			|  | 99 | +    echo 'Unable to find decrypted key file. This may mean that not enough fragments are available'
 | 
	
		
			
			|  | 100 | +    exit 8358
 | 
	
		
			
			|  | 101 | +fi
 | 
	
		
			
			|  | 102 | +echo 'Key fragments decrypted'
 | 
	
		
			
			|  | 103 | +
 | 
	
		
			
			|  | 104 | +# import the gpg key
 | 
	
		
			
			|  | 105 | +su -c "gpg --allow-secret-key-import --import $KEYS_FILE" - $MY_USERNAME
 | 
	
		
			
			|  | 106 | +if [ ! "$?" = "0" ]; then
 | 
	
		
			
			|  | 107 | +    echo 'Unable to import gpg key'
 | 
	
		
			
			|  | 108 | +    shred -zu $KEYS_FILE
 | 
	
		
			
			|  | 109 | +    rm -rf /home/$MY_USERNAME/.tempgnupg
 | 
	
		
			
			|  | 110 | +    exit 3682
 | 
	
		
			
			|  | 111 | +fi
 | 
	
		
			
			|  | 112 | +shred -zu $KEYS_FILE
 | 
	
		
			
			|  | 113 | +chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
 | 
	
		
			
			|  | 114 | +chmod -R 600 /home/$MY_USERNAME/.gnupg
 | 
	
		
			
			|  | 115 | +rm -rf /home/$MY_USERNAME/.tempgnupg
 | 
	
		
			
			|  | 116 | +
 | 
	
		
			
			|  | 117 | +echo 'GPG key was recovered'
 | 
	
		
			
			|  | 118 | +
 | 
	
		
			
			|  | 119 | +exit 0
 |