clevis-luks-askpass 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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: Harald Hoyer <harald@redhat.com>
  6. # Author: Nathaniel McCallum <npmccallum@redhat.com>
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. UUID=cb6e8904-81ff-40da-a84a-07ab9ab5715e
  22. shopt -s nullglob
  23. while getopts ":l" o; do
  24. case "$o" in
  25. l) loop=true;;
  26. esac
  27. done
  28. while true; do
  29. todo=0
  30. for question in /run/systemd/ask-password/ask.*; do
  31. metadata=false
  32. unlocked=false
  33. d=
  34. s=
  35. while read line; do
  36. case "$line" in
  37. Id=cryptsetup:*) d="${line##Id=cryptsetup:}";;
  38. Socket=*) s="${line##Socket=}";;
  39. esac
  40. done < "$question"
  41. [ -z "$d" -o -z "$s" ] && continue
  42. # If the device is not initialized, sliently skip it.
  43. luksmeta test -d "$d" || continue
  44. while read -r slot state uuid; do
  45. [ "$state" != "active" ] && continue
  46. [ "$uuid" != "$UUID" ] && continue
  47. metadata=true
  48. if pt="`luksmeta load -d $d -s $slot -u $UUID | clevis decrypt`"; then
  49. echo -n "+$pt" | nc -U -u --send-only "$s"
  50. unlocked=true
  51. break
  52. fi
  53. done < <(luksmeta show -d "$d")
  54. [ $metadata == true ] || continue
  55. [ $unlocked == true ] && continue
  56. todo=$((todo + 1))
  57. done
  58. if [ $todo -eq 0 ] || [ "$loop" != "true" ]; then
  59. break;
  60. fi
  61. sleep 0.5
  62. done