0N/A/*
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage com.sun.jmx.snmp.IPAcl;
0N/A
0N/A
0N/A
0N/Aimport java.security.Principal;
0N/Aimport java.security.acl.Acl;
0N/Aimport java.security.acl.AclEntry;
0N/Aimport java.security.acl.NotOwnerException;
0N/A
0N/Aimport java.io.Serializable;
0N/Aimport java.security.acl.Permission;
0N/Aimport java.util.Vector;
0N/Aimport java.util.Enumeration;
0N/A
0N/A
0N/A/**
0N/A * Represent an Access Control List (ACL) which is used to guard access to http adaptor.
0N/A * <P>
0N/A * It is a data structure with multiple ACL entries. Each ACL entry, of interface type
0N/A * AclEntry, contains a set of permissions and a set of communities associated with a
0N/A * particular principal. (A principal represents an entity such as a host or a group of host).
0N/A * Additionally, each ACL entry is specified as being either positive or negative.
0N/A * If positive, the permissions are to be granted to the associated principal.
0N/A * If negative, the permissions are to be denied.
0N/A *
0N/A * @see java.security.acl.Acl
0N/A */
0N/A
0N/Aclass AclImpl extends OwnerImpl implements Acl, Serializable {
0N/A private static final long serialVersionUID = -2250957591085270029L;
0N/A
0N/A private Vector<AclEntry> entryList = null;
0N/A private String aclName = null;
0N/A
0N/A /**
0N/A * Constructs the ACL with a specified owner
0N/A *
0N/A * @param owner owner of the ACL.
0N/A * @param name name of this ACL.
0N/A */
0N/A public AclImpl (PrincipalImpl owner, String name) {
0N/A super(owner);
0N/A entryList = new Vector<AclEntry>();
0N/A aclName = name;
0N/A }
0N/A
0N/A /**
0N/A * Sets the name of this ACL.
0N/A *
0N/A * @param caller the principal invoking this method. It must be an owner
0N/A * of this ACL.
0N/A * @param name the name to be given to this ACL.
0N/A *
0N/A * @exception NotOwnerException if the caller principal is not an owner
0N/A * of this ACL.
0N/A * @see java.security.Principal
0N/A */
0N/A public void setName(Principal caller, String name)
0N/A throws NotOwnerException {
0N/A if (!isOwner(caller))
0N/A throw new NotOwnerException();
0N/A aclName = name;
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of this ACL.
0N/A *
0N/A * @return the name of this ACL.
0N/A */
0N/A public String getName(){
0N/A return aclName;
0N/A }
0N/A
0N/A /**
0N/A * Adds an ACL entry to this ACL. An entry associates a principal (e.g., an individual or a group)
0N/A * with a set of permissions. Each principal can have at most one positive ACL entry
0N/A * (specifying permissions to be granted to the principal) and one negative ACL entry
0N/A * (specifying permissions to be denied). If there is already an ACL entry
0N/A * of the same type (negative or positive) already in the ACL, false is returned.
0N/A *
0N/A * @param caller the principal invoking this method. It must be an owner
0N/A * of this ACL.
0N/A * @param entry the ACL entry to be added to this ACL.
0N/A * @return true on success, false if an entry of the same type (positive
0N/A * or negative) for the same principal is already present in this ACL.
0N/A * @exception NotOwnerException if the caller principal is not an owner of
0N/A * this ACL.
0N/A * @see java.security.Principal
0N/A */
0N/A public boolean addEntry(Principal caller, AclEntry entry)
0N/A throws NotOwnerException {
0N/A if (!isOwner(caller))
0N/A throw new NotOwnerException();
0N/A
0N/A if (entryList.contains(entry))
0N/A return false;
0N/A /*
0N/A for (Enumeration e = entryList.elements();e.hasMoreElements();){
0N/A AclEntry ent = (AclEntry) e.nextElement();
0N/A if (ent.getPrincipal().equals(entry.getPrincipal()))
0N/A return false;
0N/A }
0N/A */
0N/A
0N/A entryList.addElement(entry);
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Removes an ACL entry from this ACL.
0N/A *
0N/A * @param caller the principal invoking this method. It must be an owner
0N/A * of this ACL.
0N/A * @param entry the ACL entry to be removed from this ACL.
0N/A * @return true on success, false if the entry is not part of this ACL.
0N/A * @exception NotOwnerException if the caller principal is not an owner
0N/A * of this Acl.
0N/A * @see java.security.Principal
0N/A * @see java.security.acl.AclEntry
0N/A */
0N/A public boolean removeEntry(Principal caller, AclEntry entry)
0N/A throws NotOwnerException {
0N/A if (!isOwner(caller))
0N/A throw new NotOwnerException();
0N/A
0N/A return (entryList.removeElement(entry));
0N/A }
0N/A
0N/A /**
0N/A * Removes all ACL entries from this ACL.
0N/A *
0N/A * @param caller the principal invoking this method. It must be an owner
0N/A * of this ACL.
0N/A * @exception NotOwnerException if the caller principal is not an owner of
0N/A * this Acl.
0N/A * @see java.security.Principal
0N/A */
0N/A public void removeAll(Principal caller)
0N/A throws NotOwnerException {
0N/A if (!isOwner(caller))
0N/A throw new NotOwnerException();
0N/A entryList.removeAllElements();
0N/A }
0N/A
0N/A /**
0N/A * Returns an enumeration for the set of allowed permissions for
0N/A * the specified principal
0N/A * (representing an entity such as an individual or a group).
0N/A * This set of allowed permissions is calculated as follows:
0N/A * <UL>
0N/A * <LI>If there is no entry in this Access Control List for the specified
0N/A * principal, an empty permission set is returned.</LI>
0N/A * <LI>Otherwise, the principal's group permission sets are determined.
0N/A * (A principal can belong to one or more groups, where a group is a group
0N/A * of principals, represented by the Group interface.)</LI>
0N/A * </UL>
0N/A * @param user the principal whose permission set is to be returned.
0N/A * @return the permission set specifying the permissions the principal
0N/A * is allowed.
0N/A * @see java.security.Principal
0N/A */
0N/A public Enumeration<Permission> getPermissions(Principal user){
0N/A Vector<Permission> empty = new Vector<Permission>();
0N/A for (Enumeration<AclEntry> e = entryList.elements();e.hasMoreElements();){
0N/A AclEntry ent = e.nextElement();
0N/A if (ent.getPrincipal().equals(user))
0N/A return ent.permissions();
0N/A }
0N/A return empty.elements();
0N/A }
0N/A
0N/A /**
0N/A * Returns an enumeration of the entries in this ACL. Each element in the
0N/A * enumeration is of type AclEntry.
0N/A *
0N/A * @return an enumeration of the entries in this ACL.
0N/A */
0N/A public Enumeration<AclEntry> entries(){
0N/A return entryList.elements();
0N/A }
0N/A
0N/A /**
0N/A * Checks whether or not the specified principal has the specified
0N/A * permission.
0N/A * If it does, true is returned, otherwise false is returned.
0N/A * More specifically, this method checks whether the passed permission
0N/A * is a member of the allowed permission set of the specified principal.
0N/A * The allowed permission set is determined by the same algorithm as is
0N/A * used by the getPermissions method.
0N/A *
0N/A * @param user the principal, assumed to be a valid authenticated Principal.
0N/A * @param perm the permission to be checked for.
0N/A * @return true if the principal has the specified permission,
0N/A * false otherwise.
0N/A * @see java.security.Principal
0N/A * @see java.security.Permission
0N/A */
0N/A public boolean checkPermission(Principal user,
0N/A java.security.acl.Permission perm) {
0N/A for (Enumeration e = entryList.elements();e.hasMoreElements();){
0N/A AclEntry ent = (AclEntry) e.nextElement();
0N/A if (ent.getPrincipal().equals(user))
0N/A if (ent.checkPermission(perm)) return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Checks whether or not the specified principal has the specified
0N/A * permission.
0N/A * If it does, true is returned, otherwise false is returned.
0N/A * More specifically, this method checks whether the passed permission
0N/A * is a member of the allowed permission set of the specified principal.
0N/A * The allowed permission set is determined by the same algorithm as is
0N/A * used by the getPermissions method.
0N/A *
0N/A * @param user the principal, assumed to be a valid authenticated Principal.
0N/A * @param community the community name associated with the principal.
0N/A * @param perm the permission to be checked for.
0N/A * @return true if the principal has the specified permission, false
0N/A * otherwise.
0N/A * @see java.security.Principal
0N/A * @see java.security.Permission
0N/A */
0N/A public boolean checkPermission(Principal user, String community,
0N/A java.security.acl.Permission perm) {
0N/A for (Enumeration e = entryList.elements();e.hasMoreElements();){
0N/A AclEntryImpl ent = (AclEntryImpl) e.nextElement();
0N/A if (ent.getPrincipal().equals(user))
0N/A if (ent.checkPermission(perm) && ent.checkCommunity(community)) return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Checks whether or not the specified community string is defined.
0N/A *
0N/A * @param community the community name associated with the principal.
0N/A *
0N/A * @return true if the specified community string is defined, false
0N/A * otherwise.
0N/A * @see java.security.Principal
0N/A * @see java.security.Permission
0N/A */
0N/A public boolean checkCommunity(String community) {
0N/A for (Enumeration e = entryList.elements();e.hasMoreElements();){
0N/A AclEntryImpl ent = (AclEntryImpl) e.nextElement();
0N/A if (ent.checkCommunity(community)) return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of the ACL contents.
0N/A *
0N/A * @return a string representation of the ACL contents.
0N/A */
0N/A public String toString(){
0N/A return ("AclImpl: "+ getName());
0N/A }
0N/A}