1. /*
  2. * @(#)file AclEntryImpl.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.11
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp.IPAcl;
  12. import java.util.Vector;
  13. import java.util.Enumeration;
  14. import java.io.Serializable;
  15. import java.net.UnknownHostException;
  16. import java.security.Principal;
  17. import java.security.acl.AclEntry;
  18. /**
  19. * Represent one entry in the Access Control List (ACL).
  20. * This ACL entry object contains a permission associated with a particular principal.
  21. * (A principal represents an entity such as an individual machine or a group).
  22. *
  23. * @see java.security.acl.AclEntry
  24. * @version 4.11 12/19/03
  25. * @author Sun Microsystems, Inc
  26. */
  27. class AclEntryImpl implements AclEntry, Serializable {
  28. private AclEntryImpl (AclEntryImpl i) throws UnknownHostException {
  29. setPrincipal(i.getPrincipal());
  30. permList = new Vector();
  31. commList = new Vector();
  32. for (Enumeration en = i.communities(); en.hasMoreElements();){
  33. addCommunity((String)en.nextElement());
  34. }
  35. for (Enumeration en = i.permissions(); en.hasMoreElements();){
  36. addPermission((java.security.acl.Permission)en.nextElement());
  37. }
  38. if (i.isNegative()) setNegativePermissions();
  39. }
  40. /**
  41. * Contructs an empty ACL entry.
  42. */
  43. public AclEntryImpl (){
  44. princ = null;
  45. permList = new Vector();
  46. commList = new Vector();
  47. }
  48. /**
  49. * Constructs an ACL entry with a specified principal.
  50. *
  51. * @param p the principal to be set for this entry.
  52. */
  53. public AclEntryImpl (Principal p) throws UnknownHostException {
  54. princ = p;
  55. permList = new Vector();
  56. commList = new Vector();
  57. }
  58. /**
  59. * Clones this ACL entry.
  60. *
  61. * @return a clone of this ACL entry.
  62. */
  63. public Object clone() {
  64. AclEntryImpl i;
  65. try {
  66. i = new AclEntryImpl(this);
  67. }catch (UnknownHostException e) {
  68. i = null;
  69. }
  70. return (Object) i;
  71. }
  72. /**
  73. * Returns true if this is a negative ACL entry (one denying the associated principal
  74. * the set of permissions in the entry), false otherwise.
  75. *
  76. * @return true if this is a negative ACL entry, false if it's not.
  77. */
  78. public boolean isNegative(){
  79. return neg;
  80. }
  81. /**
  82. * Adds the specified permission to this ACL entry. Note: An entry can
  83. * have multiple permissions.
  84. *
  85. * @param perm the permission to be associated with the principal in this
  86. * entry
  87. * @return true if the permission is removed, false if the permission was
  88. * not part of this entry's permission set.
  89. *
  90. */
  91. public boolean addPermission(java.security.acl.Permission perm){
  92. if (permList.contains(perm)) return false;
  93. permList.addElement(perm);
  94. return true;
  95. }
  96. /**
  97. * Removes the specified permission from this ACL entry.
  98. *
  99. * @param perm the permission to be removed from this entry.
  100. * @return true if the permission is removed, false if the permission
  101. * was not part of this entry's permission set.
  102. */
  103. public boolean removePermission(java.security.acl.Permission perm){
  104. if (!permList.contains(perm)) return false;
  105. permList.removeElement(perm);
  106. return true;
  107. }
  108. /**
  109. * Checks if the specified permission is part of the permission set in
  110. * this entry.
  111. *
  112. * @param perm the permission to be checked for.
  113. * @return true if the permission is part of the permission set in this
  114. * entry, false otherwise.
  115. */
  116. public boolean checkPermission(java.security.acl.Permission perm){
  117. return (permList.contains(perm));
  118. }
  119. /**
  120. * Returns an enumeration of the permissions in this ACL entry.
  121. *
  122. * @return an enumeration of the permissions in this ACL entry.
  123. */
  124. public Enumeration permissions(){
  125. return permList.elements();
  126. }
  127. /**
  128. * Sets this ACL entry to be a negative one. That is, the associated principal
  129. * (e.g., a user or a group) will be denied the permission set specified in the
  130. * entry. Note: ACL entries are by default positive. An entry becomes a negative
  131. * entry only if this setNegativePermissions method is called on it.
  132. *
  133. * Not Implemented.
  134. */
  135. public void setNegativePermissions(){
  136. neg = true;
  137. }
  138. /**
  139. * Returns the principal for which permissions are granted or denied by this ACL
  140. * entry. Returns null if there is no principal set for this entry yet.
  141. *
  142. * @return the principal associated with this entry.
  143. */
  144. public Principal getPrincipal(){
  145. return princ;
  146. }
  147. /**
  148. * Specifies the principal for which permissions are granted or denied by
  149. * this ACL entry. If a principal was already set for this ACL entry,
  150. * false is returned, otherwise true is returned.
  151. *
  152. * @param p the principal to be set for this entry.
  153. * @return true if the principal is set, false if there was already a
  154. * principal set for this entry.
  155. */
  156. public boolean setPrincipal(Principal p) {
  157. if (princ != null )
  158. return false;
  159. princ = p;
  160. return true;
  161. }
  162. /**
  163. * Returns a string representation of the contents of this ACL entry.
  164. *
  165. * @return a string representation of the contents.
  166. */
  167. public String toString(){
  168. return "AclEntry:"+princ.toString();
  169. }
  170. /**
  171. * Returns an enumeration of the communities in this ACL entry.
  172. *
  173. * @return an enumeration of the communities in this ACL entry.
  174. */
  175. public Enumeration communities(){
  176. return commList.elements();
  177. }
  178. /**
  179. * Adds the specified community to this ACL entry. Note: An entry can
  180. * have multiple communities.
  181. *
  182. * @param comm the community to be associated with the principal
  183. * in this entry.
  184. * @return true if the community was added, false if the community was
  185. * already part of this entry's community set.
  186. */
  187. public boolean addCommunity(String comm){
  188. if (commList.contains(comm)) return false;
  189. commList.addElement(comm);
  190. return true;
  191. }
  192. /**
  193. * Removes the specified community from this ACL entry.
  194. *
  195. * @param comm the community to be removed from this entry.
  196. * @return true if the community is removed, false if the community was
  197. * not part of this entry's community set.
  198. */
  199. public boolean removeCommunity(String comm){
  200. if (!commList.contains(comm)) return false;
  201. commList.removeElement(comm);
  202. return true;
  203. }
  204. /**
  205. * Checks if the specified community is part of the community set in this
  206. * entry.
  207. *
  208. * @param comm the community to be checked for.
  209. * @return true if the community is part of the community set in this
  210. * entry, false otherwise.
  211. */
  212. public boolean checkCommunity(String comm){
  213. return (commList.contains(comm));
  214. }
  215. private Principal princ = null;
  216. private boolean neg = false;
  217. private Vector permList = null;
  218. private Vector commList = null;
  219. }