[LON-CAPA-cvs] cvs: loncom(version_1_0_3) /homework lonhomework.pm

albertel lon-capa-cvs@mail.lon-capa.org
Wed, 15 Oct 2003 22:18:27 -0000


albertel		Wed Oct 15 18:18:27 2003 EDT

  Modified files:              (Branch: version_1_0_3)
    /loncom/homework	lonhomework.pm 
  Log:
  - backport 1.152-1.155
  
  
Index: loncom/homework/lonhomework.pm
diff -u loncom/homework/lonhomework.pm:1.138 loncom/homework/lonhomework.pm:1.138.2.1
--- loncom/homework/lonhomework.pm:1.138	Tue Jul 22 15:55:26 2003
+++ loncom/homework/lonhomework.pm	Wed Oct 15 18:18:26 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # The LON-CAPA Homework handler
 #
-# $Id: lonhomework.pm,v 1.138 2003/07/22 19:55:26 albertel Exp $
+# $Id: lonhomework.pm,v 1.138.2.1 2003/10/15 22:18:26 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -146,6 +146,51 @@
 
 $Apache::lonxml::browse='';
 
+sub check_ip_acc {
+    my ($acc)=@_;
+    if (!defined($acc) || $acc =~ /^\s*$/) { return 1; }
+    my $allowed=0;
+    my $ip=$ENV{'REMOTE_ADDR'};
+    my $name;
+    foreach my $pattern (split(',',$acc)) {
+	if ($pattern =~ /\*$/) {
+	    #35.8.*
+	    $pattern=~s/\*//;
+	    if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
+	} elsif ($pattern =~ /(\d+\.\d+\.\d+)\.\[(\d+)-(\d+)\]$/) {    
+	    #35.8.3.[34-56]
+	    my $low=$2;
+	    my $high=$3;
+	    $pattern=$1;
+	    if ($ip =~ /^\Q$pattern\E/) { 
+		my $last=(split(/\./,$ip))[3];
+		if ($last <=$high && $last >=$low) { $allowed=1; }
+	    }
+	} elsif ($pattern =~ /^\*/) {
+	    #*.msu.edu
+	    $pattern=~s/\*//;
+	    if (!defined($name)) {
+		use Socket;
+		my $netaddr=inet_aton($ip);
+		($name)=gethostbyaddr($netaddr,AF_INET);
+		}
+	    if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
+	} elsif ($pattern =~ /\d+\.\d+\.\d+\.\d+/) {
+	    #127.0.0.1
+	    if ($ip =~ /^\Q$pattern\E/) { $allowed=1; }
+	} else {
+	    #some.name.com
+	    if (!defined($name)) {
+		use Socket;
+		my $netaddr=inet_aton($ip);
+		($name)=gethostbyaddr($netaddr,AF_INET);
+	    }
+	    if ($name =~ /\Q$pattern\E$/i) { $allowed=1; }
+	}
+	if ($allowed) { last; }
+    }
+    return $allowed;
+}
 # JB, 9/24/2002: Any changes in this function may require a change
 # in lonnavmaps::resource::getDateStatus.
 sub check_access {
@@ -167,6 +212,14 @@
 
   &Apache::lonxml::debug("checking for part :$id:");
   &Apache::lonxml::debug("time:".time);
+    
+    my $allowed=&check_ip_acc(&Apache::lonnet::EXT("resource.$id.acc"));
+    if (!$allowed) {
+	$status='INVALID_ACCESS';
+	$date=&mt("can not be accessed from your location.");
+	return($status,$date);
+    }
+
   foreach $temp ("opendate","duedate","answerdate") {
     $lastdate = $date;
     $date = &Apache::lonnet::EXT("resource.$id.$temp");