freedombone-encrypt-mail 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # GPG Encrypt a Maildir using gpgit.pl
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  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. USERNAME=$1
  31. ADMIN_USER=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  32. if [ ! $USERNAME ]; then
  33. USERNAME=$ADMIN_USER
  34. fi
  35. MAIL_DIR=/home/$USERNAME/Maildir
  36. EMAIL_ADDRESS=$USERNAME@$HOSTNAME
  37. # Does this key exist?
  38. gpg --list-keys "$EMAIL_ADDRESS" > /dev/null 2>&1
  39. if [ $? -gt 0 ]; then
  40. echo $"A GPG key for $EMAIL_ADDRESS could not be found!"
  41. exit 0
  42. fi
  43. # Find all files in the Maildir specified.
  44. echo $"Calling find"
  45. find "$MAIL_DIR" -type f -regex '.*/\(cur\|new\)/.*' $4|while read line; do
  46. gpgit.pl --encrypt-mode prefer-inline "$EMAIL_ADDRESS" "/tmp/msg_$USERNAME"
  47. # Check to see if there are differences between the existing
  48. # Maildir file and what was created by gpgit.pl
  49. diff -qa "$line" "/tmp/msg_$USERNAME" > /dev/null 2>&1;
  50. if [ $? -gt 0 ]; then
  51. # Preserve timestamps, set ownership.
  52. chown $USERNAME:$USERNAME "/tmp/msg_$USERNAME"
  53. chmod 600 "/tmp/msg_$USERNAME"
  54. touch "/tmp/msg_$USERNAME" --reference="$line"
  55. # Unlink the original Maildir message
  56. unlink "$line"
  57. # Strip message sizes, retain experimental flags
  58. # and status flags, and copy the file over.
  59. STRIPSIZES=$(/bin/echo "$line"|/bin/sed -e "s/W=[[:digit:]]*//" -e "s/S=[[:digit:]]*//" -e "s/,,//" -e "s/,:2/:2/")
  60. cp -av "/tmp/msg_$USERNAME" "$STRIPSIZES"
  61. #Indexes must be rebuilt, weve modified Maildir.
  62. touch "/tmp/rebuild_index_$USERNAME"
  63. else
  64. echo $"Not copying, no differences between /tmp/msg_$USERNAME and $line"
  65. fi
  66. # Remove the temporary file
  67. unlink "/tmp/msg_$USERNAME"
  68. done
  69. # Remove Dovecot index and uids for regeneration.
  70. if [ -f "/tmp/rebuild_index_$USERNAME" ]; then
  71. echo $"Removing Dovecot indexes and uids"
  72. find "$MAIL_DIR" -type f -regex '.*\(dovecot-\|dovecot\.\|\.uidvalidity\).*' -delete
  73. # Remove the temporary file
  74. unlink "/tmp/rebuild_index_$USERNAME"
  75. else
  76. echo -n $"No messages found needing GPG encryption, not"
  77. echo $"removing Dovecot indexes and UIDs."
  78. fi
  79. exit 0