1 19 20 package com.sslexplorer.security; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.IOException ; 26 import java.io.InputStreamReader ; 27 import java.net.InetAddress ; 28 import java.util.ArrayList ; 29 import java.util.Calendar ; 30 import java.util.GregorianCalendar ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 import javax.servlet.http.HttpSession ; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.mortbay.jetty.servlet.SessionManager.Session; 41 42 import com.sslexplorer.boot.ContextHolder; 43 import com.sslexplorer.boot.Util; 44 import com.sslexplorer.core.CoreEvent; 45 import com.sslexplorer.core.CoreEventConstants; 46 import com.sslexplorer.core.CoreListener; 47 import com.sslexplorer.core.CoreServlet; 48 import com.sslexplorer.core.CoreUtil; 49 import com.sslexplorer.realms.Realm; 50 51 57 public class SessionInfo implements CoreListener { 58 59 final static Log log = LogFactory.getLog(SessionInfo.class); 60 61 private static List davUserAgents = null; 62 63 64 65 68 public final static int UI = 0; 69 70 73 public final static int AGENT = 1; 74 75 78 public final static int DAV_CLIENT = 2; 79 80 81 82 85 public static final int USER_CONSOLE_CONTEXT = 1; 86 87 91 public static final int MANAGEMENT_CONSOLE_CONTEXT = 2; 92 93 97 public static final int SETUP_CONSOLE_CONTEXT = 4; 98 99 103 public static final int HELP_CONTEXT = 8; 104 105 108 public static final int ALL_CONTEXTS = 255; 109 110 111 private User user; 112 private InetAddress address; 113 private Calendar logonTime; 114 private int type; 115 private String logonTicket; 116 private int navigationContext; 117 private HttpSession session; 118 private int id; 119 private String userAgent; 120 private long lastAccessTime; 121 private boolean invalidating; 122 private Map <String , Object > attributes; 123 private List <SessionInfoListener> listeners = new ArrayList <SessionInfoListener>(); 124 125 126 127 private static HashMap sessions = new HashMap (); 128 private static int nextId = 1; 129 130 141 public static SessionInfo nextSession(HttpSession session, String logonTicket, User user, InetAddress address, int type, 142 String userAgent) { 143 synchronized (sessions) { 144 SessionInfo info = new SessionInfo(nextId, session, logonTicket, user, address, type, userAgent); 145 if (LogonControllerFactory.getInstance().isAdministrator(user)) { 146 info.setNavigationContext(SessionInfo.MANAGEMENT_CONSOLE_CONTEXT); 147 } 148 sessions.put(String.valueOf(nextId), info); 149 nextId++; 150 return info; 151 } 152 } 153 154 160 public static SessionInfo getSession(int id) { 161 return (SessionInfo) sessions.get(String.valueOf(id)); 162 } 163 164 167 public void release() { 168 synchronized (sessions) { 169 sessions.remove(String.valueOf(id)); 170 171 174 Map.Entry e; 175 int next = 1; 176 boolean found; 177 while (true) { 178 found = false; 179 for (Iterator i = sessions.entrySet().iterator(); i.hasNext();) { 180 e = (Map.Entry ) i.next(); 181 if (((SessionInfo) e.getValue()).getId() == next) { 182 found = true; 183 break; 184 } 185 } 186 if (!found) { 187 nextId = next; 188 break; 189 } 190 next++; 191 192 } 193 } 194 } 195 196 197 198 private SessionInfo(int id, HttpSession session, String logonTicket, User user, InetAddress address, int type, String userAgent) { 199 attributes = new HashMap <String , Object >(); 200 this.user = user; 201 this.id = id; 202 this.session = session; 203 this.logonTicket = logonTicket; 204 this.address = address; 205 navigationContext = USER_CONSOLE_CONTEXT; 206 this.type = type; 207 this.userAgent = userAgent; 208 logonTime = new GregorianCalendar (); 209 lastAccessTime = System.currentTimeMillis(); 210 CoreServlet.getServlet().addCoreListener(this); 211 } 212 213 public Object setAttribute(String key, Object value) { 214 if(value instanceof SessionInfoListener && 215 !listeners.contains(value)) { 216 listeners.add((SessionInfoListener)value); 217 } 218 return attributes.put(key, value); 219 } 220 221 public Object getAttribute(String key) { 222 return attributes.get(key); 223 } 224 225 public Object removeAttribute(String key) { 226 Object val = attributes.remove(key); 227 if(val instanceof SessionInfoListener) { 228 listeners.remove((SessionInfoListener)val); 229 } 230 return val; 231 } 232 233 public void access() { 234 ((Session ) session).access(); 236 } 237 238 243 public int getId() { 244 return id; 245 } 246 247 252 public HttpSession getHttpSession() { 253 return session; 254 } 255 256 261 public String getLogonTicket() { 262 return logonTicket; 263 } 264 265 272 public int getType() { 273 return type; 274 } 275 276 283 public void setType(int type) { 284 this.type = type; 285 } 286 287 292 public InetAddress getAddress() { 293 return address; 294 } 295 296 301 public Calendar getLogonTime() { 302 return logonTime; 303 } 304 305 310 public User getUser() { 311 return user; 312 } 313 314 320 public Realm getRealm() { 321 return user == null ? null : user.getRealm(); 322 } 323 324 330 public int getRealmId() { 331 Realm r = user == null ? null : user.getRealm(); 332 return r == null ? 0 : r.getResourceId(); 333 } 334 335 340 public void setSession(HttpSession session) { 341 this.session = session; 342 } 343 344 350 public int getNavigationContext() { 351 return navigationContext; 352 } 353 354 360 public void setNavigationContext(int navigationContext) { 361 this.navigationContext = navigationContext; 362 } 363 364 369 public AuthenticationScheme getCredentials() { 370 return (AuthenticationScheme) session.getAttribute(Constants.AUTH_SESSION); 371 } 372 373 378 public String getUserAgent() { 379 return userAgent; 380 } 381 382 387 public void setUser(User user) { 388 this.user = user; 389 } 390 391 413 public static int getSessionTypeForUserAgent(String userAgent) { 414 if (userAgent == null) { 415 return UI; 416 } 417 if (userAgent.equals("SSL-Explorer/Agent")) { 418 return AGENT; 419 } 420 if (davUserAgents == null) { 421 davUserAgents = new ArrayList (); 422 File f = new File (ContextHolder.getContext().getConfDirectory(), "dav.agents"); 423 FileInputStream fin = null; 424 try { 425 fin = new FileInputStream (f); 426 BufferedReader br = new BufferedReader (new InputStreamReader (fin)); 427 String line = null; 428 while ((line = br.readLine()) != null) { 429 line = Util.trimBoth(line); 430 if (!line.startsWith("#")) { 431 davUserAgents.add(line); 432 } 433 } 434 } catch (IOException ioe) { 435 log.warn("Failed to read " + f.getAbsolutePath() + ". Will not be able to identify DAV clients."); 436 } finally { 437 Util.closeStream(fin); 438 } 439 } 440 for (Iterator i = davUserAgents.iterator(); i.hasNext();) { 441 String us = (String ) i.next(); 442 if (userAgent.matches(us)) { 443 return SessionInfo.DAV_CLIENT; 444 } 445 } 446 return SessionInfo.UI; 447 } 448 449 public String toString() { 450 return session.getId() + "/" + user.getPrincipalName(); 451 } 452 453 public boolean isInvalidating() { 454 return invalidating; 455 } 456 457 public void invalidate() { 458 if (session != null) { 459 invalidating = true; 460 session.invalidate(); 461 invalidating = false; 462 } 463 for(SessionInfoListener l : listeners) { 464 l.invalidated(); 465 } 466 CoreServlet.getServlet().removeCoreListener(this); 467 } 468 469 public void coreEvent(CoreEvent evt) { 470 if (evt.getId() == CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL 471 || evt.getId() == CoreEventConstants.REVOKE_POLICY_FROM_PRINCIPAL 472 || evt.getId() == CoreEventConstants.RESOURCE_DETACHED_FROM_POLICY 473 || evt.getId() == CoreEventConstants.RESOURCE_ATTACHED_TO_POLICY) { 474 if (session != null) { 475 synchronized (session) { 476 CoreUtil.resetMainNavigation(getHttpSession()); 477 } 478 } 479 } 480 } 481 482 } | Popular Tags |