freedombone-keydrive 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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-2016 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. USB_DRIVE=/dev/${1}1
  60. ;;
  61. -m|--master)
  62. shift
  63. MASTER_DRIVE="$1"
  64. ;;
  65. -n|--fragments)
  66. shift
  67. KEY_FRAGMENTS=$1
  68. ;;
  69. -f|--format)
  70. shift
  71. FORMAT="yes"
  72. ;;
  73. *)
  74. # unknown option
  75. ;;
  76. esac
  77. shift
  78. done
  79. if [ ! $MY_USERNAME ]; then
  80. echo $'No username given'
  81. exit 69350
  82. fi
  83. if [ ! -d /home/$MY_USERNAME ]; then
  84. echo $"Home directory for $MY_USERNAME not found. This user may not exist on the system"
  85. exit 72378
  86. fi
  87. if [ ! -b $USB_DRIVE ]; then
  88. echo $'Please attach a USB drive'
  89. exit 65743
  90. fi
  91. umount -f $USB_MOUNT
  92. if [ ! -d $USB_MOUNT ]; then
  93. mkdir $USB_MOUNT
  94. fi
  95. if [ -f /dev/mapper/encrypted_usb ]; then
  96. rm -rf /dev/mapper/encrypted_usb
  97. fi
  98. cryptsetup luksClose encrypted_usb
  99. # optionally format the drive
  100. if [[ $FORMAT == "yes" ]]; then
  101. ${PROJECT_NAME}-format ${USB_DRIVE::-1}
  102. if [ ! "$?" = "0" ]; then
  103. exit 36823
  104. fi
  105. fi
  106. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  107. if [ "$?" = "0" ]; then
  108. USB_DRIVE=/dev/mapper/encrypted_usb
  109. fi
  110. mount $USB_DRIVE $USB_MOUNT
  111. if [ ! "$?" = "0" ]; then
  112. echo $"There was a problem mounting the USB drive to $USB_MOUNT"
  113. rm -rf $USB_MOUNT
  114. exit 78543
  115. fi
  116. # optionally create a master drive which contains the full GPG keyring
  117. if [[ $MASTER_DRIVE == "yes" || $MASTER_DRIVE == "y" || $MASTER_DRIVE == "1" ]]; then
  118. if [ ! -d /home/$MY_USERNAME/.gnupg ]; then
  119. echo $"No .gnupg directory was found for $MY_USERNAME"
  120. umount -f $USB_MOUNT
  121. rm -rf $USB_MOUNT
  122. exit 73025
  123. fi
  124. cp -rf /home/$MY_USERNAME/.gnupg $USB_MOUNT
  125. if [ -d /etc/letsencrypt ]; then
  126. cp -rf /etc/letsencrypt $USB_MOUNT
  127. echo $"LetsEncrypt keys copied to $USB_DRIVE"
  128. fi
  129. if [ -d $USB_MOUNT/.gnupg ]; then
  130. echo $"GPG Keyring copied to $USB_DRIVE. You may now remove the drive."
  131. else
  132. echo $"Unable to copy gpg keyring to $USB_DRIVE"
  133. fi
  134. umount -f $USB_MOUNT
  135. rm -rf $USB_MOUNT
  136. exit 0
  137. fi
  138. # Don't use the USB drive if it already contains a full keyring
  139. if [ -d $USB_MOUNT/.gnupg ]; then
  140. echo $'A full GPG keyring already exists on the USB drive.'
  141. echo $'Either reformat the USB drive or use a different drive.'
  142. umount -f $USB_MOUNT
  143. rm -rf $USB_MOUNT
  144. exit 3392
  145. fi
  146. # Append the username as a subdirectory.
  147. # This has a down side in that it does identify a given fragment
  148. # as belonging to a given user, but has the convenience upside
  149. # of being able to carry key fragments for multiple friends on
  150. # the same USB drive
  151. FRAGMENTS_DIR=$FRAGMENTS_DIR/$MY_USERNAME
  152. # make a directory to contain the fragments
  153. if [ ! -d $FRAGMENTS_DIR ]; then
  154. mkdir -p $FRAGMENTS_DIR
  155. echo $"Made directory $FRAGMENTS_DIR"
  156. fi
  157. if [ ! -d $FRAGMENTS_DIR ]; then
  158. echo $"There was a problem making the directory $FRAGMENTS_DIR"
  159. umount -f $USB_MOUNT
  160. rm -rf $USB_MOUNT
  161. exit 6843
  162. fi
  163. cd $FRAGMENTS_DIR
  164. no_of_usb_shares=$(ls -afq keyshare.asc.* | wc -l)
  165. if [ ! "$?" = "0" ]; then
  166. no_of_usb_shares=0
  167. fi
  168. if (( no_of_usb_shares > 0 )); then
  169. echo $"A key fragment already exists on the drive for the user $MY_USERNAME"
  170. cd ~/
  171. umount -f $USB_MOUNT
  172. rm -rf $USB_MOUNT
  173. exit 58945
  174. fi
  175. # copy a random fragment to the drive
  176. LOCAL_FRAGMENTS_DIR=/home/$MY_USERNAME/.gnupg_fragments
  177. if [ ! -d $LOCAL_FRAGMENTS_DIR ]; then
  178. ${PROJECT_NAME}-splitkey -u $MY_USERNAME -n $KEY_FRAGMENTS
  179. fi
  180. cd $LOCAL_FRAGMENTS_DIR
  181. no_of_local_shares=$(ls -afq keyshare.asc.* | wc -l)
  182. if [ ! "$?" = "0" ]; then
  183. no_of_local_shares=0
  184. fi
  185. if (( no_of_local_shares < 3 )); then
  186. ${PROJECT_NAME}-splitkey -u $MY_USERNAME -n $KEY_FRAGMENTS
  187. cd $LOCAL_FRAGMENTS_DIR
  188. no_of_local_shares=$(ls -afq keyshare.asc.* | wc -l)
  189. if [ ! "$?" = "0" ]; then
  190. no_of_local_shares=0
  191. fi
  192. fi
  193. if (( no_of_local_shares < 3 )); then
  194. echo $"Not enough key fragments available ${no_of_local_shares}"
  195. cd ~/
  196. umount -f $USB_MOUNT
  197. rm -rf $USB_MOUNT
  198. exit 63386
  199. fi
  200. share_files=($LOCAL_FRAGMENTS_DIR/keyshare.asc.*)
  201. SHARE_FILENAME=${share_files[RANDOM % ${#share_files[@]}]}
  202. cp -f $SHARE_FILENAME $FRAGMENTS_DIR
  203. cd $FRAGMENTS_DIR
  204. no_of_usb_shares=$(ls -afq keyshare.asc.* | wc -l)
  205. echo $"Number of fragments on the drive: ${no_of_usb_shares}"
  206. if (( no_of_usb_shares > 1 )); then
  207. echo $"Too many key fragments exist in $FRAGMENTS_DIR"
  208. ls $FRAGMENTS_DIR
  209. cd ~/
  210. umount -f $USB_MOUNT
  211. rm -rf $USB_MOUNT
  212. exit 54292
  213. fi
  214. if (( no_of_usb_shares <= 0 )); then
  215. echo $"There was a problem copying the key fragment to $USB_DRIVE"
  216. echo $"Files found: ${no_of_usb_shares}"
  217. ls $FRAGMENTS_DIR
  218. cd ~/
  219. umount -f $USB_MOUNT
  220. rm -rf $USB_MOUNT
  221. exit 54292
  222. fi
  223. cd ~/
  224. umount -f $USB_MOUNT
  225. rm -rf $USB_MOUNT
  226. echo $"Key fragment copied to $USB_DRIVE. You may now remove the drive."
  227. exit 0