decrypt_gnupg-sc 953 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/bin/sh
  2. if [ -d "/cryptroot/gnupghome" ]; then
  3. export GNUPGHOME="/cryptroot/gnupghome"
  4. fi
  5. run_gpg() {
  6. gpg --no-options --trust-model=always "$@"
  7. }
  8. decrypt_gpg () {
  9. local console _
  10. if ! GPG_TTY="$(tty)"; then
  11. read console _ </proc/consoles
  12. GPG_TTY="/dev/$console"
  13. fi
  14. export GPG_TTY
  15. if ! run_gpg --decrypt -- "$1"; then
  16. return 1
  17. fi
  18. return 0
  19. }
  20. # `gpg-connect-agent LEARN /bye` is another (lighter) way, but it's
  21. # harder to retrieve the return code
  22. if ! run_gpg --batch --quiet --no-tty --card-status >/dev/null; then
  23. echo "Please insert OpenPGP SmartCard..." >&2
  24. until run_gpg --batch --quiet --no-tty --card-status; do
  25. sleep 1
  26. done >/dev/null 2>&1
  27. fi
  28. if [ ! -x /usr/bin/gpg ]; then
  29. echo "$0: /usr/bin/gpg is not available" >&2
  30. exit 1
  31. fi
  32. if [ -z "$1" ] || [ ! -f "$1" ]; then
  33. echo "$0: missing key as argument" >&2
  34. exit 1
  35. fi
  36. decrypt_gpg "$1"
  37. exit $?