| 1 | #!/bin/shscip-shell filesystem . . `/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 | diescip-shell shell shell-project 0.0.0 die().() |
| 17 | { |
| 18 | echoscip-shell system . . echo. "$@" >&2 |
| 19 | exit 1 |
| 20 | } |
| 21 | |
| 22 | if [scip-shell system . . `[`. -z "${CRYPTTAB_KEY:-}" ] || [scip-shell system . . `[`. "$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/askpassscip-shell filesystem . . `/lib/cryptsetup/askpass`.' |
| 32 | PROMPT_="Caching passphrase for ${CRYPTTAB_NAME}: " |
| 33 | |
| 34 | |
| 35 | if ! KID_="$(keyctl search @u user "$ID_" 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" || \ |
| 36 | [ -z "$KID_" ] || [scip-shell system . . `[`. "$CRYPTTAB_TRIED" -gt 0 ]; then |
| 37 | # key not found or wrong, ask the user |
| 38 | KEY_="$($ASKPASS_ "$PROMPT_")" || diescip-shell shell shell-project 0.0.0 die(). "Error executing $ASKPASS_" |
| 39 | if [scip-shell system . . `[`. -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_="$(printfscip-shell system . . printf. "%s" "$KEY_" | keyctl padd user "$ID_" @u)" |
| 47 | [scip-shell system . . `[`. -n "$KID_" ] || diescip-shell shell shell-project 0.0.0 die(). "Error adding passphrase to kernel keyring" |
| 48 | if ! keyctl timeout "$KID_" "$TIMEOUT_"; then |
| 49 | keyctl unlink "$KID_" @u |
| 50 | diescip-shell shell shell-project 0.0.0 die(). "Error setting timeout on key ($KID_), removing" |
| 51 | fi |
| 52 | else |
| 53 | echoscip-shell system . . echo. "Using cached passphrase for ${CRYPTTAB_NAME}." >&2 |
| 54 | fi |
| 55 | keyctl pipe "$KID_" |