1 26 27 package org.objectweb.jonas_ejb.container; 28 29 import java.net.URI ; 30 import java.net.URL ; 31 import java.security.CodeSource ; 32 import java.security.PermissionCollection ; 33 import java.security.Principal ; 34 import java.security.ProtectionDomain ; 35 import java.security.cert.Certificate ; 36 import java.util.Iterator ; 37 38 import javax.security.jacc.EJBMethodPermission ; 39 import javax.security.jacc.EJBRoleRefPermission ; 40 import javax.security.jacc.PolicyContext ; 41 import javax.security.jacc.PolicyContextException ; 42 43 import org.objectweb.jonas_ejb.deployment.api.BeanDesc; 44 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc; 45 import org.objectweb.jonas_ejb.deployment.api.ExcludeListDesc; 46 import org.objectweb.jonas_ejb.deployment.api.MethodPermissionDesc; 47 import org.objectweb.jonas_ejb.lib.EJBInvocation; 48 49 import org.objectweb.jonas_lib.deployment.api.SecurityRoleRefDesc; 50 import org.objectweb.jonas_lib.security.AbsPermissionManager; 51 import org.objectweb.jonas_lib.security.PermissionManagerException; 52 53 import org.objectweb.jonas.security.jacc.JPolicyContextHandlerCurrent; 54 import org.objectweb.jonas.security.jacc.JPolicyContextHandlerData; 55 56 import org.objectweb.security.context.SecurityContext; 57 import org.objectweb.security.context.SecurityCurrent; 58 59 import org.objectweb.util.monolog.api.BasicLevel; 60 61 66 public class PermissionManager extends AbsPermissionManager { 67 68 71 private DeploymentDesc ejbDeploymentDesc = null; 72 73 79 public PermissionManager(DeploymentDesc ejbDeploymentDesc, String contextId) throws PermissionManagerException { 80 super(contextId); 81 this.ejbDeploymentDesc = ejbDeploymentDesc; 82 } 83 84 103 public void translateEjbDeploymentDescriptor() throws PermissionManagerException { 104 translateEjbMethodPermission(); 105 translateEjbExcludeList(); 106 translateEjbSecurityRoleRef(); 107 } 108 109 130 protected void translateEjbMethodPermission() throws PermissionManagerException { 131 if (ejbDeploymentDesc == null || getPolicyConfiguration() == null) { 132 throw new PermissionManagerException("PolicyConfiguration or ejbDeploymentDesc is null"); 133 } 134 135 MethodPermissionDesc methodPermissionDesc = null; 136 PermissionCollection permissionCollection = null; 137 138 for (Iterator it = ejbDeploymentDesc.getMethodPermissionsDescList().iterator(); it.hasNext();) { 139 methodPermissionDesc = (MethodPermissionDesc) it.next(); 140 permissionCollection = methodPermissionDesc.getEJBMethodPermissions(); 141 try { 142 if (methodPermissionDesc.isUnchecked()) { 144 getPolicyConfiguration().addToUncheckedPolicy(permissionCollection); 145 } else { 146 for (Iterator rolesIt = methodPermissionDesc.getRoleNameList().iterator(); rolesIt.hasNext();) { 147 getPolicyConfiguration().addToRole((String ) rolesIt.next(), permissionCollection); 148 } 149 } 150 } catch (PolicyContextException pce) { 151 throw new PermissionManagerException("Can not add add excluded policy", pce); 152 } 153 } 154 } 155 156 167 protected void translateEjbExcludeList() throws PermissionManagerException { 168 if (ejbDeploymentDesc == null || getPolicyConfiguration() == null) { 169 throw new PermissionManagerException("PolicyConfiguration or ejbDeploymentDesc is null"); 170 } 171 ExcludeListDesc excludeListDesc = ejbDeploymentDesc.getExcludeListDesc(); 172 if (excludeListDesc != null) { 173 try { 174 getPolicyConfiguration().addToExcludedPolicy(excludeListDesc.getEJBMethodPermissions()); 175 } catch (PolicyContextException pce) { 176 throw new PermissionManagerException("Can not add add excluded policy", pce); 177 } 178 } 179 } 180 181 194 public void translateEjbSecurityRoleRef() throws PermissionManagerException { 195 if (ejbDeploymentDesc == null || getPolicyConfiguration() == null) { 196 throw new PermissionManagerException("PolicyConfiguration or ejbDeploymentDesc is null"); 197 } 198 199 SecurityRoleRefDesc securityRoleRefDesc = null; 200 BeanDesc beanDesc = null; 201 202 for (Iterator itEjb = ejbDeploymentDesc.getBeanDescIterator(); itEjb.hasNext();) { 204 beanDesc = (BeanDesc) itEjb.next(); 205 for (Iterator it = beanDesc.getSecurityRoleRefDescList().iterator(); it.hasNext();) { 206 securityRoleRefDesc = (SecurityRoleRefDesc) it.next(); 207 try { 208 getPolicyConfiguration().addToRole(securityRoleRefDesc.getRoleLink(), 209 securityRoleRefDesc.getEJBRoleRefPermission()); 210 } catch (PolicyContextException pce) { 211 throw new PermissionManagerException("Can not add add excluded policy", pce); 212 } 213 } 214 } 215 } 216 217 225 public boolean checkSecurity(String ejbName, EJBInvocation ejbInv, boolean inRunAs) { 226 try { 227 PolicyContext.setContextID(getContextId()); 228 String methodSignature = ejbInv.methodPermissionSignature; 229 230 if (TraceEjb.isDebugSecurity()) { 231 TraceEjb.security.log(BasicLevel.DEBUG, "EjbName = " + ejbName + ", methodSignature = " + methodSignature); 232 } 233 234 JPolicyContextHandlerData jPolicyContextHandlerData = JPolicyContextHandlerCurrent.getCurrent() 236 .getJPolicyContextHandlerData(); 237 if (jPolicyContextHandlerData == null) { 238 TraceEjb.security.log(BasicLevel.ERROR, "The Handler data retrieved is null !"); 239 return false; 240 } 241 jPolicyContextHandlerData.setEjbArguments(ejbInv.arguments); 242 jPolicyContextHandlerData.setProcessingBean(ejbInv.bean); 243 244 PolicyContext.setHandlerData(jPolicyContextHandlerData); 245 246 URI uri = new URI ("file://" + getContextId()); 248 CodeSource codesource = new CodeSource (new URL (uri.toString()), (Certificate []) null); 249 SecurityCurrent current = SecurityCurrent.getCurrent(); 250 final SecurityContext sctx = current.getSecurityContext(); 251 if (TraceEjb.isDebugSecurity()) { 252 TraceEjb.security.log(BasicLevel.DEBUG, "Security Context = " + sctx); 253 if (sctx != null) { 254 TraceEjb.security.log(BasicLevel.DEBUG, "sctx.getCallerPrincipalRoles() = " + sctx.getCallerPrincipalRoles(inRunAs)); 255 } 256 } 257 258 259 String runAsRole = null; 260 261 Principal [] principals = null; 262 String [] runAsPrincipalRoles = null; 263 String [] principalRoles = null; 264 if (sctx != null) { 265 synchronized (sctx) { 266 runAsRole = sctx.peekRunAsRole(); 267 runAsPrincipalRoles = sctx.peekRunAsPrincipalRoles(); 268 principalRoles = sctx.getCallerPrincipalRoles(inRunAs); 269 } 270 if (runAsRole != null) { 271 principals = new Principal [runAsPrincipalRoles.length]; 272 for (int k = 0; k < runAsPrincipalRoles.length; k++) { 273 principals[k] = new org.objectweb.jonas.security.auth.JPrincipal( 274 runAsPrincipalRoles[k]); 275 } 276 } else { 277 principals = new Principal [principalRoles.length]; 278 for (int k = 0; k < principalRoles.length; k++) { 279 principals[k] = new org.objectweb.jonas.security.auth.JPrincipal( 280 principalRoles[k]); 281 } 282 } 283 } else { 284 if (TraceEjb.isDebugSecurity()) { 285 TraceEjb.security.log(BasicLevel.DEBUG, "Security context is null"); 286 } 287 } 288 ProtectionDomain protectionDomain = new ProtectionDomain (codesource, null, null, principals); 289 290 EJBMethodPermission ejbMethodPermission = new EJBMethodPermission (ejbName, methodSignature); 294 boolean accessOK = getPolicy().implies(protectionDomain, ejbMethodPermission); 295 if (TraceEjb.isDebugSecurity()) { 296 TraceEjb.security.log(BasicLevel.DEBUG, "Policy.implies result = " + accessOK); 297 } 298 jPolicyContextHandlerData = null; 299 return accessOK; 300 301 } catch (Exception e) { 302 TraceEjb.security.log(BasicLevel.ERROR, "Cannot check security", e); 303 return false; 304 } 305 306 } 307 308 319 public boolean isCallerInRole(String ejbName, String roleName, boolean inRunAs) { 320 try { 321 PolicyContext.setContextID(getContextId()); 322 if (TraceEjb.isDebugSecurity()) { 323 TraceEjb.security.log(BasicLevel.DEBUG, "roleName = " + roleName); 324 } 325 326 URI uri = new URI ("file://" + getContextId()); 328 CodeSource codesource = new CodeSource (new URL (uri.toString()), (Certificate []) null); 329 SecurityCurrent current = SecurityCurrent.getCurrent(); 330 final SecurityContext sctx = current.getSecurityContext(); 331 if (TraceEjb.isDebugSecurity()) { 332 TraceEjb.security.log(BasicLevel.DEBUG, "Security Context = " + sctx); 333 TraceEjb.security.log(BasicLevel.DEBUG, "sctx.getCallerPrincipalRoles() = " 334 + sctx.getCallerPrincipalRoles(inRunAs)); 335 } 336 Principal [] principals = null; 337 if (sctx != null) { 338 principals = new Principal [sctx.getCallerPrincipalRoles(inRunAs).length]; 339 for (int k = 0; k < sctx.getCallerPrincipalRoles(inRunAs).length; k++) { 340 principals[k] = new org.objectweb.jonas.security.auth.JPrincipal(sctx.getCallerPrincipalRoles(inRunAs)[k]); 341 } 342 } else { 343 if (TraceEjb.isDebugSecurity()) { 344 TraceEjb.security.log(BasicLevel.DEBUG, "Security context is null"); 345 } 346 } 347 ProtectionDomain protectionDomain = new ProtectionDomain (codesource, null, null, principals); 348 349 EJBRoleRefPermission ejbRoleRefPermission = new EJBRoleRefPermission (ejbName, roleName); 352 boolean isInRole = getPolicy().implies(protectionDomain, ejbRoleRefPermission); 353 if (TraceEjb.isDebugSecurity()) { 354 TraceEjb.security.log(BasicLevel.DEBUG, "Policy.implies result = " + isInRole); 355 } 356 return isInRole; 357 358 } catch (Exception e) { 359 TraceEjb.security.log(BasicLevel.ERROR, "Cannot check isCallerInRole", e); 360 return false; 361 } 362 363 } 364 365 368 protected void resetDeploymentDesc() { 369 ejbDeploymentDesc = null; 370 } 371 372 } | Popular Tags |