1. /*
  2. * @(#)PropertyPermission.java 1.30 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 java.util;
  8. import java.io.Serializable;
  9. import java.io.IOException;
  10. import java.security.*;
  11. import java.util.Map;
  12. import java.util.HashMap;
  13. import java.util.Enumeration;
  14. import java.util.Hashtable;
  15. import java.util.Collections;
  16. import java.io.ObjectStreamField;
  17. import java.io.ObjectOutputStream;
  18. import java.io.ObjectInputStream;
  19. import java.io.IOException;
  20. import sun.security.util.SecurityConstants;
  21. /**
  22. * This class is for property permissions.
  23. *
  24. * <P>
  25. * The name is the name of the property ("java.home",
  26. * "os.name", etc). The naming
  27. * convention follows the hierarchical property naming convention.
  28. * Also, an asterisk
  29. * may appear at the end of the name, following a ".", or by itself, to
  30. * signify a wildcard match. For example: "java.*" or "*" is valid,
  31. * "*java" or "a*b" is not valid.
  32. * <P>
  33. * <P>
  34. * The actions to be granted are passed to the constructor in a string containing
  35. * a list of zero or more comma-separated keywords. The possible keywords are
  36. * "read" and "write". Their meaning is defined as follows:
  37. * <P>
  38. * <DL>
  39. * <DT> read
  40. * <DD> read permission. Allows <code>System.getProperty</code> to
  41. * be called.
  42. * <DT> write
  43. * <DD> write permission. Allows <code>System.setProperty</code> to
  44. * be called.
  45. * </DL>
  46. * <P>
  47. * The actions string is converted to lowercase before processing.
  48. * <P>
  49. * Care should be taken before granting code permission to access
  50. * certain system properties. For example, granting permission to
  51. * access the "java.home" system property gives potentially malevolent
  52. * code sensitive information about the system environment (the Java
  53. * installation directory). Also, granting permission to access
  54. * the "user.name" and "user.home" system properties gives potentially
  55. * malevolent code sensitive information about the user environment
  56. * (the user's account name and home directory).
  57. *
  58. * @see java.security.BasicPermission
  59. * @see java.security.Permission
  60. * @see java.security.Permissions
  61. * @see java.security.PermissionCollection
  62. * @see java.lang.SecurityManager
  63. *
  64. * @version 1.30 03/01/23
  65. *
  66. * @author Roland Schemers
  67. * @since 1.2
  68. *
  69. * @serial exclude
  70. */
  71. public final class PropertyPermission extends BasicPermission {
  72. /**
  73. * Read action.
  74. */
  75. private final static int READ = 0x1;
  76. /**
  77. * Write action.
  78. */
  79. private final static int WRITE = 0x2;
  80. /**
  81. * All actions (read,write);
  82. */
  83. private final static int ALL = READ|WRITE;
  84. /**
  85. * No actions.
  86. */
  87. private final static int NONE = 0x0;
  88. /**
  89. * The actions mask.
  90. *
  91. */
  92. private transient int mask;
  93. /**
  94. * The actions string.
  95. *
  96. * @serial
  97. */
  98. private String actions; // Left null as long as possible, then
  99. // created and re-used in the getAction function.
  100. /**
  101. * initialize a PropertyPermission object. Common to all constructors.
  102. * Also called during de-serialization.
  103. *
  104. * @param mask the actions mask to use.
  105. *
  106. */
  107. private void init(int mask)
  108. {
  109. if ((mask & ALL) != mask)
  110. throw new IllegalArgumentException("invalid actions mask");
  111. if (mask == NONE)
  112. throw new IllegalArgumentException("invalid actions mask");
  113. if (getName() == null)
  114. throw new NullPointerException("name can't be null");
  115. this.mask = mask;
  116. }
  117. /**
  118. * Creates a new PropertyPermission object with the specified name.
  119. * The name is the name of the system property, and
  120. * <i>actions</i> contains a comma-separated list of the
  121. * desired actions granted on the property. Possible actions are
  122. * "read" and "write".
  123. *
  124. * @param name the name of the PropertyPermission.
  125. * @param actions the actions string.
  126. */
  127. public PropertyPermission(String name, String actions)
  128. {
  129. super(name,actions);
  130. init(getMask(actions));
  131. }
  132. /**
  133. * Checks if this PropertyPermission object "implies" the specified
  134. * permission.
  135. * <P>
  136. * More specifically, this method returns true if:<p>
  137. * <ul>
  138. * <li> <i>p</i> is an instanceof PropertyPermission,<p>
  139. * <li> <i>p</i>'s actions are a subset of this
  140. * object's actions, and <p>
  141. * <li> <i>p</i>'s name is implied by this object's
  142. * name. For example, "java.*" implies "java.home".
  143. * </ul>
  144. * @param p the permission to check against.
  145. *
  146. * @return true if the specified permission is implied by this object,
  147. * false if not.
  148. */
  149. public boolean implies(Permission p) {
  150. if (!(p instanceof PropertyPermission))
  151. return false;
  152. PropertyPermission that = (PropertyPermission) p;
  153. // we get the effective mask. i.e., the "and" of this and that.
  154. // They must be equal to that.mask for implies to return true.
  155. return ((this.mask & that.mask) == that.mask) && super.implies(that);
  156. }
  157. /**
  158. * Checks two PropertyPermission objects for equality. Checks that <i>obj</i> is
  159. * a PropertyPermission, and has the same name and actions as this object.
  160. * <P>
  161. * @param obj the object we are testing for equality with this object.
  162. * @return true if obj is a PropertyPermission, and has the same name and
  163. * actions as this PropertyPermission object.
  164. */
  165. public boolean equals(Object obj) {
  166. if (obj == this)
  167. return true;
  168. if (! (obj instanceof PropertyPermission))
  169. return false;
  170. PropertyPermission that = (PropertyPermission) obj;
  171. return (this.mask == that.mask) &&
  172. (this.getName().equals(that.getName()));
  173. }
  174. /**
  175. * Returns the hash code value for this object.
  176. * The hash code used is the hash code of this permissions name, that is,
  177. * <code>getName().hashCode()</code>, where <code>getName</code> is
  178. * from the Permission superclass.
  179. *
  180. * @return a hash code value for this object.
  181. */
  182. public int hashCode() {
  183. return this.getName().hashCode();
  184. }
  185. /**
  186. * Converts an actions String to an actions mask.
  187. *
  188. * @param action the action string.
  189. * @return the actions mask.
  190. */
  191. private static int getMask(String actions) {
  192. int mask = NONE;
  193. if (actions == null) {
  194. return mask;
  195. }
  196. // Check against use of constants (used heavily within the JDK)
  197. if (actions == SecurityConstants.PROPERTY_READ_ACTION) {
  198. return READ;
  199. } if (actions == SecurityConstants.PROPERTY_WRITE_ACTION) {
  200. return WRITE;
  201. } else if (actions == SecurityConstants.PROPERTY_RW_ACTION) {
  202. return READ|WRITE;
  203. }
  204. char[] a = actions.toCharArray();
  205. int i = a.length - 1;
  206. if (i < 0)
  207. return mask;
  208. while (i != -1) {
  209. char c;
  210. // skip whitespace
  211. while ((i!=-1) && ((c = a[i]) == ' ' ||
  212. c == '\r' ||
  213. c == '\n' ||
  214. c == '\f' ||
  215. c == '\t'))
  216. i--;
  217. // check for the known strings
  218. int matchlen;
  219. if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') &&
  220. (a[i-2] == 'e' || a[i-2] == 'E') &&
  221. (a[i-1] == 'a' || a[i-1] == 'A') &&
  222. (a[i] == 'd' || a[i] == 'D'))
  223. {
  224. matchlen = 4;
  225. mask |= READ;
  226. } else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') &&
  227. (a[i-3] == 'r' || a[i-3] == 'R') &&
  228. (a[i-2] == 'i' || a[i-2] == 'I') &&
  229. (a[i-1] == 't' || a[i-1] == 'T') &&
  230. (a[i] == 'e' || a[i] == 'E'))
  231. {
  232. matchlen = 5;
  233. mask |= WRITE;
  234. } else {
  235. // parse error
  236. throw new IllegalArgumentException(
  237. "invalid permission: " + actions);
  238. }
  239. // make sure we didn't just match the tail of a word
  240. // like "ackbarfaccept". Also, skip to the comma.
  241. boolean seencomma = false;
  242. while (i >= matchlen && !seencomma) {
  243. switch(a[i-matchlen]) {
  244. case ',':
  245. seencomma = true;
  246. /*FALLTHROUGH*/
  247. case ' ': case '\r': case '\n':
  248. case '\f': case '\t':
  249. break;
  250. default:
  251. throw new IllegalArgumentException(
  252. "invalid permission: " + actions);
  253. }
  254. i--;
  255. }
  256. // point i at the location of the comma minus one (or -1).
  257. i -= matchlen;
  258. }
  259. return mask;
  260. }
  261. /**
  262. * Return the canonical string representation of the actions.
  263. * Always returns present actions in the following order:
  264. * read, write.
  265. *
  266. * @return the canonical string representation of the actions.
  267. */
  268. static String getActions(int mask)
  269. {
  270. StringBuffer sb = new StringBuffer();
  271. boolean comma = false;
  272. if ((mask & READ) == READ) {
  273. comma = true;
  274. sb.append("read");
  275. }
  276. if ((mask & WRITE) == WRITE) {
  277. if (comma) sb.append(',');
  278. else comma = true;
  279. sb.append("write");
  280. }
  281. return sb.toString();
  282. }
  283. /**
  284. * Returns the "canonical string representation" of the actions.
  285. * That is, this method always returns present actions in the following order:
  286. * read, write. For example, if this PropertyPermission object
  287. * allows both write and read actions, a call to <code>getActions</code>
  288. * will return the string "read,write".
  289. *
  290. * @return the canonical string representation of the actions.
  291. */
  292. public String getActions()
  293. {
  294. if (actions == null)
  295. actions = getActions(this.mask);
  296. return actions;
  297. }
  298. /**
  299. * Return the current action mask.
  300. * Used by the PropertyPermissionCollection
  301. *
  302. * @return the actions mask.
  303. */
  304. int getMask() {
  305. return mask;
  306. }
  307. /**
  308. * Returns a new PermissionCollection object for storing
  309. * PropertyPermission objects.
  310. * <p>
  311. *
  312. * @return a new PermissionCollection object suitable for storing
  313. * PropertyPermissions.
  314. */
  315. public PermissionCollection newPermissionCollection() {
  316. return new PropertyPermissionCollection();
  317. }
  318. /**
  319. * WriteObject is called to save the state of the PropertyPermission
  320. * to a stream. The actions are serialized, and the superclass
  321. * takes care of the name.
  322. */
  323. private synchronized void writeObject(java.io.ObjectOutputStream s)
  324. throws IOException
  325. {
  326. // Write out the actions. The superclass takes care of the name
  327. // call getActions to make sure actions field is initialized
  328. if (actions == null)
  329. getActions();
  330. s.defaultWriteObject();
  331. }
  332. /**
  333. * readObject is called to restore the state of the PropertyPermission from
  334. * a stream.
  335. */
  336. private synchronized void readObject(java.io.ObjectInputStream s)
  337. throws IOException, ClassNotFoundException
  338. {
  339. // Read in the action, then initialize the rest
  340. s.defaultReadObject();
  341. init(getMask(actions));
  342. }
  343. }
  344. /**
  345. * A PropertyPermissionCollection stores a set of PropertyPermission
  346. * permissions.
  347. *
  348. * @see java.security.Permission
  349. * @see java.security.Permissions
  350. * @see java.security.PermissionCollection
  351. *
  352. * @version 1.30, 01/23/03
  353. *
  354. * @author Roland Schemers
  355. *
  356. * @serial include
  357. */
  358. final class PropertyPermissionCollection extends PermissionCollection
  359. implements Serializable
  360. {
  361. /**
  362. * Key is property name; value is PropertyPermission.
  363. * Not serialized; see serialization section at end of class.
  364. */
  365. private transient Map perms;
  366. /**
  367. * Boolean saying if "*" is in the collection.
  368. *
  369. * @see #serialPersistentFields
  370. */
  371. private boolean all_allowed;
  372. /**
  373. * Create an empty PropertyPermissions object.
  374. *
  375. */
  376. public PropertyPermissionCollection() {
  377. perms = new HashMap(32); // Capacity for default policy
  378. all_allowed = false;
  379. }
  380. /**
  381. * Adds a permission to the PropertyPermissions. The key for the hash is
  382. * the name.
  383. *
  384. * @param permission the Permission object to add.
  385. *
  386. * @exception IllegalArgumentException - if the permission is not a
  387. * PropertyPermission
  388. *
  389. * @exception SecurityException - if this PropertyPermissionCollection
  390. * object has been marked readonly
  391. */
  392. public void add(Permission permission)
  393. {
  394. if (! (permission instanceof PropertyPermission))
  395. throw new IllegalArgumentException("invalid permission: "+
  396. permission);
  397. if (isReadOnly())
  398. throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
  399. PropertyPermission pp = (PropertyPermission) permission;
  400. PropertyPermission existing =
  401. (PropertyPermission) perms.get(pp.getName());
  402. // No need to synchronize because all adds are done sequentially
  403. // before any implies() calls
  404. if (existing != null) {
  405. int oldMask = existing.getMask();
  406. int newMask = pp.getMask();
  407. if (oldMask != newMask) {
  408. int effective = oldMask | newMask;
  409. String actions = PropertyPermission.getActions(effective);
  410. perms.put(pp.getName(),
  411. new PropertyPermission(pp.getName(), actions));
  412. }
  413. } else {
  414. perms.put(pp.getName(), permission);
  415. }
  416. if (!all_allowed) {
  417. if (pp.getName().equals("*"))
  418. all_allowed = true;
  419. }
  420. }
  421. /**
  422. * Check and see if this set of permissions implies the permissions
  423. * expressed in "permission".
  424. *
  425. * @param p the Permission object to compare
  426. *
  427. * @return true if "permission" is a proper subset of a permission in
  428. * the set, false if not.
  429. */
  430. public boolean implies(Permission permission)
  431. {
  432. if (! (permission instanceof PropertyPermission))
  433. return false;
  434. PropertyPermission pp = (PropertyPermission) permission;
  435. PropertyPermission x;
  436. int desired = pp.getMask();
  437. int effective = 0;
  438. // short circuit if the "*" Permission was added
  439. if (all_allowed) {
  440. x = (PropertyPermission) perms.get("*");
  441. if (x != null) {
  442. effective |= x.getMask();
  443. if ((effective & desired) == desired)
  444. return true;
  445. }
  446. }
  447. // strategy:
  448. // Check for full match first. Then work our way up the
  449. // name looking for matches on a.b.*
  450. String name = pp.getName();
  451. //System.out.println("check "+name);
  452. x = (PropertyPermission) perms.get(name);
  453. if (x != null) {
  454. // we have a direct hit!
  455. effective |= x.getMask();
  456. if ((effective & desired) == desired)
  457. return true;
  458. }
  459. // work our way up the tree...
  460. int last, offset;
  461. offset = name.length()-1;
  462. while ((last = name.lastIndexOf(".", offset)) != -1) {
  463. name = name.substring(0, last+1) + "*";
  464. //System.out.println("check "+name);
  465. x = (PropertyPermission) perms.get(name);
  466. if (x != null) {
  467. effective |= x.getMask();
  468. if ((effective & desired) == desired)
  469. return true;
  470. }
  471. offset = last -1;
  472. }
  473. // we don't have to check for "*" as it was already checked
  474. // at the top (all_allowed), so we just return false
  475. return false;
  476. }
  477. /**
  478. * Returns an enumeration of all the PropertyPermission objects in the
  479. * container.
  480. *
  481. * @return an enumeration of all the PropertyPermission objects.
  482. */
  483. public Enumeration elements() {
  484. // Convert Iterator of Map values into an Enumeration
  485. return Collections.enumeration(perms.values());
  486. }
  487. private static final long serialVersionUID = 7015263904581634791L;
  488. // Need to maintain serialization interoperability with earlier releases,
  489. // which had the serializable field:
  490. //
  491. // Table of permissions.
  492. //
  493. // @serial
  494. //
  495. // private Hashtable permissions;
  496. /**
  497. * @serialField permissions java.util.Hashtable
  498. * A table of the PropertyPermissions.
  499. * @serialField all_allowed boolean
  500. * boolean saying if "*" is in the collection.
  501. */
  502. private static final ObjectStreamField[] serialPersistentFields = {
  503. new ObjectStreamField("permissions", Hashtable.class),
  504. new ObjectStreamField("all_allowed", Boolean.TYPE),
  505. };
  506. /**
  507. * @serialData Default fields.
  508. */
  509. /*
  510. * Writes the contents of the perms field out as a Hashtable for
  511. * serialization compatibility with earlier releases. all_allowed
  512. * unchanged.
  513. */
  514. private void writeObject(ObjectOutputStream out) throws IOException {
  515. // Don't call out.defaultWriteObject()
  516. // Copy perms into a Hashtable
  517. Hashtable permissions = new Hashtable(perms.size()*2);
  518. permissions.putAll(perms);
  519. // Write out serializable fields
  520. ObjectOutputStream.PutField pfields = out.putFields();
  521. pfields.put("all_allowed", all_allowed);
  522. pfields.put("permissions", permissions);
  523. out.writeFields();
  524. }
  525. /*
  526. * Reads in a Hashtable of PropertyPermissions and saves them in the
  527. * perms field. Reads in all_allowed.
  528. */
  529. private void readObject(ObjectInputStream in) throws IOException,
  530. ClassNotFoundException {
  531. // Don't call defaultReadObject()
  532. // Read in serialized fields
  533. ObjectInputStream.GetField gfields = in.readFields();
  534. // Get all_allowed
  535. all_allowed = gfields.get("all_allowed", false);
  536. // Get permissions
  537. Hashtable permissions = (Hashtable)gfields.get("permissions", null);
  538. perms = new HashMap(permissions.size()*2);
  539. perms.putAll(permissions);
  540. }
  541. }