summaryrefslogtreecommitdiffstats
path: root/misc/pvs-vncsrv
blob: fa003036f91fdfd0ef4d9fab65a7b7657dc9eb5f (plain) (blame)
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
#!/bin/bash
# -----------------------------------------------------------------------------
# Copyright (c) 2009 - RZ Uni FR
# Copyright (c) 2009 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# pvs-vncsrv
#    - This is a generic wrapper script for starting any userspace VNC server
#      to offer connectivity from the pvs contol console. The script expects
#      start/stop in $1, the port to start on in $2, the password in $3 and
#      the rwpassword in $4. 
#      The latter one should be changed to piping for security reasons ...
# -----------------------------------------------------------------------------

# parameters for x11vnc
X11VNC_PARAMS="-bg -forever -repeat -display :0 -passwdfile rm:$HOME/.pvs/vncpasswd -o $HOME/.pvs/log.vncsrv -shared"
X11VNC_X11="0"

# at the moment the poolVSClient is expected to use the ~/.pvs directory
[ -d ~/.pvs ] || mkdir ~/.pvs

# write the password file
if [ -z "$4" ]; then
	echo -e "__BEGIN_VIEWONLY__\n$3" > ~/.pvs/vncpasswd
else 
	echo -e "$4\n__BEGIN_VIEWONLY__\n$3" > ~/.pvs/vncpasswd
fi

# find xauthority file
find_xauth () {
  FOUND=0
  RETRIES=4
  [ -z "$1" ] || RETRIES="$1"

  [ -e "/var/lib/kdm/" ] && 
     XAUTHFILE_KDM=`find /var/lib/kdm/ -iname "A\:0-*"`
  [ -e "/var/run/xauth/" ] && 
     XAUTHFILE_KDM2=$(find /var/run/xauth/ -iname "A\:0-*")
  [ -e "/var/lib/xdm/authdir/authfiles/" ] && 
     XAUTHFILE_XDM=$(find /var/lib/xdm/authdir/authfiles/ -iname "A\:0-*")
  [ -e "/var/lib/gdm/" ] && 
     XAUTHFILE_GDM=$(find /var/lib/gdm/ -iname *Xauth*)
  
  [ -f "$XAUTHFILE_KDM" ]  && FOUND=1 && XAUTHORITY="$XAUTHFILE_KDM"
  [ -f "$XAUTHFILE_KDM2" ] && FOUND=1 && XAUTHORITY="$XAUTHFILE_KDM2"
  [ -f "$XAUTHFILE_XDM" ]  && FOUND=1 && XAUTHORITY="$XAUTHFILE_XDM"
  [ -f "$XAUTHFILE_GDM" ]  && FOUND=1 && XAUTHORITY="$XAUTHFILE_GDM"

  if [ "$FOUND" -eq "0" ]; then
    if [ "$RETRIES" -gt "0" ]; then
      let "RETRIES-=1"
      find_xauth "$RETRIES"
    else
      echo "start FAILED (can't find way to authenticate myself against X)" \
        >>~/.pvs/log.vncsrv
      exit 1
    fi
  else
    echo "found authfile ($XAUTHORITY)" >>~/.pvs/log.vncsrv
  fi
}

START_COMMAND="x11vnc"

case "$1" in
  start)
    [ -z "$2" -o -z "$3" ] && echo " Port and/or Password not set" \
      >>~/.pvs/log.vncsrv
    echo "$2 $3" >>~/.pvs/log.test
    if [ ! -f ~/.pvs/vncpasswd ]; then
      echo " Start FAILED (~/.pvs/vncpasswd not found)" >>~/.pvs/log.vncsrv
      echo " Create it manualy and retry starting x11vnc" >>~/.pvs/log.vncsrv
      exit 2;
    fi

    if [ $X11VNC_X11 = 1 ]; then
      # find_xauth
      START_COMMAND="$START_COMMAND -auth $XAUTHORITY $X11VNC_PARAMS"
    else
      START_COMMAND="$START_COMMAND $X11VNC_PARAMS"
    fi
    echo "$START_COMMAND" >>~/.pvs/log.vncsrv
    echo "$OUTPUT" >>~/.pvs/log.vncsrv
    OUTPUT=$($START_COMMAND -rfbport $2)
    # return codes: http://tldp.org/LDP/abs/html/exitcodes.html
    exit $?
  ;;
  stop)
    pid=$(pidof x11vnc)
    if [ -z "$pid" ]
    then
     echo "x11vnc not running" >>~/.pvs/log.vncsrv
     exit 1;
    else
     kill -9 $pid 2>/dev/null
    echo "x11vnc stopped" >>~/.pvs/log.vncsrv
    fi
  ;;
  *)
    echo "x11vnc startscript"
    echo "Usage: $0 (start|stop)" 
  ;;
esac
exit 0