Changeset 242 for branches/stable/cgi-bin/main.cgi
- Timestamp:
- 04/19/05 15:42:43 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/cgi-bin/main.cgi
r224 r242 23 23 openlog "IPDB","pid","local2"; 24 24 25 # Collect the username from HTTP auth. If undefined, we're in a test environment. 25 # Collect the username from HTTP auth. If undefined, we're in 26 # a test environment, or called without a username. 26 27 my $authuser; 27 28 if (!defined($ENV{'REMOTE_USER'})) { … … 40 41 ($ip_dbh,$errstr) = connectDB_My; 41 42 if (!$ip_dbh) { 42 printAndExit("Database error: $errstr\n");43 exitError("Database error: $errstr\n"); 43 44 } 44 45 initIPDBGlobals($ip_dbh); 46 47 # Headerize! Make sure we replace the $$EXTRA0$$ bit as needed. 48 printHeader('', ($IPDBacl{$authuser} =~ /a/ ? 49 '<a href="/ip/cgi-bin/main.cgi?action=assign">Add new assignment</a>' : '' 50 )); 51 45 52 46 53 #prototypes … … 65 72 if($webvar{action} eq 'index') { 66 73 showSummary(); 74 } elsif ($webvar{action} eq 'addmaster') { 75 if ($IPDBacl{$authuser} !~ /a/) { 76 printError("You shouldn't have been able to get here. Access denied."); 77 } else { 78 open HTML, "<../addmaster.html"; 79 print while <HTML>; 80 } 67 81 } elsif ($webvar{action} eq 'newmaster') { 68 printHeader(''); 69 70 my $cidr = new NetAddr::IP $webvar{cidr}; 71 72 print "<div type=heading align=center>Adding $cidr as master block....</div>\n"; 73 74 # Allow transactions, and raise an exception on errors so we can catch it later. 75 # Use local to make sure these get "reset" properly on exiting this block 76 local $ip_dbh->{AutoCommit} = 0; 77 local $ip_dbh->{RaiseError} = 1; 78 79 # Wrap the SQL in a transaction 80 eval { 81 $sth = $ip_dbh->prepare("insert into masterblocks values ('$webvar{cidr}')"); 82 $sth->execute; 82 83 if ($IPDBacl{$authuser} !~ /a/) { 84 printError("You shouldn't have been able to get here. Access denied."); 85 } else { 86 87 my $cidr = new NetAddr::IP $webvar{cidr}; 88 89 print "<div type=heading align=center>Adding $cidr as master block....</div>\n"; 90 91 # Allow transactions, and raise an exception on errors so we can catch it later. 92 # Use local to make sure these get "reset" properly on exiting this block 93 local $ip_dbh->{AutoCommit} = 0; 94 local $ip_dbh->{RaiseError} = 1; 95 96 # Wrap the SQL in a transaction 97 eval { 98 $sth = $ip_dbh->prepare("insert into masterblocks values ('$webvar{cidr}')"); 99 $sth->execute; 83 100 84 101 # Unrouted blocks aren't associated with a city (yet). We don't rely on this … … 86 103 # Thus the "routed" flag. 87 104 88 $sth = $ip_dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".105 $sth = $ip_dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)". 89 106 " values ('$webvar{cidr}',".$cidr->masklen.",'<NULL>','n')"); 90 $sth->execute; 91 92 # If we get here, everything is happy. Commit changes. 93 $ip_dbh->commit; 94 }; # end eval 95 96 if ($@) { 97 carp "Transaction aborted because $@"; 98 eval { $ip_dbh->rollback; }; 99 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$@'"; 100 printError("Could not add master block $webvar{cidr} to database: $@"); 101 } else { 102 print "<div type=heading align=center>Success!</div>\n"; 103 syslog "info", "$authuser added master block $webvar{cidr}"; 104 } 107 $sth->execute; 108 109 # If we get here, everything is happy. Commit changes. 110 $ip_dbh->commit; 111 }; # end eval 112 113 if ($@) { 114 carp "Transaction aborted because $@"; 115 eval { $ip_dbh->rollback; }; 116 syslog "err", "Could not add master block '$webvar{cidr}' to database: '$@'"; 117 printError("Could not add master block $webvar{cidr} to database: $@"); 118 } else { 119 print "<div type=heading align=center>Success!</div>\n"; 120 syslog "info", "$authuser added master block $webvar{cidr}"; 121 } 122 123 } # ACL check 105 124 106 125 } # end add new master … … 116 135 } 117 136 elsif($webvar{action} eq 'search') { 118 printHeader('');119 137 if (!$webvar{input}) { 120 138 # No search term. Display everything. … … 154 172 # which is not in any way guaranteed to provide anything useful. 155 173 else { 156 printHeader('');157 174 my $rnd = rand 500; 158 175 my $boing = sprintf("%.2f", rand 500); … … 172 189 print qq(<div align=right style="position: absolute; right: 30px;">). 173 190 qq(<a href="/ip/cgi-bin/admin.cgi">Admin tools</a></div><br>\n) 174 if $ authuser =~ /kdeugau|jodyh|jipp/;191 if $IPDBacl{$authuser} =~ /A/; 175 192 176 193 # We print the footer here, so we don't have to do it elsewhere. … … 393 410 # Initial display: Show master blocks with total allocated subnets, total free subnets 394 411 sub showSummary { 395 # this is horrible-ugly-bad and will Go Away real soon now(TM)396 print "Content-type: text/html\n\n";397 412 398 413 startTable('Master netblock', 'Routed netblocks', 'Allocated netblocks', … … 451 466 } 452 467 print "</table>\n"; 453 print qq(<a href="/ip/addmaster.shtml">Add new master block</a><br><br>\n); 468 if ($IPDBacl{$authuser} =~ /a/) { 469 print qq(<a href="/ip/cgi-bin/main.cgi?action=addmaster">Add new master block</a><br><br>\n); 470 } 454 471 print "Note: Free blocks noted here include both routed and unrouted blocks.\n"; 455 472 … … 463 480 # else should follow. YMMV.) 464 481 sub showMaster { 465 printHeader('');466 482 467 483 print qq(<center><div class="heading">Summarizing routed blocks for ). … … 539 555 print qq(<hr width="60%"><center><div class="heading">No allocations in ). 540 556 qq($master.</div>\n). 541 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n). 542 qq(<input type=hidden name=action value="delete">\n). 543 qq(<input type=hidden name=block value="$master">\n). 544 qq(<input type=hidden name=alloctype value="mm">\n). 545 qq(<input type=submit value=" Remove this master ">\n). 546 qq(</form></center>\n); 557 ($IPDBacl{$authuser} =~ /d/ ? 558 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n). 559 qq(<input type=hidden name=action value="delete">\n). 560 qq(<input type=hidden name=block value="$master">\n). 561 qq(<input type=hidden name=alloctype value="mm">\n). 562 qq(<input type=submit value=" Remove this master ">\n). 563 qq(</form></center>\n) : 564 ''); 547 565 548 566 } # end check for existence of routed blocks in master … … 578 596 # not have anything useful to spew. 579 597 sub showRBlock { 580 printHeader('');581 598 582 599 my $master = new NetAddr::IP $webvar{block}; … … 626 643 print qq(<hr width="60%"><center><div class="heading">No allocations in ). 627 644 qq($master.</div></center>\n). 628 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n). 629 qq(<input type=hidden name=action value="delete">\n). 630 qq(<input type=hidden name=block value="$master">\n). 631 qq(<input type=hidden name=alloctype value="rm">\n). 632 qq(<input type=submit value=" Remove this block ">\n). 633 qq(</form>\n); 645 ($IPDBacl{$authuser} =~ /d/ ? 646 qq(<form action="/ip/cgi-bin/main.cgi" method=POST>\n). 647 qq(<input type=hidden name=action value="delete">\n). 648 qq(<input type=hidden name=block value="$master">\n). 649 qq(<input type=hidden name=alloctype value="rm">\n). 650 qq(<input type=submit value=" Remove this block ">\n). 651 qq(</form>\n) : 652 ''); 634 653 } 635 654 … … 650 669 # Include some HairyPerl(TM) to prefix subblocks with "Sub " 651 670 my @row = ((($data[1] ne 'y' && $data[1] ne 'n') ? 'Sub ' : ''). 652 qq(<a href="/ip/cgi-bin/main.cgi?action=assign&block=$cidr&fbtype=$data[1]">$cidr</a>),671 ($IPDBacl{$authuser} =~ /a/ ? qq(<a href="/ip/cgi-bin/main.cgi?action=assign&block=$cidr&fbtype=$data[1]">$cidr</a>) : $cidr), 653 672 $cidr->range); 654 673 printRow(\@row, 'color1') if ($count%2 == 0); … … 663 682 # List the IPs used in a pool 664 683 sub listPool { 665 printHeader('');666 684 667 685 my $cidr = new NetAddr::IP $webvar{pool}; … … 709 727 my @row = ( qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>), 710 728 $data[1],$data[2],$data[3], 711 ( ( $data[2] eq 'n') ?729 ( (($data[2] eq 'n') && ($IPDBacl{$authuser} =~ /d/)) ? 712 730 ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[0]&". 713 731 "alloctype=$data[4]\">Unassign this IP</a>") : … … 726 744 # be one of two templates, and the lists come from the database. 727 745 sub assignBlock { 728 printHeader(''); 746 747 if ($IPDBacl{$authuser} !~ /a/) { 748 printError("You shouldn't have been able to get here. Access denied."); 749 return; 750 } 729 751 730 752 my $html; … … 804 826 # Take info on requested IP assignment and see what we can provide. 805 827 sub confirmAssign { 806 printHeader(''); 828 if ($IPDBacl{$authuser} !~ /a/) { 829 printError("You shouldn't have been able to get here. Access denied."); 830 return; 831 } 807 832 808 833 my $cidr; … … 963 988 # Do the work of actually inserting a block in the database. 964 989 sub insertAssign { 990 if ($IPDBacl{$authuser} !~ /a/) { 991 printError("You shouldn't have been able to get here. Access denied."); 992 return; 993 } 965 994 # Some things are done more than once. 966 printHeader('');967 995 return if !validateInput(); 968 996 … … 1078 1106 # action=edit 1079 1107 sub edit { 1080 printHeader('');1081 1108 1082 1109 my $sql; … … 1098 1125 $data[2] =~ s/\s//; 1099 1126 1100 ##fixme LEGACY CODE1101 # Postfix "i" on pool IP types1102 if ($data[2] =~ /^[cdsmw]$/) {1103 $data[2] .= "i";1104 }1105 1106 1127 open (HTML, "../editDisplay.html") 1107 1128 or croak "Could not open editDisplay.html :$!"; … … 1114 1135 # Needs thinking. Have to allow changes to city to correct errors, no? 1115 1136 $html =~ s/\$\$BLOCK\$\$/$webvar{block}/g; 1116 $html =~ s/\$\$CITY\$\$/$data[3]/g; 1137 1138 if ($IPDBacl{$authuser} =~ /c/) { 1139 $html =~ s/\$\$CUSTID\$\$/<input type=text name=custid value="$data[1]" maxlength=15 class="regular">/; 1117 1140 1118 1141 # Screw it. Changing allocation types gets very ugly VERY quickly- especially … … 1123 1146 1124 1147 ##fixme The check here should be built from the database 1125 if ($data[2] =~ /^.[ne]$/) {1126 # Block that can be changed1127 my $blockoptions = "<select name=alloctype><option".1148 if ($data[2] =~ /^.[ne]$/) { 1149 # Block that can be changed 1150 my $blockoptions = "<select name=alloctype><option". 1128 1151 (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option". 1129 1152 (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option". … … 1134 1157 (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n". 1135 1158 "</select>\n"; 1136 $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g; 1159 $html =~ s/\$\$TYPESELECT\$\$/$blockoptions/g; 1160 } else { 1161 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g; 1162 } 1163 $html =~ s/\$\$CITY\$\$/<input type=text name=city value="$data[3]">/g; 1164 $html =~ s/\$\$CIRCID\$\$/<input type="text" name="circid" value="$data[4]" maxlength=64 size=64 class="regular">/g; 1165 $html =~ s/\$\$DESC\$\$/<input type="text" name="desc" value="$data[5]" maxlength=64 size=64 class="regular">/g; 1166 $html =~ s|\$\$NOTES\$\$|<textarea rows="8" cols="64" name="notes" class="regular">$data[6]</textarea>|g; 1137 1167 } else { 1138 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}<input type=hidden name=alloctype value="$data[2]">/g; 1139 } 1140 1141 # These can be modified, although CustID changes may get ignored. 1142 $html =~ s/\$\$CUSTID\$\$/$data[1]/g; 1143 $html =~ s/\$\$TYPE\$\$/$data[2]/g; 1144 $html =~ s/\$\$CIRCID\$\$/$data[4]/g; 1145 $html =~ s/\$\$DESC\$\$/$data[5]/g; 1146 $html =~ s/\$\$NOTES\$\$/$data[6]/g; 1168 $html =~ s/\$\$CUSTID\$\$/$data[1]/g; 1169 $html =~ s/\$\$TYPESELECT\$\$/$disp_alloctypes{$data[2]}/g; 1170 $html =~ s/\$\$CITY\$\$/$data[3]/g; 1171 $html =~ s/\$\$CIRCID\$\$/$data[4]/g; 1172 $html =~ s/\$\$DESC\$\$/$data[5]/g; 1173 $html =~ s/\$\$NOTES\$\$/$data[6]/g; 1174 } 1175 1176 # More ACL trickery - we can live with forms that don't submit, 1177 # but we can't leave the extra table rows there, and we *really* 1178 # can't leave the submit buttons there. 1179 my $updok = ''; 1180 my $i=2; 1181 if ($IPDBacl{$authuser} =~ /c/) { 1182 $updok = qq(<tr class="color$i"><td colspan=2 class=regular><div class="center">). 1183 qq(<input type="submit" value=" Update this block " class="regular">). 1184 "</div></td></tr></form>\n"; 1185 $i--; 1186 } 1187 $html =~ s/\$\$UPDOK\$\$/$updok/g; 1188 1189 my $delok = ''; 1190 if ($IPDBacl{$authuser} =~ /d/) { 1191 $delok = qq(<form method="POST" action="main.cgi"> 1192 <tr class="color$i"><td colspan=2 class="regular"><div class=center> 1193 <input type="hidden" name="action" value="delete"> 1194 <input type="hidden" name="block" value="$webvar{block}"> 1195 <input type="hidden" name="alloctype" value="$data[2]"> 1196 <input type=submit value=" Delete this block "> 1197 </div></td></tr>); 1198 } 1199 $html =~ s/\$\$DELOK\$\$/$delok/; 1147 1200 1148 1201 print $html; … … 1154 1207 # action=update 1155 1208 sub update { 1156 printHeader('');1157 1209 1158 1210 # Make sure incoming data is in correct format - custID among other things. … … 1213 1265 # Delete an allocation. 1214 1266 sub remove { 1215 printHeader(''); 1267 if ($IPDBacl{$authuser} !~ /d/) { 1268 printError("You shouldn't have been able to get here. Access denied."); 1269 return; 1270 } 1271 1216 1272 #show confirm screen. 1217 1273 open HTML, "../confirmRemove.html" … … 1303 1359 # Remove IPs from pool listing if necessary 1304 1360 sub finalDelete { 1305 printHeader(''); 1361 if ($IPDBacl{$authuser} !~ /d/) { 1362 printError("You shouldn't have been able to get here. Access denied."); 1363 return; 1364 } 1306 1365 1307 1366 my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype}); … … 1326 1385 1327 1386 1387 sub exitError { 1388 my $errStr = $_[0]; 1389 printHeader('',''); 1390 print qq(<center><p class="regular"> $errStr </p> 1391 <input type="button" value="Back" onclick="history.go(-1)"> 1392 </center> 1393 ); 1394 printFooter(); 1395 exit; 1396 } # errorExit 1397 1398 1328 1399 # Just in case we manage to get here. 1329 1400 exit 0;
Note:
See TracChangeset
for help on using the changeset viewer.