Perl

(Updated: 2019-09-05)

All Whois

#!/usr/local/bin/perl
$domain=shift;
$registrar=`whois -h whois.crsnic.net $domain`;
$registrar =~ m/Whois Server: (.+)/;
$whois = $1;
print $registrar;
print "\nChecking $whois.....\n";
system("whois -h $whois $domain");


Free IPs

#!/usr/local/bin/perl
$host=`hostname`;
$host =~ m/^(\w*)\..*/;
$host = $1;
open IN, "</etc/serverlist";
while (<IN>) {
  last if /^$host\s/;
}
close IN;
$hostline=$_;
($junk,$support,$ipbase,$serverip,$nvhip,$maxnvh) = split;

#compose master list of IPs for this server
#for ($x=1;$x<=255;$x++) {push @IP,"no";}
if (-f "/etc/virtualip") {
  open IN,"</etc/virtualip";
  while (<IN>) {
    #($a,$b,$c,$d) = split '\.',$_;
    chomp;
    $d = $_;
    $IP{$d} = 'yes';
    #print "$d -> $IP[$d]\n";
  }
  close IN;
}

$vh=0;$ip=0;
$IP{"${ipbase}.${nvhip}"} = 'no';

open CONF, "</mnt/web/conf/httpd.conf";
while (<CONF>) {
  chomp;
  $vh++ if (/^<VIRTUALHOST /i);
  next if (/^<VIRTUALHOST\s+$ipbase\.$nvhip\s*>/i);  #if named virt host by ip number

  if (/^<VIRTUALHOST /i) {
    chop;
    $ip++;
    @vhw = split / /;
    $vhx = $vhw[1];
    $IP{$vhx} = "no";
    #print "$vhx -> $IP{$vhx}\n";
    next;
  }

}


#for ($x=2;$x<=245;$x++) {
while ( ($key,$vhvalue) = each %IP ) {
  if ($vhvalue eq "yes") {
   print "$key\n";
  }
    #print "$key -> $vhvalue\n";
}
        

print "Total: $vh IP: $ip Named: ",$vh-$ip,"\n";


Mass Replace

#!/usr/bin/perl

require 'newgetopt.pl';

main: {

  if (! do NGetOpt("skip=s", "dir=s", "suffix=s", "clobber=s"))
  {
    &Usage && exit 1;
  } 

  if ($#ARGV < 2)
  {
    &Usage && exit 1;
  } 

  $Outdir = ($opt_dir ? $opt_dir : ".");
  $Suffix = ($opt_suffix ? $opt_suffix : ".out");

  $replace = $ARGV[0];
  $with = $ARGV[1];
  shift; shift;

ONE:
  while (@ARGV)
  {
    $file = $ARGV[0];
    shift;
    if (! -f $file)
    {
      print "$file is not a file (skipping)\n"; 
      next ONE;
    }

    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
       $atime,$mtime,$ctime,$blksize,$blocks) 
      = stat $file;
    $outfile = "$Outdir/$file$Suffix";
    print "$outfile\n";
    open (IN, "$file") || print "Unable to open $file\n" && next ONE;

    if (! $opt_clobber && -f $outfile)
    {
      print "$outfile already exists, do you want to overwrite it?(y/n) ";
      $yorn = <STDIN>;
      next ONE if ($yorn !~ /^y/ && $yorn !~ /^Y/);
    }
    open (OUT, ">$outfile") || print "Unable to open $outfile\n" && next ONE;
    while (<IN>)
    {
      if ($opt_skip)
      {
        if (! m/$opt_skip/)
        {
          s/$replace/$with/g;
        }
      }
      else
      {
        s/$replace/$with/g;
      }
  print OUT $_;
    }
    close (IN);
    close (OUT);
    system "/bin/mv -f $outfile $file";
    system "/bin/rm -f $outfile";
    chown $uid, $gid, $file;
    chmod $mode, $file;
  }
}

sub Usage {

  print <<"DO";
Usage: massreplace [-skip string] [-dir dirname] [-suffix string] [-clobber] string1 string2 file [file...]

  -dir dirname  Put the output files in this directory.
      (Default is current directory)

  -suffix string  Use this suffix instead of .out.
      (Default is .out)

  -skip string  Skip the entire line if it contains this string.

  -clobber  Overwrite existing output files without prompting,
      if they exist.
      (Asks otherwise)
DO
}


Move Apache Domain

#!/usr/local/bin/perl

use File::Copy;
$|=1;
$LOGDIR = "/.logs/";
$MAIL = "/etc/mailertable";
$CONF = "/web/conf/httpd.conf";
$SSL = "/web/ssl_conf/httpd.conf";
$HISTORY="/usr/root/.history.$yourid";
chomp($HOSTFULL=`hostname`);
($HOST,$HOSTDOM,$HOSTEXT) = split /\./, $HOSTFULL;
chomp($IPBASE = `grep -w ^$HOST /etc/serverlist | awk '{print \$3}'`);
$FP = "/usr/local/frontpage/";

&lock or &q("");

if ( $ARGV[0] eq "" ){
   print "What is the old name? ";
   chomp($old = <STDIN>);
}
else {
   $old = $ARGV[0];
}
if ( $ARGV[1] eq "" ){
   print "What is the new name? ";
   chomp($new = <STDIN>);
}
else {
   $new = $ARGV[1];
}

open HISTORY,">>/usr/root/.history.$ENV{'yourid'}"  or die "Cannot open History file";
print HISTORY "mvdomain $old $new","\n";
close ( HISTORY );

open SUSPLOG,">>/var/log/suspended.log"  or die "Cannot open Suspended Site Log";
print SUSPLOG "$old had domain name changed to $new by $ENV{'yourid'} on $WEEKDAY, $MON $mdat, $YEAR at $hour:$min \n"; 
close ( SUSPLOG );


#validate formats
($old1,$old2) = split /\./, $old;
($new1,$new2) = split /\./, $new;
($old2) or &q("Bad format for $old\n");
($new2) or &q("Bad format for $new\n");

#Check old name
$nslookup = `/usr/sbin/nslookup $old`;
($nslookup =~ m/^Server:  ns\d*\.web2010\.com/) or &q("Name server unavailable\n");
$nslookup =~ s/.*\n.*\n\nName:    $old\nAddress:  (.*)\n\n/$1/;
$oldip = $nslookup;
$onbox=`grep ^$oldip\$ /etc/virtualip`;
($oldip == $onbox) and &q("$old is not on this server\n");
print "$old is at $oldip on this server\n";

#Check new name
$nslookup = `/usr/sbin/nslookup $new`;           
($nslookup =~ m/^Server:  ns\d*\.web2010\.com/) or &q("Name server unavailable\n");
$nslookup =~ m/.*\n.*\n\nName:    $new\nAddress:  (.*)/;  # to get $1
$newip = $1;
if ($newip) {
  print "$new already exists at $newip\n";
  if ($newip =~ m/$IPBASE/) {
    print "$newip is on this server\n";
  }
  print "Do you wish to continue?";
  $yn = <STDIN>;
  if ($yn !~ m/[Yy]/) {
    &q("OK\n");
  }
}
else {
  print "That's good!\n";
}

#is it a FP site
if (-e "${FP}${old}:80.cnf") {
  $fp_exist = "yes";
  system ("/usr/local/frontpage/version4.0/bin/fpsrvadm.exe -o uninstall -p ${old}:80");
#  &q("You must remove Frontpage first");
}

#create all backups
copy("$CONF","${CONF}.bak");
copy("$MAIL","${MAIL}.bak");
copy("$SSL","${SSL}.bak");


#Get the Document Root
$dh = "";
$sn = "";
open CONF or &q("Couldn't open $CONF\n");
LINE: while (<CONF>) {
  if (m/^<VIRTUALHOST /i) { $vh = $_; $dr = ""; $sn = ""; next LINE;}
  if (m/DocumentRoot /i)  { $dr = $_; }
  if (m/ServerName $old/i)    { $sn = $_; }
  if ($dr ne "" and $sn ne "") { last LINE;} # we have everything for now
}
close CONF;
if ($dr eq "" or $sn eq "") {
  &q("$old is missing entries in $CONF\n");
}
print $vh,$dr,$sn;  

#get user from mailer
$ml=""; 
open MAIL or &q("Couldn't open $MAIL\n");
LINE: while (<MAIL>) {
  if (m/$old/i) { $ml = $_; last LINE;}
}
close MAIL;
($ml ne "") or &q("$old is missing from $MAIL\n");
$ml =~ m/$old spcl:$old\.(.*)_spcl/;  #to get $1
$user = $1;
print "The Associated User is $user\n";


#get homedir from user's password record
$homedir = (getpwnam $user)[7];
($homedir ne "") or &q("Invalid user $user\n");
print "The Home Dir is $homedir\n";


#Next let's rename the main log files
if ($old1 ne $new1) {
  &filemove("${LOGDIR}$old1/$old1.log","${LOGDIR}$old1/$new1.log");
  &filemove("${LOGDIR}$old1/$old1.log2","${LOGDIR}$old1/$new1.log2");
  &filemove("${LOGDIR}$old1/usage.html","${LOGDIR}$old1/usage.html"); 
  &filemove("${LOGDIR}$old1","${LOGDIR}$new1");
  &filemove("/usr/root/setup.copies/${old1}.setup","/usr/root/setup.copies/${new1}.setup");
}

#Is there a local logs directory
if (-d "$homedir/logs") {  #if so remove links
  system("rm $homedir/logs/mtd.log $homedir/logs/today.log $homedir/logs/$old1.log $homedir/logs/$old1.log2 $homedir/logs/usage.html"); }
else {  #otherwise create dir
  if (-e "$homedir/logs") {  #if logs is a file then move it
    &filemove("$homedir/logs","$homedir/logs.$old1.$new1");
  }
  system("mkdir $homedir/logs") or &q("Couldn't create $homedir/logs")
}

#Create links for logs
filelink("${LOGDIR}$new1/$new1.log","${homedir}/logs/today.log");
filelink("${LOGDIR}$new1/$new1.log2","${homedir}/logs/mtd.log");
filelink("${LOGDIR}$new1/usage.html","${homedir}/logs/usage.html");

#Change name in @FILES - these are local files and are more forgiving
@files = ("${homedir}/setup",
          "${homedir}/.domains",
    "${homedir}/index.htm",
          "${homedir}/.mreply.rc",
    "${homedir}/welcome.htm",
    "/usr/root/setup.copies/${new1}.setup",
          "${homedir}/logs/sample.htaccess");
$dr =~ s/Documentroot *//i;
chomp($dr);     #get old document root (must have homedir in path)
if ($homedir ne $dr) { push @files,("${dr}/index.htm","${dr}/welcome.htm","${dr}/setup"); }
foreach $file (@files) {
  if (-e $file) {
    print "Updating $file\n";
  }
  else {
    print "$file doesn't exist\n";
    next;
  }
  open IN,"<$file";
  open OUT,">${file}.temp";
  while (<IN>) {
    $keep = $_;
    s/\b$old/$new/g;   #substitute old name - this is the general replacement
    s/\/old1/\/$new1/g;  #this takes care of path info '/old'
    print OUT;
    if ($keep ne $_) { print "Changed: $keep      to: $_"; }
  }
  close IN;
  close OUT;
  copy("${file}.temp","$file");
  chown $user,-1, ${file}.temp;
  print "Updated $file\n";
}

#change $CONF and $SSL
foreach $file ("$CONF","$SSL"){ 
  print "Updating $file\n";
  open IN,"<$file";
  open OUT,">${file}.temp";
  $flag = 0;
  while (<IN>) {
    if (/$vh/i) {  #is this the start
      $flag = -1;
    }
    if (/<\/VIRTUALHOST>/) {  #end of section
      $flag = 0;   #if flag is set then unset
    }
    if ($flag == 0) {  #not inside block, so ignore
      print OUT;
      next;
    }
    # We are inside VH block so check for replacements
    $keep = $_;
    if (m/^User/) {print OUT;next;}  #Don't want to make mistake on this line
    if (m/^Group/) {print OUT;next;}  # or this one
    s/$old/$new/gi;  #substitute whole domain
    s/\*$old1/\*$new1/g;  #substitute ServerAlias
    s/\*\.$old1/\*\.$new1/g;  #substitute ServerAlias
    s/\/guide\/$old1/\/guide\/$new1/gi;  #substitute path info
    s/\/.logs\/$old1/\/.logs\/$new1/gi;  #sub log path info
    s/$old1\.l/$new1\.l/gi;  #substitute log file
    print OUT;
    if ($keep ne $_) { print "Changed: $keep      to: $_"; }
  }
  close IN;
  close OUT;
  print "Updated $file\n";
}

#Change Mailertable
print "Updating $MAIL\n";
open IN,"<$MAIL";
open OUT,">${MAIL}.temp";
while (<IN>) {
  $keep = $_;
  if (/^$old/ or /^\.$old/) {
    s/$old/$new/gi;  #substitute whole domain
    print OUT;
    if ($keep ne $_) { print "Changed: $keep      to: $_"; }
  }
  else {
    print OUT;
  }
}
close IN;
close OUT;
print "Updated $Mail\n";
system("/etc/newda");

#Change Home DIr
print "Updating home directory\n";
open IN,"</etc/master.passwd";
open OUT,">/etc/master.passwd.temp";
while (<IN>) {
   if (/^${user}:/) {
     @pass = split /:/;
     @pass[8] =~ s/$old1/$new1/i;
     $_ = join ':' , @pass;
   }
  print OUT;
}
close IN;
close OUT;
system("pwd_mkdb -p /etc/master.passwd.temp") == 0
           or die "Can't rebuild password:$!\n";



#Rename Root Dir
$newroot = $dr;
$newroot =~ s/$old1/$new1/i;
@dr = split '/',$dr;
@newroot = split '/',$newroot;
$drp = "";
$newrootp = "";
for ($x=1;$x<=$#dr;$x++) {
  $drp = $drp . "/" . @dr[$x];
  $newrootp = $newrootp . "/" . @newroot[$x];
  if ($drp ne $newrootp) {
    &filemove($drp,$newrootp);
    last;
  }
}


#Move Temp Files
copy("${CONF}.temp","$CONF");
copy("${SSL}.temp","$SSL");
copy("${MAIL}.temp","$MAIL");

$domain_id=`echo "select domain_id from domain.info where domain_name='$old1.$old2';" | mysql -u serv_man -h carol | grep -v domain_id `;

if ( $ARGV[2] eq "" ){
   $reseller_id=`echo "select resell_id from domain.info where domain_id=$domain_id;" | mysql -u serv_man -h carol | grep -v resell_id `;
   $reseller=`echo "select domain_name from domain.info where domain_id=$reseller_id;" | mysql -u serv_man -h carol | grep -v domain_name `;
#   print "If resold account, what is the master domain name?";
#   chomp($reseller=<STDIN>);
}
else {
   $reseller = $ARGV[2];
}

#Change Nameserver entry and reboot web daemon

print "Remove old nameserver entry $old1.$old2 ?\n";
  $yn = <STDIN>;
  if ($yn =~ m/[Yy]/) {
    system("rmvdns","$old1.$old2","$reseller");
  }

print "Clearing the new nameserver entry $new1 $new2 (if it exists)...\n";
system("rmvdns","$new1.$new2","$reseller");
print "Creating new nameserver entry $new1 $new2 $newip\n";
system("adddns","$new1.$new2","$oldip","$reseller");

print "Updating CAROL\n";
$reply=`echo "update domain.info set domain_name='$new1.$new2' where domain_name='$old1.$old2';" | mysql -u serv_man -h carol `;
print $reply;

if ( $fp_exist == "yes" ) {
   print "Running frontpage setup\n";
   $pass=`echo "select passwd from domain.users where domain_id=$domain_id;" | mysql -u serv_man -h carol | grep -v passwd `;  
   $syscmd = "/usr/local/frontpage/version4.0/bin/fpsrvadm.exe " .
        "-o install " .
        "-t apache-fp " .
        "-s /web/conf/httpd.conf " .
        "-p 80 " .
        "-m $new " .
        "-u $user " .
        "-a administrators " .
        "-xUser $user " .
        "-xGroup bbsuser " .
        "-password $pass";
   print $syscmd;
$response = `$syscmd`;
print $response;

print "Adding mail bot configuration \n";

open CONF, ">>/usr/local/frontpage/$new:80.cnf";
print CONF "SMTPHost:mail.$new\n";
print CONF "MailSender:$user\@$new\n";
close CONF;
$dir="/web/guide/$new1";
system("touch $dir/wwwfp/_vti_pvt/service.lck; chmod 666 $dir/wwwfp/_vti_pvt/service.lck; chown $user:bbsuser $dir/wwwfp/_vti_pvt/service.lck");
}

kill 'HUP',`cat /var/run/httpd.pid`;
&q("Done!\n");
exit;

sub q {
&unlock;
unlink("${CONF}.temp");
unlink("${SSL}.temp");
unlink("${MAIL}.temp");
#unlink("/etc/master.passwd.temp");

print (@_);
exit;
}

#creates lock file, returns 1 if success, 0 if fail or already locked
sub lock {
# check for lock
$LOCK = "/web/ssl_conf/conf.lock";
if (-e $LOCK) {
  open LOCK,"<$LOCK" or die "Can't open lock file";
  while (<LOCK>) {
    print;
  }
  close LOCK;
  return 0;
}
#make lock
open LOCK,">$LOCK" or die "Can't open lock file";
$date = dateinfo(time);
print LOCK "$ENV{yourid} started $0 on $date\n";
close LOCK;
return 1;
}

sub unlock {
sleep 2;
return  unlink "$LOCK";
}


sub filemove {
  my ($o,$n) = @_;
  if (system("mv $o $n") == 0) {
    print "Renamed $o to $n\n";
  }
  else {
    print "*** Couldn't rename $o\n";
  }
}


sub filelink {
  my ($f,$l) = @_;
  if (symlink($f,$l)) {
    print "Created $l link to $f\n";
  }
  else {
    print "*** Couldn't create $l link\n";
  }
}


sub dateinfo {
  my $time = shift(@_);
  ($sec,$min,$hour,$mdat,$mon,$year,$wday,$yday,$isdst) = localtime $time;
  $x = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[$wday];
  $x = $x . " " . (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon];
  $x = $x . " " . sprintf "%2d",$mdat;
  $x = $x . sprintf " %2d:%2d:%2d", $hour,$min,$sec ;
  if ($isdst) { $x = $x . " EDT "; }
  else { $x = $x . " EST "; }
  $x = $x . (1900+$year);
  return $x;
}


Remove Account

#!/usr/local/bin/perl 
use File::Copy;
use Getopt::Std;
#use DBI;
use LWP;
use URI::URL;


getopt('');
&usage;

#load arguments
@domains = @ARGV;

#turn off buffering
$|=1;

&lock or &goaway("");



#get domain name
if (! $domains[0]) { 
  #present list
  opendir GUIDE,"/mnt/web/guide"; @dir=readdir(GUIDE);for $dir (@dir) {next if ($dir !~ m/shutdown.tar/); push @list,$dir}closedir(GUIDE);
  grep { s/.shutdown.tar// } @list;
  for ($o=0;$o<=$#list;$o++) {
    $conf[$o] = `egrep -i "ServerName +$list[$o]\." /web/conf/httpd.conf | awk '{print $2}'`;
    $conf[$o] =~ s/Servername +//i;
    chomp($conf[$o]);
    if ((-M "/mnt/web/guide/$list[$o].shutdown.tar") > 21) {
  $ls = "Over 21 days";
  $st = `cat /mnt/web/guide/$list[$o]/shutdown.txt | tr -d "\n"`;
    } else {
  $st = `cat /mnt/web/guide/$list[$o]/shutdown.txt | tr -d "\n"`;
  $ls = "DO NOT REMOVE YET";
    }
    printf "%2d: %30s %-18s %-25s %-25s\n",$o, $conf[$o], $ls,$list[$o], $st;
  }
  print "Enter selection(s):";
  chomp($sel=<STDIN>);
  #handle spaces and commas the same
  $sel =~ tr/ /,/;
  while ($sel =~ s/,,/,/){};
  @selections=split /,/,$sel;
  for $o (@selections) {
    push @domains,$conf[$o];
  }
    
}
for $domain (@domains) {

@msqldb=();
@docroots=();
@tlogs=();
@deadusers=();
@homedirs=();

print "Removing $domain\n";
#my $dbh=DBI->connect("DBI:FreeTDS:database=plat;host=216.157.79.239;port=1433;", "platuser", "nott00smart");
#$sth = $dbh->prepare("select active from customer where name='$domain'");
#$sth->execute;
#($plat_active) = $sth->fetchrow_array;
($plat_active,$plat_deact) = &getaccountinfo($domain);
print "Platypus shows the active status of this account as: $plat_active\n";
if ($plat_active ne 'N') {
  print "If you wish to remove the account from the server anyway type 'REMOVE' - ";
  chomp($remove = (<STDIN>));
  ($remove eq 'REMOVE') or next; #domains
} else {
  print "The deactivate date was: $plat_deact\n";
}
#$sth->finish;
#$dbh->disconnect;


print "-----Removing $domain from this server\n";

open CONF,"</web/conf/httpd.conf";
while (<CONF>) {
  last if m/^ServerName $domain/i;
}
($_ =~ /^ServerName $domain/i) or &goaway("I did not find $domain on this server\n");


#create list of everything to be removed
open CONF,"</mnt/web/conf/httpd.conf";
while (<CONF>) {  
  if (/^<virtualhost/i) { $inside=1; $contents=$_; next};
  unless ($inside) {next;}    
  #inside a virtual host tag
  if (/^DocumentRoot +(.+)/i){$documentroot = $1;}      
  if (/^ServerName +$domain/i) {$rightone=1;}     
  if (/^TransferLog +(.+)/i) {$transferlog = $1;}
  $contents .= $_;
  if (/^<\/virtualhost>/i) {  
    if ($rightone) {
      print "-----Removing from httpd.conf:\n";
      print $contents; 
      push @docroots,$documentroot;
      push @tlogs,$transferlog;
      $contents = "";
      $rightone=0;
    }
    else {
      $contents="";
    }
  }
}
close CONF; 

$rightone=0;
open SSL,"</mnt/web/ssl_conf/httpd.conf";
while (<SSL>) {
  if (/^<virtualhost/i) { $inside=1; $contents=$_; next};
  unless ($inside) {next;}
  #inside a virtual host tag
  if (/^ServerName +$domain/i) {$rightone=1;}
  $contents .= $_;
  if (/^<\/virtualhost>/i) {
    if ($rightone) {
      print "-----Removing from SSL httpd.conf:\n";
      print $contents;
      $contents = "";
      $rightone=0;
    } 
    else {
      $contents="";
    } 
  }  
}  
close SSL;
#determine master user
$dr=$docroots[0];
if ($dr =~ /(.+)\/www/) { $dr = $1; }
if ($dr =~ /(.+)\/wwwfp/) { $dr = $1; }
if ($dr =~ /(.+)\/frontpage/) { $dr = $1; }
if ($dr =~ /^\/web/) { $dr = '/mnt' . $dr; }
setpwent;
while (($name,$j,$uid,$j,$j,$j,$j,$dir,$j) = getpwent) {
  if ($dir =~ /^\/web/) { $dir = '/mnt' . $dir; }
  if ($dir eq $dr) {
    $masteruser = $name;
    last;
  }
}
$masteruser or &goaway("No user for this site - byebye\n");

$contents="";
open MTBL,"</etc/mailertable";
while (<MTBL>) {
  if (m/$domain /) { $contents .= $_; next; }
  if (m/\.$domain /) { $contents .= $_; next; }
  if (m/$domain\.$masteruser_spcl/) { $contents .= $_; next; }
}
print "-----Removing from /etc/mailertable:\n";
print $contents;

if (-e "/usr/local/frontpage/$domain:80.cnf") {
  print "-----Removing file /usr/local/frontpage/$domain:80.cnf\n";
}

open MSQL,"</usr/local/Minerva/msql.acl"; 
$contents="";
$rightone=0;
while (<MSQL>) {
  $contents .= $_;
  if (m/database=(.+)/) { $db=$1 }
  if ((m/read=.*$masteruser,/) || (m/read=.*$masteruser$/)) { $rightone=1;}
  if (m/host=$domain/) { $rightone=1;}
  if (m/access=/) {
    if ($rightone) {
      print "-----Removing from /usr/local/Minerva/msql.acl:\n";
      print $contents;
      push @msqldb,$db; 
      $contents="";
      $rightone=0;
    } else {
      $contents="";
    }
  }
}
close MSQL;

for $db (@msqldb) {
if (-e "/usr/local/Minerva/msqld/$db") {
  print "-----Removing database file /usr/local/Minerva/msqld/$db\n";
}
}

($a,$b,$c,$d) = unpack('C4',((gethostbyname($domain))[4])[0]);
print "Determined IP address to be $a.$b.$c.$d\n";
if (-e "/mnt/ftp/$a.$b.$c.$d") {
  print "-----Removing directory /mnt/ftp/$a.$b.$c.$d\n";
}


for $lg (@tlogs) {
  chomp($lg=`dirname $lg  2>&1`);
  if (-d "$lg") {
    print "-----Removing log directory $lg \n";
  }
}

print "-----Removing users:\n";
setpwent;
while (($name,$j,$uid,$j,$j,$j,$j,$dir,$j) = getpwent) {
  if ($name =~ m/^$masteruser\d*$/) {
    print "$name \n";
    push @deadusers, $name;
    if ($dir ne '/mnt/web/guide/mx') {
      push @homedirs,$dir;
  }
  }
}

for $u (@deadusers) {
  if (-e "/var/mail/$u") {
    print "-----Removing Mailbox file: /var/mail/$u\n";
  }
}

for $d (@homedirs) {   
  if (-e $d) {
  print "-----Removing Home Directory: $d\n";
  }
  if (-e "$d.shutdown.tar") {
    print "-----Removing Tar file: $d.shutdown.tar\n";
  }
}


  

print "\n\nDo you wish to proceed?";
chomp($yn = <STDIN>);
if ($yn =~ m/y/i) {
  print "Here we go...\n";
  #create save dir
  system("mkdir /dump/$domain.$$");

  open HISTORY,">>/usr/root/.history.$ENV{'yourid'}"  or die "Cannot open History file";
  print HISTORY "removeaccount $domain\n";
  close ( HISTORY );

  open SUSPLOG,">>/var/log/suspended.log"  or die "Cannot open Suspended Site Log";
  print SUSPLOG "$domain removed by $ENV{'yourid'} on $WEEKDAY, $MON $mdat, $YEAR at $hour:$min \n";
  close ( SUSPLOG );

  $inside=0;
  $rightone=0;
  copy("/mnt/web/conf/httpd.conf","/mnt/web/conf/httpd.conf.bak");
  open IN,"</mnt/web/conf/httpd.conf.bak";
  open OUT,">/mnt/web/conf/httpd.conf";
  open SAVE,">/dump/$domain.$$/httpd.conf";
  while (<IN>) {
    if (/^<virtualhost/i) { $inside=1; $contents=$_; next};
    if (! $inside) {
    print OUT $_; 
    next;
  }
    #inside a virtual host tag
    if (/^DocumentRoot +(.+)/i){$documentroot = $1;}
    if (/^ServerName +$domain/i) {$rightone=1;}
    if (/^TransferLog +(.+)/i) {$transferlog = $1;}
    $contents .= $_;
    if (/^<\/virtualhost>/i) {
      if ($rightone) {
        print SAVE $contents;
      $rightone=0;
      } else {
      print OUT $contents;
      }
      $contents="";
    }
  }
  close IN;
  close OUT;
  close SAVE;

  $rightone=0;
  $inside = 0;
  copy("/mnt/web/ssl_conf/httpd.conf","/mnt/web/ssl_conf/httpd.conf.bak");
  open IN,"</mnt/web/ssl_conf/httpd.conf.bak";
  open OUT,">/mnt/web/ssl_conf/httpd.conf";
  open SAVE,">/dump/$domain.$$/ssl.conf";
  while (<IN>) {
    if (/^<virtualhost/i) { $inside=1; $contents=$_; next};
    if (! $inside) {
        print OUT $_;
        next;
    }   
    #inside a virtual host tag
    if (/^ServerName +$domain/i) {$rightone=1;}
    $contents .= $_;
    if (/^<\/virtualhost>/i) {
      if ($rightone) {
      print SAVE $contents;
      $rightone=0;
      }
      else {
      print OUT $contents;
      }
      $contents = "";
    }
  }
  close IN;
  close OUT;
  close SAVE;

  copy("/etc/mailertable","/etc/mailertable.bak");
  open IN,"</etc/mailertable.bak";
  open OUT,">/etc/mailertable";
  open SAVE,">/dump/$domain.$$/mailertable";
  while (<IN>) {
    if (m/$domain /) { print SAVE ; next; }
    if (m/\.$domain /) { print SAVE ; next; }
    if (m/$domain\.$masteruser_spcl/) { print SAVE ; next; }
    print OUT;
  }
  close IN;
  close OUT;
  close SAVE;

  system("/etc/newda");
 
  copy("/usr/local/Minerva/msql.acl","/usr/local/Minerva/msql.acl.bak");
  open IN,"</usr/local/Minerva/msql.acl.bak";
  open OUT,">/usr/local/Minerva/msql.acl";
  open SAVE,">/dump/$domain.$$/msql.acl";
  $contents="";
  $rightone=0;
  while (<IN>) {
    $contents .= $_;
    if (m/database=(.+)/) { $db=$1 }
    if ((m/read=.*$masteruser,/) || (m/read=.*$masteruser$/)) { $rightone=1;}
    if (m/host=$domain/) { $rightone=1;}
    if (m/access=/) {
      if ($rightone) {
        print SAVE $contents;
      $rightone=0;
      } else {
        print OUT $contents;
      }
    $contents="";
    }
  }
  close IN;
  close OUT;
  close SAVE;

  for $db (@msqldb) {
    if (-e "/usr/local/Minerva/msqld/$db") {
      copy("/usr/local/Minerva/msqld/$db","/dump/$domain.$$/msqldb.$db");
      unlink("/usr/local/Minerva/msqld/$db");
    }
  }

  if (-e "/mnt/ftp/$a.$b.$c.$d") {
    system("cp -r /mnt/ftp/$a.$b.$c.$d /dump/$domain.$$");
    system("rm -r /mnt/ftp/$a.$b.$c.$d");
  }

  for $lg (@tlogs) {
    if (-e "$lg") {
      system("cp -r $lg /dump/$domain.$$");
      system("rm -r $lg");
    }
  }

  for $u (@deadusers) {
    if (-e "/var/mail/$u") {
      system("cp /var/mail/$u /dump/$domain.$$/mail.$u");
      system("rm /var/mail/$u");
    }
  }

  for $d (@homedirs) {   
    $x++;
    system("cp -r $d /dump/$domain.$$/homedir.$x");
    system("rm -r $d");
  }
  
  open SAVE,">/dump/$domain.$$/users.passwd";
  for $u (@deadusers) {
    ($n,$p,$i,$g,$q,$c,$s,$d,$h) = getpwnam($u);
    print SAVE "$n,$p,$i,$g,$q,$c,$s,$d,$h\n";
    system("rmuser $u >> /dev/null");
  }
  if (-e "/usr/local/frontpage/$domain:80.cnf") {
    system("cp /usr/local/frontpage/$domain:80.cnf /dump/$domain.$$/$domain:80.cnf");
  }
  
  for $d (@homedirs) {
    if (-e $d) {
    system("cp -r $d /dump/$domain.$$/");
    system("rm -r $d");
    }
    if (-e "$d.shutdown.tar") {
    system("cp -r $d.shutdown.tar /dump/$domain.$$/");
    system("rm -r $d.shutdown.tar");
    }
  }
 
  if (! $opt_d) {
  system("rmvdns $domain");
  }
}
}

&goaway("-All gone-\n");

############
#creates lock file, returns 1 if success, 0 if fail or already locked; sets time values
sub lock {
  (undef,$min,$hour,$mdat,$mon,$year,$wday,undef,undef) = localtime(time);
  $MON = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon];
  $YEAR = 1900 + $year;
  $WEEKDAY = (Sun, Mon, Tue, Wed, Thu, Fri, Sat)[$wday];

  # check for lock
  $LOCK = "/web/ssl_conf/conf.lock";
  if (-e $LOCK) {
    open LOCK,"<$LOCK" or die "Can't open lock file";
    while (<LOCK>) {
      print;
    }
    close LOCK;
    return 0;
  }
  #make lock
  open LOCK,">$LOCK" or die "Can't open lock file";
  print LOCK "$ENV{'yourid'} started $0 on $MON $mdat at $hour:$min\n";
  close LOCK;
  return 1;
}


sub goaway {
  #print any message
  print (@_);
  #wait just a sec
  sleep 1;
  #now remove lock file
  unlink "/web/ssl_conf/conf.lock";
 exit;
}

sub usage {
  if ($opt_h) {
    print "USAGE: removeaccount [-d] [domainname]\n";
    exit;
  }
  if ($opt_v) {
    print "06/16/99 - CLM - Reworked shell script to perl to handle ipless\n";
    exit;
  }
}


sub getaccountinfo {
  my $domain = shift;
  $active = ''; @rates = (-1);
  &getcookie if (! $cookie);
  my $req = new HTTP::Request 'POST',"http://custinfo.web2010.com/clm-customer.asp?custname='$domain'";
  $req->header('Cookie' => $cookie);
  $req->content_type('application/x-www-form-urlencoded');
  for ($x=1; $x<=3; $x++) {   #try 3 times
    $res = $ua->request($req);
    next if ($res->content =~ m/Timeout expired/);
    next if ($res->content =~ m/deadlocked with another/);
    next if ($res->is_error);
  }
  return if ($res->is_error);    #error occured
  return if ($res->content =~ m/Either BOF or EOF is True, or the current record has /);    #no such record
  if ($res->content =~ m/<TD><B>Rates:<\/B><\/TD><TD><B>(.*),<\/B><\/TD>/s) {
    $rates = $1;
    @rates = split /,/, $rates;
  }

  $res->content =~ m/<TD><B>Active:<\/B><\/TD><TD><B>(.)<\/B><\/TD>/;
  $active = $1;
  $res->content =~ m/<TD><B>Deac Date:<\/B><\/TD><TD><B>(.*)<\/B><\/TD>/;
  $deactivedate = $1;
  return($active,$deactivedate);
}
sub getcookie {
    $ua = new LWP::UserAgent;
    my $req = new HTTP::Request 'POST','http://custinfo.web2010.com/stafflog.asp';
    $req->content_type('application/x-www-form-urlencoded');
    my $curl = url("http:");
    %form = ( username => 'honeydo',
                        password => 'a0s9d8f7' );
    $curl->query_form(%form);
    $req->content($curl->equery);
    my $res = $ua->request($req);
    $response=$res->as_string;
    $response =~ m/Cookie: (.*)/;
    $cookie = $1;
    if ($response !~ m'<body><h1>Object Moved</h1>This object may be found <a HREF="staffmenu.asp">here</a>.</bo
dy>') {
      $lookupfailed = 1;
      return;
    }
}