-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccio
executable file
·276 lines (240 loc) · 7.97 KB
/
accio
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/sh
# Copyright 2020 Riccardo Paolo Bestetti <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
read -d '' help <<HEREDOC || true
Commands:
- help: shows this message
- key <volname>: deploy the key for <volname> in /run/cryptsetup-keys.d/<devname>.key
- enroll <volname> <dev>: guided setup for <volname> on device <dev>
- deroll <volname> <dev>: delete enrolled key for <volname> on device <dev>
- verify: verify all configuration files in /etc/cryptsetup-keys.d/
HEREDOC
chal_prompt="Insert the key challenge now, or press Ctrl-C to proceed with a passphrase"
key_prompt="Plug in and activate your hardware token now, or press Enter or Ctrl-C to proceed with a passphrase"
Fluffy="Accio"
fluffy="accio"
slot=
challenge=
config_valid=1
parse_config()
{
IFS="="
while read -r name value; do
case $name in
"slot")
slot=$value
;;
"challenge")
challenge=$value
;;
*)
if [ ! -z $name ]; then
>&2 echo "$Fluffy: unknown option '$name' in $1.$fluffy"
fi
esac
done < "/etc/cryptsetup-keys.d/$1.$fluffy"
unset IFS
if [ ! "$slot" = "1" ] && [ ! "$slot" = "2" ]; then
config_valid=0
slot=
>&2 echo "$Fluffy: invalid slot '$slot' in $1.$fluffy"
fi
case $challenge in
"query"|"file"|"ascii:"*) ;;
*)
config_valid=0
challenge=
>&2 echo "$Fluffy: invalid challenge '$challenge' in $1.$fluffy"
;;
esac
}
prepare_challenge()
{
mkdir -p "/run/cryptsetup-keys.d/"
case $challenge in
"query")
chal=$(systemd-ask-password "$Fluffy: $chal_prompt: ")
if [ ! $? -eq "0" ]; then
return 1
fi
echo -n "$chal" > "/run/cryptsetup-keys.d/$1.challenge"
;;
"file")
if [ ! -f "/etc/cryptsetup-keys.d/$1.challenge" ]; then
>&2 echo "$Fluffy: can't find challenge file for $1"
return 1
fi
cp "/etc/cryptsetup-keys.d/$1.challenge" "/run/cryptsetup-keys.d/$1.challenge"
;;
"ascii:"*)
echo -n "$challenge" | cut -c 7- | tr -d "\n\r" > "/run/cryptsetup-keys.d/$1.challenge"
;;
*)
>&2 echo "$Fluffy: invalid challenge! This is a bug; please report"
return 1
esac
return 0
}
deploy()
{
if [ ! -f "/etc/cryptsetup-keys.d/$1.$fluffy" ]; then
echo "$Fluffy: $1 doesn't have a corresponding $fluffy file, skipping"
exit 0
fi
parse_config $1
if [ "$config_valid" -eq "0" ]; then
>&2 echo "$Fluffy: key deployment failed: invalid configuration"
exit 1
fi
prepare_challenge $1
if [ "$?" -eq "1" ]; then
>&2 echo "$Fluffy: key deployment failed: cannot prepare challenge"
exit 1
fi
systemd-ask-password --no-tty --echo --no-output "$Fluffy: $key_prompt" &
prompt_pid=$!
while kill -0 $prompt_pid >/dev/null 2>&1; do
key=$(cat "/run/cryptsetup-keys.d/$1.challenge" | ykchalresp -$slot -H -i- 2>/dev/null)
if [ "$?" -eq "0" ]; then
break
fi
sleep 1
done
shred -u "/run/cryptsetup-keys.d/$1.challenge"
if [ -z $key ]; then
exit 0
fi
kill -15 $prompt_pid
echo -n "$key" > "/run/cryptsetup-keys.d/$1.key"
}
enroll()
{
if [ ! -f "/etc/cryptsetup-keys.d/$1.$fluffy" ]; then
echo "$Fluffy: $3 failed: $1 doesn't have a corresponding $fluffy file"
exit 1
fi
lsblk $2 >/dev/null 2>&1
if [ ! "$?" -eq "0" ]; then
echo "$Fluffy: $3 failed: device $2 does not exist"
exit 1
fi
parse_config $1
if [ "$config_valid" -eq "0" ]; then
>&2 echo "$Fluffy: $3 failed: invalid configuration"
exit 1
fi
prepare_challenge $1
if [ "$?" -eq "1" ]; then
>&2 echo "$Fluffy: $3 failed: cannot prepare challenge"
exit 1
fi
echo -n "$Fluffy: $3: please insert your Yubikey..."
while true; do
key=$(cat "/run/cryptsetup-keys.d/$1.challenge" | ykchalresp -$slot -H -i- 2>/dev/null)
if [ "$?" -eq "0" ]; then
break
fi
sleep 1
echo -n "."
done
echo " ok!"
echo "$Fluffy: $3: please wait, this is going to take a while"
shred -u "/run/cryptsetup-keys.d/$1.challenge"
echo -n "$key" > "/run/cryptsetup-keys.d/$1.key"
cat "/run/cryptsetup-keys.d/$1.key" | cryptsetup --test-passphrase open $2 >/dev/null 2>&1
res=$?
case $3 in
"enroll")
if [ "$res" -eq "0" ]; then
echo "$Fluffy: enroll cancelled: device $2 can already be unlocked with this key"
shred -u "/run/cryptsetup-keys.d/$1.key"
exit 1
fi
echo "$Fluffy: enroll: cryptsetup will now ask your for an existing passphrase"
cryptsetup luksAddKey $2 "/run/cryptsetup-keys.d/$1.key"
cat "/run/cryptsetup-keys.d/$1.key" | cryptsetup --test-passphrase open $2 >/dev/null 2>&1
if [ ! "$?" -eq "0" ]; then
echo "$Fluffy: enroll failed. This is probably a bug; please report"
shred -u "/run/cryptsetup-keys.d/$1.key"
exit 1
fi
;;
"deroll")
if [ ! "$res" -eq "0" ]; then
echo "$Fluffy: deroll cancelled: device $2 cannot be unlocked with this key"
shred -u "/run/cryptsetup-keys.d/$1.key"
exit 1
fi
cryptsetup luksRemoveKey $2 "/run/cryptsetup-keys.d/$1.key"
cat "/run/cryptsetup-keys.d/$1.key" | cryptsetup --test-passphrase open $2 >/dev/null 2>&1
if [ "$?" -eq "0" ]; then
echo "$Fluffy: deroll failed. This is probably a bug; please report"
shred -u "/run/cryptsetup-keys.d/$1.key"
exit 1
fi
;;
esac
echo "$Fluffy: $3: success!"
shred -u "/run/cryptsetup-keys.d/$1.key"
}
print_help()
{
if [ "$2" -eq "0" ]; then
cat <<HEREDOC
$Fluffy, initrd cryptsetup hook to retrieve keys from hardware tokens
usage: $1 <command> [<params>]
$help
HEREDOC
else
cat <<HEREDOC
$Fluffy: invalid usage
usage: $1 <command> [<params>]
$help
HEREDOC
fi
}
case $1 in
"help"|"--help"|"-h")
print_help $0 0
;;
"key")
deploy $2
;;
"verify")
for f in /etc/cryptsetup-keys.d/*.$fluffy; do
if [ -f "$f" ]; then
vol=$(basename "$f" ".$fluffy")
echo "$Fluffy: verifying $vol..."
parse_config $vol
if [ "$config_valid" -eq "0" ]; then
echo "$Fluffy: verification for $vol failed"
else
if [ "$challenge" = "file" ] &&
[ ! -f "/etc/cryptsetup-keys.d/$vol.challenge" ]; then
echo "$Fluffy: challenge for $vol is 'file' but $vol.challenge does not exist"
echo "$Fluffy: verification for $vol failed"
else
echo "$Fluffy: verification for $vol succeeded"
fi
fi
fi
done
;;
"enroll"|"deroll")
enroll $2 $3 $1
;;
*)
print_help $0 1
esac