#!/bin/sh
#
# Copyright (c) 1996-2005, Adobe Systems Incorporated
# All Rights Reserved
#

#Used to specify the start directory for finding acroread
SCRIPT_PATH=

#Contains the install directory path for acroread
ACROREAD_DIR=

#OS specific for acroread
CONFIG_FOLDER=

#log file
LOG_FILE="/tmp/adobe_selinux_log"

#file context file
FILE_CONTEXT_FILE="/tmp/adobe_file_context"

#This function gets the complete path of this script.
GetScriptPath()
{
	CURRENT_PATH=`pwd`
	SCRIPT_PATH=`dirname "$0"`
	COMPLETE_PATH="${SCRIPT_PATH}/../../"
	cd "$COMPLETE_PATH"
	SCRIPT_PATH=`pwd`
	cd "$CURRENT_PATH"
}


#This function fetches the installation path if not specified and validates the existance of the plugin file.
GetAcroreadInfo()
{
    #Prompt the user for acroread's installation path if not specified on the command prompt
    if [ -z "$1" ]
	then
	GetScriptPath
	echo -n "Enter the install directory for Adobe Reader 7.0 [${SCRIPT_PATH}] "
	read ACROREAD_DIR
	if [ -z "$ACROREAD_DIR" ]
	    then
	    ACROREAD_DIR="$SCRIPT_PATH"
	fi
    else
	ACROREAD_DIR="$1"
    fi

    OSNAME=`uname -s`
    if [ "$OSNAME" = "Linux" ]
	then
	CONFIG_FOLDER="intellinux"
    elif [ "$OSNAME" = "SunOS" ]
	then
	CONFIG_FOLDER="sparcsolaris"
    else
	echo "INTERNAL ERROR ..... No matching OS name."
	return 1
    fi
}


#This function fetches the absolute path of the file and dereferences them if it is a link.
GetAbsoluteFile()
{
    if [ -h "$1" ]
	then
	CURRENT_PATH=`pwd`
	cd `dirname $1`
	COMPLETE_PATH=`ls -l $1 | awk '{ print $NF }'`
	COMPLETE_PATH_DIR=`dirname $COMPLETE_PATH`
	COMPLETE_PATH_FILE=`basename $COMPLETE_PATH`
	cd $COMPLETE_PATH_DIR
	COMPLETE_PATH_DIR=`pwd`
	cd "$CURRENT_PATH"
	GetAbsoluteFile ${COMPLETE_PATH_DIR}/${COMPLETE_PATH_FILE}
    else
	CURRENT_PATH=`pwd`
	COMPLETE_PATH_DIR=`dirname $1`
	COMPLETE_PATH_FILE=`basename $1`
	cd $COMPLETE_PATH_DIR
	COMPLETE_PATH_DIR=`pwd`
	cd "$CURRENT_PATH"
	echo "${COMPLETE_PATH_DIR}/${COMPLETE_PATH_FILE}"
	return 0
    fi
}


PerformPatch()
{
    ERROR_OCCURED=0

    if [ ! -d "${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/lib" ] || 
       [ ! -d "${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/plug_ins" ] || 
       [ ! -d "${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/SPPlugins" ]
	then
	echo "Unable to find appropriate files in the installation path."
	return 1
    fi

    echo "Resetting old contexts ..."
    echo "restorecon ----" >> $LOG_FILE
    restorecon -R -v -F $ACROREAD_DIR >> $LOG_FILE 2>&1
    if [ $? -ne 0 ]
	then
	echo "Failure in command - restorecon"
	ERROR_OCCURED=1
    fi

    echo "Setting new contexts for library files ..."
    echo "file listing ----" >> $LOG_FILE

    cd ${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/lib
    LIB_FILES=`ls`
    for i in $LIB_FILES
      do
      CURRENT_LIB_FILE=`GetAbsoluteFile $i`
      #setfiles does the chcon for us, so we don't need to redo it.
      #chcon -v -u system_u -r object_r -t shlib_t $CURRENT_LIB_FILE >> $LOG_FILE 2>&1
      echo "$CURRENT_LIB_FILE -- system_u:object_r:shlib_t" >> $FILE_CONTEXT_FILE
    done

    cd ${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/plug_ins
    LIB_FILES=`ls`
    for i in $LIB_FILES
      do
      CURRENT_LIB_FILE=`GetAbsoluteFile $i`
      #setfiles does the chcon for us, so we don't need to redo it.
      #chcon -v -u system_u -r object_r -t shlib_t $CURRENT_LIB_FILE >> $LOG_FILE 2>&1
      echo "$CURRENT_LIB_FILE -- system_u:object_r:shlib_t" >> $FILE_CONTEXT_FILE
    done

    cd ${ACROREAD_DIR}/Reader/${CONFIG_FOLDER}/SPPlugins
    LIB_FILES=`ls`
    for i in $LIB_FILES
      do
      CURRENT_LIB_FILE=`GetAbsoluteFile $i`
      #setfiles does the chcon for us, so we don't need to redo it.
      #chcon -v -u system_u -r object_r -t shlib_t $CURRENT_LIB_FILE >> $LOG_FILE 2>&1
      echo "$CURRENT_LIB_FILE -- system_u:object_r:shlib_t" >> $FILE_CONTEXT_FILE
    done
    
    LIB_FILES=`cat $FILE_CONTEXT_FILE | sort | uniq`
    echo "$LIB_FILES" > $FILE_CONTEXT_FILE

    echo "setfiles ----" >> $LOG_FILE
    setfiles -l -vv -W -F $FILE_CONTEXT_FILE $ACROREAD_DIR >> $LOG_FILE 2>&1
    if [ $? -ne 0 ]
	then
	echo "Failure in command - setfiles"
	ERROR_OCCURED=1
    fi

    return $ERROR_OCCURED
}


Init()
{
    echo "" >> $LOG_FILE
    echo "" >> $LOG_FILE
    echo "--------------" >> $LOG_FILE
    CUR_DATE=`date`
    echo "$CUR_DATE" >> $LOG_FILE

    clear
    echo "This will try to update the library files to make them SELinux aware."
    echo ""
    GetAcroreadInfo "$1"
    if [ $? -ne 0 ]
	then
	return 1
    fi
    
    if [ ! -d "$ACROREAD_DIR" ]
	then
	echo "Could not find the installation folder."
	return 1
    fi

    PerformPatch
    return $?
}

Cleanup()
{
    rm -f $FILE_CONTEXT_FILE
#   rm -f $LOG_FILE
    if [ $1 -eq 0 ]
	then
	echo "Finished setting the contexts. You can check the log at $LOG_FILE"
    else
	echo "Patch might not have completed properly. You can check the log at $LOG_FILE"
    fi
}

trap 'Cleanup ; exit 0' SIGINT
Init "$1"
Cleanup $?