freedombone-encrypt-mail 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # GPG Encrypt a Maildir using gpgit.pl
  10. #
  11. # License
  12. # =======
  13. #
  14. # Copyright (C) 2014-2018 Bob Mottram <bob@freedombone.net>
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. USERNAME=$1
  29. PROJECT_NAME='freedombone'
  30. COMPLETION_FILE="$HOME/${PROJECT_NAME}-completed.txt"
  31. UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
  32. for f in $UTILS_FILES
  33. do
  34. source "$f"
  35. done
  36. ADMIN_USER=$(get_completion_param "Admin user")
  37. if [ ! "$USERNAME" ]; then
  38. USERNAME=$ADMIN_USER
  39. fi
  40. MAIL_DIR=/home/$USERNAME/Maildir
  41. EMAIL_ADDRESS=$USERNAME@$HOSTNAME
  42. # Does this key exist?
  43. if ! gpg --list-keys "$EMAIL_ADDRESS" > /dev/null 2>&1; then
  44. echo $"A GPG key for $EMAIL_ADDRESS could not be found!"
  45. exit 0
  46. fi
  47. # Find all files in the Maildir specified.
  48. echo $"Calling find"
  49. find "$MAIL_DIR" -type f -regex '.*/\(cur\|new\)/.*' "$4"|while read -r line; do
  50. gpgit.pl --encrypt-mode prefer-inline "$EMAIL_ADDRESS" "/tmp/msg_$USERNAME"
  51. # Check to see if there are differences between the existing
  52. # Maildir file and what was created by gpgit.pl
  53. diff -qa "$line" "/tmp/msg_$USERNAME" > /dev/null 2>&1;
  54. # shellcheck disable=SC2181
  55. if [ $? -gt 0 ]; then
  56. # Preserve timestamps, set ownership.
  57. chown "$USERNAME":"$USERNAME" "/tmp/msg_$USERNAME"
  58. chmod 600 "/tmp/msg_$USERNAME"
  59. touch "/tmp/msg_$USERNAME" --reference="$line"
  60. # Unlink the original Maildir message
  61. unlink "$line"
  62. # Strip message sizes, retain experimental flags
  63. # and status flags, and copy the file over.
  64. STRIPSIZES=$(/bin/echo "$line"|/bin/sed -e "s/W=[[:digit:]]*//" -e "s/S=[[:digit:]]*//" -e "s/,,//" -e "s/,:2/:2/")
  65. cp -av "/tmp/msg_$USERNAME" "$STRIPSIZES"
  66. #Indexes must be rebuilt, weve modified Maildir.
  67. touch "/tmp/rebuild_index_$USERNAME"
  68. else
  69. echo $"Not copying, no differences between /tmp/msg_$USERNAME and $line"
  70. fi
  71. # Remove the temporary file
  72. unlink "/tmp/msg_$USERNAME"
  73. done
  74. # Remove Dovecot index and uids for regeneration.
  75. if [ -f "/tmp/rebuild_index_$USERNAME" ]; then
  76. echo $"Removing Dovecot indexes and uids"
  77. find "$MAIL_DIR" -type f -regex '.*\(dovecot-\|dovecot\.\|\.uidvalidity\).*' -delete
  78. # Remove the temporary file
  79. unlink "/tmp/rebuild_index_$USERNAME"
  80. else
  81. echo -n $"No messages found needing GPG encryption, not"
  82. echo $"removing Dovecot indexes and UIDs."
  83. fi
  84. exit 0