freedombone-utils-keys 11KB

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