Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 20.69 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. #  Copyright (C) 2004-2006,2008-2009 Ola Lundqvist <opal@debian.org>
  4. #  Copyright (C) 2002-2009 Constantin Kaplinsky.  All Rights Reserved.
  5. #  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
  6. #
  7. #  This is free software; you can redistribute it and/or modify
  8. #  it under the terms of the GNU General Public License as published by
  9. #  the Free Software Foundation; either version 2 of the License, or
  10. #  (at your option) any later version.
  11. #
  12. #  This software is distributed in the hope that it will be useful,
  13. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #  GNU General Public License for more details.
  16. #
  17. #  You should have received a copy of the GNU General Public License
  18. #  along with this software; if not, write to the Free Software
  19. #  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  20. #  USA.
  21. #
  22.  
  23. # This file was heavily edited by
  24. #              Ola Lundqvist <opal@debian.org>
  25. #               Marcus Brinkmann <Marcus.Brinkmann@ruhr-uni-bochum.de>
  26. # Clean option by Dirk Eddelbuettel <edd@debian.org>
  27.  
  28. # Please report all errors to Debian and not to ORL.
  29.  
  30. #
  31. # vncserver - wrapper script to start an X VNC server.
  32. #
  33.  
  34. # First make sure we're operating in a sane environment.
  35.  
  36. &SanityCheck();
  37.  
  38. # Default configuration of the TightVNC Server:
  39.  
  40. $geometry = "1024x768";
  41. $depth = 24;
  42. $desktopName = "X";
  43. if (-d "/usr/share/tightvnc-java") {
  44.     $vncClasses = "/usr/share/tightvnc-java";
  45. }
  46. $vncUserDir = "$ENV{HOME}/.vnc";
  47. #$fontPath = "unix/:7100";
  48. $authType = "-rfbauth $vncUserDir/passwd";
  49.  
  50. # Read configuration from the system-wide and user files if present.
  51.  
  52. $configFile = "/etc/tightvncserver.conf";
  53. ReadConfiguration();
  54. $configFile = "$ENV{HOME}/.vnc/tightvncserver.conf";
  55. ReadConfiguration();
  56.  
  57. # Done reading configuration.
  58.  
  59. $defaultXStartup
  60.     = ("#!/bin/sh\n\n".
  61.        "xrdb \$HOME/.Xresources\n".
  62.        "xsetroot -solid grey\n".
  63.        "#x-terminal-emulator -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
  64.        "#x-window-manager &\n".
  65.        "# Fix to make GNOME work\n".
  66.        "export XKL_XMODMAP_DISABLE=1\n".
  67.        "/etc/X11/Xsession\n");
  68.  
  69. $xauthorityFile = "$ENV{XAUTHORITY}";
  70.  
  71. ######## Adding configuration possibility ################
  72. $Config_file = "/etc/vnc.conf";
  73. &ReadConfigFile();
  74. $Config_file = "$ENV{HOME}/.vncrc";
  75. &ReadConfigFile();
  76.  
  77. if (!$XFConfigPath) {
  78.   foreach ("/etc/X11/xorg.conf", "/etc/X11/XF86Config-4", "/etc/X11/XF86Config" ){
  79.       $XFConfigPath = $_;
  80.       last if ( -e $XFConfigPath );
  81.   }
  82. }
  83. if (!$fontPath) {
  84.   &ReadXFConfigFont;
  85. }
  86. if (!$fontPath) {
  87.   $fontPath = "/usr/share/fonts/X11/misc/,".
  88.               "/usr/share/fonts/X11/Type1/,".
  89.               "/usr/share/fonts/X11/75dpi/,".
  90.               "/usr/share/fonts/X11/100dpi/"
  91. }
  92. if (!$colorPath) {
  93.   &ReadXFConfigColor;
  94. }
  95. if (!$colorPath) {
  96.   foreach ("/etc/X11/rgb", "/usr/share/X11/rgb", "/usr/X11R6/lib/X11/rgb"){
  97.       $colorPath = $_;
  98.       last if ( -e "${colorPath}.txt" );
  99.   }
  100. }
  101. ##########################################################
  102.  
  103. $vncUserDirUnderTmp = ($vncUserDir =~ m|^/tmp/.+|) ? 1 : 0;
  104. $xstartup = ($vncUserDirUnderTmp) ?
  105.   "$ENV{HOME}/.vncstartup" : "$vncUserDir/xstartup";
  106. $xstartup = $vncStartup if ($vncStartup);
  107. unless ($xauthorityFile) {
  108.     if ($vncUserDirUnderTmp) {
  109.         $xauthorityFile = "$vncUserDir/.Xauthority";
  110.     } else {
  111.         $xauthorityFile = "$ENV{HOME}/.Xauthority";
  112.     }
  113. }
  114.  
  115. chop($host = `uname -n`);
  116.  
  117. # Check command line options
  118.  
  119. &ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
  120.           "-help",0,"-h",0,"--help",0,
  121.           "-clean",0, "-fp",1,
  122.               "-alwaysshared",0, "-nevershared",0,
  123.               "-improvedhextile",1,"-framerate",1,"-comparefb",1,
  124.               "-httpport",1,"-basehttpport",1);
  125.  
  126. &Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});
  127.  
  128. &Kill() if ($opt{'-kill'});
  129.  
  130. $useClasses = 0;
  131. if (defined $vncClasses) {
  132.   if(! -d $vncClasses) {
  133.     die "VNC class files can not be found at $vncClasses.";
  134.   }
  135.   $useClasses = 1;
  136. }
  137.  
  138. # Uncomment this line if you want default geometry, depth and pixelformat
  139. # to match the current X display:
  140. # &GetXDisplayDefaults();
  141.  
  142. if ($opt{'-geometry'}) {
  143.     $geometry = $opt{'-geometry'};
  144. }
  145. if ($opt{'-depth'}) {
  146.     $depth = $opt{'-depth'};
  147.     $pixelformat = "";
  148. }
  149. if ($opt{'-pixelformat'}) {
  150.     $pixelformat = $opt{'-pixelformat'};
  151. }
  152.  
  153. if ($opt{'-fp'}) {
  154.     @fontPathElements = split(/\s*,\s*/, "$opt{'-fp'}");
  155.  
  156.     $fontPath = '';
  157.  
  158.     foreach $i (0.."$#fontPathElements") {
  159.         $tempFontPath = $fontPathElements[$i];
  160.         if ($tempFontPath !~ m!^[^/]*/[^/]*:\d+$!) {
  161.             $tempFontPath =~ s/:unscaled$//;
  162.             if (-r "$tempFontPath/fonts.dir") {
  163.                 $fontPath .= "$fontPathElements[$i],";
  164.             }
  165.         } else {
  166.             $fontPath .= "$fontPathElements[$i],";
  167.         }
  168.     }
  169.     chop $fontPath;
  170. }
  171.  
  172. &CheckGeometryAndDepth();
  173.  
  174. if ($opt{'-name'}) {
  175.     $desktopName = $opt{'-name'};
  176. }
  177.  
  178. # Compression and framerate params.
  179. # Disable compression options improvedhextile and comparefb to reduce CPU load
  180.  
  181. if (defined $opt{'-improvedhextile'}) {
  182.     $improvedhextile = $opt{'-improvedhextile'};
  183. }
  184.  
  185. if (defined $opt{'-framerate'}) {
  186.     $framerate = $opt{'-framerate'};
  187. }
  188.  
  189. if (defined $opt{'-comparefb'}) {
  190.     $comparefb = $opt{'-comparefb'};
  191. }
  192.  
  193. # Create the user's vnc directory if necessary.
  194.  
  195. unless (-e $vncUserDir) {
  196.     unless (mkdir($vncUserDir, 0700)) {
  197.         die "$prog: Could not create $vncUserDir.\n";
  198.     }
  199. }
  200. ($z,$z,$mode) = stat("$vncUserDir");
  201. if (!-d _ || !-o _ || ($vncUserDirUnderTmp && ($mode & 0777) != 0700)) {
  202.     die "$prog: Wrong type or access mode of $vncUserDir.\n";
  203. }
  204.  
  205. # Make sure the user has a password.
  206.  
  207. ($z,$z,$mode) = stat("$vncUserDir/passwd");
  208. if (-e _ && (!-f _ || !-o _)) {
  209.     die "$prog: Wrong type or ownership on $vncUserDir/passwd.\n";
  210. }
  211. if (!-e _ || ($mode & 077) != 0) {
  212.     warn "\nYou will require a password to access your desktops.\n\n";
  213.     system("vncpasswd $vncUserDir/passwd");
  214.     if (($? & 0xFF00) != 0) {
  215.         exit 1;
  216.     }
  217. }
  218.  
  219. # Find display number.
  220.  
  221. if ((@ARGV > 0) && ($ARGV[0] =~ /^:(\d+)$/)) {
  222.     $displayNumber = $1;
  223.     shift(@ARGV);
  224.     unless (&CheckDisplayNumber($displayNumber)) {
  225.     die "A VNC server is already running as :$displayNumber\n";
  226.     }
  227. } elsif ((@ARGV > 0) && ($ARGV[0] !~ /^-/)) {
  228.     &Usage();
  229. } else {
  230.     $displayNumber = &GetDisplayNumber();
  231. }
  232.  
  233. $vncPort = 5900 + $displayNumber;
  234.  
  235. $desktopLog = "$vncUserDir/$host:$displayNumber.log";
  236. unlink($desktopLog);
  237.  
  238. # Make an X server cookie - use as the seed the sum of the current time, our
  239. # PID and part of the encrypted form of the password.  Ideally we'd use
  240. # /dev/urandom, but that's only available on Linux.
  241.  
  242. srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
  243. $cookie = "";
  244. for (1..16) {
  245.     $cookie .= sprintf("%02x", int(rand(256)));
  246. }
  247.  
  248. system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
  249. system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie");
  250.  
  251. # Now start the X VNC Server
  252.  
  253. $cmd = "Xtightvnc :$displayNumber";
  254. $cmd .= " -desktop " . &quotedString($desktopName);
  255. if ($useClasses) {
  256.   $cmd .= " -httpd $vncClasses";
  257.   print ("Found $vncClasses for http connections.\n");
  258.   if ($opt{'-httpport'}) {
  259.     $cmd .= " -httpport $opt{'-httpport'}";
  260.     print ("Listening to $opt{'-httpport'} for http connections.\n");
  261.   }
  262.   elsif ($opt{'-basehttpport'}) {
  263.     my $v = $opt{'-basehttpport'} + $displayNumber;
  264.     print ("Listening to $v for http connections.\n");
  265.     $cmd .= " -httpport $v";
  266.   }
  267. }
  268. $cmd .= " -auth $xauthorityFile";
  269. $cmd .= " -geometry $geometry" if ($geometry);
  270. $cmd .= " -depth $depth" if ($depth);
  271. $cmd .= " -pixelformat $pixelformat" if ($pixelformat);
  272. $cmd .= " -rfbwait 120000";
  273. $cmd .= " $authType";
  274. $cmd .= " -rfbport $vncPort";
  275. $cmd .= " -fp $fontPath" if ($fontPath);
  276. # $cmd .= " -co $colorPath" if ($colorPath);
  277. $cmd .= " -alwaysshared" if ($opt{'-alwaysshared'});
  278. $cmd .= " -nevershared" if ($opt{'-nevershared'});
  279. $cmd .= " -improvedhextile=$improvedhextile" if (defined $improvedhextile); # New Param
  280. $cmd .= " -framerate=$framerate" if (defined $framerate);                   # New Param
  281. $cmd .= " -comparefb=$comparefb" if (defined $comparefb);                   # New Param
  282.  
  283. foreach $arg (@ARGV) {
  284.     $cmd .= " " . &quotedString($arg);
  285. }
  286. $cmd .= " >> " . &quotedString($desktopLog) . " 2>&1";
  287.  
  288. # Run $cmd and record the process ID.
  289.  
  290. $pidFile = "$vncUserDir/$host:$displayNumber.pid";
  291. system("$cmd & echo \$! >$pidFile");
  292.  
  293. # Give Xtightvnc a chance to start up
  294.  
  295. sleep(1);
  296. unless (kill 0, `cat $pidFile`) {
  297.     warn "Couldn't start Xtightvnc; trying default font path.\n";
  298.     warn "Please set correct fontPath in the $prog script.\n";
  299.     $cmd =~ s@-fp [^ ]+@@;
  300.     system("$cmd & echo \$! >$pidFile");
  301.     sleep(1);
  302. }
  303. unless (kill 0, `cat $pidFile`) {
  304.     warn "Couldn't start Xtightvnc process.\n\n";
  305.     open(LOG, "<$desktopLog");
  306.     while (<LOG>) { print; }
  307.     close(LOG);
  308.     die "\n";
  309. }
  310.  
  311. warn "\nNew '$desktopName' desktop is $host:$displayNumber\n\n";
  312.  
  313. # Create the user's xstartup script if necessary.
  314.  
  315. unless (-e "$xstartup") {
  316.     warn "Creating default startup script $xstartup\n";
  317.     open(XSTARTUP, ">$xstartup");
  318.     print XSTARTUP $defaultXStartup;
  319.     close(XSTARTUP);
  320.     chmod 0755, "$xstartup";
  321. }
  322.  
  323. # Run the X startup script.
  324.  
  325. warn "Starting applications specified in $xstartup\n";
  326. warn "Log file is $desktopLog\n\n";
  327.  
  328. # If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
  329. # TCP (DISPLAY=host:n)
  330.  
  331. if (-e "/tmp/.X11-unix/X$displayNumber") {
  332.     $ENV{DISPLAY}= ":$displayNumber";
  333. } else {
  334.     $ENV{DISPLAY}= "$host:$displayNumber";
  335. }
  336. $ENV{VNCDESKTOP}= $desktopName;
  337.  
  338. system("$xstartup >> " . &quotedString($desktopLog) . " 2>&1 &");
  339.  
  340. exit;
  341.  
  342. ############################ Debian functions #################################
  343. # I thank Manoj for the code below.
  344. #
  345. # ReadConfigFile reads in a config file and sets variables according to it.
  346. #
  347.  
  348. sub ReadConfigFile
  349. {
  350.   open(CONFIG, "$Config_file") || return;
  351.   my $lineno = 0;
  352.   while (<CONFIG>) {
  353.       chomp;
  354.       $lineno++;
  355.       s/\#.*//og;
  356.       next if /^\s*$/og;
  357.       $_ .= ";" unless /;\s*$/;
  358.       if (/^\s*([^=]+)\s*=\s*(\S.*)$/o) {
  359.           my $ret = eval "$1=$2";
  360.           if ($@) {
  361.               print STDERR "Error parsing config file $Config_file!\n";
  362.               print STDERR "$lineno:$_\n";
  363.           }
  364.       }
  365.   }
  366. }
  367.  
  368. sub ReadXFConfigFont
  369. {
  370.   open(CONFIG, "$XFConfigPath") || return;
  371.   my $lineno = 0;
  372.   while (<CONFIG>) {
  373.       chomp;
  374.       $lineno++;
  375.       s/\#.*//og;
  376.       next if /^\s*$/og;
  377.       if (/^\s*FontPath\s*"(\S.*)"\s*$/o) {
  378.           $fontPath .= "," if $fontPath;
  379.           $fontPath .= $1;
  380.       }
  381.   }
  382. }
  383.  
  384. sub ReadXFConfigColor
  385. {
  386.   open(CONFIG, "$XFConfigPath") || return;
  387.   my $lineno = 0;
  388.   while (<CONFIG> && !$colorPath) {
  389.       chomp;
  390.       $lineno++;
  391.       s/\#.*//og;
  392.       next if /^\s*$/og;
  393.       if (/^\s*RgbPath\s*"(\S.*)"\s*$/o) {
  394.           $colorPath = $1;
  395.       }
  396.   }
  397. }
  398.  
  399.  
  400. ########## End of debian functions ###########
  401.  
  402. ###############################################################################
  403. #
  404. # CheckGeometryAndDepth simply makes sure that the geometry and depth values
  405. # are sensible.
  406. #
  407.  
  408. sub CheckGeometryAndDepth
  409. {
  410.     if ($geometry =~ /^(\d+)x(\d+)$/) {
  411.     $width = $1; $height = $2;
  412.  
  413.     if (($width<1) || ($height<1)) {
  414.         die "$prog: geometry $geometry is invalid\n";
  415.     }
  416.  
  417.     while (($width % 4)!=0) {
  418.         $width = $width + 1;
  419.     }
  420.  
  421.     while (($height % 2)!=0) {
  422.         $height = $height + 1;
  423.     }
  424.  
  425.     $geometry = "${width}x$height";
  426.     } else {
  427.     die "$prog: geometry $geometry is invalid\n";
  428.     }
  429.  
  430.     if (($depth < 8) || ($depth > 32)) {
  431.     die "Depth must be between 8 and 32\n";
  432.     }
  433. }
  434.  
  435.  
  436. #
  437. # GetDisplayNumber gets the lowest available display number.  A display number
  438. # n is taken if something is listening on the VNC server port (5900+n) or the
  439. # X server port (6000+n).
  440. #
  441.  
  442. sub GetDisplayNumber
  443. {
  444.     foreach $n (1..99) {
  445.     if (&CheckDisplayNumber($n)) {
  446.         return $n+0; # Bruce Mah's workaround for bug in perl 5.005_02
  447.     }
  448.     }
  449.    
  450.     die "$prog: no free display number on $host.\n";
  451. }
  452.  
  453.  
  454. #
  455. # CheckDisplayNumber checks if the given display number is available.  A
  456. # display number n is taken if something is listening on the VNC server port
  457. # (5900+n) or the X server port (6000+n).
  458. #
  459.  
  460. sub CheckDisplayNumber
  461. {
  462.     local ($n) = @_;
  463.  
  464.     socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
  465.     eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
  466.     #unless (bind(S, pack('S n x12', $AF_INET, 6000 + $n))) {
  467.     unless (bind(S, sockaddr_in(6000 + $n, &INADDR_ANY))) {
  468.     close(S);
  469.     return 0;
  470.     }
  471.     close(S);
  472.  
  473.     socket(S, $AF_INET, $SOCK_STREAM, 0) || die "$prog: socket failed: $!\n";
  474.     eval 'setsockopt(S, &SOL_SOCKET, &SO_REUSEADDR, pack("l", 1))';
  475.     #unless (bind(S, pack('S n x12', $AF_INET, 5900 + $n))) {
  476.     unless (bind(S, sockaddr_in(5900 + $n, &INADDR_ANY))) {
  477.     close(S);
  478.     return 0;
  479.     }
  480.     close(S);
  481.  
  482.     if (-e "/tmp/.X$n-lock") {
  483.     warn "\nWarning: $host:$n is taken because of /tmp/.X$n-lock\n";
  484.     warn "Remove this file if there is no X server $host:$n\n";
  485.     return 0;
  486.     }
  487.  
  488.     if (-e "/tmp/.X11-unix/X$n") {
  489.     warn "\nWarning: $host:$n is taken because of /tmp/.X11-unix/X$n\n";
  490.     warn "Remove this file if there is no X server $host:$n\n";
  491.     return 0;
  492.     }
  493.  
  494.     return 1;
  495. }
  496.  
  497.  
  498. #
  499. # GetXDisplayDefaults uses xdpyinfo to find out the geometry, depth and pixel
  500. # format of the current X display being used.  If successful, it sets the
  501. # options as appropriate so that the X VNC server will use the same settings
  502. # (minus an allowance for window manager decorations on the geometry).  Using
  503. # the same depth and pixel format means that the VNC server won't have to
  504. # translate pixels when the desktop is being viewed on this X display (for
  505. # TrueColor displays anyway).
  506. #
  507.  
  508. sub GetXDisplayDefaults
  509. {
  510.     local (@lines, @matchlines, $width, $height, $defaultVisualId, $i,
  511.        $red, $green, $blue);
  512.  
  513.     $wmDecorationWidth = 4; # a guess at typical size for window manager
  514.     $wmDecorationHeight = 24;   # decoration size
  515.  
  516.     return unless (defined($ENV{DISPLAY}));
  517.  
  518.     @lines = `xdpyinfo 2>/dev/null`;
  519.  
  520.     return if ($? != 0);
  521.  
  522.     @matchlines = grep(/dimensions/, @lines);
  523.     if (@matchlines) {
  524.     ($width, $height) = ($matchlines[0] =~ /(\d+)x(\d+) pixels/);
  525.  
  526.     $width -= $wmDecorationWidth;
  527.     $height -= $wmDecorationHeight;
  528.  
  529.     $geometry = "${width}x$height";
  530.     }
  531.  
  532.     @matchlines = grep(/default visual id/, @lines);
  533.     if (@matchlines) {
  534.     ($defaultVisualId) = ($matchlines[0] =~ /id:\s+(\S+)/);
  535.  
  536.     for ($i = 0; $i < @lines; $i++) {
  537.         if ($lines[$i] =~ /^\s*visual id:\s+$defaultVisualId$/) {
  538.         if (($lines[$i+1] !~ /TrueColor/) ||
  539.             ($lines[$i+2] !~ /depth/) ||
  540.             ($lines[$i+4] !~ /red, green, blue masks/))
  541.         {
  542.             return;
  543.         }
  544.         last;
  545.         }
  546.     }
  547.  
  548.     return if ($i >= @lines);
  549.  
  550.     ($depth) = ($lines[$i+2] =~ /depth:\s+(\d+)/);
  551.     ($red,$green,$blue)
  552.         = ($lines[$i+4]
  553.            =~ /masks:\s+0x([0-9a-f]+), 0x([0-9a-f]+), 0x([0-9a-f]+)/);
  554.  
  555.     $red = hex($red);
  556.     $green = hex($green);
  557.     $blue = hex($blue);
  558.  
  559.     if ($red > $blue) {
  560.         $red = int(log($red) / log(2)) - int(log($green) / log(2));
  561.         $green = int(log($green) / log(2)) - int(log($blue) / log(2));
  562.         $blue = int(log($blue) / log(2)) + 1;
  563.         $pixelformat = "rgb$red$green$blue";
  564.     } else {
  565.         $blue = int(log($blue) / log(2)) - int(log($green) / log(2));
  566.         $green = int(log($green) / log(2)) - int(log($red) / log(2));
  567.         $red = int(log($red) / log(2)) + 1;
  568.         $pixelformat = "bgr$blue$green$red";
  569.     }
  570.     }
  571. }
  572.  
  573.  
  574. #
  575. # quotedString returns a string which yields the original string when parsed
  576. # by a shell.
  577. #
  578.  
  579. sub quotedString
  580. {
  581.     local ($in) = @_;
  582.  
  583.     $in =~ s/\'/\'\"\'\"\'/g;
  584.  
  585.     return "'$in'";
  586. }
  587.  
  588.  
  589. #
  590. # removeSlashes turns slashes into underscores for use as a file name.
  591. #
  592.  
  593. sub removeSlashes
  594. {
  595.     local ($in) = @_;
  596.  
  597.     $in =~ s|/|_|g;
  598.  
  599.     return "$in";
  600. }
  601.  
  602.  
  603. #
  604. # Usage
  605. #
  606.  
  607. sub Usage
  608. {
  609.     die("TightVNC Server version 1.3.10\n".
  610.     "\n".
  611.     "Usage: $prog [<OPTIONS>] [:<DISPLAY#>]\n".
  612.     "       $prog -kill :<DISPLAY#>\n".
  613.     "\n".
  614.     "<OPTIONS> are Xtightvnc options, or:\n".
  615.     "\n".
  616.     "        -name <DESKTOP-NAME>\n".
  617.     "        -depth <DEPTH>\n".
  618.     "        -geometry <WIDTH>x<HEIGHT>\n".
  619.         "        -httpport number\n".
  620.         "        -basehttpport number\n".
  621.         "        -alwaysshared\n".
  622.         "        -nevershared\n".
  623.     "        -pixelformat rgb<NNN>\n".
  624.     "        -pixelformat bgr<NNN>\n".
  625.     "\n".
  626.     "See vncserver and Xtightvnc manual pages for more information.\n");
  627. }
  628.  
  629.  
  630. #
  631. # Kill
  632. #
  633.  
  634. sub Kill
  635. {
  636.     $opt{'-kill'} =~ s/(:\d+)\.\d+$/$1/; # e.g. turn :1.0 into :1
  637.  
  638.     if ($opt{'-kill'} =~ /^:\d+$/) {
  639.     $pidFile = "$vncUserDir/$host$opt{'-kill'}.pid";
  640.     } else {
  641.     if ($opt{'-kill'} !~ /^$host:/) {
  642.         die "\nCan't tell if $opt{'-kill'} is on $host\n".
  643.         "Use -kill :<number> instead\n\n";
  644.     }
  645.     $pidFile = "$vncUserDir/$opt{'-kill'}.pid";
  646.     }
  647.  
  648.     unless (-r $pidFile) {
  649.     die "\nCan't find file $pidFile\n".
  650.         "You'll have to kill the Xtightvnc process manually\n\n";
  651.     }
  652.  
  653.     $SIG{'HUP'} = 'IGNORE';
  654.     chop($pid = `cat $pidFile`);
  655.     warn "Killing Xtightvnc process ID $pid\n";
  656.     system("kill $pid");
  657.     unlink $pidFile;
  658.  
  659.     ## If option -clean is given, also remove the logfile
  660.     unlink "$vncUserDir/$host$opt{'-kill'}.log" if ($opt{'-clean'});
  661.  
  662.     exit;
  663. }
  664.  
  665.  
  666. #
  667. # ParseOptions takes a list of possible options and a boolean indicating
  668. # whether the option has a value following, and sets up an associative array
  669. # %opt of the values of the options given on the command line. It removes all
  670. # the arguments it uses from @ARGV and returns them in @optArgs.
  671. #
  672.  
  673. sub ParseOptions
  674. {
  675.     local (@optval) = @_;
  676.     local ($opt, @opts, %valFollows, @newargs);
  677.  
  678.     while (@optval) {
  679.     $opt = shift(@optval);
  680.     push(@opts,$opt);
  681.     $valFollows{$opt} = shift(@optval);
  682.     }
  683.  
  684.     @optArgs = ();
  685.     %opt = ();
  686.  
  687.     arg: while (defined($arg = shift(@ARGV))) {
  688.     foreach $opt (@opts) {
  689.         if ($arg eq $opt) {
  690.         push(@optArgs, $arg);
  691.         if ($valFollows{$opt}) {
  692.             if (@ARGV == 0) {
  693.             &Usage();
  694.             }
  695.             $opt{$opt} = shift(@ARGV);
  696.             push(@optArgs, $opt{$opt});
  697.         } else {
  698.             $opt{$opt} = 1;
  699.         }
  700.         next arg;
  701.         }
  702.     }
  703.     push(@newargs,$arg);
  704.     }
  705.  
  706.     @ARGV = @newargs;
  707. }
  708.  
  709.  
  710. #
  711. # Routine to make sure we're operating in a sane environment.
  712. #
  713.  
  714. sub SanityCheck
  715. {
  716.     local ($cmd);
  717.  
  718.     #
  719.     # Get the program name
  720.     #
  721.  
  722.     ($prog) = ($0 =~ m|([^/]+)$|);
  723.  
  724.     #
  725.     # Check we have all the commands we'll need on the path.
  726.     #
  727.  
  728.  cmd:
  729.     foreach $cmd ("uname","xauth","Xtightvnc","vncpasswd") {
  730.     for (split(/:/,$ENV{PATH})) {
  731.         if (-x "$_/$cmd") {
  732.         next cmd;
  733.         }
  734.     }
  735.     die "$prog: couldn't find \"$cmd\" on your PATH.\n";
  736.     }
  737.  
  738.     #
  739.     # Check the HOME and USER environment variables are both set.
  740.     #
  741.  
  742.     unless (defined($ENV{HOME})) {
  743.     die "$prog: The HOME environment variable is not set.\n";
  744.     }
  745.     unless (defined($ENV{USER})) {
  746.     die "$prog: The USER environment variable is not set.\n";
  747.     }
  748.  
  749.     #
  750.     # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
  751.     # eval, and if it fails we try 'require "sys/socket.ph"'.  If this fails,
  752.     # we just guess at the values.  If you find perl moaning here, just
  753.     # hard-code the values of AF_INET and SOCK_STREAM.  You can find these out
  754.     # for your platform by looking in /usr/include/sys/socket.h and related
  755.     # files.
  756.     #
  757.  
  758.     chop($os = `uname`);
  759.     chop($osrev = `uname -r`);
  760.  
  761.     eval 'use Socket';
  762.     if ($@) {
  763.     eval 'require "sys/socket.ph"';
  764.     if ($@) {
  765.         if (($os eq "SunOS") && ($osrev !~ /^4/)) {
  766.         $AF_INET = 2;
  767.         $SOCK_STREAM = 2;
  768.         } else {
  769.         $AF_INET = 2;
  770.         $SOCK_STREAM = 1;
  771.         }
  772.     } else {
  773.         $AF_INET = &AF_INET;
  774.         $SOCK_STREAM = &SOCK_STREAM;
  775.     }
  776.     } else {
  777.     $AF_INET = &AF_INET;
  778.     $SOCK_STREAM = &SOCK_STREAM;
  779.     }
  780. }
  781.  
  782. sub ReadConfiguration
  783. {
  784.   my @configurableVariables =
  785.     qw(geometry
  786.        depth
  787.        desktopName
  788.        vncClasses
  789.        vncUserDir
  790.        fontPath
  791.        authType
  792.        colorPath
  793.       );
  794.  
  795.   if (open CONF, "<$configFile") {
  796.     while (<CONF>) {
  797.       if (/^\s*\$(\w+)\s*=\s*(.*)$/) {
  798.         for my $var (@configurableVariables) {
  799.           if ($1 eq $var) {
  800.             eval $_;
  801.             last;
  802.           }
  803.         }
  804.       }
  805.     }
  806.     close CONF;
  807.   }
  808. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement