ss_vncviewer 29.2 KB
Newer Older
runge's avatar
runge committed
1 2
#!/bin/sh
#
runge's avatar
runge committed
3 4
# ss_vncviewer:  wrapper for vncviewer to use an stunnel SSL tunnel
#                or an SSH tunnel.
runge's avatar
runge committed
5
#
6
# Copyright (c) 2006-2007 by Karl J. Runge <runge@karlrunge.com>
runge's avatar
runge committed
7 8 9 10 11 12 13 14 15 16
#
# You must have stunnel(8) installed on the system and in your PATH
# (however, see the -ssh option below, in which case you will need ssh(1)
# installed)  Note: stunnel is usually installed in an "sbin" subdirectory.
#
# You should have "x11vnc -ssl ..." or "x11vnc -stunnel ..." 
# already running as the VNC server on the remote machine.
# (or use stunnel on the server side for any other VNC server)
#
#
runge's avatar
runge committed
17
# Usage: ss_vncviewer [cert-args] host:display <vncviewer-args>
runge's avatar
runge committed
18
#
runge's avatar
runge committed
19 20
# e.g.:  ss_vncviewer snoopy:0
#        ss_vncviewer snoopy:0 -encodings "copyrect tight zrle hextile"
runge's avatar
runge committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#
# [cert-args] can be:
#
#	-verify /path/to/cacert.pem		
#	-mycert /path/to/mycert.pem		
#	-proxy  host:port
#
# -verify specifies a CA cert PEM file (or a self-signed one) for
#         authenticating the VNC server.
#
# -mycert specifies this client's cert+key PEM file for the VNC server to
#	  authenticate this client. 
#
# -proxy  try host:port as a Web proxy to use the CONNECT method
#         to reach the VNC server (e.g. your firewall requires a proxy).
#
#         For the "double proxy" case use -proxy host1:port1,host2:port2
#         (the first CONNECT is done through host1:port1 to host2:port2
#         and then a 2nd CONNECT to the destination VNC server.)
#
runge's avatar
runge committed
41 42 43
# -showcert  Only fetch the certificate using the 'openssl s_client'
#            command (openssl(1) must in installed).
#
runge's avatar
runge committed
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
#    See http://www.karlrunge.com/x11vnc/#faq-ssl-ca for details on SSL
#    certificates with VNC.
#
# A few other args (not related to SSL and certs):
#
# -ssh    Use ssh instead of stunnel SSL.  ssh(1) must be installed and you
#         must be able to log into the remote machine via ssh.
#
#         In this case "host:display" may be of the form "user@host:display"
#         where "user@host" is used for the ssh login (see ssh(1) manpage).
#
#         If -proxy is supplied it can be of the forms: "gwhost" "gwhost:port"
#         "user@gwhost" or "user@gwhost:port".  "gwhost" is an incoming ssh 
#         gateway machine (the VNC server is not running there), an ssh -L
#         redir is used to "host" in "host:display" from "gwhost". Any "user@"
#         part must be in the -proxy string (not in "host:display").
#
#         Under -proxy use "gwhost:port" if connecting to any ssh port
#         other than the default (22).  (even for the non-gateway case,
#         -proxy must be used to specify a non-standard ssh port)
#
#         A "double ssh" can be specified via a -proxy string with the two
#         hosts separated by a comma:
#
#             [user1@]host1[:port1],[user2@]host2[:port2]
#
#         in which case a ssh to host1 and thru it via a -L redir a 2nd
#         ssh is established to host2.  
#
#         Examples:
#
runge's avatar
runge committed
75 76
#         ss_vncviewer -ssh bob@bobs-home.net:0
#         ss_vncviewer -ssh -sshcmd 'x11vnc -localhost' bob@bobs-home.net:0
runge's avatar
runge committed
77
#
runge's avatar
runge committed
78 79
#         ss_vncviewer -ssh -proxy fred@mygate.com:2022 mymachine:0
#         ss_vncviewer -ssh -proxy bob@bobs-home.net:2222 localhost:0
runge's avatar
runge committed
80
#
runge's avatar
runge committed
81
#         ss_vncviewer -ssh -proxy fred@gw-host,fred@peecee localhost:0
runge's avatar
runge committed
82 83 84 85 86 87 88 89 90 91 92 93 94
#
# -sshcmd cmd   Run "cmd" via ssh instead of the default "sleep 15"
#               e.g. -sshcmd 'x11vnc -display :0 -localhost -rfbport 5900'
#
# -sshargs "args"  pass "args" to the ssh process, e.g. -L/-R port redirs.
#
# -sshssl Tunnel the SSL connection thru a SSH connection.  The tunnel as
#         under -ssh is set up and the SSL connection goes thru it.  Use
#         this if you want to have and end-to-end SSL connection but must
#         go thru a SSH gateway host (e.g. not the vnc server).  Or use
#         this if you need to tunnel additional services via -R and -L 
#         (see -sshargs above).
#
runge's avatar
runge committed
95
#         ss_vncviewer -sshssl -proxy fred@mygate.com mymachine:0
runge's avatar
runge committed
96
#
97
# -listen (or -reverse) set up a reverse connection.
runge's avatar
runge committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#
# -alpha  turn on cursor alphablending hack if you are using the
#         enhanced tightvnc vncviewer.
#
# -grab   turn on XGrabServer hack if you are using the enhanced tightvnc
#         vncviewer (e.g. for fullscreen mode in some windowmanagers like
#         fvwm that do not otherwise work in fullscreen mode)
#
#
# set VNCVIEWERCMD to whatever vncviewer command you want to use.
#
VNCIPCMD=${VNCVIEWERCMD:-vncip}
VNCVIEWERCMD=${VNCVIEWERCMD:-vncviewer}
#
# Same for STUNNEL, e.g. set it to /path/to/stunnel or stunnel4, etc.
#

runge's avatar
runge committed
115 116 117
if [ "X$SS_DEBUG" != "X" ]; then
	set -xv 
fi
118

runge's avatar
runge committed
119 120 121 122 123 124 125 126 127 128 129 130
PATH=$PATH:/usr/sbin:/usr/local/sbin:/dist/sbin; export PATH

if [ "X$STUNNEL" = "X" ]; then
	type stunnel4 > /dev/null 2>&1
	if [ $? = 0 ]; then
		STUNNEL=stunnel4
	else
		STUNNEL=stunnel
	fi
fi

help() {
131
	tail -n +2 "$0" | sed -e '/^$/ q'
runge's avatar
runge committed
132 133 134 135 136 137 138
}

gotalpha=""
use_ssh=""
use_sshssl=""
direct_connect=""
ssh_sleep=15
139 140 141 142
if echo "$*" | grep '.*-listen' > /dev/null; then
	ssh_sleep=1800
fi
ssh_cmd=""
runge's avatar
runge committed
143 144
if [ "X$SS_VNCVIEWER_SSH_CMD" != "X" ]; then
	ssh_cmd="$SS_VNCVIEWER_SSH_CMD"
runge's avatar
runge committed
145 146
fi
ssh_args=""
runge's avatar
runge committed
147
showcert=""
148
reverse=""
runge's avatar
runge committed
149

runge's avatar
runge committed
150
if [ "X$1" = "X-viewerflavor" ]; then
runge's avatar
runge committed
151 152 153 154
	if echo "$VNCVIEWERCMD" | egrep -i '^(xmessage|sleep )' > /dev/null; then
		echo "unknown"
		exit 0
	fi
runge's avatar
runge committed
155 156 157 158
	if echo "$VNCVIEWERCMD" | grep -i chicken.of > /dev/null; then
		echo "cotvnc"
		exit 0
	fi
runge's avatar
runge committed
159 160 161 162
	if echo "$VNCVIEWERCMD" | grep -i ultra > /dev/null; then
		echo "ultravnc"
		exit 0
	fi
163
	str=`$VNCVIEWERCMD -h 2>&1 | head -n 5`
runge's avatar
runge committed
164 165 166 167 168 169 170 171 172 173 174 175
	if echo "$str" | grep -i 'TightVNC.viewer' > /dev/null; then
		echo "tightvnc"
	elif echo "$str" | grep -i 'RealVNC.Ltd' > /dev/null; then
		echo "realvnc4"
	elif echo "$str" | grep -i 'VNC viewer version 3' > /dev/null; then
		echo "realvnc3"
	else
		echo "unknown"
	fi
	exit 0
fi

runge's avatar
runge committed
176 177 178 179 180 181
if [ "X$SS_VNCVIEWER_NO_MAXCONN" != "X" ]; then
	STUNNEL_EXTRA_OPTS=`echo "$STUNNEL_EXTRA_OPTS" | sed -e 's/maxconn/#maxconn/'`
elif echo "$VNCVIEWERCMD" | egrep -i '^(xmessage|sleep )' > /dev/null; then
	STUNNEL_EXTRA_OPTS=`echo "$STUNNEL_EXTRA_OPTS" | sed -e 's/maxconn/#maxconn/'`
fi

runge's avatar
runge committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
# grab our cmdline options:
while [ "X$1" != "X" ]
do
    case $1 in 
	"-verify")	shift; verify="$1"
                ;;
	"-mycert")	shift; mycert="$1"
                ;;
	"-proxy")	shift; proxy="$1"
                ;;
	"-ssh")		use_ssh=1
                ;;
	"-sshssl")	use_ssh=1
			use_sshssl=1
                ;;
	"-sshcmd")	shift; ssh_cmd="$1"
                ;;
	"-sshargs")	shift; ssh_args="$1"
                ;;
	"-alpha")	gotalpha=1
                ;;
runge's avatar
runge committed
203 204
	"-showcert")	showcert=1
                ;;
205 206 207 208
	"-listen")	reverse=1
                ;;
	"-reverse")	reverse=1
                ;;
runge's avatar
runge committed
209 210 211 212 213 214 215 216 217 218 219 220
	"-grab")	VNCVIEWER_GRAB_SERVER=1; export VNCVIEWER_GRAB_SERVER
                ;;
	"-h"*)	help; exit 0
                ;;
	"--h"*)	help; exit 0
                ;;
	*)	break
                ;;
    esac
    shift
done

221 222 223 224 225
targ="-t"
if [ "X$SS_VNCVIEWER_NO_T" != "X" ]; then
	targ=""
fi

runge's avatar
runge committed
226 227 228 229
if [ "X$gotalpha" = "X1" ]; then
	VNCVIEWER_ALPHABLEND=1
	export VNCVIEWER_ALPHABLEND
else
runge's avatar
runge committed
230 231 232
	NO_ALPHABLEND=1
	export NO_ALPHABLEND
fi
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
if [ "X$reverse" != "X" ]; then
	ssh_sleep=1800
	if [ "X$proxy" != "X" ]; then
		if [ "X$use_ssh" = "X" -a "X$use_sshssl" = "X" ]; then
			echo ""
			echo "*Warning*: SSL -listen and a Web proxy does not make sense."
			sleep 3
		elif echo "$proxy" | grep "," > /dev/null; then
			:
		else
			echo ""
			echo "*Warning*: -listen and a single proxy/gateway does not make sense."
			sleep 3
		fi
	fi
fi
if [ "X$ssh_cmd" = "X" ]; then
	ssh_cmd="sleep $ssh_sleep"
fi
runge's avatar
runge committed
252 253 254 255 256 257 258 259 260 261 262

orig="$1"
shift

if [ "X$use_ssh" = "X1" -a "X$use_sshssl" = "X" ]; then
	if [ "X$mycert" != "X" -o "X$verify" != "X" ]; then
		echo "-mycert and -verify cannot be used in -ssh mode" 
		exit 1
	fi
fi

263 264 265 266 267 268
if echo "$orig" | grep '^V[Nn][Cc]://' > /dev/null; then
	SSVNC_NO_ENC_WARN=1
	export SSVNC_NO_ENC_WARN
	orig=`echo "$orig" | sed -e 's/^...:/vnc:/'`
fi

runge's avatar
runge committed
269 270 271 272 273 274 275
if echo "$orig" | grep '^vnc://' > /dev/null; then
	orig=`echo "$orig" | sed -e 's,vnc://,,'`
	verify=""
	mycert=""
	use_ssh=""
	use_sshssl=""
	direct_connect=1
276 277
elif echo "$orig" | grep '^vncs://' > /dev/null; then
	orig=`echo "$orig" | sed -e 's,vncs://,,'`
runge's avatar
runge committed
278 279
elif echo "$orig" | grep '^vncssl://' > /dev/null; then
	orig=`echo "$orig" | sed -e 's,vncssl://,,'`
280 281
elif echo "$orig" | grep '^vnc+ssl://' > /dev/null; then
	orig=`echo "$orig" | sed -e 's,vnc.ssl://,,'`
runge's avatar
runge committed
282 283 284
elif echo "$orig" | grep '^vncssh://' > /dev/null; then
	orig=`echo "$orig" | sed -e 's,vncssh://,,'`
	use_ssh=1
285 286 287
elif echo "$orig" | grep '^vnc+ssh://' > /dev/null; then
	orig=`echo "$orig" | sed -e 's,vnc.ssh://,,'`
	use_ssh=1
runge's avatar
runge committed
288 289 290 291
fi
if [ "X$reverse" != "X" -a "X$direct_connect" = "X" ]; then
	VNCVIEWER_LISTEN_LOCALHOST=1
	export VNCVIEWER_LISTEN_LOCALHOST
runge's avatar
runge committed
292 293
fi

294 295 296 297 298 299 300 301 302 303 304 305
rsh=""
if echo "$orig" | grep '^rsh://' > /dev/null; then
	use_ssh=1
	rsh=1
	orig=`echo "$orig" | sed -e 's,rsh://,,'`
elif echo "$orig" | grep '^rsh:' > /dev/null; then
	use_ssh=1
	rsh=1
	orig=`echo "$orig" | sed -e 's,rsh:,,'`
fi


runge's avatar
runge committed
306 307 308 309
# play around with host:display port:
if echo "$orig" | grep ':' > /dev/null; then
	:
else
310 311
	if [ "X$reverse" = "X" ]; then
		orig="$orig:0"
runge's avatar
runge committed
312 313
	elif [ "X$orig" = "X" ]; then
		orig=":0"
314
	fi
runge's avatar
runge committed
315 316 317 318 319 320 321
fi

host=`echo "$orig" | awk -F: '{print $1}'`
disp=`echo "$orig" | awk -F: '{print $2}'`
if [ "X$host" = "X" ]; then
	host=localhost
fi
322 323 324 325 326 327 328 329
if [ $disp -lt 0 ]; then
	port=`expr 0 - $disp`
elif [ $disp -lt 200 ]; then
	if [ "X$reverse" = "X" ]; then
		port=`expr $disp + 5900`
	else
		port=`expr $disp + 5500`
	fi
runge's avatar
runge committed
330 331 332 333 334 335 336 337 338 339
else
	port=$disp
fi

# try to find an open listening port via netstat(1):
inuse=""
if uname | grep Linux > /dev/null; then
	inuse=`netstat -ant | egrep 'LISTEN|WAIT|ESTABLISH|CLOSE' | awk '{print $4}' | sed 's/^.*://'`
elif uname | grep SunOS > /dev/null; then
	inuse=`netstat -an -f inet -P tcp | grep LISTEN | awk '{print $1}' | sed 's/^.*\.//'`
340 341
elif uname | grep -i bsd > /dev/null; then
	inuse=`netstat -ant -f inet | grep LISTEN | awk '{print $4}' | sed 's/^.*\.//'`
runge's avatar
runge committed
342 343 344 345 346
# add others...
fi

date_sec=`date +%S`

runge's avatar
runge committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
if echo "$VNCVIEWERCMD" | grep '^sleep[ 	][ 	]*[0-9][0-9]*' > /dev/null; then
	if [ "X$SS_VNCVIEWER_LISTEN_PORT" = "X" ]; then
		p=`echo "$VNCVIEWERCMD" | awk '{print $3}'`
		if [ "X$p" != "X" ]; then
			SS_VNCVIEWER_LISTEN_PORT=$p
		fi
	fi
	p2=`echo "$VNCVIEWERCMD" | awk '{print $2}'`
	VNCVIEWERCMD="eval sleep $p2; echo Local "
elif echo "$VNCVIEWERCMD" | grep '^xmessage[ 	][ 	]*[0-9][0-9]*' > /dev/null; then
	if [ "X$SS_VNCVIEWER_LISTEN_PORT" = "X" ]; then
		p=`echo "$VNCVIEWERCMD" | awk '{print $2}'`
		SS_VNCVIEWER_LISTEN_PORT=$p
	fi
fi

runge's avatar
runge committed
363 364 365 366 367
findfree() {
	try0=$1
	try=$try0
	use0=""

runge's avatar
runge committed
368 369 370 371 372
	if [ "X$SS_VNCVIEWER_LISTEN_PORT" != "X" ]; then
		echo "$SS_VNCVIEWER_LISTEN_PORT"
		return	
	fi

runge's avatar
runge committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	while [ $try -lt 6000 ]
	do
		if [ "X$inuse" = "X" ]; then
			break
		fi
		if echo "$inuse" | grep -w $try > /dev/null; then
			:
		else
			use0=$try
			break
		fi
		try=`expr $try + 1`
	done
	if [ "X$use0" = "X" ]; then
		use0=`expr $date_sec + $try0`
	fi

	echo $use0
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
final() {
	echo ""
	if [ "X$SS_VNCVIEWER_RM" != "X" ]; then
		rm -f $SS_VNCVIEWER_RM 2>/dev/null
	fi
	if [ "X$tcert" != "X" ]; then
		rm -f $tcert
	fi
	if [ "X$pssh" != "X" ]; then
		echo "Terminating background ssh process"
		echo kill -TERM "$pssh"
		kill -TERM "$pssh" 2>/dev/null
		sleep 1
		kill -KILL "$pssh" 2>/dev/null
		pssh=""
	fi
	if [ "X$stunnel_pid" != "X" ]; then
		echo "Terminating background stunnel process"
		echo kill -TERM "$stunnel_pid"
		kill -TERM "$stunnel_pid" 2>/dev/null
		sleep 1
		kill -KILL "$stunnel_pid" 2>/dev/null
		stunnel_pid=""
	fi
417 418 419
	if [ "X$tail_pid" != "X" ]; then
		kill -TERM $tail_pid
	fi
420
}
runge's avatar
runge committed
421

422 423 424 425 426 427 428
if [ "X$reverse" = "X" ]; then
	use=`findfree 5930`
	if [ $use -ge 5900 ]; then
		N=`expr $use - 5900`
	else
		N=$use
	fi
runge's avatar
runge committed
429
else
430 431 432 433 434 435 436
	p2=`expr $port + 30`
	use=`findfree $p2`
	if [ $use -ge 5500 ]; then
		N=`expr $use - 5500`
	else
		N=$use
	fi
runge's avatar
runge committed
437 438 439 440 441 442
fi

if echo "$0" | grep vncip > /dev/null; then
	VNCVIEWERCMD="$VNCIPCMD"
fi

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
rchk() {
	if [ "X$BASH_VERSION" = "X" ]; then
		RANDOM=`date +%S``sh -c 'echo $$'``ps -elf 2>&1 | sum 2>&1 | awk '{print $1}'`
	fi
}
rchk

mytmp() {
	tf=$1
	rm -rf "$tf" || exit 1
	if [ -d "$tf" ]; then
		echo "tmp file $tf still exists as a directory."
		exit 1
	elif [ -L "$tf" ]; then
		echo "tmp file $tf still exists as a symlink."
		exit 1
	elif [ -f "$tf" ]; then
		echo "tmp file $tf still exists."
		exit 1
	fi
	touch "$tf" || exit 1
	chmod 600 "$tf" || exit 1
	rchk
}

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
rsh_setup() {
	if echo "$ssh_host" | grep '@' > /dev/null; then
		ul=`echo "$ssh_host" | awk -F@ '{print $1}'`
		ul="-l $ul"
		ssh_host=`echo "$ssh_host" | awk -F@ '{print $2}'`
	else
		ul=""
	fi
	ssh_cmd=`echo "$ssh_cmd" | sed -e 's/ -localhost/ /g'`
}

rsh_viewer() {
	trap "final" 0 2 15
	if [ "X$PORT" = "X" ]; then
		exit 1
	elif [ $PORT -ge 5900 ]; then
		vdpy=`expr $PORT - 5900`
	else
		vdpy=":$PORT"
	fi
	stty sane
	echo "$VNCVIEWERCMD" "$@" $ssh_host:$vdpy
	echo ""
	$VNCVIEWERCMD "$@" $ssh_host:$vdpy
}

runge's avatar
runge committed
494 495 496 497 498
if [ "X$use_ssh" = "X1" ]; then
	ssh_port="22"
	ssh_host="$host"
	vnc_host="localhost"
	ssh=${SSH:-"ssh -x"}
499

runge's avatar
runge committed
500 501 502 503 504 505
	if echo "$proxy" | grep "," > /dev/null; then
		proxy1=`echo "$proxy" | awk -F, '{print $1}'`
		proxy2=`echo "$proxy" | awk -F, '{print $2}'`
		# user1@gw1.com:port1,user2@ws2:port2
		ssh_host1=`echo "$proxy1" | awk -F: '{print $1}'`
		ssh_port1=`echo "$proxy1" | awk -F: '{print $2}'`
506 507
		if [ "X$ssh_port1" != "X" ]; then
			ssh_port1="-p 22"
runge's avatar
runge committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
		fi
		ssh_host2=`echo "$proxy2" | awk -F: '{print $1}'`
		ssh_user2=`echo "$ssh_host2" | awk -F@ '{print $1}'`
		ssh_host2=`echo "$ssh_host2" | awk -F@ '{print $2}'`
		if [ "X$ssh_host2" = "X" ]; then
			ssh_host2=$ssh_user2
			ssh_user2=""
		else
			ssh_user2="${ssh_user2}@"
		fi
		ssh_port2=`echo "$proxy2" | awk -F: '{print $2}'`
		if [ "X$ssh_port2" = "X" ]; then
			ssh_port2="22"
		fi
		proxport=`findfree 3500`
		echo
		echo "Running 1st ssh proxy:"
525 526
		echo "$ssh -f -x $ssh_port1 $targ -e none -L $proxport:$ssh_host2:$ssh_port2 $ssh_host1 \"sleep 30\""
		      $ssh -f -x $ssh_port1 $targ -e none -L $proxport:$ssh_host2:$ssh_port2 $ssh_host1 "sleep 30"
runge's avatar
runge committed
527 528 529 530 531
		ssh_args="$ssh_args -o NoHostAuthenticationForLocalhost=yes"
		sleep 1
		stty sane
		proxy="${ssh_user2}localhost:$proxport"
	fi
532

runge's avatar
runge committed
533 534 535 536 537 538 539 540 541 542 543
	if [ "X$proxy" != "X" ]; then
		ssh_port=`echo "$proxy" | awk -F: '{print $2}'`
		if [ "X$ssh_port" = "X" ]; then
			ssh_port="22"
		fi
		ssh_host=`echo "$proxy" | awk -F: '{print $1}'`
		vnc_host="$host"
	fi
	echo ""
	echo "Running ssh:"
	sz=`echo "$ssh_cmd" | wc -c`
544
	if [ "$sz" -gt 300 ]; then
runge's avatar
runge committed
545 546 547 548 549 550
		info="..."
	else
		info="$ssh_cmd"
	fi

	C=""
runge's avatar
runge committed
551
	if [ "X$SS_VNCVIEWER_USE_C" != "X" ]; then
runge's avatar
runge committed
552 553
		C="-C"
	fi
554 555

	getport=""
556
	teeport=""
557 558
	if echo "$ssh_cmd" | egrep "^(PORT=|P=)" > /dev/null; then
		getport=1
559 560 561 562
		if echo "$ssh_cmd" | egrep "^P=" > /dev/null; then
			teeport=1
		fi

563 564 565 566 567 568 569 570 571 572
		PORT=""
		ssh_cmd=`echo "$ssh_cmd" | sed -e 's/^PORT=[ 	]*//' -e 's/^P=//'`
		SSVNC_NO_ENC_WARN=1
		if [ "X$use_sshssl" = "X" ]; then
			direct_connect=1
		fi
	fi
	if [ "X$getport" != "X" ]; then
		ssh_redir="-D ${use}"
	elif [ "X$reverse" = "X" ]; then
573 574 575 576 577
		ssh_redir="-L ${use}:${vnc_host}:${port}"
	else
		ssh_redir="-R ${port}:${vnc_host}:${use}"
	fi
	pmark=`sh -c 'echo $$'`
578

runge's avatar
runge committed
579
	# the -t option actually speeds up typing response via VNC!!
580 581 582 583 584
	if [ "X$ssh_port" = "X22" ]; then
		ssh_port=""
	else
		ssh_port="-p $ssh_port"
	fi
585

runge's avatar
runge committed
586
	if [ "X$SS_VNCVIEWER_SSH_ONLY" != "X" ]; then
587
		echo "$ssh -x $ssh_port $targ $C $ssh_args $ssh_host \"$info\""
runge's avatar
runge committed
588
		echo ""
589
		$ssh -x $ssh_port $targ $C $ssh_args $ssh_host "$ssh_cmd"
runge's avatar
runge committed
590
		exit $?
591

runge's avatar
runge committed
592
	elif [ "X$SS_VNCVIEWER_NO_F" != "X" ]; then
593
		echo "$ssh -x $ssh_port $targ $C $ssh_redir $ssh_args $ssh_host \"$info\""
runge's avatar
runge committed
594
		echo ""
595
		$ssh -x $ssh_port $targ $C $ssh_redir $ssh_args $ssh_host "$ssh_cmd"
596 597 598 599 600
		rc=$?

	elif [ "X$getport" != "X" ]; then
		tport=/tmp/tport${RANDOM}.$$
		mytmp $tport
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627

		if [ "X$rsh" != "X1" ]; then
			if echo "$ssh_cmd" | grep -w sudo > /dev/null; then
				echo ""
				echo "Initial ssh with 'sudo id' to prime sudo so hopefully the next one"
				echo "will require no password..."
				echo ""
				targ="-t"
				$ssh -x $ssh_port $targ $ssh_args $ssh_host "sudo id; tty"
				echo ""
			fi
			echo "$ssh -x -f $ssh_port $targ $C $ssh_redir $ssh_args $ssh_host \"$info\""
			echo ""
			$ssh -x -f $ssh_port $targ $C $ssh_redir $ssh_args $ssh_host "$ssh_cmd" > $tport 
			if [ "X$teeport" = "X1" ]; then
				tail -f $tport 1>&2 &
				tail_pid=$!
			fi
			rc=$?
		else
			rsh_setup
			echo "rsh $ul $ssh_host \"$ssh_cmd\""
			echo ""
			rsh $ul $ssh_host "$ssh_cmd" > $tport &
			sleep 1
			rc=0
		fi
628

629 630 631 632
		if [ "X$SSVNC_EXTRA_SLEEP" != "X" ]; then
			sleep $SSVNC_EXTRA_SLEEP
		fi

633 634 635 636
		stty sane
		i=0
		while [ $i -lt 10 ]; do
			sleep 1
637
			PORT=`grep "^PORT=" $tport | head -n 1 | sed -e 's/PORT=//' -e 's/\r//g'`
638 639 640
			if echo "$PORT" | grep '^[0-9][0-9]*$' > /dev/null; then
				break
			fi
641
			vnss=`sed -e 's/\r//g' $tport | egrep -i '^(New.* desktop is|A VNC server is already running).*:[0-9[0-9]*$' | head -n 1 | awk '{print $NF}'`
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
			if [ "X$vnss" != "X" ]; then
				PORT=`echo "$vnss" | awk -F: '{print $2}'`
				if echo "$PORT" | grep '^[0-9][0-9]*$' > /dev/null; then
					if [ $PORT -lt 100 ]; then
						PORT=`expr $PORT + 5900`
					fi
				fi
				if echo "$PORT" | grep '^[0-9][0-9]*$' > /dev/null; then
					break
				fi
			fi
			i=`expr $i + 1`
		done

		echo "PORT=$PORT" 1>&2
657 658 659 660 661
		rm -f $tport
		if [ "X$rsh" = "X1" ]; then
			rsh_viewer "$@"
			exit $?
		fi
662 663 664 665 666 667
		PPROXY_SOCKS=1
		export PPROXY_SOCKS
		host="localhost"
		port="$PORT"
		proxy="localhost:$use"

runge's avatar
runge committed
668
	else
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
		if [ "X$rsh" != "X1" ]; then
			echo "$ssh -x -f $ssh_port $targ $C $ssh_redir $ssh_args $ssh_host \"$info\""
			echo ""
			$ssh -x -f $ssh_port $targ $C $ssh_redir $ssh_args $ssh_host "$ssh_cmd"
			rc=$?
		else
			rsh_setup
			echo "rsh $ul $ssh_host \"$ssh_cmd\""
			echo ""
			rsh $ul $ssh_host "$ssh_cmd" &
			sleep 1
			PORT=$port
			rsh_viewer "$@"
			exit $?
		fi
runge's avatar
runge committed
684
	fi
685 686

	if [ "$rc" != "0" ]; then
runge's avatar
runge committed
687 688 689 690
		echo ""
		echo "ssh to $ssh_host failed."
		exit 1
	fi
691 692 693 694 695 696 697 698 699 700 701 702 703
	stty sane

	c=0
	pssh=""
	while [ $c -lt 30 ]
	do
		p=`expr $pmark + $c`
		if ps -p "$p" 2>&1 | grep "$ssh" > /dev/null; then
			pssh=$p
			break
		fi
		c=`expr $c + 1`
	done
704 705 706
	if [ "X$getport" != "X" ]; then
		:
	elif [ "X$ssh_cmd" = "Xsleep $ssh_sleep" ] ; then
runge's avatar
runge committed
707 708 709 710 711 712
		sleep 1
	else
		# let any command get started a bit.
		sleep 5
	fi
	echo ""
713 714 715
	if [ "X$SSVNC_EXTRA_SLEEP" != "X" ]; then
		sleep $SSVNC_EXTRA_SLEEP
	fi
runge's avatar
runge committed
716 717
	#reset
	stty sane
718
	#echo "pssh=\"$pssh\""
719
	if [ "X$use_sshssl" = "X" -a "X$getport" = "X" ]; then
runge's avatar
runge committed
720
		echo "Running viewer:"
721 722 723 724 725 726 727 728 729 730 731 732 733 734

		trap "final" 0 2 15
		if [ "X$reverse" = "X" ]; then
			echo "$VNCVIEWERCMD" "$@" localhost:$N
			echo ""
			$VNCVIEWERCMD "$@" localhost:$N
		else
			echo ""
			echo "NOTE: Press Ctrl-C to terminate viewer LISTEN mode."
			echo ""
			echo "$VNCVIEWERCMD" "$@" -listen $N
			echo ""
			$VNCVIEWERCMD "$@" -listen $N
		fi
runge's avatar
runge committed
735 736 737 738 739 740 741 742 743 744

		exit $?
	else
		use2=`findfree 5960`
		host0=$host
		port0=$port
		host=localhost
		port=$use
		use=$use2
		N=`expr $use - 5900`
745 746 747 748 749 750
		if [ "X$getport" != "X" ]; then
			host="$host0"
			port="$port0"
		else
			proxy=""
		fi
runge's avatar
runge committed
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
	fi
fi

# create the stunnel config file:
if [ "X$verify" != "X" ]; then
	if [ -d $verify ]; then
		verify="CApath = $verify"
	else
		verify="CAfile = $verify"
	fi
	verify="$verify
verify = 2"
fi
if [ "X$mycert" != "X" ]; then
	cert="cert = $mycert"
fi

pcode() {
	tf=$1
770 771
	PPROXY_PROXY=$proxy; export PPROXY_PROXY
	PPROXY_DEST="$host:$port"; export PPROXY_DEST
runge's avatar
runge committed
772 773
	cod='#!/usr/bin/perl

774
# A hack to glue stunnel to a Web proxy or SOCKS for client connections.
runge's avatar
runge committed
775 776 777

use IO::Socket::INET;

778
my ($first, $second) = split(/,/, $ENV{PPROXY_PROXY});
runge's avatar
runge committed
779
my ($proxy_host, $proxy_port) = split(/:/, $first);
780
my $connect = $ENV{PPROXY_DEST};
runge's avatar
runge committed
781

782
print STDERR "PPROXY v0.1: a tool for Web proxies and SOCKS connections.\n";
runge's avatar
runge committed
783 784 785
print STDERR "proxy_host:       $proxy_host\n";
print STDERR "proxy_port:       $proxy_port\n";
print STDERR "proxy_connect:    $connect\n";
786 787
print STDERR "pproxy_listen:    $ENV{PPROXY_LISTEN}\n";
print STDERR "\n";
runge's avatar
runge committed
788 789

my $listen_handle = "";
790
if ($ENV{PPROXY_LISTEN} != "") {
runge's avatar
runge committed
791 792 793
	my $listen_sock = IO::Socket::INET->new(
		Listen    => 2,
		LocalAddr => "localhost",
794 795 796
		LocalPort => $ENV{PPROXY_LISTEN},
		Proto     => "tcp"
	);
runge's avatar
runge committed
797
	if (! $listen_sock) {
798
		die "pproxy: $!\n";
runge's avatar
runge committed
799 800 801 802
	}
	my $ip;
	($listen_handle, $ip) = $listen_sock->accept();
	if (! $listen_handle) {
803
		die "pproxy: $!\n";
runge's avatar
runge committed
804 805 806 807 808 809
	}
}

my $sock = IO::Socket::INET->new(
	PeerAddr => $proxy_host,
	PeerPort => $proxy_port,
810 811
	Proto => "tcp"
);
runge's avatar
runge committed
812 813 814

if (! $sock) {
	unlink($0);
815
	die "pproxy: $!\n";
runge's avatar
runge committed
816 817 818
}

my $con = "";
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
my $con0 = "";
if ($ENV{PPROXY_SOCKS} ne "") {
	$second = "";
	my ($h, $p) = split(/:/, $connect);
	$con .= pack("C", 0x04);
	$con .= pack("C", 0x01);
	$con .= pack("n", $p);

	my $SOCKS_4a = 0;
	if ($h eq "localhost" || $h eq "127.0.0.1") {
		$con .= pack("C", 127);
		$con .= pack("C", 0);
		$con .= pack("C", 0);
		$con .= pack("C", 1);
	} elsif ($h =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {
		$con .= pack("C", $1);
		$con .= pack("C", $2);
		$con .= pack("C", $3);
		$con .= pack("C", $4);
	} else {
		$con .= pack("C", 0);
		$con .= pack("C", 0);
		$con .= pack("C", 0);
		$con .= pack("C", 3);
		$SOCKS_4a = 1;
	}

	$con .= "nobody";
	$con .= pack("C", 0);

	if ($SOCKS_4a) {
		$con .= $h;
		$con .= pack("C", 0);
	}
	$con0 = "SOCKS4 via $proxy_host:$proxy_port to $h:$p\n\n";

} elsif ($second ne "") {
runge's avatar
runge committed
856 857
	$con = "CONNECT $second HTTP/1.1\r\n";
	$con   .= "Host: $second\r\n\r\n";
858
	$con0 = $con;
runge's avatar
runge committed
859 860 861
} else {
	$con = "CONNECT $connect HTTP/1.1\r\n";
	$con   .= "Host: $connect\r\n\r\n";
862
	$con0 = $con;
runge's avatar
runge committed
863 864
}

865
print STDERR "proxy_request1: $con0";
runge's avatar
runge committed
866 867 868 869 870
print $sock $con;

unlink($0);

my $rep = "";
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
if ($ENV{PPROXY_SOCKS} ne "") {
	$rep = "HTTP/1.0 200";
	for (my $i = 0; $i < 8; $i++) {
		my $c;
		sysread($sock, $c, 1);
		my $s = unpack("C", $c);
		if ($i == 0) {
			$rep = "" if $s != 0x0;
		} elsif ($i == 1) {
			$rep = "" if $s != 0x5a;
		}
	}
} else {
	while ($rep !~ /\r\n\r\n/) {
		my $c;
		sysread($sock, $c, 1);
		print STDERR $c;
		$rep .= $c;
	}
runge's avatar
runge committed
890 891 892 893 894 895 896 897
}
if ($rep !~ m,HTTP/.* 200,) {
	die "proxy error: $rep\n";
}

if ($second ne "") {
	$con = "CONNECT $connect HTTP/1.1\r\n";
	$con   .= "Host: $connect\r\n\r\n";
898
	print STDERR "proxy_request2: $con";
runge's avatar
runge committed
899 900 901 902 903

	print $sock $con;

	$rep = "";
	while ($rep !~ /\r\n\r\n/) {
904 905
		my $c;
		sysread($sock, $c, 1);
runge's avatar
runge committed
906 907 908 909 910 911 912 913 914
		print STDERR $c;
		$rep .= $c;
	}
	if ($rep !~ m,HTTP/.* 200,) {
		die "proxy error: $rep\n";
	}
}

if (fork) {
915
	print STDERR "pproxy parent\[$$]  STDIN -> socket\n";
runge's avatar
runge committed
916 917 918 919 920 921
	if ($listen_handle) {
		xfer($listen_handle, $sock);
	} else {
		xfer(STDIN, $sock);
	}
} else {
922
	print STDERR "pproxy child \[$$]  socket -> STDOUT\n";
runge's avatar
runge committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
	if ($listen_handle) {
		xfer($sock, $listen_handle);
	} else {
		xfer($sock, STDOUT);
	}
}
exit;

sub xfer {
	my($in, $out) = @_;
	$RIN = $WIN = $EIN = "";
	$ROUT = "";
	vec($RIN, fileno($in), 1) = 1;
	vec($WIN, fileno($in), 1) = 1;
	$EIN = $RIN | $WIN;

	while (1) {
		my $nf = 0;
		while (! $nf) {
			$nf = select($ROUT=$RIN, undef, undef, undef);
		}
		my $len = sysread($in, $buf, 8192);
		if (! defined($len)) {
			next if $! =~ /^Interrupted/;
947
			print STDERR "pproxy\[$$]: $!\n";
runge's avatar
runge committed
948 949
			last;
		} elsif ($len == 0) {
950
			print STDERR "pproxy\[$$]: Input is EOF.\n";
runge's avatar
runge committed
951 952 953 954 955 956 957
			last;
		}
		my $offset = 0;
		my $quit = 0;
		while ($len) {
			my $written = syswrite($out, $buf, $len, $offset);
			if (! defined $written) {
958
				print STDERR "pproxy\[$$]: Output is EOF. $!\n";
runge's avatar
runge committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
				$quit = 1;
				last;
			}
			$len -= $written;
			$offset += $written;
		}
		last if $quit;
	}
	close($in);
	close($out);
}
'
	echo "$cod" > $tf
	chmod 700 $tf
}

ptmp=""
if [ "X$proxy" != "X" ]; then
runge's avatar
runge committed
977
	ptmp="/tmp/ss_vncviewer${RANDOM}.$$.pl"
runge's avatar
runge committed
978 979
	mytmp "$ptmp"
	pcode "$ptmp"
runge's avatar
runge committed
980 981 982
	if [ "X$showcert" != "X1" -a "X$direct_connect" = "X" ]; then
		if uname | grep Darwin >/dev/null; then
			nd=`expr $use + 333`
983 984
			PPROXY_LISTEN=$nd
			export PPROXY_LISTEN
runge's avatar
runge committed
985 986 987 988 989 990 991 992 993 994 995
			$ptmp 2>/dev/null &
			sleep 3
			host="localhost"
			port="$nd"
			connect="connect = localhost:$nd"
		else
			connect="exec = $ptmp"
		fi
	else
		connect="exec = $ptmp"
	fi
runge's avatar
runge committed
996 997 998 999
else
	connect="connect = $host:$port"
fi

runge's avatar
runge committed
1000 1001
if [ "X$showcert" = "X1" ]; then
	if [ "X$proxy" != "X" ]; then
1002 1003
		PPROXY_LISTEN=$use
		export PPROXY_LISTEN
runge's avatar
runge committed
1004 1005 1006 1007 1008 1009 1010 1011 1012
		$ptmp 2>/dev/null &
		sleep 3
		host="localhost"
		port="$use"
	fi
	openssl s_client -connect $host:$port 2>&1 < /dev/null
	exit $?
fi

runge's avatar
runge committed
1013
if [ "X$direct_connect" != "X" ]; then
1014 1015 1016 1017 1018 1019 1020
	if [ "X$getport" = "X" ]; then
		echo ""
		echo "Running viewer for direct connection:"
		echo ""
		echo "** NOTE: THERE WILL BE NO SSL OR SSH ENCRYPTION **"
		echo ""
	fi
1021 1022 1023
	if [ "X$SSVNC_NO_ENC_WARN" != "X" ]; then
		sleep 1
	elif type printf > /dev/null 2>&1; then
runge's avatar
runge committed
1024
		printf  "Are you sure you want to continue? [y]/n "
1025
		read x
runge's avatar
runge committed
1026 1027
	else
		echo -n "Are you sure you want to continue? [y]/n "
1028
		read x
runge's avatar
runge committed
1029 1030 1031 1032 1033 1034
	fi
	if [ "X$x" = "Xn" ]; then
		exit 1
	fi
	echo ""
	if [ "X$ptmp" != "X" ]; then
1035 1036
		PPROXY_LISTEN=$use
		export PPROXY_LISTEN
runge's avatar
runge committed
1037
		$ptmp &
1038 1039 1040
		if [ "X$reverse" = "X" ]; then
			sleep 2
		fi
runge's avatar
runge committed
1041 1042 1043
		host="localhost"
		disp="$N"
	fi
1044 1045 1046
	if [ "X$SSVNC_EXTRA_SLEEP" != "X" ]; then
		sleep $SSVNC_EXTRA_SLEEP
	fi
1047 1048 1049 1050 1051 1052 1053 1054 1055
	if [ "X$reverse" = "X" ]; then
		echo "$VNCVIEWERCMD" "$@" $host:$disp
		trap "final" 0 2 15
		echo ""
		$VNCVIEWERCMD "$@" $host:$disp
	else
		echo ""
		echo "NOTE: Press Ctrl-C to terminate viewer LISTEN mode."
		echo ""
runge's avatar
runge committed
1056
		echo "$VNCVIEWERCMD" "$@" -listen $disp
1057 1058
		trap "final" 0 2 15
		echo ""
runge's avatar
runge committed
1059
		$VNCVIEWERCMD "$@" -listen $disp
1060
	fi
runge's avatar
runge committed
1061 1062 1063
	exit $?
fi

runge's avatar
runge committed
1064
tmp=/tmp/ss_vncviewer${RANDOM}.$$
runge's avatar
runge committed
1065 1066
mytmp "$tmp"

1067 1068 1069
if [ "X$reverse" = "X" ]; then

	cat > "$tmp" <<END
runge's avatar
runge committed
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
foreground = yes
pid =
client = yes
debug = 6
$STUNNEL_EXTRA_OPTS
$verify
$cert

[vnc_stunnel]
accept = localhost:$use
$connect
1081 1082 1083 1084 1085 1086 1087

END
else

	p2=`expr 5500 + $N`
	connect="connect = localhost:$p2"
	if [ "X$cert" = "X" ]; then
runge's avatar
runge committed
1088
		tcert="/tmp/tcert${RANDOM}.$$"
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
		cat > $tcert <<END
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAvkfXxb0wcxgrjV2ziFikjII+ze8iKcTBt47L0GM/c21efelN
+zZpJUUXLu4zz8Ryq8Q+sQgfNy7uTOpN9bUUaOk1TnD7gaDQnQWiNHmqbW2kL+DS
OKngJVPo9dETAS8hf7+D1e1DBZxjTc1a4RQqWJixwpYj99ixWzu8VC2m/xXsjvOs
jp4+DLBB490nbkwvstmhmiWm1CmI5O5xOkgioVNQqHvQMdVKOSz9PpbjvZiRX1Uo
qoMrk+2NOqwP90TB35yPASXb9zXKpO7DLhkube+yYGf+yk46aD707L07Eb7cosFP
S84vNZ9gX7rQ0UOwm5rYA/oZTBskgaqhtIzkLwIDAQABAoIBAD4ot/sXt5kRn0Ca
CIkU9AQWlC+v28grR2EQW9JiaZrqcoDNUzUqbCTJsi4ZkIFh2lf0TsqELbZYNW6Y
6AjJM7al4E0UqYSKJTv2WCuuRxdiRs2BMwthqyBmjeanev7bB6V0ybt7u3Y8xU/o
MrTuYnr4vrEjXPKdLirwk7AoDbKsRXHSIiHEIBOq1+dUQ32t36ukdnnza4wKDLZc
PKHiCdCk/wOGhuDlxD6RspqUAlRnJ8/aEhrgWxadFXw1hRhRsf/v1shtB0T3DmTe
Jchjwyiw9mryb9JZAcKxW+fUc4EVvj6VdQGqYInQJY5Yxm5JAlVQUJicuuJEvn6A
rj5osQECgYEA552CaHpUiFlB4HGkjaH00kL+f0+gRF4PANCPk6X3UPDVYzKnzmuu
yDvIdEETGFWBwoztUrOOKqVvPEQ+kBa2+DWWYaERZLtg2cI5byfDJxQ3ldzilS3J
1S3WgCojqcsG/hlxoQJ1dZFanUy/QhUZ0B+wlC+Zp1Q8AyuGQvhHp68CgYEA0lBI
eqq2GGCdJuNHMPFbi8Q0BnX55LW5C1hWjhuYiEkb3hOaIJuJrqvayBlhcQa2cGqp
uP34e9UCfoeLgmoCQ0b4KpL2NGov/mL4i8bMgog4hcoYuIi3qxN18vVR14VKEh4U
RLk0igAYPU+IK2QByaQlBo9OSaKkcfm7U1/pK4ECgYAxr6VpGk0GDvfF2Tsusv6d
GIgV8ZP09qSLTTJvvxvF/lQYeqZq7sjI5aJD5i3de4JhpO/IXQJzfZfWOuGc8XKA
3qYK/Y2IqXXGYRcHFGWV/Y1LFd55mCADHlk0l1WdOBOg8P5iRu/Br9PbiLpCx9oI
vrOXpnp03eod1/luZmqguwKBgQCWFRSj9Q7ddpSvG6HCG3ro0qsNsUMTI1tZ7UBX
SPogx4tLf1GN03D9ZUZLZVFUByZKMtPLX/Hi7K9K/A9ikaPrvsl6GEX6QYzeTGJx
3Pw0amFrmDzr8ySewNR6/PXahxPEuhJcuI31rPufRRI3ZLah3rFNbRbBFX+klkJH
zTnoAQKBgDbUK/aQFGduSy7WUT7LlM3UlGxJ2sA90TQh4JRQwzur0ACN5GdYZkqM
YBts4sBJVwwJoxD9OpbvKu3uKCt41BSj0/KyoBzjT44S2io2tj1syujtlVUsyyBy
/ca0A7WBB8lD1D7QMIhYUm2O9kYtSCLlUTHt5leqGaRG38DqlX36
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
MIIDzDCCArQCCQDSzxzxqhyqLzANBgkqhkiG9w0BAQQFADCBpzELMAkGA1UEBhMC
VVMxFjAUBgNVBAgTDU1hc3NhY2h1c2V0dHMxDzANBgNVBAcTBkJvc3RvbjETMBEG
A1UEChMKTXkgQ29tcGFueTEcMBoGA1UECxMTUHJvZHVjdCBEZXZlbG9wbWVudDEZ
MBcGA1UEAxMQd3d3Lm5vd2hlcmUubm9uZTEhMB8GCSqGSIb3DQEJARYSYWRtaW5A
bm93aGVyZS5ub25lMB4XDTA3MDMyMzE4MDc0NVoXDTI2MDUyMjE4MDc0NVowgacx
CzAJBgNVBAYTAlVTMRYwFAYDVQQIEw1NYXNzYWNodXNldHRzMQ8wDQYDVQQHEwZC
b3N0b24xEzARBgNVBAoTCk15IENvbXBhbnkxHDAaBgNVBAsTE1Byb2R1Y3QgRGV2
ZWxvcG1lbnQxGTAXBgNVBAMTEHd3dy5ub3doZXJlLm5vbmUxITAfBgkqhkiG9w0B
CQEWEmFkbWluQG5vd2hlcmUubm9uZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC
AQoCggEBAL5H18W9MHMYK41ds4hYpIyCPs3vIinEwbeOy9BjP3NtXn3pTfs2aSVF
Fy7uM8/EcqvEPrEIHzcu7kzqTfW1FGjpNU5w+4Gg0J0FojR5qm1tpC/g0jip4CVT
6PXREwEvIX+/g9XtQwWcY03NWuEUKliYscKWI/fYsVs7vFQtpv8V7I7zrI6ePgyw
QePdJ25ML7LZoZolptQpiOTucTpIIqFTUKh70DHVSjks/T6W472YkV9VKKqDK5Pt
jTqsD/dEwd+cjwEl2/c1yqTuwy4ZLm3vsmBn/spOOmg+9Oy9OxG+3KLBT0vOLzWf
YF+60NFDsJua2AP6GUwbJIGqobSM5C8CAwEAATANBgkqhkiG9w0BAQQFAAOCAQEA
vGomHEp6TVU83X2EBUgnbOhzKJ9u3fOI/Uf5L7p//Vxqow7OR1cguzh/YEzmXOIL
ilMVnzX9nj/bvcLAuqEP7MR1A8f4+E807p/L/Sf49BiCcwQq5I966sGKYXjkve+T
2GTBNwMSq+5kLSf6QY8VZI+qnrAudEQMeJByQhTZZ0dH8Njeq8EGl9KUio+VWaiW
CQK6xJuAvAHqa06OjLmwu1fYD4GLGSrOIiRVkSXV8qLIUmzxdJaIRznkFWsrCEKR
wAH966SAOvd2s6yOHMvyDRIL7WHxfESB6rDHsdIW/yny1fBePjv473KrxyXtbz7I
dMw1yW09l+eEo4A7GzwOdw==
-----END CERTIFICATE-----
END
		chmod 600 $tcert
		cert="cert = $tcert"
	fi

	STUNNEL_EXTRA_OPTS=`echo "$STUNNEL_EXTRA_OPTS" | sed -e 's/maxconn/#maxconn/'`

	hloc=""
	if [ "X$use_ssh" = "X1" ]; then
		hloc="localhost:"
	fi
	cat > "$tmp" <<END
foreground = yes
pid =
client = no
debug = 6
$STUNNEL_EXTRA_OPTS
$verify
$cert

[vnc_stunnel]
accept = $hloc$port
$connect

runge's avatar
runge committed
1164 1165
END

1166 1167
fi

runge's avatar
runge committed
1168 1169 1170 1171 1172 1173 1174 1175
echo ""
echo "Using this stunnel configuration:"
echo ""
cat "$tmp" | uniq
echo ""
sleep 1

echo ""
1176
echo "Running stunnel:"
runge's avatar
runge committed
1177 1178
echo "$STUNNEL $tmp"
$STUNNEL "$tmp" < /dev/tty > /dev/tty &
1179
stunnel_pid=$!
runge's avatar
runge committed
1180 1181 1182 1183 1184
echo ""

# pause here to let the user supply a possible passphrase for the
# mycert key:
if [ "X$mycert" != "X" ]; then
1185 1186 1187 1188 1189
	sleep 2
	echo ""
	echo "(pausing for possible certificate passphrase dialog)"
	echo ""
	sleep 2
runge's avatar
runge committed
1190 1191 1192 1193 1194
fi
sleep 2
rm -f "$tmp"

echo ""
1195 1196 1197
if [ "X$SSVNC_EXTRA_SLEEP" != "X" ]; then
	sleep $SSVNC_EXTRA_SLEEP
fi
runge's avatar
runge committed
1198
echo "Running viewer:"
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
if [ "X$reverse" = "X" ]; then
	echo "$VNCVIEWERCMD" "$@" localhost:$N
	trap "final" 0 2 15
	echo ""
	$VNCVIEWERCMD "$@" localhost:$N
else
	echo ""
	echo "NOTE: Press Ctrl-C to terminate viewer LISTEN mode."
	echo ""
	echo "$VNCVIEWERCMD" "$@" -listen $N
	trap "final" 0 2 15
	echo ""
	$VNCVIEWERCMD "$@" -listen $N
fi
runge's avatar
runge committed
1213 1214

sleep 1