freedombone-utils-keys 9.8KB

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