clevis-luks-unlock 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. function usage() {
  23. echo >&2
  24. echo "Usage: clevis luks unlock -d DEV [-n NAME]" >&2
  25. echo >&2
  26. echo "$SUMMARY": >&2
  27. echo >&2
  28. echo " -d DEV The LUKS device on which to perform unlocking" >&2
  29. echo >&2
  30. echo " -n NAME The name of the unlocked device node" >&2
  31. echo >&2
  32. exit 1
  33. }
  34. if [ $# -eq 1 -a "$1" == "--summary" ]; then
  35. echo "$SUMMARY"
  36. exit 0
  37. fi
  38. while getopts ":d:n:" o; do
  39. case "$o" in
  40. d) DEV=$OPTARG;;
  41. n) NAME=$OPTARG;;
  42. *) usage;;
  43. esac
  44. done
  45. if [ -z "$DEV" ]; then
  46. echo "Did not specify a device!" >&2
  47. usage
  48. fi
  49. NAME=${NAME:-luks-`cryptsetup luksUUID $DEV`}
  50. if cryptsetup isLuks --type luks1 "$DEV"; then
  51. while read -r slot state uuid; do
  52. [ "$state" != "active" ] && continue
  53. [ "$uuid" != "$UUID" ] && continue
  54. if pt=`luksmeta load -d $DEV -s $slot -u $UUID | clevis decrypt`; then
  55. echo -n "$pt" | cryptsetup open -d- "$DEV" "$NAME"
  56. exit 0
  57. fi
  58. done <<< "$(luksmeta show -d "$DEV")"
  59. elif cryptsetup isLuks --type luks2 "$DEV"; then
  60. for id in `cryptsetup luksDump "$DEV" | sed -rn 's|^\s+([0-9]+): clevis|\1|p'`; do
  61. tok=`cryptsetup token export --token-id "$id" "$DEV"`
  62. jwe=`jose fmt -j- -Og jwe -o- <<<"$tok" | jose jwe fmt -i- -c`
  63. if pt=`echo -n "$jwe" | clevis decrypt`; then
  64. echo -n "$pt" | cryptsetup open -d- "$DEV" "$NAME"
  65. exit 0
  66. fi
  67. done
  68. fi
  69. exit 1