/
installer.pm
717 lines (545 loc) · 21.9 KB
/
installer.pm
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
=head1 NAME
Servers::ftpd::vsftpd::installer - i-MSCP VsFTPd Server implementation
=cut
# i-MSCP - internet Multi Server Control Panel
# Copyright (C) 2015-2016 by Laurent Declercq <l.declercq@nuxwin.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
# of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package Servers::ftpd::vsftpd::installer;
use strict;
use warnings;
use Cwd;
use iMSCP::Crypt 'randomStr';
use iMSCP::Debug;
use iMSCP::Execute;
use iMSCP::File;
use iMSCP::LsbRelease;
use iMSCP::Stepper;
use iMSCP::TemplateParser;
use File::Basename;
use Servers::ftpd::vsftpd;
use Servers::sqld;
use version;
use parent 'Common::SingletonClass';
%main::sqlUsers = () unless %main::sqlUsers;
@main::createdSqlUsers = () unless @main::createdSqlUsers;
=head1 DESCRIPTION
Installer for the i-MSCP VsFTPd Server implementation.
=head1 PUBLIC METHODS
=over 4
=item registerSetupListeners(\%eventManager)
Register setup event listeners
Param iMSCP::EventManager \%eventManager
Return int 0 on success, other on failure
=cut
sub registerSetupListeners
{
my ($self, $eventManager) = @_;
$eventManager->register( 'beforeSetupDialog', sub {
push @{$_[0]}, sub { $self->sqlUserDialog( @_ ) }, sub { $self->passivePortRangeDialog( @_ ) };
0;
} );
}
=item sqlUserDialog(\%dialog)
Show dialog
Param iMSCP::Dialog \%dialog
Return int 0 on success, other on failure
=cut
sub sqlUserDialog
{
my ($self, $dialog) = @_;
my $dbUser = main::setupGetQuestion( 'FTPD_SQL_USER' ) || $self->{'config'}->{'DATABASE_USER'} || 'vftp_user';
my $dbPass = main::setupGetQuestion( 'FTPD_SQL_PASSWORD' ) || $self->{'config'}->{'DATABASE_PASSWORD'};
my ($rs, $msg) = (0, '');
if (grep($_ eq $main::reconfigure, ( 'ftpd', 'servers', 'all', 'forced' ))
|| length $dbUser < 6 || length $dbUser > 16 || $dbUser !~ /^[\x21-\x22\x24-\x5b\x5d-\x7e]+$/
|| length $dbPass < 6 || $dbPass !~ /^[\x21-\x22\x24-\x5b\x5d-\x7e]+$/
) {
do {
($rs, $dbUser) = $dialog->inputbox( "\nPlease enter an username for the VsFTPd SQL user:$msg", $dbUser );
if ($dbUser eq $main::imscpConfig{'DATABASE_USER'}) {
$msg = "\n\n\\Z1You cannot reuse the i-MSCP SQL user '$dbUser'.\\Zn\n\nPlease try again:";
$dbUser = '';
} elsif (length $dbUser > 16) {
$msg = "\n\n\\Username can be up to 16 characters long.\\Zn\n\nPlease try again:";
$dbUser = '';
} elsif (length $dbUser < 6) {
$msg = "\n\n\\Z1Username must be at least 6 characters long.\\Zn\n\nPlease try again:";
$dbUser = '';
} elsif ($dbUser !~ /^[\x21-\x22\x24-\x5b\x5d-\x7e]+$/) {
$msg = "\n\n\\Z1Only printable ASCII characters (excepted space and number sign and backslash) are allowed.\\Zn\n\nPlease try again:";
$dbUser = '';
}
} while ($rs < 30 && !$dbUser);
if ($rs < 30) {
$msg = '';
# Ask for the VsFTPd SQL user password unless we reuses existent SQL user
unless (grep($_ eq $dbUser, ( keys %main::sqlUsers ))) {
do {
($rs, $dbPass) = $dialog->passwordbox(
"\nPlease, enter a password for the VsFTPd SQL user (blank for autogenerate):$msg", $dbPass
);
if ($dbPass ne '') {
if (length $dbPass < 6) {
$msg = "\n\n\\Z1Password must be at least 6 characters long.\\Zn\n\nPlease try again:";
$dbPass = '';
} elsif ($dbPass !~ /^[\x21-\x22\x24-\x5b\x5d-\x7e]+$/) {
$msg = "\n\n\\Z1Only printable ASCII characters (excepted space and number sign and backslash) are allowed.\\Zn\n\nPlease try again:";
$dbPass = '';
} else {
$msg = '';
}
} else {
$msg = '';
}
} while ($rs < 30 && $msg);
} else {
$dbPass = $main::sqlUsers{$dbUser};
}
if ($rs < 30) {
$dbPass = randomStr( 16 ) unless $dbPass;
$dialog->msgbox( "\nPassword for the VsFTPd SQL user set to: $dbPass" );
}
}
}
if ($rs < 30) {
main::setupSetQuestion( 'FTPD_SQL_USER', $dbUser );
main::setupSetQuestion( 'FTPD_SQL_PASSWORD', $dbPass );
$main::sqlUsers{$dbUser} = $dbPass;
}
$rs;
}
=item passivePortRangeDialog(\%dialog)
Ask for VsFTPd port range to use for passive data transfers
Param iMSCP::Dialog \%dialog
Return int 0 on success, other on failure
=cut
sub passivePortRangeDialog
{
my ($self, $dialog) = @_;
my ($rs, $msg) = (0, '');
my $passivePortRange = main::setupGetQuestion( 'FTPD_PASSIVE_PORT_RANGE' ) || $self->{'config'}->{'FTPD_PASSIVE_PORT_RANGE'};
if (grep($_ eq $main::reconfigure, ( 'ftpd', 'servers', 'all', 'forced' ))
|| $passivePortRange !~ /^(\d+)\s+(\d+)$/
|| $1 < 32768 || $1 >= 60999 || $1 >= $2
) {
$passivePortRange = '32768 60999' unless $1 && $2;
do {
($rs, $passivePortRange) = $dialog->inputbox( <<EOF
\\Z4\\Zb\\ZuVsFTPd passive port range\\Zn
Please, choose the passive port range for VsFTPd.
Be aware that if you're behind a NAT, you must forward those ports to this server.$msg
EOF
,
$passivePortRange
);
if ($passivePortRange !~ /^(\d+)\s+(\d+)$/ || $1 < 32768 || $1 >= 60999 || $1 >= $2) {
$passivePortRange = '32768 60999';
$msg = "\n\n\\Z1Invalid port range.\\Zn\n\nPlease try again:"
} else {
$passivePortRange = "$1 $2";
$msg = '';
}
} while ($rs < 30 && $msg);
}
$self->{'config'}->{'FTPD_PASSIVE_PORT_RANGE'} = $passivePortRange if $rs < 30;
$rs;
}
=item install()
Process install tasks
Return int 0 on success, other on failure
=cut
sub install
{
my $self = shift;
my %lsbInfo = iMSCP::LsbRelease->getInstance()->getDistroInformation();
if ($lsbInfo{'ID'} eq 'Ubuntu'
|| $lsbInfo{'ID'} eq 'Debian' && version->parse( $lsbInfo{'RELEASE'} ) < version->parse( '8.0' )
) {
my $rs = $self->_rebuildVsFTPdDebianPackage();
return $rs if $rs;
}
undef %lsbInfo;
my $rs = $self->_setVersion();
$rs ||= $self->_setupDatabase();
$rs ||= $self->_buildConfigFile();
$rs ||= $self->_saveConf();
}
=back
=head1 PRIVATE METHODS
=over 4
=item _init()
Initialize instance
Return Servers::ftpd::vsftpd::installer
=cut
sub _init
{
my $self = shift;
$self->{'ftpd'} = Servers::ftpd::vsftpd->getInstance();
$self->{'eventManager'} = $self->{'ftpd'}->{'eventManager'};
$self->{'cfgDir'} = $self->{'ftpd'}->{'cfgDir'};
$self->{'bkpDir'} = "$self->{'cfgDir'}/backup";
$self->{'config'} = $self->{'ftpd'}->{'config'};
my $oldConf = "$self->{'cfgDir'}/vsftpd.old.data";
return $self unless -f $oldConf;
tie my %oldConfig, 'iMSCP::Config', fileName => $oldConf;
for my $param(keys %oldConfig) {
next unless exists $self->{'config'}->{$param};
$self->{'config'}->{$param} = $oldConfig{$param};
}
$self;
}
=item _rebuildVsFTPdDebianPackage()
Rebuild VsFTPd debian package
Return int 0 on success, other on failure
=cut
sub _rebuildVsFTPdDebianPackage
{
my $self = shift;
startDetail();
my $oldDir = getcwd();
my $rs = step(
sub {
my $buildir = iMSCP::Dir->new( dirname => '/usr/local/src/vsftpd' );
my $rs = $buildir->remove(); # Cleanup previous build directory if any
$rs ||= $buildir->make();
return $rs if $rs;
unless (chdir '/usr/local/src/vsftpd') {
error( sprintf( 'Could not change directory: %s', $! ) );
return 1;
}
0;
}, 'Creating build directory for local vsftpd package...', 7, 1
);
$rs ||= step(
sub {
# Ignore exit code due to https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1258958 bug
execute( 'LANG=C apt-mark unhold vsftpd', \my $stdout, \my $stderr );
debug( $stdout ) if $stdout;
debug( $stderr ) if $stderr;
my $rs = execute( 'apt-get -y source vsftpd', \$stdout, \$stderr );
error( sprintf( 'Could not get vsftpd source package: %s', $stderr || 'Unknown error' ) ) if $rs;
return $rs if $rs;
debug( $stdout ) if $stdout;
0;
}, 'Downloading vsftpd source package...', 7, 2
);
$rs ||= step(
sub {
my $rs = execute( 'apt-get -y build-dep vsftpd', \my $stdout, \my $stderr );
error( sprintf( 'Could not install vsftpd package build dependencies: %s',
$stderr || 'Unknown error' ) ) if $rs;
return $rs if $rs;
debug( $stdout ) if $stdout;
0;
}, 'Installing vsftpd build dependencies...', 7, 3
);
$rs ||= step(
sub {
unless (chdir glob 'vsftpd-*') {
error( sprintf( 'Could not change directory: %s', $! ) );
return 1;
}
my $file = iMSCP::File->new( filename => 'debian/patches/series' );
my $fileContent = $file->get();
# Apply the imscp_allow_writeable_root.patch patch for vsftpd version < 3.0.0 only
my $rs = execute( 'dpkg-query --show --showformat \'${Version}\' vsftpd', \my $stdout, \my $stderr );
debug( $stdout ) if $stdout;
error( $stderr ) if $rs && $stderr;
return $rs if $rs;
my $ret = execute( "dpkg --compare-versions $stdout lt 3", \$stdout, \$stderr );
if ($stderr) {
error( sprintf( 'Could not compare vsftpd package version: %s', $stderr ) );
return 1;
}
unless ($ret) {
$rs = iMSCP::File->new( filename => "$self->{'cfgDir'}/imscp_allow_writeable_root.patch" )->copyFile(
'debian/patches/imscp_allow_writeable_root'
);
return $rs if $rs;
$fileContent .= "imscp_allow_writeable_root\n"
}
# apply the imscp_pthread_cancel.patch if available
if (-f "$self->{'cfgDir'}/imscp_pthread_cancel.patch") {
$rs = iMSCP::File->new( filename => "$self->{'cfgDir'}/imscp_pthread_cancel.patch" )->copyFile(
'debian/patches/imscp_pthread_cancel'
);
return $rs if $rs;
$fileContent .= "imscp_pthread_cancel\n";
}
$rs = $file->set( $fileContent );
$rs ||= $file->save();
}, 'Patching vsftpd source package...', 7, 4
);
$rs ||= step(
sub {
my $rs = execute( "dch --local imscp 'i-MSCP patched version.'", \my $stdout, \my $stderr );
error( sprintf( "Could not add 'imscp' local suffix to vsftpd package: %s",
$stderr || 'Unknown error' ) ) if $rs;
return $rs if $rs;
$rs = execute( 'dpkg-buildpackage -b -uc', \$stdout, \$stderr );
error( sprintf( 'Could not build local vsftpd package: %s', $stderr || 'Unknown error' ) ) if $rs;
return $rs if $rs;
debug( $stdout ) if $stdout;
0;
}, 'Building local vsftpd package...', 7, 5
);
$rs ||= step(
sub {
unless (chdir '..') {
error( sprintf( 'Could not change directory: %s', $! ) );
return 1;
}
my $rs = execute( 'dpkg --force-confnew -i vsftpd_*.deb', \my $stdout, \my $stderr );
error( sprintf( 'Could not install local vsftpd package: %s', $stderr || 'Unknown error' ) ) if $rs;
debug( $stdout ) if $stdout;
return $rs if $rs;
# Ignore exit code due to https://bugs.launchpad.net/ubuntu/+source/apt/+bug/1258958 bug
execute( 'LANG=C apt-mark hold vsftpd', \$stdout, \$stderr );
debug( $stdout ) if $stdout;
debug( $stderr ) if $stderr;
0;
}, 'Installing local vsftpd package...', 7, 6
);
$rs ||= step(
sub {
unless (chdir $oldDir) {
error( sprintf( 'Could not change directory: %s', $! ) );
return 1;
}
iMSCP::Dir->new( dirname => '/usr/local/src/vsftpd' )->remove();
}, 'Removing local vsftpd package build directory', 7, 7
);
endDetail();
$rs;
}
=item _setVersion
Set version
Return int 0 on success, other on failure
=cut
sub _setVersion
{
my $self = shift;
# Version is print through STDIN (see: strace vsftpd -v)
my $rs = execute( 'vsftpd -v 0>&1', \my $stdout, \my $stderr );
debug( $stdout ) if $stdout;
error( $stderr ) if $stderr && $rs;
return $rs if $rs;
if ($stdout !~ m%([\d.]+)%) {
error( 'Could not find VsFTPd version from `vsftpd -v 0>&1` command output.' );
return 1;
}
$self->{'config'}->{'VSFTPD_VERSION'} = $1;
debug( sprintf( 'VsFTPd version set to: %s', $1 ) );
0;
}
=item _setupDatabase()
Setup database
Return int 0 on success, other on failure
=cut
sub _setupDatabase
{
my $self = shift;
my $sqlServer = Servers::sqld->factory();
my $dbName = main::setupGetQuestion( 'DATABASE_NAME' );
my $dbUser = main::setupGetQuestion( 'FTPD_SQL_USER' );
my $dbUserHost = main::setupGetQuestion( 'DATABASE_USER_HOST' );
my $dbPass = main::setupGetQuestion( 'FTPD_SQL_PASSWORD' );
my $dbOldUser = $self->{'config'}->{'DATABASE_USER'};
$self->{'eventManager'}->trigger( 'beforeFtpdSetupDb', $dbUser, $dbPass );
for my $sqlUser ($dbOldUser, $dbUser) {
next if !$sqlUser || grep($_ eq "$sqlUser\@$dbUserHost", @main::createdSqlUsers);
for my $host($dbUserHost, $main::imscpOldConfig{'DATABASE_USER_HOST'}) {
next unless $host;
$sqlServer->dropUser( $sqlUser, $host );
}
}
# Create SQL user if not already created by another server/package installer
unless (grep($_ eq "$dbUser\@$dbUserHost", @main::createdSqlUsers)) {
debug( sprintf( 'Creating %s@%s SQL user', $dbUser, $dbUserHost ) );
$sqlServer->createUser( $dbUser, $dbUserHost, $dbPass );
push @main::createdSqlUsers, "$dbUser\@$dbUserHost";
}
my ($db, $errStr) = main::setupGetSqlConnect();
unless ($db) {
error( sprintf( 'Could not connect to SQL server: %s', $errStr ) ),
return 1;
}
# Give needed privileges to this SQL user
my $quotedDbName = $db->quoteIdentifier( $dbName );
my $quotedTableName = $db->quoteIdentifier( 'ftp_users' );
my $rs = $db->doQuery( 'g', "GRANT SELECT ON $quotedDbName.$quotedTableName TO ?@?", $dbUser, $dbUserHost );
unless (ref $rs eq 'HASH') {
error( sprintf( 'Could not add SQL privileges: %s', $rs ) );
return 1;
}
$self->{'config'}->{'DATABASE_USER'} = $dbUser;
$self->{'config'}->{'DATABASE_PASSWORD'} = $dbPass;
$self->{'eventManager'}->trigger( 'afterFtpSetupDb', $dbUser, $dbPass );
}
=item _buildConfigFile()
Build configuration file
Return int 0 on success, other on failure
=cut
sub _buildConfigFile
{
my $self = shift;
# Make sure to start with clean user configuration directory
unlink glob "$self->{'config'}->{'FTPD_USER_CONF_DIR'}/*";
my ($passvMinPort, $passvMaxPort) = split( /\s+/, $self->{'config'}->{'FTPD_PASSIVE_PORT_RANGE'} );
my $data = {
DATABASE_NAME => $main::imscpConfig{'DATABASE_NAME'},
DATABASE_HOST => $main::imscpConfig{'DATABASE_HOST'},
DATABASE_PORT => $main::imscpConfig{'DATABASE_PORT'},
DATABASE_USER => $self->{'config'}->{'DATABASE_USER'},
DATABASE_PASS => $self->{'config'}->{'DATABASE_PASSWORD'},
FTPD_BANNER => $self->{'config'}->{'FTPD_BANNER'},
FRONTEND_USER_SYS_NAME => $main::imscpConfig{'SYSTEM_USER_PREFIX'}.$main::imscpConfig{'SYSTEM_USER_MIN_UID'},
PASSV_ENABLE => $self->{'config'}->{'PASSV_ENABLE'},
PASSV_MIN_PORT => $passvMinPort,
PASSV_MAX_PORT => $passvMaxPort,
FTP_MAX_CLIENTS => $self->{'config'}->{'FTP_MAX_CLIENTS'},
MAX_PER_IP => $self->{'config'}->{'MAX_PER_IP'},
LOCAL_MAX_RATE => $self->{'config'}->{'LOCAL_MAX_RATE'},
USER_WEB_DIR => $main::imscpConfig{'USER_WEB_DIR'},
FTPD_USER_CONF_DIR => $self->{'config'}->{'FTPD_USER_CONF_DIR'}
};
# vsftpd main configuration file
my $rs = $self->_bkpConfFile( $self->{'config'}->{'FTPD_CONF_FILE'} );
$rs ||= $self->{'eventManager'}->trigger( 'onLoadTemplate', 'vsftpd', 'vsftpd.conf', \my $cfgTpl, $data );
return $rs if $rs;
unless (defined $cfgTpl) {
$cfgTpl = iMSCP::File->new( filename => "$self->{'cfgDir'}/vsftpd.conf" )->get();
unless (defined $cfgTpl) {
error( sprintf( 'Could not read %s file', "$self->{'cfgDir'}/vsftpd.conf" ) );
return 1;
}
}
$rs = $self->{'eventManager'}->trigger( 'beforeFtpdBuildConf', \$cfgTpl, 'vsftpd.conf' );
return $rs if $rs;
if ($self->_isVsFTPdInsideCt()) {
$cfgTpl .= <<EOF;
# VsFTPd run inside unprivileged VE
# See http://youtrack.i-mscp.net/issue/IP-1503
seccomp_sandbox=NO
EOF
}
if ($main::imscpConfig{'BASE_SERVER_IP'} ne $main::imscpConfig{'BASE_SERVER_PUBLIC_IP'}) {
$cfgTpl .= <<EOF;
# VsFTPd behind NAT - Use public IP address
pasv_address=$main::imscpConfig{'BASE_SERVER_PUBLIC_IP'}
pasv_promiscuous=YES
EOF
}
if (main::setupGetQuestion( 'SERVICES_SSL_ENABLED' ) eq 'yes') {
$cfgTpl .= <<EOF;
# SSL support
ssl_enable=YES
force_local_data_ssl=NO
force_local_logins_ssl=NO
ssl_sslv2=NO
ssl_sslv3=NO
ssl_tlsv1=YES
require_ssl_reuse=NO
ssl_ciphers=HIGH
rsa_cert_file=$main::imscpConfig{'CONF_DIR'}/imscp_services.pem
rsa_private_key_file=$main::imscpConfig{'CONF_DIR'}/imscp_services.pem
EOF
}
$cfgTpl = iMSCP::TemplateParser::process( $data, $cfgTpl );
$rs = $self->{'eventManager'}->trigger( 'afterFtpdBuildConf', \$cfgTpl, 'vsftpd.conf' );
return $rs if $rs;
my $file = iMSCP::File->new( filename => $self->{'config'}->{'FTPD_CONF_FILE'} );
$rs = $file->set( $cfgTpl );
$rs ||= $file->save();
$rs ||= $file->owner( $main::imscpConfig{'ROOT_USER'}, $main::imscpConfig{'ROOT_GROUP'} );
$rs ||= $file->mode( 0640 );
return $rs if $rs;
# VsFTPd pam-mysql configuration file
undef $cfgTpl;
$rs = $self->_bkpConfFile( $self->{'config'}->{'FTPD_PAM_CONF_FILE'} );
$rs ||= $self->{'eventManager'}->trigger( 'onLoadTemplate', 'vsftpd', 'vsftpd.pam', \$cfgTpl, $data );
return $rs if $rs;
unless (defined $cfgTpl) {
$cfgTpl = iMSCP::File->new( filename => "$self->{'cfgDir'}/vsftpd.pam" )->get();
unless (defined $cfgTpl) {
error( sprintf( 'Could not read %s file', "$self->{'cfgDir'}/vsftpd.pam" ) );
return 1;
}
}
$rs = $self->{'eventManager'}->trigger( 'beforeFtpdBuildConf', \$cfgTpl, 'vsftpd.pam' );
return $rs if $rs;
$cfgTpl = iMSCP::TemplateParser::process( $data, $cfgTpl );
$rs = $self->{'eventManager'}->trigger( 'afterFtpdBuildConf', \$cfgTpl, 'vsftpd.pam' );
return $rs if $rs;
$file = iMSCP::File->new( filename => $self->{'config'}->{'FTPD_PAM_CONF_FILE'} );
$rs ||= $file->set( $cfgTpl );
$rs ||= $file->save();
$rs ||= $file->owner( $main::imscpConfig{'ROOT_USER'}, $main::imscpConfig{'ROOT_GROUP'} );
$rs ||= $file->mode( 0640 );
}
=item _saveConf()
Save configuration file
Return int 0 on success, other on failure
=cut
sub _saveConf
{
my $self = shift;
iMSCP::File->new( filename => "$self->{'cfgDir'}/vsftpd.data" )->copyFile( "$self->{'cfgDir'}/vsftpd.old.data" );
}
=item _bkpConfFile()
Backup file
Return int 0 on success, other on failure
=cut
sub _bkpConfFile
{
my ($self, $cfgFile) = @_;
my $rs = $self->{'eventManager'}->trigger( 'beforeFtpdBkpConfFile', $cfgFile );
return $rs if $rs;
if (-f $cfgFile) {
my $file = iMSCP::File->new( filename => $cfgFile );
my $basename = basename( $cfgFile );
unless (-f "$self->{'bkpDir'}/$basename.system") {
$rs = $file->copyFile( "$self->{'bkpDir'}/$basename.system" );
return $rs if $rs;
} else {
$rs = $file->copyFile( "$self->{'bkpDir'}/$basename.".time );
return $rs if $rs;
}
}
$self->{'eventManager'}->trigger( 'afterFtpdBkpConfFile', $cfgFile );
}
=item _isVsFTPdInsideCt()
Does the VsFTPd server is run inside an unprivileged VE (OpenVZ container)
Return bool TRUE if the VsFTPd server is run inside an OpenVZ container, FALSE otherwise
=cut
sub _isVsFTPdInsideCt
{
return 0 unless -f '/proc/user_beancounters';
my $rs = execute( 'cat /proc/1/status | grep --color=never envID', \my $stdout, \my $stderr );
debug( $stdout ) if $stdout;
debug( $stderr ) if $rs && $stderr;
return $rs if $rs;
if ($stdout =~ /envID:\s+(\d+)/) {
return ($1 > 0) ? 1 : 0;
}
0;
}
=back
=head1 AUTHOR
Laurent Declercq <l.declercq@nuxwin.com>
=cut
1;
__END__