clevis-luks-askpass 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. luks1_decrypt() {
  31. luksmeta load "$@" \
  32. | clevis decrypt
  33. local rc
  34. for rc in "${PIPESTATUS[@]}"; do
  35. [ $rc -eq 0 ] || return $rc
  36. done
  37. return 0
  38. }
  39. luks2_jwe() {
  40. # jose jwe fmt -c outputs extra \n, so clean it up
  41. cryptsetup token export "$@" \
  42. | jose fmt -j- -Og jwe -o- \
  43. | jose jwe fmt -i- -c \
  44. | tr -d '\n'
  45. local rc
  46. for rc in "${PIPESTATUS[@]}"; do
  47. [ $rc -eq 0 ] || return $rc
  48. done
  49. return 0
  50. }
  51. while true; do
  52. todo=0
  53. for question in "$path"/ask.*; do
  54. metadata=false
  55. unlocked=false
  56. d=
  57. s=
  58. while read line; do
  59. case "$line" in
  60. Id=cryptsetup:*) d="${line##Id=cryptsetup:}";;
  61. Socket=*) s="${line##Socket=}";;
  62. esac
  63. done < "$question"
  64. [ "$d" ] && [ "$s" ] || continue
  65. if cryptsetup isLuks --type luks1 "$d"; then
  66. # If the device is not initialized, sliently skip it.
  67. luksmeta test -d "$d" || continue
  68. while read -r slot state uuid; do
  69. [ "$state" == "active" ] || continue
  70. [ "$uuid" == "$UUID" ] || continue
  71. metadata=true
  72. if pt="$(luks1_decrypt -d "$d" -s "$slot" -u "$UUID")"; then
  73. echo -n "+$pt" | ncat -U -u --send-only "$s"
  74. unlocked=true
  75. break
  76. fi
  77. done < <(luksmeta show -d "$d")
  78. elif cryptsetup isLuks --type luks2 "$d"; then
  79. while read -r id; do
  80. jwe="$(luks2_jwe --token-id "$id" "$d")" \
  81. || continue
  82. metadata=true
  83. if pt="$(echo -n "$jwe" | clevis decrypt)"; then
  84. echo -n "+$pt" | ncat -U -u --send-only "$s"
  85. unlocked=true
  86. break
  87. fi
  88. done < <(cryptsetup luksDump "$d" | sed -rn 's|^\s+([0-9]+): clevis|\1|p')
  89. fi
  90. [ "$metadata" == true ] || continue
  91. [ "$unlocked" == true ] && continue
  92. ((todo++))
  93. done
  94. if [ $todo -eq 0 ] || [ "$loop" != true ]; then
  95. break;
  96. fi
  97. sleep 0.5
  98. done