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

https://github.com/lululiferay/liferay-portal · Java · 92 lines · 51 code · 24 blank · 17 comment · 9 complexity · 14c95d37afc270c894c927b41f2f5665 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.JavaDetector;
  18. import java.security.Permission;
  19. import sun.reflect.Reflection;
  20. /**
  21. * @author Brian Wing Shun Chan
  22. */
  23. public class NetChecker extends BaseChecker {
  24. public void afterPropertiesSet() {
  25. }
  26. public void checkPermission(Permission permission) {
  27. String name = permission.getName();
  28. if (name.equals(NET_PERMISSION_GET_PROXY_SELECTOR)) {
  29. if (!hasGetProxySelector()) {
  30. throwSecurityException(_log, "Attempted to get proxy selector");
  31. }
  32. }
  33. else if (name.equals(NET_PERMISSION_SPECIFY_STREAM_HANDLER)) {
  34. // TODO
  35. }
  36. }
  37. protected boolean hasGetProxySelector() {
  38. if (JavaDetector.isJDK7()) {
  39. Class<?> callerClass8 = Reflection.getCallerClass(8);
  40. String className8 = callerClass8.getName();
  41. if (className8.startsWith(_CLASS_NAME_SOCKS_SOCKET_IMPL) &&
  42. CheckerUtil.isAccessControllerDoPrivileged(9)) {
  43. logGetProxySelector(callerClass8, 8);
  44. return true;
  45. }
  46. }
  47. else {
  48. Class<?> callerClass7 = Reflection.getCallerClass(7);
  49. String className7 = callerClass7.getName();
  50. if (className7.startsWith(_CLASS_NAME_SOCKS_SOCKET_IMPL) &&
  51. CheckerUtil.isAccessControllerDoPrivileged(8)) {
  52. logGetProxySelector(callerClass7, 7);
  53. return true;
  54. }
  55. }
  56. return false;
  57. }
  58. protected void logGetProxySelector(Class<?> callerClass, int frame) {
  59. if (_log.isInfoEnabled()) {
  60. _log.info(
  61. "Allowing frame " + frame + " with caller " + callerClass +
  62. " to get the proxy selector");
  63. }
  64. }
  65. private static final String _CLASS_NAME_SOCKS_SOCKET_IMPL =
  66. "java.net.SocksSocketImpl$";
  67. private static Log _log = LogFactoryUtil.getLog(NetChecker.class);
  68. }