File: //proc/self/root/scripts.20110531.215904.25158/convertemails
#!/usr/bin/perl
# cpanel - convertemails 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
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use strict;
use Cpanel::SafeFile ();
use Cpanel::AccessIds::SetUids ();
use Cpanel::Config::Users ();
use Cpanel::PwCache ();
use Cpanel::Logger ();
use Cpanel::Config::LoadCpConf ();
use Cpanel::Config::LoadCpUserFile ();
use Cpanel::FileUtils::TouchFile ();
$| = 1;
my $cpconf = Cpanel::Config::LoadCpConf::loadcpconf();
# not needed for maildir
if ( $cpconf->{'maildir'} ) {
exit;
}
else {
print "!!!! Warning: deprecated mbox mail format still in use. Please convert to maildir soon. !!!!\n";
sleep 2;
}
my $logger = Cpanel::Logger->new();
my $forcecheck = 0;
if ( $ARGV[0] =~ /force/i ) {
$forcecheck = 1;
shift @ARGV;
}
my $quiet = 0;
if ( $ARGV[0] =~ /quiet/i ) {
$quiet = 1;
shift @ARGV;
}
my @USERSlist;
if ( $ARGV[0] ) {
my $cuser = $ARGV[0];
if ( Cpanel::PwCache::getpwnam($cuser) && -e '/var/cpanel/users/' . $cuser ) {
push @USERSlist, $cuser;
}
}
else {
Cpanel::PwCache::init_passwdless_pwcache();
@USERSlist = Cpanel::Config::Users::getcpusers();
}
if ( !@USERSlist ) {
print "No users to process!\n";
exit;
}
my $hasmailnull=0;
if ( Cpanel::PwCache::getpwnam('mailnull') ) {
$hasmailnull = 1;
}
print "cPanel Email Convert v11\n" if !$quiet;
foreach my $cpuser (@USERSlist) {
my @PW = Cpanel::PwCache::getpwnam($cpuser);
my ( $user, $uid, $gid, $homedir ) = @PW[ 0, 2, 3, 7 ];
$homedir =~ /(.*)/; # Untaint
$homedir = $1;
next
if ( $uid < 500
|| length($homedir) < 5
|| ( -e $homedir . '/etc/.imapv4cp5c' && !$forcecheck )
|| -l $homedir . '/etc/passwd' );
if ( my $pid = fork() ) {
waitpid( $pid, 0 );
}
else {
my $cpuser_ref = Cpanel::Config::LoadCpUserFile::loadcpuserfile($user); # This needs to be ran before setuids
if ( !defined $cpuser_ref || !$cpuser_ref->{'DOMAIN'} ) {
die "Invalid user $user. No domains associated with user.\n";
}
Cpanel::AccessIds::SetUids::setuids( $uid, $gid );
if ( open my $imap_fh, '>', $homedir . '/etc/.imapv4cp5c' ) {
close $imap_fh;
}
my %MP;
foreach my $mdomain ( $cpuser_ref->{'DOMAIN'}, @{ $cpuser_ref->{'DOMAINS'} } ) {
next if ( !$mdomain || $MP{$mdomain} );
$MP{$mdomain} = 1;
if ( -e $homedir . '/etc/' . $mdomain . '/passwd' ) {
print "Converting $mdomain..." if !$quiet;
if ( !-e "$homedir/etc/$mdomain/passwd" ) {
$logger->warn("Could not find $homedir/etc/$mdomain/passwd");
Cpanel::FileUtils::TouchFile::touchfile("$homedir/etc/$mdomain/passwd");
}
my $pwlock = Cpanel::SafeFile::safeopen( \*PW, '+<', $homedir . '/etc/' . $mdomain . '/passwd' );
if ( !$pwlock ) {
$logger->warn("Could not edit $homedir/etc/$mdomain/passwd");
next;
}
my @PW;
while (<PW>) {
chomp;
if ( !/\d+\,\d+/ ) {
push @PW, $_;
}
}
seek( PW, 0, 0 );
foreach my $line (@PW) {
my @PWLINE = split( /:/, $line );
$PWLINE[5] = $homedir . '/mail/' . $mdomain . '/' . $PWLINE[0];
print PW join( ':', @PWLINE ) . "\n";
}
truncate( PW, tell(PW) );
Cpanel::SafeFile::safeclose( \*PW, $pwlock );
chmod( ( $hasmailnull ? 0644 : 0600 ), "$homedir/etc/${mdomain}/passwd" );
print "...Done\n";
}
}
exit 0;
}
}