001: /*
002: * @(#)PackagePermission.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.security;
010:
011: import java.io.IOException;
012: import java.io.ObjectInputStream;
013: import java.io.Serializable;
014: import java.security.BasicPermission;
015: import java.security.Permission;
016: import java.security.PermissionCollection;
017: import java.util.Enumeration;
018: import java.util.Hashtable;
019:
020: /**
021: * This class represents access to a Package in Pnuts. A PackagePermission consists
022: * of a package name and a set of actions.
023: *
024: * @see <a href="../../../doc/permission.html">Pnuts User's Guide</a>
025: * @version 1.1
026: */
027: public final class PackagePermission extends BasicPermission {
028:
029: private static final int WRITE = 1;
030: private static final int ADD = 2;
031: private static final int REMOVE = 4;
032:
033: private transient int mask = 0;
034: private transient String actions;
035: private transient boolean wildcard = false;
036: private transient String path;
037:
038: public PackagePermission(String name) {
039: super (name);
040: }
041:
042: public PackagePermission(String name, String actions) {
043: super (name, actions);
044: this .actions = actions;
045: init(actions);
046: }
047:
048: void init(String actions) {
049: if (actions.indexOf("write") >= 0) {
050: mask |= WRITE;
051: }
052: if (actions.indexOf("add") >= 0) {
053: mask |= ADD;
054: }
055: if (actions.indexOf("remove") >= 0) {
056: mask |= REMOVE;
057: }
058: String name = getName();
059: if (name.endsWith("::*")) {
060: wildcard = true;
061: path = name.substring(0, name.length() - 2);
062: } else if (name.endsWith(".*")) {
063: wildcard = true;
064: path = name.substring(0, name.length() - 1);
065: } else if (name.equals("*")) {
066: wildcard = true;
067: path = "";
068: path = name.substring(0, name.length() - 2);
069: } else {
070: path = name;
071: }
072: }
073:
074: public String getActions() {
075: String s = "";
076: boolean first = true;
077: if ((mask & WRITE) != 0) {
078: s += "write";
079: first = false;
080: }
081: if ((mask & ADD) != 0) {
082: if (!first) {
083: s += ", ";
084: }
085: s += "add";
086: first = false;
087: }
088: if ((mask & REMOVE) != 0) {
089: if (!first) {
090: s += ", ";
091: }
092: s += "remove";
093: }
094: return s;
095: }
096:
097: public boolean implies(Permission p) {
098: if (!(p instanceof PackagePermission)) {
099: return false;
100: }
101: PackagePermission pp = (PackagePermission) p;
102: int m = pp.getMask();
103: if ((m & this .mask) != m) {
104: return false;
105: }
106:
107: if (this .wildcard) {
108: if (pp.wildcard) {
109: return pp.path.startsWith(path);
110: } else {
111: return (pp.path.length() > this .path.length())
112: && pp.path.startsWith(this .path);
113: }
114: } else {
115: if (pp.wildcard) {
116: return false;
117: } else {
118: return this .path.equals(pp.path);
119: }
120: }
121: }
122:
123: int getMask() {
124: return mask;
125: }
126:
127: public PermissionCollection newPermissionCollection() {
128: return new PackagePermissionCollection();
129: }
130:
131: private void readObject(ObjectInputStream s) throws IOException,
132: ClassNotFoundException {
133: s.defaultReadObject();
134: init(actions);
135: }
136: }
137:
138: final class PackagePermissionCollection extends PermissionCollection
139: implements Serializable {
140: private Hashtable permissions;
141: private boolean all_allowed;
142:
143: public PackagePermissionCollection() {
144: permissions = new Hashtable(10);
145: all_allowed = false;
146: }
147:
148: public void add(Permission permission) {
149: if (!(permission instanceof PackagePermission)) {
150: throw new IllegalArgumentException("invalid permission: "
151: + permission);
152: }
153: PackagePermission pp = (PackagePermission) permission;
154: permissions.put(pp.getName(), permission);
155: if (!all_allowed) {
156: if (pp.getName().equals("*")) {
157: all_allowed = true;
158: }
159: }
160: }
161:
162: public boolean implies(Permission permission) {
163: if (!(permission instanceof PackagePermission)) {
164: return false;
165: }
166:
167: PackagePermission pp = (PackagePermission) permission;
168: if (all_allowed) {
169: return true;
170: }
171: String path = pp.getName();
172: Permission x = (Permission) permissions.get(path);
173: if (x != null) {
174: return x.implies(permission);
175: }
176:
177: int offset = path.length() - 1;
178:
179: while (true) {
180: int last1 = path.lastIndexOf("::", offset);
181: int last2 = path.lastIndexOf(".", offset);
182: if (last1 == -1 && last2 == -1) {
183: break;
184: }
185: int last;
186: if (last2 > last1) {
187: last = last2;
188: } else {
189: last = last1;
190: }
191: path = path.substring(0, last + 1) + "*";
192: x = (Permission) permissions.get(path);
193: if (x != null) {
194: return x.implies(permission);
195: }
196: offset = last - 1;
197: }
198: return false;
199: }
200:
201: public Enumeration elements() {
202: return permissions.elements();
203: }
204: }
|