summaryrefslogtreecommitdiff
path: root/localmailtransportupdate
blob: 2dcceb9ff60b58cfe98beb0f11015b1e009a32f2 (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/sbin/localmailtransportinit
  4. # Copyright 2006 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localmailtransportupdate,v 1.2 2006-07-28 17:04:48 jonas Exp $
  7. #
  8. # Setup postfix transport map for a redpill organisation
  9. #
  10. set -e
  11. PRG=`basename $0`
  12. TEMP=$(getopt -s sh -o fid:h -l force,init,diffopts:,help -n "$PRG" -- "$@")
  13. if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  14. eval set -- "$TEMP"
  15. function usage() {
  16. echo "Usage: $PRG [opts...]"
  17. echo
  18. echo " -f, --force Update without asking for confirmation"
  19. echo " -i, --init Setup postfix to use the transport map"
  20. echo " -d, --diffopts=diffopts Extra options applied to diff when"
  21. echo " comparing current and new map"
  22. echo " -h|--help This help text"
  23. exit 1
  24. }
  25. function exit1() {
  26. echo "Error: $1"
  27. echo "Exiting..."
  28. exit 1
  29. }
  30. force=""
  31. init=""
  32. diffopts=""
  33. while true ; do
  34. case "$1" in
  35. -f|--force) force="1" ; shift ;;
  36. -i|--init) init="1" ; shift ;;
  37. -d|--diffopts) diffopts="1" ; shift ;;
  38. -h|--help) usage;;
  39. --) shift ; break ;;
  40. *) exit1 "Internal error!";;
  41. esac
  42. done
  43. firstitem() {
  44. file="$1"
  45. cat "$file" | grep -v '^#' | sed 's/#.*//' | head -n 1
  46. }
  47. items() {
  48. file="$1"
  49. cat "$file" | grep -v '^#' | sed 's/#.*//'
  50. }
  51. mktransportmap() {
  52. for realm in $@; do
  53. realmdir="/etc/local-$realm"
  54. [ -d "$realmdir/redpill" ] || continue
  55. mailhost="$(firstitem "$realmdir/redpill/mailhost")"
  56. maildomains="$(items "$realmdir/redpill/maildomains")"
  57. for maildomain in $maildomains; do
  58. echo "$maildomain relay:$mailhost:submission"
  59. done
  60. done
  61. }
  62. mktransportmap $(items /etc/local-ORG/realms) > /etc/postfix/transport.new
  63. if [ -f /etc/postfix/transport.addon ]; then
  64. cat /etc/postfix/transport.addon >> /etc/postfix/transport.new
  65. fi
  66. ( cd /etc/postfix && diff $diffopts transport transport.new )
  67. if [ "$force" = "1" ]; then
  68. do_update="y"
  69. else
  70. echo -n "Above is the intended changes. OK to update (y/N)? "
  71. read do_update
  72. fi
  73. case $do_update in
  74. y|Y)
  75. mv /etc/postfix/transport.new /etc/postfix/transport
  76. postmap /etc/postfix/transport
  77. echo "Changes applied!"
  78. ;;
  79. *)
  80. echo "Aborted!"
  81. exit 1
  82. ;;
  83. esac
  84. if [ "$init" = "1" ]; then
  85. postconf -e "transport_maps = hash:/etc/postfix/transport"
  86. invoke-rc.d postfix reload
  87. fi