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