freedombone-keydrive 5.4KB

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