clevis-luks-unbind.in 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/bash -e
  2. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  3. #
  4. # Copyright (c) 2017 Red Hat, Inc.
  5. # Author: Javier Martinez Canillas <javierm@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="Unbinds a pin bound to 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 unbind -d DEV -s SLT"
  31. echo
  32. echo "$SUMMARY":
  33. echo
  34. echo " -d DEV The bound LUKS device"
  35. echo
  36. echo " -s SLOT The LUKS slot number for the pin unbind"
  37. echo
  38. echo " -f Do not ask for confirmation and wipe slot in batch-mode"
  39. echo
  40. exit 2
  41. }
  42. if [ $# -eq 1 ] && [ "$1" == "--summary" ]; then
  43. echo "$SUMMARY"
  44. exit 0
  45. fi
  46. FRC=()
  47. while getopts ":d:s:f" o; do
  48. case "$o" in
  49. f) FRC+=(-q);;
  50. d) DEV="$OPTARG";;
  51. s) SLT="$OPTARG";;
  52. *) usage;;
  53. esac
  54. done
  55. if [ -z "$DEV" ]; then
  56. echo "Did not specify a device!" >&2
  57. usage
  58. fi
  59. if [ -z "$SLT" ]; then
  60. echo "Did not specify a slot!" >&2
  61. usage
  62. fi
  63. if ! cryptsetup isLuks "$DEV"; then
  64. echo "$DEV is not a LUKS device!" >&2
  65. exit 1
  66. fi
  67. if luks2_supported; then
  68. if cryptsetup isLuks --type luks1 "$DEV"; then
  69. luks_type="luks1"
  70. elif cryptsetup isLuks --type luks2 "$DEV";then
  71. luks_type="luks2"
  72. else
  73. echo "$DEV is not a supported LUKS device!" >&2
  74. exit 1
  75. fi
  76. else
  77. luks_type="luks1"
  78. fi
  79. if [ "$luks_type" == "luks1" ]; then
  80. if ! luksmeta test -d "$DEV" 2>/dev/null; then
  81. echo "The $DEV device does not contain a LUKSMeta header!" >&2
  82. exit 1
  83. fi
  84. read -r slot state uuid < <(luksmeta show -d "$DEV" | grep "^$SLT *")
  85. if [ "$uuid" == "empty" ]; then
  86. echo "The LUKSMeta slot $SLT on device $DEV is already empty." >&2
  87. exit 1
  88. fi
  89. [ "$state" == "active" ] && KILL=true
  90. elif [ "$luks_type" == "luks2" ]; then
  91. dump="$(cryptsetup luksDump "$DEV")"
  92. grep -q "^\s*$SLT: luks2" <<< "$dump" && KILL=true
  93. TOK="$(grep -E -B1 "^\s+Keyslot:\s+$SLT$" <<< "$dump" \
  94. | sed -rn 's|^\s+([0-9]+): clevis|\1|p')"
  95. fi
  96. if [ -z "${FRC[*]}" ]; then
  97. echo "The unbind operation will wipe a slot. This operation is unrecoverable." >&2
  98. read -r -p "Do you wish to erase LUKS slot $SLT on $DEV? [ynYN] " ans < /dev/tty
  99. [[ "$ans" =~ ^[yY]$ ]] || exit 0
  100. fi
  101. if [ -n "$KILL" ]; then
  102. if ! cryptsetup luksKillSlot "$DEV" "$SLT" "${FRC[@]}"; then
  103. echo "LUKS slot $SLT for device $DEV couldn't be deleted"
  104. exit 1
  105. fi
  106. fi
  107. if [ "$luks_type" == "luks1" ]; then
  108. if ! luksmeta wipe -f -d "$DEV" -u "$UUID" -s "$SLT"; then
  109. echo "LUKSMeta slot $SLT for device $DEV couldn't be deleted"
  110. exit 1
  111. fi
  112. elif [ "$luks_type" == "luks2" ] && [ -n "$TOK" ]; then
  113. if ! cryptsetup token remove --token-id "$TOK" "$DEV"; then
  114. echo "Error while removing token $TOK from LUKS device $DEV!" >&2
  115. exit 1
  116. fi
  117. fi