KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > acl > IpAddressAccessControl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.auth.acl;
18
19 import java.net.InetAddress JavaDoc;
20
21 import org.alfresco.filesys.server.SrvSession;
22 import org.alfresco.filesys.server.core.SharedDevice;
23 import org.alfresco.filesys.util.IPAddress;
24
25 /**
26  * Ip Address Access Control Class
27  * <p>
28  * Allow/disallow access by checking for a particular TCP/IP address or checking that the address is
29  * within a specified subnet.
30  */

31 public class IpAddressAccessControl extends AccessControl
32 {
33
34     // Subnet and network mask if the address specifies the subnet
35

36     private String JavaDoc m_subnet;
37     private String JavaDoc m_netMask;
38
39     /**
40      * Class constructor
41      *
42      * @param address String
43      * @param mask String
44      * @param type String
45      * @param access int
46      */

47     protected IpAddressAccessControl(String JavaDoc address, String JavaDoc mask, String JavaDoc type, int access)
48     {
49         super(address, type, access);
50
51         // Save the subnet and network mask, if specified
52

53         m_subnet = address;
54         m_netMask = mask;
55
56         // Change the rule name if a network mask has been specified
57

58         if (m_netMask != null)
59             setName(m_subnet + "/" + m_netMask);
60     }
61
62     /**
63      * Check if the TCP/IP address matches the specifed address or is within the subnet.
64      *
65      * @param sess SrvSession
66      * @param share SharedDevice
67      * @param mgr AccessControlManager
68      * @return int
69      */

70     public int allowsAccess(SrvSession sess, SharedDevice share, AccessControlManager mgr)
71     {
72
73         // Check if the remote address is set for the session
74

75         InetAddress JavaDoc remoteAddr = sess.getRemoteAddress();
76
77         if (remoteAddr == null)
78             return Default;
79
80         // Get the remote address as a numeric IP address string
81

82         String JavaDoc ipAddr = remoteAddr.getHostAddress();
83
84         // Check if the access control is a single TCP/IP address check
85

86         int sts = Default;
87
88         if (m_netMask == null)
89         {
90
91             // Check if the TCP/IP address matches the check address
92

93             if (IPAddress.parseNumericAddress(ipAddr) == IPAddress.parseNumericAddress(getName()))
94                 sts = getAccess();
95         }
96         else
97         {
98
99             // Check if the address is within the subnet range
100

101             if (IPAddress.isInSubnet(ipAddr, m_subnet, m_netMask) == true)
102                 sts = getAccess();
103         }
104
105         // Return the access status
106

107         return sts;
108     }
109 }
110
Popular Tags