1. /*
  2. * @(#)file AclImpl.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.security.Principal;
  13. import java.security.acl.Acl;
  14. import java.security.acl.AclEntry;
  15. import java.security.acl.NotOwnerException;
  16. import java.io.Serializable;
  17. import java.util.Vector;
  18. import java.util.Enumeration;
  19. /**
  20. * Represent an Access Control List (ACL) which is used to guard access to http adaptor.
  21. * <P>
  22. * It is a data structure with multiple ACL entries. Each ACL entry, of interface type
  23. * AclEntry, contains a set of permissions and a set of communities associated with a
  24. * particular principal. (A principal represents an entity such as a host or a group of host).
  25. * Additionally, each ACL entry is specified as being either positive or negative.
  26. * If positive, the permissions are to be granted to the associated principal.
  27. * If negative, the permissions are to be denied.
  28. *
  29. * @see java.security.acl.Acl
  30. * @version 4.11 12/19/03
  31. * @author Sun Microsystems, Inc
  32. */
  33. class AclImpl extends OwnerImpl implements Acl, Serializable {
  34. private Vector entryList = null;
  35. private String aclName = null;
  36. /**
  37. * Constructs the ACL with a specified owner
  38. *
  39. * @param owner owner of the ACL.
  40. * @param name name of this ACL.
  41. */
  42. public AclImpl (PrincipalImpl owner, String name) {
  43. super(owner);
  44. entryList = new Vector();
  45. aclName = name;
  46. }
  47. /**
  48. * Sets the name of this ACL.
  49. *
  50. * @param caller the principal invoking this method. It must be an owner
  51. * of this ACL.
  52. * @param name the name to be given to this ACL.
  53. *
  54. * @exception NotOwnerException if the caller principal is not an owner
  55. * of this ACL.
  56. * @see java.security.Principal
  57. */
  58. public void setName(Principal caller, String name)
  59. throws NotOwnerException {
  60. if (!isOwner(caller))
  61. throw new NotOwnerException();
  62. aclName = name;
  63. }
  64. /**
  65. * Returns the name of this ACL.
  66. *
  67. * @return the name of this ACL.
  68. */
  69. public String getName(){
  70. return aclName;
  71. }
  72. /**
  73. * Adds an ACL entry to this ACL. An entry associates a principal (e.g., an individual or a group)
  74. * with a set of permissions. Each principal can have at most one positive ACL entry
  75. * (specifying permissions to be granted to the principal) and one negative ACL entry
  76. * (specifying permissions to be denied). If there is already an ACL entry
  77. * of the same type (negative or positive) already in the ACL, false is returned.
  78. *
  79. * @param caller the principal invoking this method. It must be an owner
  80. * of this ACL.
  81. * @param entry the ACL entry to be added to this ACL.
  82. * @return true on success, false if an entry of the same type (positive
  83. * or negative) for the same principal is already present in this ACL.
  84. * @exception NotOwnerException if the caller principal is not an owner of
  85. * this ACL.
  86. * @see java.security.Principal
  87. */
  88. public boolean addEntry(Principal caller, AclEntry entry)
  89. throws NotOwnerException {
  90. if (!isOwner(caller))
  91. throw new NotOwnerException();
  92. if (entryList.contains(entry))
  93. return false;
  94. /*
  95. for (Enumeration e = entryList.elements();e.hasMoreElements();){
  96. AclEntry ent = (AclEntry) e.nextElement();
  97. if (ent.getPrincipal().equals(entry.getPrincipal()))
  98. return false;
  99. }
  100. */
  101. entryList.addElement(entry);
  102. return true;
  103. }
  104. /**
  105. * Removes an ACL entry from this ACL.
  106. *
  107. * @param caller the principal invoking this method. It must be an owner
  108. * of this ACL.
  109. * @param entry the ACL entry to be removed from this ACL.
  110. * @return true on success, false if the entry is not part of this ACL.
  111. * @exception NotOwnerException if the caller principal is not an owner
  112. * of this Acl.
  113. * @see java.security.Principal
  114. * @see java.security.acl.AclEntry
  115. */
  116. public boolean removeEntry(Principal caller, AclEntry entry)
  117. throws NotOwnerException {
  118. if (!isOwner(caller))
  119. throw new NotOwnerException();
  120. return (entryList.removeElement(entry));
  121. }
  122. /**
  123. * Removes all ACL entries from this ACL.
  124. *
  125. * @param caller the principal invoking this method. It must be an owner
  126. * of this ACL.
  127. * @exception NotOwnerException if the caller principal is not an owner of
  128. * this Acl.
  129. * @see java.security.Principal
  130. */
  131. public void removeAll(Principal caller)
  132. throws NotOwnerException {
  133. if (!isOwner(caller))
  134. throw new NotOwnerException();
  135. entryList.removeAllElements();
  136. }
  137. /**
  138. * Returns an enumeration for the set of allowed permissions for
  139. * the specified principal
  140. * (representing an entity such as an individual or a group).
  141. * This set of allowed permissions is calculated as follows:
  142. * <UL>
  143. * <LI>If there is no entry in this Access Control List for the specified
  144. * principal, an empty permission set is returned.</LI>
  145. * <LI>Otherwise, the principal's group permission sets are determined.
  146. * (A principal can belong to one or more groups, where a group is a group
  147. * of principals, represented by the Group interface.)</LI>
  148. * </UL>
  149. * @param user the principal whose permission set is to be returned.
  150. * @return the permission set specifying the permissions the principal
  151. * is allowed.
  152. * @see java.security.Principal
  153. */
  154. public Enumeration getPermissions(Principal user){
  155. Vector empty = new Vector();
  156. for (Enumeration e = entryList.elements();e.hasMoreElements();){
  157. AclEntry ent = (AclEntry) e.nextElement();
  158. if (ent.getPrincipal().equals(user))
  159. return ent.permissions();
  160. }
  161. return empty.elements();
  162. }
  163. /**
  164. * Returns an enumeration of the entries in this ACL. Each element in the
  165. * enumeration is of type AclEntry.
  166. *
  167. * @return an enumeration of the entries in this ACL.
  168. */
  169. public Enumeration entries(){
  170. return entryList.elements();
  171. }
  172. /**
  173. * Checks whether or not the specified principal has the specified
  174. * permission.
  175. * If it does, true is returned, otherwise false is returned.
  176. * More specifically, this method checks whether the passed permission
  177. * is a member of the allowed permission set of the specified principal.
  178. * The allowed permission set is determined by the same algorithm as is
  179. * used by the getPermissions method.
  180. *
  181. * @param user the principal, assumed to be a valid authenticated Principal.
  182. * @param perm the permission to be checked for.
  183. * @return true if the principal has the specified permission,
  184. * false otherwise.
  185. * @see java.security.Principal
  186. * @see java.security.Permission
  187. */
  188. public boolean checkPermission(Principal user,
  189. java.security.acl.Permission perm) {
  190. for (Enumeration e = entryList.elements();e.hasMoreElements();){
  191. AclEntry ent = (AclEntry) e.nextElement();
  192. if (ent.getPrincipal().equals(user))
  193. if (ent.checkPermission(perm)) return true;
  194. }
  195. return false;
  196. }
  197. /**
  198. * Checks whether or not the specified principal has the specified
  199. * permission.
  200. * If it does, true is returned, otherwise false is returned.
  201. * More specifically, this method checks whether the passed permission
  202. * is a member of the allowed permission set of the specified principal.
  203. * The allowed permission set is determined by the same algorithm as is
  204. * used by the getPermissions method.
  205. *
  206. * @param user the principal, assumed to be a valid authenticated Principal.
  207. * @param community the community name associated with the principal.
  208. * @param perm the permission to be checked for.
  209. * @return true if the principal has the specified permission, false
  210. * otherwise.
  211. * @see java.security.Principal
  212. * @see java.security.Permission
  213. */
  214. public boolean checkPermission(Principal user, String community,
  215. java.security.acl.Permission perm) {
  216. for (Enumeration e = entryList.elements();e.hasMoreElements();){
  217. AclEntryImpl ent = (AclEntryImpl) e.nextElement();
  218. if (ent.getPrincipal().equals(user))
  219. if (ent.checkPermission(perm) && ent.checkCommunity(community)) return true;
  220. }
  221. return false;
  222. }
  223. /**
  224. * Checks whether or not the specified community string is defined.
  225. *
  226. * @param community the community name associated with the principal.
  227. *
  228. * @return true if the specified community string is defined, false
  229. * otherwise.
  230. * @see java.security.Principal
  231. * @see java.security.Permission
  232. */
  233. public boolean checkCommunity(String community) {
  234. for (Enumeration e = entryList.elements();e.hasMoreElements();){
  235. AclEntryImpl ent = (AclEntryImpl) e.nextElement();
  236. if (ent.checkCommunity(community)) return true;
  237. }
  238. return false;
  239. }
  240. /**
  241. * Returns a string representation of the ACL contents.
  242. *
  243. * @return a string representation of the ACL contents.
  244. */
  245. public String toString(){
  246. return ("AclImpl: "+ getName());
  247. }
  248. }