freedombone-keydrive 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU 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 General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. USB_DRIVE=/dev/sdb1
  31. USB_MOUNT=/mnt/usb
  32. KEY_FRAGMENTS=3
  33. FRAGMENTS_DIR=$USB_MOUNT/.gnupg_fragments
  34. MY_USERNAME=$USER
  35. MASTER_DRIVE="no"
  36. function show_help {
  37. echo ''
  38. echo 'freedombone-keydrive -u [username] -d [device, eg. sdb] --master [yes/no] -n [no of fragments]'
  39. echo ''
  40. exit 0
  41. }
  42. while [[ $# > 1 ]]
  43. do
  44. key="$1"
  45. case $key in
  46. -h|--help)
  47. show_help
  48. ;;
  49. -u|--user)
  50. shift
  51. MY_USERNAME="$1"
  52. ;;
  53. -d|--dev)
  54. shift
  55. USB_DRIVE=/dev/${1}1
  56. ;;
  57. -m|--master)
  58. shift
  59. MASTER_DRIVE="$1"
  60. ;;
  61. -n|--fragments)
  62. shift
  63. KEY_FRAGMENTS=$1
  64. ;;
  65. *)
  66. # unknown option
  67. ;;
  68. esac
  69. shift
  70. done
  71. if [ ! $MY_USERNAME ]; then
  72. echo 'No username given'
  73. exit 69350
  74. fi
  75. if [ ! -d /home/$MY_USERNAME ]; then
  76. echo "Home directory for $MY_USERNAME not found. This user may not exist on the system"
  77. exit 72378
  78. fi
  79. if [ ! -b $USB_DRIVE ]; then
  80. echo 'Please attach a USB drive'
  81. exit 65743
  82. fi
  83. umount -f $USB_MOUNT
  84. if [ ! -d $USB_MOUNT ]; then
  85. mkdir $USB_MOUNT
  86. fi
  87. if [ -f /dev/mapper/encrypted_usb ]; then
  88. rm -rf /dev/mapper/encrypted_usb
  89. fi
  90. cryptsetup luksClose encrypted_usb
  91. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  92. if [ "$?" = "0" ]; then
  93. USB_DRIVE=/dev/mapper/encrypted_usb
  94. fi
  95. mount $USB_DRIVE $USB_MOUNT
  96. if [ ! "$?" = "0" ]; then
  97. echo "There was a problem mounting the USB drive to $USB_MOUNT"
  98. rm -rf $USB_MOUNT
  99. exit 78543
  100. fi
  101. # optionally create a master drive which contains the full GPG keyring
  102. if [[ $MASTER_DRIVE == "yes" || $MASTER_DRIVE == "y" || $MASTER_DRIVE == "1" ]]; then
  103. if [ ! -d /home/$MY_USERNAME/.gnupg ]; then
  104. echo "No .gnupg directory was found for $MY_USERNAME"
  105. umount -f $USB_MOUNT
  106. rm -rf $USB_MOUNT
  107. exit 73025
  108. fi
  109. cp -rf /home/$MY_USERNAME/.gnupg $USB_MOUNT
  110. if [ -d $USB_MOUNT/.gnupg ]; then
  111. echo "GPG Keyring copied to $USB_DRIVE. You may now remove the drive."
  112. else
  113. echo "Unable to copy gpg keyring to $USB_DRIVE"
  114. fi
  115. umount -f $USB_MOUNT
  116. rm -rf $USB_MOUNT
  117. exit 0
  118. fi
  119. # Don't use the USB drive if it already contains a full keyring
  120. if [ -d $USB_MOUNT/.gnupg ]; then
  121. echo 'A full GPG keyring already exists on the USB drive.'
  122. echo 'Either reformat the USB drive or use a different drive.'
  123. umount -f $USB_MOUNT
  124. rm -rf $USB_MOUNT
  125. exit 3392
  126. fi
  127. # Append the username as a subdirectory.
  128. # This has a down side in that it does identify a given fragment
  129. # as belonging to a given user, but has the convenience upside
  130. # of being able to carry key fragments for multiple friends on
  131. # the same USB drive
  132. FRAGMENTS_DIR=$FRAGMENTS_DIR/$MY_USERNAME
  133. # make a directory to contain the fragments
  134. if [ ! -d $FRAGMENTS_DIR ]; then
  135. mkdir -p $FRAGMENTS_DIR
  136. echo "Made directory $FRAGMENTS_DIR"
  137. fi
  138. if [ ! -d $FRAGMENTS_DIR ]; then
  139. echo "There was a problem making the directory $FRAGMENTS_DIR"
  140. umount -f $USB_MOUNT
  141. rm -rf $USB_MOUNT
  142. exit 6843
  143. fi
  144. cd $FRAGMENTS_DIR
  145. no_of_usb_shares=$(ls -afq keyshare.asc.* | wc -l)
  146. if [ ! "$?" = "0" ]; then
  147. no_of_usb_shares=0
  148. fi
  149. if (( no_of_usb_shares > 0 )); then
  150. echo "A key fragment already exists on the drive for the user $MY_USERNAME"
  151. cd ~/
  152. umount -f $USB_MOUNT
  153. rm -rf $USB_MOUNT
  154. exit 58945
  155. fi
  156. # copy a random fragment to the drive
  157. LOCAL_FRAGMENTS_DIR=/home/$MY_USERNAME/.gnupg_fragments
  158. if [ ! -d $LOCAL_FRAGMENTS_DIR ]; then
  159. freedombone-splitkey -u $MY_USERNAME -n $KEY_FRAGMENTS
  160. fi
  161. cd $LOCAL_FRAGMENTS_DIR
  162. no_of_local_shares=$(ls -afq keyshare.asc.* | wc -l)
  163. if [ ! "$?" = "0" ]; then
  164. no_of_local_shares=0
  165. fi
  166. if (( no_of_local_shares < 3 )); then
  167. freedombone-splitkey -u $MY_USERNAME -n $KEY_FRAGMENTS
  168. cd $LOCAL_FRAGMENTS_DIR
  169. no_of_local_shares=$(ls -afq keyshare.asc.* | wc -l)
  170. if [ ! "$?" = "0" ]; then
  171. no_of_local_shares=0
  172. fi
  173. fi
  174. if (( no_of_local_shares < 3 )); then
  175. echo "Not enough key fragments available ${no_of_local_shares}"
  176. cd ~/
  177. umount -f $USB_MOUNT
  178. rm -rf $USB_MOUNT
  179. exit 63386
  180. fi
  181. share_files=($LOCAL_FRAGMENTS_DIR/keyshare.asc.*)
  182. SHARE_FILENAME=${share_files[RANDOM % ${#share_files[@]}]}
  183. cp -f $SHARE_FILENAME $FRAGMENTS_DIR
  184. cd $FRAGMENTS_DIR
  185. no_of_usb_shares=$(ls -afq keyshare.asc.* | wc -l)
  186. echo "Number of fragments on the drive: ${no_of_usb_shares}"
  187. if (( no_of_usb_shares > 1 )); then
  188. echo "Too many key fragments exist in $FRAGMENTS_DIR"
  189. ls $FRAGMENTS_DIR
  190. cd ~/
  191. umount -f $USB_MOUNT
  192. rm -rf $USB_MOUNT
  193. exit 54292
  194. fi
  195. if (( no_of_usb_shares <= 0 )); then
  196. echo "There was a problem copying the key fragment to $USB_DRIVE"
  197. echo "Files found: ${no_of_usb_shares}"
  198. ls $FRAGMENTS_DIR
  199. cd ~/
  200. umount -f $USB_MOUNT
  201. rm -rf $USB_MOUNT
  202. exit 54292
  203. fi
  204. cd ~/
  205. umount -f $USB_MOUNT
  206. rm -rf $USB_MOUNT
  207. echo "Key fragment copied to $USB_DRIVE. You may now remove the drive."
  208. exit 0