Debian Codegraph @

debian/scripts/decrypt_keyctl

raw symbols SCIP
1 #!/bin/sh
2 # decrypt_keyctl - to use in /etc/crypttab as keyscript
3 # Allows to cache passwords for cryptdevices for 60s
4 # The same password is used for for cryptdevices with the same identifier.
5 # The keyfile parameter, which is the third field from /etc/crypttab, is
6 # used as identifier in this keyscript.
7 #
8 # sample crypttab entries:
9 # test1 /dev/sda1 test_pw luks,keyscript=decrypt_keyctl
10 # test2 /dev/sda2 test_pw luks,keyscript=decrypt_keyctl
11 # test3 /dev/sda3 test_other_pw luks,keyscript=decrypt_keyctl
12 #
13 # test1 and test2 have the same identifier thus test2 does not need a password
14 # typed in manually
15
16 die()
17 {
18 echo "$@" >&2
19 exit 1
20 }
21
22 if [ -z "${CRYPTTAB_KEY:-}" ] || [ "$CRYPTTAB_KEY" = "none" ]; then
23 # store the passphrase in the key name used by systemd-ask-password
24 ID_="cryptsetup"
25 else
26 # the keyfile given from crypttab is used as identifier in the keyring
27 # including the prefix "cryptsetup:"
28 ID_="cryptsetup:$CRYPTTAB_KEY"
29 fi
30 TIMEOUT_='60'
31 ASKPASS_='/lib/cryptsetup/askpass'
32 PROMPT_="Caching passphrase for ${CRYPTTAB_NAME}: "
33
34
35 if ! KID_="$(keyctl search @u user "$ID_" 2>/dev/null)" || \
36 [ -z "$KID_" ] || [ "$CRYPTTAB_TRIED" -gt 0 ]; then
37 # key not found or wrong, ask the user
38 KEY_="$($ASKPASS_ "$PROMPT_")" || die "Error executing $ASKPASS_"
39 if [ -n "$KID_" ]; then
40 # I have cached wrong password and now i may use either `keyctl update`
41 # to update $KID_ or just unlink old key, and add new. With `update` i
42 # may hit "Key has expired", though. So i'll go "unlink and add" way.
43 keyctl unlink "$KID_" @u
44 KID_=""
45 fi
46 KID_="$(printf "%s" "$KEY_" | keyctl padd user "$ID_" @u)"
47 [ -n "$KID_" ] || die "Error adding passphrase to kernel keyring"
48 if ! keyctl timeout "$KID_" "$TIMEOUT_"; then
49 keyctl unlink "$KID_" @u
50 die "Error setting timeout on key ($KID_), removing"
51 fi
52 else
53 echo "Using cached passphrase for ${CRYPTTAB_NAME}." >&2
54 fi
55 keyctl pipe "$KID_"