37 |
|
|
38 |
package java.security; |
package java.security; |
39 |
|
|
40 |
|
import java.util.Collections; |
41 |
|
import java.util.Enumeration; |
42 |
|
import java.util.LinkedHashMap; |
43 |
|
import java.util.Map; |
44 |
|
|
45 |
/** |
/** |
46 |
* <p>This is an abstract class for representing the system security policy for |
* <p>This is an abstract class for representing the system security policy for |
47 |
* a Java application environment (specifying which permissions are available |
* a Java application environment (specifying which permissions are available |
95 |
*/ |
*/ |
96 |
public abstract class Policy |
public abstract class Policy |
97 |
{ |
{ |
|
// FIXME: The class name of the Policy provider should really be sourced |
|
|
// from the "java.security" configuration file. For now, just hard-code |
|
|
// a stub implementation. |
|
98 |
static private Policy currentPolicy = null; |
static private Policy currentPolicy = null; |
99 |
static |
|
100 |
{ |
/** Map of ProtectionDomains to PermissionCollections for this instance. */ |
101 |
String pp = System.getProperty ("policy.provider"); |
private Map pd2pc = null; |
|
if (pp != null) |
|
|
try |
|
|
{ |
|
|
currentPolicy = (Policy)Class.forName(pp).newInstance(); |
|
|
} |
|
|
catch (Exception _) |
|
|
{ |
|
|
currentPolicy = null; |
|
|
} |
|
|
if (currentPolicy == null) |
|
|
currentPolicy = new gnu.java.security.provider.DefaultPolicy(); |
|
|
} |
|
102 |
|
|
103 |
/** Constructs a new <code>Policy</code> object. */ |
/** Constructs a new <code>Policy</code> object. */ |
104 |
public Policy() |
public Policy() |
125 |
if (sm != null) |
if (sm != null) |
126 |
sm.checkPermission(new SecurityPermission("getPolicy")); |
sm.checkPermission(new SecurityPermission("getPolicy")); |
127 |
|
|
128 |
return currentPolicy; |
return getCurrentPolicy(); |
129 |
} |
} |
130 |
|
|
131 |
/** |
/** |
147 |
if (sm != null) |
if (sm != null) |
148 |
sm.checkPermission(new SecurityPermission("setPolicy")); |
sm.checkPermission(new SecurityPermission("setPolicy")); |
149 |
|
|
150 |
|
setup(policy); |
151 |
currentPolicy = policy; |
currentPolicy = policy; |
152 |
} |
} |
153 |
|
|
154 |
|
private static void setup(final Policy policy) |
155 |
|
{ |
156 |
|
if (policy.pd2pc == null) |
157 |
|
policy.pd2pc = Collections.synchronizedMap(new LinkedHashMap()); |
158 |
|
|
159 |
|
ProtectionDomain pd = policy.getClass().getProtectionDomain(); |
160 |
|
if (pd.getCodeSource() != null) |
161 |
|
{ |
162 |
|
PermissionCollection pc = null; |
163 |
|
if (currentPolicy != null) |
164 |
|
pc = currentPolicy.getPermissions(pd); |
165 |
|
|
166 |
|
if (pc == null) // assume it has all |
167 |
|
{ |
168 |
|
pc = new Permissions(); |
169 |
|
pc.add(new AllPermission()); |
170 |
|
} |
171 |
|
|
172 |
|
policy.pd2pc.put(pd, pc); // add the mapping pd -> pc |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
|
* Ensures/forces loading of the configured policy provider, while bypassing |
178 |
|
* the {@link SecurityManager} checks for <code>"getPolicy"</code> security |
179 |
|
* permission. Nneeded by {@link ProtectionDomain}. |
180 |
|
*/ |
181 |
|
static Policy getCurrentPolicy() |
182 |
|
{ |
183 |
|
// FIXME: The class name of the Policy provider should really be sourced |
184 |
|
// from the "java.security" configuration file. For now, just hard-code |
185 |
|
// a stub implementation. |
186 |
|
if (currentPolicy == null) |
187 |
|
{ |
188 |
|
String pp = System.getProperty ("policy.provider"); |
189 |
|
if (pp != null) |
190 |
|
try |
191 |
|
{ |
192 |
|
currentPolicy = (Policy) Class.forName(pp).newInstance(); |
193 |
|
} |
194 |
|
catch (Exception ignored) {} |
195 |
|
|
196 |
|
if (currentPolicy == null) |
197 |
|
currentPolicy = new gnu.java.security.provider.DefaultPolicy(); |
198 |
|
} |
199 |
|
return currentPolicy; |
200 |
|
} |
201 |
|
|
202 |
|
/** |
203 |
|
* Tests if <code>currentPolicy</code> is not <code>null</code>, thus allowing |
204 |
|
* clients to not force loading of any policy provider. needed by {@link |
205 |
|
* ProtectionDomain}. |
206 |
|
*/ |
207 |
|
static boolean isLoaded() |
208 |
|
{ |
209 |
|
return currentPolicy != null; |
210 |
|
} |
211 |
|
|
212 |
/** |
/** |
213 |
* Evaluates the global policy and returns a {@link PermissionCollection} |
* Evaluates the global policy and returns a {@link PermissionCollection} |
224 |
public abstract PermissionCollection getPermissions(CodeSource codesource); |
public abstract PermissionCollection getPermissions(CodeSource codesource); |
225 |
|
|
226 |
/** |
/** |
227 |
|
* Evaluates the global policy and returns a {@link PermissionCollection} |
228 |
|
* object specifying the set of permissions allowed given the characteristics |
229 |
|
* of the protection domain. |
230 |
|
* |
231 |
|
* @param domain the {@link ProtectionDomain} associated with the caller. |
232 |
|
* @return the set of permissions allowed for the domain according to the |
233 |
|
* policy. The returned set of permissions must be a new mutable instance and |
234 |
|
* it must support heterogeneous {@link Permission} types. |
235 |
|
* @since 1.4 |
236 |
|
* @see ProtectionDomain |
237 |
|
* @see SecureClassLoader |
238 |
|
*/ |
239 |
|
public PermissionCollection getPermissions(ProtectionDomain domain) |
240 |
|
{ |
241 |
|
if (domain == null) |
242 |
|
return new Permissions(); |
243 |
|
|
244 |
|
if (pd2pc == null) |
245 |
|
setup(this); |
246 |
|
|
247 |
|
PermissionCollection result = (PermissionCollection) pd2pc.get(domain); |
248 |
|
if (result != null) |
249 |
|
{ |
250 |
|
Permissions realResult = new Permissions(); |
251 |
|
for (Enumeration e = result.elements(); e.hasMoreElements(); ) |
252 |
|
realResult.add((Permission) e.nextElement()); |
253 |
|
|
254 |
|
return realResult; |
255 |
|
} |
256 |
|
|
257 |
|
result = getPermissions(domain.getCodeSource()); |
258 |
|
if (result == null) |
259 |
|
result = new Permissions(); |
260 |
|
|
261 |
|
PermissionCollection pc = domain.getPermissions(); |
262 |
|
if (pc != null) |
263 |
|
for (Enumeration e = pc.elements(); e.hasMoreElements(); ) |
264 |
|
result.add((Permission) e.nextElement()); |
265 |
|
|
266 |
|
return result; |
267 |
|
} |
268 |
|
|
269 |
|
/** |
270 |
|
* Evaluates the global policy for the permissions granted to the {@link |
271 |
|
* ProtectionDomain} and tests whether the <code>permission</code> is granted. |
272 |
|
* |
273 |
|
* @param domain the {@link ProtectionDomain} to test. |
274 |
|
* @param permission the {@link Permission} object to be tested for |
275 |
|
* implication. |
276 |
|
* @return <code>true</code> if <code>permission</code> is a proper subset of |
277 |
|
* a permission granted to this {@link ProtectionDomain}. |
278 |
|
* @since 1.4 |
279 |
|
* @see ProtectionDomain |
280 |
|
*/ |
281 |
|
public boolean implies(ProtectionDomain domain, Permission permission) |
282 |
|
{ |
283 |
|
if (pd2pc == null) |
284 |
|
setup(this); |
285 |
|
|
286 |
|
PermissionCollection pc = (PermissionCollection) pd2pc.get(domain); |
287 |
|
if (pc != null) |
288 |
|
return pc.implies(permission); |
289 |
|
|
290 |
|
boolean result = false; |
291 |
|
pc = getPermissions(domain); |
292 |
|
if (pc != null) |
293 |
|
{ |
294 |
|
result = pc.implies(permission); |
295 |
|
pd2pc.put(domain, pc); |
296 |
|
} |
297 |
|
|
298 |
|
return result; |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
* Refreshes/reloads the policy configuration. The behavior of this method |
* Refreshes/reloads the policy configuration. The behavior of this method |
303 |
* depends on the implementation. For example, calling refresh on a file-based |
* depends on the implementation. For example, calling refresh on a file-based |
304 |
* policy will cause the file to be re-read. |
* policy will cause the file to be re-read. |