summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: 12af73f3f2fb0ebcdbdffdb898f9eb070f1d8a5b (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.8 2002-09-04 23:35:52 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. # Suggestion: Add mailgroup users like this:
  25. # adduser --system --no-create-home --group --disabled-password <uid>
  26. #
  27. # TODO: reuse getent requests (drastically improves speed)
  28. # TODO: Write command "members" as internal code
  29. #
  30. function get_fullname_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $1}'; }
  31. function get_roomnumber_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $2}'; }
  32. function get_other_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $5}'; }
  33. function get_groups() { groups $1 | sed -e 's/^.*: //' -e "s/\( \+\|^\)$1\( \+\|$\)/\1/"; }
  34. function get_domain() { echo $1 | egrep "^@[\.[:alnum:]-]+$" | sed -e 's/@//'; }
  35. function get_account() { echo $1 | egrep "^[\.[:alnum:]-]+@($gid)?$" | sed -e 's/@.*//'; }
  36. loop=""
  37. for gid in $@; do
  38. for maildomainchunk in `get_other_field $gid`; do
  39. for maildomain in `get_domain $maildomainchunk`; do
  40. if [ $loop ]; then
  41. echo
  42. echo "##################################################################"
  43. echo
  44. fi
  45. loop=true
  46. echo "$maildomain VIRTUAL"
  47. uid_seen=""
  48. for mailaccountchunk in `get_other_field root`; do
  49. for mailaccount in `get_account $mailaccountchunk`; do
  50. echo "$mailaccount@$maildomain root"
  51. uid_seen=true
  52. done
  53. done
  54. if [ ! $uid_seen ]; then
  55. echo "postmaster@$maildomain root"
  56. fi
  57. echo
  58. mailusers=""
  59. mailgroups=`get_roomnumber_field $gid`
  60. [ -z "$mailgroups" ] && mailgroups=$gid
  61. for mailgroup in $mailgroups; do
  62. mailusers="$mailusers `members $mailgroup`"
  63. done
  64. # for uid in `members $gid | sort`; do
  65. for uid in `echo $mailusers | tsort | uniq | sort`; do
  66. echo "# `get_fullname_field $uid` (`get_groups $uid`)"
  67. uid_seen=""
  68. for mailaccountchunk in `get_other_field $uid`; do
  69. for mailaccount in `get_account $mailaccountchunk`; do
  70. echo "$mailaccount@$maildomain $uid"
  71. uid_seen=true
  72. done
  73. done
  74. if [ ! $uid_seen ]; then
  75. echo "#WARNING: No addresses for $uid"
  76. fi
  77. echo
  78. done
  79. done
  80. done
  81. done