1 23 package com.sun.enterprise.security.application; 24 25 import java.io.File ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.net.URLEncoder ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.Set ; 31 import java.util.HashSet ; 32 import java.util.Hashtable ; 33 import java.util.Map ; 34 import java.util.HashMap ; 35 import java.util.WeakHashMap ; 36 import java.lang.reflect.Method ; 37 import java.util.Collections ; 38 import java.security.Permission ; 39 import java.security.Principal ; 40 import java.security.AccessController ; 41 import java.security.AccessControlContext ; 42 import java.security.CodeSource ; 43 import javax.security.auth.Subject ; 44 import javax.security.auth.SubjectDomainCombiner ; 45 import java.security.ProtectionDomain ; 46 import java.security.Permissions ; 47 import java.security.PrivilegedAction ; 48 import java.security.PrivilegedExceptionAction ; 49 import java.net.URL ; 50 import java.security.Policy ; 51 import javax.security.jacc.EJBMethodPermission ; 52 import javax.security.jacc.EJBRoleRefPermission ; 53 import javax.security.jacc.PolicyConfigurationFactory ; 54 import javax.security.jacc.PolicyConfiguration ; 55 import javax.security.jacc.PolicyContext ; 56 import javax.security.jacc.PolicyContextException ; 57 import com.sun.ejb.Invocation; 58 import com.sun.enterprise.ComponentInvocation; 59 import com.sun.enterprise.InvocationManager; 60 import com.sun.enterprise.InvocationException; 61 import com.sun.enterprise.Switch; 62 import com.sun.enterprise.SecurityManager; 63 import com.sun.enterprise.deployment.*; 64 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapperFactory; 65 import com.sun.enterprise.deployment.interfaces.SecurityRoleMapperFactoryMgr; 66 import com.sun.enterprise.deployment.web.SecurityRoleReference; 67 import com.sun.enterprise.security.SecurityContext; 68 import com.sun.enterprise.security.factory.SecurityManagerFactory; 69 import com.sun.enterprise.security.factory.FactoryForSecurityManagerFactory; 70 import com.sun.enterprise.security.factory.FactoryForSecurityManagerFactoryImpl; 71 import com.sun.enterprise.security.auth.LoginContextDriver; 72 import com.sun.enterprise.security.authorize.PolicyContextHandlerImpl; 73 import com.sun.enterprise.security.LoginException; 74 import com.sun.enterprise.security.authorize.*; 75 import com.sun.enterprise.security.audit.AuditManager; 76 import com.sun.enterprise.security.audit.AuditManagerFactory; 77 import com.sun.enterprise.security.CachedPermission; 78 import com.sun.enterprise.security.CachedPermissionImpl; 79 import com.sun.enterprise.security.PermissionCache; 80 import com.sun.enterprise.security.PermissionCacheFactory; 81 import com.sun.enterprise.util.LocalStringManagerImpl; 82 import com.sun.enterprise.util.TypeUtil; 83 84 import java.util.logging.*; 86 import com.sun.logging.LogDomains; 87 88 import java.security.ProtectionDomain ; 89 90 98 public final class EJBSecurityManager implements SecurityManager { 99 100 private static Logger _logger=null; 101 static { 102 _logger=LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 103 } 104 private static LocalStringManagerImpl localStrings = 105 new LocalStringManagerImpl(EJBSecurityManager.class); 106 107 private static AuditManager auditManager = 108 AuditManagerFactory.getAuditManagerInstance(); 109 110 private static final PolicyContextHandlerImpl pcHandlerImpl = 111 (PolicyContextHandlerImpl)PolicyContextHandlerImpl.getInstance(); 112 113 private static SecurityRoleMapperFactory roleMapperFactory = 114 SecurityRoleMapperFactoryMgr.getFactory(); 115 116 private EjbDescriptor deploymentDescriptor = null; 117 private Switch theSwitch; 118 private RunAsIdentityDescriptor runAs = null; 120 121 private static PolicyConfigurationFactory pcf = null; 123 private String ejbName = null; 124 private String contextId = null; 127 private String codebase = null; 128 private CodeSource codesource = null; 129 private String realmName = null; 130 private Hashtable cacheRoleToPerm = new Hashtable (); 133 134 private Map cacheProtectionDomain = 141 Collections.synchronizedMap(new WeakHashMap ()); 142 private Map protectionDomainCache = 143 Collections.synchronizedMap(new WeakHashMap ()); 144 145 private Map accessControlContextCache = 146 Collections.synchronizedMap(new WeakHashMap ()); 147 148 private PermissionCache uncheckedMethodPermissionCache = null; 149 150 private Policy policy = null; 151 152 private static CodeSource managerCodeSource = 153 EJBSecurityManager.class.getProtectionDomain().getCodeSource(); 154 155 private boolean isMdb; 156 157 162 public static EJBSecurityManager getInstance(Descriptor des) 163 throws Exception { 164 return new EJBSecurityManager(des); 165 } 166 170 private EJBSecurityManager(Descriptor ejbDescriptor) 171 throws Exception 172 { 173 if(ejbDescriptor == null || !(ejbDescriptor instanceof EjbDescriptor)) { 174 throw new IllegalArgumentException ("Illegal Deployment Descriptor Information."); 175 } 176 this.deploymentDescriptor = (EjbDescriptor) ejbDescriptor; 177 178 this.isMdb = (EjbMessageBeanDescriptor.TYPE.equals( 179 this.deploymentDescriptor.getType())); 180 181 policy = Policy.getPolicy(); 183 184 boolean runas = !(deploymentDescriptor.getUsesCallerIdentity()); 185 if (runas){ 186 runAs = deploymentDescriptor.getRunAsIdentity(); 187 188 if(runAs != null) { 191 if (_logger.isLoggable(Level.FINE)){ 192 _logger.log(Level.FINE,deploymentDescriptor.getEjbClassName() + 193 " will run-as: " + runAs.getPrincipal() + 194 " (" + runAs.getRoleName() + ")"); 195 } 196 } 197 } 198 199 theSwitch = Switch.getSwitch(); 200 initialize(); 201 } 202 private static CodeSource getApplicationCodeSource(String pcid) throws Exception { 203 CodeSource result = null; 204 String archiveURI = "file:///" + pcid.replace(' ', '_'); 205 try{ 206 java.net.URI uri = null; 207 try{ 208 uri = new java.net.URI (archiveURI); 209 if(uri != null){ 210 result = new CodeSource (uri.toURL(), 211 (java.security.cert.Certificate []) null); 212 } 213 } catch(java.net.URISyntaxException use){ 214 _logger.log(Level.SEVERE,"JACC: Error Creating URI ", use); 216 throw new RuntimeException (use); 217 } 218 219 } catch(java.net.MalformedURLException mue){ 220 _logger.log(Level.SEVERE,"JACC: ejbsm.codesourceerror", mue); 222 throw new RuntimeException (mue); 223 } 224 return result; 225 } 226 227 private static PolicyConfigurationFactory getPolicyFactory() 229 throws PolicyContextException { 230 synchronized (EJBSecurityManager.class) { 231 if (pcf == null) { 232 try { 233 pcf = PolicyConfigurationFactory.getPolicyConfigurationFactory(); 234 } catch(ClassNotFoundException cnfe){ 235 _logger.severe("jaccfactory.notfound"); 236 throw new PolicyContextException (cnfe); 237 } catch(PolicyContextException pce){ 238 _logger.severe("jaccfactory.notfound"); 239 throw pce; 240 } 241 } 242 } 243 return pcf; 244 } 245 246 public boolean getUsesCallerIdentity() { 247 return (runAs == null); 248 } 249 250 public static void loadPolicyConfiguration(EjbDescriptor eDescriptor) throws Exception 251 { 252 String pcid = getContextID(eDescriptor); 253 String appName = eDescriptor.getApplication().getRegistrationName(); 254 roleMapperFactory.setAppNameForContext(appName, pcid); 255 boolean inService = getPolicyFactory().inService(pcid); 256 257 264 if (!inService) { 265 266 268 convertEJBMethodPermissions(eDescriptor,pcid); 269 convertEJBRoleReferences(eDescriptor,pcid); 270 271 if(_logger.isLoggable(Level.FINE)){ 272 _logger.fine("JACC: policy translated for policy context:" +pcid); 273 } 274 } 275 } 276 277 public static String getContextID(EjbDescriptor ejbDesc) { 278 return getContextID(ejbDesc.getEjbBundleDescriptor()); 279 } 280 281 public static String getContextID(EjbBundleDescriptor ejbBundleDesc) { 282 String cid = null; 283 if (ejbBundleDesc != null ) { 284 cid = ejbBundleDesc.getApplication().getRegistrationName() + 285 '/' + ejbBundleDesc.getUniqueFriendlyId(); 286 } 287 return cid; 288 } 289 290 291 private void initialize() throws Exception { 292 contextId = getContextID(deploymentDescriptor); 293 String appName = deploymentDescriptor.getApplication().getRegistrationName(); 294 roleMapperFactory.setAppNameForContext(appName, contextId); 295 codesource = getApplicationCodeSource(contextId); 296 ejbName = deploymentDescriptor.getName(); 297 298 realmName= deploymentDescriptor.getApplication().getRealm(); 299 300 if (realmName == null) { 301 Set iorConfigs = deploymentDescriptor.getIORConfigurationDescriptors(); 302 Iterator iter = iorConfigs.iterator(); 304 if (iter != null) { 305 while (iter.hasNext()) { 308 EjbIORConfigurationDescriptor iorConfig = 309 (EjbIORConfigurationDescriptor)iter.next(); 310 realmName = iorConfig.getRealmName(); 311 } 312 } 313 } 314 315 if(_logger.isLoggable(Level.FINE)){ 316 _logger.fine("JACC: Context id (id under which all EJB's in application will be created) = " + contextId); 317 _logger.fine("Codebase (module id for ejb "+ejbName+") = "+ codebase); 318 } 319 320 addEJBRoleReferenceToCache(deploymentDescriptor); 322 323 uncheckedMethodPermissionCache = 325 PermissionCacheFactory.createPermissionCache( 326 this.contextId, this.codesource, 327 EJBMethodPermission .class, 328 this.ejbName); 329 } 330 331 339 private static void 340 convertEJBRoleReferences(EjbDescriptor eDescriptor, String pcid) 341 throws PolicyContextException { 342 343 PolicyConfiguration pc = 344 getPolicyFactory().getPolicyConfiguration(pcid, false); 345 346 assert pc != null; 347 348 if (pc != null) { 349 350 String eName = eDescriptor.getName(); 351 352 Iterator iroleref = eDescriptor.getRoleReferences().iterator(); 353 while(iroleref.hasNext()){ 354 SecurityRoleReference roleRef = 355 (SecurityRoleReference) iroleref.next(); 356 String rolename = roleRef.getRolename(); 357 EJBRoleRefPermission ejbrr = 358 new EJBRoleRefPermission (eName, rolename); 359 String rolelink = roleRef.getSecurityRoleLink().getName(); 360 361 pc.addToRole(rolelink, ejbrr); 362 363 if(_logger.isLoggable(Level.FINE)){ 364 _logger.fine("JACC: Converting role-ref -> "+roleRef.toString()+ 365 " to permission with name("+ejbrr.getName()+ 366 ") and actions ("+ejbrr.getActions()+ 367 ")" + "mapped to role ("+rolelink+")"); 368 } 369 } 370 } 371 } 372 373 377 private void addEJBRoleReferenceToCache(EjbDescriptor eDescriptor) { 378 379 String eName = eDescriptor.getName(); 380 381 Iterator iroleref = eDescriptor.getRoleReferences().iterator(); 382 while(iroleref.hasNext()){ 383 SecurityRoleReference roleRef = 384 (SecurityRoleReference) iroleref.next(); 385 String rolename = roleRef.getRolename(); 386 EJBRoleRefPermission ejbrr = 387 new EJBRoleRefPermission (eName, rolename); 388 String rolelink = roleRef.getSecurityRoleLink().getName(); 389 390 cacheRoleToPerm.put(eName+"_"+rolename, ejbrr); 391 392 if(_logger.isLoggable(Level.FINE)){ 393 _logger.fine("JACC: Converting role-ref -> "+roleRef.toString()+ 394 " to permission with name("+ejbrr.getName()+ 395 ") and actions ("+ejbrr.getActions()+ 396 ")" + "mapped to role ("+rolelink+")"); 397 } 398 } 399 } 400 401 private static HashMap addToRolePermissionsTable(HashMap table, 403 MethodPermission mp, 404 EJBMethodPermission ejbmp) { 405 if (mp.isRoleBased()){ 406 if (table == null) { 407 table = new HashMap (); 408 } 409 String roleName = mp.getRole().getName(); 410 Permissions rolePermissions = 411 (Permissions ) table.get(roleName); 412 if (rolePermissions == null) { 413 rolePermissions = new Permissions (); 414 table.put(roleName,rolePermissions); 415 } 416 rolePermissions.add(ejbmp); 417 if(_logger.isLoggable(Level.FINE)){ 418 _logger.fine("JACC DD conversion: EJBMethodPermission ->("+ 419 ejbmp.getName()+" "+ejbmp.getActions()+ 420 ")protected by role -> "+roleName); 421 } 422 } 423 return table; 424 } 425 426 private static Permissions addToUncheckedPermissions(Permissions permissions, 428 MethodPermission mp, 429 EJBMethodPermission ejbmp) { 430 if(mp.isUnchecked()){ 431 if (permissions == null) { 432 permissions = new Permissions (); 433 } 434 permissions.add(ejbmp); 435 if(_logger.isLoggable(Level.FINE)){ 436 _logger.fine("JACC DD conversion: EJBMethodPermission ->(" 437 +ejbmp.getName()+" "+ejbmp.getActions()+ 438 ") is (unchecked)"); 439 } 440 } 441 return permissions; 442 } 443 444 private static Permissions addToExcludedPermissions(Permissions permissions, 446 MethodPermission mp, 447 EJBMethodPermission ejbmp) { 448 if(mp.isExcluded()){ 449 if (permissions == null) { 450 permissions = new Permissions (); 451 } 452 permissions.add(ejbmp); 453 if(_logger.isLoggable(Level.FINE)){ 454 _logger.fine("JACC DD conversion: EJBMethodPermission ->(" 455 +ejbmp.getName()+" "+ejbmp.getActions()+ 456 ") is (excluded)"); 457 } 458 } 459 return permissions; 460 } 461 462 485 private static void 486 convertEJBMethodPermissions (EjbDescriptor eDescriptor, String pcid) 487 throws PolicyContextException { 488 489 PolicyConfiguration pc = 490 getPolicyFactory().getPolicyConfiguration(pcid, false); 491 492 assert pc != null; 493 494 if (pc != null) { 495 496 String eName = eDescriptor.getName(); 497 498 Permissions uncheckedPermissions = null; 499 Permissions excludedPermissions = null; 500 HashMap rolePermissionsTable = null; 501 502 EJBMethodPermission ejbmp = null; 503 504 Map mpMap = eDescriptor.getMethodPermissionsFromDD(); 506 if (mpMap != null) { 507 508 Iterator mpIt = mpMap.keySet().iterator(); 509 510 while(mpIt.hasNext()) { 511 512 MethodPermission mp = (MethodPermission )mpIt.next(); 513 514 Iterator mdIt = ((ArrayList ) mpMap.get(mp)).iterator(); 515 516 while(mdIt.hasNext()) { 517 518 MethodDescriptor md = (MethodDescriptor) mdIt.next(); 519 520 String mthdName = md.getName(); 521 String mthdIntf = md.getEjbClassSymbol(); 522 String mthdParams[] = md.getStyle() == 3 ? 523 md.getParameterClassNames() : null; 524 525 ejbmp = new EJBMethodPermission (eName,mthdName.equals("*") ? 526 null : mthdName, 527 mthdIntf,mthdParams); 528 rolePermissionsTable = 529 addToRolePermissionsTable(rolePermissionsTable,mp,ejbmp); 530 531 uncheckedPermissions = 532 addToUncheckedPermissions(uncheckedPermissions,mp,ejbmp); 533 534 excludedPermissions = 535 addToExcludedPermissions(excludedPermissions,mp,ejbmp); 536 } 537 } 538 } 539 540 544 Iterator mdIt = eDescriptor.getMethodDescriptors().iterator(); 545 while(mdIt.hasNext()) { 546 547 MethodDescriptor md = (MethodDescriptor)mdIt.next(); 548 Method mthd = md.getMethod(eDescriptor); 549 String mthdIntf = md.getEjbClassSymbol(); 550 551 if(mthd == null){ 552 continue; 553 } 554 555 if(mthdIntf == null || mthdIntf.equals("")) { 556 _logger.severe("MethodDescriptor interface not defined - "+ 557 " ejbName: "+eName+ 558 " methodName: " +md.getName()+ 559 " methodParams: " +md.getParameterClassNames()); 560 continue; 561 } 562 563 ejbmp = new EJBMethodPermission (eName,mthdIntf,mthd); 564 565 Iterator mpIt = eDescriptor.getMethodPermissionsFor(md).iterator(); 566 567 while(mpIt.hasNext()) { 568 569 MethodPermission mp = (MethodPermission ) mpIt.next(); 570 571 rolePermissionsTable = 572 addToRolePermissionsTable(rolePermissionsTable,mp,ejbmp); 573 574 uncheckedPermissions = 575 addToUncheckedPermissions(uncheckedPermissions,mp,ejbmp); 576 577 excludedPermissions = 578 addToExcludedPermissions(excludedPermissions,mp,ejbmp); 579 } 580 } 581 582 if (uncheckedPermissions != null) { 583 pc.addToUncheckedPolicy(uncheckedPermissions); 584 } 585 if (excludedPermissions != null) { 586 pc.addToExcludedPolicy(excludedPermissions); 587 } 588 if (rolePermissionsTable != null) { 589 590 Iterator roleIt = rolePermissionsTable.keySet().iterator(); 591 592 while (roleIt.hasNext()) { 593 String roleName = (String ) roleIt.next(); 594 pc.addToRole(roleName, 595 (Permissions )rolePermissionsTable.get(roleName)); 596 } 597 } 598 } 599 } 600 601 private ProtectionDomain getCachedProtectionDomain(Set principalSet, 602 boolean applicationCodeSource) { 603 604 ProtectionDomain prdm = null; 605 Principal [] principals = null; 606 607 613 614 CodeSource cs = null; 615 616 if (applicationCodeSource) { 617 prdm = (ProtectionDomain )cacheProtectionDomain.get(principalSet); 618 cs = codesource; 619 } else { 620 prdm = (ProtectionDomain )protectionDomainCache.get(principalSet); 621 cs = managerCodeSource; 622 } 623 624 if(prdm == null) { 625 626 principals = (principalSet == null ? null : 627 (Principal []) principalSet.toArray(new Principal [0])); 628 629 prdm = new ProtectionDomain (cs, null, null, principals); 630 631 if (applicationCodeSource) { 632 cacheProtectionDomain.put(new HashSet (principalSet),prdm); 634 } else { 635 protectionDomainCache.put(new HashSet (principalSet),prdm); 637 } 638 639 _logger.fine("JACC: new ProtectionDomain added to cache"); 640 641 } 642 643 if(_logger.isLoggable(Level.FINE)){ 644 if (principalSet == null) { 645 _logger.fine("JACC: returning cached ProtectionDomain PrincipalSet: null"); 646 } else { 647 StringBuffer pBuf = null; 648 principals = (Principal [])principalSet.toArray(new Principal [0]); 649 for (int i=0; i<principals.length; i++) { 650 if (i == 0) pBuf = new StringBuffer (principals[i].toString()); 651 else pBuf.append(" " + principals[i].toString()); 652 } 653 _logger.fine("JACC: returning cached ProtectionDomain - CodeSource: (" 654 + cs + ") PrincipalSet: "+pBuf); 655 } 656 } 657 658 return prdm; 659 } 660 661 662 670 public boolean authorize(Invocation inv) { 671 672 if (inv.auth != null) { 673 return inv.auth.booleanValue(); 674 } 675 676 boolean ret=false; 677 678 CachedPermission cp = null; 679 Permission ejbmp = null; 680 681 if (inv.invocationInfo == null || inv.invocationInfo.cachedPermission == null) { 682 ejbmp = new EJBMethodPermission (ejbName,inv.getMethodInterface(),inv.method); 683 cp = new CachedPermissionImpl(uncheckedMethodPermissionCache,ejbmp); 684 if (inv.invocationInfo != null) { 685 inv.invocationInfo.cachedPermission = cp; 686 if (_logger.isLoggable(Level.FINE)){ 687 _logger.fine("JACC: permission initialized in InvocationInfo: EJBMethodPermission (Name) = "+ ejbmp.getName() + " (Action) = "+ ejbmp.getActions()); 688 } 689 } 690 } else { 691 cp = inv.invocationInfo.cachedPermission; 692 ejbmp = cp.getPermission(); 693 } 694 695 String caller = null; 696 SecurityContext sc = null; 697 698 ret = cp.checkPermission(); 699 700 if (!ret) { 701 702 pcHandlerImpl.getHandlerData().setInvocation(inv); 703 704 sc = SecurityContext.getCurrent(); 705 706 Set principalSet = sc.getPrincipalSet(); 707 708 ProtectionDomain prdm = getCachedProtectionDomain(principalSet,true); 709 710 try { 711 String oldContextId = setPolicyContext(this.contextId); 713 714 try { 715 716 ret = policy.implies(prdm, ejbmp); 717 718 } catch (SecurityException se){ 719 _logger.log(Level.SEVERE,"JACC: Unexpected security exception on access decision",se); 720 ret = false; 721 } catch (Throwable t) { 722 _logger.log(Level.SEVERE,"JACC: Unexpected exception on access decision",t); 723 ret = false; 724 } finally { 725 resetPolicyContext(oldContextId,this.contextId); 726 } 727 728 } catch (Throwable t) { 729 _logger.log(Level.SEVERE,"JACC: Unexpected exception manipulating policy context",t); 730 ret = false; 731 } 732 } 733 734 inv.auth = (ret) ? Boolean.TRUE : Boolean.FALSE; 735 736 if (auditManager.isAuditOn()){ 737 if (sc == null) { 738 sc = SecurityContext.getCurrent(); 739 } 740 caller = sc.getCallerPrincipal().getName(); 741 auditManager.ejbInvocation(caller, ejbName, inv.method.toString(), ret); 742 } 743 744 if (ret && inv.isWebService && !inv.preInvokeDone) { 745 preInvoke(inv); 746 } 747 748 if(_logger.isLoggable(Level.FINE)){ 749 _logger.fine("JACC: Access Control Decision Result: " +ret + " EJBMethodPermission (Name) = "+ ejbmp.getName() + " (Action) = "+ ejbmp.getActions() + " (Caller) = " + caller); 750 } 751 752 return ret; 753 } 754 755 760 private boolean areMethodPermissionsSet(){ 761 boolean empty = 762 deploymentDescriptor.getPermissionedMethodsByPermission().isEmpty(); 763 return !empty; 764 } 765 766 772 public void preInvoke (ComponentInvocation inv){ 773 boolean isWebService = false; 774 if (inv instanceof Invocation) { 775 isWebService = ((Invocation)inv).isWebService; 776 } 777 778 if ((!isWebService || (inv.auth != null && inv.auth.booleanValue())) 781 && !inv.preInvokeDone) { 782 if (isMdb) { 783 SecurityContext.setUnauthenticatedContext(); 784 } 785 if (runAs != null){ 786 inv.setOldSecurityContext(SecurityContext.getCurrent()); 787 loginForRunAs(); 788 } 789 inv.preInvokeDone = true; 790 } 791 } 792 793 798 public void postInvoke (ComponentInvocation inv){ 799 if (runAs != null && inv.preInvokeDone){ 800 final ComponentInvocation finv = inv; 801 AccessController.doPrivileged(new PrivilegedAction () { 802 public Object run() { 803 SecurityContext.setCurrent (finv.getOldSecurityContext()); 804 return null; 805 } 806 }); 807 } 808 } 809 810 817 private void loginForRunAs(){ 818 AccessController.doPrivileged(new PrivilegedAction () { 819 public Object run() { 820 LoginContextDriver.loginPrincipal (runAs.getPrincipal(), realmName); 821 return null; 822 } 823 }); 824 } 825 826 827 834 public boolean isCallerInRole(String role) { 835 838 839 boolean ret= false; 840 841 if(_logger.isLoggable(Level.FINE)){ 842 _logger.entering("EJBSecurityManager", "isCallerInRole", role); 843 844 } 845 EJBRoleRefPermission ejbrr = 846 (EJBRoleRefPermission )cacheRoleToPerm.get(ejbName+"_"+role); 847 848 if(ejbrr == null){ 849 ejbrr = new EJBRoleRefPermission (ejbName, role); 850 } 851 852 SecurityContext sc; 853 if (runAs != null) { 854 InvocationManager im = theSwitch.getInvocationManager(); 855 ComponentInvocation ci = im.getCurrentInvocation(); 856 sc = ci.getOldSecurityContext(); 857 } else { 858 sc = SecurityContext.getCurrent(); 859 } 860 Set principalSet = null; 861 if (sc != null) principalSet = sc.getPrincipalSet(); 862 863 ProtectionDomain prdm = getCachedProtectionDomain(principalSet,true); 864 865 try { 866 867 ret = policy.implies(prdm, ejbrr); 868 } catch(SecurityException se){ 869 _logger.log(Level.SEVERE,"JACC: Unexpected security exception isCallerInRole",se); 870 ret = false; 871 } catch (Throwable t) { 872 _logger.log(Level.SEVERE,"JACC: Unexpected exception isCallerInRole",t); 873 ret = false; 874 } 875 876 if(_logger.isLoggable(Level.FINE)){ 877 _logger.fine("JACC: isCallerInRole Result: " +ret + " EJBRoleRefPermission (Name) = "+ ejbrr.getName() + " (Action) = "+ ejbrr.getActions() + " (Codesource) = " + prdm.getCodeSource()); 878 } 879 880 return ret; 881 } 882 883 889 public Principal getCallerPrincipal() { 890 SecurityContext sc = null; 891 if (runAs != null){ 895 InvocationManager im = theSwitch.getInvocationManager(); 896 ComponentInvocation ci = im.getCurrentInvocation(); 897 898 if (ci == null) { 899 throw new InvocationException(); } 901 sc = ci.getOldSecurityContext(); 902 903 } else{ 904 sc = SecurityContext.getCurrent(); 907 } 908 909 Principal prin; 910 911 if (sc != null) { 912 prin = sc.getCallerPrincipal(); 913 } else { 914 prin = SecurityContext.getDefaultCallerPrincipal(); 915 } 916 return prin; 917 } 918 919 public void destroy() { 920 921 try { 922 923 PolicyConfigurationFactory pcf = getPolicyFactory(); 924 boolean wasInService = pcf.inService(this.contextId); 925 926 PolicyConfiguration 927 pc = pcf.getPolicyConfiguration(this.contextId, false); 928 930 if (wasInService) { 931 policy.refresh(); 932 PermissionCacheFactory.removePermissionCache(uncheckedMethodPermissionCache); 933 uncheckedMethodPermissionCache = null; 934 } 935 roleMapperFactory.removeAppNameForContext(this.contextId); 936 937 } catch (PolicyContextException pce){ 938 String msg = localStrings.getLocalString("ejbsm.could_not_delete", 939 "Could not delete policy file during undeployment"); 940 _logger.log(Level.WARNING, msg, pce); 942 } 943 944 FactoryForSecurityManagerFactory ffsmf 945 = FactoryForSecurityManagerFactoryImpl.getInstance(); 946 SecurityManagerFactory smf = ffsmf.getSecurityManagerFactory("ejb"); 947 smf.removeSecurityManager(contextId); 948 } 949 957 public Subject getCurrentSubject(){ 958 return SecurityContext.getCurrent().getSubject(); 961 } 962 963 967 public Object doAsPrivileged(PrivilegedExceptionAction pea) 968 throws Throwable { 969 970 SecurityContext sc = SecurityContext.getCurrent(); 971 972 Set principalSet = sc.getPrincipalSet(); 973 974 AccessControlContext acc = 975 (AccessControlContext )accessControlContextCache.get(principalSet); 976 977 if(acc == null){ 978 979 final ProtectionDomain [] pdArray = new ProtectionDomain [1]; 980 pdArray[0] = getCachedProtectionDomain(principalSet,false); 981 982 try{ 983 984 if (principalSet != null) { 985 986 final Subject s = sc.getSubject(); 987 988 acc = (AccessControlContext ) 989 AccessController.doPrivileged(new PrivilegedExceptionAction (){ 990 public java.lang.Object run() throws Exception { 991 return new AccessControlContext 992 (new AccessControlContext (pdArray), 993 new SubjectDomainCombiner (s)); 994 } 995 }); 996 } else { 997 acc = new AccessControlContext (pdArray); 998 } 999 1000 accessControlContextCache.put(new HashSet (principalSet),acc); 1003 1004 _logger.fine("JACC: new AccessControlContext added to cache"); 1005 1006 } catch(Exception e){ 1007 _logger.log(Level.SEVERE, 1008 "java_security.security_context_exception",e); 1009 acc = null; 1010 throw e; 1011 } 1012 } 1013 1014 Object rvalue = null; 1015 1016 String oldContextId = setPolicyContext(this.contextId); 1017 1018 if(_logger.isLoggable(Level.FINE)){ 1019 _logger.fine("JACC: doAsPrivileged contextId("+this.contextId+")"); 1020 } 1021 1022 try { 1023 1024 rvalue = AccessController.doPrivileged(pea,acc); 1025 1026 } finally { 1027 resetPolicyContext(oldContextId,this.contextId); 1028 } 1029 1030 return rvalue; 1031 1032 } 1033 1034 1045 public Object runMethod(Method beanClassMethod, Object obj, Object [] oa) 1046 throws Throwable { 1047 String oldCtxID = setPolicyContext(this.contextId); 1048 Object ret = null; 1049 try { 1050 ret = beanClassMethod.invoke(obj, oa); 1051 } finally { 1052 resetPolicyContext(oldCtxID, this.contextId); 1053 } 1054 return ret; 1055 } 1056 1057 private static void resetPolicyContext(final String newV, String oldV) 1058 throws Throwable { 1059 if (oldV != newV && newV != null && (oldV == null || !oldV.equals(newV))) { 1060 1061 if(_logger.isLoggable(Level.FINE)){ 1062 _logger.fine("JACC: Changing Policy Context ID: oldV = " 1063 + oldV + " newV = " + newV); 1064 } 1065 try { 1066 AccessController.doPrivileged(new PrivilegedExceptionAction (){ 1067 public java.lang.Object run() throws Exception { 1068 PolicyContext.setContextID(newV); 1069 return null; 1070 } 1071 }); 1072 } catch (java.security.PrivilegedActionException pae) { 1073 Throwable cause = pae.getCause(); 1074 if( cause instanceof java.security.AccessControlException ) { 1075 _logger.log(Level.SEVERE,"setPolicy SecurityPermission required to call PolicyContext.setContextID",cause); 1076 } else { 1077 _logger.log(Level.SEVERE,"Unexpected Exception while setting PolicyContext",cause); 1078 } 1079 throw cause; 1080 } 1081 } 1082 } 1083 1084 private static String setPolicyContext(String newV) throws Throwable { 1085 1086 String oldV = PolicyContext.getContextID(); 1087 1088 resetPolicyContext(newV,oldV); 1089 1090 return oldV; 1091 } 1092 1093} 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 | Popular Tags |