freedombone-keydrive 8.8KB

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