blob: a4d6ad939da68cb37cc40ac23d7d28fa5038826d [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
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000041&SanityCheck();
42
43#
44# Global variables. You may want to configure some of these for your site.
45#
46
47$geometry = "1024x768";
DRC9d1c1572009-03-26 18:18:51 +000048#$depth = 16;
DRCe85eba52011-10-04 06:57:19 +000049$vncJavaFiles = (((-d "$vncClasses") && "$vncClasses") ||
50 ((-d "/usr/share/vnc/classes") && "/usr/share/vnc/classes") ||
51 ((-d "/usr/local/vnc/classes") && "/usr/local/vnc/classes"));
52
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000053$vncUserDir = "$ENV{HOME}/.vnc";
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +020054$skipxstartup = 0;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000055$xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority";
56
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +010057$xstartup = $vncUserDir . "/xstartup";
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000058$defaultXStartup
59 = ("#!/bin/sh\n\n".
DRC93248982009-03-26 18:14:38 +000060 "unset SESSION_MANAGER\n".
Adam Tkacc071e492009-05-20 09:01:24 +000061 "unset DBUS_SESSION_BUS_ADDRESS\n".
DRC93248982009-03-26 18:14:38 +000062 "OS=`uname -s`\n".
63 "if [ \$OS = 'Linux' ]; then\n".
64 " case \"\$WINDOWMANAGER\" in\n".
65 " \*gnome\*)\n".
66 " if [ -e /etc/SuSE-release ]; then\n".
67 " PATH=\$PATH:/opt/gnome/bin\n".
68 " export PATH\n".
69 " fi\n".
70 " ;;\n".
71 " esac\n".
72 "fi\n".
73 "if [ -x /etc/X11/xinit/xinitrc ]; then\n".
74 " exec /etc/X11/xinit/xinitrc\n".
75 "fi\n".
76 "if [ -f /etc/X11/xinit/xinitrc ]; then\n".
77 " exec sh /etc/X11/xinit/xinitrc\n".
78 "fi\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000079 "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n".
80 "xsetroot -solid grey\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000081 "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
82 "twm &\n");
83
grayskyddff8d02015-10-19 08:24:14 -040084$defaultConfig
85 = ("## Supported server options to pass to vncserver upon invocation can be listed\n".
86 "## in this file. See the following manpages for more: vncserver(1) Xvnc(1).\n".
87 "## Several common ones are shown below. Uncomment and modify to your liking.\n".
88 "##\n".
89 "# securitytypes=vncauth,tlsvnc\n".
90 "# desktop=sandbox\n".
91 "# geometry=2000x1200\n".
92 "# localhost\n".
93 "# alwaysshared\n");
94
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000095chop($host = `uname -n`);
96
DRC989dbd12009-04-22 13:23:17 +000097if (-d "/etc/X11/fontpath.d") {
98 $fontPath = "catalogue:/etc/X11/fontpath.d";
99}
DRC36546c12009-04-15 06:47:23 +0000100
DRCd6821bf2009-03-26 18:17:49 +0000101@fontpaths = ('/usr/share/X11/fonts', '/usr/share/fonts', '/usr/share/fonts/X11/');
102if (! -l "/usr/lib/X11") {push(@fontpaths, '/usr/lib/X11/fonts');}
103if (! -l "/usr/X11") {push(@fontpaths, '/usr/X11/lib/X11/fonts');}
104if (! -l "/usr/X11R6") {push(@fontpaths, '/usr/X11R6/lib/X11/fonts');}
105push(@fontpaths, '/usr/share/fonts/default');
106
107@fonttypes = ('misc',
108 '75dpi',
109 '100dpi',
110 'Speedo',
111 'Type1');
112
113foreach $_fpath (@fontpaths) {
114 foreach $_ftype (@fonttypes) {
115 if (-f "$_fpath/$_ftype/fonts.dir") {
116 if (! -l "$_fpath/$_ftype") {
DRC36546c12009-04-15 06:47:23 +0000117 $defFontPath .= "$_fpath/$_ftype,";
DRCd6821bf2009-03-26 18:17:49 +0000118 }
119 }
120 }
121}
DRCd28792b2010-01-11 20:53:00 +0000122
DRC36546c12009-04-15 06:47:23 +0000123if ($defFontPath) {
124 if (substr($defFontPath, -1, 1) == ',') {
125 chop $defFontPath;
DRCd6821bf2009-03-26 18:17:49 +0000126 }
127}
128
DRCd28792b2010-01-11 20:53:00 +0000129if ($fontPath eq "") {
130 $fontPath = $defFontPath;
131}
132
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000133# Check command line options
134
135&ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100136 "-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 +0000137
138&Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
139
140&Kill() if ($opt{'-kill'});
141
DRCb9d8e762011-02-09 08:24:58 +0000142&List() if ($opt{'-list'});
143
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000144# Uncomment this line if you want default geometry, depth and pixelformat
145# to match the current X display:
146# &GetXDisplayDefaults();
147
148if ($opt{'-geometry'}) {
149 $geometry = $opt{'-geometry'};
150}
151if ($opt{'-depth'}) {
152 $depth = $opt{'-depth'};
153 $pixelformat = "";
154}
155if ($opt{'-pixelformat'}) {
156 $pixelformat = $opt{'-pixelformat'};
157}
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200158if ($opt{'-noxstartup'}) {
159 $skipxstartup = 1;
160}
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100161if ($opt{'-xstartup'}) {
162 $xstartup = $opt{'-xstartup'};
163}
DRCeed5d1f2009-03-26 19:16:19 +0000164if ($opt{'-fp'}) {
165 $fontPath = $opt{'-fp'};
DRC36546c12009-04-15 06:47:23 +0000166 $fpArgSpecified = 1;
DRCeed5d1f2009-03-26 19:16:19 +0000167}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000168
169&CheckGeometryAndDepth();
170
171
172# Create the user's vnc directory if necessary.
173
174if (!(-e $vncUserDir)) {
175 if (!mkdir($vncUserDir,0755)) {
176 die "$prog: Could not create $vncUserDir.\n";
177 }
178}
179
Adam Tkacf586b842011-04-27 11:20:18 +0000180# Check whether VNC authentication is enabled, and if so, prompt the user to
181# create a VNC password if they don't already have one.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000182
Adam Tkacf586b842011-04-27 11:20:18 +0000183$securityTypeArgSpecified = 0;
184$vncAuthEnabled = 0;
185$passwordArgSpecified = 0;
186
187for ($i = 0; $i < @ARGV; ++$i) {
188 # -SecurityTypes can be followed by a space or "="
189 my @splitargs = split('=', $ARGV[$i]);
190 if (@splitargs <= 1 && $i < @ARGV - 1) {
191 push(@splitargs, $ARGV[$i + 1]);
192 }
193 if (lc(@splitargs[0]) eq "-securitytypes") {
194 if (@splitargs > 1) {
195 $securityTypeArgSpecified = 1;
196 }
197 foreach $arg2 (split(',', @splitargs[1])) {
198 if (lc($arg2) eq "vncauth" || lc($arg2) eq "tlsvnc"
199 || lc($arg2) eq "x509vnc") {
200 $vncAuthEnabled = 1;
201 }
202 }
203 }
204 if ((lc(@splitargs[0]) eq "-password")
205 || (lc(@splitargs[0]) eq "-passwordfile"
206 || (lc(@splitargs[0]) eq "-rfbauth"))) {
207 $passwordArgSpecified = 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000208 }
209}
210
Adam Tkacf586b842011-04-27 11:20:18 +0000211if ((!$securityTypeArgSpecified || $vncAuthEnabled) && !$passwordArgSpecified) {
212 ($z,$z,$mode) = stat("$vncUserDir/passwd");
213 if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
214 warn "\nYou will require a password to access your desktops.\n\n";
215 system($exedir."vncpasswd -q $vncUserDir/passwd");
216 if (($? >> 8) != 0) {
217 exit 1;
218 }
219 }
220}
221
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000222# Find display number.
223
224if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
225 $displayNumber = $1;
226 shift(@ARGV);
227 if (!&CheckDisplayNumber($displayNumber)) {
228 die "A VNC server is already running as :$displayNumber\n";
229 }
Adam Tkac39c9d992010-07-21 14:08:38 +0000230} elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/) && ($ARGV[0] !~ /^\+/)) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000231 &Usage();
232} else {
233 $displayNumber = &GetDisplayNumber();
234}
235
236$vncPort = 5900 + $displayNumber;
237
238$desktopLog = "$vncUserDir/$host:$displayNumber.log";
239unlink($desktopLog);
240
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000241# Make an X server cookie - use /dev/urandom on systems that have it,
242# otherwise use perl's random number generator, seeded with the sum
243# of the current time, our PID and part of the encrypted form of the password.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000244
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000245my $cookie = "";
246if (open(URANDOM, '<', '/dev/urandom')) {
247 my $randata;
248 if (sysread(URANDOM, $randata, 16) == 16) {
249 $cookie = unpack 'h*', $randata;
250 }
251 close(URANDOM);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000252}
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000253if ($cookie eq "") {
254 srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
255 for (1..16) {
256 $cookie .= sprintf("%02x", int(rand(256)) % 256);
257 }
258}
259
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000260system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
261system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
262
263if ($opt{'-name'}) {
264 $desktopName = $opt{'-name'};
265} else {
266 $desktopName = "$host:$displayNumber ($ENV{USER})";
267}
268
269# Now start the X VNC Server
270
DRC190854c2009-03-26 18:13:00 +0000271$cmd = $exedir."Xvnc :$displayNumber";
grayskyddff8d02015-10-19 08:24:14 -0400272
273my %default_opts;
274my %config;
275
276$default_opts{desktop} = &quotedString($desktopName);
277$default_opts{httpd} = $vncJavaFiles if ($vncJavaFiles);
278$default_opts{auth} = $xauthorityFile;
279$default_opts{geometry} = $geometry if ($geometry);
280$default_opts{depth} = $depth if ($depth);
281$default_opts{pixelformat} = $pixelformat if ($pixelformat);
282$default_opts{rfbwait} = 30000;
283$default_opts{rfbauth} = "$vncUserDir/passwd";
284$default_opts{rfbport} = $vncPort;
285$default_opts{fp} = $fontPath if ($fontPath);
286$default_opts{pn} = "";
287
288# if a user configuration file already exists
289if(stat("$vncUserDir/config")) {
290
291 # loads and parses configuration file
292 if(open(IN, "$vncUserDir/config")) {
293 while(<IN>) {
294 next if /^#/;
295 if(my ($k, $v) = /^\s*(\w+)\s*=\s*(.+)$/) {
296 $config{$k} = $v;
297 } elsif ($_ =~ m/^\s*(\S+)/) {
298 $config{$1} = $k;
299 }
300 }
301 close(IN);
302 }
303}
304
305foreach my $k (sort keys %config) {
306 $cmd .= " -$k $config{$k}";
307 # user's option takes precedence
308 delete $default_opts{$k};
309}
310
311foreach my $k (sort keys %default_opts) {
312 $cmd .= " -$k $default_opts{$k}";
313}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000314
DRCd6821bf2009-03-26 18:17:49 +0000315# Add color database stuff here, e.g.:
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000316#
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000317# $cmd .= " -co /usr/lib/X11/rgb";
318#
319
320foreach $arg (@ARGV) {
321 $cmd .= " " . &quotedString($arg);
322}
323$cmd .= " >> " . &quotedString($desktopLog) . " 2>&1";
324
325# Run $cmd and record the process ID.
326
327$pidFile = "$vncUserDir/$host:$displayNumber.pid";
328system("$cmd & echo \$! >$pidFile");
329
330# Give Xvnc a chance to start up
331
332sleep(3);
DRCd28792b2010-01-11 20:53:00 +0000333if ($fontPath ne $defFontPath) {
334 unless (kill 0, `cat $pidFile`) {
335 if ($fpArgSpecified) {
336 warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n";
337 warn "path you specified using the -fp argument is incorrect. Attempting to\n";
338 warn "determine an appropriate font path for this system and restart Xvnc using\n";
339 warn "that font path ...\n";
340 } else {
341 warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the font\n";
342 warn "catalog is not properly configured. Attempting to determine an appropriate\n";
343 warn "font path for this system and restart Xvnc using that font path ...\n";
344 }
345 $cmd =~ s@-fp [^ ]+@@;
346 $cmd .= " -fp $defFontPath" if ($defFontPath);
347 system("$cmd & echo \$! >$pidFile");
348 sleep(3);
DRC36546c12009-04-15 06:47:23 +0000349 }
DRCd6821bf2009-03-26 18:17:49 +0000350}
351unless (kill 0, `cat $pidFile`) {
352 warn "Could not start Xvnc.\n\n";
Michal Srbe6e11f92015-10-02 02:28:26 +0300353 unlink $pidFile;
DRCd6821bf2009-03-26 18:17:49 +0000354 open(LOG, "<$desktopLog");
355 while (<LOG>) { print; }
356 close(LOG);
357 die "\n";
358}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000359
360warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
361
362# Create the user's xstartup script if necessary.
363
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200364if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100365 if (!(-e "$xstartup")) {
366 warn "Creating default startup script $xstartup\n";
367 open(XSTARTUP, ">$xstartup");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200368 print XSTARTUP $defaultXStartup;
369 close(XSTARTUP);
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100370 chmod 0755, "$xstartup";
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200371 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000372}
373
grayskyddff8d02015-10-19 08:24:14 -0400374# Create the user's config file if necessary.
375
376if (!(-e "$vncUserDir/config")) {
377 warn "Creating default config $vncUserDir/config\n";
378 open(XSTARTUP, ">$vncUserDir/config");
379 print XSTARTUP $defaultConfig;
380 close(XSTARTUP);
381 chmod 0644, "$vncUserDir/config";
382}
383
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000384# Run the X startup script.
385
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200386if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100387 warn "Starting applications specified in $xstartup\n";
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200388}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000389warn "Log file is $desktopLog\n\n";
390
391# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
392# TCP (DISPLAY=host:n)
393
394if (-e "/tmp/.X11-unix/X$displayNumber" ||
395 -e "/usr/spool/sockets/X11/$displayNumber")
396{
397 $ENV{DISPLAY}= ":$displayNumber";
398} else {
399 $ENV{DISPLAY}= "$host:$displayNumber";
400}
401$ENV{VNCDESKTOP}= $desktopName;
402
Pierre Ossmana5b37c02015-07-30 11:04:02 +0200403system($exedir."vncconfig -nowin >> " . &quotedString($desktopLog) . " 2>&1 &");
DRCf6b58402011-10-05 21:28:03 +0000404
DRC8fb11912011-03-03 10:42:14 +0000405if ($opt{'-fg'}) {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200406 if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100407 system("$xstartup >> " . &quotedString($desktopLog) . " 2>&1");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200408 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000409 if (kill 0, `cat $pidFile`) {
410 $opt{'-kill'} = ':'.$displayNumber;
411 &Kill();
412 }
DRC8fb11912011-03-03 10:42:14 +0000413} else {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000414 if ($opt{'-autokill'}) {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200415 if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100416 system("($xstartup; $0 -kill :$displayNumber) >> "
417 . &quotedString($desktopLog) . " 2>&1 &");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200418 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000419 } else {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200420 if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100421 system("$xstartup >> " . &quotedString($desktopLog)
422 . " 2>&1 &");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200423 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000424 }
DRC8fb11912011-03-03 10:42:14 +0000425}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000426
427exit;
428
429
430###############################################################################
431#
432# CheckGeometryAndDepth simply makes sure that the geometry and depth values
433# are sensible.
434#
435
436sub CheckGeometryAndDepth
437{
438 if ($geometry =~ /^(\d+)x(\d+)$/) {
439 $width = $1; $height = $2;
440
441 if (($width<1) || ($height<1)) {
442 die "$prog: geometry $geometry is invalid\n";
443 }
444
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000445 $geometry = "${width}x$height";
446 } else {
447 die "$prog: geometry $geometry is invalid\n";
448 }
449
DRCe5b4f752009-03-26 18:23:29 +0000450 if ($depth && (($depth < 8) || ($depth > 32))) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000451 die "Depth must be between 8 and 32\n";
452 }
453}
454
455
456#
457# GetDisplayNumber gets the lowest available display number. A display number
458# n is taken if something is listening on the VNC server port (5900+n) or the
459# X server port (6000+n).
460#
461
462sub GetDisplayNumber
463{
464 foreach $n (1..99) {
465 if (&CheckDisplayNumber($n)) {
466 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
467 }
468 }
469
470 die "$prog: no free display number on $host.\n";
471}
472
473
474#
475# CheckDisplayNumber checks if the given display number is available. A
476# display number n is taken if something is listening on the VNC server port
477# (5900+n) or the X server port (6000+n).
478#
479
480sub CheckDisplayNumber
481{
482 local ($n) = @_;
483
484 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
485 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
486 if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
487 close(S);
488 return 0;
489 }
490 close(S);
491
492 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
493 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
494 if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
495 close(S);
496 return 0;
497 }
498 close(S);
499
500 if (-e "/tmp/.X$n-lock") {
501 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
502 warn "Remove this file if there is no X server $host:$n\n";
503 return 0;
504 }
505
506 if (-e "/tmp/.X11-unix/X$n") {
507 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
508 warn "Remove this file if there is no X server $host:$n\n";
509 return 0;
510 }
511
512 if (-e "/usr/spool/sockets/X11/$n") {
513 warn("\nWarning: $host:$n is taken because of ".
514 "/usr/spool/sockets/X11/$n\n");
515 warn "Remove this file if there is no X server $host:$n\n";
516 return 0;
517 }
518
519 return 1;
520}
521
522
523#
524# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
525# format of the current X display being used. If successful, it sets the
526# options as appropriate so that the X VNC server will use the same settings
527# (minus an allowance for window manager decorations on the geometry). Using
528# the same depth and pixel format means that the VNC server won't have to
529# translate pixels when the desktop is being viewed on this X display (for
530# TrueColor displays anyway).
531#
532
533sub GetXDisplayDefaults
534{
535 local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
536 $red, $green, $blue);
537
538 $wmDecorationWidth = 4; # a guess at typical size for window manager
539 $wmDecorationHeight = 24; # decoration size
540
541 return if (!defined($ENV{DISPLAY}));
542
543 @lines = `xdpyinfo 2>/dev/null`;
544
545 return if ($? != 0);
546
547 @matchlines = grep(/dimensions/, @lines);
548 if (@matchlines) {
549 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
550
551 $width -= $wmDecorationWidth;
552 $height -= $wmDecorationHeight;
553
554 $geometry = "${width}x$height";
555 }
556
557 @matchlines = grep(/default visual id/, @lines);
558 if (@matchlines) {
559 ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
560
561 for ($i = 0; $i < @lines; $i++) {
562 if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
563 if (($lines[$i+1] !~ /TrueColor/) ||
564 ($lines[$i+2] !~ /depth/) ||
565 ($lines[$i+4] !~ /red, green, blue masks/))
566 {
567 return;
568 }
569 last;
570 }
571 }
572
573 return if ($i >= @lines);
574
575 ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
576 ($red,$green,$blue)
577 = ($lines[$i+4]
578 =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
579
580 $red = hex($red);
581 $green = hex($green);
582 $blue = hex($blue);
583
584 if ($red > $blue) {
585 $red = int(log($red) / log(2)) - int(log($green) / log(2));
586 $green = int(log($green) / log(2)) - int(log($blue) / log(2));
587 $blue = int(log($blue) / log(2)) + 1;
588 $pixelformat = "rgb$red$green$blue";
589 } else {
590 $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
591 $green = int(log($green) / log(2)) - int(log($red) / log(2));
592 $red = int(log($red) / log(2)) + 1;
593 $pixelformat = "bgr$blue$green$red";
594 }
595 }
596}
597
598
599#
600# quotedString returns a string which yields the original string when parsed
601# by a shell.
602#
603
604sub quotedString
605{
606 local ($in) = @_;
607
608 $in =~ s/\'/\'\"\'\"\'/g;
609
610 return "'$in'";
611}
612
613
614#
615# removeSlashes turns slashes into underscores for use as a file name.
616#
617
618sub removeSlashes
619{
620 local ($in) = @_;
621
622 $in =~ s|/|_|g;
623
624 return "$in";
625}
626
627
628#
629# Usage
630#
631
632sub Usage
633{
634 die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
635 " [-geometry <width>x<height>]\n".
636 " [-pixelformat rgbNNN|bgrNNN]\n".
DRCeed5d1f2009-03-26 19:16:19 +0000637 " [-fp <font-path>]\n".
DRC8fb11912011-03-03 10:42:14 +0000638 " [-fg]\n".
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000639 " [-autokill]\n".
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200640 " [-noxstartup]\n".
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100641 " [-xstartup <file>]\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000642 " <Xvnc-options>...\n\n".
DRCb9d8e762011-02-09 08:24:58 +0000643 " $prog -kill <X-display>\n\n".
644 " $prog -list\n\n");
645}
646
647
648#
649# List
650#
651
652sub List
653{
654 opendir(dir, $vncUserDir);
655 my @filelist = readdir(dir);
656 closedir(dir);
DRC075d9fa2011-02-10 04:19:46 +0000657 print "\nTigerVNC server sessions:\n\n";
DRCb9d8e762011-02-09 08:24:58 +0000658 print "X DISPLAY #\tPROCESS ID\n";
659 foreach my $file (@filelist) {
660 if ($file =~ /$host:(\d+)$\.pid/) {
Michal Srbe6e11f92015-10-02 02:28:26 +0300661 chop($tmp_pid = `cat $vncUserDir/$file`);
662 if (kill 0, $tmp_pid) {
663 print ":".$1."\t\t".`cat $vncUserDir/$file`;
664 } else {
665 unlink ($vncUserDir . "/" . $file);
666 }
DRCb9d8e762011-02-09 08:24:58 +0000667 }
668 }
669 exit 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000670}
671
672
673#
674# Kill
675#
676
677sub Kill
678{
679 $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
680
681 if ($opt{'-kill'} =~ /^:\d+$/) {
682 $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
683 } else {
684 if ($opt{'-kill'} !~ /^$host:/) {
685 die "\nCan't tell if $opt{'-kill'} is on $host\n".
686 "Use -kill :<number> instead\n\n";
687 }
688 $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
689 }
690
691 if (! -r $pidFile) {
692 die "\nCan't find file $pidFile\n".
693 "You'll have to kill the Xvnc process manually\n\n";
694 }
695
696 $SIG{'HUP'} = 'IGNORE';
697 chop($pid = `cat $pidFile`);
698 warn "Killing Xvnc process ID $pid\n";
DRCb9d8e762011-02-09 08:24:58 +0000699
700 if (kill 0, $pid) {
701 system("kill $pid");
702 sleep(1);
703 if (kill 0, $pid) {
704 print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n";
705 print " ".$0." -kill ".$opt{'-kill'}."\n";
706 print "to clean up the socket files.\n";
707 exit
708 }
709
710 } else {
711 warn "Xvnc process ID $pid already killed\n";
712 $opt{'-kill'} =~ s/://;
713
714 if (-e "/tmp/.X11-unix/X$opt{'-kill'}") {
715 print "Xvnc did not appear to shut down cleanly.";
716 print " Removing /tmp/.X11-unix/X$opt{'-kill'}\n";
717 unlink "/tmp/.X11-unix/X$opt{'-kill'}";
718 }
719 if (-e "/tmp/.X$opt{'-kill'}-lock") {
720 print "Xvnc did not appear to shut down cleanly.";
721 print " Removing /tmp/.X$opt{'-kill'}-lock\n";
722 unlink "/tmp/.X$opt{'-kill'}-lock";
723 }
724 }
725
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000726 unlink $pidFile;
727 exit;
728}
729
730
731#
732# ParseOptions takes a list of possible options and a boolean indicating
733# whether the option has a value following, and sets up an associative array
734# %opt of the values of the options given on the command line. It removes all
735# the arguments it uses from @ARGV and returns them in @optArgs.
736#
737
738sub ParseOptions
739{
740 local (@optval) = @_;
741 local ($opt, @opts, %valFollows, @newargs);
742
743 while (@optval) {
744 $opt = shift(@optval);
745 push(@opts,$opt);
746 $valFollows{$opt} = shift(@optval);
747 }
748
749 @optArgs = ();
750 %opt = ();
751
752 arg: while (defined($arg = shift(@ARGV))) {
753 foreach $opt (@opts) {
754 if ($arg eq $opt) {
755 push(@optArgs, $arg);
756 if ($valFollows{$opt}) {
757 if (@ARGV == 0) {
758 &Usage();
759 }
760 $opt{$opt} = shift(@ARGV);
761 push(@optArgs, $opt{$opt});
762 } else {
763 $opt{$opt} = 1;
764 }
765 next arg;
766 }
767 }
768 push(@newargs,$arg);
769 }
770
771 @ARGV = @newargs;
772}
773
774
775#
776# Routine to make sure we're operating in a sane environment.
777#
778
779sub SanityCheck
780{
781 local ($cmd);
782
783 #
784 # Get the program name
785 #
786
787 ($prog) = ($0 =~ m|([^/]+)$|);
788
789 #
790 # Check we have all the commands we'll need on the path.
791 #
792
793 cmd:
Pierre Ossmana7ac6a92015-12-29 14:24:55 +0100794 foreach $cmd ("uname","xauth") {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000795 for (split(/:/,$ENV{PATH})) {
796 if (-x "$_/$cmd") {
797 next cmd;
798 }
799 }
800 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
801 }
DRC190854c2009-03-26 18:13:00 +0000802
803 if($exedir eq "") {
804 cmd2:
805 foreach $cmd ("Xvnc","vncpasswd") {
806 for (split(/:/,$ENV{PATH})) {
807 if (-x "$_/$cmd") {
808 $vncClasses = "$_/../vnc/classes";
809 next cmd2;
810 }
811 }
812 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
813 }
814 }
815 else {
816 cmd3:
817 foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") {
818 for (split(/:/,$ENV{PATH})) {
819 if (-x "$cmd") {
820 $vncClasses = $exedir."../vnc/classes";
821 next cmd3;
822 }
823 }
824 die "$prog: couldn't find \"$cmd\".\n";
825 }
826 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000827
828 #
829 # Check the HOME environment variable is set
830 #
831
832 if (!defined($ENV{HOME})) {
833 die "$prog: The HOME environment variable is not set.\n";
834 }
DRC190854c2009-03-26 18:13:00 +0000835# chdir($ENV{HOME});
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000836
837 #
838 # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
839 # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
840 # we just guess at the values. If you find perl moaning here, just
841 # hard-code the values of AF_INET and SOCK_STREAM. You can find these out
842 # for your platform by looking in /usr/include/sys/socket.h and related
843 # files.
844 #
845
846 chop($os = `uname`);
847 chop($osrev = `uname -r`);
848
849 eval 'use Socket';
850 if ($@) {
851 eval 'require "sys/socket.ph"';
852 if ($@) {
853 if (($os eq "SunOS") && ($osrev !~ /^4/)) {
854 $AF_INET = 2;
855 $SOCK_STREAM = 2;
856 } else {
857 $AF_INET = 2;
858 $SOCK_STREAM = 1;
859 }
860 } else {
861 $AF_INET = &AF_INET;
862 $SOCK_STREAM = &SOCK_STREAM;
863 }
864 } else {
865 $AF_INET = &AF_INET;
866 $SOCK_STREAM = &SOCK_STREAM;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000867 }
868}