summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: 9637ba63a4eeef4e3a94f3670fc0bcec5060c76f (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.11 2002-11-24 17:17:13 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. # Optional: Several mailgroups can be tied together (when amount of hints
  19. # exceeds the limit of the "Other" field: List them all in
  20. # "Office" or "roomnumber" field of primary mailgroup (include the
  21. # primary mailgroup itself!).
  22. #
  23. # Optional: root can have hints like "postmaster@ hostmaster@ support@"
  24. # for default accounts tied to the sysadmin (default: "postmaster@").
  25. #
  26. # Suggestion: Add mailgroup users like this:
  27. # adduser --system --no-create-home --group --disabled-password <uid>
  28. #
  29. # TODO: reuse getent requests (drastically improves speed)
  30. # TODO: Write command "members" as internal code
  31. #
  32. function get_fullname_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $1}'; }
  33. function get_roomnumber_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $2}'; }
  34. function get_other_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $5}'; }
  35. function get_groups() { groups $1 | sed -e 's/^.*: //' -e "s/\( \+\|^\)$1\( \+\|$\)/\1/"; }
  36. function get_domain() { echo $1 | egrep "^@[\.[:alnum:]-]+$" | sed -e 's/@//'; }
  37. function get_account() { echo $1 | egrep "^([\.[:alnum:]-]+|\+)@($gid|$maildomain)?$" | sed -e 's/@.*//'; }
  38. function print_accounts() {
  39. uid=$1
  40. maildomain=$2
  41. pre_text=$3
  42. post_fallback_text=$4
  43. test -n "$pre_text" && echo "$pre_text"
  44. uid_seen=""
  45. joker_seen=""
  46. for mailaccountchunk in `get_other_field $uid`; do
  47. for mailaccount in `get_account $mailaccountchunk`; do
  48. case $mailaccount in
  49. '+')
  50. if [ -z "$joker_seen" ]; then
  51. echo "@$maildomain $uid"
  52. joker_seen="$uid"
  53. else
  54. echo "#WARNING: Catch-all for $maildomain already set to $joker_seen: `get_fullname_field $uid`"
  55. fi
  56. ;;
  57. *)
  58. echo "$mailaccount@$maildomain $uid"
  59. ;;
  60. esac
  61. uid_seen=1
  62. done
  63. done
  64. test -n "$post_fallback_text" -a -z "$uid_seen" && echo "$post_fallback_text"
  65. echo
  66. }
  67. loop=""
  68. for gid in $@; do
  69. for maildomainchunk in `get_other_field $gid`; do
  70. for maildomain in `get_domain $maildomainchunk`; do
  71. if [ $loop ]; then
  72. echo
  73. echo "##################################################################"
  74. echo
  75. fi
  76. loop=true
  77. print_accounts root "$maildomain" "$maildomain VIRTUAL" "postmaster@$maildomain root"
  78. mailgroupowners=""
  79. mailusers=""
  80. mailgroups=`get_roomnumber_field $gid`
  81. test -z "$mailgroups" && mailgroups=$gid
  82. for mailgroup in $mailgroups; do
  83. mailgroupowners="$mailgroup_owners `members -p $mailgroup`"
  84. mailusers="$mailusers `members -s $mailgroup`"
  85. done
  86. # Do mailgroup owners (and don't warn if there's no addresses attached)
  87. for uid in `echo $mailgroupowners | tsort | uniq | sort`; do
  88. print_accounts $uid "$maildomain" "# `get_fullname_field $uid` (`get_groups $uid`)" ""
  89. done
  90. # Do secondary mailgroup members
  91. for uid in `echo $mailusers | tsort | uniq | sort`; do
  92. print_accounts $uid "$maildomain" "# `get_fullname_field $uid` (`get_groups $uid`)" "#WARNING: No addresses for $uid"
  93. done
  94. done
  95. done
  96. done