#!/bin/sh
PATH=$PATH:/usr/local/bin:/usr/bin:
#######################################################
# 06/04/2012
# Autor : Guillaume PRIOU
#Contact : guillaume.priou@ocsinventory-ng.org
# Plugin : Retrieve Security status
######################################################
#CAUTION : MAKE SURE THAT NO OTHER PLUGIN IS INSTALLED
######################################################
#DEFINE VARIABLE
######################################################
# Where is Apache daemon binary (if empty, will try to find it)
APACHE_BIN=""
# Where is Apache configuration file (if empty, will try to find it)
APACHE_CONFIG_FILE=""
# Where is Apache includes configuration directory (if emty, will try to find it)
APACHE_CONFIG_DIRECTORY=""
# Which user is running Apache web server (if empty, will try to find it)
APACHE_USER=""
# Which group is running Apache web server (if empty, will try to find it)
APACHE_GROUP=""
# Where is Apache document root directory (if empty, will try to find it)
APACHE_ROOT_DOCUMENT=""
# Which version of mod_perl is apache using,  1 for <= 1.999_21 and 2 for >= 1.999_22 (if empty, user will be asked for)
APACHE_MOD_PERL_VERSION=""
# Which host run database server
DB_SERVER_HOST="localhost"
# On which port run database server
DB_SERVER_PORT="3306"
# Database server credentials
DB_SERVER_USER="ocs"
DB_SERVER_PWD="ocs"
DB_NAME="ocsweb"
# Where is located perl interpreter
PERL_BIN=`which perl 2>/dev/null`
 # Where is locate command
LOCATE_BIN=`which locate 2>/dev/null`
# Where is updatedb command
UPDATEDB_BIN=`which updatedb 2>/dev/null`
# Administration console directory
ADM_SERVER_STATIC_DIR="/usr/share/ocsinventory-reports"
#Curent directory
SETUP_PATH=`pwd`
# Where to store setup logs
SETUP_LOG=`pwd`/plugin_ocs_security_center.log
#Where we are
CURRENT_PATH=`pwd`
echo "==============================================================================="
echo "| CAUTION: You will install a plugin witch modify the OCS Inventory NG engine.|"
echo "| This plugin is ONLY supported by OCS Inventory NG Server 2.0.               |"
echo "| Make sure that no other plugin is installed                                 |"
echo "| This script overwrite the Map.pm and cd_config.txt files                    |"
echo "| BE CAREFULL : Script will automatically restart apache.                     |"
echo "| REQUIEREMENTS : locate and updatedb MUST be installed on system.            |"
echo "==============================================================================="
echo
echo -n "Do you wish to continue ([y]/n)?"
read ligne
if [ -z "$ligne" ] || [ "$ligne" = "y" ] || [ "$ligne" = "Y" ]
then
    echo "Assuming Communication server 2.0 is installed"
    echo "on this computer."
    echo
else
    echo "Installation aborted !"
    echo
    exit 1
fi
echo "Starting Plugin SecurityCenter setup for OCS Inventory NG Server 2.x" >> $SETUP_LOG
echo -n "from folder " >> $SETUP_LOG
pwd >> $SETUP_LOG
echo "Storing log in file $SETUP_LOG" >> $SETUP_LOG
echo "Storing log in file $SETUP_LOG"
echo >> $SETUP_LOG
echo "============================================================" >> $SETUP_LOG
echo "Checking requierements..." >> $SETUP_LOG 
echo "============================================================" >> $SETUP_LOG
#Check that locate is installed on the system
if [ -f $LOCATE_BIN ]
then 
	echo "Locate seems to be installed"
	echo "Locate seems to be installed" >> $SETUP_LOG 
else
	echo "Locate missing !" >> $SETUP_LOG 
    echo "Locate missing !"
	echo "Installation aborted !" >> $SETUP_LOG 
    echo "Installation aborted !"
    exit 1
fi
#Check that updatedb is installed on the system
if [ -f $UPDATEDB_BIN ]
then 
	echo "UpdateDB seems to be installed"
	echo "UpdateDB seems to be installed" >> $SETUP_LOG 
else
	echo "UpdateDB missing !" >> $SETUP_LOG 
    echo "UpdateDB missing !"
	echo "Installation aborted !" >> $SETUP_LOG 
    echo "Installation aborted !"
    exit 1
fi
echo
echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo "| Checking for Apache web server daemon...                 |" >> $SETUP_LOG
echo "+----------------------------------------------------------+" >> $SETUP_LOG

echo
echo "Checking for Apache web server daemon" >> $SETUP_LOG
# Try to find Apache daemon
if [ -z "$APACHE_BIN" ]
then
    APACHE_BIN_FOUND=`which httpd 2>/dev/null`
    if [ -z "$APACHE_BIN_FOUND" ]
    then
        APACHE_BIN_FOUND=`which apache 2>/dev/null`
        if [ -z "$APACHE_BIN_FOUND" ]
        then
            APACHE_BIN_FOUND=`which apache2 2>/dev/null`
        fi
    fi
fi
echo "Found Apache daemon $APACHE_BIN_FOUND" >> $SETUP_LOG
# Ask user's confirmation 
res=0
while [ $res -eq 0 ]
do
    echo -n "Where is Apache daemon binary [$APACHE_BIN_FOUND] ?"
    read ligne
    if [ -z "$ligne" ]
    then
        APACHE_BIN=$APACHE_BIN_FOUND
    else
        APACHE_BIN="$ligne"
    fi
    # Ensure file exists and is executable
    if [ -x $APACHE_BIN ]
    then
        res=1
    else
        echo "*** ERROR: $APACHE_BIN is not executable !"
        res=0
    fi
    # Ensure file is not a directory
    if [ -d $APACHE_BIN ]
    then 
        echo "*** ERROR: $APACHE_BIN is a directory !"
        res=0
    fi
done
echo "OK, using Apache daemon $APACHE_BIN ;-)"
echo "Using Apache daemon $APACHE_BIN" >> $SETUP_LOG

echo "============================================================" >> $SETUP_LOG
echo "Checking Engine directory..." >> $SETUP_LOG 
echo "============================================================" >> $SETUP_LOG

#Search Support.pm witch is a new module, appears in 2.0.0
#Search to define the Operating System
founded=`"$LOCATE_BIN" Support.pm | head -n 1`

if [ -f /etc/issue ] &&  grep "openSUSE" /etc/issue
then
	SUPPORT_PM_PATH=`dirname $founded`
    MAP_PATH=`dirname "$SUPPORT_PM_PATH"`
else
	if [ -f /etc/issue ] &&  grep "Debian\|Ubuntu" /etc/issue
	then
		SUPPORT_PM_PATH=`dirname $founded`
        MAP_PATH=`dirname "$SUPPORT_PM_PATH"`
	else
		if [ -f /etc/issue ] &&  grep "CentOS\|RedHat" /etc/issue
		then
			SUPPORT_PM_PATH=`dirname $founded`
			MAP_PATH=`dirname "$SUPPORT_PM_PATH"`
		else
			echo Define the path where is located Map.pm
			echo -n "Where is Map.pm located (define the full path) ?"
			echo "(exemple: /usr/local/share/perl/5.10.1/Apache/Ocsinventory)"
			read ligne
			if [ -z "$ligne" ]
			then
				echo "Installation aborted !"
				exit 1
			else
				MAP_PATH="$ligne"
			fi
		fi
	fi
fi

#define Map.pm directory
MAP_PM_PATH=$MAP_PATH
#Go to the install directory
cd $SETUP_PATH
echo "Map.pm is located in $MAP_PM_PATH"

echo " Map.pm is located in $MAP_PM_PATH" >> $SETUP_LOG

echo "============================================================"
echo "Checking OCS Inventory NG Management Server requirements..." 
echo "============================================================"
echo "============================================================" >> $SETUP_LOG
echo "Checking OCS Inventory NG Management Server requirements..." >> $SETUP_LOG 
echo "============================================================" >> $SETUP_LOG
echo
echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo "| Checking for database server properties...               |" >> $SETUP_LOG
echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo "+----------------------------------------------------------+"
echo "| Configuring database access...                           |"
echo "+----------------------------------------------------------+"
echo
# Ask user for database server host
res=0
while [ $res -eq 0 ]
do
    echo -n "Which host is running database server [$DB_SERVER_HOST] ?"
    read ligne
    if [ -z "$ligne" ]
    then
        res=1
    else
        DB_SERVER_HOST="$ligne"
        res=1
    fi
done
echo "OK, database server is running on host $DB_SERVER_HOST ;-)"
echo "Database server is running on host $DB_SERVER_HOST" >> $SETUP_LOG
echo

# Ask user for database server port
res=0
while [ $res -eq 0 ]
do
    echo -n "On which port is running database server [$DB_SERVER_PORT] ?"
    read ligne
    if [ -z "$ligne" ]
    then
        res=1
    else
        DB_SERVER_PORT="$ligne"
        res=1
    fi
done
echo "OK, database server is running on port $DB_SERVER_PORT ;-)"
echo "Database server is running on port $DB_SERVER_PORT" >> $SETUP_LOG

# Ask user for database user
res=0
while [ $res -eq 0 ]
do
    echo -n "Enter your mysql user login [$DB_SERVER_USER] ?"
    read ligne
    if [ -z "$ligne" ]
    then
        res=1
    else
        DB_SERVER_USER="$ligne"
        res=1
    fi
done
echo "OK, DB_SERVER_USER set to $DB_SERVER_USER ;-)"
echo "OK, DB_SERVER_USER set to $DB_SERVER_USER ;-)" >> $SETUP_LOG

# Ask user for database user password
res=0
while [ $res -eq 0 ]
do
    echo -n "Enter your mysql user password [$DB_SERVER_PWD] ?"
    read ligne
    if [ -z "$ligne" ]
    then
        res=1
    else
        DB_SERVER_PWD="$ligne"
        res=1
    fi
done
echo "OK, DB_SERVER_PWD set to $DB_SERVER_PWD ;-)"
echo "OK, DB_SERVER_PWD set to $DB_SERVER_PWD ;-)" >> $SETUP_LOG

# Ask for database name
res=0
while [ $res -eq 0 ]
do
    echo -n "Enter your OCS database name [$DB_NAME] ?"
    read ligne
    if [ -z "$ligne" ]
    then
        res=1
    else
        DB_NAME="$ligne"
        res=1
    fi
done
echo "OK, DB_NAME set to $DB_NAME ;-)"
echo "OK, DB_NAME set to $DB_NAME ;-)" >> $SETUP_LOG
#End check mysql  requirements
echo
echo "+----------------------------------------------------------+"
echo "| Checking for Apache main configuration file...           |"
echo "+----------------------------------------------------------+"
echo
# Try to find Apache main configuration file
echo "Checking for Apache main configuration file" >> $SETUP_LOG
if [ -z "$APACHE_CONFIG_FILE" ]
then
    APACHE_ROOT=`eval $APACHE_BIN -V | grep "HTTPD_ROOT" | cut -d'=' -f2 | tr -d '"'`
    echo "Found Apache HTTPD_ROOT $APACHE_ROOT" >> $SETUP_LOG
    APACHE_CONFIG=`eval $APACHE_BIN -V | grep "SERVER_CONFIG_FILE" | cut -d'=' -f2 | tr -d '"'`
    echo "Found Apache SERVER_CONFIG_FILE $APACHE_CONFIG" >> $SETUP_LOG
    if [ -e $APACHE_CONFIG ]
    then
        APACHE_CONFIG_FILE_FOUND="$APACHE_CONFIG"
    else
        APACHE_CONFIG_FILE_FOUND="$APACHE_ROOT/$APACHE_CONFIG"
    fi
fi
echo "Found Apache main configuration file $APACHE_CONFIG_FILE_FOUND" >> $SETUP_LOG
# Ask user's confirmation 
res=0
while [ $res -eq 0 ]
do
    echo -n "Where is Apache main configuration file [$APACHE_CONFIG_FILE_FOUND] ?"
    read ligne
    if [ -z "$ligne" ]
    then
        APACHE_CONFIG_FILE=$APACHE_CONFIG_FILE_FOUND
    else
        APACHE_CONFIG_FILE="$ligne"
    fi
    # Ensure file is not a directory
    if [ -d $APACHE_CONFIG_FILE ]
    then 
        echo "*** ERROR: $APACHE_CONFIG_FILE is a directory !"
        res=0
    fi
    # Ensure file exists and is readable
    if [ -r $APACHE_CONFIG_FILE ]
    then
        res=1
    else
        echo "*** ERROR: $APACHE_CONFIG_FILE is not readable !"
        res=0
    fi
done
echo "OK, using Apache main configuration file $APACHE_CONFIG_FILE ;-)"
echo "Using Apache main configuration file $APACHE_CONFIG_FILE" >> $SETUP_LOG
echo

echo
echo
echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo "| Checking for Apache user account...                      |" >> $SETUP_LOG
echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo "+----------------------------------------------------------+"
echo "| Checking for Apache user account...                      |"
echo "+----------------------------------------------------------+"
echo
if [ -z "$APACHE_USER" ]
then
    # Debian
    if [ -f /etc/apache2/envvars ]; then
        . /etc/apache2/envvars
        APACHE_USER_FOUND=$APACHE_RUN_USER
    else
        APACHE_USER_FOUND=`cat $APACHE_CONFIG_FILE | grep "User " | tail -1 | cut -d' ' -f2`
    fi
fi
echo "Found Apache user account $APACHE_USER_FOUND" >> $SETUP_LOG
# Ask user's confirmation 
res=0
while [ $res -eq 0 ]
do
    echo -n "Which user account is running Apache web server [$APACHE_USER_FOUND] ?"
    read ligne
    if [ -z "$ligne" ]
    then
        APACHE_USER=$APACHE_USER_FOUND
    else
        APACHE_USER="$ligne"
    fi
    # Ensure group exist in /etc/passwd
    if [ `cat /etc/passwd | grep $APACHE_USER | wc -l` -eq 0 ]
    then
        echo "*** ERROR: account $APACHE_USER not found in system table /etc/passwd !"
    else
        res=1
    fi
done
echo "OK, Apache is running under user account $APACHE_USER ;-)"
echo "Using Apache user account $APACHE_USER" >> $SETUP_LOG
echo

echo
echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo "| Checking for Apache group...                             |" >> $SETUP_LOG
echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo "+----------------------------------------------------------+"
echo "| Checking for Apache group...                             |"
echo "+----------------------------------------------------------+"
echo
if [ -z "$APACHE_GROUP" ]
then
    # Debian
    if [ -f /etc/apache2/envvars ]; then
        . /etc/apache2/envvars
        APACHE_GROUP_FOUND=$APACHE_RUN_GROUP
    else
        APACHE_GROUP_FOUND=`cat $APACHE_CONFIG_FILE | grep "Group" | tail -1 | cut -d' ' -f2`
    fi

    if [ -z "$APACHE_GROUP_FOUND" ]
    then
        # No group found, assume group name is the same as account
        echo "No Apache user group found, assuming group name is the same as user account" >> $SETUP_LOG
        APACHE_GROUP_FOUND=$APACHE_USER
    fi
fi
echo "Found Apache user group $APACHE_GROUP_FOUND" >> $SETUP_LOG
# Ask user's confirmation 
res=0
while [ $res -eq 0 ]
do
    echo -n "Which user group is running Apache web server [$APACHE_GROUP_FOUND] ?"
    read ligne
    if [ -z "$ligne" ]
    then
        APACHE_GROUP=$APACHE_GROUP_FOUND
    else
        APACHE_GROUP="$ligne"
    fi
    # Ensure group exist in /etc/group
    if [ `cat /etc/group | grep $APACHE_GROUP | wc -l` -eq 0 ]
    then
        echo "*** ERROR: group $APACHE_GROUP not found in system table /etc/group !"
    else
        res=1
    fi
done
echo "OK, Apache is running under users group $APACHE_GROUP ;-)"
echo "Using Apache user group $APACHE_GROUP" >> $SETUP_LOG
echo

echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo "| Checking for Administration Server directories...        |" >> $SETUP_LOG
echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo "+----------------------------------------------------------+"
echo "| Checking for Administration Server directories...        |"
echo "+----------------------------------------------------------+"
    echo "Where is your 'ocsreports' directory ?"
    echo -n "[$ADM_SERVER_STATIC_DIR] ?"
    read ligne
    if test -z $ligne
    then
       ADM_SERVER_STATIC_DIR=$ADM_SERVER_STATIC_DIR
    else
       ADM_SERVER_STATIC_DIR="$ligne"
    fi
    echo "OK, using directory $ADM_SERVER_STATIC_DIR to install new static files ;-)"
    echo "Using directory $ADM_SERVER_STATIC_DIR for new static files" >> $SETUP_LOG

    echo "+----------------------------------------------------------+"
    echo "| OK, looks good ;-)                                       |"
    echo "| Installing OCS Plugin SecurityCenter                     |"
    echo "+----------------------------------------------------------+"
echo "All requirements seem to be good!" >> $SETUP_LOG
echo "+----------------------------------------------------------+"
echo "| Copy PHP file in cd_securitycenter directory...          |"
echo "+----------------------------------------------------------+"
#Copy php file to display security status in the detail of machines
cp $CURRENT_PATH/cd_securitycenter -R $ADM_SERVER_STATIC_DIR/ocsreports/plugins/computer_detail/

echo "+----------------------------------------------------------+"
echo "| Copy icons in /img directory...                          |"
echo "+----------------------------------------------------------+"
#Copy 3 icons displayed in administration console
cp $CURRENT_PATH/img/cd_securitycenter.png $ADM_SERVER_STATIC_DIR/ocsreports/plugins/computer_detail/img/
cp $CURRENT_PATH/img/cd_securitycenter_a.png $ADM_SERVER_STATIC_DIR/ocsreports/plugins/computer_detail/img/
cp $CURRENT_PATH/img/cd_securitycenter_d.png $ADM_SERVER_STATIC_DIR/ocsreports/plugins/computer_detail/img/

echo "+----------------------------------------------------------+"
echo "| Modify management console...                             |"
echo "+----------------------------------------------------------+"
#Save cd_config.txt in cd_config.txt.bak  before overwrite
mv $ADM_SERVER_STATIC_DIR/ocsreports/plugins/computer_detail/cd_config.txt $ADM_SERVER_STATIC_DIR/ocsreports/plugins/computer_detail/cd_config.txt.bak
#Replace old cd_config.txt by new
cp $CURRENT_PATH/conf/cd_config.txt $ADM_SERVER_STATIC_DIR/ocsreports/plugins/computer_detail/

echo "+----------------------------------------------------------+"
echo "| Create new table in database ocsweb...                   |"
echo "+----------------------------------------------------------+"
#Create table 'securitycenter' in database 'ocsweb'
mysql -u $DB_SERVER_USER -p$DB_SERVER_USER $DB_NAME < $CURRENT_PATH/creation_table_securitycenter.sql >> $SETUP_LOG
echo "+----------------------------------------------------------+"
echo "| Overwrite OCS Inventory NG engine...                     |"
echo "+----------------------------------------------------------+"

#retrieve the Map.pm path SUPPORT_PM_PATH
	#if file doesn't exist, OCS not correctly installed
   if [ -d $MAP_PM_PATH ];
	then
		echo "Stop Apache service"
		echo "Stop Apache service" >> $SETUP_LOG
		service apache2 stop
		echo "Overwrite Map.pm file"
		echo "Overwrite Map.pm file" >> $SETUP_LOG
		#Save Map.pm in Map.pm.bak  before overwrite
		mv $MAP_PM_PATH/Map.pm $MAP_PM_PATH/Map.pm.bak
		#Replace old Map.pm by new
		cp  -f $CURRENT_PATH/NewMap.pm $MAP_PM_PATH/Map.pm
		echo "Start Apache service"
		echo "Start Apache service" >> $SETUP_LOG
		service apache2 start
		echo "New Map.pm reload without problem!" >> $SETUP_LOG
   else
		echo "Installation aborted !"
		echo
		exit 1
   fi
echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo "| End of installation...                                   |" >> $SETUP_LOG
echo "+----------------------------------------------------------+" >> $SETUP_LOG
echo
echo "Setup has created a log file $SETUP_LOG. Please, save this file."
echo "If you encounter error while running this plugin, refer to the developer"
echo "End of installation"
echo "Now, you have to place the .vbs in Agent directory on remote computers."
echo "+--------------------------------------------------------------------+"
echo "| OK, Plugin OCS SecurityCenter installation finished ;-)            |"
echo "|                                                                    |"
echo "| Point your browser to http://server/ocsreports and check that      |"
echo "| Security icon is present in the detail of machines.                |"
echo "+--------------------------------------------------------------------+"
	
echo "Enjoy :-)"
exit 0



