#!/usr/bin/perl
use Apache::Htpasswd;
use Net::IPAddress;
use DBlib;
use DBI;
# Get the command line parameters...
$domainname = $ARGV[0] || display_usage();
# Parse the domain name for the TLD and the host
($host, $fqdomain) = fqdn($domainname);
if ($fqdomain ne 'com' && $fqdomain ne 'net' && $fqdomain ne 'org')
{
# The domain is NOT just "domain.com"...
# Verify if it is "www.domain.com"...
if ($domainname eq 'www.'.$fqdomain)
{
$domain = $fqdomain;
$host = 'www';
}
else
{
$domain = $fqdomain;
}
}
else
{
$domain = $domainname;
$host = 'www';
}
$fqdomain = $host.'.'.$domain;
print " Domain Information:\\n";
print "=====================\\n";
print " Host: ".$host."\\n";
print " Domain: ".$domain."\\n";
print " FQDN: ".$fqdomain."\\n";
print "\\n";
# Check if the domain name is using our dns servers...
@nsresult = `dig NS $domain +short`;
$ns_count = 0;
foreach $ns_name (@nsresult)
{
print " Nameserver #".($ns_count++).": ".trim($ns_name)."\\n";
}
if (!grep /ns[12]\\.#####\\.com\\./, @nsresult)
{
if (!grep /--force/, @ARGV)
{
print "\\n\\nERROR: The nameservers of $domain are not set up correctly.\\n";
print " The nameservers for $domain should be set to:\\n";
print " ns1.#####.com\\n";
print " ns2.#####.com\\n";
print " If $domain is using different name servers, and you\\n";
print " have properly configured $fqdomain to point to\\n";
print " ###.###.###.### then you can use the --force command line\\n";
print " parameter to skip this check.\\n";
exit;
}
else
{
print "\\n\\n";
print "WARNING: The name servers listed for $domain are incorrect. You\\n";
print " have given the --force command line parameter, doing so\\n";
print " so will allow this script to continue, however success\\n";
print " can not be guaranteed if manual set up is done improperly.\\n";
print "\\n";
print " For manual configuration, please use the following settings:\\n";
print " The nameservers for $domain should be set to:\\n";
print " ns1.#####.com\\n";
print " ns2.#####.com\\n";
print " If $domain is using different name servers, then the IP\\n";
print " address for $fqdomain should be set to 64.131.77.15\\n";
print "\\n";
}
}
# Start by building the actual directory structure for this domain...
# We create the directory first since we want to output a useful info file...
print "\\n";
if (!(-d "/srv/sites/$fqdomain"))
{
mkdir("/srv/sites/$fqdomain");
$dir_create_status = 'Created';
}
else
{
$dir_create_status = 'Exists';
}
# Redirect STDOUT to both screen as well as a file...
open (STDOUT, "| tee /srv/sites/$fqdomain/info.txt") or die "Teeing off: $!\\n";
print " Directory Configuration:\\n";
print "==========================\\n";
print " /srv/sites/$fqdomain\\t\\t";
print $dir_create_status."\\n";
print " /srv/sites/$fqdomain/httpdocs\\t\\t";
if (!(-d "/srv/sites/$fqdomain/httpdocs"))
{
mkdir("/srv/sites/$fqdomain/httpdocs");
print "Created\\n";
}
else
{
print "Exists\\n";
}
print " /srv/sites/$fqdomain/logs\\t\\t";
if (!(-d "/srv/sites/$fqdomain/logs"))
{
mkdir("/srv/sites/$fqdomain/logs");
print "Created\\n";
}
else
{
print "Exists\\n";
}
# $dummy = `touch /srv/sites/$fqdomain/httpdocs/index.html`;
$dummy = `chown -R apache.apache /srv/sites/$fqdomain`;
print "\\n";
print " Apache Configuration:\\n";
print "=======================\\n";
print " Creating vhost record for $fqdomain in /etc/httpd/conf/vhosts/$domain.conf\\n";
open (WRITE, ">>/etc/httpd/conf/vhosts/$domain.conf");
print WRITE <<EOF;
<VirtualHost *:80>
ServerName $fqdomain
ServerAdmin webadmin@#####.com
ServerAlias $domain
DocumentRoot /srv/sites/$fqdomain/httpdocs
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/srv/sites/$fqdomain/httpdocs">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
DirectoryIndex index.php index.html
AccessFileName .htaccess
HostnameLookups Off
ErrorLog "|/usr/local/sbin/cronolog /srv/sites/$fqdomain/logs/%Y/%m/errors.log"
LogLevel warn
LogFormat "%h %l %u %t \\\\"%r\\\\" %>s %b \\\\"%{Referer}i\\\\" \\\\"%{User-Agent}i\\\\"" combined
CustomLog "|/usr/local/sbin/cronolog /srv/sites/$fqdomain/logs/%Y/%m/access.log" combined
ServerSignature Email
</VirtualHost>
EOF
close(WRITE);
print " Create a username and password for this site? [Y/n] ";
chomp ($r = <STDIN>);
if ($r eq '' || $r eq 'Y' || $r eq 'y')
{
print " Username: ";
chomp ($htuser = <STDIN>);
print " Password: ";
chomp ($htpass = <STDIN>);
open (WRITE, ">/srv/sites/$fqdomain/httpdocs/.htaccess");
print WRITE <<EOF;
AuthUserFile /srv/sites/$fqdomain/.htpasswd
AuthName "$fqdomain"
AuthType Basic
Require valid-user
EOF
close(WRITE);
$dummy = `touch /srv/sites/$fqdomain/.htpasswd`;
$htpassfile = new Apache::Htpasswd("/srv/sites/$fqdomain/.htpasswd");
$dummy = ($htpassfile->htpasswd($htuser, $htpass));
$dummy = ($htpassfile->htpasswd($htuser, $htpass, {'overwrite' => 1}));
$dummy = `chown apache.apache /srv/sites/$fqdomain/.htpasswd`;
}
print "\\n";
print " MaraDNS Configuration:\\n";
print "========================\\n";
if (!(-f "/etc/maradns/db.$domain"))
{
print " Creating MaraDNS zone file for $domain\\n";
open (WRITE, ">/etc/maradns/db.$domain");
print WRITE <<EOF;
$domain. NS ns1.#####.com. ~
$domain. NS ns2.#####.com. ~
$domain. A ###.###.###.### ~
$host.$domain. A ###.###.###.### ~
mail.$domain. A ###.###.###.### ~
ftp.$domain. A ###.###.###.### ~
$domain. MX 10 mail.$domain. ~
EOF
close(WRITE);
print " Adding zone to MaraDNS\\n";
open (WRITE, ">>/etc/mararc_primary");
print WRITE "csv2[\\"$domain.\\"] = \\"db.$domain\\"\\n";
close(WRITE);
}
else
{
# The DNS file already exists... first check if this specific entry exists yet...
open (READ, "/etc/maradns/db.$domain");
@contents = <READ>;
close (READ);
if (!grep /$fqdomain/, @contents)
{
open (WRITE, ">/etc/maradns/db.$domain");
foreach $content_line (@contents)
{
$content_line =~ s/\\n//gi;
if ($content_line =~ /^mail.$domain/)
{
print WRITE "$fqdomain.\\tA\\t###.###.###.###\\t~\\n\\n";
}
print WRITE $content_line."\\n";
}
close(WRITE);
print " Added $fqdomain to existing /etc/maradns/db.$domain file.\\n";
}
}
print "\\n";
print " MySQL Configuration:\\n";
print "======================\\n";
&DBlib::openDB("mysql", "localhost", "", "mysql_admin", "#########");
$database = $fqdomain;
$database =~ s/\\./_/gi;
$database =~ s/-//gi;
$sqluser = $domain;
$sqluser =~ s/\\./_/gi;
$sqluser =~ s/-//gi;
$sqluser = 'db_'.substr($domain, 0, 13);
$sqluser = substr($fqdomain, 0, 16);
print " Creating database '$database'...\\n";
$sql_str = "CREATE DATABASE $database;";
&DBlib::execSQL($sql_str);
$mysql_pass = generate_password(8);
print " Granting privileges to $sqluser using generated password '$mysql_pass' on $database...\\n";
$sql_str = "GRANT ALL PRIVILEGES ON $database.* TO '$sqluser'\\@'localhost' IDENTIFIED BY '$mysql_pass';";
&DBlib::execSQL($sql_str);
print " Flushing privileges...\\n";
$sql_str = "FLUSH PRIVILEGES;";
&DBlib::execSQL($sql_str);
print "\\n";
print " SVN Configuration:\\n";
print "====================\\n";
print " Creating SVN Project...\\n";
$dummy = `svnadmin create --fs-type fsfs /srv/svn/$fqdomain`;
$dummy = `chown -R apache /srv/svn/$fqdomain`;
$dummy = `chmod -R go-rwx /srv/svn/$fqdomain`;
print " Generating post-commit script...\\n";
open (WRITE, ">/srv/svn/$fqdomain/hooks/post-commit");
print WRITE "#!/bin/sh\\n";
print WRITE "svn update --username autoexport --password ######### /srv/sites/$fqdomain/httpdocs";
close(WRITE);
$dummy = `chmod +x /srv/svn/$fqdomain/hooks/post-commit`;
$dummy = `chown apache /srv/svn/$fqdomain/hooks/post-commit`;
print " Adding SVN authentication rules...\\n";
open (WRITE, ">>/etc/httpd/conf/svn-auth.ini");
print WRITE "\\n";
print WRITE "[$fqdomain:/]\\n";
print WRITE "\\@projectadmins = rw\\n";
print WRITE "\\@projectmembers = rw\\n";
print WRITE "* = r\\n";
print WRITE "\\n";
close (WRITE);
print "\\n";
print " Trac Configuration:\\n";
print "=====================\\n";
print " Creating Trac Project...\\n";
$dummy = `trac-admin /srv/trac/$fqdomain initenv "$fqdomain" "sqlite:db/trac.db" "svn" "/srv/svn/$fqdomain"`;
$dummy = `chgrp -R apache /srv/trac/$fqdomain`;
$dummy = `chown -R apache /srv/trac/$fqdomain/{attachments,db,log}`;
$dummy = `chmod -R o-rwx /srv/trac/$fqdomain`;
$dummy = `trac-admin /srv/trac/$fqdomain permission remove anonymous`;
$dummy = `trac-admin /srv/trac/$fqdomain permission add authenticated TRAC_ADMIN`;
print "\\n";
print " Server Maintenance:\\n";
print "=====================\\n";
print " Restarting Apache...\\n";
system("service", "httpd", "restart");
$dummy = `svn checkout --username autoexport --password ######### http://www.#####.com/svn/$fqdomain /srv/sites/$fqdomain/httpdocs`;
$dummy = `chown -R apache.apache /srv/sites/$fqdomain/httpdocs`;
print " Reloading MaraDNS...\\n";
system("service", "maradns", "restart");
print " == DONE ==\\n";
close(STDOUT);
exit;
sub display_usage()
{
print "addvhost.pl\\n";
print "\\n";
print "Usage:\\n";
print " ./addvhost.pl [DOMAIN]\\n";
print "\\n";
exit;
}
sub trim($)
{
my $string = shift;
$string =~ s/^\\s+//;
$string =~ s/\\s+$//;
return $string;
}
sub generate_password
{
$length = shift;
$possible = 'abcdefghijkmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
while (length($password) < $length)
{
$password.= substr($possible, (int(rand(length($possible)))), 1);
}
return $password
}