From 064ac198a91cd549b230af1735b6a5d2a272ad2a Mon Sep 17 00:00:00 2001 From: Colin GAGNAIRE Date: Wed, 17 Jan 2018 14:49:37 +0100 Subject: [PATCH 1/4] improve authentification handling --- centreon/plugins/http.pm | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/centreon/plugins/http.pm b/centreon/plugins/http.pm index 9a5762eeb..ee0c4d7d7 100644 --- a/centreon/plugins/http.pm +++ b/centreon/plugins/http.pm @@ -27,6 +27,18 @@ use HTTP::Cookies; use URI; use IO::Socket::SSL; +{ + package CentreonUserAgent; + our @ISA = qw(LWP::UserAgent); + + sub get_basic_credentials { + my($self, $realm, $uri, $proxy) = @_; + return if $proxy; + return $centreon::plugins::http::request_options->{username}, $centreon::plugins::http::request_options->{password} if $centreon::plugins::http::request_options->{credentials}; + return undef, undef; + } +} + sub new { my ($class, %options) = @_; my $self = {}; @@ -204,14 +216,14 @@ sub set_proxy { sub request { my ($self, %options) = @_; - my $request_options = { %{$self->{options}} }; + our $request_options = { %{$self->{options}} }; foreach (keys %options) { $request_options->{$_} = $options{$_} if (defined($options{$_})); } $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} = CentreonUserAgent->new(keep_alive => 1, protocols_allowed => ['http', 'https'], timeout => $request_options->{timeout}); if (defined($request_options->{cookies_file})) { $self->{ua}->cookie_jar(HTTP::Cookies->new(file => $request_options->{cookies_file}, autosave => 1)); @@ -264,16 +276,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 +323,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(); } From a9e52da83a0248d06e0ab48a0f2f80e1f05397ff Mon Sep 17 00:00:00 2001 From: Colin GAGNAIRE Date: Fri, 19 Jan 2018 12:11:16 +0100 Subject: [PATCH 2/4] create new class --- centreon/plugins/http.pm | 19 +++----------- centreon/plugins/useragent.pm | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 centreon/plugins/useragent.pm diff --git a/centreon/plugins/http.pm b/centreon/plugins/http.pm index ee0c4d7d7..681b9124e 100644 --- a/centreon/plugins/http.pm +++ b/centreon/plugins/http.pm @@ -22,23 +22,11 @@ package centreon::plugins::http; use strict; use warnings; -use LWP::UserAgent; +use centreon::plugins::useragent; use HTTP::Cookies; use URI; use IO::Socket::SSL; -{ - package CentreonUserAgent; - our @ISA = qw(LWP::UserAgent); - - sub get_basic_credentials { - my($self, $realm, $uri, $proxy) = @_; - return if $proxy; - return $centreon::plugins::http::request_options->{username}, $centreon::plugins::http::request_options->{password} if $centreon::plugins::http::request_options->{credentials}; - return undef, undef; - } -} - sub new { my ($class, %options) = @_; my $self = {}; @@ -216,14 +204,15 @@ sub set_proxy { sub request { my ($self, %options) = @_; - our $request_options = { %{$self->{options}} }; + my $request_options = { %{$self->{options}} }; foreach (keys %options) { $request_options->{$_} = $options{$_} if (defined($options{$_})); } $self->check_options(request => $request_options); if (!defined($self->{ua})) { - $self->{ua} = CentreonUserAgent->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)); diff --git a/centreon/plugins/useragent.pm b/centreon/plugins/useragent.pm new file mode 100644 index 000000000..245194894 --- /dev/null +++ b/centreon/plugins/useragent.pm @@ -0,0 +1,49 @@ +# +# 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}; + return undef, undef; +} + +1; From 51da04b5e3561f94f5bc2961a4c433035190e154 Mon Sep 17 00:00:00 2001 From: Colin GAGNAIRE Date: Fri, 19 Jan 2018 13:34:10 +0100 Subject: [PATCH 3/4] add wantarray or string --- centreon/plugins/useragent.pm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/centreon/plugins/useragent.pm b/centreon/plugins/useragent.pm index 245194894..5eca396e9 100644 --- a/centreon/plugins/useragent.pm +++ b/centreon/plugins/useragent.pm @@ -42,8 +42,9 @@ sub new { sub get_basic_credentials { my($self, $realm, $uri, $proxy) = @_; return if $proxy; - return $self->{username}, $self->{password} if $self->{credentials}; - return undef, undef; + return $self->{username}, $self->{password} if $self->{credentials} and wantarray; + return $self->{username}.":".$self->{password} if $self->{credentials}; + return undef; } 1; From 6cf7c3153fe0f0082d5bb5bc4831336d9af67c62 Mon Sep 17 00:00:00 2001 From: Colin GAGNAIRE Date: Fri, 19 Jan 2018 16:54:07 +0100 Subject: [PATCH 4/4] remove ntlm option from pod --- apps/protocols/http/mode/expectedcontent.pm | 4 ---- apps/protocols/http/mode/jsoncontent.pm | 4 ---- apps/protocols/http/mode/response.pm | 4 ---- apps/protocols/http/mode/soapcontent.pm | 4 ---- 4 files changed, 16 deletions(-) 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)