Postfix Virtual domain support utility

Michael Fair michael at daclubhouse.net
Sun Sep 8 19:36:31 EDT 2002


Hey all,

Appended to the bottom of this email is a small
perl script which is designed to help with 
administering virtual domains in a Postfix setup.

Essentially it lets the administrator specify
multiple files in a "domains" directory (each
logically representing a different domain)
inside which can be specified a transport to 
use and multiple domain local aliases.  It then
finishes by running postmap and postfix reload.

It's main usefullness in regards to Cyrus is
that you do not have add anything to the
mydestination line in order to support new
domains.  A new file with the single line:

domain.dom    :

(yes the ':' is important as it says to use
the default transport) and running the script
is all that is needed.

The names of the files aren't significant though
I name them after the domain they represent.

It's published under the terms of the GPL.

I hope this useful to others aside myself.

-- Michael --


#!/usr/bin/perl

# rehash.pl
#
# This utility is published under the terms of
# the GNU GPL license version 2 or any later license.
# See http://www.gnu.org/licenses/gpl.html
# for the complete text.
#
# This is a very simple perl utility that looks in
# the /etc/postfix/domains directory for a list of
# files and then them creates two Postfix config
# files virtual_map and transport_map.  Finally it
# runs the postmap command on the two new files and
# runs postfix reload to activate them.
#
# The transport_map file allows you to not be forced
# to specify each domain name you host on the
# mydestination line in main.cf and gives you the
# ability to choose which transport is called on
# a per domain basis.  This helps for automated
# creation/deletion of domains and putting domains
# on separate servers.
#
# The virtual_map file gives you domain local aliases.
#
# See the Postfix documentation for more info on
# these two mechanisms.
#
# To make this work add the following to main.cf:
# virtual_maps = hash:/etc/postfix/virtual_map
# transport_maps = hash:/etc/postfix/trasnport_map
#
# Optionally you can specify "default_transport"
# which will be used when a transport isn't specfied
# elsewhere.  Again see the Postfix docs for more info.
#
# You can remove all references to domains from the
# mydestination parameter as long as there is an entry
# for it in the "domains" directory.
#
# Then the domain specific files look like this:
# domain.dom            transport:nexthop
# alias1 at domain.dom     user1
# alias2 at domain.dom     user2
# alias3 at domain.dom     user at otherdomain.dom

##################################################
# WHAT IT DOES                                   #
##################################################
# First it looks for all the files in $mapsdir.
# Any file starting with '.' is skipped.
#
# For each file it processes, the first line is
# put into the transport_map file unless the first
# character is '#' in which case nothing is added.
#
# Next the whole file is appended to virtual_map
# and the postmap commands are run.
#
# Lastly it runs postfix reload
##################################################


# Edit these as needed
$postfix_etc = "/etc/postfix";
$postfix_bin = "/usr/sbin";

$mapsdir = shift(@ARGV) || "$postfix_etc/domains";
$vfile = "$postfix_etc/virtual_map";
$tfile = "$postfix_etc/transport_map";

###################################################
# Shouldn't need to edit beyond this point        #
###################################################

opendir(MAPS, "$mapsdir") or die "Couldn't open $mapsdir: $!";
@allfiles = readdir MAPS;
closedir(MAPS);

open(VFILE, ">$vfile") or die "Couldn't open $vfile: $!";


for $file (@allfiles) {
    if ($file =~ /^\./) { next; }
    open(TMP, "<$mapsdir/$file") or die "Couldn't open $file: $!";
    $line = <TMP>;
    $transport_map .= $line unless ($line =~ /^\\#/);

    print VFILE $line;
    while ($line = <TMP>) {
        print VFILE $line;
    }
    close(TMP);
}

close(VFILE);

open(TFILE, ">$tfile") or die "Couldn't open $vfile: $!";
print TFILE $transport_map;
close(TFILE);

system("$postfix_bin/postmap hash:$vfile");
system("$postfix_bin/postmap hash:$tfile");
system("$postfix_bin/postfix reload");






More information about the Info-cyrus mailing list