freedombone-utils-keys 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Encryption key related functions
  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. function reconstruct_key {
  30. if [ ! -d /home/$MY_USERNAME/.gnupg_fragments ]; then
  31. return
  32. fi
  33. cd /home/$MY_USERNAME/.gnupg_fragments
  34. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  35. if (( no_of_shares < 4 )); then
  36. dialog --title $"Recover Encryption Keys" --msgbox $'Not enough fragments to reconstruct the key' 6 70
  37. exit 7348
  38. fi
  39. apt-get -yq install libgfshare-bin gnupg
  40. gfcombine /home/$MY_USERNAME/.gnupg_fragments/keyshare*
  41. if [ ! "$?" = "0" ]; then
  42. dialog --title $"Recover Encryption Keys" --msgbox $'Unable to reconstruct the key' 6 70
  43. exit 7348
  44. fi
  45. KEYS_FILE=/home/$MY_USERNAME/.gnupg_fragments/keyshare.asc
  46. if [ ! -f $KEYS_FILE ]; then
  47. dialog --title $"Recover Encryption Keys" --msgbox $'Unable to reconstruct the key' 6 70
  48. fi
  49. su -c "gpg --allow-secret-key-import --import $KEYS_FILE" - $MY_USERNAME
  50. if [ ! "$?" = "0" ]; then
  51. echo $'Unable to import gpg key'
  52. shred -zu $KEYS_FILE
  53. rm -rf /home/$MY_USERNAME/.tempgnupg
  54. exit 9654
  55. fi
  56. shred -zu $KEYS_FILE
  57. dialog --title $"Recover Encryption Keys" --msgbox $'Key has been reconstructed' 6 70
  58. }
  59. function interactive_gpg_from_usb {
  60. dialog --title $"Recover Encryption Keys" \
  61. --msgbox $'Plug in a USB keydrive containing a copy of your full key or key fragment' 6 70
  62. HOME_DIR=/home/$MY_USERNAME
  63. GPG_LOADING="yes"
  64. SSH_IMPORTED="no"
  65. GPG_CTR=0
  66. while [[ $GPG_LOADING == "yes" ]]
  67. do
  68. detect_usb_drive
  69. if [ ! -b $USB_DRIVE ]; then
  70. if (( GPG_CTR > 0 )); then
  71. reconstruct_key
  72. return 0
  73. fi
  74. dialog --title $"Recover Encryption Keys" --msgbox $'No USB drive found' 6 30
  75. exit 739836
  76. fi
  77. backup_mount_drive ${USB_DRIVE} ${MY_USERNAME}
  78. if [ ! -d $USB_MOUNT ]; then
  79. if (( GPG_CTR > 0 )); then
  80. backup_unmount_drive ${USB_DRIVE}
  81. reconstruct_key
  82. return 0
  83. fi
  84. dialog --title $"Recover Encryption Keys" \
  85. --msgbox $"There was a problem mounting the USB drive $USB_DRIVE to $USB_MOUNT" 6 70
  86. backup_unmount_drive ${USB_DRIVE}
  87. exit 74393
  88. fi
  89. if [ ! -d $USB_MOUNT/.gnupg ]; then
  90. if [ ! -d $USB_MOUNT/.gnupg_fragments ]; then
  91. if (( GPG_CTR > 0 )); then
  92. backup_unmount_drive ${USB_DRIVE}
  93. reconstruct_key
  94. return 0
  95. fi
  96. dialog --title $"Recover Encryption Keys" \
  97. --msgbox $"The directory $USB_MOUNT/.gnupg or $USB_MOUNT/.gnupg_fragments was not found" 6 70
  98. backup_unmount_drive ${USB_DRIVE}
  99. exit 723814
  100. fi
  101. fi
  102. if [ -d $USB_MOUNT/letsencrypt ]; then
  103. if [ ! -d /etc/letsencrypt ]; then
  104. mkdir /etc/letsencrypt
  105. fi
  106. echo $'Recovering LetsEncrypt keys'
  107. cp -r $USB_MOUNT/letsencrypt/* /etc/letsencrypt
  108. fi
  109. if [ -d $USB_MOUNT/.gnupg ]; then
  110. if [ ! -d $HOME_DIR/.gnupg ]; then
  111. mkdir $HOME_DIR/.gnupg
  112. fi
  113. echo $'Recovering GPG keys'
  114. cp -r $USB_MOUNT/.gnupg/* $HOME_DIR/.gnupg
  115. GPG_LOADING="no"
  116. dialog --title $"Recover Encryption Keys" \
  117. --msgbox $"GPG Keyring loaded to $HOME_DIR" 6 70
  118. else
  119. if [ ! -d $HOME_DIR/.gnupg_fragments ]; then
  120. mkdir $HOME_DIR/.gnupg_fragments
  121. fi
  122. cp -r $USB_MOUNT/.gnupg_fragments/* $HOME_DIR/.gnupg_fragments
  123. fi
  124. if [[ $SSH_IMPORTED == "no" ]]; then
  125. if [ -d $USB_MOUNT/.ssh ]; then
  126. if [ ! -d $HOME_DIR/.ssh ]; then
  127. mkdir $HOME_DIR/.ssh
  128. fi
  129. cp $USB_MOUNT/.ssh/* $HOME_DIR/.ssh
  130. dialog --title $"Recover Encryption Keys" \
  131. --msgbox $"ssh keys imported" 6 70
  132. SSH_IMPORTED="yes"
  133. fi
  134. fi
  135. if [ -d $USB_MOUNT ]; then
  136. backup_unmount_drive ${USB_DRIVE}
  137. fi
  138. if [[ $GPG_LOADING == "yes" ]]; then
  139. dialog --title $"Recover Encryption Keys" \
  140. --msgbox $"Now remove the USB drive. Insert the next drive containing a key fragment, or select Ok to finish" 6 70
  141. fi
  142. GPG_CTR=$((GPG_CTR + 1))
  143. done
  144. }
  145. function interactive_gpg_from_remote {
  146. REMOTE_SERVERS_LIST=/home/$MY_USERNAME/keyshareservers.txt
  147. # get a list of remote servers
  148. ${PROJECT_NAME}-remote -u $MY_USERNAME -l $REMOTE_SERVERS_LIST -t "Remote server"
  149. if [ ! -f $REMOTE_SERVERS_LIST ]; then
  150. dialog --title $"Encryption Keys Recovery" --msgbox $'Error obtaining server list' 6 70
  151. return 1
  152. fi
  153. # check the number of entries in the file
  154. no_of_servers=$(cat $REMOTE_SERVERS_LIST | wc -l)
  155. if (( no_of_servers < 3 )); then
  156. dialog --title $"Encryption Keys Recovery" \
  157. --msgbox $'There must be at least three servers to recover the key' 6 70
  158. return 2
  159. fi
  160. # try to recover the key from the servers
  161. apt-get -yq install libgfshare-bin gnupg
  162. ${PROJECT_NAME}-recoverkey -u $MY_USERNAME -l $REMOTE_SERVERS_LIST
  163. if [ ! "$?" = "0" ]; then
  164. dialog --title $"Encryption Keys Recovery" --msgbox $'Your key could not be recovered' 6 70
  165. return 3
  166. fi
  167. dialog --title $"Encryption Keys Recovery" --msgbox $'Your key has been recovered' 6 70
  168. return 0
  169. }
  170. function interactive_gpg {
  171. GPG_CONFIGURED="no"
  172. while [[ $GPG_CONFIGURED != "yes" ]]
  173. do
  174. GPG_CONFIGURED="yes"
  175. data=$(tempfile 2>/dev/null)
  176. trap "rm -f $data" 0 1 2 5 15
  177. dialog --backtitle $"Freedombone Configuration" \
  178. --radiolist $"GPG/PGP keys for your system:" 13 70 3 \
  179. 1 $"Generate new keys (new user)" on \
  180. 2 $"Import keys from USB drive/s" off \
  181. 3 $"Retrieve keys from friends servers" off 2> $data
  182. sel=$?
  183. case $sel in
  184. 1) exit 1;;
  185. 255) exit 2;;
  186. esac
  187. case $(cat $data) in
  188. 1) if [ -d /home/${MY_USERNAME}/.gnupg ]; then
  189. rm -rf /home/${MY_USERNAME}/.gnupg
  190. fi
  191. break;;
  192. 2) interactive_gpg_from_usb
  193. break;;
  194. 3) interactive_gpg_from_remote
  195. if [ ! "$?" = "0" ]; then
  196. GPG_CONFIGURED="no"
  197. fi;;
  198. esac
  199. done
  200. }
  201. function interactive_key_recovery {
  202. data=$(tempfile 2>/dev/null)
  203. trap "rm -f $data" 0 1 2 5 15
  204. dialog --title $"Encryption Keys Recovery" \
  205. --backtitle $"Freedombone Configuration" \
  206. --defaultno \
  207. --yesno $"Do you wish to recover your previous encryption keys from a USB master keydrive?" 7 60
  208. sel=$?
  209. case $sel in
  210. 1) return;;
  211. 255) return;;
  212. esac
  213. apt-get -yq install cryptsetup
  214. ${PROJECT_NAME}-recoverkey -u $MY_USERNAME
  215. if [ -d /home/$MY_USERNAME/.gnupg ]; then
  216. cp -rf /home/$MY_USERNAME/.gnupg /root
  217. fi
  218. }
  219. function set_password_for_all_users {
  220. app_name="$1"
  221. change_password="$2"
  222. for d in /home/*/ ; do
  223. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  224. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  225. ${PROJECT_NAME}-pass -u "${USERNAME}" -a "${app_name}" -p "${change_password}"
  226. fi
  227. done
  228. }
  229. # NOTE: deliberately there is no "exit 0"