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