1 23 package com.sun.enterprise.security; 24 25 import java.lang.System ; 26 import java.lang.SecurityManager ; 27 import java.util.logging.Logger ; 28 import java.util.logging.Level ; 29 30 import java.util.Set ; 31 32 import java.security.Principal ; 33 import java.security.AccessController ; 34 import java.security.AccessControlContext ; 35 import java.security.PrivilegedExceptionAction ; 36 import java.security.PrivilegedAction ; 37 import java.security.ProtectionDomain ; 38 import javax.security.auth.Subject ; 39 import com.sun.enterprise.deployment.PrincipalImpl; 40 import com.sun.enterprise.config.serverbeans.*; 41 import com.sun.enterprise.config.*; 42 import com.sun.enterprise.server.ApplicationServer; 43 import com.sun.web.security.PrincipalGroupFactory; 44 import com.sun.logging.*; 45 46 47 63 public class SecurityContext extends AbstractSecurityContext { 64 65 private static Logger _logger=null; 66 static { 67 _logger=LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 68 } 69 70 private static InheritableThreadLocal currentSecCtx = 71 new InheritableThreadLocal (); 72 private static SecurityContext defaultSecurityContext = 73 generateDefaultSecurityContext(); 74 75 private static javax.security.auth.AuthPermission doAsPrivilegedPerm = 76 new javax.security.auth.AuthPermission ("doAsPrivileged"); 77 78 private boolean SERVER_GENERATED_SECURITY_CONTEXT = false; 80 81 82 93 public SecurityContext(String userName, Subject subject) { 94 Subject s = subject; 95 if (s == null) { 96 s = new Subject (); 97 _logger.warning("java_security.null_subject"); 98 } 99 this.initiator = new PrincipalImpl(userName); 100 final Subject sub = s; 101 this.subject = (javax.security.auth.Subject ) 102 AccessController.doPrivileged(new PrivilegedAction (){ 103 public java.lang.Object run() { 104 sub.getPrincipals().add(initiator); 105 return sub; 106 } 107 }); 108 } 109 110 public SecurityContext(String userName, Subject subject, String realm) { 111 Subject s = subject; 112 if (s == null) { 113 s = new Subject (); 114 _logger.warning("java_security.null_subject"); 115 } 116 this.initiator = PrincipalGroupFactory.getPrincipalInstance(userName, realm); 117 final Subject sub = s; 118 this.subject = (javax.security.auth.Subject ) 119 AccessController.doPrivileged(new PrivilegedAction (){ 120 public java.lang.Object run() { 121 sub.getPrincipals().add(initiator); 122 return sub; 123 } 124 }); 125 } 126 127 129 private SecurityContext() { 130 this.subject = new Subject (); 131 this.initiator = null; 133 this.setServerGeneratedCredentials(); 134 this.subject.setReadOnly(); 136 } 137 138 142 public static SecurityContext init(){ 143 SecurityContext sc = (SecurityContext) currentSecCtx.get(); 144 if(sc == null) { sc = defaultSecurityContext; 146 } 147 return sc; 148 } 149 150 public static SecurityContext getDefaultSecurityContext(){ 151 return defaultSecurityContext; 153 } 154 155 public static Subject getDefaultSubject(){ 156 return defaultSecurityContext.subject; 158 } 159 160 public static Principal getDefaultCallerPrincipal(){ 162 synchronized(SecurityContext.class) { 163 if (defaultSecurityContext.initiator == null) { 164 String guestUser = null; 165 try { 166 guestUser = (String ) 167 AccessController.doPrivileged(new PrivilegedExceptionAction () { 168 public java.lang.Object run() throws Exception { 169 ConfigContext configContext = 170 ApplicationServer.getServerContext(). 171 getConfigContext(); 172 assert(configContext != null); 173 SecurityService securityBean = 174 ServerBeansFactory. 175 getSecurityServiceBean(configContext); 176 assert(securityBean != null); 177 return securityBean.getDefaultPrincipal(); 178 } 179 }); 180 } catch (Exception e) { 181 _logger.log(Level.SEVERE, 182 "java_security.default_user_login_Exception", e); 183 } finally { 184 if (guestUser == null) { 185 guestUser = "ANONYMOUS"; 186 } 187 } 188 defaultSecurityContext.initiator = new PrincipalImpl(guestUser); 189 } 190 } 191 return defaultSecurityContext.initiator; 192 } 193 194 private static SecurityContext generateDefaultSecurityContext() { 195 synchronized (SecurityContext.class) { 196 try{ 197 return (SecurityContext) 198 AccessController.doPrivileged(new PrivilegedExceptionAction () { 199 public java.lang.Object run() throws Exception { 200 return new SecurityContext(); 201 } 202 }); 203 } catch(Exception e){ 204 _logger.log(Level.SEVERE, 205 "java_security.security_context_exception",e); 206 } 207 } 208 return null; 209 } 210 211 214 public static void reset(SecurityContext sc){ 215 setCurrent(sc); 216 } 217 218 219 225 public static SecurityContext getCurrent() { 226 SecurityContext sc = (SecurityContext) currentSecCtx.get(); 227 if (sc == null) { 228 sc = defaultSecurityContext; 229 } 230 return sc; 231 } 232 233 234 244 public static void setCurrent(SecurityContext sc) { 245 246 if (sc != null && sc != defaultSecurityContext) { 247 248 SecurityContext current = (SecurityContext)currentSecCtx.get(); 249 250 if (sc != current) { 251 252 boolean permitted = false; 253 254 try { 255 SecurityManager sm = System.getSecurityManager(); 256 if (sm != null) { 257 if(_logger.isLoggable(Level.FINE)){ 258 _logger.fine("permission check done to set SecurityContext"); 259 } 260 sm.checkPermission(doAsPrivilegedPerm); 261 } 262 permitted = true; 263 } catch (java.lang.SecurityException se) { 264 _logger.log(Level.SEVERE, "java_security.security_context_permission_exception", se); 265 } catch (Throwable t) { 266 _logger.log(Level.SEVERE, "java_security.security_context_unexpected_exception", t); 267 } 268 269 if (permitted) { 270 currentSecCtx.set(sc); 271 } else { 272 _logger.severe("java_security.security_context_nochange"); 273 } 274 } 275 } else { 276 currentSecCtx.set(sc); 277 } 278 } 279 280 public static void setUnauthenticatedContext() { 281 currentSecCtx.set(defaultSecurityContext); 282 } 283 284 public boolean didServerGenerateCredentials (){ 285 return SERVER_GENERATED_SECURITY_CONTEXT; 286 } 287 288 private void setServerGeneratedCredentials(){ 289 SERVER_GENERATED_SECURITY_CONTEXT = true; 290 } 291 292 293 299 public Principal getCallerPrincipal() { 300 return this == defaultSecurityContext ? getDefaultCallerPrincipal() : initiator; 301 } 302 303 304 public Subject getSubject() { 305 return subject; 306 } 307 308 309 public String toString() { 310 return "SecurityContext[ " + "Initiator: " + 311 initiator + "Subject " + subject + " ]"; 312 } 313 314 public Set getPrincipalSet() { 315 return subject.getPrincipals(); 316 } 317 } 318 319 320 321 322 323 324 325 | Popular Tags |