-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgnupg-hook
98 lines (89 loc) · 2.94 KB
/
gnupg-hook
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
#!/usr/bin/ash
# This file is part of mkinitcpio-gnupg.
#
# mkinitcpio-gnupg 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.
#
# mkinitcpio-gnupg 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 mkinitcpio-gnupg. If not, see <https://www.gnu.org/licenses/>.
# The hook was greatly inspired from archlinux's encrypt hook from the
# cryptsetup package
run_earlyhook() {
mkdir -m 0700 /tmp/gpg
echo "pinentry-program /usr/bin/pinentry-tty" > /tmp/gpg/gpg-agent.conf
}
run_hook() {
mktmp() {
i=1
while [ -e /tmp/$i ]; do
i=$(($i + 1))
done
echo -n "/tmp/$i"
}
get_file() {
IFS=: read ckdev ckarg1 ckarg2 ckarg3 <<EOF
$1
EOF
if [ "$ckdev" = "rootfs" ];then
echo -n "$ckarg1"
if [ -n "$ckarg2" ]; then
echo -n ":$ckarg2"
fi
elif resolved=$(resolve_device "${ckdev}" ${rootdelay}); then
file=$(mktmp)
case ${ckarg1} in
*[!0-9]*)
# Use a file on the device
# ckarg1 is not numeric: ckarg1=filesystem, ckarg2=path
mkdir /ckey
mount -r -t "$ckarg1" "$resolved" /ckey
dd if="/ckey/$ckarg2" of="$file" >/dev/null 2>&1
umount /ckey
;;
*)
# Read raw data from the block device
# ckarg1 is numeric: ckarg1=offset, ckarg2=length
dd if="$resolved" of="$file" bs=1 skip="$ckarg1" count="$ckarg2" >/dev/null 2>&1
;;
esac
echo -n "$file"
if [ -n "$ckarg3" ]; then
echo -n ":$ckarg3"
fi
fi
}
export GPG_TTY=/dev/tty0
if [ -n "$gpg_import" ]; then
while [ -n "$gpg_import" ]; do
IFS=\; read file gpg_import <<EOF
$gpg_import
EOF
gpg --homedir /tmp/gpg --import $(get_file "$file")
done
fi
if [ -n "$gpg_decrypt" ]; then
gpg --homedir /tmp/gpg --card-status &> /dev/null
while [ -n "$gpg_decrypt" ]; do
IFS=\; read file gpg_decrypt <<EOF
$gpg_decrypt
EOF
IFS=: read in_file out_file <<EOF
$(get_file "$file")
EOF
[ ! -f ${in_file} ] && echo "Could not open ciphered file, aborting."
gpg --homedir /tmp/gpg -o $out_file --decrypt $in_file
done
fi
}
run_cleanuphook()
{
killall -9 gpg-agent scdaemon pcscd &> /dev/null
}
# vim: set ft=sh ts=4 sw=4 et: