/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.ant.core.source_3.2.300.v20110511/org/eclipse/ant/internal/core/AntSecurityManager.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 373 lines · 206 code · 40 blank · 127 comment · 77 complexity · 2320d99d8cb4f96d555ee247204d4def MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2005 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.ant.internal.core;
  12. import java.io.FileDescriptor;
  13. import java.net.InetAddress;
  14. import java.net.SocketPermission;
  15. import java.security.Permission;
  16. import java.util.PropertyPermission;
  17. import org.eclipse.ant.core.AntSecurityException;
  18. /**
  19. * A security manager that always throws an <code>AntSecurityException</code>
  20. * if the calling thread attempts to cause the Java Virtual Machine to
  21. * exit/halt or if the restricted thread attempts to set a System property.
  22. * Otherwise this manager just delegates to the pre-existing manager
  23. * passed in the constructor or mimics the default security manager behavior
  24. */
  25. public class AntSecurityManager extends SecurityManager {
  26. private SecurityManager fSecurityManager= null;
  27. private Thread fRestrictedThread= null;
  28. //ensure that the PropertyPermission class is loaded before we
  29. //start checking permissions: bug 85908
  30. private static final PropertyPermission fgPropertyPermission= new PropertyPermission("*", "write"); //$NON-NLS-1$ //$NON-NLS-2$
  31. private boolean fAllowSettingSystemProperties= true;
  32. public AntSecurityManager(SecurityManager securityManager, Thread restrictedThread, boolean allowSettingProperties) {
  33. fSecurityManager= securityManager;
  34. fRestrictedThread= restrictedThread;
  35. fAllowSettingSystemProperties= allowSettingProperties;
  36. }
  37. public AntSecurityManager(SecurityManager securityManager, Thread restrictedThread) {
  38. this(securityManager, restrictedThread, true);
  39. }
  40. /* (non-Javadoc)
  41. * @see java.lang.SecurityManager#checkExit(int)
  42. */
  43. public void checkExit(int status) {
  44. //no exit allowed from the restricted thread...System.exit is being called
  45. //by some ant task...do not want Eclipse to exit if
  46. //in the same VM.
  47. if (Thread.currentThread() == fRestrictedThread) {
  48. throw new AntSecurityException();
  49. }
  50. if (fSecurityManager != null) {
  51. fSecurityManager.checkExit(status);
  52. }
  53. }
  54. /* (non-Javadoc)
  55. * @see java.lang.SecurityManager#checkAccept(java.lang.String, int)
  56. */
  57. public void checkAccept(String host, int port) {
  58. if (fSecurityManager != null) {
  59. fSecurityManager.checkAccept(host, port);
  60. }
  61. }
  62. /* (non-Javadoc)
  63. * @see java.lang.SecurityManager#checkAccess(java.lang.Thread)
  64. */
  65. public void checkAccess(Thread t) {
  66. if (fSecurityManager != null) {
  67. fSecurityManager.checkAccess(t);
  68. }
  69. }
  70. /* (non-Javadoc)
  71. * @see java.lang.SecurityManager#checkAccess(java.lang.ThreadGroup)
  72. */
  73. public void checkAccess(ThreadGroup g) {
  74. if (fSecurityManager != null) {
  75. fSecurityManager.checkAccess(g);
  76. }
  77. }
  78. /* (non-Javadoc)
  79. * @see java.lang.SecurityManager#checkAwtEventQueueAccess()
  80. */
  81. public void checkAwtEventQueueAccess() {
  82. if (fSecurityManager != null) {
  83. fSecurityManager.checkAwtEventQueueAccess();
  84. }
  85. }
  86. /* (non-Javadoc)
  87. * @see java.lang.SecurityManager#checkConnect(java.lang.String, int, java.lang.Object)
  88. */
  89. public void checkConnect(String host, int port, Object context) {
  90. if (fSecurityManager != null) {
  91. fSecurityManager.checkConnect(host, port, context);
  92. }
  93. }
  94. /* (non-Javadoc)
  95. * @see java.lang.SecurityManager#checkConnect(java.lang.String, int)
  96. */
  97. public void checkConnect(String host, int port) {
  98. if (fSecurityManager != null) {
  99. fSecurityManager.checkConnect(host, port);
  100. }
  101. }
  102. /* (non-Javadoc)
  103. * @see java.lang.SecurityManager#checkCreateClassLoader()
  104. */
  105. public void checkCreateClassLoader() {
  106. if (fSecurityManager != null) {
  107. fSecurityManager.checkCreateClassLoader();
  108. }
  109. }
  110. /* (non-Javadoc)
  111. * @see java.lang.SecurityManager#checkDelete(java.lang.String)
  112. */
  113. public void checkDelete(String file) {
  114. if (fSecurityManager != null) {
  115. fSecurityManager.checkDelete(file);
  116. }
  117. }
  118. /* (non-Javadoc)
  119. * @see java.lang.SecurityManager#checkExec(java.lang.String)
  120. */
  121. public void checkExec(String cmd) {
  122. if (fSecurityManager != null) {
  123. fSecurityManager.checkExec(cmd);
  124. }
  125. }
  126. /* (non-Javadoc)
  127. * @see java.lang.SecurityManager#checkLink(java.lang.String)
  128. */
  129. public void checkLink(String lib) {
  130. if (fSecurityManager != null) {
  131. fSecurityManager.checkLink(lib);
  132. }
  133. }
  134. /* (non-Javadoc)
  135. * @see java.lang.SecurityManager#checkListen(int)
  136. */
  137. public void checkListen(int port) {
  138. if (fSecurityManager != null) {
  139. fSecurityManager.checkListen(port);
  140. }
  141. }
  142. /* (non-Javadoc)
  143. * @see java.lang.SecurityManager#checkMemberAccess(java.lang.Class, int)
  144. */
  145. public void checkMemberAccess(Class clazz, int which) {
  146. if (fSecurityManager != null) {
  147. fSecurityManager.checkMemberAccess(clazz, which);
  148. }
  149. }
  150. /**
  151. * @see java.lang.SecurityManager#checkMulticast(java.net.InetAddress, byte)
  152. * @deprecated
  153. */
  154. public void checkMulticast(InetAddress maddr, byte ttl) {
  155. if (fSecurityManager != null) {
  156. String host = maddr.getHostAddress();
  157. if (!host.startsWith("[") && host.indexOf(':') != -1) { //$NON-NLS-1$
  158. host = "[" + host + "]"; //$NON-NLS-1$ //$NON-NLS-2$
  159. }
  160. checkPermission(new SocketPermission(host, "accept,connect")); //$NON-NLS-1$
  161. }
  162. }
  163. /* (non-Javadoc)
  164. * @see java.lang.SecurityManager#checkMulticast(java.net.InetAddress)
  165. */
  166. public void checkMulticast(InetAddress maddr) {
  167. if (fSecurityManager != null) {
  168. fSecurityManager.checkMulticast(maddr);
  169. }
  170. }
  171. /* (non-Javadoc)
  172. * @see java.lang.SecurityManager#checkPackageAccess(java.lang.String)
  173. */
  174. public void checkPackageAccess(String pkg) {
  175. if (fSecurityManager != null) {
  176. fSecurityManager.checkPackageAccess(pkg);
  177. }
  178. }
  179. /* (non-Javadoc)
  180. * @see java.lang.SecurityManager#checkPackageDefinition(java.lang.String)
  181. */
  182. public void checkPackageDefinition(String pkg) {
  183. if (fSecurityManager != null) {
  184. fSecurityManager.checkPackageDefinition(pkg);
  185. }
  186. }
  187. /* (non-Javadoc)
  188. * @see java.lang.SecurityManager#checkPermission(java.security.Permission, java.lang.Object)
  189. */
  190. public void checkPermission(Permission perm, Object context) {
  191. if (fSecurityManager != null) {
  192. fSecurityManager.checkPermission(perm, context);
  193. }
  194. }
  195. /* (non-Javadoc)
  196. * @see java.lang.SecurityManager#checkPermission(java.security.Permission)
  197. */
  198. public void checkPermission(Permission perm) {
  199. if (!fAllowSettingSystemProperties && fgPropertyPermission.implies(perm) && fRestrictedThread == Thread.currentThread()) {
  200. //attempting to write a system property
  201. throw new AntSecurityException();
  202. }
  203. if (fSecurityManager != null) {
  204. fSecurityManager.checkPermission(perm);
  205. }
  206. }
  207. /* (non-Javadoc)
  208. * @see java.lang.SecurityManager#checkPrintJobAccess()
  209. */
  210. public void checkPrintJobAccess() {
  211. if (fSecurityManager != null) {
  212. fSecurityManager.checkPrintJobAccess();
  213. }
  214. }
  215. /* (non-Javadoc)
  216. * @see java.lang.SecurityManager#checkPropertiesAccess()
  217. */
  218. public void checkPropertiesAccess() {
  219. if (fSecurityManager != null) {
  220. fSecurityManager.checkPropertiesAccess();
  221. }
  222. super.checkPropertiesAccess();
  223. }
  224. /* (non-Javadoc)
  225. * @see java.lang.SecurityManager#checkPropertyAccess(java.lang.String)
  226. */
  227. public void checkPropertyAccess(String key) {
  228. if (fSecurityManager != null) {
  229. fSecurityManager.checkPropertyAccess(key);
  230. }
  231. }
  232. /* (non-Javadoc)
  233. * @see java.lang.SecurityManager#checkRead(java.io.FileDescriptor)
  234. */
  235. public void checkRead(FileDescriptor fd) {
  236. if (fSecurityManager != null) {
  237. fSecurityManager.checkRead(fd);
  238. }
  239. }
  240. /* (non-Javadoc)
  241. * @see java.lang.SecurityManager#checkRead(java.lang.String, java.lang.Object)
  242. */
  243. public void checkRead(String file, Object context) {
  244. if (fSecurityManager != null) {
  245. fSecurityManager.checkRead(file, context);
  246. }
  247. }
  248. /* (non-Javadoc)
  249. * @see java.lang.SecurityManager#checkRead(java.lang.String)
  250. */
  251. public void checkRead(String file) {
  252. if (fSecurityManager != null) {
  253. fSecurityManager.checkRead(file);
  254. }
  255. }
  256. /* (non-Javadoc)
  257. * @see java.lang.SecurityManager#checkSecurityAccess(java.lang.String)
  258. */
  259. public void checkSecurityAccess(String target) {
  260. if (fSecurityManager != null) {
  261. fSecurityManager.checkSecurityAccess(target);
  262. }
  263. }
  264. /* (non-Javadoc)
  265. * @see java.lang.SecurityManager#checkSetFactory()
  266. */
  267. public void checkSetFactory() {
  268. if (fSecurityManager != null) {
  269. fSecurityManager.checkSetFactory();
  270. }
  271. }
  272. /* (non-Javadoc)
  273. * @see java.lang.SecurityManager#checkSystemClipboardAccess()
  274. */
  275. public void checkSystemClipboardAccess() {
  276. if (fSecurityManager != null) {
  277. fSecurityManager.checkSystemClipboardAccess();
  278. }
  279. }
  280. /* (non-Javadoc)
  281. * @see java.lang.SecurityManager#checkTopLevelWindow(java.lang.Object)
  282. */
  283. public boolean checkTopLevelWindow(Object window) {
  284. if (fSecurityManager != null) {
  285. return fSecurityManager.checkTopLevelWindow(window);
  286. }
  287. return super.checkTopLevelWindow(window);
  288. }
  289. /* (non-Javadoc)
  290. * @see java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
  291. */
  292. public void checkWrite(FileDescriptor fd) {
  293. if (fSecurityManager != null) {
  294. fSecurityManager.checkWrite(fd);
  295. }
  296. }
  297. /* (non-Javadoc)
  298. * @see java.lang.SecurityManager#checkWrite(java.lang.String)
  299. */
  300. public void checkWrite(String file) {
  301. if (fSecurityManager != null) {
  302. fSecurityManager.checkWrite(file);
  303. }
  304. }
  305. /**
  306. * @see java.lang.SecurityManager#getInCheck()
  307. * @deprecated
  308. */
  309. public boolean getInCheck() {
  310. if (fSecurityManager != null) {
  311. return fSecurityManager.getInCheck();
  312. }
  313. return super.getInCheck();
  314. }
  315. /* (non-Javadoc)
  316. * @see java.lang.SecurityManager#getSecurityContext()
  317. */
  318. public Object getSecurityContext() {
  319. if (fSecurityManager != null) {
  320. return fSecurityManager.getSecurityContext();
  321. }
  322. return super.getSecurityContext();
  323. }
  324. /* (non-Javadoc)
  325. * @see java.lang.SecurityManager#getThreadGroup()
  326. */
  327. public ThreadGroup getThreadGroup() {
  328. if (fSecurityManager != null) {
  329. fSecurityManager.getThreadGroup();
  330. }
  331. return super.getThreadGroup();
  332. }
  333. }