clevis-luks-unbind 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. function usage() {
  23. echo >&2
  24. echo "Usage: clevis luks unbind -d DEV -s SLT" >&2
  25. echo >&2
  26. echo "$SUMMARY": >&2
  27. echo >&2
  28. echo " -d DEV The bound LUKS device" >&2
  29. echo >&2
  30. echo " -s SLOT The LUKS slot number for the pin unbind" >&2
  31. echo >&2
  32. echo " -f Do not ask for confirmation and wipe slot in batch-mode" >&2
  33. echo >&2
  34. exit 1
  35. }
  36. if [ $# -eq 1 -a "$1" == "--summary" ]; then
  37. echo "$SUMMARY"
  38. exit 0
  39. fi
  40. while getopts ":d:s:f" o; do
  41. case "$o" in
  42. f) FRC=-q;;
  43. d) DEV=$OPTARG;;
  44. s) SLT=$OPTARG;;
  45. *) usage;;
  46. esac
  47. done
  48. if [ -z "$DEV" ]; then
  49. echo "Did not specify a device!" >&2
  50. usage
  51. fi
  52. if [ -z "$SLT" ]; then
  53. echo "Did not specify a slot!" >&2
  54. usage
  55. fi
  56. if cryptsetup isLuks --type luks1 "$DEV"; then
  57. if ! luksmeta test -d $DEV 2>/dev/null; then
  58. echo "The $DEV device does not contain a LUKSMeta header!" >&2
  59. exit 1
  60. fi
  61. read -r slot active uuid <<< $(luksmeta show -d "$DEV" | grep "^$SLT *")
  62. if [ "$uuid" == "empty" ]; then
  63. echo "The LUKSMeta slot $SLT on device $DEV is already empty." >&2
  64. exit 1
  65. fi
  66. [ "$active" == "active" ] && KILL=true
  67. elif cryptsetup isLuks --type luks2 "$DEV"; then
  68. dump=`cryptsetup luksDump "$DEV"`
  69. grep -q "^\s*$SLT: luks2" <<<"$dump" && KILL=true
  70. TOK=`grep -E -B1 "^\s+Keyslot:\s+$SLT$" <<<"$dump" \
  71. | sed -rn 's|^\s+([0-9]+): clevis|\1|p'`
  72. else
  73. echo "$DEV is not a supported LUKS device!" >&2
  74. exit 1
  75. fi
  76. if [ -z "$FRC" ]; then
  77. echo "The unbind operation will wipe a slot. This operation is unrecoverable." >&2
  78. read -r -p "Do you wish to erase LUKS slot $SLT on $DEV? [ynYN] " ans < /dev/tty
  79. [[ "$ans" =~ ^[yY]$ ]] || exit 0
  80. fi
  81. if [ -n "$KILL" ]; then
  82. if ! cryptsetup luksKillSlot "$DEV" "$SLT" $FRC; then
  83. echo "LUKS slot $SLT for device $DEV couldn't be deleted"
  84. exit 1
  85. fi
  86. fi
  87. if cryptsetup isLuks --type luks1 "$DEV"; then
  88. if ! luksmeta wipe -f -d "$DEV" -u "$UUID" -s "$SLT"; then
  89. echo "LUKSMeta slot $SLT for device $DEV couldn't be deleted"
  90. exit 1
  91. fi
  92. elif cryptsetup isLuks --type luks2 "$DEV" && [ -n "$TOK" ]; then
  93. if ! cryptsetup token remove --token-id "$TOK" "$DEV"; then
  94. echo "Error while removing token $TOK from LUKS device $DEV!" >&2
  95. exit 1
  96. fi
  97. fi