3 # Copyright (c) 1999-2003 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.6 2003/03/15 23:30:09 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 are some example files from the /etc/mail/virtusers/ directory:
75 =head2 /etc/mail/virtusers/bsdunix.org:
78 MAILER-DAEMON gshapiro+bounce.$DOMAIN@gshapiro.net
79 postmaster gshapiro+$LHS.$DOMAIN@gshapiro.net
80 webmaster gshapiro+$LHS.$DOMAIN@gshapiro.net
83 error:nouser No such user
86 gshapiro gshapiro+$DOMAIN@gshapiro.net
89 =head2 /etc/mail/virtusers/smtp.org:
92 gshapiro+$DOMAIN@gshapiro.net
100 Gregory Neil Shapiro E<lt>F<gshapiro@gshapiro.net>E<gt>
107 my $makemap = "/usr/sbin/makemap";
109 my $maildir = "/etc/mail";
110 my $virthosts = "$maildir/virthosts";
111 my $newvirthosts = "$maildir/virthosts.new";
112 my $virts = "$maildir/virtusers";
113 my $newvirt = "$maildir/virtusertable.new.db";
114 my $virt = "$maildir/virtusertable.db";
117 my ($lhs, $domain, $key, $value);
120 getopts('ft', $opts) || die "Usage: $0 [-f] [-t]\n";
124 $newvirt = "$maildir/virtusertable.new";
125 $virt = "$maildir/virtusertable";
128 opendir(VIRTS, $virts) || die "Could not open directory $virts: $!\n";
129 my @virts = grep { -f "$virts/$_" } readdir(VIRTS);
130 closedir(VIRTS) || die "Could not close directory $virts: $!\n";
132 foreach $domain (@virts)
134 open(DOMAIN, "$virts/$domain") || die "Could not open file $virts/$domain: $!\n";
136 my $mtime = (stat(DOMAIN))[9] || 0;
137 if ($mtime > $newest)
141 LINE: while (<DOMAIN>)
147 if (m/^([^\t ]*)[\t ]+(.*)$/)
152 $key = "$1\@$domain";
163 die "Bogus line $line in $virts/$domain\n";
166 # Variable subsitution
167 $key =~ s/\$DOMAIN/$domain/g;
168 $value =~ s/\$DOMAIN/$domain/g;
169 $value =~ s/\$LHS/$lhs/g;
170 $virt{$key} = $value;
172 close(DOMAIN) || die "Could not close $virts/$domain: $!\n";
175 my $virtmtime = (stat($virt))[9] || 0;
176 if ($opts->{f} || $virtmtime < $newest)
178 print STDOUT "Rebuilding $virt\n";
179 # logger -s -t ${prog} -p mail.info "Rebuilding ${basedir}/virtusertable"
182 open(MAKEMAP, ">$newvirt") || die "Could not open $newvirt: $!\n";
186 open(MAKEMAP, "|$makemap $dbtype $newvirt") || die "Could not start makemap: $!\n";
189 foreach $key (keys %virt)
191 print MAKEMAP "$key\t\t$virt{$key}\n";
193 close(MAKEMAP) || die "Could not close makemap ($?): $!\n";
194 rename($newvirt, $virt) || die "Could not rename $newvirt to $virt: $!\n";
196 open(VIRTHOST, ">$newvirthosts") || die "Could not open file $newvirthosts: $!\n";
197 foreach $domain (sort @virts)
199 print VIRTHOST "$domain\n";
201 close(VIRTHOST) || die "Could not close $newvirthosts: $!\n";
202 rename($newvirthosts, $virthosts) || die "Could not rename $newvirthosts to $virthosts: $!\n";