1 25 26 package org.objectweb.jonas.web.lib; 27 28 import java.net.MalformedURLException ; 29 import java.net.URI ; 30 import java.net.URISyntaxException ; 31 import java.net.URL ; 32 import java.security.CodeSource ; 33 import java.security.Permission ; 34 import java.security.PermissionCollection ; 35 import java.security.Principal ; 36 import java.security.ProtectionDomain ; 37 import java.security.cert.Certificate ; 38 import java.util.ArrayList ; 39 import java.util.Collection ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.Map ; 43 44 import javax.security.jacc.PolicyContext ; 45 import javax.security.jacc.PolicyContextException ; 46 import javax.security.jacc.WebResourcePermission ; 47 import javax.security.jacc.WebRoleRefPermission ; 48 import javax.security.jacc.WebUserDataPermission ; 49 import javax.servlet.http.HttpServletRequest ; 50 51 import org.objectweb.jonas.common.Log; 52 import org.objectweb.jonas.security.jacc.JPolicyContextHandlerCurrent; 53 import org.objectweb.jonas.security.jacc.JPolicyContextHandlerData; 54 import org.objectweb.jonas.security.jacc.JPolicyUserRoleMapping; 55 56 import org.objectweb.jonas_lib.deployment.api.SecurityRoleRefDesc; 57 import org.objectweb.jonas_lib.security.AbsPermissionManager; 58 import org.objectweb.jonas_lib.security.PermissionManagerException; 59 import org.objectweb.jonas_web.deployment.api.SecurityConstraintListDesc; 60 import org.objectweb.jonas_web.deployment.api.SecurityRoleDesc; 61 import org.objectweb.jonas_web.deployment.api.ServletDesc; 62 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc; 63 import org.objectweb.util.monolog.api.BasicLevel; 64 import org.objectweb.util.monolog.api.Logger; 65 66 71 public class PermissionManager extends AbsPermissionManager { 72 73 76 private static Logger logger = null; 77 78 81 private WebContainerDeploymentDesc webContainerDeploymentDesc = null; 82 83 89 public PermissionManager(final WebContainerDeploymentDesc webContainerDeploymentDesc, final String contextId) 90 throws PermissionManagerException { 91 super(contextId); 92 this.webContainerDeploymentDesc = webContainerDeploymentDesc; 93 logger = Log.getLogger(Log.JONAS_WEB_PREFIX); 94 } 95 96 112 public void translateServletDeploymentDescriptor() throws PermissionManagerException { 113 translateSecurityConstraintElements(); 114 translateServletSecurityRoleRef(); 115 } 116 117 131 protected void translateSecurityConstraintElements() throws PermissionManagerException { 132 if (webContainerDeploymentDesc == null || getPolicyConfiguration() == null) { 133 throw new PermissionManagerException("PolicyConfiguration or webContainerbDeploymentDesc is null"); 134 } 135 136 SecurityConstraintListDesc securityConstraintListDesc = webContainerDeploymentDesc 137 .getSecurityConstraintListDesc(); 138 PermissionCollection excludedPermissions = securityConstraintListDesc.getExcludedPermissions(); 139 PermissionCollection uncheckedPermissions = securityConstraintListDesc.getUncheckedPermissions(); 140 PermissionCollection rolePermissions = null; 141 String roleName = null; 142 Map roleMapPermissions = securityConstraintListDesc.getPermissionsByRole(); 143 144 try { 145 getPolicyConfiguration().addToExcludedPolicy(excludedPermissions); 146 getPolicyConfiguration().addToUncheckedPolicy(uncheckedPermissions); 147 148 for (Iterator rolesIt = roleMapPermissions.keySet().iterator(); rolesIt.hasNext();) { 149 roleName = (String ) rolesIt.next(); 150 rolePermissions = (PermissionCollection ) roleMapPermissions.get(roleName); 151 getPolicyConfiguration().addToRole(roleName, rolePermissions); 152 } 153 } catch (PolicyContextException pce) { 154 throw new PermissionManagerException("Can not add add permissions to policy", pce); 155 } 156 } 157 158 181 protected void translateServletSecurityRoleRef() throws PermissionManagerException { 182 if (webContainerDeploymentDesc == null || getPolicyConfiguration() == null) { 183 throw new PermissionManagerException("PolicyConfiguration or webContainerbDeploymentDesc is null"); 184 } 185 186 Collection servlets = webContainerDeploymentDesc.getServletDescList(); 187 188 List rolesAppearedInSecurityRoleRef = new ArrayList (); 190 191 199 ServletDesc servletDesc = null; 200 String servletName = null; 201 List roleRefs = null; 202 SecurityRoleRefDesc securityRoleRefDesc = null; 203 for (Iterator itServlet = servlets.iterator(); itServlet.hasNext();) { 204 servletDesc = (ServletDesc) itServlet.next(); 205 roleRefs = servletDesc.getSecurityRoleRefList(); 206 servletName = servletDesc.getServletName(); 207 for (Iterator itRoleRef = roleRefs.iterator(); itRoleRef.hasNext();) { 208 securityRoleRefDesc = (SecurityRoleRefDesc) itRoleRef.next(); 209 215 Permission webRoleRefPermission = securityRoleRefDesc.getWebRoleRefPermission(); 216 217 rolesAppearedInSecurityRoleRef.add(securityRoleRefDesc.getRoleName()); 219 220 try { 221 getPolicyConfiguration().addToRole(securityRoleRefDesc.getRoleLink(), webRoleRefPermission); 222 } catch (PolicyContextException pce) { 223 throw new PermissionManagerException("Can not add add permission '" + webRoleRefPermission 224 + "' to policy", pce); 225 } 226 } 227 } 228 229 236 List securityRoles = webContainerDeploymentDesc.getSecurityRoleList(); 237 SecurityRoleDesc securityRoleDesc = null; 238 String securityRoleName = null; 239 240 for (Iterator itServlet = servlets.iterator(); itServlet.hasNext();) { 241 servletDesc = (ServletDesc) itServlet.next(); 242 servletName = servletDesc.getServletName(); 243 244 for (Iterator itSecurityRoles = securityRoles.iterator(); itSecurityRoles.hasNext();) { 245 securityRoleDesc = (SecurityRoleDesc) itSecurityRoles.next(); 246 securityRoleName = securityRoleDesc.getRoleName(); 247 if (!rolesAppearedInSecurityRoleRef.contains(securityRoleName)) { 251 252 261 Permission webRoleRefPermission = new WebRoleRefPermission (servletName, securityRoleName); 262 try { 263 getPolicyConfiguration().addToRole(securityRoleName, webRoleRefPermission); 264 } catch (PolicyContextException pce) { 265 throw new PermissionManagerException("Can not add add permission '" + webRoleRefPermission 266 + "' to policy", pce); 267 } 268 } 269 } 270 } 271 272 276 securityRoles = webContainerDeploymentDesc.getSecurityRoleList(); 277 for (Iterator itSecurityRoles = securityRoles.iterator(); itSecurityRoles.hasNext();) { 278 securityRoleDesc = (SecurityRoleDesc) itSecurityRoles.next(); 279 securityRoleName = securityRoleDesc.getRoleName(); 280 284 Permission webRoleRefPermission = new WebRoleRefPermission ("", securityRoleName); 285 try { 286 getPolicyConfiguration().addToRole(securityRoleName, webRoleRefPermission); 287 } catch (PolicyContextException pce) { 288 throw new PermissionManagerException("Can not add add permission '" + webRoleRefPermission 289 + "' to policy", pce); 290 } 291 } 292 } 293 294 301 public boolean checkWebUserDataPermission(HttpServletRequest request, String principalName, String [] roles) { 302 303 try { 304 ProtectionDomain protectionDomain = initPolicyContext(request, principalName, roles); 305 306 WebUserDataPermission webUserDataPermission = new WebUserDataPermission (request); 310 boolean accessOK = getPolicy().implies(protectionDomain, webUserDataPermission); 311 312 if (logger.isLoggable(BasicLevel.DEBUG)) { 313 logger.log(BasicLevel.DEBUG, "Policy.implies result = " + accessOK); 314 } 315 return accessOK; 316 317 } catch (Exception e) { 318 logger.log(BasicLevel.ERROR, "Can't check web user data permission :" + e.getMessage()); 319 return false; 320 } 321 322 } 323 324 331 public boolean checkWebResourcePermission(HttpServletRequest request, String principalName, String [] roles) { 332 try { 333 ProtectionDomain protectionDomain = initPolicyContext(request, principalName, roles); 334 335 WebResourcePermission webResourcePermission = new WebResourcePermission (request); 339 boolean accessOK = getPolicy().implies(protectionDomain, webResourcePermission); 340 if (logger.isLoggable(BasicLevel.DEBUG)) { 341 logger.log(BasicLevel.DEBUG, "Policy.implies result = " + accessOK); 342 } 343 return accessOK; 344 345 } catch (Exception e) { 346 logger.log(BasicLevel.ERROR, "Can't check web resource permission :" + e.getMessage()); 347 return false; 348 } 349 350 } 351 352 361 public boolean checkWebRoleRefPermission(HttpServletRequest request, String servletName, String principalName, String [] roles, 362 String roleName) { 363 try { 364 365 ProtectionDomain protectionDomain = initPolicyContext(request, principalName, roles); 366 367 WebRoleRefPermission webRoleRefPermission = new WebRoleRefPermission (servletName, roleName); 371 boolean accessOK = getPolicy().implies(protectionDomain, webRoleRefPermission); 372 if (logger.isLoggable(BasicLevel.DEBUG)) { 373 logger.log(BasicLevel.DEBUG, "Policy.implies result = " + accessOK); 374 } 375 return accessOK; 376 377 } catch (Exception e) { 378 logger.log(BasicLevel.ERROR, "Can't check web role ref permission :" + e.getMessage()); 379 return false; 380 } 381 382 } 383 384 393 private synchronized ProtectionDomain initPolicyContext(HttpServletRequest request, String principalName, String [] roles) 394 throws URISyntaxException , MalformedURLException { 395 396 PolicyContext.setContextID(getContextId()); 398 399 JPolicyContextHandlerData jPolicyContextHandlerData = JPolicyContextHandlerCurrent.getCurrent() 401 .getJPolicyContextHandlerData(); 402 if (jPolicyContextHandlerData == null) { 403 logger.log(BasicLevel.ERROR, "The Handler data retrieved is null !"); 404 return null; 405 } 406 jPolicyContextHandlerData.setHttpServletRequest(request); 407 PolicyContext.setHandlerData(jPolicyContextHandlerData); 408 409 URI uri = new URI ("file://" + getContextId()); 411 CodeSource codesource = new CodeSource (new URL (uri.toString()), (Certificate []) null); 412 413 String [] overridedRoles = JPolicyUserRoleMapping.getMappingForPrincipal(getContextId(), principalName); 415 if (overridedRoles != null) { 416 roles = overridedRoles; 417 } 418 419 Principal [] principals = null; 420 if (roles != null) { 421 principals = new Principal [roles.length]; 422 for (int k = 0; k < roles.length; k++) { 423 principals[k] = new org.objectweb.jonas.security.auth.JPrincipal(roles[k]); 424 } 425 } 426 427 return new ProtectionDomain (codesource, null, null, principals); 428 } 429 430 433 protected void resetDeploymentDesc() { 434 webContainerDeploymentDesc = null; 435 } 436 437 } 438 | Popular Tags |