1. /*
  2. * @(#)Policy.java 1.48 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.security.auth;
  8. /**
  9. * <p> This is an abstract class for representing the system policy for
  10. * Subject-based authorization. A subclass implementation
  11. * of this class provides a means to specify a Subject-based
  12. * access control <code>Policy</code>.
  13. *
  14. * <p> A <code>Policy</code> object can be queried for the set of
  15. * Permissions granted to code running as a
  16. * <code>Principal</code> in the following manner:
  17. *
  18. * <pre>
  19. * policy = Policy.getPolicy();
  20. * PermissionCollection perms = policy.getPermissions(subject,
  21. * codeSource);
  22. * </pre>
  23. *
  24. * The <code>Policy</code> object consults the local policy and returns
  25. * and appropriate <code>Permissions</code> object with the
  26. * Permissions granted to the Principals associated with the
  27. * provided <i>subject</i>, and granted to the code specified
  28. * by the provided <i>codeSource</i>.
  29. *
  30. * <p> A <code>Policy</code> contains the following information.
  31. * Note that this example only represents the syntax for the default
  32. * <code>Policy</code> implementation. Subclass implementations of this class
  33. * may implement alternative syntaxes and may retrieve the
  34. * <code>Policy</code> from any source such as files, databases,
  35. * or servers.
  36. *
  37. * <p> Each entry in the <code>Policy</code> is represented as
  38. * a <b><i>grant</i></b> entry. Each <b><i>grant</i></b> entry
  39. * specifies a codebase, code signers, and Principals triplet,
  40. * as well as the Permissions granted to that triplet.
  41. *
  42. * <pre>
  43. * grant CodeBase ["URL"], Signedby ["signers"],
  44. * Principal [Principal_Class] "Principal_Name" {
  45. * Permission Permission_Class ["Target_Name"]
  46. * [, "Permission_Actions"]
  47. * [, signedBy "SignerName"];
  48. * };
  49. * </pre>
  50. *
  51. * The CodeBase and Signedby components of the triplet name/value pairs
  52. * are optional. If they are not present, then any any codebase will match,
  53. * and any signer (including unsigned code) will match.
  54. * For Example,
  55. *
  56. * <pre>
  57. * grant CodeBase "foo.com", Signedby "foo",
  58. * Principal com.sun.security.auth.SolarisPrincipal "duke" {
  59. * permission java.io.FilePermission "/home/duke", "read, write";
  60. * };
  61. * </pre>
  62. *
  63. * This <b><i>grant</i></b> entry specifies that code from "foo.com",
  64. * signed by "foo', and running as a <code>SolarisPrincipal</code> with the
  65. * name, duke, has one <code>Permission</code>. This <code>Permission</code>
  66. * permits the executing code to read and write files in the directory,
  67. * "/home/duke".
  68. *
  69. * <p> To "run" as a particular <code>Principal</code>,
  70. * code invokes the <code>Subject.doAs(subject, ...)</code> method.
  71. * After invoking that method, the code runs as all the Principals
  72. * associated with the specified <code>Subject</code>.
  73. * Note that this <code>Policy</code> (and the Permissions
  74. * granted in this <code>Policy</code>) only become effective
  75. * after the call to <code>Subject.doAs</code> has occurred.
  76. *
  77. * <p> Multiple Principals may be listed within one <b><i>grant</i></b> entry.
  78. * All the Principals in the grant entry must be associated with
  79. * the <code>Subject</code> provided to <code>Subject.doAs</code>
  80. * for that <code>Subject</code> to be granted the specified Permissions.
  81. *
  82. * <pre>
  83. * grant Principal com.sun.security.auth.SolarisPrincipal "duke",
  84. * Principal com.sun.security.auth.SolarisNumericUserPrincipal "0" {
  85. * permission java.io.FilePermission "/home/duke", "read, write";
  86. * permission java.net.SocketPermission "duke.com", "connect";
  87. * };
  88. * </pre>
  89. *
  90. * This entry grants any code running as both "duke" and "0"
  91. * permission to read and write files in duke's home directory,
  92. * as well as permission to make socket connections to "duke.com".
  93. *
  94. * <p> Note that non Principal-based grant entries are not permitted
  95. * in this <code>Policy</code>. Therefore, grant entries such as:
  96. *
  97. * <pre>
  98. * grant CodeBase "foo.com", Signedby "foo" {
  99. * permission java.io.FilePermission "/tmp/scratch", "read, write";
  100. * };
  101. * </pre>
  102. *
  103. * are rejected. Such permission must be listed in the
  104. * <code>java.security.Policy</code>.
  105. *
  106. * <p> The default <code>Policy</code> implementation can be changed by
  107. * setting the value of the "auth.policy.provider" security property
  108. * (in the Java security properties file) to the fully qualified name of
  109. * the desired <code>Policy</code> implementation class.
  110. * The Java security properties file is located in the file named
  111. * <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME>
  112. * refers to the directory where the JDK was installed.
  113. *
  114. * @deprecated as of JDK version 1.4 -- Replaced by java.security.Policy.
  115. * java.security.Policy has a method:
  116. * <pre>
  117. * public PermissionCollection getPermissions
  118. * (java.security.ProtectionDomain pd)
  119. *
  120. * </pre>
  121. * and ProtectionDomain has a constructor:
  122. * <pre>
  123. * public ProtectionDomain
  124. * (CodeSource cs,
  125. * PermissionCollection permissions,
  126. * ClassLoader loader,
  127. * Principal[] principals)
  128. * </pre>
  129. *
  130. * These two APIs provide callers the means to query the
  131. * Policy for Principal-based Permission entries.
  132. *
  133. *
  134. * @version 1.48, 01/23/03
  135. */
  136. public abstract class Policy {
  137. private static Policy policy;
  138. private static ClassLoader contextClassLoader;
  139. static {
  140. contextClassLoader =
  141. (ClassLoader)java.security.AccessController.doPrivileged
  142. (new java.security.PrivilegedAction() {
  143. public Object run() {
  144. return Thread.currentThread().getContextClassLoader();
  145. }
  146. });
  147. };
  148. /**
  149. * Sole constructor. (For invocation by subclass constructors, typically
  150. * implicit.)
  151. */
  152. protected Policy() { }
  153. /**
  154. * Returns the installed Policy object.
  155. * This method first calls
  156. * <code>SecurityManager.checkPermission</code> with the
  157. * <code>AuthPermission("getPolicy")</code> permission
  158. * to ensure the caller has permission to get the Policy object.
  159. *
  160. * <p>
  161. *
  162. * @return the installed Policy. The return value cannot be
  163. * <code>null</code>.
  164. *
  165. * @exception java.lang.SecurityException if the current thread does not
  166. * have permission to get the Policy object.
  167. *
  168. * @see #setPolicy
  169. */
  170. public static Policy getPolicy() {
  171. java.lang.SecurityManager sm = System.getSecurityManager();
  172. if (sm != null) sm.checkPermission(new AuthPermission("getPolicy"));
  173. return getPolicyNoCheck();
  174. }
  175. /**
  176. * Returns the installed Policy object, skipping the security check.
  177. *
  178. * @return the installed Policy.
  179. *
  180. */
  181. static Policy getPolicyNoCheck() {
  182. if (policy == null) {
  183. synchronized(Policy.class) {
  184. if (policy == null) {
  185. String policy_class = null;
  186. policy_class = (String)
  187. java.security.AccessController.doPrivileged
  188. (new java.security.PrivilegedAction() {
  189. public Object run() {
  190. return java.security.Security.getProperty
  191. ("auth.policy.provider");
  192. }
  193. });
  194. if (policy_class == null) {
  195. policy_class = "com.sun.security.auth.PolicyFile";
  196. }
  197. try {
  198. final String finalClass = policy_class;
  199. policy = (Policy)
  200. java.security.AccessController.doPrivileged
  201. (new java.security.PrivilegedExceptionAction() {
  202. public Object run() throws ClassNotFoundException,
  203. InstantiationException,
  204. IllegalAccessException {
  205. return Class.forName
  206. (finalClass,
  207. true,
  208. contextClassLoader).newInstance();
  209. }
  210. });
  211. } catch (Exception e) {
  212. throw new SecurityException
  213. (sun.security.util.ResourcesMgr.getString
  214. ("unable to instantiate Subject-based policy"));
  215. }
  216. }
  217. }
  218. }
  219. return policy;
  220. }
  221. /**
  222. * Sets the system-wide Policy object. This method first calls
  223. * <code>SecurityManager.checkPermission</code> with the
  224. * <code>AuthPermission("setPolicy")</code>
  225. * permission to ensure the caller has permission to set the Policy.
  226. *
  227. * <p>
  228. *
  229. * @param policy the new system Policy object.
  230. *
  231. * @exception java.lang.SecurityException if the current thread does not
  232. * have permission to set the Policy.
  233. *
  234. * @see #getPolicy
  235. */
  236. public static void setPolicy(Policy policy) {
  237. java.lang.SecurityManager sm = System.getSecurityManager();
  238. if (sm != null) sm.checkPermission(new AuthPermission("setPolicy"));
  239. Policy.policy = policy;
  240. }
  241. /**
  242. * Retrieve the Permissions granted to the Principals associated with
  243. * the specified <code>CodeSource</code>.
  244. *
  245. * <p>
  246. *
  247. * @param subject the <code>Subject</code>
  248. * whose associated Principals,
  249. * in conjunction with the provided
  250. * <code>CodeSource</code>, determines the Permissions
  251. * returned by this method. This parameter
  252. * may be <code>null</code>. <p>
  253. *
  254. * @param cs the code specified by its <code>CodeSource</code>
  255. * that determines, in conjunction with the provided
  256. * <code>Subject</code>, the Permissions
  257. * returned by this method. This parameter may be
  258. * <code>null</code>.
  259. *
  260. * @return the Collection of Permissions granted to all the
  261. * <code>Subject</code> and code specified in
  262. * the provided <i>subject</i> and <i>cs</i>
  263. * parameters.
  264. */
  265. public abstract java.security.PermissionCollection getPermissions
  266. (Subject subject,
  267. java.security.CodeSource cs);
  268. /**
  269. * Refresh and reload the Policy.
  270. *
  271. * <p>This method causes this object to refresh/reload its current
  272. * Policy. This is implementation-dependent.
  273. * For example, if the Policy object is stored in
  274. * a file, calling <code>refresh</code> will cause the file to be re-read.
  275. *
  276. * <p>
  277. *
  278. * @exception SecurityException if the caller does not have permission
  279. * to refresh the Policy.
  280. */
  281. public abstract void refresh();
  282. }