#!/bin/sh
#  $Id: UpgradeFrom_520000.sh 144933 2017-02-08 09:50:25Z justin.carveley@oracle.com $
#  Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
#  This software code contains proprietary information 
#  and should be treated as confidential.
#
#  Component: Installation/upgrade

FROM_VERSION=520000 TO_VERSION=530000

. ./util

# handy shortcut vars
TTA="${instdir}/bin/tarantella"
VIN="${instdir}/var/info"
VLG="${instdir}/var/log"
TSR="${instdir}/etc/templates/serverresources"
VSR="${instdir}/var/serverresources"
TSC="${TSR}/serverconfig"
VSC="${instdir}/var/serverconfig"
TSB="${TSC}/global/beans"
VSB="${VSC}/global/beans"
TC="tarantella.config"
T_C="--tarantella-config"

CIPHER_CONFIG_KEY="security-ciphers"
PEER_CIPHER_CONFIG_KEY="security-peerssl-ciphers"

removeCipher () {
    CIPHERS=`get_value "${T_C}-${1}"`
    TO_REMOVE="$2"
    NEW_CIPHERS=''
    (
        IFS="$3"
        for cipher in $CIPHERS; do
            cipher=`echo "$cipher" | sed -e "s|^\"||" -e "s|\"$||"`
            echo "$cipher" | grep "$TO_REMOVE" > /dev/null
            if [ $? -ne 0 ]; then
                if [ -n "$NEW_CIPHERS" ]; then
                    NEW_CIPHERS="${NEW_CIPHERS}${3}"
                fi
                NEW_CIPHERS="${NEW_CIPHERS}${cipher}"
            fi
        done

        update_gprop "$1" "$NEW_CIPHERS"
    )
}

## Update SSL Daemon Cipher Suites ##
current_ciphers=`get_value "${T_C}-${CIPHER_CONFIG_KEY}"`
if [ "$current_ciphers" = "AES256:RC4-MD5" ]; then
    update_gprop $CIPHER_CONFIG_KEY "AES256-SHA"
fi

# Disable unacceptable cipher suites
# SSLDaemon
NEW_SSLD_CIPHERS=`get_value "${T_C}-${CIPHER_CONFIG_KEY}"`
if [ -n "$NEW_SSLD_CIPHERS" ]; then
    NEW_SSLD_CIPHERS="${NEW_SSLD_CIPHERS}:"
fi
NEW_SSLD_CIPHERS="${NEW_SSLD_CIPHERS}!aNULL:!eNULL:!EXPORT:!LOW:!MD5:!DES:!RC2:!RC4:!PSK"
update_gprop $CIPHER_CONFIG_KEY "$NEW_SSLD_CIPHERS"

# Peer SSL
removeCipher $PEER_CIPHER_CONFIG_KEY "anon" ','
removeCipher $PEER_CIPHER_CONFIG_KEY "WITH_NULL" ','
removeCipher $PEER_CIPHER_CONFIG_KEY "EXPORT" ','
removeCipher $PEER_CIPHER_CONFIG_KEY "MD5" ','
removeCipher $PEER_CIPHER_CONFIG_KEY "WITH_DES" ','
removeCipher $PEER_CIPHER_CONFIG_KEY "WITH_RC2" ','
removeCipher $PEER_CIPHER_CONFIG_KEY "WITH_RC4" ','
removeCipher $PEER_CIPHER_CONFIG_KEY "PSK" ','

# Audio Quality Changes
remove_property "global/audio.properties" "${TC}.audio.quality"
SCRIPTVARS="`get_value "local/scottawindowsapplication.properties" "${TC}.scottaapplication.scottascriptvars"`"
MODIFIED_SCRIPTVARS="`echo "${SCRIPTVARS}" | sed "s/TTA_AudioQuality=scottaaudioquality/TTA_AudioEnabled=scottaaudioenabled/g"`"
change_property "local/scottawindowsapplication.properties" \
                "${TC}.scottaapplication.scottascriptvars" \
                "${MODIFIED_SCRIPTVARS}"
                
# Clipboard Defaults
change_property "local/xpe.properties" \
                "${TC}.xpeconfig.clipboard.targets.nodeal" \
                ""

# TTA_Hints
append_property "local/scottaapplication.properties" \
                "${TC}.scottaapplication.scottascriptvars" \
                "TTA_Hints=scottahints"

# Reset SSH X applications 'Maintain connection' to 0
${TTA} post_SGD_5_30_upgrade_fix

# Set a default value for the DH params file name
add_property "local/ssldaemon.properties" \
             "${TC}.ssldaemon.dhparamsfile" \
             "%%INSTALLDIR%%/var/tsp/dhparams.pem"

# Set a default value for the commandconstraints
add_property "local/server.properties" \
             "${TC}.server.commandconstraints" \
             ""

# Unix Audio Properties
## Remove incorrect property
remove_property "global/audio.properties" "${TC}.unixaudioin-enabled"

## Add correct property back, add_property will only add if not already present.
add_property "global/audio.properties" "${TC}.unixaudioin.enabled" "0"

# Tidy up files that could have been left over from previous upgrades
rm -f ${TSR}/tfn/tta/jserver/rdplicpooldelta.properties ${VSR}/tfn/tta/jserver/rdplicpooldelta.properties 2>/dev/null

# X11
## Update install location of XORG to point at new XORGINSTALLDIR placeholder.
X11_PROPS="${TC}.xpeconfig.fontpath ${TC}.xpeconfig.xkb.basedir ${TC}.xpeconfig.xkb.binarydir"
X11_PROP_FILE="local/xpe.properties"

for PROPERTY in ${X11_PROPS}; do
    CURRENT_VAL="`get_value ${X11_PROP_FILE} ${PROPERTY}`"
    NEW_VAL="`echo "${CURRENT_VAL}" | sed 's|%%INSTALLDIR%%/X11|%%XORGINSTALLDIR%%|g'`"
    change_property "${X11_PROP_FILE}" "${PROPERTY}" "${NEW_VAL}"
done
exit 0

