source: trunk/cgi-bin/admin.cgi @ 416

Last change on this file since 416 was 416, checked in by Kris Deugau, 12 years ago

/trunk

Update mailNotify to use new table notify to decide who to spam
with notices on various events. See #2.

  • existing notification calls updated. Still need to move calls into IPDB.pm's subs
  • IPDB.pm and MyIPDB.pm updated with new semiglobal vars for org name, SMTP host, domain. Will add others as needed so MyIPDB.pm can be moved to /etc/ipdb/ (see #17).

Internal allocation types will need to be rearranged somewhat to
make full use of the flexibility possible. See #20.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 25.7 KB
Line 
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: 2010-05-18 22:08:12 +0000 (Tue, 18 May 2010) $
9# SVN revision $Rev: 416 $
10# Last update by $Author: kdeugau $
11###
12# Copyright (C) 2004-2006 - Kris Deugau
13
14use strict;
15use warnings;
16use CGI::Carp qw(fatalsToBrowser);
17use DBI;
18use CommonWeb qw(:ALL);
19use MyIPDB;
20use CustIDCK;
21#use POSIX qw(ceil);
22use NetAddr::IP;
23
24use Sys::Syslog;
25
26openlog "IPDB-admin","pid","local2";
27
28# Collect the username from HTTP auth.  If undefined, we're in a test environment.
29my $authuser;
30if (!defined($ENV{'REMOTE_USER'})) {
31  $authuser = '__temptest';
32} else {
33  $authuser = $ENV{'REMOTE_USER'};
34}
35
36syslog "debug", "$authuser active";
37
38# Why not a global DB handle?  (And a global statement handle, as well...)
39# Use the connectDB function, otherwise we end up confusing ourselves
40my $ip_dbh;
41my $sth;
42my $errstr;
43($ip_dbh,$errstr) = connectDB_My;
44if (!$ip_dbh) {
45  printAndExit("Database error: $errstr\n");
46}
47initIPDBGlobals($ip_dbh);
48
49if ($IPDBacl{$authuser} !~ /A/) {
50  print "Content-Type: text/html\n\n".
51        "<html>\n<head>\n\t<title>Access denied</title>\n".
52        qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n).
53        qq(\t<link rel="stylesheet" type="text/css" href="/ip/local.css">\n).
54        "</head>\n<body>\n".
55        qq(Access to this tool is restricted.  Contact the <a href="mailto:ipdbadmin\@example.com">IPDB administrator</a> \n).
56        "for more information.\n</body>\n</html>\n";
57  exit;
58}
59
60my %webvar = parse_post();
61cleanInput(\%webvar);
62
63print "Content-type: text/html\n\n".
64        "<html>\n<head>\n\t<title>[IPDB admin tools]</title>\n".
65        qq(\t<link rel="stylesheet" type="text/css" href="/ip/ipdb.css">\n).
66        qq(\t<link rel="stylesheet" type="text/css" href="/ip/local.css">\n).
67        "</head>\n<body>\n".
68        "<h2>IPDB - Administrative Tools</h2>\n<hr>\n";
69
70if(!defined($webvar{action})) {
71  $webvar{action} = "<NULL>";   #shuts up the warnings.
72
73  my $typelist = '';
74  $sth = $ip_dbh->prepare("select type,listname from alloctypes where listorder < 900 order by listorder");
75  $sth->execute;
76  my @data = $sth->fetchrow_array;
77  $typelist .= "<option value='$data[0]' selected>$data[1]</option>\n";
78  while (my @data = $sth->fetchrow_array) {
79    $typelist .= "<option value='$data[0]'>$data[1]</option>\n";
80  }
81
82  my $masterlist = '';
83  $sth = $ip_dbh->prepare("select cidr,mtime from masterblocks order by cidr");
84  $sth->execute;
85  while (my @data = $sth->fetchrow_array) {
86    $masterlist .= "<option value='$data[0]'>$data[0] ($data[1])</option>\n";
87  }
88
89  print qq(WARNING:  There are FAR fewer controls on what you can do here.  Use the
90main interface if at all possible.
91<hr>
92<a href="admin.cgi?action=newalloc">Add allocation</a>
93<hr>
94<form action="admin.cgi" method="POST">
95<input type=hidden name=action value=alloc>
96Allocate block/IP: <input name=cidr> as <select name=alloctype>$typelist</select> to <input name=custid>
97<input type=submit value=" GIMME!! "></form>
98<hr><form action="admin.cgi" method="POST">
99<input type=hidden name=action value=alloctweak>
100Manually update allocation data in this /24: <input name=allocfrom>
101<input type=submit value="Show allocations">
102</form>
103
104<hr>rWHOIS tools:
105<form action="admin.cgi" method="POST">
106<input type=hidden name=action value=touch>
107Bump "last updated" timestamp on this master: <select name=whichmaster>$masterlist</select>
108<input type=submit value="Update timestamp"> (Sets timestamp to "now")</form>
109<a href="admin.cgi?action=listcust">Edit customer data for rWHOIS</a>
110
111<hr><a href="admin.cgi?action=showpools">List IP Pools</a> for manual tweaking and updates
112<hr><a href="admin.cgi?action=showusers">Manage users</a> (add/remove users;  change
113internal access controls - note that this does NOT include IP-based limits)
114<hr>Consistency check tools<br>
115<a href="consistency-check.pl">General</a>:  Check general netblock consistency.<br>
116<a href="freespace.pl">Free space</a>:  List total and aggregate free space.  Does not
117include private networks (192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8)
118<hr>(r)WHOIS<br>
119<a href="list-cust.php">List customer data for WHOIS</a> - data used for blocks with the SWIP box checkmarked.
120Links to edit/add data are on this page.
121);
122} else {
123  print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>';
124}
125
126
127## Possible actions.
128if ($webvar{action} eq 'alloc') {
129  # OK, we know what we're allocating.
130
131  if ($webvar{cidr} !~ /^\s*(\d{1,3}\.){3}\d{1,3}(\/\d{2})?\s*$/) {
132    printAndExit("Can't allocate something that's not a netblock/ip");
133  }
134
135  $sth = $ip_dbh->prepare("select def_custid from alloctypes where type='$webvar{alloctype}'");
136  $sth->execute;
137  my @data = $sth->fetchrow_array;
138  my $custid = $data[0];
139  if ($custid eq '') {
140    if ($webvar{custid} !~ /^(?:\d{10}|\d{7}|STAFF)(?:-\d\d?)?$/) {
141      # Force uppercase for now...
142      $webvar{custid} =~ tr/a-z/A-Z/;
143      # Crosscheck with billing.
144      my $status = CustIDCK->custid_exist($webvar{custid});
145      if ($CustIDCK::Error) {
146        printError("Error verifying customer ID: ".$CustIDCK::ErrMsg);
147        return;
148      }
149      if (!$status) {
150        printError("Customer ID not valid.  Make sure the Customer ID ".
151          "is correct.<br>\nUse STAFF for staff static IPs, and 6750400 for any other ".
152          "non-customer assignments.");
153        return;
154      }
155    }
156    # Type that doesn't have a default custid
157    $custid = $webvar{custid};
158  }
159
160  my $cidr = new NetAddr::IP $webvar{cidr};
161  my @data;
162  if ($webvar{alloctype} eq 'rm') {
163    $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and routed='n'");
164    $sth->execute;
165    @data = $sth->fetchrow_array;
166# User deserves errors if user can't be bothered to find the free block first.
167    printAndExit("Can't allocate from outside a free block!!\n")
168        if !$data[0];
169  } elsif ($webvar{alloctype} =~ /^(.)i$/) {
170    $sth = $ip_dbh->prepare("select cidr from allocations where cidr >>='$cidr' and (type like '_d' or type like '_p')");
171    $sth->execute;
172    @data = $sth->fetchrow_array;
173# User deserves errors if user can't be bothered to find the pool and a free IP first.
174    printAndExit("Can't allocate static IP from outside a pool!!\n")
175        if !$data[0];
176  } else {
177    $sth = $ip_dbh->prepare("select cidr from freeblocks where cidr >>='$cidr' and not (routed='n')");
178    $sth->execute;
179    @data = $sth->fetchrow_array;
180# User deserves errors if user can't be bothered to find the free block first.
181    printAndExit("Can't allocate from outside a routed block!!\n")
182        if !$data[0];
183  }
184
185  my $alloc_from = new NetAddr::IP $data[0];
186  $sth->finish;
187
188  my $cities = '';
189  foreach my $city (@citylist) {
190    $cities .= "<option>$city</option>\n";
191  }
192
193  print qq(<table class=regular>
194<form method=POST action=admin.cgi>
195<tr class=color1>
196<td>Allocating:</td>
197<td>$cidr<input type=hidden name=cidr value="$cidr"></td>
198</tr><tr class=color2>
199<td>Type:</td><td>$disp_alloctypes{$webvar{alloctype}}
200<input type=hidden name=alloctype value="$webvar{alloctype}"></td>
201</tr><tr class=color1>
202<td>Allocated from:</td>
203<td>$alloc_from<input type=hidden name=alloc_from value="$alloc_from"></td>
204</tr><tr class="color2">
205<td>Customer ID:</td><td>$custid<input type=hidden name=custid value="$custid"></td>
206</tr><tr class=color1>
207<td>Customer location:</td><td>
208<select name="city"><option selected>-</option>
209$cities
210</select>
211&nbsp;<a href="javascript:popNotes('/ip/newcity.html')">Add new location</a>
212</td>
213</tr>
214<tr class="color2">
215<td>Circuit ID:</td><td><input name=circid size=40></td>
216</tr><tr class="color1">
217<td>Description/Name:</td><td><input name="desc" size=40></td>
218</tr><tr class="color2">
219<td>Notes:</td><td><textarea name="notes" rows="3" cols="40"></textarea></td>
220</tr><tr class="warning">
221<td colspan=2><center>WARNING:  This will IMMEDIATELY assign this block!!</center></td>
222</tr><tr class="color2">
223<td class="center" colspan="2"><input type="submit" value="  Assign  "></td>
224<input type="hidden" name="action" value="confirm">
225</form>
226</tr>
227</table>
228);
229
230
231} elsif ($webvar{action} eq 'confirm') {
232
233  print "Assigning $webvar{cidr} to $webvar{custid} (\"$webvar{desc}\") as ".
234        "$disp_alloctypes{$webvar{alloctype}}...<br>\n";
235  # Only need to check city here.
236  if ($webvar{city} eq '-') {
237    printError("Invalid customer location!  Go back and select customer's location.");
238  } else {
239    if ($webvar{alloctype} =~ /^.i$/) {
240      $sth = $ip_dbh->prepare("update poolips set available='n', custid='$webvar{custid}', ".
241        "city='$webvar{city}', description='$webvar{desc}', notes='$webvar{notes}' ".
242        "where ip='$webvar{cidr}'");
243      $sth->execute;
244      if ($sth->err) {
245        print "Allocation failed!  DBI said:\n".$sth->errstr."\n";
246        syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
247                "'$webvar{alloctype}' failed: '".$sth->errstr."'";
248      } else {
249        print "Allocation OK!\n";
250        syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
251                "'$webvar{alloctype}'";
252        mailNotify($ip_dbh, "$disp_alloctypes{$webvar{alloctype}} allocation",
253          "$disp_alloctypes{$webvar{alloctype}} $webvar{cidr} allocated to customer".
254          " $webvar{custid}\n".
255          "Description: $webvar{desc}\n\nAllocated by: $authuser\n");
256      }
257    } else {
258      my ($retcode,$msg) = allocateBlock($ip_dbh, $webvar{cidr}, $webvar{alloc_from},
259        $webvar{custid}, $webvar{alloctype}, $webvar{city}, $webvar{desc}, $webvar{notes},
260        $webvar{circid});
261      if ($retcode eq 'OK') {
262        print "Allocation OK!\n";
263        syslog "notice", "$authuser allocated '$webvar{cidr}' to '$webvar{custid}' as ".
264                "'$webvar{alloctype}'";
265      } else {
266        print "Allocation failed!  IPDB::allocateBlock said:\n$msg\n";
267        syslog "err", "($authuser) Allocation of '$webvar{cidr}' to '$webvar{custid}' as ".
268                "'$webvar{alloctype}' failed: '$msg'";
269      }
270    } # static IP vs netblock
271
272  } # done city check
273
274} elsif ($webvar{action} eq 'alloctweak') {
275  fix_allocfrom();
276  showAllocs($webvar{allocfrom});
277} elsif ($webvar{action} eq 'update') {
278  update();
279} elsif ($webvar{action} eq 'assign') {
280  # Display a list of possible blocks within the requested block.
281  open (HTML, "../admin_alloc.html")
282        or croak "Could not open admin_alloc.html :$!";
283  my $html = join('', <HTML>);
284  $html =~ s/\$\$MASK\$\$/$webvar{masklen}/g;
285  $html =~ s/\$\$ALLOCFROM\$\$/$webvar{allocfrom}/g;
286
287  my $from = new NetAddr::IP $webvar{allocfrom};
288  my @blocklist = $from->split($webvar{masklen});
289  my $availblocks;
290  foreach (@blocklist) {
291    $availblocks .= qq(<tr><td colspan=2 align=center><input type=radio name=block value="$_">$_</td></tr>\n);
292  }
293  $html =~ s/\$\$BLOCKLIST\$\$/$availblocks/g;
294
295  print $html;
296} elsif ($webvar{action} eq 'touch') {
297  print "Touching master $webvar{whichmaster}\n";
298  $sth = $ip_dbh->prepare("update masterblocks set mtime=now() where cidr='$webvar{whichmaster}'");
299  $sth->execute;
300  if ($sth->err) {
301    print "<p>Error updating modified timestamp on master $webvar{whichmaster}: ".$sth->errstr."\n";
302  }
303} elsif ($webvar{action} eq 'listcust') {
304  print qq(Add new entry:\n
305<form action=admin.cgi method=POST>
306<table border=1><tr>
307<input type=hidden name=action value=newcust>
308<td>CustID:</td><td><input name=custid></td>
309<td>Name:</td><td><input name=name></td></tr>
310<tr><td>Street:</td><td><input name=street></td></tr>
311<!-- <td>Street2:</td><td><input name=street2></td> -->
312<tr><td>City:</td><td><input name=city></td>
313<td>Province: (2-letter code)</td><td><input name=province value=ON length=2 size=2></td></tr>
314<tr><td>Country: (2-letter code)</td><td><input name=country value=CA length=2 size=2></td>
315<td>Postal/ZIP Code:</td><td><input name=pocode></td></tr>
316<tr><td>Phone:</td><td><input name=phone></td>
317<!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
318<td>Description:</td><td><input name=description></td> -->
319<td>ARIN Handles:</td><td>
320 Tech: <input name=tech_handle value="VH25-ORG-ARIN"><br>
321 Abuse: <input name=abuse_handle><br>
322 Admin: <input name=admin_handle><br>
323Note:  Only tech is required at the moment.
324</td></tr>
325<tr><td colspan=4 align=center><input type=submit value="Add"></td></tr>
326</form></table>
327);
328  print "<p>Click CustID to edit existing customer contact data:\n".
329        "<table border=1>\n<tr><td>CustID</td><td>Name</td><td>Tech handle</td></tr>\n";
330  $sth = $ip_dbh->prepare("select custid,name,tech_handle from customers order by custid");
331  $sth->execute;
332  while (my @data = $sth->fetchrow_array) {
333    print qq(<tr><td><a href="admin.cgi?action=edcust&custid=$data[0]">$data[0]</td>).
334        "<td>$data[1]</td><td>$data[2]</td></tr>\n";
335  }
336  print "</table>\n";
337} elsif ($webvar{action} eq 'newcust') {
338  if ($webvar{custid} eq '') {
339    print 'No CustID entered.  PTHBT!  (Hit "Back" and fix the problem.)';
340  } else {
341    $sth = $ip_dbh->prepare("insert into customers ".
342        "(custid, name, street, city, province, country, pocode, ".
343        "phone, tech_handle, abuse_handle, admin_handle) values ".
344        "('$webvar{custid}', '$webvar{name}', '$webvar{street}', ".
345        "'$webvar{city}', '$webvar{province}', '$webvar{country}', ".
346        "'$webvar{pocode}', '$webvar{phone}', '$webvar{techhandle}', ".
347        "'$webvar{abusehandle}', '$webvar{adminhandle}')");
348    $sth->execute;
349    if ($sth->err) {
350      print "INSERT failed:  ".$sth->errstr."\n";
351    } else {
352      print "Success!  Added customer contact data:\n".
353        qq(<table border=1><tr>
354<td>CustID:</td>$webvar{custid}</td><td>Name:</td>$webvar{name}</td></tr>
355<tr><td>Street:</td><td>$webvar{street}</td></tr>
356<tr><td>City:</td><td>$webvar{city}</td><td>Province:</td><td>$webvar{province}</td></tr>
357<tr><td>Country:</td><td>$webvar{country}</td>
358<td>Postal/ZIP Code:</td><td>$webvar{pocode}</td></tr>
359<tr><td>Phone:</td><td>$webvar{phone}</td>
360<!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
361<tr><td>Description:</td><td><input name=description></td> -->
362<td>ARIN Handles:</td><td>
363 Tech: $webvar{tech_handle}<br>
364 Abuse: $webvar{abuse_handle}<br>
365 Admin: $webvar{admin_handle}<br>
366</td></tr></table>
367);
368    } # $sth err check
369  } # bad custid
370} elsif ($webvar{action} eq 'edcust') {
371  $sth = $ip_dbh->prepare("select custid,name,street,city,province,".
372        "country,pocode,phone,tech_handle,abuse_handle,admin_handle ".
373        "from customers where custid='$webvar{custid}'");
374  $sth->execute;
375  my ($custid, $name, $street, $city, $prov, $country, $pocode, $phone, $tech, $abuse, $admin) =
376        $sth->fetchrow_array;
377  print qq(<form action=admin.cgi method=POST>
378<table border=1><tr>
379<input type=hidden name=action value=updcust>
380<td>CustID:</td><td>$custid<input type=hidden name=custid value=$custid></td>
381<td>Name:</td><td><input name=name value="$name"></td></tr>
382<tr><td>Street:</td><td><input name=street value="$street"></td></tr>
383<!-- <td>Street2:</td><td><input name=street2></td> -->
384<tr><td>City:</td><td><input name=city value="$city"></td>
385<td>Province: (2-letter code)</td><td><input name=province value="$prov" length=2 size=2></td></tr>
386<tr><td>Country: (2-letter code)</td><td><input name=country value="$country" length=2 size=2></td>
387<td>Postal/ZIP Code:</td><td><input name=pocode value="$pocode"></td></tr>
388<tr><td>Phone:</td><td><input name=phone value="$pocode"></td>
389<!-- <td>Default rDNS:</td><td><input name=def_rdns></td></tr>
390<td>Description:</td><td><input name=description></td> -->
391<td>ARIN Handles:</td><td>
392 Tech: <input name=tech_handle value="$tech"><br>
393 Abuse: <input name=abuse_handle value="$abuse"><br>
394 Admin: <input name=admin_handle value="$admin"><br>
395Note:  Only tech is required at the moment.
396</td></tr>
397<tr><td colspan=4 align=center><input type=submit value="Update"></td></tr>
398</form></table>
399);
400
401} elsif ($webvar{action} eq 'updcust') {
402  print "Updated $webvar{custid}\n";
403} elsif ($webvar{action} eq 'showpools') {
404  print "IP Pools currently allocated:\n".
405        "<table border=1>\n<tr><td>Pool</td><td># of free IPs</td></tr>\n";
406  $sth = $ip_dbh->prepare("select cidr from allocations where type like '%p' or type like '%d' order by cidr");
407  $sth->execute;
408  my %poolfree;
409  while (my @data = $sth->fetchrow_array) {
410    $poolfree{$data[0]} = 0;
411  }
412  $sth = $ip_dbh->prepare("select pool,ip from poolips where available='y' order by ip");
413  $sth->execute;
414  while (my @data = $sth->fetchrow_array) {
415    $poolfree{$data[0]}++;
416  }
417  foreach my $key (keys %poolfree) {
418    print qq(<tr><td><a href="admin.cgi?action=tweakpool&pool=$key">$key</a></td>).
419        "<td>$poolfree{$key}</td></tr>\n";
420  }
421  print "</table>\n";
422} elsif ($webvar{action} eq 'tweakpool') {
423  showPool($webvar{pool});
424} elsif ($webvar{action} eq 'updatepool') {
425
426  $sth = $ip_dbh->prepare("update poolips set custid='$webvar{custid}', ".
427        "city='$webvar{city}', type='$webvar{type}', available='".
428        (($webvar{available} eq 'y') ? 'y' : 'n').
429        "', notes='$webvar{notes}', description='$webvar{desc}' ".
430        "where ip='$webvar{ip}'");
431  $sth->execute;
432  if ($sth->err) {
433    print "Error updating pool IP $webvar{ip}: $@<hr>\n";
434    syslog "err", "$authuser could not update pool IP $webvar{ip}: $@";
435  } else { 
436    $sth = $ip_dbh->prepare("select pool from poolips where ip='$webvar{ip}'");
437    $sth->execute;
438    my @data = $sth->fetchrow_array;
439    print "$webvar{ip} in $data[0] updated\n<hr>\n";
440    syslog "notice", "$authuser updated pool IP $webvar{ip}";
441  }
442} elsif ($webvar{action} eq 'showusers') {
443  print "Notes:<br>\n".
444        "<li>Admin users automatically get all other priviledges.\n".
445        "<li>Everyone has basic read access.\n".
446        "<hr>Add new user:<form action=admin.cgi method=POST>\n".
447        "Username: <input name=username><br>\n".
448        "Password: <input name=password> <input type=checkbox name=preenc>Password is pre-encrypted (MUST be crypt() encrypted)<br>\n".
449        "<input type=submit value='Add user'><input type=hidden name=action value=newuser></form>\n";
450
451  print "<hr>Users with access:\n<table border=1>\n";
452  print "<tr><td></td><td align=center colspan=3>General access</td></tr>\n";
453  print "<tr><td>Username</td><td>Add new</td><td>Change</td>".
454        "<td>Delete</td><td>Systems/Networking</td><td>Admin user</td></tr>\n".
455        "<form action=admin.cgi method=POST>\n";
456  $sth = $ip_dbh->prepare("select username,acl from users order by username");
457  $sth->execute;
458  while (my @data = $sth->fetchrow_array) {
459    print "<form action=admin.cgi method=POST><input type=hidden name=action value=updacl>".
460        qq(<tr><td>$data[0]<input type=hidden name=username value="$data[0]"></td><td>).
461    # Now for the fun bit.  We have to pull apart the ACL field and
462    # output a bunch of checkboxes.
463        "<input type=checkbox name=add".($data[1] =~ /a/ ? ' checked=y' : '').
464        "></td><td><input type=checkbox name=change".($data[1] =~ /c/ ? ' checked=y' : '').
465        "></td><td><input type=checkbox name=del".($data[1] =~ /d/ ? ' checked=y' : '').
466        "></td><td><input type=checkbox name=sysnet".($data[1] =~ /s/ ? ' checked=y' : '').
467        "></td><td><input type=checkbox name=admin".($data[1] =~ /A/ ? ' checked=y' : '').
468        qq(></td><td><input type=submit value="Update"></td></form>\n).
469        "<form action=admin.cgi method=POST><td><input type=hidden name=action value=deluser>".
470        "<input type=hidden name=username value=$data[0]>".
471        qq(<input type=submit value="Delete user"></tr></form>\n);
472
473  }
474  print "</table>\n";
475} elsif ($webvar{action} eq 'updacl') {
476  print "Updating ACL for $webvar{username}:<br>\n";
477  my $acl = 'b';
478  if ($webvar{admin} eq 'on') {
479    $acl .= "acdsA";
480  } else {
481    $acl .= ($webvar{add} eq 'on' ? 'a' : '').
482        ($webvar{change} eq 'on' ? 'c' : '').
483        ($webvar{del} eq 'on' ? 'd' : '').
484        ($webvar{sysnet} eq 'on' ? 's' : '');
485  }
486  print "New ACL: $acl<br>\n";
487
488  $sth = $ip_dbh->prepare("update users set acl='$acl' where username='$webvar{username}'");
489  $sth->execute;
490  print "OK\n" if !$sth->err;
491
492  print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
493
494} elsif ($webvar{action} eq 'newuser') {
495  print "Adding user $webvar{username}...\n";
496  my $cr_pass = ($webvar{preenc} ? $webvar{password} :
497        crypt $webvar{password}, join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64, rand 64]));
498  $sth = $ip_dbh->prepare("insert into users (username,password,acl) values ".
499        "('$webvar{username}','$cr_pass','b')");
500  $sth->execute;
501  if ($sth->err) {
502    print "<br>Error adding user: ".$sth->errstr;
503  } else {
504    print "OK\n";
505  }
506
507  print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
508
509} elsif ($webvar{action} eq 'deluser') {
510  print "Deleting user $webvar{username}.<br>\n";
511  $sth = $ip_dbh->prepare("delete from users where username='$webvar{username}'");
512  $sth->execute;
513  print "OK\n" if !$sth->err;
514
515  print qq(<hr><a href="admin.cgi?action=showusers">Back</a> to user listing\n);
516
517} elsif ($webvar{action} ne '<NULL>') {
518  print "webvar{action} check failed: Don't know how to $webvar{action}";
519}
520
521# Hokay.  This is a little different.  We have a few specific functions here:
522#  -> Assign arbitrary subnet from arbitrary free space
523#  -> Tweak individual DB fields
524#
525
526print qq(<hr><a href="/ip/">Back</a> to main interface</a>\n);
527
528printFooter;
529
530$ip_dbh->disconnect;
531
532exit;
533
534
535# Tweak allocfrom into shape.
536sub fix_allocfrom {
537  if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
538    # 3-octet class C specified
539    $webvar{allocfrom} .= ".0/24";
540  } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
541    # 4-octet IP specified; 
542    $webvar{allocfrom} .= "/24";
543  }
544}
545
546
547# List free blocks in a /24 for arbitrary manual allocation
548sub showfree($) {
549  my $cidr = new NetAddr::IP $_[0];
550  print "Showing free blocks in $cidr<br>\n".
551        "<table border=1>\n";
552  $sth = $ip_dbh->prepare("select * from freeblocks where cidr <<= '$cidr' order by cidr");
553  $sth->execute;
554  while (my @data = $sth->fetchrow_array) {
555    my $temp = new NetAddr::IP $data[0];
556    print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=assign>\n".
557        qq(<td>$temp<input type=hidden name=allocfrom value="$temp"></td>\n).
558        "<td>".
559        (($temp->masklen == 30) ? '<input type=hidden name=masklen value=30>30'
560          : "<select name=masklen><option>30</option>\n<option>29</option>\n") .
561        (($temp->masklen < 29) ? "<option>28</option>\n" : '') .
562        (($temp->masklen < 28) ? "<option>27</option>\n" : '') .
563        (($temp->masklen < 27) ? "<option>26</option>\n" : '') .
564        (($temp->masklen < 26) ? "<option>25</option>\n" : '') .
565        (($temp->masklen < 25) ? "<option>24</option>\n" : '') .
566        "</td>".
567        qq(<td>$data[2]</td><td><input type=submit value="Allocate from here"></td>).
568        "\n</form></tr>\n";
569  }
570  print "</table>\n";
571}
572
573
574# Show allocations to allow editing.
575sub showAllocs($) {
576  my $cidr = new NetAddr::IP $_[0];
577  print "Edit custID, allocation type, city for allocations in ".
578        "$cidr:\n<table border=1>";
579  $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr");
580  $sth->execute;
581  while (my @data = $sth->fetchrow_array) {
582    print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n".
583        qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n).
584        qq(<td><input name=custid value="$data[1]"></td>\n).
585        "<td><select name=alloctype>";
586
587    my $sth2 = $ip_dbh->prepare("select type,listname from alloctypes".
588        " where listorder < 500 and not (type like '_i') order by listorder");
589    $sth2->execute;
590    while (my @types = $sth2->fetchrow_array) {
591      print "<option". (($data[2] eq $types[0]) ? ' selected' : '') .
592        " value='$types[0]'>$types[1]</option>\n";
593    }
594
595    print qq(<td><input name=city value="$data[3]"></td>\n).
596        "<td>$data[4]</td><td>$data[5]</td>".
597        qq(<td><input type=submit value="Update"></td></form></tr>\n);
598  }
599  print "</table>\n";
600
601  # notes
602  print "<hr><b>Notes:</b>\n".
603        "<ul>\n<li>Use the main interface to update description and notes fields\n".
604        "<li>Changing the allocation type here will NOT affect IP pool data.\n".
605        "</ul>\n";
606}
607
608
609# Stuff updates into DB
610sub update {
611  eval {
612    # Relatively simple SQL transaction here.  Note that we're deliberately NOT
613    # updating notes/desc here as it's available through the main interface.
614    $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
615        "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'");
616    $sth->execute;
617    $ip_dbh->commit;
618  };
619  if ($@) {
620    carp "Transaction aborted because $@";
621    eval { $ip_dbh->rollback; };
622    syslog "err", "$authuser could not update block '$webvar{block}': '$@'";
623  } else {
624    # If we get here, the operation succeeded.
625    syslog "notice", "$authuser updated $webvar{block}";
626    print "Allocation $webvar{block} updated<hr>\n";
627  }
628  # need to get /24 that block is part of
629  my @bits = split /\./, $webvar{block};
630  $bits[3] = "0/24";
631  showAllocs((join ".", @bits));
632}
633
634
635# showPool()
636# List all IPs in a pool, and allow arbitrary admin changes to each
637# Allow changes to ALL fields
638sub showPool($) {
639  my $pool = new NetAddr::IP $_[0];
640  print qq(Listing pool $pool:\n<table border=1>
641<form action=admin.cgi method=POST>
642<input type=hidden name=action value=updatepool>
643<tr><td align=right>Customer ID:</td><td><input name=custid></td></tr>
644<tr><td align=right>Customer location:</td><td><input name=city></td></tr>
645<tr><td align=right>Type:</td><td><select name=type><option selected>-</option>\n);
646
647  $sth = $ip_dbh->prepare("select type,listname from alloctypes where type like '_i' order by listorder");
648  $sth->execute;
649  while (my @data = $sth->fetchrow_array) {
650    print "<option value='$data[0]'>$data[1]</option>\n";
651  }
652
653  print qq(</select></td></tr>
654<tr><td align=right>Available?</td><td><input type=checkbox value=y></td></tr>
655<tr><td align=right>Description/name:</td><td><input name=desc size=40></td></tr>
656<tr><td align=right>Notes:</td><td><textarea name=notes rows=3 cols=40></textarea></td></tr>
657<tr><td colspan=2 align=center><input type=submit value="Update"></td></tr>
658).
659        "</table>Update the following record:<table border=1>\n";
660  $sth = $ip_dbh->prepare("select pool,ip,custid,city,type,available,description,notes from poolips where pool='$pool' order by ip");
661  $sth->execute;
662  while (my @data = $sth->fetchrow_array) {
663    print qq(<tr><td><input type=radio name=ip value="$data[1]">$data[1]</td>).
664        "<td>$data[2]</td><td>$data[3]</td><td>$data[4]</td>".
665        "<td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>\n";
666  }
667  print "</form></table>\n";
668}
Note: See TracBrowser for help on using the repository browser.