blob: c4d0535994e3f86ca1331f13b55d3ef84a4062a3 [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
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +010059$xstartup = $vncUserDir . "/xstartup";
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000060$defaultXStartup
61 = ("#!/bin/sh\n\n".
DRC93248982009-03-26 18:14:38 +000062 "unset SESSION_MANAGER\n".
Adam Tkacc071e492009-05-20 09:01:24 +000063 "unset DBUS_SESSION_BUS_ADDRESS\n".
DRC93248982009-03-26 18:14:38 +000064 "OS=`uname -s`\n".
65 "if [ \$OS = 'Linux' ]; then\n".
66 " case \"\$WINDOWMANAGER\" in\n".
67 " \*gnome\*)\n".
68 " if [ -e /etc/SuSE-release ]; then\n".
69 " PATH=\$PATH:/opt/gnome/bin\n".
70 " export PATH\n".
71 " fi\n".
72 " ;;\n".
73 " esac\n".
74 "fi\n".
75 "if [ -x /etc/X11/xinit/xinitrc ]; then\n".
76 " exec /etc/X11/xinit/xinitrc\n".
77 "fi\n".
78 "if [ -f /etc/X11/xinit/xinitrc ]; then\n".
79 " exec sh /etc/X11/xinit/xinitrc\n".
80 "fi\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000081 "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n".
82 "xsetroot -solid grey\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000083 "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
84 "twm &\n");
85
grayskyddff8d02015-10-19 08:24:14 -040086$defaultConfig
87 = ("## Supported server options to pass to vncserver upon invocation can be listed\n".
88 "## in this file. See the following manpages for more: vncserver(1) Xvnc(1).\n".
89 "## Several common ones are shown below. Uncomment and modify to your liking.\n".
90 "##\n".
91 "# securitytypes=vncauth,tlsvnc\n".
92 "# desktop=sandbox\n".
93 "# geometry=2000x1200\n".
94 "# localhost\n".
95 "# alwaysshared\n");
96
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000097chop($host = `uname -n`);
98
DRC989dbd12009-04-22 13:23:17 +000099if (-d "/etc/X11/fontpath.d") {
100 $fontPath = "catalogue:/etc/X11/fontpath.d";
101}
DRC36546c12009-04-15 06:47:23 +0000102
DRCd6821bf2009-03-26 18:17:49 +0000103@fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/');
104if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');}
105if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');}
106if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');}
107push(@fontpaths, '/usr/share/fonts/default');
108
109@fonttypes = ('misc',
110 '75dpi',
111 '100dpi',
112 'Speedo',
113 'Type1');
114
115foreach $_fpath (@fontpaths) {
116 foreach $_ftype (@fonttypes) {
117 if (-f "$_fpath/$_ftype/fonts.dir") {
118 if (! -l "$_fpath/$_ftype") {
DRC36546c12009-04-15 06:47:23 +0000119 $defFontPath .= "$_fpath/$_ftype,";
DRCd6821bf2009-03-26 18:17:49 +0000120 }
121 }
122 }
123}
DRCd28792b2010-01-11 20:53:00 +0000124
DRC36546c12009-04-15 06:47:23 +0000125if ($defFontPath) {
126 if (substr($defFontPath, -1, 1) == ',') {
127 chop $defFontPath;
DRCd6821bf2009-03-26 18:17:49 +0000128 }
129}
130
DRCd28792b2010-01-11 20:53:00 +0000131if ($fontPath eq "") {
132 $fontPath = $defFontPath;
133}
134
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000135# Check command line options
136
137&ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100138 "-help",0,"-h",0,"--help",0,"-fp",1,"-list",0,"-fg",0,"-autokill",0,"-noxstartup",0,"-xstartup",1);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000139
140&Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
141
142&Kill() if ($opt{'-kill'});
143
DRCb9d8e762011-02-09 08:24:58 +0000144&List() if ($opt{'-list'});
145
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000146# Uncomment this line if you want default geometry, depth and pixelformat
147# to match the current X display:
148# &GetXDisplayDefaults();
149
150if ($opt{'-geometry'}) {
151 $geometry = $opt{'-geometry'};
152}
153if ($opt{'-depth'}) {
154 $depth = $opt{'-depth'};
155 $pixelformat = "";
156}
157if ($opt{'-pixelformat'}) {
158 $pixelformat = $opt{'-pixelformat'};
159}
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200160if ($opt{'-noxstartup'}) {
161 $skipxstartup = 1;
162}
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100163if ($opt{'-xstartup'}) {
164 $xstartup = $opt{'-xstartup'};
165}
DRCeed5d1f2009-03-26 19:16:19 +0000166if ($opt{'-fp'}) {
167 $fontPath = $opt{'-fp'};
DRC36546c12009-04-15 06:47:23 +0000168 $fpArgSpecified = 1;
DRCeed5d1f2009-03-26 19:16:19 +0000169}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000170
171&CheckGeometryAndDepth();
172
173
174# Create the user's vnc directory if necessary.
175
176if (!(-e $vncUserDir)) {
177 if (!mkdir($vncUserDir,0755)) {
178 die "$prog: Could not create $vncUserDir.\n";
179 }
180}
181
Adam Tkacf586b842011-04-27 11:20:18 +0000182# Check whether VNC authentication is enabled, and if so, prompt the user to
183# create a VNC password if they don't already have one.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000184
Adam Tkacf586b842011-04-27 11:20:18 +0000185$securityTypeArgSpecified = 0;
186$vncAuthEnabled = 0;
187$passwordArgSpecified = 0;
188
189for ($i = 0; $i < @ARGV; ++$i) {
190 # -SecurityTypes can be followed by a space or "="
191 my @splitargs = split('=', $ARGV[$i]);
192 if (@splitargs <= 1 && $i < @ARGV - 1) {
193 push(@splitargs, $ARGV[$i + 1]);
194 }
195 if (lc(@splitargs[0]) eq "-securitytypes") {
196 if (@splitargs > 1) {
197 $securityTypeArgSpecified = 1;
198 }
199 foreach $arg2 (split(',', @splitargs[1])) {
200 if (lc($arg2) eq "vncauth" || lc($arg2) eq "tlsvnc"
201 || lc($arg2) eq "x509vnc") {
202 $vncAuthEnabled = 1;
203 }
204 }
205 }
206 if ((lc(@splitargs[0]) eq "-password")
207 || (lc(@splitargs[0]) eq "-passwordfile"
208 || (lc(@splitargs[0]) eq "-rfbauth"))) {
209 $passwordArgSpecified = 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000210 }
211}
212
Adam Tkacf586b842011-04-27 11:20:18 +0000213if ((!$securityTypeArgSpecified || $vncAuthEnabled) && !$passwordArgSpecified) {
214 ($z,$z,$mode) = stat("$vncUserDir/passwd");
215 if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
216 warn "\nYou will require a password to access your desktops.\n\n";
217 system($exedir."vncpasswd -q $vncUserDir/passwd");
218 if (($? >> 8) != 0) {
219 exit 1;
220 }
221 }
222}
223
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000224# Find display number.
225
226if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
227 $displayNumber = $1;
228 shift(@ARGV);
229 if (!&CheckDisplayNumber($displayNumber)) {
230 die "A VNC server is already running as :$displayNumber\n";
231 }
Adam Tkac39c9d992010-07-21 14:08:38 +0000232} elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/) && ($ARGV[0] !~ /^\+/)) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000233 &Usage();
234} else {
235 $displayNumber = &GetDisplayNumber();
236}
237
238$vncPort = 5900 + $displayNumber;
239
240$desktopLog = "$vncUserDir/$host:$displayNumber.log";
241unlink($desktopLog);
242
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000243# Make an X server cookie - use /dev/urandom on systems that have it,
244# otherwise use perl's random number generator, seeded with the sum
245# of the current time, our PID and part of the encrypted form of the password.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000246
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000247my $cookie = "";
248if (open(URANDOM, '<', '/dev/urandom')) {
249 my $randata;
250 if (sysread(URANDOM, $randata, 16) == 16) {
251 $cookie = unpack 'h*', $randata;
252 }
253 close(URANDOM);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000254}
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000255if ($cookie eq "") {
256 srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
257 for (1..16) {
258 $cookie .= sprintf("%02x", int(rand(256)) % 256);
259 }
260}
261
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000262system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
263system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
264
265if ($opt{'-name'}) {
266 $desktopName = $opt{'-name'};
267} else {
268 $desktopName = "$host:$displayNumber ($ENV{USER})";
269}
270
271# Now start the X VNC Server
272
DRC190854c2009-03-26 18:13:00 +0000273$cmd = $exedir."Xvnc :$displayNumber";
grayskyddff8d02015-10-19 08:24:14 -0400274
275my %default_opts;
276my %config;
277
278$default_opts{desktop} = &quotedString($desktopName);
279$default_opts{httpd} = $vncJavaFiles if ($vncJavaFiles);
280$default_opts{auth} = $xauthorityFile;
281$default_opts{geometry} = $geometry if ($geometry);
282$default_opts{depth} = $depth if ($depth);
283$default_opts{pixelformat} = $pixelformat if ($pixelformat);
284$default_opts{rfbwait} = 30000;
285$default_opts{rfbauth} = "$vncUserDir/passwd";
286$default_opts{rfbport} = $vncPort;
287$default_opts{fp} = $fontPath if ($fontPath);
288$default_opts{pn} = "";
289
290# if a user configuration file already exists
291if(stat("$vncUserDir/config")) {
292
293 # loads and parses configuration file
294 if(open(IN, "$vncUserDir/config")) {
295 while(<IN>) {
296 next if /^#/;
297 if(my ($k, $v) = /^\s*(\w+)\s*=\s*(.+)$/) {
298 $config{$k} = $v;
299 } elsif ($_ =~ m/^\s*(\S+)/) {
300 $config{$1} = $k;
301 }
302 }
303 close(IN);
304 }
305}
306
307foreach my $k (sort keys %config) {
308 $cmd .= " -$k $config{$k}";
309 # user's option takes precedence
310 delete $default_opts{$k};
311}
312
313foreach my $k (sort keys %default_opts) {
314 $cmd .= " -$k $default_opts{$k}";
315}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000316
DRCd6821bf2009-03-26 18:17:49 +0000317# Add color database stuff here, e.g.:
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000318#
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000319# $cmd .= " -co /usr/lib/X11/rgb";
320#
321
322foreach $arg (@ARGV) {
323 $cmd .= " " . &quotedString($arg);
324}
325$cmd .= " >> " . &quotedString($desktopLog) . " 2>&1";
326
327# Run $cmd and record the process ID.
328
329$pidFile = "$vncUserDir/$host:$displayNumber.pid";
330system("$cmd & echo \$! >$pidFile");
331
332# Give Xvnc a chance to start up
333
334sleep(3);
DRCd28792b2010-01-11 20:53:00 +0000335if ($fontPath ne $defFontPath) {
336 unless (kill 0, `cat $pidFile`) {
337 if ($fpArgSpecified) {
338 warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n";
339 warn "path you specified using the -fp argument is incorrect. Attempting to\n";
340 warn "determine an appropriate font path for this system and restart Xvnc using\n";
341 warn "that font path ...\n";
342 } else {
343 warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the font\n";
344 warn "catalog is not properly configured. Attempting to determine an appropriate\n";
345 warn "font path for this system and restart Xvnc using that font path ...\n";
346 }
347 $cmd =~ s@-fp [^ ]+@@;
348 $cmd .= " -fp $defFontPath" if ($defFontPath);
349 system("$cmd & echo \$! >$pidFile");
350 sleep(3);
DRC36546c12009-04-15 06:47:23 +0000351 }
DRCd6821bf2009-03-26 18:17:49 +0000352}
353unless (kill 0, `cat $pidFile`) {
354 warn "Could not start Xvnc.\n\n";
Michal Srbe6e11f92015-10-02 02:28:26 +0300355 unlink $pidFile;
DRCd6821bf2009-03-26 18:17:49 +0000356 open(LOG, "<$desktopLog");
357 while (<LOG>) { print; }
358 close(LOG);
359 die "\n";
360}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000361
362warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
363
364# Create the user's xstartup script if necessary.
365
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200366if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100367 if (!(-e "$xstartup")) {
368 warn "Creating default startup script $xstartup\n";
369 open(XSTARTUP, ">$xstartup");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200370 print XSTARTUP $defaultXStartup;
371 close(XSTARTUP);
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100372 chmod 0755, "$xstartup";
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200373 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000374}
375
grayskyddff8d02015-10-19 08:24:14 -0400376# Create the user's config file if necessary.
377
378if (!(-e "$vncUserDir/config")) {
379 warn "Creating default config $vncUserDir/config\n";
380 open(XSTARTUP, ">$vncUserDir/config");
381 print XSTARTUP $defaultConfig;
382 close(XSTARTUP);
383 chmod 0644, "$vncUserDir/config";
384}
385
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000386# Run the X startup script.
387
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200388if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100389 warn "Starting applications specified in $xstartup\n";
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200390}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000391warn "Log file is $desktopLog\n\n";
392
393# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
394# TCP (DISPLAY=host:n)
395
396if (-e "/tmp/.X11-unix/X$displayNumber" ||
397 -e "/usr/spool/sockets/X11/$displayNumber")
398{
399 $ENV{DISPLAY}= ":$displayNumber";
400} else {
401 $ENV{DISPLAY}= "$host:$displayNumber";
402}
403$ENV{VNCDESKTOP}= $desktopName;
404
Pierre Ossmana5b37c02015-07-30 11:04:02 +0200405system($exedir."vncconfig -nowin >> " . &quotedString($desktopLog) . " 2>&1 &");
DRCf6b58402011-10-05 21:28:03 +0000406
DRC8fb11912011-03-03 10:42:14 +0000407if ($opt{'-fg'}) {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200408 if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100409 system("$xstartup >> " . &quotedString($desktopLog) . " 2>&1");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200410 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000411 if (kill 0, `cat $pidFile`) {
412 $opt{'-kill'} = ':'.$displayNumber;
413 &Kill();
414 }
DRC8fb11912011-03-03 10:42:14 +0000415} else {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000416 if ($opt{'-autokill'}) {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200417 if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100418 system("($xstartup; $0 -kill :$displayNumber) >> "
419 . &quotedString($desktopLog) . " 2>&1 &");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200420 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000421 } else {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200422 if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100423 system("$xstartup >> " . &quotedString($desktopLog)
424 . " 2>&1 &");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200425 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000426 }
DRC8fb11912011-03-03 10:42:14 +0000427}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000428
429exit;
430
431
432###############################################################################
433#
434# CheckGeometryAndDepth simply makes sure that the geometry and depth values
435# are sensible.
436#
437
438sub CheckGeometryAndDepth
439{
440 if ($geometry =~ /^(\d+)x(\d+)$/) {
441 $width = $1; $height = $2;
442
443 if (($width<1) || ($height<1)) {
444 die "$prog: geometry $geometry is invalid\n";
445 }
446
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000447 $geometry = "${width}x$height";
448 } else {
449 die "$prog: geometry $geometry is invalid\n";
450 }
451
DRCe5b4f752009-03-26 18:23:29 +0000452 if ($depth && (($depth < 8) || ($depth > 32))) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000453 die "Depth must be between 8 and 32\n";
454 }
455}
456
457
458#
459# GetDisplayNumber gets the lowest available display number. A display number
460# n is taken if something is listening on the VNC server port (5900+n) or the
461# X server port (6000+n).
462#
463
464sub GetDisplayNumber
465{
466 foreach $n (1..99) {
467 if (&CheckDisplayNumber($n)) {
468 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
469 }
470 }
471
472 die "$prog: no free display number on $host.\n";
473}
474
475
476#
477# CheckDisplayNumber checks if the given display number is available. A
478# display number n is taken if something is listening on the VNC server port
479# (5900+n) or the X server port (6000+n).
480#
481
482sub CheckDisplayNumber
483{
484 local ($n) = @_;
485
486 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
487 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
488 if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
489 close(S);
490 return 0;
491 }
492 close(S);
493
494 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
495 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
496 if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
497 close(S);
498 return 0;
499 }
500 close(S);
501
502 if (-e "/tmp/.X$n-lock") {
503 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
504 warn "Remove this file if there is no X server $host:$n\n";
505 return 0;
506 }
507
508 if (-e "/tmp/.X11-unix/X$n") {
509 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
510 warn "Remove this file if there is no X server $host:$n\n";
511 return 0;
512 }
513
514 if (-e "/usr/spool/sockets/X11/$n") {
515 warn("\nWarning: $host:$n is taken because of ".
516 "/usr/spool/sockets/X11/$n\n");
517 warn "Remove this file if there is no X server $host:$n\n";
518 return 0;
519 }
520
521 return 1;
522}
523
524
525#
526# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
527# format of the current X display being used. If successful, it sets the
528# options as appropriate so that the X VNC server will use the same settings
529# (minus an allowance for window manager decorations on the geometry). Using
530# the same depth and pixel format means that the VNC server won't have to
531# translate pixels when the desktop is being viewed on this X display (for
532# TrueColor displays anyway).
533#
534
535sub GetXDisplayDefaults
536{
537 local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
538 $red, $green, $blue);
539
540 $wmDecorationWidth = 4; # a guess at typical size for window manager
541 $wmDecorationHeight = 24; # decoration size
542
543 return if (!defined($ENV{DISPLAY}));
544
545 @lines = `xdpyinfo 2>/dev/null`;
546
547 return if ($? != 0);
548
549 @matchlines = grep(/dimensions/, @lines);
550 if (@matchlines) {
551 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
552
553 $width -= $wmDecorationWidth;
554 $height -= $wmDecorationHeight;
555
556 $geometry = "${width}x$height";
557 }
558
559 @matchlines = grep(/default visual id/, @lines);
560 if (@matchlines) {
561 ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
562
563 for ($i = 0; $i < @lines; $i++) {
564 if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
565 if (($lines[$i+1] !~ /TrueColor/) ||
566 ($lines[$i+2] !~ /depth/) ||
567 ($lines[$i+4] !~ /red, green, blue masks/))
568 {
569 return;
570 }
571 last;
572 }
573 }
574
575 return if ($i >= @lines);
576
577 ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
578 ($red,$green,$blue)
579 = ($lines[$i+4]
580 =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
581
582 $red = hex($red);
583 $green = hex($green);
584 $blue = hex($blue);
585
586 if ($red > $blue) {
587 $red = int(log($red) / log(2)) - int(log($green) / log(2));
588 $green = int(log($green) / log(2)) - int(log($blue) / log(2));
589 $blue = int(log($blue) / log(2)) + 1;
590 $pixelformat = "rgb$red$green$blue";
591 } else {
592 $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
593 $green = int(log($green) / log(2)) - int(log($red) / log(2));
594 $red = int(log($red) / log(2)) + 1;
595 $pixelformat = "bgr$blue$green$red";
596 }
597 }
598}
599
600
601#
602# quotedString returns a string which yields the original string when parsed
603# by a shell.
604#
605
606sub quotedString
607{
608 local ($in) = @_;
609
610 $in =~ s/\'/\'\"\'\"\'/g;
611
612 return "'$in'";
613}
614
615
616#
617# removeSlashes turns slashes into underscores for use as a file name.
618#
619
620sub removeSlashes
621{
622 local ($in) = @_;
623
624 $in =~ s|/|_|g;
625
626 return "$in";
627}
628
629
630#
631# Usage
632#
633
634sub Usage
635{
636 die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
637 " [-geometry <width>x<height>]\n".
638 " [-pixelformat rgbNNN|bgrNNN]\n".
DRCeed5d1f2009-03-26 19:16:19 +0000639 " [-fp <font-path>]\n".
DRC8fb11912011-03-03 10:42:14 +0000640 " [-fg]\n".
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000641 " [-autokill]\n".
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200642 " [-noxstartup]\n".
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100643 " [-xstartup <file>]\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000644 " <Xvnc-options>...\n\n".
DRCb9d8e762011-02-09 08:24:58 +0000645 " $prog -kill <X-display>\n\n".
646 " $prog -list\n\n");
647}
648
649
650#
651# List
652#
653
654sub List
655{
656 opendir(dir, $vncUserDir);
657 my @filelist = readdir(dir);
658 closedir(dir);
DRC075d9fa2011-02-10 04:19:46 +0000659 print "\nTigerVNC server sessions:\n\n";
DRCb9d8e762011-02-09 08:24:58 +0000660 print "X DISPLAY #\tPROCESS ID\n";
661 foreach my $file (@filelist) {
662 if ($file =~ /$host:(\d+)$\.pid/) {
Michal Srbe6e11f92015-10-02 02:28:26 +0300663 chop($tmp_pid = `cat $vncUserDir/$file`);
664 if (kill 0, $tmp_pid) {
665 print ":".$1."\t\t".`cat $vncUserDir/$file`;
666 } else {
667 unlink ($vncUserDir . "/" . $file);
668 }
DRCb9d8e762011-02-09 08:24:58 +0000669 }
670 }
671 exit 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000672}
673
674
675#
676# Kill
677#
678
679sub Kill
680{
681 $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
682
683 if ($opt{'-kill'} =~ /^:\d+$/) {
684 $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
685 } else {
686 if ($opt{'-kill'} !~ /^$host:/) {
687 die "\nCan't tell if $opt{'-kill'} is on $host\n".
688 "Use -kill :<number> instead\n\n";
689 }
690 $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
691 }
692
693 if (! -r $pidFile) {
694 die "\nCan't find file $pidFile\n".
695 "You'll have to kill the Xvnc process manually\n\n";
696 }
697
698 $SIG{'HUP'} = 'IGNORE';
699 chop($pid = `cat $pidFile`);
700 warn "Killing Xvnc process ID $pid\n";
DRCb9d8e762011-02-09 08:24:58 +0000701
702 if (kill 0, $pid) {
703 system("kill $pid");
704 sleep(1);
705 if (kill 0, $pid) {
706 print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n";
707 print " ".$0." -kill ".$opt{'-kill'}."\n";
708 print "to clean up the socket files.\n";
709 exit
710 }
711
712 } else {
713 warn "Xvnc process ID $pid already killed\n";
714 $opt{'-kill'} =~ s/://;
715
716 if (-e "/tmp/.X11-unix/X$opt{'-kill'}") {
717 print "Xvnc did not appear to shut down cleanly.";
718 print " Removing /tmp/.X11-unix/X$opt{'-kill'}\n";
719 unlink "/tmp/.X11-unix/X$opt{'-kill'}";
720 }
721 if (-e "/tmp/.X$opt{'-kill'}-lock") {
722 print "Xvnc did not appear to shut down cleanly.";
723 print " Removing /tmp/.X$opt{'-kill'}-lock\n";
724 unlink "/tmp/.X$opt{'-kill'}-lock";
725 }
726 }
727
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000728 unlink $pidFile;
729 exit;
730}
731
732
733#
734# ParseOptions takes a list of possible options and a boolean indicating
735# whether the option has a value following, and sets up an associative array
736# %opt of the values of the options given on the command line. It removes all
737# the arguments it uses from @ARGV and returns them in @optArgs.
738#
739
740sub ParseOptions
741{
742 local (@optval) = @_;
743 local ($opt, @opts, %valFollows, @newargs);
744
745 while (@optval) {
746 $opt = shift(@optval);
747 push(@opts,$opt);
748 $valFollows{$opt} = shift(@optval);
749 }
750
751 @optArgs = ();
752 %opt = ();
753
754 arg: while (defined($arg = shift(@ARGV))) {
755 foreach $opt (@opts) {
756 if ($arg eq $opt) {
757 push(@optArgs, $arg);
758 if ($valFollows{$opt}) {
759 if (@ARGV == 0) {
760 &Usage();
761 }
762 $opt{$opt} = shift(@ARGV);
763 push(@optArgs, $opt{$opt});
764 } else {
765 $opt{$opt} = 1;
766 }
767 next arg;
768 }
769 }
770 push(@newargs,$arg);
771 }
772
773 @ARGV = @newargs;
774}
775
776
777#
778# Routine to make sure we're operating in a sane environment.
779#
780
781sub SanityCheck
782{
783 local ($cmd);
784
785 #
786 # Get the program name
787 #
788
789 ($prog) = ($0 =~ m|([^/]+)$|);
790
791 #
792 # Check we have all the commands we'll need on the path.
793 #
794
795 cmd:
DRC190854c2009-03-26 18:13:00 +0000796 foreach $cmd ("uname") {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000797 for (split(/:/,$ENV{PATH})) {
798 if (-x "$_/$cmd") {
799 next cmd;
800 }
801 }
802 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
803 }
DRC190854c2009-03-26 18:13:00 +0000804 if (-x "/usr/X11R6/bin/xauth") {
805 $xauth = "/usr/X11R6/bin/xauth";
806 }
807 else {
808 cmd1:
809 foreach $cmd ("xauth") {
810 for (split(/:/,$ENV{PATH})) {
811 if (-x "$_/$cmd") {
812 next cmd1;
813 }
814 }
815 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
816 }
817 }
818
819 if($exedir eq "") {
820 cmd2:
821 foreach $cmd ("Xvnc","vncpasswd") {
822 for (split(/:/,$ENV{PATH})) {
823 if (-x "$_/$cmd") {
824 $vncClasses = "$_/../vnc/classes";
825 next cmd2;
826 }
827 }
828 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
829 }
830 }
831 else {
832 cmd3:
833 foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") {
834 for (split(/:/,$ENV{PATH})) {
835 if (-x "$cmd") {
836 $vncClasses = $exedir."../vnc/classes";
837 next cmd3;
838 }
839 }
840 die "$prog: couldn't find \"$cmd\".\n";
841 }
842 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000843
844 #
845 # Check the HOME environment variable is set
846 #
847
848 if (!defined($ENV{HOME})) {
849 die "$prog: The HOME environment variable is not set.\n";
850 }
DRC190854c2009-03-26 18:13:00 +0000851# chdir($ENV{HOME});
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000852
853 #
854 # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
855 # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
856 # we just guess at the values. If you find perl moaning here, just
857 # hard-code the values of AF_INET and SOCK_STREAM. You can find these out
858 # for your platform by looking in /usr/include/sys/socket.h and related
859 # files.
860 #
861
862 chop($os = `uname`);
863 chop($osrev = `uname -r`);
864
865 eval 'use Socket';
866 if ($@) {
867 eval 'require "sys/socket.ph"';
868 if ($@) {
869 if (($os eq "SunOS") && ($osrev !~ /^4/)) {
870 $AF_INET = 2;
871 $SOCK_STREAM = 2;
872 } else {
873 $AF_INET = 2;
874 $SOCK_STREAM = 1;
875 }
876 } else {
877 $AF_INET = &AF_INET;
878 $SOCK_STREAM = &SOCK_STREAM;
879 }
880 } else {
881 $AF_INET = &AF_INET;
882 $SOCK_STREAM = &SOCK_STREAM;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000883 }
884}