Hello,

This may or may not be of use to some of you, in fact, I may not even find
it terribly useful.  It is a "vncserver" script for use with inetd.  All
the standard Xvnc+inetd caveats apply here.

To use:

1.  Edit vncserver.inetd to taste.  (bit depth, display size, etc.)

2. add a line similar to this to your /etc/services:

        vnc-1  5901/tcp  # Virtual Network Computer Display :1

3. add something like this to your /etc/inetd.conf  (I will not dignify
   that bastard stepchild known as xinetd with an analogue example):

        vnc-1 stream tcp nowait nic /usr/people/nic/bin/vncserver.inetd vncserver.inetd

4. HUP inetd.  (killall -HUP inetd, unless you use Solaris.  killall does
   something completely different there, not good)

5. Connect to the VNC port.  It should fire up a new Xvnc session every
time you connect, and tear it down when you disconnect.  Yum.

-- 
Nicolas Simonds

  In Google non est, ergo non est.
  If it is not on Google, then it does not exist.
#!/usr/bin/perl
#
#  Copyright (C) 2002 RealVNC Ltd.
#  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
#
#  This is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This software is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this software; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
#  USA.
#

#
# vncserver.inetd - wrapper script to start an X VNC server via inetd.
#                   much simpler than the full-blown version because there's
#                   less stuff that needs figuring out
#                                                         --nic
#
use Sys::Hostname;
use Sys::Syslog qw(:DEFAULT setlogsock);

setlogsock('unix');

#
# Edit path to taste.  If getpwuid() returns your homedir at a different
# offset than mine, you'll need to change that, too.
#
BEGIN {
        $ENV{'PATH'} = '/bin:/usr/bin/X11:/usr/local/bin';
        $ENV{'HOME'} = (getpwuid($<))[7];
}

END {
        closelog();
}

#
# First make sure we're operating in a sane environment.
#
SanityCheck();

#
# Global variables.  Configure these for your site.  $displayNumber is
# hard-coded, since inetd only listens on one port..
#
$displayNumber = 1;
$geometry = "1280x1024";
$depth = 24;
$desktopName = "X";
$vncUserDir = "$ENV{HOME}/.vnc";
$xauthorityFile = "$ENV{XAUTHORITY}" || "$ENV{HOME}/.Xauthority";

$defaultXStartup
    = ("#!/bin/sh\n\n".
       "[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources\n".
       "xsetroot -solid grey\n".
       "xterm -geometry 80x24+10+10 -ls -title \"\$VNCDESKTOP Desktop\" &\n".
       "twm &\n");

$host = hostname();

# Check command line options
ParseOptions("-geometry",1,"-depth",1,"-pixelformat",1,"-name",1,"-kill",1,
              "-help",0,"-h",0,"--help",0);

Usage() if ($opt{'-help'} || $opt{'-h'} || $opt{'--help'});

if ($opt{'-geometry'}) {
    $geometry = $opt{'-geometry'};
}
if ($opt{'-depth'}) {
    $depth = $opt{'-depth'};
    $pixelformat = "";
}
if ($opt{'-pixelformat'}) {
    $pixelformat = $opt{'-pixelformat'};
}

CheckGeometryAndDepth();

if ($opt{'-name'}) {
    $desktopName = $opt{'-name'};
}

# Create the user's vnc directory if necessary.
unless (-e $vncUserDir) {
        die_log("could not create $vncUserDir.") unless mkdir($vncUserDir,0755);
}
    
# Make sure the user has a password.
($z,$z,$mode) = stat("$vncUserDir/passwd");
if (!(-e "$vncUserDir/passwd") || ($mode & 077)) {
        die_log("$vncUserDir/passwd is not valid - run vncpasswd")
}

$desktopLog = "$vncUserDir/$host:$displayNumber.log";
# don't truncate the log
#unlink($desktopLog);

# Make an X server cookie - use as the seed the sum of the current time, our
# PID and part of the encrypted form of the password.  Ideally we'd use
# /dev/urandom, but that's only available on Linux.
srand(time+$$+unpack("L",`cat $vncUserDir/passwd`));
$cookie = "";
for (1..16) {
    $cookie .= sprintf("%02x", int(rand(256)) % 256);
}
    
system("xauth -f $xauthorityFile add $host:$displayNumber . $cookie");
system("xauth -f $xauthorityFile add $host/unix:$displayNumber . $cookie"); 

# Now start the X VNC Server
$cmd = "Xvnc :$displayNumber";
$cmd .= ' -inetd -once -dontdisconnect';
$cmd .= " -desktop " . quotedString($desktopName);
$cmd .= " -auth $xauthorityFile";
$cmd .= " -geometry $geometry" if ($geometry);
$cmd .= " -depth $depth" if ($depth);
$cmd .= " -pixelformat $pixelformat" if ($pixelformat);
$cmd .= " -rfbwait 120000";
$cmd .= " -rfbauth $vncUserDir/passwd";

# Add font path and color database stuff here, e.g.:
#
# $cmd .= " -fp /usr/lib/X11/fonts/misc/,/usr/lib/X11/fonts/75dpi/";
# $cmd .= " -co /usr/lib/X11/rgb";
#
$cmd .= " -fp tcp/localhost:7100";

foreach $arg (@ARGV) {
    $cmd .= " " . quotedString($arg);
}
$cmd .= " >> " . quotedString($desktopLog) . " 2>&1";

# fork a copy and Exec $cmd.  would use a system() call instead, but
# that calls waitpid() automatically, which we don't want..
if ($pid = fork) {
        exec($cmd);
        exit;
} else {

# Give Xvnc a chance to start up

sleep(3); 
chdir($ENV{'HOME'});

# Create the user's xstartup script if necessary.

if (!(-e "$vncUserDir/xstartup")) {
    syslog('info', "Creating default startup script $vncUserDir/xstartup");
    open(XSTARTUP, ">$vncUserDir/xstartup");
    print XSTARTUP $defaultXStartup;
    close(XSTARTUP);
    chmod 0755, "$vncUserDir/xstartup";
}

# Run the X startup script.

syslog('info', "Starting applications specified in $vncUserDir/xstartup");
syslog('info', "Log file is $desktopLog");

# If the unix domain socket exists then use that (DISPLAY=:n) otherwise use
# TCP (DISPLAY=host:n)

if (-e "/tmp/.X11-unix/X$displayNumber" ||
    -e "/usr/spool/sockets/X11/$displayNumber")
{
    $ENV{DISPLAY}= ":$displayNumber";
} else {
    $ENV{DISPLAY}= "$host:$displayNumber";
}
$ENV{VNCDESKTOP}= $desktopName;

system("$vncUserDir/xstartup >> " . quotedString($desktopLog) . " 2>&1 &");

exit;
}

###############################################################################
#
# CheckGeometryAndDepth simply makes sure that the geometry and depth values
# are sensible.
#
sub CheckGeometryAndDepth {
    if ($geometry =~ /^(\d+)x(\d+)$/) {
        $width = $1; $height = $2;

        if (($width<1) || ($height<1)) {
            die_log("geometry $geometry is invalid");
        }

        while (($width % 4)!=0) {
            $width = $width + 1;
        }

        while (($height % 2)!=0) {
            $height = $height + 1;
        }

        $geometry = "${width}x$height";
    } else {
        die_log("geometry $geometry is invalid");
    }

    if (($depth < 8) || ($depth > 32)) {
        die_log("Depth must be between 8 and 32");
    }
}

#
# quotedString returns a string which yields the original string when parsed
# by a shell.
#
sub quotedString {
    local ($in) = @_;

    $in =~ s#\'#\'\"\'\"\'#g;

    return "'$in'";
}

#
# removeSlashes turns slashes into underscores for use as a file name.
#
sub removeSlashes {
    local ($in) = @_;

    $in =~ tr|/|_|;

    return "$in";
}

#
# Usage
#
sub Usage { die_log('bad usage') }

#
# ParseOptions takes a list of possible options and a boolean indicating
# whether the option has a value following, and sets up an associative array
# %opt of the values of the options given on the command line. It removes all
# the arguments it uses from @ARGV and returns them in @optArgs.
#
sub ParseOptions {
    local (@optval) = @_;
    local ($opt, @opts, %valFollows, @newargs);

    while (@optval) {
        $opt = shift(@optval);
        push(@opts,$opt);
        $valFollows{$opt} = shift(@optval);
    }

    @optArgs = ();
    %opt = ();

    arg: while (defined($arg = shift(@ARGV))) {
        foreach $opt (@opts) {
            if ($arg eq $opt) {
                push(@optArgs, $arg);
                if ($valFollows{$opt}) {
                    if (@ARGV == 0) {
                        Usage();
                    }
                    $opt{$opt} = shift(@ARGV);
                    push(@optArgs, $opt{$opt});
                } else {
                    $opt{$opt} = 1;
                }
                next arg;
            }
        }
        push(@newargs,$arg);
    }

    @ARGV = @newargs;
}

#
# Routine to make sure we're operating in a sane environment.
#
sub SanityCheck {
    local ($cmd);

    #
    # Get the program name
    #
    ($prog) = ($0 =~ m|([^/]+)$|);

        #
        # Open the syslog
        #
        openlog($prog, 'cons,pid,ndelay', 'daemon');

    #
    # Check we have all the commands we'll need on the path.
    #
 cmd:
    foreach $cmd ("uname","xauth","Xvnc","vncpasswd") {
        for (split(/:/,$ENV{PATH})) {
            if (-x "$_/$cmd") {
                next cmd;
            }
        }
        die_log("couldn't find \"$cmd\" on your PATH.");
    }

    #
    # Check the HOME environment variable is set
    #
    if (!defined($ENV{HOME})) {
        die_log("The HOME environment variable is not set.");
    }

    #
    # Find socket constants. 'use Socket' is a perl5-ism, so we wrap it in an
    # eval, and if it fails we try 'require "sys/socket.ph"'.  If this fails,
    # we just guess at the values.  If you find perl moaning here, just
    # hard-code the values of AF_INET and SOCK_STREAM.  You can find these out
    # for your platform by looking in /usr/include/sys/socket.h and related
    # files.
    #
    chop($os = `uname`);
    chop($osrev = `uname -r`);

    eval 'use Socket';
    if ($@) {
        eval 'require "sys/socket.ph"';
        if ($@) {
            if (($os eq "SunOS") && ($osrev !~ /^4/)) {
                $AF_INET = 2;
                $SOCK_STREAM = 2;
            } else {
                $AF_INET = 2;
                $SOCK_STREAM = 1;
            }
        } else {
            $AF_INET = &AF_INET;
            $SOCK_STREAM = &SOCK_STREAM;
        }
    } else {
        $AF_INET = &AF_INET;
        $SOCK_STREAM = &SOCK_STREAM;
    }
}

sub die_log {
        my $msg = "@_";
        syslog('err', $msg) if $msg;
        exit 1;
}
_______________________________________________
VNC-List mailing list
[EMAIL PROTECTED]
http://www.realvnc.com/mailman/listinfo/vnc-list

Reply via email to