Ignore:
Timestamp:
04/19/05 15:42:43 (20 years ago)
Author:
Kris Deugau
Message:

/branches/stable

Merge ACL support from /branches/acl up to r241

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/cgi-bin/main.cgi

    r224 r242  
    2323openlog "IPDB","pid","local2";
    2424
    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.
    2627my $authuser;
    2728if (!defined($ENV{'REMOTE_USER'})) {
     
    4041($ip_dbh,$errstr) = connectDB_My;
    4142if (!$ip_dbh) {
    42   printAndExit("Database error: $errstr\n");
     43  exitError("Database error: $errstr\n");
    4344}
    4445initIPDBGlobals($ip_dbh);
     46
     47# Headerize!  Make sure we replace the $$EXTRA0$$ bit as needed.
     48printHeader('', ($IPDBacl{$authuser} =~ /a/ ?
     49        '<a href="/ip/cgi-bin/main.cgi?action=assign">Add new assignment</a>' : ''
     50        ));
     51
    4552
    4653#prototypes
     
    6572if($webvar{action} eq 'index') {
    6673  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  }
    6781} 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;
    83100
    84101# Unrouted blocks aren't associated with a city (yet).  We don't rely on this
     
    86103# Thus the "routed" flag.
    87104
    88     $sth = $ip_dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
     105      $sth = $ip_dbh->prepare("insert into freeblocks (cidr,maskbits,city,routed)".
    89106        " 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
    105124
    106125} # end add new master
     
    116135}
    117136elsif($webvar{action} eq 'search') {
    118   printHeader('');
    119137  if (!$webvar{input}) {
    120138    # No search term.  Display everything.
     
    154172# which is not in any way guaranteed to provide anything useful.
    155173else {
    156   printHeader('');
    157174  my $rnd = rand 500;
    158175  my $boing = sprintf("%.2f", rand 500);
     
    172189print qq(<div align=right style="position: absolute; right: 30px;">).
    173190        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/;
    175192
    176193# We print the footer here, so we don't have to do it elsewhere.
     
    393410# Initial display:  Show master blocks with total allocated subnets, total free subnets
    394411sub showSummary {
    395   # this is horrible-ugly-bad and will Go Away real soon now(TM)
    396   print "Content-type: text/html\n\n";
    397412
    398413  startTable('Master netblock', 'Routed netblocks', 'Allocated netblocks',
     
    451466  }
    452467  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  }
    454471  print "Note:  Free blocks noted here include both routed and unrouted blocks.\n";
    455472
     
    463480# else should follow.  YMMV.)
    464481sub showMaster {
    465   printHeader('');
    466482
    467483  print qq(<center><div class="heading">Summarizing routed blocks for ).
     
    539555    print qq(<hr width="60%"><center><div class="heading">No allocations in ).
    540556        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                '');
    547565
    548566  } # end check for existence of routed blocks in master
     
    578596# not have anything useful to spew.
    579597sub showRBlock {
    580   printHeader('');
    581598
    582599  my $master = new NetAddr::IP $webvar{block};
     
    626643    print qq(<hr width="60%"><center><div class="heading">No allocations in ).
    627644        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                '');
    634653  }
    635654
     
    650669    # Include some HairyPerl(TM) to prefix subblocks with "Sub "
    651670    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),
    653672        $cidr->range);
    654673    printRow(\@row, 'color1') if ($count%2 == 0);
     
    663682# List the IPs used in a pool
    664683sub listPool {
    665   printHeader('');
    666684
    667685  my $cidr = new NetAddr::IP $webvar{pool};
     
    709727    my @row = ( qq(<a href="/ip/cgi-bin/main.cgi?action=edit&block=$data[0]">$data[0]</a>),
    710728        $data[1],$data[2],$data[3],
    711         ( ($data[2] eq 'n') ?
     729        ( (($data[2] eq 'n') && ($IPDBacl{$authuser} =~ /d/)) ?
    712730          ("<a href=\"/ip/cgi-bin/main.cgi?action=delete&block=$data[0]&".
    713731           "alloctype=$data[4]\">Unassign this IP</a>") :
     
    726744# be one of two templates, and the lists come from the database.
    727745sub 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  }
    729751
    730752  my $html;
     
    804826# Take info on requested IP assignment and see what we can provide.
    805827sub confirmAssign {
    806   printHeader('');
     828  if ($IPDBacl{$authuser} !~ /a/) {
     829    printError("You shouldn't have been able to get here.  Access denied.");
     830    return;
     831  }
    807832
    808833  my $cidr;
     
    963988# Do the work of actually inserting a block in the database.
    964989sub insertAssign {
     990  if ($IPDBacl{$authuser} !~ /a/) {
     991    printError("You shouldn't have been able to get here.  Access denied.");
     992    return;
     993  }
    965994  # Some things are done more than once.
    966   printHeader('');
    967995  return if !validateInput();
    968996
     
    10781106# action=edit
    10791107sub edit {
    1080   printHeader('');
    10811108
    10821109  my $sql;
     
    10981125  $data[2] =~ s/\s//;
    10991126
    1100 ##fixme LEGACY CODE
    1101   # Postfix "i" on pool IP types
    1102   if ($data[2] =~ /^[cdsmw]$/) {
    1103     $data[2] .= "i";
    1104   }
    1105 
    11061127  open (HTML, "../editDisplay.html")
    11071128        or croak "Could not open editDisplay.html :$!";
     
    11141135# Needs thinking.  Have to allow changes to city to correct errors, no?
    11151136  $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">/;
    11171140
    11181141# Screw it.  Changing allocation types gets very ugly VERY quickly- especially
     
    11231146
    11241147##fixme The check here should be built from the database
    1125   if ($data[2] =~ /^.[ne]$/) {
    1126     # Block that can be changed
    1127     my $blockoptions = "<select name=alloctype><option".
     1148    if ($data[2] =~ /^.[ne]$/) {
     1149      # Block that can be changed
     1150      my $blockoptions = "<select name=alloctype><option".
    11281151        (($data[2] eq 'me') ? ' selected' : '') ." value='me'>Dialup netblock</option>\n<option".
    11291152        (($data[2] eq 'de') ? ' selected' : '') ." value='de'>Dynamic DSL netblock</option>\n<option".
     
    11341157        (($data[2] eq 'in') ? ' selected' : '') ." value='in'>Internal netblock</option>\n".
    11351158        "</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;
    11371167  } 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/;
    11471200
    11481201  print $html;
     
    11541207# action=update
    11551208sub update {
    1156   printHeader('');
    11571209
    11581210  # Make sure incoming data is in correct format - custID among other things.
     
    12131265# Delete an allocation.
    12141266sub 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
    12161272  #show confirm screen.
    12171273  open HTML, "../confirmRemove.html"
     
    13031359# Remove IPs from pool listing if necessary
    13041360sub finalDelete {
    1305   printHeader('');
     1361  if ($IPDBacl{$authuser} !~ /d/) {
     1362    printError("You shouldn't have been able to get here.  Access denied.");
     1363    return;
     1364  }
    13061365
    13071366  my ($code,$msg) = deleteBlock($ip_dbh, $webvar{block}, $webvar{alloctype});
     
    13261385
    13271386
     1387sub 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
    13281399# Just in case we manage to get here.
    13291400exit 0;
Note: See TracChangeset for help on using the changeset viewer.