MBeanPermission.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /* MBeanPermission.java -- Permissions controlling server access.
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.management;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.security.Permission;
  34. import java.io.IOException;
  35. import java.io.ObjectInputStream;
  36. import java.util.HashSet;
  37. import java.util.Iterator;
  38. import java.util.Set;
  39. import java.util.TreeSet;
  40. /**
  41. * <p>
  42. * Represents the permissions required to perform
  43. * operations using the {@link MBeanServer}. As with
  44. * all {@link java.security.Permission} objects, an
  45. * instance of this class either represents a permission
  46. * already held or one that is required to access a
  47. * particular service. In the case of {@link MBeanPermission}s,
  48. * implication checks are made using an instance of this class
  49. * when a user requests an operation from the server, and a
  50. * {@link SecurityManager} is in place.
  51. * </p>
  52. * <p>
  53. * An {@link MBeanPermission} consists of four elements,
  54. * which all have to match for the permission to be implied.
  55. * These are as follows:
  56. * </p>
  57. * <ol>
  58. * <li><strong>The action</strong>. For a required permission,
  59. * this is a single value. For a permission held by the user,
  60. * this is a list of comma-separated actions (with spaces allowed),
  61. * or <code>*</code> (representing all actions). {@link #getActions()}
  62. * returns this value.</li>
  63. * <li><strong>The class name</strong>. For a required permission,
  64. * this is the class name of the bean being accessed, if any. If
  65. * a bean isn't involved in this action, the value is <code>null</code>.
  66. * For a permission held by the user, it has one of three values:
  67. * <ol>
  68. * <li>The empty string, implying any class.</li>
  69. * <li><code>*</code>, also implying any class.</li>
  70. * <li>A class name pattern, which may specify a single class
  71. * (e.g. <code>java.lang.Object</code>) or a series of classes
  72. * using the wildcard character <code>*</code> (e.g.
  73. * <code>javax.swing.*</code>.)</li>
  74. * </ol></li>
  75. * <li><strong>The member</strong>. For a required permission,
  76. * this is the member of the bean being accessed (an attribute
  77. * or operation), if any. If a member of the bean isn't involved
  78. * in this action, the value is <code>null</code>.
  79. * For a permission held by the user, it has one of three values:
  80. * <ol>
  81. * <li>The empty string, implying any member.</li>
  82. * <li><code>*</code>, also implying any member.</li>
  83. * <li>The name of a member.</li>
  84. * </ol></li>
  85. * <li>The object name</strong>. For a required permission,
  86. * this is the {@link ObjectName} of the bean being accessed, if
  87. * any. If a bean isn't involved in this action, the value is
  88. * <code>null</code>. The name may not be a pattern.
  89. * For a permission held by the user, it may be the empty
  90. * string (allowing everything) or an {@link ObjectName}
  91. * pattern.
  92. * </li></ol>
  93. * {@link #getName()} returns the latter three of these as a
  94. * single string:
  95. * </p>
  96. * <p><code>className#member[objectName]</code></p>
  97. * <p>
  98. * where <code>""</code> is disallowed, as, although any of
  99. * the elements may be omitted, not all of them should be
  100. * left out simultaneously. <code>"-"</code> is used to
  101. * represent <code>null</code>. When this occurs in a
  102. * required permission, anything may match it. When this
  103. * forms part of a permission held by the user, it only
  104. * matches another <code>null</code> value.
  105. * </p>
  106. * <p>The list of valid actions is as follows:</p>
  107. * <ul>
  108. * <li>addNotificationListener</li>
  109. * <li>getAttribute</li>
  110. * <li>getClassLoader</li>
  111. * <li>getClassLoaderFor</li>
  112. * <li>getClassLoaderRepository</li>
  113. * <li>getDomains</li>
  114. * <li>getMBeanInfo</li>
  115. * <li>getObjectInstance</li>
  116. * <li>instantiate</li>
  117. * <li>invoke</li>
  118. * <li>isInstanceOf</li>
  119. * <li>queryMBeans</li>
  120. * <li>queryNames</li>
  121. * <li>registerMBean</li>
  122. * <li>removeNotificationListener</li>
  123. * <li>setAttribute</li>
  124. * <li>unregisterMBean</li>
  125. * </ul>
  126. *
  127. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  128. * @since 1.5
  129. */
  130. public class MBeanPermission
  131. extends Permission
  132. {
  133. /**
  134. * Compatible with JDK 1.5
  135. */
  136. private static final long serialVersionUID = -2416928705275160661L;
  137. /**
  138. * The list of actions associated with this permission.
  139. */
  140. private String actions;
  141. /**
  142. * The list of actions as an ordered set.
  143. */
  144. private transient Set<String> actionSet;
  145. /**
  146. * The set of valid actions.
  147. */
  148. private static final Set<String> validSet;
  149. /**
  150. * Initialise the set of valid actions.
  151. */
  152. static
  153. {
  154. validSet = new HashSet<String>();
  155. validSet.add("addNotificationListener");
  156. validSet.add("getAttribute");
  157. validSet.add("getClassLoader");
  158. validSet.add("getClassLoaderFor");
  159. validSet.add("getClassLoaderRepository");
  160. validSet.add("getDomains");
  161. validSet.add("getMBeanInfo");
  162. validSet.add("getObjectInstance");
  163. validSet.add("instantiate");
  164. validSet.add("invoke");
  165. validSet.add("isInstanceOf");
  166. validSet.add("queryMBeans");
  167. validSet.add("queryNames");
  168. validSet.add("registerMBean");
  169. validSet.add("removeNotificationListener");
  170. validSet.add("setAttribute");
  171. validSet.add("unregisterMBean");
  172. }
  173. /**
  174. * Constructs a new {@link MBeanPermission} with the specified name
  175. * and actions. The name is of the form <code>className#member[objectName]</code>,
  176. * where each element is optional, but a completely empty or <code>null</code>
  177. * name is disallowed. Actions are specified as a comma-separated list
  178. * and may also not be empty or <code>null</code>.
  179. *
  180. * @param name the name of the permission.
  181. * @param actions the actions associated with this permission.
  182. * @throws IllegalArgumentException if the name or actions are invalid.
  183. */
  184. public MBeanPermission(String name, String actions)
  185. {
  186. super(name);
  187. if (name == null || name.length() == 0)
  188. throw new IllegalArgumentException("The supplied name was null or empty.");
  189. if (actions == null || actions.length() == 0)
  190. throw new IllegalArgumentException("The supplied action list was null or empty.");
  191. this.actions = actions;
  192. updateActionSet();
  193. }
  194. /**
  195. * Constructs a new {@link MBeanPermission} with the specified class name,
  196. * member, object name and actions. The name of the permission is created
  197. * using the form <code>className#member[objectName]</code>,
  198. * where each element is optional, but an empty or <code>null</code>
  199. * name is disallowed. Actions are specified as a comma-separated list
  200. * and may also not be empty or <code>null</code>.
  201. *
  202. * @param className the name of the class to which this permission applies,
  203. * or either <code>null</code> or <code>"-"</code> for a
  204. * value which may be implied by any class name, but not
  205. * imply any class name itself.
  206. * @param member the member of the class to which this permission applies,
  207. * or either <code>null</code> or <code>"-"</code> for a
  208. * value which may be implied by any member, but not
  209. * imply any member itself.
  210. * @param name the {@link ObjectName} to which this permission applies,
  211. * or <code>null</code> for a value which may be implied by
  212. * any object name, but not imply any object name itself.
  213. * @param actions the actions associated with this permission.
  214. */
  215. public MBeanPermission(String className, String member,
  216. ObjectName name, String actions)
  217. {
  218. this((className == null ? "-" : className) + "#"
  219. + (member == null ? "-" : member) + "["
  220. + (name == null ? "-" : name.toString()) + "]", actions);
  221. }
  222. /**
  223. * Returns true if the given object is also an {@link MBeanPermission}
  224. * with the same name and actions.
  225. *
  226. * @param obj the object to test.
  227. * @return true if the object is an {@link MBeanPermission} with
  228. * the same name and actions.
  229. */
  230. public boolean equals(Object obj)
  231. {
  232. if (obj instanceof MBeanPermission)
  233. {
  234. MBeanPermission p = (MBeanPermission) obj;
  235. return (p.getName().equals(getName()) &&
  236. p.getActions().equals(actions));
  237. }
  238. return false;
  239. }
  240. /**
  241. * Returns the list of actions in alphabetical order.
  242. *
  243. * @return the list of actions.
  244. */
  245. public String getActions()
  246. {
  247. Iterator<String> it = actionSet.iterator();
  248. CPStringBuilder builder = new CPStringBuilder();
  249. while (it.hasNext())
  250. {
  251. builder.append(it.next());
  252. if (it.hasNext())
  253. builder.append(",");
  254. }
  255. return builder.toString();
  256. }
  257. /**
  258. * Returns the hashcode of the permission as the sum
  259. * of the hashcodes of the name and actions.
  260. *
  261. * @return the hashcode of the permission.
  262. */
  263. public int hashCode()
  264. {
  265. return getName().hashCode() + actions.hashCode();
  266. }
  267. /**
  268. * <p>
  269. * Returns true if this permission implies the supplied permission.
  270. * This happens if the following holds:
  271. * </p>
  272. * <ul>
  273. * <li>The supplied permission is an {@link MBeanPermission}</li>
  274. * <li>The supplied permission has either a <code>null</code> classname
  275. * or its classname matches the classname of this permission. A
  276. * classname of <code>"*"</code> for this permission always matches
  277. * the classname of the supplied permission. Generally, <code>'*'</code>
  278. * acts as a wildcard, so <code>".*"</code> matches <code>'.'</code>
  279. * followed by anything.</li>
  280. * <li>The supplied permission has either a <code>null</code> member
  281. * or its member matches the member of this permission. A member of
  282. * <code>"*"</code> for this permission always matches the member
  283. * of the supplied permission.</li>
  284. * <li>The supplied permission has either a <code>null</code> object name
  285. * or its object name matches the object name of this permission. If the
  286. * object name of this permission is a pattern, {@link ObjectName#apply(ObjectName)}
  287. * may be used as well.</li>
  288. * <li>The supplied permission's actions are a subset of the actions
  289. * of this permission. If the <code>queryMBeans</code> action is presented,
  290. * the <code>queryNames</code> action is implied.</li>
  291. * </ul>
  292. *
  293. * @param p the permission to check that this permission implies.
  294. * @return true if this permission implies <code>p</code>.
  295. */
  296. public boolean implies(Permission p)
  297. {
  298. if (p instanceof MBeanPermission)
  299. {
  300. MBeanPermission mp = (MBeanPermission) p;
  301. NameHolder pName = new NameHolder(mp.getName());
  302. NameHolder name = new NameHolder(getName());
  303. if (!(name.equals(pName)))
  304. return false;
  305. for (String nextAction : mp.actionSet)
  306. {
  307. boolean found = actions.contains(nextAction);
  308. if (!found)
  309. if (nextAction.equals("queryNames"))
  310. found = actions.contains("queryMBeans");
  311. if (!found)
  312. return false;
  313. }
  314. return true;
  315. }
  316. return false;
  317. }
  318. /**
  319. * Small helper class to handle deconstruction of the name.
  320. *
  321. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  322. */
  323. private class NameHolder
  324. {
  325. /**
  326. * The class name.
  327. */
  328. private String className;
  329. /**
  330. * The member.
  331. */
  332. private String member;
  333. /**
  334. * The object name.
  335. */
  336. private ObjectName objectName;
  337. /**
  338. * Constructs a broken-down name from a given name.
  339. *
  340. * @param name the name to break down.
  341. */
  342. public NameHolder(String name)
  343. {
  344. String objectName = null;
  345. int memberIndex = name.indexOf("#");
  346. int onIndex = name.indexOf("[");
  347. if (onIndex == -1)
  348. {
  349. if (memberIndex == -1)
  350. className = name;
  351. else
  352. {
  353. className = name.substring(0, memberIndex);
  354. member = name.substring(memberIndex + 1);
  355. }
  356. }
  357. else
  358. {
  359. if (memberIndex == -1)
  360. {
  361. className = name.substring(0, onIndex);
  362. objectName = name.substring(onIndex + 1,
  363. name.length() - 1);
  364. }
  365. else
  366. {
  367. className = name.substring(0, memberIndex);
  368. member = name.substring(memberIndex + 1, onIndex);
  369. objectName = name.substring(onIndex + 1,
  370. name.length() - 1);
  371. }
  372. }
  373. if (className.equals("-"))
  374. className = null;
  375. if (member.equals("-"))
  376. member = null;
  377. if (objectName == null || objectName.equals("-"))
  378. this.objectName = null;
  379. else
  380. try
  381. {
  382. this.objectName = new ObjectName(objectName);
  383. }
  384. catch (MalformedObjectNameException e)
  385. {
  386. throw (Error)
  387. (new InternalError("Invalid object name.").initCause(e));
  388. }
  389. }
  390. /**
  391. * <p>
  392. * Returns true if the supplied object is also a
  393. * {@link NameHolder} and the following holds:
  394. * </p>
  395. * <ul>
  396. * <li>The supplied classname is <code>null</code> or the two match. A
  397. * classname of <code>"*"</code> for this holder always matches
  398. * the classname of the supplied holder. Generally, <code>'*'</code>
  399. * acts as a wildcard, so <code>".*"</code> matches <code>'.'</code>
  400. * followed by anything.</li>
  401. * <li>The supplied name holder has either a <code>null</code> member
  402. * or its member matches the member of this name holder. A member of
  403. * <code>"*"</code> for this name holder always matches the member
  404. * of the supplied name holder.</li>
  405. * <li>The supplied name holder has either a <code>null</code> object name
  406. * or its object name matches the object name of this name holder. If the
  407. * object name of this name holder is a pattern,
  408. * {@link ObjectName#apply(ObjectName)} may be used as well.</li>
  409. * </ul>
  410. *
  411. * @param obj the object to compare with this.
  412. * @return true if the above holds.
  413. */
  414. public boolean equals(Object obj)
  415. {
  416. if (obj instanceof NameHolder)
  417. {
  418. NameHolder nh = (NameHolder) obj;
  419. boolean cn = false;
  420. String ocn = nh.getClassName();
  421. if (ocn == null || className.equals("*"))
  422. cn = true;
  423. else
  424. {
  425. int wcIndex = className.indexOf("*");
  426. if (wcIndex != -1)
  427. cn = ocn.startsWith(className.substring(0, wcIndex));
  428. else
  429. cn = ocn.equals(className);
  430. }
  431. boolean m = false;
  432. String om = nh.getMember();
  433. if (om == null || member.equals("*"))
  434. m = true;
  435. else
  436. m = om.equals(member);
  437. boolean on = false;
  438. ObjectName oon = nh.getObjectName();
  439. if (oon == null)
  440. on = true;
  441. else if (objectName.isPattern())
  442. on = objectName.apply(oon);
  443. else
  444. on = oon.equals(objectName);
  445. return (cn && m && on);
  446. }
  447. return false;
  448. }
  449. /**
  450. * Returns the class name.
  451. */
  452. public String getClassName()
  453. {
  454. return className;
  455. }
  456. /**
  457. * Returns the member.
  458. */
  459. public String getMember()
  460. {
  461. return member;
  462. }
  463. /**
  464. * Returns the object name.
  465. */
  466. public ObjectName getObjectName()
  467. {
  468. return objectName;
  469. }
  470. }
  471. /**
  472. * Updates the action set from the current value of
  473. * the actions string.
  474. */
  475. private void updateActionSet()
  476. {
  477. String[] actionsArray = actions.split(",");
  478. actionSet = new TreeSet<String>();
  479. for (int a = 0; a < actionsArray.length; ++a)
  480. actionSet.add(actionsArray[a].trim());
  481. }
  482. /**
  483. * Reads the object from a stream and ensures the incoming
  484. * data is valid.
  485. *
  486. * @param in the input stream.
  487. * @throws IOException if an I/O error occurs.
  488. * @throws ClassNotFoundException if a class used by the object
  489. * can not be found.
  490. */
  491. private void readObject(ObjectInputStream in)
  492. throws IOException, ClassNotFoundException
  493. {
  494. in.defaultReadObject();
  495. updateActionSet();
  496. checkActions();
  497. }
  498. /**
  499. * Checks that the actions used in this permission
  500. * are from the valid set.
  501. *
  502. * @throws IllegalArgumentException if the name or actions are invalid.
  503. */
  504. private void checkActions()
  505. {
  506. for (String action : actionSet)
  507. {
  508. if (!(validSet.contains(action)))
  509. throw new IllegalArgumentException("Invalid action "
  510. + action + " found.");
  511. }
  512. }
  513. }