1 /*
2 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.security;
27
28 import java.security.*;
29 import java.util.Enumeration;
30 import java.util.Hashtable;
31 import java.util.StringTokenizer;
32 import sun.security.util.SecurityConstants;
33
34 /**
35 * The AllPermission is a permission that implies all other permissions.
36 * <p>
37 * <b>Note:</b> Granting AllPermission should be done with extreme care,
38 * as it implies all other permissions. Thus, it grants code the ability
39 * to run with security
40 * disabled. Extreme caution should be taken before granting such
41 * a permission to code. This permission should be used only during testing,
42 * or in extremely rare cases where an application or applet is
43 * completely trusted and adding the necessary permissions to the policy
44 * is prohibitively cumbersome.
45 *
46 * @see java.security.Permission
47 * @see java.security.AccessController
48 * @see java.security.Permissions
49 * @see java.security.PermissionCollection
50 * @see java.lang.SecurityManager
51 *
52 *
53 * @author Roland Schemers
54 * @since 1.2
55 *
56 * @serial exclude
57 */
58
59 public final class AllPermission extends Permission {
60
61 private static final long serialVersionUID = -2916474571451318075L;
62
63 /**
64 * Creates a new AllPermission object.
65 */
66 public AllPermission() {
67 super("<all permissions>");
68 }
69
70
71 /**
72 * Creates a new AllPermission object. This
73 * constructor exists for use by the {@code Policy} object
74 * to instantiate new Permission objects.
75 *
76 * @param name ignored
77 * @param actions ignored.
78 */
79 public AllPermission(String name, String actions) {
80 this();
81 }
82
83 /**
84 * Checks if the specified permission is "implied" by
85 * this object. This method always returns true.
86 *
87 * @param p the permission to check against.
88 *
89 * @return return
90 */
91 public boolean implies(Permission p) {
92 return true;
93 }
94
95 /**
96 * Checks two AllPermission objects for equality. Two AllPermission
97 * objects are always equal.
98 *
99 * @param obj the object we are testing for equality with this object.
100 * @return true if {@code obj} is an AllPermission, false otherwise.
101 */
102 public boolean equals(Object obj) {
103 return (obj instanceof AllPermission);
104 }
105
106 /**
107 * Returns the hash code value for this object.
108 *
109 * @return a hash code value for this object.
110 */
111
112 public int hashCode() {
113 return 1;
114 }
115
116 /**
117 * Returns the canonical string representation of the actions.
118 *
119 * @return the actions.
120 */
121 public String getActions() {
122 return "<all actions>";
123 }
124
125 /**
126 * Returns a new PermissionCollection object for storing AllPermission
127 * objects.
128 *
129 * @return a new PermissionCollection object suitable for
130 * storing AllPermissions.
131 */
132 public PermissionCollection newPermissionCollection() {
133 return new AllPermissionCollection();
134 }
135
136 }
137
138 /**
139 * A AllPermissionCollection stores a collection
140 * of AllPermission permissions. AllPermission objects
141 * must be stored in a manner that allows them to be inserted in any
142 * order, but enable the implies function to evaluate the implies
143 * method in an efficient (and consistent) manner.
144 *
145 * @see java.security.Permission
146 * @see java.security.Permissions
147 *
148 *
149 * @author Roland Schemers
150 *
151 * @serial include
152 */
153
154 final class AllPermissionCollection
155 extends PermissionCollection
156 implements java.io.Serializable
157 {
158
159 // use serialVersionUID from JDK 1.2.2 for interoperability
160 private static final long serialVersionUID = -4023755556366636806L;
161
162 private boolean all_allowed; // true if any all permissions have been added
163
164 /**
165 * Create an empty AllPermissions object.
166 *
167 */
168
169 public AllPermissionCollection() {
170 all_allowed = false;
171 }
172
173 /**
174 * Adds a permission to the AllPermissions. The key for the hash is
175 * permission.path.
176 *
177 * @param permission the Permission object to add.
178 *
179 * @exception IllegalArgumentException - if the permission is not a
180 * AllPermission
181 *
182 * @exception SecurityException - if this AllPermissionCollection object
183 * has been marked readonly
184 */
185
186 public void add(Permission permission) {
187 if (! (permission instanceof AllPermission))
188 throw new IllegalArgumentException("invalid permission: "+
189 permission);
190 if (isReadOnly())
191 throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
192
193 all_allowed = true; // No sync; staleness OK
194 }
195
196 /**
197 * Check and see if this set of permissions implies the permissions
198 * expressed in "permission".
199 *
200 * @param permission the Permission object to compare
201 *
202 * @return always returns true.
203 */
204
205 public boolean implies(Permission permission) {
206 return all_allowed; // No sync; staleness OK
207 }
208
209 /**
210 * Returns an enumeration of all the AllPermission objects in the
211 * container.
212 *
213 * @return an enumeration of all the AllPermission objects.
214 */
215 public Enumeration<Permission> elements() {
216 return new Enumeration<>() {
217 private boolean hasMore = all_allowed;
218
219 public boolean hasMoreElements() {
220 return hasMore;
221 }
222
223 public Permission nextElement() {
224 hasMore = false;
225 return SecurityConstants.ALL_PERMISSION;
226 }
227 };
228 }
229 }
230