NetReg: banning machines in 1.3rc2

New Message Reply Date view Thread view Subject view Author view Attachment view

From: John Crowley (jcrowley@wolf.smith.edu)
Date: Tue Oct 14 2003 - 09:44:41 CDT


Someone just mentioned that Netreg 2 supports unregistering a system and
not allowing it to re-register. I recently built this into my netreg
1.3rc2 setup. In case anyone is interested I'll share it here.

I added a variable to the variables.pl called $RESTRICTED, which is the
flat file containing the restricted systems information. I have mine set
to /etc/netreg/restricted.

Then I added a little check in register.cgi, right before it checks to see
if the MAC address is already registered:

    open(RESTRICTED, "$RESTRICTED") or error("Could not open restricted file: $R
ESTRICTED");
    while(<RESTRICTED>) {
      if($_ =~ /$RECORDS{$IP}/)
      {
        error("This machine is restricted from the network. Please Contact the
USC for details.");
      }
    }#while(<RESTRICTED>)

The $RESTRICTED file has a format that looks like this:
00:03:93:9E:51:CD jcrowley 2003-10-14-10:02:19

The rest of the code is in admin.cgi. I added new functions:
restrict_access()
unrestrict_access()
show_restricted()

I also edited find_conf() and view_subnet() to add in the restrict gif,
plus print_header() to add in the Java for the "Do you really want to
restrict this person" pop up box.

I put 'jcrowley' at the beginning of areas I edited so I could keep track.
I also a while ago costumized some of the registration error messages for
our site (like our help desk number etc.) and didn't notate that.

I didn't know the font of the netreg menu, so instead of trying to add my
new menu on, I created a new, less pretty menu (netreg-menunew.gif). The
restriction gif is red, because I wanted it to stand out. I made a black
one to see how it looks, and I'll include that in my tar too. You can
download the gifs I created (nothing special) plus admin.cgi from:
netreg.smith.edu/~jcrowley/restrict.tar

I hope we rarely have to use this function, but it was fun to build.

Here is my complete admin.cgi:

#!/usr/bin/perl --
#
#----------------------------------------------------------------------------\
# NetReg 1.rc2
# Copyright (C) 1999-2002 Peter Valian, Southwestern University
#
# admin.cgi -- Registration Server Administrator
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Peter Valian <valianp@southwestern.edu>
# http://www.netreg.org/
#
# Code contributions by:
#
# Allie M. Tate <allie@lsu.edu>, Louisiana State University
# Lease Parser for use with dhcpd-3.0rc10
# Variable subnet masks
#
#----------------------------------------------------------------------------/

require "../variables.pl";
require "../subnet.pl";

#----------------------------------------------------------------------------\
# Main Function
#----------------------------------------------------------------------------\
get_input();
print_header();

# Need to modify IP for hook into IPAudit
# IPAudit likes IPs in the form of xxx.xxx.xxx.xxx
if ($FORM{'src'} eq "ipaudit") {
    @OCTETS = split /\./,$FORM{'query'};
    # Cast each string of numbers into an integer -- gotta love Perl ;)
    foreach $octet (@OCTETS) {
        $octet = $octet - 0;
    }
    $FORM{'query'} = join(".",@OCTETS);
}

if ($FORM{'action'} eq "SM") { server_manager(); }
elsif ($FORM{'action'} eq "FC") { find_conf(); }
elsif ($FORM{'action'} eq "FL") { find_lease(); }
elsif ($FORM{'action'} eq "SO") { subnet_overview(); }
elsif ($FORM{'action'} eq "VS") { view_subnet(); }
elsif ($FORM{'action'} eq "RA") { restrict_access(); }
elsif ($FORM{'action'} eq "UR") { unrestrict_access(); }
elsif ($FORM{'action'} eq "SR") { show_restricted(); }
elsif (!$FORM{'action'}) { subnet_overview(); }
print_footer();
#----------------------------------------------------------------------------/

#############################################################################\
## SUBROUTINES BEGIN
#############################################################################/

#----------------------------------------------------------------------------\
# Get the input from the CGI FORM
#----------------------------------------------------------------------------\
sub get_input {
    @pairs = split(/&/, $ENV{'QUERY_STRING'});
    foreach $pair (@pairs) {
        ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $FORM{$name} = $value;
    }
}
#----------------------------------------------------------------------------/

#----------------------------------------------------------------------------\
# Checks to see if we have a query, if not then call find_form.
# If we do have a query, grep the dhcpd conf for host entries that conatin
# the query string
#----------------------------------------------------------------------------\
sub find_conf {
    # Do we have a query, if not then print the Find Form
    if (!$FORM{'query'}) {
        find_form(FC);
    }
    # We have something to query for; look in dhcpd.conf for Query
    else {
        print "<FONT SIZE=5>Query: <B>$FORM{'query'}</B></FONT><P>\n";
        print "<img src=$GFX/small-netreg-lease.gif align=top> = Lease Info";
        print "<br><img src=$GFX/small-netreg-del.gif";
        print " align=top> = Delete User Entry";
        print "<br><img src=$GFX/small-resr.gif";
        print " align=top> = Restrict Network Access<P>";

        print "<table cellpadding=10><TR><td></td><TH>User</TH>";
        print "<TH>MAC Address</TH>";
        print "<TH>Platform</TH><TH>Registration Timestamp</TH></TR>";
        my $CNT=0;
        open (DHCPDCONF, "$DHCPDCONFPATH/$DHCPDCONFFILE") or
            error( "Cannot open DHCP conf file: $DHCPCONFPATH/$DHCPCONFFILE"),
            print_footer(),
            exit 0;
        my $color = get_color($color);
        while (<DHCPDCONF>) {
            if (/^host\s.*}\#/) {
              my ($TEMP1, $PLATFORM, $DATE, $IP) = split /\#/;
              my ($TRASH, $USER, $TRASH, $TRASH, $TRASH, $MAC) =
                  split /\s+|\#/,$TEMP1;
              ($USER) = split /-/,$USER;
              # Use anchors to match a username exactly if asked to, e.g. view_subnet()
              if ( $FORM{'exact'} ) {
                  $a1 = "\^";
                  $a2 = "\$";
              } else {
                  $a1 = '';
                  $a2 = '';
              }
              if ( ($PLATFORM =~ /$FORM{'query'}/i) ||
                    ($DATE =~ /$FORM{'query'}/i) ||
                    ($IP =~ /$FORM{'query'}/) ||
                    ($USER =~ /$a1$FORM{'query'}$a2/i) ||
                    ($MAC =~ /$FORM{'query'}/i)) {
                  print "<TR><TD ALIGN=CENTER><A HREF=$ADMINPATH/admin.cgi";
                  print "\?action=FL\&query=$MAC>";
                  print "<IMG SRC=$GFX/netreg-lease.gif border=0></a>";
                  print "<TD bgcolor=$color>";

                  ($QUERY) = split /-/,$USER;
                  if ($LDAP_URL) {
                      $LDAP_U = $LDAP_URL;
                      $LDAP_U =~ s/<USERNAME>/$QUERY/;
                      print "<A HREF=$LDAP_U target=blank>$QUERY</A>";
                  } else {
                      print $QUERY;
                  }

                  print "</TD>";
                  $USER =~ s/$FORM{'query'}/<b>$FORM{'query'}<\/b>/;
                  $MAC =~ s/$FORM{'query'}/<b>$FORM{'query'}<\/b>/;
                  $PLATFORM =~ s/$FORM{'query'}/<b>$FORM{'query'}<\/b>/;
                  $DATE =~ s/$FORM{'query'}/<b>$FORM{'query'}<\/b>/;
                  print "<TD bgcolor=$color>$MAC</TD><TD bgcolor=$color>$PLATFORM";
                  print "</TD><TD bgcolor=$color align=center>$DATE</TD><TD>";
                  $MAC =~ s/<b>//;
                  $MAC =~ s/<\/b>//;
                  $USER =~ s/<b>//;
                  $USER =~ s/<\/b>//;
                  print "<A HREF=\"$ADMINPATH/admin.cgi\?action=FL\&";
                  print "USER=$USER\&MAC=$MAC\"";
                  print " onClick=\"ConfirmDelete(\'$ADMINPATH/admin.cgi";
                  print "\?action=FL\&USER=$USER\&MAC=$MAC\',\'$MAC\',\'$USER\')\; ";
                  print "return false\;\">";
                  print "<IMG SRC=$GFX/netreg-del.gif border=0></A></TD>\n";
# jcrowley
                  print "<TD ALIGN=CENTER>";
                  print "<A HREF=\"$ADMINPATH/admin.cgi\?action=RA\&";
                  print "USER=$USER\&MAC=$MAC\"";
                  print " onClick=\"ConfirmRestrict(\'$ADMINPATH/admin.cgi";
                  print "\?action=RA\&USER=$USER\&MAC=$MAC\',\'$MAC\',\'$USER\')\; ";
                  print "return false\;\">";
                  print "<IMG SRC=$GFX/netreg-resr.gif border=0></A></TD></TR>\n";

                  $CNT++;
                  $color = get_color($color);
              }
            }
        }
        print "<p>Found: <b>$CNT</b> matches<p>\n";
        close(DHCPDCONF);
        print "</table>";
    }
}
#----------------------------------------------------------------------------/

#----------------------------------------------------------------------------\
# Search for Leases; Also used to search for and delete registrations
#----------------------------------------------------------------------------\
sub find_lease {
    # Two entries with the same MAC cannot exist in the dhcpd.conf

    if (($FORM{'USER'}) && ($FORM{'MAC'})) { # Delete entry
        open (DHCPDCONFNEW, "$DHCPDCONFPATH/$DHCPDCONFFILE.new") or
            error( "Cannot open new DHCP conf file: $DHCPCONFPATH/$DHCPCONFFILE.new" ),
            print_footer(),
            exit 0;
        while (<DHCPDCONFNEW>) {
            # copy everything in dhcpd.conf.new over to temp variable
            # except what we want to delete
            if ($_ !~ /$FORM{'MAC'}/) {
                $DHCPDCONFTMP .= $_;
            }
        }
        close(DHCPDCONFNEW);
        # now copy the dhcpd.conf.tmp file to dhcpd.conf.new
        # refresh-dhcpdconf will take care of the rest
        open (DHCPDCONFNEW, ">$DHCPDCONFPATH/$DHCPDCONFFILE.new") or
            error( "Cannot write new DHCP conf file: $DHCPCONFPATH/$DHCPCONFFILE.new" ),
            print_footer(),
            exit 0;
        print DHCPDCONFNEW $DHCPDCONFTMP;
        close (DHCPDCONFNEW);
        print "<FONT SIZE=5>Query: <B>Delete Entry</B></FONT>";
        print "<P>Operation Complete.";

        my ($SEC) = localtime;
        my $refresh = (abs($SEC-=60))+2;
        $SEC = (abs($SEC));
        print "<BR>Database refreshing in $SEC seconds...";
        # probably need more error checking; currently just
        # continues even if dhcpd restart fails.
        print "<META HTTP-EQUIV=\"refresh\" content=\"$refresh\;";
        print "URL=$ADMINPATH/admin.cgi\">";
    }
    # if we don't have query data, print the Find Form
    elsif (!$FORM{'query'}) {
        find_form(FL);
    }
    # we must have data to query for in dhcpd.leases
    # will produce all leases for a host, MAC or IP (or timestamps)
    else {
        open (LEASES, "$LEASESPATH/$LEASESFILE") or
            error( "Cannot open leases file: $LEASESPATH/$LEASESFILE" ),
            print_footer(),
            exit 0;
        $/ = "}\n";
        my $LTOTAL = 0;
        while (<LEASES>) {
            if (/lease ([0-9\.]+).*starts \d+ ([0-9\/: ]+);.*ends \d+ ([0-9\/: ]+);.* binding state (\w+).*hardware ethernet ([0-9A-Fa-f:]+);/s) {
                $testquery = join '#', $1,$5,$2,$3,$4;
                $lease_IP = $1;
                if ($testquery =~ /$FORM{'query'}/i) {
                    $key = sprintf( "%d\.%03d\.%03d\.%03d", split(/\./, $lease_IP) );
                    $lease_info{$key} = $testquery;
                }
            }
            $LTOTAL++;
        }
        close (LEASES);
        $/ = "\n";

        print "<FONT SIZE=5>Query: <B>$FORM{'query'}</B></FONT><P>";
        print "<table cellpadding=3 border=0>\n";
        print "<tr><td><img src=$GFX/small-netreg-user.gif align=top></td><td>= User Info</td></tr><br>\n";
        print "<tr><td><img src=$GFX/reddot.gif align=top></td><td>= Active Lease</td></tr></table>";
        print "<P><TABLE CELLPADDING=10>";
        print "<TR><TH></TH><TH>MAC Address</TH><TH>IP Address</TH>";
        print "<TH>Obtained (GMT)</TH><TH>Expires (GMT)</TH></TR>\n";

        my $CNT = 0;
        my $ACNT = 0;
        @sortedIP = sort keys(%lease_info);
        foreach $key (@sortedIP) {
            ($IP,$MAC,$TIMESTART,$TIMEEND,$BINDSTATE) = split /#/, $lease_info{$key};
            $MAC=uc($MAC);
            $color = get_color($color);
            print "<tr><td align=center><A HREF=$ADMINPATH/admin.cgi";
            print "\?action=FC\&query=$MAC>";
            print "<img src=/gfx/netreg-user.gif border=0></a></td>";
            $MAC =~ s/$FORM{'query'}/<b>$FORM{'query'}<\/b>/g;
            $IP =~ s/$FORM{'query'}/<b>$FORM{'query'}<\/b>/g;
            $TIMESTART =~ s/$FORM{'query'}/<b>$FORM{'query'}<\/b>/g;
            $TIMEEND =~ s/$FORM{'query'}/<b>$FORM{'query'}<\/b>/g;
            print "<td bgcolor=$color>$MAC</td><td bgcolor=$color>$IP ";
            if ($BINDSTATE eq "active") {
                print "<img src=$GFX/reddot.gif border=0>";
            }
            print "</td>";
            print "<td bgcolor=$color>$TIMESTART</td>";
            print "<td bgcolor=$color>$TIMEEND</td></tr>\n";
            $CNT++;
            if ($BINDSTATE eq "active") {
                $ACNT++;
            }
        }
        print "<p>Found/Active: <b>$CNT</b> / <b>$ACNT</b> matches\n";
        print "<br>Total Leases: <b>$LTOTAL</b><p>\n";
        print "</TABLE>";
    }
}
#----------------------------------------------------------------------------/

#----------------------------------------------------------------------------\
# Simple search form called by FL, FC
#----------------------------------------------------------------------------\
sub find_form {
    my $action = shift(@_);
    if ($action eq "FL") {
        print "<font size=4>Search Leases</font><br>";
        print "<font size=2>(IP, MAC, Lease Start, Lease End)</font><P>";
    }
    else {
        print "<font size=4>Search Registration Database</font><br>";
        print "<font size=2>(User, MAC, Platform, Registration Timestamp)";
        print "</font><P>";
    }
    print "<form method=get action=$ADMINPATH/admin.cgi>";
    print "<input type=hidden name=action value=$action>";
    print "<input type=text name=query><P>";
    print "<input type=submit>";
}
#----------------------------------------------------------------------------/

#----------------------------------------------------------------------------\
# grep for the servers' running PID and Time Start
# Note, PID and Time Start for HTTPD doesn't really make sense as
# HTTPD launches several children...the first one is listed, thats enough to
# know the server is up.
#----------------------------------------------------------------------------\
sub server_manager {
    print "<font size=5>Server Manager</font><P>";
    $UPTIME = `uptime`;
    print "$UPTIME<P>";

    my %server = ("1:dhcpd","DHCP", "2:named","DNS", "3:httpd","HTTP" );
    my @list;
    my $pid;
    my $start;
    my $status;
    my $color;

    print "<table width=360 border=0 cellspacing=0 cellpadding=2><tr>";
    print "<th bgcolor=black>\n";
    print "<table width=358 cellpadding=5 cellspacing=0 align=center border=0>";
    print "<tr>\n";
    print "<th bgcolor=\#373799><b><font color=white size=4>Server</font></b></th>";
    print "<th bgcolor=\#373799><b><font color=white size=4>Status</font></b></th>";
    print "<th bgcolor=\#373799><b><font color=white size=4>PID</font></b></th>";
    print "<th bgcolor=\#373799><b><font color=white size=4>Time Started</font></b></th></tr>";

    foreach $proc (sort keys %server) {
        ($order,$procname) = split /:/, $proc;
        $pso = `ps auxw|grep $procname|grep -v grep`;
        $pso =~ s/\s+/ /g;
        @list = split / /, $pso;
        $pid = $list[1];
        $start = $list[8];
        if ($start =~ /^[a-zA-Z]+$/) {
            $start .= " $list[9]";
        }

        if ($pid ne "") {
            $status = "UP";
            $color = "green";
        } else {
            $status = "DOWN";
            $color = "red";
        }

        print "<tr bgcolor=white><td>$server{$proc}</td>";
        print "<td align=center><b><font color=$color>$status</font></b></td>";
        print "<td align=center>$pid</td><td align=center>$start</td></tr>\n";
    }
    print "</table></th></tr></table>\n";

}
#----------------------------------------------------------------------------/

#----------------------------------------------------------------------------\
# grep the dhcpd.conf for the specified subnet
# NOTE: not selecting a subnet will print all the subnets
#----------------------------------------------------------------------------\
sub view_subnet {
    parse_subnets();

    print "<font size=5>Subnet Overview: <b>$FORM{'subnet'}</b><P>";
    print "<img src=$GFX/small-netreg-lease.gif align=top> = Lease Info";
    print "<br><img src=$GFX/small-netreg-user.gif align=top> = User Info";
    print "<br><img src=$GFX/small-netreg-del.gif";
    print " align=top> = Delete User Entry";
    print "<br><img src=$GFX/small-resr.gif";
    print " align=top> = Restrict Network Access<P>";
    print "<table cellpadding=10><TR><td></td><TH>User</TH><TH>MAC Address";
    print "</TH><TH>Platform</TH><TH>Registration Timestamp</TH>";
    print "<TH></TH></TR>";
    open (DHCPDCONF, "$DHCPDCONFPATH/dhcpd.conf") or
      error( "Cannot open DHCP conf file: $DHCPCONFPATH/$DHCPCONFFILE"),
      print_footer(),
      exit 0;
    my @entries;
    while (<DHCPDCONF>) {
        if (/^host\s.*\#(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {
            if (ip_in_subnet($1,$URSUBS{$FORM{'subnet'}})) {
                ($DATA1, $PLATFORM, $DATE, $IP) = split /\#/;
                ($TRASH,$USER,$TRASH,$TRASH,$TRASH,$MAC) = split /\s+|\#/,$DATA1;
                ($QUERY) = split /-/,$USER;
                push(@entries,join('#',($QUERY,$MAC,$PLATFORM,$DATE,$IP)));
            }
        }
    }

    @entries = sort( @entries );
    my $i;
    for ($i=0; $i<=$#entries; $i++) {
        $color = get_color($color);
        ($USER,$MAC,$PLATFORM,$DATE,$IP) = split( '#', $entries[$i] );
        print "<TR><TD ALIGN=CENTER><A HREF=$ADMINPATH/admin.cgi";
        print "\?action=FL\&query=$MAC>";
        print "<IMG SRC=$GFX/netreg-lease.gif border=0 hspace=2></a>";
        #Ask for an exact match to avoid similar usernames
        print "<A HREF=$ADMINPATH/admin.cgi\?action=FC\&query=$USER&exact=1>";
        print "<IMG SRC=$GFX/netreg-user.gif border=0></A></TD>";
        print "<TD bgcolor=$color>";
        if ($LDAP_URL) {
            $LDAP_U = $LDAP_URL;
            $LDAP_U =~ s/<USERNAME>/$USER/;
            print "<a href=$LDAP_U target=blank>$USER</a>";
        } else {
            print $USER;
        }
        print "</TD><TD bgcolor=$color>$MAC</TD>";
        print "<TD bgcolor=$color>$PLATFORM</TD>";
        print "<TD bgcolor=$color align=center>$DATE";
        print "<br>$TIME</TD><TD ALIGN=CENTER>";
        $MAC =~ s/<b>//;
        $MAC =~ s/<\/b>//;
        $USER =~ s/<b>//;
        $USER =~ s/<\/b>//;
        print "<A HREF=\"$ADMINPATH/admin.cgi\?action=FL\&";
        print "USER=$USER\&MAC=$MAC\"";
        print " onClick=\"ConfirmDelete(\'$ADMINPATH/admin.cgi";
        print "\?action=FL\&USER=$USER\&MAC=$MAC\',\'$MAC\',\'$USER\')\; ";
        print "return false\;\">";
        print "<IMG SRC=$GFX/netreg-del.gif border=0></A></TD>";
# jcrowley
        print "<TD ALIGN=CENTER>";
        print "<A HREF=\"$ADMINPATH/admin.cgi\?action=RA\&";
        print "USER=$USER\&MAC=$MAC\"";
        print " onClick=\"ConfirmRestrict(\'$ADMINPATH/admin.cgi";
        print "\?action=RA\&USER=$USER\&MAC=$MAC\',\'$MAC\',\'$USER\')\; ";
        print "return false\;\">";
        print "<IMG SRC=$GFX/netreg-resr.gif border=0></A></TD></TR>\n";

    }
    print "</table>";
}
#----------------------------------------------------------------------------/

#----------------------------------------------------------------------------/
# Adds a MAC address restriction to netreg.
# Includes the username and date stamp in the file.
# It also removes the systems entry from the dhcpd.conf file in the same
# manor as find_lease does.
# jcrowley
#----------------------------------------------------------------------------/

sub restrict_access {

    $MAC = $FORM{'MAC'};
    $USER= $FORM{'USER'};
    $TIME= `/bin/date +%Y-%m-%d-%H:%M:%S`;
    my $refresh = 5;

    if (($FORM{'USER'}) && ($FORM{'MAC'})) {

        open(RESTRICTED,"$RESTRICTED") or
         error("Cannon open file: $RESTRICTED"),
         print_footer(),
         exit 0;

        while(<RESTRICTED>){
            if($_ =~ /$MAC/) {
                error("MAC address is already restricted.\n");
                exit 0;
            }
        }
        close RESTRICTED;

        open(RESTRICTED,">>$RESTRICTED") or
         error("Cannon open file: $RESTRICTED"),
         print_footer(),
         exit 0;

        print RESTRICTED "$MAC $USER $TIME";

        open (DHCPDCONFNEW, "$DHCPDCONFPATH/$DHCPDCONFFILE.new") or
            error( "Cannot open new DHCP conf file: $DHCPCONFPATH/$DHCPCONFFILE.new" ),
            print_footer(),
            exit 0;
        while (<DHCPDCONFNEW>) {
            # copy everything in dhcpd.conf.new over to temp variable
            # except what we want to delete
            if ($_ !~ /$FORM{'MAC'}/) {
                $DHCPDCONFTMP .= $_;
            }
        }
        close(DHCPDCONFNEW);
        # now copy the dhcpd.conf.tmp file to dhcpd.conf.new
        # refresh-dhcpdconf will take care of the rest
        open (DHCPDCONFNEW, ">$DHCPDCONFPATH/$DHCPDCONFFILE.new") or
            error( "Cannot write new DHCP conf file: $DHCPCONFPATH/$DHCPCONFFILE.new" ),
            print_footer(),
            exit 0;
        print DHCPDCONFNEW $DHCPDCONFTMP;
        close DHCPDCONFNEW;

        print "<FONT SIZE=5>Query: <B>MAC Address Restricted</B></FONT>";
        print "<P>Operation Complete.";
        print "<META HTTP-EQUIV=\"refresh\" content=\"$refresh\;";
        print "URL=$ADMINPATH/admin.cgi\?action=SR\">";
        print_footer();

        close RESTRICTED;

    }
    

}
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------/
# Removes an entry from the restricted list.
# jcrowley
#----------------------------------------------------------------------------/

sub unrestrict_access {

    $MAC = $FORM{'MAC'};
    $found = 0;
    my $refresh = 5;

    open(RESTRICTED, "$RESTRICTED") or
     error("Cannon open file: $RESTRICTED"),
     print_footer(),
     exit 0;

    # Read into RESTRICTTMP everything but the line with the MAC Address
    while(<RESTRICTED>){
       if($_ !~ /$MAC/) {
           $RESTRICTTMP .= $_;
       }
       if($_ =~ /$MAC/){
           $found = 1;
       }
    }#while(<RESTRICTED>)
    
    close RESTRICTED;

    if($found){
        open(RESTRICTED, ">$RESTRICTED") or
         error("Cannon open file: $RESTRICTED"),
         print_footer(),
         exit 0;
        print RESTRICTED $RESTRICTTMP;
        close RESTRICTED;

        print "<FONT SIZE=5>MAC: <B>$MAC Unrestricted</B></FONT>";
        print "<P>Operation Complete.";
        print "<META HTTP-EQUIV=\"refresh\" content=\"$refresh\;";
        print "URL=$ADMINPATH/admin.cgi\?action=SR\">";
        print_footer();
    }
    else{
        print "<FONT SIZE=5>MAC: <B>$MAC not found in the restricted list</B></FONT>";
        print_footer();
    }

}

#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# Display Restricted MAC addresses (with user name, and time stamp of
# restriction. Includes button for lifting restriction
# jcrowley
#----------------------------------------------------------------------------

sub show_restricted {

    print "<font size=5>Restricted Access List: <b>$FORM{'subnet'}</b><P>";
    print "<img src=$GFX/small-netreg-lease.gif align=top> = Lease Info";
    print "<br><img src=$GFX/small-netreg-user.gif align=top> = User Info";
    print "<br><img src=$GFX/small-netreg-del.gif";
    print " align=top> = Unrestrict Access<P>";

    open(RESTRICTED, "$RESTRICTED") or
     error("Cannon open file: $RESTRICTED"),
     print_footer(),
     exit 0;

    print "<table cellpadding=10><TR><td></td><TH>User</TH><TH>MAC Address";
    print "</TH><TH>Date</TH><TH></TH></TR>";

    while(<RESTRICTED>){

        $color = get_color($color);
        ($MAC, $USER, $TIME) = split("\t",$_);

        print "<TR><TD ALIGN=CENTER><A HREF=$ADMINPATH/admin.cgi";
        print "\?action=FL\&query=$MAC>";
        print "<IMG SRC=$GFX/netreg-lease.gif border=0 hspace=2></a>";
        print "<A HREF=$ADMINPATH/admin.cgi\?action=FC\&query=$USER&exact=1>";
        print "<IMG SRC=$GFX/netreg-user.gif border=0></A></TD>";
        print "<TD bgcolor=$color>";
        print "$USER";
        print "</TD><TD bgcolor=$color>$MAC</TD>";
        print "<TD bgcolor=$color align=center>$TIME</TD>";

        print "<TD ALIGN=CENTER>";
        print "<A HREF=\"$ADMINPATH/admin.cgi\?action=UR\&";
        print "USER=$USER\&MAC=$MAC\"";
        print " onClick=\"ConfirmUnrestrict(\'$ADMINPATH/admin.cgi";
        print "\?action=UR\&USER=$USER\&MAC=$MAC\',\'$MAC\',\'$USER\')\; ";
        print "return false\;\">";
        print "<IMG SRC=$GFX/netreg-del.gif border=0></A></TD></TR>\n";

    }#while(<RESTRICTED>)
    close RESTRICTED;

    print "</table>";

    print_footer();

}

#----------------------------------------------------------------------------

#----------------------------------------------------------------------------\
# gets a line count of how many clients on each subnet
# prints out a table listing from subnet.dat of all the
# subnets and host entries in dhcpd.conf
#----------------------------------------------------------------------------\
sub subnet_overview {
    my $TOTAL = 0;
    my $ATOTAL = 0;
    my $UTOTAL = 0;
    my %reg_cnt;
    my %active_cnt;
    my %reg_ip;
    my %ureg_ip;
    parse_subnets();
    foreach $SUBNET (keys %LOCS) {
        $active_cnt{$SUBNET} = 0;
        $reg_cnt{$SUBNET} = 0;
    }

    print "<table border=0><tr><Th></th><TH align=center>";
    print "<font size=5>Subnet Overview</font>";

    open (LEASES, "$LEASESPATH/$LEASESFILE") or
        error( "Cannot open leases file: $LEASESPATH/$LEASESFILE" ),
        print_footer(),
        exit 0;
    $/ = "}\n";
    while (<LEASES>) {
        if (/lease ([0-9\.]+).* binding state active/s) {
            $lease_IP = $1;
            foreach $subnet (keys %LOCS) {
                if (ip_in_subnet($lease_IP,$URSUBS{$subnet})) {
                    $ureg_ip{$subnet}{$lease_IP} = 1;
                    last;
                } elsif (ip_in_subnet($lease_IP,$subnet)) {
                    $reg_ip{$subnet}{$lease_IP} = 1;
                    last;
                }
            }
        }
    }
    close (LEASES);
    $/ = "\n";

    my $cnt = 0;
    foreach $subnet (keys %LOCS) {
        @list = (keys %{$ureg_ip{$subnet}});
        $cnt = $#list + 1;
        $UTOTAL += $cnt;

        @list = (keys %{$reg_ip{$subnet}});
        $cnt = $#list + 1;
        $ATOTAL += $cnt;
        $active_cnt{$subnet} = $cnt;
    }

    open(DHCPDCONF, "$DHCPDCONFPATH/dhcpd.conf") or
      error( "Cannot open DHCP conf file: $DHCPCONFPATH/$DHCPCONFFILE"),
      print_footer(),
      exit 0;
    @CONFILE = <DHCPDCONF>;
    close (DHCPDCONF);

    foreach $LINE (@CONFILE) {
        if ($LINE =~ /^host\s.*\#(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {
            $plain_IP = $1;
            foreach $subnet (keys %LOCS) {
                if (ip_in_subnet($plain_IP,$URSUBS{$subnet})) {
                    $reg_cnt{$subnet}++;
                    last;
                }
            }
            $TOTAL++;
        }
    }

    print "<p><table>";
    print "<tr><td colspan=\"2\"><b><font size=4>Current DHCP Clients</font></b></td></tr>";
    print "<tr><td>Active Unregistered</td><td align=right>$UTOTAL</td></tr>";
    print "<tr><td>Active Registered</td><td align=right>$ATOTAL</td></tr>";
    print "<tr><td>Total Registered</td><td align=right>$TOTAL</td></tr></table></p>\n";
    print "Active / Registered Clients";
    print "</th><th></th>\n";

    foreach $SUBNET(sort { $a <=> $b; } keys %LOCS) {
        print "<tr><td align=right>";
        print "<a href=$ADMINPATH/admin.cgi?action=VS&subnet=$SUBNET>";
        print "$SUBNET</a></td>";
        print "<td width=350 bgcolor=\#DCDCEE>";
        if ($reg_cnt{$SUBNET}) {
            $barwidth = int (($reg_cnt{$SUBNET} * 300)/$SUBIPS{$SUBNET}) + 1;
            if ($barwidth > 300) {
                $barwidth = 300;
            }
        } else {
            $barwidth = 0;
        }
        if ($active_cnt{$SUBNET}) {
            $redwidth = int (($active_cnt{$SUBNET} * 300)/$SUBIPS{$SUBNET}) + 1;
            if ($redwidth > 300) {
                $redwidth = 300;
            }
        } else {
            $redwidth = 0;
        }
        $barwidth2 = $barwidth - $redwidth;
        if ($redwidth) {
            print "<img src=$GFX/reddot.gif height=5 width=$redwidth border=1 hspace=0>";
        }
        if ($barwidth2 > 0) {
            print "<img lowsrc=$GFX/meter.gif src=$GFX/meter.jpg height=5 width=$barwidth2 border=1 hspace=0>";
        }
        print " $active_cnt{$SUBNET}/$reg_cnt{$SUBNET}</td><td><a ";
        print "href=$ADMINPATH/admin.cgi?action=VS&subnet=$SUBNET>$LOCS{$SUBNET}</a>";
        print "</td></tr>\n";
    }
    print "</table></form>";
}

#----------------------------------------------------------------------------/

#----------------------------------------------------------------------------\
# Alternate colors for table rows
#----------------------------------------------------------------------------\
  sub get_color {
    my $color = shift(@_);
    if ($color eq "A9A9D4") { $color = "FFD1D1"; }
    else { $color = "A9A9D4"; }
    return ($color);
  }
#----------------------------------------------------------------------------/

#----------------------------------------------------------------------------\
# Print out the error message
#----------------------------------------------------------------------------\
sub error {
    my $ERROR_MSG = shift(@_);
    my $ERROR_TYP = shift(@_);
    if ($ERROR_TYP eq '') {
        $ERROR_TYP = "Script";
    }
    print "<TABLE WIDTH=350 border=0 cellspacing=0 cellpadding=1><TR>";
    print "<TH BGCOLOR=black><table width=348 cellpadding=5";
    print "cellspacing=0 align=center><TR><TH bgcolor=black>";
    print "<font color=white size=5><b>$ERROR_TYP Error</b></FONT></th>";
    print "</tr><tr><th bgcolor=\#DDD7FF>$ERROR_MSG</center><P>";
    print "</td></tr></table></th></tr></table>";
}

#----------------------------------------------------------------------------\
# Print HTML Header
#----------------------------------------------------------------------------\
  sub print_header {
    print "content-type: text/html\n\n";
    print "<HTML><HEAD><TITLE>NetReg</TITLE>";
    print "<META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"no-cache\">";
    print "<Script Language=\"JavaScript\"><\!--\n";
    print "function ConfirmDelete(URL,MAC,USER) {\n";
    print "answer=confirm(\"Are you sure you want to delete \"+MAC+\" ";
    print "which is registered to \"+USER+\"?\")\n";
    print "if(answer !=0) { location = URL; } }\n";
    print "// --></script>\n";
#jcrowley
    print "<Script Language=\"JavaScript\"><\!--\n";
    print "function ConfirmRestrict(URL,MAC,USER) {\n";
    print "answer=confirm(\"Are you sure you want to restrict \"+MAC+\" ";
    print "which is registered to \"+USER+\" from the network?\")\n";
    print "if(answer !=0) { location = URL; } }\n";
    print "// --></script>\n";
    print "<Script Language=\"JavaScript\"><\!--\n";
    print "function ConfirmUnrestrict(URL,MAC,USER) {\n";
    print "answer=confirm(\"Are you sure you want to allow \"+MAC+\" ";
    print "which is registered to \"+USER+\" back onto the network?\")\n";
    print "if(answer !=0) { location = URL; } }\n";
    print "// --></script></HEAD>\n";

    print "<BODY BGCOLOR=WHITE><CENTER>";
    print "<IMG SRC=$GFX/netreg-top-logo.gif><BR>";
    print "<IMG SRC=$GFX/netreg-menunew.gif USEMAP=\#menu BORDER=0><P>";
  }
#----------------------------------------------------------------------------/

#----------------------------------------------------------------------------\
# Print HTML Footer
#----------------------------------------------------------------------------\
  sub print_footer {
    print "<P><img src=$GFX/netreg-menunew.gif usemap=\#menu border=0>";
    print "<map name=menu>";
    print "<area shape=rect coords=0,0,130,13 ";
    print "href=$ADMINPATH/admin.cgi\?action=SO>";
    print "<area shape=rect coords=130,0,280,13 ";
    print "href=$ADMINPATH/admin.cgi\?action=FC>";
    print "<area shape=rect coords=280,0,400,13 ";
    print "href=$ADMINPATH/admin.cgi\?action=FL>";
    print "<area shape=rect coords=400,0,545,13 ";
    print "href=$ADMINPATH/admin.cgi\?action=SM>";
    print "<area shape=rect coords=545,0,645,13 ";
    print "href=$ADMINPATH/admin.cgi\?action=SR>";
    print "<AREA SHAPE=DEFAULT NOHREF></map><BR>";
    print "<IMG SRC=$GFX/netreg-bottom-logo.gif><BR><FONT SIZE=2>";
    print "<a href=\"http://www.netreg.org/\">NetReg";
    print " v1.3</a> &copy;2002 Southwestern University</FONT>";
    print "</BODY></HTML>";
    exit;
  }
#----------------------------------------------------------------------------/

**********************************************************************
To unsubscribe from this list, send an e-mail message to
majordomo@southwestern.edu containing a single line with the words:
unsubscribe netreg
Send requests for assistance to: owner-netreg@southwestern.edu
**********************************************************************


New Message Reply Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.4 : Thu Aug 12 2004 - 12:01:41 CDT