summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: 79ad78f0386a8104c1aac18873f3894dbe3e8b55 (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.9 2002-11-03 03:06:32 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. echo "$mailaccount@$maildomain $uid"
  48. uid_seen=1
  49. done
  50. done
  51. test -n "$post_fallback_text" -a -z "$uid_seen" && echo "$post_fallback_text"
  52. echo
  53. }
  54. loop=""
  55. for gid in $@; do
  56. for maildomainchunk in `get_other_field $gid`; do
  57. for maildomain in `get_domain $maildomainchunk`; do
  58. if [ $loop ]; then
  59. echo
  60. echo "##################################################################"
  61. echo
  62. fi
  63. loop=true
  64. print_accounts root "$maildomain" "$maildomain VIRTUAL" "postmaster@$maildomain root"
  65. mailusers=""
  66. mailgroups=`get_roomnumber_field $gid`
  67. [ -z "$mailgroups" ] && mailgroups=$gid
  68. for mailgroup in $mailgroups; do
  69. mailusers="$mailusers `members $mailgroup`"
  70. done
  71. # for uid in `members $gid | sort`; do
  72. for uid in `echo $mailusers | tsort | uniq | grep -v $gid | sort`; do
  73. print_accounts $uid "$maildomain" "# `get_fullname_field $uid` (`get_groups $uid`)" "#WARNING: No addresses for $uid"
  74. done
  75. done
  76. done
  77. done