| 1 |
if [ "${0#/usr/share/initramfs-tools/hooks/}" != "$0" ] || |
| 2 |
[ "${0#/etc/initramfs-tools/hooks/}" != "$0" ]; then |
| 3 |
# called from an initramfs-tools hook script |
| 4 |
TABFILE="$DESTDIR/cryptroot/crypttab" |
| 5 |
elif [ "${0#/scripts/}" != "$0" ]; then |
| 6 |
# called at initramfs stage from a boot script |
| 7 |
TABFILE="/cryptroot/crypttab" |
| 8 |
CRYPTROOT_COUNT_FILE="/run/cryptroot.initrd.cnt" |
| 9 |
else |
| 10 |
TABFILE="${TABFILE-/etc/crypttab}" |
| 11 |
fi |
| 12 |
export DM_DEFAULT_NAME_MANGLING_MODE=hex # for dmsetup(8) |
| 13 |
|
| 14 |
# Logging helpers. Send the argument list to plymouth(1), or fold it |
| 15 |
# and print it to the standard error. |
| 16 |
cryptsetup_message() { |
| 17 |
local IFS=' ' |
| 18 |
if [ "${0#/scripts/}" != "$0" ] && [ -x /bin/plymouth ] && plymouth --ping; then |
| 19 |
plymouth message --text="cryptsetup: $*" |
| 20 |
elif [ ${#*} -lt 70 ]; then |
| 21 |
echo "cryptsetup: $*" >&2 |
| 22 |
else |
| 23 |
# use busybox's fold(1) and sed(1) at initramfs stage |
| 24 |
echo "cryptsetup: $*" | fold -s | sed '1! s/^/ /' >&2 |
| 25 |
fi |
| 26 |
return 0 |
| 27 |
} |
| 28 |
|
| 29 |
# crypttab_parse_options([--export], [--quiet], [--missing-path={ignore|warn|fail}]) |
| 30 |
# Parse $_CRYPTTAB_OPTIONS, a comma-separated option string from the |
| 31 |
# crypttab(5) 4th column, and sets corresponding variables |
| 32 |
# CRYPTTAB_OPTION_<option>=<value> (which are added to the environment |
| 33 |
# if --export is set). If --path-exists isn't set to "ignore" (the |
| 34 |
# default), then options taking a file name, such as header=<path>, |
| 35 |
# need to point to an existing path, otherwise a warning is printed; |
| 36 |
# and an error is raised if the value is set to "fail". |
| 37 |
# For error and warning messages, CRYPTTAB_NAME, (resp. CRYPTTAB_KEY) |
| 38 |
# should be set to the (unmangled) mapped device name (resp. key |
| 39 |
# file). |
| 40 |
# Moreover CRYPTTAB_TYPE is set the device type. |
| 41 |
# Return 1 on parsing error, 0 otherwise (incl. if unknown options |
| 42 |
# were encountered). |
| 43 |
crypttab_parse_options() { |
| 44 |
local quiet="n" export="n" missing_path="ignore" |
| 45 |
while [ $# -gt 0 ]; do |
| 46 |
case "$1" in |
| 47 |
--quiet) quiet="y";; |
| 48 |
--export) export="y";; |
| 49 |
--missing-path=*) missing_path="${1#--missing-path=}";; |
| 50 |
*) cryptsetup_message "WARNING: crypttab_parse_options(): unknown option $1" |
| 51 |
esac |
| 52 |
shift |
| 53 |
done |
| 54 |
|
| 55 |
local IFS=',' x OPTION VALUE |
| 56 |
CRYPTTAB_TYPE="" |
| 57 |
unset -v CRYPTTAB_OPTION_cipher \ |
| 58 |
CRYPTTAB_OPTION_size \ |
| 59 |
CRYPTTAB_OPTION_sector_size \ |
| 60 |
CRYPTTAB_OPTION_hash \ |
| 61 |
CRYPTTAB_OPTION_offset \ |
| 62 |
CRYPTTAB_OPTION_skip \ |
| 63 |
CRYPTTAB_OPTION_verify \ |
| 64 |
CRYPTTAB_OPTION_readonly \ |
| 65 |
CRYPTTAB_OPTION_discard \ |
| 66 |
CRYPTTAB_OPTION_plain \ |
| 67 |
CRYPTTAB_OPTION_luks \ |
| 68 |
CRYPTTAB_OPTION_tcrypt \ |
| 69 |
CRYPTTAB_OPTION_veracrypt \ |
| 70 |
CRYPTTAB_OPTION_bitlk \ |
| 71 |
CRYPTTAB_OPTION_fvault2 \ |
| 72 |
CRYPTTAB_OPTION_swap \ |
| 73 |
CRYPTTAB_OPTION_tmp \ |
| 74 |
CRYPTTAB_OPTION_check \ |
| 75 |
CRYPTTAB_OPTION_checkargs \ |
| 76 |
CRYPTTAB_OPTION_tries \ |
| 77 |
CRYPTTAB_OPTION_initramfs \ |
| 78 |
CRYPTTAB_OPTION_noearly \ |
| 79 |
CRYPTTAB_OPTION_noauto \ |
| 80 |
CRYPTTAB_OPTION_loud \ |
| 81 |
CRYPTTAB_OPTION_quiet \ |
| 82 |
CRYPTTAB_OPTION_keyscript \ |
| 83 |
CRYPTTAB_OPTION_keyslot \ |
| 84 |
CRYPTTAB_OPTION_header \ |
| 85 |
CRYPTTAB_OPTION_tcrypthidden \ |
| 86 |
CRYPTTAB_OPTION_tcryptsystem \ |
| 87 |
CRYPTTAB_OPTION_same_cpu_crypt \ |
| 88 |
CRYPTTAB_OPTION_submit_from_crypt_cpus \ |
| 89 |
CRYPTTAB_OPTION_no_read_workqueue \ |
| 90 |
CRYPTTAB_OPTION_no_write_workqueue |
| 91 |
# use $_CRYPTTAB_OPTIONS not $CRYPTTAB_OPTIONS as options values may |
| 92 |
# contain '\054' which is decoded to ',' in the latter |
| 93 |
for x in $_CRYPTTAB_OPTIONS; do |
| 94 |
OPTION="${x%%=*}" |
| 95 |
VALUE="${x#*=}" |
| 96 |
if [ "$x" = "$OPTION" ]; then |
| 97 |
unset -v VALUE |
| 98 |
else |
| 99 |
VALUE="$(printf '%b' "$VALUE")" |
| 100 |
fi |
| 101 |
if ! crypttab_validate_option; then |
| 102 |
if [ "$quiet" = "n" ]; then |
| 103 |
cryptsetup_message "ERROR: $CRYPTTAB_NAME: invalid value for '${x%%=*}' option, skipping" |
| 104 |
fi |
| 105 |
return 1 |
| 106 |
elif [ -z "${OPTION+x}" ]; then |
| 107 |
continue |
| 108 |
fi |
| 109 |
if [ "$export" = "y" ]; then |
| 110 |
export "CRYPTTAB_OPTION_$OPTION"="${VALUE-yes}" |
| 111 |
else |
| 112 |
eval "CRYPTTAB_OPTION_$OPTION"='${VALUE-yes}' |
| 113 |
fi |
| 114 |
done |
| 115 |
IFS=" " |
| 116 |
|
| 117 |
if ! _get_crypt_type; then # set CRYPTTAB_TYPE to the type of crypt device |
| 118 |
CRYPTTAB_TYPE="plain" |
| 119 |
if [ "$quiet" = "n" ]; then |
| 120 |
cryptsetup_message "WARNING: $CRYPTTAB_NAME: couldn't determine device type," \ |
| 121 |
"assuming default ($CRYPTTAB_TYPE)." |
| 122 |
fi |
| 123 |
fi |
| 124 |
|
| 125 |
if [ "$quiet" = "n" ] && [ -n "${CRYPTTAB_OPTION_header+x}" ] && [ "$CRYPTTAB_TYPE" != "luks" ]; then |
| 126 |
cryptsetup_message "WARNING: $CRYPTTAB_NAME: Headers are only supported for LUKS devices." |
| 127 |
fi |
| 128 |
if [ "$CRYPTTAB_TYPE" = "plain" ]; then |
| 129 |
# the compiled-in default for these are subject to change |
| 130 |
options='cipher size' |
| 131 |
if [ -n "${CRYPTTAB_OPTION_keyscript+x}" ] || [ "$CRYPTTAB_KEY" = "none" ]; then |
| 132 |
options="$options hash" # --hash is being ignored in plain mode with keyfile specified |
| 133 |
fi |
| 134 |
for o in $options; do |
| 135 |
if [ "$quiet" = "n" ] && eval [ -z "\${CRYPTTAB_OPTION_$o+x}" ]; then |
| 136 |
cryptsetup_message "WARNING: Option '$o' missing in crypttab for plain dm-crypt" \ |
| 137 |
"mapping $CRYPTTAB_NAME. Please read /usr/share/doc/cryptsetup-initramfs/README.initramfs.gz and" \ |
| 138 |
"add the correct '$o' option to your crypttab(5)." |
| 139 |
fi |
| 140 |
done |
| 141 |
fi |
| 142 |
} |
| 143 |
|
| 144 |
# crypttab_validate_option() |
| 145 |
# Validate $OPTION=$VALUE (or flag $OPTION if VALUE is unset). return |
| 146 |
# 1 on error, unsets OPTION for unknown or useless options. |
| 147 |
crypttab_validate_option() { |
| 148 |
# option aliases |
| 149 |
case "$OPTION" in |
| 150 |
read-only) OPTION="readonly";; |
| 151 |
key-slot) OPTION="keyslot";; |
| 152 |
tcrypt-hidden) OPTION="tcrypthidden";; |
| 153 |
tcrypt-veracrypt) OPTION="veracrypt";; |
| 154 |
tcrypt-system) OPTION="tcryptsystem";; |
| 155 |
esac |
| 156 |
|
| 157 |
# sanitize the option name so CRYPTTAB_OPTION_$OPTION is a valid variable name |
| 158 |
local o="$OPTION" |
| 159 |
case "$o" in |
| 160 |
keyfile-offset) OPTION="keyfile_offset";; |
| 161 |
keyfile-size) OPTION="keyfile_size";; |
| 162 |
sector-size) OPTION="sector_size";; |
| 163 |
same-cpu-crypt) OPTION="same_cpu_crypt";; |
| 164 |
submit-from-crypt-cpus) OPTION="submit_from_crypt_cpus";; |
| 165 |
no-read-workqueue) OPTION="no_read_workqueue";; |
| 166 |
no-write-workqueue) OPTION="no_write_workqueue";; |
| 167 |
esac |
| 168 |
|
| 169 |
case "$o" in |
| 170 |
# value must be a non-empty string |
| 171 |
cipher|hash) |
| 172 |
[ -n "${VALUE:+x}" ] || return 1 |
| 173 |
;; |
| 174 |
# value must be a non-empty string, and an existing path if --missing-path is set |
| 175 |
header) |
| 176 |
[ -n "${VALUE:+x}" ] || return 1 |
| 177 |
if [ "$missing_path" != "ignore" ]; then |
| 178 |
if [ ! -e "$VALUE" ]; then |
| 179 |
cryptsetup_message "WARNING: $CRYPTTAB_NAME: $VALUE does not exist"; |
| 180 |
[ "$missing_path" = "warn" ] || return 1 |
| 181 |
fi |
| 182 |
fi |
| 183 |
;; |
| 184 |
# numeric options >0 |
| 185 |
size|keyfile-size|sector-size) |
| 186 |
if ! printf '%s' "${VALUE-}" | grep -Exq "0*[1-9][0-9]*"; then |
| 187 |
return 1 |
| 188 |
fi |
| 189 |
;; |
| 190 |
# numeric options >=0 |
| 191 |
offset|skip|tries|keyslot|keyfile-offset) |
| 192 |
if ! printf '%s' "${VALUE-}" | grep -Exq "[0-9]+"; then |
| 193 |
return 1 |
| 194 |
fi |
| 195 |
;; |
| 196 |
tmp) |
| 197 |
if [ -z "${VALUE+x}" ]; then |
| 198 |
VALUE="ext4" # 'tmp flag' |
| 199 |
elif [ -z "$VALUE" ]; then |
| 200 |
return 1 |
| 201 |
fi |
| 202 |
;; |
| 203 |
check) |
| 204 |
if [ -z "${VALUE+x}" ]; then |
| 205 |
if [ -n "${CRYPTDISKS_CHECK-}" ]; then |
| 206 |
VALUE="$CRYPTDISKS_CHECK" |
| 207 |
else |
| 208 |
unset -v OPTION |
| 209 |
return 0 |
| 210 |
fi |
| 211 |
fi |
| 212 |
if [ "${VALUE#/}" = "$VALUE" ]; then |
| 213 |
VALUE="/lib/cryptsetup/checks/$VALUE" |
| 214 |
fi |
| 215 |
if [ ! -x "$VALUE" ] || [ ! -f "$VALUE" ]; then |
| 216 |
return 1 |
| 217 |
fi |
| 218 |
;; |
| 219 |
checkargs) |
| 220 |
[ -n "${VALUE+x}" ] || return 1 # must have a value (possibly empty) |
| 221 |
;; |
| 222 |
keyscript) |
| 223 |
[ -n "${VALUE:+x}" ] || return 1 # must have a value |
| 224 |
if [ "${VALUE#/}" = "$VALUE" ]; then |
| 225 |
VALUE="/lib/cryptsetup/scripts/$VALUE" |
| 226 |
fi |
| 227 |
if [ ! -x "$VALUE" ] || [ ! -f "$VALUE" ]; then |
| 228 |
return 1 |
| 229 |
fi |
| 230 |
;; |
| 231 |
# and now the flags |
| 232 |
verify) ;; |
| 233 |
loud) ;; |
| 234 |
quiet) ;; |
| 235 |
initramfs) ;; |
| 236 |
noearly) ;; |
| 237 |
noauto) ;; |
| 238 |
readonly) ;; |
| 239 |
discard) ;; |
| 240 |
plain) ;; |
| 241 |
luks) ;; |
| 242 |
swap) ;; |
| 243 |
tcrypt) ;; |
| 244 |
veracrypt) ;; |
| 245 |
tcrypthidden) ;; |
| 246 |
tcryptsystem) ;; |
| 247 |
bitlk) ;; |
| 248 |
fvault2) ;; |
| 249 |
same-cpu-crypt) ;; |
| 250 |
submit-from-crypt-cpus) ;; |
| 251 |
no-read-workqueue) ;; |
| 252 |
no-write-workqueue) ;; |
| 253 |
x-initrd.attach) |
| 254 |
unset -v OPTION ;; # ignored, cf. #1072058 |
| 255 |
*) |
| 256 |
if [ "${quiet:-n}" = "n" ]; then |
| 257 |
cryptsetup_message "WARNING: $CRYPTTAB_NAME: ignoring unknown option '$o'"; |
| 258 |
fi |
| 259 |
unset -v OPTION |
| 260 |
;; |
| 261 |
esac |
| 262 |
} |
| 263 |
|
| 264 |
# crypttab_resolve_source() |
| 265 |
# Resolve the CRYPTTAB_SOURCE variable, containing value of the second |
| 266 |
# field of a crypttab(5)-like file. |
| 267 |
# On error (non-existing source), CRYPTTAB_SOURCE is not changed and 1 |
| 268 |
# is returned. |
| 269 |
crypttab_resolve_source() { |
| 270 |
# return immediately if source is a regular file |
| 271 |
[ ! -f "$CRYPTTAB_SOURCE" ] || return 0 |
| 272 |
# otherwise resolve the block device specification |
| 273 |
local dev="$CRYPTTAB_SOURCE" |
| 274 |
dev="$(_resolve_device_spec "$dev")" && CRYPTTAB_SOURCE="$dev" || return 1 |
| 275 |
} |
| 276 |
|
| 277 |
# run_keyscript($tried_count) |
| 278 |
# exec()'ute `$CRYPTTAB_OPTION_keyscript "$CRYPTTAB_KEY"`. |
| 279 |
# If $CRYPTTAB_OPTION_keyscript is unset or null and $CRYPTTAB_KEY is |
| 280 |
# "none" (meaning the passphrase is to be read interactively from the |
| 281 |
# console), then use `/lib/cryptsetup/askpass` as keyscript with a |
| 282 |
# suitable prompt message instead. |
| 283 |
# Since the shell process is replaced with the $CRYPTTAB_OPTION_keyscript |
| 284 |
# program, run_keyscript() must be used on the left-hand side of a |
| 285 |
# pipe, or similar. |
| 286 |
run_keyscript() { |
| 287 |
local keyscript keyscriptarg="$CRYPTTAB_KEY" |
| 288 |
export CRYPTTAB_NAME CRYPTTAB_SOURCE CRYPTTAB_OPTIONS |
| 289 |
export _CRYPTTAB_NAME _CRYPTTAB_SOURCE _CRYPTTAB_OPTIONS |
| 290 |
export CRYPTTAB_TRIED="$1" |
| 291 |
|
| 292 |
if [ -n "${CRYPTTAB_OPTION_keyscript+x}" ] && \ |
| 293 |
[ "$CRYPTTAB_OPTION_keyscript" != "/lib/cryptsetup/askpass" ]; then |
| 294 |
# 'keyscript' option is present: export its argument as $CRYPTTAB_KEY |
| 295 |
export CRYPTTAB_KEY _CRYPTTAB_KEY |
| 296 |
keyscript="$CRYPTTAB_OPTION_keyscript" |
| 297 |
elif [ "$keyscriptarg" = "none" ]; then |
| 298 |
# don't export the prompt message as CRYPTTAB_KEY |
| 299 |
keyscript="/lib/cryptsetup/askpass" |
| 300 |
keyscriptarg="Please unlock disk $CRYPTTAB_NAME: " |
| 301 |
fi |
| 302 |
|
| 303 |
exec "$keyscript" "$keyscriptarg" |
| 304 |
} |
| 305 |
|
| 306 |
# _get_crypt_type() |
| 307 |
# Set CRYPTTAB_TYPE to the mapping type, depending on its |
| 308 |
# $CRYPTTAB_OPTION_<option> values |
| 309 |
# Return a non-zero status if the mapping couldn't be determined |
| 310 |
_get_crypt_type() { |
| 311 |
local s="$CRYPTTAB_SOURCE" t="" blk_t |
| 312 |
|
| 313 |
if [ "${CRYPTTAB_OPTION_luks-}" = "yes" ]; then |
| 314 |
t="luks" |
| 315 |
elif [ "${CRYPTTAB_OPTION_tcrypt-}" = "yes" ]; then |
| 316 |
t="tcrypt" |
| 317 |
elif [ "${CRYPTTAB_OPTION_plain-}" = "yes" ]; then |
| 318 |
t="plain" |
| 319 |
elif [ "${CRYPTTAB_OPTION_bitlk-}" = "yes" ]; then |
| 320 |
t="bitlk" |
| 321 |
elif [ "${CRYPTTAB_OPTION_fvault2-}" = "yes" ]; then |
| 322 |
t="fvault2" |
| 323 |
elif [ -n "${CRYPTTAB_OPTION_header+x}" ]; then |
| 324 |
# detached headers are only supported for LUKS devices |
| 325 |
if [ -e "$CRYPTTAB_OPTION_header" ] && /sbin/cryptsetup isLuks -- "$CRYPTTAB_OPTION_header"; then |
| 326 |
t="luks" |
| 327 |
fi |
| 328 |
elif [ -f "$s" ] || s="$(_resolve_device_spec "$CRYPTTAB_SOURCE")"; then |
| 329 |
if /sbin/cryptsetup isLuks -- "$s"; then |
| 330 |
t="luks" |
| 331 |
elif blk_t="$(blkid -s TYPE -o value -- "$s")" && [ "$blk_t" = "BitLocker" ]; then |
| 332 |
t="bitlk" |
| 333 |
fi |
| 334 |
fi |
| 335 |
|
| 336 |
[ -n "$t" ] || return 1 |
| 337 |
CRYPTTAB_TYPE="$t" |
| 338 |
} |
| 339 |
|
| 340 |
# unlock_mapping([$keyfile]) |
| 341 |
# Run cryptsetup(8) with suitable options and arguments to unlock |
| 342 |
# $CRYPTTAB_SOURCE and setup dm-crypt managed device-mapper mapping |
| 343 |
# $CRYPTTAB_NAME. |
| 344 |
unlock_mapping() { |
| 345 |
local keyfile="${1:--}" |
| 346 |
|
| 347 |
if [ "$CRYPTTAB_TYPE" = "luks" ] || [ "$CRYPTTAB_TYPE" = "tcrypt" ]; then |
| 348 |
# ignored for LUKS and TCRYPT devices |
| 349 |
unset -v CRYPTTAB_OPTION_cipher \ |
| 350 |
CRYPTTAB_OPTION_size \ |
| 351 |
CRYPTTAB_OPTION_hash \ |
| 352 |
CRYPTTAB_OPTION_offset \ |
| 353 |
CRYPTTAB_OPTION_skip |
| 354 |
fi |
| 355 |
if [ "$CRYPTTAB_TYPE" = "plain" ] || [ "$CRYPTTAB_TYPE" = "tcrypt" ]; then |
| 356 |
unset -v CRYPTTAB_OPTION_keyfile_size |
| 357 |
fi |
| 358 |
if [ "$CRYPTTAB_TYPE" = "tcrypt" ]; then |
| 359 |
# ignored for TCRYPT devices |
| 360 |
unset -v CRYPTTAB_OPTION_keyfile_offset |
| 361 |
else |
| 362 |
# ignored for non-TCRYPT devices |
| 363 |
unset -v CRYPTTAB_OPTION_veracrypt CRYPTTAB_OPTION_tcrypthidden CRYPTTAB_OPTION_tcryptsystem |
| 364 |
fi |
| 365 |
|
| 366 |
if [ "$CRYPTTAB_TYPE" != "luks" ]; then |
| 367 |
# ignored for non-LUKS devices |
| 368 |
unset -v CRYPTTAB_OPTION_keyslot |
| 369 |
fi |
| 370 |
|
| 371 |
/sbin/cryptsetup -T1 \ |
| 372 |
${CRYPTTAB_OPTION_header:+--header="$CRYPTTAB_OPTION_header"} \ |
| 373 |
${CRYPTTAB_OPTION_cipher:+--cipher="$CRYPTTAB_OPTION_cipher"} \ |
| 374 |
${CRYPTTAB_OPTION_size:+--key-size="$CRYPTTAB_OPTION_size"} \ |
| 375 |
${CRYPTTAB_OPTION_sector_size:+--sector-size="$CRYPTTAB_OPTION_sector_size"} \ |
| 376 |
${CRYPTTAB_OPTION_hash:+--hash="$CRYPTTAB_OPTION_hash"} \ |
| 377 |
${CRYPTTAB_OPTION_offset:+--offset="$CRYPTTAB_OPTION_offset"} \ |
| 378 |
${CRYPTTAB_OPTION_skip:+--skip="$CRYPTTAB_OPTION_skip"} \ |
| 379 |
${CRYPTTAB_OPTION_verify:+--verify-passphrase} \ |
| 380 |
${CRYPTTAB_OPTION_readonly:+--readonly} \ |
| 381 |
${CRYPTTAB_OPTION_discard:+--allow-discards} \ |
| 382 |
${CRYPTTAB_OPTION_veracrypt:+--veracrypt} \ |
| 383 |
${CRYPTTAB_OPTION_keyslot:+--key-slot="$CRYPTTAB_OPTION_keyslot"} \ |
| 384 |
${CRYPTTAB_OPTION_tcrypthidden:+--tcrypt-hidden} \ |
| 385 |
${CRYPTTAB_OPTION_tcryptsystem:+--tcrypt-system} \ |
| 386 |
${CRYPTTAB_OPTION_keyfile_size:+--keyfile-size="$CRYPTTAB_OPTION_keyfile_size"} \ |
| 387 |
${CRYPTTAB_OPTION_keyfile_offset:+--keyfile-offset="$CRYPTTAB_OPTION_keyfile_offset"} \ |
| 388 |
${CRYPTTAB_OPTION_same_cpu_crypt:+--perf-same_cpu_crypt} \ |
| 389 |
${CRYPTTAB_OPTION_submit_from_crypt_cpus:+--perf-submit_from_crypt_cpus} \ |
| 390 |
${CRYPTTAB_OPTION_no_read_workqueue:+--perf-no_read_workqueue} \ |
| 391 |
${CRYPTTAB_OPTION_no_write_workqueue:+--perf-no_write_workqueue} \ |
| 392 |
--type="$CRYPTTAB_TYPE" --key-file="$keyfile" \ |
| 393 |
open -- "$CRYPTTAB_SOURCE" "$CRYPTTAB_NAME" |
| 394 |
} |
| 395 |
|
| 396 |
# resume_mapping([$keyfile]) |
| 397 |
# Run cryptsetup(8) with suitable options and arguments to resume |
| 398 |
# $CRYPTTAB_NAME. |
| 399 |
resume_mapping() { |
| 400 |
local keyfile="${1:--}" |
| 401 |
|
| 402 |
/sbin/cryptsetup -T1 \ |
| 403 |
${CRYPTTAB_OPTION_header:+--header="$CRYPTTAB_OPTION_header"} \ |
| 404 |
${CRYPTTAB_OPTION_keyslot:+--key-slot="$CRYPTTAB_OPTION_keyslot"} \ |
| 405 |
${CRYPTTAB_OPTION_keyfile_size:+--keyfile-size="$CRYPTTAB_OPTION_keyfile_size"} \ |
| 406 |
${CRYPTTAB_OPTION_keyfile_offset:+--keyfile-offset="$CRYPTTAB_OPTION_keyfile_offset"} \ |
| 407 |
--type="$CRYPTTAB_TYPE" --key-file="$keyfile" \ |
| 408 |
luksResume "$CRYPTTAB_NAME" |
| 409 |
} |
| 410 |
|
| 411 |
# resume_device($device) |
| 412 |
# Resume $device with endless retries. Used by cryptsetup-suspend-wrapper. |
| 413 |
resume_device() { |
| 414 |
local device="$1" |
| 415 |
# check if device is really suspended |
| 416 |
if [ "$(dmsetup info -c --noheadings -o suspended -- "$device" 2>/dev/null)" != "Suspended" ]; then |
| 417 |
cryptsetup_message "ERROR: $device: device was not suspendend" |
| 418 |
return 1 |
| 419 |
fi |
| 420 |
|
| 421 |
if ! crypttab_find_entry "$device" || ! crypttab_parse_options --quiet; then |
| 422 |
cryptsetup_message "ERROR: $device: not found in $TABFILE" |
| 423 |
return 1 |
| 424 |
fi |
| 425 |
|
| 426 |
if [ "$CRYPTTAB_TYPE" != "luks" ]; then |
| 427 |
cryptsetup_message "ERROR: $CRYPTTAB_NAME: unable to resume non-LUKS device" |
| 428 |
return 1 |
| 429 |
fi |
| 430 |
|
| 431 |
# Loop endlessly until the resume command succeeded |
| 432 |
while true; do |
| 433 |
if [ -z "${CRYPTTAB_OPTION_keyscript+x}" ] && [ "$CRYPTTAB_KEY" != "none" ]; then |
| 434 |
resume_mapping "$CRYPTTAB_KEY" && break || true |
| 435 |
else |
| 436 |
run_keyscript 1 | resume_mapping && break || true |
| 437 |
fi |
| 438 |
done |
| 439 |
} |
| 440 |
|
| 441 |
# crypttab_key_check() |
| 442 |
# Sanity checks for keyfile $CRYPTTAB_KEY. CRYPTTAB_NAME and |
| 443 |
# CRYPTTAB_OPTION_<option> must be set appropriately. |
| 444 |
crypttab_key_check() { |
| 445 |
if [ ! -f "$CRYPTTAB_KEY" ] && [ ! -b "$CRYPTTAB_KEY" ] && [ ! -c "$CRYPTTAB_KEY" ] ; then |
| 446 |
cryptsetup_message "WARNING: $CRYPTTAB_NAME: keyfile '$CRYPTTAB_KEY' not found" |
| 447 |
return 0 |
| 448 |
fi |
| 449 |
|
| 450 |
if [ "$CRYPTTAB_KEY" = "/dev/random" ] || [ "$CRYPTTAB_KEY" = "/dev/urandom" ]; then |
| 451 |
if [ -n "${CRYPTTAB_OPTION_luks+x}" ] || [ -n "${CRYPTTAB_OPTION_tcrypt+x}" ]; then |
| 452 |
cryptsetup_message "WARNING: $CRYPTTAB_NAME: has random data as key" |
| 453 |
return 1 |
| 454 |
else |
| 455 |
return 0 |
| 456 |
fi |
| 457 |
fi |
| 458 |
|
| 459 |
local mode="$(stat -L -c"%04a" -- "$CRYPTTAB_KEY")" |
| 460 |
if [ $(stat -L -c"%u" -- "$CRYPTTAB_KEY") -ne 0 ] || [ "${mode%00}" = "$mode" ]; then |
| 461 |
cryptsetup_message "WARNING: $CRYPTTAB_NAME: key file $CRYPTTAB_KEY has" \ |
| 462 |
"insecure ownership, see /usr/share/doc/cryptsetup/README.Debian.gz." |
| 463 |
fi |
| 464 |
} |
| 465 |
|
| 466 |
# _resolve_device_spec($spec) |
| 467 |
# Resolve LABEL=<label>, UUID=<uuid>, PARTUUID=<partuuid> and |
| 468 |
# PARTLABEL=<partlabel> to a block special device. If $spec is |
| 469 |
# already a (link to a block special device) then it is echoed as is. |
| 470 |
# Return 1 if $spec doesn't correspond to a block special device. |
| 471 |
_resolve_device_spec() { |
| 472 |
local spec="$1" |
| 473 |
case "$spec" in |
| 474 |
UUID=*|LABEL=*|PARTUUID=*|PARTLABEL=*) |
| 475 |
# don't use /dev/disk/by-label/... to avoid gessing udev mangling |
| 476 |
spec="$(blkid -l -t "$spec" -o device)" || spec= |
| 477 |
;; |
| 478 |
esac |
| 479 |
[ -b "$spec" ] && printf '%s\n' "$spec" || return 1 |
| 480 |
} |
| 481 |
|
| 482 |
# dm_blkdevname($name) |
| 483 |
# Print the mapped device name, or return 1 if the the device doesn't exist. |
| 484 |
dm_blkdevname() { |
| 485 |
local name="$1" dev |
| 486 |
# /dev/mapper/$name isn't reliable due to udev mangling |
| 487 |
if dev="$(dmsetup info -c --noheadings -o blkdevname -- "$name" 2>/dev/null)" && |
| 488 |
[ -n "$dev" ] && [ -b "/dev/$dev" ]; then |
| 489 |
echo "/dev/$dev" |
| 490 |
return 0 |
| 491 |
else |
| 492 |
return 1 |
| 493 |
fi |
| 494 |
} |
| 495 |
|
| 496 |
# crypttab_find_entry([--quiet], $target) |
| 497 |
# Search in the crypttab(5) for the given $target, and sets the |
| 498 |
# variables CRYPTTAB_NAME, CRYPTTAB_SOURCE, CRYPTTAB_KEY and |
| 499 |
# CRYPTTAB_OPTIONS accordingly. (In addition _CRYPTTAB_NAME, |
| 500 |
# _CRYPTTAB_SOURCE, _CRYPTTAB_KEY and _CRYPTTAB_OPTIONS are set to the |
| 501 |
# unmangled values before decoding the escape sequence.) If there are |
| 502 |
# duplicates then only the first match is considered. |
| 503 |
# Return 0 if a match is found, and 1 otherwise. |
| 504 |
crypttab_find_entry() { |
| 505 |
local target="$1" quiet="n" IFS |
| 506 |
if [ "$target" = "--quiet" ] && [ $# -eq 2 ]; then |
| 507 |
quiet="y" |
| 508 |
target="$2" |
| 509 |
fi |
| 510 |
|
| 511 |
if [ -f "$TABFILE" ]; then |
| 512 |
while IFS=" " read -r _CRYPTTAB_NAME _CRYPTTAB_SOURCE _CRYPTTAB_KEY _CRYPTTAB_OPTIONS; do |
| 513 |
if [ "${_CRYPTTAB_NAME#\#}" != "$_CRYPTTAB_NAME" ] || [ -z "$_CRYPTTAB_NAME" ]; then |
| 514 |
# ignore comments and empty lines |
| 515 |
continue |
| 516 |
fi |
| 517 |
|
| 518 |
# unmangle names |
| 519 |
CRYPTTAB_NAME="$(printf '%b' "$_CRYPTTAB_NAME")" |
| 520 |
if [ -z "$_CRYPTTAB_SOURCE" ] || [ -z "$_CRYPTTAB_KEY" ]; then |
| 521 |
cryptsetup_message "WARNING: '$CRYPTTAB_NAME' is missing some arguments, see crypttab(5)" |
| 522 |
continue |
| 523 |
elif [ "$CRYPTTAB_NAME" = "$target" ]; then |
| 524 |
CRYPTTAB_SOURCE="$( printf '%b' "$_CRYPTTAB_SOURCE" )" |
| 525 |
CRYPTTAB_KEY="$( printf '%b' "$_CRYPTTAB_KEY" )" |
| 526 |
CRYPTTAB_OPTIONS="$(printf '%b' "$_CRYPTTAB_OPTIONS")" |
| 527 |
return 0 |
| 528 |
fi |
| 529 |
done <"$TABFILE" |
| 530 |
fi |
| 531 |
|
| 532 |
if [ "$quiet" = "n" ]; then |
| 533 |
cryptsetup_message "WARNING: target '$target' not found in $TABFILE" |
| 534 |
fi |
| 535 |
return 1 |
| 536 |
} |
| 537 |
|
| 538 |
# crypttab_foreach_entry($callback) |
| 539 |
# Iterate through the crypttab(5) and run the given $callback for each |
| 540 |
# entry found. Variables CRYPTTAB_NAME, CRYPTTAB_SOURCE, CRYPTTAB_KEY |
| 541 |
# and CRYPTTAB_OPTIONS are set accordingly and available to the |
| 542 |
# $callback. (In addition _CRYPTTAB_NAME, _CRYPTTAB_SOURCE, |
| 543 |
# _CRYPTTAB_KEY and _CRYPTTAB_OPTIONS are set to the original values |
| 544 |
# before decoding the escape sequence.) |
| 545 |
# Return 0 if a match is found, and 1 otherwise. |
| 546 |
crypttab_foreach_entry() { |
| 547 |
local callback="$1" IFS |
| 548 |
local _CRYPTTAB_NAME _CRYPTTAB_SOURCE _CRYPTTAB_KEY _CRYPTTAB_OPTIONS \ |
| 549 |
CRYPTTAB_NAME CRYPTTAB_SOURCE CRYPTTAB_KEY CRYPTTAB_OPTIONS |
| 550 |
|
| 551 |
[ -f "$TABFILE" ] || return |
| 552 |
while IFS=" " read -r _CRYPTTAB_NAME _CRYPTTAB_SOURCE _CRYPTTAB_KEY _CRYPTTAB_OPTIONS <&9; do |
| 553 |
if [ "${_CRYPTTAB_NAME#\#}" != "$_CRYPTTAB_NAME" ] || [ -z "$_CRYPTTAB_NAME" ]; then |
| 554 |
# ignore comments and empty lines |
| 555 |
continue |
| 556 |
fi |
| 557 |
|
| 558 |
# unmangle names |
| 559 |
CRYPTTAB_NAME="$(printf '%b' "$_CRYPTTAB_NAME")" |
| 560 |
|
| 561 |
if [ -z "$_CRYPTTAB_SOURCE" ] || [ -z "$_CRYPTTAB_KEY" ]; then |
| 562 |
cryptsetup_message "WARNING: '$CRYPTTAB_NAME' is missing some arguments, see crypttab(5)" |
| 563 |
continue |
| 564 |
fi |
| 565 |
|
| 566 |
CRYPTTAB_SOURCE="$( printf '%b' "$_CRYPTTAB_SOURCE" )" |
| 567 |
CRYPTTAB_KEY="$( printf '%b' "$_CRYPTTAB_KEY" )" |
| 568 |
CRYPTTAB_OPTIONS="$(printf '%b' "$_CRYPTTAB_OPTIONS")" |
| 569 |
|
| 570 |
"$callback" 9<&- |
| 571 |
done 9<"$TABFILE" |
| 572 |
} |
| 573 |
|
| 574 |
# _device_uuid($device) |
| 575 |
# Print the UUID attribute of given block special $device. Return 0 |
| 576 |
# on success, 1 on error. |
| 577 |
_device_uuid() { |
| 578 |
local device="$1" uuid |
| 579 |
if uuid="$(blkid -s UUID -o value -- "$device")" && [ -n "$uuid" ]; then |
| 580 |
printf '%s\n' "$uuid" |
| 581 |
else |
| 582 |
return 1 |
| 583 |
fi |
| 584 |
} |
| 585 |
|
| 586 |
# _resolve_device({$device | $spec}) |
| 587 |
# Take a path to (or spec for) a block special device, and set DEV to |
| 588 |
# the (symlink to block) device, and MAJ (resp. MIN) to its major-ID |
| 589 |
# (resp. minor ID) decimal value. On error these variables are not |
| 590 |
# changed and 1 is returned. |
| 591 |
_resolve_device() { |
| 592 |
local spec="$1" dev devno maj min |
| 593 |
if dev="$(_resolve_device_spec "$spec")" && |
| 594 |
devno="$(stat -L -c"%t:%T" -- "$dev" 2>/dev/null)" && |
| 595 |
maj="${devno%:*}" && min="${devno#*:}" && |
| 596 |
[ "$devno" = "$maj:$min" ] && [ -n "$maj" ] && [ -n "$min" ] && |
| 597 |
maj=$(( 0x$maj )) && min=$(( 0x$min )) && [ $maj -gt 0 ]; then |
| 598 |
DEV="$dev" |
| 599 |
MAJ="$maj" |
| 600 |
MIN="$min" |
| 601 |
return 0 |
| 602 |
else |
| 603 |
cryptsetup_message "ERROR: Couldn't resolve device $spec" |
| 604 |
fi |
| 605 |
return 1 |
| 606 |
} |
| 607 |
|
| 608 |
# get_mnt_devno($mountpoint) |
| 609 |
# Print the major:minor device ID(s) holding the file system currently |
| 610 |
# mounted currenty mounted on $mountpoint. |
| 611 |
# Return 0 on success, 1 on error (if $mountpoint is not a mountpoint). |
| 612 |
get_mnt_devno() { |
| 613 |
local wantmount="$1" devnos="" uuid dev |
| 614 |
local out spec fstype DEV MAJ MIN |
| 615 |
|
| 616 |
# use awk rather than a `while read; do done` loop here as /proc/mounts |
| 617 |
# can be many thousands lines long and the `read` builtin goes one |
| 618 |
# byte at the time which slows down execution time, see MR !36 |
| 619 |
out="$(awk -v mp="$wantmount" -- ' |
| 620 |
BEGIN { |
| 621 |
FS = "[ \t]" |
| 622 |
ret = "" |
| 623 |
} |
| 624 |
!/^\s*(#|$)/ { |
| 625 |
# decode octal sequences; per procfs(5) the format of /proc/mounts |
| 626 |
# is analogous to fstab(5) |
| 627 |
head = "" |
| 628 |
while (match($2, /\\[0-7]{3}/)) { |
| 629 |
oct = substr($2, RSTART+1, RLENGTH-1) |
| 630 |
dec = (substr(oct, 1, 1) * 8 + substr(oct, 2, 1)) * 8 + substr(oct, 3, 1) |
| 631 |
head = head substr($2, 1, RSTART-1) sprintf("%c", dec) |
| 632 |
$2 = substr($2, RSTART+RLENGTH) |
| 633 |
} |
| 634 |
if (head $2 == mp) { |
| 635 |
# take the last mountpoint if used several times (shadowed) |
| 636 |
ret = $1 " " $3 |
| 637 |
} |
| 638 |
} |
| 639 |
END { |
| 640 |
print ret |
| 641 |
}' </proc/mounts)" || out="" |
| 642 |
|
| 643 |
spec="$(printf '%b' "${out% *}")" |
| 644 |
if [ -n "$out" ] && _resolve_device "$spec"; then # _resolve_device() already warns on error |
| 645 |
fstype="${out##* }" |
| 646 |
if [ "$fstype" = "btrfs" ]; then |
| 647 |
# btrfs can span over multiple devices |
| 648 |
if uuid="$(_device_uuid "$DEV")"; then |
| 649 |
for dev in "/sys/fs/$fstype/$uuid/devices"/*/dev; do |
| 650 |
devnos="${devnos:+$devnos }$(cat "$dev")" |
| 651 |
done |
| 652 |
else |
| 653 |
cryptsetup_message "ERROR: $spec: Couldn't determine UUID" |
| 654 |
fi |
| 655 |
elif [ -n "$fstype" ]; then |
| 656 |
devnos="$MAJ:$MIN" |
| 657 |
fi |
| 658 |
fi |
| 659 |
|
| 660 |
if [ -z "${devnos:+x}" ]; then |
| 661 |
return 1 # not found |
| 662 |
else |
| 663 |
printf '%s' "$devnos" |
| 664 |
fi |
| 665 |
} |
| 666 |
|
| 667 |
# foreach_cryptdev([--reverse], $callback, $maj:$min, [$maj:$min ..]) |
| 668 |
# Run $callback on the (unmangled) name of each dm-crypt device |
| 669 |
# recursively holding $maj:$min (typically corresponding to an md, |
| 670 |
# linear, or dm-crypt device). Slaves that aren't dm-crypt devices |
| 671 |
# are ignored. |
| 672 |
# By default each device is processed after its *slaves*. If |
| 673 |
# --reverse is set then each device is processed after its *holders* |
| 674 |
# instead. |
| 675 |
foreach_cryptdev() { |
| 676 |
local callback="$1" reverse="n" devno base |
| 677 |
shift |
| 678 |
if [ "$callback" = "--reverse" ]; then |
| 679 |
reverse="y" |
| 680 |
callback="$1" |
| 681 |
shift |
| 682 |
fi |
| 683 |
for devno in "$@"; do |
| 684 |
base="/sys/dev/block/$devno" |
| 685 |
if [ ! -d "$base" ]; then |
| 686 |
cryptsetup_message "ERROR: Couldn't find sysfs directory for $devno" |
| 687 |
return 1 |
| 688 |
fi |
| 689 |
_foreach_cryptdev "$base" |
| 690 |
done |
| 691 |
} |
| 692 |
_foreach_cryptdev() { |
| 693 |
local d="$1" devno maj min name t d2 |
| 694 |
[ "$reverse" = "y" ] && t="holders" || t="slaves" |
| 695 |
[ -d "$d/$t" ] || return 0 |
| 696 |
for d2 in "$d/$t"/*; do |
| 697 |
if [ -d "$d2" ] && d2="$(realpath -e -- "$d2")"; then |
| 698 |
_foreach_cryptdev "$d2" |
| 699 |
fi |
| 700 |
done |
| 701 |
if [ -d "$d/dm" ] && devno="$(cat "$d/dev")" && |
| 702 |
maj="${devno%:*}" && min="${devno#*:}" && |
| 703 |
[ "$devno" = "$maj:$min" ] && [ -n "$maj" ] && [ -n "$min" ] && |
| 704 |
[ "$(dmsetup info -c --noheadings -o subsystem -j "$maj" -m "$min")" = "CRYPT" ] && |
| 705 |
name="$(dmsetup info -c --noheadings -o unmangled_name -j "$maj" -m "$min")"; then |
| 706 |
"$callback" "$name" |
| 707 |
fi |
| 708 |
} |
| 709 |
|
| 710 |
# vim: set filetype=sh : |