freedombone-utils-keys 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. gpg --homedir=/home/$MY_USERNAME/.gnupg --allow-secret-key-import --import $KEYS_FILE
  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. addgroup ssl-cert
  109. chown -R root:ssl-cert /etc/letsencrypt
  110. fi
  111. if [ -f $USB_MOUNT/.mastergpgkey ]; then
  112. # Recovering keys from file rather than just copying the gnupg
  113. # directory may help to avoid problems during upgrades/reinstalls
  114. if [ ! -f $USB_MOUNT/.backupgpgkey ]; then
  115. echo $'No backup key file found on USB drive'
  116. exit 725729
  117. fi
  118. gpg --homedir=$HOME_DIR/.gnupg --allow-secret-key-import --import $USB_MOUNT/.mastergpgkey
  119. echo "$BACKUP_DUMMY_PASSWORD" | gpg --batch --passphrase-fd 0 --homedir=$HOME_DIR/.gnupg --allow-secret-key-import --import $USB_MOUNT/.backupgpgkey
  120. if [ -d $HOME_DIR/.gnupg ]; then
  121. chmod 700 $HOME_DIR/.gnupg
  122. chmod -R 600 $HOME_DIR/.gnupg/*
  123. chown -R $MY_USERNAME:$MY_USERNAME $HOME_DIR/.gnupg
  124. fi
  125. GPG_LOADING="no"
  126. dialog --title $"Recover Encryption Keys" \
  127. --msgbox $"GPG Keyring loaded to $HOME_DIR from master keydrive" 6 70
  128. else
  129. if [ -d $USB_MOUNT/.gnupg ]; then
  130. if [ ! -d $HOME_DIR/.gnupg ]; then
  131. mkdir $HOME_DIR/.gnupg
  132. fi
  133. echo $'Recovering GPG keys'
  134. cp -r $USB_MOUNT/.gnupg/* $HOME_DIR/.gnupg
  135. GPG_LOADING="no"
  136. dialog --title $"Recover Encryption Keys" \
  137. --msgbox $"GPG Keyring loaded to $HOME_DIR" 6 70
  138. else
  139. if [ ! -d $HOME_DIR/.gnupg_fragments ]; then
  140. mkdir $HOME_DIR/.gnupg_fragments
  141. fi
  142. cp -r $USB_MOUNT/.gnupg_fragments/* $HOME_DIR/.gnupg_fragments
  143. fi
  144. fi
  145. if [[ $SSH_IMPORTED == "no" ]]; then
  146. if [ -d $USB_MOUNT/.ssh ]; then
  147. if [ ! -d $HOME_DIR/.ssh ]; then
  148. mkdir $HOME_DIR/.ssh
  149. fi
  150. cp $USB_MOUNT/.ssh/* $HOME_DIR/.ssh
  151. dialog --title $"Recover Encryption Keys" \
  152. --msgbox $"ssh keys imported" 6 70
  153. SSH_IMPORTED="yes"
  154. fi
  155. fi
  156. if [ -d $USB_MOUNT ]; then
  157. backup_unmount_drive ${USB_DRIVE}
  158. fi
  159. if [[ $GPG_LOADING == "yes" ]]; then
  160. dialog --title $"Recover Encryption Keys" \
  161. --msgbox $"Now remove the USB drive. Insert the next drive containing a key fragment, or select Ok to finish" 6 70
  162. fi
  163. GPG_CTR=$((GPG_CTR + 1))
  164. done
  165. }
  166. function interactive_gpg_from_remote {
  167. REMOTE_SERVERS_LIST=/home/$MY_USERNAME/keyshareservers.txt
  168. # get a list of remote servers
  169. ${PROJECT_NAME}-remote -u $MY_USERNAME -l $REMOTE_SERVERS_LIST -t "Remote server"
  170. if [ ! -f $REMOTE_SERVERS_LIST ]; then
  171. dialog --title $"Encryption Keys Recovery" --msgbox $'Error obtaining server list' 6 70
  172. return 1
  173. fi
  174. # check the number of entries in the file
  175. no_of_servers=$(cat $REMOTE_SERVERS_LIST | wc -l)
  176. if (( no_of_servers < 3 )); then
  177. dialog --title $"Encryption Keys Recovery" \
  178. --msgbox $'There must be at least three servers to recover the key' 6 70
  179. return 2
  180. fi
  181. # try to recover the key from the servers
  182. apt-get -yq install libgfshare-bin gnupg
  183. ${PROJECT_NAME}-recoverkey -u $MY_USERNAME -l $REMOTE_SERVERS_LIST
  184. if [ ! "$?" = "0" ]; then
  185. dialog --title $"Encryption Keys Recovery" --msgbox $'Your key could not be recovered' 6 70
  186. return 3
  187. fi
  188. dialog --title $"Encryption Keys Recovery" --msgbox $'Your key has been recovered' 6 70
  189. return 0
  190. }
  191. function interactive_gpg {
  192. GPG_CONFIGURED="no"
  193. while [[ $GPG_CONFIGURED != "yes" ]]
  194. do
  195. GPG_CONFIGURED="yes"
  196. data=$(tempfile 2>/dev/null)
  197. trap "rm -f $data" 0 1 2 5 15
  198. dialog --backtitle $"Freedombone Configuration" \
  199. --radiolist $"GPG/PGP keys for your system:" 13 70 3 \
  200. 1 $"Generate new keys (new user)" on \
  201. 2 $"Import keys from USB drive/s" off \
  202. 3 $"Retrieve keys from friends servers" off 2> $data
  203. sel=$?
  204. case $sel in
  205. 1) exit 1;;
  206. 255) exit 2;;
  207. esac
  208. case $(cat $data) in
  209. 1) if [ -d /home/${MY_USERNAME}/.gnupg ]; then
  210. rm -rf /home/${MY_USERNAME}/.gnupg
  211. fi
  212. break;;
  213. 2) interactive_gpg_from_usb
  214. break;;
  215. 3) interactive_gpg_from_remote
  216. if [ ! "$?" = "0" ]; then
  217. GPG_CONFIGURED="no"
  218. fi;;
  219. esac
  220. done
  221. }
  222. function interactive_key_recovery {
  223. data=$(tempfile 2>/dev/null)
  224. trap "rm -f $data" 0 1 2 5 15
  225. dialog --title $"Encryption Keys Recovery" \
  226. --backtitle $"Freedombone Configuration" \
  227. --defaultno \
  228. --yesno $"Do you wish to recover your previous encryption keys from a USB master keydrive?" 7 60
  229. sel=$?
  230. case $sel in
  231. 1) return;;
  232. 255) return;;
  233. esac
  234. clear
  235. apt-get -yq install cryptsetup
  236. ${PROJECT_NAME}-recoverkey -u $MY_USERNAME
  237. if [ -d /home/$MY_USERNAME/.gnupg ]; then
  238. cp -rf /home/$MY_USERNAME/.gnupg /root
  239. chmod 700 /root/.gnupg
  240. chmod 600 /root/.gnupg/*
  241. fi
  242. }
  243. function set_password_for_all_users {
  244. app_name="$1"
  245. change_password="$2"
  246. for d in /home/*/ ; do
  247. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  248. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  249. ${PROJECT_NAME}-pass -u "${USERNAME}" -a "${app_name}" -p "${change_password}"
  250. fi
  251. done
  252. }
  253. # NOTE: deliberately there is no "exit 0"