summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: f04987c8076e20217ddb0c5ed276571dffc78372 (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.6 2002-07-20 18:30:19 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 "mailname1@ mailname2@gid1 mailname3@gid2".
  13. #
  14. # The user of each mailgroup should have hints like "@domain1 @domain2"
  15. # for each hosted domain.
  16. #
  17. # Optional: More than one mailgroup can be grouped: List them all in
  18. # "Office" or "roomnumber" field of primary mailgroup (include the
  19. # primary mailgroup itself!).
  20. #
  21. # Optional: root can have hints like "postmaster@ hostmaster@ support@"
  22. # (default: "postmaster@").
  23. #
  24. # TODO: reuse getent requests (drastically improves speed)
  25. # TODO: Write command "members" as internal code
  26. #
  27. function get_fullname_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $1}'; }
  28. function get_roomnumber_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $2}'; }
  29. function get_other_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $5}'; }
  30. function get_groups() { groups $1 | sed -e 's/^.*: //' -e "s/\( \+\|^\)$1\( \+\|$\)/\1/"; }
  31. function get_domain() { echo $1 | egrep "^@[\.[:alnum:]-]+$" | sed -e 's/@//'; }
  32. function get_account() { echo $1 | egrep "^[\.[:alnum:]-]+@($gid)?$" | sed -e 's/@.*//'; }
  33. loop=""
  34. for gid in $@; do
  35. for maildomainchunk in `get_other_field $gid`; do
  36. for maildomain in `get_domain $maildomainchunk`; do
  37. if [ $loop ]; then
  38. echo
  39. echo "##################################################################"
  40. echo
  41. fi
  42. loop=true
  43. echo "$maildomain VIRTUAL"
  44. uid_seen=""
  45. for mailaccountchunk in `get_other_field root`; do
  46. for mailaccount in `get_account $mailaccountchunk`; do
  47. echo "$mailaccount@$maildomain root"
  48. uid_seen=true
  49. done
  50. done
  51. if [ ! $uid_seen ]; then
  52. echo "postmaster@$maildomain root"
  53. fi
  54. echo
  55. mailusers=""
  56. mailgroups=`get_roomnumber_field $gid`
  57. [ -z "$mailgroups" ] || mailgroups=$gid
  58. for mailgroup in $mailgroups; do
  59. mailusers="$mailusers `members $mailgroup`"
  60. done
  61. # for uid in `members $gid | sort`; do
  62. for uid in `echo $mailusers | tsort | uniq | sort`; do
  63. echo "# `get_fullname_field $uid` (`get_groups $uid`)"
  64. uid_seen=""
  65. for mailaccountchunk in `get_other_field $uid`; do
  66. for mailaccount in `get_account $mailaccountchunk`; do
  67. echo "$mailaccount@$maildomain $uid"
  68. uid_seen=true
  69. done
  70. done
  71. if [ ! $uid_seen ]; then
  72. echo "#WARNING: No addresses for $uid"
  73. fi
  74. echo
  75. done
  76. done
  77. done
  78. done