freedombone-recoverkey 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. export TEXTDOMAIN=${PROJECT_NAME}-recoverkey
  31. export TEXTDOMAINDIR="/usr/share/locale"
  32. source $PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars
  33. # include utils which allow function_check, go and drive mount
  34. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  35. for f in $UTILS_FILES
  36. do
  37. source $f
  38. done
  39. read_config_param USB_DRIVE
  40. FRIENDS_SERVERS_LIST=
  41. MY_USERNAME=
  42. if [ $USB_DRIVE ]; then
  43. GPG_USB_DRIVE=$USB_DRIVE
  44. else
  45. GPG_USB_DRIVE='/dev/sdb1'
  46. fi
  47. function show_help {
  48. echo ''
  49. echo $"${PROJECT_NAME}-recoverkey -u [username] -d [drive]"
  50. echo $' -l [friends servers list filename]'
  51. echo ''
  52. exit 0
  53. }
  54. while [[ $# > 1 ]]
  55. do
  56. key="$1"
  57. case $key in
  58. -h|--help)
  59. show_help
  60. ;;
  61. -u|--user)
  62. shift
  63. MY_USERNAME="$1"
  64. ;;
  65. # backup list filename
  66. # typically /home/$USER/backup.list
  67. -l|--list)
  68. shift
  69. FRIENDS_SERVERS_LIST="$1"
  70. ;;
  71. -d|--drive)
  72. shift
  73. GPG_USB_DRIVE=/dev/${1}1
  74. ;;
  75. *)
  76. # unknown option
  77. ;;
  78. esac
  79. shift
  80. done
  81. if [ ! $MY_USERNAME ]; then
  82. show_help
  83. fi
  84. if [ ! -d /home/$MY_USERNAME ]; then
  85. echo $"User $MY_USERNAME does not exist on the system"
  86. exit 7270
  87. fi
  88. if [ ! $MY_USERNAME ]; then
  89. echo $'No username given'
  90. exit 3578
  91. fi
  92. if [ ! -d /home/$MY_USERNAME ]; then
  93. echo $"User $MY_USERNAME does not exist on the system"
  94. exit 7270
  95. fi
  96. FRAGMENTS_DIR=/home/$MY_USERNAME/.gnupg_fragments
  97. # if no remote backup list was given then assume recover from USB
  98. if [ ! $FRIENDS_SERVERS_LIST ]; then
  99. interactive_gpg_from_usb
  100. exit 0
  101. fi
  102. # obtain shares/fragments from remote locations
  103. if [ $FRIENDS_SERVERS_LIST ]; then
  104. # For each remote server
  105. while read remote_server
  106. do
  107. # Get the server and its password
  108. # Format is:
  109. # username@domain:/home/username <port number> <ssh password>
  110. REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
  111. if [ $REMOTE_SERVER ]; then
  112. REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
  113. REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
  114. # create a directory if it doesn't exist
  115. if [ ! -d /home/$MY_USERNAME/.gnupg_fragments ]; then
  116. mkdir -p /home/$MY_USERNAME/.gnupg_fragments
  117. fi
  118. echo -n $"Starting key retrieval from $REMOTE_SERVER..."
  119. /usr/bin/sshpass -p $REMOTE_PASSWORD \
  120. scp -r -P $REMOTE_SSH_PORT $REMOTE_SERVER/.gnupg_fragments/* /home/$MY_USERNAME/.gnupg_fragments
  121. if [ ! "$?" = "0" ]; then
  122. echo $'FAILED'
  123. else
  124. echo $'Ok'
  125. fi
  126. fi
  127. done < $FRIENDS_SERVERS_LIST
  128. fi
  129. # was a directory created?
  130. if [ ! -d $FRAGMENTS_DIR ]; then
  131. echo $'No fragments have been recovered, so the key cannot be recovered'
  132. exit 7483
  133. fi
  134. # was anything downloaded?
  135. cd $FRAGMENTS_DIR
  136. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  137. if (( no_of_shares == 0 )); then
  138. echo $'No key fragments were retrieved'
  139. exit 76882
  140. fi
  141. # set permissions on the fragments
  142. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg_fragments
  143. # decrypt the file
  144. KEYS_FILE=$FRAGMENTS_DIR/keyshare.asc
  145. cd $FRAGMENTS_DIR
  146. gfcombine $KEYS_FILE.*
  147. if [ ! -f $KEYS_FILE ]; then
  148. echo $'Unable to decrypt key. This may mean that not enough fragments are available'
  149. exit 6283
  150. fi
  151. echo $'Key fragments recombined'
  152. # import the gpg key
  153. su -c "gpg --allow-secret-key-import --import $KEYS_FILE" - $MY_USERNAME
  154. if [ ! "$?" = "0" ]; then
  155. echo $'Unable to import gpg key'
  156. shred -zu $KEYS_FILE
  157. exit 3682
  158. fi
  159. shred -zu $KEYS_FILE
  160. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
  161. chmod -R 600 /home/$MY_USERNAME/.gnupg
  162. echo $'GPG key was recovered'
  163. exit 0