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