From dcc8b2c644f6246db47990eb76fc72b55889a81e Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Wed, 1 Jan 2014 15:54:08 +0000 Subject: [PATCH] sanity check ip inventory subnet, and fix SQL::Abstract glitching on the CidrIps query --- .../App/Netdisco/DB/Result/Virtual/CidrIps.pm | 24 ++++++++++--------- .../Netdisco/Web/Plugin/Report/IpInventory.pm | 10 ++++++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Netdisco/lib/App/Netdisco/DB/Result/Virtual/CidrIps.pm b/Netdisco/lib/App/Netdisco/DB/Result/Virtual/CidrIps.pm index 6f7783d3..8cdbeff3 100644 --- a/Netdisco/lib/App/Netdisco/DB/Result/Virtual/CidrIps.pm +++ b/Netdisco/lib/App/Netdisco/DB/Result/Virtual/CidrIps.pm @@ -11,16 +11,18 @@ __PACKAGE__->table_class('DBIx::Class::ResultSource::View'); __PACKAGE__->table('cidr_ips'); __PACKAGE__->result_source_instance->is_virtual(1); __PACKAGE__->result_source_instance->view_definition(<<'ENDSQL'); -SELECT host(network (cidr) + sub.int)::inet AS ip, NULL::text AS dns, - NULL::timestamp AS time_first, NULL::timestamp AS time_last, false::boolean AS active -FROM - ( SELECT cidr, generate_series(1, broadcast(cidr) - (network(cidr)) - 1) AS int -FROM ( -SELECT CASE WHEN family(cidr) = 4 THEN cidr - ELSE '0.0.0.0/32'::inet - END AS cidr -FROM ( SELECT ?::inet AS cidr) AS input) AS addr -) AS sub +SELECT host(network (prefix) + sub.int)::inet AS ip, + NULL::text AS dns, + NULL::timestamp AS time_first, + NULL::timestamp AS time_last, + false::boolean AS active + FROM ( + SELECT prefix, + generate_series(1, (broadcast(prefix) - network(prefix) - 1)) AS int + FROM ( + SELECT ?::inet AS prefix + ) AS addr + ) AS sub ENDSQL __PACKAGE__->add_columns( @@ -42,4 +44,4 @@ __PACKAGE__->add_columns( }, ); -1; \ No newline at end of file +1; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/IpInventory.pm b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/IpInventory.pm index 6868e6ea..d8d7feb0 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin/Report/IpInventory.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin/Report/IpInventory.pm @@ -5,6 +5,7 @@ use Dancer::Plugin::DBIC; use Dancer::Plugin::Auth::Extensible; use App::Netdisco::Web::Plugin; +use NetAddr::IP::Lite ':lower'; register_report( { category => 'IP', @@ -37,6 +38,9 @@ get '/ajax/content/report/ipinventory' => require_login sub { # Default to something simple with no results to prevent # "Search failed!" error my $subnet = param('subnet') || '0.0.0.0/32'; + $subnet = NetAddr::IP::Lite->new($subnet); + $subnet = NetAddr::IP::Lite->new('0.0.0.0/32') + if (! $subnet) or ($subnet->addr eq '0.0.0.0'); my $age = param('age_on') || '0'; my $agenot = param('age_invert') || '0'; @@ -91,9 +95,11 @@ get '/ajax/content/report/ipinventory' => require_login sub { my $rs_union = $rs1->union( [ $rs2, $rs3 ] ); if ( $never ) { + $subnet = NetAddr::IP::Lite->new('0.0.0.0/32') if ($subnet->bits ne 32); + my $rs4 = schema('netdisco')->resultset('Virtual::CidrIps')->search( undef, - { bind => [ $subnet ], + { bind => [ $subnet->cidr ], columns => [qw( ip time_first time_last dns active)], '+select' => [ \'false AS node', \'age(time_last) AS age' ], '+as' => [ 'node', 'age' ], @@ -104,7 +110,7 @@ get '/ajax/content/report/ipinventory' => require_login sub { } my $rs_sub = $rs_union->search( - { ip => { '<<' => $subnet } }, + { ip => { '<<' => $subnet->cidr } }, { order_by => [qw( ip time_last )], select => [ \'DISTINCT ON (ip) ip',