58678
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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 sun.security.util;
|
|
27 |
|
|
28 |
import java.io.File;
|
|
29 |
import java.io.FilePermission;
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.net.URL;
|
|
32 |
import java.security.CodeSource;
|
|
33 |
import java.security.Permission;
|
|
34 |
import java.security.PermissionCollection;
|
|
35 |
import java.util.Enumeration;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* This {@code PermissionCollection} implementation delegates to another
|
|
39 |
* {@code PermissionCollection}, taking care to lazily add the permission needed
|
|
40 |
* to read from the given {@code CodeSource} at first use, i.e., when either of
|
|
41 |
* {@link #elements}, {@link #implies} or {@link #toString} is called, or when
|
|
42 |
* the collection is serialized.
|
|
43 |
*/
|
|
44 |
public final class LazyCodeSourcePermissionCollection
|
|
45 |
extends PermissionCollection
|
|
46 |
{
|
|
47 |
@java.io.Serial
|
|
48 |
private static final long serialVersionUID = -6727011328946861783L;
|
|
49 |
private final PermissionCollection perms;
|
|
50 |
private final CodeSource cs;
|
|
51 |
private volatile boolean permissionAdded;
|
|
52 |
|
|
53 |
public LazyCodeSourcePermissionCollection(PermissionCollection perms,
|
|
54 |
CodeSource cs) {
|
|
55 |
this.perms = perms;
|
|
56 |
this.cs = cs;
|
|
57 |
}
|
|
58 |
|
|
59 |
private void ensureAdded() {
|
|
60 |
if (!permissionAdded) {
|
|
61 |
synchronized(perms) {
|
|
62 |
if (permissionAdded)
|
|
63 |
return;
|
|
64 |
|
|
65 |
// open connection to determine the permission needed
|
|
66 |
URL location = cs.getLocation();
|
|
67 |
if (location != null) {
|
|
68 |
try {
|
|
69 |
Permission p = location.openConnection().getPermission();
|
|
70 |
if (p != null) {
|
|
71 |
// for directories then need recursive access
|
|
72 |
if (p instanceof FilePermission) {
|
|
73 |
String path = p.getName();
|
|
74 |
if (path.endsWith(File.separator)) {
|
|
75 |
path += "-";
|
|
76 |
p = new FilePermission(path,
|
|
77 |
SecurityConstants.FILE_READ_ACTION);
|
|
78 |
}
|
|
79 |
}
|
|
80 |
perms.add(p);
|
|
81 |
}
|
|
82 |
} catch (IOException ioe) {
|
|
83 |
}
|
|
84 |
}
|
|
85 |
if (isReadOnly()) {
|
|
86 |
perms.setReadOnly();
|
|
87 |
}
|
|
88 |
permissionAdded = true;
|
|
89 |
}
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
@Override
|
|
94 |
public void add(Permission permission) {
|
|
95 |
if (isReadOnly())
|
|
96 |
throw new SecurityException(
|
|
97 |
"attempt to add a Permission to a readonly PermissionCollection");
|
|
98 |
perms.add(permission);
|
|
99 |
}
|
|
100 |
|
|
101 |
@Override
|
|
102 |
public boolean implies(Permission permission) {
|
|
103 |
ensureAdded();
|
|
104 |
return perms.implies(permission);
|
|
105 |
}
|
|
106 |
|
|
107 |
@Override
|
|
108 |
public Enumeration<Permission> elements() {
|
|
109 |
ensureAdded();
|
|
110 |
return perms.elements();
|
|
111 |
}
|
|
112 |
|
|
113 |
@Override
|
|
114 |
public String toString() {
|
|
115 |
ensureAdded();
|
|
116 |
return perms.toString();
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* On serialization, initialize and replace with the underlying
|
|
121 |
* permissions. This removes the laziness on deserialization.
|
|
122 |
*/
|
|
123 |
@java.io.Serial
|
|
124 |
private Object writeReplace() {
|
|
125 |
ensureAdded();
|
|
126 |
return perms;
|
|
127 |
}
|
|
128 |
}
|