/portal-impl/src/com/liferay/portal/security/pacl/checker/SecurityChecker.java

https://github.com/lululiferay/liferay-portal · Java · 207 lines · 137 code · 54 blank · 16 comment · 27 complexity · d6aa89174d0ac6cfb998a3edc7cfd904 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.portal.security.pacl.checker;
  15. import com.liferay.portal.kernel.log.Log;
  16. import com.liferay.portal.kernel.log.LogFactoryUtil;
  17. import com.liferay.portal.kernel.util.ServerDetector;
  18. import com.liferay.portal.security.pacl.PACLClassUtil;
  19. import java.security.Permission;
  20. import sun.reflect.Reflection;
  21. /**
  22. * @author Brian Wing Shun Chan
  23. */
  24. public class SecurityChecker extends BaseChecker {
  25. public void afterPropertiesSet() {
  26. }
  27. public void checkPermission(Permission permission) {
  28. String name = permission.getName();
  29. if (name.equals(SECURITY_PERMISSION_GET_POLICY)) {
  30. if (!hasGetPolicy()) {
  31. throwSecurityException(_log, "Attempted to get the policy");
  32. }
  33. }
  34. else if (name.equals(SECURITY_PERMISSION_SET_POLICY)) {
  35. if (!hasSetPolicy()) {
  36. throwSecurityException(_log, "Attempted to set the policy");
  37. }
  38. }
  39. else {
  40. if (_log.isDebugEnabled()) {
  41. Thread.dumpStack();
  42. }
  43. throwSecurityException(
  44. _log,
  45. "Attempted to " + permission.getName() + " on " +
  46. permission.getActions());
  47. }
  48. }
  49. protected boolean hasGetPolicy() {
  50. Class<?> callerClass8 = Reflection.getCallerClass(8);
  51. if (isGlassfishJ2EEInstanceListener(
  52. callerClass8.getEnclosingClass()) &&
  53. CheckerUtil.isAccessControllerDoPrivileged(9)) {
  54. logGetPolicy(callerClass8, 8);
  55. return true;
  56. }
  57. if (isWebSphereWASJSPExtensionServletWrapper(callerClass8)) {
  58. logGetPolicy(callerClass8, 8);
  59. return true;
  60. }
  61. return false;
  62. }
  63. protected boolean hasSetPolicy() {
  64. Class<?> callerClass6 = Reflection.getCallerClass(6);
  65. if (isGlassfishPolicyContextHandlerImpl(callerClass6)) {
  66. logSetPolicy(callerClass6, 6);
  67. return true;
  68. }
  69. Class<?> callerClass7 = Reflection.getCallerClass(7);
  70. if (isGeronimoDispatchListener(callerClass7)) {
  71. logSetPolicy(callerClass7, 7);
  72. return true;
  73. }
  74. return false;
  75. }
  76. protected boolean isGeronimoDispatchListener(Class<?> clazz) {
  77. if (!ServerDetector.isGeronimo()) {
  78. return false;
  79. }
  80. if (clazz == null) {
  81. return false;
  82. }
  83. String className = clazz.getName();
  84. if (!className.equals(_CLASS_NAME_DISPATCH_LISTENER)) {
  85. return false;
  86. }
  87. String classLocation = PACLClassUtil.getClassLocation(clazz);
  88. return classLocation.contains(
  89. "/repository/org/apache/geronimo/modules/geronimo-tomcat6/");
  90. }
  91. protected boolean isGlassfishJ2EEInstanceListener(Class<?> clazz) {
  92. if (!ServerDetector.isGlassfish()) {
  93. return false;
  94. }
  95. if (clazz == null) {
  96. return false;
  97. }
  98. String className = clazz.getName();
  99. if (!className.equals(_CLASS_NAME_J2EE_INSTANCE_LISTENER)) {
  100. return false;
  101. }
  102. String classLocation = PACLClassUtil.getClassLocation(clazz);
  103. return classLocation.startsWith("bundle://");
  104. }
  105. protected boolean isGlassfishPolicyContextHandlerImpl(Class<?> clazz) {
  106. if (!ServerDetector.isGlassfish()) {
  107. return false;
  108. }
  109. if (clazz == null) {
  110. return false;
  111. }
  112. String className = clazz.getName();
  113. if (!className.equals(_CLASS_NAME_POLICY_CONTEXT_HANDLER_IMPL)) {
  114. return false;
  115. }
  116. String classLocation = PACLClassUtil.getClassLocation(clazz);
  117. return classLocation.startsWith("bundle://");
  118. }
  119. protected boolean isWebSphereWASJSPExtensionServletWrapper(Class<?> clazz) {
  120. if (!ServerDetector.isWebSphere()) {
  121. return false;
  122. }
  123. String className = clazz.getName();
  124. if (!className.equals(_CLASS_NAME_WAS_JSP_EXTENSION_SERVLET_WRAPPER)) {
  125. return false;
  126. }
  127. String classLocation = PACLClassUtil.getClassLocation(clazz);
  128. return classLocation.startsWith("bundleresource://");
  129. }
  130. protected void logGetPolicy(Class<?> callerClass, int frame) {
  131. if (_log.isInfoEnabled()) {
  132. _log.info(
  133. "Allowing frame " + frame + " with caller " + callerClass +
  134. " to get the policy");
  135. }
  136. }
  137. protected void logSetPolicy(Class<?> callerClass, int frame) {
  138. if (_log.isInfoEnabled()) {
  139. _log.info(
  140. "Allowing frame " + frame + " with caller " + callerClass +
  141. " to set the policy");
  142. }
  143. }
  144. private static final String _CLASS_NAME_DISPATCH_LISTENER =
  145. "org.apache.geronimo.tomcat.listener.DispatchListener";
  146. private static final String _CLASS_NAME_J2EE_INSTANCE_LISTENER =
  147. "com.sun.web.server.J2EEInstanceListener";
  148. private static final String _CLASS_NAME_POLICY_CONTEXT_HANDLER_IMPL =
  149. "com.sun.enterprise.security.authorize.PolicyContextHandlerImpl";
  150. private static final String _CLASS_NAME_WAS_JSP_EXTENSION_SERVLET_WRAPPER =
  151. "com.ibm.ws.jsp.webcontainerext.ws.WASJSPExtensionServletWrapper";
  152. private static Log _log = LogFactoryUtil.getLog(SecurityChecker.class);
  153. }