File: //proc/self/root/scripts.20110531.215904.25158/convertemails2
#!/usr/bin/perl
# cpanel12 - convertemails2 Copyright(c) 1997-2007 cPanel, Inc.
# All rights Reserved.
# copyright@cpanel.net http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited
my %DOMAIN;
if ( open my $ud_fh, '<', '/etc/trueuserdomains' ) {
while ( my $line = readline $ud_fh ) {
chomp $line;
$line =~ s/\s+//g;
my ( $domain, $user ) = split /:/, $line, 2;
next if ( !$user || !$domain );
$DOMAIN{$user} = $domain;
}
close $ud_fh;
}
open( PASSWD, '/etc/passwd' );
while (<PASSWD>) {
my ( $user, $uid, $gid, $homedir ) = ( split( /:/, $_ ) )[ 0, 2, 3, 5 ];
next if ( $uid < 500 || $homedir eq '/' || $homedir eq '' || length($homedir) < 5 || !-e $homedir . '/etc' || !-e $homedir . '/etc/.imapv4cp5a' );
if ( -l $homedir . '/etc/passwd' ) {
print "Warning, passwd file $homedir/etc/passwd is a symlink\n";
next;
}
if ( my $pid = fork() ) {
waitpid( $pid, 0 );
#parent
}
else {
setuids( $uid, $gid );
if ( -e "$homedir/etc/passwd" && !-e "$homedir/etc/.imapv4cp5a" ) {
print "Updating $user ...";
my $domain = $DOMAIN{$user};
next if !$domain;
next if !-d "$homedir/etc/$domain";
chdir "$homedir/etc";
if ( -e 'passwd,v' ) {
system 'co', '-f', 'passwd';
}
print "($homedir/etc/passwd)";
my @PW;
open( PW, "$homedir/etc/passwd" );
while (<PW>) {
if ( !/\d+\,\d+/ ) {
push( @PW, $_ );
}
}
close(PW);
chmod( 0600, "$homedir/etc/passwd" );
open( SHADOW, "shadow" );
while (<SHADOW>) {
my $popuser = (split( /:/, $_, 2 ))[0];
my $hasuser = 0;
foreach my $line (@PW) {
if ( $line =~ m/^\Q$popuser\E:/ ) {
$hasuser = 1;
}
}
if ( !$hasuser ) {
push @PW, "$popuser:x:$uid:$gid::$homedir:/bin/bash\n";
}
}
close(SHADOW);
open( PW, '>', $homedir . '/etc/passwd' );
foreach (@PW) {
next if m/\d+\,\d+/;
my @PWLINE = split /:/, $_;
$PWLINE[5] = "$homedir/mail/$domain/$PWLINE[0]";
my $line = join ':', @PWLINE;
$line =~ s/\n//g;
print PW "$line\n";
}
close(PW);
unlink("$homedir/etc/$domain/passwd");
unlink("$homedir/etc/$domain/shadow");
unlink("$homedir/etc/$domain/quota");
link( "$homedir/etc/passwd", "$homedir/etc/$domain/passwd" );
link( "$homedir/etc/shadow", "$homedir/etc/$domain/shadow" );
link( "$homedir/etc/quota", "$homedir/etc/$domain/quota" );
open( PI, ">$homedir/etc/.imapv4cp5a" );
close(PI);
print "...Done\n";
}
exit;
}
}
sub setuids {
my ( $uid, $gid ) = @_;
if ( !( $( = int($gid) ) ) {
print "error setting gid\n";
exit;
}
if ( !( $) = "$gid $gid" ) ) {
print "error setting gid\n";
exit;
}
if ( !( ( $< = $uid ) && ( $> = $uid ) ) ) {
die "error setting uid ($uid)\n";
}
return $uid;
}