/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* This class represents access to a file or directory. A FilePermission consists
* of a pathname and a set of actions valid for that pathname.
* <P>
* Pathname is the pathname of the file or directory granted the specified
* actions. A pathname that ends in "/*" (where "/" is
* the file separator character, <code>File.separatorChar</code>) indicates
* all the files and directories contained in that directory. A pathname
* that ends with "/-" indicates (recursively) all files
* and subdirectories contained in that directory. A pathname consisting of
* the special token "<<ALL FILES>>" matches <b>any</b> file.
* <P>
* Note: A pathname consisting of a single "*" indicates all the files
* in the current directory, while a pathname consisting of a single "-"
* indicates all the files in the current directory and
* (recursively) all files and subdirectories contained in the current
* directory.
* <P>
* The actions to be granted are passed to the constructor in a string containing
* a list of one or more comma-separated keywords. The possible keywords are
* "read", "write", "execute", "delete", and "readlink". Their meaning is
* defined as follows:
* <P>
* <DL>
* <DT> read <DD> read permission
* <DT> write <DD> write permission
* <DT> execute
* <DD> execute permission. Allows <code>Runtime.exec</code> to
* be called. Corresponds to <code>SecurityManager.checkExec</code>.
* <DT> delete
* <DD> delete permission. Allows <code>File.delete</code> to
* be called. Corresponds to <code>SecurityManager.checkDelete</code>.
* <DT> readlink
* <DD> read link permission. Allows the target of a
* <a href="../nio/file/package-summary.html#links">symbolic link</a>
* to be read by invoking the {@link java.nio.file.Files#readSymbolicLink
* readSymbolicLink } method.
* </DL>
* <P>
* The actions string is converted to lowercase before processing.
* <P>
* Be careful when granting FilePermissions. Think about the implications
* of granting read and especially write access to various files and
* directories. The "<<ALL FILES>>" permission with write action is
* especially dangerous. This grants permission to write to the entire
* file system. One thing this effectively allows is replacement of the
* system binary, including the JVM runtime environment.
*
* <p>Please note: Code can always read a file from the same
* directory it's in (or a subdirectory of that directory); it does not
* need explicit permission to do so.
*
* @see java.security.Permission
* @see java.security.Permissions
* @see java.security.PermissionCollection
*
*
* @author Marianne Mueller
* @author Roland Schemers
* @since 1.2
*
* @serial exclude
*/
/**
* Execute action.
*/
/**
* Write action.
*/
/**
* Read action.
*/
/**
* Delete action.
*/
/**
* Read link action.
*/
/**
* All actions (read,write,execute,delete,readlink)
*/
/**
* No actions.
*/
// the actions mask
private transient int mask;
// does path indicate a directory? (wildcard or recursive)
private transient boolean directory;
// is it a recursive directory specification?
private transient boolean recursive;
/**
* the actions string.
*
* @serial
*/
// created and re-used in the getAction function.
// canonicalized dir path. In the case of
// directories, it is the name "/blah/*" or "/blah/-" without
// the last character (the "*" or "-").
// static Strings used by init(int mask)
/*
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("***\n");
sb.append("cpath = "+cpath+"\n");
sb.append("mask = "+mask+"\n");
sb.append("actions = "+getActions()+"\n");
sb.append("directory = "+directory+"\n");
sb.append("recursive = "+recursive+"\n");
sb.append("***\n");
return sb.toString();
}
*/
/**
* initialize a FilePermission object. Common to all constructors.
* Also called during de-serialization.
*
* @param mask the actions mask to use.
*
*/
{
throw new IllegalArgumentException("invalid actions mask");
throw new IllegalArgumentException("invalid actions mask");
throw new NullPointerException("name can't be null");
directory = true;
recursive = true;
cpath = "";
return;
}
// store only the canonical cpath if possible
try {
// call getCanonicalPath with a path with wildcard character
// replaced to avoid calling it with paths that are
// intended to match all entries in a directory
} else {
}
} catch (IOException ioe) {
return cpath;
}
}
});
if (last == RECURSIVE_CHAR &&
directory = true;
recursive = true;
directory = true;
//recursive = false;
} else {
// overkill since they are initialized to false, but
// commented out here to remind us...
//directory = false;
//recursive = false;
}
// XXX: at this point the path should be absolute. die if it isn't?
}
/**
* Creates a new FilePermission object with the specified actions.
* <i>path</i> is the pathname of a file or directory, and <i>actions</i>
* contains a comma-separated list of the desired actions granted on the
* file or directory. Possible actions are
* "read", "write", "execute", "delete", and "readlink".
*
* <p>A pathname that ends in "/*" (where "/" is
* the file separator character, <code>File.separatorChar</code>)
* indicates all the files and directories contained in that directory.
* A pathname that ends with "/-" indicates (recursively) all files and
* subdirectories contained in that directory. The special pathname
* "<<ALL FILES>>" matches any file.
*
* <p>A pathname consisting of a single "*" indicates all the files
* in the current directory, while a pathname consisting of a single "-"
* indicates all the files in the current directory and
* (recursively) all files and subdirectories contained in the current
* directory.
*
* <p>A pathname containing an empty string represents an empty path.
*
* @param actions the action string.
*
* @throws IllegalArgumentException
* If actions is <code>null</code>, empty or contains an action
* other than the specified possible actions.
*/
{
super(path);
}
/**
* Creates a new FilePermission object using an action mask.
* More efficient than the FilePermission(String, String) constructor.
* Can be used from within
* code that needs to create a FilePermission object to pass into the
* <code>implies</code> method.
*
* @param mask the action mask to use.
*/
// package private for use by the FilePermissionCollection add method
{
super(path);
}
/**
* Checks if this FilePermission object "implies" the specified permission.
* <P>
* More specifically, this method returns true if:<p>
* <ul>
* <li> <i>p</i> is an instanceof FilePermission,<p>
* <li> <i>p</i>'s actions are a proper subset of this
* object's actions, and <p>
* <li> <i>p</i>'s pathname is implied by this object's
* "/tmp/*" encompasses all files in the "/tmp" directory,
* including the one named "foo".
* </ul>
*
* @param p the permission to check against.
*
* @return <code>true</code> if the specified permission is not
* <code>null</code> and is implied by this object,
* <code>false</code> otherwise.
*/
if (!(p instanceof FilePermission))
return false;
// we get the effective mask. i.e., the "and" of this and that.
// They must be equal to that.mask for implies to return true.
}
/**
* Checks if the Permission's actions are a proper subset of the
* this object's actions. Returns the effective mask iff the
* this FilePermission's path also implies that FilePermission's path.
*
* @param that the FilePermission to check against.
* @param exact return immediately if the masks are not equal
* @return the effective mask
*/
if (this.directory) {
if (this.recursive) {
// make sure that.path is longer then path so
// something like /foo/- does not imply /foo
} else {
}
} else {
// if the permission passed in is a directory
// specification, make sure that a non-recursive
// permission (i.e., this object) can't imply a recursive
// permission.
return false;
else
} else {
if (last == -1)
return false;
else {
// this.cpath.equals(that.cpath.substring(0, last+1));
// Use regionMatches to avoid creating new string
}
}
}
// if this is NOT recursive/wildcarded,
// do not let it imply a recursive/wildcarded permission
return false;
} else {
}
}
/**
* Checks two FilePermission objects for equality. Checks that <i>obj</i> is
* a FilePermission, and has the same pathname and actions as this object.
* <P>
* @param obj the object we are testing for equality with this object.
* @return <code>true</code> if obj is a FilePermission, and has the same
* pathname and actions as this FilePermission object,
* <code>false</code> otherwise.
*/
if (obj == this)
return true;
if (! (obj instanceof FilePermission))
return false;
}
/**
* Returns the hash code value for this object.
*
* @return a hash code value for this object.
*/
public int hashCode() {
return 0;
}
/**
* Converts an actions String to an actions mask.
*
* @param action the action string.
* @return the actions mask.
*/
// Null action valid?
return mask;
}
// Check against use of constants (used heavily within the JDK)
return READ;
return WRITE;
return EXECUTE;
return DELETE;
return READLINK;
}
char[] a = actions.toCharArray();
int i = a.length - 1;
if (i < 0)
return mask;
while (i != -1) {
char c;
// skip whitespace
while ((i!=-1) && ((c = a[i]) == ' ' ||
c == '\r' ||
c == '\n' ||
c == '\f' ||
c == '\t'))
i--;
// check for the known strings
int matchlen;
if (i >= 3 && (a[i-3] == 'r' || a[i-3] == 'R') &&
(a[i-2] == 'e' || a[i-2] == 'E') &&
(a[i-1] == 'a' || a[i-1] == 'A') &&
(a[i] == 'd' || a[i] == 'D'))
{
matchlen = 4;
} else if (i >= 4 && (a[i-4] == 'w' || a[i-4] == 'W') &&
(a[i-3] == 'r' || a[i-3] == 'R') &&
(a[i-2] == 'i' || a[i-2] == 'I') &&
(a[i-1] == 't' || a[i-1] == 'T') &&
(a[i] == 'e' || a[i] == 'E'))
{
matchlen = 5;
} else if (i >= 6 && (a[i-6] == 'e' || a[i-6] == 'E') &&
(a[i-5] == 'x' || a[i-5] == 'X') &&
(a[i-4] == 'e' || a[i-4] == 'E') &&
(a[i-3] == 'c' || a[i-3] == 'C') &&
(a[i-2] == 'u' || a[i-2] == 'U') &&
(a[i-1] == 't' || a[i-1] == 'T') &&
(a[i] == 'e' || a[i] == 'E'))
{
matchlen = 7;
} else if (i >= 5 && (a[i-5] == 'd' || a[i-5] == 'D') &&
(a[i-4] == 'e' || a[i-4] == 'E') &&
(a[i-3] == 'l' || a[i-3] == 'L') &&
(a[i-2] == 'e' || a[i-2] == 'E') &&
(a[i-1] == 't' || a[i-1] == 'T') &&
(a[i] == 'e' || a[i] == 'E'))
{
matchlen = 6;
} else if (i >= 7 && (a[i-7] == 'r' || a[i-7] == 'R') &&
(a[i-6] == 'e' || a[i-6] == 'E') &&
(a[i-5] == 'a' || a[i-5] == 'A') &&
(a[i-4] == 'd' || a[i-4] == 'D') &&
(a[i-3] == 'l' || a[i-3] == 'L') &&
(a[i-2] == 'i' || a[i-2] == 'I') &&
(a[i-1] == 'n' || a[i-1] == 'N') &&
(a[i] == 'k' || a[i] == 'K'))
{
matchlen = 8;
} else {
// parse error
throw new IllegalArgumentException(
"invalid permission: " + actions);
}
// make sure we didn't just match the tail of a word
// like "ackbarfaccept". Also, skip to the comma.
boolean seencomma = false;
switch(a[i-matchlen]) {
case ',':
seencomma = true;
/*FALLTHROUGH*/
case ' ': case '\r': case '\n':
case '\f': case '\t':
break;
default:
throw new IllegalArgumentException(
"invalid permission: " + actions);
}
i--;
}
// point i at the location of the comma minus one (or -1).
i -= matchlen;
}
return mask;
}
/**
* Return the current action mask. Used by the FilePermissionCollection.
*
* @return the actions mask.
*/
int getMask() {
return mask;
}
/**
* Return the canonical string representation of the actions.
* Always returns present actions in the following order:
* read, write, execute, delete, readlink.
*
* @return the canonical string representation of the actions.
*/
{
boolean comma = false;
comma = true;
}
else comma = true;
}
else comma = true;
}
else comma = true;
}
else comma = true;
}
}
/**
* Returns the "canonical string representation" of the actions.
* That is, this method always returns present actions in the following order:
* read, write, execute, delete, readlink. For example, if this FilePermission
* object allows both write and read actions, a call to <code>getActions</code>
* will return the string "read,write".
*
* @return the canonical string representation of the actions.
*/
{
return actions;
}
/**
* Returns a new PermissionCollection object for storing FilePermission
* objects.
* <p>
* FilePermission objects must be stored in a manner that allows them
* to be inserted into the collection in any order, but that also enables the
* PermissionCollection <code>implies</code>
* method to be implemented in an efficient (and consistent) manner.
*
* <p>For example, if you have two FilePermissions:
* <OL>
* <LI> <code>"/tmp/-", "read"</code>
* </OL>
*
* <p>and you are calling the <code>implies</code> method with the FilePermission:
*
* <pre>
* </pre>
*
* then the <code>implies</code> function must
* permissions, so the effective permission is "read,write",
* and <code>implies</code> returns true. The "implies" semantics for
* FilePermissions are handled properly by the PermissionCollection object
* returned by this <code>newPermissionCollection</code> method.
*
* @return a new PermissionCollection object suitable for storing
* FilePermissions.
*/
return new FilePermissionCollection();
}
/**
* WriteObject is called to save the state of the FilePermission
* to a stream. The actions are serialized, and the superclass
* takes care of the name.
*/
throws IOException
{
// Write out the actions. The superclass takes care of the name
// call getActions to make sure actions field is initialized
getActions();
s.defaultWriteObject();
}
/**
* readObject is called to restore the state of the FilePermission from
* a stream.
*/
throws IOException, ClassNotFoundException
{
// Read in the actions, then restore everything else by calling init.
s.defaultReadObject();
}
}
/**
* A FilePermissionCollection stores a set of FilePermission permissions.
* FilePermission objects
* must be stored in a manner that allows them to be inserted in any
* order, but enable the implies function to evaluate the implies
* method.
* For example, if you have two FilePermissions:
* <OL>
* <LI> "/tmp/-", "read"
* </OL>
* And you are calling the implies function with the FilePermission:
* permissions, so the effective permission is "read,write".
*
* @see java.security.Permission
* @see java.security.Permissions
* @see java.security.PermissionCollection
*
*
* @author Marianne Mueller
* @author Roland Schemers
*
* @serial include
*
*/
implements Serializable {
// Not serialized; see serialization section at end of class
/**
* Create an empty FilePermissions object.
*
*/
public FilePermissionCollection() {
}
/**
* Adds a permission to the FilePermissions. The key for the hash is
* permission.path.
*
* @param permission the Permission object to add.
*
* @exception IllegalArgumentException - if the permission is not a
* FilePermission
*
* @exception SecurityException - if this FilePermissionCollection object
* has been marked readonly
*/
{
if (! (permission instanceof FilePermission))
throw new IllegalArgumentException("invalid permission: "+
if (isReadOnly())
throw new SecurityException(
"attempt to add a Permission to a readonly PermissionCollection");
synchronized (this) {
}
}
/**
* Check and see if this set of permissions implies the permissions
* expressed in "permission".
*
* @param p the Permission object to compare
*
* @return true if "permission" is a proper subset of a permission in
* the set, false if not.
*/
{
if (! (permission instanceof FilePermission))
return false;
int effective = 0;
synchronized (this) {
for (int i = 0; i < len; i++) {
return true;
}
}
}
return false;
}
/**
* Returns an enumeration of all the FilePermission objects in the
* container.
*
* @return an enumeration of all the FilePermission objects.
*/
// Convert Iterator into Enumeration
synchronized (this) {
}
}
// Need to maintain serialization interoperability with earlier releases,
// which had the serializable field:
// private Vector permissions;
/**
* @serialField permissions java.util.Vector
* A list of FilePermission objects.
*/
};
/**
* @serialData "permissions" field (a Vector containing the FilePermissions).
*/
/*
* Writes the contents of the perms field out as a Vector for
* serialization compatibility with earlier releases.
*/
// Don't call out.defaultWriteObject()
// Write out Vector
synchronized (this) {
}
out.writeFields();
}
/*
* Reads in a Vector of FilePermissions and saves them in the perms field.
*/
@SuppressWarnings("unchecked")
// Don't call defaultReadObject()
// Read in serialized fields
// Get the one we want
}
}