postfixadmin-mailbox-postdeletion.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/sh
  2. # Example script for removing a Maildir from a Courier-IMAP virtual mail
  3. # hierarchy.
  4. # The script looks at arguments 1 and 2, assuming that they
  5. # indicate username and domain, respectively.
  6. # The script will not actually delete the maildir. I moves it
  7. # to a special directory which may once in a while be cleaned up
  8. # by the system administrator.
  9. # This script should be run as the user which owns the maildirs. If
  10. # the script is actually run by the apache user (e.g. through PHP),
  11. # then you could use "sudo" to grant apache the rights to run
  12. # this script as the relevant user.
  13. # Assume this script has been saved as
  14. # /usr/local/bin/postfixadmin-mailbox-postdeletion.sh and has been
  15. # made executable. Now, an example /etc/sudoers line:
  16. # apache ALL=(courier) NOPASSWD: /usr/local/bin/postfixadmin-mailbox-postdeletion.sh
  17. # The line states that the apache user may run the script as the
  18. # user "courier" without providing a password.
  19. # Change this to where you keep your virtual mail users' maildirs.
  20. basedir=/var/spool/maildirs
  21. # Change this to where you would like deleted maildirs to reside.
  22. trashbase=/var/spool/deleted-maildirs
  23. if [ ! -e "$trashbase" ]; then
  24. echo "trashbase '$trashbase' does not exist; bailing out."
  25. exit 1
  26. fi
  27. if [ `echo $1 | fgrep '..'` ]; then
  28. echo "First argument contained a double-dot sequence; bailing out."
  29. exit 1
  30. fi
  31. if [ `echo $2 | fgrep '..'` ]; then
  32. echo "First argument contained a double-dot sequence; bailing out."
  33. exit 1
  34. fi
  35. subdir=`echo "$1" | sed 's/@.*//'`
  36. maildir="${basedir}/$2/${subdir}"
  37. trashdir="${trashbase}/$2/`date +%F_%T`_${subdir}"
  38. parent=`dirname "$trashdir"`
  39. if [ ! -d "$parent" ]; then
  40. if [ -e "$parent" ]; then
  41. echo "Strainge - directory '$parent' exists, but is not a directory."
  42. echo "Bailing out."
  43. exit 1
  44. else
  45. mkdir -p "$parent"
  46. if [ $? -ne 0 ]; then
  47. echo "mkdir -p '$parent' returned non-zero; bailing out."
  48. exit 1
  49. fi
  50. fi
  51. fi
  52. if [ ! -e "$maildir" ]; then
  53. echo "maildir '$maildir' does not exist; nothing to do."
  54. exit 1
  55. fi
  56. if [ -e "$trashdir" ]; then
  57. echo "trashdir '$trashdir' already exists; bailing out."
  58. exit 1
  59. fi
  60. mv $maildir $trashdir
  61. exit $?