Changeset 233 for trunk/cgi-bin/main.cgi
- Timestamp:
- 04/15/05 18:11:39 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cgi-bin/main.cgi
r231 r233 22 22 openlog "IPDB","pid","local2"; 23 23 24 # Collect the username from HTTP auth. If undefined, we're in a test environment. 24 # Collect the username from HTTP auth. If undefined, we're in 25 # a test environment, or called without a username. 25 26 my $authuser; 26 27 if (!defined($ENV{'REMOTE_USER'})) { … … 39 40 ($ip_dbh,$errstr) = connectDB_My; 40 41 if (!$ip_dbh) { 41 printAndExit("Database error: $errstr\n");42 exitError("Database error: $errstr\n"); 42 43 } 43 44 initIPDBGlobals($ip_dbh); 45 46 # Headerize! Make sure we replace the $$EXTRA0$$ bit as needed. 47 printHeader('', ($IPDBacl{$authuser} =~ /a/ ? 48 '<a href="/ip/cgi-bin/main.cgi?action=assign">Add new assignment</a>' : '' 49 )); 50 44 51 45 52 #prototypes … … 64 71 if($webvar{action} eq 'index') { 65 72 showSummary(); 73 } elsif ($webvar{action} eq 'addmaster') { 74 if ($IPDBacl{$authuser} !~ /a/) { 75 printError("You shouldn't have been able to get here. Access denied."); 76 } else { 77 open HTML, "<../addmaster.html"; 78 print while <HTML>; 79 } 66 80 } elsif ($webvar{action} eq 'newmaster') { 67 printHeader(''); 68 69 my $cidr = new NetAddr::IP $webvar{cidr}; 70 71 print "<div type=heading align=center>Adding $cidr as master block....</div>\n"; 72 73 # Allow transactions, and raise an exception on errors so we can catch it later. 74 # Use local to make sure these get "reset" properly on exiting this block 75 local $ip_dbh->{AutoCommit} = 0; 76 local $ip_dbh->{RaiseError} = 1; 77 78 # Wrap the SQL in a transaction 79 eval { 80 $sth = $ip_dbh->prepare("insert into masterblocks values ('$webvar{cidr}')"); 81 $sth->execute; 81 82 if ($IPDBacl{$authuser} !~ /a/) { 83 printError("You shouldn't have been able to get here. Access denied."); 84 } else { 85 86 my $cidr = new NetAddr::IP $webvar{cidr}; 87 88 print "<div type=heading align=center>Adding $cidr as master block....</div>\n"; 89 90 # Allow transactions, and raise an exception on errors so we can catch it later. 91 # Use local to make sure these get "reset" properly on exiting this block 92 local $ip_dbh->{AutoCommit} = 0; 93 local $ip_dbh->{RaiseError} = 1; 94 95 # Wrap the SQL in a transaction 96 eval { 97 $sth = $ip_dbh->prepare("insert into masterblocks values ('$webvar{cidr}')"); 98 $sth->execute; 82 99 83 100 # Unrouted blocks aren't associated with a city (yet). We don't rely on this … … 85 102 # Thus the "routed" flag. 86 103 87 $sth = $ip_dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".104 $sth = $ip_dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)". 88 105 " values ('$webvar{cidr}',".$cidr->masklen.",'<NULL>','n')"); 89 $sth->execute; 90 91 # If we get here, everything is happy. Commit changes. 92 $ip_dbh->commit; 93 }; # end eval 94 95 if ($@) { 96 carp "Transaction aborted because $@"; 97 eval { $ip_dbh->rollback; }; 98 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$@'"; 99 printError("Could not add master block $webvar{cidr} to database: $@"); 100 } else { 101 print "<div type=heading align=center>Success!</div>\n"; 102 syslog "info", "$authuser added master block $webvar{cidr}"; 103 } 106 $sth->execute; 107 108 # If we get here, everything is happy. Commit changes. 109 $ip_dbh->commit; 110 }; # end eval 111 112 if ($@) { 113 carp "Transaction aborted because $@"; 114 eval { $ip_dbh->rollback; }; 115 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$@'"; 116 printError("Could not add master block $webvar{cidr} to database: $@"); 117 } else { 118 print "<div type=heading align=center>Success!</div>\n"; 119 syslog "info", "$authuser added master block $webvar{cidr}"; 120 } 121 122 } # ACL check 104 123 105 124 } # end add new master … … 115 134 } 116 135 elsif($webvar{action} eq 'search') { 117 printHeader('');118 136 if (!$webvar{input}) { 119 137 # No search term. Display everything. … … 153 171 # which is not in any way guaranteed to provide anything useful. 154 172 else { 155 printHeader('');156 173 my $rnd = rand 500; 157 174 my $boing = sprintf("%.2f", rand 500); … … 171 188 print qq(<div align=right style="position: absolute; right: 30px;">). 172 189 qq(<a href="/ip/cgi-bin/admin.cgi">Admin tools</a></div><br>\n) 173 if $ authuser =~ /kdeugau|jodyh|jipp/;190 if $IPDBacl{$authuser} =~ /A/; 174 191 175 192 # We print the footer here, so we don't have to do it elsewhere. … … 392 409 # Initial display: Show master blocks with total allocated subnets, total free subnets 393 410 sub showSummary { 394 # this is horrible-ugly-bad and will Go Away real soon now(TM)395 print "Content-type: text/html\n\n";396 411 397 412 startTable('Master netblock', 'Routed netblocks', 'Allocated netblocks', … … 450 465 } 451 466 print "</table>\n"; 452 print qq(<a href="/ip/addmaster.shtml">Add new master block</a><br><br>\n); 467 if ($IPDBacl{$authuser} =~ /a/) { 468 print qq(<a href="/ip/cgi-bin/main.cgi?action=addmaster">Add new master block</a><br><br>\n); 469 } 453 470 print "Note: Free blocks noted here include both routed and unrouted blocks.\n"; 454 471 … … 462 479 # else should follow. YMMV.) 463 480 sub showMaster { 464 printHeader('');465 481 466 482 print qq(<center><div class="heading">Summarizing routed blocks for ). … … 538 554 print qq(<hr width="60%"><center><div class="heading">No allocations in ). 539 555 qq($master.</div>\n). 540 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n). 541 qq(<input type=hidden name=action value="delete">\n). 542 qq(<input type=hidden name=block value="$master">\n). 543 qq(<input type=hidden name=alloctype value="mm">\n). 544 qq(<input type=submit value=" Remove this master ">\n). 545 qq(</form></center>\n); 556 ($IPDBacl{$authuser} =~ /d/ ? 557 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n). 558 qq(<input type=hidden name=action value="delete">\n). 559 qq(<input type=hidden name=block value="$master">\n). 560 qq(<input type=hidden name=alloctype value="mm">\n). 561 qq(<input type=submit value=" Remove this master ">\n). 562 qq(</form></center>\n) : 563 ''); 546 564 547 565 } # end check for existence of routed blocks in master … … 577 595 # not have anything useful to spew. 578 596 sub showRBlock { 579 printHeader('');580 597 581 598 my $master = new NetAddr::IP $webvar{block}; … … 625 642 print qq(<hr width="60%"><center><div class="heading">No allocations in ). 626 643 qq($master.</div></center>\n). 627 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n). 628 qq(<input type=hidden name=action value="delete">\n). 629 qq(<input type=hidden name=block value="$master">\n). 630 qq(<input type=hidden name=alloctype value="rm">\n). 631 qq(<input type=submit value=" Remove this block ">\n). 632 qq(</form>\n); 644 ($IPDBacl{$authuser} =~ /d/ ? 645 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n). 646 qq(<input type=hidden name=action value="delete">\n). 647 qq(<input type=hidden name=block value="$master">\n). 648 qq(<input type=hidden name=alloctype value="rm">\n). 649 qq(<input type=submit value=" Remove this block ">\n). 650 qq(</form>\n) : 651 ''); 633 652 } 634 653 … … 649 668 # Include some HairyPerl(TM) to prefix subblocks with "Sub " 650 669 my @row = ((($data[1] ne 'y' && $data[1] ne 'n') ? 'Sub ' : ''). 651 qq(<a href="/ip/cgi-bin/main.cgi?action=assign&block=$cidr&fbtype=$data[1]">$cidr</a>),670 ($IPDBacl{$authuser} =~ /a/ ? qq(<a href="/ip/cgi-bin/main.cgi?action=assign&block=$cidr&fbtype=$data[1]">$cidr</a>) : $cidr), 652 671 $cidr->range); 653 672 printRow(\@row, 'color1') if ($count%2 == 0); … … 662 681 # List the IPs used in a pool 663 682 sub listPool { 664 printHeader('');665 683 666 684 my $cidr = new NetAddr::IP $webvar{pool}; … … 708 726 my @row = ( qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>), 709 727 $data[1],$data[2],$data[3], 710 ( ( $data[2] eq 'n') ?728 ( (($data[2] eq 'n') && ($IPDBacl{$authuser} =~ /d/)) ? 711 729 ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[0]&". 712 730 "alloctype=$data[4]\">Unassign this IP</a>") : … … 725 743 # be one of two templates, and the lists come from the database. 726 744 sub assignBlock { 727 printHeader(''); 745 746 if ($IPDBacl{$authuser} !~ /a/) { 747 printError("You shouldn't have been able to get here. Access denied."); 748 return; 749 } 728 750 729 751 my $html; … … 803 825 # Take info on requested IP assignment and see what we can provide. 804 826 sub confirmAssign { 805 printHeader(''); 827 if ($IPDBacl{$authuser} !~ /a/) { 828 printError("You shouldn't have been able to get here. Access denied."); 829 return; 830 } 806 831 807 832 my $cidr; … … 962 987 # Do the work of actually inserting a block in the database. 963 988 sub insertAssign { 989 if ($IPDBacl{$authuser} !~ /a/) { 990 printError("You shouldn't have been able to get here. Access denied."); 991 return; 992 } 964 993 # Some things are done more than once. 965 printHeader('');966 994 return if !validateInput(); 967 995 … … 1061 1089 # action=edit 1062 1090 sub edit { 1063 printHeader('');1064 1091 1065 1092 my $sql; … … 1081 1108 $data[2] =~ s/\s//; 1082 1109 1083 ##fixme LEGACY CODE1084 # Postfix "i" on pool IP types1085 if ($data[2] =~ /^[cdsmw]$/) {1086 $data[2] .= "i";1087 }1088 1089 1110 open (HTML, "../editDisplay.html") 1090 1111 or croak "Could not open editDisplay.html :$!"; … … 1097 1118 # Needs thinking. Have to allow changes to city to correct errors, no? 1098 1119 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g; 1099 $html =~ s/\$\$CITY\$\$/$data[3]/g; 1120 1121 if ($IPDBacl{$authuser} =~ /c/) { 1122 $html =~ s/\$\$CUSTID\$\$/<input type=text name=custid value="$data[1]" maxlength=15 class="regular">/; 1100 1123 1101 1124 # Screw it. Changing allocation types gets very ugly VERY quickly- especially … … 1106 1129 1107 1130 ##fixme The check here should be built from the database 1108 if ($data[2] =~ /^.[ne]$/) {1109 # Block that can be changed1110 my $blockoptions = "<select name=alloctype><option".1131 if ($data[2] =~ /^.[ne]$/) { 1132 # Block that can be changed 1133 my $blockoptions = "<select name=alloctype><option". 1111 1134 (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option". 1112 1135 (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option". … … 1117 1140 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n". 1118 1141 "</select>\n"; 1119 $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g; 1142 $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g; 1143 } else { 1144 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g; 1145 } 1146 $html =~ s/\$\$CITY\$\$/<input type=text name=city value="$data[3]">/g; 1147 $html =~ s/\$\$CIRCID\$\$/<input type="text" name="circid" value="$data[4]" maxlength=64 size=64 class="regular">/g; 1148 $html =~ s/\$\$DESC\$\$/<input type="text" name="desc" value="$data[5]" maxlength=64 size=64 class="regular">/g; 1149 $html =~ s|\$\$NOTES\$\$|<textarea rows="8" cols="64" name="notes" class="regular">$data[6]</textarea>|g; 1120 1150 } else { 1121 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g; 1122 } 1123 1124 # These can be modified, although CustID changes may get ignored. 1125 $html =~ s/\$\$CUSTID\$\$/$data[1]/g; 1126 $html =~ s/\$\$TYPE\$\$/$data[2]/g; 1127 $html =~ s/\$\$CIRCID\$\$/$data[4]/g; 1128 $html =~ s/\$\$DESC\$\$/$data[5]/g; 1129 $html =~ s/\$\$NOTES\$\$/$data[6]/g; 1151 $html =~ s/\$\$CUSTID\$\$/$data[1]/g; 1152 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}/g; 1153 $html =~ s/\$\$CITY\$\$/$data[3]/g; 1154 $html =~ s/\$\$CIRCID\$\$/$data[4]/g; 1155 $html =~ s/\$\$DESC\$\$/$data[5]/g; 1156 $html =~ s/\$\$NOTES\$\$/$data[6]/g; 1157 } 1158 1159 # More ACL trickery - we can live with forms that don't submit, 1160 # but we can't leave the extra table rows there, and we *really* 1161 # can't leave the submit buttons there. 1162 my $updok = ''; 1163 my $i=2; 1164 if ($IPDBacl{$authuser} =~ /c/) { 1165 $updok = qq(<tr class="color$i"><td colspan=2 class=regular><div class="center">). 1166 qq(<input type="submit" value=" Update this block " class="regular">). 1167 "</div></td></tr></form>\n"; 1168 $i--; 1169 } 1170 $html =~ s/\$\$UPDOK\$\$/$updok/g; 1171 1172 my $delok = ''; 1173 if ($IPDBacl{$authuser} =~ /d/) { 1174 $delok = qq(<form method="POST" action="main.cgi"> 1175 <tr class="color$i"><td colspan=2 class="regular"><div class=center> 1176 <input type="hidden" name="action" value="delete"> 1177 <input type="hidden" name="block" value="$webvar{block}"> 1178 <input type="hidden" name="alloctype" value="$data[2]"> 1179 <input type=submit value=" Delete this block "> 1180 </div></td></tr>); 1181 } 1182 $html =~ s/\$\$DELOK\$\$/$delok/; 1130 1183 1131 1184 print $html; … … 1137 1190 # action=update 1138 1191 sub update { 1139 printHeader('');1140 1192 1141 1193 # Make sure incoming data is in correct format - custID among other things. … … 1196 1248 # Delete an allocation. 1197 1249 sub remove { 1198 printHeader(''); 1250 if ($IPDBacl{$authuser} !~ /d/) { 1251 printError("You shouldn't have been able to get here. Access denied."); 1252 return; 1253 } 1254 1199 1255 #show confirm screen. 1200 1256 open HTML, "../confirmRemove.html" … … 1286 1342 # Remove IPs from pool listing if necessary 1287 1343 sub finalDelete { 1288 printHeader(''); 1344 if ($IPDBacl{$authuser} !~ /d/) { 1345 printError("You shouldn't have been able to get here. Access denied."); 1346 return; 1347 } 1289 1348 1290 1349 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype}); … … 1309 1368 1310 1369 1370 sub exitError { 1371 my $errStr = $_[0]; 1372 printHeader('',''); 1373 print qq(<center><p class="regular"> $errStr </p> 1374 <input type="button" value="Back" onclick="history.go(-1)"> 1375 </center> 1376 ); 1377 printFooter(); 1378 exit; 1379 } # errorExit 1380 1381 1311 1382 # Just in case we manage to get here. 1312 1383 exit 0;
Note:
See TracChangeset
for help on using the changeset viewer.