File: //proc/self/root/proc/self/root/scripts.20110531.215904.25158/userdirctl
#!/usr/bin/perl
# cpanel - userdirctl 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 Cpanel::RcsRecord ();
use Cpanel::Config ();
use Cpanel::Config::LoadCpConf ();
use Cpanel::HttpUtils::ApRestart ();
use Cpanel::SafeFile ();
use Cpanel::SafeRun::Simple ();
use Cpanel::Logger ();
my $logger = Cpanel::Logger->new();
my $ctl = lc shift @ARGV;
if ( $ctl !~ /^o(n|ff)$/ ) {
die "$0: usage: $0 <on|off>";
}
exit if ( !-e "/usr/local/apache/conf/httpd.conf" );
my $cpconf = Cpanel::Config::LoadCpConf::loadcpconf();
$cpconf->{'userdirprotect'} = $ctl eq 'on' ? 1 : 0;
Cpanel::Config::savecpconf($cpconf);
my $UDS = Cpanel::Config::loadConfig( '/var/cpanel/moddirdomains', {}, '\s*[:]\s*', '^\s*[#]' );
my $userdomains = Cpanel::Config::loaduserdomains( {}, 1 );
$httplock = Cpanel::SafeFile::safeopen( \*HC, "+<", "/usr/local/apache/conf/httpd.conf" );
if ( !$httplock ) {
$logger->die("Could not edit /usr/local/apache/conf/httpd.conf");
}
while (<HC>) {
push( @CFILE, $_ );
}
seek( HC, 0, 0 );
foreach (@CFILE) {
$line = $_;
if ( !( $line =~ /^#/ ) ) {
if ( $line =~ /<virtualhost/i ) {
$bh = 1;
$ivh = 1;
}
if ( $line =~ /<ifmodule\s*mod_userdir/i && $ivh ) {
$iuserdir = 1;
$userdirl = '';
$killuserdir = 0;
}
if ( $line =~ /^[\s]*userdir[\s]+/i && ( !($ivh) ) ) {
next if ( $line =~ /^[\s\t]*userdir[\s\t]+disable/i );
next if ( $line =~ /^[\s\t]*userdir[\s\t]+enable/i );
print HC "UserDir public_html\n";
next();
}
if ( $iuserdir && $line =~ /<\/ifmodule/i && $ivh ) {
$iuserdir = 0;
if ($killuserdir) {
$userdirl = '';
next;
}
else {
print HC $userdirl;
$userdirl = '';
}
}
if ( $line =~ /<\/virtualhost/i ) {
$ivh = 0;
}
if ( $ivh && $ctl eq "on" ) {
if ( $line =~ /^\s*Userdir\s*disabled/i ) {
$bh = 0;
}
if ( $line =~ /ServerName\s*(\S+)/i ) {
$sn = $1;
$sn =~ s/^www\.//g;
if ($bh) {
$owner = $userdomains->{$sn} || '';
if ( $owner ne "" && $owner ne "root" && $owner ne "*" && getpwnam($owner) ) {
if ( $UDS->{$sn} ne "-1" ) {
print HC " <IfModule mod_userdir.c>\n";
print HC " UserDir disabled\n";
print HC " UserDir enabled ${owner} $UDS->{$sn}\n";
print HC " </IfModule>\n";
}
}
else {
if ( $UDS->{"DefaultHost"} ne "-1" ) {
print HC " <IfModule mod_userdir.c>\n";
print HC " UserDir disabled\n";
print HC " UserDir enabled " . $UDS->{"DefaultHost"} . "\n" if ( $UDS->{'DefaultHost'} );
print HC " </IfModule>\n";
}
}
}
}
}
if ( $ivh && $ctl eq "off" ) {
$killuserdir = 1;
if ( $line =~ /^\s*Userdir\s*disable/i ) {
next;
}
}
}
if ($iuserdir) {
$userdirl .= $line;
}
elsif ( $line =~ /^\s*userdir/i ) {
next;
}
else {
print HC $line;
}
}
truncate( HC, tell(HC) );
Cpanel::SafeFile::safeclose( \*HC, $httplock );
Cpanel::RcsRecord::rcsrecord("/usr/local/apache/conf/httpd.conf");
Cpanel::HttpUtils::ApRestart::safeaprestart(1);