Dranzaz has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am working on migrating from and old server to a newer box. The old server was using a RedHat Enterprise distro that we no longer have a support contract for. As a result our server installation department has configured the new machine with the latest Fedora Core release. I began moving my CGI scripts over this week and am running into troubles. If I run the scripts from the command line (sudo root) everything runs fine. However, when calling them from a web form (Apache) they are not performing their required tasks: For example: The following snippet is supposed to grab the web output and place it into a file:
$tempfileone = "/var/www/cgi-bin/logs/ipsectest/iptestscript$userips[$ +ipcontrol].txt"; $urlone = "http://notgivingthesite/cgi-bin/block-lookup\?$userips[$ipc +ontrol]"; $mechone = WWW::Mechanize->new(); print Dumper $mechone->get( $urlone, ':content_file' => $tempfileone ) +;
With the Dumper tag, I get the following:
$VAR1 = bless( { '_content' => '500 Can\'t connect to notgivingthesite +:80 (Bad hostname \'notgivingthesite\') ', '_rc' => 500, '_headers' => bless( { 'client-warning' => 'Internal +response', 'client-date' => 'Wed, 08 Apr +2009 13:34:36 GMT', 'content-type' => 'text/plain' }, 'HTTP::Headers' ), '_msg' => 'Can\'t connect to notgivingthesite:80 (Bad + hostname \'notgivingthesite\')', '_request' => bless( { '_content' => '', '_uri' => bless( do{\(my $o = +'http://notgivingthesite/cgi-bin/block-lookup?209.18.32.55')}, 'URI:: +http' ), '_headers' => bless( { 'user-a +gent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)', 'accept +-encoding' => 'gzip' }, 'HTTP: +:Headers' ), '_method' => 'GET', '_uri_canonical' => $VAR1->{'_ +request'}{'_uri'} }, 'HTTP::Request' ) }, 'HTTP::Response' ); $VAR1 = undef;
Again, from the command line everything works just fine. This snippet takes a given IP and places it in a reversed format (10.1.2.6 became 6.2.1.10) and then looks up the PTR record. It should then output to the browser the name lookup for the IP. Unfortunately it only returns a "could not get socket" error:
$ips = inet_aton("$userips[$ipcontrol]"); $forward = gethostbyaddr($ips, AF_INET); $reverselu = gethostbyname("$forward"); $reverse = inet_ntoa($reverselu) if ($reverselu); my $revIP = join(".", reverse(split(/\./, $userips[$ipcontrol]))); $|=1; print Dumper $query = $resolver->query("$revIP.in-addr.arpa.","PTR"); if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "PTR"; print " ", $rr->ptrdname; $ptr = $rr->ptrdname; } print "<BR>"; } else { print " <STRONG><font color=\"red\">", $resolver->errorstring, "\n +"; $ptr = "NXDOMAIN"; print "</STRONG></font><BR>"; $testcondition = 1; } $|=0;
The resulting HTML output looks like the following:
<STRONG><font color="red">could not get socket </STRONG></font><BR>
Again, from CLI works just fine and on the old server it works just fine. I beleive this to be an Apache issue since forcing the scripts to run via CLI works just fine. Can anyone assist in this? My server guy is stumped at this time and says Apache is setup correctly (ie near default config). Any assistance or guidance will be greatly appreciated. Thank You

Replies are listed 'Best First'.
Re: Perl?Apache issue on newly installed server
by almut (Canon) on Apr 08, 2009 at 14:34 UTC

    Two more things to check: (1) Can you run the scripts from the command line as the user the webserver is running as (apache, www,...), instead of as root?  (2) Does the new distro have SELinux enabled? (which might be constraining what apache is allowed to do)  Check with /usr/sbin/getenforce as a first step...

      Ok, checked the SELinux status:
      # cat /selinux/enforce 1
      was set to enforcing, did the following:
      # echo 0 >/selinux/enforce
      It now writes the files and no longer gets the "could not open socket" error. Now it is timing out on the DNS Resolver, httpd log:
      [Wed Apr 08 10:51:09 2009] [error] [client 209.18.32.79] Name "main::r +everse" used only once: possible typo at /var/www/cgi-bin/ipsectest/i +psectest.pl line 98., referer: http://inoc.cdptpa.rr.com/tools/ipsect +est.html [Wed Apr 08 10:53:09 2009] [warn] [client 209.18.32.79] Timeout waitin +g for output from CGI script /var/www/cgi-bin/ipsectest/ipsectest.pl, + referer: http://inoc.cdptpa.rr.com/tools/ipsectest.html [Wed Apr 08 10:53:09 2009] [error] [client 209.18.32.79] (70007)The ti +meout specified has expired: ap_content_length_filter: apr_bucket_rea +d() failed, referer: http://inoc.cdptpa.rr.com/tools/ipsectest.html
      Changed the Resolvers nameserver variable and the output is fine now. SELinux apparently was the culprit. Thanks for the help.
Re: Perl?Apache issue on newly installed server
by cfreak (Chaplain) on Apr 08, 2009 at 14:17 UTC
      From the httpd error.log:
      [Wed Apr 08 10:14:25 2009] [error] [client 209.18.32.79] Name "main::r +everse" used only once: possible typo at /var/www/cgi-bin/ipsectest/i +psectest.pl line 98., referer: http://inoc.cdptpa.rr.com/tools/ipsect +est.html [Wed Apr 08 10:14:25 2009] [error] [client 209.18.32.79] Use of uninit +ialized value $forward in string at /var/www/cgi-bin/ipsectest/ipsect +est.pl line 97., referer: http://inoc.cdptpa.rr.com/tools/ipsectest.h +tml [Wed Apr 08 10:14:25 2009] [error] [client 209.18.32.79] readline() on + closed filehandle BLOCKREPORT at /var/www/cgi-bin/ipsectest/ipsectes +t.pl line 119., referer: http://inoc.cdptpa.rr.com/tools/ipsectest.ht +ml
      The reference to the "uninitialized value $forward" is the "$reverselu = gethostbyname("$forward");" line and since it is not writing the file in "$mechone->get( $urlone, ':content_file' => $tempfileone );" we get the "closed file handle" since there is no file.
Re: Perl?Apache issue on newly installed server
by Anonymous Monk on Apr 08, 2009 at 14:22 UTC
    Bad hostname tells you its a networking/dns issue.