freedombone-recoverkey 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU 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 General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. FRIENDS_SERVERS_LIST=
  30. MY_USERNAME=
  31. function show_help {
  32. echo ''
  33. echo 'freedombone-recoverkey -u [username] -l [friends servers list filename]'
  34. echo ''
  35. exit 0
  36. }
  37. while [[ $# > 1 ]]
  38. do
  39. key="$1"
  40. case $key in
  41. -h|--help)
  42. show_help
  43. ;;
  44. -u|--user)
  45. shift
  46. MY_USERNAME="$1"
  47. ;;
  48. # backup list filename
  49. # typically /home/$USER/backup.list
  50. -l|--list)
  51. shift
  52. FRIENDS_SERVERS_LIST="$1"
  53. ;;
  54. *)
  55. # unknown option
  56. ;;
  57. esac
  58. shift
  59. done
  60. if [ ! $MY_USERNAME ]; then
  61. show_help
  62. fi
  63. if [ ! -d /home/$MY_USERNAME ]; then
  64. echo "User $MY_USERNAME does not exist on the system"
  65. exit 7270
  66. fi
  67. if [ ! $MY_USERNAME ]; then
  68. echo 'No username given'
  69. exit 3578
  70. fi
  71. if [ ! -d /home/$MY_USERNAME ]; then
  72. echo "User $MY_USERNAME does not exist on the system"
  73. exit 7270
  74. fi
  75. FRAGMENTS_DIR=/home/$MY_USERNAME/.gnupg_fragments
  76. # find the remote backup list
  77. if [ ! $FRIENDS_SERVERS_LIST ]; then
  78. if [ -f /home/$MY_USERNAME/backup.list ]; then
  79. FRIENDS_SERVERS_LIST=/home/$MY_USERNAME/backup.list
  80. fi
  81. fi
  82. # obtain shares/fragments from remote locations
  83. if [ $FRIENDS_SERVERS_LIST ]; then
  84. # For each remote server
  85. while read remote_server
  86. do
  87. # Get the server and its password
  88. # Format is:
  89. # username@domain:/home/username <port number> <ssh password>
  90. REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
  91. if [ $REMOTE_SERVER ]; then
  92. REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
  93. REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
  94. # create a directory if it doesn't exist
  95. if [ ! -d /home/$MY_USERNAME/.gnupg_fragments ]; then
  96. mkdir -p /home/$MY_USERNAME/.gnupg_fragments
  97. fi
  98. echo -n "Starting key retrieval from $REMOTE_SERVER..."
  99. /usr/bin/sshpass -p $REMOTE_PASSWORD \
  100. scp -r -P $REMOTE_SSH_PORT $REMOTE_SERVER/.gnupg_fragments/* /home/$MY_USERNAME/.gnupg_fragments
  101. if [ ! "$?" = "0" ]; then
  102. echo 'FAILED'
  103. else
  104. echo 'Ok'
  105. fi
  106. fi
  107. done < $FRIENDS_SERVERS_LIST
  108. fi
  109. # was a directory created?
  110. if [ ! -d $FRAGMENTS_DIR ]; then
  111. echo 'No fragments have been recovered, so the key cannot be recovered'
  112. exit 7483
  113. fi
  114. # was anything downloaded?
  115. cd $FRAGMENTS_DIR
  116. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  117. if (( no_of_shares == 0 )); then
  118. echo 'No key fragments were retrieved'
  119. exit 76882
  120. fi
  121. # set permissions on the fragments
  122. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg_fragments
  123. # decrypt the file
  124. KEYS_FILE=$FRAGMENTS_DIR/keyshare.asc
  125. cd $FRAGMENTS_DIR
  126. gfcombine $KEYS_FILE.*
  127. if [ ! -f $KEYS_FILE ]; then
  128. echo 'Unable to decrypt key. This may mean that not enough fragments are available'
  129. exit 6283
  130. fi
  131. echo 'Key fragments recombined'
  132. # import the gpg key
  133. su -c "gpg --allow-secret-key-import --import $KEYS_FILE" - $MY_USERNAME
  134. if [ ! "$?" = "0" ]; then
  135. echo 'Unable to import gpg key'
  136. shred -zu $KEYS_FILE
  137. exit 3682
  138. fi
  139. shred -zu $KEYS_FILE
  140. chown -R $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.gnupg
  141. chmod -R 600 /home/$MY_USERNAME/.gnupg
  142. echo 'GPG key was recovered'
  143. exit 0