diff --git a/apps/protocols/http/mode/expectedcontent.pm b/apps/protocols/http/mode/expectedcontent.pm index 47a79b8b8..4674fbf1f 100644 --- a/apps/protocols/http/mode/expectedcontent.pm +++ b/apps/protocols/http/mode/expectedcontent.pm @@ -198,10 +198,6 @@ Set path to get Webpage (Default: '/') Specify this option if you access webpage over basic authentication -=item B<--ntlm> - -Specify this option if you access webpage over ntlm authentication (Use with --credentials option) - =item B<--ntlmv2> Specify this option if you access webpage over ntlmv2 authentication (Use with --credentials and --port options) diff --git a/apps/protocols/http/mode/jsoncontent.pm b/apps/protocols/http/mode/jsoncontent.pm index 6f21c23b1..70c241be3 100644 --- a/apps/protocols/http/mode/jsoncontent.pm +++ b/apps/protocols/http/mode/jsoncontent.pm @@ -395,10 +395,6 @@ Set path to get Webpage (Default: '/') Specify this option if you access webpage over basic authentication -=item B<--ntlm> - -Specify this option if you access webpage over ntlm authentication (Use with --credentials option) - =item B<--ntlmv2> Specify this option if you access webpage over ntlmv2 authentication (Use with --credentials and --port options) diff --git a/apps/protocols/http/mode/response.pm b/apps/protocols/http/mode/response.pm index 4ca6a58b3..b60bc2258 100644 --- a/apps/protocols/http/mode/response.pm +++ b/apps/protocols/http/mode/response.pm @@ -176,10 +176,6 @@ Set path to get webpage (Default: '/') Specify this option if you access webpage over basic authentication -=item B<--ntlm> - -Specify this option if you access webpage over ntlm authentication (Use with --credentials option) - =item B<--ntlmv2> Specify this option if you access webpage over ntlmv2 authentication (Use with --credentials and --port options) diff --git a/apps/protocols/http/mode/soapcontent.pm b/apps/protocols/http/mode/soapcontent.pm index 53d085650..9d0084cd1 100644 --- a/apps/protocols/http/mode/soapcontent.pm +++ b/apps/protocols/http/mode/soapcontent.pm @@ -422,10 +422,6 @@ Set path to get Webpage (Default: '/') Specify this option if you access webpage over basic authentication -=item B<--ntlm> - -Specify this option if you access webpage over ntlm authentication (Use with --credentials option) - =item B<--ntlmv2> Specify this option if you access webpage over ntlmv2 authentication (Use with --credentials and --port options) diff --git a/centreon/plugins/http.pm b/centreon/plugins/http.pm index 9a5762eeb..681b9124e 100644 --- a/centreon/plugins/http.pm +++ b/centreon/plugins/http.pm @@ -22,7 +22,7 @@ package centreon::plugins::http; use strict; use warnings; -use LWP::UserAgent; +use centreon::plugins::useragent; use HTTP::Cookies; use URI; use IO::Socket::SSL; @@ -211,7 +211,8 @@ sub request { $self->check_options(request => $request_options); if (!defined($self->{ua})) { - $self->{ua} = LWP::UserAgent->new(keep_alive => 1, protocols_allowed => ['http', 'https'], timeout => $request_options->{timeout}); + $self->{ua} = centreon::plugins::useragent->new(keep_alive => 1, protocols_allowed => ['http', 'https'], timeout => $request_options->{timeout}, + credentials => $request_options->{credentials}, username => $request_options->{username}, password => $request_options->{password}); if (defined($request_options->{cookies_file})) { $self->{ua}->cookie_jar(HTTP::Cookies->new(file => $request_options->{cookies_file}, autosave => 1)); @@ -264,16 +265,11 @@ sub request { $req->content($uri_post->query); } } - - if (defined($request_options->{credentials}) && defined($request_options->{ntlm})) { - $self->{ua}->credentials($request_options->{hostname} . ':' . $request_options->{port}, '', $request_options->{username}, $request_options->{password}); - } elsif (defined($request_options->{credentials}) && defined($request_options->{ntlmv2})) { + + if (defined($request_options->{credentials}) && defined($request_options->{ntlmv2})) { centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'Authen::NTLM', error_msg => "Cannot load module 'Authen::NTLM'."); Authen::NTLM::ntlmv2(1); - $self->{ua}->credentials($request_options->{hostname} . ':' . $request_options->{port}, '', $request_options->{username}, $request_options->{password}); - } elsif (defined($request_options->{credentials})) { - $req->authorization_basic($request_options->{username}, $request_options->{password}); } $self->set_proxy(request => $request_options, url => $url); @@ -316,9 +312,15 @@ sub request { } if (!$self->{output}->is_status(value => $status, compare => 'ok', litteral => 1)) { + my $short_msg = $response->status_line; + if ($short_msg =~ /^401/) { + my ($authenticate) = $response->www_authenticate =~ /(\S+)/; + $short_msg .= ' (' . $authenticate . ' authentification expected)'; + } + $self->{output}->output_add(long_msg => $response->content, debug => 1); $self->{output}->output_add(severity => $status, - short_msg => $response->status_line); + short_msg => $short_msg); $self->{output}->display(); $self->{output}->exit(); } diff --git a/centreon/plugins/useragent.pm b/centreon/plugins/useragent.pm new file mode 100644 index 000000000..5eca396e9 --- /dev/null +++ b/centreon/plugins/useragent.pm @@ -0,0 +1,50 @@ +# +# Copyright 2017 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package centreon::plugins::useragent; + +use strict; +use warnings; +use base 'LWP::UserAgent'; + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + $self = LWP::UserAgent::new(@_); + $self->agent("centreon::plugins::useragent"); + + $self->{credentials} = $options{credentials} if defined($options{credentials}); + $self->{username} = $options{username} if defined($options{username}); + $self->{password} = $options{password} if defined($options{password}); + + return $self; +} + +sub get_basic_credentials { + my($self, $realm, $uri, $proxy) = @_; + return if $proxy; + return $self->{username}, $self->{password} if $self->{credentials} and wantarray; + return $self->{username}.":".$self->{password} if $self->{credentials}; + return undef; +} + +1;