clevis-luks-unlock 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. . clevis-luks-common-functions
  21. SUMMARY="Unlocks a LUKS volume"
  22. function usage() {
  23. exec >&2
  24. echo
  25. echo "Usage: clevis luks unlock -d DEV [-n NAME] [-t SLT]"
  26. echo
  27. echo "$SUMMARY":
  28. echo
  29. echo " -d DEV The LUKS device on which to perform unlocking"
  30. echo
  31. echo " -n NAME The name of the unlocked device node"
  32. echo
  33. echo " -t SLT Test the passphrase for the given slot without unlocking"
  34. echo " the device"
  35. echo
  36. exit 2
  37. }
  38. if [ $# -eq 1 ] && [ "$1" == "--summary" ]; then
  39. echo "$SUMMARY"
  40. exit 0
  41. fi
  42. while getopts ":d:n:t:" o; do
  43. case "$o" in
  44. d) DEV="$OPTARG";;
  45. n) NAME="$OPTARG";;
  46. t) SLT="$OPTARG";;
  47. *) usage;;
  48. esac
  49. done
  50. if [ -z "$DEV" ]; then
  51. echo "Did not specify a device!" >&2
  52. usage
  53. fi
  54. if ! cryptsetup isLuks "$DEV"; then
  55. echo "$DEV is not a LUKS device!" >&2
  56. exit 1
  57. fi
  58. NAME="${NAME:-luks-"$(cryptsetup luksUUID "$DEV")"}"
  59. if [ -n "$SLT" ]; then
  60. if ! clevis_luks_unlock_device_by_slot "${DEV}" "${SLT}" >/dev/null; then
  61. echo "Test for token slot ${SLT} on device ${DEV} failed." >&2
  62. exit 1
  63. fi
  64. else
  65. if ! pt=$(clevis_luks_unlock_device "${DEV}"); then
  66. echo "${DEV} could not be opened." >&2
  67. exit 1
  68. fi
  69. echo -n "${pt}" | cryptsetup open -d- "${DEV}" "${NAME}"
  70. fi