1. /*
  2. * @(#)PropertyPermission.java 1.33 03/12/19
  3. *
  4. * Copyright 2004 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.33 03/12/19
  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. StringBuilder sb = new StringBuilder();
  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. private static final long serialVersionUID = 885438825399942851L;
  319. /**
  320. * WriteObject is called to save the state of the PropertyPermission
  321. * to a stream. The actions are serialized, and the superclass
  322. * takes care of the name.
  323. */
  324. private synchronized void writeObject(java.io.ObjectOutputStream s)
  325. throws IOException
  326. {
  327. // Write out the actions. The superclass takes care of the name
  328. // call getActions to make sure actions field is initialized
  329. if (actions == null)
  330. getActions();
  331. s.defaultWriteObject();
  332. }
  333. /**
  334. * readObject is called to restore the state of the PropertyPermission from
  335. * a stream.
  336. */
  337. private synchronized void readObject(java.io.ObjectInputStream s)
  338. throws IOException, ClassNotFoundException
  339. {
  340. // Read in the action, then initialize the rest
  341. s.defaultReadObject();
  342. init(getMask(actions));
  343. }
  344. }
  345. /**
  346. * A PropertyPermissionCollection stores a set of PropertyPermission
  347. * permissions.
  348. *
  349. * @see java.security.Permission
  350. * @see java.security.Permissions
  351. * @see java.security.PermissionCollection
  352. *
  353. * @version 1.33, 12/19/03
  354. *
  355. * @author Roland Schemers
  356. *
  357. * @serial include
  358. */
  359. final class PropertyPermissionCollection extends PermissionCollection
  360. implements Serializable
  361. {
  362. /**
  363. * Key is property name; value is PropertyPermission.
  364. * Not serialized; see serialization section at end of class.
  365. */
  366. private transient Map perms;
  367. /**
  368. * Boolean saying if "*" is in the collection.
  369. *
  370. * @see #serialPersistentFields
  371. */
  372. // No sync access; OK for this to be stale.
  373. private boolean all_allowed;
  374. /**
  375. * Create an empty PropertyPermissions object.
  376. *
  377. */
  378. public PropertyPermissionCollection() {
  379. perms = new HashMap(32); // Capacity for default policy
  380. all_allowed = false;
  381. }
  382. /**
  383. * Adds a permission to the PropertyPermissions. The key for the hash is
  384. * the name.
  385. *
  386. * @param permission the Permission object to add.
  387. *
  388. * @exception IllegalArgumentException - if the permission is not a
  389. * PropertyPermission
  390. *
  391. * @exception SecurityException - if this PropertyPermissionCollection
  392. * object has been marked readonly
  393. */
  394. public void add(Permission permission)
  395. {
  396. if (! (permission instanceof PropertyPermission))
  397. throw new IllegalArgumentException("invalid permission: "+
  398. permission);
  399. if (isReadOnly())
  400. throw new SecurityException(
  401. "attempt to add a Permission to a readonly PermissionCollection");
  402. PropertyPermission pp = (PropertyPermission) permission;
  403. String propName = pp.getName();
  404. synchronized (this) {
  405. PropertyPermission existing = (PropertyPermission) perms.get(propName);
  406. if (existing != null) {
  407. int oldMask = existing.getMask();
  408. int newMask = pp.getMask();
  409. if (oldMask != newMask) {
  410. int effective = oldMask | newMask;
  411. String actions = PropertyPermission.getActions(effective);
  412. perms.put(propName, new PropertyPermission(propName, actions));
  413. }
  414. } else {
  415. perms.put(propName, permission);
  416. }
  417. }
  418. if (!all_allowed) {
  419. if (propName.equals("*"))
  420. all_allowed = true;
  421. }
  422. }
  423. /**
  424. * Check and see if this set of permissions implies the permissions
  425. * expressed in "permission".
  426. *
  427. * @param p the Permission object to compare
  428. *
  429. * @return true if "permission" is a proper subset of a permission in
  430. * the set, false if not.
  431. */
  432. public boolean implies(Permission permission)
  433. {
  434. if (! (permission instanceof PropertyPermission))
  435. return false;
  436. PropertyPermission pp = (PropertyPermission) permission;
  437. PropertyPermission x;
  438. int desired = pp.getMask();
  439. int effective = 0;
  440. // short circuit if the "*" Permission was added
  441. if (all_allowed) {
  442. synchronized (this) {
  443. x = (PropertyPermission) perms.get("*");
  444. }
  445. if (x != null) {
  446. effective |= x.getMask();
  447. if ((effective & desired) == desired)
  448. return true;
  449. }
  450. }
  451. // strategy:
  452. // Check for full match first. Then work our way up the
  453. // name looking for matches on a.b.*
  454. String name = pp.getName();
  455. //System.out.println("check "+name);
  456. synchronized (this) {
  457. x = (PropertyPermission) perms.get(name);
  458. }
  459. if (x != null) {
  460. // we have a direct hit!
  461. effective |= x.getMask();
  462. if ((effective & desired) == desired)
  463. return true;
  464. }
  465. // work our way up the tree...
  466. int last, offset;
  467. offset = name.length()-1;
  468. while ((last = name.lastIndexOf(".", offset)) != -1) {
  469. name = name.substring(0, last+1) + "*";
  470. //System.out.println("check "+name);
  471. synchronized (this) {
  472. x = (PropertyPermission) perms.get(name);
  473. }
  474. if (x != null) {
  475. effective |= x.getMask();
  476. if ((effective & desired) == desired)
  477. return true;
  478. }
  479. offset = last -1;
  480. }
  481. // we don't have to check for "*" as it was already checked
  482. // at the top (all_allowed), so we just return false
  483. return false;
  484. }
  485. /**
  486. * Returns an enumeration of all the PropertyPermission objects in the
  487. * container.
  488. *
  489. * @return an enumeration of all the PropertyPermission objects.
  490. */
  491. public Enumeration elements() {
  492. // Convert Iterator of Map values into an Enumeration
  493. synchronized (this) {
  494. return Collections.enumeration(perms.values());
  495. }
  496. }
  497. private static final long serialVersionUID = 7015263904581634791L;
  498. // Need to maintain serialization interoperability with earlier releases,
  499. // which had the serializable field:
  500. //
  501. // Table of permissions.
  502. //
  503. // @serial
  504. //
  505. // private Hashtable permissions;
  506. /**
  507. * @serialField permissions java.util.Hashtable
  508. * A table of the PropertyPermissions.
  509. * @serialField all_allowed boolean
  510. * boolean saying if "*" is in the collection.
  511. */
  512. private static final ObjectStreamField[] serialPersistentFields = {
  513. new ObjectStreamField("permissions", Hashtable.class),
  514. new ObjectStreamField("all_allowed", Boolean.TYPE),
  515. };
  516. /**
  517. * @serialData Default fields.
  518. */
  519. /*
  520. * Writes the contents of the perms field out as a Hashtable for
  521. * serialization compatibility with earlier releases. all_allowed
  522. * unchanged.
  523. */
  524. private void writeObject(ObjectOutputStream out) throws IOException {
  525. // Don't call out.defaultWriteObject()
  526. // Copy perms into a Hashtable
  527. Hashtable permissions = new Hashtable(perms.size()*2);
  528. synchronized (this) {
  529. permissions.putAll(perms);
  530. }
  531. // Write out serializable fields
  532. ObjectOutputStream.PutField pfields = out.putFields();
  533. pfields.put("all_allowed", all_allowed);
  534. pfields.put("permissions", permissions);
  535. out.writeFields();
  536. }
  537. /*
  538. * Reads in a Hashtable of PropertyPermissions and saves them in the
  539. * perms field. Reads in all_allowed.
  540. */
  541. private void readObject(ObjectInputStream in) throws IOException,
  542. ClassNotFoundException {
  543. // Don't call defaultReadObject()
  544. // Read in serialized fields
  545. ObjectInputStream.GetField gfields = in.readFields();
  546. // Get all_allowed
  547. all_allowed = gfields.get("all_allowed", false);
  548. // Get permissions
  549. Hashtable permissions = (Hashtable)gfields.get("permissions", null);
  550. perms = new HashMap(permissions.size()*2);
  551. perms.putAll(permissions);
  552. }
  553. }