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