summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: 77a081ac661d818a07c6e1d758f5cfb657d8cd1f (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.23 2006-08-22 15:44:48 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. if (not defined(@{$addresshints{$username}})) {
  67. print STDERR "W: Skipping non-hinted username \"username\".";
  68. next;
  69. }
  70. my @localparthints = @{$addresshints{$username}};
  71. my @localparts = grep {s/(.+)@($mailgroup|$maildomain)?$/$1/} @localparthints;
  72. foreach my $localpart (@localparts) {
  73. for ($localpart) {
  74. if (/^\+$/) {
  75. if (!$joker_seen) {
  76. print "\@$maildomain $username\n";
  77. $joker_seen = $username;
  78. } else {
  79. print "#WARNING: Catch-all for $maildomain already set to $joker_seen!";
  80. }
  81. } else {
  82. print "$localpart\@$maildomain $username\n";
  83. }
  84. }
  85. $user_seen++;
  86. }
  87. print $post_fallback_text . "\n" if ($post_fallback_text && !$user_seen);
  88. print "\n";
  89. }
  90. my $loop;
  91. my @mailgroups = @ARGV ? @ARGV : @{$members{'maildomains'}};
  92. foreach my $mailgroup (@mailgroups) {
  93. if (not defined(@{$addresshints{$mailgroup}})) {
  94. print STDERR "W: Skipping empty mailgroup \"$mailgroup\".";
  95. next;
  96. }
  97. my @maildomainhints = @{$addresshints{$mailgroup}};
  98. my @maildomains = grep {s/^@(.+)/$1/} @maildomainhints;
  99. foreach my $maildomain (@maildomains) {
  100. my (@mailgroupowners, @mailusers);
  101. my @mailgroupgroups = split / +/, $office{$mailgroup};
  102. push @mailgroupgroups, $mailgroup unless (@mailgroupgroups);
  103. foreach my $mailgroupgroup (@mailgroupgroups) {
  104. push @mailgroupowners, $owner{$mailgroupgroup} if ($owner{$mailgroupgroup});
  105. push @mailusers, @{$members{$mailgroupgroup}};
  106. }
  107. my @mailgroupowners_sorted = sort @mailgroupowners;
  108. my @mailusers_sorted = sort @mailusers;
  109. #DEBUG: print STDERR Dumper(@mailusers); die;
  110. if ($loop) {
  111. print "\n##################################################################\n\n";
  112. }
  113. $loop++;
  114. &print_accounts('root', $mailgroup, $maildomain, "$maildomain VIRTUAL", "postmaster\@$maildomain root");
  115. # Do mailgroup owners (and don't warn if there's no addresses attached)
  116. foreach my $mailgroupowner (@mailgroupowners_sorted) {
  117. &print_accounts($mailgroupowner, $mailgroup, $maildomain, '# ' . $fullname{$mailgroupowner} . ' (' . join(' ', @{$groups{$mailgroupowner}}) . ')');
  118. }
  119. # Do secondary mailgroup members
  120. foreach my $mailuser (@mailusers_sorted) {
  121. &print_accounts($mailuser, $mailgroup, $maildomain, '# ' . $fullname{$mailuser} . ' (' . join(' ', @{$groups{$mailuser}}) . ')', "#WARNING: No addresses for $mailuser");
  122. }
  123. }
  124. }