| 1 | #!/usr/bin/perl
 | 
|---|
| 2 | # XMLRPC interface to IPDB search
 | 
|---|
| 3 | # Copyright (C) 2017 Kris Deugau <kdeugau@deepnet.cx>
 | 
|---|
| 4 | 
 | 
|---|
| 5 | use strict;
 | 
|---|
| 6 | use warnings;
 | 
|---|
| 7 | 
 | 
|---|
| 8 | use DBI;
 | 
|---|
| 9 | use NetAddr::IP;
 | 
|---|
| 10 | use FCGI;
 | 
|---|
| 11 | use Frontier::Responder;
 | 
|---|
| 12 | 
 | 
|---|
| 13 | use Sys::Syslog;
 | 
|---|
| 14 | 
 | 
|---|
| 15 | # don't remove!  required for GNU/FHS-ish install from tarball
 | 
|---|
| 16 | ##uselib##
 | 
|---|
| 17 | 
 | 
|---|
| 18 | # push "the directory the script is in" into @INC
 | 
|---|
| 19 | use FindBin;
 | 
|---|
| 20 | use lib "$FindBin::RealBin/";
 | 
|---|
| 21 | 
 | 
|---|
| 22 | use MyIPDB;
 | 
|---|
| 23 | use CustIDCK;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | openlog "IPDB-search-rpc","pid","$IPDB::syslog_facility";
 | 
|---|
| 26 | 
 | 
|---|
| 27 | ##fixme:  username source?  can we leverage some other auth method?
 | 
|---|
| 28 | # we don't care except for logging here, and Frontier::Client needs
 | 
|---|
| 29 | # a patch that's not well-distributed to use HTTP AUTH.
 | 
|---|
| 30 | 
 | 
|---|
| 31 | # Collect the username from HTTP auth.  If undefined, we're in
 | 
|---|
| 32 | # a test environment, or called without a username.
 | 
|---|
| 33 | my $authuser;
 | 
|---|
| 34 | if (!defined($ENV{'REMOTE_USER'})) {
 | 
|---|
| 35 |   $authuser = '__temptest';
 | 
|---|
| 36 | } else {
 | 
|---|
| 37 |   $authuser = $ENV{'REMOTE_USER'};
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | # Why not a global DB handle?  (And a global statement handle, as well...)
 | 
|---|
| 41 | # Use the connectDB function, otherwise we end up confusing ourselves
 | 
|---|
| 42 | my $ip_dbh;
 | 
|---|
| 43 | my $sth;
 | 
|---|
| 44 | my $errstr;
 | 
|---|
| 45 | ($ip_dbh,$errstr) = connectDB_My;
 | 
|---|
| 46 | initIPDBGlobals($ip_dbh);
 | 
|---|
| 47 | 
 | 
|---|
| 48 | my $methods = {
 | 
|---|
| 49 |         'ipdb.search'   =>      \&rpc_search,
 | 
|---|
| 50 | };
 | 
|---|
| 51 | 
 | 
|---|
| 52 | my $reqcnt = 0;
 | 
|---|
| 53 | 
 | 
|---|
| 54 | my $req = FCGI::Request();
 | 
|---|
| 55 | 
 | 
|---|
| 56 | # main FCGI loop.
 | 
|---|
| 57 | while ($req->Accept() >= 0) {
 | 
|---|
| 58 |   # done here to a) prevent $ENV{'REMOTE_ADDR'} from being empty and b) to collect
 | 
|---|
| 59 |   # the right user for the individual call (since we may be running with FCGI)
 | 
|---|
| 60 |   syslog "debug", "$authuser active, $ENV{'REMOTE_ADDR'}";
 | 
|---|
| 61 | 
 | 
|---|
| 62 |   # don't *think* we need any of these...
 | 
|---|
| 63 |   # %disp_alloctypes, %def_custids, %list_alloctypes
 | 
|---|
| 64 |   # @citylist, @poplist
 | 
|---|
| 65 |   # @masterblocks, %allocated, %free, %bigfree, %routed  (removed in /trunk)
 | 
|---|
| 66 |   # %IPDBacl
 | 
|---|
| 67 |   #initIPDBGlobals($ip_dbh);
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   my $res = Frontier::Responder->new(
 | 
|---|
| 70 |         methods => $methods
 | 
|---|
| 71 |         );
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   # "Can't do that" errors
 | 
|---|
| 74 |   if (!$ip_dbh) {
 | 
|---|
| 75 |     print "Content-type: text/xml\n\n".$res->{_decode}->encode_fault(5, $DBI::errstr);
 | 
|---|
| 76 |   } else {
 | 
|---|
| 77 |     print $res->answer;
 | 
|---|
| 78 |   }
 | 
|---|
| 79 |   last if $reqcnt++ > $IPDB::maxfcgi;
 | 
|---|
| 80 | } # while FCGI::accept
 | 
|---|
| 81 | 
 | 
|---|
| 82 | exit 0;
 | 
|---|
| 83 | 
 | 
|---|
| 84 | 
 | 
|---|
| 85 | ##
 | 
|---|
| 86 | ## Private subs
 | 
|---|
| 87 | ##
 | 
|---|
| 88 | 
 | 
|---|
| 89 | # Check RPC ACL
 | 
|---|
| 90 | sub _aclcheck {
 | 
|---|
| 91 |   my $subsys = shift;
 | 
|---|
| 92 |   return 1 if grep /$ENV{REMOTE_ADDR}/, @{$IPDB::rpcacl{$subsys}};
 | 
|---|
| 93 |   warn "$subsys/$ENV{REMOTE_ADDR} not in ACL\n";        # a bit of logging
 | 
|---|
| 94 |   return 0;
 | 
|---|
| 95 | }
 | 
|---|
| 96 | 
 | 
|---|
| 97 | sub _commoncheck {
 | 
|---|
| 98 |   my $argref = shift;
 | 
|---|
| 99 |   my $needslog = shift;
 | 
|---|
| 100 | 
 | 
|---|
| 101 |   die "Missing remote system name\n" if !$argref->{rpcsystem};
 | 
|---|
| 102 |   die "Access denied\n" if !_aclcheck($argref->{rpcsystem});
 | 
|---|
| 103 |   if ($needslog) {
 | 
|---|
| 104 |     die "Missing remote username\n" if !$argref->{rpcuser};
 | 
|---|
| 105 |   }
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | # stripped-down copy from from main.cgi.  should probably be moved to IPDB.pm
 | 
|---|
| 109 | sub _validateInput {
 | 
|---|
| 110 |   my $argref = shift;
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   if (!$argref->{block}) {
 | 
|---|
| 113 |     $argref->{block} = $argref->{cidr} if $argref->{cidr};
 | 
|---|
| 114 |     die "Block/IP is required\n" if !$argref->{block};
 | 
|---|
| 115 |   }
 | 
|---|
| 116 | 
 | 
|---|
| 117 |   # Alloctype check.
 | 
|---|
| 118 |   chomp $argref->{type};
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   die "Invalid allocation type\n" if (!grep /$argref->{type}/, keys %disp_alloctypes);
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   # Arguably not quite correct, as the custID won't be checked for
 | 
|---|
| 123 |   # validity if there's a default on the type.
 | 
|---|
| 124 |   if ($def_custids{$argref->{type}} eq '') {
 | 
|---|
| 125 |     # Types without a default custID must have one passed in
 | 
|---|
| 126 |     die "Customer ID is required\n" if !$argref->{custid};
 | 
|---|
| 127 |     # Crosscheck with billing.
 | 
|---|
| 128 |     my $status = CustIDCK->custid_exist($argref->{custid});
 | 
|---|
| 129 |     die "Error verifying customer ID: $CustIDCK::ErrMsg\n" if $CustIDCK::Error;
 | 
|---|
| 130 |     die "Customer ID not valid\n" if !$status;
 | 
|---|
| 131 |   } else {
 | 
|---|
| 132 |     # Types that have a default will use it unless one is specified.
 | 
|---|
| 133 |     if ((!$argref->{custid}) || ($argref->{custid} ne 'STAFF')) {
 | 
|---|
| 134 |       $argref->{custid} = $def_custids{$argref->{type}};
 | 
|---|
| 135 |     }
 | 
|---|
| 136 |   }
 | 
|---|
| 137 | } # end validateInput()
 | 
|---|
| 138 | 
 | 
|---|
| 139 | 
 | 
|---|
| 140 | ##
 | 
|---|
| 141 | ## RPC method subs
 | 
|---|
| 142 | ##
 | 
|---|
| 143 | 
 | 
|---|
| 144 | sub rpc_search {
 | 
|---|
| 145 |   my %args = @_;
 | 
|---|
| 146 | 
 | 
|---|
| 147 |   _commoncheck(\%args, 'n');
 | 
|---|
| 148 | 
 | 
|---|
| 149 |   my @fields;
 | 
|---|
| 150 |   my @vals;
 | 
|---|
| 151 |   my @matchtypes;
 | 
|---|
| 152 | 
 | 
|---|
| 153 |   my %mt = (
 | 
|---|
| 154 |         EXACT => '=',
 | 
|---|
| 155 |         EQUAL => '=',
 | 
|---|
| 156 |         NOT => '!~',    # text only?
 | 
|---|
| 157 |         # CIDR options
 | 
|---|
| 158 |         MASK => 'MASK',
 | 
|---|
| 159 |         WITHIN => '<<=',
 | 
|---|
| 160 |         CONTAINS => '>>=',
 | 
|---|
| 161 |         );
 | 
|---|
| 162 | 
 | 
|---|
| 163 |   if ($args{type}) {
 | 
|---|
| 164 |     my $notflag = '';
 | 
|---|
| 165 |     if ($args{type} =~ /^NOT:/) {
 | 
|---|
| 166 |       $args{type} =~ s/^NOT://;
 | 
|---|
| 167 |       $notflag = 'NOT ';
 | 
|---|
| 168 |     }
 | 
|---|
| 169 |     if ($args{type} =~ /\./) {
 | 
|---|
| 170 |       $args{type} =~ s/\./_/;
 | 
|---|
| 171 |       push @matchtypes, $notflag.'LIKE';
 | 
|---|
| 172 |     } else {
 | 
|---|
| 173 |       push @matchtypes, ($notflag ? '<>' : '=');
 | 
|---|
| 174 |     }
 | 
|---|
| 175 |     push @fields, 's.type';
 | 
|---|
| 176 |     push @vals, $args{type};
 | 
|---|
| 177 |   }
 | 
|---|
| 178 | 
 | 
|---|
| 179 |   ## CIDR query options.
 | 
|---|
| 180 |   if ($args{cidr}) {
 | 
|---|
| 181 |     $args{cidr} =~ s/^\s*(.+)\s*$/$1/g;
 | 
|---|
| 182 |     # strip matching type substring, if any - only applies to full-CIDR
 | 
|---|
| 183 |     my ($mnote) = $args{cidr} =~ /^(\w+):/;
 | 
|---|
| 184 |     $args{cidr} =~ s/^$mnote:// if $mnote;
 | 
|---|
| 185 | 
 | 
|---|
| 186 |     if ($args{cidr} eq '') { # We has a blank CIDR.  Ignore it.
 | 
|---|
| 187 |     } elsif ($args{cidr} =~ /\//) {
 | 
|---|
| 188 |       my ($net,$maskbits) = split /\//, $args{cidr};
 | 
|---|
| 189 |       if ($args{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}\/\d{2}$/) {
 | 
|---|
| 190 |         # Full CIDR match.
 | 
|---|
| 191 |         push @fields, 's.cidr';
 | 
|---|
| 192 |         push @vals, $args{cidr};
 | 
|---|
| 193 |         if ($mnote =~ /(EQUAL|EXACT|CONTAINS|WITHIN)/) {
 | 
|---|
| 194 |           push @matchtypes, $mt{$1};
 | 
|---|
| 195 |         } else { # default to exact match
 | 
|---|
| 196 |           push @matchtypes, '=';
 | 
|---|
| 197 |         }
 | 
|---|
| 198 |       } elsif ($args{cidr} =~ /^(\d{1,3}\.){2}\d{1,3}\/\d{2}$/) {
 | 
|---|
| 199 |         # Partial match;  beginning of subnet and maskbits are provided
 | 
|---|
| 200 |         # Show any blocks with the leading octet(s) and that masklength
 | 
|---|
| 201 |         # eg 192.168.179/26 should show all /26 subnets in 192.168.179
 | 
|---|
| 202 |         # Need some more magic for bare /nn searches:
 | 
|---|
| 203 |         push @fields, 's.cidr','masklen(s.cidr)';
 | 
|---|
| 204 |         push @vals, "$net.0/24", $maskbits;
 | 
|---|
| 205 |         push @matchtypes, '<<=','=';
 | 
|---|
| 206 |       }
 | 
|---|
| 207 |     } elsif ($args{cidr} =~ /^(\d{1,3}\.){3}\d{1,3}$/) {
 | 
|---|
| 208 |       # Specific IP address match.  Will show the parent chain down to the final allocation.
 | 
|---|
| 209 |       push @fields, 's.cidr';
 | 
|---|
| 210 |       push @vals, $args{cidr};
 | 
|---|
| 211 |       push @matchtypes, '>>=';
 | 
|---|
| 212 |     } elsif ($args{cidr} =~ /^\d{1,3}(\.(\d{1,3}(\.(\d{1,3}\.?)?)?)?)?$/) {
 | 
|---|
| 213 |       # 1, 2, or 3 leading octets in CIDR
 | 
|---|
| 214 |       push @fields, 'text(s.cidr)';
 | 
|---|
| 215 |       push @vals, "$args{cidr}\%";
 | 
|---|
| 216 |       push @matchtypes, 'LIKE';  # hmm
 | 
|---|
| 217 |     } else {
 | 
|---|
| 218 |       # do nothing.
 | 
|---|
| 219 |       ##fixme  we'll ignore this to clear out the references to legacy code.
 | 
|---|
| 220 |     } # done with CIDR query options.
 | 
|---|
| 221 | 
 | 
|---|
| 222 |   } # args{cidr}
 | 
|---|
| 223 | 
 | 
|---|
| 224 |   foreach (qw(custid description notes city) ) {
 | 
|---|
| 225 |     if ($args{$_}) {
 | 
|---|
| 226 |       push @fields, "s.$_";
 | 
|---|
| 227 |       if ($args{$_} =~ /^(EXACT|NOT):/) {
 | 
|---|
| 228 |         push @matchtypes, $mt{$1};
 | 
|---|
| 229 |         $args{$_} =~ s/^$1://;
 | 
|---|
| 230 |       } else {
 | 
|---|
| 231 |         push @matchtypes, '~*';
 | 
|---|
| 232 |       }
 | 
|---|
| 233 |       push @vals, $args{$_};
 | 
|---|
| 234 |     }
 | 
|---|
| 235 |   }
 | 
|---|
| 236 | 
 | 
|---|
| 237 |   my $cols = "s.cidr, s.custid, s.type, s.city, s.description, s.id, s.parent_id, s.available, s.vrf, a.dispname";
 | 
|---|
| 238 |   my $sql = qq(SELECT $cols FROM searchme s JOIN alloctypes a ON s.type = a.type);
 | 
|---|
| 239 |   my @sqlcriteria;
 | 
|---|
| 240 |   for (my $i = 0; $i <= $#fields; $i++) {
 | 
|---|
| 241 |     push @sqlcriteria, "$fields[$i] $matchtypes[$i] ?";
 | 
|---|
| 242 |   }
 | 
|---|
| 243 |   $sql .= " WHERE ".join(' AND ', @sqlcriteria) if @sqlcriteria;
 | 
|---|
| 244 | 
 | 
|---|
| 245 |   # multifield sorting!
 | 
|---|
| 246 |   if ($args{order}) {
 | 
|---|
| 247 |     my @ordfields = split /,/, $args{order};
 | 
|---|
| 248 |     # there are probably better ways to do this
 | 
|---|
| 249 |     my %omap = (cidr => 's.cidr', net => 's.cidr', network => 's.cidr', ip => 's.cidr',
 | 
|---|
| 250 |       custid => 's.custid', type => 's.type', city => 's.city',
 | 
|---|
| 251 |       desc => 's.description', description => 's.description');
 | 
|---|
| 252 |     my @ordlist;
 | 
|---|
| 253 |     # only pass sort field values from the list of acceptable field names or aliases as per %omap
 | 
|---|
| 254 |     foreach my $ord (@ordfields) {
 | 
|---|
| 255 |       push @ordlist, $omap{$ord}
 | 
|---|
| 256 |         if grep /^$ord$/, (keys %omap);
 | 
|---|
| 257 |     }
 | 
|---|
| 258 |     if (@ordlist) {
 | 
|---|
| 259 |       $sql .= " ORDER BY ". join(',', @ordlist);
 | 
|---|
| 260 |     }
 | 
|---|
| 261 |   }
 | 
|---|
| 262 | 
 | 
|---|
| 263 |   my $result = $ip_dbh->selectall_arrayref($sql, {Slice=>{}}, @vals);
 | 
|---|
| 264 |   die $ip_dbh->errstr if !$result;
 | 
|---|
| 265 | 
 | 
|---|
| 266 |   return $result;
 | 
|---|
| 267 | } # rpc_search()
 | 
|---|