1 19 20 package com.sslexplorer.core.actions; 21 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.servlet.http.HttpSession ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.Globals; 33 import org.apache.struts.action.ActionForm; 34 import org.apache.struts.action.ActionForward; 35 import org.apache.struts.action.ActionMapping; 36 import org.apache.struts.action.ActionMessage; 37 import org.apache.struts.action.ActionMessages; 38 39 import com.sslexplorer.boot.ContextHolder; 40 import com.sslexplorer.boot.Util; 41 import com.sslexplorer.core.CoreUtil; 42 import com.sslexplorer.core.RedirectWithMessages; 43 import com.sslexplorer.core.ServletRequestAdapter; 44 import com.sslexplorer.core.ServletResponseAdapter; 45 import com.sslexplorer.core.forms.CoreForm; 46 import com.sslexplorer.policyframework.NoPermissionException; 47 import com.sslexplorer.policyframework.Permission; 48 import com.sslexplorer.policyframework.PolicyDatabaseFactory; 49 import com.sslexplorer.policyframework.ResourceType; 50 import com.sslexplorer.properties.PropertyProfile; 51 import com.sslexplorer.security.Constants; 52 import com.sslexplorer.security.LogonController; 53 import com.sslexplorer.security.LogonControllerFactory; 54 import com.sslexplorer.security.SessionInfo; 55 import com.sslexplorer.security.SystemDatabaseFactory; 56 57 64 public abstract class AuthenticatedDispatchAction extends DefaultDispatchAction implements CoreAction { 65 static Log log = LogFactory.getLog(AuthenticatedAction.class); 66 protected ResourceType resourceType, requiresResourcesOfType; 67 protected Permission[] permissions; 68 69 73 public AuthenticatedDispatchAction() { 74 } 75 76 77 84 public AuthenticatedDispatchAction(ResourceType resourceType, Permission permissions[]) { 85 this(resourceType, permissions, null); 86 } 87 88 96 public AuthenticatedDispatchAction(ResourceType resourceType, Permission permissions[], ResourceType requiresResources) { 97 this.resourceType = resourceType; 98 this.requiresResourcesOfType = requiresResources; 99 this.permissions = permissions; 100 } 101 102 114 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 115 throws Exception { 116 boolean setupMode = ContextHolder.getContext().isSetupMode(); 118 if (setupMode) { 119 if ((getNavigationContext(mapping, form, request, response) & SessionInfo.SETUP_CONSOLE_CONTEXT) == 0) { 120 return mapping.findForward("setup"); 121 } else { 122 126 request.setAttribute(Constants.REQ_ATTR_ACTION_MAPPING, mapping); 127 request.setAttribute(Constants.REQ_ATTR_FORM, form); 128 129 CoreUtil.checkNavigationContext(this, mapping, form, request, response); 131 return super.execute(mapping, form, request, response); 132 } 133 } 134 try { 135 try { 136 if (!SystemDatabaseFactory.getInstance().verifyIPAddress(request.getRemoteAddr())) { 137 String link = null; 138 log.error(request.getRemoteHost() + " is not authorized"); 139 if (log.isInfoEnabled()) 140 log.info("Logging off, IP address verification failed."); 141 142 if(LogonControllerFactory.getInstance().hasClientLoggedOn(request, response) == LogonController.LOGGED_ON) { 143 LogonControllerFactory.getInstance().logoffSession(request, response); 144 } 145 146 if (link != null) { 147 ActionForward fwd = new ActionForward(link, true); 148 return fwd; 149 } else { 150 if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { 152 response.setStatus(HttpServletResponse.SC_FORBIDDEN); 153 return null; 154 } 155 return (mapping.findForward("logon")); 156 } 157 } else { 158 162 request.setAttribute(Constants.REQ_ATTR_ACTION_MAPPING, mapping); 163 request.setAttribute(Constants.REQ_ATTR_FORM, form); 164 165 int logonStatus = LogonControllerFactory.getInstance().hasClientLoggedOn(request, response); 166 if (logonStatus == LogonController.INVALID_TICKET) { 167 ActionMessages msgs = new ActionMessages(); 168 msgs.add(Globals.ERROR_KEY, new ActionMessage("login.invalidTicket")); 169 saveErrors(request, msgs); 170 } else if (logonStatus == LogonController.LOGGED_ON) { 171 SessionInfo session = LogonControllerFactory.getInstance().getSessionInfo(request); 172 LogonControllerFactory.getInstance().addCookies(new ServletRequestAdapter(request), 174 new ServletResponseAdapter(response), 175 (String ) request.getSession().getAttribute(Constants.LOGON_TICKET), getSessionInfo(request)); 176 177 ActionForward fwd = checkIntercept(mapping, request, response); 178 if (fwd != null) { 179 return fwd; 180 } 181 182 187 CoreUtil.checkNavigationContext(this, mapping, form, request, response); 188 189 PropertyProfile profile = null; 190 if (request.getSession().getAttribute(Constants.SESSION_LOCKED) == null) { 191 profile = (PropertyProfile) request.getSession().getAttribute(Constants.SELECTED_PROFILE); 192 if (profile == null) { 193 request.getSession().setAttribute(Constants.ORIGINAL_REQUEST, Util.getOriginalRequest(request)); 194 return mapping.findForward("selectPropertyProfile"); 195 } 196 doCheckPermissions(mapping, session, request); 197 return super.execute(mapping, form, request, response); 198 } 199 } 200 } 201 } catch (NoPermissionException e) { 202 if (log.isDebugEnabled()) 203 log.debug("User " + e.getPrincipalName() 204 + " attempted to access page they do have have permission for. Resource type = " 205 + e.getResourceType() 206 + ". Now attempting to find the first valid item in the current menu tree to display.", e); 207 response.sendError(HttpServletResponse.SC_FORBIDDEN); 208 return null; 209 } catch (SecurityException ex) { 210 } catch (ServletException ex) { 212 throw ex; 213 } 214 215 if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { 217 response.setStatus(HttpServletResponse.SC_NO_CONTENT); 218 return null; 219 } 220 221 return gotoLogon(mapping, form, request, response); 222 } catch (Throwable t) { 223 log.error("Failed to process authenticated request.", t); 224 throw t instanceof Exception ? (Exception ) t : new Exception (t); 225 } 226 } 227 228 protected void doCheckPermissions(ActionMapping mapping, SessionInfo session, HttpServletRequest request) throws Exception { 229 230 boolean ok = true; 233 if (resourceType != null && permissions != null) { 234 ok = PolicyDatabaseFactory.getInstance().isPermitted(resourceType, permissions, session.getUser(), false); 235 } 236 if (!ok && requiresResourcesOfType != null) { 237 ok = PolicyDatabaseFactory.getInstance().isPrincipalGrantedResourcesOfType(session.getUser(), requiresResourcesOfType, null); 238 } 239 if (!ok) { 240 throw new NoPermissionException(session.getUser(), resourceType); 241 } 242 } 243 244 255 protected ActionForward gotoLogon(ActionMapping mapping, ActionForm form, HttpServletRequest request, 256 HttpServletResponse response) throws Exception { 257 request.getSession().setAttribute(Constants.ORIGINAL_REQUEST, Util.getOriginalRequest(request)); 258 return mapping.findForward("logon"); 259 } 260 261 275 public SessionInfo getSessionInfo(HttpServletRequest request) { 276 return LogonControllerFactory.getInstance().getSessionInfo(request); 277 278 } 279 280 283 public ResourceType getResourceType() { 284 return resourceType; 285 } 286 287 295 public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 296 throws Exception { 297 return cleanUpAndReturnToReferer(mapping, form, request, response); 298 } 299 300 308 public ActionForward cleanUpAndReturnToReferer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 309 HttpSession session = request.getSession(); 310 String toRemove = null; 311 for (Enumeration e = session.getAttributeNames(); toRemove == null && e.hasMoreElements();) { 312 String n = (String ) e.nextElement(); 313 if (session.getAttribute(n) == form) { 314 toRemove = n; 315 } 316 } 317 if (toRemove != null) { 318 request.getSession().removeAttribute(toRemove); 319 } 320 request.getSession().removeAttribute(Constants.EDITING_ITEM); 321 322 ActionForward fwd = mapping.findForward("done"); 325 if(fwd != null) { 326 return new RedirectWithMessages(fwd, request); 327 } 328 if (((CoreForm) form).getReferer() == null) { 329 log.warn("Original referer was null, forwarding to home"); 330 return mapping.findForward("home"); 331 } else { 332 return new RedirectWithMessages(((CoreForm) form).getReferer(), request); 333 } 334 } 335 336 340 void sendAuthorizationError(HttpServletResponse response) throws IOException { 341 response.setHeader("WWW-Authenticate", "Basic realm=\"SSL-Explorer\""); 342 response.sendError(HttpServletResponse.SC_UNAUTHORIZED); 343 } 344 345 protected void saveError(HttpServletRequest request, String message ) { 346 saveError(request, message, ""); 347 } 348 349 protected void saveError(HttpServletRequest request, String message, Object ... objects) { 350 saveError(request, new ActionMessage(message, objects)); 351 } 352 353 protected void saveError(HttpServletRequest request, ActionMessage message) { 354 ActionMessages actionMessages = new ActionMessages(); 355 actionMessages.add(Globals.ERROR_KEY, message); 356 saveErrors(request, actionMessages); 357 } 358 359 protected void saveMessage(HttpServletRequest request, String message ) { 360 saveMessage(request, message, ""); 361 } 362 363 protected void saveMessage(HttpServletRequest request, String message, Object ... objects) { 364 ActionMessages actionMessages = new ActionMessages(); 365 actionMessages.add(Globals.MESSAGE_KEY, new ActionMessage(message, objects)); 366 saveMessages(request, actionMessages); 367 } 368 369 protected static ActionForward getRedirectWithMessages(ActionMapping mapping, HttpServletRequest request) { 370 return getRedirectWithMessages("refresh", mapping, request); 371 } 372 373 protected static ActionForward getRedirectWithMessages(String redirect, ActionMapping mapping, HttpServletRequest request) { 374 return new RedirectWithMessages(mapping.findForward(redirect), request); 375 } 376 } | Popular Tags |