1 16 17 package org.apache.webapp.admin.valve; 18 19 import java.lang.IllegalArgumentException ; 20 import java.net.InetAddress ; 21 import java.util.List ; 22 import java.util.regex.Pattern ; 23 import javax.servlet.http.HttpServletRequest ; 24 25 import org.apache.struts.action.ActionError; 26 import org.apache.struts.action.ActionErrors; 27 import org.apache.struts.action.ActionForm; 28 import org.apache.struts.action.ActionMapping; 29 30 import org.apache.webapp.admin.ApplicationServlet; 31 import org.apache.webapp.admin.LabelValueBean; 32 33 39 40 public final class RemoteHostValveForm extends ValveForm { 41 42 44 45 50 private String allow = ""; 51 52 55 private String deny = ""; 56 57 60 private Pattern allows[] = new Pattern [0]; 61 62 65 private Pattern denies[] = new Pattern [0]; 66 67 68 70 73 public String getAllow() { 74 75 return this.allow; 76 77 } 78 79 82 public void setAllow(String allow) { 83 84 this.allow = allow; 85 86 } 87 88 91 public String getDeny() { 92 93 return this.deny; 94 95 } 96 97 100 public void setDeny(String deny) { 101 102 this.deny = deny; 103 104 } 105 106 108 114 public void reset(ActionMapping mapping, HttpServletRequest request) { 115 116 super.reset(mapping, request); 117 this.allow = null; 118 this.deny = null; 119 this.allows = null; 120 this.denies = null; 121 122 } 123 124 127 public String toString() { 128 129 StringBuffer sb = new StringBuffer ("RemoteHostValveForm[adminAction="); 130 sb.append(getAdminAction()); 131 sb.append("',valveType="); 132 sb.append(getValveType()); 133 sb.append(",allow="); 134 sb.append(allow); 135 sb.append(",deny="); 136 sb.append(deny); 137 sb.append("',objectName='"); 138 sb.append(getObjectName()); 139 sb.append("]"); 140 return (sb.toString()); 141 142 } 143 144 154 155 public ActionErrors validate(ActionMapping mapping, 156 HttpServletRequest request) { 157 158 ActionErrors errors = new ActionErrors(); 159 160 String submit = request.getParameter("submit"); 161 162 if ((allow == null) || (allow.length() < 1)) { 167 if ((deny == null) || (deny.length() < 1)) { 168 errors.add("allow", 169 new ActionError("error.allow.deny.required")); 170 } 171 } 172 174 try { 175 allows = ValveUtil.precalculate(allow); 176 } catch (IllegalArgumentException e) { 177 errors.add("allow", new ActionError("error.syntax")); 178 return errors; 179 } 180 181 try { 182 denies = ValveUtil.precalculate(deny); 183 } catch (IllegalArgumentException e) { 184 errors.add("allow", new ActionError("error.syntax")); 185 return errors; 186 } 187 188 String host = request.getRemoteHost(); 189 String ip = request.getRemoteAddr(); 192 193 if (host == null) { 194 return errors; 195 } 196 197 for (int i = 0; i < denies.length; i++) { 198 if (denies[i].matcher(host).matches()) { 199 if (allows.length < 1) { 200 errors.add("deny", 201 new ActionError("error.denyHost")); 202 } 203 for (int j = 0; j < allows.length; j++) { 204 if (!allows[j].matcher(host).matches()) { 205 errors.add("deny", 206 new ActionError("error.denyHost")); 207 } 208 } 209 } else if (denies[i].matcher(ip).matches()) { 210 if (allows.length < 1) { 211 errors.add("deny", 212 new ActionError("error.denyHost")); 213 } 214 for (int j = 0; j < allows.length; j++) { 215 if (!allows[j].matcher(ip).matches()) { 216 errors.add("deny", 217 new ActionError("error.denyHost")); 218 } 219 } 220 } 221 } 222 223 boolean allowMatch = true; 224 225 if ((allows != null) && (allows.length > 0)) { 226 allowMatch = false; 227 } 228 229 for (int i = 0; i < allows.length; i++) { 230 if (allows[i].matcher(host).matches()) { 231 allowMatch = true; 232 } 233 } 234 235 if (!allowMatch) { 236 errors.add("allow", new ActionError("error.allowHost")); 237 } 238 239 return errors; 240 } 241 242 } 243 | Popular Tags |