Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | # |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 3 | # Copyright (C) 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 4 | # Copyright (C) 2002-2003 Constantin Kaplinsky. All Rights Reserved. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 5 | # Copyright (C) 2002-2005 RealVNC Ltd. |
| 6 | # Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved. |
| 7 | # |
| 8 | # This is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License as published by |
| 10 | # the Free Software Foundation; either version 2 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # This software is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License |
| 19 | # along with this software; if not, write to the Free Software |
| 20 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 21 | # USA. |
| 22 | # |
| 23 | |
| 24 | # |
| 25 | # vncserver - wrapper script to start an X VNC server. |
| 26 | # |
| 27 | |
| 28 | # |
| 29 | # First make sure we're operating in a sane environment. |
| 30 | # |
| 31 | |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 32 | $exedir = ""; |
| 33 | $slashndx = rindex($0, "/"); |
| 34 | if($slashndx>=0) { |
| 35 | $exedir = substr($0, 0, $slashndx+1); |
| 36 | } |
| 37 | |
| 38 | $vncClasses = ""; |
| 39 | |
| 40 | $xauth = "xauth"; |
| 41 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 42 | &SanityCheck(); |
| 43 | |
| 44 | # |
| 45 | # Global variables. You may want to configure some of these for your site. |
| 46 | # |
| 47 | |
| 48 | $geometry = "1024x768"; |
DRC | 9d1c157 | 2009-03-26 18:18:51 +0000 | [diff] [blame] | 49 | #$depth = 16; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 50 | $vncJavaFiles = (((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") || |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 51 | ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes") || |
| 52 | ((-d "$vncClasses") && "$vncClasses")); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 53 | $vncUserDir = "$ENV{HOME}/.vnc"; |
| 54 | $xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority"; |
| 55 | |
| 56 | $defaultXStartup |
| 57 | = ("#!/bin/sh\n\n". |
Adam Tkac | 804d86f | 2009-04-03 14:33:51 +0000 | [diff] [blame] | 58 | "vncconfig -iconic &\n". |
DRC | 9324898 | 2009-03-26 18:14:38 +0000 | [diff] [blame] | 59 | "unset SESSION_MANAGER\n". |
Adam Tkac | c071e49 | 2009-05-20 09:01:24 +0000 | [diff] [blame] | 60 | "unset DBUS_SESSION_BUS_ADDRESS\n". |
DRC | 9324898 | 2009-03-26 18:14:38 +0000 | [diff] [blame] | 61 | "OS=`uname -s`\n". |
| 62 | "if [ \$OS = 'Linux' ]; then\n". |
| 63 | " case \"\$WINDOWMANAGER\" in\n". |
| 64 | " \*gnome\*)\n". |
| 65 | " if [ -e /etc/SuSE-release ]; then\n". |
| 66 | " PATH=\$PATH:/opt/gnome/bin\n". |
| 67 | " export PATH\n". |
| 68 | " fi\n". |
| 69 | " ;;\n". |
| 70 | " esac\n". |
| 71 | "fi\n". |
| 72 | "if [ -x /etc/X11/xinit/xinitrc ]; then\n". |
| 73 | " exec /etc/X11/xinit/xinitrc\n". |
| 74 | "fi\n". |
| 75 | "if [ -f /etc/X11/xinit/xinitrc ]; then\n". |
| 76 | " exec sh /etc/X11/xinit/xinitrc\n". |
| 77 | "fi\n". |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 78 | "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n". |
| 79 | "xsetroot -solid grey\n". |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 80 | "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n". |
| 81 | "twm &\n"); |
| 82 | |
| 83 | chop($host = `uname -n`); |
| 84 | |
DRC | 989dbd1 | 2009-04-22 13:23:17 +0000 | [diff] [blame] | 85 | if (-d "/etc/X11/fontpath.d") { |
| 86 | $fontPath = "catalogue:/etc/X11/fontpath.d"; |
| 87 | } |
DRC | 36546c1 | 2009-04-15 06:47:23 +0000 | [diff] [blame] | 88 | |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 89 | @fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/'); |
| 90 | if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');} |
| 91 | if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');} |
| 92 | if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');} |
| 93 | push(@fontpaths, '/usr/share/fonts/default'); |
| 94 | |
| 95 | @fonttypes = ('misc', |
| 96 | '75dpi', |
| 97 | '100dpi', |
| 98 | 'Speedo', |
| 99 | 'Type1'); |
| 100 | |
| 101 | foreach $_fpath (@fontpaths) { |
| 102 | foreach $_ftype (@fonttypes) { |
| 103 | if (-f "$_fpath/$_ftype/fonts.dir") { |
| 104 | if (! -l "$_fpath/$_ftype") { |
DRC | 36546c1 | 2009-04-15 06:47:23 +0000 | [diff] [blame] | 105 | $defFontPath .= "$_fpath/$_ftype,"; |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
DRC | d28792b | 2010-01-11 20:53:00 +0000 | [diff] [blame] | 110 | |
DRC | 36546c1 | 2009-04-15 06:47:23 +0000 | [diff] [blame] | 111 | if ($defFontPath) { |
| 112 | if (substr($defFontPath, -1, 1) == ',') { |
| 113 | chop $defFontPath; |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
DRC | d28792b | 2010-01-11 20:53:00 +0000 | [diff] [blame] | 117 | if ($fontPath eq "") { |
| 118 | $fontPath = $defFontPath; |
| 119 | } |
| 120 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 121 | # Check command line options |
| 122 | |
| 123 | &ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1, |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 124 | "-help",0,"-h",0,"--help",0,"-fp",1); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 125 | |
| 126 | &Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'}); |
| 127 | |
| 128 | &Kill() if ($opt{'-kill'}); |
| 129 | |
| 130 | # Uncomment this line if you want default geometry, depth and pixelformat |
| 131 | # to match the current X display: |
| 132 | # &GetXDisplayDefaults(); |
| 133 | |
| 134 | if ($opt{'-geometry'}) { |
| 135 | $geometry = $opt{'-geometry'}; |
| 136 | } |
| 137 | if ($opt{'-depth'}) { |
| 138 | $depth = $opt{'-depth'}; |
| 139 | $pixelformat = ""; |
| 140 | } |
| 141 | if ($opt{'-pixelformat'}) { |
| 142 | $pixelformat = $opt{'-pixelformat'}; |
| 143 | } |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 144 | if ($opt{'-fp'}) { |
| 145 | $fontPath = $opt{'-fp'}; |
DRC | 36546c1 | 2009-04-15 06:47:23 +0000 | [diff] [blame] | 146 | $fpArgSpecified = 1; |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 147 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 148 | |
| 149 | &CheckGeometryAndDepth(); |
| 150 | |
| 151 | |
| 152 | # Create the user's vnc directory if necessary. |
| 153 | |
| 154 | if (!(-e $vncUserDir)) { |
| 155 | if (!mkdir($vncUserDir,0755)) { |
| 156 | die "$prog: Could not create $vncUserDir.\n"; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | # Make sure the user has a password. |
| 161 | |
| 162 | ($z,$z,$mode) = stat("$vncUserDir/passwd"); |
| 163 | if (!(-e "$vncUserDir/passwd") || ($mode & 077)) { |
| 164 | warn "\nYou will require a password to access your desktops.\n\n"; |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 165 | system($exedir."vncpasswd -q $vncUserDir/passwd"); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 166 | if (($? >> 8) != 0) { |
| 167 | exit 1; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | # Find display number. |
| 172 | |
| 173 | if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) { |
| 174 | $displayNumber = $1; |
| 175 | shift(@ARGV); |
| 176 | if (!&CheckDisplayNumber($displayNumber)) { |
| 177 | die "A VNC server is already running as :$displayNumber\n"; |
| 178 | } |
Adam Tkac | 39c9d99 | 2010-07-21 14:08:38 +0000 | [diff] [blame] | 179 | } elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/) && ($ARGV[0] !~ /^\+/)) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 180 | &Usage(); |
| 181 | } else { |
| 182 | $displayNumber = &GetDisplayNumber(); |
| 183 | } |
| 184 | |
| 185 | $vncPort = 5900 + $displayNumber; |
| 186 | |
| 187 | $desktopLog = "$vncUserDir/$host:$displayNumber.log"; |
| 188 | unlink($desktopLog); |
| 189 | |
Adam Tkac | 6cbd9d1 | 2009-11-12 10:39:54 +0000 | [diff] [blame] | 190 | # Make an X server cookie - use /dev/urandom on systems that have it, |
| 191 | # otherwise use perl's random number generator, seeded with the sum |
| 192 | # of the current time, our PID and part of the encrypted form of the password. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 193 | |
Adam Tkac | 6cbd9d1 | 2009-11-12 10:39:54 +0000 | [diff] [blame] | 194 | my $cookie = ""; |
| 195 | if (open(URANDOM, '<', '/dev/urandom')) { |
| 196 | my $randata; |
| 197 | if (sysread(URANDOM, $randata, 16) == 16) { |
| 198 | $cookie = unpack 'h*', $randata; |
| 199 | } |
| 200 | close(URANDOM); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 201 | } |
Adam Tkac | 6cbd9d1 | 2009-11-12 10:39:54 +0000 | [diff] [blame] | 202 | if ($cookie eq "") { |
| 203 | srand(time+$$+unpack("L",`cat $vncUserDir/passwd`)); |
| 204 | for (1..16) { |
| 205 | $cookie .= sprintf("%02x", int(rand(256)) % 256); |
| 206 | } |
| 207 | } |
| 208 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 209 | system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie"); |
| 210 | system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie"); |
| 211 | |
| 212 | if ($opt{'-name'}) { |
| 213 | $desktopName = $opt{'-name'}; |
| 214 | } else { |
| 215 | $desktopName = "$host:$displayNumber ($ENV{USER})"; |
| 216 | } |
| 217 | |
| 218 | # Now start the X VNC Server |
| 219 | |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 220 | $cmd = $exedir."Xvnc :$displayNumber"; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 221 | $cmd .= " -desktop " . "edString($desktopName); |
| 222 | $cmd .= " -httpd $vncJavaFiles" if ($vncJavaFiles); |
| 223 | $cmd .= " -auth $xauthorityFile"; |
| 224 | $cmd .= " -geometry $geometry" if ($geometry); |
| 225 | $cmd .= " -depth $depth" if ($depth); |
| 226 | $cmd .= " -pixelformat $pixelformat" if ($pixelformat); |
| 227 | $cmd .= " -rfbwait 30000"; |
| 228 | $cmd .= " -rfbauth $vncUserDir/passwd"; |
| 229 | $cmd .= " -rfbport $vncPort"; |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 230 | $cmd .= " -fp $fontPath" if ($fontPath); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 231 | $cmd .= " -pn"; |
| 232 | |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 233 | # Add color database stuff here, e.g.: |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 234 | # |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 235 | # $cmd .= " -co /usr/lib/X11/rgb"; |
| 236 | # |
| 237 | |
| 238 | foreach $arg (@ARGV) { |
| 239 | $cmd .= " " . "edString($arg); |
| 240 | } |
| 241 | $cmd .= " >> " . "edString($desktopLog) . " 2>&1"; |
| 242 | |
| 243 | # Run $cmd and record the process ID. |
| 244 | |
| 245 | $pidFile = "$vncUserDir/$host:$displayNumber.pid"; |
| 246 | system("$cmd & echo \$! >$pidFile"); |
| 247 | |
| 248 | # Give Xvnc a chance to start up |
| 249 | |
| 250 | sleep(3); |
DRC | d28792b | 2010-01-11 20:53:00 +0000 | [diff] [blame] | 251 | if ($fontPath ne $defFontPath) { |
| 252 | unless (kill 0, `cat $pidFile`) { |
| 253 | if ($fpArgSpecified) { |
| 254 | warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n"; |
| 255 | warn "path you specified using the -fp argument is incorrect. Attempting to\n"; |
| 256 | warn "determine an appropriate font path for this system and restart Xvnc using\n"; |
| 257 | warn "that font path ...\n"; |
| 258 | } else { |
| 259 | warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the font\n"; |
| 260 | warn "catalog is not properly configured. Attempting to determine an appropriate\n"; |
| 261 | warn "font path for this system and restart Xvnc using that font path ...\n"; |
| 262 | } |
| 263 | $cmd =~ s@-fp [^ ]+@@; |
| 264 | $cmd .= " -fp $defFontPath" if ($defFontPath); |
| 265 | system("$cmd & echo \$! >$pidFile"); |
| 266 | sleep(3); |
DRC | 36546c1 | 2009-04-15 06:47:23 +0000 | [diff] [blame] | 267 | } |
DRC | d6821bf | 2009-03-26 18:17:49 +0000 | [diff] [blame] | 268 | } |
| 269 | unless (kill 0, `cat $pidFile`) { |
| 270 | warn "Could not start Xvnc.\n\n"; |
| 271 | open(LOG, "<$desktopLog"); |
| 272 | while (<LOG>) { print; } |
| 273 | close(LOG); |
| 274 | die "\n"; |
| 275 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 276 | |
| 277 | warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n"; |
| 278 | |
| 279 | # Create the user's xstartup script if necessary. |
| 280 | |
| 281 | if (!(-e "$vncUserDir/xstartup")) { |
| 282 | warn "Creating default startup script $vncUserDir/xstartup\n"; |
| 283 | open(XSTARTUP, ">$vncUserDir/xstartup"); |
| 284 | print XSTARTUP $defaultXStartup; |
| 285 | close(XSTARTUP); |
| 286 | chmod 0755, "$vncUserDir/xstartup"; |
| 287 | } |
| 288 | |
| 289 | # Run the X startup script. |
| 290 | |
| 291 | warn "Starting applications specified in $vncUserDir/xstartup\n"; |
| 292 | warn "Log file is $desktopLog\n\n"; |
| 293 | |
| 294 | # If the unix domain socket exists then use that (DISPLAY=:n) otherwise use |
| 295 | # TCP (DISPLAY=host:n) |
| 296 | |
| 297 | if (-e "/tmp/.X11-unix/X$displayNumber" || |
| 298 | -e "/usr/spool/sockets/X11/$displayNumber") |
| 299 | { |
| 300 | $ENV{DISPLAY}= ":$displayNumber"; |
| 301 | } else { |
| 302 | $ENV{DISPLAY}= "$host:$displayNumber"; |
| 303 | } |
| 304 | $ENV{VNCDESKTOP}= $desktopName; |
| 305 | |
| 306 | system("$vncUserDir/xstartup >> " . "edString($desktopLog) . " 2>&1 &"); |
| 307 | |
| 308 | exit; |
| 309 | |
| 310 | |
| 311 | ############################################################################### |
| 312 | # |
| 313 | # CheckGeometryAndDepth simply makes sure that the geometry and depth values |
| 314 | # are sensible. |
| 315 | # |
| 316 | |
| 317 | sub CheckGeometryAndDepth |
| 318 | { |
| 319 | if ($geometry =~ /^(\d+)x(\d+)$/) { |
| 320 | $width = $1; $height = $2; |
| 321 | |
| 322 | if (($width<1) || ($height<1)) { |
| 323 | die "$prog: geometry $geometry is invalid\n"; |
| 324 | } |
| 325 | |
| 326 | while (($width % 4)!=0) { |
| 327 | $width = $width + 1; |
| 328 | } |
| 329 | |
| 330 | while (($height % 2)!=0) { |
| 331 | $height = $height + 1; |
| 332 | } |
| 333 | |
| 334 | $geometry = "${width}x$height"; |
| 335 | } else { |
| 336 | die "$prog: geometry $geometry is invalid\n"; |
| 337 | } |
| 338 | |
DRC | e5b4f75 | 2009-03-26 18:23:29 +0000 | [diff] [blame] | 339 | if ($depth && (($depth < 8) || ($depth > 32))) { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 340 | die "Depth must be between 8 and 32\n"; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | |
| 345 | # |
| 346 | # GetDisplayNumber gets the lowest available display number. A display number |
| 347 | # n is taken if something is listening on the VNC server port (5900+n) or the |
| 348 | # X server port (6000+n). |
| 349 | # |
| 350 | |
| 351 | sub GetDisplayNumber |
| 352 | { |
| 353 | foreach $n (1..99) { |
| 354 | if (&CheckDisplayNumber($n)) { |
| 355 | return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02 |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | die "$prog: no free display number on $host.\n"; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | # |
| 364 | # CheckDisplayNumber checks if the given display number is available. A |
| 365 | # display number n is taken if something is listening on the VNC server port |
| 366 | # (5900+n) or the X server port (6000+n). |
| 367 | # |
| 368 | |
| 369 | sub CheckDisplayNumber |
| 370 | { |
| 371 | local ($n) = @_; |
| 372 | |
| 373 | socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n"; |
| 374 | eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))'; |
| 375 | if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) { |
| 376 | close(S); |
| 377 | return 0; |
| 378 | } |
| 379 | close(S); |
| 380 | |
| 381 | socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n"; |
| 382 | eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))'; |
| 383 | if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) { |
| 384 | close(S); |
| 385 | return 0; |
| 386 | } |
| 387 | close(S); |
| 388 | |
| 389 | if (-e "/tmp/.X$n-lock") { |
| 390 | warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n"; |
| 391 | warn "Remove this file if there is no X server $host:$n\n"; |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | if (-e "/tmp/.X11-unix/X$n") { |
| 396 | warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n"; |
| 397 | warn "Remove this file if there is no X server $host:$n\n"; |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | if (-e "/usr/spool/sockets/X11/$n") { |
| 402 | warn("\nWarning: $host:$n is taken because of ". |
| 403 | "/usr/spool/sockets/X11/$n\n"); |
| 404 | warn "Remove this file if there is no X server $host:$n\n"; |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | return 1; |
| 409 | } |
| 410 | |
| 411 | |
| 412 | # |
| 413 | # GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel |
| 414 | # format of the current X display being used. If successful, it sets the |
| 415 | # options as appropriate so that the X VNC server will use the same settings |
| 416 | # (minus an allowance for window manager decorations on the geometry). Using |
| 417 | # the same depth and pixel format means that the VNC server won't have to |
| 418 | # translate pixels when the desktop is being viewed on this X display (for |
| 419 | # TrueColor displays anyway). |
| 420 | # |
| 421 | |
| 422 | sub GetXDisplayDefaults |
| 423 | { |
| 424 | local (@lines, @matchlines, $width, $height, $defaultVisualId, $i, |
| 425 | $red, $green, $blue); |
| 426 | |
| 427 | $wmDecorationWidth = 4; # a guess at typical size for window manager |
| 428 | $wmDecorationHeight = 24; # decoration size |
| 429 | |
| 430 | return if (!defined($ENV{DISPLAY})); |
| 431 | |
| 432 | @lines = `xdpyinfo 2>/dev/null`; |
| 433 | |
| 434 | return if ($? != 0); |
| 435 | |
| 436 | @matchlines = grep(/dimensions/, @lines); |
| 437 | if (@matchlines) { |
| 438 | ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/); |
| 439 | |
| 440 | $width -= $wmDecorationWidth; |
| 441 | $height -= $wmDecorationHeight; |
| 442 | |
| 443 | $geometry = "${width}x$height"; |
| 444 | } |
| 445 | |
| 446 | @matchlines = grep(/default visual id/, @lines); |
| 447 | if (@matchlines) { |
| 448 | ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/); |
| 449 | |
| 450 | for ($i = 0; $i < @lines; $i++) { |
| 451 | if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) { |
| 452 | if (($lines[$i+1] !~ /TrueColor/) || |
| 453 | ($lines[$i+2] !~ /depth/) || |
| 454 | ($lines[$i+4] !~ /red, green, blue masks/)) |
| 455 | { |
| 456 | return; |
| 457 | } |
| 458 | last; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | return if ($i >= @lines); |
| 463 | |
| 464 | ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/); |
| 465 | ($red,$green,$blue) |
| 466 | = ($lines[$i+4] |
| 467 | =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/); |
| 468 | |
| 469 | $red = hex($red); |
| 470 | $green = hex($green); |
| 471 | $blue = hex($blue); |
| 472 | |
| 473 | if ($red > $blue) { |
| 474 | $red = int(log($red) / log(2)) - int(log($green) / log(2)); |
| 475 | $green = int(log($green) / log(2)) - int(log($blue) / log(2)); |
| 476 | $blue = int(log($blue) / log(2)) + 1; |
| 477 | $pixelformat = "rgb$red$green$blue"; |
| 478 | } else { |
| 479 | $blue = int(log($blue) / log(2)) - int(log($green) / log(2)); |
| 480 | $green = int(log($green) / log(2)) - int(log($red) / log(2)); |
| 481 | $red = int(log($red) / log(2)) + 1; |
| 482 | $pixelformat = "bgr$blue$green$red"; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | |
| 488 | # |
| 489 | # quotedString returns a string which yields the original string when parsed |
| 490 | # by a shell. |
| 491 | # |
| 492 | |
| 493 | sub quotedString |
| 494 | { |
| 495 | local ($in) = @_; |
| 496 | |
| 497 | $in =~ s/\'/\'\"\'\"\'/g; |
| 498 | |
| 499 | return "'$in'"; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | # |
| 504 | # removeSlashes turns slashes into underscores for use as a file name. |
| 505 | # |
| 506 | |
| 507 | sub removeSlashes |
| 508 | { |
| 509 | local ($in) = @_; |
| 510 | |
| 511 | $in =~ s|/|_|g; |
| 512 | |
| 513 | return "$in"; |
| 514 | } |
| 515 | |
| 516 | |
| 517 | # |
| 518 | # Usage |
| 519 | # |
| 520 | |
| 521 | sub Usage |
| 522 | { |
| 523 | die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n". |
| 524 | " [-geometry <width>x<height>]\n". |
| 525 | " [-pixelformat rgbNNN|bgrNNN]\n". |
DRC | eed5d1f | 2009-03-26 19:16:19 +0000 | [diff] [blame] | 526 | " [-fp <font-path>]\n". |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 527 | " <Xvnc-options>...\n\n". |
| 528 | " $prog -kill <X-display>\n\n"); |
| 529 | } |
| 530 | |
| 531 | |
| 532 | # |
| 533 | # Kill |
| 534 | # |
| 535 | |
| 536 | sub Kill |
| 537 | { |
| 538 | $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1 |
| 539 | |
| 540 | if ($opt{'-kill'} =~ /^:\d+$/) { |
| 541 | $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid"; |
| 542 | } else { |
| 543 | if ($opt{'-kill'} !~ /^$host:/) { |
| 544 | die "\nCan't tell if $opt{'-kill'} is on $host\n". |
| 545 | "Use -kill :<number> instead\n\n"; |
| 546 | } |
| 547 | $pidFile = "$vncUserDir/$opt{'-kill'}.pid"; |
| 548 | } |
| 549 | |
| 550 | if (! -r $pidFile) { |
| 551 | die "\nCan't find file $pidFile\n". |
| 552 | "You'll have to kill the Xvnc process manually\n\n"; |
| 553 | } |
| 554 | |
| 555 | $SIG{'HUP'} = 'IGNORE'; |
| 556 | chop($pid = `cat $pidFile`); |
| 557 | warn "Killing Xvnc process ID $pid\n"; |
| 558 | system("kill $pid"); |
| 559 | unlink $pidFile; |
| 560 | exit; |
| 561 | } |
| 562 | |
| 563 | |
| 564 | # |
| 565 | # ParseOptions takes a list of possible options and a boolean indicating |
| 566 | # whether the option has a value following, and sets up an associative array |
| 567 | # %opt of the values of the options given on the command line. It removes all |
| 568 | # the arguments it uses from @ARGV and returns them in @optArgs. |
| 569 | # |
| 570 | |
| 571 | sub ParseOptions |
| 572 | { |
| 573 | local (@optval) = @_; |
| 574 | local ($opt, @opts, %valFollows, @newargs); |
| 575 | |
| 576 | while (@optval) { |
| 577 | $opt = shift(@optval); |
| 578 | push(@opts,$opt); |
| 579 | $valFollows{$opt} = shift(@optval); |
| 580 | } |
| 581 | |
| 582 | @optArgs = (); |
| 583 | %opt = (); |
| 584 | |
| 585 | arg: while (defined($arg = shift(@ARGV))) { |
| 586 | foreach $opt (@opts) { |
| 587 | if ($arg eq $opt) { |
| 588 | push(@optArgs, $arg); |
| 589 | if ($valFollows{$opt}) { |
| 590 | if (@ARGV == 0) { |
| 591 | &Usage(); |
| 592 | } |
| 593 | $opt{$opt} = shift(@ARGV); |
| 594 | push(@optArgs, $opt{$opt}); |
| 595 | } else { |
| 596 | $opt{$opt} = 1; |
| 597 | } |
| 598 | next arg; |
| 599 | } |
| 600 | } |
| 601 | push(@newargs,$arg); |
| 602 | } |
| 603 | |
| 604 | @ARGV = @newargs; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | # |
| 609 | # Routine to make sure we're operating in a sane environment. |
| 610 | # |
| 611 | |
| 612 | sub SanityCheck |
| 613 | { |
| 614 | local ($cmd); |
| 615 | |
| 616 | # |
| 617 | # Get the program name |
| 618 | # |
| 619 | |
| 620 | ($prog) = ($0 =~ m|([^/]+)$|); |
| 621 | |
| 622 | # |
| 623 | # Check we have all the commands we'll need on the path. |
| 624 | # |
| 625 | |
| 626 | cmd: |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 627 | foreach $cmd ("uname") { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 628 | for (split(/:/,$ENV{PATH})) { |
| 629 | if (-x "$_/$cmd") { |
| 630 | next cmd; |
| 631 | } |
| 632 | } |
| 633 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 634 | } |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 635 | if (-x "/usr/X11R6/bin/xauth") { |
| 636 | $xauth = "/usr/X11R6/bin/xauth"; |
| 637 | } |
| 638 | else { |
| 639 | cmd1: |
| 640 | foreach $cmd ("xauth") { |
| 641 | for (split(/:/,$ENV{PATH})) { |
| 642 | if (-x "$_/$cmd") { |
| 643 | next cmd1; |
| 644 | } |
| 645 | } |
| 646 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | if($exedir eq "") { |
| 651 | cmd2: |
| 652 | foreach $cmd ("Xvnc","vncpasswd") { |
| 653 | for (split(/:/,$ENV{PATH})) { |
| 654 | if (-x "$_/$cmd") { |
| 655 | $vncClasses = "$_/../vnc/classes"; |
| 656 | next cmd2; |
| 657 | } |
| 658 | } |
| 659 | die "$prog: couldn't find \"$cmd\" on your PATH.\n"; |
| 660 | } |
| 661 | } |
| 662 | else { |
| 663 | cmd3: |
| 664 | foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") { |
| 665 | for (split(/:/,$ENV{PATH})) { |
| 666 | if (-x "$cmd") { |
| 667 | $vncClasses = $exedir."../vnc/classes"; |
| 668 | next cmd3; |
| 669 | } |
| 670 | } |
| 671 | die "$prog: couldn't find \"$cmd\".\n"; |
| 672 | } |
| 673 | } |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 674 | |
| 675 | # |
| 676 | # Check the HOME environment variable is set |
| 677 | # |
| 678 | |
| 679 | if (!defined($ENV{HOME})) { |
| 680 | die "$prog: The HOME environment variable is not set.\n"; |
| 681 | } |
DRC | 190854c | 2009-03-26 18:13:00 +0000 | [diff] [blame] | 682 | # chdir($ENV{HOME}); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 683 | |
| 684 | # |
| 685 | # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an |
| 686 | # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails, |
| 687 | # we just guess at the values. If you find perl moaning here, just |
| 688 | # hard-code the values of AF_INET and SOCK_STREAM. You can find these out |
| 689 | # for your platform by looking in /usr/include/sys/socket.h and related |
| 690 | # files. |
| 691 | # |
| 692 | |
| 693 | chop($os = `uname`); |
| 694 | chop($osrev = `uname -r`); |
| 695 | |
| 696 | eval 'use Socket'; |
| 697 | if ($@) { |
| 698 | eval 'require "sys/socket.ph"'; |
| 699 | if ($@) { |
| 700 | if (($os eq "SunOS") && ($osrev !~ /^4/)) { |
| 701 | $AF_INET = 2; |
| 702 | $SOCK_STREAM = 2; |
| 703 | } else { |
| 704 | $AF_INET = 2; |
| 705 | $SOCK_STREAM = 1; |
| 706 | } |
| 707 | } else { |
| 708 | $AF_INET = &AF_INET; |
| 709 | $SOCK_STREAM = &SOCK_STREAM; |
| 710 | } |
| 711 | } else { |
| 712 | $AF_INET = &AF_INET; |
| 713 | $SOCK_STREAM = &SOCK_STREAM; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 714 | } |
| 715 | } |