#!/bin/bash
#
# UCC - RDP session script
#
# Copyright (C) 2012-2013 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.

# force user session
if [ -e /usr/share/univention-lightdm/force-user-session ]; then
    if [ -n "$1" ]; then
		if [ "$1" == "skipforce" ]; then
			echo "Session already forced"
		fi
    else
		. /usr/share/univention-lightdm/force-user-session
    fi
fi

. /usr/share/univention-lib/all.sh
. /usr/share/univention-lib/ucc.sh

function cleanup_and_exit {
    if test -e /tmp/password-store-$USER; then
	rm -rf /tmp/password-store-$USER
    fi
    exit
}

function already_joined {
  if [ -n "$ldap_base" ] && [ -n "$rdp_domainname" ] &&  [ -n "$rdp_server" ]; then
    joined="true"
  else
    joined="false"
  fi
}

eval "$(/usr/sbin/univention-config-registry shell)"

params=(-f)

if is_ucr_true 'rdp/checknla'; then
    echo "By default NLA validation is enabled"
else
    echo "Disabling NLA validation"
    params+=(--no-nla)
fi

if is_ucr_true 'rdp/checktls'; then
    echo "By default TLS validation is enabled"
else
    echo "Disabling TLS validation"
    params+=(--no-tls)
fi

if is_ucr_true 'rdp/ignorecertificate'; then
	params+=(--ignore-certificate)
fi

if [ -n "$rdp_keyboard" ]; then
    params+=(-k "$rdp_keyboard")
else
    rdpKeyboard=`univention-keyboardmapping --xtofreerdp $xorg_keyboard_options_XkbLayout`
    params+=(-k "$rdpKeyboard")
fi

if is_ucr_true 'rdp/disable-sound'; then
	echo "Deactivating sound output"
else
 	params+=(--plugin rdpsnd)
fi

if [ -n "$rdp_geometry" ]; then
    params+=(-g "$rdp_geometry")
fi

if [ -n "$rdp_clienthostname" ]; then
    params+=(-n "$rdp_clienthostname")
fi

if [ -n "$rdp_user" ]; then
    params+=(-u "$rdp_user")
    if test -e /tmp/password-store-$USER; then
      PASSWD=`cat /tmp/password-store-$USER | base64 --decode`
      params+=(-p "$PASSWD")
    fi

else

joined="false"
  if [[ $USER == guest-* ]]; then
    already_joined
    if [[ $joined == true ]]; then
      echo "Disabling NLA validation"
      params+=(--no-nla)
    else
      messageFile=$(mktemp)
	echo "
You're currently logged in with a guest user account. This usually means
that the UCC system hasn't been joined into the UCS domain yet.

You can either 

* Join this system into the domain using univention-join
  and perform a user login using the <RDP> session 
* Specify a login for the Windows terminal server using the UCR
  variable rdp/user.

A desktop session will now be started. You can initiate a terminal session 
to a Windows terminal server by launching the application Remmina Remote 
Desktop Client from the start menu.

" >>$messageFile
	xmessage -file "$messageFile"
	/usr/bin/startlxde
    fi
  else
    params+=(-u "$USER")
    if test -e /tmp/password-store-$USER; then
      PASSWD=`cat /tmp/password-store-$USER | base64 --decode`
      params+=(-p "$PASSWD")
    fi
  fi
fi

if [ -n "$rdp_domainname" ]; then
    params+=(-d "$rdp_domainname")
else
	policy_windows_domain=`get_user_policy_var univentionCorporateClientUserWindowsDomain` 

if [ -z "$policy_windows_domain" ]; then
	echo "Use a local workstation login and not a domain login"
else
	params+=(-d "$policy_windows_domain")
fi	

fi


if is_ucr_true 'rdp/redirectdisk'; then
    params+=(--plugin rdpdr --data disk:local-drives:/run/drives --)
else
	policy_enable_storage=`grep -A 1 univentionCorporateClientComputerLocalStorage= /var/cache/ucc/client-policy-"$(hostname)".txt  | grep univentionCorporateClientComputerLocalStorage= | awk -F "=" '{print $2}' | sed -e 's/^[[:space:]]*//' `

	if [ $policy_enable_storage == '"1"' ]; then
		params+=(--plugin rdpdr --data disk:local-drives:/run/drives --)
	fi
fi
	
if [ -n "$rdp_additionaloptions" ]; then
       params+=("$rdp_additionaloptions")
fi

policy_windows_ts=`get_user_policy_var univentionCorporateClientUserWindowsTerminalserver` 

server=""
if [ -n "$policy_windows_ts" ]; then
	server="$policy_windows_ts"
fi

if [ -n "$rdp_server" ]; then
	server="$rdp_server"
fi

if [ -z "$server" ]; then
	messageFile=$(mktemp)
	echo "
No RDP server has been configured.

The Windows terminal server can be configured through the
input field <Windows Terminal Server> in the UCC user session
policy or setting the UCR variable rdp/server via an UCR policy.

This hasn't been setup yet. A desktop session will now be started.
You can initiate a terminal session to a Windows terminal server
by launching the application Remmina Remote Desktop Client from
the start menu.

" >>$messageFile
	xmessage -file "$messageFile"
	/usr/bin/startlxde
fi


messageFile=$(mktemp)
echo "Could not contact terminal server" "$server"  > "$messageFile:"

/usr/bin/xfreerdp "${params[@]}" $server 1>> "$messageFile"
ret=$?

if [ $ret == 131 ]; then
    xmessage -file "$messageFile"
fi

#cleanup_and_exit

