summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: 4c3c712e4e07c5fa74f7f37a48ca58f341ad94fa (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.34 2006-10-30 13:36:49 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 field 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@ abuse@ support@abcde" root
  56. #
  57. #
  58. # Postfix lookup tables
  59. # ---------------------
  60. #
  61. # Lookup table for postfix is generated/update with this command:
  62. #
  63. # # localmaildomainupdate
  64. #
  65. #
  66. # The above example entries should generate the following in
  67. # /etc/postfix/virtual:
  68. #
  69. # abc.com VIRTUAL
  70. # postmaster@abc.com root
  71. # abuse@abc.com root
  72. # support@abc.com root
  73. #
  74. # db@abc.com david
  75. # dbowie@abc.com david
  76. #
  77. # cde.org VIRTUAL
  78. # postmaster@cde.org root
  79. # abuse@cde.org root
  80. # support@cde.org root
  81. #
  82. # db@cde.org david
  83. # dbowie@cde.org david
  84. #
  85. # If another virtual domain account "xyz" is added with @xyz.net
  86. # in comment field, the following will be included as well:
  87. #
  88. # xyz.net VIRTUAL
  89. # postmaster@xyz.net
  90. # abuse@xyz.net
  91. #
  92. # db@xyz.net
  93. # @xyz.net
  94. #
  95. # (notice how support is only tied to the abcde domains, and the
  96. # specific dbowie userpart is substituted with a wildcard)
  97. use strict;
  98. use User::pwent;
  99. use User::grent;
  100. my (%username, %fullname, %office, %workphone, %homephone, %other);
  101. my (%username_by_gid, %addresshints, %domains);
  102. while (my $pw = getpwent()) {
  103. $username_by_gid{$pw->gid} = $pw->name;
  104. ($fullname{$pw->name}, $office{$pw->name}, $workphone{$pw->name}, $homephone{$pw->name}, $other{$pw->name}) = split /\s*,\s*/, $pw->gecos;
  105. if (defined($other{$pw->name})) {
  106. @{$addresshints{$pw->name}} = grep {/^([\.[:alnum:]_-]+|\+)?@([\.[:alnum:]_-]+)?$/} split /\s+/, $other{$pw->name};
  107. }
  108. }
  109. my (%owner, %members, %groups);
  110. while (my $gr = getgrent()) {
  111. $owner{$gr->name} = $username_by_gid{$gr->gid};
  112. $members{$gr->name} = $gr->members;
  113. foreach my $member (@{$members{$gr->name}}) {
  114. push @{$groups{$member}}, $gr->name unless (defined($owner{$gr->name}) and $member eq $owner{$gr->name});
  115. }
  116. }
  117. my (%warned);
  118. sub warnonce($$$) {
  119. my ($test, $id, $warning) = @_;
  120. if ($test) {
  121. return 1;
  122. }
  123. if ($warned{$id}) {
  124. return '';
  125. }
  126. print STDERR 'W: ' . $warning . "\n";
  127. $warned{$id} = 1;
  128. return '';
  129. }
  130. my (%username_by_localpart_by_maildomain);
  131. sub print_accounts($$$$) {
  132. my ($username, $mailgroup, $maildomain, $pre_text, $post_fallback_text) = @_;
  133. ($pre_text) && print $pre_text . "\n";
  134. my $joker_seen;
  135. if (&warnonce(defined(@{$addresshints{$username}}), "addresshints_$username", "Skipping non-hinted username \"$username\".")) {
  136. my @localparthints = @{$addresshints{$username}};
  137. my @localparts = grep {s/(.+)@($mailgroup|$maildomain)?$/$1/} @localparthints;
  138. foreach my $localpart (@localparts) {
  139. for ($localpart) {
  140. # FIXME: the below doesn't work as intended?!?
  141. # if (&warnonce(! defined(@{$username_by_localpart_by_maildomain{$_}}), "address_$_", "Skipping duplicate address \"$_\" for \"$username\").")) {
  142. # next;
  143. # }
  144. if (/^\+$/) {
  145. if (!$joker_seen) {
  146. print "\@$maildomain $username\n";
  147. $joker_seen = $username;
  148. } else {
  149. print "#WARNING: Catch-all for $maildomain already set to $joker_seen!";
  150. }
  151. } else {
  152. print "$localpart\@$maildomain $username\n";
  153. }
  154. }
  155. }
  156. } else {
  157. print $post_fallback_text . "\n" if ($post_fallback_text);
  158. }
  159. print "\n";
  160. }
  161. sub usercomment($) {
  162. my $user = shift;
  163. my @s = ();
  164. if (&warnonce(defined($fullname{$user}), "fullname_$user", "User \"$user\" lacks fullname.")) {
  165. push @s, $fullname{$user};
  166. }
  167. if (&warnonce(defined(@{$groups{$user}}), "groups_$user", "User \"$user\" belongs to no (secondary) group.")) {
  168. my @groups_sorted = sort @{$groups{$user}};
  169. push @s, '(' . join(' ', @groups_sorted) . ')';
  170. }
  171. if (@s) {
  172. unshift @s, '#';
  173. }
  174. my $string = join(' ', @s);
  175. return "$string";
  176. }
  177. my $loop;
  178. my @mailgroups = @ARGV ? @ARGV : @{$members{'maildomains'}};
  179. foreach my $mailgroup (@mailgroups) {
  180. if (not &warnonce(defined(@{$addresshints{$mailgroup}}), "addresshints_$mailgroup", "Skipping empty mailgroup \"$mailgroup\".")) {
  181. next;
  182. }
  183. my @maildomainhints = @{$addresshints{$mailgroup}};
  184. my @maildomains = grep {s/^@(.+)/$1/} @maildomainhints;
  185. foreach my $maildomain (@maildomains) {
  186. my (@mailgroupowners, @mailusers);
  187. my @mailgroupgroups = split / +/, $office{$mailgroup};
  188. push @mailgroupgroups, $mailgroup unless (@mailgroupgroups);
  189. foreach my $mailgroupgroup (@mailgroupgroups) {
  190. push @mailgroupowners, $owner{$mailgroupgroup} if ($owner{$mailgroupgroup});
  191. push @mailusers, @{$members{$mailgroupgroup}};
  192. }
  193. my @mailgroupowners_sorted = sort @mailgroupowners;
  194. my @mailusers_sorted = sort @mailusers;
  195. if ($loop) {
  196. print "\n##################################################################\n\n";
  197. }
  198. $loop++;
  199. &print_accounts('root', $mailgroup, $maildomain, "$maildomain VIRTUAL", "postmaster\@$maildomain root");
  200. # Do mailgroup owners (and don't warn if there's no addresses attached)
  201. foreach my $mailgroupowner (@mailgroupowners_sorted) {
  202. &print_accounts($mailgroupowner, $mailgroup, $maildomain, &usercomment($mailgroupowner));
  203. }
  204. # Do secondary mailgroup members
  205. foreach my $mailuser (@mailusers_sorted) {
  206. &print_accounts($mailuser, $mailgroup, $maildomain, &usercomment($mailuser), "#WARNING: No addresses for $mailuser");
  207. }
  208. }
  209. }