1. /*
  2. * @(#)MBeanPermission.java 1.22 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 javax.management;
  8. import java.security.Permission;
  9. import java.io.IOException;
  10. import java.io.ObjectInputStream;
  11. /**
  12. * <p>Permission controlling access to MBeanServer operations. If a
  13. * security manager has been set using {@link
  14. * System#setSecurityManager}, most operations on the MBean Server
  15. * require that the caller's permissions imply an MBeanPermission
  16. * appropriate for the operation. This is described in detail in the
  17. * documentation for the {@link MBeanServer} interface.</p>
  18. *
  19. * <p>As with other {@link Permission} objects, an MBeanPermission can
  20. * represent either a permission that you <em>have</em> or a
  21. * permission that you <em>need</em>. When a sensitive operation is
  22. * being checked for permission, an MBeanPermission is constructed
  23. * representing the permission you need. The operation is only
  24. * allowed if the permissions you have {@link #implies imply} the
  25. * permission you need.</p>
  26. *
  27. * <p>An MBeanPermission contains four items of information:</p>
  28. *
  29. * <ul>
  30. *
  31. * <li><p>The <em>action</em>. For a permission you need,
  32. * this is one of the actions in the list <a
  33. * href="#action-list">below</a>. For a permission you have, this is
  34. * a comma-separated list of those actions, or <code>*</code>,
  35. * representing all actions.</p>
  36. *
  37. * <p>The action is returned by {@link #getActions()}.</p>
  38. *
  39. * <li><p>The <em>class name</em>.</p>
  40. *
  41. * <p>For a permission you need, this is the class name of an MBean
  42. * you are accessing, as returned by {@link
  43. * MBeanServer#getMBeanInfo(ObjectName)
  44. * MBeanServer.getMBeanInfo(name)}.{@link MBeanInfo#getClassName()
  45. * getClassName()}. Certain operations do not reference a class name,
  46. * in which case the class name is null.</p>
  47. *
  48. * <p>For a permission you have, this is either empty or a <em>class
  49. * name pattern</em>. A class name pattern is a string following the
  50. * Java conventions for dot-separated class names. It may end with
  51. * "<code>.*</code>" meaning that the permission grants access to any
  52. * class that begins with the string preceding "<code>.*</code>". For
  53. * instance, "<code>javax.management.*</code>" grants access to
  54. * <code>javax.management.MBeanServerDelegate</code> and
  55. * <code>javax.management.timer.Timer</code>, among other classes.</p>
  56. *
  57. * <p>A class name pattern can also be empty or the single character
  58. * "<code>*</code>", both of which grant access to any class.</p>
  59. *
  60. * <li><p>The <em>member</em>.</p>
  61. *
  62. * <p>For a permission you need, this is the name of the attribute or
  63. * operation you are accessing. For operations that do not reference
  64. * an attribute or operation, the member is null.</p>
  65. *
  66. * <p>For a permission you have, this is either the name of an attribute
  67. * or operation you can access, or it is empty or the single character
  68. * "<code>*</code>", both of which grant access to any member.</p>
  69. *
  70. * <li><p>The <em>object name</em>.</p>
  71. *
  72. * <p>For a permission you need, this is the {@link ObjectName} of the
  73. * MBean you are accessing. For operations that do not reference a
  74. * single MBean, it is null. It is never an object name pattern.</p>
  75. *
  76. * <p>For a permission you have, this is the {@link ObjectName} of the
  77. * MBean or MBeans you can access. It may be an object name pattern
  78. * to grant access to all MBeans whose names match the pattern. It
  79. * may also be empty, which grants access to all MBeans whatever their
  80. * name.</p>
  81. *
  82. * </ul>
  83. *
  84. * <p>If you have an MBeanPermission, it allows operations only if all
  85. * four of the items match.</p>
  86. *
  87. * <p>The class name, member, and object name can be written together
  88. * as a single string, which is the <em>name</em> of this permission.
  89. * The name of the permission is the string returned by {@link
  90. * Permission#getName() getName()}. The format of the string is:</p>
  91. *
  92. * <blockquote>
  93. * <code>className#member[objectName]</code>
  94. * </blockquote>
  95. *
  96. * <p>The object name is written using the usual syntax for {@link
  97. * ObjectName}. It may contain any legal characters, including
  98. * <code>]</code>. It is terminated by a <code>]</code> character
  99. * that is the last character in the string.</p>
  100. *
  101. * <p>One or more of the <code>className</code>, <code>member</code>,
  102. * or <code>objectName</code> may be omitted. If the
  103. * <code>member</code> is omitted, the <code>#</code> may be too (but
  104. * does not have to be). If the <code>objectName</code> is omitted,
  105. * the <code>[]</code> may be too (but does not have to be). It is
  106. * not legal to omit all three items, that is to have a <em>name</em>
  107. * that is the empty string.</p>
  108. *
  109. * <p>One or more of the <code>className</code>, <code>member</code>,
  110. * or <code>objectName</code> may be the character "<code>-</code>",
  111. * which is equivalent to a null value. A null value is implied by
  112. * any value (including another null value) but does not imply any
  113. * other value.</p>
  114. *
  115. * <p><a name="action-list">The possible actions are these:</a></p>
  116. *
  117. * <ul>
  118. * <li>addNotificationListener</li>
  119. * <li>getAttribute</li>
  120. * <li>getClassLoader</li>
  121. * <li>getClassLoaderFor</li>
  122. * <li>getClassLoaderRepository</li>
  123. * <li>getDomains</li>
  124. * <li>getMBeanInfo</li>
  125. * <li>getObjectInstance</li>
  126. * <li>instantiate</li>
  127. * <li>invoke</li>
  128. * <li>isInstanceOf</li>
  129. * <li>queryMBeans</li>
  130. * <li>queryNames</li>
  131. * <li>registerMBean</li>
  132. * <li>removeNotificationListener</li>
  133. * <li>setAttribute</li>
  134. * <li>unregisterMBean</li>
  135. * </ul>
  136. *
  137. * <p>In a comma-separated list of actions, spaces are allowed before
  138. * and after each action.</p>
  139. *
  140. * @since 1.5
  141. * @since.unbundled JMX 1.2
  142. */
  143. public class MBeanPermission extends Permission {
  144. private static final long serialVersionUID = -2416928705275160661L;
  145. /**
  146. * Actions list.
  147. */
  148. private static final int AddNotificationListener = 0x00001;
  149. private static final int GetAttribute = 0x00002;
  150. private static final int GetClassLoader = 0x00004;
  151. private static final int GetClassLoaderFor = 0x00008;
  152. private static final int GetClassLoaderRepository = 0x00010;
  153. private static final int GetDomains = 0x00020;
  154. private static final int GetMBeanInfo = 0x00040;
  155. private static final int GetObjectInstance = 0x00080;
  156. private static final int Instantiate = 0x00100;
  157. private static final int Invoke = 0x00200;
  158. private static final int IsInstanceOf = 0x00400;
  159. private static final int QueryMBeans = 0x00800;
  160. private static final int QueryNames = 0x01000;
  161. private static final int RegisterMBean = 0x02000;
  162. private static final int RemoveNotificationListener = 0x04000;
  163. private static final int SetAttribute = 0x08000;
  164. private static final int UnregisterMBean = 0x10000;
  165. /**
  166. * No actions.
  167. */
  168. private static final int NONE = 0x00000;
  169. /**
  170. * All actions.
  171. */
  172. private static final int ALL =
  173. AddNotificationListener |
  174. GetAttribute |
  175. GetClassLoader |
  176. GetClassLoaderFor |
  177. GetClassLoaderRepository |
  178. GetDomains |
  179. GetMBeanInfo |
  180. GetObjectInstance |
  181. Instantiate |
  182. Invoke |
  183. IsInstanceOf |
  184. QueryMBeans |
  185. QueryNames |
  186. RegisterMBean |
  187. RemoveNotificationListener |
  188. SetAttribute |
  189. UnregisterMBean;
  190. /**
  191. * An ObjectName that matches any other.
  192. */
  193. private static final ObjectName allObjectNames;
  194. static {
  195. try {
  196. allObjectNames = new ObjectName("*:*");
  197. } catch (MalformedObjectNameException e) {
  198. throw new IllegalArgumentException("can't happen");
  199. }
  200. }
  201. /**
  202. * The actions string.
  203. */
  204. private String actions;
  205. /**
  206. * The actions mask.
  207. */
  208. private transient int mask;
  209. /**
  210. * The classname prefix that must match. If null, is implied by any
  211. * classNamePrefix but does not imply any non-null classNamePrefix.
  212. */
  213. private transient String classNamePrefix;
  214. /**
  215. * True if classNamePrefix must match exactly. Otherwise, the
  216. * className being matched must start with classNamePrefix.
  217. */
  218. private transient boolean classNameExactMatch;
  219. /**
  220. * The member that must match. If null, is implied by any member
  221. * but does not imply any non-null member.
  222. */
  223. private transient String member;
  224. /**
  225. * The objectName that must match. If null, is implied by any
  226. * objectName but does not imply any non-null objectName.
  227. */
  228. private transient ObjectName objectName;
  229. /**
  230. * Parse <code>actions</code> parameter.
  231. */
  232. private void parseActions() {
  233. int mask;
  234. if (actions == null)
  235. throw new IllegalArgumentException("MBeanPermission: " +
  236. "actions can't be null");
  237. if (actions.equals(""))
  238. throw new IllegalArgumentException("MBeanPermission: " +
  239. "actions can't be empty");
  240. mask = getMask(actions);
  241. if ((mask & ALL) != mask)
  242. throw new IllegalArgumentException("Invalid actions mask");
  243. if (mask == NONE)
  244. throw new IllegalArgumentException("Invalid actions mask");
  245. this.mask = mask;
  246. }
  247. /**
  248. * Parse <code>name</code> parameter.
  249. */
  250. private void parseName() {
  251. String name = getName();
  252. if (name.equals(""))
  253. throw new IllegalArgumentException("MBeanPermission name " +
  254. "cannot be empty");
  255. /* The name looks like "class#member[objectname]". We subtract
  256. elements from the right as we parse, so after parsing the
  257. objectname we have "class#member" and after parsing the
  258. member we have "class". Each element is optional. */
  259. // Parse ObjectName
  260. int openingBracket = name.indexOf("[");
  261. if (openingBracket == -1) {
  262. // If "[on]" missing then ObjectName("*:*")
  263. //
  264. objectName = allObjectNames;
  265. } else {
  266. if (!name.endsWith("]")) {
  267. throw new IllegalArgumentException("MBeanPermission: " +
  268. "The ObjectName in the " +
  269. "target name must be " +
  270. "included in square " +
  271. "brackets");
  272. } else {
  273. // Create ObjectName
  274. //
  275. try {
  276. // If "[]" then ObjectName("*:*")
  277. //
  278. String on = name.substring(openingBracket + 1,
  279. name.length() - 1);
  280. if (on.equals(""))
  281. objectName = allObjectNames;
  282. else if (on.equals("-"))
  283. objectName = null;
  284. else
  285. objectName = new ObjectName(on);
  286. } catch (MalformedObjectNameException e) {
  287. throw new IllegalArgumentException("MBeanPermission: " +
  288. "The target name does " +
  289. "not specify a valid " +
  290. "ObjectName");
  291. }
  292. }
  293. name = name.substring(0, openingBracket);
  294. }
  295. // Parse member
  296. int poundSign = name.indexOf("#");
  297. if (poundSign == -1)
  298. setMember("*");
  299. else {
  300. String memberName = name.substring(poundSign + 1);
  301. setMember(memberName);
  302. name = name.substring(0, poundSign);
  303. }
  304. // Parse className
  305. setClassName(name);
  306. }
  307. /**
  308. * Assign fields based on className, member, and objectName
  309. * parameters.
  310. */
  311. private void initName(String className, String member,
  312. ObjectName objectName) {
  313. setClassName(className);
  314. setMember(member);
  315. this.objectName = objectName;
  316. }
  317. private void setClassName(String className) {
  318. if (className == null || className.equals("-")) {
  319. classNamePrefix = null;
  320. classNameExactMatch = false;
  321. } else if (className.equals("") || className.equals("*")) {
  322. classNamePrefix = "";
  323. classNameExactMatch = false;
  324. } else if (className.endsWith(".*")) {
  325. // Note that we include the "." in the required prefix
  326. classNamePrefix = className.substring(0, className.length() - 1);
  327. classNameExactMatch = false;
  328. } else {
  329. classNamePrefix = className;
  330. classNameExactMatch = true;
  331. }
  332. }
  333. private void setMember(String member) {
  334. if (member == null || member.equals("-"))
  335. this.member = null;
  336. else if (member.equals(""))
  337. this.member = "*";
  338. else
  339. this.member = member;
  340. }
  341. /**
  342. * <p>Create a new MBeanPermission object with the specified target name
  343. * and actions.</p>
  344. *
  345. * <p>The target name is of the form
  346. * "<code>className#member[objectName]</code>" where each part is
  347. * optional. It must not be empty or null.</p>
  348. *
  349. * <p>The actions parameter contains a comma-separated list of the
  350. * desired actions granted on the target name. It must not be
  351. * empty or null.</p>
  352. *
  353. * @param name the triplet "className#member[objectName]".
  354. * @param actions the action string.
  355. *
  356. * @exception IllegalArgumentException if the <code>name</code> or
  357. * <code>actions</code> is invalid.
  358. */
  359. public MBeanPermission(String name, String actions) {
  360. super(name);
  361. parseName();
  362. this.actions = actions;
  363. parseActions();
  364. }
  365. /**
  366. * <p>Create a new MBeanPermission object with the specified target name
  367. * (class name, member, object name) and actions.</p>
  368. *
  369. * <p>The class name, member and object name parameters define a
  370. * target name of the form
  371. * "<code>className#member[objectName]</code>" where each part is
  372. * optional. This will be the result of {@link #getName()} on the
  373. * resultant MBeanPermission.</p>
  374. *
  375. * <p>The actions parameter contains a comma-separated list of the
  376. * desired actions granted on the target name. It must not be
  377. * empty or null.</p>
  378. *
  379. * @param className the class name to which this permission applies.
  380. * May be null or <code>"-"</code>, which represents a class name
  381. * that is implied by any class name but does not imply any other
  382. * class name.
  383. * @param member the member to which this permission applies. May
  384. * be null or <code>"-"</code>, which represents a member that is
  385. * implied by any member but does not imply any other member.
  386. * @param objectName the object name to which this permission
  387. * applies. May be null, which represents an object name that is
  388. * implied by any object name but does not imply any other object
  389. * name.
  390. * @param actions the action string.
  391. */
  392. public MBeanPermission(String className,
  393. String member,
  394. ObjectName objectName,
  395. String actions) {
  396. super(makeName(className, member, objectName));
  397. initName(className, member, objectName);
  398. this.actions = actions;
  399. parseActions();
  400. }
  401. private static String makeName(String className, String member,
  402. ObjectName objectName) {
  403. StringBuffer name = new StringBuffer();
  404. if (className == null)
  405. className = "-";
  406. name.append(className);
  407. if (member == null)
  408. member = "-";
  409. name.append("#" + member);
  410. if (objectName == null)
  411. name.append("[-]");
  412. else
  413. name.append("[").append(objectName.getCanonicalName()).append("]");
  414. /* In the interests of legibility for Permission.toString(), we
  415. transform the empty string into "*". */
  416. if (name.length() == 0)
  417. return "*";
  418. else
  419. return name.toString();
  420. }
  421. /**
  422. * Returns the "canonical string representation" of the actions. That is,
  423. * this method always returns present actions in alphabetical order.
  424. *
  425. * @return the canonical string representation of the actions.
  426. */
  427. public String getActions() {
  428. if (actions == null)
  429. actions = getActions(this.mask);
  430. return actions;
  431. }
  432. /**
  433. * Returns the "canonical string representation"
  434. * of the actions from the mask.
  435. */
  436. private static String getActions(int mask) {
  437. StringBuffer sb = new StringBuffer();
  438. boolean comma = false;
  439. if ((mask & AddNotificationListener) == AddNotificationListener) {
  440. comma = true;
  441. sb.append("addNotificationListener");
  442. }
  443. if ((mask & GetAttribute) == GetAttribute) {
  444. if (comma) sb.append(',');
  445. else comma = true;
  446. sb.append("getAttribute");
  447. }
  448. if ((mask & GetClassLoader) == GetClassLoader) {
  449. if (comma) sb.append(',');
  450. else comma = true;
  451. sb.append("getClassLoader");
  452. }
  453. if ((mask & GetClassLoaderFor) == GetClassLoaderFor) {
  454. if (comma) sb.append(',');
  455. else comma = true;
  456. sb.append("getClassLoaderFor");
  457. }
  458. if ((mask & GetClassLoaderRepository) == GetClassLoaderRepository) {
  459. if (comma) sb.append(',');
  460. else comma = true;
  461. sb.append("getClassLoaderRepository");
  462. }
  463. if ((mask & GetDomains) == GetDomains) {
  464. if (comma) sb.append(',');
  465. else comma = true;
  466. sb.append("getDomains");
  467. }
  468. if ((mask & GetMBeanInfo) == GetMBeanInfo) {
  469. if (comma) sb.append(',');
  470. else comma = true;
  471. sb.append("getMBeanInfo");
  472. }
  473. if ((mask & GetObjectInstance) == GetObjectInstance) {
  474. if (comma) sb.append(',');
  475. else comma = true;
  476. sb.append("getObjectInstance");
  477. }
  478. if ((mask & Instantiate) == Instantiate) {
  479. if (comma) sb.append(',');
  480. else comma = true;
  481. sb.append("instantiate");
  482. }
  483. if ((mask & Invoke) == Invoke) {
  484. if (comma) sb.append(',');
  485. else comma = true;
  486. sb.append("invoke");
  487. }
  488. if ((mask & IsInstanceOf) == IsInstanceOf) {
  489. if (comma) sb.append(',');
  490. else comma = true;
  491. sb.append("isInstanceOf");
  492. }
  493. if ((mask & QueryMBeans) == QueryMBeans) {
  494. if (comma) sb.append(',');
  495. else comma = true;
  496. sb.append("queryMBeans");
  497. }
  498. if ((mask & QueryNames) == QueryNames) {
  499. if (comma) sb.append(',');
  500. else comma = true;
  501. sb.append("queryNames");
  502. }
  503. if ((mask & RegisterMBean) == RegisterMBean) {
  504. if (comma) sb.append(',');
  505. else comma = true;
  506. sb.append("registerMBean");
  507. }
  508. if ((mask & RemoveNotificationListener) == RemoveNotificationListener) {
  509. if (comma) sb.append(',');
  510. else comma = true;
  511. sb.append("removeNotificationListener");
  512. }
  513. if ((mask & SetAttribute) == SetAttribute) {
  514. if (comma) sb.append(',');
  515. else comma = true;
  516. sb.append("setAttribute");
  517. }
  518. if ((mask & UnregisterMBean) == UnregisterMBean) {
  519. if (comma) sb.append(',');
  520. else comma = true;
  521. sb.append("unregisterMBean");
  522. }
  523. return sb.toString();
  524. }
  525. /**
  526. * Returns the hash code value for this object.
  527. *
  528. * @return a hash code value for this object.
  529. */
  530. public int hashCode() {
  531. return this.getName().hashCode() + this.getActions().hashCode();
  532. }
  533. /**
  534. * Converts an action String to an integer action mask.
  535. *
  536. * @param action the action string.
  537. * @return the action mask.
  538. */
  539. private static int getMask(String action) {
  540. /*
  541. * BE CAREFUL HERE! PARSING ORDER IS IMPORTANT IN THIS ALGORITHM.
  542. *
  543. * The 'string length' test must be performed for the lengthiest
  544. * strings first.
  545. *
  546. * In this permission if the "unregisterMBean" string length test is
  547. * performed after the "registerMBean" string length test the algorithm
  548. * considers the 'unregisterMBean' action as being the 'registerMBean'
  549. * action and a parsing error is returned.
  550. */
  551. int mask = NONE;
  552. if (action == null) {
  553. return mask;
  554. }
  555. if (action.equals("*")) {
  556. return ALL;
  557. }
  558. char[] a = action.toCharArray();
  559. int i = a.length - 1;
  560. if (i < 0)
  561. return mask;
  562. while (i != -1) {
  563. char c;
  564. // skip whitespace
  565. while ((i!=-1) && ((c = a[i]) == ' ' ||
  566. c == '\r' ||
  567. c == '\n' ||
  568. c == '\f' ||
  569. c == '\t'))
  570. i--;
  571. // check for the known strings
  572. int matchlen;
  573. if (i >= 25 && /* removeNotificationListener */
  574. (a[i-25] == 'r') &&
  575. (a[i-24] == 'e') &&
  576. (a[i-23] == 'm') &&
  577. (a[i-22] == 'o') &&
  578. (a[i-21] == 'v') &&
  579. (a[i-20] == 'e') &&
  580. (a[i-19] == 'N') &&
  581. (a[i-18] == 'o') &&
  582. (a[i-17] == 't') &&
  583. (a[i-16] == 'i') &&
  584. (a[i-15] == 'f') &&
  585. (a[i-14] == 'i') &&
  586. (a[i-13] == 'c') &&
  587. (a[i-12] == 'a') &&
  588. (a[i-11] == 't') &&
  589. (a[i-10] == 'i') &&
  590. (a[i-9] == 'o') &&
  591. (a[i-8] == 'n') &&
  592. (a[i-7] == 'L') &&
  593. (a[i-6] == 'i') &&
  594. (a[i-5] == 's') &&
  595. (a[i-4] == 't') &&
  596. (a[i-3] == 'e') &&
  597. (a[i-2] == 'n') &&
  598. (a[i-1] == 'e') &&
  599. (a[i] == 'r')) {
  600. matchlen = 26;
  601. mask |= RemoveNotificationListener;
  602. } else if (i >= 23 && /* getClassLoaderRepository */
  603. (a[i-23] == 'g') &&
  604. (a[i-22] == 'e') &&
  605. (a[i-21] == 't') &&
  606. (a[i-20] == 'C') &&
  607. (a[i-19] == 'l') &&
  608. (a[i-18] == 'a') &&
  609. (a[i-17] == 's') &&
  610. (a[i-16] == 's') &&
  611. (a[i-15] == 'L') &&
  612. (a[i-14] == 'o') &&
  613. (a[i-13] == 'a') &&
  614. (a[i-12] == 'd') &&
  615. (a[i-11] == 'e') &&
  616. (a[i-10] == 'r') &&
  617. (a[i-9] == 'R') &&
  618. (a[i-8] == 'e') &&
  619. (a[i-7] == 'p') &&
  620. (a[i-6] == 'o') &&
  621. (a[i-5] == 's') &&
  622. (a[i-4] == 'i') &&
  623. (a[i-3] == 't') &&
  624. (a[i-2] == 'o') &&
  625. (a[i-1] == 'r') &&
  626. (a[i] == 'y')) {
  627. matchlen = 24;
  628. mask |= GetClassLoaderRepository;
  629. } else if (i >= 22 && /* addNotificationListener */
  630. (a[i-22] == 'a') &&
  631. (a[i-21] == 'd') &&
  632. (a[i-20] == 'd') &&
  633. (a[i-19] == 'N') &&
  634. (a[i-18] == 'o') &&
  635. (a[i-17] == 't') &&
  636. (a[i-16] == 'i') &&
  637. (a[i-15] == 'f') &&
  638. (a[i-14] == 'i') &&
  639. (a[i-13] == 'c') &&
  640. (a[i-12] == 'a') &&
  641. (a[i-11] == 't') &&
  642. (a[i-10] == 'i') &&
  643. (a[i-9] == 'o') &&
  644. (a[i-8] == 'n') &&
  645. (a[i-7] == 'L') &&
  646. (a[i-6] == 'i') &&
  647. (a[i-5] == 's') &&
  648. (a[i-4] == 't') &&
  649. (a[i-3] == 'e') &&
  650. (a[i-2] == 'n') &&
  651. (a[i-1] == 'e') &&
  652. (a[i] == 'r')) {
  653. matchlen = 23;
  654. mask |= AddNotificationListener;
  655. } else if (i >= 16 && /* getClassLoaderFor */
  656. (a[i-16] == 'g') &&
  657. (a[i-15] == 'e') &&
  658. (a[i-14] == 't') &&
  659. (a[i-13] == 'C') &&
  660. (a[i-12] == 'l') &&
  661. (a[i-11] == 'a') &&
  662. (a[i-10] == 's') &&
  663. (a[i-9] == 's') &&
  664. (a[i-8] == 'L') &&
  665. (a[i-7] == 'o') &&
  666. (a[i-6] == 'a') &&
  667. (a[i-5] == 'd') &&
  668. (a[i-4] == 'e') &&
  669. (a[i-3] == 'r') &&
  670. (a[i-2] == 'F') &&
  671. (a[i-1] == 'o') &&
  672. (a[i] == 'r')) {
  673. matchlen = 17;
  674. mask |= GetClassLoaderFor;
  675. } else if (i >= 16 && /* getObjectInstance */
  676. (a[i-16] == 'g') &&
  677. (a[i-15] == 'e') &&
  678. (a[i-14] == 't') &&
  679. (a[i-13] == 'O') &&
  680. (a[i-12] == 'b') &&
  681. (a[i-11] == 'j') &&
  682. (a[i-10] == 'e') &&
  683. (a[i-9] == 'c') &&
  684. (a[i-8] == 't') &&
  685. (a[i-7] == 'I') &&
  686. (a[i-6] == 'n') &&
  687. (a[i-5] == 's') &&
  688. (a[i-4] == 't') &&
  689. (a[i-3] == 'a') &&
  690. (a[i-2] == 'n') &&
  691. (a[i-1] == 'c') &&
  692. (a[i] == 'e')) {
  693. matchlen = 17;
  694. mask |= GetObjectInstance;
  695. } else if (i >= 14 && /* unregisterMBean */
  696. (a[i-14] == 'u') &&
  697. (a[i-13] == 'n') &&
  698. (a[i-12] == 'r') &&
  699. (a[i-11] == 'e') &&
  700. (a[i-10] == 'g') &&
  701. (a[i-9] == 'i') &&
  702. (a[i-8] == 's') &&
  703. (a[i-7] == 't') &&
  704. (a[i-6] == 'e') &&
  705. (a[i-5] == 'r') &&
  706. (a[i-4] == 'M') &&
  707. (a[i-3] == 'B') &&
  708. (a[i-2] == 'e') &&
  709. (a[i-1] == 'a') &&
  710. (a[i] == 'n')) {
  711. matchlen = 15;
  712. mask |= UnregisterMBean;
  713. } else if (i >= 13 && /* getClassLoader */
  714. (a[i-13] == 'g') &&
  715. (a[i-12] == 'e') &&
  716. (a[i-11] == 't') &&
  717. (a[i-10] == 'C') &&
  718. (a[i-9] == 'l') &&
  719. (a[i-8] == 'a') &&
  720. (a[i-7] == 's') &&
  721. (a[i-6] == 's') &&
  722. (a[i-5] == 'L') &&
  723. (a[i-4] == 'o') &&
  724. (a[i-3] == 'a') &&
  725. (a[i-2] == 'd') &&
  726. (a[i-1] == 'e') &&
  727. (a[i] == 'r')) {
  728. matchlen = 14;
  729. mask |= GetClassLoader;
  730. } else if (i >= 12 && /* registerMBean */
  731. (a[i-12] == 'r') &&
  732. (a[i-11] == 'e') &&
  733. (a[i-10] == 'g') &&
  734. (a[i-9] == 'i') &&
  735. (a[i-8] == 's') &&
  736. (a[i-7] == 't') &&
  737. (a[i-6] == 'e') &&
  738. (a[i-5] == 'r') &&
  739. (a[i-4] == 'M') &&
  740. (a[i-3] == 'B') &&
  741. (a[i-2] == 'e') &&
  742. (a[i-1] == 'a') &&
  743. (a[i] == 'n')) {
  744. matchlen = 13;
  745. mask |= RegisterMBean;
  746. } else if (i >= 11 && /* getAttribute */
  747. (a[i-11] == 'g') &&
  748. (a[i-10] == 'e') &&
  749. (a[i-9] == 't') &&
  750. (a[i-8] == 'A') &&
  751. (a[i-7] == 't') &&
  752. (a[i-6] == 't') &&
  753. (a[i-5] == 'r') &&
  754. (a[i-4] == 'i') &&
  755. (a[i-3] == 'b') &&
  756. (a[i-2] == 'u') &&
  757. (a[i-1] == 't') &&
  758. (a[i] == 'e')) {
  759. matchlen = 12;
  760. mask |= GetAttribute;
  761. } else if (i >= 11 && /* getMBeanInfo */
  762. (a[i-11] == 'g') &&
  763. (a[i-10] == 'e') &&
  764. (a[i-9] == 't') &&
  765. (a[i-8] == 'M') &&
  766. (a[i-7] == 'B') &&
  767. (a[i-6] == 'e') &&
  768. (a[i-5] == 'a') &&
  769. (a[i-4] == 'n') &&
  770. (a[i-3] == 'I') &&
  771. (a[i-2] == 'n') &&
  772. (a[i-1] == 'f') &&
  773. (a[i] == 'o')) {
  774. matchlen = 12;
  775. mask |= GetMBeanInfo;
  776. } else if (i >= 11 && /* isInstanceOf */
  777. (a[i-11] == 'i') &&
  778. (a[i-10] == 's') &&
  779. (a[i-9] == 'I') &&
  780. (a[i-8] == 'n') &&
  781. (a[i-7] == 's') &&
  782. (a[i-6] == 't') &&
  783. (a[i-5] == 'a') &&
  784. (a[i-4] == 'n') &&
  785. (a[i-3] == 'c') &&
  786. (a[i-2] == 'e') &&
  787. (a[i-1] == 'O') &&
  788. (a[i] == 'f')) {
  789. matchlen = 12;
  790. mask |= IsInstanceOf;
  791. } else if (i >= 11 && /* setAttribute */
  792. (a[i-11] == 's') &&
  793. (a[i-10] == 'e') &&
  794. (a[i-9] == 't') &&
  795. (a[i-8] == 'A') &&
  796. (a[i-7] == 't') &&
  797. (a[i-6] == 't') &&
  798. (a[i-5] == 'r') &&
  799. (a[i-4] == 'i') &&
  800. (a[i-3] == 'b') &&
  801. (a[i-2] == 'u') &&
  802. (a[i-1] == 't') &&
  803. (a[i] == 'e')) {
  804. matchlen = 12;
  805. mask |= SetAttribute;
  806. } else if (i >= 10 && /* instantiate */
  807. (a[i-10] == 'i') &&
  808. (a[i-9] == 'n') &&
  809. (a[i-8] == 's') &&
  810. (a[i-7] == 't') &&
  811. (a[i-6] == 'a') &&
  812. (a[i-5] == 'n') &&
  813. (a[i-4] == 't') &&
  814. (a[i-3] == 'i') &&
  815. (a[i-2] == 'a') &&
  816. (a[i-1] == 't') &&
  817. (a[i] == 'e')) {
  818. matchlen = 11;
  819. mask |= Instantiate;
  820. } else if (i >= 10 && /* queryMBeans */
  821. (a[i-10] == 'q') &&
  822. (a[i-9] == 'u') &&
  823. (a[i-8] == 'e') &&
  824. (a[i-7] == 'r') &&
  825. (a[i-6] == 'y') &&
  826. (a[i-5] == 'M') &&
  827. (a[i-4] == 'B') &&
  828. (a[i-3] == 'e') &&
  829. (a[i-2] == 'a') &&
  830. (a[i-1] == 'n') &&
  831. (a[i] == 's')) {
  832. matchlen = 11;
  833. mask |= QueryMBeans;
  834. } else if (i >= 9 && /* getDomains */
  835. (a[i-9] == 'g') &&
  836. (a[i-8] == 'e') &&
  837. (a[i-7] == 't') &&
  838. (a[i-6] == 'D') &&
  839. (a[i-5] == 'o') &&
  840. (a[i-4] == 'm') &&
  841. (a[i-3] == 'a') &&
  842. (a[i-2] == 'i') &&
  843. (a[i-1] == 'n') &&
  844. (a[i] == 's')) {
  845. matchlen = 10;
  846. mask |= GetDomains;
  847. } else if (i >= 9 && /* queryNames */
  848. (a[i-9] == 'q') &&
  849. (a[i-8] == 'u') &&
  850. (a[i-7] == 'e') &&
  851. (a[i-6] == 'r') &&
  852. (a[i-5] == 'y') &&
  853. (a[i-4] == 'N') &&
  854. (a[i-3] == 'a') &&
  855. (a[i-2] == 'm') &&
  856. (a[i-1] == 'e') &&
  857. (a[i] == 's')) {
  858. matchlen = 10;
  859. mask |= QueryNames;
  860. } else if (i >= 5 && /* invoke */
  861. (a[i-5] == 'i') &&
  862. (a[i-4] == 'n') &&
  863. (a[i-3] == 'v') &&
  864. (a[i-2] == 'o') &&
  865. (a[i-1] == 'k') &&
  866. (a[i] == 'e')) {
  867. matchlen = 6;
  868. mask |= Invoke;
  869. } else {
  870. // parse error
  871. throw new IllegalArgumentException("Invalid permission: " +
  872. action);
  873. }
  874. // make sure we didn't just match the tail of a word
  875. // like "ackbarfaccept". Also, skip to the comma.
  876. boolean seencomma = false;
  877. while (i >= matchlen && !seencomma) {
  878. switch(a[i-matchlen]) {
  879. case ',':
  880. seencomma = true;
  881. /*FALLTHROUGH*/
  882. case ' ': case '\r': case '\n':
  883. case '\f': case '\t':
  884. break;
  885. default:
  886. throw new IllegalArgumentException("Invalid permission: " +
  887. action);
  888. }
  889. i--;
  890. }
  891. // point i at the location of the comma minus one (or -1).
  892. i -= matchlen;
  893. }
  894. return mask;
  895. }
  896. /**
  897. * <p>Checks if this MBeanPermission object "implies" the
  898. * specified permission.</p>
  899. *
  900. * <p>More specifically, this method returns true if:</p>
  901. *
  902. * <ul>
  903. *
  904. * <li> <i>p</i> is an instance of MBeanPermission; and</li>
  905. *
  906. * <li> <i>p</i> has a null className or <i>p</i>'s className
  907. * matches this object's className; and</li>
  908. *
  909. * <li> <i>p</i> has a null member or <i>p</i>'s member matches this
  910. * object's member; and</li>
  911. *
  912. * <li> <i>p</i> has a null object name or <i>p</i>'s
  913. * object name matches this object's object name; and</li>
  914. *
  915. * <li> <i>p</i>'s actions are a subset of this object's actions</li>
  916. *
  917. * </ul>
  918. *
  919. * <p>If this object's className is "<code>*</code>", <i>p</i>'s
  920. * className always matches it. If it is "<code>a.*</code>", <i>p</i>'s
  921. * className matches it if it begins with "<code>a.</code>".</p>
  922. *
  923. * <p>If this object's member is "<code>*</code>", <i>p</i>'s
  924. * member always matches it.</p>
  925. *
  926. * <p>If this object's objectName <i>n1</i> is an object name pattern,
  927. * <i>p</i>'s objectName <i>n2</i> matches it if
  928. * {@link ObjectName#equals <i>n1</i>.equals(<i>n2</i>)} or if
  929. * {@link ObjectName#apply <i>n1</i>.apply(<i>n2</i>)}.</p>
  930. *
  931. * <p>A permission that includes the <code>queryMBeans</code> action
  932. * is considered to include <code>queryNames</code> as well.</p>
  933. *
  934. * @param p the permission to check against.
  935. * @return true if the specified permission is implied by this object,
  936. * false if not.
  937. */
  938. public boolean implies(Permission p) {
  939. if (!(p instanceof MBeanPermission))
  940. return false;
  941. MBeanPermission that = (MBeanPermission) p;
  942. // Actions
  943. //
  944. // The actions in 'this' permission must be a
  945. // superset of the actions in 'that' permission
  946. //
  947. /* "queryMBeans" implies "queryNames" */
  948. if ((this.mask & QueryMBeans) == QueryMBeans) {
  949. if (((this.mask | QueryNames) & that.mask) != that.mask) {
  950. //System.out.println("action [with QueryNames] does not imply");
  951. return false;
  952. }
  953. } else {
  954. if ((this.mask & that.mask) != that.mask) {
  955. //System.out.println("action does not imply");
  956. return false;
  957. }
  958. }
  959. // Target name
  960. //
  961. // The 'className' check is true iff:
  962. // 1) the className in 'this' permission is omitted or "*", or
  963. // 2) the className in 'that' permission is omitted or "*", or
  964. // 3) the className in 'this' permission does pattern
  965. // matching with the className in 'that' permission.
  966. //
  967. // The 'member' check is true iff:
  968. // 1) the member in 'this' permission is omitted or "*", or
  969. // 2) the member in 'that' permission is omitted or "*", or
  970. // 3) the member in 'this' permission equals the member in
  971. // 'that' permission.
  972. //
  973. // The 'object name' check is true iff:
  974. // 1) the object name in 'this' permission is omitted or "*:*", or
  975. // 2) the object name in 'that' permission is omitted or "*:*", or
  976. // 3) the object name in 'this' permission does pattern
  977. // matching with the object name in 'that' permission.
  978. //
  979. /* Check if this.className implies that.className.
  980. If that.classNamePrefix is empty that means the className is
  981. irrelevant for this permission check. Otherwise, we do not
  982. expect that "that" contains a wildcard, since it is a
  983. needed permission. So we assume that.classNameExactMatch. */
  984. if (that.classNamePrefix == null) {
  985. // bottom is implied
  986. } else if (this.classNamePrefix == null) {
  987. // bottom implies nothing but itself
  988. return false;
  989. } else if (this.classNameExactMatch) {
  990. if (!that.classNameExactMatch)
  991. return false; // exact never implies wildcard
  992. if (!that.classNamePrefix.equals(this.classNamePrefix))
  993. return false; // exact match fails
  994. } else {
  995. // prefix match, works even if "that" is also a wildcard
  996. // e.g. a.* implies a.* and a.b.*
  997. if (!that.classNamePrefix.startsWith(this.classNamePrefix))
  998. return false;
  999. }
  1000. /* Check if this.member implies that.member */
  1001. if (that.member == null) {
  1002. // bottom is implied
  1003. } else if (this.member == null) {
  1004. // bottom implies nothing but itself
  1005. return false;
  1006. } else if (this.member.equals("*")) {
  1007. // wildcard implies everything (including itself)
  1008. } else if (!this.member.equals(that.member)) {
  1009. return false;
  1010. }
  1011. /* Check if this.objectName implies that.objectName */
  1012. if (that.objectName == null) {
  1013. // bottom is implied
  1014. } else if (this.objectName == null) {
  1015. // bottom implies nothing but itself
  1016. return false;
  1017. } else if (!this.objectName.apply(that.objectName)) {
  1018. /* ObjectName.apply returns false if that.objectName is a
  1019. wildcard so we also allow equals for that case. This
  1020. never happens during real permission checks, but means
  1021. the implies relation is reflexive. */
  1022. if (!this.objectName.equals(that.objectName))
  1023. return false;
  1024. }
  1025. return true;
  1026. }
  1027. /**
  1028. * Checks two MBeanPermission objects for equality. Checks
  1029. * that <i>obj</i> is an MBeanPermission, and has the same
  1030. * name and actions as this object.
  1031. * <P>
  1032. * @param obj the object we are testing for equality with this object.
  1033. * @return true if obj is an MBeanPermission, and has the
  1034. * same name and actions as this MBeanPermission object.
  1035. */
  1036. public boolean equals(Object obj) {
  1037. if (obj == this)
  1038. return true;
  1039. if (! (obj instanceof MBeanPermission))
  1040. return false;
  1041. MBeanPermission that = (MBeanPermission) obj;
  1042. return (this.mask == that.mask) &&
  1043. (this.getName().equals(that.getName()));
  1044. }
  1045. /**
  1046. * Deserialize this object based on its name and actions.
  1047. */
  1048. private void readObject(ObjectInputStream in)
  1049. throws IOException, ClassNotFoundException {
  1050. in.defaultReadObject();
  1051. parseName();
  1052. parseActions();
  1053. }
  1054. }