0
votes

I am new to cgi and Perl, but i am trying to display ENV of of web page using a perl script. I created an index.html page inside of a vhost1 directory, which points to my .pl page which is inside my cgi-bin directory. the directory paths are as followed in coresponding order /var/www/html/vhost1/index.html and /var/www/html/vhost1/cgi-bin/first.pl. my index.html code is as followed

<html>
<head>
</head>
<body>
<h1>Hello World!</h1>
<a href = "cgi-bin/first.pl" target="_blank"> My First Perl Page</a>
</body>
</html>

And my .pl page is looks like

#!usr/bin/perl

print "Content-type: text/html \n\n ";
print "Hello, Nate";
foreach $key (sort keys %ENV)
{
print "$key -> $ENV($key) <br>\n";
}

I am using Centos7 to complete this project my URL path to my home page is http://131.183.223.173/vhost1/

I want my ENV results to display in a new tab rather then being downloaded to my computer.

1
You need to tell your server to treat .pl files (or all files in the cgi-bin dir) as CGI scriptsikegami
Also, #!usr/bin/perl should be #!/usr/bin/perlikegami
Finally, you should always use use strict; use warnings qw( all );. It will find a major and a minor error with your program. (It won't find your HTML injection error, though.)ikegami
As ikegami pointed-out, #!usr/bin/perl seems to be the problem.Srihari Karanth
@SrihariKaranth: While #!usr/bin/perl is a major problem here, I'm pretty sure it's not what is causing this behaviour. The web server isn't even trying to execute the program. I've never seen a web server configured so that if it can't successfully execute a CGI program, it will return the source code instead. That sounds like a major security issue!Dave Cross

1 Answers

0
votes

This has nothing at all to do with your HTML or your Perl program. This is down to your web server configuration. Your web server (which, I assume, is Apache) needs to be configured to recognise /var/www/html/vhost1/cgi-bin/ as a CGI directory.

You will need something like this in the configuration file for your virtual host.

ScriptAlias /cgi-bin/ "/var/www/html/vhost1/cgi-bin/"

Or

<Directory "/var/www/html/vhost1/cgi-bin/">
    Options +ExecCGI
    AddHandler cgi-script .pl
</Directory>

Once that has been fixed, you will stop getting the source code sent to your browser. At that point, however, you will almost certainly get a "500 Error" page. You can then work on fixing the errors in your CGI program (starting with the incorrect shebang line and content-type header).