head	1.1;
branch	1.1.1;
access;
symbols
	RELENG_8_4:1.1.1.9.0.18
	RELENG_9_1_0_RELEASE:1.1.1.9
	RELENG_9_1:1.1.1.9.0.16
	RELENG_9_1_BP:1.1.1.9
	RELENG_8_3_0_RELEASE:1.1.1.9
	RELENG_8_3:1.1.1.9.0.14
	RELENG_8_3_BP:1.1.1.9
	RELENG_9_0_0_RELEASE:1.1.1.9
	RELENG_9_0:1.1.1.9.0.12
	RELENG_9_0_BP:1.1.1.9
	RELENG_9:1.1.1.9.0.10
	RELENG_9_BP:1.1.1.9
	RELENG_7_4_0_RELEASE:1.1.1.8.18.1
	RELENG_8_2_0_RELEASE:1.1.1.9
	RELENG_7_4:1.1.1.8.18.1.0.10
	RELENG_7_4_BP:1.1.1.8.18.1
	RELENG_8_2:1.1.1.9.0.8
	RELENG_8_2_BP:1.1.1.9
	RELENG_8_1_0_RELEASE:1.1.1.9
	RELENG_8_1:1.1.1.9.0.6
	RELENG_8_1_BP:1.1.1.9
	RELENG_7_3_0_RELEASE:1.1.1.8.18.1
	RELENG_7_3:1.1.1.8.18.1.0.8
	RELENG_7_3_BP:1.1.1.8.18.1
	RELENG_8_0_0_RELEASE:1.1.1.9
	RELENG_8_0:1.1.1.9.0.4
	RELENG_8_0_BP:1.1.1.9
	RELENG_8:1.1.1.9.0.2
	RELENG_8_BP:1.1.1.9
	RELENG_7_2_0_RELEASE:1.1.1.8.18.1
	RELENG_7_2:1.1.1.8.18.1.0.6
	RELENG_7_2_BP:1.1.1.8.18.1
	RELENG_7_1_0_RELEASE:1.1.1.8.18.1
	RELENG_6_4_0_RELEASE:1.1.1.8.8.1
	RELENG_7_1:1.1.1.8.18.1.0.4
	RELENG_7_1_BP:1.1.1.8.18.1
	RELENG_6_4:1.1.1.8.8.1.0.4
	RELENG_6_4_BP:1.1.1.8.8.1
	RELENG_7_0_0_RELEASE:1.1.1.8.18.1
	RELENG_6_3_0_RELEASE:1.1.1.8.8.1
	RELENG_7_0:1.1.1.8.18.1.0.2
	RELENG_7_0_BP:1.1.1.8.18.1
	RELENG_6_3:1.1.1.8.8.1.0.2
	RELENG_6_3_BP:1.1.1.8.8.1
	v8_14_2:1.1.1.9
	RELENG_7:1.1.1.8.0.18
	RELENG_7_BP:1.1.1.8
	v8_14_1:1.1.1.8
	RELENG_6_2_0_RELEASE:1.1.1.8
	RELENG_6_2:1.1.1.8.0.16
	RELENG_6_2_BP:1.1.1.8
	v8_13_8:1.1.1.8
	v8_13_7:1.1.1.8
	RELENG_5_5_0_RELEASE:1.1.1.8
	RELENG_5_5:1.1.1.8.0.14
	RELENG_5_5_BP:1.1.1.8
	RELENG_6_1_0_RELEASE:1.1.1.8
	RELENG_6_1:1.1.1.8.0.12
	RELENG_6_1_BP:1.1.1.8
	v8_13_6:1.1.1.8
	RELENG_6_0_0_RELEASE:1.1.1.8
	RELENG_6_0:1.1.1.8.0.10
	RELENG_6_0_BP:1.1.1.8
	RELENG_6:1.1.1.8.0.8
	RELENG_6_BP:1.1.1.8
	v8_13_4:1.1.1.8
	RELENG_5_4_0_RELEASE:1.1.1.8
	RELENG_5_4:1.1.1.8.0.6
	RELENG_5_4_BP:1.1.1.8
	v8_13_3:1.1.1.8
	RELENG_4_11_0_RELEASE:1.1.1.1.2.6
	RELENG_4_11:1.1.1.1.2.6.0.2
	RELENG_4_11_BP:1.1.1.1.2.6
	RELENG_5_3_0_RELEASE:1.1.1.8
	RELENG_5_3:1.1.1.8.0.4
	RELENG_5_3_BP:1.1.1.8
	RELENG_5:1.1.1.8.0.2
	RELENG_5_BP:1.1.1.8
	v8_13_1:1.1.1.8
	RELENG_4_10_0_RELEASE:1.1.1.1.2.5
	RELENG_4_10:1.1.1.1.2.5.0.4
	RELENG_4_10_BP:1.1.1.1.2.5
	RELENG_5_2_1_RELEASE:1.1.1.7
	v8_12_11:1.1.1.7
	RELENG_5_2_0_RELEASE:1.1.1.7
	RELENG_5_2:1.1.1.7.0.4
	RELENG_5_2_BP:1.1.1.7
	RELENG_4_9_0_RELEASE:1.1.1.1.2.5
	RELENG_4_9:1.1.1.1.2.5.0.2
	RELENG_4_9_BP:1.1.1.1.2.5
	v8_12_10:1.1.1.7
	RELENG_5_1_0_RELEASE:1.1.1.7
	RELENG_5_1:1.1.1.7.0.2
	RELENG_5_1_BP:1.1.1.7
	RELENG_4_8_0_RELEASE:1.1.1.1.2.4
	v8_12_9:1.1.1.7
	RELENG_4_8:1.1.1.1.2.4.0.6
	RELENG_4_8_BP:1.1.1.1.2.4
	v8_12_8:1.1.1.6
	v8_12_7:1.1.1.6
	RELENG_5_0_0_RELEASE:1.1.1.6
	RELENG_5_0:1.1.1.6.0.2
	RELENG_5_0_BP:1.1.1.6
	RELENG_4_7_0_RELEASE:1.1.1.1.2.4
	RELENG_4_7:1.1.1.1.2.4.0.4
	RELENG_4_7_BP:1.1.1.1.2.4
	v8_12_6:1.1.1.6
	RELENG_4_6_2_RELEASE:1.1.1.1.2.4
	RELENG_4_6_1_RELEASE:1.1.1.1.2.4
	v8_12_5:1.1.1.6
	v8_12_4:1.1.1.6
	RELENG_4_6_0_RELEASE:1.1.1.1.2.4
	RELENG_4_6:1.1.1.1.2.4.0.2
	RELENG_4_6_BP:1.1.1.1.2.4
	v8_12_3_20020420:1.1.1.5
	v8_12_3:1.1.1.4
	v8_12_2:1.1.1.3
	RELENG_4_5_0_RELEASE:1.1.1.1.2.2
	RELENG_4_5:1.1.1.1.2.2.0.6
	RELENG_4_5_BP:1.1.1.1.2.2
	RELENG_4_4_0_RELEASE:1.1.1.1.2.2
	RELENG_4_4:1.1.1.1.2.2.0.4
	RELENG_4_4_BP:1.1.1.1.2.2
	v8_11_6:1.1.1.2
	v8_11_5:1.1.1.2
	v8_11_4:1.1.1.2
	RELENG_4_3_0_RELEASE:1.1.1.1.2.2
	RELENG_4_3:1.1.1.1.2.2.0.2
	RELENG_4_3_BP:1.1.1.1.2.2
	v8_11_3:1.1.1.2
	RELENG_4:1.1.1.1.0.2
	v8_11_2:1.1.1.1
	SENDMAIL:1.1.1;
locks; strict;
comment	@# @;


1.1
date	2001.01.21.22.16.59;	author gshapiro;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	2001.01.21.22.16.59;	author gshapiro;	state Exp;
branches
	1.1.1.1.2.1;
next	1.1.1.2;

1.1.1.2
date	2001.02.28.00.19.56;	author gshapiro;	state Exp;
branches;
next	1.1.1.3;

1.1.1.3
date	2002.02.17.21.56.45;	author gshapiro;	state Exp;
branches;
next	1.1.1.4;

1.1.1.4
date	2002.04.10.03.04.59;	author gshapiro;	state Exp;
branches;
next	1.1.1.5;

1.1.1.5
date	2002.04.20.20.31.40;	author gshapiro;	state Exp;
branches;
next	1.1.1.6;

1.1.1.6
date	2002.06.11.21.12.02;	author gshapiro;	state Exp;
branches;
next	1.1.1.7;

1.1.1.7
date	2003.03.29.19.12.47;	author gshapiro;	state Exp;
branches;
next	1.1.1.8;

1.1.1.8
date	2004.08.01.01.04.52;	author gshapiro;	state Exp;
branches
	1.1.1.8.2.1
	1.1.1.8.8.1
	1.1.1.8.18.1;
next	1.1.1.9;

1.1.1.9
date	2007.11.05.00.04.13;	author gshapiro;	state Exp;
branches
	1.1.1.9.18.1;
next	;

1.1.1.1.2.1
date	2001.02.01.00.54.18;	author gshapiro;	state Exp;
branches;
next	1.1.1.1.2.2;

1.1.1.1.2.2
date	2001.03.06.01.38.28;	author gshapiro;	state Exp;
branches;
next	1.1.1.1.2.3;

1.1.1.1.2.3
date	2002.03.25.21.24.34;	author gshapiro;	state Exp;
branches;
next	1.1.1.1.2.4;

1.1.1.1.2.4
date	2002.04.17.01.59.46;	author gshapiro;	state Exp;
branches;
next	1.1.1.1.2.5;

1.1.1.1.2.5
date	2003.03.29.19.33.14;	author gshapiro;	state Exp;
branches;
next	1.1.1.1.2.6;

1.1.1.1.2.6
date	2004.08.09.00.15.36;	author gshapiro;	state Exp;
branches;
next	1.1.1.1.2.7;

1.1.1.1.2.7
date	2007.11.11.17.09.09;	author gshapiro;	state Exp;
branches;
next	;

1.1.1.8.2.1
date	2007.11.11.17.08.43;	author gshapiro;	state Exp;
branches;
next	;

1.1.1.8.8.1
date	2007.11.11.17.07.34;	author gshapiro;	state Exp;
branches;
next	;

1.1.1.8.18.1
date	2007.11.11.17.06.41;	author gshapiro;	state Exp;
branches;
next	;

1.1.1.9.18.1
date	2007.11.05.00.04.13;	author svnexp;	state dead;
branches;
next	1.1.1.9.18.2;

1.1.1.9.18.2
date	2013.03.28.13.01.58;	author svnexp;	state Exp;
branches;
next	;


desc
@@


1.1
log
@Initial revision
@
text
@#!/usr/bin/perl -w

# Copyright (c) 1999-2000 Gregory Neil Shapiro.  All Rights Reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the author nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# $Id: buildvirtuser,v 1.1.2.1 2000/10/26 22:51:03 gshapiro Exp $

=head1 NAME

buildvirtuser - Build virtusertable support from a directory of files

=head1 SYNOPSIS

    buildvirtuser

=head1 DESCRIPTION

buildvirtuser will build /etc/mail/virtusertable.db and /etc/mail/virthosts
based on the contents of the directory /etc/mail/virtusers/.  That
directory should contain one file per virtual domain with the filename
matching the virtual domain name and the contents containing a list of
usernames on the left and the actual address for that username on the
right.  An empty left column translates to the default for that domain.
Blank lines and lines beginning with '#' are ignored.

=head1 CONFIGURATION

In order to function properly, sendmail must be configured to use these
files with:

	FEATURE(`virtusertable')dnl
	VIRTUSER_DOMAIN_FILE(`/etc/mail/virthosts')dnl

If a new domain is added (i.e., by adding a new file to
/etc/mail/virtusers/), the sendmail daemon must be restarted for the change
to take affect.

=head1 EXAMPLES

Here are some example files from the /etc/mail/virtusers/ directory:

=head2 /etc/mail/virtusers/bsdunix.org:

 # Services
 MAILER-DAEMON	gshapiro+MAILER-DAEMON.bsdunix.org@@gshapiro.net
 postmaster	gshapiro+postmaster.bsdunix.org@@gshapiro.net
 webmaster	gshapiro+webmaster.bsdunix.org@@gshapiro.net
 
 # Defaults
 		error:nouser No such user
 
 # Users
 gshapiro	gshapiro+bsdunix.org@@gshapiro.net
 bob		robert@@smtp.org

=head2 /etc/mail/virtusers/smtp.org:

 # Defaults
 		gshapiro+smtp.org@@gshapiro.net
 
 # Users
 john		john@@wookie.org
 nancy		n@@milter.com

=head1 AUTHOR

Gregory Neil Shapiro E<lt>F<gshapiro@@gshapiro.net>E<gt>

=cut

my $makemap = "/usr/sbin/makemap";
my $dbtype = "hash";
my $maildir = "/etc/mail";
my $virthosts = "$maildir/virthosts";
my $newvirthosts = "$maildir/virthosts.new";
my $virts = "$maildir/virtusers";
my $newvirt = "$maildir/virtusertable.new.db";
my $virt = "$maildir/virtusertable.db";
my %virt = ();
my $newest = 0;

opendir(VIRTS, $virts) || die "Could not open directory $virts: $!\n";
my @@virts = grep { -f "$virts/$_" } readdir(VIRTS);
closedir(VIRTS) || die "Could not close directory $virts: $!\n";

foreach $domain (@@virts)
{
	open(DOMAIN, "$virts/$domain") || die "Could not open file $virts/$domain: $!\n";
	my $line = 0;
	my $mtime = (stat(DOMAIN))[9] || 0;
	if ($mtime > $newest)
	{
		$newest = $mtime;
	}
LINE:	while (<DOMAIN>)
	{
		chomp;
		$line++;
		next LINE if /^#/;
		next LINE if /^$/;
		if (m/^([^\t ]*)[\t ]+(.*)$/)
		{
			if (defined($1))
			{
				$key = "$1\@@$domain";
			}
			else
			{
				$key = "\@@$domain";
			}
			$value = $2;
		}
		else
		{
			die "Bogus line $line in $virts/$domain\n";
		}
		$virt{$key} = $value;
	}
	close(DOMAIN) || die "Could not close $virts/$domain: $!\n";
}

my $virtmtime = (stat($virt))[9] || 0;
if ($virtmtime < $newest)
{
	print STDOUT "Rebuilding $virt\n";
# logger -s -t ${prog} -p mail.info "Rebuilding ${basedir}/virtusertable"
	open(MAKEMAP, "|$makemap $dbtype $newvirt") || die "Could not start makemap: $!\n";
	foreach $key (keys %virt)
	{
		print MAKEMAP "$key\t\t$virt{$key}\n";
	}
	close(MAKEMAP) || die "Could not close makemap ($?): $!\n";
	rename($newvirt, $virt) || die "Could not rename $newvirt to $virt: $!\n";

	open(VIRTHOST, ">$newvirthosts") || die "Could not open file $newvirthosts: $!\n";
	foreach $domain (sort @@virts)
	{
		print VIRTHOST "$domain\n";
	}
	close(VIRTHOST) || die "Could not close $newvirthosts: $!\n";
	rename($newvirthosts, $virthosts) || die "Could not rename $newvirthosts to $virthosts: $!\n";
}
exit 0;
@


1.1.1.1
log
@Import sendmail 8.11.2
@
text
@@


1.1.1.2
log
@Import sendmail 8.11.3
@
text
@d3 1
a3 1
# Copyright (c) 1999-2001 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.1.2.3 2001/02/12 02:57:13 gshapiro Exp $
d48 1
a48 4
Blank lines and lines beginning with '#' are ignored.  Occurrences of
$DOMAIN in the file are replaced by the current domain being processed.
Occurrences of $LHS in the right hand side are replaced by the address on
the left hand side.
d69 3
a71 3
 MAILER-DAEMON	gshapiro+bounce.$DOMAIN@@gshapiro.net
 postmaster	gshapiro+$LHS.$DOMAIN@@gshapiro.net
 webmaster	gshapiro+$LHS.$DOMAIN@@gshapiro.net
d77 1
a77 1
 gshapiro	gshapiro+$DOMAIN@@gshapiro.net
d83 1
a83 1
 		gshapiro+$DOMAIN@@gshapiro.net
a94 2
use strict;

a104 1
my ($lhs, $domain, $key, $value);
a128 1
				$lhs = "$1";
a132 1
				$lhs = "";
a140 5

		# Variable subsitution
		$key =~ s/\$DOMAIN/$domain/g;
		$value =~ s/\$DOMAIN/$domain/g;
		$value =~ s/\$LHS/$lhs/g;
@


1.1.1.3
log
@Import sendmail 8.12.2
@
text
@d30 1
a30 1
# $Id: buildvirtuser,v 1.3 2001/02/12 02:58:20 gshapiro Exp $
@


1.1.1.4
log
@Import sendmail 8.12.3
@
text
@d3 1
a3 1
# Copyright (c) 1999-2002 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.5 2002/02/08 08:10:59 gshapiro Exp $
d38 1
a38 1
    buildvirtuser [-f]
a52 3
The -f option forces the database to be rebuilt regardless of whether
any file changes were detected.

a98 1
use Getopt::Std;
a110 3
my $opts = {};

getopts('f', $opts) || die "Usage: $0 [-f]\n";
d160 1
a160 1
if ($opts->{f} || $virtmtime < $newest)
@


1.1.1.5
log
@Import of post-8.12.3 bug fixes from vendor repository.

These are being imported for the upcoming FreeBSD 4.6 release.
@
text
@d30 1
a30 1
# $Id: buildvirtuser,v 1.1.1.4 2002/04/10 03:04:59 gshapiro Exp $
@


1.1.1.6
log
@Import sendmail 8.12.4
@
text
@d30 1
a30 1
# $Id: buildvirtuser,v 1.5 2002/02/08 08:10:59 gshapiro Exp $
@


1.1.1.7
log
@Import sendmail 8.12.9
@
text
@d3 1
a3 1
# Copyright (c) 1999-2003 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.5.2.1 2003/03/15 23:30:26 gshapiro Exp $
d38 1
a38 1
    buildvirtuser [-f] [-t]
a55 3
The -t option instructs the program to build a text file instead of a
database.  The text file can then be used with makemap.

d117 1
a117 7
getopts('ft', $opts) || die "Usage: $0 [-f] [-t]\n";

if ($opts->{t})
{
	$newvirt = "$maildir/virtusertable.new";
	$virt = "$maildir/virtusertable";
}
d171 1
a171 9
	if ($opts->{t})
	{
		open(MAKEMAP, ">$newvirt") || die "Could not open $newvirt: $!\n";
	}
	else
	{
		open(MAKEMAP, "|$makemap $dbtype $newvirt") || die "Could not start makemap: $!\n";
	}

@


1.1.1.8
log
@Import sendmail 8.13.1
@
text
@d30 1
a30 1
# $Id: buildvirtuser,v 1.6 2003/03/15 23:30:09 gshapiro Exp $
@


1.1.1.8.2.1
log
@MFC: sendmail 8.14.2
@
text
@d3 1
a3 1
# Copyright (c) 1999-2004, 2007 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.8 2007/10/08 18:44:15 gshapiro Exp $
d73 1
a73 1
Here is an example file from the /etc/mail/virtusers/ directory:
d75 1
a75 1
=head2 /etc/mail/virtusers/example.org:
d78 3
a80 3
 MAILER-DAEMON	gshapiro+bounce.$DOMAIN@@example.net
 postmaster	gshapiro+$LHS.$DOMAIN@@example.net
 webmaster	gshapiro+$LHS.$DOMAIN@@example.net
d86 11
a96 2
 gshapiro	gshapiro+$DOMAIN@@example.net
 zoe		zoe@@example.com
a104 1
use File::stat;
a119 12
sub preserve_perms ($$)
{
	my $old = shift;
	my $new = shift;
	my $st;

	$st = stat($old);
	return if (!defined($st));
	chmod($st->mode, $new) || warn "Could not chmod($st->mode, $new): $!\n";
	chown($st->uid, $st->gid, $new) || warn "Could not chmod($st->uid, $st->gid, $new): $!\n";
}

a133 1
	next if ($domain =~ m/^\./);
d136 1
a136 3
	my $mtime = 0;
	my $st = stat("$virts/$domain");
	$mtime = $st->mtime if (defined($st));
d163 1
a163 1
			warn "Bogus line $line in $virts/$domain\n";
d175 1
a175 3
my $virtmtime = 0;
my $st = stat($virt);
$virtmtime = $st->mtime if (defined($st));
a193 1
	preserve_perms($virt, $newvirt);
a198 1
		next if ($domain =~ m/^\./);
a201 1
	preserve_perms($virthosts, $newvirthosts);
@


1.1.1.8.8.1
log
@MFC: sendmail 8.14.2

Approved by:	re (kensmith)
@
text
@d3 1
a3 1
# Copyright (c) 1999-2004, 2007 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.8 2007/10/08 18:44:15 gshapiro Exp $
d73 1
a73 1
Here is an example file from the /etc/mail/virtusers/ directory:
d75 1
a75 1
=head2 /etc/mail/virtusers/example.org:
d78 3
a80 3
 MAILER-DAEMON	gshapiro+bounce.$DOMAIN@@example.net
 postmaster	gshapiro+$LHS.$DOMAIN@@example.net
 webmaster	gshapiro+$LHS.$DOMAIN@@example.net
d86 11
a96 2
 gshapiro	gshapiro+$DOMAIN@@example.net
 zoe		zoe@@example.com
a104 1
use File::stat;
a119 12
sub preserve_perms ($$)
{
	my $old = shift;
	my $new = shift;
	my $st;

	$st = stat($old);
	return if (!defined($st));
	chmod($st->mode, $new) || warn "Could not chmod($st->mode, $new): $!\n";
	chown($st->uid, $st->gid, $new) || warn "Could not chmod($st->uid, $st->gid, $new): $!\n";
}

a133 1
	next if ($domain =~ m/^\./);
d136 1
a136 3
	my $mtime = 0;
	my $st = stat("$virts/$domain");
	$mtime = $st->mtime if (defined($st));
d163 1
a163 1
			warn "Bogus line $line in $virts/$domain\n";
d175 1
a175 3
my $virtmtime = 0;
my $st = stat($virt);
$virtmtime = $st->mtime if (defined($st));
a193 1
	preserve_perms($virt, $newvirt);
a198 1
		next if ($domain =~ m/^\./);
a201 1
	preserve_perms($virthosts, $newvirthosts);
@


1.1.1.8.18.1
log
@MFC: sendmail 8.14.2

Approved by:	re (kensmith)
@
text
@d3 1
a3 1
# Copyright (c) 1999-2004, 2007 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.8 2007/10/08 18:44:15 gshapiro Exp $
d73 1
a73 1
Here is an example file from the /etc/mail/virtusers/ directory:
d75 1
a75 1
=head2 /etc/mail/virtusers/example.org:
d78 3
a80 3
 MAILER-DAEMON	gshapiro+bounce.$DOMAIN@@example.net
 postmaster	gshapiro+$LHS.$DOMAIN@@example.net
 webmaster	gshapiro+$LHS.$DOMAIN@@example.net
d86 11
a96 2
 gshapiro	gshapiro+$DOMAIN@@example.net
 zoe		zoe@@example.com
a104 1
use File::stat;
a119 12
sub preserve_perms ($$)
{
	my $old = shift;
	my $new = shift;
	my $st;

	$st = stat($old);
	return if (!defined($st));
	chmod($st->mode, $new) || warn "Could not chmod($st->mode, $new): $!\n";
	chown($st->uid, $st->gid, $new) || warn "Could not chmod($st->uid, $st->gid, $new): $!\n";
}

a133 1
	next if ($domain =~ m/^\./);
d136 1
a136 3
	my $mtime = 0;
	my $st = stat("$virts/$domain");
	$mtime = $st->mtime if (defined($st));
d163 1
a163 1
			warn "Bogus line $line in $virts/$domain\n";
d175 1
a175 3
my $virtmtime = 0;
my $st = stat($virt);
$virtmtime = $st->mtime if (defined($st));
a193 1
	preserve_perms($virt, $newvirt);
a198 1
		next if ($domain =~ m/^\./);
a201 1
	preserve_perms($virthosts, $newvirthosts);
@


1.1.1.9
log
@Import sendmail 8.14.2
@
text
@d3 1
a3 1
# Copyright (c) 1999-2004, 2007 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.8 2007/10/08 18:44:15 gshapiro Exp $
d73 1
a73 1
Here is an example file from the /etc/mail/virtusers/ directory:
d75 1
a75 1
=head2 /etc/mail/virtusers/example.org:
d78 3
a80 3
 MAILER-DAEMON	gshapiro+bounce.$DOMAIN@@example.net
 postmaster	gshapiro+$LHS.$DOMAIN@@example.net
 webmaster	gshapiro+$LHS.$DOMAIN@@example.net
d86 11
a96 2
 gshapiro	gshapiro+$DOMAIN@@example.net
 zoe		zoe@@example.com
a104 1
use File::stat;
a119 12
sub preserve_perms ($$)
{
	my $old = shift;
	my $new = shift;
	my $st;

	$st = stat($old);
	return if (!defined($st));
	chmod($st->mode, $new) || warn "Could not chmod($st->mode, $new): $!\n";
	chown($st->uid, $st->gid, $new) || warn "Could not chmod($st->uid, $st->gid, $new): $!\n";
}

a133 1
	next if ($domain =~ m/^\./);
d136 1
a136 3
	my $mtime = 0;
	my $st = stat("$virts/$domain");
	$mtime = $st->mtime if (defined($st));
d163 1
a163 1
			warn "Bogus line $line in $virts/$domain\n";
d175 1
a175 3
my $virtmtime = 0;
my $st = stat($virt);
$virtmtime = $st->mtime if (defined($st));
a193 1
	preserve_perms($virt, $newvirt);
a198 1
		next if ($domain =~ m/^\./);
a201 1
	preserve_perms($virthosts, $newvirthosts);
@


1.1.1.9.18.1
log
@file buildvirtuser was added on branch RELENG_8_4 on 2013-03-28 13:01:58 +0000
@
text
@d1 216
@


1.1.1.9.18.2
log
@## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/248810
## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated
@
text
@a0 216
#!/usr/bin/perl -w

# Copyright (c) 1999-2004, 2007 Gregory Neil Shapiro.  All Rights Reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the author nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# $Id: buildvirtuser,v 1.8 2007/10/08 18:44:15 gshapiro Exp $

=head1 NAME

buildvirtuser - Build virtusertable support from a directory of files

=head1 SYNOPSIS

    buildvirtuser [-f] [-t]

=head1 DESCRIPTION

buildvirtuser will build /etc/mail/virtusertable.db and /etc/mail/virthosts
based on the contents of the directory /etc/mail/virtusers/.  That
directory should contain one file per virtual domain with the filename
matching the virtual domain name and the contents containing a list of
usernames on the left and the actual address for that username on the
right.  An empty left column translates to the default for that domain.
Blank lines and lines beginning with '#' are ignored.  Occurrences of
$DOMAIN in the file are replaced by the current domain being processed.
Occurrences of $LHS in the right hand side are replaced by the address on
the left hand side.

The -f option forces the database to be rebuilt regardless of whether
any file changes were detected.

The -t option instructs the program to build a text file instead of a
database.  The text file can then be used with makemap.

=head1 CONFIGURATION

In order to function properly, sendmail must be configured to use these
files with:

	FEATURE(`virtusertable')dnl
	VIRTUSER_DOMAIN_FILE(`/etc/mail/virthosts')dnl

If a new domain is added (i.e., by adding a new file to
/etc/mail/virtusers/), the sendmail daemon must be restarted for the change
to take affect.

=head1 EXAMPLES

Here is an example file from the /etc/mail/virtusers/ directory:

=head2 /etc/mail/virtusers/example.org:

 # Services
 MAILER-DAEMON	gshapiro+bounce.$DOMAIN@@example.net
 postmaster	gshapiro+$LHS.$DOMAIN@@example.net
 webmaster	gshapiro+$LHS.$DOMAIN@@example.net
 
 # Defaults
 		error:nouser No such user
 
 # Users
 gshapiro	gshapiro+$DOMAIN@@example.net
 zoe		zoe@@example.com

=head1 AUTHOR

Gregory Neil Shapiro E<lt>F<gshapiro@@gshapiro.net>E<gt>

=cut

use strict;
use File::stat;
use Getopt::Std;

my $makemap = "/usr/sbin/makemap";
my $dbtype = "hash";
my $maildir = "/etc/mail";
my $virthosts = "$maildir/virthosts";
my $newvirthosts = "$maildir/virthosts.new";
my $virts = "$maildir/virtusers";
my $newvirt = "$maildir/virtusertable.new.db";
my $virt = "$maildir/virtusertable.db";
my %virt = ();
my $newest = 0;
my ($lhs, $domain, $key, $value);
my $opts = {};

sub preserve_perms ($$)
{
	my $old = shift;
	my $new = shift;
	my $st;

	$st = stat($old);
	return if (!defined($st));
	chmod($st->mode, $new) || warn "Could not chmod($st->mode, $new): $!\n";
	chown($st->uid, $st->gid, $new) || warn "Could not chmod($st->uid, $st->gid, $new): $!\n";
}

getopts('ft', $opts) || die "Usage: $0 [-f] [-t]\n";

if ($opts->{t})
{
	$newvirt = "$maildir/virtusertable.new";
	$virt = "$maildir/virtusertable";
}

opendir(VIRTS, $virts) || die "Could not open directory $virts: $!\n";
my @@virts = grep { -f "$virts/$_" } readdir(VIRTS);
closedir(VIRTS) || die "Could not close directory $virts: $!\n";

foreach $domain (@@virts)
{
	next if ($domain =~ m/^\./);
	open(DOMAIN, "$virts/$domain") || die "Could not open file $virts/$domain: $!\n";
	my $line = 0;
	my $mtime = 0;
	my $st = stat("$virts/$domain");
	$mtime = $st->mtime if (defined($st));
	if ($mtime > $newest)
	{
		$newest = $mtime;
	}
LINE:	while (<DOMAIN>)
	{
		chomp;
		$line++;
		next LINE if /^#/;
		next LINE if /^$/;
		if (m/^([^\t ]*)[\t ]+(.*)$/)
		{
			if (defined($1))
			{
				$lhs = "$1";
				$key = "$1\@@$domain";
			}
			else
			{
				$lhs = "";
				$key = "\@@$domain";
			}
			$value = $2;
		}
		else
		{
			warn "Bogus line $line in $virts/$domain\n";
		}

		# Variable subsitution
		$key =~ s/\$DOMAIN/$domain/g;
		$value =~ s/\$DOMAIN/$domain/g;
		$value =~ s/\$LHS/$lhs/g;
		$virt{$key} = $value;
	}
	close(DOMAIN) || die "Could not close $virts/$domain: $!\n";
}

my $virtmtime = 0;
my $st = stat($virt);
$virtmtime = $st->mtime if (defined($st));
if ($opts->{f} || $virtmtime < $newest)
{
	print STDOUT "Rebuilding $virt\n";
# logger -s -t ${prog} -p mail.info "Rebuilding ${basedir}/virtusertable"
	if ($opts->{t})
	{
		open(MAKEMAP, ">$newvirt") || die "Could not open $newvirt: $!\n";
	}
	else
	{
		open(MAKEMAP, "|$makemap $dbtype $newvirt") || die "Could not start makemap: $!\n";
	}

	foreach $key (keys %virt)
	{
		print MAKEMAP "$key\t\t$virt{$key}\n";
	}
	close(MAKEMAP) || die "Could not close makemap ($?): $!\n";
	preserve_perms($virt, $newvirt);
	rename($newvirt, $virt) || die "Could not rename $newvirt to $virt: $!\n";

	open(VIRTHOST, ">$newvirthosts") || die "Could not open file $newvirthosts: $!\n";
	foreach $domain (sort @@virts)
	{
		next if ($domain =~ m/^\./);
		print VIRTHOST "$domain\n";
	}
	close(VIRTHOST) || die "Could not close $newvirthosts: $!\n";
	preserve_perms($virthosts, $newvirthosts);
	rename($newvirthosts, $virthosts) || die "Could not rename $newvirthosts to $virthosts: $!\n";
}
exit 0;
@


1.1.1.1.2.1
log
@MFC: Import sendmail 8.11.2
@
text
@@


1.1.1.1.2.2
log
@MFC: Import sendmail 8.11.3
@
text
@d3 1
a3 1
# Copyright (c) 1999-2001 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.1.2.3 2001/02/12 02:57:13 gshapiro Exp $
d48 1
a48 4
Blank lines and lines beginning with '#' are ignored.  Occurrences of
$DOMAIN in the file are replaced by the current domain being processed.
Occurrences of $LHS in the right hand side are replaced by the address on
the left hand side.
d69 3
a71 3
 MAILER-DAEMON	gshapiro+bounce.$DOMAIN@@gshapiro.net
 postmaster	gshapiro+$LHS.$DOMAIN@@gshapiro.net
 webmaster	gshapiro+$LHS.$DOMAIN@@gshapiro.net
d77 1
a77 1
 gshapiro	gshapiro+$DOMAIN@@gshapiro.net
d83 1
a83 1
 		gshapiro+$DOMAIN@@gshapiro.net
a94 2
use strict;

a104 1
my ($lhs, $domain, $key, $value);
a128 1
				$lhs = "$1";
a132 1
				$lhs = "";
a140 5

		# Variable subsitution
		$key =~ s/\$DOMAIN/$domain/g;
		$value =~ s/\$DOMAIN/$domain/g;
		$value =~ s/\$LHS/$lhs/g;
@


1.1.1.1.2.3
log
@MFC: Import sendmail 8.12.2
MFC: Resolve conflicts from sendmail 8.12.2 import
MFC: Remove files no longer part of the sendmail 8.12.2 distribution.
MFC: Fix mail.local build for non-sendmail.org code (1.15)
@
text
@d30 1
a30 1
# $Id: buildvirtuser,v 1.3 2001/02/12 02:58:20 gshapiro Exp $
@


1.1.1.1.2.4
log
@MFC: Import sendmail 8.12.3
@
text
@d3 1
a3 1
# Copyright (c) 1999-2002 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.5 2002/02/08 08:10:59 gshapiro Exp $
d38 1
a38 1
    buildvirtuser [-f]
a52 3
The -f option forces the database to be rebuilt regardless of whether
any file changes were detected.

a98 1
use Getopt::Std;
a110 3
my $opts = {};

getopts('f', $opts) || die "Usage: $0 [-f]\n";
d160 1
a160 1
if ($opts->{f} || $virtmtime < $newest)
@


1.1.1.1.2.5
log
@MFC: sendmail 8.12.9 import

Approved by:	re (bmah)
@
text
@d3 1
a3 1
# Copyright (c) 1999-2003 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.5.2.1 2003/03/15 23:30:26 gshapiro Exp $
d38 1
a38 1
    buildvirtuser [-f] [-t]
a55 3
The -t option instructs the program to build a text file instead of a
database.  The text file can then be used with makemap.

d117 1
a117 7
getopts('ft', $opts) || die "Usage: $0 [-f] [-t]\n";

if ($opts->{t})
{
	$newvirt = "$maildir/virtusertable.new";
	$virt = "$maildir/virtusertable";
}
d171 1
a171 9
	if ($opts->{t})
	{
		open(MAKEMAP, ">$newvirt") || die "Could not open $newvirt: $!\n";
	}
	else
	{
		open(MAKEMAP, "|$makemap $dbtype $newvirt") || die "Could not start makemap: $!\n";
	}

@


1.1.1.1.2.6
log
@MFC: sendmail 8.13.1 import and related changes
@
text
@d30 1
a30 1
# $Id: buildvirtuser,v 1.6 2003/03/15 23:30:09 gshapiro Exp $
@


1.1.1.1.2.7
log
@MFC: sendmail 8.14.2
@
text
@d3 1
a3 1
# Copyright (c) 1999-2004, 2007 Gregory Neil Shapiro.  All Rights Reserved.
d30 1
a30 1
# $Id: buildvirtuser,v 1.8 2007/10/08 18:44:15 gshapiro Exp $
d73 1
a73 1
Here is an example file from the /etc/mail/virtusers/ directory:
d75 1
a75 1
=head2 /etc/mail/virtusers/example.org:
d78 3
a80 3
 MAILER-DAEMON	gshapiro+bounce.$DOMAIN@@example.net
 postmaster	gshapiro+$LHS.$DOMAIN@@example.net
 webmaster	gshapiro+$LHS.$DOMAIN@@example.net
d86 11
a96 2
 gshapiro	gshapiro+$DOMAIN@@example.net
 zoe		zoe@@example.com
a104 1
use File::stat;
a119 12
sub preserve_perms ($$)
{
	my $old = shift;
	my $new = shift;
	my $st;

	$st = stat($old);
	return if (!defined($st));
	chmod($st->mode, $new) || warn "Could not chmod($st->mode, $new): $!\n";
	chown($st->uid, $st->gid, $new) || warn "Could not chmod($st->uid, $st->gid, $new): $!\n";
}

a133 1
	next if ($domain =~ m/^\./);
d136 1
a136 3
	my $mtime = 0;
	my $st = stat("$virts/$domain");
	$mtime = $st->mtime if (defined($st));
d163 1
a163 1
			warn "Bogus line $line in $virts/$domain\n";
d175 1
a175 3
my $virtmtime = 0;
my $st = stat($virt);
$virtmtime = $st->mtime if (defined($st));
a193 1
	preserve_perms($virt, $newvirt);
a198 1
		next if ($domain =~ m/^\./);
a201 1
	preserve_perms($virthosts, $newvirthosts);
@


