1 19 20 package com.sslexplorer.security.actions; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.List ; 25 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 29 import org.apache.struts.action.ActionForm; 30 import org.apache.struts.action.ActionForward; 31 import org.apache.struts.action.ActionMapping; 32 33 import com.sslexplorer.core.CoreAttributeConstants; 34 import com.sslexplorer.core.CoreEvent; 35 import com.sslexplorer.core.CoreEventConstants; 36 import com.sslexplorer.core.CoreServlet; 37 import com.sslexplorer.core.CoreUtil; 38 import com.sslexplorer.core.actions.AuthenticatedDispatchAction; 39 import com.sslexplorer.policyframework.Permission; 40 import com.sslexplorer.policyframework.PolicyConstants; 41 import com.sslexplorer.policyframework.PolicyUtil; 42 import com.sslexplorer.security.Constants; 43 import com.sslexplorer.security.IpRestriction; 44 import com.sslexplorer.security.SessionInfo; 45 import com.sslexplorer.security.SystemDatabase; 46 import com.sslexplorer.security.SystemDatabaseFactory; 47 import com.sslexplorer.security.forms.IpRestrictionForm; 48 49 55 public class ShowIpRestrictionDispatchAction extends AuthenticatedDispatchAction { 56 57 60 public ShowIpRestrictionDispatchAction() { 61 super(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_CREATE, PolicyConstants.PERM_DELETE }); 62 } 63 64 public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 65 return mapping.findForward("display"); 66 } 67 68 77 public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 78 throws Exception { 79 PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, PolicyConstants.PERM_CREATE, request); 80 IpRestriction[] ipRestriction = SystemDatabaseFactory.getInstance().getIpRestrictions(); 81 IpRestrictionForm ipRestrictionForm = (IpRestrictionForm) form; 82 ipRestrictionForm.initialize(new IpRestriction(ipRestriction.length > 0 && ipRestriction[0].getDenied()), false); 83 ipRestrictionForm.setReferer(CoreUtil.getReferer(request)); 84 CoreUtil.addRequiredFieldMessage(this, request); 85 return mapping.findForward("display"); 86 } 87 88 97 public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 98 throws Exception { 99 PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, PolicyConstants.PERM_EDIT, request); 100 IpRestrictionForm ipRestrictionForm = (IpRestrictionForm) form; 101 ipRestrictionForm.initialize((IpRestriction)request.getAttribute(Constants.EDITING_ITEM), true); 102 ipRestrictionForm.setReferer(CoreUtil.getReferer(request)); 103 CoreUtil.addRequiredFieldMessage(this, request); 104 return mapping.findForward("display"); 105 } 106 107 116 public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 117 throws Exception { 118 if (isAdditionValid(request, ((IpRestrictionForm)form))) { 119 return doCommit(mapping, form, request, response); 120 } 121 else { 122 return mapping.findForward("confirmCreateWithLockout"); 123 } 124 } 125 126 135 public ActionForward doCommit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 136 throws Exception { 137 IpRestrictionForm ipRestrictionForm = (IpRestrictionForm)form; 138 ipRestrictionForm.apply(); 139 Permission permission = ipRestrictionForm.isEditing() ? PolicyConstants.PERM_EDIT : PolicyConstants.PERM_CREATE; 140 PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, permission, request); 141 142 try { 143 if(ipRestrictionForm.isEditing()) { 144 SystemDatabaseFactory.getInstance().updateIpRestriction(ipRestrictionForm.getRestriction()); 145 } 146 else { 147 SystemDatabaseFactory.getInstance().addIpRestriction(ipRestrictionForm.getRestriction().getAddress(), ipRestrictionForm.getRestriction().getType()); 148 } 149 fireCoreEvent(request, ipRestrictionForm, CoreEvent.STATE_SUCCESSFUL); 150 saveMessage(request, "editIpRestriction.message.restrictionSaved", ipRestrictionForm.getRestriction().getAddress()); 151 } catch (Exception e) { 152 fireCoreEvent(request, ipRestrictionForm, CoreEvent.STATE_UNSUCCESSFUL); 153 throw e; 154 } 155 return cancel(mapping, form, request, response); 156 } 157 158 private boolean isAdditionValid(HttpServletRequest request, IpRestrictionForm form) throws Exception { 159 IpRestriction[] ipRestrictions = findIpRestrictions(form.getRestriction().getAddress(), form.getType().equals(IpRestrictionForm.ALLOW_TYPE)); 160 SystemDatabase database = SystemDatabaseFactory.getInstance(); 161 String remoteAddr = request.getRemoteAddr(); 162 return database.verifyIPAddress(remoteAddr, ipRestrictions); 163 } 164 165 private IpRestriction[] findIpRestrictions(String restriction, boolean isAllow) throws Exception { 166 SystemDatabase database = SystemDatabaseFactory.getInstance(); 167 IpRestriction[] restrictions = database.getIpRestrictions(); 168 int i = 0; 169 for(; i < restrictions.length; i++) { 170 if(restrictions[i].getAddress().equals(restriction)) { 171 restrictions[i] = new IpRestriction(restrictions[i].getAddress(), isAllow, restrictions[i].getPriority()); 172 break; 173 } 174 } 175 if(i == restrictions.length) { 176 List <IpRestriction> newRestrictions = new ArrayList <IpRestriction>(Arrays.asList(restrictions)); 177 newRestrictions.add(new IpRestriction(restriction, isAllow, Integer.MAX_VALUE)); 178 return newRestrictions.toArray(new IpRestriction[newRestrictions.size()]); 179 } 180 return restrictions; 181 } 182 183 private void fireCoreEvent(HttpServletRequest request, IpRestrictionForm ipRestrictionForm, int state) { 184 IpRestriction restriction = ipRestrictionForm.getRestriction(); 185 int eventType = ipRestrictionForm.isEditing() ? CoreEventConstants.EDIT_IP_RESTRICTION : CoreEventConstants.CREATE_IP_RESTRICTION; 186 CoreEvent coreEvent = new CoreEvent(this, eventType, null, getSessionInfo(request), state); 187 coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_IP_RESTRICTION_ADDRESS, restriction.getAddress()); 188 coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_IP_RESTRICTION_IS_AUTHORIZED, String.valueOf(restriction.getAllowed())); 189 CoreServlet.getServlet().fireCoreEvent(coreEvent); 190 191 } 192 193 200 public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { 201 return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT; 202 } 203 } | Popular Tags |