001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.server.security;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.InetNetwork;
033: import com.caucho.util.L10N;
034: import com.caucho.util.LongKeyMap;
035:
036: import javax.annotation.PostConstruct;
037: import java.net.InetAddress;
038: import java.util.ArrayList;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: /**
043: * A class to forbid hosts by IP.
044: */
045: public class ForbidHost {
046: static final protected Logger log = Log.open(ForbidHost.class);
047: static final L10N L = new L10N(ForbidHost.class);
048:
049: private LongKeyMap _forbiddenHosts;
050: private ArrayList _forbiddenNets;
051:
052: /**
053: * Adds a forbidden host.
054: */
055: public void addForbidIP(String addrName) {
056: try {
057: InetAddress addr = InetAddress.getByName(addrName);
058:
059: if (_forbiddenHosts == null)
060: _forbiddenHosts = new LongKeyMap();
061:
062: _forbiddenHosts.put(inetAddressToLong(addr), "true");
063: } catch (Exception e) {
064: log.log(Level.FINE, e.toString(), e);
065: }
066: }
067:
068: /**
069: * Removes a forbidden host.
070: */
071: public void removeForbidIP(String addrName) {
072: try {
073: InetAddress addr = InetAddress.getByName(addrName);
074:
075: if (_forbiddenHosts != null)
076: _forbiddenHosts.remove(inetAddressToLong(addr));
077: } catch (Exception e) {
078: log.log(Level.FINE, e.toString(), e);
079: }
080: }
081:
082: /**
083: * Adds a forbidden net.
084: */
085: public void addForbidNet(String netmask) {
086: try {
087: InetNetwork net = InetNetwork.create(netmask);
088:
089: if (net == null)
090: return;
091:
092: if (_forbiddenNets == null)
093: _forbiddenNets = new ArrayList();
094:
095: _forbiddenNets.add(net);
096: } catch (Exception e) {
097: log.log(Level.FINE, e.toString(), e);
098: }
099: }
100:
101: /**
102: * Removes a forbidden net.
103: */
104: public void removeForbidNet(String netmask) {
105: try {
106: InetNetwork net = InetNetwork.create(netmask);
107:
108: if (net == null)
109: return;
110:
111: if (_forbiddenNets != null)
112: _forbiddenNets.remove(net);
113: } catch (Exception e) {
114: log.log(Level.FINE, e.toString(), e);
115: }
116: }
117:
118: /**
119: * Initialize the forbidden host.
120: */
121: @PostConstruct
122: public void init() {
123: }
124:
125: /**
126: * Returns true if the host is forbidden.
127: */
128: public boolean isForbidden(long addr) {
129: if (_forbiddenHosts != null) {
130: if (_forbiddenHosts.get(addr) != null)
131: return true;
132: }
133:
134: if (_forbiddenNets != null) {
135: for (int i = _forbiddenNets.size(); i >= 0; i--) {
136: InetNetwork net = (InetNetwork) _forbiddenNets.get(i);
137:
138: if (net.isMatch(addr))
139: return true;
140: }
141: }
142:
143: return false;
144: }
145:
146: /**
147: * Returns true if the host is forbidden.
148: */
149: public boolean isForbidden(InetAddress addr) {
150: if (_forbiddenHosts == null && _forbiddenNets == null)
151: return false;
152:
153: long ip = inetAddressToLong(addr);
154: if (_forbiddenHosts != null) {
155: if (_forbiddenHosts.get(ip) != null)
156: return true;
157: }
158:
159: if (_forbiddenNets != null) {
160: for (int i = _forbiddenNets.size(); i >= 0; i--) {
161: InetNetwork net = (InetNetwork) _forbiddenNets.get(i);
162:
163: if (net.isMatch(ip))
164: return true;
165: }
166: }
167:
168: return false;
169: }
170:
171: private static long inetAddressToLong(InetAddress addr) {
172: byte[] bytes = addr.getAddress();
173:
174: long address = 0;
175: for (int i = 0; i < bytes.length; i++)
176: address = 256 * address + (bytes[i] & 0xff);
177:
178: return address;
179: }
180: }
|