clevis-luks-askpass 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. path=/run/systemd/ask-password
  24. while getopts ":lp:" o; do
  25. case "$o" in
  26. l) loop=true;;
  27. p) path=$OPTARG;;
  28. esac
  29. done
  30. while true; do
  31. todo=0
  32. for question in $path/ask.*; do
  33. metadata=false
  34. unlocked=false
  35. d=
  36. s=
  37. while read line; do
  38. case "$line" in
  39. Id=cryptsetup:*) d="${line##Id=cryptsetup:}";;
  40. Socket=*) s="${line##Socket=}";;
  41. esac
  42. done < "$question"
  43. [ -z "$d" -o -z "$s" ] && continue
  44. if cryptsetup isLuks --type luks1 "$d"; then
  45. # If the device is not initialized, sliently skip it.
  46. luksmeta test -d "$d" || continue
  47. while read -r slot state uuid; do
  48. [ "$state" != "active" ] && continue
  49. [ "$uuid" != "$UUID" ] && continue
  50. metadata=true
  51. if pt="`luksmeta load -d $d -s $slot -u $UUID | clevis decrypt`"; then
  52. echo -n "+$pt" | nc -U -u --send-only "$s"
  53. unlocked=true
  54. break
  55. fi
  56. done < <(luksmeta show -d "$d")
  57. elif cryptsetup isLuks --type luks2 "$d"; then
  58. ids=`cryptsetup luksDump "$d" | sed -rn 's|^\s+([0-9]+): clevis|\1|p'`
  59. for id in $ids; do
  60. tok=`cryptsetup token export --token-id "$id" "$d"`
  61. jwe=`jose fmt -j- -Og jwe -o- <<<"$tok" | jose jwe fmt -i- -c`
  62. metadata=true
  63. if pt=`echo -n "$jwe" | clevis decrypt`; then
  64. echo -n "+$pt" | nc -U -u --send-only "$s"
  65. unlocked=true
  66. break
  67. fi
  68. done
  69. fi
  70. [ $metadata == true ] || continue
  71. [ $unlocked == true ] && continue
  72. todo=$((todo + 1))
  73. done
  74. if [ $todo -eq 0 ] || [ "$loop" != "true" ]; then
  75. break;
  76. fi
  77. sleep 0.5
  78. done