summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: 40d12df53642407c8cc6ab23ca8bb1830a1ecf65 (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.28 2006-08-22 23:11:33 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 unless (defined($owner{$gr->name}) and $gr->name eq $owner{$gr->name});
  57. }
  58. }
  59. my (%warned);
  60. sub warnonce($$$) {
  61. my ($test, $id, $warning) = @_;
  62. if ($test) {
  63. return 1;
  64. }
  65. if ($warned{$id}) {
  66. return '';
  67. }
  68. print STDERR 'W: ' . $warning . "\n";
  69. $warned{$id} = 1;
  70. return '';
  71. }
  72. sub print_accounts($$$$) {
  73. my ($username, $mailgroup, $maildomain, $pre_text, $post_fallback_text) = @_;
  74. ($pre_text) && print $pre_text . "\n";
  75. my $joker_seen;
  76. #DEBUG: print STDERR "$mailgroup $maildomain\n";
  77. #DEBUG: ($username eq "annette") and print STDERR Dumper(@{$addresshints{$username}});
  78. if (&warnonce(defined(@{$addresshints{$username}}), "addresshints_$username", "Skipping non-hinted username \"$username\".")) {
  79. my @localparthints = @{$addresshints{$username}};
  80. my @localparts = grep {s/(.+)@($mailgroup|$maildomain)?$/$1/} @localparthints;
  81. foreach my $localpart (@localparts) {
  82. for ($localpart) {
  83. if (/^\+$/) {
  84. if (!$joker_seen) {
  85. print "\@$maildomain $username\n";
  86. $joker_seen = $username;
  87. } else {
  88. print "#WARNING: Catch-all for $maildomain already set to $joker_seen!";
  89. }
  90. } else {
  91. print "$localpart\@$maildomain $username\n";
  92. }
  93. }
  94. }
  95. } else {
  96. print $post_fallback_text . "\n" if ($post_fallback_text);
  97. }
  98. print "\n";
  99. }
  100. sub usercomment($) {
  101. my $user = shift;
  102. my @s = ();
  103. if (&warnonce(defined($fullname{$user}), "fullname_$user", "User \"$user\" lacks fullname.")) {
  104. push @s, $fullname{$user};
  105. }
  106. if (&warnonce(defined(@{$groups{$user}}), "groups_$user", "User \"$user\" belongs to no (secondary) group.")) {
  107. push @s, '(' . join(' ', @{$groups{$user}}) . ')';
  108. }
  109. if (@s) {
  110. unshift @s, '#';
  111. }
  112. my $string = join(' ', @s);
  113. return "$string";
  114. }
  115. my $loop;
  116. my @mailgroups = @ARGV ? @ARGV : @{$members{'maildomains'}};
  117. foreach my $mailgroup (@mailgroups) {
  118. if (not &warnonce(defined(@{$addresshints{$mailgroup}}), "addresshints_$mailgroup", "Skipping empty mailgroup \"$mailgroup\".")) {
  119. next;
  120. }
  121. my @maildomainhints = @{$addresshints{$mailgroup}};
  122. my @maildomains = grep {s/^@(.+)/$1/} @maildomainhints;
  123. foreach my $maildomain (@maildomains) {
  124. my (@mailgroupowners, @mailusers);
  125. my @mailgroupgroups = split / +/, $office{$mailgroup};
  126. push @mailgroupgroups, $mailgroup unless (@mailgroupgroups);
  127. foreach my $mailgroupgroup (@mailgroupgroups) {
  128. push @mailgroupowners, $owner{$mailgroupgroup} if ($owner{$mailgroupgroup});
  129. push @mailusers, @{$members{$mailgroupgroup}};
  130. }
  131. my @mailgroupowners_sorted = sort @mailgroupowners;
  132. my @mailusers_sorted = sort @mailusers;
  133. #DEBUG: print STDERR Dumper(@mailusers); die;
  134. if ($loop) {
  135. print "\n##################################################################\n\n";
  136. }
  137. $loop++;
  138. &print_accounts('root', $mailgroup, $maildomain, "$maildomain VIRTUAL", "postmaster\@$maildomain root");
  139. # Do mailgroup owners (and don't warn if there's no addresses attached)
  140. foreach my $mailgroupowner (@mailgroupowners_sorted) {
  141. &print_accounts($mailgroupowner, $mailgroup, $maildomain, &usercomment($mailgroupowner));
  142. }
  143. # Do secondary mailgroup members
  144. foreach my $mailuser (@mailusers_sorted) {
  145. # &print_accounts($mailuser, $mailgroup, $maildomain, '# ' . $fullname{$mailuser} . ' (' . join(' ', @{$groups{$mailuser}}) . ')', "#WARNING: No addresses for $mailuser");
  146. &print_accounts($mailuser, $mailgroup, $maildomain, &usercomment($mailuser), "#WARNING: No addresses for $mailuser");
  147. }
  148. }
  149. }