1. /*
  2. * @(#)Policy.java 1.62 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.security;
  8. import java.io.*;
  9. import java.lang.RuntimePermission;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.util.Enumeration;
  13. import java.util.Hashtable;
  14. import java.util.Vector;
  15. import java.util.StringTokenizer;
  16. import java.util.PropertyPermission;
  17. import java.lang.reflect.*;
  18. /**
  19. * This is an abstract class for representing the system security
  20. * policy for a Java application environment (specifying
  21. * which permissions are available for code from various sources).
  22. * That is, the security policy is represented by a Policy subclass
  23. * providing an implementation of the abstract methods
  24. * in this Policy class.
  25. *
  26. * <p>There is only one Policy object in effect at any given time.
  27. * It is consulted by
  28. * a ProtectionDomain when the protection domain initializes its set of
  29. * permissions. <p>
  30. *
  31. * <p>The source location for the policy information utilized by the
  32. * Policy object is up to the Policy implementation.
  33. * The policy configuration may be stored, for example, as a
  34. * flat ASCII file, as a serialized binary file of
  35. * the Policy class, or as a database. <p>
  36. *
  37. * <p>The currently-installed Policy object can be obtained by
  38. * calling the <code>getPolicy</code> method, and it can be
  39. * changed by a call to the <code>setPolicy</code> method (by
  40. * code with permission to reset the Policy).
  41. *
  42. * <p>The <code>refresh</code> method causes the policy
  43. * object to refresh/reload its current configuration. This is
  44. * implementation-dependent. For example, if the policy object stores
  45. * its policy in configuration files, calling <code>refresh</code> will
  46. * cause it to re-read the configuration policy files.
  47. *
  48. * <p>When a protection domain needs to initialize its set of
  49. * permissions, it executes code such as the following
  50. * to ask the currently installed Policy object to populate a
  51. * PermissionCollection object with the appropriate permissions:
  52. * <pre>
  53. * policy = Policy.getPolicy();
  54. * PermissionCollection perms = policy.getPermissions(MyCodeSource)
  55. * </pre>
  56. *
  57. * <p>The protection domain passes in a CodeSource
  58. * object, which encapsulates its codebase (URL) and public key attributes.
  59. * The Policy object evaluates the global policy and
  60. * returns an appropriate Permissions object specifying
  61. * the permissions allowed for code from the specified code source.
  62. *
  63. * <p>The default Policy implementation can be changed by setting the
  64. * value of the "policy.provider" security property (in the Java
  65. * security properties file) to the fully qualified name of
  66. * the desired Policy implementation class.
  67. * The Java security properties file is located in the file named
  68. * <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME>
  69. * refers to the directory where the JDK was installed.
  70. *
  71. * @author Roland Schemers
  72. * @version 1.59, 09/24/98
  73. * @see java.security.CodeSource
  74. * @see java.security.PermissionCollection
  75. */
  76. public abstract class Policy {
  77. /** the system-wide policy. */
  78. private static Policy policy; // package private for AccessControlContext
  79. /** package private for AccessControlContext */
  80. static boolean isSet()
  81. {
  82. return policy != null;
  83. }
  84. /**
  85. * Returns the installed Policy object. This value should not be cached,
  86. * as it may be changed by a call to <code>setPolicy</code>.
  87. * This method first calls
  88. * <code>SecurityManager.checkPermission</code> with a
  89. * <code>SecurityPermission("getPolicy")</code> permission
  90. * to ensure it's ok to get the Policy object..
  91. *
  92. * @return the installed Policy.
  93. *
  94. * @throws SecurityException
  95. * if a security manager exists and its
  96. * <code>checkPermission</code> method doesn't allow
  97. * getting the Policy object.
  98. *
  99. * @see SecurityManager#checkPermission
  100. */
  101. public static Policy getPolicy()
  102. {
  103. SecurityManager sm = System.getSecurityManager();
  104. if (sm != null) sm.checkPermission(new SecurityPermission
  105. ("getPolicy"));
  106. return getPolicyNoCheck();
  107. }
  108. /**
  109. * Returns the installed Policy object, skipping the security check.
  110. * Used by ProtectionDomain and getPolicy.
  111. *
  112. * @return the installed Policy.
  113. *
  114. */
  115. static Policy getPolicyNoCheck()
  116. {
  117. if (policy == null) {
  118. synchronized(Policy.class) {
  119. if (policy == null) {
  120. String policy_class = null;
  121. policy_class = (String)AccessController.doPrivileged(
  122. new PrivilegedAction() {
  123. public Object run() {
  124. return Security.getProperty("policy.provider");
  125. }
  126. });
  127. if (policy_class == null) {
  128. policy_class = "sun.security.provider.PolicyFile";
  129. }
  130. try {
  131. policy = (Policy)
  132. Class.forName(policy_class).newInstance();
  133. } catch (Exception e) {
  134. policy = new sun.security.provider.PolicyFile();
  135. }
  136. }
  137. }
  138. }
  139. return policy;
  140. }
  141. /**
  142. * Sets the system-wide Policy object. This method first calls
  143. * <code>SecurityManager.checkPermission</code> with a
  144. * <code>SecurityPermission("setPolicy")</code>
  145. * permission to ensure it's ok to set the Policy.
  146. *
  147. * @param policy the new system Policy object.
  148. *
  149. * @throws SecurityException
  150. * if a security manager exists and its
  151. * <code>checkPermission</code> method doesn't allow
  152. * setting the Policy.
  153. *
  154. * @see SecurityManager#checkPermission
  155. *
  156. */
  157. public static void setPolicy(Policy policy)
  158. {
  159. SecurityManager sm = System.getSecurityManager();
  160. if (sm != null) sm.checkPermission(
  161. new SecurityPermission("setPolicy"));
  162. Policy.policy = policy;
  163. }
  164. /**
  165. * Evaluates the global policy and returns a
  166. * PermissionCollection object specifying the set of
  167. * permissions allowed for code from the specified
  168. * code source.
  169. *
  170. * @param codesource the CodeSource associated with the caller.
  171. * This encapsulates the original location of the code (where the code
  172. * came from) and the public key(s) of its signer.
  173. *
  174. * @return the set of permissions allowed for code from <i>codesource</i>
  175. * according to the policy.
  176. *
  177. * @exception java.lang.SecurityException if the current thread does not
  178. * have permission to call <code>getPermissions</code> on the policy object.
  179. */
  180. public abstract PermissionCollection getPermissions(CodeSource codesource);
  181. /**
  182. * Refreshes/reloads the policy configuration. The behavior of this method
  183. * depends on the implementation. For example, calling <code>refresh</code>
  184. * on a file-based policy will cause the file to be re-read.
  185. *
  186. * @exception java.lang.SecurityException if the current thread does not
  187. * have permission to refresh this Policy object.
  188. */
  189. public abstract void refresh();
  190. }