3 # Copyright (c) 1999-2004, 2007 Gregory Neil Shapiro. All Rights Reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # 3. Neither the name of the author nor the names of its contributors
15 # may be used to endorse or promote products derived from this software
16 # without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # $Id: buildvirtuser,v 1.8 2007-10-08 18:44:15 gshapiro Exp $
34 buildvirtuser - Build virtusertable support from a directory of files
38 buildvirtuser [-f] [-t]
42 buildvirtuser will build /etc/mail/virtusertable.db and /etc/mail/virthosts
43 based on the contents of the directory /etc/mail/virtusers/. That
44 directory should contain one file per virtual domain with the filename
45 matching the virtual domain name and the contents containing a list of
46 usernames on the left and the actual address for that username on the
47 right. An empty left column translates to the default for that domain.
48 Blank lines and lines beginning with '#' are ignored. Occurrences of
49 $DOMAIN in the file are replaced by the current domain being processed.
50 Occurrences of $LHS in the right hand side are replaced by the address on
53 The -f option forces the database to be rebuilt regardless of whether
54 any file changes were detected.
56 The -t option instructs the program to build a text file instead of a
57 database. The text file can then be used with makemap.
61 In order to function properly, sendmail must be configured to use these
64 FEATURE(`virtusertable')dnl
65 VIRTUSER_DOMAIN_FILE(`/etc/mail/virthosts')dnl
67 If a new domain is added (i.e., by adding a new file to
68 /etc/mail/virtusers/), the sendmail daemon must be restarted for the change
73 Here is an example file from the /etc/mail/virtusers/ directory:
75 =head2 /etc/mail/virtusers/example.org:
78 MAILER-DAEMON gshapiro+bounce.$DOMAIN@example.net
79 postmaster gshapiro+$LHS.$DOMAIN@example.net
80 webmaster gshapiro+$LHS.$DOMAIN@example.net
83 error:nouser No such user
86 gshapiro gshapiro+$DOMAIN@example.net
91 Gregory Neil Shapiro E<lt>F<gshapiro@gshapiro.net>E<gt>
99 my $makemap = "/usr/sbin/makemap";
101 my $maildir = "/etc/mail";
102 my $virthosts = "$maildir/virthosts";
103 my $newvirthosts = "$maildir/virthosts.new";
104 my $virts = "$maildir/virtusers";
105 my $newvirt = "$maildir/virtusertable.new.db";
106 my $virt = "$maildir/virtusertable.db";
109 my ($lhs, $domain, $key, $value);
112 sub preserve_perms ($$)
119 return if (!defined($st));
120 chmod($st->mode, $new) || warn "Could not chmod($st->mode, $new): $!\n";
121 chown($st->uid, $st->gid, $new) || warn "Could not chmod($st->uid, $st->gid, $new): $!\n";
124 getopts('ft', $opts) || die "Usage: $0 [-f] [-t]\n";
128 $newvirt = "$maildir/virtusertable.new";
129 $virt = "$maildir/virtusertable";
132 opendir(VIRTS, $virts) || die "Could not open directory $virts: $!\n";
133 my @virts = grep { -f "$virts/$_" } readdir(VIRTS);
134 closedir(VIRTS) || die "Could not close directory $virts: $!\n";
136 foreach $domain (@virts)
138 next if ($domain =~ m/^\./);
139 open(DOMAIN, "$virts/$domain") || die "Could not open file $virts/$domain: $!\n";
142 my $st = stat("$virts/$domain");
143 $mtime = $st->mtime if (defined($st));
144 if ($mtime > $newest)
148 LINE: while (<DOMAIN>)
154 if (m/^([^\t ]*)[\t ]+(.*)$/)
159 $key = "$1\@$domain";
170 warn "Bogus line $line in $virts/$domain\n";
173 # Variable subsitution
174 $key =~ s/\$DOMAIN/$domain/g;
175 $value =~ s/\$DOMAIN/$domain/g;
176 $value =~ s/\$LHS/$lhs/g;
177 $virt{$key} = $value;
179 close(DOMAIN) || die "Could not close $virts/$domain: $!\n";
183 my $st = stat($virt);
184 $virtmtime = $st->mtime if (defined($st));
185 if ($opts->{f} || $virtmtime < $newest)
187 print STDOUT "Rebuilding $virt\n";
188 # logger -s -t ${prog} -p mail.info "Rebuilding ${basedir}/virtusertable"
191 open(MAKEMAP, ">$newvirt") || die "Could not open $newvirt: $!\n";
195 open(MAKEMAP, "|$makemap $dbtype $newvirt") || die "Could not start makemap: $!\n";
198 foreach $key (keys %virt)
200 print MAKEMAP "$key\t\t$virt{$key}\n";
202 close(MAKEMAP) || die "Could not close makemap ($?): $!\n";
203 preserve_perms($virt, $newvirt);
204 rename($newvirt, $virt) || die "Could not rename $newvirt to $virt: $!\n";
206 open(VIRTHOST, ">$newvirthosts") || die "Could not open file $newvirthosts: $!\n";
207 foreach $domain (sort @virts)
209 next if ($domain =~ m/^\./);
210 print VIRTHOST "$domain\n";
212 close(VIRTHOST) || die "Could not close $newvirthosts: $!\n";
213 preserve_perms($virthosts, $newvirthosts);
214 rename($newvirthosts, $virthosts) || die "Could not rename $newvirthosts to $virthosts: $!\n";