Debian Codegraph @

debian/initramfs/cryptroot-unlock

raw
1 #!/bin/busybox ash
2
3 # Remotely unlock encrypted volumes.
4 #
5 # Copyright © 2015-2018 Guilhem Moulin <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 set -ue
21 PATH=/sbin:/bin
22
23 TIMEOUT=10
24 PASSFIFO=/lib/cryptsetup/passfifo
25 ASKPASS=/lib/cryptsetup/askpass
26 UNLOCK_ALL=n
27
28 [ -f /lib/cryptsetup/functions ] || return 0
29 . /lib/cryptsetup/functions
30 TABFILE="/cryptroot/crypttab"
31 unset -v IFS
32
33 if [ ! -f "$TABFILE" ] || [ "$TABFILE" -ot "/proc/1" ]; then
34 # Too early, init-top/cryptroot hasn't finished yet
35 echo "Try again later" >&2
36 exit 1
37 fi
38
39 # Print the list of PIDs the executed command of which is $exe.
40 pgrep_exe() {
41 local exe pid
42 exe="$(readlink -f -- "$1" 2>/dev/null)" && [ -f "$exe" ] || return 0
43 ps -eo pid= | while read pid; do
44 [ "$(readlink -f "/proc/$pid/exe")" != "$exe" ] || printf '%d\n' "$pid"
45 done
46 }
47
48 # Return 0 if $pid has a file descriptor pointing to $name, and 1
49 # otherwise.
50 in_fds() {
51 local pid="$1" name fd
52 name="$(readlink -f -- "$2" 2>/dev/null)" && [ -e "$name" ] || return 1
53 for fd in $(find "/proc/$pid/fd" -type l); do
54 [ "$(readlink -f "$fd")" != "$name" ] || return 0
55 done
56 return 1
57 }
58
59 # Print the PID of the askpass process with a file descriptor opened to
60 # /lib/cryptsetup/passfifo.
61 get_askpass_pid() {
62 local pid
63 for pid in $(pgrep_exe "$ASKPASS"); do
64 if in_fds "$pid" "$PASSFIFO"; then
65 echo "$pid"
66 return 0
67 fi
68 done
69 return 1
70 }
71
72 # Print the number of configured crypt devices that have not been unlocked yet.
73 count_locked_devices() {
74 local COUNT=0
75 crypttab_foreach_entry count_locked_devices_callback
76 printf '%d\n' "$COUNT"
77 }
78 count_locked_devices_callback() {
79 dm_blkdevname "$CRYPTTAB_NAME" >/dev/null || COUNT=$(( $COUNT + 1 ))
80 }
81
82 # Wait for askpass, then set $PID (resp. $BIRTH) to the PID (resp.
83 # birth date) of the cryptsetup process with same $CRYPTTAB_NAME.
84 wait_for_prompt() {
85 local pid timer num_locked_devices=-1 n
86
87 # wait for the fifo
88 while :; do
89 n=$(count_locked_devices)
90 if [ $n -eq 0 ]; then
91 # all configured devices have been unlocked, we're done
92 exit 0
93 elif [ $num_locked_devices -lt 0 ] || [ $n -lt $num_locked_devices ]; then
94 # reset $timer if a device was unlocked (for instance using
95 # a keyscript) while we were waiting
96 timer=$(( 10 * $TIMEOUT ))
97 fi
98 num_locked_devices=$n
99
100 if pid=$(get_askpass_pid) && [ -p "$PASSFIFO" ]; then
101 break
102 fi
103
104 usleep 100000
105 timer=$(( $timer - 1 ))
106 if [ $timer -le 0 ]; then
107 echo "Error: Timeout reached while waiting for askpass." >&2
108 exit 1
109 fi
110 done
111
112 # find the cryptsetup process with same $CRYPTTAB_NAME
113 local o v
114 for o in NAME TRIED OPTION_tries; do
115 if v="$(grep -z -m1 "^CRYPTTAB_$o=" "/proc/$pid/environ")"; then
116 eval "CRYPTTAB_$o"="\${v#CRYPTTAB_$o=}"
117 else
118 eval unset -v "CRYPTTAB_$o"
119 fi
120 done
121 if [ -z "${CRYPTTAB_NAME:+x}" ] || [ -z "${CRYPTTAB_TRIED:+x}" ]; then
122 return 1
123 fi
124 if ( ! crypttab_find_entry --quiet "$CRYPTTAB_NAME" ); then
125 # use a subshell to avoid polluting our enironment
126 echo "Error: Refusing to process unknown device $CRYPTTAB_NAME" >&2
127 exit 1
128 fi
129
130 for pid in $(pgrep_exe "/sbin/cryptsetup"); do
131 if grep -Fxqz "CRYPTTAB_NAME=$CRYPTTAB_NAME" "/proc/$pid/environ"; then
132 PID=$pid
133 BIRTH=$(stat -c"%Z" "/proc/$PID" 2>/dev/null) || break
134 return 0
135 fi
136 done
137
138 PID=
139 BIRTH=
140 return 1
141 }
142
143 # Wait until $PID no longer exists or has a birth date greater that
144 # $BIRTH (ie was reallocated). Then return with exit value 0 if
145 # /dev/mapper/$CRYPTTAB_NAME exists, and with exit value 1 if the
146 # maximum number of tries exceeded. Otherwise (if the unlocking
147 # failed), return with value 1.
148 wait_for_answer() {
149 local timer=$(( 10 * $TIMEOUT )) b
150 while [ -d "/proc/$PID" ] && b=$(stat -c"%Z" "/proc/$PID" 2>/dev/null) && [ $b -le $BIRTH ]; do
151 usleep 100000
152 timer=$(( $timer - 1 ))
153 if [ $timer -le 0 ]; then
154 echo "Error: Timeout reached while waiting for PID $PID." >&2
155 exit 1
156 fi
157 done
158
159 if dm_blkdevname "$CRYPTTAB_NAME" >/dev/null; then
160 echo "cryptsetup: $CRYPTTAB_NAME set up successfully" >&2
161 [ "$UNLOCK_ALL" = y ] && return 0 || exit 0
162 elif [ $(( ${CRYPTTAB_TRIED:-0} + 1 )) -ge ${CRYPTTAB_OPTION_tries:-3} ] &&
163 [ ${CRYPTTAB_OPTION_tries:-3} -gt 0 ]; then
164 echo "cryptsetup: maximum number of tries exceeded for $CRYPTTAB_NAME" >&2
165 exit 1
166 else
167 echo "cryptsetup: cryptsetup failed, bad password or options?" >&2
168 return 1
169 fi
170 }
171
172
173 if [ -t 0 ] && [ -x "$ASKPASS" ]; then
174 # interactive mode on a TTY: keep trying until all configured devices have
175 # been unlocked or the maximum number of tries exceeded
176 UNLOCK_ALL=y
177 while :; do
178 # note: if the script is not killed before pivot_root it should
179 # exit on its own once $TIMEOUT is reached
180 if ! wait_for_prompt; then
181 usleep 100000
182 continue
183 fi
184 read -rs -p "Please unlock disk $CRYPTTAB_NAME: "; echo
185 printf '%s' "$REPLY" >"$PASSFIFO"
186 wait_for_answer || true
187 done
188 else
189 # non-interactive mode: slurp the passphrase from stdin and exit
190 wait_for_prompt || exit 1
191 echo "Please unlock disk $CRYPTTAB_NAME"
192 cat >"$PASSFIFO"
193 wait_for_answer || exit 1
194 fi
195
196 # vim: set filetype=sh :