#!/usr/bin/perl
# cpanel4 - scripts        Copyright(c) 1997-2002 cPanel, Inc..
#                                 All rights Reserved.
# copyright@cpanel.net      http://cpanel.net
# This code is subject to the cpanel license. Unauthorized copying is prohibited
print "Scanning httpd.conf.";

BEGIN {
   push(@INC,"/scripts");
}

use SafeFile;

my(%USERS);
open(UD,"/etc/userdomains");
while(<UD>) {
        s/[\s\n\r]*//g;
        my($domain,$user) = split(/:/, $_);
        $USERS{$domain} = $user;
}
close(UD);
                                                                                                                                      


my $httpconf = find_httpconf();

open(HTTPDCONF,"$httpconf");
$xu=0;
while(<HTTPDCONF>) {
   s/\>//g;

   if ($_ !~ /#/) {
      if (/\<virtualhost (\S+)($|\s)/i) {
         print ".";
         $mvh = $1;
         $vh = 1;
         $xu++;
      }
      if ($vh == 1) {
         if (/^jkmount/i) {
            $JK{$xu} = 1;
         }
         if (/^WebApp/i) {
            $JK{$xu} = 1;
         }
         if (/servername (\S+)($|\s)/i) {
#            print ".";
            $sn = $1;
            $SN{$xu} = $1;
         }
         if (/documentroot (\S+)/i) {
#            print "X";
            $DR{$xu} = $1;
         }
      }
      if (/\<\/virtualhost (\S+)($|\s)/i) {
         print ".";
         $vh = 0;
      }
   }
}
close(HTTPDCONF);
print "\n";
foreach $server (sort keys %JK) {
   $s1 = $SN{$server};
   $s2 = $s1;
   $s2 =~ s/^www\.//g;
   next if ($s2 eq "" || $s1 eq "");

   if ($DR{$server} ne "") {
      $nhosts{$s1} = $DR{$server};
   }
   if ("$s2" ne "$s1") {
      $nhosts{$s2} = $DR{$server};
   }
}


foreach my $nhost (sort keys %nhosts) {
   my $docbase = $nhosts{$nhost};
   my $tdoc = $docbase;
   $tdoc =~ s/public_html\/$//g;
   $tdoc =~ s/public_html$//g;
   $tdoc .= "webapps";
   if (-d $tdoc) {
      $docbase = $tdoc;
   }
   
   $entries = $entries . <<EOM;
       <Host name="$nhost" appBase="$docbase">
          <Context path="" reloadable="true" docBase="$docbase" debug="1"/>
          <Context path="/manager" debug="0" privileged="true"
              docBase="/usr/local/jakarta/tomcat/server/webapps/manager">
          </Context>
       </Host>
EOM

      if ($USERS{$nhost} ne "") {

         if (my $pid = fork()) {
#master
         } else {
            setuids($USERS{$nhost});
            if (! -e "${docbase}/WEB-INF") {
               mkdir("${docbase}/WEB-INF",0755);
            }
            if (! -e "${docbase}/WEB-INF/classes") {
               mkdir("${docbase}/WEB-INF/classes",0755);
            }
            if (! -e "${docbase}/WEB-INF/lib") {
               mkdir("${docbase}/WEB-INF/lib",0755);
            }
            if (! -e "${docbase}/WEB-INF/classes/ServletsTestPage.class" &&
                  -e "/usr/local/jakarta/tomcat/samples/ServletsTestPage.class") {
               system("cp","-f","/usr/local/jakarta/tomcat/samples/ServletsTestPage.class",
                     "${docbase}/WEB-INF/classes/ServletsTestPage.class");
            }
            if (! -e "${docbase}/jsptest.jsp" &&
                  -e "/usr/local/jakarta/tomcat/samples/jsptest.jsp") {
               system("cp","-f","/usr/local/jakarta/tomcat/samples/jsptest.jsp",
                     "${docbase}/jsptest.jsp");
            }
            exit();
         }

      }
}

my(@SO);
my $inhost = 0;
my $lasthost = 0;
my $line = 1;
my $solock = SafeFile::safeopen(\*SO,"+<","/usr/local/jakarta/tomcat/conf/server.xml");
while(<SO>) {
   if ( ! $inhost && /<Host/ ) { 
      $inhost = 1;
   }
   if ( $inhost && /<\/Host/) { 
      if ( ! $lasthost ) {
         $lasthost = $line;
         $line++; 
         push(@SO,$_); 
      }
      if ( $lasthost ) {
           $inhost = 0;
           next;
      }
   }
   if ( $lasthost && ! $inhost ) {
       $line++;
       push(@SO,$_);
   } elsif ( $inhost && ! $lasthost ) {
       $line++;
       push(@SO,$_);
   } elsif ( ! $inhost && ! $lasthost  ) {
       $line++;
       push(@SO,$_);
   }
}
seek(SO,0,0);
$line = 0;
foreach (@SO) {
   $line++;
   if ( $line == ($lasthost + 1) ) {
      print SO $entries;
   }
   print SO;
}
truncate(SO,tell(SO));
SafeFile::safeclose(\*SO,$solock);

system("/usr/sbin/stoptomcat");
system("/usr/sbin/starttomcat");

sub find_httpconf {
   my(@LOC,$loc);
   @LOC = ("/usr/local/apache/conf/httpd.conf",
         "/etc/httpd/conf/httpd.conf");
   foreach $loc (@LOC) {
      if (-e $loc) { return $loc; }
   }
   return "";
}

sub setuids {
   my($user) = $_[0];
   my($uid,$gid);
   (undef,undef,$uid,$gid) = getpwnam($user);
   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) [$user]\n";
   }
   return $uid;
}