0N/A/*
2362N/A * Copyright (c) 1997, 2003, 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/Apackage java.util;
0N/A
0N/Aimport java.io.Serializable;
0N/Aimport java.io.IOException;
0N/Aimport java.security.*;
0N/Aimport java.util.Map;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Collections;
0N/Aimport java.io.ObjectStreamField;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/Aimport sun.security.util.SecurityConstants;
0N/A
0N/A/**
0N/A * This class is for property permissions.
0N/A *
0N/A * <P>
0N/A * The name is the name of the property ("java.home",
0N/A * "os.name", etc). The naming
0N/A * convention follows the hierarchical property naming convention.
0N/A * Also, an asterisk
0N/A * may appear at the end of the name, following a ".", or by itself, to
0N/A * signify a wildcard match. For example: "java.*" or "*" is valid,
0N/A * "*java" or "a*b" is not valid.
0N/A * <P>
0N/A * <P>
0N/A * The actions to be granted are passed to the constructor in a string containing
0N/A * a list of one or more comma-separated keywords. The possible keywords are
0N/A * "read" and "write". Their meaning is defined as follows:
0N/A * <P>
0N/A * <DL>
0N/A * <DT> read
0N/A * <DD> read permission. Allows <code>System.getProperty</code> to
0N/A * be called.
0N/A * <DT> write
0N/A * <DD> write permission. Allows <code>System.setProperty</code> to
0N/A * be called.
0N/A * </DL>
0N/A * <P>
0N/A * The actions string is converted to lowercase before processing.
0N/A * <P>
0N/A * Care should be taken before granting code permission to access
0N/A * certain system properties. For example, granting permission to
0N/A * access the "java.home" system property gives potentially malevolent
0N/A * code sensitive information about the system environment (the Java
0N/A * installation directory). Also, granting permission to access
0N/A * the "user.name" and "user.home" system properties gives potentially
0N/A * malevolent code sensitive information about the user environment
0N/A * (the user's account name and home directory).
0N/A *
0N/A * @see java.security.BasicPermission
0N/A * @see java.security.Permission
0N/A * @see java.security.Permissions
0N/A * @see java.security.PermissionCollection
0N/A * @see java.lang.SecurityManager
0N/A *
0N/A *
0N/A * @author Roland Schemers
0N/A * @since 1.2
0N/A *
0N/A * @serial exclude
0N/A */
0N/A
0N/Apublic final class PropertyPermission extends BasicPermission {
0N/A
0N/A /**
0N/A * Read action.
0N/A */
0N/A private final static int READ = 0x1;
0N/A
0N/A /**
0N/A * Write action.
0N/A */
0N/A private final static int WRITE = 0x2;
0N/A /**
0N/A * All actions (read,write);
0N/A */
0N/A private final static int ALL = READ|WRITE;
0N/A /**
0N/A * No actions.
0N/A */
0N/A private final static int NONE = 0x0;
0N/A
0N/A /**
0N/A * The actions mask.
0N/A *
0N/A */
0N/A private transient int mask;
0N/A
0N/A /**
0N/A * The actions string.
0N/A *
0N/A * @serial
0N/A */
0N/A private String actions; // Left null as long as possible, then
0N/A // created and re-used in the getAction function.
0N/A
0N/A /**
0N/A * initialize a PropertyPermission object. Common to all constructors.
0N/A * Also called during de-serialization.
0N/A *
0N/A * @param mask the actions mask to use.
0N/A *
0N/A */
0N/A
0N/A private void init(int mask)
0N/A {
0N/A
0N/A if ((mask & ALL) != mask)
0N/A throw new IllegalArgumentException("invalid actions mask");
0N/A
0N/A if (mask == NONE)
0N/A throw new IllegalArgumentException("invalid actions mask");
0N/A
0N/A if (getName() == null)
0N/A throw new NullPointerException("name can't be null");
0N/A
0N/A this.mask = mask;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new PropertyPermission object with the specified name.
0N/A * The name is the name of the system property, and
0N/A * <i>actions</i> contains a comma-separated list of the
0N/A * desired actions granted on the property. Possible actions are
0N/A * "read" and "write".
0N/A *
0N/A * @param name the name of the PropertyPermission.
0N/A * @param actions the actions string.
0N/A *
0N/A * @throws NullPointerException if <code>name</code> is <code>null</code>.
0N/A * @throws IllegalArgumentException if <code>name</code> is empty or if
0N/A * <code>actions</code> is invalid.
0N/A */
0N/A
0N/A public PropertyPermission(String name, String actions)
0N/A {
0N/A super(name,actions);
0N/A init(getMask(actions));
0N/A }
0N/A
0N/A /**
0N/A * Checks if this PropertyPermission object "implies" the specified
0N/A * permission.
0N/A * <P>
0N/A * More specifically, this method returns true if:<p>
0N/A * <ul>
0N/A * <li> <i>p</i> is an instanceof PropertyPermission,<p>
0N/A * <li> <i>p</i>'s actions are a subset of this
0N/A * object's actions, and <p>
0N/A * <li> <i>p</i>'s name is implied by this object's
0N/A * name. For example, "java.*" implies "java.home".
0N/A * </ul>
0N/A * @param p the permission to check against.
0N/A *
0N/A * @return true if the specified permission is implied by this object,
0N/A * false if not.
0N/A */
0N/A public boolean implies(Permission p) {
0N/A if (!(p instanceof PropertyPermission))
0N/A return false;
0N/A
0N/A PropertyPermission that = (PropertyPermission) p;
0N/A
0N/A // we get the effective mask. i.e., the "and" of this and that.
0N/A // They must be equal to that.mask for implies to return true.
0N/A
0N/A return ((this.mask & that.mask) == that.mask) && super.implies(that);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Checks two PropertyPermission objects for equality. Checks that <i>obj</i> is
0N/A * a PropertyPermission, and has the same name and actions as this object.
0N/A * <P>
0N/A * @param obj the object we are testing for equality with this object.
0N/A * @return true if obj is a PropertyPermission, and has the same name and
0N/A * actions as this PropertyPermission object.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj == this)
0N/A return true;
0N/A
0N/A if (! (obj instanceof PropertyPermission))
0N/A return false;
0N/A
0N/A PropertyPermission that = (PropertyPermission) obj;
0N/A
0N/A return (this.mask == that.mask) &&
0N/A (this.getName().equals(that.getName()));
0N/A }
0N/A
0N/A /**
0N/A * Returns the hash code value for this object.
0N/A * The hash code used is the hash code of this permissions name, that is,
0N/A * <code>getName().hashCode()</code>, where <code>getName</code> is
0N/A * from the Permission superclass.
0N/A *
0N/A * @return a hash code value for this object.
0N/A */
0N/A
0N/A public int hashCode() {
0N/A return this.getName().hashCode();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Converts an actions String to an actions mask.
0N/A *
0N/A * @param action the action string.
0N/A * @return the actions mask.
0N/A */
0N/A private static int getMask(String actions) {
0N/A
0N/A int mask = NONE;
0N/A
0N/A if (actions == null) {
0N/A return mask;
0N/A }
0N/A
0N/A // Check against use of constants (used heavily within the JDK)
0N/A if (actions == SecurityConstants.PROPERTY_READ_ACTION) {
0N/A return READ;
0N/A } if (actions == SecurityConstants.PROPERTY_WRITE_ACTION) {
0N/A return WRITE;
0N/A } else if (actions == SecurityConstants.PROPERTY_RW_ACTION) {
0N/A return READ|WRITE;
0N/A }
0N/A
0N/A char[] a = actions.toCharArray();
0N/A
0N/A int i = a.length - 1;
0N/A if (i < 0)
0N/A return mask;
0N/A
0N/A while (i != -1) {
0N/A char c;
0N/A
0N/A // skip whitespace
0N/A while ((i!=-1) && ((c = a[i]) == ' ' ||
0N/A c == '\r' ||
0N/A c == '\n' ||
0N/A c == '\f' ||
0N/A c == '\t'))
0N/A i--;
0N/A
0N/A // check for the known strings
0N/A int matchlen;
0N/A
0N/A if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') &&
0N/A (a[i-2] == 'e' || a[i-2] == 'E') &&
0N/A (a[i-1] == 'a' || a[i-1] == 'A') &&
0N/A (a[i] == 'd' || a[i] == 'D'))
0N/A {
0N/A matchlen = 4;
0N/A mask |= READ;
0N/A
0N/A } else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') &&
0N/A (a[i-3] == 'r' || a[i-3] == 'R') &&
0N/A (a[i-2] == 'i' || a[i-2] == 'I') &&
0N/A (a[i-1] == 't' || a[i-1] == 'T') &&
0N/A (a[i] == 'e' || a[i] == 'E'))
0N/A {
0N/A matchlen = 5;
0N/A mask |= WRITE;
0N/A
0N/A } else {
0N/A // parse error
0N/A throw new IllegalArgumentException(
0N/A "invalid permission: " + actions);
0N/A }
0N/A
0N/A // make sure we didn't just match the tail of a word
0N/A // like "ackbarfaccept". Also, skip to the comma.
0N/A boolean seencomma = false;
0N/A while (i >= matchlen && !seencomma) {
0N/A switch(a[i-matchlen]) {
0N/A case ',':
0N/A seencomma = true;
0N/A /*FALLTHROUGH*/
0N/A case ' ': case '\r': case '\n':
0N/A case '\f': case '\t':
0N/A break;
0N/A default:
0N/A throw new IllegalArgumentException(
0N/A "invalid permission: " + actions);
0N/A }
0N/A i--;
0N/A }
0N/A
0N/A // point i at the location of the comma minus one (or -1).
0N/A i -= matchlen;
0N/A }
0N/A
0N/A return mask;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Return the canonical string representation of the actions.
0N/A * Always returns present actions in the following order:
0N/A * read, write.
0N/A *
0N/A * @return the canonical string representation of the actions.
0N/A */
0N/A static String getActions(int mask)
0N/A {
0N/A StringBuilder sb = new StringBuilder();
0N/A boolean comma = false;
0N/A
0N/A if ((mask & READ) == READ) {
0N/A comma = true;
0N/A sb.append("read");
0N/A }
0N/A
0N/A if ((mask & WRITE) == WRITE) {
0N/A if (comma) sb.append(',');
0N/A else comma = true;
0N/A sb.append("write");
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns the "canonical string representation" of the actions.
0N/A * That is, this method always returns present actions in the following order:
0N/A * read, write. For example, if this PropertyPermission object
0N/A * allows both write and read actions, a call to <code>getActions</code>
0N/A * will return the string "read,write".
0N/A *
0N/A * @return the canonical string representation of the actions.
0N/A */
0N/A public String getActions()
0N/A {
0N/A if (actions == null)
0N/A actions = getActions(this.mask);
0N/A
0N/A return actions;
0N/A }
0N/A
0N/A /**
0N/A * Return the current action mask.
0N/A * Used by the PropertyPermissionCollection
0N/A *
0N/A * @return the actions mask.
0N/A */
0N/A
0N/A int getMask() {
0N/A return mask;
0N/A }
0N/A
0N/A /**
0N/A * Returns a new PermissionCollection object for storing
0N/A * PropertyPermission objects.
0N/A * <p>
0N/A *
0N/A * @return a new PermissionCollection object suitable for storing
0N/A * PropertyPermissions.
0N/A */
0N/A
0N/A public PermissionCollection newPermissionCollection() {
0N/A return new PropertyPermissionCollection();
0N/A }
0N/A
0N/A
0N/A private static final long serialVersionUID = 885438825399942851L;
0N/A
0N/A /**
0N/A * WriteObject is called to save the state of the PropertyPermission
0N/A * to a stream. The actions are serialized, and the superclass
0N/A * takes care of the name.
0N/A */
0N/A private synchronized void writeObject(java.io.ObjectOutputStream s)
0N/A throws IOException
0N/A {
0N/A // Write out the actions. The superclass takes care of the name
0N/A // call getActions to make sure actions field is initialized
0N/A if (actions == null)
0N/A getActions();
0N/A s.defaultWriteObject();
0N/A }
0N/A
0N/A /**
0N/A * readObject is called to restore the state of the PropertyPermission from
0N/A * a stream.
0N/A */
0N/A private synchronized void readObject(java.io.ObjectInputStream s)
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A // Read in the action, then initialize the rest
0N/A s.defaultReadObject();
0N/A init(getMask(actions));
0N/A }
0N/A}
0N/A
0N/A/**
0N/A * A PropertyPermissionCollection stores a set of PropertyPermission
0N/A * permissions.
0N/A *
0N/A * @see java.security.Permission
0N/A * @see java.security.Permissions
0N/A * @see java.security.PermissionCollection
0N/A *
0N/A *
0N/A * @author Roland Schemers
0N/A *
0N/A * @serial include
0N/A */
0N/Afinal class PropertyPermissionCollection extends PermissionCollection
0N/Aimplements Serializable
0N/A{
0N/A
0N/A /**
0N/A * Key is property name; value is PropertyPermission.
0N/A * Not serialized; see serialization section at end of class.
0N/A */
0N/A private transient Map perms;
0N/A
0N/A /**
0N/A * Boolean saying if "*" is in the collection.
0N/A *
0N/A * @see #serialPersistentFields
0N/A */
0N/A // No sync access; OK for this to be stale.
0N/A private boolean all_allowed;
0N/A
0N/A /**
0N/A * Create an empty PropertyPermissions object.
0N/A *
0N/A */
0N/A
0N/A public PropertyPermissionCollection() {
0N/A perms = new HashMap(32); // Capacity for default policy
0N/A all_allowed = false;
0N/A }
0N/A
0N/A /**
0N/A * Adds a permission to the PropertyPermissions. The key for the hash is
0N/A * the name.
0N/A *
0N/A * @param permission the Permission object to add.
0N/A *
0N/A * @exception IllegalArgumentException - if the permission is not a
0N/A * PropertyPermission
0N/A *
0N/A * @exception SecurityException - if this PropertyPermissionCollection
0N/A * object has been marked readonly
0N/A */
0N/A
0N/A public void add(Permission permission)
0N/A {
0N/A if (! (permission instanceof PropertyPermission))
0N/A throw new IllegalArgumentException("invalid permission: "+
0N/A permission);
0N/A if (isReadOnly())
0N/A throw new SecurityException(
0N/A "attempt to add a Permission to a readonly PermissionCollection");
0N/A
0N/A PropertyPermission pp = (PropertyPermission) permission;
0N/A String propName = pp.getName();
0N/A
0N/A synchronized (this) {
0N/A PropertyPermission existing = (PropertyPermission) perms.get(propName);
0N/A
0N/A if (existing != null) {
0N/A int oldMask = existing.getMask();
0N/A int newMask = pp.getMask();
0N/A if (oldMask != newMask) {
0N/A int effective = oldMask | newMask;
0N/A String actions = PropertyPermission.getActions(effective);
0N/A perms.put(propName, new PropertyPermission(propName, actions));
0N/A }
0N/A } else {
0N/A perms.put(propName, permission);
0N/A }
0N/A }
0N/A
0N/A if (!all_allowed) {
0N/A if (propName.equals("*"))
0N/A all_allowed = true;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Check and see if this set of permissions implies the permissions
0N/A * expressed in "permission".
0N/A *
0N/A * @param p the Permission object to compare
0N/A *
0N/A * @return true if "permission" is a proper subset of a permission in
0N/A * the set, false if not.
0N/A */
0N/A
0N/A public boolean implies(Permission permission)
0N/A {
0N/A if (! (permission instanceof PropertyPermission))
0N/A return false;
0N/A
0N/A PropertyPermission pp = (PropertyPermission) permission;
0N/A PropertyPermission x;
0N/A
0N/A int desired = pp.getMask();
0N/A int effective = 0;
0N/A
0N/A // short circuit if the "*" Permission was added
0N/A if (all_allowed) {
0N/A synchronized (this) {
0N/A x = (PropertyPermission) perms.get("*");
0N/A }
0N/A if (x != null) {
0N/A effective |= x.getMask();
0N/A if ((effective & desired) == desired)
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A // strategy:
0N/A // Check for full match first. Then work our way up the
0N/A // name looking for matches on a.b.*
0N/A
0N/A String name = pp.getName();
0N/A //System.out.println("check "+name);
0N/A
0N/A synchronized (this) {
0N/A x = (PropertyPermission) perms.get(name);
0N/A }
0N/A
0N/A if (x != null) {
0N/A // we have a direct hit!
0N/A effective |= x.getMask();
0N/A if ((effective & desired) == desired)
0N/A return true;
0N/A }
0N/A
0N/A // work our way up the tree...
0N/A int last, offset;
0N/A
0N/A offset = name.length()-1;
0N/A
0N/A while ((last = name.lastIndexOf(".", offset)) != -1) {
0N/A
0N/A name = name.substring(0, last+1) + "*";
0N/A //System.out.println("check "+name);
0N/A synchronized (this) {
0N/A x = (PropertyPermission) perms.get(name);
0N/A }
0N/A
0N/A if (x != null) {
0N/A effective |= x.getMask();
0N/A if ((effective & desired) == desired)
0N/A return true;
0N/A }
0N/A offset = last -1;
0N/A }
0N/A
0N/A // we don't have to check for "*" as it was already checked
0N/A // at the top (all_allowed), so we just return false
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns an enumeration of all the PropertyPermission objects in the
0N/A * container.
0N/A *
0N/A * @return an enumeration of all the PropertyPermission objects.
0N/A */
0N/A
0N/A public Enumeration elements() {
0N/A // Convert Iterator of Map values into an Enumeration
0N/A synchronized (this) {
0N/A return Collections.enumeration(perms.values());
0N/A }
0N/A }
0N/A
0N/A private static final long serialVersionUID = 7015263904581634791L;
0N/A
0N/A // Need to maintain serialization interoperability with earlier releases,
0N/A // which had the serializable field:
0N/A //
0N/A // Table of permissions.
0N/A //
0N/A // @serial
0N/A //
0N/A // private Hashtable permissions;
0N/A /**
0N/A * @serialField permissions java.util.Hashtable
0N/A * A table of the PropertyPermissions.
0N/A * @serialField all_allowed boolean
0N/A * boolean saying if "*" is in the collection.
0N/A */
0N/A private static final ObjectStreamField[] serialPersistentFields = {
0N/A new ObjectStreamField("permissions", Hashtable.class),
0N/A new ObjectStreamField("all_allowed", Boolean.TYPE),
0N/A };
0N/A
0N/A /**
0N/A * @serialData Default fields.
0N/A */
0N/A /*
0N/A * Writes the contents of the perms field out as a Hashtable for
0N/A * serialization compatibility with earlier releases. all_allowed
0N/A * unchanged.
0N/A */
0N/A private void writeObject(ObjectOutputStream out) throws IOException {
0N/A // Don't call out.defaultWriteObject()
0N/A
0N/A // Copy perms into a Hashtable
0N/A Hashtable permissions = new Hashtable(perms.size()*2);
0N/A synchronized (this) {
0N/A permissions.putAll(perms);
0N/A }
0N/A
0N/A // Write out serializable fields
0N/A ObjectOutputStream.PutField pfields = out.putFields();
0N/A pfields.put("all_allowed", all_allowed);
0N/A pfields.put("permissions", permissions);
0N/A out.writeFields();
0N/A }
0N/A
0N/A /*
0N/A * Reads in a Hashtable of PropertyPermissions and saves them in the
0N/A * perms field. Reads in all_allowed.
0N/A */
0N/A private void readObject(ObjectInputStream in) throws IOException,
0N/A ClassNotFoundException {
0N/A // Don't call defaultReadObject()
0N/A
0N/A // Read in serialized fields
0N/A ObjectInputStream.GetField gfields = in.readFields();
0N/A
0N/A // Get all_allowed
0N/A all_allowed = gfields.get("all_allowed", false);
0N/A
0N/A // Get permissions
0N/A Hashtable permissions = (Hashtable)gfields.get("permissions", null);
0N/A perms = new HashMap(permissions.size()*2);
0N/A perms.putAll(permissions);
0N/A }
0N/A}