summaryrefslogtreecommitdiffstats
path: root/postfix_wizard/Postfix.pm
blob: 46991f394a51b3950984b38b1acb8ec252cd5684 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2002, 2003 Mandrakesoft
#
# Authors: Arnaud Desmons <adesmons@mandrakesoft.com>
#          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;
use MDK::Wizard::Varspaceval;


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 => N("Postfix wizard"),
	 var => { 
		 wiz_mail_masquerade => '',
		 wiz_ext_mail_relay => '',
		 wiz_myorigin => $wiz_domain_name,
		},
	 init => sub {
	   check_sendmail();
	   my $t = grep { /$wiz_host_name/ } cat_("/etc/hosts");
	   if ($t == 0) {
	     return 0, N("Error, can't find your hostname in /etc/hosts. Exiting.")
	   }
	   1
	 },
	 needed_rpm => [ 'postfix' ],
	 defaultimage => "$ENV{__WIZ_HOME__}postfix_wizard/images/courrier.png"
	};


my %level = (
             1 => N("External Mail server"),
             2 => N("Internal Mail server"),
             );


$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, or configure an Internet Mail server."),
			   no_back => 1,
                           pre => sub {
                             $o->{var}{wiz_level} ||= 1;
                           },
			   post => sub {
			     if ($o->{var}{wiz_level} == 2) {
                               return 'originext' }
			   },
                           data => [
                                    { label => N("What do you want to do:"), val => \$o->{var}{wiz_level}, list => [ keys %level ], format => sub { $level{$_[0]} } },
                                   ],

			   next => 'external',
			  },
	       external => {
			  name => N("Outgoing Mail Address") . "\n\n" . N("You can select the kind of address that outgoing mail will show in the \"From:\" and \"Reply-to\" 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();
			  },
			  complete => sub {
			      print "in is $::in\n";
			      if ($o->{var}{wiz_mail_masquerade} !~ /\w+\.\w+$/) {
			          $::in->ask_warn(N("Error"), N("Masquerade should be a valid domain name such as \"mydomain.com\"!"));
			          return 1;
			      }
			  },
			  data => [
				   { label => N("Masquerade domain name:"),  val => \$o->{var}{wiz_mail_masquerade} },
				  ],
			  next => 'isp',
			 },
	       warning => {
			   name => N("Warning:") . "\n\n" . N("You entered an empty address for the mail gateway.") . "\n\n" . 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 => 'summaryext',
			  },
	       error_sendmail => {
				  name => N("Error, sendmail is installed, please remove it before install and configure Postfix"),
				  no_back => 1,
				  next => 0,
				  end => 1,
				 },
	       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 \"host.domain.domaintype\"; for example, if your provider is \"provider.com\", the internet mail server is usually \"smtp.provider.com\"."),
		       pre => sub {
			 $o->{var}{wiz_ext_mail_relay} ||= get_mail_relay();
		       },
		       post => \&check_relay,
		       data => [
				{ label => N("Mail Server Name:"),  val => \$o->{var}{wiz_ext_mail_relay} },
			       ],
		       next => 'originext',
		      },
	       originext => {
			     name => N("The myorigin parameter specifies the domain that locally-posted mail appears to come from") . "\n\n" . N("The default is to append myhostname which is fine for small sites."),
			     pre => sub {
			       $o->{var}{wiz_myorigin} ||= get_myorigin();
			     },
			     data => [
				      { label => N("myorigin:"), val => \$o->{var}{wiz_myorigin} },
				     ],
			     post => sub {
			       if ($o->{var}{wiz_level} == 2) {
				 next => 'summaryint' }
			     },
			     next => 'summaryext',
			 },
	       summaryext => {
			   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 => N("Form of the Address"), fixed_val => \$o->{var}{wiz_mail_masquerade} },
				    { label => N("myorigin"), fixed_val => \$o->{var}{wiz_myorigin} },
				   ],
			   post => \&do_it_external,
			   next => 'endext'
			  },
	       summaryint => {
			      name =>  N("Configuring the Internal Mail Server"),
			      post => \&do_it_internal,
			      next => 'endext'
			     },
	       endext => {
		       name => N("Congratulations") . "\n\n" . N("The wizard successfully configured your Internet Mail service of your server."),
		       end => 1,
		       next => 0
		      },
	       error_end => {
			     name => N("Failed"),
                             data => [ { label => N("Relaunch drakwizard, and try to change some parameters.") } ],
                             no_back => 1,
                             end => 1,
                             next => 0,
                            },
	      };

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

sub check_sendmail {
  my $in = 'interactive'->vnew('su', 'Check sendmail');
  my $w = $in->wait_message(N("Postfix Server"), N("Check if sendmail is installed, to avoid conflict...."));
  my $test = grep { /sendmail/ } system("rpm -qa sendmail");
  !$test or return 'error_sendmail';
  undef $w;
}


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 $relayhost = `/usr/sbin/postconf -h relayhost`;
    chomp($relayhost);
    ($relayhost) = $relayhost =~ /.*\.(.*\..*)/;
    !length $relayhost and $relayhost = $wiz_domain_name;
    $relayhost
}

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

sub get_myorigin {
  my $myorigin = `/usr/sbin/postconf -h myorigin`;
  chomp $myorigin;
  $myorigin;
}

sub save_config {
  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");
    }
}

sub cmd_needed {
  system("/usr/sbin/postmap -r -w /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')
      }
}

sub do_it_internal {
  $::testing and return;
  my $in = 'interactive'->vnew('su', 'postfix configuration');
  my $w = $in->wait_message(N("Postfix Server"), N("Configuring your Postfix server....."));
  save_config();
  my  @conf = ("myhostname = $wiz_host_name",
	     'inet_interfaces = $myhostname',
	     'mydestination = $myhostname, localhost.$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',
	     "mydomain = $wiz_domain_name",
	     "masquerade_domains = $wiz_domain_name",
	     );
# example of internal mail working confoguration
#myhostname = xp2400.guibland.com
#virtual_maps = hash:/etc/postfix/virtual
#canonical_maps = hash:/etc/postfix/canonical
#masquerade_domains = guibland.com
  foreach (@conf) {
    system("/usr/sbin/postconf -e '$_'");
  }
  cmd_needed();
  undef $w;
  check_started('master');
}

sub do_it_external {
  $::testing and return;
  my $in = 'interactive'->vnew('su', 'postfix configuration');
  my $w = $in->wait_message(N("Postfix Server"), N("Configuring your Postfix server....."));
  save_config();

  my @conf = ("myhostname = $wiz_host_name",
	     'inet_interfaces = $myhostname',
	     'mydestination = $myhostname, localhost.$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',
	     "mydomain = $wiz_domain_name",
	     "relayhost = $o->{var}{wiz_ext_mail_relay}",
	     );

#	     "myorigin = $o->{var}{wiz_myorigin}",
#	     "masquerade_domains = $o->{var}{wiz_mail_masquerade}",

    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|| } }
	else { append_to_file($file, $canon) }
      }
  cmd_needed();
  undef $w;
  check_started('master');
}

1;