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

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

/trunk

Merge addition of CustIDCK.pm from r56 - proprietary info this contained
was stripped during repo history conversion and it makes a good base for
providing data validation hooks.
Also merge all updates and related additions since.
See #13.

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