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

#Used to specify the start directory for finding acroread

set_lang_DEU_utf()
{
}


set_lang_DEU()
{
}

if echo $LANG | grep -i utf >/dev/null; then
    set_lang_DEU_utf
else
    set_lang_DEU
fi

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
	printf "Enter the install directory for Adobe Reader 8.1  [%s]  " "${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
	printf "%s\n" "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
	printf "%s\n" "Unable to find appropriate files in the installation path."
	return 1
    fi

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

    printf "%s\n" "Setting new contexts for library files ....."
    echo "file listing ----" >> $LOG_FILE

#    No need to enforce the patch on library files in Reader 8
#
#    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
#    No need to enforce the patch on all plugins in Reader 8. Needed for only PPKLite.
	if [ $i == "PPKLite.api" ]
	then
      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
      fi
    done

#    No need to enforce the patch on all plugins in Reader 8
#     
#     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
	printf "Failure in command '%s'.\n" "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
    printf "%s\n" "This will try to update the library files to make them SELinux aware."
    GetAcroreadInfo "$1"
    if [ $? -ne 0 ]
	then
	return 1
    fi
    
    if [ ! -d "$ACROREAD_DIR" ]
	then
	printf "%s\n" "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
	printf "Finished setting the contexts. You can check the log at  %s.\n" "$LOG_FILE"
    else
	printf "Patch might not have completed properly. You can check the log at  %s.\n" "$LOG_FILE"
    fi
}

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