clevis-luks-unlock.in 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash -e
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2016 Red Hat, Inc.
  5. # Author: Nathaniel McCallum <npmccallum@redhat.com>
  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. SUMMARY="Unlocks a LUKS volume"
  21. UUID=cb6e8904-81ff-40da-a84a-07ab9ab5715e
  22. # We require cryptsetup >= 2.0.4 to fully support LUKSv2.
  23. # Support is determined at build time.
  24. function luks2_supported() {
  25. return @OLD_CRYPTSETUP@
  26. }
  27. function usage() {
  28. exec >&2
  29. echo
  30. echo "Usage: clevis luks unlock -d DEV [-n NAME]"
  31. echo
  32. echo "$SUMMARY":
  33. echo
  34. echo " -d DEV The LUKS device on which to perform unlocking"
  35. echo
  36. echo " -n NAME The name of the unlocked device node"
  37. echo
  38. exit 2
  39. }
  40. if [ $# -eq 1 ] && [ "$1" == "--summary" ]; then
  41. echo "$SUMMARY"
  42. exit 0
  43. fi
  44. while getopts ":d:n:" o; do
  45. case "$o" in
  46. d) DEV="$OPTARG";;
  47. n) NAME="$OPTARG";;
  48. *) usage;;
  49. esac
  50. done
  51. if [ -z "$DEV" ]; then
  52. echo "Did not specify a device!" >&2
  53. usage
  54. fi
  55. if ! cryptsetup isLuks "$DEV"; then
  56. echo "$DEV is not a LUKS device!" >&2
  57. exit 1
  58. fi
  59. if luks2_supported; then
  60. if cryptsetup isLuks --type luks1 "$DEV"; then
  61. luks_type="luks1"
  62. elif cryptsetup isLuks --type luks2 "$DEV";then
  63. luks_type="luks2"
  64. else
  65. echo "$DEV is not a supported LUKS device!" >&2
  66. exit 1
  67. fi
  68. else
  69. luks_type="luks1"
  70. fi
  71. NAME="${NAME:-luks-"$(cryptsetup luksUUID "$DEV")"}"
  72. luks1_decrypt() {
  73. luksmeta load "$@" \
  74. | clevis decrypt
  75. local rc
  76. for rc in "${PIPESTATUS[@]}"; do
  77. [ $rc -eq 0 ] || return $rc
  78. done
  79. return 0
  80. }
  81. luks2_decrypt() {
  82. # jose jwe fmt -c outputs extra \n, so clean it up
  83. cryptsetup token export "$@" \
  84. | jose fmt -j- -Og jwe -o- \
  85. | jose jwe fmt -i- -c \
  86. | tr -d '\n' \
  87. | clevis decrypt
  88. local rc
  89. for rc in "${PIPESTATUS[@]}"; do
  90. [ $rc -eq 0 ] || return $rc
  91. done
  92. return 0
  93. }
  94. if [ "$luks_type" == "luks1" ]; then
  95. while read -r slot state uuid; do
  96. [ "$state" == "active" ] || continue
  97. [ "$uuid" == "$UUID" ] || continue
  98. pt="$(luks1_decrypt -d $DEV -s $slot -u $UUID)" \
  99. || continue
  100. exec cryptsetup open -d- "$DEV" "$NAME" < <(
  101. echo -n "$pt"
  102. )
  103. done < <(luksmeta show -d "$DEV")
  104. elif [ "$luks_type" == "luks2" ]; then
  105. while read -r id; do
  106. pt="$(luks2_decrypt --token-id "$id" "$DEV")" \
  107. || continue
  108. exec cryptsetup open -d- "$DEV" "$NAME" < <(
  109. echo -n "$pt"
  110. )
  111. done < <(cryptsetup luksDump "$DEV" | sed -rn 's|^\s+([0-9]+): clevis|\1|p')
  112. fi
  113. echo "$DEV could not be opened." >&2
  114. exit 1