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