File: //proc/self/root/scripts.20110531.215904.25158/killsslvhost
#!/usr/bin/perl
# cpanel - killsslvhost 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::ConfigFiles ();
use Cpanel::DomainTools ();
use Cpanel::AcctUtils ();
use Cpanel::Config ();
use Cpanel::Config::userdata ();
use Cpanel::SSL::Domains ();
use Cpanel::Hostname ();
use Cpanel::Logger ();
use Cpanel::RcsRecord ();
use Cpanel::CachedDataStore ();
use Cpanel::SafeFile;
my $logger = Cpanel::Logger->new();
my $httpconf = Cpanel::ConfigFiles::find_httpconf();
my $ssl_port = Cpanel::Config::get_ssl_httpd_port(1);
my $servername = $ARGV[0];
$servername =~ s/[\n\s]*//g;
exit if !$servername;
exit if length($servername) < 3;
my $domain = $servername;
$domain = Cpanel::DomainTools::normalize($domain);
if ( !Cpanel::DomainTools::is_valid($domain) ) {
print "Sorry, that's an invalid domain\n";
exit;
}
Cpanel::RcsRecord::rcsrecord($httpconf);
my $in_vhost = 0;
my $hasip = 1;
my $pln = 1;
my $vhost = '';
## REVIEW NOTE: it is possible that $in_vhost_to_remove is the same state as "$in_vhost && !$pln", but
## without a better variable name for $pln (which I think means print_line), it is hard to say.
## If this chunk of code gets more complex, I would recommend having an explicit $state variable,
## that would have values such as 'in_vhost_to_remove'...
my $in_vhost_to_remove = 0;
if ( !-e $httpconf ) { $logger->die("Could not find $httpconf"); }
my $httplock = Cpanel::SafeFile::safeopen( \*HTTPCONF, '+<', $httpconf );
if ( !$httplock ) {
$logger->die("Could not edit $httpconf");
}
my @THTTPCONF = <HTTPCONF>;
seek( HTTPCONF, 0, 0 );
my $removed = 0;
my $linecount = 0;
my $in_proxy_vhost = 0;
my @files_to_remove;
foreach my $line (@THTTPCONF) {
$linecount++;
if ($in_vhost) {
if ( $line =~ /^\s*servername\s+(www\.)?\Q$domain\E$/i ) {
print "Removed $domain Server at line: $linecount.\n";
$pln = 0;
$removed = 1;
$in_vhost_to_remove = 1;
}
if ( $in_vhost_to_remove && $line =~ m/\s*(SSLCertificateKeyFile|SSLCertificateFile|SSLCACertificateFile)\s+(.*)$/i ) {
push(@files_to_remove, $2);
}
}
if ( $line =~ m/^\s*# CPANEL\/WHM\/WEBMAIL(\/WEBDISK)? PROXY SUBDOMAINS/ ) {
$in_proxy_vhost = 1;
$pln = 0;
$in_vhost = 0;
}
if ( !$in_proxy_vhost && $line =~ /^\s*<virtualhost/i && ( $line =~ /:443/ || $line =~ /:${ssl_port}/ ) ) {
$in_vhost = 1;
$vhost = '';
$pln = 1;
}
if ($in_vhost) { $vhost .= $line; }
if ( $in_vhost == 0 ) {
print HTTPCONF $line;
}
if ( $line =~ /\s*<\/virtualhost/i ) {
$in_vhost = 0;
$in_proxy_vhost = 0;
if ( $pln == 1 ) { print HTTPCONF $vhost; }
$vhost = '';
$in_vhost_to_remove = 0;
}
}
truncate( HTTPCONF, tell(HTTPCONF) );
Cpanel::SafeFile::safeclose( \*HTTPCONF, $httplock );
if ( !$removed ) {
print "$domain not found in httpd.conf (SSL).\n";
}
else {
print "Removed Entry from httpd.conf\n";
my $owner = Cpanel::AcctUtils::getdomainowner($domain);
if ($owner) {
$owner = 'nobody' if $owner eq 'root';
unlink '/var/cpanel/userdata/' . $owner . '/' . $domain . '_SSL';
unlink '/var/cpanel/userdata/' . $owner . '/' . $domain . '_SSL.cache';
unlink '/var/cpanel/userdata/' . $owner . '/www.' . $domain . '_SSL';
unlink '/var/cpanel/userdata/' . $owner . '/www.' . $domain . '_SSL.cache';
if ( $owner eq 'nobody' ) {
# update the main userdata file
my $wwwacct = Cpanel::Config::loadwwwacctconf();
my $main_hostname = exists $wwwacct->{'HOST'} && $wwwacct->{'HOST'} ? $wwwacct->{'HOST'} : Cpanel::Hostname::gethostname();
if ( $domain ne $main_hostname ) {
my $userdata_file = '/var/cpanel/userdata/' . $owner . '/main';
my $main_ref = Cpanel::CachedDataStore::loaddatastore( $userdata_file, 1 ); # lock the file.
if ( !defined $main_ref ) {
print "Unable to load '$userdata_file'\n";
exit;
}
my $ud_main_ref = $main_ref->{'data'};
my @good_subdomains = ();
foreach my $subdomain ( @{ $ud_main_ref->{'sub_domains'} } ) {
push @good_subdomains if ( $subdomain && $subdomain ne $domain );
}
@{ $ud_main_ref->{'sub_domains'} } = @good_subdomains;
Cpanel::CachedDataStore::savedatastore( $userdata_file, $main_ref );
Cpanel::Config::userdata::update_cache( $owner );
}
}
# remove from ssldomains
Cpanel::SSL::Domains::remove_ssl_domain($domain);
}
unlink @files_to_remove if scalar @files_to_remove;
}
Cpanel::RcsRecord::rcsrecord($httpconf);