summaryrefslogtreecommitdiffstats
path: root/postfix_wizard/Postfix.pm
blob: 7a78fb834c59310e6a1b3bc178065edaa27449a2 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2002 Arnaud Desmons <adesmons@mandrakesoft.com>
#               2003 Florent Villard <warly@mandrakesoft.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

package MDK::Wizard::Postfix;
use strict;
use lib qw(/usr/lib/libDrakX); # required for service package

use common;
use MDK::Wizard::Wizcommon;

my $wiz = new MDK::Wizard::Wizcommon;
my $wiz_domain_name = $wiz->{net}->network_get("DOMAINNAME");
my $wiz_host_name = $wiz->{net}->network_get("HOSTNAME");

my $o = { 
    name => 'Postfix wizard',
	var => { 
	    wiz_mail_masquerade => '',
	    wiz_ext_mail_relay => ''
	}
    };

$o->{pages} = { 
		  welcome => {
		      name => N("Internet Mail Configuration Wizard") . "\n\n" . N("This wizard will help you configuring the Internet Mail services for your network."),
		  no_back => 1,
	          next => 'config'
	      },
	      config => {
	          name => N("Outgoing Mail Address") . "\n\n" . N("You can select the kind of address that outgoing mail will show in the \qFrom:\q and \qReply-to\q field.") . "\n\n" . N("This should be chosen consistently with the address you use for incoming mail."),
                  pre => sub {
			$o->{var}{wiz_mail_masquerade} ||= get_mail_masquerade();
		  },
		  post => \&check_masquerade,
                  data => [
		      { label => '' },
		      { label => N("Mail Address:"),  val => \$o->{var}{wiz_mail_masquerade} },
		      ],
	          next => 'isp'
	      },
	      warning => {
		name => N("Warning:"),
		data => [ 
		    { label => N("You entered an empty address for the mail gateway.") },
		    { label => "" },
		    { label => N("Your choice can be accepted, but this will not allow you to send mail outside your local network. Press next to continue, or back to enter a value.") } 
		],
	        next => 'summary'
	      },
	      masquerade_not_good => {
		name => N("Error."),
		data => [ { label => N("Masquerade not good!") } ],
	        next => 'config'
	      },
	      isp => {
	          name => N("Internet Mail Gateway") . "\n\n" . N("Your server will send the outgoing through a mail gateway, that will take care of the final delivery.") . "\n\n" . N("Internet host names must be in the form \qhost.domain.domaintype\q; for example, if your provider is \qprovider.com\q, the internet mail server is usually \qsmtp.provider.com\q."),
                  pre => sub {
			$o->{var}{wiz_ext_mail_relay} ||= get_mail_relay();
		  },
		  post => \&check_relay,
                  data => [
		      { label => '' },
		      { label => N("Mail Server Name:"),  val => \$o->{var}{wiz_ext_mail_relay} },
		      ],
	          next => 'summary'
	      },
	      summary => {
	        name =>  N("Configuring the Internet Mail") . "\n\n" . N("The wizard collected the following parameters needed to configure your Internet Mail Service:") . "\n\n" . N("To accept these values, and configure your server, click the Next button or use the Back button to correct them."),
                data => [
		      { label => N("Internet Mail Gateway"), fixed_val => \$o->{var}{wiz_ext_mail_relay} },
		      { label => '' },
		      { label => N("Form of the Address"), fixed_val => \$o->{var}{wiz_mail_masquerade} },
		  ],
		post => \&do_it,
	        next => 'end'
	      },
	      end => { 
	        name => N("Congratulations"),
                data => [ { label => N("The wizard successfully configured your Internet Mail service of your server.") } ], 
		end => 1,
	        next => 0
	  },
};

sub new {
    my ($class, $_conf) = @_;
    bless {
	    o   => $o,
       }, $class;
}

sub check_masquerade {
    $o->{var}{wiz_mail_masquerade} or return 'masquerade_not_good';
    $o->{var}{wiz_mail_masquerade} =~ /@(\S+)$/ or return 'masquerade_not_good';
}

sub check_relay {
    $o->{var}{wiz_ext_mail_relay} or return 'warning';
    $o->{var}{wiz_ext_mail_relay} =~ /(\S+)\.(\S+)$/ or return 'warning';
}

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 {
    $::DEBUG and return
    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 = $o->{var}{wiz_ext_mail_relay}"
	     );
    foreach (@conf) {
	system("/usr/sbin/postconf -e '$_'");
    }
    if (defined $o->{var}{wiz_ext_mail_relay}) {
	my $file = "/etc/postfix/canonical";
	my $canon = "\n\@$wiz_domain_name $o->{var}{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");
    require services;
    if (services::is_service_running('postfix')) {
	services::restart('postfix') 
    } else {
        services::start('postfix')
    }
    services::is_service_running('xinetd') and services::restart('xinetd');
}

1;