This repository has been archived by the owner on Apr 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
nv-hook.sh
79 lines (73 loc) · 2.53 KB
/
nv-hook.sh
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
#!/bin/bash
# vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
# clevis-dracut-tpm
# Author: Greg Stamper ([email protected])
#
# Original source: http://github.com/latchset/clevis
# Original header:
# Copyright (c) 2016 Red Hat, Inc.
# Author: Harald Hoyer <[email protected]>
# Author: Nathaniel McCallum <[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 <http://www.gnu.org/licenses/>.
# METHOD should be one of:
# 0 = tpm_nvread via trousers
# 1 = nv_readvalue
METHOD=0
shopt -s nullglob
for question in /run/systemd/ask-password/ask.*; do
# Check all questions in systemd-ask-password for a socket corresponding
# to an encrypted device
d=
s=
while read line; do
case "$line" in
Id=cryptsetup:*) d="${line##Id=cryptsetup:}";;
Socket=*) s="${line##Socket=}";;
esac
done < "$question"
# If the device isn't cryptsetup or a socket wasn't found, pass
[ -z "$d" -o -z "$s" ] && continue
# If using trousers
if [[ $METHOD -eq 0 ]]; then
# At this point, begin setup for tcsd
mkdir -p /var/lib/tpm
# Create the RAMFS to hold the key in transit
mkdir -p /mnt/ramfs
mount -t tmpfs -o size=1m tmpfs /mnt/ramfs
if [[ $? -ne 0 ]]; then
echo "Mounting RAMFS failed."
exit 2
fi
# Start trousers
tcsd
# Read data from specified index into ramfs
tpm_nvread -i 1 -f /mnt/ramfs/key
# tcsd may sometimes crash on the first read; if so, tpm_nvread returns 255
# restart tcsd and try again, after which the command should succeed.
if [[ $? -ne 0 ]]; then
tcsd
tpm_nvread -i 1 -f /mnt/ramfs/key
fi
# Store key
pt="`cat /mnt/ramfs/key`"
# Unmount the RAMFS
umount /mnt/ramfs
# Else we are using nv_readvalue
else
pt="`nv_readvalue -ix 1 -sz 64 -a`"
fi
# Send key to systemd-ask-password socket
echo -n "+$pt" | nc -U -u --send-only "$s"
done