1 22 package org.jboss.ejb.plugins; 23 24 import java.security.PrivilegedAction ; 25 import java.security.PrivilegedExceptionAction ; 26 import java.security.Principal ; 27 import java.security.AccessController ; 28 import java.security.PrivilegedActionException ; 29 import java.util.HashMap ; 30 import java.lang.reflect.UndeclaredThrowableException ; 31 32 import javax.security.auth.Subject ; 33 import javax.security.jacc.PolicyContext ; 34 import javax.security.jacc.PolicyContextException ; 35 36 import org.jboss.security.SecurityAssociation; 37 import org.jboss.security.RunAsIdentity; 38 import org.jboss.security.SecurityConstants; 39 import org.jboss.security.SecurityContext; 40 41 46 class SecurityActions 47 { 48 interface PrincipalInfoAction 49 { 50 PrincipalInfoAction PRIVILEGED = new PrincipalInfoAction() 51 { 52 public void push(final Principal principal, final Object credential, 53 final Subject subject) 54 { 55 AccessController.doPrivileged( 56 new PrivilegedAction () 57 { 58 public Object run() 59 { 60 SecurityAssociation.pushSubjectContext(subject, principal, credential); 61 return null; 62 } 63 } 64 ); 65 } 66 public void dup() 67 { 68 AccessController.doPrivileged( 69 new PrivilegedAction () 70 { 71 public Object run() 72 { 73 SecurityAssociation.dupSubjectContext(); 74 return null; 75 } 76 } 77 ); 78 } 79 public void pop() 80 { 81 AccessController.doPrivileged( 82 new PrivilegedAction () 83 { 84 public Object run() 85 { 86 SecurityAssociation.popSubjectContext(); 87 return null; 88 } 89 } 90 ); 91 } 92 }; 93 94 PrincipalInfoAction NON_PRIVILEGED = new PrincipalInfoAction() 95 { 96 public void push(Principal principal, Object credential, Subject subject) 97 { 98 SecurityAssociation.pushSubjectContext(subject, principal, credential); 99 } 100 public void dup() 101 { 102 SecurityAssociation.dupSubjectContext(); 103 } 104 public void pop() 105 { 106 SecurityAssociation.popSubjectContext(); 107 } 108 }; 109 110 void push(Principal principal, Object credential, Subject subject); 111 void dup(); 112 void pop(); 113 } 114 115 interface RunAsIdentityActions 116 { 117 RunAsIdentityActions PRIVILEGED = new RunAsIdentityActions() 118 { 119 private final PrivilegedAction peekAction = new PrivilegedAction () 120 { 121 public Object run() 122 { 123 return SecurityAssociation.peekRunAsIdentity(); 124 } 125 }; 126 127 private final PrivilegedAction popAction = new PrivilegedAction () 128 { 129 public Object run() 130 { 131 return SecurityAssociation.popRunAsIdentity(); 132 } 133 }; 134 135 public RunAsIdentity peek() 136 { 137 return (RunAsIdentity)AccessController.doPrivileged(peekAction); 138 } 139 140 public void push(final RunAsIdentity id) 141 { 142 AccessController.doPrivileged( 143 new PrivilegedAction () 144 { 145 public Object run() 146 { 147 SecurityAssociation.pushRunAsIdentity(id); 148 return null; 149 } 150 } 151 ); 152 } 153 154 public RunAsIdentity pop() 155 { 156 return (RunAsIdentity)AccessController.doPrivileged(popAction); 157 } 158 }; 159 160 RunAsIdentityActions NON_PRIVILEGED = new RunAsIdentityActions() 161 { 162 public RunAsIdentity peek() 163 { 164 return SecurityAssociation.peekRunAsIdentity(); 165 } 166 167 public void push(RunAsIdentity id) 168 { 169 SecurityAssociation.pushRunAsIdentity(id); 170 } 171 172 public RunAsIdentity pop() 173 { 174 return SecurityAssociation.popRunAsIdentity(); 175 } 176 }; 177 178 RunAsIdentity peek(); 179 180 void push(RunAsIdentity id); 181 182 RunAsIdentity pop(); 183 } 184 185 interface ContextInfoActions 186 { 187 static final String EX_KEY = "org.jboss.security.exception"; 188 ContextInfoActions PRIVILEGED = new ContextInfoActions() 189 { 190 private final PrivilegedAction exAction = new PrivilegedAction () 191 { 192 public Object run() 193 { 194 return SecurityAssociation.getContextInfo(EX_KEY); 195 } 196 }; 197 public Exception getContextException() 198 { 199 return (Exception )AccessController.doPrivileged(exAction); 200 } 201 }; 202 203 ContextInfoActions NON_PRIVILEGED = new ContextInfoActions() 204 { 205 public Exception getContextException() 206 { 207 return (Exception )SecurityAssociation.getContextInfo(EX_KEY); 208 } 209 }; 210 211 Exception getContextException(); 212 } 213 214 interface PolicyContextActions 215 { 216 217 static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container"; 218 PolicyContextActions PRIVILEGED = new PolicyContextActions() 219 { 220 private final PrivilegedExceptionAction exAction = new PrivilegedExceptionAction () 221 { 222 public Object run() throws Exception 223 { 224 return (Subject ) PolicyContext.getContext(SUBJECT_CONTEXT_KEY); 225 } 226 }; 227 public Subject getContextSubject() 228 throws PolicyContextException 229 { 230 try 231 { 232 return (Subject ) AccessController.doPrivileged(exAction); 233 } 234 catch(PrivilegedActionException e) 235 { 236 Exception ex = e.getException(); 237 if( ex instanceof PolicyContextException ) 238 throw (PolicyContextException ) ex; 239 else 240 throw new UndeclaredThrowableException (ex); 241 } 242 } 243 }; 244 245 PolicyContextActions NON_PRIVILEGED = new PolicyContextActions() 246 { 247 public Subject getContextSubject() 248 throws PolicyContextException 249 { 250 return (Subject ) PolicyContext.getContext(SUBJECT_CONTEXT_KEY); 251 } 252 }; 253 254 Subject getContextSubject() 255 throws PolicyContextException ; 256 } 257 258 static ClassLoader getContextClassLoader() 259 { 260 return TCLAction.UTIL.getContextClassLoader(); 261 } 262 263 static void setContextClassLoader(ClassLoader loader) 264 { 265 TCLAction.UTIL.setContextClassLoader(loader); 266 } 267 268 static void pushSubjectContext(Principal principal, Object credential, 269 Subject subject) 270 { 271 if(System.getSecurityManager() == null) 272 { 273 PrincipalInfoAction.NON_PRIVILEGED.push(principal, credential, subject); 274 } 275 else 276 { 277 PrincipalInfoAction.PRIVILEGED.push(principal, credential, subject); 278 } 279 } 280 static void dupSubjectContext() 281 { 282 if(System.getSecurityManager() == null) 283 { 284 PrincipalInfoAction.NON_PRIVILEGED.dup(); 285 } 286 else 287 { 288 PrincipalInfoAction.PRIVILEGED.dup(); 289 } 290 } 291 static void popSubjectContext() 292 { 293 if(System.getSecurityManager() == null) 294 { 295 PrincipalInfoAction.NON_PRIVILEGED.pop(); 296 } 297 else 298 { 299 PrincipalInfoAction.PRIVILEGED.pop(); 300 } 301 } 302 303 static RunAsIdentity peekRunAsIdentity() 304 { 305 if(System.getSecurityManager() == null) 306 { 307 return RunAsIdentityActions.NON_PRIVILEGED.peek(); 308 } 309 else 310 { 311 return RunAsIdentityActions.PRIVILEGED.peek(); 312 } 313 } 314 315 static void pushRunAsIdentity(RunAsIdentity principal) 316 { 317 if(System.getSecurityManager() == null) 318 { 319 RunAsIdentityActions.NON_PRIVILEGED.push(principal); 320 } 321 else 322 { 323 RunAsIdentityActions.PRIVILEGED.push(principal); 324 } 325 } 326 327 static RunAsIdentity popRunAsIdentity() 328 { 329 if(System.getSecurityManager() == null) 330 { 331 return RunAsIdentityActions.NON_PRIVILEGED.pop(); 332 } 333 else 334 { 335 return RunAsIdentityActions.PRIVILEGED.pop(); 336 } 337 } 338 339 static Exception getContextException() 340 { 341 if(System.getSecurityManager() == null) 342 { 343 return ContextInfoActions.NON_PRIVILEGED.getContextException(); 344 } 345 else 346 { 347 return ContextInfoActions.PRIVILEGED.getContextException(); 348 } 349 } 350 351 static Subject getContextSubject() 352 throws PolicyContextException 353 { 354 if(System.getSecurityManager() == null) 355 { 356 return PolicyContextActions.NON_PRIVILEGED.getContextSubject(); 357 } 358 else 359 { 360 return PolicyContextActions.PRIVILEGED.getContextSubject(); 361 } 362 } 363 364 interface TCLAction 365 { 366 class UTIL 367 { 368 static TCLAction getTCLAction() 369 { 370 return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED; 371 } 372 373 static ClassLoader getContextClassLoader() 374 { 375 return getTCLAction().getContextClassLoader(); 376 } 377 378 static ClassLoader getContextClassLoader(Thread thread) 379 { 380 return getTCLAction().getContextClassLoader(thread); 381 } 382 383 static void setContextClassLoader(ClassLoader cl) 384 { 385 getTCLAction().setContextClassLoader(cl); 386 } 387 388 static void setContextClassLoader(Thread thread, ClassLoader cl) 389 { 390 getTCLAction().setContextClassLoader(thread, cl); 391 } 392 } 393 394 TCLAction NON_PRIVILEGED = new TCLAction() 395 { 396 public ClassLoader getContextClassLoader() 397 { 398 return Thread.currentThread().getContextClassLoader(); 399 } 400 401 public ClassLoader getContextClassLoader(Thread thread) 402 { 403 return thread.getContextClassLoader(); 404 } 405 406 public void setContextClassLoader(ClassLoader cl) 407 { 408 Thread.currentThread().setContextClassLoader(cl); 409 } 410 411 public void setContextClassLoader(Thread thread, ClassLoader cl) 412 { 413 thread.setContextClassLoader(cl); 414 } 415 }; 416 417 TCLAction PRIVILEGED = new TCLAction() 418 { 419 private final PrivilegedAction getTCLPrivilegedAction = new PrivilegedAction () 420 { 421 public Object run() 422 { 423 return Thread.currentThread().getContextClassLoader(); 424 } 425 }; 426 427 public ClassLoader getContextClassLoader() 428 { 429 return (ClassLoader )AccessController.doPrivileged(getTCLPrivilegedAction); 430 } 431 432 public ClassLoader getContextClassLoader(final Thread thread) 433 { 434 return (ClassLoader )AccessController.doPrivileged(new PrivilegedAction () 435 { 436 public Object run() 437 { 438 return thread.getContextClassLoader(); 439 } 440 }); 441 } 442 443 public void setContextClassLoader(final ClassLoader cl) 444 { 445 AccessController.doPrivileged( 446 new PrivilegedAction () 447 { 448 public Object run() 449 { 450 Thread.currentThread().setContextClassLoader(cl); 451 return null; 452 } 453 } 454 ); 455 } 456 457 public void setContextClassLoader(final Thread thread, final ClassLoader cl) 458 { 459 AccessController.doPrivileged( 460 new PrivilegedAction () 461 { 462 public Object run() 463 { 464 thread.setContextClassLoader(cl); 465 return null; 466 } 467 } 468 ); 469 } 470 }; 471 472 ClassLoader getContextClassLoader(); 473 474 ClassLoader getContextClassLoader(Thread thread); 475 476 void setContextClassLoader(ClassLoader cl); 477 478 void setContextClassLoader(Thread thread, ClassLoader cl); 479 } 480 481 private static class GetSecurityContextAction implements PrivilegedAction 482 { 483 private String securityDomain; 484 GetSecurityContextAction(String sd) 485 { 486 this.securityDomain = sd; 487 } 488 public Object run() 489 { 490 String sc = SecurityConstants.SECURITY_CONTEXT; 491 HashMap map = (HashMap )SecurityAssociation.getContextInfo(sc); 492 if(map == null) 493 { 494 map = new HashMap (); 495 SecurityAssociation.setContextInfo(sc, map); 496 } 497 SecurityAssociation.setContextInfo(sc, map); 498 return map.get(this.securityDomain); 499 } 500 } 501 502 private static class SetSecurityContextAction implements PrivilegedAction 503 { 504 private SecurityContext securityContext; 505 private String securityDomain; 506 SetSecurityContextAction(SecurityContext sc, String sd) 507 { 508 this.securityContext = sc; 509 this.securityDomain = sd; 510 } 511 512 public Object run() 513 { 514 String sc = SecurityConstants.SECURITY_CONTEXT; 515 HashMap map = (HashMap )SecurityAssociation.getContextInfo(sc); 516 if(map == null) 517 { 518 map = new HashMap (); 519 SecurityAssociation.setContextInfo(sc, map); 520 } 521 map.put(securityDomain, securityContext); 522 SecurityAssociation.setContextInfo(sc, map); 523 return null; 524 } 525 } 526 527 private static class ClearSecurityContextAction implements PrivilegedAction 528 { 529 private String securityDomain; 530 ClearSecurityContextAction(String sd) 531 { 532 this.securityDomain = sd; 533 } 534 public Object run() 535 { 536 String sc = SecurityConstants.SECURITY_CONTEXT; 537 HashMap map = (HashMap )SecurityAssociation.getContextInfo(sc); 538 if(map == null) 539 { 540 map = new HashMap (); 541 SecurityAssociation.setContextInfo(sc, map); 542 } 543 if(map.containsKey(securityDomain)) 544 map.remove(securityDomain); 545 546 SecurityAssociation.setContextInfo(sc, map); 547 return null; 548 } 549 } 550 551 static void clearSecurityContext(String securityDomain) 552 { 553 ClearSecurityContextAction action = new ClearSecurityContextAction(securityDomain); 554 AccessController.doPrivileged(action); 555 } 556 557 static SecurityContext getSecurityContext(String securityDomain) 558 { 559 GetSecurityContextAction action = new GetSecurityContextAction(securityDomain); 560 return (SecurityContext)AccessController.doPrivileged(action); 561 } 562 563 static void setSecurityContext(SecurityContext sc, String securityDomain) 564 { 565 SetSecurityContextAction action = new SetSecurityContextAction(sc,securityDomain); 566 AccessController.doPrivileged(action); 567 } 568 } 569 | Popular Tags |