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

Hi Monks

I have a script that I'm using on a Windows 2003 64-bit server, to do LDAP queries and updates. It runs fine from the command line but I need it to run as a CGI script.

When running in CGI, I get an error

IO::Socket::INET: Bad hostname '<hostname>'

When I try putting in an IP address instead of a host name, I get:

IO::Socket::INET: Unknown error

Does anyone know of a reason why IO::Socket would fail in this situation? Here is the code up to where the failure occurs

use strict; use Net::LDAP; # global variables # my $bind_dn = "uid=xxx,ou=Directory Administrators,dc=xxx,dc=com"; my $bind_pw = "secret"; my $host = "ldapd1.xxx.com"; my $port = 389; my $group = "Operator"; my $action = "add"; # Read in user name from Web Field my ($buffer, @pairs, %FORM); $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } else { $buffer = $ENV{'QUERY_STRING'}; } @pairs = split(/&/, $buffer); foreach my $pair (@pairs) { my ($name, $fvalue) = split(/=/, $pair); $fvalue =~ tr/+/ /; $fvalue =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $fvalue; } my $mail = $FORM{'j_username'}; # Print out header stuff print "Content-type: text/html\n\n"; print "<!DOCTYPE HTML PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01\/\/EN\" \"htt +p:\/\/www.w3.org\/TR\/html4\/strict.dtd\">\n"; print "<html>\n"; print "<head>\n"; print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset= +UTF-8\">\n"; print "<link rel=stylesheet type=text/css href=\"/css/default.css\" /> +\n"; print "<title>Request Access</title>\n"; if ($mail eq "") { printError ("You must enter a user name"); } # configuration # my $usr_base_dn = "ou=people,ou=global,dc=xxx,dc=com"; my $grp_base_dn = "ou=xxx,ou=groups,ou=services,ou=global,dc=xxx,dc=co +m"; # open an LDAP connection my $ldap = new Net::LDAP($host, port => $port); if (!$ldap) { printError ("Cannot contact Directory Server: $@\n"); exit; }

Any help greatly appreciated!

Thanks!
Rich

Replies are listed 'Best First'.
Re: Net::LDAP in a CGI script
by jethro (Monsignor) on Jan 22, 2010 at 22:38 UTC

    When I try putting in an IP address instead of a host name, I get

    I assume you mean $host where you are putting the IP address, right?

    If you check out IO::Socket::INET (just search for the file INET.pm in your perl install), you will see that the error message is not only printed for the PeerAddr, but also for the LocalAddr, i.e. the local hostname. To check if this is the cause you could change the Net::LDAP call to

    my $ldap = new Net::LDAP($host, port => $port, localaddr => "yourhostn +ame");

    If this doesn't help, you might add the following lines to the start of the sub configure in INET.pm to check out which values it gets when it is called

    use Data::Dumper; open($f,'>','/tmp/log); print $f Data::Dumper($arg); close($f);

    It probably will tell you where INET gets this '<hostname>' string from that it prints.

      Thanks for the reply guys

      I did eventually get this working. For information, this was down to the environment. The "SYSTEMROOT" environment variable was not set, and this was required for the query to work correctly.

      Regards
      Rich

Re: Net::LDAP in a CGI script
by james2vegas (Chaplain) on Jan 24, 2010 at 09:27 UTC
    You should make sure that your webserver isn't running as LocalSystem which by default does not have network access. See this microsoft kb article (and for NT read NT/2000/XP).