# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/sbin/snappd/scripts/IPDoSetEthernet.pl 1.5 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2000,2002 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#
#!/usr/bin/perl -T
# @(#)92        1.5  src/bos/usr/sbin/snappd/scripts/IPDoSetEthernet.pl, snapp, bos720 5/31/02 07:50:03

#------------------------------------------------------------------------------
#--------Ethernet Interface Modification Page for Snapp Program	---------------
#------------------------------------------------------------------------------
#--------This page is used to execute the command to configure	---------------
#--------the selected interface			-------------------------------
#------------------------------------------------------------------------------
#--------Links to Pages: IPViewMessage.pl and/or 	-----------------------
#--------IPSetEthernet.pl or IPDoneSetEthernet.pl------------------------------
#------------------------------------------------------------------------------

$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin';

#----- get the user entered data from the command line

%hshObjectTags=@ARGV;


#-----------------------------------------------------------------------------
#--*Note*-> Minimal Error Checking Done on IP Addresses and also on the Command
#----- 		itself  -display of erros is possible on limited scale
#-----------------------------------------------------------------------------

#	getting values is like this -->
# 1. get value
# 2. remove any newline characters
# 3. remove any spaces
# 4. generate the flag to be used in the command
# 5. Any error checking if applicable

#-----------------	Hostname	------------------------------
$strHostname = $hshObjectTags{"Hostname"};
$strHostname =~ s/\n//g;
$strHostname =~ s/\s+//g;
$strFlagHostname = join "", "-h'", $strHostname, "'";

#-----------------	IP Address	------------------------------
$error = 0;
$strIPAddress = $hshObjectTags{"IPAddress"};
$strIPAddress =~ s/\n//g;
$strIPAddress =~ s/\s+//g;
$strFlagIPAddress = join "", "-a'", $strIPAddress, "'";
&ErrorCheck($strIPAddress);
if($error == 1)	{
	$strMessage = "Invalid IP Address\n";
	$error = 0;
	$wasError = 1;
}

#-----------------	Netmask		------------------------------
$strNetmask = $hshObjectTags{"Netmask"};
$strNetmask =~ s/\n//g;
$strNetmask =~ s/\s+//g;
$strFlagNetmask = join "", "-m'", $strNetmask, "'";
&ErrorCheck($strNetmask);
if($error == 1)	{
	$strMessage = join '', $strMessage, "Invalid Netmask\n";
	$error = 0;
	$wasError = 1;
}

#-----------------	Gateway		------------------------------
$strGateway = $hshObjectTags{"Gateway"};
$strGateway =~ s/\n//g;
$strGateway =~ s/\s+//g;
$strFlagGateway = "";
if($strGateway ne "")	{
	$strFlagGateway = join "", "-g'", $strGateway, "'";
	&ErrorCheck($strGateway);
	if($error == 1)	{
		$strMessage = join '', $strMessage, "Invalid Gateway Address\n";
		$error = 0;
		$wasError = 1;
	}
}

#-----------------	Domain and Nameserver	------------------------------
$strDomain = $hshObjectTags{"Domain"};
$strDomain =~ s/\n//g;
$strDomain =~ s/\s+//g;
$strFlagDomain = "";

$strNameserver = $hshObjectTags{"Nameserver"};
$strNameserver =~ s/\n//g;
$strNameserver =~ s/\s+//g;
$strFlagNameserver = "";

if(($strDomain ne "") && ($strNameserver ne ""))	{
	$strFlagDomain = join "", "-d'", $strDomain, "'";
	$strFlagNameserver = join "", "-n'", $strNameserver, "'";
	&ErrorCheck($strNameserver);
	if($error == 1)	{
		$strMessage = join '', $strMessage, "Invalid Nameserver Address\n";
		$error = 0;
		$wasError = 1;
	}
}
elsif(($strDomain ne "") || ($strNameserver ne ""))	{
	$strMessage = join '', $strMessage, "Both the Nameserver and  the Domain have to be defined\n";
	$wasError = 1;
}

#-----------------	Interface Selected	------------------------------
open(ENFILE, "/var/snapp/snapp.eni");
$en = <ENFILE>;
close(ENFILE);

#------		Check to see if the tcpip Daemons are running, if they're not start them at command	------
if($argdf = `/usr/bin/lssrc -g tcpip | /usr/bin/grep active`)	{
	$startNowFlag = "";
}
else	{
	$startNowFlag = "-s";
}

if($wasError != 1)	{
	#-------------run command
	$errorChecking = `/usr/sbin/mktcpip $strFlagHostname $strFlagIPAddress $strFlagNetmask -i'$en' $strFlagNameserver $strFlagDomain $strFlagGateway $startNowFlag\n`;

	#------- check to see if returned value is correct
	if($? == 0)
	{
		$strLabel = "<DY>12</DY> <LABEL>Configuration: SUCCESS</LABEL>";
		$strButton = "<W>30</W> <H>12</H> <X>65</X> <Y>130</Y> <BUTTON Name=\"IPDoneSetEthernet.pl\">OK</BUTTON>";
	}
	else
	{
		#-------- if return isn't, tell user on screen
		open(ENFILE, "/var/snapp/snapp.eni");
		print OUTFILE "$en";
		close(ENFILE);
		$strLabel = "<DY>12</DY> <LABEL>Configuration: FAILED</LABEL>";
		$strButton = "<W>30</W> <H>12</H> <X>10</X> <Y>120</Y> <BUTTON Name=\"IPSetEthernet.pl\">OK</BUTTON> <DX>35</DX> <W>65</W> <BUTTON Name=\"IPViewMessage.pl\">View Results</BUTTON>";
	}

	$errorChecking =~ s/\n/,/g;
	#-----save return value in case user wants to view it
	open(OUTFILE,">/var/snapp/snapp.message");
	print OUTFILE "$errorChecking";
	close (OUTFILE);

	#generate page, this page also includes a link to view the message that was returned
	open(OUTFILE,">/var/snapp/snapp.fres.tmp");
	print OUTFILE <<EOXML;

<PAGE>
  <TITLE>Ethernet Configuration:</TITLE>

<W>150</W>
<H>12</H>
<X>5</X>
<Y>30</Y>
$strLabel
$strButton

</PAGE>

EOXML


close(OUTFILE);
system("/usr/bin/cp /var/snapp/snapp.fres.tmp /var/snapp/snapp.fres");
}
else	{
	#-------this page is printed in case the information entered doesn't pass the loose error checking
	#-------an option to try again is displayed
	
	open(OUTFILE,">/var/snapp/snapp.fres.tmp");
	print OUTFILE <<EOXML;

<PAGE>
<TITLE>Error With Information:</TITLE>

<W>150</W>
<H>12</H>
<X>5</X>
<Y>30</Y>
<LABEL>The Following Problems Occurred:</LABEL>

<W>150</W>
<DY>20</DY>
<H>100</H>
<LABEL>$strMessage</LABEL>

<W>60</W>
<H>12</H>
<X>50</X>
<DY>50</DY>
<BUTTON Name="IPSetEthernet.pl">OK</BUTTON>

</PAGE>

EOXML


close(OUTFILE);
system("/usr/bin/cp /var/snapp/snapp.fres.tmp /var/snapp/snapp.fres");

}

#-------------------------------------------------------------
# Do Error Checking on the inputed data
# Only the address fields are being tested for now
#-------------------------------------------------------------
sub ErrorCheck
{
  my($strIP) = @_;
 

  @nums = split(/\./, $strIP);
  $length = @nums;
  if ($length != 4) {
    $error = 1;
  }
  foreach $_ (@nums) {
    if ((length($_) > 3) or (length($_) < 1)) {
      $error = 1;
      last;
    }
    if (/[^0-9]/) {
      $error = 1;
      last;
    }
  }
}