3
votes

I'm trying to get LWP to work against an IIS server configured with NTLM authentication. When NTLM authentication is turned off on the server, the code work fine, so I assume that the only problem here is the NTLM authentication.

So far, I have the following:

my $ua = LWP::UserAgent->new(agent => "whatever",
                            timeout => $timeout, keep_alive => 1);
$ua->credentials('hostname:80', '', $username, $password);

my $hdr = HTTP::Headers->new("Content-Type" => "text/xml; charset=UTF-8",
                             "SOAPAction" => "\"whatever\"");

my $req = HTTP::Request->new("POST" => $url, $hdr, encode_utf8($post));
$res = $ua->request($req);

If I turn on debugging, I get the following messages:

LWP::UserAgent::new: ()
LWP::UserAgent::request: ()
LWP::UserAgent::send_request: POST http://hostname
LWP::UserAgent::_need_proxy: Not proxied
LWP::Protocol::http::request: ()
LWP::Protocol::http::request: Keep the http connection to hostname:80
LWP::UserAgent::request: Simple response: Unauthorized
LWP::Authen::Ntlm::authenticate: authenticate() has been called
Use of uninitialized value in exists at /usr/lib/perl5/vendor_perl/5.8.5/LWP/UserAgent.pm line 560.
Use of uninitialized value in hash element at /usr/lib/perl5/vendor_perl/5.8.5/LWP/UserAgent.pm line 561.
LWP::Authen::Ntlm::authenticate: In first phase of NTLM authentication
[Thu Apr 12 13:55:28 2012] [error] Wide character in subroutine entry at /usr/lib/perl5/site_perl/5.8.5/Authen/NTLM.pm line 346.\n
LWP::Protocol::collect: read 625 bytes
LWP::UserAgent::request: Simple response: Internal Server Error

Trying to access the same URL with wget works fine. The documentation for MIME::Base64 says that the encode function will croak with Wide character in subroutine entry if $bytes contains characters with code above 255.

Am I missing something essential here, or could this be a bug in Authen::NTLM?

2
I notice the docs for LWP::Authen::Ntlm says The module takes advantage of the Authen::NTLM module by Mark Bush. Since there is also another Authen::NTLM module available from CPAN by Yee Man Chan with an entirely different interface, it is necessary to ensure that you have the correct NTLM module. Maybe this is the problem?Borodin
@Borodin: the module lists Mark Bush in the Author section. I also installed the specific version required in LWP - I had Authen::NTLM 1.09, but the version of LWP I'm using required 1.02, but get the same error.Vetle
Hi Vetle :) Do your $username or $password contain non-ascii characters by any chance?Cosimo
Hi Cosimo! :D I double checked, since I originally copy/pasted them, and typed them in once more - still no worky. Checking further, I noticed I had to add a \ to the username, to get LWP::Authen::Ntlm to pass the username to Authen::NTLM (it expects domain\username). That got me fewer messages about uninitialized values, but same error message in the end.Vetle
@vetler: any chance your username or password is > 14 chars?Edward Thomson

2 Answers

-1
votes

What is in $post? Maybe is is contain bad data.

Try this:

my $post_encoded = encode_utf8($post);
print Dumper($post,$post_encoded);
my $req = HTTP::Request->new("POST" => $url, $hdr, $post_encoded);
-4
votes

Just use curl

curl --ntlm -u 'username:password' url