summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: 654de19beebee9b4e901aa1772474105a8a33c92 (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.5 2002-03-07 16:22:51 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" for each hosted domain.
  15. #
  16. # Optional: root can have hints like "postmaster@ hostmaster@ support@" (default: "postmaster@").
  17. #
  18. # TODO: reuse getent requests (drastically improves speed)
  19. #
  20. function get_fullname_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $1}'; }
  21. function get_roomnumber_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $2}'; }
  22. function get_other_field() { getent passwd $1 | awk -F: '{print $5}' | awk -F, '{print $5}'; }
  23. function get_groups() { groups $1 | sed -e 's/^.*: //' -e "s/\( \+\|^\)$1\( \+\|$\)/\1/"; }
  24. function get_domain() { echo $1 | egrep "^@[\.[:alnum:]-]+$" | sed -e 's/@//'; }
  25. function get_account() { echo $1 | egrep "^[\.[:alnum:]-]+@($gid)?$" | sed -e 's/@.*//'; }
  26. loop=""
  27. for gid in $@; do
  28. for maildomainchunk in `get_other_field $gid`; do
  29. for maildomain in `get_domain $maildomainchunk`; do
  30. if [ $loop ]; then
  31. echo
  32. echo "##################################################################"
  33. echo
  34. fi
  35. loop=true
  36. echo "$maildomain VIRTUAL"
  37. uid_seen=""
  38. for mailaccountchunk in `get_other_field root`; do
  39. for mailaccount in `get_account $mailaccountchunk`; do
  40. echo "$mailaccount@$maildomain root"
  41. uid_seen=true
  42. done
  43. done
  44. if [ ! $uid_seen ]; then
  45. echo "postmaster@$maildomain root"
  46. fi
  47. echo
  48. mailusers=""
  49. for mailgroup in `get_roomnumber_field $gid`; do
  50. mailusers="$mailusers `members $mailgroup`"
  51. done
  52. # for uid in `members $gid | sort`; do
  53. for uid in `echo $mailusers | tsort | uniq | sort`; do
  54. echo "# `get_fullname_field $uid` (`get_groups $uid`)"
  55. uid_seen=""
  56. for mailaccountchunk in `get_other_field $uid`; do
  57. for mailaccount in `get_account $mailaccountchunk`; do
  58. echo "$mailaccount@$maildomain $uid"
  59. uid_seen=true
  60. done
  61. done
  62. if [ ! $uid_seen ]; then
  63. echo "#WARNING: No addresses for $uid"
  64. fi
  65. echo
  66. done
  67. done
  68. done
  69. done