File: //scripts.20110531.215904.25158/linksubemailtomainacct
#!/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 Getopt::Long;
use Cpanel;
use Cpanel::SafeFile;
use Cpanel::AccessIds ();
use Cpanel::PwCache ();
sub usage {
die "Usage: $0 [ -force ] [ -quiet ] <username>\n(Note: The -force option is not yet implemented, and currently has no effect.)\n";
}
$| = 1;
# The -force option is not used, but we'll retain it due to
# historical precedent, and in case it might be implemented
# some day.
my $force = 0;
my $quiet = 0;
my $delete = ""; # delete old symlinks
my $opt_parse_result = GetOptions(
"force" => \$force,
"quiet" => \$quiet,
"delete=s" => \$delete, # don't create new symlinks, just delete old ones for given domain
);
usage unless $opt_parse_result && @ARGV <= 1; # note ok if $ARGV == 0
my $convertuser = $ARGV[0];
my $hasmailnull = getpwnam 'mailnull' ? 1 : 0;
print "cPanel Email Linker v1.0\n" if !$quiet;
Cpanel::PwCache::setpwent();
while ( my @PW = Cpanel::PwCache::getpwent() ) {
my ( $user, $uid, $gid, $homedir ) = @PW[ 0, 2, 3, 7 ];
next if ( $convertuser && $user ne $convertuser );
$homedir =~ /(.*)/; # Untaint
$homedir = $1;
next
if ( $uid < 500
|| length($homedir) < 5
|| !-e $homedir . '/etc/'
|| -l $homedir . '/etc/passwd' );
if ( my $pid = fork() ) {
#parent
waitpid( $pid, 0 );
}
else {
my $cp = Cpanel->new;
$cp->initcp($user);
Cpanel::AccessIds::setuids( $uid, $gid );
if ($delete) {
# If -delete was specified on the command line
# followed by a domain name, it means this script
# was called to delete symlinks rather than to
# create any. The symlinks to be deleted are the
# email links pertaining to the specified domain,
# that were all in all probability created by this
# very script at some time in the past.
$delete =~ s/\./_/g;
my @links = glob "$homedir/mail/.*\@$delete";
($_) = /(.*)/ for @links; #Untaint
unlink @links;
exit 0;
}
my %MP;
foreach my $mdomain (@Cpanel::DOMAINS) {
next if ( $MP{$mdomain} );
$MP{$mdomain} = 1;
opendir( my $dddir, "$homedir/mail/$mdomain" );
while ( my $acct = readdir($dddir) ) {
next if ( $acct =~ /^\./ );
next if ( $acct eq 'new' || $acct eq 'cur' || $acct eq 'tmp' || $acct =~ /^courier/ );
$acct =~ /(.*)/;
$acct = $1;
my $linkname = "${acct}\@${mdomain}";
$linkname =~ s/\./_/g;
if ( !-l "$homedir/mail/.${linkname}" ) {
symlink( "$mdomain/$acct", "$homedir/mail/.${linkname}" );
}
}
}
exit 0;
}
}
Cpanel::PwCache::endpwent();