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