File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/ftpquotacheck
#!/usr/bin/perl
# cpanel - ftpquotacheck Copyright(c) 2010 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
use strict;
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use warnings;
use Cpanel::SafeFind ();
use Cpanel::PwCache ();
use Cpanel::SafeFile ();
use Cpanel::AccessIds ();
use Cpanel::SafetyBits ();
my $purequotacheck =
-x '/usr/sbin/pure-quotacheck' ? '/usr/sbin/pure-quotacheck'
: -x '/usr/local/sbin/pure-quotacheck' ? '/usr/local/sbin/pure-quotacheck'
: '';
exit if !$purequotacheck;
print "Ftp Quota Check v1.7\n";
my $force = ( @ARGV && grep( /force/, @ARGV ) ) ? 1 : 0;
my $now = time;
my $ftp_gid = scalar( getgrnam 'ftp' ) || 65535;
Cpanel::PwCache::no_uid_cache(); #uid cache only needed if we are going to make lots of getpwuid calls
Cpanel::PwCache::init_passwdless_pwcache();
my $pwcache_ref = Cpanel::PwCache::fetch_pwcache();
foreach my $pwref (@$pwcache_ref) {
my ( $username, $uid, $gid, $homedir ) = (@$pwref)[ 0, 2, 3, 7 ];
if ( -e $homedir . '/etc/ftpquota' && -e '/etc/proftpd/' . $username ) {
if ( open my $ftp_fh, '<', '/etc/proftpd/' . $username ) {
print "Updating $username: ";
while ( my $line = readline $ftp_fh ) {
chomp $line;
my ( $user, $ftphome ) = ( split( /:/, $line ) )[ 0, 5 ];
next if $user eq $username . '_logs';
if ( -d $ftphome ) {
my $mode = ( stat(_) )[2];
next if ( !$force && -e $ftphome . '/.ftpquota' && ( stat(_) )[9] + ( 86400 * 30 ) > $now );
print "$user ... ";
if ( $user eq 'ftp' ) {
Cpanel::SafetyBits::safe_chmod( oct(770), $uid, $ftphome );
if ( ( stat( $ftphome . '/.ftpquota' ) )[4] && ( stat(_) )[4] != 65535 ) {
unlink( $ftphome . '/.ftpquota' );
}
Cpanel::SafetyBits::safe_userchgid( $uid, $ftp_gid, $ftphome );
if ( my $pid = fork() ) {
waitpid $pid, 0;
}
else {
Cpanel::AccessIds::setuids( 65535, $ftp_gid );
my $files = 0;
my $bytes = 0;
Cpanel::SafeFind::find(
{
'wanted' => sub {
return if $File::Find::name =~ m/\/\.+$/;
my ( $tuid, $tgid, $tbytes ) = ( stat($File::Find::name) )[ 4, 5, 7 ];
return if ( $tuid != 65535 || $tgid != $ftp_gid );
$files += 1;
$bytes += $tbytes;
},
'no_chdir' => 1
},
$ftphome
);
if ( open my $ftp_quota_fh, '>', $ftphome . '/.ftpquota' ) {
chmod 0600, $ftphome . '/.ftpquota';
print {$ftp_quota_fh} "$files $bytes\n";
close $ftp_quota_fh;
}
else {
warn "Unable to write $ftphome/.ftpquota: $!";
}
exit;
}
Cpanel::SafetyBits::safe_userchgid( $uid, $gid, $ftphome );
Cpanel::SafetyBits::safe_chmod( $mode, $uid, $ftphome );
}
else {
system $purequotacheck, '-u', $username, '-d', $ftphome;
}
}
}
close $ftp_fh;
print "...Done\n";
}
else {
warn "Failed to read /etc/proftpd/$username: $!";
}
}
}