#!/bin/sh
SRV_INSTALL_DIR=/opt/safenet_sentinel/common_files/sentinel_keys_server
#########################################################################
#            Copyright (C) 2006 SafeNet, Inc.            		#
#                   All Rights Reserved                  		#
#                                                        		#
# loadserv Start/Stop the Sentinel Keys Server daemon version 1.0.2.   	#
# Status - shows the Sentinel Keys Server status.        		#
#                                                        		#
#                                                        		#
#                                                        		#
#########################################################################
#!/bin/sh
#Function Library
OS="other"

if [ -f /etc/SuSE-release ]
then
OS="SuSE"
fi
if [ "$OS" == "SuSE" ]
then
        . /etc/rc.status
else
        #Function Library
        . /etc/init.d/functions
fi

RETVAL=0

startFirewall()
{
######################### Firewall implementation#############################################
#
#  You can change the default socket ports used by the Sentinel Keys Server (7001) and Sentinel
#  Keys License Monitor (7002) using the server configuration file (sntlconfigsrvr.xml).
#
#  If you specify any other port for Sentinel Keys Server in the server configuration file, then 
#  set the same port in the client configuration file (sntlconfig.xml).
#
#  However, after such customizations, you will need to modify the default entries in the loadserv
#  file. Else you can set the system Firewall settings manually.
##################################################################################################
                redhat=":RH-Firewall-1-INPUT"
                iptables-save >/tmp/firewall 2>&1
                if [ -f /etc/redhat-release ]
                then
                        grep $redhat /tmp/firewall >/dev/null 2>&1

                        if [ $? -eq 0 ]
                        then
                                iptables -I INPUT 1 -p udp --dport 7001 -j ACCEPT > /dev/null 2>&1
                                iptables -I INPUT 2 -p udp --dport 7002 -j ACCEPT > /dev/null 2>&1
                                iptables -I INPUT 3 -p tcp --dport 7001 -j ACCEPT > /dev/null 2>&1
                                iptables -I INPUT 4 -p tcp --dport 7002 -j ACCEPT > /dev/null 2>&1
                        fi
                fi
                        rm -f /tmp/firwall >/dev/null 2>&1

}


# A function to stop a program.
killServer() {
	RC=0
	# Test syntax.
	if [ $# = 0 ]; then
		echo $"Usage: killServer {program} [signal]"
		return 1
	fi

        # Save basename.
        base=${1##*/}

        # Find pid.
	pid=`pidofproc $1`
        
        # Kill it.
        if [ -n "${pid:-}" ] ; then
                [ $BOOTUP = "verbose" ] && echo -n "$base "
        	if checkpid $pid 2>&1; then
			kill -TERM $pid
			usleep 100000
		fi
		checkpid $pid
		RC=$?
		[ $RC -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
		RC=$((! $RC))
	else
	    	failure $"$base shutdown"
	    	RC=1
	fi
	return $RC
}


startServer() {
        #echo -n $"Starting Sentinel Keys Server:"

        cd $SRV_INSTALL_DIR
        if [ "$OS" == "SuSE" ] ; then
                #checkproc $SRV_INSTALL_DIR/SntlKeysSrvrlnx
                #rc_status -v > /dev/null 2>&1
                sh $SRV_INSTALL_DIR/loadserv status | grep running > /dev/null 2>&1
                if [ $? -eq 0 ]
                then
                        echo "SntlKeysSrvrlnx already running!!"
                else
                        echo -n $"Starting Sentinel Keys Server:"
                        ./SntlKeysSrvrlnx $Param
			startFirewall > /dev/null 2>&1
                        rc_status -v
                fi
        else
                 var=`status SntlKeysSrvrlnx | grep running`

                 if [ $? -ne 0 ]
                 then
                         echo -n $"Starting Sentinel Keys Server:"
                         daemon ./SntlKeysSrvrlnx $Param
			 startFirewall > /dev/null 2>&1
                 else
                         echo "SntlKeysSrvrlnx already running!!"
                 fi
                RETVAL=$?
                echo
                [ $RETVAL -eq 0 ]
                return $RETVAL
        fi
}


serverStatus(){

  cat /etc/issue |grep -i SUSE > /dev/null
  if [ $? -eq 0 ]
  then
  	checkproc $SRV_INSTALL_DIR/SntlKeysSrvrlnx
  	rc_status -v
  else
	status SntlKeysSrvrlnx
  fi
}

stop() {
	echo -n $"Stopping Sentinel Keys Server:"
	if [ $OS == "SuSE" ] ; then

		killproc SntlKeysSrvrlnx
	        rc_status -v
	else
		killproc SntlKeysSrvrlnx
        	RETVAL=$?
		iptables -D INPUT -p udp --dport 7001 -j ACCEPT > /dev/null 2>&1
		iptables -D INPUT -p udp --dport 7002 -j ACCEPT > /dev/null 2>&1
		iptables -D INPUT -p tcp --dport 7001 -j ACCEPT > /dev/null 2>&1
		iptables -D INPUT -p tcp --dport 7002 -j ACCEPT > /dev/null 2>&1
	        echo
        	[ $RETVAL -eq 0 ]
	        return $RETVAL
	fi
        
}

user_id=`id -u`
if [ $user_id -ne 0 ]
then
        echo "Permission denied (You must be root). "
fi

Param=""
Space=" "
for i in $*
do
  if test $i != start
  then
	Param=$Param$Space$i
  fi
done
 
case "$1" in
  start)
	startServer $Param
	;;
  stop)
	stop
	;;
  status)
	serverStatus 
	;;
   restart)
        stop
	sleep 5
        startServer $Param
        ;;
  *)
	echo $"Usage: $0 {start|stop|status|restart}"
        RETVAL=1

esac

cat /etc/issue |grep -i SUSE >/dev/null
if [ $? -eq 0 ]
then
rc_exit
fi
exit $RETVAL


