summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: b2b94a70c6cf037827e5d3168ab125918f7ef407 (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.10 2002-11-04 22:43:31 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. for mailaccountchunk in `get_other_field $uid`; do
  46. for mailaccount in `get_account $mailaccountchunk`; do
  47. case $mailaccount in
  48. '+')
  49. if [ -z "$joker_seen" ]; then
  50. echo "@$maildomain $uid"
  51. joker_seen="$uid"
  52. else
  53. echo "#WARNING: Catch-all for $maildomain already set to $joker_seen: `get_fullname_field $uid`"
  54. fi
  55. ;;
  56. *)
  57. echo "$mailaccount@$maildomain $uid"
  58. ;;
  59. esac
  60. uid_seen=1
  61. done
  62. done
  63. test -n "$post_fallback_text" -a -z "$uid_seen" && echo "$post_fallback_text"
  64. echo
  65. }
  66. loop=""
  67. for gid in $@; do
  68. for maildomainchunk in `get_other_field $gid`; do
  69. for maildomain in `get_domain $maildomainchunk`; do
  70. if [ $loop ]; then
  71. echo
  72. echo "##################################################################"
  73. echo
  74. fi
  75. loop=true
  76. print_accounts root "$maildomain" "$maildomain VIRTUAL" "postmaster@$maildomain root"
  77. mailgroupowners=""
  78. mailusers=""
  79. mailgroups=`get_roomnumber_field $gid`
  80. test -z "$mailgroups" && mailgroups=$gid
  81. for mailgroup in $mailgroups; do
  82. mailgroupowners="$mailgroup_owners `members -p $mailgroup`"
  83. mailusers="$mailusers `members -s $mailgroup`"
  84. done
  85. # Do mailgroup owners (and don't warn if there's no addresses attached)
  86. for uid in `echo $mailgroupowners | tsort | uniq | sort`; do
  87. print_accounts $uid "$maildomain" "# `get_fullname_field $uid` (`get_groups $uid`)" ""
  88. done
  89. # Do secondary mailgroup members
  90. for uid in `echo $mailusers | tsort | uniq | sort`; do
  91. print_accounts $uid "$maildomain" "# `get_fullname_field $uid` (`get_groups $uid`)" "#WARNING: No addresses for $uid"
  92. done
  93. done
  94. done
  95. done