All pastes #790696 Raw Edit

hostmanager.pl

public text v1 · immutable
·published 2007-11-20 15:32 UTC
rendered paste body
#!/usr/bin/perl

use Switch;
use File::Copy;

# Configuration
###################
$host_path = "/server/hosts";
$host_conf = "/server/httpd/conf/hosts.conf";
$owner = "apache";
$httpd_path = "/server/httpd/bin";
$httpd_opt = "-DDAV -DSecurity";

# Main Program
###################
sub header{
	if(unixcompatible()){
		system('clear');
	}else{
		system('cls');
	}
	print "#####################################\n";
	print "#      Virtual Host Manager         #\n";
	print "#      By Jorge Schrauwen 2006      #\n";
	print "#####################################\n";
}

sub unixcompatible{
	$UNIX = `perl -v`;
	if($UNIX=~m/(darwin|(li|u)n(i|u)x|\s*bsd\s)/i){
		return 1;
	}else{
		return 0;
	}
}

sub main{
	print "1) Create Virtual Host\n";
	print "2) Remove Virtual Host\n";
	print "3) Change Virtual Host\n";
	print "4) List Virtual Hosts\n\n";
	print "0) Exit\n\n";
	print "Select: ";
	chomp($opt = <STDIN>);
	switch  ($opt){
		case 1 { mkhost(); }
		case 2 { rmhost(); }
		case 3 { chhost(); }
		case 4 { listhost(); }
		case 0 { }
		else { main(); }
	}
}

sub mkhost{
	header();
	# get data
	print "Create Host: ";
	chomp($fqdn = <STDIN>);
	print "DAV Username: ";
	chomp($davuser = <STDIN>);
	print "DAV Password: ";
	chomp($davpass = <STDIN>);
	header();
	if(!-d "$host_path/$fqdn"){
		print "Creating directories...\n";
		mkdir("$host_path/$fqdn", 0755);
		mkdir("$host_path/$fqdn/_server", 0755);
		mkdir("$host_path/$fqdn/_server/logs", 0755);
		mkdir("$host_path/$fqdn/_server/ssl", 0755);
		mkdir("$host_path/$fqdn/_server/tmp", 0755); #for php temp directory
		mkdir("$host_path/$fqdn/_server/sessions", 0755); #for php sessions directory
		mkdir("$host_path/$fqdn/httpdocs", 0755);
		#mkdir("$host_path/$fqdn/httpsdocs", 0755);
		mkdir("$host_path/$fqdn/cgi-bin", 0755);
		print "Creating users.db...\n";
		htpasswd('add', $davuser, $davpass, "$host_path/$fqdn/_server/users.db");
		print "Updating configuration files...\n";
		configure(1, 1, 1, "$host_path/$fqdn/_server/host_settings.conf");
		updatehost(1, $fqdn);
		if(unixcompatible()){
			 system("sudo chown -R $owner:$owner $host_path/$fqdn");
			 system("sudo chmod -R 0775 $host_path/$fqdn");
		}
		print "Restarting httpd server...\n";
		httpdmanage("restart");
		$msg = "Successfully created $fqdn!\n\n";
	}else{
		$msg = "$fqdn allready exists!\n\n";
	}
	header();
	print $msg;
	main();
}

sub rmhost{
	header();
	print "Remove Host: ";
	chomp($fqdn = <STDIN>);
	
	if(-d "$host_path/$fqdn"){
		print "Remove $fqdn and all its files? (y/N): ";
		chomp($confirm = <STDIN>);
		if(lc($confirm) eq "y"){
			$confirm = 1;
		}else{
			$confirm = 0;
		}
		if($confirm){
			print "Updating configuration files...\n";
			updatehost(0, $fqdn);
			print "Restarting httpd server...\n";
			httpdmanage("restart");
			print "Removing directories...\n";
			rmdir_r("$host_path/$fqdn");
			$msg = "Successfully removed $fqdn!\n\n";
		}else{
			$msg = "$fqdn was not removed!\n\n";
		}
	}else{
		$msg = "$fqdn doesn't exists!\n\n";
	}
	header();
	print $msg;
	main();
}

sub chhost{
	header();
	print "Host Domain: ";
	chomp($fqdn = <STDIN>);
	if(-d "$host_path/$fqdn"){
		chhost_menu($fqdn);
	}else{
		header();
		print "$fqdn doesn't exists!\n\n";
		main();
	}
	
}

sub chhost_menu{
	$fqdn=$_[0];
	header();
	print "1) Update Configuration\n";
	print "2) Manage DAV Users\n\n";
	print "0) Back\n\n";
	print "Select: ";
	chomp($opt = <STDIN>);
	switch  ($opt){
		case 1 { chhost_config($fqdn); }
		case 2 { chhost_dav_menu($fqdn); }
		case 0 { header(); main(); }
		else { chhost_menu($fqdn); }
	}
}

sub chhost_config{
	$fqdn=$_[0];
	header();
	# get data
	print "Enable PHP (Y/n): ";
	chomp($php = <STDIN>);
	if(lc($php) eq "n"){
		$php = 0;
	}else{
		$php = 1;
	}
	print "Enable Perl (Y/n): ";
	chomp($perl = <STDIN>);
	if(lc($perl) eq "n"){
		$perl = 0;
	}else{
		$perl = 1;
	}
	print "Enable SSI (Y/n): ";
	chomp($ssi = <STDIN>);
	if(lc($ssi) eq "n"){
		$ssi = 0;
	}else{
		$ssi = 1;
	}
	header();
	print "Updating configuration files...\n";
	$msg = configure($php, $perl, $ssi, "$host_path/$fqdn/_server/host_settings.conf");
	print "Restarting httpd server...\n";
	httpdmanage("restart");
	header();
	if($msg == 1){
		print "$fqdn has been updated!\n\n";
	}else{
		print "$msg\n\n";
	}
	main();
}

sub chhost_dav_menu{
	$fqdn=$_[0];
	header();
	print "1) Add/Update User\n";
	print "2) Remove User\n";
	print "3) List Users\n\n";
	print "0) Back\n\n";
	print "Select: ";
	chomp($opt = <STDIN>);
	switch  ($opt){
		case 1 { dav_add($fqdn); }
		case 2 { dav_remove($fqdn); }
		case 3 { dav_list($fqdn); }
		case 0 { chhost_menu($fqdn); }
		else { chhost_dav_menu($fqdn); }
	}
}

sub dav_add{
	$fqdn=$_[0];
	header();
	print "Username: ";
	chomp($davuser = <STDIN>);
	print "Password: ";
	chomp($davpass = <STDIN>);
	htpasswd('add', $davuser, $davpass, "$host_path/$fqdn/_server/users.db");
	chhost_dav_menu($fqdn);
}

sub dav_remove{
	$fqdn=$_[0];
	header();
	print "Username: ";
	chomp($davuser = <STDIN>);
	htpasswd('rm', $davuser, '', "$host_path/$fqdn/_server/users.db");
	chhost_dav_menu($fqdn);
}

sub dav_list{
	$fqdn=$_[0];
	header();
	open (USERFILE, "$host_path/$fqdn/_server/users.db");
	$i = 0;
	while ($uline = <USERFILE>) {
		$i++;
		@user = split(':', $uline);
		print "$i. @user[0]\n";
	}
	close(USERFILE);
	print "\nManage DAV Users>";
	<STDIN>;
	chhost_dav_menu($fqdn);
}

sub listhost{
	$fqdn=$_[0];
	header();
    opendir(HDIR, $host_path);
	$i = 0;
    while (my $file = readdir(HDIR)) {
		if(($file ne '.') and ($file ne '..')){
			if(-d "$host_path/$file"){
				$i++;
				print "$i. $file\n";
			}
		}
    }
    closedir(HDIR);
	print "\nManage Hosts>";
	<STDIN>;
	header();
	main();
}

sub configure{
	# get variables
	$php=$_[0];
	$perl=$_[1];
	$ssi=$_[2];
	$conf_path=$_[3];
	# gen host_settings
	open(HCONF,">$conf_path") or return 'Failed to open configuration file!';
	print HCONF "# Host Configuration\n";
	if($ssi){
		print HCONF "Use EnableSSI\n";
	}
	if($perl){
		print HCONF "Use EnableCGI\n";
	}
	if($php){
		print HCONF "Use EnablePHP\n";
	}
	close(HCONF);
}

sub updatehost{
	# get variables
	$action=$_[0];
	$host=$_[1];
	# edit hosts.conf
	open (HOSTFILE, $host_conf) or die('Failed to open host file!');
	open (HOSTFILE_NEW,">".$host_conf."_new");
	if($action){
		while ($hline = <HOSTFILE>) {
			print HOSTFILE_NEW $hline;
		}
		print HOSTFILE_NEW "Use VirtualHost $host\n";
	}else{
		while ($hline = <HOSTFILE>) {
			@chunks = split(' ', $hline);
			if(lc(@chunks[0]) ne 'use'){
				print HOSTFILE_NEW $hline;
			}else{
				$chost = lc(@chunks[2]);
				chomp($chost);
				if($chost ne $host){
					print HOSTFILE_NEW $hline;
				}
			}
		}
	}
	close(HOSTFILE);
	close(HOSTFILE_NEW);
	unlink("$host_conf");
	move($host_conf."_new", "$host_conf");
}

sub htpasswd{
	# get variables
	$action=lc($_[0]);
	$user=$_[1];
	$pass=$_[2];
	$htpassfile=$_[3];
	# create users.db
	$htpasswdcmd = "$httpd_path/htpasswd -";
	if(!-e $htpassfile){ $htpasswdcmd .= "c"; }
	if($action eq 'add'){
		$htpasswdcmd .= "sb ".$htpassfile." ".$user." ".$pass;
	}
	if($action eq 'rm'){
		$htpasswdcmd .= "D ".$htpassfile." ".$user;
	}
	system($htpasswdcmd);
}

sub httpdmanage{
	if(-e "$httpd_path/apachectl"){
		$httpdcmd = "sudo $httpd_path/apachectl $httpd_opt -k ";
	}else{
		$httpdcmd = "$httpd_path/httpd $httpd_opt -k ";
	}
	$httpdcmd .= $_[0];
	system($httpdcmd);
}

sub rmdir_r{
    my $dir = shift;
	local *DIR;

	opendir DIR, $dir or die "opendir $dir: $!";
	my $found = 0;
	while ($_ = readdir DIR) {
	        next if /^\.{1,2}$/;
	        my $path = "$dir/$_";
		unlink $path if -f $path;
		rmdir_r($path) if -d $path;
	}
	closedir DIR;
	rmdir $dir or print "error - $!";
}

# init
###################
header();
main();