summaryrefslogtreecommitdiffstats
path: root/postfix_wizard/scripts/Postfixconf.pm
blob: e34234d92f39fb37e6d8c171ad6af29ad38b5b54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/perl

package Postfixconf;
use lib '/usr/lib/libDrakX';
require "__WIZ_HOME__/common/scripts/Vareqval.pm";
require "__WIZ_HOME__/common/scripts/IFCFG.pm";
use MDK::Common;
use services;
use strict;

my $o = IFCFG->new();
my $wiz_domain_name = $o->network_get("DOMAINNAME");
my $wiz_host_name = $o->network_get("HOSTNAME");

sub check_masquerade {
    $ENV{wiz_mail_masquerade} or return 1;
    $ENV{wiz_mail_masquerade} =~ /@(\S+)$/ or return 1;
    return 10
}

sub check_relay {
    $ENV{wiz_ext_mail_relay} or return 1;
    $ENV{wiz_ext_mail_relay} =~ /(\S+)\.(\S+)$/ or return 1;
    return 10;
}

sub	get_mail_masquerade {
    my $login = `logname`;
    my $relayhost = `/usr/sbin/postconf -h relayhost`;
    chomp($relayhost);
    ($relayhost) = $relayhost =~ /.*\.(.*\..*)/;
    !length $relayhost and $relayhost = $wiz_domain_name;
    chomp($login);
    "$login\@$relayhost";
}

sub	get_mail_relay {
    my $relayhost = `/usr/sbin/postconf -h relayhost`;
    chomp $relayhost;
    !length $relayhost and $relayhost = "smtp.$wiz_domain_name";
    $relayhost;
}

sub	do_it {
    my @conf = qw(/etc/postfix/aliases
		  /etc/postfix/canonical
		  /etc/postfix/main.cf
		  /etc/postfix/master.cf
		  /etc/postfix/virtual);
    foreach (@conf) {
        (-f $_) and MDK::Common::cp_af($_, $_.".orig");
    }
    @conf = ("myhostname = $wiz_host_name",
	     'myorigin = $mydomain',
	     'inet_interfaces = all',
	     'mydestination = $myhostname, localhost.$mydomain',
	     'masquerade_domains = $mydomain',
	     'alias_maps = hash:/etc/postfix/aliases',
	     'alias_database = hash:/etc/postfix/aliases',
	     'virtual_maps = hash:/etc/postfix/virtual',
	     'canonical_maps = hash:/etc/postfix/canonical',
	     "relayhost = $ENV{wiz_ext_mail_relay}"
	     );
    foreach (@conf) {
	system("/usr/sbin/postconf -e \'$_\'");
    }
    if (defined $ENV{wiz_ext_mail_relay}) {
	my $file = "/etc/postfix/canonical";
	my $canon = "\n\@$wiz_domain_name $ENV{wiz_mail_masquerade}";
	my $t;
	foreach (cat_($file)) {
	    if (/^\s*(?!\#)\s*\@$wiz_domain_name.*/) {
		$t = $_;
		last;
	    }
	}
	if ($t) { substInFile { s|$t|\#$&$canon|} $file } 
	else { append_to_file($file, $canon) }
    }
    system("/usr/sbin/postmap /etc/postfix/canonical");
    system("/usr/sbin/postmap /etc/postfix/virtual");
    system("/usr/sbin/postalias /etc/postfix/aliases");
    system("/usr/sbin/postfix check");
    if (services::is_service_running('postfix')) {
	services::restart('postfix') 
    } else {
        services::start('postfix')
    }
    services::is_service_running('xinetd') and services::restart('xinetd');
    10;
}

1;