Check-in [4d6a646b70]
Overview
SHA1:4d6a646b709385317059f167578a294764184059
Date: 2016-03-29 17:39:54
User: rkeene
Comment:Added initial rdp.sh release
Timelines: family | ancestors | descendants | both | trunk
Downloads: Tarball | ZIP archive
Other Links: files | file ages | folders | manifest
Tags And Properties
Context
2016-03-29
17:48
[5996946805] Leaf: Added Makefile (user: rkeene, tags: trunk, 1.0)
17:39
[4d6a646b70] Added initial rdp.sh release (user: rkeene, tags: trunk)
17:27
[9258ed238d] initial empty check-in (user: rkeene, tags: trunk)
Changes

Added rdp.sh version [a814622ace].











































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#! /bin/bash

if [ "$1" = '-ssh' ]; then
	shift

	RDP_SSH_JUMPBOX="$1"
	shift
else
	RDP_SSH_JUMPBOX=''
fi

RDP_HOST="$1"
shift

function printHelp() {
	echo 'Usage: rdp [-ssh host] <host> [<xfreeRdpArgs...>]'
}

if [ -z "${RDP_HOST}" ]; then
	printHelp >&2

	exit 1
fi

# Include a config file, which is actually just a script
# This script can do things like update the "RDP_SSH_JUMPBOX"
# based on the "RDP_HOST", provide a password (from a password
# manager, like hunter2, ideally), etc.
if [ -f ~/.rdp.conf ]; then
	. ~/.rdp.conf
fi

## Split the target into host/port pairs
case "${RDP_HOST}" in
	*:*)
		hostName="$(echo "${RDP_HOST}" | cut -f 1 -d ':')"
		hostPort="$(echo "${RDP_HOST}" | cut -f 2 -d ':')"
		;;
	*)
		hostName="${RDP_HOST}"
		hostPort='3389'
		;;
esac

# If we are jumping through an SSH jumpbox, setup port forwarding
if [ -n "${RDP_SSH_JUMPBOX}" ]; then
	# Pick a random port to forward on
	randomPort=$[${RANDOM} % 1024 + 3390]

	# Determine an SSH control socket
	sshControlSocket="/tmp/ssh-sock-${randomPort}-$$${RANDOM}${RANDOM}${RANDOM}"

	function cleanup() {
		# Kill the SSH session
		sshPid="$(timeout 30 ssh -S "${sshControlSocket}" -O check /dev/null 2>&1 | grep 'pid=' | sed 's@.*pid=@@;s@).*$@@')"
		if [ -n "${sshPid}" ]; then
			kill -9 "${sshPid}" >/dev/null 2>/dev/null
		fi

		rm -f "${sshControlSocket}"
	}

	# Start SSH
	ssh -L${randomPort}:${hostName}:${hostPort} -o ExitOnForwardFailure=yes -o ControlMaster=yes -S "${sshControlSocket}" -a -f -N "${RDP_SSH_JUMPBOX}"

	# Verify SSH started
	sshPid="$(ssh -S "${sshControlSocket}" -O check /dev/null 2>&1 | grep 'pid=' | sed 's@.*pid=@@;s@).*$@@')"
	if [ -z "${sshPid}" ]; then
		echo "Unable to start SSH" >&2

		exit 1
	fi

	hostId="${hostName}:${hostPort}!${RDP_SSH_JUMPBOX}"

	hostName='localhost'
	hostPort="${randomPort}"
else
	function cleanup() {
		return 0
	}

	hostId="${hostName}:${hostPort}"
fi

trap cleanup EXIT

# Connect to the RDP session
## Determine username and domain
if [ -n "${RDP_USERNAME}" ]; then
	addArgs=("${addArgs[@]}" -u "${RDP_USERNAME}")
else
	addArgs=("${addArgs[@]}" -u "$(whoami)")
fi

if [ -n "${RDP_DOMAIN}" ]; then
	addArgs=("${addArgs[@]}" -d "${RDP_DOMAIN}")
fi

## Determine port
if [ -n "${hostPort}" -a "${hostPort}" != '3389' ]; then
	addArgs=("${addArgs[@]}" -t "${hostPort}")
fi

## Export the password to the environment and tell xfreerdp to use
## it.  This requires a patched xfreerdp.
if [ -n "${RDP_PASSWORD}" ]; then
	export RDP_PASSWORD

	addArgs=("${addArgs[@]}" -p ENV:RDP_PASSWORD)
fi

## Actually call xfreerdp
xfreerdp --plugin drdynvc --plugin rdpdr --data scard "disk:pwd:$(pwd)" -- --certificate-name "${hostId}" "${addArgs[@]}" "$@" "${hostName}"
returnCode="$?"

exit "${returnCode}"