summaryrefslogtreecommitdiffstats
path: root/ftp_wizard/Proftpd.pm
blob: 5a93d069341a1141ae1a14740bf293f55c2169c1 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2003 Mandrakesoft
#
# Author: Florent Villard <warly@mandrakesoft.com>
#         aginies <aginies _ateuh_ 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::Proftpd;
use strict;

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

my $wiz = MDK::Wizard::Wizcommon->new;
my $in = interactive->vnew;

my $file = "/etc/proftpd.conf";

my ($servername) = cat_($file) =~ /ServerName\s+(\S*.*)/;
my ($ftp_port) = cat_($file) =~ /Port\s+(\d+)/;

my $o = {
	 name => N("FTP wizard"),
	 var => {
		 wiz_ftp_external => '0',
		 wiz_ftp_internal => '1',
		 wiz_root_login => '',
		 wiz_server_admin => '',
		 wiz_default_root => '',
		 wiz_ftp_resume => '',
		 wiz_ftp_fxp => '',
		 wiz_server_name => '',
		 wiz_port => '',
		},
	 needed_rpm => [ 'proftpd' ],
	 defaultimage => "/usr/share/mcc/themes/default/ftp-mdk.png",
	};

my %opt = (
	   "SystemLog" => "/var/log/proftpd/proftpd.log",
	   "TransferLog" => "/var/log/proftpd/xferlog",
	   "Extendedlog" => "/var/log/proftpd/ftp.log",
	   "LogFormat default" => qq("%h %l %u %t "%r" %s %b"),
	   "LogFormat auth" => qq("%v [%P] %h %t "%r" %s"),
	   "LogFormat write" => qq("%h %l %u %t "%r" %s %b"),
	   "ServerIdent" => "off",
	   "DeferWelcome" => "on",
	   "DisplayConnect" => "/etc/banner-proftpd",
	   "AccessDenyMsg" => qq(" !-!! ACCESS DENY !!-! SEEMS YOU HAVE NO RIGHT THERE !!"),
	   "AccessGrantMsg" => qq(" -- Guest access granted for %u --"),
	   "IdentLookups" => "off",
	   "UseReverseDNS" => "off",
	   "TimesGMT" => "off",
	   "DirFakeUser" => "off nobody",
	   "DirFakeGroup" => "off nobody",
	   "DeleteAbortedStores" => "off",
	   "PersistentPasswd" => "off",
	 );

$o->{pages} = { 
	       welcome => {
			   name => N("FTP server configuration wizard") . "\n\n" . N("This wizard will help you configuring an FTP server for your network."),
			   no_back => 1,
			   post => \&check,
			   next => 'config'
			  },
	       config => {
			  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:"),
			  data => [
				   { label => N("Enable the FTP server for the intranet"), type => 'bool', val => \$o->{var}{wiz_ftp_internal} },
				   { label => N("Enable the FTP server for the Internet"), type => 'bool', val => \$o->{var}{wiz_ftp_external} },
				  ],
			  complete => sub {
			    if ($o->{var}{wiz_ftp_external} == 0 && $o->{var}{wiz_ftp_internal} == 0) {
			      $in->ask_warn(N("Error"), N("Please choose whether to allow a connection to FTP server from internal or external hosts."));
			      return 1;
			    } else { return 0 }
			  },
			  next => 'options'
	      },
	       options => {
			   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."),
			   pre => sub {
			     $o->{var}{wiz_root_login} ||= 0;
			     $o->{var}{wiz_server_name} = $servername;
			   },
			   data => [
				    { label => N('Server name:'), val => \$o->{var}{wiz_server_name} },
				    { label => N("Admin email:"), val => \$o->{var}{wiz_server_admin}, help => 'admin@guibland.com' },
				    { label => N("Permit root login:"), type => 'bool', val => \$o->{var}{wiz_root_login} },
				   ],
			   complete => sub {
			     if (!any { /bash/ } cat_("/etc/shells")) {
			       $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."));
			       return 1;
			     }
			     if (!$o->{var}{wiz_server_name}) { $in->ask_warn(N("Error"), N("Need a server name")); return 1; }
			   },
			   next => 'options_step2',
			  },
	       options_step2 => {
				 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."),
				 pre => sub {
				   $o->{var}{wiz_default_root} ||= 1;
				   $o->{var}{wiz_ftp_resume} ||= 1;
				   $o->{var}{wiz_ftp_fxp} ||= 0;
				   $o->{var}{wiz_port} = -e $ftp_port ? $ftp_port : "21";
				 },
				 data => [
					  { label => N('FTP Port:'), val => \$o->{var}{wiz_port}, help => 'Default port is 21 for an FTP server' },
					  { label => N("Chroot home user:"), type => 'bool', val => \$o->{var}{wiz_default_root} },
					  { label => N("Allow FTP resume:"), type => 'bool', val => \$o->{var}{wiz_ftp_resume} },
					  { label => N("Allow FXP:"), type => 'bool', val => \$o->{var}{wiz_ftp_fxp} },
					 ],
				 complete => sub { if ($o->{var}{wiz_port} !~ /^\d+$/) { $in->ask_warn(N('Error'), N('FTP Port should be a number.')); return 1; } },
				 next => 'summary',
				},
	       warning_dhcp => {
				name => N("Warning.") . "\n\n" . N("You are in DHCP, server may not work with your configuration."),
				ignore => 1,
				next => 'config'
			       },
	       must_be_root => {
				name => N("Error.") . "\n\n" . N("Sorry, you must be root to do this..."),
				ignore => 1,
	        next => 'config'
			       },
	       summary => {
			   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"),
			   pre => sub {
			     $o->{var}{internal} = $o->{var}{wiz_ftp_internal} ? N("enabled") : N("disabled");
			     $o->{var}{external} = $o->{var}{wiz_ftp_external} ? N("enabled") : N("disabled");
			     $o->{var}{rootlogin} = $o->{var}{wiz_root_login} ? N("enabled") : N("disabled");
			     $o->{var}{defaultroot} = $o->{var}{wiz_default_root} ? N("enabled") : N("disabled");
			     $o->{var}{ftpresume} = $o->{var}{wiz_ftp_resume} ? N("enabled") : N("disabled");
			     $o->{var}{fxp} = $o->{var}{wiz_ftp_fxp} ? N("enabled") : N("disabled");
			   },
			   data => [
				    { label => N('FTP Port:'), val_ref => \$o->{var}{wiz_port} },
				    { label => N("Intranet FTP server:"), val_ref => \$o->{var}{internal} },
				    { label => N("Internet FTP server:"), val_ref => \$o->{var}{external} },
				    { label => N("Permit root Login"), val_ref => \$o->{var}{rootlogin} },
				    { label => N("Chroot Home user"), val_ref => \$o->{var}{defaultroot} },
				    { label => N("Allow FTP resume"), val_ref => \$o->{var}{ftpresume} },
				    { label => N("Allow FXP"), val_ref => \$o->{var}{fxp} },
				   ],
			   post => \&do_it,
			   next => 'end'
			  },
	       end => { 
		       name => N("Congratulations") . "\n\n" . N("The wizard successfully configured your intranet/Internet FTP server"),
		       end => 1,
		       no_back => 1,
		      },
	       error_end => {
			     name => N("Failed"),
                             data => [ { label => N("Please relaunch drakwizard, and try to change some parameters.") } ],
                             no_back => 1,
                             end => 1,
                            },
};

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

sub true {
    my ($val) = @_;
    return member($val, qw(1 '1' "1" true 'true' "true"));
}

sub get_dir {
  die "no FTP configuration file found! warning." if !-f $file;
  local *NEW;
  open(NEW, "< $file") or die "error while opening $file: $!";
  local $_;
  while (<NEW>) { # we need 3 elements to consider section as known
    if (m/^\s*<drakwizard>/s...m!^\s*</drakwizard>!s) {
      if (m/^\s*<Anonymous\s*(.*)>/s) {
	return $1;
      }
    }
  }
  "";
}

sub check {
    $> and return 'must_be_root';
    $wiz->{net}->is_dhcp and return 'warning_dhcp';
    '';
}

sub print_anonymous {
    my ($arg) = @_;
    print '
#<drakwizard>
<Anonymous '.$arg.'>
  User	ftp
  Group	ftp
  UserAlias anonymous ftp
  MaxClients 10
  <Limit WRITE>
    DenyAll
  </Limit>
</Anonymous>
#</drakwizard>
';
}

sub change_options {
  my ($var, $var_in_conf) = @_;
  my $status;
  if ($var == 1) { $status = "on" } else { $status = "off" }
  if (any { /^$var_in_conf/ } cat_($file)) {
    substInFile { s/$var_in_conf.*/$var_in_conf $status/ } $file;
  } else {
    append_to_file($file, "$var_in_conf $status\n");
  }
}

sub add_options {
    my ($var, $value) = @_;
    if (any { /^$var/ } cat_($file)) {
    substInFile { s/$var.*/$var $value/ } $file;
  } else {
    append_to_file($file, "$var $value\n");
  }
}


sub do_it {
    $::testing and return;
    my $wiz_ftp_internal = $o->{var}{wiz_ftp_external} ? 1 : true $o->{var}{wiz_ftp_internal};
    my $wiz_ftp_external = true $o->{var}{wiz_ftp_external};
    die "no FTP configuration file found! warning." if !-f $file;
    MDK::Common::cp_af($file, $file . ".orig");
    my $allow;
    if ($wiz_ftp_internal && !$wiz_ftp_external) {
	($allow) = $wiz->{net}->itf_get("IPADDR") =~ qr/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)\d{1,3}$/;
	$allow .= " 127.0.0.1";
    }
    elsif ($wiz_ftp_external) {
	$allow = "all";
    }

    my $conf = cat_($file) . "#EndOfFile";
    my ($bloc) = $conf =~ /.*?(#drakwizard_proftpd.*?#drakwizard_proftpd)\n.*#EndOfFile/s;

    if (!$bloc) {
	$bloc = "
#drakwizard_proftpd
<Global>
  <Limit LOGIN>
    Order allow,deny
    Allow from $allow
    Deny from all
  </Limit>
</Global>
#drakwizard_proftpd

";
	append_to_file($file, $bloc);
      } else {
	substInFile { s/Allow from.*/Allow from $allow/ } $file;
      }

    # options
    # wiz_root_login wiz_server_admin wiz_default_root wiz_ftp_resume wiz_ftp_fxp
    if ($o->{var}{wiz_server_admin}) {
      if (any { /^ServerAdmin/ } cat_($file)) {
	substInFile { s/ServerAdmin.*/ServerAdmin $o->{var}{wiz_server_admin}/ } $file;
      } else {
	append_to_file($file, "ServerAdmin $o->{var}{wiz_server_admin}\n");
      }
    } else { substInFile { s/ServerAdmin.*// } $file }

    change_options($o->{var}{wiz_root_login}, "RootLogin");
    if ($o->{var}{wiz_root_login} == 1) { substInFile { s/root// } "/etc/ftpusers" }
    change_options($o->{var}{wiz_ftp_fxp}, "AllowForeignAddress");
    # in ftp resume there is two options (store or retrieve)
    change_options($o->{var}{wiz_ftp_resume}, "AllowStoreRestart");
    change_options($o->{var}{wiz_ftp_resume}, "AllowRetrieveRestart");

    substInFile { s/^ServerName.*/ServerName $o->{var}{wiz_server_name}/ } $file;
    substInFile { s/^Port.*/Port $o->{var}{wiz_port}/ } $file;

    my $data;
    if ($o->{var}{wiz_default_root} == 1) {
      $data = "DefaultRoot ~";
    } else {
      $data = "";
    }
    if (any { /^DefaultRoot/ } cat_($file)) {
      substInFile { s/DefaultRoot.*/$data/ } $file;
    } else {
      append_to_file($file, "$data\n");
    }

    my $cle, my $val;
    while (($cle, $val) = each %opt) {
      add_options($cle, $val);
    }

    if (services::is_service_running('proftpd')) {
        services::restart('proftpd');
    } else {
        services::start('proftpd');
    }
    check_started('proftpd');
}

1;