Welcome to WebmasterWorld Guest from 54.167.185.18

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

CGI displays Perl Script

   
7:21 pm on Feb 28, 2012 (gmt 0)

5+ Year Member



Running Debian Squeeze, Apache 2.2 and Perl 5.10.1. Just migrated from old box running Apache 1.3 and Debian Sarge.

One old html page we have ran a rudimentary pearl search script on the old box. But now whenever it's invoked, we see the text of the perl script.

The files were username:users, but I tried changing them to www-data:www-date. No change. The html file invoking the script is 755, as are the cgi-lib.pl and srch.cgi files. Perl is at #!/usr/bin/perl
Other perl scripts on the system are working.

I'd appreciate any thoughts on how to proceed. Thanks.
7:43 pm on Feb 28, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do you mean the files were usernames:users? Does cgi-lib.pl and srch.cgi execute if you access them directly?

It may be that without a file extension, Apache has no idea that those (username:users) are perl files.
7:58 pm on Feb 28, 2012 (gmt 0)

5+ Year Member



File owner was username, group was users, as in
"chown username:users *.*"

I changed to www-data:www-data *.* just to see if that might get it working. Sorry I wasn't clear if you thought the actual filenames were username or users.

Executing "perl srch.cgi":

perl srch.cgi
$* is no longer supported at cgi-lib.pl line 97.
Content-type: text/html

<HTML><HEAD>
<TITLE>Page Title</TITLE></head><body BGCOLOR="ffffe7" text="000000">


Same error message executing cgi-lib.pl

As to the error, looking at cgi-lib (line 97 is "$old = $*; $* =1;"

sub PrintVariables {
local (%in) = @_;
local ($old, $out, $output);
$old = $*; $* =1;
$output .= "<DL COMPACT>";
foreach $key (sort keys(%in)) {
foreach (split("\0", $in{$key})) {
($out = $_) =~ s/\n/<BR>/g;
$output .= "<DT><B>$key</B><DD><I>$out</I><BR>";
}
}
$output .= "</DL>";
$* = $old;
8:17 pm on Feb 28, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok. That makes a lot more sense. cgi-lib.pl is a very old script. Instead of trying to make it work, see if you can use CGI.pm instead.

[wrcc.dri.edu...]
8:27 pm on Feb 28, 2012 (gmt 0)

5+ Year Member



Actually I just found that and tried it - unsuccessfully.

See the discussion at [search.cpan.org...] under "COMPATIBILITY WITH CGI-LIB.PL"

They suggest in srch.cgi, change

require("cgi-lib.pl");
to
use CGI;

and substituting

&ReadParse;
to
CGI::ReadParse();

They also suggest substitution of
print "The value of the antique is $in{antique}.\n";
but the new and old text is the same. So I left that alone.

In any case, I made the first two substitutions, and nothing changed.
8:32 pm on Feb 28, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you try:

use CGI qw(:cgi-lib);


or

use CGI qw(:cgi-lib :standard)
;
8:49 pm on Feb 28, 2012 (gmt 0)

5+ Year Member



I tried both. Here's the top (not all) of srch.cgi:

!/usr/bin/perl
# srch.pl
$|=1;
#require("cgi-lib.pl");
#use CGI;
#use CGI qw(:cgi-lib);
use CGI qw(:cgi-lib :standard);
print &PrintHeader;
# &ReadParse;
CGI::ReadParse();
$delm="\t";
#########enter file names here
@file=("aaa.txt","bbb.txt","ccc.txt","ddd.txt","eee.txt","fff.txt","ggg.txt","hhh.txt");
#########
9:04 pm on Feb 28, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think this updated cgi-lib.pl script will solve your problem (cross fingers). It doesn't use the depricated $* special variable.

[cgi-lib.berkeley.edu...]
12:29 am on Feb 29, 2012 (gmt 0)

5+ Year Member



Nope still not working
12:58 am on Feb 29, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What kind of error are you getting?
1:00 am on Feb 29, 2012 (gmt 0)

5+ Year Member



Backing up... when I execute srch.cgi

# perl srch.cgi
Content-type: text/html

<HTML><HEAD>
<TITLE>Page Title</TITLE></head><body BGCOLOR="ffffe7" text="000000">end
#

no warnings, nothing in error or access logs
1:05 am on Feb 29, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I noticed that the newer version doesn't have a path to perl at the top of the script. If adding one doesn't work, maybe you can switch out the PrintVariables subroutine in the older script and replace it with the newer one.
1:09 am on Feb 29, 2012 (gmt 0)

5+ Year Member



FYI, here's the text of srch.cgi. It's old but it worked for many years.

#!/usr/bin/perl
# srch.pl
$|=1;
require("cgi-lib.pl");
print &PrintHeader;
&ReadParse;
$delm="\t";
#########enter file names here
@file=("ny.txt","bronx.txt","queens.txt","brooklyn.txt","richmond.txt","west.txt","nassau.txt","rockland.txt");
#########

$whichfile=$file[$in{'sel'}];
print "<HTML><HEAD>\n";
print "<TITLE>Page Title</TITLE></head>";
print '<body BGCOLOR="ffffe7" text="000000">';

########in input is not a null string then search#####
if($in{'query'}){

print '<h1><b> Page text</b></h1>';
print '<i> More Page text</i><br>';
print '<i> Even More Page text.</i><br>';
print ' <b>Page text</b> <a href="misc.html">Misc information</a>';
print '<hr>';

###############
#open online database
##############
open(FILE,"$whichfile");

$found=0;
$count=$tally;
#initialize variable to tally
#loop to look for model by name
#print $in{'query'};

while (<FILE>){
$ct++;
#@field=split($delm,$_);
#if(($_=~/^$in{'query'}/i)&&($in{'query'})){
#####test query#####
if(($_=~/$in{'query'}/i)&&($in{'query'})){

$_=~s/\#/ <b> Page text<\/b> /i;
$_=~s/\*/ <b> More page text<\/b> /i;
print $_,"<br>";
$found++
}
#####################end test query###
}

#print " $ct";

if($found <1){print'<h1>Nothing Matched</H1>'}else{print "$found Matches found<br>"};

close FILE;
}


######################GET TIME############################
($sec,$min,$hour,$monthday,$month,$year,$weekday,$yearday,$isdaylight)=localtime;
if ($min<10){$minute="0$min"}else{$minute=$min;}
##########################################################log activity.
$browser=$ENV{"HTTP_USER_AGENT"};
$host=$ENV{"REMOTE_HOST"};
$addr=$ENV{"REMOTE_ADDR"};

open(LOG,">>logfile.dat");
print LOG $month+1, '/', $monthday, '/', $year, $delm, $hour,':', $minute, $delm, $whichfile, $delm, '"', $in{'query'}, '"', $delm, $found, $delm, "Found", $delm, $browser, $delm, $host, $delm, $addr, $delm, "\n";
close LOG;
#####################

print "end";
__END__;

[edited by: phranque at 4:47 am (utc) on Feb 29, 2012]
[edit reason] edited to fix sidescroll [/edit]

1:10 am on Feb 29, 2012 (gmt 0)

5+ Year Member



I saw that too. Just added #!/usr/bin/perl but still not working.
1:15 am on Feb 29, 2012 (gmt 0)

5+ Year Member



Are you saying leave out the path to perl (on the new cgi-lib.pl), but then take the PrintVariables subroutine (approx. 40 lines to the end of the file) and put it in the old cgi-lib.pl file?
1:21 am on Feb 29, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After seeing the code for srch.cgi, I don't see any need for that subroutine. If you're not using cgi-lib.pl for any other script, comment out the two lines in the old cgi-lib.pl file that contain $*.
1:26 am on Feb 29, 2012 (gmt 0)

5+ Year Member



I'll try that, but also FYI, here's the old cgi-lib.pl

#!/usr/bin/perl -- -*- C -*-
# Perl Routines to Manipulate CGI input
# S.E.Brenner@bioc.cam.ac.uk
# $Header: /cys/people/seb1005/http/cgi-bin/RCS/cgi-lib.pl,v 1.7 1994/11/04 00:17:17 seb1005 Exp $
#
# Copyright 1994 Steven E. Brenner
# Unpublished work.
# Permission granted to use and modify this library so long as the
# copyright above is maintained, modifications are documented, and
# credit is given for any use of the library.
#
# Thanks are due to many people for reporting bugs and suggestions
# especially Meng Weng Wong, Maki Watanabe, Bo Frese Rasmussen,
# Andrew Dalke, Mark-Jason Dominus and Dave Dittrich.

# see [seas.upenn.edu...] or
# [bio.cam.ac.uk...] for more information

# Minimalist http form and script (http://www.bio.cam.ac.uk/web/minimal.cgi):
# if (&MethGet) {
# print &PrintHeader,
# '<form method=POST><input type="submit">Data: <input name="myfield">';
# } else {
# &ReadParse(*input);
# print &PrintHeader, &PrintVariables(%input);
# }


# MethGet
# Return true if this cgi call was using the GET request, false otherwise
# Now that cgi scripts can be put in the normal file space, it is useful
# to combine both the form and the script in one place with GET used to
# retrieve the form, and POST used to get the result.

sub MethGet {
return ($ENV{'REQUEST_METHOD'} eq "GET");
}

# ReadParse
# Reads in GET or POST data, converts it to unescaped text, and puts
# one key=value in each member of the list "@in"
# Also creates key/value pairs in %in, using '\0' to separate multiple
# selections

# If a variable-glob parameter (e.g., *cgi_input) is passed to ReadParse,
# information is stored there, rather than in $in, @in, and %in.

sub ReadParse {
local (*in) = @_ if @_;

local ($i, $loc, $key, $val);

# Read in text
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}

@in = split(/&/,$in);

foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;

# Split into key and value.
($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;

# Associate key and value
$in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;

}

return 1; # just for fun
}

# PrintHeader
# Returns the magic line which tells WWW that we're an HTML document

sub PrintHeader {
return "Content-type: text/html\n\n";
}

# PrintVariables
# Nicely formats variables in an associative array passed as a parameter
# And returns the HTML string.

sub PrintVariables {
local (%in) = @_;
local ($old, $out, $output);
$old = $*; $* =1;
$output .= "<DL COMPACT>";
foreach $key (sort keys(%in)) {
foreach (split("\0", $in{$key})) {
($out = $_) =~ s/\n/<BR>/g;
$output .= "<DT><B>$key</B><DD><I>$out</I><BR>";
}
}
$output .= "</DL>";
$* = $old;

return $output;
}

# PrintVariablesShort
# Nicely formats variables in an associative array passed as a parameter
# Using one line per pair (unless value is multiline)

# Using one line per pair (unless value is multiline)
# And returns the HTML string.


sub PrintVariablesShort {
local (%in) = @_;
local ($old, $out, $output);
$old = $*; $* =1;
foreach $key (sort keys(%in)) {
foreach (split("\0", $in{$key})) {
($out = $_) =~ s/\n/<BR>/g;
$output .= "<B>$key</B> is <I>$out</I><BR>";
}
}
$* = $old;

return $output;
}

1; #return true
1:30 am on Feb 29, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll have to comment out the two lines that use $* in sub PrintVariablesShort as well.
1:32 am on Feb 29, 2012 (gmt 0)

5+ Year Member



So I've put the original cgi-lib.pl back and commented out the two lines

sub PrintVariablesShort {
local (%in) = @_;
local ($old, $out, $output);
# $old = $*; $* =1;
foreach $key (sort keys(%in)) {
foreach (split("\0", $in{$key})) {
($out = $_) =~ s/\n/<BR>/g;
$output .= "<B>$key</B> is <I>$out</I><BR>";
}
}
# $* = $old;

return $output;
}

1; #return true
1:39 am on Feb 29, 2012 (gmt 0)

5+ Year Member



And like a broken record :(, still broken. Maybe I could sleep better if I just scrapped this section of the web site. Could the issue be in the apache config?, the vhost? the .htaccess file? elsewhere?
1:54 am on Feb 29, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



srch.cgi only uses two subroutines from cgi-lib.pl. Cut them out and make a new cgi-lib.pl using this code:

#!/usr/bin/perl 

# ReadParse
# Reads in GET or POST data, converts it to unescaped text, and puts
# one key=value in each member of the list "@in"
# Also creates key/value pairs in %in, using '\0' to separate multiple
# selections

# If a variable-glob parameter (e.g., *cgi_input) is passed to ReadParse,
# information is stored there, rather than in $in, @in, and %in.


sub ReadParse {
local (*in) = @_ if @_;

local ($i, $loc, $key, $val);

# Read in text
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
}
elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
}

@in = split(/&/,$in);

foreach $i (0 .. $#in) {
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;

# Split into key and value.
($key, $val) = split(/=/,$in[$i],2); # splits on the first =.

# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;

# Associate key and value
$in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;

}

return 1; # just for fun
}


# PrintHeader
# Returns the magic line which tells WWW that we're an HTML document

sub PrintHeader {
return "Content-type: text/html\n\n";
}

1; #return true


If this doesn't work, I don't know what will.
2:05 am on Feb 29, 2012 (gmt 0)

5+ Year Member



I'm confused when you say "cut them out." Not sure if I understand. The above quoted text is the entire new cgi-lib.pl? If so, I just tried it and it doesn't work.

You've been very helpful and I very much appreciate it ... but it's getting late in the day and I need to walk away for now. (I'm sure you have other things on your plate as well).

Would it help if I sent you the URL (they call it sticky mail on this forum, right?)

Thank you
4:09 am on Feb 29, 2012 (gmt 0)

5+ Year Member



Now I can go to bed.

When success eludes you, always go back and read a tutorial. I did, and it seemed so basic I didn't think much about it. At this point I'm not even sure which version of cgi-lib.pl is there, but it's working.

The fix... Going back to the site's docroot .htaccess file, I noticed that "AddHandler cgi-script .cgi .pl" had been commented out. I'm not even sure it was there on the old server, but I think it had been added when dealing with includes on the home page (some were cgi, some were php). The .htaccess file always did have an "Options +ExecCGI" line and I thought that was sufficient. Obviously not.

So I uncommented the AddHandler and there it was!

Why the logs showed no error on this, I don't know. In any case, even though the solution was outside our discussion, I very much appreciate the time and effort you put in to help. Thanks again.

Software error:

Can't locate /home/deploy/webmasterworld/code_format-v6.lib in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at decode-post-v6.lib line 27, <THREADDAT> line 25.

For help, please send mail to the webmaster (it@imninjas.com), giving this error message and the time and date of the error.