| [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: 2011-09-27 17:41:43 +0000 (Tue, 27 Sep 2011) $ | 
|---|
|  | 8 | # SVN revision $Rev: 505 $ | 
|---|
|  | 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); | 
|---|
| [447] | 16 | use CGI::Simple; | 
|---|
| [449] | 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 |  | 
|---|
| [197] | 41 | # Global variables | 
|---|
| [351] | 42 | my $RESULTS_PER_PAGE = 25; | 
|---|
| [197] | 43 |  | 
|---|
| [481] | 44 | # anyone got a better name?  :P | 
|---|
|  | 45 | my $thingroot = $ENV{SCRIPT_FILENAME}; | 
|---|
|  | 46 | $thingroot =~ s|cgi-bin/search.cgi||; | 
|---|
|  | 47 |  | 
|---|
| [447] | 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 |  | 
|---|
| [370] | 57 | if (defined($webvar{rpp})) { | 
|---|
|  | 58 | ($RESULTS_PER_PAGE) = ($webvar{rpp} =~ /(\d+)/); | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
| [502] | 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; | 
|---|
|  | 67 | if ($ip_dbh) { | 
|---|
|  | 68 | checkDBSanity($ip_dbh); | 
|---|
|  | 69 | initIPDBGlobals($ip_dbh); | 
|---|
|  | 70 | } | 
|---|
|  | 71 |  | 
|---|
| [481] | 72 | # Set up some globals | 
|---|
|  | 73 | $ENV{HTML_TEMPLATE_ROOT} = $thingroot."templates"; | 
|---|
|  | 74 |  | 
|---|
|  | 75 | my $page; | 
|---|
| [197] | 76 | if (!defined($webvar{stype})) { | 
|---|
|  | 77 | $webvar{stype} = "<NULL>";   #shuts up the warnings. | 
|---|
| [481] | 78 | $page = HTML::Template->new(filename => "search/compsearch.tmpl"); | 
|---|
|  | 79 | } else { | 
|---|
|  | 80 | $page = HTML::Template->new(filename => "search/sresults.tmpl"); | 
|---|
| [197] | 81 | } | 
|---|
|  | 82 |  | 
|---|
| [451] | 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 |  | 
|---|
| [502] | 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 { | 
|---|
| [502] | 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 |  | 
|---|
| [207] | 148 | # First chunk of SQL.  Filter on custid, description, and notes as necessary. | 
|---|
| [351] | 149 | my $sql = "(select $cols from searchme where". | 
|---|
|  | 150 | " $webvar{custexclude} (custid ilike '%$webvar{custid}%'". | 
|---|
| [201] | 151 | " $sqlconcat (select $cols from searchme where $webvar{descexclude} description ilike '%$webvar{desc}%')". | 
|---|
|  | 152 | " $sqlconcat (select $cols from searchme where $webvar{notesexclude} notes ilike '%$webvar{notes}%')"; | 
|---|
|  | 153 |  | 
|---|
| [207] | 154 | # If we're not supposed to search for all types, search for the selected types. | 
|---|
|  | 155 | if ($webvar{alltypes} ne 'on') { | 
|---|
|  | 156 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{typeexclude} type in ("; | 
|---|
|  | 157 | foreach my $key (keys %webvar) { | 
|---|
|  | 158 | $sql .= "'$1'," if $key =~ /type\[(..)\]/; | 
|---|
|  | 159 | } | 
|---|
|  | 160 | chop $sql; | 
|---|
|  | 161 | $sql .= "))"; | 
|---|
| [201] | 162 | } | 
|---|
|  | 163 |  | 
|---|
| [207] | 164 | # If we're not supposed to search for all cities, search for the selected cities. | 
|---|
|  | 165 | # This could be vastly improved with proper foreign keys in the database. | 
|---|
|  | 166 | if ($webvar{allcities} ne 'on') { | 
|---|
|  | 167 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{cityexclude} city in ("; | 
|---|
|  | 168 | $sth = $ip_dbh->prepare("select city from cities where id=?"); | 
|---|
|  | 169 | foreach my $key (keys %webvar) { | 
|---|
|  | 170 | if ($key =~ /city\[(\d+)\]/) { | 
|---|
|  | 171 | $sth->execute($1); | 
|---|
|  | 172 | my $city; | 
|---|
|  | 173 | $sth->bind_columns(\$city); | 
|---|
|  | 174 | $sth->fetch; | 
|---|
|  | 175 | $city =~ s/'/''/; | 
|---|
|  | 176 | $sql .= "'$city',"; | 
|---|
|  | 177 | } | 
|---|
| [201] | 178 | } | 
|---|
| [207] | 179 | chop $sql; | 
|---|
|  | 180 | $sql .= "))"; | 
|---|
| [201] | 181 | } | 
|---|
|  | 182 |  | 
|---|
| [207] | 183 | ## CIDR query options. | 
|---|
|  | 184 | $webvar{cidr} =~ s/\s+//;     # Hates the nasty spaceseseses we does. | 
|---|
| [351] | 185 | if ($webvar{cidr} eq '') { # We has a blank CIDR.  Ignore it. | 
|---|
| [285] | 186 | } elsif ($webvar{cidr} =~ /\//) { | 
|---|
| [427] | 187 | # 192.168.179/26 should show all /26 subnets in 192.168.179 | 
|---|
| [207] | 188 | my ($net,$maskbits) = split /\//, $webvar{cidr}; | 
|---|
|  | 189 | if ($webvar{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) { | 
|---|
|  | 190 | # /0->/9 are silly to worry about right now.  I don't think | 
|---|
|  | 191 | # we'll be getting a class A anytime soon.  <g> | 
|---|
|  | 192 | $sql .= " $sqlconcat (select $cols from searchme where ". | 
|---|
| [351] | 193 | "$webvar{cidrexclude} cidr<<='$webvar{cidr}')"; | 
|---|
| [207] | 194 | } else { | 
|---|
|  | 195 | # Partial match;  beginning of subnet and maskbits are provided | 
|---|
|  | 196 | # Show any blocks with the leading octet(s) and that masklength | 
|---|
| [351] | 197 | # Need some more magic for bare /nn searches: | 
|---|
|  | 198 | my $condition = ($net eq '' ? | 
|---|
|  | 199 | "masklen(cidr)=$maskbits" : "text(cidr) like '$net%' and masklen(cidr)=$maskbits"); | 
|---|
| [207] | 200 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{cidrexclude} ". | 
|---|
| [351] | 201 | "($condition))"; | 
|---|
| [207] | 202 | } | 
|---|
|  | 203 | } elsif ($webvar{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}$/) { | 
|---|
|  | 204 | # Specific IP address match.  Will show either a single netblock, | 
|---|
|  | 205 | # or a static pool plus an IP. | 
|---|
|  | 206 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{cidrexclude} ". | 
|---|
|  | 207 | "cidr >>= '$webvar{cidr}')"; | 
|---|
|  | 208 | } elsif ($webvar{cidr} =~ /^\d{1,3}(\.(\d{1,3}(\.(\d{1,3}\.?)?)?)?)?$/) { | 
|---|
|  | 209 | # Leading octets in CIDR | 
|---|
|  | 210 | $sql .= " $sqlconcat (select $cols from searchme where $webvar{cidrexclude} ". | 
|---|
|  | 211 | "text(cidr) like '$webvar{cidr}%')"; | 
|---|
|  | 212 | } else { | 
|---|
| [502] | 213 | # do nothing. | 
|---|
|  | 214 | ##fixme  we'll ignore this to clear out the references to legacy code. | 
|---|
| [207] | 215 | } # done with CIDR query options. | 
|---|
| [201] | 216 |  | 
|---|
| [207] | 217 | # Find the offset for multipage results | 
|---|
|  | 218 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE; | 
|---|
| [201] | 219 |  | 
|---|
| [207] | 220 | # Find out how many rows the "core" query will return. | 
|---|
|  | 221 | my $count = countRows($sql); | 
|---|
| [201] | 222 |  | 
|---|
| [207] | 223 | if ($count == 0) { | 
|---|
| [481] | 224 | $page->param(errmsg => "No matches found.  Try eliminating one of the criteria,". | 
|---|
|  | 225 | " or making one or more criteria more general."); | 
|---|
| [207] | 226 | } else { | 
|---|
|  | 227 | # Add the limit/offset clauses | 
|---|
| [370] | 228 | $sql .= " order by cidr"; | 
|---|
|  | 229 | $sql .= " limit $RESULTS_PER_PAGE offset $offset" if $RESULTS_PER_PAGE != 0; | 
|---|
| [207] | 230 | # And tell the user. | 
|---|
|  | 231 | print "<div class=heading>Searching...............</div>\n"; | 
|---|
|  | 232 | queryResults($sql, $webvar{page}, $count); | 
|---|
|  | 233 | } | 
|---|
| [201] | 234 |  | 
|---|
| [397] | 235 | } elsif ($webvar{stype} eq 'n') { | 
|---|
|  | 236 | # Node search. | 
|---|
|  | 237 |  | 
|---|
|  | 238 | my $sql = "SELECT cidr,custid,type,city,description FROM searchme". | 
|---|
|  | 239 | " WHERE cidr IN (SELECT block FROM noderef WHERE node_id=$webvar{node})"; | 
|---|
|  | 240 |  | 
|---|
|  | 241 | # Find the offset for multipage results | 
|---|
|  | 242 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE; | 
|---|
|  | 243 |  | 
|---|
|  | 244 | # Find out how many rows the "core" query will return. | 
|---|
|  | 245 | my $count = countRows($sql); | 
|---|
|  | 246 |  | 
|---|
|  | 247 | if ($count == 0) { | 
|---|
| [481] | 248 | $page->param(errmsg => "No customers currently listed as connected through this node."); | 
|---|
|  | 249 | ##fixme:  still get the results table header | 
|---|
| [397] | 250 | } else { | 
|---|
|  | 251 | # Add the limit/offset clauses | 
|---|
|  | 252 | $sql .= " order by cidr"; | 
|---|
|  | 253 | $sql .= " limit $RESULTS_PER_PAGE offset $offset" if $RESULTS_PER_PAGE != 0; | 
|---|
|  | 254 | # And tell the user. | 
|---|
|  | 255 | print "<div class=heading>Searching...............</div>\n"; | 
|---|
|  | 256 | queryResults($sql, $webvar{page}, $count); | 
|---|
|  | 257 | } | 
|---|
|  | 258 |  | 
|---|
| [207] | 259 | } else { # how script was called.  General case is to show the search criteria page. | 
|---|
| [201] | 260 |  | 
|---|
| [197] | 261 | # Generate table of types | 
|---|
|  | 262 | $sth = $ip_dbh->prepare("select type,dispname from alloctypes where listorder <500 ". | 
|---|
|  | 263 | "order by listorder"); | 
|---|
|  | 264 | $sth->execute; | 
|---|
|  | 265 | my $i=0; | 
|---|
| [481] | 266 | my @typelist; | 
|---|
|  | 267 | while (my ($type,$dispname) = $sth->fetchrow_array) { | 
|---|
|  | 268 | my %row = ( | 
|---|
|  | 269 | newrow => ($i % 4 == 0), | 
|---|
|  | 270 | type => $type, | 
|---|
|  | 271 | dispname => $dispname, | 
|---|
|  | 272 | endrow => ($i++ % 4 == 3) | 
|---|
|  | 273 | ); | 
|---|
|  | 274 | push @typelist, \%row; | 
|---|
| [197] | 275 | } | 
|---|
| [481] | 276 | $page->param(typelist => \@typelist); | 
|---|
| [197] | 277 |  | 
|---|
|  | 278 | # Generate table of cities | 
|---|
|  | 279 | $sth = $ip_dbh->prepare("select id,city from cities order by city"); | 
|---|
|  | 280 | $sth->execute; | 
|---|
| [481] | 281 | $i=0; | 
|---|
|  | 282 | my @citylist; | 
|---|
|  | 283 | while (my ($id, $city) = $sth->fetchrow_array) { | 
|---|
|  | 284 | my %row = ( | 
|---|
|  | 285 | newrow => ($i % 4 == 0), | 
|---|
|  | 286 | id => $id, | 
|---|
|  | 287 | city => $city, | 
|---|
|  | 288 | endrow => ($i++ % 4 == 3) | 
|---|
|  | 289 | ); | 
|---|
|  | 290 | push @citylist, \%row; | 
|---|
| [197] | 291 | } | 
|---|
| [481] | 292 | $page->param(citylist => \@citylist); | 
|---|
| [197] | 293 |  | 
|---|
|  | 294 | } | 
|---|
|  | 295 |  | 
|---|
| [481] | 296 | print $page->output; | 
|---|
|  | 297 |  | 
|---|
| [197] | 298 | # Shut down and clean up. | 
|---|
|  | 299 | finish($ip_dbh); | 
|---|
| [449] | 300 |  | 
|---|
|  | 301 | # We print the footer here, so we don't have to do it elsewhere. | 
|---|
|  | 302 | my $footer = HTML::Template->new(filename => "footer.tmpl"); | 
|---|
|  | 303 | # include the admin tools link in the output? | 
|---|
|  | 304 | $footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/)); | 
|---|
|  | 305 |  | 
|---|
|  | 306 | print $footer->output; | 
|---|
|  | 307 |  | 
|---|
| [197] | 308 | # We shouldn't need to directly execute any code below here;  it's all subroutines. | 
|---|
|  | 309 | exit 0; | 
|---|
|  | 310 |  | 
|---|
| [207] | 311 |  | 
|---|
|  | 312 | # viewBy() | 
|---|
|  | 313 | # The quick search | 
|---|
|  | 314 | # Takes a category descriptor and a query string | 
|---|
|  | 315 | # Creates appropriate SQL to run the search and display the results | 
|---|
|  | 316 | # with queryResults() | 
|---|
| [197] | 317 | sub viewBy($$) { | 
|---|
|  | 318 | my ($category,$query) = @_; | 
|---|
|  | 319 |  | 
|---|
|  | 320 | # Local variables | 
|---|
|  | 321 | my $sql; | 
|---|
|  | 322 |  | 
|---|
|  | 323 | # Calculate start point for LIMIT clause | 
|---|
|  | 324 | my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE; | 
|---|
|  | 325 |  | 
|---|
|  | 326 | # Possible cases: | 
|---|
| [207] | 327 | # 1) Partial IP/subnet.  Treated as "octet-prefix". | 
|---|
|  | 328 | # 2a) CIDR subnet.  Exact match. | 
|---|
|  | 329 | # 2b) CIDR netmask.  YMMV but it should be octet-prefix-with-netmask | 
|---|
|  | 330 | #       (ie, all matches with the octet prefix *AND* that netmask) | 
|---|
|  | 331 | # 3) Customer ID.  "Match-any-segment" | 
|---|
|  | 332 | # 4) Description.  "Match-any-segment" | 
|---|
| [197] | 333 | # 5) Invalid data which might be interpretable as an IP or something, but | 
|---|
|  | 334 | #    which probably shouldn't be for reasons of sanity. | 
|---|
|  | 335 |  | 
|---|
| [371] | 336 | my $cols = "cidr,custid,type,city,description"; | 
|---|
|  | 337 |  | 
|---|
| [197] | 338 | if ($category eq 'all') { | 
|---|
|  | 339 |  | 
|---|
| [371] | 340 | $sql = "select $cols from searchme"; | 
|---|
| [202] | 341 | my $count = countRows($sql); | 
|---|
| [197] | 342 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset"; | 
|---|
|  | 343 | queryResults($sql, $webvar{page}, $count); | 
|---|
|  | 344 |  | 
|---|
|  | 345 | } elsif ($category eq 'cust') { | 
|---|
|  | 346 |  | 
|---|
| [481] | 347 | ##fixme:  this and other quick-search areas;  fix up page heading title similar to first grouping above | 
|---|
| [197] | 348 | print qq(<div class="heading">Searching for Customer IDs containing '$query'</div><br>\n); | 
|---|
|  | 349 |  | 
|---|
|  | 350 | # Query for a customer ID.  Note that we can't restrict to "numeric-only" | 
|---|
|  | 351 | # as we have non-numeric custIDs in the legacy data.  :/ | 
|---|
| [468] | 352 | $sql = "select $cols from searchme where custid ilike '%$query%' or description like '%$query%'"; | 
|---|
| [202] | 353 | my $count = countRows($sql); | 
|---|
| [197] | 354 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset"; | 
|---|
|  | 355 | queryResults($sql, $webvar{page}, $count); | 
|---|
|  | 356 |  | 
|---|
|  | 357 | } elsif ($category eq 'desc') { | 
|---|
|  | 358 |  | 
|---|
|  | 359 | print qq(<div class="heading">Searching for descriptions containing '$query'</div><br>\n); | 
|---|
|  | 360 | # Query based on description (includes "name" from old DB). | 
|---|
| [371] | 361 | $sql = "select $cols from searchme where description ilike '%$query%'". | 
|---|
| [285] | 362 | " or custid ilike '%$query%'"; | 
|---|
| [202] | 363 | my $count = countRows($sql); | 
|---|
| [197] | 364 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset"; | 
|---|
|  | 365 | queryResults($sql, $webvar{page}, $count); | 
|---|
|  | 366 |  | 
|---|
|  | 367 | } elsif ($category =~ /ipblock/) { | 
|---|
|  | 368 |  | 
|---|
|  | 369 | # Query is for a partial IP, a CIDR block in some form, or a flat IP. | 
|---|
|  | 370 | print qq(<div class="heading">Searching for IP-based matches on '$query'</div><br>\n); | 
|---|
|  | 371 |  | 
|---|
|  | 372 | $query =~ s/\s+//g; | 
|---|
|  | 373 | if ($query =~ /\//) { | 
|---|
| [427] | 374 | # 192.168.179/26 should show all /26 subnets in 192.168.179 | 
|---|
| [197] | 375 | my ($net,$maskbits) = split /\//, $query; | 
|---|
|  | 376 | if ($query =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) { | 
|---|
|  | 377 | # /0->/9 are silly to worry about right now.  I don't think | 
|---|
|  | 378 | # we'll be getting a class A anytime soon.  <g> | 
|---|
| [371] | 379 | $sql = "select $cols from searchme where cidr='$query'"; | 
|---|
| [197] | 380 | queryResults($sql, $webvar{page}, 1); | 
|---|
|  | 381 | } else { | 
|---|
| [289] | 382 | #print "Finding all blocks with netmask /$maskbits, leading octet(s) $net<br>\n"; | 
|---|
| [197] | 383 | # Partial match;  beginning of subnet and maskbits are provided | 
|---|
| [371] | 384 | $sql = "select $cols from searchme where text(cidr) like '$net%' and ". | 
|---|
| [197] | 385 | "text(cidr) like '%$maskbits'"; | 
|---|
| [202] | 386 | my $count = countRows($sql); | 
|---|
| [197] | 387 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset"; | 
|---|
|  | 388 | queryResults($sql, $webvar{page}, $count); | 
|---|
|  | 389 | } | 
|---|
|  | 390 | } elsif ($query =~ /^(\d{1,3}\.){3}\d{1,3}$/) { | 
|---|
|  | 391 | # Specific IP address match | 
|---|
| [289] | 392 | #print "4-octet pattern found;  finding netblock containing IP $query<br>\n"; | 
|---|
| [197] | 393 | my ($net,$ip) = ($query =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/); | 
|---|
|  | 394 | my $sfor = new NetAddr::IP $query; | 
|---|
| [371] | 395 | $sth = $ip_dbh->prepare("select $cols from searchme where text(cidr) like '$net%'"); | 
|---|
| [197] | 396 | $sth->execute; | 
|---|
|  | 397 | while (my @data = $sth->fetchrow_array()) { | 
|---|
|  | 398 | my $cidr = new NetAddr::IP $data[0]; | 
|---|
|  | 399 | if ($cidr->contains($sfor)) { | 
|---|
| [371] | 400 | queryResults("select $cols from searchme where cidr='$cidr'", $webvar{page}, 1); | 
|---|
| [197] | 401 | } | 
|---|
|  | 402 | } | 
|---|
| [202] | 403 | } elsif ($query =~ /^(\d{1,3}\.){1,3}\d{1,3}\.?$/) { | 
|---|
| [289] | 404 | #print "Finding matches with leading octet(s) $query<br>\n"; | 
|---|
| [371] | 405 | $sql = "select $cols from searchme where text(cidr) like '$query%'"; | 
|---|
| [202] | 406 | my $count = countRows($sql); | 
|---|
| [197] | 407 | $sql .= " order by cidr limit $RESULTS_PER_PAGE offset $offset"; | 
|---|
|  | 408 | queryResults($sql, $webvar{page}, $count); | 
|---|
|  | 409 | } else { | 
|---|
|  | 410 | # This shouldn't happen, but if it does, whoever gets it deserves what they get... | 
|---|
| [481] | 411 | $page->param(errmsg => "Invalid query."); | 
|---|
| [197] | 412 | } | 
|---|
|  | 413 | } else { | 
|---|
|  | 414 | # This shouldn't happen, but if it does, whoever gets it deserves what they get... | 
|---|
| [481] | 415 | $page->param(errmsg => "Invalid searchfor."); | 
|---|
| [197] | 416 | } | 
|---|
|  | 417 | } # viewBy | 
|---|
|  | 418 |  | 
|---|
|  | 419 |  | 
|---|
|  | 420 | # args are: a reference to an array with the row to be printed and the | 
|---|
|  | 421 | # class(stylesheet) to use for formatting. | 
|---|
|  | 422 | # if ommitting the class - call the sub as &printRow(\@array) | 
|---|
|  | 423 | sub printRow { | 
|---|
|  | 424 | my ($rowRef,$class) = @_; | 
|---|
|  | 425 |  | 
|---|
|  | 426 | if (!$class) { | 
|---|
|  | 427 | print "<tr>\n"; | 
|---|
|  | 428 | } else { | 
|---|
|  | 429 | print "<tr class=\"$class\">\n"; | 
|---|
|  | 430 | } | 
|---|
|  | 431 |  | 
|---|
|  | 432 | ELEMENT:  foreach my $element (@$rowRef) { | 
|---|
|  | 433 | if (!defined($element)) { | 
|---|
|  | 434 | print "<td></td>\n"; | 
|---|
|  | 435 | next ELEMENT; | 
|---|
|  | 436 | } | 
|---|
|  | 437 | $element =~ s|\n|</br>|g; | 
|---|
|  | 438 | print "<td>$element</td>\n"; | 
|---|
|  | 439 | } | 
|---|
|  | 440 | print "</tr>"; | 
|---|
|  | 441 | } # printRow | 
|---|
|  | 442 |  | 
|---|
|  | 443 |  | 
|---|
| [207] | 444 | # queryResults() | 
|---|
|  | 445 | # Display search queries based on the passed SQL. | 
|---|
|  | 446 | # Takes SQL, page number (for multipage search results), and a total count. | 
|---|
| [197] | 447 | sub queryResults($$$) { | 
|---|
|  | 448 | my ($sql, $pageNo, $rowCount) = @_; | 
|---|
|  | 449 | my $offset = 0; | 
|---|
|  | 450 | $offset = $1 if($sql =~ m/.*limit\s+(.*),.*/); | 
|---|
|  | 451 |  | 
|---|
|  | 452 | my $sth = $ip_dbh->prepare($sql); | 
|---|
|  | 453 | $sth->execute(); | 
|---|
|  | 454 |  | 
|---|
| [481] | 455 | $page->param(searchtitle => "Showing all netblock and static-IP allocations"); | 
|---|
|  | 456 |  | 
|---|
| [197] | 457 | my $count = 0; | 
|---|
| [481] | 458 | my @sresults; | 
|---|
|  | 459 | while (my ($block, $custid, $type, $city, $desc) = $sth->fetchrow_array) { | 
|---|
|  | 460 | my %row = ( | 
|---|
|  | 461 | rowclass => $count++ % 2, | 
|---|
|  | 462 | issub => ($type =~ /^.r$/ ? 1 : 0), | 
|---|
|  | 463 | block => $block, | 
|---|
|  | 464 | ispool => ($type =~ /^.[pd]$/ ? 1 : 0), | 
|---|
|  | 465 | custid => $custid, | 
|---|
|  | 466 | disptype => $disp_alloctypes{$type}, | 
|---|
|  | 467 | city => $city, | 
|---|
|  | 468 | desc => $desc | 
|---|
|  | 469 | ); | 
|---|
|  | 470 | push @sresults, \%row; | 
|---|
| [197] | 471 | } | 
|---|
| [481] | 472 | $page->param(sresults => \@sresults); | 
|---|
| [197] | 473 |  | 
|---|
|  | 474 | # Have to think on this call, it's primarily to clean up unfetched rows from a select. | 
|---|
|  | 475 | # In this context it's probably a good idea. | 
|---|
|  | 476 | $sth->finish(); | 
|---|
|  | 477 |  | 
|---|
|  | 478 | my $upper = $offset+$count; | 
|---|
|  | 479 |  | 
|---|
| [481] | 480 | $page->param(resfound => $rowCount); | 
|---|
|  | 481 | $page->param(resstart => $offset+1); | 
|---|
|  | 482 | $page->param(resstop => $upper); | 
|---|
|  | 483 |  | 
|---|
| [197] | 484 | # print the page thing.. | 
|---|
| [370] | 485 | if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) { | 
|---|
| [481] | 486 | $page->param(multipage => 1); | 
|---|
| [197] | 487 | my $pages = ceil($rowCount/$RESULTS_PER_PAGE); | 
|---|
| [481] | 488 | my @pagelist; | 
|---|
| [197] | 489 | for (my $i = 1; $i <= $pages; $i++) { | 
|---|
| [481] | 490 | my %row; | 
|---|
|  | 491 | $row{pgnum} = $i; | 
|---|
| [197] | 492 | if ($i == $pageNo) { | 
|---|
| [481] | 493 | $row{thispage} = 1; | 
|---|
| [197] | 494 | } else { | 
|---|
| [481] | 495 | $row{stype} = $webvar{stype}; | 
|---|
| [202] | 496 | if ($webvar{stype} eq 'c') { | 
|---|
| [481] | 497 | $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&". | 
|---|
| [202] | 498 | "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&". | 
|---|
|  | 499 | "allcities=$webvar{allcities}&"; | 
|---|
|  | 500 | foreach my $key (keys %webvar) { | 
|---|
| [351] | 501 | if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) { | 
|---|
| [481] | 502 | $row{extraopts} .= "$key=$webvar{$key}&"; | 
|---|
| [202] | 503 | } | 
|---|
|  | 504 | } | 
|---|
|  | 505 | } else { | 
|---|
| [481] | 506 | $row{extraopts} = "input=$webvar{input}&"; | 
|---|
| [202] | 507 | } | 
|---|
| [197] | 508 | } | 
|---|
| [481] | 509 | push @pagelist, \%row; | 
|---|
| [197] | 510 | } | 
|---|
| [481] | 511 | $page->param(pgnums => \@pagelist); | 
|---|
| [197] | 512 | } | 
|---|
| [481] | 513 |  | 
|---|
| [197] | 514 | } # queryResults | 
|---|
|  | 515 |  | 
|---|
|  | 516 |  | 
|---|
|  | 517 | # Prints table headings.  Accepts any number of arguments; | 
|---|
|  | 518 | # each argument is a table heading. | 
|---|
|  | 519 | sub startTable { | 
|---|
|  | 520 | print qq(<center><table width="98%" cellspacing="0" class="center"><tr>); | 
|---|
|  | 521 |  | 
|---|
|  | 522 | foreach(@_) { | 
|---|
|  | 523 | print qq(<td class="heading">$_</td>); | 
|---|
|  | 524 | } | 
|---|
|  | 525 | print "</tr>\n"; | 
|---|
|  | 526 | } # startTable | 
|---|
|  | 527 |  | 
|---|
|  | 528 |  | 
|---|
| [202] | 529 | # Return count of rows to be returned in a "real" query | 
|---|
|  | 530 | # with the passed SQL statement | 
|---|
| [197] | 531 | sub countRows($) { | 
|---|
| [202] | 532 | # Note that the "as foo" is required | 
|---|
|  | 533 | my $sth = $ip_dbh->prepare("select count(*) from ($_[0]) as foo"); | 
|---|
| [197] | 534 | $sth->execute(); | 
|---|
|  | 535 | my @a = $sth->fetchrow_array(); | 
|---|
|  | 536 | $sth->finish(); | 
|---|
|  | 537 | return $a[0]; | 
|---|
|  | 538 | } | 
|---|