Douglas Mauch
Douglas Mauch

Reputation: 869

Perl IO::Socket::INET permission denied

Have a perl script to connect with a Java service running on localhost, passes encrypted cookie, and returns decrypted data. When I run script from command line, it works fine. Even gave apache user a shell, and ran from command line as that user, which also works fine. If the script is run as CGI from apache, the socket new returns undef and $! is set with "permission denied". ???

Running CentOS 6.3 on this server, and IPtables are disabled.

#!/usr/bin/perl

use strict;

use CGI;
use IO::Socket;
use JSON;

my $cgi = CGI->new();

my $cookie = $cgi->cookie('attESSec') || shift (@ARGV) || undef;
my $data = JSON::false;

if($cookie){
  my $socket = IO::Socket::INET->new(
    'PeerHost' => '127.0.0.1',
    'PeerPort' => '1500',
    'Proto'    => 'tcp'
  );
  if($socket){
    $socket->send($cookie . "\r\n");
    $socket->recv(my $auth,1024);
    $socket->close();
    chomp($auth);
    if($auth){
      $data = (split(/\|/,$auth))[5];
    }
  }
  else{
    $data = $!;
  }
}

print($cgi->header('application/javascript'));
print(JSON->new()->allow_nonref()->utf8()->encode($data));

exit();

Upvotes: 3

Views: 1583

Answers (1)

Douglas Mauch
Douglas Mauch

Reputation: 869

I found the answer. The problem was SElinux. By default it doesn't let the httpd process (or anything that spawns from it, such as CGI scripts) establish network sockets. So just had to enable that particular feature with command "setsebool -P httpd_can_network_connect 1". Now it works perfectly.

Upvotes: 5

Related Questions