head	1.3;
access;
symbols;
locks; strict;
comment	@# @;


1.3
date	2001.08.20.19.40.20;	author edk;	state Exp;
branches;
next	1.2;

1.2
date	2001.06.26.23.04.30;	author edk;	state Exp;
branches;
next	1.1;

1.1
date	2001.06.26.22.23.44;	author edk;	state Exp;
branches;
next	;


desc
@@


1.3
log
@* integrate a patch from andrewr which avoids magic strings outside the
  top of the script
* using qmail init script so that this will work on solaris as well as
  linux, and so qmail will notice changes to concurrencyremote /
  concurrencylocal (thanks to andrewr for pointing out the need for this)
* allow the domain to be specified via --domain rather than by sourcing
  env.sh
* minor bug fixes / refactoring
@
text
@#!/usr/bin/perl

use Getopt::Long;

# Where qmail control files are located
$qmail_control_dir = "/var/qmail/control";

# User who gets tigris email
$tigris_mail_user = "tigrisq";

# Default local concurrency
$concurrencylocal_default = 20;

# Default remote concurrency
$concurrencyremote_default = 100;

main();

sub main
{
    my $options = getOptions();
    checkEnv($options);
    my $files;
    if ($options->{all})
    {
        $files = allFiles($options);
    }
    else
    {
        $files = checkFiles($options);
    }
    if ($options->{debug})
    {
        print "files are : ", join ' ', @@$files, "\n";
    }
    if ($options->{install})
    {
        installFiles($options, $files);
    }
    elsif (scalar(@@$files))
    {
        showFiles($options, $files);
    }
}

sub checkEnv
{
    my ($options) = @@_;
    if (! $ENV{DOMAIN} && ! $options->{domain})
    {
        showUsage("you must source env.sh or specify the --domain option");
    }
}


sub getOptions
{
    my %options;
    if (!GetOptions(\%options, 
                "help",
                "debug",
                "domain=s",
                "install",
                "all") or $options{help})
    {
        showUsage();
    }

    return \%options;
}

sub showUsage
{
    my ($message) = @@_;
    print <<EOM;
Usage: qmail-conf [--help] [--install] [--all] [--debug] [--domain=DOMAIN]
        help            Show this information
        install         Install new qmail conf files if needed
        all             Operate on all qmail conf files, not just the
                            ones with known problems.
        debug           Produce verbose debugging output
        domain          The domain for this system

This script generates -- and if requested installs -- qmail configuration
information.

Note that you must either source env.sh before running this script, or
specify --domain when you call it.
EOM

    print "\n\tERROR MESSAGE\n\t$message\n" if ($message);

    exit;
}

sub installFiles
{
    my ($options, $files) = @@_;
    foreach my $file (@@$files)
    {
        my $contents = generateFile($options, $file);
        my $fileName = "$qmail_control_dir/$file";
        open (OUT, ">$fileName") or die "unable to write $fileName: $!";
        print OUT $contents;
        close OUT;
    }
    
    my @@search_dirs = ('/etc/init.d', '/etc/rc.d/init.d/');
    #
    # restart qmail
    #
    my $program = undef;
    foreach my $dir (@@search_dirs)
    {
        if (-x "$dir/qmail")
        {
            $program = "$dir/qmail";
        }
    }

    if (not defined($program))
    {
        showUsage("unable to find qmail init script in any of "
                . join(' ', @@search_dirs));
    }
    if (system($program, 'stop') != 0)
    {
        warn "attempt to stop qmail via $program failed: $!";
    }
    sleep(2);
    if (system($program, 'start') != 0)
    {
        warn "attempt to start qmail via $program failed: $!";
    }
}

sub showFiles
{
    my ($options, $files) = @@_;
    foreach my $file (@@$files)
    {
        my $contents = generateFile($options, $file);
        print "for file $file, the following contents would be valid: ",
                "\n\n",'*'x50,"\n$contents",'*'x50,"\n\n";
    }
}

sub generateFile
{
    my ($options, $filename) = @@_;
    my $domain = findDomain($options);
    if ($filename eq 'locals')
    {
        return '';
    }
    elsif ($filename eq 'virtualdomains')
    {
        return "$domain:$tigris_mail_user\n.$domain:$tigris_mail_user\n";
    }
    elsif ($filename eq 'rcpthosts')
    {
        my $hostname = `uname -n`;
        if ($hostname !~ m/\./)
        {
            $hostname = `hostname -f`;
        }
        chomp $hostname;
        my $string = "$domain\n.$domain\n";
        if ($hostname ne $domain)
        {
            $string .= "$hostname\n";
        }
        return $string;
    }
    elsif ($filename eq 'concurrencylocal')
    {
        return "$concurrencylocal_default\n";
    }
    elsif ($filename eq 'concurrencyremote')
    {
        return "$concurrencyremote_default\n";
    }
    elsif ($filename eq 'me')
    {
        return "$domain\n";
    }
    elsif ($filename eq 'defaultdomain')
    {
        return "$domain\n";
    }
    else
    {
        warn "unknown file $filename";
        return '';
    }
}

sub allFiles
{
    return [ qw/virtualdomains rcpthosts locals me defaultdomain concurrencyremote concurrencylocal/ ];
}

sub checkFiles
{
    my ($options) = @@_;
    my @@bad_files;
    my @@lines;
    my $domain = findDomain($options);

    if ($options->{debug})
    {
        print "checking virtualdomains, locals, rcpthosts\n";
    }
    # check virtualdomains
    open(IN, "$qmail_control_dir/virtualdomains");
    @@lines = <IN>;
    close IN;
    if (! scalar(grep /^$domain:$tigris_mail_user$/, @@lines) ||
            ! scalar(grep /^\.$domain:$tigris_mail_user$/, @@lines))
    {
        push @@bad_files, 'virtualdomains';
    }
    
    # check locals
    open(IN, "$qmail_control_dir/locals");
    @@lines = <IN>;
    close IN;
    if (scalar(grep /$domain$/, @@lines))
    {
        push @@bad_files, 'locals';
    }

    # check rcpthosts
    open (IN, "$qmail_control_dir/rcpthosts");
    @@lines = <IN>;
    close IN;
    if (! scalar(grep /^$domain$/, @@lines) ||
            ! scalar(grep /^\.$domain$/, @@lines))
    {
        push @@bad_files, 'rcpthosts';
    }
    return \@@bad_files;
}

sub findDomain
{
    my ($options) = @@_;
    return $options->{domain} || $ENV{DOMAIN};
}

@


1.2
log
@fix bug in checking algorithm where bad files weren't communicated back.
@
text
@d5 12
a20 1
    checkEnv();
d22 1
d48 2
a49 1
    if (! $ENV{SANDBOX})
d51 1
a51 1
        showUsage("you must source env.sh");
d62 1
d76 1
a76 1
Usage: qmail-conf [--help] [--install] [--all] [--debug]
d82 1
d87 2
a88 2
Note that you must have the env variables from env.sh defined in order to
use this script.
d101 2
a102 2
        my $contents = generateFile($file);
        my $fileName = "/var/qmail/control/$file";
d107 28
a134 1
    system('killall','-HUP','qmail-send');
d142 1
a142 1
        my $contents = generateFile($file);
d150 2
a151 1
    my ($filename) = @@_;
d158 1
a158 1
        return "$ENV{DOMAIN}:tigrisq\n.$ENV{DOMAIN}:tigrisq\n";
d168 2
a169 2
        my $string = "$ENV{DOMAIN}\n.$ENV{DOMAIN}\n";
        if ($hostname ne $ENV{DOMAIN})
d177 1
a177 1
        return "20\n";
d181 1
a181 1
        return "50\n";
d185 1
a185 1
        return "$ENV{DOMAIN}\n";
d189 1
a189 1
        return "$ENV{DOMAIN}\n";
d200 1
a200 1
    return [ qw/virtualdomains rcpthosts locals me defaultdomain/ ];
d208 1
d215 1
a215 1
    open(IN, "/var/qmail/control/virtualdomains");
d218 2
a219 2
    if (! scalar(grep /^$ENV{DOMAIN}:tigrisq$/, @@lines) ||
            ! scalar(grep /^\.$ENV{DOMAIN}:tigrisq$/, @@lines))
d225 1
a225 1
    open(IN, "/var/qmail/control/locals");
d228 1
a228 1
    if (scalar(grep /$ENV{DOMAIN}$/, @@lines))
d234 1
a234 1
    open (IN, "/var/qmail/control/rcpthosts");
d237 2
a238 2
    if (! scalar(grep /^$ENV{DOMAIN}$/, @@lines) ||
            ! scalar(grep /^\.$ENV{DOMAIN}:tigrisq$/, @@lines))
d244 7
@


1.1
log
@a first pass at a script to handle qmail conf files
@
text
@d198 1
@

