freedombone-utils-keys 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Encryption key related functions
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. function interactive_gpg_from_usb {
  29. dialog --title $"Recover Encryption Keys" \
  30. --msgbox $'Plug in a USB keydrive containing a copy of your full key or key fragment' 6 70
  31. HOME_DIR=/home/$MY_USERNAME
  32. GPG_LOADING="yes"
  33. SSH_IMPORTED="no"
  34. GPG_CTR=0
  35. while [[ $GPG_LOADING == "yes" ]]
  36. do
  37. detect_usb_drive
  38. if [ ! -b "$USB_DRIVE" ]; then
  39. if (( GPG_CTR > 0 )); then
  40. gpg_reconstruct_key "$MY_USERNAME" interactive
  41. gpg_update_mutt "$MY_USERNAME"
  42. return 0
  43. fi
  44. dialog --title $"Recover Encryption Keys" --msgbox $'No USB drive found' 6 30
  45. exit 739836
  46. fi
  47. backup_mount_drive "${USB_DRIVE}" "${MY_USERNAME}"
  48. if [ ! -d "$USB_MOUNT" ]; then
  49. if (( GPG_CTR > 0 )); then
  50. backup_unmount_drive "${USB_DRIVE}"
  51. gpg_reconstruct_key "$MY_USERNAME" interactive
  52. return 0
  53. fi
  54. dialog --title $"Recover Encryption Keys" \
  55. --msgbox $"There was a problem mounting the USB drive $USB_DRIVE to $USB_MOUNT" 6 70
  56. backup_unmount_drive "${USB_DRIVE}"
  57. exit 74393
  58. fi
  59. if [ ! -d "$USB_MOUNT/.gnupg" ]; then
  60. if [ ! -d "$USB_MOUNT/.gnupg_fragments" ]; then
  61. if (( GPG_CTR > 0 )); then
  62. backup_unmount_drive "${USB_DRIVE}"
  63. gpg_reconstruct_key "$MY_USERNAME" interactive
  64. gpg_update_mutt "$MY_USERNAME"
  65. return 0
  66. fi
  67. dialog --title $"Recover Encryption Keys" \
  68. --msgbox $"The directory $USB_MOUNT/.gnupg or $USB_MOUNT/.gnupg_fragments was not found" 6 70
  69. backup_unmount_drive "${USB_DRIVE}"
  70. exit 723814
  71. fi
  72. fi
  73. if [ -d "$USB_MOUNT/letsencrypt" ]; then
  74. if [ ! -d /etc/letsencrypt ]; then
  75. mkdir /etc/letsencrypt
  76. fi
  77. echo $'Recovering LetsEncrypt keys'
  78. cp -r "$USB_MOUNT/letsencrypt/"* /etc/letsencrypt
  79. addgroup ssl-cert
  80. chown -R root:ssl-cert /etc/letsencrypt
  81. fi
  82. if [ -f "$USB_MOUNT/.mastergpgkey" ]; then
  83. # Recovering keys from file rather than just copying the gnupg
  84. # directory may help to avoid problems during upgrades/reinstalls
  85. if [ ! -f "$USB_MOUNT/.backupgpgkey" ]; then
  86. echo $'No backup key file found on USB drive'
  87. exit 725729
  88. fi
  89. CURR_EMAIL_ADDRESS=$MY_USERNAME@$HOSTNAME
  90. CURR_GPG_ID=$(gpg --homedir="$HOME_DIR/.gnupg" --list-keys "$CURR_EMAIL_ADDRESS" | sed -n '2p' | sed 's/^[ \t]*//')
  91. CURR_GPG_BACKUP_ID=$(gpg --homedir="$HOME_DIR/.gnupg" --list-keys "(backup key)" | sed -n '2p' | sed 's/^[ \t]*//')
  92. # If the default key is specified within gpg.conf
  93. if [ -f "$HOME_DIR/gpg.conf" ]; then
  94. if grep -q "default-key" "$HOME_DIR/gpg.conf"; then
  95. default_gpg_key=$(grep "default-key" "$HOME_DIR/gpg.conf")
  96. if [[ "$default_gpg_key" != *'#'* ]]; then
  97. default_gpg_key=$(grep "default-key" "$HOME_DIR/gpg.conf" | awk -F ' ' '{print $2}')
  98. if [ ${#default_gpg_key} -gt 3 ]; then
  99. CURR_GPG_ID=$(gpg --homedir="$HOME_DIR/.gnupg" --list-keys "$default_gpg_key" | sed -n '2p' | sed 's/^[ \t]*//')
  100. fi
  101. fi
  102. fi
  103. fi
  104. echo $'Making backup copy of existing gpg keys'
  105. if [ -d "$HOME_DIR/.gnupg" ]; then
  106. if [ -d "$HOME_DIR/.gnupg_old" ]; then
  107. rm -rf "$HOME_DIR/.gnupg_old"
  108. fi
  109. cp -r "$HOME_DIR/.gnupg $HOME_DIR/.gnupg_old"
  110. chmod 700 "$HOME_DIR/.gnupg_old"
  111. chmod -R 600 "$HOME_DIR/.gnupg_old/"*
  112. chown -R "$MY_USERNAME":"$MY_USERNAME" "$HOME_DIR/.gnupg_old"
  113. fi
  114. echo $'Removing old gpg keys'
  115. gpg_delete_key "$MY_USERNAME" "$CURR_GPG_BACKUP_ID"
  116. gpg_delete_key "$MY_USERNAME" "$CURR_GPG_ID"
  117. echo $'Importing master keys'
  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. echo $'Setting permissions'
  122. gpg_set_permissions "$MY_USERNAME"
  123. echo $"Updating muttrc for $MY_USERNAME"
  124. gpg_update_mutt "$MY_USERNAME"
  125. fi
  126. GPG_LOADING="no"
  127. dialog --title $"Recover Encryption Keys" \
  128. --msgbox $"GPG Keyring loaded to $HOME_DIR from master keydrive" 6 70
  129. else
  130. if [ -d "$USB_MOUNT/.gnupg" ]; then
  131. if [ ! -d "$HOME_DIR/.gnupg" ]; then
  132. mkdir "$HOME_DIR/.gnupg"
  133. fi
  134. echo $'Recovering GPG keys'
  135. cp -r "$USB_MOUNT/.gnupg/"* "$HOME_DIR/.gnupg"
  136. GPG_LOADING="no"
  137. dialog --title $"Recover Encryption Keys" \
  138. --msgbox $"GPG Keyring directory loaded to $HOME_DIR" 6 70
  139. else
  140. # Collect fragments from the USB drive
  141. if [ ! -d "$HOME_DIR/.gnupg_fragments" ]; then
  142. mkdir "$HOME_DIR/.gnupg_fragments"
  143. fi
  144. cp -r "$USB_MOUNT/.gnupg_fragments/"* "$HOME_DIR/.gnupg_fragments"
  145. fi
  146. fi
  147. if [[ "$SSH_IMPORTED" == "no" ]]; then
  148. if [ -d "$USB_MOUNT/.ssh" ]; then
  149. if [ ! -d "$HOME_DIR/.ssh" ]; then
  150. mkdir "$HOME_DIR/.ssh"
  151. fi
  152. cp "$USB_MOUNT/.ssh/"* "$HOME_DIR/.ssh"
  153. dialog --title $"Recover Encryption Keys" \
  154. --msgbox $"ssh keys imported" 6 70
  155. SSH_IMPORTED="yes"
  156. fi
  157. fi
  158. if [ -d "$USB_MOUNT" ]; then
  159. backup_unmount_drive "${USB_DRIVE}"
  160. fi
  161. if [[ $GPG_LOADING == "yes" ]]; then
  162. dialog --title $"Recover Encryption Keys" \
  163. --msgbox $"Now remove the USB drive. Insert the next drive containing a key fragment, or select Ok to finish" 6 70
  164. fi
  165. GPG_CTR=$((GPG_CTR + 1))
  166. done
  167. }
  168. function interactive_gpg_from_remote {
  169. REMOTE_SERVERS_LIST=/home/$MY_USERNAME/keyshareservers.txt
  170. # get a list of remote servers
  171. "${PROJECT_NAME}-remote" -u "$MY_USERNAME" -l "$REMOTE_SERVERS_LIST" -t "Remote server"
  172. if [ ! -f "$REMOTE_SERVERS_LIST" ]; then
  173. dialog --title $"Encryption Keys Recovery" --msgbox $'Error obtaining server list' 6 70
  174. return 1
  175. fi
  176. # check the number of entries in the file
  177. no_of_servers=$(wc -l < "$REMOTE_SERVERS_LIST")
  178. if (( no_of_servers < 3 )); then
  179. dialog --title $"Encryption Keys Recovery" \
  180. --msgbox $'There must be at least three servers to recover the key' 6 70
  181. return 2
  182. fi
  183. # try to recover the key from the servers
  184. if ! "${PROJECT_NAME}-recoverkey" -u "$MY_USERNAME" -l "$REMOTE_SERVERS_LIST"; 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=$(mktemp 2>/dev/null)
  197. dialog --backtitle $"Freedombone Configuration" \
  198. --radiolist $"GPG/PGP keys for your system:" 13 70 3 \
  199. 1 $"Generate new keys (new user)" on \
  200. 2 $"Import keys from USB drive/s" off \
  201. 3 $"Retrieve keys from friends servers" off 2> "$data"
  202. sel=$?
  203. case $sel in
  204. 1) rm -f "$data"
  205. exit 1;;
  206. 255) rm -f "$data"
  207. exit 2;;
  208. esac
  209. case $(cat "$data") in
  210. 1) if [ -d "/home/${MY_USERNAME}/.gnupg" ]; then
  211. rm -rf "/home/${MY_USERNAME}/.gnupg"
  212. fi
  213. break;;
  214. 2) interactive_gpg_from_usb
  215. break;;
  216. 3) if ! interactive_gpg_from_remote; then
  217. GPG_CONFIGURED="no"
  218. fi;;
  219. esac
  220. rm -f "$data"
  221. done
  222. }
  223. function interactive_key_recovery {
  224. data=$(mktemp 2>/dev/null)
  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) rm -f "$data"
  232. return;;
  233. 255) rm -f "$data"
  234. return;;
  235. esac
  236. clear
  237. apt-get -yq install cryptsetup
  238. "${PROJECT_NAME}-recoverkey" -u "$MY_USERNAME"
  239. if [ -d "/home/$MY_USERNAME/.gnupg" ]; then
  240. cp -rf "/home/$MY_USERNAME/.gnupg" /root
  241. chmod 700 /root/.gnupg
  242. chmod 600 /root/.gnupg/*
  243. printf '%%Assuan%%\nsocket=/dev/shm/S.dirmngr\n' > /root/.gnupg/S.dirmngr
  244. if [ -d /root/.gnupg/crls.d ]; then
  245. chmod +x /root/.gnupg/crls.d
  246. fi
  247. fi
  248. rm -f "$data"
  249. }
  250. function set_password_for_all_users {
  251. app_name="$1"
  252. change_password="$2"
  253. for d in /home/*/ ; do
  254. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  255. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  256. "${PROJECT_NAME}-pass" -u "${USERNAME}" -a "${app_name}" -p "${change_password}"
  257. fi
  258. done
  259. }
  260. # NOTE: deliberately there is no "exit 0"