1. /*
  2. * @(#)SecurityManager.java 1.108 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. import java.security.*;
  9. import java.io.FileDescriptor;
  10. import java.io.File;
  11. import java.io.FilePermission;
  12. import java.awt.AWTPermission;
  13. import java.util.PropertyPermission;
  14. import java.lang.RuntimePermission;
  15. import java.net.SocketPermission;
  16. import java.net.NetPermission;
  17. import java.util.Hashtable;
  18. import java.net.InetAddress;
  19. import java.lang.reflect.Member;
  20. import java.lang.reflect.*;
  21. import java.net.URL;
  22. /**
  23. * The security manager is a class that allows
  24. * applications to implement a security policy. It allows an
  25. * application to determine, before performing a possibly unsafe or
  26. * sensitive operation, what the operation is and whether
  27. * it is being attempted in a security context that allows the
  28. * operation to be performed. The
  29. * application can allow or disallow the operation.
  30. * <p>
  31. * The <code>SecurityManager</code> class contains many methods with
  32. * names that begin with the word <code>check</code>. These methods
  33. * are called by various methods in the Java libraries before those
  34. * methods perform certain potentially sensitive operations. The
  35. * invocation of such a <code>check</code> method typically looks like this:
  36. * <p><blockquote><pre>
  37. * SecurityManager security = System.getSecurityManager();
  38. * if (security != null) {
  39. * security.check<i>XXX</i>(argument,  . . . );
  40. * }
  41. * </pre></blockquote>
  42. * <p>
  43. * The security manager is thereby given an opportunity to prevent
  44. * completion of the operation by throwing an exception. A security
  45. * manager routine simply returns if the operation is permitted, but
  46. * throws a <code>SecurityException</code> if the operation is not
  47. * permitted. The only exception to this convention is
  48. * <code>checkTopLevelWindow</code>, which returns a
  49. * <code>boolean</code> value.
  50. * <p>
  51. * The current security manager is set by the
  52. * <code>setSecurityManager</code> method in class
  53. * <code>System</code>. The current security manager is obtained
  54. * by the <code>getSecurityManager</code> method.
  55. * <p>
  56. * The special method
  57. * {@link SecurityManager#checkPermission(java.security.Permission)}
  58. * determines whether an access request indicated by a specified
  59. * permission should be granted or denied. The
  60. * default implementation calls
  61. *
  62. * <pre>
  63. * AccessController.checkPermission(perm);
  64. * </pre>
  65. *
  66. * <p>
  67. * If a requested access is allowed,
  68. * <code>checkPermission</code> returns quietly. If denied, a
  69. * <code>SecurityException</code> is thrown.
  70. * <p>
  71. * As of JDK 1.2, the default implementation of each of the other
  72. * <code>check</code> methods in <code>SecurityManager</code> is to
  73. * call the <code>SecurityManager checkPermission</code> method
  74. * to determine if the calling thread has permission to perform the requested
  75. * operation.
  76. * <p>
  77. * Note that the <code>checkPermission</code> method with
  78. * just a single permission argument always performs security checks
  79. * within the context of the currently executing thread.
  80. * Sometimes a security check that should be made within a given context
  81. * will actually need to be done from within a
  82. * <i>different</i> context (for example, from within a worker thread).
  83. * The {@link SecurityManager#getSecurityContext getSecurityContext} method
  84. * and the {@link SecurityManager#checkPermission(java.security.Permission,
  85. * java.lang.Object) checkPermission}
  86. * method that includes a context argument are provided
  87. * for this situation. The
  88. * <code>getSecurityContext</code> method returns a "snapshot"
  89. * of the current calling context. (The default implementation
  90. * returns an AccessControlContext object.) A sample call is
  91. * the following:
  92. *
  93. * <pre>
  94. * Object context = null;
  95. * SecurityManager sm = System.getSecurityManager();
  96. * if (sm != null) context = sm.getSecurityContext();
  97. * </pre>
  98. *
  99. * <p>
  100. * The <code>checkPermission</code> method
  101. * that takes a context object in addition to a permission
  102. * makes access decisions based on that context,
  103. * rather than on that of the current execution thread.
  104. * Code within a different context can thus call that method,
  105. * passing the permission and the
  106. * previously-saved context object. A sample call, using the
  107. * SecurityManager <code>sm</code> obtained as in the previous example,
  108. * is the following:
  109. *
  110. * <pre>
  111. * if (sm != null) sm.checkPermission(permission, context);
  112. * </pre>
  113. *
  114. * <p>Permissions fall into these categories: File, Socket, Net,
  115. * Security, Runtime, Property, AWT, Reflect, and Serializable.
  116. * The classes managing these various
  117. * permission categories are <code>java.io.FilePermission</code>,
  118. * <code>java.net.SocketPermission</code>,
  119. * <code>java.net.NetPermission</code>,
  120. * <code>java.security.SecurityPermission</code>,
  121. * <code>java.lang.RuntimePermission</code>,
  122. * <code>java.util.PropertyPermission</code>,
  123. * <code>java.awt.AWTPermission</code>,
  124. * <code>java.lang.reflect.ReflectPermission</code>, and
  125. * <code>java.io.SerializablePermission</code>.
  126. *
  127. * <p>All but the first two (FilePermission and SocketPermission) are
  128. * subclasses of <code>java.security.BasicPermission</code>, which itself
  129. * is an abstract subclass of the
  130. * top-level class for permissions, which is
  131. * <code>java.security.Permission</code>. BasicPermission defines the
  132. * functionality needed for all permissions that contain a name
  133. * that follows the hierarchical property naming convention
  134. * (for example, "exitVM", "setFactory", "queuePrintJob", etc).
  135. * An asterisk
  136. * may appear at the end of the name, following a ".", or by itself, to
  137. * signify a wildcard match. For example: "a.*" or "*" is valid,
  138. * "*a" or "a*b" is not valid.
  139. *
  140. * <p>FilePermission and SocketPermission are subclasses of the
  141. * top-level class for permissions
  142. * (<code>java.security.Permission</code>). Classes like these
  143. * that have a more complicated name syntax than that used by
  144. * BasicPermission subclass directly from Permission rather than from
  145. * BasicPermission. For example,
  146. * for a <code>java.io.FilePermission</code> object, the permission name is
  147. * the pathname of a file (or directory).
  148. *
  149. * <p>Some of the permission classes have an "actions" list that tells
  150. * the actions that are permitted for the object. For example,
  151. * for a <code>java.io.FilePermission</code> object, the actions list
  152. * (such as "read, write") specifies which actions are granted for the
  153. * specified file (or for files in the specified directory).
  154. *
  155. * <p>Other permission classes are for "named" permissions -
  156. * ones that contain a name but no actions list; you either have the
  157. * named permission or you don't.
  158. *
  159. * <p>Note: There is also a <code>java.security.AllPermission</code>
  160. * permission that implies all permissions. It exists to simplify the work
  161. * of system administrators who might need to perform multiple
  162. * tasks that require all (or numerous) permissions.
  163. * <p>
  164. * See <a href ="../../../guide/security/permissions.html">
  165. * Permissions in JDK1.2</a> for permission-related information.
  166. * This document includes, for example, a table listing the various SecurityManager
  167. * <code>check</code> methods and the permission(s) the default
  168. * implementation of each such method requires.
  169. * It also contains a table of all the JDK 1.2 methods
  170. * that require permissions, and for each such method tells
  171. * which permission it requires.
  172. * <p>
  173. * For more information about <code>SecurityManager</code> changes made in
  174. * JDK 1.2 and advice regarding porting of 1.1-style security managers,
  175. * see the release documentation at
  176. * <a href= "http://java.sun.com/products/jdk/1.2/docs/guide/security/index.html">
  177. * http://java.sun.com/products/jdk/1.2/docs/guide/security/index.html</a>.
  178. *
  179. * @author Arthur van Hoff
  180. * @author Roland Schemers
  181. *
  182. * @version 1.108, 11/29/01
  183. * @see java.lang.ClassLoader
  184. * @see java.lang.SecurityException
  185. * @see java.lang.SecurityManager#checkTopLevelWindow(java.lang.Object)
  186. * checkTopLevelWindow
  187. * @see java.lang.System#getSecurityManager() getSecurityManager
  188. * @see java.lang.System#setSecurityManager(java.lang.SecurityManager)
  189. * setSecurityManager
  190. * @see java.security.AccessController AccessController
  191. * @see java.security.AccessControlContext AccessControlContext
  192. * @see java.security.AccessControlException AccessControlException
  193. * @see java.security.Permission
  194. * @see java.security.BasicPermission
  195. * @see java.io.FilePermission
  196. * @see java.net.SocketPermission
  197. * @see java.util.PropertyPermission
  198. * @see java.lang.RuntimePermission
  199. * @see java.awt.AWTPermission
  200. * @see java.security.Policy Policy
  201. * @see java.security.SecurityPermission SecurityPermission
  202. * @see java.security.ProtectionDomain
  203. *
  204. * @since JDK1.0
  205. */
  206. public
  207. class SecurityManager {
  208. /**
  209. * This field is <code>true</code> if there is a security check in
  210. * progress; <code>false</code> otherwise.
  211. *
  212. * @deprecated This type of security checking is not recommended.
  213. * It is recommended that the <code>checkPermission</code>
  214. * call be used instead.
  215. */
  216. protected boolean inCheck;
  217. /*
  218. * Have we been initialized. Effective against finalizer attacks.
  219. */
  220. private boolean initialized = false;
  221. // static permissions. Create here and used in the various
  222. // check permission calls
  223. private static RuntimePermission createClassLoaderPermission;
  224. private static AWTPermission topLevelWindowPermission;
  225. private static AWTPermission accessClipboardPermission;
  226. private static AWTPermission checkAwtEventQueuePermission;
  227. private static RuntimePermission checkMemberAccessPermission;
  228. private static AllPermission allPermission;
  229. private static RuntimePermission threadPermission;
  230. private static RuntimePermission threadGroupPermission;
  231. /**
  232. * returns true if the current context has been granted AllPermission
  233. */
  234. private boolean hasAllPermission()
  235. {
  236. if (allPermission == null)
  237. allPermission = new AllPermission();
  238. try {
  239. checkPermission(allPermission);
  240. return true;
  241. } catch (SecurityException se) {
  242. return false;
  243. }
  244. }
  245. /**
  246. * Tests if there is a security check in progress.
  247. *
  248. * @return the value of the <code>inCheck</code> field. This field
  249. * should contain <code>true</code> if a security check is
  250. * in progress,
  251. * <code>false</code> otherwise.
  252. * @see java.lang.SecurityManager#inCheck
  253. * @deprecated This type of security checking is not recommended.
  254. * It is recommended that the <code>checkPermission</code>
  255. * call be used instead.
  256. */
  257. public boolean getInCheck() {
  258. return inCheck;
  259. }
  260. /**
  261. * Constructs a new <code>SecurityManager</code>.
  262. *
  263. * <p> If there is a security manager already installed, this method first
  264. * calls the security manager's <code>checkPermission</code> method
  265. * with the <code>RuntimePermission("createSecurityManager")</code>
  266. * permission to ensure the calling thread has permission to create a new
  267. * security manager.
  268. * This may result in throwing a <code>SecurityException</code>.
  269. *
  270. * @exception java.lang.SecurityException if a security manager already
  271. * exists and its <code>checkPermission</code> method
  272. * doesn't allow creation of a new security manager.
  273. * @see java.lang.System#getSecurityManager()
  274. * @see #checkPermission(java.security.Permission) checkPermission
  275. * @see java.lang.RuntimePermission
  276. */
  277. public SecurityManager() {
  278. synchronized(SecurityManager.class) {
  279. SecurityManager sm = System.getSecurityManager();
  280. if (sm != null) {
  281. // ask the currently installed security manager if we
  282. // can create a new one.
  283. sm.checkPermission(new RuntimePermission
  284. ("createSecurityManager"));
  285. }
  286. initialized = true;
  287. }
  288. }
  289. /**
  290. * Returns the current execution stack as an array of classes.
  291. * <p>
  292. * The length of the array is the number of methods on the execution
  293. * stack. The element at index <code>0</code> is the class of the
  294. * currently executing method, the element at index <code>1</code> is
  295. * the class of that method's caller, and so on.
  296. *
  297. * @return the execution stack.
  298. */
  299. protected native Class[] getClassContext();
  300. /**
  301. * Returns the class loader of the most recently executing method from
  302. * a class defined using a non-system class loader. A non-system
  303. * class loader is defined as being a class loader that is not equal to
  304. * the system class loader (as returned
  305. * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
  306. * <p>
  307. * This method will return
  308. * <code>null</code> in the following three cases:<p>
  309. * <ol>
  310. * <li>All methods on the execution stack are from classes
  311. * defined using the system class loader or one of its ancestors.
  312. *
  313. * <li>All methods on the execution stack up to the first
  314. * "privileged" caller
  315. * (see {@link java.security.AccessController#doPrivileged})
  316. * are from classes
  317. * defined using the system class loader or one of its ancestors.
  318. *
  319. * <li> A call to <code>checkPermission</code> with
  320. * <code>java.security.AllPermission</code> does not
  321. * result in a SecurityException.
  322. *
  323. * </ol>
  324. *
  325. * @return the class loader of the most recent occurrence on the stack
  326. * of a method from a class defined using a non-system class
  327. * loader.
  328. *
  329. * @deprecated This type of security checking is not recommended.
  330. * It is recommended that the <code>checkPermission</code>
  331. * call be used instead.
  332. *
  333. * @see java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
  334. * @see #checkPermission(java.security.Permission) checkPermission
  335. */
  336. protected ClassLoader currentClassLoader()
  337. {
  338. ClassLoader cl = currentClassLoader0();
  339. if ((cl != null) && hasAllPermission())
  340. cl = null;
  341. return cl;
  342. }
  343. private native ClassLoader currentClassLoader0();
  344. /**
  345. * Returns the class of the most recently executing method from
  346. * a class defined using a non-system class loader. A non-system
  347. * class loader is defined as being a class loader that is not equal to
  348. * the system class loader (as returned
  349. * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
  350. * <p>
  351. * This method will return
  352. * <code>null</code> in the following three cases:<p>
  353. * <ol>
  354. * <li>All methods on the execution stack are from classes
  355. * defined using the system class loader or one of its ancestors.
  356. *
  357. * <li>All methods on the execution stack up to the first
  358. * "privileged" caller
  359. * (see {@link java.security.AccessController#doPrivileged})
  360. * are from classes
  361. * defined using the system class loader or one of its ancestors.
  362. *
  363. * <li> A call to <code>checkPermission</code> with
  364. * <code>java.security.AllPermission</code> does not
  365. * result in a SecurityException.
  366. *
  367. * </ol>
  368. *
  369. * @return the class of the most recent occurrence on the stack
  370. * of a method from a class defined using a non-system class
  371. * loader.
  372. *
  373. * @deprecated This type of security checking is not recommended.
  374. * It is recommended that the <code>checkPermission</code>
  375. * call be used instead.
  376. *
  377. * @see java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
  378. * @see #checkPermission(java.security.Permission) checkPermission
  379. */
  380. protected Class currentLoadedClass() {
  381. Class c = currentLoadedClass0();
  382. if ((c != null) && hasAllPermission())
  383. c = null;
  384. return c;
  385. }
  386. /**
  387. * Returns the stack depth of the specified class.
  388. *
  389. * @param name the fully qualified name of the class to search for.
  390. * @return the depth on the stack frame of the first occurrence of a
  391. * method from a class with the specified name;
  392. * <code>-1</code> if such a frame cannot be found.
  393. * @deprecated This type of security checking is not recommended.
  394. * It is recommended that the <code>checkPermission</code>
  395. * call be used instead.
  396. *
  397. */
  398. protected native int classDepth(String name);
  399. /**
  400. * Returns the stack depth of the most recently executing method
  401. * from a class defined using a non-system class loader. A non-system
  402. * class loader is defined as being a class loader that is not equal to
  403. * the system class loader (as returned
  404. * by {@link ClassLoader#getSystemClassLoader}) or one of its ancestors.
  405. * <p>
  406. * This method will return
  407. * -1 in the following three cases:<p>
  408. * <ol>
  409. * <li>All methods on the execution stack are from classes
  410. * defined using the system class loader or one of its ancestors.
  411. *
  412. * <li>All methods on the execution stack up to the first
  413. * "privileged" caller
  414. * (see {@link java.security.AccessController#doPrivileged})
  415. * are from classes
  416. * defined using the system class loader or one of its ancestors.
  417. *
  418. * <li> A call to <code>checkPermission</code> with
  419. * <code>java.security.AllPermission</code> does not
  420. * result in a SecurityException.
  421. *
  422. * </ol>
  423. *
  424. * @return the depth on the stack frame of the most recent occurrence of
  425. * a method from a class defined using a non-system class loader.
  426. *
  427. * @deprecated This type of security checking is not recommended.
  428. * It is recommended that the <code>checkPermission</code>
  429. * call be used instead.
  430. *
  431. * @see java.lang.ClassLoader#getSystemClassLoader() getSystemClassLoader
  432. * @see #checkPermission(java.security.Permission) checkPermission
  433. */
  434. protected int classLoaderDepth()
  435. {
  436. int depth = classLoaderDepth0();
  437. if (depth != -1) {
  438. if (hasAllPermission())
  439. depth = -1;
  440. else
  441. depth--; // make sure we don't include ourself
  442. }
  443. return depth;
  444. }
  445. private native int classLoaderDepth0();
  446. /**
  447. * Tests if a method from a class with the specified
  448. * name is on the execution stack.
  449. *
  450. * @param name the fully qualified name of the class.
  451. * @return <code>true</code> if a method from a class with the specified
  452. * name is on the execution stack; <code>false</code> otherwise.
  453. * @deprecated This type of security checking is not recommended.
  454. * It is recommended that the <code>checkPermission</code>
  455. * call be used instead.
  456. */
  457. protected boolean inClass(String name) {
  458. return classDepth(name) >= 0;
  459. }
  460. /**
  461. * Basically, tests if a method from a class defined using a
  462. * class loader is on the execution stack.
  463. *
  464. * @return <code>true</code> if a call to <code>currentClassLoader</code>
  465. * has a non-null return value.
  466. *
  467. * @deprecated This type of security checking is not recommended.
  468. * It is recommended that the <code>checkPermission</code>
  469. * call be used instead.
  470. * @see #currentClassLoader() currentClassLoader
  471. */
  472. protected boolean inClassLoader() {
  473. return currentClassLoader() != null;
  474. }
  475. /**
  476. * Creates an object that encapsulates the current execution
  477. * environment. The result of this method is used, for example, by the
  478. * three-argument <code>checkConnect</code> method and by the
  479. * two-argument <code>checkRead</code> method.
  480. * These methods are needed because a trusted method may be called
  481. * on to read a file or open a socket on behalf of another method.
  482. * The trusted method needs to determine if the other (possibly
  483. * untrusted) method would be allowed to perform the operation on its
  484. * own.
  485. * <p> The default implementation of this method is to return
  486. * an <code>AccessControlContext</code> object.
  487. *
  488. * @return an implementation-dependent object that encapsulates
  489. * sufficient information about the current execution environment
  490. * to perform some security checks later.
  491. * @see java.lang.SecurityManager#checkConnect(java.lang.String, int,
  492. * java.lang.Object) checkConnect
  493. * @see java.lang.SecurityManager#checkRead(java.lang.String,
  494. * java.lang.Object) checkRead
  495. * @see java.security.AccessControlContext AccessControlContext
  496. */
  497. public Object getSecurityContext() {
  498. return AccessController.getContext();
  499. }
  500. /**
  501. * Throws a <code>SecurityException</code> if the requested
  502. * access, specified by the given permission, is not permitted based
  503. * on the security policy currently in effect.
  504. * <p>
  505. * This method calls <code>AccessController.checkPermission</code>
  506. * with the given permission.
  507. *
  508. * @param perm the requested permission.
  509. * @exception SecurityException if access is not permitted based on
  510. * the current security policy.
  511. * @since JDK1.2
  512. */
  513. public void checkPermission(Permission perm) {
  514. java.security.AccessController.checkPermission(perm);
  515. }
  516. /**
  517. * Throws a <code>SecurityException</code> if the
  518. * specified security context is denied access to the resource
  519. * specified by the given permission.
  520. * The context must be a security
  521. * context returned by a previous call to
  522. * <code>getSecurityContext</code> and the access control
  523. * decision is based upon the configured security policy for
  524. * that security context.
  525. * <p>
  526. * If <code>context</code> is an instance of
  527. * <code>AccessControlContext</code> then the
  528. * <code>AccessControlContext.checkPermission</code> method is
  529. * invoked with the specified permission.
  530. * <p>
  531. * If <code>context</code> is not an instance of
  532. * <code>AccessControlContext</code> then a
  533. * <code>SecurityException</code> is thrown.
  534. *
  535. * @param perm the specified permission
  536. * @param context a system-dependent security context.
  537. * @exception SecurityException if the specified security context is
  538. * denied access to the resource specified by the
  539. * given permission.
  540. * @see java.lang.SecurityManager#getSecurityContext()
  541. * @see java.security.AccessControlContext#checkPermission(java.security.Permission)
  542. * @since JDK1.2
  543. */
  544. public void checkPermission(Permission perm, Object context) {
  545. if (context instanceof AccessControlContext) {
  546. ((AccessControlContext)context).checkPermission(perm);
  547. } else {
  548. throw new SecurityException();
  549. }
  550. }
  551. /**
  552. * Throws a <code>SecurityException</code> if the
  553. * calling thread is not allowed to create a new class loader.
  554. * <p>
  555. * This method calls <code>checkPermission</code> with the
  556. * <code>RuntimePermission("createClassLoader")</code>
  557. * permission.
  558. * <p>
  559. * If you override this method, then you should make a call to
  560. * <code>super.checkCreateClassLoader</code>
  561. * at the point the overridden method would normally throw an
  562. * exception.
  563. *
  564. * @exception SecurityException if the calling thread does not
  565. * have permission
  566. * to create a new class loader.
  567. * @see java.lang.ClassLoader#ClassLoader()
  568. * @see #checkPermission(java.security.Permission) checkPermission
  569. */
  570. public void checkCreateClassLoader() {
  571. if (createClassLoaderPermission == null)
  572. createClassLoaderPermission =
  573. new RuntimePermission("createClassLoader");
  574. checkPermission(createClassLoaderPermission);
  575. }
  576. /**
  577. * reference to the root thread group, used for the checkAccess
  578. * methods.
  579. */
  580. private static ThreadGroup rootGroup = getRootGroup();
  581. private static ThreadGroup getRootGroup() {
  582. ThreadGroup root = Thread.currentThread().getThreadGroup();
  583. while (root.getParent() != null) {
  584. root = root.getParent();
  585. }
  586. return root;
  587. }
  588. /**
  589. * Throws a <code>SecurityException</code> if the
  590. * calling thread is not allowed to modify the thread argument.
  591. * <p>
  592. * This method is invoked for the current security manager by the
  593. * <code>stop</code>, <code>suspend</code>, <code>resume</code>,
  594. * <code>setPriority</code>, <code>setName</code>, and
  595. * <code>setDaemon</code> methods of class <code>Thread</code>.
  596. * <p>
  597. * If the thread argument is a system thread (belongs to
  598. * the thread group with a <code>null</code> parent) then
  599. * this method calls <code>checkPermission</code> with the
  600. * <code>RuntimePermission("modifyThread")</code> permission.
  601. * If the thread argument is <i>not</i> a system thread,
  602. * this method just returns silently.
  603. * <p>
  604. * Applications that want a stricter policy should override this
  605. * method. If this method is overridden, the method that overrides
  606. * it should additionally check to see if the calling thread has the
  607. * <code>RuntimePermission("modifyThread")</code> permission, and
  608. * if so, return silently. This is to ensure that code granted
  609. * that permission (such as the JDK itself) is allowed to
  610. * manipulate any thread.
  611. * <p>
  612. * If this method is overridden, then
  613. * <code>super.checkAccess</code> should
  614. * be called by the first statement in the overridden method, or the
  615. * equivalent security check should be placed in the overridden method.
  616. *
  617. * @param t the thread to be checked.
  618. * @exception SecurityException if the calling thread does not have
  619. * permission to modify the thread.
  620. * @see java.lang.Thread#resume() resume
  621. * @see java.lang.Thread#setDaemon(boolean) setDaemon
  622. * @see java.lang.Thread#setName(java.lang.String) setName
  623. * @see java.lang.Thread#setPriority(int) setPriority
  624. * @see java.lang.Thread#stop() stop
  625. * @see java.lang.Thread#suspend() suspend
  626. * @see #checkPermission(java.security.Permission) checkPermission
  627. */
  628. public void checkAccess(Thread t) {
  629. if (t.getThreadGroup() == rootGroup) {
  630. if (threadPermission == null)
  631. threadPermission = new RuntimePermission("modifyThread");
  632. checkPermission(threadPermission);
  633. } else {
  634. // just return
  635. }
  636. }
  637. /**
  638. * Throws a <code>SecurityException</code> if the
  639. * calling thread is not allowed to modify the thread group argument.
  640. * <p>
  641. * This method is invoked for the current security manager when a
  642. * new child thread or child thread group is created, and by the
  643. * <code>setDaemon</code>, <code>setMaxPriority</code>,
  644. * <code>stop</code>, <code>suspend</code>, <code>resume</code>, and
  645. * <code>destroy</code> methods of class <code>ThreadGroup</code>.
  646. * <p>
  647. * If the thread group argument is the system thread group (
  648. * has a <code>null</code> parent) then
  649. * this method calls <code>checkPermission</code> with the
  650. * <code>RuntimePermission("modifyThreadGroup")</code> permission.
  651. * If the thread group argument is <i>not</i> the system thread group,
  652. * this method just returns silently.
  653. * <p>
  654. * Applications that want a stricter policy should override this
  655. * method. If this method is overridden, the method that overrides
  656. * it should additionally check to see if the calling thread has the
  657. * <code>RuntimePermission("modifyThreadGroup")</code> permission, and
  658. * if so, return silently. This is to ensure that code granted
  659. * that permission (such as the JDK itself) is allowed to
  660. * manipulate any thread.
  661. * <p>
  662. * If this method is overridden, then
  663. * <code>super.checkAccess</code> should
  664. * be called by the first statement in the overridden method, or the
  665. * equivalent security check should be placed in the overridden method.
  666. *
  667. * @param g the thread group to be checked.
  668. * @exception SecurityException if the calling thread does not have
  669. * permission to modify the thread group.
  670. * @see java.lang.ThreadGroup#destroy() destroy
  671. * @see java.lang.ThreadGroup#resume() resume
  672. * @see java.lang.ThreadGroup#setDaemon(boolean) setDaemon
  673. * @see java.lang.ThreadGroup#setMaxPriority(int) setMaxPriority
  674. * @see java.lang.ThreadGroup#stop() stop
  675. * @see java.lang.ThreadGroup#suspend() suspend
  676. * @see #checkPermission(java.security.Permission) checkPermission
  677. */
  678. public void checkAccess(ThreadGroup g) {
  679. if (g == null) {
  680. throw new NullPointerException("thread group can't be null");
  681. }
  682. if (g == rootGroup) {
  683. if (threadGroupPermission == null)
  684. threadGroupPermission =
  685. new RuntimePermission("modifyThreadGroup");
  686. checkPermission(threadGroupPermission);
  687. } else {
  688. // just return
  689. }
  690. }
  691. /**
  692. * Throws a <code>SecurityException</code> if the
  693. * calling thread is not allowed to cause the Java Virtual Machine to
  694. * halt with the specified status code.
  695. * <p>
  696. * This method is invoked for the current security manager by the
  697. * <code>exit</code> method of class <code>Runtime</code>. A status
  698. * of <code>0</code> indicates success; other values indicate various
  699. * errors.
  700. * <p>
  701. * This method calls <code>checkPermission</code> with the
  702. * <code>RuntimePermission("exitVM")</code> permission.
  703. * <p>
  704. * If you override this method, then you should make a call to
  705. * <code>super.checkExit</code>
  706. * at the point the overridden method would normally throw an
  707. * exception.
  708. *
  709. * @param status the exit status.
  710. * @exception SecurityException if the calling thread does not have
  711. * permission to halt the Java Virtual Machine with
  712. * the specified status.
  713. * @see java.lang.Runtime#exit(int) exit
  714. * @see #checkPermission(java.security.Permission) checkPermission
  715. */
  716. public void checkExit(int status) {
  717. checkPermission(new RuntimePermission("exitVM"));
  718. }
  719. /**
  720. * Throws a <code>SecurityException</code> if the
  721. * calling thread is not allowed to create a subprocess.
  722. * <p>
  723. * This method is invoked for the current security manager by the
  724. * <code>exec</code> methods of class <code>Runtime</code>.
  725. * <p>
  726. * This method calls <code>checkPermission</code> with the
  727. * <code>FilePermission(cmd,"execute")</code> permission.
  728. * <p>
  729. * If you override this method, then you should make a call to
  730. * <code>super.checkExec</code>
  731. * at the point the overridden method would normally throw an
  732. * exception.
  733. *
  734. * @param cmd the specified system command.
  735. * @exception SecurityException if the calling thread does not have
  736. * permission to create a subprocess.
  737. * @see java.lang.Runtime#exec(java.lang.String)
  738. * @see java.lang.Runtime#exec(java.lang.String, java.lang.String[])
  739. * @see java.lang.Runtime#exec(java.lang.String[])
  740. * @see java.lang.Runtime#exec(java.lang.String[], java.lang.String[])
  741. * @see #checkPermission(java.security.Permission) checkPermission
  742. */
  743. public void checkExec(String cmd) {
  744. File f = new File(cmd);
  745. if (f.isAbsolute()) {
  746. checkPermission(new FilePermission(cmd, "execute"));
  747. } else {
  748. checkPermission(new FilePermission("<<ALL FILES>>", "execute"));
  749. }
  750. }
  751. /**
  752. * Throws a <code>SecurityException</code> if the
  753. * calling thread is not allowed to dynamic link the library code
  754. * specified by the string argument file. The argument is either a
  755. * simple library name or a complete filename.
  756. * <p>
  757. * This method is invoked for the current security manager by
  758. * methods <code>load</code> and <code>loadLibrary</code> of class
  759. * <code>Runtime</code>.
  760. * <p>
  761. * This method calls <code>checkPermission</code> with the
  762. * <code>RuntimePermission("loadLibrary."+lib)</code> permission.
  763. * <p>
  764. * If you override this method, then you should make a call to
  765. * <code>super.checkLink</code>
  766. * at the point the overridden method would normally throw an
  767. * exception.
  768. *
  769. * @param lib the name of the library.
  770. * @exception SecurityException if the calling thread does not have
  771. * permission to dynamically link the library.
  772. * @see java.lang.Runtime#load(java.lang.String)
  773. * @see java.lang.Runtime#loadLibrary(java.lang.String)
  774. * @see #checkPermission(java.security.Permission) checkPermission
  775. */
  776. public void checkLink(String lib) {
  777. if (lib == null) {
  778. throw new NullPointerException("library can't be null");
  779. }
  780. checkPermission(new RuntimePermission("loadLibrary."+lib));
  781. }
  782. /**
  783. * Throws a <code>SecurityException</code> if the
  784. * calling thread is not allowed to read from the specified file
  785. * descriptor.
  786. * <p>
  787. * This method calls <code>checkPermission</code> with the
  788. * <code>RuntimePermission("readFileDescriptor")</code>
  789. * permission.
  790. * <p>
  791. * If you override this method, then you should make a call to
  792. * <code>super.checkRead</code>
  793. * at the point the overridden method would normally throw an
  794. * exception.
  795. *
  796. * @param fd the system-dependent file descriptor.
  797. * @exception SecurityException if the calling thread does not have
  798. * permission to access the specified file descriptor.
  799. * @see java.io.FileDescriptor
  800. * @see #checkPermission(java.security.Permission) checkPermission
  801. */
  802. public void checkRead(FileDescriptor fd) {
  803. if (fd == null) {
  804. throw new NullPointerException("file descriptor can't be null");
  805. }
  806. checkPermission(new RuntimePermission("readFileDescriptor"));
  807. }
  808. /**
  809. * Throws a <code>SecurityException</code> if the
  810. * calling thread is not allowed to read the file specified by the
  811. * string argument.
  812. * <p>
  813. * This method calls <code>checkPermission</code> with the
  814. * <code>FilePermission(file,"read")</code> permission.
  815. * <p>
  816. * If you override this method, then you should make a call to
  817. * <code>super.checkRead</code>
  818. * at the point the overridden method would normally throw an
  819. * exception.
  820. *
  821. * @param file the system-dependent file name.
  822. * @exception SecurityException if the calling thread does not have
  823. * permission to access the specified file.
  824. * @see #checkPermission(java.security.Permission) checkPermission
  825. */
  826. public void checkRead(String file) {
  827. checkPermission(new FilePermission(file, "read"));
  828. }
  829. /**
  830. * Throws a <code>SecurityException</code> if the
  831. * specified security context is not allowed to read the file
  832. * specified by the string argument. The context must be a security
  833. * context returned by a previous call to
  834. * <code>getSecurityContext</code>.
  835. * <p> If <code>context</code> is an instance of
  836. * <code>AccessControlContext</code> then the
  837. * <code>AccessControlContext.checkPermission</code> method will
  838. * be invoked with the <code>FilePermission(file,"read")</code> permission.
  839. * <p> If <code>context</code> is not an instance of
  840. * <code>AccessControlContext</code> then a
  841. * <code>SecurityException</code> is thrown.
  842. * <p>
  843. * If you override this method, then you should make a call to
  844. * <code>super.checkRead</code>
  845. * at the point the overridden method would normally throw an
  846. * exception.
  847. *
  848. * @param file the system-dependent filename.
  849. * @param context a system-dependent security context.
  850. * @exception SecurityException if the specified security context does
  851. * not have permission to read the specified file.
  852. * @see java.lang.SecurityManager#getSecurityContext()
  853. * @see java.security.AccessControlContext#checkPermission(java.security.Permission)
  854. */
  855. public void checkRead(String file, Object context) {
  856. if (context instanceof AccessControlContext) {
  857. ((AccessControlContext)context).checkPermission(
  858. new FilePermission(file, "read"));
  859. } else{
  860. throw new SecurityException();
  861. }
  862. }
  863. /**
  864. * Throws a <code>SecurityException</code> if the
  865. * calling thread is not allowed to write to the specified file
  866. * descriptor.
  867. * <p>
  868. * This method calls <code>checkPermission</code> with the
  869. * <code>RuntimePermission("writeFileDescriptor")</code>
  870. * permission.
  871. * <p>
  872. * If you override this method, then you should make a call to
  873. * <code>super.checkWrite</code>
  874. * at the point the overridden method would normally throw an
  875. * exception.
  876. *
  877. * @param fd the system-dependent file descriptor.
  878. * @exception SecurityException if the calling thread does not have
  879. * permission to access the specified file descriptor.
  880. * @see java.io.FileDescriptor
  881. * @see #checkPermission(java.security.Permission) checkPermission
  882. */
  883. public void checkWrite(FileDescriptor fd) {
  884. if (fd == null) {
  885. throw new NullPointerException("file descriptor can't be null");
  886. }
  887. checkPermission(new RuntimePermission("writeFileDescriptor"));
  888. }
  889. /**
  890. * Throws a <code>SecurityException</code> if the
  891. * calling thread is not allowed to write to the file specified by
  892. * the string argument.
  893. * <p>
  894. * This method calls <code>checkPermission</code> with the
  895. * <code>FilePermission(file,"write")</code> permission.
  896. * <p>
  897. * If you override this method, then you should make a call to
  898. * <code>super.checkWrite</code>
  899. * at the point the overridden method would normally throw an
  900. * exception.
  901. *
  902. * @param file the system-dependent filename.
  903. * @exception SecurityException if the calling thread does not
  904. * have permission to access the specified file.
  905. * @see #checkPermission(java.security.Permission) checkPermission
  906. */
  907. public void checkWrite(String file) {
  908. checkPermission(new FilePermission(file, "write"));
  909. }
  910. /**
  911. * Throws a <code>SecurityException</code> if the
  912. * calling thread is not allowed to delete the specified file.
  913. * <p>
  914. * This method is invoked for the current security manager by the
  915. * <code>delete</code> method of class <code>File</code>.
  916. * <p>
  917. * This method calls <code>checkPermission</code> with the
  918. * <code>FilePermission(file,"delete")</code> permission.
  919. * <p>
  920. * If you override this method, then you should make a call to
  921. * <code>super.checkDelete</code>
  922. * at the point the overridden method would normally throw an
  923. * exception.
  924. *
  925. * @param file the system-dependent filename.
  926. * @exception SecurityException if the calling thread does not
  927. * have permission to delete the file.
  928. *
  929. * @see java.io.File#delete()
  930. * @see #checkPermission(java.security.Permission) checkPermission
  931. */
  932. public void checkDelete(String file) {
  933. checkPermission(new FilePermission(file, "delete"));
  934. }
  935. /**
  936. * Throws a <code>SecurityException</code> if the
  937. * calling thread is not allowed to open a socket connection to the
  938. * specified host and port number.
  939. * <p>
  940. * A port number of <code>-1</code> indicates that the calling
  941. * method is attempting to determine the IP address of the specified
  942. * host name.
  943. * <p>
  944. * This method calls <code>checkPermission</code> with the
  945. * <code>SocketPermission(host+":"+port,"connect")</code> permission if
  946. * the port is not equal to -1. If the port is equal to -1, then
  947. * it calls <code>checkPermission</code> with the
  948. * <code>SocketPermission(host,"resolve")</code> permission.
  949. * <p>
  950. * If you override this method, then you should make a call to
  951. * <code>super.checkConnect</code>
  952. * at the point the overridden method would normally throw an
  953. * exception.
  954. *
  955. * @param host the host name port to connect to.
  956. * @param port the protocol port to connect to.
  957. * @exception SecurityException if the calling thread does not have
  958. * permission to open a socket connection to the specified
  959. * <code>host</code> and <code>port</code>.
  960. * @see #checkPermission(java.security.Permission) checkPermission
  961. */
  962. public void checkConnect(String host, int port) {
  963. if (host == null) {
  964. throw new NullPointerException("host can't be null");
  965. }
  966. if (port == -1) {
  967. checkPermission(new SocketPermission(host,"resolve"));
  968. } else {
  969. checkPermission(new SocketPermission(host+":"+port,"connect"));
  970. }
  971. }
  972. /**
  973. * Throws a <code>SecurityException</code> if the
  974. * specified security context is not allowed to open a socket
  975. * connection to the specified host and port number.
  976. * <p>
  977. * A port number of <code>-1</code> indicates that the calling
  978. * method is attempting to determine the IP address of the specified
  979. * host name.
  980. * <p> If <code>context</code> is not an instance of
  981. * <code>AccessControlContext</code> then a
  982. * <code>SecurityException</code> is thrown.
  983. * <p>
  984. * Otherwise, the port number is checked. If it is not equal
  985. * to -1, the <code>context</code>'s <code>checkPermission</code>
  986. * method is called with a
  987. * <code>SocketPermission(host+":"+port,"connect")</code> permission.
  988. * If the port is equal to -1, then
  989. * the <code>context</code>'s <code>checkPermission</code> method
  990. * is called with a
  991. * <code>SocketPermission(host,"resolve")</code> permission.
  992. * <p>
  993. * If you override this method, then you should make a call to
  994. * <code>super.checkConnect</code>
  995. * at the point the overridden method would normally throw an
  996. * exception.
  997. *
  998. * @param host the host name port to connect to.
  999. * @param port the protocol port to connect to.
  1000. * @param context a system-dependent security context.
  1001. * @exception SecurityException if the specified security context does
  1002. * not have permission to open a socket connection to the
  1003. * specified <code>host</code> and <code>port</code>.
  1004. * @see java.lang.SecurityManager#getSecurityContext()
  1005. * @see java.security.AccessControlContext#checkPermission(java.security.Permission)
  1006. */
  1007. public void checkConnect(String host, int port, Object context) {
  1008. if (host == null) {
  1009. throw new NullPointerException("host can't be null");
  1010. }
  1011. if (context instanceof AccessControlContext) {
  1012. if (port == -1) {
  1013. ((AccessControlContext)context).checkPermission(
  1014. new SocketPermission(host,"resolve"));
  1015. } else {
  1016. ((AccessControlContext)context).checkPermission(
  1017. new SocketPermission(host+":"+port,"connect"));
  1018. }
  1019. } else{
  1020. throw new SecurityException();
  1021. }
  1022. }
  1023. /**
  1024. * Throws a <code>SecurityException</code> if the
  1025. * calling thread is not allowed to wait for a connection request on
  1026. * the specified local port number.
  1027. * <p>
  1028. * If port is not 0, this method calls
  1029. * <code>checkPermission</code> with the
  1030. * <code>SocketPermission("localhost:"+port,"listen")</code>.
  1031. * If port is zero, this method calls <code>checkPermission</code>
  1032. * with <code>SocketPermission("localhost:1024-","listen").</code>
  1033. * <p>
  1034. * If you override this method, then you should make a call to
  1035. * <code>super.checkListen</code>
  1036. * at the point the overridden method would normally throw an
  1037. * exception.
  1038. *
  1039. * @param port the local port.
  1040. * @exception SecurityException if the calling thread does not have
  1041. * permission to listen on the specified port.
  1042. * @see #checkPermission(java.security.Permission) checkPermission
  1043. */
  1044. public void checkListen(int port) {
  1045. if (port == 0) {
  1046. if (localListenPermission == null)
  1047. localListenPermission =
  1048. new SocketPermission("localhost:1024-","listen");
  1049. checkPermission(localListenPermission);
  1050. } else {
  1051. checkPermission(new SocketPermission("localhost:"+port,"listen"));
  1052. }
  1053. }
  1054. private static SocketPermission localListenPermission;
  1055. /**
  1056. * Throws a <code>SecurityException</code> if the
  1057. * calling thread is not permitted to accept a socket connection from
  1058. * the specified host and port number.
  1059. * <p>
  1060. * This method is invoked for the current security manager by the
  1061. * <code>accept</code> method of class <code>ServerSocket</code>.
  1062. * <p>
  1063. * This method calls <code>checkPermission</code> with the
  1064. * <code>SocketPermission(host+":"+port,"accept")</code> permission.
  1065. * <p>
  1066. * If you override this method, then you should make a call to
  1067. * <code>super.checkAccept</code>
  1068. * at the point the overridden method would normally throw an
  1069. * exception.
  1070. *
  1071. * @param host the host name of the socket connection.
  1072. * @param port the port number of the socket connection.
  1073. * @exception SecurityException if the calling thread does not have
  1074. * permission to accept the connection.
  1075. * @see java.net.ServerSocket#accept()
  1076. * @see #checkPermission(java.security.Permission) checkPermission
  1077. */
  1078. public void checkAccept(String host, int port) {
  1079. if (host == null) {
  1080. throw new NullPointerException("host can't be null");
  1081. }
  1082. checkPermission(new SocketPermission(host+":"+port,"accept"));
  1083. }
  1084. /**
  1085. * Throws a <code>SecurityException</code> if the
  1086. * calling thread is not allowed to use
  1087. * (join/leave/send/receive) IP multicast.
  1088. * <p>
  1089. * This method calls <code>checkPermission</code> with the
  1090. * <code>java.net.SocketPermission(maddr.getHostAddress(),
  1091. * "accept,connect")</code> permission.
  1092. * <p>
  1093. * If you override this method, then you should make a call to
  1094. * <code>super.checkMulticast</code>
  1095. * at the point the overridden method would normally throw an
  1096. * exception.
  1097. *
  1098. * @param maddr Internet group address to be used.
  1099. * @exception SecurityException if the calling thread is not allowed to
  1100. * use (join/leave/send/receive) IP multicast.
  1101. * @since JDK1.1
  1102. * @see #checkPermission(java.security.Permission) checkPermission
  1103. */
  1104. public void checkMulticast(InetAddress maddr) {
  1105. checkPermission(new SocketPermission(maddr.getHostAddress(), "accept,connect"));
  1106. }
  1107. /**
  1108. * Throws a <code>SecurityException</code> if the
  1109. * calling thread is not allowed to use
  1110. * (join/leave/send/receive) IP multicast.
  1111. * <p>
  1112. * This method calls <code>checkPermission</code> with the
  1113. * <code>java.net.SocketPermission(maddr.getHostAddress(),
  1114. * "accept,connect")</code> permission.
  1115. * <p>
  1116. * If you override this method, then you should make a call to
  1117. * <code>super.checkMulticast</code>
  1118. * at the point the overridden method would normally throw an
  1119. * exception.
  1120. *
  1121. * @param maddr Internet group address to be used.
  1122. * @param ttl value in use, if it is multicast send.
  1123. * @exception SecurityException if the calling thread is not allowed to
  1124. * use (join/leave/send/receive) IP multicast.
  1125. * @since JDK1.1
  1126. * @see #checkPermission(java.security.Permission) checkPermission
  1127. */
  1128. public void checkMulticast(InetAddress maddr, byte ttl) {
  1129. checkPermission(new SocketPermission(maddr.getHostAddress(),
  1130. "accept,connect"));
  1131. }
  1132. /**
  1133. * Throws a <code>SecurityException</code> if the
  1134. * calling thread is not allowed to access or modify the system
  1135. * properties.
  1136. * <p>
  1137. * This method is used by the <code>getProperties</code> and
  1138. * <code>setProperties</code> methods of class <code>System</code>.
  1139. * <p>
  1140. * This method calls <code>checkPermission</code> with the
  1141. * <code>PropertyPermission("*", "read,write")</code> permission.
  1142. * <p>
  1143. * If you override this method, then you should make a call to
  1144. * <code>super.checkPropertiesAccess</code>
  1145. * at the point the overridden method would normally throw an
  1146. * exception.
  1147. * <p>
  1148. *
  1149. * @exception SecurityException if the calling thread does not have
  1150. * permission to access or modify the system properties.
  1151. * @see java.lang.System#getProperties()
  1152. * @see java.lang.System#setProperties(java.util.Properties)
  1153. * @see #checkPermission(java.security.Permission) checkPermission
  1154. */
  1155. public void checkPropertiesAccess() {
  1156. checkPermission(new PropertyPermission("*", "read,write"));
  1157. }
  1158. /**
  1159. * Throws a <code>SecurityException</code> if the
  1160. * calling thread is not allowed to access the system property with
  1161. * the specified <code>key</code> name.
  1162. * <p>
  1163. * This method is used by the <code>getProperty</code> method of
  1164. * class <code>System</code>.
  1165. * <p>
  1166. * This method calls <code>checkPermission</code> with the
  1167. * <code>PropertyPermission(key, "read")</code> permission.
  1168. * <p>
  1169. * <p>
  1170. * If you override this method, then you should make a call to
  1171. * <code>super.checkPropertyAccess</code>
  1172. * at the point the overridden method would normally throw an
  1173. * exception.
  1174. *
  1175. * @param key a system property key.
  1176. * @exception SecurityException if the calling thread does not have
  1177. * permission to access the specified system property.
  1178. * @see java.lang.System#getProperty(java.lang.String)
  1179. * @see #checkPermission(java.security.Permission) checkPermission
  1180. */
  1181. public void checkPropertyAccess(String key) {
  1182. checkPermission(new PropertyPermission(key,"read"));
  1183. }
  1184. /**
  1185. * Returns <code>false</code> if the calling
  1186. * thread is not trusted to bring up the top-level window indicated
  1187. * by the <code>window</code> argument. In this case, the caller can
  1188. * still decide to show the window, but the window should include
  1189. * some sort of visual warning. If the method returns
  1190. * <code>true</code>, then the window can be shown without any
  1191. * special restrictions.
  1192. * <p>
  1193. * See class <code>Window</code> for more information on trusted and
  1194. * untrusted windows.
  1195. * <p>
  1196. * This method calls
  1197. * <code>checkPermission</code> with the
  1198. * <code>AWTPermission("showWindowWithoutWarningBanner")</code> permission,
  1199. * and returns <code>true</code> if a SecurityException is not thrown,
  1200. * otherwise it returns <code>false</code>.
  1201. * <p>
  1202. * If you override this method, then you should make a call to
  1203. * <code>super.checkTopLevelWindow</code>
  1204. * at the point the overridden method would normally return
  1205. * <code>false</code>, and the value of
  1206. * <code>super.checkTopLevelWindow</code> should
  1207. * be returned.
  1208. *
  1209. * @param window the new window that is being created.
  1210. * @return <code>true</code> if the calling thread is trusted to put up
  1211. * top-level windows; <code>false</code> otherwise.
  1212. * @exception SecurityException if creation is disallowed entirely.
  1213. * @see java.awt.Window
  1214. * @see #checkPermission(java.security.Permission) checkPermission
  1215. */
  1216. public boolean checkTopLevelWindow(Object window) {
  1217. if (window == null) {
  1218. throw new NullPointerException("window can't be null");
  1219. }
  1220. try {
  1221. if (topLevelWindowPermission == null)
  1222. topLevelWindowPermission =
  1223. new AWTPermission("showWindowWithoutWarningBanner");
  1224. checkPermission(topLevelWindowPermission);
  1225. return true;
  1226. } catch (SecurityException se) {
  1227. // just return false
  1228. }
  1229. return false;
  1230. }
  1231. /**
  1232. * Throws a <code>SecurityException</code> if the
  1233. * calling thread is not allowed to initiate a print job request.
  1234. * <p>
  1235. * This method calls
  1236. * <code>checkPermission</code> with the
  1237. * <code>RuntimePermission("queuePrintJob")</code> permission.
  1238. * <p>
  1239. * If you override this method, then you should make a call to
  1240. * <code>super.checkPrintJobAccess</code>
  1241. * at the point the overridden method would normally throw an
  1242. * exception.
  1243. * <p>
  1244. *
  1245. * @exception SecurityException if the calling thread does not have
  1246. * permission to initiate a print job request.
  1247. * @since JDK1.1
  1248. * @see #checkPermission(java.security.Permission) checkPermission
  1249. */
  1250. public void checkPrintJobAccess() {
  1251. checkPermission(new RuntimePermission("queuePrintJob"));
  1252. }
  1253. /**
  1254. * Throws a <code>SecurityException</code> if the
  1255. * calling thread is not allowed to access the system clipboard.
  1256. * <p>
  1257. * This method calls <code>checkPermission</code> with the
  1258. * <code>AWTPermission("accessClipboard")</code>
  1259. * permission.
  1260. * <p>
  1261. * If you override this method, then you should make a call to
  1262. * <code>super.checkSystemClipboardAccess</code>
  1263. * at the point the overridden method would normally throw an
  1264. * exception.
  1265. *
  1266. * @since JDK1.1
  1267. * @exception SecurityException if the calling thread does not have
  1268. * permission to access the system clipboard.
  1269. * @see #checkPermission(java.security.Permission) checkPermission
  1270. */
  1271. public void checkSystemClipboardAccess() {
  1272. if (accessClipboardPermission == null)
  1273. accessClipboardPermission = new AWTPermission("accessClipboard");
  1274. checkPermission(accessClipboardPermission);
  1275. }
  1276. /**
  1277. * Throws a <code>SecurityException</code> if the
  1278. * calling thread is not allowed to access the AWT event queue.
  1279. * <p>
  1280. * This method calls <code>checkPermission</code> with the
  1281. * <code>AWTPermission("accessEventQueue")</code> permission.
  1282. * <p>
  1283. * If you override this method, then you should make a call to
  1284. * <code>super.checkAwtEventQueueAccess</code>
  1285. * at the point the overridden method would normally throw an
  1286. * exception.
  1287. *
  1288. * @since JDK1.1
  1289. * @exception SecurityException if the calling thread does not have
  1290. * permission to access the AWT event queue.
  1291. * @see #checkPermission(java.security.Permission) checkPermission
  1292. */
  1293. public void checkAwtEventQueueAccess() {
  1294. if (checkAwtEventQueuePermission == null)
  1295. checkAwtEventQueuePermission =
  1296. new AWTPermission("accessEventQueue");
  1297. checkPermission(checkAwtEventQueuePermission);
  1298. }
  1299. private static String[] packageAccess =
  1300. getPackages("package.access");
  1301. private static String[] packageDefinition =
  1302. getPackages("package.definition");
  1303. private static String[] getPackages(String prop) {
  1304. String packages[] = null;
  1305. String p = java.security.Security.getProperty(prop);
  1306. if (p != null && !p.equals("")) {
  1307. java.util.StringTokenizer tok =
  1308. new java.util.StringTokenizer(p, ",");
  1309. int n = tok.countTokens();
  1310. if (n > 0) {
  1311. packages = new String[n];
  1312. int i = 0;
  1313. while (tok.hasMoreElements()) {
  1314. String s = tok.nextToken();
  1315. packages[i++] = s;
  1316. }
  1317. }
  1318. }
  1319. if (packages == null)
  1320. packages = new String[0];
  1321. return packages;
  1322. }
  1323. /**
  1324. * Throws a <code>SecurityException</code> if the
  1325. * calling thread is not allowed to access the package specified by
  1326. * the argument.
  1327. * <p>
  1328. * This method is used by the <code>loadClass</code> method of class
  1329. * loaders.
  1330. * <p>
  1331. * This method first gets a list of
  1332. * restricted packages by obtaining a comma-separated list from
  1333. * a call to
  1334. * <code>java.security.Security.getProperty("package.access")</code>,
  1335. * and checks to see if <code>pkg</code> starts with or equals
  1336. * any of the restricted packages. If it does, then
  1337. * <code>checkPermission</code> gets called with the
  1338. * <code>RuntimePermission("accessClassInPackage."+pkg)</code>
  1339. * permission.
  1340. * <p>
  1341. * If this method is overridden, then
  1342. * <code>super.checkPackageAccess</code> should be called
  1343. * as the first line in the overridden method.
  1344. *
  1345. * @param pkg the package name.
  1346. * @exception SecurityException if the calling thread does not have
  1347. * permission to access the specified package.
  1348. * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
  1349. * loadClass
  1350. * @see java.security.Security#getProperty getProperty
  1351. * @see #checkPermission(java.security.Permission) checkPermission
  1352. */
  1353. public void checkPackageAccess(String pkg) {
  1354. if (pkg == null) {
  1355. throw new NullPointerException("package name can't be null");
  1356. }
  1357. for (int i = 0; i < packageAccess.length; i++) {
  1358. if (pkg.startsWith(packageAccess[i]) ||
  1359. (pkg.equals(packageAccess[i]))) {
  1360. checkPermission(
  1361. new RuntimePermission("accessClassInPackage."+pkg));
  1362. }
  1363. }
  1364. }
  1365. /**
  1366. * Throws a <code>SecurityException</code> if the
  1367. * calling thread is not allowed to define classes in the package
  1368. * specified by the argument.
  1369. * <p>
  1370. * This method is used by the <code>loadClass</code> method of some
  1371. * class loaders.
  1372. * <p>
  1373. * This method first gets a list of restricted packages by
  1374. * obtaining a comma-separated list from a call to
  1375. * <code>java.security.Security.getProperty("package.definition")</code>,
  1376. * and checks to see if <code>pkg</code> starts with or equals
  1377. * any of the restricted packages. If it does, then
  1378. * <code>checkPermission</code> gets called with the
  1379. * <code>RuntimePermission("defineClassInPackage."+pkg)</code>
  1380. * permission.
  1381. * <p>
  1382. * If this method is overridden, then
  1383. * <code>super.checkPackageDefinition</code> should be called
  1384. * as the first line in the overridden method.
  1385. *
  1386. * @param pkg the package name.
  1387. * @exception SecurityException if the calling thread does not have
  1388. * permission to define classes in the specified package.
  1389. * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
  1390. * @see java.security.Security#getProperty getProperty
  1391. * @see #checkPermission(java.security.Permission) checkPermission
  1392. */
  1393. public void checkPackageDefinition(String pkg) {
  1394. if (pkg == null) {
  1395. throw new NullPointerException("package name can't be null");
  1396. }
  1397. for (int i = 0; i < packageDefinition.length; i++) {
  1398. if (pkg.startsWith(packageDefinition[i]) ||
  1399. pkg.equals(packageDefinition[i])) {
  1400. checkPermission(
  1401. new RuntimePermission("defineClassInPackage."+pkg));
  1402. }
  1403. }
  1404. }
  1405. /**
  1406. * Throws a <code>SecurityException</code> if the
  1407. * calling thread is not allowed to set the socket factory used by
  1408. * <code>ServerSocket</code> or <code>Socket</code>, or the stream
  1409. * handler factory used by <code>URL</code>.
  1410. * <p>
  1411. * This method calls <code>checkPermission</code> with the
  1412. * <code>RuntimePermission("setFactory")</code> permission.
  1413. * <p>
  1414. * If you override this method, then you should make a call to
  1415. * <code>super.checkSetFactory</code>
  1416. * at the point the overridden method would normally throw an
  1417. * exception.
  1418. * <p>
  1419. *
  1420. * @exception SecurityException if the calling thread does not have
  1421. * permission to specify a socket factory or a stream
  1422. * handler factory.
  1423. *
  1424. * @see java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory) setSocketFactory
  1425. * @see java.net.Socket#setSocketImplFactory(java.net.SocketImplFactory) setSocketImplFactory
  1426. * @see java.net.URL#setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory) setURLStreamHandlerFactory
  1427. * @see #checkPermission(java.security.Permission) checkPermission
  1428. */
  1429. public void checkSetFactory() {
  1430. checkPermission(new RuntimePermission("setFactory"));
  1431. }
  1432. /**
  1433. * Throws a <code>SecurityException</code> if the
  1434. * calling thread is not allowed to access members.
  1435. * <p>
  1436. * The default policy is to allow access to PUBLIC members, as well
  1437. * as access to classes that have the same class loader as the caller.
  1438. * In all other cases, this method calls <code>checkPermission</code>
  1439. * with the <code>RuntimePermission("accessDeclaredMembers")
  1440. * </code> permission.
  1441. * <p>
  1442. * If this method is overridden, then a call to
  1443. * <code>super.checkMemberAccess</code> cannot be made,
  1444. * as the default implementation of <code>checkMemberAccess</code>
  1445. * relies on the code being checked being at a stack depth of
  1446. * 4.
  1447. *
  1448. * @param clazz the class that reflection is to be performed on.
  1449. *
  1450. * @param which type of access, PUBLIC or DECLARED.
  1451. *
  1452. * @exception SecurityException if the caller does not have
  1453. * permission to access members.
  1454. *
  1455. * @see java.lang.reflect.Member
  1456. * @since JDK1.1
  1457. * @see #checkPermission(java.security.Permission) checkPermission
  1458. */
  1459. public void checkMemberAccess(Class clazz, int which) {
  1460. if (clazz == null) {
  1461. throw new NullPointerException("class can't be null");
  1462. }
  1463. if (which != Member.PUBLIC) {
  1464. Class stack[] = getClassContext();
  1465. /*
  1466. * stack depth of 4 should be the caller of one of the
  1467. * methods in java.lang.Class that invoke checkMember
  1468. * access. The stack should look like:
  1469. *
  1470. * someCaller [3]
  1471. * java.lang.Class.someReflectionAPI [2]
  1472. * java.lang.Class.checkMemeberAccess [1]
  1473. * SecurityManager.checkMemeberAccess [0]
  1474. *
  1475. */
  1476. if ((stack.length<4) ||
  1477. (stack[3].getClassLoader() != clazz.getClassLoader())) {
  1478. if (checkMemberAccessPermission == null)
  1479. checkMemberAccessPermission =
  1480. new RuntimePermission("accessDeclaredMembers");
  1481. checkPermission(checkMemberAccessPermission);
  1482. }
  1483. }
  1484. }
  1485. /**
  1486. * Determines whether the permission with the specified permission target
  1487. * name should be granted or denied.
  1488. *
  1489. * <p> If the requested permission is allowed, this method returns
  1490. * quietly. If denied, a SecurityException is raised.
  1491. *
  1492. * <p> This method creates a <code>SecurityPermission</code> object for
  1493. * the given permission target name and calls <code>checkPermission</code>
  1494. * with it.
  1495. *
  1496. * <p> See the documentation for
  1497. * <code>{@link java.security.SecurityPermission}</code> for
  1498. * a list of possible permission target names.
  1499. *
  1500. * <p> If you override this method, then you should make a call to
  1501. * <code>super.checkSecurityAccess</code>
  1502. * at the point the overridden method would normally throw an
  1503. * exception.
  1504. *
  1505. * @param target the target name of the <code>SecurityPermission</code>.
  1506. *
  1507. * @exception SecurityException if the calling thread does not have
  1508. * permission for the requested access.
  1509. *
  1510. * @since JDK1.1
  1511. * @see #checkPermission(java.security.Permission) checkPermission
  1512. */
  1513. public void checkSecurityAccess(String target) {
  1514. checkPermission(new SecurityPermission(target));
  1515. }
  1516. private native Class currentLoadedClass0();
  1517. /**
  1518. * Returns the thread group into which to instantiate any new
  1519. * thread being created at the time this is being called.
  1520. * By default, it returns the thread group of the current
  1521. * thread. This should be overridden by a specific security
  1522. * manager to return the appropriate thread group.
  1523. *
  1524. * @since JDK1.1
  1525. * @see java.lang.ThreadGroup
  1526. */
  1527. public ThreadGroup getThreadGroup() {
  1528. return Thread.currentThread().getThreadGroup();
  1529. }
  1530. }