summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: 13c35e200dda88bfe86bd188e7e4195ca2e89382 (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.32 2006-10-18 12:00:32 jonas Exp $
  7. #
  8. # Generate virtual file for postfix
  9. #
  10. # This script provides a poor-man's email ISP tool using the standard
  11. # Unix users/groups database (with generic getent lookups, supporting
  12. # smooth upgrades to LDAP or other enterprise systems supporting the
  13. # libc Name Service Switch.
  14. #
  15. #
  16. # Email domains
  17. # -------------
  18. #
  19. # A system group is needed to locate email domains:
  20. #
  21. # # addgroup --system maildomains
  22. #
  23. #
  24. # Each domain (or group of syncronously maintained domains) needs a
  25. # virtual user, added to that central group:
  26. #
  27. # # adduser --system --no-create-home --group --disabled-password abcde
  28. #
  29. # # adduser abcde maildomains
  30. #
  31. # In the "Other" field of the GECOS info, the virtual user accounts has
  32. # hint(s) on the email domains tied to that domain:
  33. #
  34. # # usermod -c "@abc.com @cde.org" abcde
  35. #
  36. # The GECOS info has limited space. A domain group can span multiple
  37. # virtual accounts by mentioning the secondary account names in the
  38. # group field of the main virtual account.
  39. # "Office" or "roomnumber" field of the primary mailgroup (include the
  40. # primary mailgroup itself too, if using this filed at all!).
  41. #
  42. #
  43. # Email addresses
  44. # ---------------
  45. #
  46. # In the "Other" field of the GECOS info, real user accounts has hint(s)
  47. # on the email userparts tied to that user:
  48. #
  49. # # usermod -c "db@ dbowie@abcde +@xyz" david
  50. #
  51. #
  52. # Suggestion: Add postmaster-specific hints to the root account
  53. # (default: "postmaster@"):
  54. #
  55. # # usermod -c "postmaster@ hostmaster@ support@" root
  56. #
  57. #
  58. # Postfix lookup tables
  59. # ---------------------
  60. #
  61. # Lookup table for postfix is generated like this:
  62. #
  63. # # localmaildomaincreate
  64. #
  65. # ..and if satisified, apply the new file to the running system:
  66. #
  67. # # localmaildomainupdate
  68. #
  69. use strict;
  70. use User::pwent;
  71. use User::grent;
  72. my (%username, %fullname, %office, %workphone, %homephone, %other);
  73. my (%username_by_gid, %addresshints, %domains);
  74. while (my $pw = getpwent()) {
  75. $username_by_gid{$pw->gid} = $pw->name;
  76. ($fullname{$pw->name}, $office{$pw->name}, $workphone{$pw->name}, $homephone{$pw->name}, $other{$pw->name}) = split /\s*,\s*/, $pw->gecos;
  77. if (defined($other{$pw->name})) {
  78. @{$addresshints{$pw->name}} = grep {/^([\.[:alnum:]_-]+|\+)?@([\.[:alnum:]_-]+)?$/} split /\s+/, $other{$pw->name};
  79. }
  80. }
  81. my (%owner, %members, %groups);
  82. while (my $gr = getgrent()) {
  83. $owner{$gr->name} = $username_by_gid{$gr->gid};
  84. $members{$gr->name} = $gr->members;
  85. foreach my $member (@{$members{$gr->name}}) {
  86. push @{$groups{$member}}, $gr->name unless (defined($owner{$gr->name}) and $member eq $owner{$gr->name});
  87. }
  88. }
  89. my (%warned);
  90. sub warnonce($$$) {
  91. my ($test, $id, $warning) = @_;
  92. if ($test) {
  93. return 1;
  94. }
  95. if ($warned{$id}) {
  96. return '';
  97. }
  98. print STDERR 'W: ' . $warning . "\n";
  99. $warned{$id} = 1;
  100. return '';
  101. }
  102. sub print_accounts($$$$) {
  103. my ($username, $mailgroup, $maildomain, $pre_text, $post_fallback_text) = @_;
  104. ($pre_text) && print $pre_text . "\n";
  105. my $joker_seen;
  106. if (&warnonce(defined(@{$addresshints{$username}}), "addresshints_$username", "Skipping non-hinted username \"$username\".")) {
  107. my @localparthints = @{$addresshints{$username}};
  108. my @localparts = grep {s/(.+)@($mailgroup|$maildomain)?$/$1/} @localparthints;
  109. foreach my $localpart (@localparts) {
  110. for ($localpart) {
  111. if (/^\+$/) {
  112. if (!$joker_seen) {
  113. print "\@$maildomain $username\n";
  114. $joker_seen = $username;
  115. } else {
  116. print "#WARNING: Catch-all for $maildomain already set to $joker_seen!";
  117. }
  118. } else {
  119. print "$localpart\@$maildomain $username\n";
  120. }
  121. }
  122. }
  123. } else {
  124. print $post_fallback_text . "\n" if ($post_fallback_text);
  125. }
  126. print "\n";
  127. }
  128. sub usercomment($) {
  129. my $user = shift;
  130. my @s = ();
  131. if (&warnonce(defined($fullname{$user}), "fullname_$user", "User \"$user\" lacks fullname.")) {
  132. push @s, $fullname{$user};
  133. }
  134. if (&warnonce(defined(@{$groups{$user}}), "groups_$user", "User \"$user\" belongs to no (secondary) group.")) {
  135. my @groups_sorted = sort @{$groups{$user}};
  136. push @s, '(' . join(' ', @groups_sorted) . ')';
  137. }
  138. if (@s) {
  139. unshift @s, '#';
  140. }
  141. my $string = join(' ', @s);
  142. return "$string";
  143. }
  144. my $loop;
  145. my @mailgroups = @ARGV ? @ARGV : @{$members{'maildomains'}};
  146. foreach my $mailgroup (@mailgroups) {
  147. if (not &warnonce(defined(@{$addresshints{$mailgroup}}), "addresshints_$mailgroup", "Skipping empty mailgroup \"$mailgroup\".")) {
  148. next;
  149. }
  150. my @maildomainhints = @{$addresshints{$mailgroup}};
  151. my @maildomains = grep {s/^@(.+)/$1/} @maildomainhints;
  152. foreach my $maildomain (@maildomains) {
  153. my (@mailgroupowners, @mailusers);
  154. my @mailgroupgroups = split / +/, $office{$mailgroup};
  155. push @mailgroupgroups, $mailgroup unless (@mailgroupgroups);
  156. foreach my $mailgroupgroup (@mailgroupgroups) {
  157. push @mailgroupowners, $owner{$mailgroupgroup} if ($owner{$mailgroupgroup});
  158. push @mailusers, @{$members{$mailgroupgroup}};
  159. }
  160. my @mailgroupowners_sorted = sort @mailgroupowners;
  161. my @mailusers_sorted = sort @mailusers;
  162. if ($loop) {
  163. print "\n##################################################################\n\n";
  164. }
  165. $loop++;
  166. &print_accounts('root', $mailgroup, $maildomain, "$maildomain VIRTUAL", "postmaster\@$maildomain root");
  167. # Do mailgroup owners (and don't warn if there's no addresses attached)
  168. foreach my $mailgroupowner (@mailgroupowners_sorted) {
  169. &print_accounts($mailgroupowner, $mailgroup, $maildomain, &usercomment($mailgroupowner));
  170. }
  171. # Do secondary mailgroup members
  172. foreach my $mailuser (@mailusers_sorted) {
  173. &print_accounts($mailuser, $mailgroup, $maildomain, &usercomment($mailuser), "#WARNING: No addresses for $mailuser");
  174. }
  175. }
  176. }