summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: d75c270b5d2ec1748ad41d2f2f8b67e86eca8159 (plain)
  1. #!/bin/bash
  2. #
  3. # /usr/local/sbin/localmkpostfixvirtual
  4. # Copyright 2001-2002 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localmkpostfixvirtual,v 1.16 2004-06-17 01:26:29 jonas Exp $
  7. #
  8. # Generate virtual file for postfix
  9. #
  10. # Hints are stored in the "Other" field (using chfn).
  11. #
  12. # Each user should have space-separated hints like this:
  13. # "mailname1@ mailname2@mailgroup1 mailname3@mailgroup2 mailname4@maildomain7".
  14. #
  15. # The user of each mailgroup should have hints like "@domain1 @domain2"
  16. # for each hosted maildomain.
  17. #
  18. # Generate virtual file like this:
  19. #
  20. # # ( cd /etc/postfix && localmkpostfixvirtual > virtual.new && diff virtual virtual.new )
  21. #
  22. # ..and if the changes are correct, activate the new virtual file:
  23. #
  24. # # ( cd /etc/postfix && mv virtual.new virtual && postmap virtual )
  25. #
  26. # Optional: Several mailgroups can be tied together (when amount of hints
  27. # exceeds the limit of the "Other" field: List them all in
  28. # "Office" or "roomnumber" field of primary mailgroup (include the
  29. # primary mailgroup itself!).
  30. #
  31. # Optional: root can have hints like "postmaster@ hostmaster@ support@"
  32. # for default accounts tied to the sysadmin (default: "postmaster@").
  33. #
  34. # Suggestion: Add mailgroup users like this:
  35. # adduser --system --no-create-home --group --disabled-password <uid>
  36. #
  37. # TODO: reuse getent requests (drastically improves speed)
  38. # TODO: Write command "members" as internal code
  39. #
  40. function get_fullname_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $1}'; }
  41. function get_roomnumber_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $2}'; }
  42. function get_other_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $5}'; }
  43. function get_groups() { groups $1 | sed -e 's/^.*: //' -e "s/\( \+\|^\)$1\( \+\|$\)/\1/"; }
  44. function get_domain() { echo $1 | egrep "^@[\.[:alnum:]-]+$" | sed -e 's/@//'; }
  45. function get_account() { echo $1 | egrep "^([\.[:alnum:]_-]+|\+)@($gid|$maildomain)?$" | sed -e 's/@.*//'; }
  46. function print_accounts() {
  47. uid=$1
  48. maildomain=$2
  49. pre_text=$3
  50. post_fallback_text=$4
  51. test -n "$pre_text" && echo "$pre_text"
  52. uid_seen=""
  53. joker_seen=""
  54. for mailaccountchunk in `get_other_field $uid`; do
  55. for mailaccount in `get_account $mailaccountchunk`; do
  56. case $mailaccount in
  57. '+')
  58. if [ -z "$joker_seen" ]; then
  59. echo "@$maildomain $uid"
  60. joker_seen="$uid"
  61. else
  62. echo "#WARNING: Catch-all for $maildomain already set to $joker_seen: `get_fullname_field $uid`"
  63. fi
  64. ;;
  65. *)
  66. echo "$mailaccount@$maildomain $uid"
  67. ;;
  68. esac
  69. uid_seen=1
  70. done
  71. done
  72. test -n "$post_fallback_text" -a -z "$uid_seen" && echo "$post_fallback_text"
  73. echo
  74. }
  75. loop=""
  76. if [ $# -lt 1 ]; then
  77. mailgroups="`members maildomains`"
  78. else
  79. mailgroups="$@"
  80. fi
  81. for gid in $mailgroups; do
  82. for maildomainchunk in `get_other_field $gid`; do
  83. for maildomain in `get_domain $maildomainchunk`; do
  84. if [ $loop ]; then
  85. echo
  86. echo "##################################################################"
  87. echo
  88. fi
  89. loop=true
  90. print_accounts root "$maildomain" "$maildomain VIRTUAL" "postmaster@$maildomain root"
  91. mailgroupowners=""
  92. mailusers=""
  93. mailgroups=`get_roomnumber_field $gid`
  94. test -z "$mailgroups" && mailgroups=$gid
  95. for mailgroup in $mailgroups; do
  96. mailgroupowners="$mailgroup_owners `members -p $mailgroup`"
  97. mailusers="$mailusers `members -s $mailgroup`"
  98. done
  99. # Do mailgroup owners (and don't warn if there's no addresses attached)
  100. for uid in `for x in $mailgroupowners; do echo $x; done | uniq | sort`; do
  101. print_accounts $uid "$maildomain" "# `get_fullname_field $uid` (`get_groups $uid`)" ""
  102. done
  103. # Do secondary mailgroup members
  104. for uid in `for x in $mailusers; do echo $x; done | uniq | sort`; do
  105. print_accounts $uid "$maildomain" "# `get_fullname_field $uid` (`get_groups $uid`)" "#WARNING: No addresses for $uid"
  106. done
  107. done
  108. done
  109. done
  110. test -f /etc/postfix/virtual.addon && cat /etc/postfix/virtual.addon || true