| [54] | 1 | #!/usr/bin/perl | 
|---|
|  | 2 | # ipdb/cgi-bin/admin.cgi | 
|---|
|  | 3 | # Hack interface to make specific changes to IPDB that (for one reason | 
|---|
|  | 4 | # or another) can't be made through the main interface. | 
|---|
| [65] | 5 | # | 
|---|
| [54] | 6 | ### | 
|---|
|  | 7 | # SVN revision info | 
|---|
|  | 8 | # $Date: 2022-12-23 18:17:53 +0000 (Fri, 23 Dec 2022) $ | 
|---|
|  | 9 | # SVN revision $Rev: 941 $ | 
|---|
|  | 10 | # Last update by $Author: kdeugau $ | 
|---|
|  | 11 | ### | 
|---|
| [929] | 12 | # Copyright (C) 2004-2018,2022 - Kris Deugau | 
|---|
| [54] | 13 |  | 
|---|
|  | 14 | use strict; | 
|---|
|  | 15 | use warnings; | 
|---|
|  | 16 | use CGI::Carp qw(fatalsToBrowser); | 
|---|
| [517] | 17 | use CGI::Simple; | 
|---|
|  | 18 | use HTML::Template; | 
|---|
| [54] | 19 | use DBI; | 
|---|
|  | 20 | #use POSIX qw(ceil); | 
|---|
|  | 21 | use NetAddr::IP; | 
|---|
|  | 22 |  | 
|---|
|  | 23 | use Sys::Syslog; | 
|---|
|  | 24 |  | 
|---|
| [417] | 25 | # don't remove!  required for GNU/FHS-ish install from tarball | 
|---|
|  | 26 | ##uselib## | 
|---|
|  | 27 |  | 
|---|
| [906] | 28 | # push "the directory the script is in" into @INC | 
|---|
|  | 29 | use FindBin; | 
|---|
|  | 30 | use lib "$FindBin::RealBin/"; | 
|---|
|  | 31 |  | 
|---|
| [515] | 32 | use CustIDCK; | 
|---|
| [417] | 33 | use MyIPDB; | 
|---|
|  | 34 |  | 
|---|
| [431] | 35 | openlog "IPDB-admin","pid","$IPDB::syslog_facility"; | 
|---|
| [54] | 36 |  | 
|---|
|  | 37 | # Collect the username from HTTP auth.  If undefined, we're in a test environment. | 
|---|
|  | 38 | my $authuser; | 
|---|
|  | 39 | if (!defined($ENV{'REMOTE_USER'})) { | 
|---|
|  | 40 | $authuser = '__temptest'; | 
|---|
|  | 41 | } else { | 
|---|
|  | 42 | $authuser = $ENV{'REMOTE_USER'}; | 
|---|
|  | 43 | } | 
|---|
|  | 44 |  | 
|---|
|  | 45 | syslog "debug", "$authuser active"; | 
|---|
|  | 46 |  | 
|---|
| [517] | 47 | # Set up the CGI object... | 
|---|
|  | 48 | my $q = new CGI::Simple; | 
|---|
|  | 49 | # ... and get query-string params as well as POST params if necessary | 
|---|
|  | 50 | $q->parse_query_string; | 
|---|
|  | 51 |  | 
|---|
|  | 52 | # Convenience;  saves changing all references to %webvar | 
|---|
|  | 53 | ##fixme:  tweak for handling <select multiple='y' size=3> (list with multiple selection) | 
|---|
|  | 54 | my %webvar = $q->Vars; | 
|---|
|  | 55 |  | 
|---|
|  | 56 | # anyone got a better name?  :P | 
|---|
|  | 57 | my $thingroot = $ENV{SCRIPT_FILENAME}; | 
|---|
|  | 58 | $thingroot =~ s|cgi-bin/admin.cgi||; | 
|---|
|  | 59 |  | 
|---|
|  | 60 | # Set up some globals | 
|---|
| [801] | 61 | $ENV{HTML_TEMPLATE_ROOT} = $thingroot; | 
|---|
|  | 62 | my @templatepath = [ "localtemplates", "templates" ]; | 
|---|
| [517] | 63 |  | 
|---|
| [199] | 64 | # Why not a global DB handle?  (And a global statement handle, as well...) | 
|---|
|  | 65 | # Use the connectDB function, otherwise we end up confusing ourselves | 
|---|
|  | 66 | my $ip_dbh; | 
|---|
|  | 67 | my $sth; | 
|---|
|  | 68 | my $errstr; | 
|---|
|  | 69 | ($ip_dbh,$errstr) = connectDB_My; | 
|---|
|  | 70 | if (!$ip_dbh) { | 
|---|
| [517] | 71 | $webvar{action} = "dberr"; | 
|---|
|  | 72 | } else { | 
|---|
|  | 73 | initIPDBGlobals($ip_dbh); | 
|---|
| [199] | 74 | } | 
|---|
|  | 75 |  | 
|---|
| [548] | 76 | if(!defined($webvar{action})) { | 
|---|
|  | 77 | $webvar{action} = "main";   #shuts up the warnings. | 
|---|
|  | 78 | } | 
|---|
|  | 79 |  | 
|---|
| [517] | 80 | # handle DB error output | 
|---|
|  | 81 | if ($webvar{action} eq 'dberr') { | 
|---|
| [801] | 82 | my $page = HTML::Template->new(filename => "admin/dberr.tmpl", path => @templatepath); | 
|---|
| [517] | 83 | $page->param(errmsg => $errstr); | 
|---|
|  | 84 | print "Content-Type: text/html\n\n".$page->output; | 
|---|
|  | 85 | exit; | 
|---|
|  | 86 | } | 
|---|
|  | 87 |  | 
|---|
| [233] | 88 | if ($IPDBacl{$authuser} !~ /A/) { | 
|---|
| [801] | 89 | my $page = HTML::Template->new(filename => "admin/aclerr.tmpl", path => @templatepath); | 
|---|
| [517] | 90 | ##fixme:  need params for IPDB admin email and name | 
|---|
|  | 91 | $page->param(ipdbadmin_email => 'ipdbadmin@example.com'); | 
|---|
|  | 92 | $page->param(ipdbadmin_name => 'the IPDB administrator'); | 
|---|
|  | 93 | print "Content-Type: text/html\n\n".$page->output; | 
|---|
| [233] | 94 | exit; | 
|---|
|  | 95 | } | 
|---|
|  | 96 |  | 
|---|
| [801] | 97 | my $header = HTML::Template->new(filename => "admin/header.tmpl", path => @templatepath); | 
|---|
| [641] | 98 | $header->param(mainpage => 1) if $webvar{action} eq 'main'; | 
|---|
| [669] | 99 | $header->param(webpath => $IPDB::webpath); | 
|---|
| [641] | 100 | print "Content-type: text/html\n\n".$header->output; | 
|---|
| [58] | 101 |  | 
|---|
| [517] | 102 | my $page; | 
|---|
| [801] | 103 | if (-e "$ENV{HTML_TEMPLATE_ROOT}/templates/admin/$webvar{action}.tmpl") { | 
|---|
|  | 104 | $page = HTML::Template->new(filename => "admin/$webvar{action}.tmpl", path => @templatepath); | 
|---|
| [517] | 105 | } else { | 
|---|
| [801] | 106 | $page = HTML::Template->new(filename => "admin/dunno.tmpl", path => @templatepath); | 
|---|
| [517] | 107 | } | 
|---|
|  | 108 |  | 
|---|
|  | 109 | # handle index page | 
|---|
|  | 110 | if ($webvar{action} eq 'main') { | 
|---|
| [541] | 111 | my $tlist = getTypeList($ip_dbh, 'a'); | 
|---|
|  | 112 | $tlist->[0]->{sel} = 1; | 
|---|
|  | 113 | $page->param(typelist => $tlist); | 
|---|
| [199] | 114 |  | 
|---|
| [541] | 115 | my $mlist = getMasterList($ip_dbh, 'm'); | 
|---|
|  | 116 | $page->param(masterlist => $mlist); | 
|---|
| [941] | 117 |  | 
|---|
|  | 118 | my $nodelist = getNodeList($ip_dbh); | 
|---|
|  | 119 | $page->param(nodelist => $nodelist); | 
|---|
| [54] | 120 | } | 
|---|
|  | 121 |  | 
|---|
| [517] | 122 | ## Non-default actions. | 
|---|
| [199] | 123 |  | 
|---|
| [517] | 124 | elsif ($webvar{action} eq 'alloc') { | 
|---|
| [199] | 125 |  | 
|---|
| [929] | 126 | $webvar{cidr} =~ s/^\s+//; | 
|---|
|  | 127 | $webvar{cidr} =~ s/\s+$//; | 
|---|
| [543] | 128 | my $cidr = new NetAddr::IP $webvar{cidr}; | 
|---|
|  | 129 | if (!$cidr || "$cidr" =~ /^0/) { | 
|---|
| [517] | 130 | $page->param(errmsg => "Can't allocate something that's not a netblock/ip"); | 
|---|
|  | 131 | goto ERRJUMP; | 
|---|
| [199] | 132 | } | 
|---|
|  | 133 |  | 
|---|
| [544] | 134 | my $custid = $def_custids{$webvar{alloctype}}; | 
|---|
| [199] | 135 | if ($custid eq '') { | 
|---|
| [546] | 136 | # Crosscheck with billing. | 
|---|
|  | 137 | my $status = CustIDCK->custid_exist($webvar{custid}); | 
|---|
|  | 138 | if ($CustIDCK::Error) { | 
|---|
|  | 139 | $page->param(errmsg => "Error verifying customer ID: ".$CustIDCK::ErrMsg); | 
|---|
|  | 140 | goto ERRJUMP; | 
|---|
| [400] | 141 | } | 
|---|
| [546] | 142 | if (!$status) { | 
|---|
|  | 143 | $page->param(errmsg => "Customer ID not valid.  Make sure the Customer ID ". | 
|---|
|  | 144 | "is correct.<br>\nUse STAFF for staff static IPs, and $IPDB::defcustid for any other ". | 
|---|
|  | 145 | "non-customer assignments."); | 
|---|
|  | 146 | goto ERRJUMP; | 
|---|
|  | 147 | } | 
|---|
| [199] | 148 | # Type that doesn't have a default custid | 
|---|
|  | 149 | $custid = $webvar{custid}; | 
|---|
|  | 150 | } | 
|---|
|  | 151 |  | 
|---|
| [544] | 152 | my $maskbits = $cidr->masklen; | 
|---|
| [642] | 153 | my ($fbid, $fb, $fbparent); | 
|---|
|  | 154 | ($fbid, $fb, $fbparent) = findAllocateFrom($ip_dbh, $maskbits, $webvar{alloctype}, '','', | 
|---|
| [544] | 155 | (gimme => "$cidr", allowpriv => 1)); | 
|---|
| [915] | 156 |  | 
|---|
|  | 157 | # retrieve any notices | 
|---|
|  | 158 | my $nlist = getBlockNotices($ip_dbh, $fbparent); | 
|---|
|  | 159 | my @notices; | 
|---|
|  | 160 | foreach (@$nlist) { | 
|---|
|  | 161 | push @notices, $_->{notice} if $_->{notice}; | 
|---|
|  | 162 | } | 
|---|
|  | 163 | my $blockmsg = join("<br>\n", @notices); | 
|---|
|  | 164 | $page->param(blockmsg => $blockmsg); | 
|---|
|  | 165 |  | 
|---|
| [642] | 166 | $page->param(fbid => $fbid); | 
|---|
|  | 167 | $page->param(parid => $fbparent); | 
|---|
| [880] | 168 | my ($rdns,$rcached) = getBlockRDNS($ip_dbh, id => $fbparent, type => ($webvar{alloctype} =~ /^.i$/ ? 'i' : 'b'), | 
|---|
| [642] | 169 | user => $authuser); | 
|---|
|  | 170 | $page->param(rdns => $rdns); | 
|---|
|  | 171 | if ($webvar{alloctype} =~ /^(.)i$/) { | 
|---|
| [544] | 172 | my $iptype = $1; | 
|---|
| [642] | 173 | my $ptmp = getBlockData($ip_dbh, $fbparent); | 
|---|
| [544] | 174 | if ($ptmp->{type} =~ /^(.)[dp]$/) { | 
|---|
|  | 175 | my $newiptype = "$1i"; | 
|---|
|  | 176 | if ($ptmp->{type} !~ /^$iptype./) { | 
|---|
|  | 177 | $page->param(warnmsg => "Warning:  Allocating IP as '".$disp_alloctypes{$newiptype}."' instead of '". | 
|---|
| [642] | 178 | $disp_alloctypes{$webvar{alloctype}}."' to match pool ".$ptmp->{block}."\n"); | 
|---|
| [544] | 179 | $webvar{alloctype} = $newiptype; | 
|---|
|  | 180 | } | 
|---|
|  | 181 | } | 
|---|
| [642] | 182 | if (!$fbid) { | 
|---|
| [517] | 183 | $page->param(errmsg => "Can't allocate static IP from outside a pool!!"); | 
|---|
|  | 184 | goto ERRJUMP; | 
|---|
|  | 185 | } | 
|---|
| [199] | 186 | } else { | 
|---|
| [642] | 187 | if (!$fbid) { | 
|---|
|  | 188 | $page->param(errmsg => "Can't allocate from outside a free block!!"); | 
|---|
| [517] | 189 | goto ERRJUMP; | 
|---|
|  | 190 | } | 
|---|
| [199] | 191 | } | 
|---|
|  | 192 |  | 
|---|
| [642] | 193 | my $alloc_from = new NetAddr::IP $fb; | 
|---|
| [199] | 194 |  | 
|---|
| [517] | 195 | my @cities; | 
|---|
| [199] | 196 | foreach my $city (@citylist) { | 
|---|
| [517] | 197 | my %row = (city => $city); | 
|---|
|  | 198 | push @cities, \%row; | 
|---|
| [199] | 199 | } | 
|---|
| [517] | 200 | $page->param( | 
|---|
|  | 201 | cidr => $cidr, | 
|---|
|  | 202 | disptype => $disp_alloctypes{$webvar{alloctype}}, | 
|---|
|  | 203 | type => $webvar{alloctype}, | 
|---|
|  | 204 | alloc_from => $alloc_from, | 
|---|
|  | 205 | custid => $custid, | 
|---|
|  | 206 | citylist => \@cities | 
|---|
|  | 207 | ); | 
|---|
| [199] | 208 |  | 
|---|
|  | 209 | } elsif ($webvar{action} eq 'confirm') { | 
|---|
|  | 210 |  | 
|---|
| [517] | 211 | $page->param( | 
|---|
|  | 212 | cidr => $webvar{cidr}, | 
|---|
|  | 213 | custid => $webvar{custid}, | 
|---|
|  | 214 | desc => $webvar{desc}, | 
|---|
|  | 215 | disptype => $disp_alloctypes{$webvar{alloctype}} | 
|---|
|  | 216 | ); | 
|---|
| [199] | 217 | # Only need to check city here. | 
|---|
|  | 218 | if ($webvar{city} eq '-') { | 
|---|
| [517] | 219 | $page->param(locerr => "Invalid customer location!  Go back and select customer's location."); | 
|---|
|  | 220 | goto ERRJUMP; | 
|---|
| [545] | 221 | } | 
|---|
|  | 222 |  | 
|---|
| [915] | 223 | # retrieve any notices | 
|---|
|  | 224 | my $nlist = getBlockNotices($ip_dbh, $webvar{parent}); | 
|---|
|  | 225 | my @notices; | 
|---|
|  | 226 | foreach (@$nlist) { | 
|---|
|  | 227 | push @notices, $_->{notice} if $_->{notice}; | 
|---|
|  | 228 | } | 
|---|
|  | 229 | my $blockmsg = join("<br>\n", @notices); | 
|---|
|  | 230 | $page->param(blockmsg => $blockmsg); | 
|---|
|  | 231 |  | 
|---|
| [642] | 232 | my ($retcode,$msg) = allocateBlock($ip_dbh, cidr => $webvar{cidr}, fbid => $webvar{fbid}, | 
|---|
|  | 233 | parent => $webvar{parent}, custid => $webvar{custid}, type => $webvar{alloctype}, city => $webvar{city}, | 
|---|
|  | 234 | desc => $webvar{desc}, notes => $webvar{notes}, circid => $webvar{circid}, | 
|---|
|  | 235 | privdata => $webvar{privdata}, nodeid => $webvar{node}, rdns => $webvar{rdns}, user => $authuser); | 
|---|
|  | 236 |  | 
|---|
| [545] | 237 | if ($retcode eq 'OK') { | 
|---|
|  | 238 | syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
|  | 239 | "'$webvar{alloctype}'"; | 
|---|
|  | 240 | if ($webvar{alloctype} =~ /^.i$/) { | 
|---|
|  | 241 | mailNotify($ip_dbh, "a$webvar{alloctype}", "ADDED: $disp_alloctypes{$webvar{alloctype}} allocation", | 
|---|
|  | 242 | "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer $webvar{custid}\n". | 
|---|
| [881] | 243 | "Description: $webvar{desc}\n". | 
|---|
|  | 244 | ($webvar{rdns} ? "DNS name: $webvar{rdns}\n" : ''). | 
|---|
|  | 245 | "\nAllocated by: $authuser\n"); | 
|---|
| [545] | 246 | } | 
|---|
|  | 247 | } else { | 
|---|
|  | 248 | $page->param(errmsg => $msg); | 
|---|
|  | 249 | syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ". | 
|---|
|  | 250 | "'$webvar{alloctype}' failed: '$msg'"; | 
|---|
|  | 251 | } | 
|---|
| [199] | 252 |  | 
|---|
| [54] | 253 | } elsif ($webvar{action} eq 'alloctweak') { | 
|---|
| [517] | 254 |  | 
|---|
| [54] | 255 | fix_allocfrom(); | 
|---|
|  | 256 | showAllocs($webvar{allocfrom}); | 
|---|
| [517] | 257 |  | 
|---|
| [54] | 258 | } elsif ($webvar{action} eq 'update') { | 
|---|
| [517] | 259 |  | 
|---|
| [54] | 260 | update(); | 
|---|
| [58] | 261 |  | 
|---|
| [517] | 262 | } elsif ($webvar{action} eq 'touch') { | 
|---|
| [58] | 263 |  | 
|---|
| [547] | 264 | my ($code,$msg) = touchMaster($ip_dbh, $webvar{whichmaster}); | 
|---|
|  | 265 | $page->param(errmsg => $msg) if $code eq 'FAIL'; | 
|---|
| [517] | 266 |  | 
|---|
| [329] | 267 | } elsif ($webvar{action} eq 'listcust') { | 
|---|
| [517] | 268 |  | 
|---|
| [709] | 269 | my $clist = $ip_dbh->selectall_arrayref("SELECT custid,name AS custname,tech_handle AS tech ". | 
|---|
|  | 270 | "FROM customers ORDER BY custid", { Slice => {} } ); | 
|---|
|  | 271 | $page->param(custlist => $clist); | 
|---|
| [517] | 272 |  | 
|---|
| [331] | 273 | } elsif ($webvar{action} eq 'edcust') { | 
|---|
| [517] | 274 |  | 
|---|
| [418] | 275 | if ($webvar{newcust}) { | 
|---|
|  | 276 | $sth = $ip_dbh->prepare("INSERT INTO customers (custid) VALUES (?)"); | 
|---|
|  | 277 | $sth->execute($webvar{custid}); | 
|---|
|  | 278 | } | 
|---|
| [331] | 279 | $sth = $ip_dbh->prepare("select custid,name,street,city,province,". | 
|---|
| [436] | 280 | "country,pocode,phone,tech_handle,abuse_handle,admin_handle,special ". | 
|---|
| [331] | 281 | "from customers where custid='$webvar{custid}'"); | 
|---|
|  | 282 | $sth->execute; | 
|---|
| [436] | 283 | my ($custid, $name, $street, $city, $prov, $country, $pocode, $phone, $tech, $abuse, $admin, $special) = | 
|---|
| [331] | 284 | $sth->fetchrow_array; | 
|---|
|  | 285 |  | 
|---|
| [517] | 286 | $page->param( | 
|---|
|  | 287 | custid => $custid, | 
|---|
|  | 288 | name => $name, | 
|---|
|  | 289 | street => $street, | 
|---|
|  | 290 | city => $city, | 
|---|
|  | 291 | prov => $prov, | 
|---|
|  | 292 | country => $country, | 
|---|
|  | 293 | pocode => $pocode, | 
|---|
|  | 294 | phone => $phone, | 
|---|
|  | 295 | tech => $tech, | 
|---|
|  | 296 | abuse => $abuse, | 
|---|
|  | 297 | admin => $admin, | 
|---|
|  | 298 | special => $special | 
|---|
|  | 299 | ); | 
|---|
|  | 300 |  | 
|---|
| [329] | 301 | } elsif ($webvar{action} eq 'updcust') { | 
|---|
| [517] | 302 |  | 
|---|
| [709] | 303 | if ($webvar{abutton} eq 'Update') { | 
|---|
|  | 304 | $ip_dbh->do(q( | 
|---|
|  | 305 | UPDATE customers | 
|---|
|  | 306 | SET name = ?, street = ?, city = ?, province=?, country=?, pocode=?, | 
|---|
|  | 307 | phone = ?, tech_handle = ?, abuse_handle = ?, admin_handle = ?, special = ? | 
|---|
|  | 308 | WHERE custid = ? | 
|---|
|  | 309 | ), undef, | 
|---|
|  | 310 | ($webvar{name}, $webvar{street}, $webvar{city}, $webvar{province}, $webvar{country}, $webvar{pocode}, | 
|---|
|  | 311 | $webvar{phone}, $webvar{tech_handle}, $webvar{abuse_handle}, $webvar{admin_handle}, $webvar{special}, | 
|---|
|  | 312 | $webvar{custid}) | 
|---|
|  | 313 | ); | 
|---|
|  | 314 | $page->param(whatact => "Updated"); | 
|---|
|  | 315 | } elsif ($webvar{abutton} eq 'Delete (immediate)') { | 
|---|
|  | 316 | $ip_dbh->do("DELETE FROM customers WHERE custid = ?", undef, $webvar{custid}); | 
|---|
|  | 317 | $page->param(whatact => "Deleted"); | 
|---|
|  | 318 | } else { | 
|---|
|  | 319 | # Your llama is on fire | 
|---|
|  | 320 | } | 
|---|
|  | 321 | # Show what we've just either updated or deleted. | 
|---|
|  | 322 | # Deleted, so that in case of "OOOPS!!", the "special" data can be recovered. | 
|---|
| [517] | 323 | $page->param( | 
|---|
|  | 324 | custid => $webvar{custid}, | 
|---|
|  | 325 | name => $webvar{name}, | 
|---|
|  | 326 | street => $webvar{street}, | 
|---|
|  | 327 | city => $webvar{city}, | 
|---|
|  | 328 | prov => $webvar{province}, | 
|---|
|  | 329 | country => $webvar{country}, | 
|---|
|  | 330 | pocode => $webvar{pocode}, | 
|---|
|  | 331 | phone => $webvar{phone}, | 
|---|
|  | 332 | tech => $webvar{tech_handle}, | 
|---|
|  | 333 | abuse => $webvar{abuse_handle}, | 
|---|
|  | 334 | admin => $webvar{admin_handle}, | 
|---|
|  | 335 | special => $webvar{special} | 
|---|
|  | 336 | ); | 
|---|
| [418] | 337 |  | 
|---|
| [65] | 338 | } elsif ($webvar{action} eq 'showpools') { | 
|---|
| [517] | 339 |  | 
|---|
| [644] | 340 | my $plist = $ip_dbh->selectall_arrayref("SELECT a.id,a.cidr AS pool, count(*) AS free FROM poolips p ". | 
|---|
|  | 341 | "JOIN allocations a ON a.id=p.parent_id ". | 
|---|
|  | 342 | "WHERE available='y' GROUP BY a.cidr,a.id ORDER BY a.cidr", { Slice => {} }); | 
|---|
|  | 343 | $page->param(poollist => $plist); | 
|---|
| [517] | 344 |  | 
|---|
| [65] | 345 | } elsif ($webvar{action} eq 'tweakpool') { | 
|---|
| [517] | 346 |  | 
|---|
| [65] | 347 | showPool($webvar{pool}); | 
|---|
| [517] | 348 |  | 
|---|
| [65] | 349 | } elsif ($webvar{action} eq 'updatepool') { | 
|---|
| [199] | 350 |  | 
|---|
| [644] | 351 | my $ip = $ip_dbh->selectrow_array("SELECT ip FROM poolips WHERE id=?", undef, ($webvar{id}) ); | 
|---|
|  | 352 | $page->param(ip => $ip); | 
|---|
|  | 353 | $sth = $ip_dbh->prepare("UPDATE poolips SET custid=?, city=?, type=?, available='". | 
|---|
| [65] | 354 | (($webvar{available} eq 'y') ? 'y' : 'n'). | 
|---|
| [517] | 355 | "', notes=?, description=? ". | 
|---|
| [644] | 356 | "WHERE id=?"); | 
|---|
|  | 357 | $sth->execute($webvar{custid}, $webvar{city}, $webvar{type}, $webvar{notes}, $webvar{desc}, $webvar{id}); | 
|---|
| [65] | 358 | if ($sth->err) { | 
|---|
| [517] | 359 | $page->param(errmsg => $sth->errstr); | 
|---|
| [644] | 360 | syslog "err", "$authuser could not update pool IP $ip: ".$sth->errstr; | 
|---|
| [517] | 361 | } else { | 
|---|
| [644] | 362 | syslog "notice", "$authuser updated pool IP $ip"; | 
|---|
| [65] | 363 | } | 
|---|
| [644] | 364 | my $poolid = $ip_dbh->selectrow_array("SELECT parent_id FROM poolips WHERE id=?", undef, ($webvar{id}) ); | 
|---|
|  | 365 | $page->param(poolid => $poolid); | 
|---|
|  | 366 | my $pool = $ip_dbh->selectrow_array("SELECT cidr FROM allocations WHERE id=?", undef, ($poolid) ); | 
|---|
|  | 367 | $page->param(pool => $pool); | 
|---|
| [517] | 368 |  | 
|---|
| [941] | 369 | } elsif ($webvar{action} eq 'nodereport') { | 
|---|
|  | 370 | my $nodename = getNodeName($ip_dbh, $webvar{node}); | 
|---|
|  | 371 | $page->param(node_name => $nodename); | 
|---|
|  | 372 | $page->param(node => $webvar{node}); | 
|---|
|  | 373 | $page->param(webpath => $IPDB::webpath); | 
|---|
|  | 374 | # can't use this until any disjoint between allocation IDs and pool IP IDs is resolved. | 
|---|
|  | 375 | #  my $alloclist = $ip_dbh->selectall_arrayref("SELECT cidr,custid,type,description FROM searchme WHERE node_id = ?", | 
|---|
|  | 376 | #       { Slice => {} }, $webvar{node_id}); | 
|---|
|  | 377 | my $alloclist = $ip_dbh->selectall_arrayref("SELECT a.id,a.cidr,a.custid,at.dispname,a.circuitid,a.description FROM allocations a". | 
|---|
|  | 378 | " JOIN noderef nr ON a.cidr=nr.block JOIN alloctypes at ON a.type=at.type". | 
|---|
|  | 379 | " WHERE nr.node_id = ? AND NOT a.type LIKE '_m'", | 
|---|
|  | 380 | { Slice => {} }, $webvar{node}); | 
|---|
|  | 381 | $page->param(alloclist => $alloclist); | 
|---|
|  | 382 |  | 
|---|
| [258] | 383 | } elsif ($webvar{action} eq 'showusers') { | 
|---|
| [233] | 384 |  | 
|---|
|  | 385 | $sth = $ip_dbh->prepare("select username,acl from users order by username"); | 
|---|
|  | 386 | $sth->execute; | 
|---|
| [517] | 387 | my @userlist; | 
|---|
|  | 388 | while (my ($username,$acl) = $sth->fetchrow_array) { | 
|---|
|  | 389 | ##fixme: funky things happening with HTML::Template here;  shouldn't need the "logic ? iftrue : iffalse" structure | 
|---|
|  | 390 | my %row = ( | 
|---|
|  | 391 | username => $username, | 
|---|
|  | 392 | can_add => ($acl =~ /a/ ? 1 : 0), | 
|---|
|  | 393 | can_change => ($acl =~ /c/ ? 1 : 0), | 
|---|
|  | 394 | can_del => ($acl =~ /d/ ? 1 : 0), | 
|---|
|  | 395 | sysnet => ($acl =~ /s/ ? 1 : 0), | 
|---|
| [789] | 396 | can_merge => ($acl =~ /m/ ? 1 : 0), | 
|---|
| [517] | 397 | is_admin => ($acl =~ /A/ ? 1 : 0), | 
|---|
|  | 398 | acl => $acl | 
|---|
|  | 399 | ); | 
|---|
|  | 400 | push @userlist, \%row; | 
|---|
|  | 401 | } | 
|---|
|  | 402 | $page->param(userlist => \@userlist); | 
|---|
| [233] | 403 |  | 
|---|
|  | 404 | } elsif ($webvar{action} eq 'updacl') { | 
|---|
| [517] | 405 |  | 
|---|
|  | 406 | $page->param(username => $webvar{username}); | 
|---|
| [233] | 407 | my $acl = 'b'; | 
|---|
|  | 408 | if ($webvar{admin} eq 'on') { | 
|---|
| [789] | 409 | # as per request "admin" users do not automatically get merge permission.  Networkz iz hard. | 
|---|
|  | 410 | # Admin users that add the priviledge and then shoot everybody in all the feet probably | 
|---|
|  | 411 | # shouldn't have had admin access in the first place. | 
|---|
| [284] | 412 | $acl .= "acdsA"; | 
|---|
| [789] | 413 | $acl .= 'm' if $webvar{merge} eq 'on'; | 
|---|
| [233] | 414 | } else { | 
|---|
|  | 415 | $acl .= ($webvar{add} eq 'on' ? 'a' : ''). | 
|---|
|  | 416 | ($webvar{change} eq 'on' ? 'c' : ''). | 
|---|
| [284] | 417 | ($webvar{del} eq 'on' ? 'd' : ''). | 
|---|
| [789] | 418 | ($webvar{sysnet} eq 'on' ? 's' : ''). | 
|---|
|  | 419 | ($webvar{merge} eq 'on' ? 'm' : ''); | 
|---|
| [233] | 420 | } | 
|---|
| [517] | 421 | $page->param(acl => $acl); | 
|---|
| [233] | 422 |  | 
|---|
|  | 423 | $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'"); | 
|---|
|  | 424 | $sth->execute; | 
|---|
| [517] | 425 | $page->param(errmsg => $sth->errstr) if $sth->err; | 
|---|
| [233] | 426 |  | 
|---|
| [517] | 427 | } elsif ($webvar{action} eq 'newuser') { | 
|---|
| [233] | 428 |  | 
|---|
| [517] | 429 | $page->param(username => $webvar{username}); | 
|---|
| [259] | 430 | my $cr_pass = ($webvar{preenc} ? $webvar{password} : | 
|---|
|  | 431 | crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64])); | 
|---|
| [258] | 432 | $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ". | 
|---|
|  | 433 | "('$webvar{username}','$cr_pass','b')"); | 
|---|
|  | 434 | $sth->execute; | 
|---|
| [517] | 435 | $page->param(errmsg => $sth->errstr) if $sth->err; | 
|---|
| [258] | 436 |  | 
|---|
| [517] | 437 | } elsif ($webvar{action} eq 'deluser') { | 
|---|
| [258] | 438 |  | 
|---|
| [517] | 439 | $page->param(username => $webvar{username}); | 
|---|
| [258] | 440 | $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'"); | 
|---|
|  | 441 | $sth->execute; | 
|---|
| [517] | 442 | $page->param(errmsg => $sth->errstr) if $sth->err; | 
|---|
| [258] | 443 |  | 
|---|
| [517] | 444 | } elsif ($webvar{action} eq 'emailnotice') { | 
|---|
| [258] | 445 |  | 
|---|
| [422] | 446 | $sth = $ip_dbh->prepare("SELECT action,reciplist FROM notify"); | 
|---|
|  | 447 | $sth->execute; | 
|---|
| [517] | 448 | my @spamlist; | 
|---|
| [422] | 449 | while (my ($notice_code,$reciplist) = $sth->fetchrow_array() ) { | 
|---|
|  | 450 | ##fixme: hairy mess, only a few things call mailNotify() anyway, so many possible notices won't work. | 
|---|
|  | 451 | my $action_out = dispNoticeCode($notice_code); | 
|---|
| [517] | 452 | my %row = ( | 
|---|
|  | 453 | action => $action_out, | 
|---|
|  | 454 | code => $notice_code, | 
|---|
|  | 455 | recips => $reciplist | 
|---|
|  | 456 | ); | 
|---|
|  | 457 | push @spamlist, \%row; | 
|---|
| [422] | 458 | } | 
|---|
| [517] | 459 | $page->param(spamlist => \@spamlist); | 
|---|
| [423] | 460 |  | 
|---|
|  | 461 | $sth = $ip_dbh->prepare("SELECT type,dispname FROM alloctypes WHERE listorder < 500 ". | 
|---|
|  | 462 | "ORDER BY listorder"); | 
|---|
|  | 463 | $sth->execute; | 
|---|
|  | 464 | my $i=0; | 
|---|
| [517] | 465 | my @typelist; | 
|---|
| [423] | 466 | while (my ($type,$disp) = $sth->fetchrow_array) { | 
|---|
| [517] | 467 | my %row = ( | 
|---|
|  | 468 | type => $type, | 
|---|
|  | 469 | disptype => $disp, | 
|---|
|  | 470 | # ahh, off-by-one counts, how we do love thee...  NOT! | 
|---|
|  | 471 | newrow => ($i+2 > $sth->rows ? 1 : (++$i % 4)), | 
|---|
|  | 472 | ); | 
|---|
|  | 473 | push @typelist, \%row; | 
|---|
| [423] | 474 | } | 
|---|
| [517] | 475 | $page->param(typelist => \@typelist); | 
|---|
| [423] | 476 |  | 
|---|
| [517] | 477 | } elsif ($webvar{action} eq 'addnotice') { | 
|---|
| [423] | 478 |  | 
|---|
| [426] | 479 | $webvar{alloctype} = $webvar{special} if $webvar{msgaction} eq 's:'; | 
|---|
|  | 480 | if ($webvar{msgaction} && $webvar{alloctype} && $webvar{reciplist}) { | 
|---|
| [517] | 481 | $page->param(cantry => 1); | 
|---|
| [426] | 482 | $webvar{reciplist} =~ s/[\r\n]+/,/g; | 
|---|
| [438] | 483 | $webvar{msgaction} = "f:$webvar{msgaction}" if $webvar{onfail}; | 
|---|
| [517] | 484 | $page->param(reciplist => $webvar{reciplist}); | 
|---|
|  | 485 | $page->param(dispnotice => dispNoticeCode($webvar{msgaction}.$webvar{alloctype})); | 
|---|
| [426] | 486 | $sth = $ip_dbh->prepare("INSERT INTO notify (action, reciplist) VALUES (?,?)"); | 
|---|
| [423] | 487 | ##fixme:  automagically merge reciplists iff action already exists | 
|---|
| [426] | 488 | $sth->execute($webvar{msgaction}.$webvar{alloctype}, $webvar{reciplist}); | 
|---|
| [517] | 489 | $page->param(addfailed => $sth->errstr) if $sth->err; | 
|---|
| [423] | 490 | } | 
|---|
|  | 491 |  | 
|---|
| [424] | 492 | } elsif ($webvar{action} eq 'delnotice') { | 
|---|
| [517] | 493 |  | 
|---|
|  | 494 | $page->param(dispnotice => dispNoticeCode($webvar{code}.$webvar{alloctype})); | 
|---|
| [424] | 495 | $sth = $ip_dbh->prepare("DELETE FROM notify WHERE action=?"); | 
|---|
|  | 496 | $sth->execute($webvar{code}); | 
|---|
| [517] | 497 | $page->param(delfailed => $sth->errstr) if $sth->err; | 
|---|
| [424] | 498 |  | 
|---|
| [422] | 499 | } elsif ($webvar{action} eq 'ednotice') { | 
|---|
| [517] | 500 |  | 
|---|
|  | 501 | $page->param(dispnotice => dispNoticeCode($webvar{code})); | 
|---|
|  | 502 | $page->param(code => $webvar{code}); | 
|---|
| [422] | 503 | $sth = $ip_dbh->prepare("SELECT reciplist FROM notify WHERE action=?"); | 
|---|
|  | 504 | $sth->execute($webvar{code}); | 
|---|
|  | 505 | my ($reciplist) = $sth->fetchrow_array; | 
|---|
|  | 506 | $reciplist =~ s/,/\n/g; | 
|---|
| [517] | 507 | $page->param(reciplist => $reciplist); | 
|---|
|  | 508 |  | 
|---|
| [422] | 509 | } elsif ($webvar{action} eq 'updnotice') { | 
|---|
| [517] | 510 |  | 
|---|
|  | 511 | $page->param(dispnotice => dispNoticeCode($webvar{code})); | 
|---|
| [422] | 512 | $sth = $ip_dbh->prepare("UPDATE notify SET reciplist=? WHERE action=?"); | 
|---|
|  | 513 | $webvar{reciplist} =~ s/[\r\n]+/,/g; | 
|---|
|  | 514 | $sth->execute($webvar{reciplist}, $webvar{code}); | 
|---|
| [517] | 515 | $page->param(updfailed => $sth->errstr) if $sth->err; | 
|---|
|  | 516 |  | 
|---|
| [256] | 517 | } elsif ($webvar{action} ne '<NULL>') { | 
|---|
| [517] | 518 | $page->param(dunno => $webvar{action}); | 
|---|
| [54] | 519 | } | 
|---|
|  | 520 |  | 
|---|
| [643] | 521 | ERRJUMP: | 
|---|
| [517] | 522 | print $page->output; | 
|---|
| [54] | 523 |  | 
|---|
| [517] | 524 | ##fixme:  make me a footer param! | 
|---|
| [643] | 525 | print qq(<hr><div><a href="$IPDB::webpath/">Back</a> to the main IPDB</div>\n); | 
|---|
| [54] | 526 |  | 
|---|
| [517] | 527 | # We print the footer here, so we don't have to do it elsewhere. | 
|---|
| [801] | 528 | my $footer = HTML::Template->new(filename => "footer.tmpl", path => @templatepath); | 
|---|
| [517] | 529 | # we're already in the admin tools, no need to provide a bottom link.  maybe. | 
|---|
|  | 530 | #$footer->param(adminlink => ($IPDBacl{$authuser} =~ /A/)); | 
|---|
| [54] | 531 |  | 
|---|
| [517] | 532 | print $footer->output; | 
|---|
|  | 533 |  | 
|---|
| [708] | 534 | $sth->finish if $sth; | 
|---|
| [54] | 535 | $ip_dbh->disconnect; | 
|---|
|  | 536 |  | 
|---|
|  | 537 | exit; | 
|---|
|  | 538 |  | 
|---|
|  | 539 |  | 
|---|
| [517] | 540 | # Hokay.  This is a little different.  We have a few specific functions here: | 
|---|
|  | 541 | #  -> Assign arbitrary subnet from arbitrary free space | 
|---|
|  | 542 | #  -> Tweak individual DB fields | 
|---|
|  | 543 | # | 
|---|
|  | 544 |  | 
|---|
|  | 545 |  | 
|---|
| [54] | 546 | # Tweak allocfrom into shape. | 
|---|
|  | 547 | sub fix_allocfrom { | 
|---|
|  | 548 | if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) { | 
|---|
|  | 549 | # 3-octet class C specified | 
|---|
|  | 550 | $webvar{allocfrom} .= ".0/24"; | 
|---|
|  | 551 | } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) { | 
|---|
|  | 552 | # 4-octet IP specified; | 
|---|
|  | 553 | $webvar{allocfrom} .= "/24"; | 
|---|
|  | 554 | } | 
|---|
|  | 555 | } | 
|---|
|  | 556 |  | 
|---|
|  | 557 |  | 
|---|
| [517] | 558 | # Show allocations to allow editing. | 
|---|
|  | 559 | sub showAllocs { | 
|---|
| [58] | 560 |  | 
|---|
| [517] | 561 | my $within = new NetAddr::IP $_[0]; | 
|---|
|  | 562 | $page->param(within => $within); | 
|---|
| [58] | 563 |  | 
|---|
| [647] | 564 |  | 
|---|
|  | 565 | $sth = $ip_dbh->prepare("SELECT id,vrf,cidr,custid,type,city,description FROM allocations". | 
|---|
|  | 566 | " WHERE cidr <<= ? ORDER BY vrf,cidr"); | 
|---|
|  | 567 | $sth->execute($within); | 
|---|
| [517] | 568 | my @blocklist; | 
|---|
| [647] | 569 | while (my ($id,$vrf,$cidr,$custid,$type,$city,$desc) = $sth->fetchrow_array) { | 
|---|
| [517] | 570 | my %row = ( | 
|---|
| [647] | 571 | id => $id, | 
|---|
| [517] | 572 | cidr => $cidr, | 
|---|
|  | 573 | custid => $custid, | 
|---|
|  | 574 | city => $city, | 
|---|
|  | 575 | desc => $desc, | 
|---|
|  | 576 | ); | 
|---|
| [54] | 577 |  | 
|---|
| [517] | 578 | ##fixme:  don't wanna retrieve the whole type list *every time around the outer loop* | 
|---|
| [647] | 579 | my $sth2 = $ip_dbh->prepare("SELECT type,listname FROM alloctypes". | 
|---|
|  | 580 | " WHERE listorder < 999 AND NOT (type LIKE '_i') ORDER BY listorder"); | 
|---|
| [348] | 581 | $sth2->execute; | 
|---|
| [517] | 582 | my @typelist; | 
|---|
|  | 583 | while (my ($listtype,$dispname) = $sth2->fetchrow_array) { | 
|---|
|  | 584 | my %subrow = ( | 
|---|
|  | 585 | type => $listtype, | 
|---|
|  | 586 | dispname => $dispname, | 
|---|
|  | 587 | selected => ($listtype eq $type) | 
|---|
|  | 588 | ); | 
|---|
|  | 589 | push @typelist, \%subrow; | 
|---|
| [348] | 590 | } | 
|---|
| [517] | 591 | $row{typelist} = \@typelist; | 
|---|
|  | 592 | push @blocklist, \%row; | 
|---|
| [54] | 593 | } | 
|---|
| [517] | 594 | $page->param(blocklist => \@blocklist); | 
|---|
|  | 595 | } # end showAllocs() | 
|---|
| [54] | 596 |  | 
|---|
|  | 597 |  | 
|---|
|  | 598 | # Stuff updates into DB | 
|---|
|  | 599 | sub update { | 
|---|
| [647] | 600 | my $cidr = $ip_dbh->selectrow_array("SELECT cidr FROM allocations WHERE id=?", undef, ($webvar{block}) ); | 
|---|
|  | 601 |  | 
|---|
| [517] | 602 | # Relatively simple SQL transaction here.  Note that we're deliberately NOT | 
|---|
|  | 603 | # updating notes/desc here as it's available through the main interface. | 
|---|
| [647] | 604 | $ip_dbh->do("UPDATE allocations SET custid=?, city=?, type=? WHERE id=?", undef, | 
|---|
|  | 605 | ($webvar{custid}, $webvar{city}, $webvar{alloctype}, $webvar{block}) ); | 
|---|
| [517] | 606 |  | 
|---|
| [647] | 607 | $page->param(block => $cidr); | 
|---|
|  | 608 | if ($ip_dbh->err) { | 
|---|
|  | 609 | $page->param(updfailed => $ip_dbh->errstr); | 
|---|
|  | 610 | syslog "err", "$authuser could not update block '$cidr': '".$ip_dbh->errstr."'"; | 
|---|
| [54] | 611 | } else { | 
|---|
| [647] | 612 | syslog "notice", "$authuser updated $cidr"; | 
|---|
| [54] | 613 | } | 
|---|
|  | 614 | # need to get /24 that block is part of | 
|---|
| [647] | 615 | my @bits = split /\./, $cidr; | 
|---|
| [54] | 616 | $bits[3] = "0/24"; | 
|---|
|  | 617 | showAllocs((join ".", @bits)); | 
|---|
| [517] | 618 | } # end update() | 
|---|
| [65] | 619 |  | 
|---|
|  | 620 |  | 
|---|
|  | 621 | # showPool() | 
|---|
|  | 622 | # List all IPs in a pool, and allow arbitrary admin changes to each | 
|---|
|  | 623 | # Allow changes to ALL fields | 
|---|
| [548] | 624 | sub showPool { | 
|---|
| [647] | 625 | my $pool = shift; | 
|---|
| [348] | 626 |  | 
|---|
| [647] | 627 | # arguably even presenting a list here is Wrong, because Pool IPs Should Alwasy Match The Pool Type, but... | 
|---|
|  | 628 | # could also set "selected" on the "correct" type | 
|---|
|  | 629 | my $tlist = getTypeList($ip_dbh, 'i'); | 
|---|
|  | 630 | $page->param(typelist => $tlist); | 
|---|
| [348] | 631 |  | 
|---|
| [647] | 632 | $sth = $ip_dbh->prepare("SELECT id,ip,custid,city,type,available,description,notes from poolips". | 
|---|
|  | 633 | " WHERE parent_id=? ORDER BY ip"); | 
|---|
| [548] | 634 | $sth->execute($pool); | 
|---|
| [517] | 635 | my @iplist; | 
|---|
| [647] | 636 | while (my ($id,$ip,$custid,$city,$type,$avail,$desc,$notes) = $sth->fetchrow_array) { | 
|---|
| [517] | 637 | my %row = ( | 
|---|
| [647] | 638 | id => $id, | 
|---|
| [517] | 639 | ip => $ip, | 
|---|
|  | 640 | custid => $custid, | 
|---|
|  | 641 | city => $city, | 
|---|
|  | 642 | type => $type, | 
|---|
|  | 643 | avail => $avail, | 
|---|
|  | 644 | desc => $desc, | 
|---|
|  | 645 | notes => $notes | 
|---|
|  | 646 | ); | 
|---|
|  | 647 | push @iplist, \%row; | 
|---|
| [65] | 648 | } | 
|---|
| [517] | 649 | $page->param(iplist => \@iplist); | 
|---|
|  | 650 | } # end showPool() | 
|---|
| [422] | 651 |  | 
|---|
|  | 652 |  | 
|---|
|  | 653 | # interpret the notify codes | 
|---|
|  | 654 | sub dispNoticeCode { | 
|---|
|  | 655 | my $code = shift; | 
|---|
|  | 656 | my $action_out = ''; | 
|---|
| [426] | 657 |  | 
|---|
|  | 658 | if ($code =~ /^s:/) { | 
|---|
|  | 659 | $code =~ s/^s:/Special: /; | 
|---|
|  | 660 | return $code; | 
|---|
|  | 661 | } | 
|---|
| [422] | 662 | if ($code =~ /^f:(.+)$/) { | 
|---|
|  | 663 | $code =~ s/^f://; | 
|---|
|  | 664 | $action_out = "Failure on "; | 
|---|
|  | 665 | } | 
|---|
|  | 666 | if (my $target = $code =~ /^n(.+)/) { | 
|---|
|  | 667 | $action_out .= "New "; | 
|---|
|  | 668 | if ($1 eq 'ci') { $action_out .= "city"; } | 
|---|
|  | 669 | elsif ($1 eq 'no') { $action_out .= "node"; } | 
|---|
|  | 670 | else { $action_out .= '<unknown>'; } | 
|---|
|  | 671 | } else { | 
|---|
|  | 672 | my ($action,$target) = ($code =~ /^(.)(.+)$/); | 
|---|
|  | 673 | if ($action eq 'a')      { $action_out .= 'Add '; } | 
|---|
|  | 674 | elsif ($action eq 'u')   { $action_out .= 'Update '; } | 
|---|
|  | 675 | elsif ($action eq 'd')   { $action_out .= 'Delete '; } | 
|---|
|  | 676 | ##fixme:  what if we get something funky? | 
|---|
| [423] | 677 | # What about the eleventy-billion odd combinations possible? | 
|---|
|  | 678 | # this should give an idea of the structure tho | 
|---|
| [422] | 679 | if ($target eq 'a') { $action_out .= "all"; } | 
|---|
| [438] | 680 | elsif ($target eq '.i') { | 
|---|
| [423] | 681 | $action_out .= "all static IPs"; | 
|---|
|  | 682 | } | 
|---|
| [422] | 683 | else { $action_out .= $disp_alloctypes{$target}; } | 
|---|
|  | 684 | } | 
|---|
|  | 685 | return $action_out; | 
|---|
|  | 686 | } | 
|---|