[LON-CAPA-cvs] cvs: loncom /interface loncommon.pm

raeburn lon-capa-cvs-allow@mail.lon-capa.org
Mon, 08 Sep 2008 19:39:29 -0000


raeburn		Mon Sep  8 15:39:29 2008 EDT

  Modified files:              
    /loncom/interface	loncommon.pm 
  Log:
  Bug 5777. Forgot to commit this with lonhomework.pm rev 1.297. 
  - check_ip_acc() moved from lonhomework.pm to loncommon.pm to be more generally available
  
  
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.681 loncom/interface/loncommon.pm:1.682
--- loncom/interface/loncommon.pm:1.681	Fri Sep  5 15:20:39 2008
+++ loncom/interface/loncommon.pm	Mon Sep  8 15:39:26 2008
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.681 2008/09/05 19:20:39 riegler Exp $
+# $Id: loncommon.pm,v 1.682 2008/09/08 19:39:26 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -3747,6 +3747,60 @@
 
 ###############################################
 
+sub check_ip_acc {
+    my ($acc)=@_;
+    &Apache::lonxml::debug("acc is $acc");
+    if (!defined($acc) || $acc =~ /^\s*$/ || $acc =~/^\s*no\s*$/i) {
+        return 1;
+    }
+    my $allowed=0;
+    my $ip=$env{'request.host'} || $ENV{'REMOTE_ADDR'};
+
+    my $name;
+    foreach my $pattern (split(',',$acc)) {
+        $pattern =~ s/^\s*//;
+        $pattern =~ s/\s*$//;
+        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;
+}
+
+###############################################
+
 =pod
 
 =head1 Domain Template Functions