summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: f093d00c32903cd50f839d5696f6f135fdfe6989 (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.19 2006-08-02 16:00:12 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 sort_u() { for x in $@; do echo $x; done | sort -u; }
  47. function print_accounts() {
  48. uid=$1
  49. maildomain=$2
  50. pre_text=$3
  51. post_fallback_text=$4
  52. test -n "$pre_text" && echo "$pre_text"
  53. uid_seen=""
  54. joker_seen=""
  55. for mailaccountchunk in `get_other_field $uid`; do
  56. for mailaccount in `get_account $mailaccountchunk`; do
  57. case $mailaccount in
  58. '+')
  59. if [ -z "$joker_seen" ]; then
  60. echo "@$maildomain $uid"
  61. joker_seen="$uid"
  62. else
  63. echo "#WARNING: Catch-all for $maildomain already set to $joker_seen: `get_fullname_field $uid`"
  64. fi
  65. ;;
  66. *)
  67. echo "$mailaccount@$maildomain $uid"
  68. ;;
  69. esac
  70. uid_seen=1
  71. done
  72. done
  73. test -n "$post_fallback_text" -a -z "$uid_seen" && echo "$post_fallback_text"
  74. echo
  75. }
  76. loop=""
  77. if [ $# -lt 1 ]; then
  78. mailgroups="`members maildomains`"
  79. else
  80. mailgroups="$@"
  81. fi
  82. for mailgroup in $mailgroups; do
  83. for maildomainchunk in `get_other_field $mailgroup`; do
  84. for maildomain in `get_domain $maildomainchunk`; do
  85. if [ $loop ]; then
  86. echo
  87. echo "##################################################################"
  88. echo
  89. fi
  90. loop=true
  91. print_accounts root "$maildomain" "$maildomain VIRTUAL" "postmaster@$maildomain root"
  92. mailgroupowners=""
  93. mailusers=""
  94. mailgroupgroups="`get_roomnumber_field $mailgroup`"
  95. test -z "$mailgroupgroups" && mailgroupgroups="$mailgroup"
  96. for mailgroupgroup in $mailgroupgroups; do
  97. mailgroupowners="$mailgroupowners `members -p "$mailgroupgroup"`"
  98. mailusers="$mailusers `members -s "$mailgroupgroup"`"
  99. done
  100. # Do mailgroup owners (and don't warn if there's no addresses attached)
  101. for mailgroupowner in `sort_u $mailgroupowners`; do
  102. print_accounts $mailgroupowner "$maildomain" "# `get_fullname_field "$mailgroupowner"` (`get_groups "$mailgroupowner"`)" ""
  103. done
  104. # Do secondary mailgroup members
  105. for mailuser in `sort_u $mailusers`; do
  106. print_accounts "$mailuser" "$maildomain" "# `get_fullname_field "$mailuser"` (`get_groups "$mailuser"`)" "#WARNING: No addresses for $mailuser"
  107. done
  108. done
  109. done
  110. done