summaryrefslogtreecommitdiff
path: root/localmkpostfixvirtual
blob: b737b45d5cb25a1ea3f1d46731800052508312d7 (plain)
  1. #!/usr/bin/perl
  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 warnings;
  99. use User::pwent;
  100. use User::grent;
  101. my (%username, %fullname, %office, %workphone, %homephone, %other);
  102. my (%username_by_gid, %addresshints, %domains);
  103. while (my $pw = getpwent()) {
  104. $username_by_gid{$pw->gid} = $pw->name;
  105. ($fullname{$pw->name}, $office{$pw->name}, $workphone{$pw->name}, $homephone{$pw->name}, $other{$pw->name}) = split /\s*,\s*/, $pw->gecos;
  106. if (defined($other{$pw->name})) {
  107. @{$addresshints{$pw->name}} = grep {/^([\.[:alnum:]_-]+|\+)?@([\.[:alnum:]_-]+)?$/} split /\s+/, $other{$pw->name};
  108. }
  109. }
  110. my (%owner, %members, %groups);
  111. while (my $gr = getgrent()) {
  112. $owner{$gr->name} = $username_by_gid{$gr->gid};
  113. $members{$gr->name} = $gr->members;
  114. foreach my $member (@{$members{$gr->name}}) {
  115. push @{$groups{$member}}, $gr->name unless (defined($owner{$gr->name}) and $member eq $owner{$gr->name});
  116. }
  117. }
  118. my (%warned);
  119. sub warnonce($$$) {
  120. my ($test, $id, $warning) = @_;
  121. if ($test) {
  122. return 1;
  123. }
  124. if ($warned{$id}) {
  125. return '';
  126. }
  127. print STDERR 'W: ' . $warning . "\n";
  128. $warned{$id} = 1;
  129. return '';
  130. }
  131. my (%username_by_localpart_by_maildomain);
  132. sub print_accounts($$$$) {
  133. my ($username, $mailgroup, $maildomain, $pre_text, $post_fallback_text) = @_;
  134. ($pre_text) && print $pre_text . "\n";
  135. my $joker_seen;
  136. if (&warnonce(@{$addresshints{$username}}, "addresshints_$username", "Skipping non-hinted username \"$username\".")) {
  137. my @localparthints = @{$addresshints{$username}};
  138. my @localparts = grep {s/(.+)@($mailgroup|$maildomain)?$/$1/} @localparthints;
  139. foreach my $localpart (@localparts) {
  140. for ($localpart) {
  141. # FIXME: the below doesn't work as intended?!?
  142. # if (&warnonce(! @{$username_by_localpart_by_maildomain{$_}}, "address_$_", "Skipping duplicate address \"$_\" for \"$username\").")) {
  143. # next;
  144. # }
  145. if (/^\+$/) {
  146. if (!$joker_seen) {
  147. print "\@$maildomain $username\n";
  148. $joker_seen = $username;
  149. } else {
  150. print "#WARNING: Catch-all for $maildomain already set to $joker_seen!";
  151. }
  152. } else {
  153. print "$localpart\@$maildomain $username\n";
  154. }
  155. }
  156. }
  157. } else {
  158. print $post_fallback_text . "\n" if ($post_fallback_text);
  159. }
  160. print "\n";
  161. }
  162. sub usercomment($) {
  163. my $user = shift;
  164. my @s = ();
  165. if (&warnonce(defined($fullname{$user}), "fullname_$user", "User \"$user\" lacks fullname.")) {
  166. push @s, $fullname{$user};
  167. }
  168. if (&warnonce(@{$groups{$user}}, "groups_$user", "User \"$user\" belongs to no (secondary) group.")) {
  169. my @groups_sorted = sort @{$groups{$user}};
  170. push @s, '(' . join(' ', @groups_sorted) . ')';
  171. }
  172. if (@s) {
  173. unshift @s, '#';
  174. }
  175. my $string = join(' ', @s);
  176. return "$string";
  177. }
  178. my $loop;
  179. my @mailgroups = @ARGV ? @ARGV : @{$members{'maildomains'}};
  180. foreach my $mailgroup (@mailgroups) {
  181. if (not &warnonce(@{$addresshints{$mailgroup}}, "addresshints_$mailgroup", "Skipping empty mailgroup \"$mailgroup\".")) {
  182. next;
  183. }
  184. my @maildomainhints = @{$addresshints{$mailgroup}};
  185. my @maildomains = grep {s/^@(.+)/$1/} @maildomainhints;
  186. foreach my $maildomain (@maildomains) {
  187. my (@mailgroupowners, @mailusers);
  188. my @mailgroupgroups = split / +/, $office{$mailgroup};
  189. push @mailgroupgroups, $mailgroup unless (@mailgroupgroups);
  190. foreach my $mailgroupgroup (@mailgroupgroups) {
  191. push @mailgroupowners, $owner{$mailgroupgroup} if ($owner{$mailgroupgroup});
  192. push @mailusers, @{$members{$mailgroupgroup}};
  193. }
  194. my @mailgroupowners_sorted = sort @mailgroupowners;
  195. my @mailusers_sorted = sort @mailusers;
  196. if ($loop) {
  197. print "\n##################################################################\n\n";
  198. }
  199. $loop++;
  200. &print_accounts('root', $mailgroup, $maildomain, "$maildomain VIRTUAL", "postmaster\@$maildomain root");
  201. # Do mailgroup owners (and don't warn if there's no addresses attached)
  202. foreach my $mailgroupowner (@mailgroupowners_sorted) {
  203. &print_accounts($mailgroupowner, $mailgroup, $maildomain, &usercomment($mailgroupowner));
  204. }
  205. # Do secondary mailgroup members
  206. foreach my $mailuser (@mailusers_sorted) {
  207. &print_accounts($mailuser, $mailgroup, $maildomain, &usercomment($mailuser), "#WARNING: No addresses for $mailuser");
  208. }
  209. }
  210. }