source: trunk/cgi-bin/search.cgi@ 930

Last change on this file since 930 was 930, checked in by Kris Deugau, 19 months ago

/trunk

Scrape a lot of the fixme-itis out of search.cgi while updating it to
properly search for ciruict IDs. Still needs a clean top-to-bottom
rewrite.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 18.6 KB
Line 
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###
11# Copyright 2005-2010,2012,2015-2017,2022 - Kris Deugau <kdeugau@deepnet.cx>
12
13use strict;
14use warnings;
15use CGI::Carp qw(fatalsToBrowser);
16use CGI::Simple;
17use HTML::Template;
18use DBI;
19use POSIX qw(ceil);
20use NetAddr::IP;
21
22# don't remove! required for GNU/FHS-ish install from tarball
23##uselib##
24
25# push "the directory the script is in" into @INC
26use FindBin;
27use lib "$FindBin::RealBin/";
28
29use MyIPDB;
30
31# Don't formally need a username or syslog here. syslog left active for debugging.
32use Sys::Syslog;
33openlog "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.
38my $authuser;
39if (!defined($ENV{'REMOTE_USER'})) {
40 $authuser = '__temptest';
41} else {
42 $authuser = $ENV{'REMOTE_USER'};
43}
44
45# Global variables
46my $RESULTS_PER_PAGE = 25;
47
48# anyone got a better name? :P
49my $thingroot = $ENV{SCRIPT_FILENAME};
50$thingroot =~ s|cgi-bin/search.cgi||;
51
52# Set up the CGI object...
53my $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)
59my %webvar = $q->Vars;
60
61if (defined($webvar{rpp})) {
62 ($RESULTS_PER_PAGE) = ($webvar{rpp} =~ /(\d+)/);
63}
64
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
67my $ip_dbh;
68my $sth;
69my $errstr;
70($ip_dbh,$errstr) = connectDB_My;
71if ($ip_dbh) {
72 checkDBSanity($ip_dbh);
73 initIPDBGlobals($ip_dbh);
74}
75
76# Set up some globals
77$ENV{HTML_TEMPLATE_ROOT} = $thingroot;
78my @templatepath = [ "localtemplates", "templates" ];
79
80## FIXME!
81## Pretty much everything from here on down is one giant FIXME
82## FIXME!
83
84my $page;
85if (!defined($webvar{stype})) {
86 $webvar{stype} = "<NULL>"; #shuts up the warnings.
87 $page = HTML::Template->new(filename => "search/compsearch.tmpl", path => @templatepath);
88 $page->param(webpath => $IPDB::webpath);
89} else {
90 $page = HTML::Template->new(filename => "search/sresults.tmpl", global_vars => 1, path => @templatepath);
91 $page->param(webpath => $IPDB::webpath);
92}
93
94my $header = HTML::Template->new(filename => "header.tmpl", path => @templatepath);
95$header->param(version => $IPDB::VERSION);
96$header->param(addperm => $IPDBacl{$authuser} =~ /a/);
97$header->param(webpath => $IPDB::webpath);
98print "Content-type: text/html\n\n", $header->output;
99
100# Columns actually returned. Slightly better than hardcoding it
101# in each (sub)select
102my $cols = "s.cidr, s.custid, s.type, s.city, s.description, s.id, s.parent_id, s.available, a.vrf";
103# Common base select. JOIN provides the VRF which may not be noted on individual allocations
104my $sqlbase = "SELECT $cols FROM searchme s JOIN allocations a ON s.master_id=a.id";
105
106# Handle the DB error first
107if (!$ip_dbh) {
108 $page = HTML::Template->new(filename => "dberr.tmpl", path => @templatepath);
109 $page->param(errmsg => $errstr);
110} elsif ($webvar{stype} eq 'q') {
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+$//;
123 if ($webvar{input} =~ /^\d+$/) {
124 # All-digits, new custID
125 $searchfor = "cust";
126 } elsif ($webvar{input} =~ /^[\d\.]+(\/\d{1,3})?$/) {
127 # IP addresses should only have numbers, digits, and maybe a slash+netmask
128 $searchfor = "ipblock";
129 } elsif ($webvar{input} =~ /(?:^\d{6}\-|[A-Z][A-Z]\d\d\d)/) {
130 # Looks like part of a circuit ID
131 $searchfor = "circuitid";
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
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
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 {
156 # sum-buddy tryn'a game the system. Match "all"
157 $sqlconcat = "INTERSECT";
158 }
159
160# We actually construct a monster SQL statement for all criteria.
161# Iff something has been entered, it will be used as a filter.
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.
164
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
173 # First chunk of SQL. Filter on custid, description, and notes as necessary.
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 }
190
191 # If we're not supposed to search for all types, search for the selected types.
192 $webvar{alltypes} = '' if !$webvar{alltypes};
193 $webvar{typeexclude} = '' if !$webvar{typeexclude};
194 if ($webvar{alltypes} ne 'on') {
195 $sql .= " $sqlconcat ($sqlbase WHERE $webvar{typeexclude} s.type IN (";
196 foreach my $key (keys %webvar) {
197 $sql .= "'$1'," if $key =~ /type\[(\w\w)\]/;
198 }
199 chop $sql;
200 $sql .= "))";
201 }
202
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.
205 $webvar{allcities} = '' if !$webvar{allcities};
206 $webvar{cityexclude} = '' if !$webvar{cityexclude};
207 if ($webvar{allcities} ne 'on') {
208 $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cityexclude} s.city IN (";
209 $sth = $ip_dbh->prepare("SELECT city FROM cities WHERE id=?");
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 }
219 }
220 chop $sql;
221 $sql .= "))";
222 }
223
224 ## CIDR query options.
225 $webvar{cidr} =~ s/\s+//; # Hates the nasty spaceseseses we does.
226 if ($webvar{cidr} eq '') { # We has a blank CIDR. Ignore it.
227 } elsif ($webvar{cidr} =~ /\//) {
228 # 192.168.179/26 should show all /26 subnets in 192.168.179
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>
233 $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} s.cidr <<= ?)";
234 push @bindargs, $webvar{cidr};
235 } else {
236 # Partial match; beginning of subnet and maskbits are provided
237 # Show any blocks with the leading octet(s) and that masklength
238 # Need some more magic for bare /nn searches:
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 .= "))";
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.
250 $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} s.cidr >>= ?)";
251 push @bindargs, $webvar{cidr};
252 } elsif ($webvar{cidr} =~ /^\d{1,3}(\.(\d{1,3}(\.(\d{1,3}\.?)?)?)?)?$/) {
253 # Leading octets in CIDR
254 $sql .= " $sqlconcat ($sqlbase WHERE $webvar{cidrexclude} text(s.cidr) LIKE ?)";
255 push @bindargs, "$webvar{cidr}%";
256 } else {
257 # do nothing.
258 ##fixme we'll ignore this to clear out the references to legacy code.
259 } # done with CIDR query options.
260
261 # Find the offset for multipage results
262 my $offset = ($webvar{page}-1)*$RESULTS_PER_PAGE;
263
264 # Find out how many rows the "core" query will return.
265 my $count = countRows($sql, @bindargs);
266
267 if ($count == 0) {
268 $page->param(errmsg => "No matches found. Try eliminating one of the criteria,".
269 " or making one or more criteria more general.");
270 } else {
271 # Add the limit/offset clauses
272 $sql .= " ORDER BY s.cidr";
273 $sql .= " LIMIT $RESULTS_PER_PAGE OFFSET $offset" if $RESULTS_PER_PAGE != 0;
274 # And tell the user.
275 print "<div class=heading>Searching...............</div>\n";
276 queryResults($sql, $webvar{page}, $count, @bindargs);
277 }
278
279} elsif ($webvar{stype} eq 'n') {
280 # Node search.
281
282 my $sql = "$sqlbase JOIN noderef nr ON nr.block=s.cidr WHERE nr.node_id = ?";
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.
288 my $count = countRows($sql, $webvar{node});
289
290 my $nodename = getNodeName($ip_dbh, $webvar{node});
291
292 if ($count == 0) {
293 $page->param(errmsg => "No customers currently listed as connected through $nodename.");
294##fixme: still get the results table header
295 } else {
296 # Add the limit/offset clauses
297 $sql .= " ORDER BY cidr";
298 $sql .= " LIMIT $RESULTS_PER_PAGE OFFSET $offset" if $RESULTS_PER_PAGE != 0;
299 # And tell the user.
300 print "<div class=heading>Searching for assignments terminating on $nodename...</div>\n";
301 queryResults($sql, $webvar{page}, $count, $webvar{node});
302 }
303
304} else { # how script was called. General case is to show the search criteria page.
305
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;
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;
320 }
321 $page->param(typelist => \@typelist);
322
323# Generate table of cities
324 $sth = $ip_dbh->prepare("select id,city from cities order by city");
325 $sth->execute;
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;
336 }
337 $page->param(citylist => \@citylist);
338
339}
340
341print $page->output;
342
343# Shut down and clean up.
344finish($ip_dbh);
345
346# We print the footer here, so we don't have to do it elsewhere.
347my $footer = HTML::Template->new(filename => "footer.tmpl", path => @templatepath);
348# include the admin tools link in the output?
349$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/));
350
351print $footer->output;
352
353# We shouldn't need to directly execute any code below here; it's all subroutines.
354exit 0;
355
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()
362sub viewBy {
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;
370##fixme: squeeze ORDER BY etc out into somewhere common, or at least an
371# includeable bit instead of hardcoding in each block
372
373 if ($category eq 'all') {
374
375 # Sort of pointless, just horks up everything.
376 $sql = "$sqlbase";
377 my $count = countRows($sql);
378 $sql .= " ORDER BY s.cidr LIMIT $RESULTS_PER_PAGE OFFSET $offset";
379 queryResults($sql, $webvar{page}, $count);
380
381 } elsif ($category eq 'cust') {
382
383##fixme: this and other quick-search areas; fix up page heading title similar to first grouping above
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. :/
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);
392
393 } elsif ($category eq 'desc') {
394
395 print qq(<div class="heading">Searching for description, customer ID, or circuit ID matching '$query'</div><br>\n);
396 # Query based on description (includes "name" from old DB).
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);
401
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
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 =~ /\//) {
419 # 192.168.179/26 should show all /26 subnets in 192.168.179
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>
424 $sql = "$sqlbase WHERE s.cidr = ?";
425 queryResults($sql, $webvar{page}, 1, $query);
426 } else {
427 #print "Finding all blocks with netmask /$maskbits, leading octet(s) $net<br>\n";
428 # Partial match; beginning of subnet and maskbits are provided
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");
433 }
434
435 } elsif ($query =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
436 # Specific IP address match
437 #print "4-octet pattern found; finding netblock containing IP $query<br>\n";
438 my ($net,$ip) = ($query =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.)(\d{1,3})/);
439 my $sfor = new NetAddr::IP $query;
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);
444
445 } elsif ($query =~ /^(\d{1,3}\.){1,3}\d{1,3}\.?$/) {
446 #print "Finding matches with leading octet(s) $query<br>\n";
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%");
451 } else {
452 # This shouldn't happen, but if it does, whoever gets it deserves what they get...
453 $page->param(errmsg => "Invalid query.");
454 }
455 } else {
456 # This shouldn't happen, but if it does, whoever gets it deserves what they get...
457 $page->param(errmsg => "Invalid searchfor.");
458 }
459} # viewBy
460
461
462
463# queryResults()
464# Display search queries based on the passed SQL.
465# Takes SQL, page number (for multipage search results), and a total count.
466sub queryResults {
467 my $sql = shift;
468 my $pageNo = shift;
469 my $rowCount = shift;
470 my @bindargs = @_;
471
472 my $offset = 0;
473 $offset = $1 if($sql =~ m/.*LIMIT\s+(.*),.*/);
474
475 my $sth = $ip_dbh->prepare($sql);
476 $sth->execute(@bindargs);
477
478 $page->param(searchtitle => "Showing all netblock and static-IP allocations");
479
480 my $count = 0;
481 my @sresults;
482 while (my ($block, $custid, $type, $city, $desc, $id, $parent, $avail, $vrf) = $sth->fetchrow_array) {
483 my %row = (
484 rowclass => $count++ % 2,
485 vrf => $vrf,
486 issub => ($type =~ /^.r$/ ? 1 : 0),
487 ispool => ($type =~ /^.[pd]$/ ? 1 : 0),
488 basetype => ($type =~ /^.i/ ? 'i' : 'b'),
489 freeip => ($avail eq 'y'),
490 parent => $parent,
491 block => $block,
492 custid => $custid,
493 disptype => $disp_alloctypes{$type},
494 city => $city,
495 desc => $desc,
496 id => $id,
497 );
498 push @sresults, \%row;
499 }
500 $page->param(sresults => \@sresults);
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
508 $page->param(resfound => $rowCount);
509 $page->param(resstart => $offset+1);
510 $page->param(resstop => $upper);
511
512 # print the page thing..
513 if ($RESULTS_PER_PAGE > 0 && $rowCount > $RESULTS_PER_PAGE) {
514 $page->param(multipage => 1);
515 my $pages = ceil($rowCount/$RESULTS_PER_PAGE);
516 my @pagelist;
517 for (my $i = 1; $i <= $pages; $i++) {
518 my %row;
519 $row{pgnum} = $i;
520 if ($i == $pageNo) {
521 $row{thispage} = 1;
522 } else {
523 $row{stype} = $webvar{stype};
524 if ($webvar{stype} eq 'c') {
525 $row{extraopts} = "cidr=$webvar{cidr}&custid=$webvar{custid}&desc=$webvar{desc}&".
526 "notes=$webvar{notes}&which=$webvar{which}&alltypes=$webvar{alltypes}&".
527 "allcities=$webvar{allcities}&";
528 foreach my $key (keys %webvar) {
529 if ($key =~ /^(?:type|city)\[/ || $key =~ /exclude$/) {
530 $row{extraopts} .= "$key=$webvar{$key}&";
531 }
532 }
533 } else {
534 $row{extraopts} = "input=$webvar{input}&";
535 }
536 }
537 push @pagelist, \%row;
538 }
539 $page->param(pgnums => \@pagelist);
540 }
541
542} # queryResults
543
544
545
546# Return count of rows to be returned in a "real" query
547# with the passed SQL statement
548sub countRows {
549 my $sql = shift;
550
551 # Note that the "as foo" is required
552 my @a = $ip_dbh->selectrow_array("SELECT count(*) FROM ($sql) AS foo", undef, @_);
553 return $a[0];
554}
Note: See TracBrowser for help on using the repository browser.