freedombone-utils-gpg 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # gpg functions
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2016 Bob Mottram <bob@freedombone.net>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. function gpg_agent_setup {
  31. gpg_username=$1
  32. if [[ $gpg_username == 'root' ]]; then
  33. if ! grep -q 'GPG_TTY' /root/.bashrc; then
  34. echo '' >> /root/.bashrc
  35. echo 'GPG_TTY=$(tty)' >> /root/.bashrc
  36. echo 'export GPG_TTY' >> /root/.bashrc
  37. fi
  38. if ! grep -q 'use-agent' /root/.gnupg/gpg.conf; then
  39. echo 'use-agent' >> /root/.gnupg/gpg.conf
  40. fi
  41. if ! grep -q 'pinentry-mode loopback' /root/.gnupg/gpg.conf; then
  42. echo 'pinentry-mode loopback' >> /root/.gnupg/gpg.conf
  43. fi
  44. if [ ! -f /root/.gnupg/gpg-agent.conf ]; then
  45. touch /root/.gnupg/gpg-agent.conf
  46. fi
  47. if ! grep -q 'allow-loopback-pinentry' /root/.gnupg/gpg-agent.conf; then
  48. echo 'allow-loopback-pinentry' >> /root/.gnupg/gpg-agent.conf
  49. fi
  50. echo RELOADAGENT | gpg-connect-agent
  51. else
  52. if ! grep -q 'GPG_TTY' /home/$gpg_username/.bashrc; then
  53. echo '' >> /home/$gpg_username/.bashrc
  54. echo 'GPG_TTY=$(tty)' >> /home/$gpg_username/.bashrc
  55. echo 'export GPG_TTY' >> /home/$gpg_username/.bashrc
  56. chown $gpg_username:$gpg_username /home/$gpg_username/.bashrc
  57. fi
  58. if ! grep -q 'use-agent' /home/$gpg_username/.gnupg/gpg.conf; then
  59. echo 'use-agent' >> /home/$gpg_username/.gnupg/gpg.conf
  60. fi
  61. if ! grep -q 'pinentry-mode loopback' /home/$gpg_username/.gnupg/gpg.conf; then
  62. echo 'pinentry-mode loopback' >> /home/$gpg_username/.gnupg/gpg.conf
  63. fi
  64. if [ ! -f /home/$gpg_username/.gnupg/gpg-agent.conf ]; then
  65. touch /home/$gpg_username/.gnupg/gpg-agent.conf
  66. fi
  67. if ! grep -q 'allow-loopback-pinentry' /home/$gpg_username/.gnupg/gpg-agent.conf; then
  68. echo 'allow-loopback-pinentry' >> /home/$gpg_username/.gnupg/gpg-agent.conf
  69. fi
  70. su -c "echo RELOADAGENT | gpg-connect-agent" - $gpg_username
  71. fi
  72. }
  73. function gpg_pubkey_from_email {
  74. key_owner_username=$1
  75. key_email_address=$2
  76. key_id=
  77. if [[ $key_owner_username != "root" ]]; then
  78. key_id=$(su -c "gpg --list-keys $key_email_address" - $key_owner_username | sed -n '2p' | sed 's/^[ \t]*//')
  79. else
  80. key_id=$(gpg --list-keys $key_email_address | sed -n '2p' | sed 's/^[ \t]*//')
  81. fi
  82. echo $key_id
  83. }
  84. function enable_email_encryption_at_rest {
  85. for d in /home/*/ ; do
  86. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  87. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  88. if grep '#| /usr/bin/gpgit.pl' /home/$USERNAME/.procmailrc; then
  89. sed -i 's@#| /usr/bin/gpgit.pl@| /usr/bin/gpgit.pl@g' /home/$USERNAME/.procmailrc
  90. sed -i 's|#:0 f|:0 f|g' /home/$USERNAME/.procmailrc
  91. fi
  92. fi
  93. done
  94. if grep '#| /usr/bin/gpgit.pl' /etc/skel/.procmailrc; then
  95. sed -i 's@#| /usr/bin/gpgit.pl@| /usr/bin/gpgit.pl@g' /etc/skel/.procmailrc
  96. sed -i 's|#:0 f|:0 f|g' /etc/skel/.procmailrc
  97. fi
  98. }
  99. function disable_email_encryption_at_rest {
  100. for d in /home/*/ ; do
  101. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  102. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  103. if ! grep '#| /usr/bin/gpgit.pl' /home/$USERNAME/.procmailrc; then
  104. sed -i 's@| /usr/bin/gpgit.pl@#| /usr/bin/gpgit.pl@g' /home/$USERNAME/.procmailrc
  105. sed -i 's|:0 f|#:0 f|g' /home/$USERNAME/.procmailrc
  106. fi
  107. fi
  108. done
  109. if ! grep '#| /usr/bin/gpgit.pl' /etc/skel/.procmailrc; then
  110. sed -i 's@| /usr/bin/gpgit.pl@#| /usr/bin/gpgit.pl@g' /etc/skel/.procmailrc
  111. sed -i 's|:0 f|#:0 f|g' /etc/skel/.procmailrc
  112. fi
  113. }
  114. # NOTE: deliberately no exit 0