#!/usr/bin/perl
# $Revision: 1.18 $
# Copyright 2010 Teleflora
# 
# Setup File permissions in an RTI directory "tree".
#
use strict;
use warnings;
use Getopt::Long;


my $RTIDIR = "";
my $HELP = 0;
my $VERSION = 0;

GetOptions(
	"help" => \$HELP,
	"version" => \$VERSION,
);


# --help
if($HELP != 0) {
	usage();
	exit(0);
}

# --version
if($VERSION != 0) {
	print("$0 " . '$Revision: 1.18 $' . "\n");
	exit(0);
}

if($#ARGV < 0) {
	usage();
	exit(1);
}



# Make sure a commandline paramter was given.
$RTIDIR = $ARGV[0];
if(! defined $RTIDIR
||  $RTIDIR eq "") {
	usage();
	exit(1);
}


if(! -d "$RTIDIR/bbxd") {
	usage();
	print("Error: \"$RTIDIR\" is not an RTI directory.\n");
	exit(1);
}


# Make sure we are superuser
if($< != 0) {
	print("Error: Need sudo privileges to execute properly.\n");
	exit(2);
}




# "New PABP" permission sets.
exit(assign_pabp_permissions($RTIDIR));
###################################################################
###################################################################
###################################################################



sub usage
{
	print("$0 " . '$Revision: 1.18 $' . "\n");
	print("\n");
	print("Usage:\n");
	print("\t$0 --help #This help.\n");
	print("\t$0 /usr2/bbx\n");
	print("\t$0 \$RTI_DIR\n");
}



sub assign_pabp_permissions
{
	my $destroot = $_[0];
	my $file = "";
	my $thisdir =  "";
	my $thisfile =  "";


	if( (! defined $destroot) || ($destroot eq "") ) {
		return(-1);
	}


	# Here, we need to make sure permissions are correct for the *parents*
	# of "destroot".




	#
	# Default permissions.
	# Our "support" and "admins" group owns / groups the file. We do not want our "non-privileged" user being able to write these files.
	# Note that we want to skip setting defaults for some known files/diectories. If this script is executed on a running system
	# we could break software when we "flip" file permissions from writable, to readable, back to writable. See tracker 104357.
	#
	opendir(DIR, "$destroot");
	foreach $thisdir (readdir(DIR)) {
		#/usr2/bbx/thisdir
		next if($thisdir =~ /\.$/); # Non-admin writable directory
		next if($thisdir =~ /\.\.$/); # A must have "skip".
		next if($thisdir =~ /log$/); # Non-admin writable directory
		next if($thisdir =~ /bbxd$/); # Non-admin writable directory
		next if($thisdir =~ /bbxtmp$/); # Non-admin writable directory
		next if($thisdir =~ /kiosk$/); # Non-admin writable directory

		# Don't know what this file is, so, set default owner/group/perms.
		system("chown -R tfsupport:rtiadmins $destroot/$thisdir");
		system("chmod -R 555 $destroot/$thisdir");
		system ("chcon -R --reference=/usr/local/share $destroot/$thisdir");
	}
	closedir(DIR);


	# Executable directories
	foreach $thisdir("bin") {
		if(! -d "$destroot/$thisdir") {
			print("Warning! \"$thisdir\" directory not found!\n");
		} else {

			system("chown -R tfsupport:rtiadmins $destroot");
			system("chmod -R 555 $destroot/$thisdir");
			system ("chcon --reference=/usr/local/bin $destroot/$thisdir/*");
		}
	}



	# Non-admin Writable directories
	foreach $thisdir("bbxd", "bbxpf", "bbxtmp", "qb", "delivery", "dove", "kiosk", "log", "fax", "dict", "html", "html/custom", "bbxreports", "odbc_dict", "bbxps" ) {
		if(! -d "$destroot/$thisdir" && ! -f "$destroot/$thisdir" ) {
			print("Warning! \"$thisdir\" directory not found!\n");
		} else {
			system("chown -R rti:rti $destroot/$thisdir");
			system("chmod -R 775 $destroot/$thisdir");
		}
	}


	# Administratively Writable items.
	foreach $thisdir("config", "bbxd/RTI.ini", "bbxd/HSOP01", "bbxd/ONRO01", "bbxd/ONKO01", "bbxd/bytefile", "bbxd/CCXF02") {
		if(! -d "$destroot/$thisdir" && ! -f "$destroot/$thisdir") {
			print("Warning! \"$thisdir\" directory not found!\n");
		} else {
			system("chown -R tfsupport:rtiadmins $destroot/$thisdir");
			system("chmod -R 775 $destroot/$thisdir");
		}
	}


	# Fax Related Executables are in the "fax" directory.
	foreach $thisfile("fax/efax_linux", "fax/hp2pbm_linux", "fax/rti_sendfax.pl") {
		system("chown tfsupport:rtiadmins $destroot/$thisfile");
		system("chmod 555 $destroot/$thisfile");
		system ("chcon --reference=/usr/local/bin $destroot/$thisfile");
	}

	# INI related config files that non-admin should be able to write to prevent error 12's.
	foreach $thisfile("config/aqscan.ini", "config/dove.ini", "config/fax.ini", "config/kiosk.ini") {
		system("chown rti:rti $destroot/$thisfile");
	}


	# Delvconf directory needs to be writable to RTI users.
	system("chown tfsupport:rti $destroot/delvconf");
	system("chmod 555 $destroot/bin/delvconf");
	system("chmod 664 $destroot/delvconf");

	# Fetchmail is apparently picky about it's ownership and permissions.
	system("chown root:rtiadmins $destroot/config/fetchmail.rc");
	system("chmod 600 $destroot/config/fetchmail.rc");

	if(-d "/usr2/OSAS") {
		assignOSAS();
	}

	return 0;
}

sub assignOSAS
{

	system("chmod 755 /usr2/OSAS 2>/dev/null");
	system("chown tfsupport:rtiadmins /usr2/OSAS 2>/dev/null");
 
	system("chmod 775 /usr2/OSAS/data 2>/dev/null");
	system("chmod 775 /usr2/OSAS/data/* 2>/dev/null");
	system("chmod 775 /usr2/OSAS/Document 2>/dev/null");
	system("chmod 664 /usr2/OSAS/Document/* 2>/dev/null");
	system("chmod 555 /usr2/OSAS/gui 2>/dev/null");
	system("chmod 555 /usr2/OSAS/gui/* 2>/dev/null");
	system("chmod 555 /usr2/OSAS/prog?? 2>/dev/null");
	system("chmod 555 /usr2/OSAS/prog??/* 2>/dev/null");
	system("chmod 775 /usr2/OSAS/RWdata 2>/dev/null");
	system("chmod 775 /usr2/OSAS/RWdata/* 2>/dev/null");
	system("chmod 775 /usr2/OSAS/sample 2>/dev/null");
	system("chmod 775 /usr2/OSAS/sample/* 2>/dev/null");
	system("chmod 775 /usr2/OSAS/sysfil 2>/dev/null");
	system("chmod 775 /usr2/OSAS/sysfil/* 2>/dev/null");
	system("chown rti:rti /usr2/OSAS/data 2>/dev/null");
	system("chown rti:rti /usr2/OSAS/data/* 2>/dev/null");
	system("chown tfsupport:rtiadmins /usr2/OSAS/Document 2>/dev/null");
	system("chown rti:rti /usr2/OSAS/Document/* 2>/dev/null");
	system("chown tfsupport:rtiadmins /usr2/OSAS/gui 2>/dev/null");
	system("chown tfsupport:rtiadmins /usr2/OSAS/gui/* 2>/dev/null");
	system("chown tfsupport:rtiadmins /usr2/OSAS/prog?? 2>/dev/null");
	system("chown tfsupport:rtiadmins /usr2/OSAS/prog??/* 2>/dev/null");
	system("chown rti:rti /usr2/OSAS/RWdata 2>/dev/null");
	system("chown rti:rti /usr2/OSAS/RWdata/* 2>/dev/null");
	system("chown rti:rti /usr2/OSAS/sample 2>/dev/null");
	system("chown rti:rti /usr2/OSAS/sample/* 2>/dev/null");
	system("chown rti:rti /usr2/OSAS/sysfil 2>/dev/null");
	system("chown rti:rti /usr2/OSAS/sysfil/* 2>/dev/null");
 
}
