3

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?

RobEarl
  • 7,862
  • 6
  • 35
  • 50
Vetle
  • 3,287
  • 2
  • 27
  • 28
  • 1
    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 Apr 12 '12 at 12:38
  • @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 Apr 12 '12 at 12:47
  • 1
    Hi Vetle :) Do your `$username` or `$password` contain non-ascii characters by any chance? – Cosimo Apr 12 '12 at 13:04
  • 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 Apr 12 '12 at 13:20
  • @vetler: any chance your username or password is > 14 chars? – Edward Thomson Apr 12 '12 at 14:09
  • @EdwardThomson they're both < 14 characters – Vetle Apr 13 '12 at 08:03

2 Answers2

-1

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);
user1126070
  • 5,059
  • 1
  • 16
  • 15
  • I tried replacing `$post_encoded` with 'asdasd', and get the same error message. I've tested this with NTLM authentication turned off on the server, and it works fine. The NTML authentication process *should* be independent on whatever I want to post, as it happens in separate HTTP requests. – Vetle Apr 12 '12 at 12:30
  • encode_utf8 is not only convert a string to utf-8, it will set the utf-8 flag on it. This way the perl internals know that it is contain an utf-8 value. Or you could try out the opposite way and use decode_utf8. – user1126070 Apr 16 '12 at 07:17
  • This is irrelevant. I've replaced $post_encoded with 'asdasd' (without doing any encoding at all), and it still doesn't work. – Vetle Apr 17 '12 at 08:45
-4

Just use curl

curl --ntlm -u 'username:password' url 
Luke
  • 11,426
  • 43
  • 60
  • 69
  • Thanks, but not really helpful. ;) Wget also works fine, but I need this to work with LWP. – Vetle Apr 13 '12 at 08:02