On 10/25/2006 01:23 AM, Richard Luckhurst wrote:
Hi All
I have a simple perl cgi script that works fine and produces the correct output
when run from a command line. However when called as a cgi script from within a
web page the script does not run correctly.
The script is called log-errors and is called as follows
log-errors?domain=www.resmaster.com
The script is as follows
#!/usr/bin/perl
#
use strict;
use warnings;
use CGI qw/:standard -no_xhtml/;
my $domain = param('domain');
my $tail = `tail -n2000 /var/log/httpd/$domain-error_log 2>/dev/null | tac`;
Mr. Luckhurst, this is a disaster waiting to happen:
log-errors?domain=%3Bls%20%2F%3B
Perhaps you want to sanitize $domain before giving it to the shell:
$domain =~ s/[^a-z0-9_.-]//ig;
Or, better, you can abort if $domain is "improper":
die if ($domain =~ m/[^a-z0-9._-]/);
$tail =~ s/</</g;
$tail =~ s/>/>/g;
$tail = escapeHTML($tail);
As Mr. Kennedy said, it might be a PATH or permissions problem. Build
$tail up in pieces, testing only a little bit at a time. First, test if
you can use the tail command:
$tail = `tail /some/publicly/accessible/file`;
Then try it with the parameter:
$tail = `tail -n200 /some/publicly/accessible/file`;
Then try it with your log file:
$tail = `tail -n2000 /var/log/httpd/${domain}-error_log`;
And so on. You might also want to change the header() to 'text/plain'
for testing, because you want to see the raw data in $tail. In an HTML
file, a "<" without a corresponding ">" would make the rest of the file
invisible.
print header;
print start_html (
-title => 'Exodus Web Server Log',
-lang => 'en-UK',
-link => 'blue',
-vlink => 'blue');
print font({size => 4}, b("Last 2000 Lines of the Error log for: $domain")), br,
"\n";
print font({size => 1}, br), "\n";
print 'Go to ', a({href => '#bottom'}, i('End of log')), br, br, "\n";
print pre($tail), "\n";
print a({name => 'bottom'}), br, "\n";
print b('End of log'), br, "\n";
print end_html, "\n\n\n";
I have tried putting a print statement in to see if the $domain variable gets
set and it does get set to www.resmaster.com. The print line also uses the
$domain variable and the correct thing is printed on the html page.
The system command produces nothing, the $tail variable is empty, when run as a
cgi script. When I run the script from a command line (and I force $domain to be
www.resmaster.com as I can't pass it in) I find that the system call works and I
get the data I would expect. In fact I left the $domain set to www.resmaster.com
and tried running it as a cgi and I get nothing from the system call.
I am using Apache 2.2 as my webserver and perl 5.8.8 both as they came with
Fedora core 5.
Can anyone offer any suggestions about why this does not work?
Regards
Richard Luckhurst
Fedora Core has something called selinux, that restricts access. If
necessary, configure selinux to let apache cgi scripts have access to
files in /var/log/httpd/*
Good luck.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>