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