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
|
#!/usr/bin/perl -w
require 5;
use strict;
use Debconf::Client::ConfModule ':all';
use Carp;
use vars qw($reconfiguring $exim3conf $capb $default_configtype);
sub get_value($) {
my $key = shift;
my($code,$text) = get($key);
return $text if ($code == 0);
croak("Unable to fetch value for \"$key\" (debconf code $code)");
}
sub enqueue_question($$) {
my $priority = shift;
my $template = shift;
my($code,$text) = input($priority, $template);
if ($code && $code != 30) {
croak("Error asking question \"$template\" (debconf code $code)");
}
}
sub set_default($$) {
my $key = shift;
my $defaultvalue = shift;
my($code, $text) = get($key);
if ($code == 0) {
if ($text eq '') {
($code,$text) = set($key, $defaultvalue);
# Check $code...
}
}
else {
croak("Unable to fetch value for \"$key\" (debconf code $code)");
}
}
sub fetch_default($$) {
my $key = shift;
my $defaultcb = shift;
my($code, $text) = get($key);
if ($code == 0) {
if ($text eq '') {
my $defaultvalue = &$defaultcb;
if (defined($defaultvalue)) {
($code, $text) = set($key, $defaultvalue);
# Check $code...
}
}
}
else {
croak("Unable to fetch value for \"$key\" (debconf code $code)");
}
}
sub debug {
if ($ENV{EXIMCONF_DEBUG}) {
print STDERR "$0: @_\n";
}
}
sub exim3_readconfig {
my $filename = shift || "/etc/exim/exim.conf";
if (open(EXIMCONF, $filename)) {
my $config;
my $keyword;
scanmain:
while (<EXIMCONF>) {
chomp;
if (/^end\b/) {
last scanmain;
}
elsif (/^\s*([a-zA-z0-9_]+)\s*=\s*(.*)/) {
$keyword = $1;
my $value = $2;
while ($value =~ s/\\$//) {
my $nextline = <EXIMCONF>;
$value .= $nextline;
}
$config->{main}->{$keyword} = $value;
}
elsif (/^\s*no_([a-zA-Z0-9_]+)\s*$/) {
$config->{main}->{$keyword} = '';
}
}
for my $section (qw|transports directors routers|) {
scansection:
while (<EXIMCONF>) {
chomp;
if (/^end\b/) {
last scansection;
}
elsif (/^([a-zA-Z0-9_]+):\s*/) {
my $objname = $1;
objectline:
while (<EXIMCONF>) {
next if (/^\s*\#/ || !/\S/);
if (/^\s*([a-zA-Z0-9_]+)\s*=\s*(.*)/) {
my $propname = $1;
my $value = $2;
while ($value =~ s/\\$//) {
my $nextline = <EXIMCONF>;
$value .= $nextline;
}
$config->{$section}->{$objname}->{$propname} = $value;
}
else {
redo scansection;
}
}
}
}
}
close(EXIMCONF);
debug("Read Exim 3 config file $filename");
return $config;
}
else {
debug("Unable to read $filename: $!");
}
return undef;
}
version('2.0');
$capb = capb(''); # FIXME: Support backup
######################################################################
$reconfiguring = 1 if ($ARGV[0] eq 'reconfigure');
if (-f "/etc/exim4/exim4.conf" && !$reconfiguring) {
exit(0); # exit if exim4.conf already built and we are not explicitly reconfiguring
}
######################################################################
# Look for old Exim3 config file
if (-f "/etc/exim/exim.conf") {
enqueue_question("high", "exim/configconvert/question");
go();
my $convertresponse = get_value("exim/configconvert/question");
if ($convertresponse eq 'convert') {
enqueue_question("high", "exim/configconvert/convertnotice");
go();
system("/etc/init.d/exim4", "stop") if ($reconfiguring);
exit(0);
}
elsif ($convertresponse eq 'none') {
enqueue_question("high", "exim/configconvert/breakagenotice");
go();
exit(0);
}
elsif ($convertresponse eq 'createnew') {
# Do not read exim3 config file for defaults if exim4.conf has been created already
# The defaults should have been seen once by the user and stored in debconf anyway.
$exim3conf = exim3_readconfig() unless (-f "/etc/exim4/exim4.conf");
}
else {
die "exim/configconvert/question has an unrecognised answer: (\"$convertresponse\")\n";
}
}
######################################################################
# Ask eximconfig questions
# Grab the configtype from the old exim3 config file
# TRANSPORT(remote_smtp) does not exist ==> local
# ROUTER(lookuphost) exists ==> internet
# DIRECTOR(smart) exists ==> satellite
# DEFAULT ==> smarthost
fetch_default("exim/eximconfig/configtype", sub {
return "local" if (!defined($exim3conf->{transports}->{remote_smtp}));
return "internet" if (defined($exim3conf->{routers}->{lookuphost}));
return "satellite" if (defined($exim3conf->{directors}->{smart}));
return "smarthost"; });
enqueue_question("high", "exim/eximconfig/configtype");
go();
my $configtype = get_value("exim/eximconfig/configtype");
if ($configtype eq 'none') {
exit(0);
}
my $syshostname = $exim3conf->{main}->{primary_hostname} || $exim3conf->{main}->{qualify_domain} || `hostname --fqdn` || `hostname`;
my $visiblename;
if ($configtype eq 'satellite') {
set_default("exim/eximconfig/satellite_hostname", $syshostname);
enqueue_question("medium", "exim/eximconfig/satellite_hostname");
go();
$visiblename = get_value("exim/eximconfig/satellite_hostname");
}
else {
set_default("exim/eximconfig/visible_hostname", $syshostname);
enqueue_question("medium", "exim/eximconfig/visible_hostname");
go();
$visiblename = get_value("exim/eximconfig/visible_hostname");
}
if ($configtype eq 'internet' || $configtype eq 'smarthost') {
subst("exim/eximconfig/other_hostnames", "visiblename", $visiblename);
enqueue_question("high", "exim/eximconfig/other_hostnames");
# Downgrade these to medium since having them empty is likely to be reasonable?
enqueue_question("high", "exim/eximconfig/relay_domains");
enqueue_question("high", "exim/eximconfig/relay_nets");
}
my $ourdomain = $syshostname;
$ourdomain =~ s/^[^.][^.]*\.//;
if ($configtype eq 'satellite') {
# Exim 3 satellite configuration?
# Look for director called "smart"
# .. and new_address setting of "${local_part}@SOME_DOMAIN"
if ($exim3conf->{directors}->{smart} && $exim3conf->{directors}->{smart}->{new_address} =~ /^\${local_part}\@(.+)/) {
set_default("exim/eximconfig/readhost", $1);
}
else {
set_default("exim/eximconfig/readhost", $ourdomain);
}
# This is high priority even though it has a default, since $ourdomain
# is not necessarily "reasonable".. mail.$ourdomain would be equally "reasonable"
enqueue_question("high", "exim/eximconfig/readhost");
go();
my $readhost = get_value("exim/eximconfig/readhost");
set_default("exim/eximconfig/smarthost", $readhost);
enqueue_question("medium", "exim/eximconfig/smarthost");
}
elsif ($configtype eq 'smarthost') {
set_default("exim/eximconfig/smarthost", $ourdomain);
enqueue_question("high", "exim/eximconfig/smarthost");
}
enqueue_question("medium", "exim/eximconfig/overwrite_aliases");
enqueue_question("medium", "exim/daemon/permissions");
go();
if ($reconfiguring) {
system("/etc/init.d/exim4", "stop");
if (-f "/etc/exim4/exim4.conf") {
unlink("/etc/exim4/exim4.conf-reconfigure");
rename("/etc/exim4/exim4.conf", "/etc/exim4/exim4.conf-reconfigure");
}
}
exit(0);
|