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

https://github.com/lululiferay/liferay-portal · Java · 236 lines · 175 code · 45 blank · 16 comment · 38 complexity · 3971cafb13cada2d4e0430880c63a551 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.executor.PortalExecutorManagerUtil;
  16. import com.liferay.portal.kernel.log.Log;
  17. import com.liferay.portal.kernel.log.LogFactoryUtil;
  18. import com.liferay.portal.kernel.messaging.BaseAsyncDestination;
  19. import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
  20. import com.liferay.portal.kernel.util.GetterUtil;
  21. import com.liferay.portal.kernel.util.StringPool;
  22. import com.liferay.portal.kernel.util.Validator;
  23. import java.security.Permission;
  24. import java.util.Set;
  25. import java.util.TreeSet;
  26. import sun.reflect.Reflection;
  27. /**
  28. * @author Brian Wing Shun Chan
  29. */
  30. public class PortalRuntimeChecker extends BaseChecker {
  31. public void afterPropertiesSet() {
  32. initExpandoBridgeClassNames();
  33. initGetBeanPropertyClassNames();
  34. initSearchEngineIds();
  35. initSetBeanPropertyClassNames();
  36. initThreadPoolExecutorNames();
  37. }
  38. public void checkPermission(Permission permission) {
  39. PortalRuntimePermission portalRuntimePermission =
  40. (PortalRuntimePermission)permission;
  41. String name = portalRuntimePermission.getName();
  42. Object subject = portalRuntimePermission.getSubject();
  43. String property = GetterUtil.getString(
  44. portalRuntimePermission.getProperty());
  45. if (name.equals(PORTAL_RUNTIME_PERMISSION_EXPANDO_BRIDGE)) {
  46. String className = (String)subject;
  47. if (!_expandoBridgeClassNames.contains(className)) {
  48. throwSecurityException(
  49. _log, "Attempted to get Expando bridge on " + className);
  50. }
  51. }
  52. else if (name.equals(PORTAL_RUNTIME_PERMISSION_GET_BEAN_PROPERTY)) {
  53. Class<?> clazz = (Class<?>)subject;
  54. if (!hasGetBeanProperty(clazz, property)) {
  55. if (Validator.isNotNull(property)) {
  56. throwSecurityException(
  57. _log,
  58. "Attempted to get bean property " + property + " on " +
  59. clazz);
  60. }
  61. else {
  62. throwSecurityException(
  63. _log, "Attempted to get bean property on " + clazz);
  64. }
  65. }
  66. }
  67. else if (name.equals(PORTAL_RUNTIME_PERMISSION_SEARCH_ENGINE)) {
  68. String searchEngineId = (String)subject;
  69. if (!_searchEngineIds.contains(searchEngineId)) {
  70. throwSecurityException(
  71. _log, "Attempted to get search engine " + searchEngineId);
  72. }
  73. }
  74. else if (name.equals(PORTAL_RUNTIME_PERMISSION_SET_BEAN_PROPERTY)) {
  75. Class<?> clazz = (Class<?>)subject;
  76. if (!hasSetBeanProperty(clazz, property)) {
  77. if (Validator.isNotNull(property)) {
  78. throwSecurityException(
  79. _log,
  80. "Attempted to set bean property " + property + " on " +
  81. clazz);
  82. }
  83. else {
  84. throwSecurityException(
  85. _log, "Attempted to set bean property on " + clazz);
  86. }
  87. }
  88. }
  89. else if (name.equals(PORTAL_RUNTIME_PERMISSION_THREAD_POOL_EXECUTOR)) {
  90. String threadPoolExecutorName = (String)subject;
  91. if (!_threadPoolExecutorNames.contains(threadPoolExecutorName)) {
  92. throwSecurityException(
  93. _log,
  94. "Attempted to modify thread pool executor " +
  95. threadPoolExecutorName);
  96. }
  97. }
  98. }
  99. protected boolean hasGetBeanProperty(Class<?> clazz, String property) {
  100. String className = clazz.getName();
  101. if (_getBeanPropertyClassNames.contains(className)) {
  102. return true;
  103. }
  104. if (Validator.isNotNull(property)) {
  105. if (_getBeanPropertyClassNames.contains(
  106. className.concat(StringPool.POUND).concat(property))) {
  107. return true;
  108. }
  109. }
  110. if (clazz == PortalExecutorManagerUtil.class) {
  111. Class<?> callerClass10 = Reflection.getCallerClass(10);
  112. if (callerClass10 == BaseAsyncDestination.class) {
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. protected boolean hasSetBeanProperty(Class<?> clazz, String property) {
  119. String className = clazz.getName();
  120. if (_setBeanPropertyClassNames.contains(className)) {
  121. return true;
  122. }
  123. if (Validator.isNotNull(property)) {
  124. if (_setBeanPropertyClassNames.contains(
  125. className.concat(StringPool.POUND).concat(property))) {
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. protected void initExpandoBridgeClassNames() {
  132. _expandoBridgeClassNames = getPropertySet(
  133. "security-manager-expando-bridge");
  134. if (_log.isDebugEnabled()) {
  135. Set<String> classNames = new TreeSet<String>(
  136. _expandoBridgeClassNames);
  137. for (String className : classNames) {
  138. _log.debug("Allowing Expando bridge on class " + className);
  139. }
  140. }
  141. }
  142. protected void initGetBeanPropertyClassNames() {
  143. _getBeanPropertyClassNames = getPropertySet(
  144. "security-manager-get-bean-property");
  145. if (_log.isDebugEnabled()) {
  146. Set<String> classNames = new TreeSet<String>(
  147. _getBeanPropertyClassNames);
  148. for (String className : classNames) {
  149. _log.debug("Allowing get bean property on class " + className);
  150. }
  151. }
  152. }
  153. protected void initSearchEngineIds() {
  154. _searchEngineIds = getPropertySet("security-manager-search-engine-ids");
  155. if (_log.isDebugEnabled()) {
  156. Set<String> searchEngineIds = new TreeSet<String>(_searchEngineIds);
  157. for (String searchEngineId : searchEngineIds) {
  158. _log.debug("Allowing search engine " + searchEngineId);
  159. }
  160. }
  161. }
  162. protected void initSetBeanPropertyClassNames() {
  163. _setBeanPropertyClassNames = getPropertySet(
  164. "security-manager-set-bean-property");
  165. if (_log.isDebugEnabled()) {
  166. Set<String> classNames = new TreeSet<String>(
  167. _setBeanPropertyClassNames);
  168. for (String className : classNames) {
  169. _log.debug("Allowing set bean property on class " + className);
  170. }
  171. }
  172. }
  173. protected void initThreadPoolExecutorNames() {
  174. _threadPoolExecutorNames = getPropertySet(
  175. "security-manager-thread-pool-executor-names");
  176. if (_log.isDebugEnabled()) {
  177. Set<String> threadPoolExecutorNames = new TreeSet<String>(
  178. _threadPoolExecutorNames);
  179. for (String threadPoolExecutorName : threadPoolExecutorNames) {
  180. _log.debug(
  181. "Allowing thread pool executor " + threadPoolExecutorName);
  182. }
  183. }
  184. }
  185. private static Log _log = LogFactoryUtil.getLog(PortalRuntimeChecker.class);
  186. private Set<String> _expandoBridgeClassNames;
  187. private Set<String> _getBeanPropertyClassNames;
  188. private Set<String> _searchEngineIds;
  189. private Set<String> _setBeanPropertyClassNames;
  190. private Set<String> _threadPoolExecutorNames;
  191. }