[197] | 1 | #!/usr/bin/perl
|
---|
| 2 | # ipdb/cgi-bin/search.cgi
|
---|
| 3 | # Started splitting search functions (quick and otherwise) from
|
---|
| 4 | # main IPDB interface 03/11/2005
|
---|
| 5 | ###
|
---|
| 6 | # SVN revision info
|
---|
| 7 | # $Date: 2022-10-20 21:46:32 +0000 (Thu, 20 Oct 2022) $
|
---|
| 8 | # SVN revision $Rev: 930 $
|
---|
| 9 | # Last update by $Author: kdeugau $
|
---|
| 10 | ###
|
---|
[930] | 11 | # Copyright 2005-2010,2012,2015-2017,2022 - Kris Deugau <kdeugau@deepnet.cx>
|
---|
[197] | 12 |
|
---|
| 13 | use strict;
|
---|
| 14 | use warnings;
|
---|
| 15 | use CGI::Carp qw(fatalsToBrowser);
|
---|
[517] | 16 | use CGI::Simple;
|
---|
| 17 | use HTML::Template;
|
---|
[197] | 18 | use DBI;
|
---|
| 19 | use POSIX qw(ceil);
|
---|
| 20 | use NetAddr::IP;
|
---|
| 21 |
|
---|
[417] | 22 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
| 23 | ##uselib##
|
---|
[197] | 24 |
|
---|
[906] | 25 | # push "the directory the script is in" into @INC
|
---|
| 26 | use FindBin;
|
---|
| 27 | use lib "$FindBin::RealBin/";
|
---|
| 28 |
|
---|
[417] | 29 | use MyIPDB;
|
---|
| 30 |
|
---|
[439] | 31 | # Don't formally need a username or syslog here. syslog left active for debugging.
|
---|
| 32 | use Sys::Syslog;
|
---|
| 33 | openlog "IPDBsearch","pid","$IPDB::syslog_facility";
|
---|
| 34 |
|
---|
| 35 | # ... but we do *use* the username on ACLs now.
|
---|
| 36 | # Collect the username from HTTP auth. If undefined, we're in
|
---|
| 37 | # a test environment, or called without a username.
|
---|
| 38 | my $authuser;
|
---|
| 39 | if (!defined($ENV{'REMOTE_USER'})) {
|
---|
| 40 | $authuser = '__temptest';
|
---|
| 41 | } else {
|
---|
| 42 | $authuser = $ENV{'REMOTE_USER'};
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[517] | 45 | # Global variables
|
---|
| 46 | my $RESULTS_PER_PAGE = 25;
|
---|
| 47 |
|
---|
| 48 | # anyone got a better name? :P
|
---|
| 49 | my $thingroot = $ENV{SCRIPT_FILENAME};
|
---|
| 50 | $thingroot =~ s|cgi-bin/search.cgi||;
|
---|
| 51 |
|
---|
| 52 | # Set up the CGI object...
|
---|
| 53 | my $q = new CGI::Simple;
|
---|
| 54 | # ... and get query-string params as well as POST params if necessary
|
---|
| 55 | $q->parse_query_string;
|
---|
| 56 |
|
---|
| 57 | # Convenience; saves changing all references to %webvar
|
---|
| 58 | ##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
|
---|
| 59 | my %webvar = $q->Vars;
|
---|
| 60 |
|
---|
| 61 | if (defined($webvar{rpp})) {
|
---|
| 62 | ($RESULTS_PER_PAGE) = ($webvar{rpp} =~ /(\d+)/);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[197] | 65 | # Why not a global DB handle? (And a global statement handle, as well...)
|
---|
| 66 | # Use the connectDB function, otherwise we end up confusing ourselves
|
---|
| 67 | my $ip_dbh;
|
---|
| 68 | my $sth;
|
---|
| 69 | my $errstr;
|
---|
| 70 | ($ip_dbh,$errstr) = connectDB_My;
|
---|
[517] | 71 | if ($ip_dbh) {
|
---|
| 72 | checkDBSanity($ip_dbh);
|
---|
| 73 | initIPDBGlobals($ip_dbh);
|
---|
[197] | 74 | }
|
---|
| 75 |
|
---|
[517] | 76 | # Set up some globals
|
---|
[801] | 77 | $ENV{HTML_TEMPLATE_ROOT} = $thingroot;
|
---|
| 78 | my @templatepath = [ "localtemplates", "templates" ];
|
---|
[197] | 79 |
|
---|
[823] | 80 | ## FIXME!
|
---|
| 81 | ## Pretty much everything from here on down is one giant FIXME
|
---|
| 82 | ## FIXME!
|
---|
| 83 |
|
---|
[517] | 84 | my $page;
|
---|
[197] | 85 | if (!defined($webvar{stype})) {
|
---|
| 86 | $webvar{stype} = "<NULL>"; #shuts up the warnings.
|
---|
[801] | 87 | $page = HTML::Template->new(filename => "search/compsearch.tmpl", path => @templatepath);
|
---|
[896] | 88 | $page->param(webpath => $IPDB::webpath);
|
---|
[517] | 89 | } else {
|
---|
[801] | 90 | $page = HTML::Template->new(filename => "search/sresults.tmpl", global_vars => 1, path => @templatepath);
|
---|
[670] | 91 | $page->param(webpath => $IPDB::webpath);
|
---|
[197] | 92 | }
|
---|
| 93 |
|
---|
[801] | 94 | my $header = HTML::Template->new(filename => "header.tmpl", path => @templatepath);
|
---|
[517] | 95 | $header->param(version => $IPDB::VERSION);
|
---|
| 96 | $header->param(addperm => $IPDBacl{$authuser} =~ /a/);
|
---|
[670] | 97 | $header->param(webpath => $IPDB::webpath);
|
---|
[517] | 98 | print "Content-type: text/html\n\n", $header->output;
|
---|
[197] | 99 |
|
---|
[670] | 100 | # Columns actually returned. Slightly better than hardcoding it
|
---|
| 101 | # in each (sub)select
|
---|
[823] | 102 | my $cols = "s.cidr, s.custid, s.type, s.city, s.description, s.id, s.parent_id, s.available, a.vrf";
|
---|
[930] | 103 | # Common base select. JOIN provides the VRF which may not be noted on individual allocations
|
---|
| 104 | my $sqlbase = "SELECT $cols FROM searchme s JOIN allocations a ON s.master_id=a.id";
|
---|
[670] | 105 |
|
---|
[517] | 106 | # Handle the DB error first
|
---|
| 107 | if (!$ip_dbh) {
|
---|
[801] | 108 | $page = HTML::Template->new(filename => "dberr.tmpl", path => @templatepath);
|
---|
[517] | 109 | $page->param(errmsg => $errstr);
|
---|
| 110 | } elsif ($webvar{stype} eq 'q') {
|
---|
[197] | 111 | # Quick search.
|
---|
| 112 |
|
---|
| 113 | if (!$webvar{input}) {
|
---|
| 114 | # No search term. Display everything.
|
---|
| 115 | viewBy('all', '');
|
---|
| 116 | } else {
|
---|
| 117 | # Search term entered. Display matches.
|
---|
| 118 | # We should really sanitize $webvar{input}, no?
|
---|
| 119 | my $searchfor;
|
---|
| 120 | # Chew up leading and trailing whitespace
|
---|
| 121 | $webvar{input} =~ s/^\s+//;
|
---|
| 122 | $webvar{input} =~ s/\s+$//;
|
---|
[285] | 123 | if ($webvar{input} =~ /^\d+$/) {
|
---|
| 124 | # All-digits, new custID
|
---|
| 125 | $searchfor = "cust";
|
---|
| 126 | } elsif ($webvar{input} =~ /^[\d\.]+(\/\d{1,3})?$/) {
|
---|
[201] | 127 | # IP addresses should only have numbers, digits, and maybe a slash+netmask
|
---|
[197] | 128 | $searchfor = "ipblock";
|
---|
[930] | 129 | } elsif ($webvar{input} =~ /(?:^\d{6}\-|[A-Z][A-Z]\d\d\d)/) {
|
---|
| 130 | # Looks like part of a circuit ID
|
---|
| 131 | $searchfor = "circuitid";
|
---|
[197] | 132 | } else {
|
---|
| 133 | # Anything else.
|
---|
| 134 | $searchfor = "desc";
|
---|
| 135 | }
|
---|
| 136 | viewBy($searchfor, $webvar{input});
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | } elsif ($webvar{stype} eq 'c') {
|
---|
| 140 | # Complex search.
|
---|
| 141 |
|
---|
[201] | 142 | # Several major cases, and a whole raft of individual cases.
|
---|
| 143 | # -> Show all types means we do not need to limit records retrieved by type
|
---|
| 144 | # -> Show all cities means we do not need to limit records retrieved by city
|
---|
| 145 | # Individual cases are for the CIDR/IP, CustID, Description, Notes, and individual type
|
---|
| 146 | # requests.
|
---|
| 147 |
|
---|
[207] | 148 | my $sqlconcat;
|
---|
| 149 | if ($webvar{which} eq 'all') {
|
---|
| 150 | # Must match *all* specified criteria. ## use INTERSECT or EXCEPT
|
---|
| 151 | $sqlconcat = "INTERSECT";
|
---|
| 152 | } elsif ($webvar{which} eq 'any') {
|
---|
| 153 | # Match on any specified criteria ## use UNION
|
---|
| 154 | $sqlconcat = "UNION";
|
---|
| 155 | } else {
|
---|
[517] | 156 | # sum-buddy tryn'a game the system. Match "all"
|
---|
| 157 | $sqlconcat = "INTERSECT";
|
---|
[207] | 158 | }
|
---|
[197] | 159 |
|
---|
[202] | 160 | # We actually construct a monster SQL statement for all criteria.
|
---|
| 161 | # Iff something has been entered, it will be used as a filter.
|
---|
[208] | 162 | # Iff something has NOT been entered, we still include it but in
|
---|
| 163 | # such a way that it does not actually filter anything out.
|
---|
[201] | 164 |
|
---|
[521] | 165 | # hack fix for undefined variables
|
---|
| 166 | $webvar{custid} = '' if !$webvar{custid};
|
---|
| 167 | $webvar{desc} = '' if !$webvar{desc};
|
---|
| 168 | $webvar{notes} = '' if !$webvar{notes};
|
---|
| 169 | $webvar{custexclude} = '' if !$webvar{custexclude};
|
---|
| 170 | $webvar{descexclude} = '' if !$webvar{descexclude};
|
---|
| 171 | $webvar{notesexclude} = '' if !$webvar{notesexclude};
|
---|
| 172 |
|
---|
[207] | 173 | # First chunk of SQL. Filter on custid, description, and notes as necessary.
|
---|
[930] | 174 | # Putting newlines in the SQL so that any SQL logging is somewhat more readable
|
---|
| 175 | # than a gigantic long line of conditions.
|
---|
| 176 | my $sql = "$sqlbase\n";
|
---|
| 177 | my @bindargs;
|
---|
| 178 | if ($webvar{custid}) {
|
---|
| 179 | $sql .= " WHERE $webvar{custexclude} (s.custid ~ ?)\n";
|
---|
| 180 | push @bindargs, $webvar{custid};
|
---|
| 181 | }
|
---|
| 182 | if ($webvar{desc}) {
|
---|
| 183 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{descexclude} s.description ~* ?)\n";
|
---|
| 184 | push @bindargs, $webvar{desc};
|
---|
| 185 | }
|
---|
| 186 | if ($webvar{notes}) {
|
---|
| 187 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{notesexclude} s.notes ~ ?)";
|
---|
| 188 | push @bindargs, $webvar{notes};
|
---|
| 189 | }
|
---|
[201] | 190 |
|
---|
[207] | 191 | # If we're not supposed to search for all types, search for the selected types.
|
---|
[522] | 192 | $webvar{alltypes} = '' if !$webvar{alltypes};
|
---|
| 193 | $webvar{typeexclude} = '' if !$webvar{typeexclude};
|
---|
[207] | 194 | if ($webvar{alltypes} ne 'on') {
|
---|
[930] | 195 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{typeexclude} s.type IN (";
|
---|
[207] | 196 | foreach my $key (keys %webvar) {
|
---|
[930] | 197 | $sql .= "'$1'," if $key =~ /type\[(\w\w)\]/;
|
---|
[207] | 198 | }
|
---|
| 199 | chop $sql;
|
---|
| 200 | $sql .= "))";
|
---|
[201] | 201 | }
|
---|
| 202 |
|
---|
[207] | 203 | # If we're not supposed to search for all cities, search for the selected cities.
|
---|
| 204 | # This could be vastly improved with proper foreign keys in the database.
|
---|
[522] | 205 | $webvar{allcities} = '' if !$webvar{allcities};
|
---|
| 206 | $webvar{cityexclude} = '' if !$webvar{cityexclude};
|
---|
[207] | 207 | if ($webvar{allcities} ne 'on') {
|
---|
[930] | 208 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cityexclude} s.city IN (";
|
---|
[823] | 209 | $sth = $ip_dbh->prepare("SELECT city FROM cities WHERE id=?");
|
---|
[207] | 210 | foreach my $key (keys %webvar) {
|
---|
| 211 | if ($key =~ /city\[(\d+)\]/) {
|
---|
| 212 | $sth->execute($1);
|
---|
| 213 | my $city;
|
---|
| 214 | $sth->bind_columns(\$city);
|
---|
| 215 | $sth->fetch;
|
---|
| 216 | $city =~ s/'/''/;
|
---|
| 217 | $sql .= "'$city',";
|
---|
| 218 | }
|
---|
[201] | 219 | }
|
---|
[207] | 220 | chop $sql;
|
---|
| 221 | $sql .= "))";
|
---|
[201] | 222 | }
|
---|
| 223 |
|
---|
[207] | 224 | ## CIDR query options.
|
---|
| 225 | $webvar{cidr} =~ s/\s+//; # Hates the nasty spaceseseses we does.
|
---|
[351] | 226 | if ($webvar{cidr} eq '') { # We has a blank CIDR. Ignore it.
|
---|
[285] | 227 | } elsif ($webvar{cidr} =~ /\//) {
|
---|
[427] | 228 | # 192.168.179/26 should show all /26 subnets in 192.168.179
|
---|
[207] | 229 | my ($net,$maskbits) = split /\//, $webvar{cidr};
|
---|
| 230 | if ($webvar{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
|
---|
| 231 | # /0->/9 are silly to worry about right now. I don't think
|
---|
| 232 | # we'll be getting a class A anytime soon. <g>
|
---|
[930] | 233 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} s.cidr <<= ?)";
|
---|
| 234 | push @bindargs, $webvar{cidr};
|
---|
[207] | 235 | } else {
|
---|
| 236 | # Partial match; beginning of subnet and maskbits are provided
|
---|
| 237 | # Show any blocks with the leading octet(s) and that masklength
|
---|
[351] | 238 | # Need some more magic for bare /nn searches:
|
---|
[930] | 239 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} (masklen(s.cidr) = ?";
|
---|
| 240 | push @bindargs, $maskbits;
|
---|
| 241 | if ($net ne '') {
|
---|
| 242 | $sql .= " AND text(s.cidr) LIKE ?";
|
---|
| 243 | push @bindargs, $net;
|
---|
| 244 | }
|
---|
| 245 | $sql .= "))";
|
---|
[207] | 246 | }
|
---|
| 247 | } elsif ($webvar{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
|
---|
| 248 | # Specific IP address match. Will show either a single netblock,
|
---|
| 249 | # or a static pool plus an IP.
|
---|
[930] | 250 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} s.cidr >>= ?)";
|
---|
| 251 | push @bindargs, $webvar{cidr};
|
---|
[207] | 252 | } elsif ($webvar{cidr} =~ /^\d{1,3}(\.(\d{1,3}(\.(\d{1,3}\.?)?)?)?)?$/) {
|
---|
| 253 | # Leading octets in CIDR
|
---|
[930] | 254 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} text(s.cidr) LIKE ?)";
|
---|
| 255 | push @bindargs, "$webvar{cidr}%";
|
---|
[207] | 256 | } else {
|
---|
[517] | 257 | # do nothing.
|
---|
| 258 | ##fixme we'll ignore this to clear out the references to legacy code.
|
---|
[207] | 259 | } # done with CIDR query options.
|
---|
[201] | 260 |
|
---|
[207] | 261 | # Find the offset for multipage results
|
---|
| 262 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
[201] | 263 |
|
---|
[207] | 264 | # Find out how many rows the "core" query will return.
|
---|
[930] | 265 | my $count = countRows($sql, @bindargs);
|
---|
[201] | 266 |
|
---|
[207] | 267 | if ($count == 0) {
|
---|
[517] | 268 | $page->param(errmsg => "No matches found. Try eliminating one of the criteria,".
|
---|
| 269 | " or making one or more criteria more general.");
|
---|
[207] | 270 | } else {
|
---|
| 271 | # Add the limit/offset clauses
|
---|
[930] | 272 | $sql .= " ORDER BY s.cidr";
|
---|
| 273 | $sql .= " LIMIT $RESULTS_PER_PAGE OFFSET $offset" if $RESULTS_PER_PAGE != 0;
|
---|
[207] | 274 | # And tell the user.
|
---|
| 275 | print "<div class=heading>Searching...............</div>\n";
|
---|
[930] | 276 | queryResults($sql, $webvar{page}, $count, @bindargs);
|
---|
[207] | 277 | }
|
---|
[201] | 278 |
|
---|
[397] | 279 | } elsif ($webvar{stype} eq 'n') {
|
---|
| 280 | # Node search.
|
---|
| 281 |
|
---|
[930] | 282 | my $sql = "$sqlbase JOIN noderef nr ON nr.block=s.cidr WHERE nr.node_id = ?";
|
---|
[397] | 283 |
|
---|
| 284 | # Find the offset for multipage results
|
---|
| 285 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
| 286 |
|
---|
| 287 | # Find out how many rows the "core" query will return.
|
---|
[930] | 288 | my $count = countRows($sql, $webvar{node});
|
---|
[397] | 289 |
|
---|
[930] | 290 | my $nodename = getNodeName($ip_dbh, $webvar{node});
|
---|
| 291 |
|
---|
[397] | 292 | if ($count == 0) {
|
---|
[930] | 293 | $page->param(errmsg => "No customers currently listed as connected through $nodename.");
|
---|
[517] | 294 | ##fixme: still get the results table header
|
---|
[397] | 295 | } else {
|
---|
| 296 | # Add the limit/offset clauses
|
---|
[930] | 297 | $sql .= " ORDER BY cidr";
|
---|
| 298 | $sql .= " LIMIT $RESULTS_PER_PAGE OFFSET $offset" if $RESULTS_PER_PAGE != 0;
|
---|
[397] | 299 | # And tell the user.
|
---|
[930] | 300 | print "<div class=heading>Searching for assignments terminating on $nodename...</div>\n";
|
---|
| 301 | queryResults($sql, $webvar{page}, $count, $webvar{node});
|
---|
[397] | 302 | }
|
---|
| 303 |
|
---|
[207] | 304 | } else { # how script was called. General case is to show the search criteria page.
|
---|
[201] | 305 |
|
---|
[197] | 306 | # Generate table of types
|
---|
| 307 | $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ".
|
---|
| 308 | "order by listorder");
|
---|
| 309 | $sth->execute;
|
---|
| 310 | my $i=0;
|
---|
[517] | 311 | my @typelist;
|
---|
| 312 | while (my ($type,$dispname) = $sth->fetchrow_array) {
|
---|
| 313 | my %row = (
|
---|
| 314 | newrow => ($i % 4 == 0),
|
---|
| 315 | type => $type,
|
---|
| 316 | dispname => $dispname,
|
---|
| 317 | endrow => ($i++ % 4 == 3)
|
---|
| 318 | );
|
---|
| 319 | push @typelist, \%row;
|
---|
[197] | 320 | }
|
---|
[517] | 321 | $page->param(typelist => \@typelist);
|
---|
[197] | 322 |
|
---|
| 323 | # Generate table of cities
|
---|
| 324 | $sth = $ip_dbh->prepare("select id,city from cities order by city");
|
---|
| 325 | $sth->execute;
|
---|
[517] | 326 | $i=0;
|
---|
| 327 | my @citylist;
|
---|
| 328 | while (my ($id, $city) = $sth->fetchrow_array) {
|
---|
| 329 | my %row = (
|
---|
| 330 | newrow => ($i % 4 == 0),
|
---|
| 331 | id => $id,
|
---|
| 332 | city => $city,
|
---|
| 333 | endrow => ($i++ % 4 == 3)
|
---|
| 334 | );
|
---|
| 335 | push @citylist, \%row;
|
---|
[197] | 336 | }
|
---|
[517] | 337 | $page->param(citylist => \@citylist);
|
---|
[197] | 338 |
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[517] | 341 | print $page->output;
|
---|
| 342 |
|
---|
[197] | 343 | # Shut down and clean up.
|
---|
| 344 | finish($ip_dbh);
|
---|
[517] | 345 |
|
---|
| 346 | # We print the footer here, so we don't have to do it elsewhere.
|
---|
[801] | 347 | my $footer = HTML::Template->new(filename => "footer.tmpl", path => @templatepath);
|
---|
[517] | 348 | # include the admin tools link in the output?
|
---|
| 349 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
|
---|
| 350 |
|
---|
| 351 | print $footer->output;
|
---|
| 352 |
|
---|
[197] | 353 | # We shouldn't need to directly execute any code below here; it's all subroutines.
|
---|
| 354 | exit 0;
|
---|
| 355 |
|
---|
[207] | 356 |
|
---|
| 357 | # viewBy()
|
---|
| 358 | # The quick search
|
---|
| 359 | # Takes a category descriptor and a query string
|
---|
| 360 | # Creates appropriate SQL to run the search and display the results
|
---|
| 361 | # with queryResults()
|
---|
[520] | 362 | sub viewBy {
|
---|
[197] | 363 | my ($category,$query) = @_;
|
---|
| 364 |
|
---|
| 365 | # Local variables
|
---|
| 366 | my $sql;
|
---|
| 367 |
|
---|
| 368 | # Calculate start point for LIMIT clause
|
---|
| 369 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
[930] | 370 | ##fixme: squeeze ORDER BY etc out into somewhere common, or at least an
|
---|
| 371 | # includeable bit instead of hardcoding in each block
|
---|
[197] | 372 |
|
---|
| 373 | if ($category eq 'all') {
|
---|
| 374 |
|
---|
[930] | 375 | # Sort of pointless, just horks up everything.
|
---|
| 376 | $sql = "$sqlbase";
|
---|
[202] | 377 | my $count = countRows($sql);
|
---|
[930] | 378 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
[197] | 379 | queryResults($sql, $webvar{page}, $count);
|
---|
| 380 |
|
---|
| 381 | } elsif ($category eq 'cust') {
|
---|
| 382 |
|
---|
[517] | 383 | ##fixme: this and other quick-search areas; fix up page heading title similar to first grouping above
|
---|
[197] | 384 | print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
|
---|
| 385 |
|
---|
| 386 | # Query for a customer ID. Note that we can't restrict to "numeric-only"
|
---|
| 387 | # as we have non-numeric custIDs in the legacy data. :/
|
---|
[930] | 388 | $sql = "$sqlbase WHERE s.custid ~* ? OR s.description ~* ?";
|
---|
| 389 | my $count = countRows($sql, $query, $query);
|
---|
| 390 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
| 391 | queryResults($sql, $webvar{page}, $count, $query, $query);
|
---|
[197] | 392 |
|
---|
| 393 | } elsif ($category eq 'desc') {
|
---|
| 394 |
|
---|
[930] | 395 | print qq(<div class="heading">Searching for description, customer ID, or circuit ID matching '$query'</div><br>\n);
|
---|
[197] | 396 | # Query based on description (includes "name" from old DB).
|
---|
[930] | 397 | $sql = "$sqlbase WHERE s.description ~* ? OR s.custid ~* ? OR s.circuitid ~* ?";
|
---|
| 398 | my $count = countRows($sql, $query, $query, $query);
|
---|
| 399 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
| 400 | queryResults($sql, $webvar{page}, $count, $query, $query, $query);
|
---|
[197] | 401 |
|
---|
[930] | 402 | } elsif ($category eq 'circuitid') {
|
---|
| 403 |
|
---|
| 404 | print qq(<div class="heading">Searching for allocations with circuit ID matching '$query'</div><br>\n);
|
---|
| 405 | # Pretty similar to description and cust searches above, but focus on circuit ID
|
---|
| 406 | # JOIN needed for VRF field
|
---|
| 407 | $sql = "$sqlbase WHERE s.circuitid ~* ? OR s.description ~* ?";
|
---|
| 408 | my $count = countRows($sql, $query, $query);
|
---|
| 409 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
| 410 | queryResults($sql, $webvar{page}, $count, $query, $query);
|
---|
| 411 |
|
---|
[197] | 412 | } elsif ($category =~ /ipblock/) {
|
---|
| 413 |
|
---|
| 414 | # Query is for a partial IP, a CIDR block in some form, or a flat IP.
|
---|
| 415 | print qq(<div class="heading">Searching for IP-based matches on '$query'</div><br>\n);
|
---|
| 416 |
|
---|
| 417 | $query =~ s/\s+//g;
|
---|
| 418 | if ($query =~ /\//) {
|
---|
[427] | 419 | # 192.168.179/26 should show all /26 subnets in 192.168.179
|
---|
[197] | 420 | my ($net,$maskbits) = split /\//, $query;
|
---|
| 421 | if ($query =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
|
---|
| 422 | # /0->/9 are silly to worry about right now. I don't think
|
---|
| 423 | # we'll be getting a class A anytime soon. <g>
|
---|
[930] | 424 | $sql = "$sqlbase WHERE s.cidr = ?";
|
---|
| 425 | queryResults($sql, $webvar{page}, 1, $query);
|
---|
[197] | 426 | } else {
|
---|
[289] | 427 | #print "Finding all blocks with netmask /$maskbits, leading octet(s) $net<br>\n";
|
---|
[197] | 428 | # Partial match; beginning of subnet and maskbits are provided
|
---|
[930] | 429 | $sql = "$sqlbase WHERE text(s.cidr) LIKE ? AND text(s.cidr) LIKE ?";
|
---|
| 430 | my $count = countRows($sql, "$net%", "%$maskbits");
|
---|
| 431 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
| 432 | queryResults($sql, $webvar{page}, $count, "$net%", "%$maskbits");
|
---|
[197] | 433 | }
|
---|
[930] | 434 |
|
---|
[197] | 435 | } elsif ($query =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
|
---|
| 436 | # Specific IP address match
|
---|
[289] | 437 | #print "4-octet pattern found; finding netblock containing IP $query<br>\n";
|
---|
[197] | 438 | my ($net,$ip) = ($query =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/);
|
---|
| 439 | my $sfor = new NetAddr::IP $query;
|
---|
[930] | 440 | $sql = "$sqlbase WHERE s.cidr >>= ? AND s.type <> 'mm'";
|
---|
| 441 | my $count = countRows($sql, $sfor);
|
---|
| 442 | $sql .= " ORDER BY masklen(s.cidr) DESC";
|
---|
| 443 | queryResults($sql, $webvar{page}, $count, $sfor);
|
---|
[823] | 444 |
|
---|
[202] | 445 | } elsif ($query =~ /^(\d{1,3}\.){1,3}\d{1,3}\.?$/) {
|
---|
[289] | 446 | #print "Finding matches with leading octet(s) $query<br>\n";
|
---|
[930] | 447 | $sql = "$sqlbase WHERE text(s.cidr) LIKE ?";
|
---|
| 448 | my $count = countRows($sql, "$query%");
|
---|
| 449 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
| 450 | queryResults($sql, $webvar{page}, $count, "$query%");
|
---|
[197] | 451 | } else {
|
---|
| 452 | # This shouldn't happen, but if it does, whoever gets it deserves what they get...
|
---|
[517] | 453 | $page->param(errmsg => "Invalid query.");
|
---|
[197] | 454 | }
|
---|
| 455 | } else {
|
---|
| 456 | # This shouldn't happen, but if it does, whoever gets it deserves what they get...
|
---|
[517] | 457 | $page->param(errmsg => "Invalid searchfor.");
|
---|
[197] | 458 | }
|
---|
| 459 | } # viewBy
|
---|
| 460 |
|
---|
| 461 |
|
---|
| 462 |
|
---|
[207] | 463 | # queryResults()
|
---|
| 464 | # Display search queries based on the passed SQL.
|
---|
| 465 | # Takes SQL, page number (for multipage search results), and a total count.
|
---|
[520] | 466 | sub queryResults {
|
---|
[930] | 467 | my $sql = shift;
|
---|
| 468 | my $pageNo = shift;
|
---|
| 469 | my $rowCount = shift;
|
---|
| 470 | my @bindargs = @_;
|
---|
| 471 |
|
---|
[197] | 472 | my $offset = 0;
|
---|
[930] | 473 | $offset = $1 if($sql =~ m/.*LIMIT\s+(.*),.*/);
|
---|
[197] | 474 |
|
---|
| 475 | my $sth = $ip_dbh->prepare($sql);
|
---|
[930] | 476 | $sth->execute(@bindargs);
|
---|
[197] | 477 |
|
---|
[517] | 478 | $page->param(searchtitle => "Showing all netblock and static-IP allocations");
|
---|
| 479 |
|
---|
[197] | 480 | my $count = 0;
|
---|
[517] | 481 | my @sresults;
|
---|
[823] | 482 | while (my ($block, $custid, $type, $city, $desc, $id, $parent, $avail, $vrf) = $sth->fetchrow_array) {
|
---|
[517] | 483 | my %row = (
|
---|
| 484 | rowclass => $count++ % 2,
|
---|
[823] | 485 | vrf => $vrf,
|
---|
[517] | 486 | issub => ($type =~ /^.r$/ ? 1 : 0),
|
---|
[670] | 487 | ispool => ($type =~ /^.[pd]$/ ? 1 : 0),
|
---|
| 488 | basetype => ($type =~ /^.i/ ? 'i' : 'b'),
|
---|
| 489 | freeip => ($avail eq 'y'),
|
---|
| 490 | parent => $parent,
|
---|
[517] | 491 | block => $block,
|
---|
| 492 | custid => $custid,
|
---|
| 493 | disptype => $disp_alloctypes{$type},
|
---|
| 494 | city => $city,
|
---|
[670] | 495 | desc => $desc,
|
---|
| 496 | id => $id,
|
---|
[517] | 497 | );
|
---|
| 498 | push @sresults, \%row;
|
---|
[197] | 499 | }
|
---|
[517] | 500 | $page->param(sresults => \@sresults);
|
---|
[197] | 501 |
|
---|
| 502 | # Have to think on this call, it's primarily to clean up unfetched rows from a select.
|
---|
| 503 | # In this context it's probably a good idea.
|
---|
| 504 | $sth->finish();
|
---|
| 505 |
|
---|
| 506 | my $upper = $offset+$count;
|
---|
| 507 |
|
---|
[517] | 508 | $page->param(resfound => $rowCount);
|
---|
| 509 | $page->param(resstart => $offset+1);
|
---|
| 510 | $page->param(resstop => $upper);
|
---|
| 511 |
|
---|
[197] | 512 | # print the page thing..
|
---|
[370] | 513 | if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) {
|
---|
[517] | 514 | $page->param(multipage => 1);
|
---|
[197] | 515 | my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
|
---|
[517] | 516 | my @pagelist;
|
---|
[197] | 517 | for (my $i = 1; $i <= $pages; $i++) {
|
---|
[517] | 518 | my %row;
|
---|
| 519 | $row{pgnum} = $i;
|
---|
[197] | 520 | if ($i == $pageNo) {
|
---|
[517] | 521 | $row{thispage} = 1;
|
---|
[197] | 522 | } else {
|
---|
[517] | 523 | $row{stype} = $webvar{stype};
|
---|
[202] | 524 | if ($webvar{stype} eq 'c') {
|
---|
[517] | 525 | $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
|
---|
[202] | 526 | "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&".
|
---|
| 527 | "allcities=$webvar{allcities}&";
|
---|
| 528 | foreach my $key (keys %webvar) {
|
---|
[351] | 529 | if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) {
|
---|
[517] | 530 | $row{extraopts} .= "$key=$webvar{$key}&";
|
---|
[202] | 531 | }
|
---|
| 532 | }
|
---|
| 533 | } else {
|
---|
[517] | 534 | $row{extraopts} = "input=$webvar{input}&";
|
---|
[202] | 535 | }
|
---|
[197] | 536 | }
|
---|
[517] | 537 | push @pagelist, \%row;
|
---|
[197] | 538 | }
|
---|
[517] | 539 | $page->param(pgnums => \@pagelist);
|
---|
[197] | 540 | }
|
---|
[517] | 541 |
|
---|
[197] | 542 | } # queryResults
|
---|
| 543 |
|
---|
| 544 |
|
---|
| 545 |
|
---|
[202] | 546 | # Return count of rows to be returned in a "real" query
|
---|
| 547 | # with the passed SQL statement
|
---|
[520] | 548 | sub countRows {
|
---|
[930] | 549 | my $sql = shift;
|
---|
| 550 |
|
---|
[202] | 551 | # Note that the "as foo" is required
|
---|
[930] | 552 | my @a = $ip_dbh->selectrow_array("SELECT count(*) FROM ($sql) AS foo", undef, @_);
|
---|
[197] | 553 | return $a[0];
|
---|
| 554 | }
|
---|