[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: override write protection?



Jeff Licquia said:
> (For the purpose of allowing traceroutes: why can't traceroute be
> wrapped in a CGI of some kind and provided through the Web?  I've seen
> it done before.)

Or, for that matter, have a user "traceroute" with something like this
as a shell:

    #!/usr/bin/perl -Tw

    use strict;

    # Build a sanitized environment.
    # You'll have to modify this if
    # the program expects things here.
    $ENV=("PATH"  => "/bin:/usr/bin",
          "SHELL" => "/bin/sh");

    # Don't buffer STDOUT.
    $|=1;

    # Do this block until we run out of
    # input or the user enters "quit".
    while (!eof(STDIN))
    {
        print "Host (or "quit"): ";

        my $host=<STDIN>;

	exit 0 if ($host=~/^\s*quit\s*$/i);

        if (validhost($host))
	{
            my $ret=system "/usr/sbin/traceroute", $host;
	    print STDERR "traceroute failed.\n" if (($ret >> 8) > 0);
	}
	else
	{
	    print "** Invalid hostname or address.\n";
	}
    }

    # This should return 1 (true) if the value passed looks
    # like a hostname or IP address, or 0 (false) otherwise.
    sub isvalidhost($)
    {
        my $host=shift;

        if ((!$host) || ($host=~/^\./))
        {
            return 0;
        }
        elsif (($host=~/^([a-z0-9\-]{0,67}\.){0,254}[a-z]{2,3}\.?$/i)
               || (($host=~/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
                   && ($1 > 0) && ($1 < 255) && ($1 != 127) && ($1 != 224)
                   && ($2 >= 0) && ($2 < 256) && ($3 >= 0) && ($3 < 256)
                   && ($4 > 0) && ($4 < 255)))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

I don't guarantee that it'll work or is 100% safe, but it should be
better than having a shell of some sort.  (I leave it as an exercise
for the reader to figure out how to turn that into a CGI.  :)

Steve
-- 
steve@silug.org           | Linux Users of Central Illinois
(618)398-7320             | Meetings the 4th Tuesday of every month
Steven Pritchard          | http://www.luci.org/ for more info
-
To unsubscribe, send email to majordomo@luci.org with
"unsubscribe luci-discuss" in the body.