[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-21 16:48:30 +0000 (Fri, 21 Oct 2022) $
|
---|
| 8 | # SVN revision $Rev: 931 $
|
---|
| 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:
|
---|
[931] | 239 | $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} (masklen(s.cidr)) = ?";
|
---|
[930] | 240 | push @bindargs, $maskbits;
|
---|
| 241 | if ($net ne '') {
|
---|
| 242 | $sql .= " AND text(s.cidr) LIKE ?";
|
---|
[931] | 243 | push @bindargs, "$net%";
|
---|
[930] | 244 | }
|
---|
[931] | 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
|
---|
[931] | 272 | # note ORDER BY needs to NOT reference the table alias s as in $sqlbase because Reasons
|
---|
| 273 | $sql .= " ORDER BY cidr";
|
---|
[930] | 274 | $sql .= " LIMIT $RESULTS_PER_PAGE OFFSET $offset" if $RESULTS_PER_PAGE != 0;
|
---|
[207] | 275 | # And tell the user.
|
---|
| 276 | print "<div class=heading>Searching...............</div>\n";
|
---|
[930] | 277 | queryResults($sql, $webvar{page}, $count, @bindargs);
|
---|
[207] | 278 | }
|
---|
[201] | 279 |
|
---|
[397] | 280 | } elsif ($webvar{stype} eq 'n') {
|
---|
| 281 | # Node search.
|
---|
| 282 |
|
---|
[930] | 283 | my $sql = "$sqlbase JOIN noderef nr ON nr.block=s.cidr WHERE nr.node_id = ?";
|
---|
[397] | 284 |
|
---|
| 285 | # Find the offset for multipage results
|
---|
| 286 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
| 287 |
|
---|
| 288 | # Find out how many rows the "core" query will return.
|
---|
[930] | 289 | my $count = countRows($sql, $webvar{node});
|
---|
[397] | 290 |
|
---|
[930] | 291 | my $nodename = getNodeName($ip_dbh, $webvar{node});
|
---|
| 292 |
|
---|
[397] | 293 | if ($count == 0) {
|
---|
[930] | 294 | $page->param(errmsg => "No customers currently listed as connected through $nodename.");
|
---|
[517] | 295 | ##fixme: still get the results table header
|
---|
[397] | 296 | } else {
|
---|
| 297 | # Add the limit/offset clauses
|
---|
[930] | 298 | $sql .= " ORDER BY cidr";
|
---|
| 299 | $sql .= " LIMIT $RESULTS_PER_PAGE OFFSET $offset" if $RESULTS_PER_PAGE != 0;
|
---|
[397] | 300 | # And tell the user.
|
---|
[930] | 301 | print "<div class=heading>Searching for assignments terminating on $nodename...</div>\n";
|
---|
| 302 | queryResults($sql, $webvar{page}, $count, $webvar{node});
|
---|
[397] | 303 | }
|
---|
| 304 |
|
---|
[207] | 305 | } else { # how script was called. General case is to show the search criteria page.
|
---|
[201] | 306 |
|
---|
[197] | 307 | # Generate table of types
|
---|
| 308 | $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ".
|
---|
| 309 | "order by listorder");
|
---|
| 310 | $sth->execute;
|
---|
| 311 | my $i=0;
|
---|
[517] | 312 | my @typelist;
|
---|
| 313 | while (my ($type,$dispname) = $sth->fetchrow_array) {
|
---|
| 314 | my %row = (
|
---|
| 315 | newrow => ($i % 4 == 0),
|
---|
| 316 | type => $type,
|
---|
| 317 | dispname => $dispname,
|
---|
| 318 | endrow => ($i++ % 4 == 3)
|
---|
| 319 | );
|
---|
| 320 | push @typelist, \%row;
|
---|
[197] | 321 | }
|
---|
[517] | 322 | $page->param(typelist => \@typelist);
|
---|
[197] | 323 |
|
---|
| 324 | # Generate table of cities
|
---|
| 325 | $sth = $ip_dbh->prepare("select id,city from cities order by city");
|
---|
| 326 | $sth->execute;
|
---|
[517] | 327 | $i=0;
|
---|
| 328 | my @citylist;
|
---|
| 329 | while (my ($id, $city) = $sth->fetchrow_array) {
|
---|
| 330 | my %row = (
|
---|
| 331 | newrow => ($i % 4 == 0),
|
---|
| 332 | id => $id,
|
---|
| 333 | city => $city,
|
---|
| 334 | endrow => ($i++ % 4 == 3)
|
---|
| 335 | );
|
---|
| 336 | push @citylist, \%row;
|
---|
[197] | 337 | }
|
---|
[517] | 338 | $page->param(citylist => \@citylist);
|
---|
[197] | 339 |
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[517] | 342 | print $page->output;
|
---|
| 343 |
|
---|
[197] | 344 | # Shut down and clean up.
|
---|
| 345 | finish($ip_dbh);
|
---|
[517] | 346 |
|
---|
| 347 | # We print the footer here, so we don't have to do it elsewhere.
|
---|
[801] | 348 | my $footer = HTML::Template->new(filename => "footer.tmpl", path => @templatepath);
|
---|
[517] | 349 | # include the admin tools link in the output?
|
---|
| 350 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
|
---|
| 351 |
|
---|
| 352 | print $footer->output;
|
---|
| 353 |
|
---|
[197] | 354 | # We shouldn't need to directly execute any code below here; it's all subroutines.
|
---|
| 355 | exit 0;
|
---|
| 356 |
|
---|
[207] | 357 |
|
---|
| 358 | # viewBy()
|
---|
| 359 | # The quick search
|
---|
| 360 | # Takes a category descriptor and a query string
|
---|
| 361 | # Creates appropriate SQL to run the search and display the results
|
---|
| 362 | # with queryResults()
|
---|
[520] | 363 | sub viewBy {
|
---|
[197] | 364 | my ($category,$query) = @_;
|
---|
| 365 |
|
---|
| 366 | # Local variables
|
---|
| 367 | my $sql;
|
---|
| 368 |
|
---|
| 369 | # Calculate start point for LIMIT clause
|
---|
| 370 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
|
---|
[930] | 371 | ##fixme: squeeze ORDER BY etc out into somewhere common, or at least an
|
---|
| 372 | # includeable bit instead of hardcoding in each block
|
---|
[197] | 373 |
|
---|
| 374 | if ($category eq 'all') {
|
---|
| 375 |
|
---|
[930] | 376 | # Sort of pointless, just horks up everything.
|
---|
| 377 | $sql = "$sqlbase";
|
---|
[202] | 378 | my $count = countRows($sql);
|
---|
[930] | 379 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
[197] | 380 | queryResults($sql, $webvar{page}, $count);
|
---|
| 381 |
|
---|
| 382 | } elsif ($category eq 'cust') {
|
---|
| 383 |
|
---|
[517] | 384 | ##fixme: this and other quick-search areas; fix up page heading title similar to first grouping above
|
---|
[197] | 385 | print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n);
|
---|
| 386 |
|
---|
| 387 | # Query for a customer ID. Note that we can't restrict to "numeric-only"
|
---|
| 388 | # as we have non-numeric custIDs in the legacy data. :/
|
---|
[930] | 389 | $sql = "$sqlbase WHERE s.custid ~* ? OR s.description ~* ?";
|
---|
| 390 | my $count = countRows($sql, $query, $query);
|
---|
| 391 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
| 392 | queryResults($sql, $webvar{page}, $count, $query, $query);
|
---|
[197] | 393 |
|
---|
| 394 | } elsif ($category eq 'desc') {
|
---|
| 395 |
|
---|
[930] | 396 | print qq(<div class="heading">Searching for description, customer ID, or circuit ID matching '$query'</div><br>\n);
|
---|
[197] | 397 | # Query based on description (includes "name" from old DB).
|
---|
[930] | 398 | $sql = "$sqlbase WHERE s.description ~* ? OR s.custid ~* ? OR s.circuitid ~* ?";
|
---|
| 399 | my $count = countRows($sql, $query, $query, $query);
|
---|
| 400 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
| 401 | queryResults($sql, $webvar{page}, $count, $query, $query, $query);
|
---|
[197] | 402 |
|
---|
[930] | 403 | } elsif ($category eq 'circuitid') {
|
---|
| 404 |
|
---|
| 405 | print qq(<div class="heading">Searching for allocations with circuit ID matching '$query'</div><br>\n);
|
---|
| 406 | # Pretty similar to description and cust searches above, but focus on circuit ID
|
---|
| 407 | # JOIN needed for VRF field
|
---|
| 408 | $sql = "$sqlbase WHERE s.circuitid ~* ? OR s.description ~* ?";
|
---|
| 409 | my $count = countRows($sql, $query, $query);
|
---|
| 410 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
| 411 | queryResults($sql, $webvar{page}, $count, $query, $query);
|
---|
| 412 |
|
---|
[197] | 413 | } elsif ($category =~ /ipblock/) {
|
---|
| 414 |
|
---|
| 415 | # Query is for a partial IP, a CIDR block in some form, or a flat IP.
|
---|
| 416 | print qq(<div class="heading">Searching for IP-based matches on '$query'</div><br>\n);
|
---|
| 417 |
|
---|
| 418 | $query =~ s/\s+//g;
|
---|
| 419 | if ($query =~ /\//) {
|
---|
[427] | 420 | # 192.168.179/26 should show all /26 subnets in 192.168.179
|
---|
[197] | 421 | my ($net,$maskbits) = split /\//, $query;
|
---|
| 422 | if ($query =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
|
---|
| 423 | # /0->/9 are silly to worry about right now. I don't think
|
---|
| 424 | # we'll be getting a class A anytime soon. <g>
|
---|
[930] | 425 | $sql = "$sqlbase WHERE s.cidr = ?";
|
---|
| 426 | queryResults($sql, $webvar{page}, 1, $query);
|
---|
[197] | 427 | } else {
|
---|
[289] | 428 | #print "Finding all blocks with netmask /$maskbits, leading octet(s) $net<br>\n";
|
---|
[197] | 429 | # Partial match; beginning of subnet and maskbits are provided
|
---|
[930] | 430 | $sql = "$sqlbase WHERE text(s.cidr) LIKE ? AND text(s.cidr) LIKE ?";
|
---|
| 431 | my $count = countRows($sql, "$net%", "%$maskbits");
|
---|
| 432 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
| 433 | queryResults($sql, $webvar{page}, $count, "$net%", "%$maskbits");
|
---|
[197] | 434 | }
|
---|
[930] | 435 |
|
---|
[197] | 436 | } elsif ($query =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
|
---|
| 437 | # Specific IP address match
|
---|
[289] | 438 | #print "4-octet pattern found; finding netblock containing IP $query<br>\n";
|
---|
[197] | 439 | my ($net,$ip) = ($query =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/);
|
---|
| 440 | my $sfor = new NetAddr::IP $query;
|
---|
[930] | 441 | $sql = "$sqlbase WHERE s.cidr >>= ? AND s.type <> 'mm'";
|
---|
| 442 | my $count = countRows($sql, $sfor);
|
---|
| 443 | $sql .= " ORDER BY masklen(s.cidr) DESC";
|
---|
| 444 | queryResults($sql, $webvar{page}, $count, $sfor);
|
---|
[823] | 445 |
|
---|
[202] | 446 | } elsif ($query =~ /^(\d{1,3}\.){1,3}\d{1,3}\.?$/) {
|
---|
[289] | 447 | #print "Finding matches with leading octet(s) $query<br>\n";
|
---|
[930] | 448 | $sql = "$sqlbase WHERE text(s.cidr) LIKE ?";
|
---|
| 449 | my $count = countRows($sql, "$query%");
|
---|
| 450 | $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
|
---|
| 451 | queryResults($sql, $webvar{page}, $count, "$query%");
|
---|
[197] | 452 | } else {
|
---|
| 453 | # This shouldn't happen, but if it does, whoever gets it deserves what they get...
|
---|
[517] | 454 | $page->param(errmsg => "Invalid query.");
|
---|
[197] | 455 | }
|
---|
| 456 | } else {
|
---|
| 457 | # This shouldn't happen, but if it does, whoever gets it deserves what they get...
|
---|
[517] | 458 | $page->param(errmsg => "Invalid searchfor.");
|
---|
[197] | 459 | }
|
---|
| 460 | } # viewBy
|
---|
| 461 |
|
---|
| 462 |
|
---|
| 463 |
|
---|
[207] | 464 | # queryResults()
|
---|
| 465 | # Display search queries based on the passed SQL.
|
---|
| 466 | # Takes SQL, page number (for multipage search results), and a total count.
|
---|
[520] | 467 | sub queryResults {
|
---|
[930] | 468 | my $sql = shift;
|
---|
| 469 | my $pageNo = shift;
|
---|
| 470 | my $rowCount = shift;
|
---|
| 471 | my @bindargs = @_;
|
---|
| 472 |
|
---|
[197] | 473 | my $offset = 0;
|
---|
[930] | 474 | $offset = $1 if($sql =~ m/.*LIMIT\s+(.*),.*/);
|
---|
[197] | 475 |
|
---|
| 476 | my $sth = $ip_dbh->prepare($sql);
|
---|
[930] | 477 | $sth->execute(@bindargs);
|
---|
[197] | 478 |
|
---|
[517] | 479 | $page->param(searchtitle => "Showing all netblock and static-IP allocations");
|
---|
| 480 |
|
---|
[197] | 481 | my $count = 0;
|
---|
[517] | 482 | my @sresults;
|
---|
[823] | 483 | while (my ($block, $custid, $type, $city, $desc, $id, $parent, $avail, $vrf) = $sth->fetchrow_array) {
|
---|
[517] | 484 | my %row = (
|
---|
| 485 | rowclass => $count++ % 2,
|
---|
[823] | 486 | vrf => $vrf,
|
---|
[517] | 487 | issub => ($type =~ /^.r$/ ? 1 : 0),
|
---|
[670] | 488 | ispool => ($type =~ /^.[pd]$/ ? 1 : 0),
|
---|
| 489 | basetype => ($type =~ /^.i/ ? 'i' : 'b'),
|
---|
| 490 | freeip => ($avail eq 'y'),
|
---|
| 491 | parent => $parent,
|
---|
[517] | 492 | block => $block,
|
---|
| 493 | custid => $custid,
|
---|
| 494 | disptype => $disp_alloctypes{$type},
|
---|
| 495 | city => $city,
|
---|
[670] | 496 | desc => $desc,
|
---|
| 497 | id => $id,
|
---|
[517] | 498 | );
|
---|
| 499 | push @sresults, \%row;
|
---|
[197] | 500 | }
|
---|
[517] | 501 | $page->param(sresults => \@sresults);
|
---|
[197] | 502 |
|
---|
| 503 | # Have to think on this call, it's primarily to clean up unfetched rows from a select.
|
---|
| 504 | # In this context it's probably a good idea.
|
---|
| 505 | $sth->finish();
|
---|
| 506 |
|
---|
| 507 | my $upper = $offset+$count;
|
---|
| 508 |
|
---|
[517] | 509 | $page->param(resfound => $rowCount);
|
---|
| 510 | $page->param(resstart => $offset+1);
|
---|
| 511 | $page->param(resstop => $upper);
|
---|
| 512 |
|
---|
[197] | 513 | # print the page thing..
|
---|
[370] | 514 | if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) {
|
---|
[517] | 515 | $page->param(multipage => 1);
|
---|
[197] | 516 | my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
|
---|
[517] | 517 | my @pagelist;
|
---|
[197] | 518 | for (my $i = 1; $i <= $pages; $i++) {
|
---|
[517] | 519 | my %row;
|
---|
| 520 | $row{pgnum} = $i;
|
---|
[197] | 521 | if ($i == $pageNo) {
|
---|
[517] | 522 | $row{thispage} = 1;
|
---|
[197] | 523 | } else {
|
---|
[517] | 524 | $row{stype} = $webvar{stype};
|
---|
[202] | 525 | if ($webvar{stype} eq 'c') {
|
---|
[517] | 526 | $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
|
---|
[202] | 527 | "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&".
|
---|
| 528 | "allcities=$webvar{allcities}&";
|
---|
| 529 | foreach my $key (keys %webvar) {
|
---|
[351] | 530 | if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) {
|
---|
[517] | 531 | $row{extraopts} .= "$key=$webvar{$key}&";
|
---|
[202] | 532 | }
|
---|
| 533 | }
|
---|
| 534 | } else {
|
---|
[517] | 535 | $row{extraopts} = "input=$webvar{input}&";
|
---|
[202] | 536 | }
|
---|
[197] | 537 | }
|
---|
[517] | 538 | push @pagelist, \%row;
|
---|
[197] | 539 | }
|
---|
[517] | 540 | $page->param(pgnums => \@pagelist);
|
---|
[197] | 541 | }
|
---|
[517] | 542 |
|
---|
[197] | 543 | } # queryResults
|
---|
| 544 |
|
---|
| 545 |
|
---|
| 546 |
|
---|
[202] | 547 | # Return count of rows to be returned in a "real" query
|
---|
| 548 | # with the passed SQL statement
|
---|
[520] | 549 | sub countRows {
|
---|
[930] | 550 | my $sql = shift;
|
---|
| 551 |
|
---|
[202] | 552 | # Note that the "as foo" is required
|
---|
[930] | 553 | my @a = $ip_dbh->selectrow_array("SELECT count(*) FROM ($sql) AS foo", undef, @_);
|
---|
[197] | 554 | return $a[0];
|
---|
| 555 | }
|
---|