1 28 29 package com.caucho.server.security; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.log.Log; 33 import com.caucho.util.InetNetwork; 34 import com.caucho.util.L10N; 35 import com.caucho.util.LruCache; 36 37 import javax.annotation.PostConstruct; 38 import javax.servlet.ServletContext ; 39 import javax.servlet.ServletException ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import java.io.IOException ; 43 import java.util.ArrayList ; 44 import java.util.logging.Logger ; 45 46 77 public class IPConstraint extends AbstractConstraint { 78 79 80 static final Logger log = Log.open(IPConstraint.class); 81 static L10N L = new L10N(IPConstraint.class); 82 83 private ArrayList <InetNetwork> _allowNetworkList; 84 private ArrayList <InetNetwork> _denyNetworkList; 85 86 private int _cacheSize = 256; 87 private int _errorCode = HttpServletResponse.SC_FORBIDDEN; 88 private String _errorMessage = L.l("Forbidden IP Address"); 89 90 private LruCache<String ,Boolean > _cache; 91 92 93 private boolean _oldStyle = false; 94 95 public IPConstraint() 96 { 97 } 98 99 102 public void setErrorCode(int errorCode) 103 { 104 _errorCode = errorCode; 105 } 106 107 110 public int getErrorCode() 111 { 112 return _errorCode; 113 } 114 115 119 public void setErrorMessage(String errorMessage) 120 { 121 _errorMessage = errorMessage; 122 } 123 124 128 public String getErrorMessage() 129 { 130 return _errorMessage; 131 } 132 133 142 public void setCacheSize(int cacheSize) 143 { 144 _cacheSize = cacheSize; 145 } 146 147 151 public int getCacheSize() 152 { 153 return _cacheSize; 154 } 155 156 160 public void addAllow(String network) 161 { 162 if (_allowNetworkList == null) 163 _allowNetworkList = new ArrayList <InetNetwork>(); 164 165 _allowNetworkList.add(InetNetwork.create(network)); 166 } 167 168 171 public void addDeny(String network) 172 { 173 if (_denyNetworkList == null) 174 _denyNetworkList = new ArrayList <InetNetwork>(); 175 176 _denyNetworkList.add(InetNetwork.create(network)); 177 } 178 179 180 public void addText(String network) 181 { 182 _oldStyle = true; 183 addAllow(network); 184 } 185 186 187 boolean isOldStyle() 188 { 189 return _oldStyle; 190 } 191 192 193 void copyInto(IPConstraint target) 194 { 195 if (_allowNetworkList != null) { 196 for (int i = 0; i < _allowNetworkList.size(); i++) { 197 target.addAllowInetNetwork(_allowNetworkList.get(i)); 198 } 199 } 200 if (_denyNetworkList != null) { 201 for (int i = 0; i < _denyNetworkList.size(); i++) { 202 target.addDenyInetNetwork(_denyNetworkList.get(i)); 203 } 204 } 205 } 206 207 private void addAllowInetNetwork(InetNetwork a) 208 { 209 if (_allowNetworkList == null) 210 _allowNetworkList = new ArrayList <InetNetwork>(); 211 _allowNetworkList.add(a); 212 } 213 214 private void addDenyInetNetwork(InetNetwork d) 215 { 216 if (_denyNetworkList == null) 217 _denyNetworkList = new ArrayList <InetNetwork>(); 218 _denyNetworkList.add(d); 219 } 220 221 @PostConstruct 222 public void init() 223 throws ConfigException 224 { 225 if (_allowNetworkList == null && _denyNetworkList == null) 226 throw new ConfigException(L.l("either `{0}' or `{1}' or both are expected", "<allow>", "<deny>")); 227 228 if (_allowNetworkList != null) 229 _allowNetworkList.trimToSize(); 230 231 if (_denyNetworkList != null) 232 _denyNetworkList.trimToSize(); 233 234 int rules = _allowNetworkList == null ? 0 : _allowNetworkList.size(); 235 rules += _denyNetworkList == null ? 0 : _denyNetworkList.size(); 236 237 _cache = new LruCache<String ,Boolean >(_cacheSize); 238 } 239 240 243 public boolean isAuthorized(HttpServletRequest request, 244 HttpServletResponse response, 245 ServletContext application) 246 throws ServletException , IOException 247 { 248 String remoteAddr = request.getRemoteAddr(); 249 long addr = 0; 250 boolean allow = false; 251 252 if (remoteAddr != null) { 253 if (_cache != null) { 254 Boolean cacheValue = _cache.get(remoteAddr); 255 if (cacheValue != null) { 256 allow = cacheValue.booleanValue(); 257 258 if (!allow) 259 response.sendError(_errorCode, _errorMessage); 260 261 return allow; 262 } 263 } 264 265 int len = remoteAddr.length(); 266 int ch; 267 int i = 0; 268 269 while (i < len && (ch = remoteAddr.charAt(i)) >= '0' && ch <= '9') { 270 int digit = 0; 271 272 for (; i < len && (ch = remoteAddr.charAt(i)) >= '0' && ch <= '9'; i++) 273 digit = 10 * digit + ch - '0'; 274 275 addr = 256 * addr + digit; 276 277 if (ch == '.') 278 i++; 279 } 280 } 281 282 284 if (_allowNetworkList == null) { 285 allow = true; 287 } 288 else { 289 for (int i = 0; i < _allowNetworkList.size(); i++) { 290 InetNetwork net = _allowNetworkList.get(i); 291 292 if (net.isMatch(addr)) { 293 allow = true; 294 break; 295 } 296 } 297 } 298 299 301 if (allow && _denyNetworkList != null) { 302 for (int i = 0; i < _denyNetworkList.size(); i++) { 303 InetNetwork net = _denyNetworkList.get(i); 304 305 if (net.isMatch(addr)) { 306 allow = false; 307 break; 308 } 309 } 310 } 311 312 314 if (_cache != null) 315 _cache.put(remoteAddr, allow ? Boolean.TRUE : Boolean.FALSE); 316 317 319 if (!allow) 320 response.sendError(_errorCode, _errorMessage); 321 322 return allow; 323 324 } 325 326 } 327 | Popular Tags |