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


In reply to Net::LDAP in a CGI script by rhirst

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.