#!/usr/bin/perl -w
##
## Copyright (c) 2001, 2002, Amnon BARAK (amnon@MOSIX.org).
## All rights reserved.
## This software is subject to the MOSIX SOFTWARE LICENSE AGREEMENT.
##
## THIS SOFTWARE IS PROVIDED IN ITS "AS IS" CONDITION, WITH NO WARRANTY
## WHATSOEVER. NO LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER RESULTING
## FROM THE USE OF THIS SOFTWARE WILL BE ACCEPTED.
##
##
## Author(s): Shlomo Matichin 
##

# This script modifies the xinet daemon services configuration files to unlock
# remote shells in order to enable migration.

# Default arguments currently compatible with all Linux distributions using
# xinet. 
# Note: currently RedHat 7.x is the only distribution that uses xinet.

use FindBin;
use Getopt::Long;
use lib "$FindBin::Bin";
use common;

$files = "telnet ssh login shell exec rexec rlogin rsh";

GetOptions(	"help" ,		\$usage ,
		"uninstall" ,		\$uninstall );

usage() if defined $usage ;
-e "/etc/xinetd.conf" and not -d "/etc/xinetd.d" and
	xinet_only_conf();
-w "/etc/xinetd.d" or
	dead "You do not have write permissions on /etc/xinetd.d";
uninstall() and exit 1 if defined $uninstall ;
-e "$FindBin::Bin/xinet_shell" or
	dead "MOSIX Distribution is not intact: Unable to find $FindBin::Bin/xinet_shell";
install();
exit 1;
    
sub install
{
	foreach $file( split /\ / , $files ) {
		-e "/etc/xinetd.d/$file.pre_mosix" and
			dead "Please Uninstall MOSIX before reinstalling";
	}
	foreach $file( split /\ / , $files ) {
		-e "/etc/xinetd.d/$file" or next;
		system( "cp -fa /etc/xinetd.d/$file /etc/xinetd.d/$file.pre_mosix" ) and
			dead "Failed to backup /etc/xinetd.d/$file";
	}
	system( "$FindBin::Bin/xinet_shell" ) and
		dead "Failed to patch xinetd configuration files";
	exit ;
}

sub usage {
	print "Usage : xinet [OPTION]\n";
	print "--uninstall         uninstall\n";
	print "--help              this message\n";		
	exit ;
}

sub uninstall {
	`grep mosrun /etc/xinetd.d/* 2>&1` =~ /mosrun/ or
		print "\nYour xinet-daemon configuration files (/etc/xinetd.d/*)\n" and
		print "were not previously patched for MOSIX!\n" and
		exit ;
	foreach $file( split /\ / , $files ) {
		if(not -e "/etc/xinetd.d/$file") { next ; }
		if(not -e "/etc/xinetd.d/$file.pre_mosix") {
			print "\nCannot find the Pre-MOSIX backup for /etc/xinetd.d/$file. skipping\n";
			next;
		}
		system( "mv -f /etc/xinetd.d/$file.pre_mosix /etc/xinetd.d/$file" ) and
			dead "Failed to restore the pre-MOSIX back-copy of /etc/xinetd.d/$file";
	}
	exit ;
}

sub xinet_only_conf {
	-w "/etc/xinetd.conf" or
		dead "You do not have write permission on /etc/xinetd.conf";
	uninstall_conf() and exit 1 if defined $uninstall ;
	install_conf();
	exit 1;
}

sub install_conf {
	-e "/etc/xinetd.conf.pre_mosix" and
		dead "Please uninstall MOSIX before resintalling";
	system( "cp -f /etc/xinetd.conf /etc/xinetd.conf.pre_mosix" ) and
		dead "Failed to backup /etc/xinetd.conf";
	open( XINETD_CONF , "</etc/xinetd.conf" ) or
		dead "Failed to open '/etc/xinetd.conf' for reading";
	@conf = <XINETD_CONF> ;
	close( XINETD_CONF );
	$conf = join '' , @conf ;
	@conf = split /\nservice\s/ , $conf ;
	foreach $file( split /\ /, $files ) {
		foreach $conf(@conf) {
			$file eq substr $conf , 0 , length $file or next;
			$conf =~ s/server\s+\=\s+(.*)\n/server\t=\ \/bin\/mosrun\n/ or next;
			$server = $1;
			$conf =~ s/server_args\s+\=\s+(.*)\n/server_args\t=\ -l\ $server\ $1\n/ or
				s/\}/\tserver_args\t=\ -l\ $server\n\}/ ;
		}
	}
	open( XINETD_CONF , ">/etc/xinetd.conf" ) or
		dead "Failed to open '/etc/xinetd.conf' for writing";
	$conf = join "\nservice " , @conf ;
	print XINETD_CONF $conf ;
	exit;
}

sub uninstall_conf {
	`grep mosrun /etc/xinetd.conf 2>&1` =~ /mosrun/ or
		print "\nYour xinet-daemon configuration file (/etc/xinetd.conf)\n" and
		print "was not previously patched for MOSIX!\n" and
		exit ;
	-e "/etc/xinetd.conf.pre_mosix" or 
		dead "\nCannot find the Pre-MOSIX backup for /etc/xinetd.conf\n";
	system( "mv -f /etc/xinetd.conf.pre_mosix /etc/xinetd.conf" ) and
		dead "Failed to restore the pre-MOSIX back-copy of /etc/xinetd.conf";
	exit ;
}
