freedombone-keydrive 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Makes a USB drive containing a gpg key fragment
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2015-2017 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. PROJECT_NAME='freedombone'
  31. export TEXTDOMAIN=${PROJECT_NAME}-keydrive
  32. export TEXTDOMAINDIR="/usr/share/locale"
  33. USB_DRIVE=/dev/sdb1
  34. USB_MOUNT=/mnt/usb
  35. KEY_FRAGMENTS=3
  36. FRAGMENTS_DIR=$USB_MOUNT/.gnupg_fragments
  37. MY_USERNAME=$USER
  38. MASTER_DRIVE="no"
  39. FORMAT="no"
  40. function show_help {
  41. echo ''
  42. echo $"${PROJECT_NAME}-keydrive -u [username] -d [device, eg. sdb] --master [yes/no] -n [no of fragments] --format [yes/no]"
  43. echo ''
  44. exit 0
  45. }
  46. while [[ $# > 1 ]]
  47. do
  48. key="$1"
  49. case $key in
  50. -h|--help)
  51. show_help
  52. ;;
  53. -u|--user)
  54. shift
  55. MY_USERNAME="$1"
  56. ;;
  57. -d|--dev)
  58. shift
  59. if [[ "${1}" != '/dev/'* ]]; then
  60. USB_DRIVE=/dev/${1}1
  61. else
  62. USB_DRIVE=${1}
  63. fi
  64. ;;
  65. -m|--master)
  66. shift
  67. MASTER_DRIVE="$1"
  68. ;;
  69. -n|--fragments)
  70. shift
  71. KEY_FRAGMENTS=$1
  72. ;;
  73. -f|--format)
  74. shift
  75. FORMAT="yes"
  76. ;;
  77. *)
  78. # unknown option
  79. ;;
  80. esac
  81. shift
  82. done
  83. if [ ! $MY_USERNAME ]; then
  84. echo $'No username given'
  85. exit 69350
  86. fi
  87. if [ ! -d /home/$MY_USERNAME ]; then
  88. echo $"Home directory for $MY_USERNAME not found. This user may not exist on the system"
  89. exit 72378
  90. fi
  91. if [ ! -b $USB_DRIVE ]; then
  92. echo $'Please attach a USB drive'
  93. exit 65743
  94. fi
  95. umount -f $USB_MOUNT
  96. if [ ! -d $USB_MOUNT ]; then
  97. mkdir $USB_MOUNT
  98. fi
  99. if [ -f /dev/mapper/encrypted_usb ]; then
  100. rm -rf /dev/mapper/encrypted_usb
  101. fi
  102. cryptsetup luksClose encrypted_usb
  103. # optionally format the drive
  104. if [[ $FORMAT == "yes" ]]; then
  105. ${PROJECT_NAME}-format ${USB_DRIVE::-1}
  106. if [ ! "$?" = "0" ]; then
  107. exit 36823
  108. fi
  109. fi
  110. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  111. if [ "$?" = "0" ]; then
  112. USB_DRIVE=/dev/mapper/encrypted_usb
  113. fi
  114. mount $USB_DRIVE $USB_MOUNT
  115. if [ ! "$?" = "0" ]; then
  116. echo $"There was a problem mounting the USB drive to $USB_MOUNT"
  117. rm -rf $USB_MOUNT
  118. exit 78543
  119. fi
  120. # optionally create a master drive which contains the full GPG keyring
  121. if [[ $MASTER_DRIVE == "yes" || $MASTER_DRIVE == "y" || $MASTER_DRIVE == "1" ]]; then
  122. if [ ! -d /home/$MY_USERNAME/.gnupg ]; then
  123. echo $"No .gnupg directory was found for $MY_USERNAME"
  124. umount -f $USB_MOUNT
  125. rm -rf $USB_MOUNT
  126. exit 73025
  127. fi
  128. # export the gpg key and backup key as text
  129. # so that it may be imported at the beginning of new installs
  130. GPG_TTY=$(tty)
  131. export GPG_TTY
  132. USER_EMAIL_ADDRESS=$MY_USERNAME@$HOSTNAME
  133. GPG_ID=$(su -m root -c "gpg --list-keys $USER_EMAIL_ADDRESS | sed -n '2p' | sed 's/^[ \t]*//'" - $MY_USERNAME)
  134. GPG_BACKUP_ID=$(su -m root -c "gpg --list-keys \"(backup key)\" | sed -n '2p' | sed 's/^[ \t]*//'" - $MY_USERNAME)
  135. gpgerrstr=$'error'
  136. gpgkey=$(gpg --homedir=/home/$MY_USERNAME/.gnupg --armor --export $GPG_ID)
  137. if [[ "$gpgkey" == *"$gpgerrstr"* ]]; then
  138. echo $'Problem exporting public gpg key'
  139. echo "$gpgkey"
  140. exit 735282
  141. fi
  142. echo ''
  143. echo $'Enter your gpg private key passphrase:'
  144. gpgprivkey=$(gpg --homedir=/home/$MY_USERNAME/.gnupg --armor --export-secret-key $GPG_ID)
  145. if [[ "$gpgprivkey" == *"$gpgerrstr"* ]]; then
  146. echo $'Problem exporting private gpg key'
  147. echo "$gpgprivkey"
  148. gpgprivkey=
  149. exit 629362
  150. fi
  151. # Dummy password to get around not being able to create a key without passphrase
  152. BACKUP_DUMMY_PASSWORD='backup'
  153. backupgpgkey=$(gpg --homedir=/home/$MY_USERNAME/.gnupg --armor --export $GPG_BACKUP_ID)
  154. if [[ "$backupgpgkey" == *"$gpgerrstr"* ]]; then
  155. echo $'Problem exporting public gpg backup key'
  156. echo "$backupgpgkey"
  157. exit 735282
  158. fi
  159. backupgpgprivkey=$(echo "$BACKUP_DUMMY_PASSWORD" | gpg --batch --passphrase-fd 0 --homedir=/home/$MY_USERNAME/.gnupg --armor --export-secret-key $GPG_BACKUP_ID)
  160. if [[ "$backupgpgprivkey" == *"$gpgerrstr"* ]]; then
  161. echo $'Problem exporting private gpg backup key'
  162. echo "$backupgpgprivkey"
  163. backupgpgprivkey=
  164. exit 629362
  165. fi
  166. echo "$gpgkey" > $USB_MOUNT/.mastergpgkey
  167. echo "$gpgprivkey" >> $USB_MOUNT/.mastergpgkey
  168. echo "$backupgpgkey" > $USB_MOUNT/.backupgpgkey
  169. echo "$backupgpgprivkey" >> $USB_MOUNT/.backupgpgkey
  170. cp -rf /home/$MY_USERNAME/.gnupg $USB_MOUNT
  171. if [ -d /etc/letsencrypt ]; then
  172. cp -rf /etc/letsencrypt $USB_MOUNT
  173. echo $"LetsEncrypt keys copied to $USB_DRIVE"
  174. fi
  175. if [ -d $USB_MOUNT/.gnupg ]; then
  176. echo $"GPG Keyring copied to $USB_DRIVE. You may now remove the drive."
  177. else
  178. echo $"Unable to copy gpg keyring to $USB_DRIVE"
  179. fi
  180. umount -f $USB_MOUNT
  181. rm -rf $USB_MOUNT
  182. exit 0
  183. fi
  184. # Don't use the USB drive if it already contains a full keyring
  185. if [ -d $USB_MOUNT/.gnupg ]; then
  186. echo $'A full GPG keyring already exists on the USB drive.'
  187. echo $'Either reformat the USB drive or use a different drive.'
  188. umount -f $USB_MOUNT
  189. rm -rf $USB_MOUNT
  190. exit 3392
  191. fi
  192. # Append the username as a subdirectory.
  193. # This has a down side in that it does identify a given fragment
  194. # as belonging to a given user, but has the convenience upside
  195. # of being able to carry key fragments for multiple friends on
  196. # the same USB drive
  197. FRAGMENTS_DIR=$FRAGMENTS_DIR/$MY_USERNAME
  198. # make a directory to contain the fragments
  199. if [ ! -d $FRAGMENTS_DIR ]; then
  200. mkdir -p $FRAGMENTS_DIR
  201. echo $"Made directory $FRAGMENTS_DIR"
  202. fi
  203. if [ ! -d $FRAGMENTS_DIR ]; then
  204. echo $"There was a problem making the directory $FRAGMENTS_DIR"
  205. umount -f $USB_MOUNT
  206. rm -rf $USB_MOUNT
  207. exit 6843
  208. fi
  209. cd $FRAGMENTS_DIR
  210. no_of_usb_shares=$(ls -afq keyshare.asc.* | wc -l)
  211. if [ ! "$?" = "0" ]; then
  212. no_of_usb_shares=0
  213. fi
  214. if (( no_of_usb_shares > 0 )); then
  215. echo $"A key fragment already exists on the drive for the user $MY_USERNAME"
  216. cd ~/
  217. umount -f $USB_MOUNT
  218. rm -rf $USB_MOUNT
  219. exit 58945
  220. fi
  221. # copy a random fragment to the drive
  222. LOCAL_FRAGMENTS_DIR=/home/$MY_USERNAME/.gnupg_fragments
  223. if [ ! -d $LOCAL_FRAGMENTS_DIR ]; then
  224. ${PROJECT_NAME}-splitkey -u $MY_USERNAME -n $KEY_FRAGMENTS
  225. fi
  226. cd $LOCAL_FRAGMENTS_DIR
  227. no_of_local_shares=$(ls -afq keyshare.asc.* | wc -l)
  228. if [ ! "$?" = "0" ]; then
  229. no_of_local_shares=0
  230. fi
  231. if (( no_of_local_shares < 3 )); then
  232. ${PROJECT_NAME}-splitkey -u $MY_USERNAME -n $KEY_FRAGMENTS
  233. cd $LOCAL_FRAGMENTS_DIR
  234. no_of_local_shares=$(ls -afq keyshare.asc.* | wc -l)
  235. if [ ! "$?" = "0" ]; then
  236. no_of_local_shares=0
  237. fi
  238. fi
  239. if (( no_of_local_shares < 3 )); then
  240. echo $"Not enough key fragments available ${no_of_local_shares}"
  241. cd ~/
  242. umount -f $USB_MOUNT
  243. rm -rf $USB_MOUNT
  244. exit 63386
  245. fi
  246. share_files=($LOCAL_FRAGMENTS_DIR/keyshare.asc.*)
  247. SHARE_FILENAME=${share_files[RANDOM % ${#share_files[@]}]}
  248. cp -f $SHARE_FILENAME $FRAGMENTS_DIR
  249. cd $FRAGMENTS_DIR
  250. no_of_usb_shares=$(ls -afq keyshare.asc.* | wc -l)
  251. echo $"Number of fragments on the drive: ${no_of_usb_shares}"
  252. if (( no_of_usb_shares > 1 )); then
  253. echo $"Too many key fragments exist in $FRAGMENTS_DIR"
  254. ls $FRAGMENTS_DIR
  255. cd ~/
  256. umount -f $USB_MOUNT
  257. rm -rf $USB_MOUNT
  258. exit 54292
  259. fi
  260. if (( no_of_usb_shares <= 0 )); then
  261. echo $"There was a problem copying the key fragment to $USB_DRIVE"
  262. echo $"Files found: ${no_of_usb_shares}"
  263. ls $FRAGMENTS_DIR
  264. cd ~/
  265. umount -f $USB_MOUNT
  266. rm -rf $USB_MOUNT
  267. exit 54292
  268. fi
  269. cd ~/
  270. umount -f $USB_MOUNT
  271. rm -rf $USB_MOUNT
  272. echo $"Key fragment copied to $USB_DRIVE. You may now remove the drive."
  273. exit 0