File: //scripts.20110531.215904.25158/convertemails5
#!/usr/bin/perl
# cpanel - convertemails5 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::FileUtils::TouchFile ();
use Cpanel::AccessIds::SetUids ();
use Cpanel::Config::LoadCpUserFile ();
use Cpanel::PwCache ();
use Cpanel::Config::Users ();
use Cpanel::Logger ();
my $logger = Cpanel::Logger->new();
my $migrate = 0;
my $forcecheck = 0;
if ( grep( /\-+migrate/, @ARGV ) ) {
$migrate = 1;
@ARGV = grep( !/\-+migrate/, @ARGV );
}
if ( grep( /\-+forcecheck/, @ARGV ) ) {
$forcecheck = 1;
@ARGV = grep( !/\-+forcecheck/, @ARGV );
}
exit if !$migrate;
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;
}
open( STDERR, '>', '/dev/null' );
my $hasmailnull = 0;
if ( Cpanel::PwCache::getpwnam('mailnull') ) {
$hasmailnull = 1;
}
foreach my $user (@USERSlist) {
my @USER = Cpanel::PwCache::getpwnam($user);
my $homedir = $USER[7];
next if -e "$homedir/etc/.cpanel5mail2";
if ( my $pid = fork() ) {
waitpid( $pid, 0 );
#parent
}
else {
Cpanel::AccessIds::SetUids::setuids( $USER[2], $USER[3] );
if ( -l "$homedir/etc/passwd" ) {
$logger->die( "Warning, passwd file $homedir/etc/passwd is a symlink\n" );
}
my $cpuser_ref = Cpanel::Config::LoadCpUserFile::loadcpuserfile( $USER[0] );
my $dns = $cpuser_ref->{'DOMAIN'};
$dns =~ /(.*)/;
$dns = $1;
my @DOMAINS;
if ( ref $cpuser_ref->{'DOMAINS'} ) {
@DOMAINS = @{ $cpuser_ref->{'DOMAINS'} };
}
exit if -d "${homedir}/etc/$dns";
if ( !-e "$homedir/etc" ) { mkdir( "$homedir/etc", 0755 ); }
if ( !-e "$homedir/etc/passwd" || $forcecheck ) {
foreach my $fname ( 'passwd', 'shadow', 'quota' ) {
Cpanel::FileUtils::TouchFile::touchfile("$homedir/etc/$fname");
}
foreach my $fname ( 'passwd', 'quota' ) {
chmod( ( $hasmailnull ? 0644 : 0600 ), "$homedir/etc/$fname" );
}
chmod( 0600, "$homedir/etc/shadow" );
}
if ( !-e "$homedir/etc/$dns" ) {
mkdir( "$homedir/etc/$dns", ( $hasmailnull ? 0710 : 0700 ) );
exit if ( -e "$homedir/etc/$dns/passwd" );
foreach my $fname ( 'passwd', 'shadow', 'quota' ) {
link( "$homedir/etc/$fname", "$homedir/etc/$dns/$fname" )
|| $logger->warn( "Cannot link $homedir/etc/$dns/$fname to $homedir/etc/$fname: $!\n" );
}
}
if ( !-e "$homedir/mail/$dns" ) {
opendir( RDR, "$homedir/mail" );
my @USERS = readdir(RDR);
closedir(RDR);
mkdir( "$homedir/mail/$dns", ( $hasmailnull ? 0710 : 0700 ) );
foreach my $muser (@USERS) {
next if ( !-d "$homedir/mail/$muser" );
next if ( $muser =~ /^\./ );
next if ( $muser eq 'cur' || $muser eq 'new' || $muser eq 'tmp' );
if ( grep( /^\Q$muser\E/, @DOMAINS ) ) { #if the user is really a domain skip it
next;
}
$muser =~ /(.*)/;
$muser = $1;
print "Processing $muser\n";
mkdir( "$homedir/mail/$dns/$muser", ( $hasmailnull ? 0710 : 0700 ) );
opendir( RDS, "$homedir/mail/$muser" );
my @BOXS = readdir(RDS);
closedir(RDS);
foreach my $box (@BOXS) {
$box =~ /(.*)/;
$box = $1;
next if $box =~ /cur|new|tmp/i;
next if ( !-f "$homedir/mail/$muser/$box" );
mkdir("$homedir/mail/$dns/$muser/") if !-e "$homedir/mail/$dns/$muser/";
system( 'mv', "$homedir/mail/$muser/$box", "$homedir/mail/$dns/$muser/$box" );
rmdir("$homedir/mail/$muser/");
}
}
}
foreach my $mdomain (@DOMAINS) {
$mdomain =~ /(.*)/;
$mdomain = $1;
if ( !-e "$homedir/etc/$mdomain" || $forcecheck ) {
mkdir( "$homedir/etc/$mdomain", ( $hasmailnull ? 0710 : 0700 ) );
foreach my $fname ( 'passwd', 'shadow', 'quota' ) {
Cpanel::FileUtils::TouchFile::touchfile("$homedir/etc/$mdomain/$fname");
}
foreach my $fname ( 'passwd', 'quota' ) {
chmod( ( $hasmailnull ? 0644 : 0600 ), "$homedir/etc/$mdomain/$fname" );
}
chmod( 0600, "$homedir/etc/$mdomain/shadow" );
}
if ( !-e "$homedir/mail/$mdomain" ) {
mkdir( "$homedir/mail/$mdomain", ( $hasmailnull ? 0710 : 0700 ) );
}
}
exit;
}
}