clevis-luks-askpass 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. set -eu
  3. # vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80:
  4. #
  5. # Copyright (c) 2016 Red Hat, Inc.
  6. # Author: Harald Hoyer <harald@redhat.com>
  7. # Author: Nathaniel McCallum <npmccallum@redhat.com>
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. . clevis-luks-common-functions
  23. # Make sure to exit cleanly if SIGTERM is received.
  24. trap 'echo "Exiting due to SIGTERM" && exit 0' TERM
  25. loop=
  26. path=/run/systemd/ask-password
  27. while getopts ":lp:" o; do
  28. case "${o}" in
  29. l) loop=true;;
  30. p) path="${OPTARG}";;
  31. *) ;;
  32. esac
  33. done
  34. while true; do
  35. for question in "${path}"/ask.*; do
  36. # question will expand to itself, in case no files match, so we verify
  37. # whether it actually exists, before proceeding.
  38. [ ! -e "${question}" ] && continue
  39. d=
  40. s=
  41. while read -r line; do
  42. case "$line" in
  43. Id=cryptsetup:*) d="${line##Id=cryptsetup:}";;
  44. Socket=*) s="${line##Socket=}";;
  45. esac
  46. done < "$question"
  47. [ -b "${d}" ] || continue
  48. [ -S "${s}" ] || continue
  49. if ! pt="$(clevis_luks_unlock_device "${d}")" || [ -z "${pt}" ]; then
  50. continue
  51. fi
  52. uuid="$(cryptsetup luksUUID "${d}")"
  53. if ! printf '+%s' "${pt}" | ncat -U -u --send-only "${s}"; then
  54. echo "Unable to unlock ${d} (UUID=${uuid}) with recovered passphrase" >&2
  55. continue
  56. fi
  57. echo "Unlocked ${d} (UUID=${uuid}) successfully" >&2
  58. done
  59. [ "${loop}" != true ] && break
  60. # Checking for pending devices to be unlocked.
  61. if remaining=$(clevis_devices_to_unlock) && [ -z "${remaining}" ]; then
  62. break;
  63. fi
  64. sleep 0.5
  65. done