#!/usr/bin/perl
#
# $Id: configure.pl,v 1.4 2005/09/20 17:12:34 asv Exp $
#
# Configuration script for Dr.Web Postfix
#

my $dw_bin_dir_def = "/opt/drweb";
my $dw_etc_dir_def = "/etc/drweb";
if ($^O=~/BSD/i) {
    $dw_bin_dir_def = "/usr/local/drweb";
    $dw_etc_dir_def = "/usr/local/etc/drweb";
}
if ($^O =~ /Linux/i) { $ps_etc_dir_def = "/etc/postfix"; }
if ($^O =~ /BSD/i) { $ps_etc_dir_def = "/usr/local/etc/postfix"; }
else { $ps_etc_dir_def = "/etc/postfix"; }
$hostname = `hostname`;
chomp $hostname;
if ($hostname eq "") {
    $hostname = "localhost";
}
$admin_mail_def = "postmaster\@$hostname";
$filter_mail_def = "DrWEB-DAEMON\@$hostname";

print "\nEnter the Dr.Web binary directory.\n [default=$dw_bin_dir_def]:";
$dw_bin_dir = <>;
chomp $dw_bin_dir;
if ($dw_bin_dir eq "") {
    $dw_bin_dir = $dw_bin_dir_def;
}
   
print "\nEnter the Dr.Web configs directory.\n [default=$dw_etc_dir_def]:";
$dw_etc_dir = <>;
chomp $dw_etc_dir;
if ($dw_etc_dir eq "") {
    $dw_etc_dir = $dw_etc_dir_def;
}
   
print "\nEnter directory, where Postfix configuration files was installed.\n default=[$ps_etc_dir_def]:";
$ps_etc_dir = <>;
chomp $ps_etc_dir;
if ($ps_etc_dir eq "") {
    $ps_etc_dir = "$ps_etc_dir_def";
}
$mastercf = "$ps_etc_dir/master.cf";

open (MASTERCF,"<$mastercf") or die "ERROR: cannot open Postfix configuration file $mastercf\n";
    while (<MASTERCF>) {
	@substr = split(/\s/);
	if ($substr[0] eq "filter") {
	    print "ERROR: master.cf already contain filter !\n"; 
	    exit;
	}
    }
close MASTERCF;

print "\nEnter mail account where virus notifications would be sent.\n [default=$admin_mail_def]:";
$admin_mail = <>;
chomp $admin_mail;
if ($admin_mail eq "") {
    $admin_mail = $admin_mail_def;
}

print "\nEnter mail account that be used as source of notifications.\n [default=$filter_mail_def]:";
$filter_mail = <>;
chomp $filter_mail;
if ($filter_mail eq "") {
    $filter_mail = $filter_mail_def;
}
   
print "\n\n==============\nConfiguration:\n";
print qq(
Dr.Web bin directory = $dw_bin_dir
Dr.Web etc directory = $dw_etc_dir
Postfix configuration = $ps_etc_dir
Admin mail = $admin_mail
Filter mail = $filter_mail
Is configuration correct ? [y/n]
);
$confirm = <>; chomp $confirm; if ($confirm eq "y"){} else {exit;}

print "\nNow script modify Postfix configuration files, original files\n";
print "would be storied with .drwebsave extension.\n";
`cp $mastercf $mastercf.drwebsave`;
open (MASTERCF,">>$mastercf")
  or die "ERROR: cannot open for append $mastercf\n";
print MASTERCF qq(\# Added by Dr.Web for Postfix
filter    unix  -   n   n   -   16   pipe
    flags=R user=drweb argv=$dw_bin_dir/drweb-postfix -s \${size} -f \${sender} -- \${recipient} \n);
    close MASTERCF;
    
    open (MASTERCF,"<$mastercf");
    undef $/;
    $file = <MASTERCF>;
    close MASTERCF;
    
    open (MASTERCF,">$mastercf")
      or die "ERROR: cannot open for write $mastercf\n";
    $file =~s/smtpd/smtpd -o content_filter=filter:dummy/g;
    print MASTERCF "$file";
    close MASTERCF;
print "\n$mastercf patched - OK\n\n";

`cp $dw_etc_dir/drweb_postfix.conf $dw_etc_dir/drweb_postfix.conf.drwebsave`;
open (DRWEBPOSTFIX,"<$dw_etc_dir/drweb_postfix.conf") 
 or die "ERROR: cannot open for read $dw_etc_dir/drweb_postfix.conf\n";
    undef $/;
    $drweb_postfix_conf = <DRWEBPOSTFIX>;
    close DRWEBPOSTFIX;

    open (DRWEBPOSTFIX,">$dw_etc_dir/drweb_postfix.conf") 
      or die "ERROR: cannot open for write $dw_etc_dir/drweb_postfix.conf\n";
    $drweb_postfix_conf =~s/AdminMail.*/AdminMail = $admin_mail/g;
    $drweb_postfix_conf =~s/FilterMail.*/FilterMail = $filter_mail/g;
    print DRWEBPOSTFIX "$drweb_postfix_conf";
    close DRWEBPOSTFIX;
print "$dw_etc_dir/drweb_postfix.conf patched - OK\n\n";
       

print qq(\nDrweb filter now integrated with Postfix MTA.\n
You also need to configure $dw_etc_dir/drweb_postfix.conf and restart Postfix!\n\n);