summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: 30143828ebef21721689fb74d76ad3b0cc5a5e5e (plain)
  1. #!/usr/bin/perl -wT
  2. #
  3. # /usr/local/sbin/localmkpostfixvirtual
  4. # Copyright 2001-2006 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localmkpostfixvirtual,v 1.22 2006-08-22 15:36:21 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. # Generate virtual file like this:
  19. #
  20. # # ( cd /etc/postfix && localmkpostfixvirtual > virtual.new && diff virtual virtual.new )
  21. #
  22. # ..and if the changes are correct, activate the new virtual file:
  23. #
  24. # # ( cd /etc/postfix && mv virtual.new virtual && postmap virtual )
  25. #
  26. # Optional: Several mailgroups can be tied together (when amount of hints
  27. # exceeds the limit of the "Other" field: List them all in
  28. # "Office" or "roomnumber" field of primary mailgroup (include the
  29. # primary mailgroup itself!).
  30. #
  31. # Optional: root can have hints like "postmaster@ hostmaster@ support@"
  32. # for default accounts tied to the sysadmin (default: "postmaster@").
  33. #
  34. # Suggestion: Add mailgroup users like this:
  35. # adduser --system --no-create-home --group --disabled-password <uid>
  36. #
  37. use strict;
  38. use User::pwent;
  39. use User::grent;
  40. #use Data::Dumper;
  41. my (%username, %fullname, %office, %workphone, %homephone, %other);
  42. my (%username_by_gid, %addresshints, %domains);
  43. while (my $pw = getpwent()) {
  44. $username_by_gid{$pw->gid} = $pw->name;
  45. ($fullname{$pw->name}, $office{$pw->name}, $workphone{$pw->name}, $homephone{$pw->name}, $other{$pw->name}) = split /\s*,\s*/, $pw->gecos;
  46. if (defined($other{$pw->name})) {
  47. @{$addresshints{$pw->name}} = grep {/^([\.[:alnum:]_-]+|\+)?@([\.[:alnum:]_-]+)?$/} split /\s+/, $other{$pw->name};
  48. }
  49. #DEBUG: ($pw->name eq "annette") and print STDERR Dumper(@{$addresses{$pw->name}});
  50. }
  51. my (%owner, %members, %groups);
  52. while (my $gr = getgrent()) {
  53. $owner{$gr->name} = $username_by_gid{$gr->gid};
  54. $members{$gr->name} = $gr->members;
  55. foreach my $member (@{$members{$gr->name}}) {
  56. push @{$groups{$member}}, $gr->name;
  57. }
  58. }
  59. sub print_accounts($$$$) {
  60. my ($username, $mailgroup, $maildomain, $pre_text, $post_fallback_text) = @_;
  61. ($pre_text) && print $pre_text . "\n";
  62. my $user_seen;
  63. my $joker_seen;
  64. #DEBUG: print STDERR "$mailgroup $maildomain\n";
  65. #DEBUG: ($username eq "annette") and print STDERR Dumper(@{$addresshints{$username}});
  66. my @localparthints = @{$addresshints{$username}};
  67. my @localparts = grep {s/(.+)@($mailgroup|$maildomain)?$/$1/} @localparthints;
  68. foreach my $localpart (@localparts) {
  69. for ($localpart) {
  70. if (/^\+$/) {
  71. if (!$joker_seen) {
  72. print "\@$maildomain $username\n";
  73. $joker_seen = $username;
  74. } else {
  75. print "#WARNING: Catch-all for $maildomain already set to $joker_seen!";
  76. }
  77. } else {
  78. print "$localpart\@$maildomain $username\n";
  79. }
  80. }
  81. $user_seen++;
  82. }
  83. print $post_fallback_text . "\n" if ($post_fallback_text && !$user_seen);
  84. print "\n";
  85. }
  86. my $loop;
  87. my @mailgroups = @ARGV ? @ARGV : @{$members{'maildomains'}};
  88. foreach my $mailgroup (@mailgroups) {
  89. if (not defined(@{$addresshints{$mailgroup}})) {
  90. print STDERR "W: Skipping empty mailgroup \"$mailgroup\".";
  91. next;
  92. }
  93. my @maildomainhints = @{$addresshints{$mailgroup}};
  94. my @maildomains = grep {s/^@(.+)/$1/} @maildomainhints;
  95. foreach my $maildomain (@maildomains) {
  96. my (@mailgroupowners, @mailusers);
  97. my @mailgroupgroups = split / +/, $office{$mailgroup};
  98. push @mailgroupgroups, $mailgroup unless (@mailgroupgroups);
  99. foreach my $mailgroupgroup (@mailgroupgroups) {
  100. push @mailgroupowners, $owner{$mailgroupgroup} if ($owner{$mailgroupgroup});
  101. push @mailusers, @{$members{$mailgroupgroup}};
  102. }
  103. my @mailgroupowners_sorted = sort @mailgroupowners;
  104. my @mailusers_sorted = sort @mailusers;
  105. #DEBUG: print STDERR Dumper(@mailusers); die;
  106. if ($loop) {
  107. print "\n##################################################################\n\n";
  108. }
  109. $loop++;
  110. &print_accounts('root', $mailgroup, $maildomain, "$maildomain VIRTUAL", "postmaster\@$maildomain root");
  111. # Do mailgroup owners (and don't warn if there's no addresses attached)
  112. foreach my $mailgroupowner (@mailgroupowners_sorted) {
  113. &print_accounts($mailgroupowner, $mailgroup, $maildomain, '# ' . $fullname{$mailgroupowner} . ' (' . join(' ', @{$groups{$mailgroupowner}}) . ')');
  114. }
  115. # Do secondary mailgroup members
  116. foreach my $mailuser (@mailusers_sorted) {
  117. &print_accounts($mailuser, $mailgroup, $maildomain, '# ' . $fullname{$mailuser} . ' (' . join(' ', @{$groups{$mailuser}}) . ')', "#WARNING: No addresses for $mailuser");
  118. }
  119. }
  120. }