blob: d7cd711ed76048e69e5783ef5a48709ce87c7733 [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001#!/usr/bin/env perl
2#
DRCb9d8e762011-02-09 08:24:58 +00003# Copyright (C) 2009-2010 D. R. Commander. All Rights Reserved.
DRC190854c2009-03-26 18:13:00 +00004# Copyright (C) 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
5# Copyright (C) 2002-2003 Constantin Kaplinsky. All Rights Reserved.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00006# Copyright (C) 2002-2005 RealVNC Ltd.
7# Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
8#
9# This is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This software is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this software; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22# USA.
23#
24
25#
26# vncserver - wrapper script to start an X VNC server.
27#
28
29#
30# First make sure we're operating in a sane environment.
31#
32
DRC190854c2009-03-26 18:13:00 +000033$exedir = "";
34$slashndx = rindex($0, "/");
35if($slashndx>=0) {
36 $exedir = substr($0, 0, $slashndx+1);
37}
38
39$vncClasses = "";
40
41$xauth = "xauth";
42
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000043&SanityCheck();
44
45#
46# Global variables. You may want to configure some of these for your site.
47#
48
49$geometry = "1024x768";
DRC9d1c1572009-03-26 18:18:51 +000050#$depth = 16;
DRCe85eba52011-10-04 06:57:19 +000051$vncJavaFiles = (((-d "$vncClasses") && "$vncClasses") ||
52 ((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") ||
53 ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes"));
54
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000055$vncUserDir = "$ENV{HOME}/.vnc";
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +020056$skipxstartup = 0;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000057$xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority";
58
59$defaultXStartup
60 = ("#!/bin/sh\n\n".
DRC93248982009-03-26 18:14:38 +000061 "unset SESSION_MANAGER\n".
Adam Tkacc071e492009-05-20 09:01:24 +000062 "unset DBUS_SESSION_BUS_ADDRESS\n".
DRC93248982009-03-26 18:14:38 +000063 "OS=`uname -s`\n".
64 "if [ \$OS = 'Linux' ]; then\n".
65 " case \"\$WINDOWMANAGER\" in\n".
66 " \*gnome\*)\n".
67 " if [ -e /etc/SuSE-release ]; then\n".
68 " PATH=\$PATH:/opt/gnome/bin\n".
69 " export PATH\n".
70 " fi\n".
71 " ;;\n".
72 " esac\n".
73 "fi\n".
74 "if [ -x /etc/X11/xinit/xinitrc ]; then\n".
75 " exec /etc/X11/xinit/xinitrc\n".
76 "fi\n".
77 "if [ -f /etc/X11/xinit/xinitrc ]; then\n".
78 " exec sh /etc/X11/xinit/xinitrc\n".
79 "fi\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000080 "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n".
81 "xsetroot -solid grey\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000082 "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
83 "twm &\n");
84
grayskyddff8d02015-10-19 08:24:14 -040085$defaultConfig
86 = ("## Supported server options to pass to vncserver upon invocation can be listed\n".
87 "## in this file. See the following manpages for more: vncserver(1) Xvnc(1).\n".
88 "## Several common ones are shown below. Uncomment and modify to your liking.\n".
89 "##\n".
90 "# securitytypes=vncauth,tlsvnc\n".
91 "# desktop=sandbox\n".
92 "# geometry=2000x1200\n".
93 "# localhost\n".
94 "# alwaysshared\n");
95
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000096chop($host = `uname -n`);
97
DRC989dbd12009-04-22 13:23:17 +000098if (-d "/etc/X11/fontpath.d") {
99 $fontPath = "catalogue:/etc/X11/fontpath.d";
100}
DRC36546c12009-04-15 06:47:23 +0000101
DRCd6821bf2009-03-26 18:17:49 +0000102@fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/');
103if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');}
104if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');}
105if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');}
106push(@fontpaths, '/usr/share/fonts/default');
107
108@fonttypes = ('misc',
109 '75dpi',
110 '100dpi',
111 'Speedo',
112 'Type1');
113
114foreach $_fpath (@fontpaths) {
115 foreach $_ftype (@fonttypes) {
116 if (-f "$_fpath/$_ftype/fonts.dir") {
117 if (! -l "$_fpath/$_ftype") {
DRC36546c12009-04-15 06:47:23 +0000118 $defFontPath .= "$_fpath/$_ftype,";
DRCd6821bf2009-03-26 18:17:49 +0000119 }
120 }
121 }
122}
DRCd28792b2010-01-11 20:53:00 +0000123
DRC36546c12009-04-15 06:47:23 +0000124if ($defFontPath) {
125 if (substr($defFontPath, -1, 1) == ',') {
126 chop $defFontPath;
DRCd6821bf2009-03-26 18:17:49 +0000127 }
128}
129
DRCd28792b2010-01-11 20:53:00 +0000130if ($fontPath eq "") {
131 $fontPath = $defFontPath;
132}
133
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000134# Check command line options
135
136&ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200137 "-help",0,"-h",0,"--help",0,"-fp",1,"-list",0,"-fg",0,"-autokill",0,"-noxstartup",0);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000138
139&Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
140
141&Kill() if ($opt{'-kill'});
142
DRCb9d8e762011-02-09 08:24:58 +0000143&List() if ($opt{'-list'});
144
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000145# Uncomment this line if you want default geometry, depth and pixelformat
146# to match the current X display:
147# &GetXDisplayDefaults();
148
149if ($opt{'-geometry'}) {
150 $geometry = $opt{'-geometry'};
151}
152if ($opt{'-depth'}) {
153 $depth = $opt{'-depth'};
154 $pixelformat = "";
155}
156if ($opt{'-pixelformat'}) {
157 $pixelformat = $opt{'-pixelformat'};
158}
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200159if ($opt{'-noxstartup'}) {
160 $skipxstartup = 1;
161}
DRCeed5d1f2009-03-26 19:16:19 +0000162if ($opt{'-fp'}) {
163 $fontPath = $opt{'-fp'};
DRC36546c12009-04-15 06:47:23 +0000164 $fpArgSpecified = 1;
DRCeed5d1f2009-03-26 19:16:19 +0000165}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000166
167&CheckGeometryAndDepth();
168
169
170# Create the user's vnc directory if necessary.
171
172if (!(-e $vncUserDir)) {
173 if (!mkdir($vncUserDir,0755)) {
174 die "$prog: Could not create $vncUserDir.\n";
175 }
176}
177
Adam Tkacf586b842011-04-27 11:20:18 +0000178# Check whether VNC authentication is enabled, and if so, prompt the user to
179# create a VNC password if they don't already have one.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000180
Adam Tkacf586b842011-04-27 11:20:18 +0000181$securityTypeArgSpecified = 0;
182$vncAuthEnabled = 0;
183$passwordArgSpecified = 0;
184
185for ($i = 0; $i < @ARGV; ++$i) {
186 # -SecurityTypes can be followed by a space or "="
187 my @splitargs = split('=', $ARGV[$i]);
188 if (@splitargs <= 1 && $i < @ARGV - 1) {
189 push(@splitargs, $ARGV[$i + 1]);
190 }
191 if (lc(@splitargs[0]) eq "-securitytypes") {
192 if (@splitargs > 1) {
193 $securityTypeArgSpecified = 1;
194 }
195 foreach $arg2 (split(',', @splitargs[1])) {
196 if (lc($arg2) eq "vncauth" || lc($arg2) eq "tlsvnc"
197 || lc($arg2) eq "x509vnc") {
198 $vncAuthEnabled = 1;
199 }
200 }
201 }
202 if ((lc(@splitargs[0]) eq "-password")
203 || (lc(@splitargs[0]) eq "-passwordfile"
204 || (lc(@splitargs[0]) eq "-rfbauth"))) {
205 $passwordArgSpecified = 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000206 }
207}
208
Adam Tkacf586b842011-04-27 11:20:18 +0000209if ((!$securityTypeArgSpecified || $vncAuthEnabled) && !$passwordArgSpecified) {
210 ($z,$z,$mode) = stat("$vncUserDir/passwd");
211 if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
212 warn "\nYou will require a password to access your desktops.\n\n";
213 system($exedir."vncpasswd -q $vncUserDir/passwd");
214 if (($? >> 8) != 0) {
215 exit 1;
216 }
217 }
218}
219
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000220# Find display number.
221
222if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
223 $displayNumber = $1;
224 shift(@ARGV);
225 if (!&CheckDisplayNumber($displayNumber)) {
226 die "A VNC server is already running as :$displayNumber\n";
227 }
Adam Tkac39c9d992010-07-21 14:08:38 +0000228} elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/) && ($ARGV[0] !~ /^\+/)) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000229 &Usage();
230} else {
231 $displayNumber = &GetDisplayNumber();
232}
233
234$vncPort = 5900 + $displayNumber;
235
236$desktopLog = "$vncUserDir/$host:$displayNumber.log";
237unlink($desktopLog);
238
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000239# Make an X server cookie - use /dev/urandom on systems that have it,
240# otherwise use perl's random number generator, seeded with the sum
241# of the current time, our PID and part of the encrypted form of the password.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000242
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000243my $cookie = "";
244if (open(URANDOM, '<', '/dev/urandom')) {
245 my $randata;
246 if (sysread(URANDOM, $randata, 16) == 16) {
247 $cookie = unpack 'h*', $randata;
248 }
249 close(URANDOM);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000250}
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000251if ($cookie eq "") {
252 srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
253 for (1..16) {
254 $cookie .= sprintf("%02x", int(rand(256)) % 256);
255 }
256}
257
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000258system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
259system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
260
261if ($opt{'-name'}) {
262 $desktopName = $opt{'-name'};
263} else {
264 $desktopName = "$host:$displayNumber ($ENV{USER})";
265}
266
267# Now start the X VNC Server
268
DRC190854c2009-03-26 18:13:00 +0000269$cmd = $exedir."Xvnc :$displayNumber";
grayskyddff8d02015-10-19 08:24:14 -0400270
271my %default_opts;
272my %config;
273
274$default_opts{desktop} = &quotedString($desktopName);
275$default_opts{httpd} = $vncJavaFiles if ($vncJavaFiles);
276$default_opts{auth} = $xauthorityFile;
277$default_opts{geometry} = $geometry if ($geometry);
278$default_opts{depth} = $depth if ($depth);
279$default_opts{pixelformat} = $pixelformat if ($pixelformat);
280$default_opts{rfbwait} = 30000;
281$default_opts{rfbauth} = "$vncUserDir/passwd";
282$default_opts{rfbport} = $vncPort;
283$default_opts{fp} = $fontPath if ($fontPath);
284$default_opts{pn} = "";
285
286# if a user configuration file already exists
287if(stat("$vncUserDir/config")) {
288
289 # loads and parses configuration file
290 if(open(IN, "$vncUserDir/config")) {
291 while(<IN>) {
292 next if /^#/;
293 if(my ($k, $v) = /^\s*(\w+)\s*=\s*(.+)$/) {
294 $config{$k} = $v;
295 } elsif ($_ =~ m/^\s*(\S+)/) {
296 $config{$1} = $k;
297 }
298 }
299 close(IN);
300 }
301}
302
303foreach my $k (sort keys %config) {
304 $cmd .= " -$k $config{$k}";
305 # user's option takes precedence
306 delete $default_opts{$k};
307}
308
309foreach my $k (sort keys %default_opts) {
310 $cmd .= " -$k $default_opts{$k}";
311}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000312
DRCd6821bf2009-03-26 18:17:49 +0000313# Add color database stuff here, e.g.:
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000314#
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000315# $cmd .= " -co /usr/lib/X11/rgb";
316#
317
318foreach $arg (@ARGV) {
319 $cmd .= " " . &quotedString($arg);
320}
321$cmd .= " >> " . &quotedString($desktopLog) . " 2>&1";
322
323# Run $cmd and record the process ID.
324
325$pidFile = "$vncUserDir/$host:$displayNumber.pid";
326system("$cmd & echo \$! >$pidFile");
327
328# Give Xvnc a chance to start up
329
330sleep(3);
DRCd28792b2010-01-11 20:53:00 +0000331if ($fontPath ne $defFontPath) {
332 unless (kill 0, `cat $pidFile`) {
333 if ($fpArgSpecified) {
334 warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n";
335 warn "path you specified using the -fp argument is incorrect. Attempting to\n";
336 warn "determine an appropriate font path for this system and restart Xvnc using\n";
337 warn "that font path ...\n";
338 } else {
339 warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the font\n";
340 warn "catalog is not properly configured. Attempting to determine an appropriate\n";
341 warn "font path for this system and restart Xvnc using that font path ...\n";
342 }
343 $cmd =~ s@-fp [^ ]+@@;
344 $cmd .= " -fp $defFontPath" if ($defFontPath);
345 system("$cmd & echo \$! >$pidFile");
346 sleep(3);
DRC36546c12009-04-15 06:47:23 +0000347 }
DRCd6821bf2009-03-26 18:17:49 +0000348}
349unless (kill 0, `cat $pidFile`) {
350 warn "Could not start Xvnc.\n\n";
Michal Srbe6e11f92015-10-02 02:28:26 +0300351 unlink $pidFile;
DRCd6821bf2009-03-26 18:17:49 +0000352 open(LOG, "<$desktopLog");
353 while (<LOG>) { print; }
354 close(LOG);
355 die "\n";
356}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000357
358warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
359
360# Create the user's xstartup script if necessary.
361
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200362if (! $skipxstartup) {
363 if (!(-e "$vncUserDir/xstartup")) {
364 warn "Creating default startup script $vncUserDir/xstartup\n";
365 open(XSTARTUP, ">$vncUserDir/xstartup");
366 print XSTARTUP $defaultXStartup;
367 close(XSTARTUP);
368 chmod 0755, "$vncUserDir/xstartup";
369 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000370}
371
grayskyddff8d02015-10-19 08:24:14 -0400372# Create the user's config file if necessary.
373
374if (!(-e "$vncUserDir/config")) {
375 warn "Creating default config $vncUserDir/config\n";
376 open(XSTARTUP, ">$vncUserDir/config");
377 print XSTARTUP $defaultConfig;
378 close(XSTARTUP);
379 chmod 0644, "$vncUserDir/config";
380}
381
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000382# Run the X startup script.
383
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200384if (! $skipxstartup) {
385 warn "Starting applications specified in $vncUserDir/xstartup\n";
386}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000387warn "Log file is $desktopLog\n\n";
388
389# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
390# TCP (DISPLAY=host:n)
391
392if (-e "/tmp/.X11-unix/X$displayNumber" ||
393 -e "/usr/spool/sockets/X11/$displayNumber")
394{
395 $ENV{DISPLAY}= ":$displayNumber";
396} else {
397 $ENV{DISPLAY}= "$host:$displayNumber";
398}
399$ENV{VNCDESKTOP}= $desktopName;
400
Pierre Ossmana5b37c02015-07-30 11:04:02 +0200401system($exedir."vncconfig -nowin >> " . &quotedString($desktopLog) . " 2>&1 &");
DRCf6b58402011-10-05 21:28:03 +0000402
DRC8fb11912011-03-03 10:42:14 +0000403if ($opt{'-fg'}) {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200404 if (! $skipxstartup) {
405 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog) . " 2>&1");
406 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000407 if (kill 0, `cat $pidFile`) {
408 $opt{'-kill'} = ':'.$displayNumber;
409 &Kill();
410 }
DRC8fb11912011-03-03 10:42:14 +0000411} else {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000412 if ($opt{'-autokill'}) {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200413 if (! $skipxstartup) {
414 system("($vncUserDir/xstartup; $0 -kill :$displayNumber) >> "
415 . &quotedString($desktopLog) . " 2>&1 &");
416 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000417 } else {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200418 if (! $skipxstartup) {
419 system("$vncUserDir/xstartup >> " . &quotedString($desktopLog)
420 . " 2>&1 &");
421 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000422 }
DRC8fb11912011-03-03 10:42:14 +0000423}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000424
425exit;
426
427
428###############################################################################
429#
430# CheckGeometryAndDepth simply makes sure that the geometry and depth values
431# are sensible.
432#
433
434sub CheckGeometryAndDepth
435{
436 if ($geometry =~ /^(\d+)x(\d+)$/) {
437 $width = $1; $height = $2;
438
439 if (($width<1) || ($height<1)) {
440 die "$prog: geometry $geometry is invalid\n";
441 }
442
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000443 $geometry = "${width}x$height";
444 } else {
445 die "$prog: geometry $geometry is invalid\n";
446 }
447
DRCe5b4f752009-03-26 18:23:29 +0000448 if ($depth && (($depth < 8) || ($depth > 32))) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000449 die "Depth must be between 8 and 32\n";
450 }
451}
452
453
454#
455# GetDisplayNumber gets the lowest available display number. A display number
456# n is taken if something is listening on the VNC server port (5900+n) or the
457# X server port (6000+n).
458#
459
460sub GetDisplayNumber
461{
462 foreach $n (1..99) {
463 if (&CheckDisplayNumber($n)) {
464 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
465 }
466 }
467
468 die "$prog: no free display number on $host.\n";
469}
470
471
472#
473# CheckDisplayNumber checks if the given display number is available. A
474# display number n is taken if something is listening on the VNC server port
475# (5900+n) or the X server port (6000+n).
476#
477
478sub CheckDisplayNumber
479{
480 local ($n) = @_;
481
482 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
483 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
484 if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
485 close(S);
486 return 0;
487 }
488 close(S);
489
490 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
491 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
492 if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
493 close(S);
494 return 0;
495 }
496 close(S);
497
498 if (-e "/tmp/.X$n-lock") {
499 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
500 warn "Remove this file if there is no X server $host:$n\n";
501 return 0;
502 }
503
504 if (-e "/tmp/.X11-unix/X$n") {
505 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
506 warn "Remove this file if there is no X server $host:$n\n";
507 return 0;
508 }
509
510 if (-e "/usr/spool/sockets/X11/$n") {
511 warn("\nWarning: $host:$n is taken because of ".
512 "/usr/spool/sockets/X11/$n\n");
513 warn "Remove this file if there is no X server $host:$n\n";
514 return 0;
515 }
516
517 return 1;
518}
519
520
521#
522# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
523# format of the current X display being used. If successful, it sets the
524# options as appropriate so that the X VNC server will use the same settings
525# (minus an allowance for window manager decorations on the geometry). Using
526# the same depth and pixel format means that the VNC server won't have to
527# translate pixels when the desktop is being viewed on this X display (for
528# TrueColor displays anyway).
529#
530
531sub GetXDisplayDefaults
532{
533 local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
534 $red, $green, $blue);
535
536 $wmDecorationWidth = 4; # a guess at typical size for window manager
537 $wmDecorationHeight = 24; # decoration size
538
539 return if (!defined($ENV{DISPLAY}));
540
541 @lines = `xdpyinfo 2>/dev/null`;
542
543 return if ($? != 0);
544
545 @matchlines = grep(/dimensions/, @lines);
546 if (@matchlines) {
547 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
548
549 $width -= $wmDecorationWidth;
550 $height -= $wmDecorationHeight;
551
552 $geometry = "${width}x$height";
553 }
554
555 @matchlines = grep(/default visual id/, @lines);
556 if (@matchlines) {
557 ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
558
559 for ($i = 0; $i < @lines; $i++) {
560 if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
561 if (($lines[$i+1] !~ /TrueColor/) ||
562 ($lines[$i+2] !~ /depth/) ||
563 ($lines[$i+4] !~ /red, green, blue masks/))
564 {
565 return;
566 }
567 last;
568 }
569 }
570
571 return if ($i >= @lines);
572
573 ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
574 ($red,$green,$blue)
575 = ($lines[$i+4]
576 =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
577
578 $red = hex($red);
579 $green = hex($green);
580 $blue = hex($blue);
581
582 if ($red > $blue) {
583 $red = int(log($red) / log(2)) - int(log($green) / log(2));
584 $green = int(log($green) / log(2)) - int(log($blue) / log(2));
585 $blue = int(log($blue) / log(2)) + 1;
586 $pixelformat = "rgb$red$green$blue";
587 } else {
588 $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
589 $green = int(log($green) / log(2)) - int(log($red) / log(2));
590 $red = int(log($red) / log(2)) + 1;
591 $pixelformat = "bgr$blue$green$red";
592 }
593 }
594}
595
596
597#
598# quotedString returns a string which yields the original string when parsed
599# by a shell.
600#
601
602sub quotedString
603{
604 local ($in) = @_;
605
606 $in =~ s/\'/\'\"\'\"\'/g;
607
608 return "'$in'";
609}
610
611
612#
613# removeSlashes turns slashes into underscores for use as a file name.
614#
615
616sub removeSlashes
617{
618 local ($in) = @_;
619
620 $in =~ s|/|_|g;
621
622 return "$in";
623}
624
625
626#
627# Usage
628#
629
630sub Usage
631{
632 die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
633 " [-geometry <width>x<height>]\n".
634 " [-pixelformat rgbNNN|bgrNNN]\n".
DRCeed5d1f2009-03-26 19:16:19 +0000635 " [-fp <font-path>]\n".
DRC8fb11912011-03-03 10:42:14 +0000636 " [-fg]\n".
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000637 " [-autokill]\n".
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200638 " [-noxstartup]\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000639 " <Xvnc-options>...\n\n".
DRCb9d8e762011-02-09 08:24:58 +0000640 " $prog -kill <X-display>\n\n".
641 " $prog -list\n\n");
642}
643
644
645#
646# List
647#
648
649sub List
650{
651 opendir(dir, $vncUserDir);
652 my @filelist = readdir(dir);
653 closedir(dir);
DRC075d9fa2011-02-10 04:19:46 +0000654 print "\nTigerVNC server sessions:\n\n";
DRCb9d8e762011-02-09 08:24:58 +0000655 print "X DISPLAY #\tPROCESS ID\n";
656 foreach my $file (@filelist) {
657 if ($file =~ /$host:(\d+)$\.pid/) {
Michal Srbe6e11f92015-10-02 02:28:26 +0300658 chop($tmp_pid = `cat $vncUserDir/$file`);
659 if (kill 0, $tmp_pid) {
660 print ":".$1."\t\t".`cat $vncUserDir/$file`;
661 } else {
662 unlink ($vncUserDir . "/" . $file);
663 }
DRCb9d8e762011-02-09 08:24:58 +0000664 }
665 }
666 exit 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000667}
668
669
670#
671# Kill
672#
673
674sub Kill
675{
676 $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
677
678 if ($opt{'-kill'} =~ /^:\d+$/) {
679 $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
680 } else {
681 if ($opt{'-kill'} !~ /^$host:/) {
682 die "\nCan't tell if $opt{'-kill'} is on $host\n".
683 "Use -kill :<number> instead\n\n";
684 }
685 $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
686 }
687
688 if (! -r $pidFile) {
689 die "\nCan't find file $pidFile\n".
690 "You'll have to kill the Xvnc process manually\n\n";
691 }
692
693 $SIG{'HUP'} = 'IGNORE';
694 chop($pid = `cat $pidFile`);
695 warn "Killing Xvnc process ID $pid\n";
DRCb9d8e762011-02-09 08:24:58 +0000696
697 if (kill 0, $pid) {
698 system("kill $pid");
699 sleep(1);
700 if (kill 0, $pid) {
701 print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n";
702 print " ".$0." -kill ".$opt{'-kill'}."\n";
703 print "to clean up the socket files.\n";
704 exit
705 }
706
707 } else {
708 warn "Xvnc process ID $pid already killed\n";
709 $opt{'-kill'} =~ s/://;
710
711 if (-e "/tmp/.X11-unix/X$opt{'-kill'}") {
712 print "Xvnc did not appear to shut down cleanly.";
713 print " Removing /tmp/.X11-unix/X$opt{'-kill'}\n";
714 unlink "/tmp/.X11-unix/X$opt{'-kill'}";
715 }
716 if (-e "/tmp/.X$opt{'-kill'}-lock") {
717 print "Xvnc did not appear to shut down cleanly.";
718 print " Removing /tmp/.X$opt{'-kill'}-lock\n";
719 unlink "/tmp/.X$opt{'-kill'}-lock";
720 }
721 }
722
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000723 unlink $pidFile;
724 exit;
725}
726
727
728#
729# ParseOptions takes a list of possible options and a boolean indicating
730# whether the option has a value following, and sets up an associative array
731# %opt of the values of the options given on the command line. It removes all
732# the arguments it uses from @ARGV and returns them in @optArgs.
733#
734
735sub ParseOptions
736{
737 local (@optval) = @_;
738 local ($opt, @opts, %valFollows, @newargs);
739
740 while (@optval) {
741 $opt = shift(@optval);
742 push(@opts,$opt);
743 $valFollows{$opt} = shift(@optval);
744 }
745
746 @optArgs = ();
747 %opt = ();
748
749 arg: while (defined($arg = shift(@ARGV))) {
750 foreach $opt (@opts) {
751 if ($arg eq $opt) {
752 push(@optArgs, $arg);
753 if ($valFollows{$opt}) {
754 if (@ARGV == 0) {
755 &Usage();
756 }
757 $opt{$opt} = shift(@ARGV);
758 push(@optArgs, $opt{$opt});
759 } else {
760 $opt{$opt} = 1;
761 }
762 next arg;
763 }
764 }
765 push(@newargs,$arg);
766 }
767
768 @ARGV = @newargs;
769}
770
771
772#
773# Routine to make sure we're operating in a sane environment.
774#
775
776sub SanityCheck
777{
778 local ($cmd);
779
780 #
781 # Get the program name
782 #
783
784 ($prog) = ($0 =~ m|([^/]+)$|);
785
786 #
787 # Check we have all the commands we'll need on the path.
788 #
789
790 cmd:
DRC190854c2009-03-26 18:13:00 +0000791 foreach $cmd ("uname") {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000792 for (split(/:/,$ENV{PATH})) {
793 if (-x "$_/$cmd") {
794 next cmd;
795 }
796 }
797 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
798 }
DRC190854c2009-03-26 18:13:00 +0000799 if (-x "/usr/X11R6/bin/xauth") {
800 $xauth = "/usr/X11R6/bin/xauth";
801 }
802 else {
803 cmd1:
804 foreach $cmd ("xauth") {
805 for (split(/:/,$ENV{PATH})) {
806 if (-x "$_/$cmd") {
807 next cmd1;
808 }
809 }
810 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
811 }
812 }
813
814 if($exedir eq "") {
815 cmd2:
816 foreach $cmd ("Xvnc","vncpasswd") {
817 for (split(/:/,$ENV{PATH})) {
818 if (-x "$_/$cmd") {
819 $vncClasses = "$_/../vnc/classes";
820 next cmd2;
821 }
822 }
823 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
824 }
825 }
826 else {
827 cmd3:
828 foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") {
829 for (split(/:/,$ENV{PATH})) {
830 if (-x "$cmd") {
831 $vncClasses = $exedir."../vnc/classes";
832 next cmd3;
833 }
834 }
835 die "$prog: couldn't find \"$cmd\".\n";
836 }
837 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000838
839 #
840 # Check the HOME environment variable is set
841 #
842
843 if (!defined($ENV{HOME})) {
844 die "$prog: The HOME environment variable is not set.\n";
845 }
DRC190854c2009-03-26 18:13:00 +0000846# chdir($ENV{HOME});
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000847
848 #
849 # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
850 # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
851 # we just guess at the values. If you find perl moaning here, just
852 # hard-code the values of AF_INET and SOCK_STREAM. You can find these out
853 # for your platform by looking in /usr/include/sys/socket.h and related
854 # files.
855 #
856
857 chop($os = `uname`);
858 chop($osrev = `uname -r`);
859
860 eval 'use Socket';
861 if ($@) {
862 eval 'require "sys/socket.ph"';
863 if ($@) {
864 if (($os eq "SunOS") && ($osrev !~ /^4/)) {
865 $AF_INET = 2;
866 $SOCK_STREAM = 2;
867 } else {
868 $AF_INET = 2;
869 $SOCK_STREAM = 1;
870 }
871 } else {
872 $AF_INET = &AF_INET;
873 $SOCK_STREAM = &SOCK_STREAM;
874 }
875 } else {
876 $AF_INET = &AF_INET;
877 $SOCK_STREAM = &SOCK_STREAM;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000878 }
879}