1 |
#!/usr/bin/perl |
2 |
|
3 |
# Drakwizard |
4 |
|
5 |
# Copyright (C) 2003 Mandrakesoft |
6 |
# |
7 |
# Author: Florent Villard <warly@mandrakesoft.com> |
8 |
# aginies <aginies _ateuh_ mandrakesoft.com> |
9 |
# |
10 |
# This program is free software; you can redistribute it and/or modify |
11 |
# it under the terms of the GNU General Public License as published by |
12 |
# the Free Software Foundation; either version 2, or (at your option) |
13 |
# any later version. |
14 |
# |
15 |
# This program is distributed in the hope that it will be useful, |
16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
# GNU General Public License for more details. |
19 |
# |
20 |
# You should have received a copy of the GNU General Public License |
21 |
# along with this program; if not, write to the Free Software |
22 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
|
24 |
package MDK::Wizard::Proftpd; |
25 |
use strict; |
26 |
|
27 |
use common; |
28 |
use services; |
29 |
use MDK::Wizard::Wizcommon; |
30 |
|
31 |
my $wiz = MDK::Wizard::Wizcommon->new; |
32 |
my $in = interactive->vnew; |
33 |
|
34 |
my $file = "/etc/proftpd.conf"; |
35 |
|
36 |
my ($servername) = cat_($file) =~ /ServerName\s+(\S*.*)/; |
37 |
my ($ftp_port) = cat_($file) =~ /Port\s+(\d+)/; |
38 |
|
39 |
my $o = { |
40 |
name => N("FTP wizard"), |
41 |
var => { |
42 |
wiz_ftp_external => '0', |
43 |
wiz_ftp_internal => '1', |
44 |
wiz_root_login => '', |
45 |
wiz_server_admin => '', |
46 |
wiz_default_root => '', |
47 |
wiz_ftp_resume => '', |
48 |
wiz_ftp_fxp => '', |
49 |
wiz_server_name => '', |
50 |
wiz_port => '', |
51 |
}, |
52 |
needed_rpm => [ 'proftpd' ], |
53 |
defaultimage => "/usr/share/mcc/themes/default/ftp-mdk.png", |
54 |
}; |
55 |
|
56 |
my %opt = ( |
57 |
"SystemLog" => "/var/log/proftpd/proftpd.log", |
58 |
"TransferLog" => "/var/log/proftpd/xferlog", |
59 |
"Extendedlog" => "/var/log/proftpd/ftp.log", |
60 |
"LogFormat default" => qq("%h %l %u %t "%r" %s %b"), |
61 |
"LogFormat auth" => qq("%v [%P] %h %t "%r" %s"), |
62 |
"LogFormat write" => qq("%h %l %u %t "%r" %s %b"), |
63 |
"ServerIdent" => "off", |
64 |
"DeferWelcome" => "on", |
65 |
"DisplayConnect" => "/etc/banner-proftpd", |
66 |
"AccessDenyMsg" => qq(" !-!! ACCESS DENY !!-! SEEMS YOU HAVE NO RIGHT THERE !!"), |
67 |
"AccessGrantMsg" => qq(" -- Guest access granted for %u --"), |
68 |
"IdentLookups" => "off", |
69 |
"UseReverseDNS" => "off", |
70 |
"TimesGMT" => "off", |
71 |
"DirFakeUser" => "off nobody", |
72 |
"DirFakeGroup" => "off nobody", |
73 |
"DeleteAbortedStores" => "off", |
74 |
"PersistentPasswd" => "off", |
75 |
); |
76 |
|
77 |
$o->{pages} = { |
78 |
welcome => { |
79 |
name => N("FTP server configuration wizard") . "\n\n" . N("This wizard will help you configuring an FTP server for your network."), |
80 |
no_back => 1, |
81 |
post => \&check, |
82 |
next => 'config' |
83 |
}, |
84 |
config => { |
85 |
name => N("FTP server") . "\n\n" . N("Your server can act as an FTP server toward your internal network (intranet) and as an FTP server for the Internet.") . "\n\n" . N("Select the kind of FTP service you want to activate:"), |
86 |
data => [ |
87 |
{ label => N("Enable the FTP server for the intranet"), type => 'bool', val => \$o->{var}{wiz_ftp_internal} }, |
88 |
{ label => N("Enable the FTP server for the Internet"), type => 'bool', val => \$o->{var}{wiz_ftp_external} }, |
89 |
], |
90 |
complete => sub { |
91 |
if ($o->{var}{wiz_ftp_external} == 0 && $o->{var}{wiz_ftp_internal} == 0) { |
92 |
$in->ask_warn(N("Error"), N("Please choose whether to allow a connection to FTP server from internal or external hosts.")); |
93 |
return 1; |
94 |
} else { return 0 } |
95 |
}, |
96 |
next => 'options' |
97 |
}, |
98 |
options => { |
99 |
name => N("FTP Proftpd server options, step 1") . "\n\n" . N("Permit root login: allow root to log on FTP server.") . "\n" . N("Admin email: email address of the FTP administrator."), |
100 |
pre => sub { |
101 |
$o->{var}{wiz_root_login} ||= 0; |
102 |
$o->{var}{wiz_server_name} = $servername; |
103 |
}, |
104 |
data => [ |
105 |
{ label => N("Server name:"), val => \$o->{var}{wiz_server_name} }, |
106 |
{ label => N("Admin email:"), val => \$o->{var}{wiz_server_admin}, help => 'admin@guibland.com' }, |
107 |
{ label => N("Permit root login:"), type => 'bool', val => \$o->{var}{wiz_root_login} }, |
108 |
], |
109 |
complete => sub { |
110 |
if (!any { /bash/ } cat_("/etc/shells")) { |
111 |
$in->ask_warn(N("Error"), N("I can't find bash in list of shells! It seems you have modified it by hand! Please correct.")); |
112 |
return 1; |
113 |
} |
114 |
if (!$o->{var}{wiz_server_name}) { |
115 |
$in->ask_warn(N("Error"), N("Need a server name")); |
116 |
return 1; |
117 |
} |
118 |
}, |
119 |
next => 'options_step2', |
120 |
}, |
121 |
options_step2 => { |
122 |
name => N("FTP server options, step 2") . "\n\n" . N("Chroot home user: users will only see their home directory.") . "\n" . N("Allow FTP resume: allow resume upload or download on FTP server.") . "\n" . N("Allow FXP: allow file transfer via another FTP."), |
123 |
pre => sub { |
124 |
$o->{var}{wiz_default_root} ||= 1; |
125 |
$o->{var}{wiz_ftp_resume} ||= 1; |
126 |
$o->{var}{wiz_ftp_fxp} ||= 0; |
127 |
$o->{var}{wiz_port} = $ftp_port || "21"; |
128 |
}, |
129 |
data => [ |
130 |
{ label => N("FTP Port:"), val => \$o->{var}{wiz_port}, help => 'Default port is 21 for an FTP server' }, |
131 |
{ label => N("Chroot home user:"), type => 'bool', val => \$o->{var}{wiz_default_root} }, |
132 |
{ label => N("Allow FTP resume:"), type => 'bool', val => \$o->{var}{wiz_ftp_resume} }, |
133 |
{ label => N("Allow FXP:"), type => 'bool', val => \$o->{var}{wiz_ftp_fxp} }, |
134 |
], |
135 |
complete => sub { |
136 |
if ($o->{var}{wiz_port} !~ /^\d+$/) { |
137 |
$in->ask_warn(N("Error"), N("FTP Port should be a number.")); |
138 |
return 1; |
139 |
} }, |
140 |
next => 'summary', |
141 |
}, |
142 |
warning_dhcp => { |
143 |
name => N("Warning.") . "\n\n" . N("You are in DHCP, server may not work with your configuration."), |
144 |
ignore => 1, |
145 |
next => 'config' |
146 |
}, |
147 |
must_be_root => { |
148 |
name => N("Error.") . "\n\n" . N("Sorry, you must be root to do this..."), |
149 |
ignore => 1, |
150 |
next => 'config' |
151 |
}, |
152 |
summary => { |
153 |
name => N("The wizard collected the following parameters needed to configure your FTP server") . "\n" . N("To accept those values, and configure your server, click the next button or use the back button to correct them"), |
154 |
pre => sub { |
155 |
$o->{var}{internal} = $o->{var}{wiz_ftp_internal} ? N("enabled") : N("disabled"); |
156 |
$o->{var}{external} = $o->{var}{wiz_ftp_external} ? N("enabled") : N("disabled"); |
157 |
$o->{var}{rootlogin} = $o->{var}{wiz_root_login} ? N("enabled") : N("disabled"); |
158 |
$o->{var}{defaultroot} = $o->{var}{wiz_default_root} ? N("enabled") : N("disabled"); |
159 |
$o->{var}{ftpresume} = $o->{var}{wiz_ftp_resume} ? N("enabled") : N("disabled"); |
160 |
$o->{var}{fxp} = $o->{var}{wiz_ftp_fxp} ? N("enabled") : N("disabled"); |
161 |
}, |
162 |
data => [ |
163 |
{ label => N("FTP Port:"), val_ref => \$o->{var}{wiz_port} }, |
164 |
{ label => N("Intranet FTP server:"), val_ref => \$o->{var}{internal} }, |
165 |
{ label => N("Internet FTP server:"), val_ref => \$o->{var}{external} }, |
166 |
{ label => N("Permit root Login"), val_ref => \$o->{var}{rootlogin} }, |
167 |
{ label => N("Chroot Home user"), val_ref => \$o->{var}{defaultroot} }, |
168 |
{ label => N("Allow FTP resume"), val_ref => \$o->{var}{ftpresume} }, |
169 |
{ label => N("Allow FXP"), val_ref => \$o->{var}{fxp} }, |
170 |
], |
171 |
post => \&do_it, |
172 |
next => 'end' |
173 |
}, |
174 |
end => { |
175 |
name => N("Congratulations") . "\n\n" . N("The wizard successfully configured your intranet/Internet FTP server"), |
176 |
end => 1, |
177 |
no_back => 1, |
178 |
}, |
179 |
error_end => { |
180 |
name => N("Failed"), |
181 |
data => [ { label => N("Please relaunch drakwizard, and try to change some parameters.") } ], |
182 |
no_back => 1, |
183 |
end => 1, |
184 |
}, |
185 |
}; |
186 |
|
187 |
sub new { |
188 |
my ($class) = @_; |
189 |
bless $o, $class; |
190 |
} |
191 |
|
192 |
sub true { |
193 |
my ($val) = @_; |
194 |
return member($val, qw(1 '1' "1" true 'true' "true")); |
195 |
} |
196 |
|
197 |
sub get_dir { |
198 |
die "no FTP configuration file found! warning." if !-f $file; |
199 |
foreach (cat_or_die($file)) { # we need 3 elements to consider section as known |
200 |
if (m/^\s*<drakwizard>/s...m!^\s*</drakwizard>!s) { |
201 |
if (m/^\s*<Anonymous\s*(.*)>/s) { |
202 |
return $1; |
203 |
} |
204 |
} |
205 |
} |
206 |
""; |
207 |
} |
208 |
|
209 |
sub check { |
210 |
$> and return 'must_be_root'; |
211 |
$wiz->{net}->is_dhcp and return 'warning_dhcp'; |
212 |
''; |
213 |
} |
214 |
|
215 |
sub print_anonymous { |
216 |
my ($arg) = @_; |
217 |
print ' |
218 |
#<drakwizard> |
219 |
<Anonymous ' . $arg . '> |
220 |
User ftp |
221 |
Group ftp |
222 |
UserAlias anonymous ftp |
223 |
MaxClients 10 |
224 |
<Limit WRITE> |
225 |
DenyAll |
226 |
</Limit> |
227 |
</Anonymous> |
228 |
#</drakwizard> |
229 |
'; |
230 |
} |
231 |
|
232 |
sub change_options { |
233 |
my ($var, $var_in_conf) = @_; |
234 |
my $status; |
235 |
if ($var == 1) { $status = "on" } else { $status = "off" } |
236 |
if (any { /^$var_in_conf/ } cat_($file)) { |
237 |
substInFile { s/$var_in_conf.*/$var_in_conf $status/ } $file; |
238 |
} else { |
239 |
append_to_file($file, "$var_in_conf $status\n"); |
240 |
} |
241 |
} |
242 |
|
243 |
sub add_options { |
244 |
my ($var, $value) = @_; |
245 |
if (any { /^$var/ } cat_($file)) { |
246 |
substInFile { s/$var.*/$var $value/ } $file; |
247 |
} else { |
248 |
append_to_file($file, "$var $value\n"); |
249 |
} |
250 |
} |
251 |
|
252 |
|
253 |
sub do_it { |
254 |
$::testing and return; |
255 |
my $wiz_ftp_internal = $o->{var}{wiz_ftp_external} ? 1 : true $o->{var}{wiz_ftp_internal}; |
256 |
my $wiz_ftp_external = true $o->{var}{wiz_ftp_external}; |
257 |
die "no FTP configuration file found! warning." if !-f $file; |
258 |
MDK::Common::cp_af($file, $file . ".orig"); |
259 |
my $allow; |
260 |
if ($wiz_ftp_internal && !$wiz_ftp_external) { |
261 |
($allow) = $wiz->{net}->itf_get("IPADDR") =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)\d{1,3}$/; |
262 |
$allow .= " 127.0.0.1"; |
263 |
} elsif ($wiz_ftp_external) { |
264 |
$allow = "all"; |
265 |
} |
266 |
|
267 |
my $conf = cat_($file) . "#EndOfFile"; |
268 |
my ($bloc) = $conf =~ /.*?(#drakwizard_proftpd.*?#drakwizard_proftpd)\n.*#EndOfFile/s; |
269 |
|
270 |
if (!$bloc) { |
271 |
$bloc = " |
272 |
#drakwizard_proftpd |
273 |
<Global> |
274 |
<Limit LOGIN> |
275 |
Order allow,deny |
276 |
Allow from $allow |
277 |
Deny from all |
278 |
</Limit> |
279 |
</Global> |
280 |
#drakwizard_proftpd |
281 |
|
282 |
"; |
283 |
append_to_file($file, $bloc); |
284 |
} else { |
285 |
substInFile { s/Allow from.*/Allow from $allow/ } $file; |
286 |
} |
287 |
|
288 |
# options |
289 |
# wiz_root_login wiz_server_admin wiz_default_root wiz_ftp_resume wiz_ftp_fxp |
290 |
if ($o->{var}{wiz_server_admin}) { |
291 |
if (any { /^ServerAdmin/ } cat_($file)) { |
292 |
substInFile { s/ServerAdmin.*/ServerAdmin $o->{var}{wiz_server_admin}/ } $file; |
293 |
} else { |
294 |
append_to_file($file, "ServerAdmin $o->{var}{wiz_server_admin}\n"); |
295 |
} |
296 |
} else { substInFile { s/ServerAdmin.*// } $file } |
297 |
|
298 |
change_options($o->{var}{wiz_root_login}, "RootLogin"); |
299 |
if ($o->{var}{wiz_root_login} == 1) { substInFile { s/root// } "/etc/ftpusers" } |
300 |
change_options($o->{var}{wiz_ftp_fxp}, "AllowForeignAddress"); |
301 |
# in ftp resume there is two options (store or retrieve) |
302 |
change_options($o->{var}{wiz_ftp_resume}, "AllowStoreRestart"); |
303 |
change_options($o->{var}{wiz_ftp_resume}, "AllowRetrieveRestart"); |
304 |
|
305 |
substInFile { s/^ServerName.*/ServerName $o->{var}{wiz_server_name}/ } $file; |
306 |
substInFile { s/^Port.*/Port $o->{var}{wiz_port}/ } $file; |
307 |
|
308 |
my $data = $o->{var}{wiz_default_root} == 1 ? "DefaultRoot ~" : ''; |
309 |
if (any { /^DefaultRoot/ } cat_($file)) { |
310 |
substInFile { s/DefaultRoot.*/$data/ } $file; |
311 |
} else { |
312 |
append_to_file($file, "$data\n"); |
313 |
} |
314 |
|
315 |
my $cle, my $val; |
316 |
while (($cle, $val) = each %opt) { |
317 |
add_options($cle, $val); |
318 |
} |
319 |
|
320 |
reload_or_restart('proftpd'); |
321 |
check_started('proftpd'); |
322 |
} |
323 |
|
324 |
1; |