blob: bb95506db9ffd251591f9170b8428c4f2446ba09 [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
Pierre Ossman5fe60702015-12-29 14:27:07 +0100241# Make an X server cookie and set up the Xauthority file
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000242
Pierre Ossman5fe60702015-12-29 14:27:07 +0100243$cookie = `mcookie`;
Adam Tkac6cbd9d12009-11-12 10:39:54 +0000244
Pierre Ossmancddd6482015-12-29 14:30:32 +0100245open(XAUTH, "|xauth -f $xauthorityFile source -");
246print XAUTH "add $host:$displayNumber . $cookie\n";
247print XAUTH "add $host/unix:$displayNumber . $cookie\n";
248close(XAUTH);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000249
250if ($opt{'-name'}) {
251 $desktopName = $opt{'-name'};
252} else {
253 $desktopName = "$host:$displayNumber ($ENV{USER})";
254}
255
256# Now start the X VNC Server
257
DRC190854c2009-03-26 18:13:00 +0000258$cmd = $exedir."Xvnc :$displayNumber";
grayskyddff8d02015-10-19 08:24:14 -0400259
260my %default_opts;
261my %config;
262
263$default_opts{desktop} = &quotedString($desktopName);
264$default_opts{httpd} = $vncJavaFiles if ($vncJavaFiles);
265$default_opts{auth} = $xauthorityFile;
266$default_opts{geometry} = $geometry if ($geometry);
267$default_opts{depth} = $depth if ($depth);
268$default_opts{pixelformat} = $pixelformat if ($pixelformat);
269$default_opts{rfbwait} = 30000;
270$default_opts{rfbauth} = "$vncUserDir/passwd";
271$default_opts{rfbport} = $vncPort;
272$default_opts{fp} = $fontPath if ($fontPath);
273$default_opts{pn} = "";
274
275# if a user configuration file already exists
276if(stat("$vncUserDir/config")) {
277
278 # loads and parses configuration file
279 if(open(IN, "$vncUserDir/config")) {
280 while(<IN>) {
281 next if /^#/;
282 if(my ($k, $v) = /^\s*(\w+)\s*=\s*(.+)$/) {
283 $config{$k} = $v;
284 } elsif ($_ =~ m/^\s*(\S+)/) {
285 $config{$1} = $k;
286 }
287 }
288 close(IN);
289 }
290}
291
292foreach my $k (sort keys %config) {
293 $cmd .= " -$k $config{$k}";
294 # user's option takes precedence
295 delete $default_opts{$k};
296}
297
298foreach my $k (sort keys %default_opts) {
299 $cmd .= " -$k $default_opts{$k}";
300}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000301
DRCd6821bf2009-03-26 18:17:49 +0000302# Add color database stuff here, e.g.:
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000303#
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000304# $cmd .= " -co /usr/lib/X11/rgb";
305#
306
307foreach $arg (@ARGV) {
308 $cmd .= " " . &quotedString($arg);
309}
310$cmd .= " >> " . &quotedString($desktopLog) . " 2>&1";
311
312# Run $cmd and record the process ID.
313
314$pidFile = "$vncUserDir/$host:$displayNumber.pid";
315system("$cmd & echo \$! >$pidFile");
316
317# Give Xvnc a chance to start up
318
319sleep(3);
DRCd28792b2010-01-11 20:53:00 +0000320if ($fontPath ne $defFontPath) {
321 unless (kill 0, `cat $pidFile`) {
322 if ($fpArgSpecified) {
323 warn "\nWARNING: The first attempt to start Xvnc failed, probably because the font\n";
324 warn "path you specified using the -fp argument is incorrect. Attempting to\n";
325 warn "determine an appropriate font path for this system and restart Xvnc using\n";
326 warn "that font path ...\n";
327 } else {
328 warn "\nWARNING: The first attempt to start Xvnc failed, possibly because the font\n";
329 warn "catalog is not properly configured. Attempting to determine an appropriate\n";
330 warn "font path for this system and restart Xvnc using that font path ...\n";
331 }
332 $cmd =~ s@-fp [^ ]+@@;
333 $cmd .= " -fp $defFontPath" if ($defFontPath);
334 system("$cmd & echo \$! >$pidFile");
335 sleep(3);
DRC36546c12009-04-15 06:47:23 +0000336 }
DRCd6821bf2009-03-26 18:17:49 +0000337}
338unless (kill 0, `cat $pidFile`) {
339 warn "Could not start Xvnc.\n\n";
Michal Srbe6e11f92015-10-02 02:28:26 +0300340 unlink $pidFile;
DRCd6821bf2009-03-26 18:17:49 +0000341 open(LOG, "<$desktopLog");
342 while (<LOG>) { print; }
343 close(LOG);
344 die "\n";
345}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000346
347warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
348
349# Create the user's xstartup script if necessary.
350
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200351if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100352 if (!(-e "$xstartup")) {
353 warn "Creating default startup script $xstartup\n";
354 open(XSTARTUP, ">$xstartup");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200355 print XSTARTUP $defaultXStartup;
356 close(XSTARTUP);
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100357 chmod 0755, "$xstartup";
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200358 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000359}
360
grayskyddff8d02015-10-19 08:24:14 -0400361# Create the user's config file if necessary.
362
363if (!(-e "$vncUserDir/config")) {
364 warn "Creating default config $vncUserDir/config\n";
365 open(XSTARTUP, ">$vncUserDir/config");
366 print XSTARTUP $defaultConfig;
367 close(XSTARTUP);
368 chmod 0644, "$vncUserDir/config";
369}
370
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000371# Run the X startup script.
372
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200373if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100374 warn "Starting applications specified in $xstartup\n";
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200375}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000376warn "Log file is $desktopLog\n\n";
377
378# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
379# TCP (DISPLAY=host:n)
380
381if (-e "/tmp/.X11-unix/X$displayNumber" ||
382 -e "/usr/spool/sockets/X11/$displayNumber")
383{
384 $ENV{DISPLAY}= ":$displayNumber";
385} else {
386 $ENV{DISPLAY}= "$host:$displayNumber";
387}
388$ENV{VNCDESKTOP}= $desktopName;
389
Pierre Ossmana5b37c02015-07-30 11:04:02 +0200390system($exedir."vncconfig -nowin >> " . &quotedString($desktopLog) . " 2>&1 &");
DRCf6b58402011-10-05 21:28:03 +0000391
DRC8fb11912011-03-03 10:42:14 +0000392if ($opt{'-fg'}) {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200393 if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100394 system("$xstartup >> " . &quotedString($desktopLog) . " 2>&1");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200395 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000396 if (kill 0, `cat $pidFile`) {
397 $opt{'-kill'} = ':'.$displayNumber;
398 &Kill();
399 }
DRC8fb11912011-03-03 10:42:14 +0000400} else {
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000401 if ($opt{'-autokill'}) {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200402 if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100403 system("($xstartup; $0 -kill :$displayNumber) >> "
404 . &quotedString($desktopLog) . " 2>&1 &");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200405 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000406 } else {
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200407 if (! $skipxstartup) {
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100408 system("$xstartup >> " . &quotedString($desktopLog)
409 . " 2>&1 &");
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200410 }
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000411 }
DRC8fb11912011-03-03 10:42:14 +0000412}
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000413
414exit;
415
416
417###############################################################################
418#
419# CheckGeometryAndDepth simply makes sure that the geometry and depth values
420# are sensible.
421#
422
423sub CheckGeometryAndDepth
424{
425 if ($geometry =~ /^(\d+)x(\d+)$/) {
426 $width = $1; $height = $2;
427
428 if (($width<1) || ($height<1)) {
429 die "$prog: geometry $geometry is invalid\n";
430 }
431
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000432 $geometry = "${width}x$height";
433 } else {
434 die "$prog: geometry $geometry is invalid\n";
435 }
436
DRCe5b4f752009-03-26 18:23:29 +0000437 if ($depth && (($depth < 8) || ($depth > 32))) {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000438 die "Depth must be between 8 and 32\n";
439 }
440}
441
442
443#
444# GetDisplayNumber gets the lowest available display number. A display number
445# n is taken if something is listening on the VNC server port (5900+n) or the
446# X server port (6000+n).
447#
448
449sub GetDisplayNumber
450{
451 foreach $n (1..99) {
452 if (&CheckDisplayNumber($n)) {
453 return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
454 }
455 }
456
457 die "$prog: no free display number on $host.\n";
458}
459
460
461#
462# CheckDisplayNumber checks if the given display number is available. A
463# display number n is taken if something is listening on the VNC server port
464# (5900+n) or the X server port (6000+n).
465#
466
467sub CheckDisplayNumber
468{
469 local ($n) = @_;
470
471 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
472 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
473 if (!bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
474 close(S);
475 return 0;
476 }
477 close(S);
478
479 socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
480 eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
481 if (!bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
482 close(S);
483 return 0;
484 }
485 close(S);
486
487 if (-e "/tmp/.X$n-lock") {
488 warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
489 warn "Remove this file if there is no X server $host:$n\n";
490 return 0;
491 }
492
493 if (-e "/tmp/.X11-unix/X$n") {
494 warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
495 warn "Remove this file if there is no X server $host:$n\n";
496 return 0;
497 }
498
499 if (-e "/usr/spool/sockets/X11/$n") {
500 warn("\nWarning: $host:$n is taken because of ".
501 "/usr/spool/sockets/X11/$n\n");
502 warn "Remove this file if there is no X server $host:$n\n";
503 return 0;
504 }
505
506 return 1;
507}
508
509
510#
511# GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
512# format of the current X display being used. If successful, it sets the
513# options as appropriate so that the X VNC server will use the same settings
514# (minus an allowance for window manager decorations on the geometry). Using
515# the same depth and pixel format means that the VNC server won't have to
516# translate pixels when the desktop is being viewed on this X display (for
517# TrueColor displays anyway).
518#
519
520sub GetXDisplayDefaults
521{
522 local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
523 $red, $green, $blue);
524
525 $wmDecorationWidth = 4; # a guess at typical size for window manager
526 $wmDecorationHeight = 24; # decoration size
527
528 return if (!defined($ENV{DISPLAY}));
529
530 @lines = `xdpyinfo 2>/dev/null`;
531
532 return if ($? != 0);
533
534 @matchlines = grep(/dimensions/, @lines);
535 if (@matchlines) {
536 ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
537
538 $width -= $wmDecorationWidth;
539 $height -= $wmDecorationHeight;
540
541 $geometry = "${width}x$height";
542 }
543
544 @matchlines = grep(/default visual id/, @lines);
545 if (@matchlines) {
546 ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
547
548 for ($i = 0; $i < @lines; $i++) {
549 if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
550 if (($lines[$i+1] !~ /TrueColor/) ||
551 ($lines[$i+2] !~ /depth/) ||
552 ($lines[$i+4] !~ /red, green, blue masks/))
553 {
554 return;
555 }
556 last;
557 }
558 }
559
560 return if ($i >= @lines);
561
562 ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
563 ($red,$green,$blue)
564 = ($lines[$i+4]
565 =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
566
567 $red = hex($red);
568 $green = hex($green);
569 $blue = hex($blue);
570
571 if ($red > $blue) {
572 $red = int(log($red) / log(2)) - int(log($green) / log(2));
573 $green = int(log($green) / log(2)) - int(log($blue) / log(2));
574 $blue = int(log($blue) / log(2)) + 1;
575 $pixelformat = "rgb$red$green$blue";
576 } else {
577 $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
578 $green = int(log($green) / log(2)) - int(log($red) / log(2));
579 $red = int(log($red) / log(2)) + 1;
580 $pixelformat = "bgr$blue$green$red";
581 }
582 }
583}
584
585
586#
587# quotedString returns a string which yields the original string when parsed
588# by a shell.
589#
590
591sub quotedString
592{
593 local ($in) = @_;
594
595 $in =~ s/\'/\'\"\'\"\'/g;
596
597 return "'$in'";
598}
599
600
601#
602# removeSlashes turns slashes into underscores for use as a file name.
603#
604
605sub removeSlashes
606{
607 local ($in) = @_;
608
609 $in =~ s|/|_|g;
610
611 return "$in";
612}
613
614
615#
616# Usage
617#
618
619sub Usage
620{
621 die("\nusage: $prog [:<number>] [-name <desktop-name>] [-depth <depth>]\n".
622 " [-geometry <width>x<height>]\n".
623 " [-pixelformat rgbNNN|bgrNNN]\n".
DRCeed5d1f2009-03-26 19:16:19 +0000624 " [-fp <font-path>]\n".
DRC8fb11912011-03-03 10:42:14 +0000625 " [-fg]\n".
Adam Tkac38ba8cf2011-04-27 11:28:09 +0000626 " [-autokill]\n".
Llorenç Garcia Martinez861cb062015-10-23 13:37:42 +0200627 " [-noxstartup]\n".
Llorenç Garcia Martineze76c2fb2015-10-30 11:07:40 +0100628 " [-xstartup <file>]\n".
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000629 " <Xvnc-options>...\n\n".
DRCb9d8e762011-02-09 08:24:58 +0000630 " $prog -kill <X-display>\n\n".
631 " $prog -list\n\n");
632}
633
634
635#
636# List
637#
638
639sub List
640{
641 opendir(dir, $vncUserDir);
642 my @filelist = readdir(dir);
643 closedir(dir);
DRC075d9fa2011-02-10 04:19:46 +0000644 print "\nTigerVNC server sessions:\n\n";
DRCb9d8e762011-02-09 08:24:58 +0000645 print "X DISPLAY #\tPROCESS ID\n";
646 foreach my $file (@filelist) {
647 if ($file =~ /$host:(\d+)$\.pid/) {
Michal Srbe6e11f92015-10-02 02:28:26 +0300648 chop($tmp_pid = `cat $vncUserDir/$file`);
649 if (kill 0, $tmp_pid) {
650 print ":".$1."\t\t".`cat $vncUserDir/$file`;
651 } else {
652 unlink ($vncUserDir . "/" . $file);
653 }
DRCb9d8e762011-02-09 08:24:58 +0000654 }
655 }
656 exit 1;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000657}
658
659
660#
661# Kill
662#
663
664sub Kill
665{
666 $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
667
668 if ($opt{'-kill'} =~ /^:\d+$/) {
669 $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
670 } else {
671 if ($opt{'-kill'} !~ /^$host:/) {
672 die "\nCan't tell if $opt{'-kill'} is on $host\n".
673 "Use -kill :<number> instead\n\n";
674 }
675 $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
676 }
677
678 if (! -r $pidFile) {
679 die "\nCan't find file $pidFile\n".
680 "You'll have to kill the Xvnc process manually\n\n";
681 }
682
683 $SIG{'HUP'} = 'IGNORE';
684 chop($pid = `cat $pidFile`);
685 warn "Killing Xvnc process ID $pid\n";
DRCb9d8e762011-02-09 08:24:58 +0000686
687 if (kill 0, $pid) {
688 system("kill $pid");
689 sleep(1);
690 if (kill 0, $pid) {
691 print "Xvnc seems to be deadlocked. Kill the process manually and then re-run\n";
692 print " ".$0." -kill ".$opt{'-kill'}."\n";
693 print "to clean up the socket files.\n";
694 exit
695 }
696
697 } else {
698 warn "Xvnc process ID $pid already killed\n";
699 $opt{'-kill'} =~ s/://;
700
701 if (-e "/tmp/.X11-unix/X$opt{'-kill'}") {
702 print "Xvnc did not appear to shut down cleanly.";
703 print " Removing /tmp/.X11-unix/X$opt{'-kill'}\n";
704 unlink "/tmp/.X11-unix/X$opt{'-kill'}";
705 }
706 if (-e "/tmp/.X$opt{'-kill'}-lock") {
707 print "Xvnc did not appear to shut down cleanly.";
708 print " Removing /tmp/.X$opt{'-kill'}-lock\n";
709 unlink "/tmp/.X$opt{'-kill'}-lock";
710 }
711 }
712
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000713 unlink $pidFile;
714 exit;
715}
716
717
718#
719# ParseOptions takes a list of possible options and a boolean indicating
720# whether the option has a value following, and sets up an associative array
721# %opt of the values of the options given on the command line. It removes all
722# the arguments it uses from @ARGV and returns them in @optArgs.
723#
724
725sub ParseOptions
726{
727 local (@optval) = @_;
728 local ($opt, @opts, %valFollows, @newargs);
729
730 while (@optval) {
731 $opt = shift(@optval);
732 push(@opts,$opt);
733 $valFollows{$opt} = shift(@optval);
734 }
735
736 @optArgs = ();
737 %opt = ();
738
739 arg: while (defined($arg = shift(@ARGV))) {
740 foreach $opt (@opts) {
741 if ($arg eq $opt) {
742 push(@optArgs, $arg);
743 if ($valFollows{$opt}) {
744 if (@ARGV == 0) {
745 &Usage();
746 }
747 $opt{$opt} = shift(@ARGV);
748 push(@optArgs, $opt{$opt});
749 } else {
750 $opt{$opt} = 1;
751 }
752 next arg;
753 }
754 }
755 push(@newargs,$arg);
756 }
757
758 @ARGV = @newargs;
759}
760
761
762#
763# Routine to make sure we're operating in a sane environment.
764#
765
766sub SanityCheck
767{
768 local ($cmd);
769
770 #
771 # Get the program name
772 #
773
774 ($prog) = ($0 =~ m|([^/]+)$|);
775
776 #
777 # Check we have all the commands we'll need on the path.
778 #
779
780 cmd:
Pierre Ossman5fe60702015-12-29 14:27:07 +0100781 foreach $cmd ("uname","mcookie","xauth") {
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000782 for (split(/:/,$ENV{PATH})) {
783 if (-x "$_/$cmd") {
784 next cmd;
785 }
786 }
787 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
788 }
DRC190854c2009-03-26 18:13:00 +0000789
790 if($exedir eq "") {
791 cmd2:
792 foreach $cmd ("Xvnc","vncpasswd") {
793 for (split(/:/,$ENV{PATH})) {
794 if (-x "$_/$cmd") {
795 $vncClasses = "$_/../vnc/classes";
796 next cmd2;
797 }
798 }
799 die "$prog: couldn't find \"$cmd\" on your PATH.\n";
800 }
801 }
802 else {
803 cmd3:
804 foreach $cmd ($exedir."Xvnc",$exedir."vncpasswd") {
805 for (split(/:/,$ENV{PATH})) {
806 if (-x "$cmd") {
807 $vncClasses = $exedir."../vnc/classes";
808 next cmd3;
809 }
810 }
811 die "$prog: couldn't find \"$cmd\".\n";
812 }
813 }
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000814
815 #
816 # Check the HOME environment variable is set
817 #
818
819 if (!defined($ENV{HOME})) {
820 die "$prog: The HOME environment variable is not set.\n";
821 }
DRC190854c2009-03-26 18:13:00 +0000822# chdir($ENV{HOME});
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000823
824 #
825 # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
826 # eval, and if it fails we try 'require "sys/socket.ph"'. If this fails,
827 # we just guess at the values. If you find perl moaning here, just
828 # hard-code the values of AF_INET and SOCK_STREAM. You can find these out
829 # for your platform by looking in /usr/include/sys/socket.h and related
830 # files.
831 #
832
833 chop($os = `uname`);
834 chop($osrev = `uname -r`);
835
836 eval 'use Socket';
837 if ($@) {
838 eval 'require "sys/socket.ph"';
839 if ($@) {
840 if (($os eq "SunOS") && ($osrev !~ /^4/)) {
841 $AF_INET = 2;
842 $SOCK_STREAM = 2;
843 } else {
844 $AF_INET = 2;
845 $SOCK_STREAM = 1;
846 }
847 } else {
848 $AF_INET = &AF_INET;
849 $SOCK_STREAM = &SOCK_STREAM;
850 }
851 } else {
852 $AF_INET = &AF_INET;
853 $SOCK_STREAM = &SOCK_STREAM;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000854 }
855}