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

Last change on this file since 54 was 54, checked in by Kris Deugau, 20 years ago

/trunk

Added admin.cgi with mostly-complete "update allocation data" functionality

  • Property svn:executable set to *
File size: 6.1 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# SVN revision info
7# $Date$
8# SVN revision $Rev$
9# Last update by $Author$
10###
11
12use strict;
13use warnings;
14use CGI::Carp qw(fatalsToBrowser);
15use DBI;
16use CommonWeb qw(:ALL);
17use IPDB qw(:ALL);
18#use POSIX qw(ceil);
19use NetAddr::IP;
20
21use Sys::Syslog;
22
23openlog "IPDB-admin","pid","local2";
24
25# Collect the username from HTTP auth. If undefined, we're in a test environment.
26my $authuser;
27if (!defined($ENV{'REMOTE_USER'})) {
28 $authuser = '__temptest';
29} else {
30 $authuser = $ENV{'REMOTE_USER'};
31}
32
33if ($authuser !~ /^(kdeugau|jodyh|__temptest)$/) {
34 print "Content-Type: text/html\n\n".
35 "<html><head><title>Access denied</title></head><body>\n".
36 'Access to this tool is restricted. Contact <a href="mailto:kdeugau@vianet.ca">Kris</a> '.
37 "for more information.</body></html>\n";
38 exit;
39}
40
41syslog "debug", "$authuser active";
42
43my %webvar = parse_post();
44cleanInput(\%webvar);
45my $ip_dbh = connectDB;
46my $sth;
47
48print "Content-type: text/html\n\n".
49 "<html>\n<head>\n\t<title>IPDB admin tools</title>\n</head>\n<body>\n".
50 "<h2>IPDB - Administrative Tools</h2>\n<hr>\n";
51
52if(!defined($webvar{action})) {
53 $webvar{action} = "<NULL>"; #shuts up the warnings.
54 print qq(WARNING: There are FAR fewer controls on what you can do here. Use the
55main interface if at all possible.
56<hr><form action="admin.cgi" method="POST">
57<input type=hidden name=action value=alloc>
58Allocate block from this /24: <input name=allocfrom>
59<input type=submit value="List available free blocks">
60</form>
61<hr><form action="admin.cgi" method="POST">
62<input type=hidden name=action value=alloctweak>
63Manually update allocation data in this /24: <input name=allocfrom>
64<input type=submit value="Show allocations">
65);
66} else {
67 print '<a href="/ip/cgi-bin/admin.cgi">Back</a> to main<hr>';
68}
69
70if ($webvar{action} eq 'alloc') {
71 fix_allocfrom();
72 showfree($webvar{allocfrom});
73} elsif ($webvar{action} eq 'alloctweak') {
74 fix_allocfrom();
75 showAllocs($webvar{allocfrom});
76} elsif ($webvar{action} eq 'update') {
77 update();
78}
79
80# Hokay. This is a little different. We have a few specific functions here:
81# -> Assign arbitrary subnet from arbitrary free space
82# -> Tweak individual DB fields
83#
84
85
86printFooter;
87
88$ip_dbh->disconnect;
89
90exit;
91
92
93# List free blocks in a /24 for arbitrary manual allocation
94sub showfree($) {
95 my $cidr = $_[0];
96 print "Showing free blocks in $cidr\n";
97}
98
99# Tweak allocfrom into shape.
100sub fix_allocfrom {
101 if ($webvar{allocfrom} =~ /^(\d+\.){2}\d+$/) {
102 # 3-octet class C specified
103 $webvar{allocfrom} .= ".0/24";
104 } elsif ($webvar{allocfrom} =~ /^(\d+\.){3}\d+$/) {
105 # 4-octet IP specified;
106 $webvar{allocfrom} .= "/24";
107 }
108}
109
110
111# Show allocations to allow editing.
112sub showAllocs($) {
113 my $cidr = new NetAddr::IP $_[0];
114 print "Edit custID, allocation type, city for allocations in ".
115 "$cidr:\n<table border=1>";
116 $sth = $ip_dbh->prepare("select * from allocations where cidr <<= '$cidr' order by cidr");
117 $sth->execute;
118 while (my @data = $sth->fetchrow_array) {
119 print "<tr><form action=admin.cgi method=POST><input type=hidden name=action value=update>\n".
120 qq(<td>$data[0]<input type=hidden value="$data[0]" name=block></td>\n).
121 qq(<td><input name=custid value="$data[1]"></td>\n);
122
123 print "<td><select name=alloctype><option".
124 (($data[2] eq 'cn') ? ' selected' : '') ." value='cn'>Customer netblock</option>\n<option".
125 (($data[2] eq 'si') ? ' selected' : '') ." value='si'>Static IP - Server pool</option>\n<option".
126 (($data[2] eq 'ci') ? ' selected' : '') ." value='ci'>Static IP - Cable</option>\n<option".
127 (($data[2] eq 'di') ? ' selected' : '') ." value='di'>Static IP - DSL</option>\n<option".
128 (($data[2] eq 'mi') ? ' selected' : '') ." value='mi'>Static IP - Dialup</option>\n<option".
129 (($data[2] eq 'wi') ? ' selected' : '') ." value='wi'>Static IP - Wireless</option>\n<option".
130 (($data[2] eq 'sp') ? ' selected' : '') ." value='sp'>Static Pool - Server pool</option>\n<option".
131 (($data[2] eq 'cp') ? ' selected' : '') ." value='cp'>Static Pool - Cable</option>\n<option".
132 (($data[2] eq 'dp') ? ' selected' : '') ." value='dp'>Static Pool - DSL</option>\n<option".
133 (($data[2] eq 'mp') ? ' selected' : '') ." value='mp'>Static Pool - Dialup</option>\n<option".
134 (($data[2] eq 'wp') ? ' selected' : '') ." value='wp'>Static Pool - Wireless</option>\n<option".
135 (($data[2] eq 'ee') ? ' selected' : '') ." value='ee'>End-use netblock</option>\n<option".
136 (($data[2] eq 'dn') ? ' selected' : '') ." value='dn'>Dialup netblock</option>\n<option".
137 (($data[2] eq 'dy') ? ' selected' : '') ." value='dy'>Dynamic DSL netblock</option>\n<option".
138 (($data[2] eq 'dc') ? ' selected' : '') ." value='dc'>Dynamic cable netblock</option>\n<option".
139 (($data[2] eq 'ii') ? ' selected' : '') ." value='ii'>Internal netblock</option>\n".
140 "</select></td>\n";
141 print qq(<td><input name=city value="$data[3]"></td>\n).
142 "<td>$data[4]</td><td>$data[5]</td>".
143 qq(<td><input type=submit value="Update"></td></form></tr>\n);
144 }
145 print "</table>\n";
146
147 # notes
148 print "<hr><b>Notes:</b>\n".
149 "<ul>\n<li>Use the main interface to update description and notes fields\n".
150 "<li>Changing the allocation type here will NOT affect IP pool data.\n".
151 "</ul>\n";
152}
153
154
155# Stuff updates into DB
156sub update {
157 eval {
158 # Relatively simple SQL transaction here. Note that we're deliberately NOT
159 # updating notes/desc here as it's available through the main interface.
160 $sth = $ip_dbh->prepare("update allocations set custid='$webvar{custid}',".
161 "city='$webvar{city}',type='$webvar{alloctype}' where cidr='$webvar{block}'");
162 $sth->execute;
163 $ip_dbh->commit;
164 };
165 if ($@) {
166 carp "Transaction aborted because $@";
167 eval { $ip_dbh->rollback; };
168 syslog "err", "$authuser could not update block/IP '$webvar{block}': '$@'";
169 } else {
170 # If we get here, the operation succeeded.
171 syslog "notice", "$authuser updated $webvar{block}";
172 print "Allocation $webvar{block} updated<hr>\n";
173 }
174 # need to get /24 that block is part of
175 my @bits = split /\./, $webvar{block};
176 $bits[3] = "0/24";
177 showAllocs((join ".", @bits));
178}
Note: See TracBrowser for help on using the repository browser.