2
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 Sun Microsystems, Inc. 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. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.net;
|
|
27 |
|
|
28 |
import java.util.Enumeration;
|
|
29 |
import java.util.Vector;
|
|
30 |
import java.util.List;
|
|
31 |
import java.util.ArrayList;
|
|
32 |
import java.util.Collections;
|
|
33 |
import java.util.StringTokenizer;
|
|
34 |
import java.net.InetAddress;
|
|
35 |
import java.security.Permission;
|
|
36 |
import java.security.PermissionCollection;
|
|
37 |
import java.io.Serializable;
|
|
38 |
import java.io.ObjectStreamField;
|
|
39 |
import java.io.ObjectOutputStream;
|
|
40 |
import java.io.ObjectInputStream;
|
|
41 |
import java.io.IOException;
|
|
42 |
import sun.net.util.IPAddressUtil;
|
|
43 |
import sun.security.util.SecurityConstants;
|
|
44 |
|
|
45 |
|
|
46 |
/**
|
|
47 |
* This class represents access to a network via sockets.
|
|
48 |
* A SocketPermission consists of a
|
|
49 |
* host specification and a set of "actions" specifying ways to
|
|
50 |
* connect to that host. The host is specified as
|
|
51 |
* <pre>
|
|
52 |
* host = (hostname | IPv4address | iPv6reference) [:portrange]
|
|
53 |
* portrange = portnumber | -portnumber | portnumber-[portnumber]
|
|
54 |
* </pre>
|
|
55 |
* The host is expressed as a DNS name, as a numerical IP address,
|
|
56 |
* or as "localhost" (for the local machine).
|
|
57 |
* The wildcard "*" may be included once in a DNS name host
|
|
58 |
* specification. If it is included, it must be in the leftmost
|
|
59 |
* position, as in "*.sun.com".
|
|
60 |
* <p>
|
|
61 |
* The format of the IPv6reference should follow that specified in <a
|
|
62 |
* href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC 2732: Format
|
|
63 |
* for Literal IPv6 Addresses in URLs</i></a>:
|
|
64 |
* <pre>
|
|
65 |
* ipv6reference = "[" IPv6address "]"
|
|
66 |
*</pre>
|
|
67 |
* For example, you can construct a SocketPermission instance
|
|
68 |
* as the following:
|
|
69 |
* <pre>
|
|
70 |
* String hostAddress = inetaddress.getHostAddress();
|
|
71 |
* if (inetaddress instanceof Inet6Address) {
|
|
72 |
* sp = new SocketPermission("[" + hostAddress + "]:" + port, action);
|
|
73 |
* } else {
|
|
74 |
* sp = new SocketPermission(hostAddress + ":" + port, action);
|
|
75 |
* }
|
|
76 |
* </pre>
|
|
77 |
* or
|
|
78 |
* <pre>
|
|
79 |
* String host = url.getHost();
|
|
80 |
* sp = new SocketPermission(host + ":" + port, action);
|
|
81 |
* </pre>
|
|
82 |
* <p>
|
|
83 |
* The <A HREF="Inet6Address.html#lform">full uncompressed form</A> of
|
|
84 |
* an IPv6 literal address is also valid.
|
|
85 |
* <p>
|
|
86 |
* The port or portrange is optional. A port specification of the
|
|
87 |
* form "N-", where <i>N</i> is a port number, signifies all ports
|
|
88 |
* numbered <i>N</i> and above, while a specification of the
|
|
89 |
* form "-N" indicates all ports numbered <i>N</i> and below.
|
|
90 |
* <p>
|
|
91 |
* The possible ways to connect to the host are
|
|
92 |
* <pre>
|
|
93 |
* accept
|
|
94 |
* connect
|
|
95 |
* listen
|
|
96 |
* resolve
|
|
97 |
* </pre>
|
|
98 |
* The "listen" action is only meaningful when used with "localhost".
|
|
99 |
* The "resolve" action is implied when any of the other actions are present.
|
|
100 |
* The action "resolve" refers to host/ip name service lookups.
|
|
101 |
* <P>
|
|
102 |
* The actions string is converted to lowercase before processing.
|
|
103 |
* <p>As an example of the creation and meaning of SocketPermissions,
|
|
104 |
* note that if the following permission:
|
|
105 |
*
|
|
106 |
* <pre>
|
|
107 |
* p1 = new SocketPermission("puffin.eng.sun.com:7777", "connect,accept");
|
|
108 |
* </pre>
|
|
109 |
*
|
|
110 |
* is granted to some code, it allows that code to connect to port 7777 on
|
|
111 |
* <code>puffin.eng.sun.com</code>, and to accept connections on that port.
|
|
112 |
*
|
|
113 |
* <p>Similarly, if the following permission:
|
|
114 |
*
|
|
115 |
* <pre>
|
|
116 |
* p2 = new SocketPermission("localhost:1024-", "accept,connect,listen");
|
|
117 |
* </pre>
|
|
118 |
*
|
|
119 |
* is granted to some code, it allows that code to
|
|
120 |
* accept connections on, connect to, or listen on any port between
|
|
121 |
* 1024 and 65535 on the local host.
|
|
122 |
*
|
|
123 |
* <p>Note: Granting code permission to accept or make connections to remote
|
|
124 |
* hosts may be dangerous because malevolent code can then more easily
|
|
125 |
* transfer and share confidential data among parties who may not
|
|
126 |
* otherwise have access to the data.
|
|
127 |
*
|
|
128 |
* @see java.security.Permissions
|
|
129 |
* @see SocketPermission
|
|
130 |
*
|
|
131 |
*
|
|
132 |
* @author Marianne Mueller
|
|
133 |
* @author Roland Schemers
|
|
134 |
*
|
|
135 |
* @serial exclude
|
|
136 |
*/
|
|
137 |
|
|
138 |
public final class SocketPermission extends Permission
|
|
139 |
implements java.io.Serializable
|
|
140 |
{
|
|
141 |
private static final long serialVersionUID = -7204263841984476862L;
|
|
142 |
|
|
143 |
/**
|
|
144 |
* Connect to host:port
|
|
145 |
*/
|
|
146 |
private final static int CONNECT = 0x1;
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Listen on host:port
|
|
150 |
*/
|
|
151 |
private final static int LISTEN = 0x2;
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Accept a connection from host:port
|
|
155 |
*/
|
|
156 |
private final static int ACCEPT = 0x4;
|
|
157 |
|
|
158 |
/**
|
|
159 |
* Resolve DNS queries
|
|
160 |
*/
|
|
161 |
private final static int RESOLVE = 0x8;
|
|
162 |
|
|
163 |
/**
|
|
164 |
* No actions
|
|
165 |
*/
|
|
166 |
private final static int NONE = 0x0;
|
|
167 |
|
|
168 |
/**
|
|
169 |
* All actions
|
|
170 |
*/
|
|
171 |
private final static int ALL = CONNECT|LISTEN|ACCEPT|RESOLVE;
|
|
172 |
|
|
173 |
// various port constants
|
|
174 |
private static final int PORT_MIN = 0;
|
|
175 |
private static final int PORT_MAX = 65535;
|
|
176 |
private static final int PRIV_PORT_MAX = 1023;
|
|
177 |
|
|
178 |
// the actions mask
|
|
179 |
private transient int mask;
|
|
180 |
|
|
181 |
/**
|
|
182 |
* the actions string.
|
|
183 |
*
|
|
184 |
* @serial
|
|
185 |
*/
|
|
186 |
|
|
187 |
private String actions; // Left null as long as possible, then
|
|
188 |
// created and re-used in the getAction function.
|
|
189 |
|
|
190 |
// hostname part as it is passed
|
|
191 |
private transient String hostname;
|
|
192 |
|
|
193 |
// the canonical name of the host
|
|
194 |
// in the case of "*.foo.com", cname is ".foo.com".
|
|
195 |
|
|
196 |
private transient String cname;
|
|
197 |
|
|
198 |
// all the IP addresses of the host
|
|
199 |
private transient InetAddress[] addresses;
|
|
200 |
|
|
201 |
// true if the hostname is a wildcard (e.g. "*.sun.com")
|
|
202 |
private transient boolean wildcard;
|
|
203 |
|
|
204 |
// true if we were initialized with a single numeric IP address
|
|
205 |
private transient boolean init_with_ip;
|
|
206 |
|
|
207 |
// true if this SocketPermission represents an invalid/unknown host
|
|
208 |
// used for implies when the delayed lookup has already failed
|
|
209 |
private transient boolean invalid;
|
|
210 |
|
|
211 |
// port range on host
|
|
212 |
private transient int[] portrange;
|
|
213 |
|
|
214 |
// true if the trustProxy system property is set
|
|
215 |
private static boolean trustProxy;
|
|
216 |
|
|
217 |
static {
|
|
218 |
Boolean tmp = java.security.AccessController.doPrivileged(
|
|
219 |
new sun.security.action.GetBooleanAction("trustProxy"));
|
|
220 |
trustProxy = tmp.booleanValue();
|
|
221 |
}
|
|
222 |
|
|
223 |
/**
|
|
224 |
* Creates a new SocketPermission object with the specified actions.
|
|
225 |
* The host is expressed as a DNS name, or as a numerical IP address.
|
|
226 |
* Optionally, a port or a portrange may be supplied (separated
|
|
227 |
* from the DNS name or IP address by a colon).
|
|
228 |
* <p>
|
|
229 |
* To specify the local machine, use "localhost" as the <i>host</i>.
|
|
230 |
* Also note: An empty <i>host</i> String ("") is equivalent to "localhost".
|
|
231 |
* <p>
|
|
232 |
* The <i>actions</i> parameter contains a comma-separated list of the
|
|
233 |
* actions granted for the specified host (and port(s)). Possible actions are
|
|
234 |
* "connect", "listen", "accept", "resolve", or
|
|
235 |
* any combination of those. "resolve" is automatically added
|
|
236 |
* when any of the other three are specified.
|
|
237 |
* <p>
|
|
238 |
* Examples of SocketPermission instantiation are the following:
|
|
239 |
* <pre>
|
|
240 |
* nr = new SocketPermission("www.catalog.com", "connect");
|
|
241 |
* nr = new SocketPermission("www.sun.com:80", "connect");
|
|
242 |
* nr = new SocketPermission("*.sun.com", "connect");
|
|
243 |
* nr = new SocketPermission("*.edu", "resolve");
|
|
244 |
* nr = new SocketPermission("204.160.241.0", "connect");
|
|
245 |
* nr = new SocketPermission("localhost:1024-65535", "listen");
|
|
246 |
* nr = new SocketPermission("204.160.241.0:1024-65535", "connect");
|
|
247 |
* </pre>
|
|
248 |
*
|
|
249 |
* @param host the hostname or IPaddress of the computer, optionally
|
|
250 |
* including a colon followed by a port or port range.
|
|
251 |
* @param action the action string.
|
|
252 |
*/
|
|
253 |
public SocketPermission(String host, String action) {
|
|
254 |
super(getHost(host));
|
|
255 |
// name initialized to getHost(host); NPE detected in getHost()
|
|
256 |
init(getName(), getMask(action));
|
|
257 |
}
|
|
258 |
|
|
259 |
|
|
260 |
SocketPermission(String host, int mask) {
|
|
261 |
super(getHost(host));
|
|
262 |
// name initialized to getHost(host); NPE detected in getHost()
|
|
263 |
init(getName(), mask);
|
|
264 |
}
|
|
265 |
|
|
266 |
private static String getHost(String host)
|
|
267 |
{
|
|
268 |
if (host.equals("")) {
|
|
269 |
return "localhost";
|
|
270 |
} else {
|
|
271 |
/* IPv6 literal address used in this context should follow
|
|
272 |
* the format specified in RFC 2732;
|
|
273 |
* if not, we try to solve the unambiguous case
|
|
274 |
*/
|
|
275 |
int ind;
|
|
276 |
if (host.charAt(0) != '[') {
|
|
277 |
if ((ind = host.indexOf(':')) != host.lastIndexOf(':')) {
|
|
278 |
/* More than one ":", meaning IPv6 address is not
|
|
279 |
* in RFC 2732 format;
|
|
280 |
* We will rectify user errors for all unambiguious cases
|
|
281 |
*/
|
|
282 |
StringTokenizer st = new StringTokenizer(host, ":");
|
|
283 |
int tokens = st.countTokens();
|
|
284 |
if (tokens == 9) {
|
|
285 |
// IPv6 address followed by port
|
|
286 |
ind = host.lastIndexOf(':');
|
|
287 |
host = "[" + host.substring(0, ind) + "]" +
|
|
288 |
host.substring(ind);
|
|
289 |
} else if (tokens == 8 && host.indexOf("::") == -1) {
|
|
290 |
// IPv6 address only, not followed by port
|
|
291 |
host = "[" + host + "]";
|
|
292 |
} else {
|
|
293 |
// could be ambiguous
|
|
294 |
throw new IllegalArgumentException("Ambiguous"+
|
|
295 |
" hostport part");
|
|
296 |
}
|
|
297 |
}
|
|
298 |
}
|
|
299 |
return host;
|
|
300 |
}
|
|
301 |
}
|
|
302 |
|
|
303 |
private int[] parsePort(String port)
|
|
304 |
throws Exception
|
|
305 |
{
|
|
306 |
|
|
307 |
if (port == null || port.equals("") || port.equals("*")) {
|
|
308 |
return new int[] {PORT_MIN, PORT_MAX};
|
|
309 |
}
|
|
310 |
|
|
311 |
int dash = port.indexOf('-');
|
|
312 |
|
|
313 |
if (dash == -1) {
|
|
314 |
int p = Integer.parseInt(port);
|
|
315 |
return new int[] {p, p};
|
|
316 |
} else {
|
|
317 |
String low = port.substring(0, dash);
|
|
318 |
String high = port.substring(dash+1);
|
|
319 |
int l,h;
|
|
320 |
|
|
321 |
if (low.equals("")) {
|
|
322 |
l = PORT_MIN;
|
|
323 |
} else {
|
|
324 |
l = Integer.parseInt(low);
|
|
325 |
}
|
|
326 |
|
|
327 |
if (high.equals("")) {
|
|
328 |
h = PORT_MAX;
|
|
329 |
} else {
|
|
330 |
h = Integer.parseInt(high);
|
|
331 |
}
|
|
332 |
if (l < 0 || h < 0 || h<l)
|
|
333 |
throw new IllegalArgumentException("invalid port range");
|
|
334 |
|
|
335 |
return new int[] {l, h};
|
|
336 |
}
|
|
337 |
}
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Initialize the SocketPermission object. We don't do any DNS lookups
|
|
341 |
* as this point, instead we hold off until the implies method is
|
|
342 |
* called.
|
|
343 |
*/
|
|
344 |
private void init(String host, int mask) {
|
|
345 |
// Set the integer mask that represents the actions
|
|
346 |
|
|
347 |
if ((mask & ALL) != mask)
|
|
348 |
throw new IllegalArgumentException("invalid actions mask");
|
|
349 |
|
|
350 |
// always OR in RESOLVE if we allow any of the others
|
|
351 |
this.mask = mask | RESOLVE;
|
|
352 |
|
|
353 |
// Parse the host name. A name has up to three components, the
|
|
354 |
// hostname, a port number, or two numbers representing a port
|
|
355 |
// range. "www.sun.com:8080-9090" is a valid host name.
|
|
356 |
|
|
357 |
// With IPv6 an address can be 2010:836B:4179::836B:4179
|
|
358 |
// An IPv6 address needs to be enclose in []
|
|
359 |
// For ex: [2010:836B:4179::836B:4179]:8080-9090
|
|
360 |
// Refer to RFC 2732 for more information.
|
|
361 |
|
|
362 |
int rb = 0 ;
|
|
363 |
int start = 0, end = 0;
|
|
364 |
int sep = -1;
|
|
365 |
String hostport = host;
|
|
366 |
if (host.charAt(0) == '[') {
|
|
367 |
start = 1;
|
|
368 |
rb = host.indexOf(']');
|
|
369 |
if (rb != -1) {
|
|
370 |
host = host.substring(start, rb);
|
|
371 |
} else {
|
|
372 |
throw new
|
|
373 |
IllegalArgumentException("invalid host/port: "+host);
|
|
374 |
}
|
|
375 |
sep = hostport.indexOf(':', rb+1);
|
|
376 |
} else {
|
|
377 |
start = 0;
|
|
378 |
sep = host.indexOf(':', rb);
|
|
379 |
end = sep;
|
|
380 |
if (sep != -1) {
|
|
381 |
host = host.substring(start, end);
|
|
382 |
}
|
|
383 |
}
|
|
384 |
|
|
385 |
if (sep != -1) {
|
|
386 |
String port = hostport.substring(sep+1);
|
|
387 |
try {
|
|
388 |
portrange = parsePort(port);
|
|
389 |
} catch (Exception e) {
|
|
390 |
throw new
|
|
391 |
IllegalArgumentException("invalid port range: "+port);
|
|
392 |
}
|
|
393 |
} else {
|
|
394 |
portrange = new int[] { PORT_MIN, PORT_MAX };
|
|
395 |
}
|
|
396 |
|
|
397 |
hostname = host;
|
|
398 |
|
|
399 |
// is this a domain wildcard specification
|
|
400 |
if (host.lastIndexOf('*') > 0) {
|
|
401 |
throw new
|
|
402 |
IllegalArgumentException("invalid host wildcard specification");
|
|
403 |
} else if (host.startsWith("*")) {
|
|
404 |
wildcard = true;
|
|
405 |
if (host.equals("*")) {
|
|
406 |
cname = "";
|
|
407 |
} else if (host.startsWith("*.")) {
|
|
408 |
cname = host.substring(1).toLowerCase();
|
|
409 |
} else {
|
|
410 |
throw new
|
|
411 |
IllegalArgumentException("invalid host wildcard specification");
|
|
412 |
}
|
|
413 |
return;
|
|
414 |
} else {
|
|
415 |
if (host.length() > 0) {
|
|
416 |
// see if we are being initialized with an IP address.
|
|
417 |
char ch = host.charAt(0);
|
|
418 |
if (ch == ':' || Character.digit(ch, 16) != -1) {
|
|
419 |
byte ip[] = IPAddressUtil.textToNumericFormatV4(host);
|
|
420 |
if (ip == null) {
|
|
421 |
ip = IPAddressUtil.textToNumericFormatV6(host);
|
|
422 |
}
|
|
423 |
if (ip != null) {
|
|
424 |
try {
|
|
425 |
addresses =
|
|
426 |
new InetAddress[]
|
|
427 |
{InetAddress.getByAddress(ip) };
|
|
428 |
init_with_ip = true;
|
|
429 |
} catch (UnknownHostException uhe) {
|
|
430 |
// this shouldn't happen
|
|
431 |
invalid = true;
|
|
432 |
}
|
|
433 |
}
|
|
434 |
}
|
|
435 |
}
|
|
436 |
}
|
|
437 |
}
|
|
438 |
|
|
439 |
/**
|
|
440 |
* Convert an action string to an integer actions mask.
|
|
441 |
*
|
|
442 |
* @param action the action string
|
|
443 |
* @return the action mask
|
|
444 |
*/
|
|
445 |
private static int getMask(String action) {
|
|
446 |
|
|
447 |
if (action == null) {
|
|
448 |
throw new NullPointerException("action can't be null");
|
|
449 |
}
|
|
450 |
|
|
451 |
if (action.equals("")) {
|
|
452 |
throw new IllegalArgumentException("action can't be empty");
|
|
453 |
}
|
|
454 |
|
|
455 |
int mask = NONE;
|
|
456 |
|
|
457 |
// Check against use of constants (used heavily within the JDK)
|
|
458 |
if (action == SecurityConstants.SOCKET_RESOLVE_ACTION) {
|
|
459 |
return RESOLVE;
|
|
460 |
} else if (action == SecurityConstants.SOCKET_CONNECT_ACTION) {
|
|
461 |
return CONNECT;
|
|
462 |
} else if (action == SecurityConstants.SOCKET_LISTEN_ACTION) {
|
|
463 |
return LISTEN;
|
|
464 |
} else if (action == SecurityConstants.SOCKET_ACCEPT_ACTION) {
|
|
465 |
return ACCEPT;
|
|
466 |
} else if (action == SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION) {
|
|
467 |
return CONNECT|ACCEPT;
|
|
468 |
}
|
|
469 |
|
|
470 |
char[] a = action.toCharArray();
|
|
471 |
|
|
472 |
int i = a.length - 1;
|
|
473 |
if (i < 0)
|
|
474 |
return mask;
|
|
475 |
|
|
476 |
while (i != -1) {
|
|
477 |
char c;
|
|
478 |
|
|
479 |
// skip whitespace
|
|
480 |
while ((i!=-1) && ((c = a[i]) == ' ' ||
|
|
481 |
c == '\r' ||
|
|
482 |
c == '\n' ||
|
|
483 |
c == '\f' ||
|
|
484 |
c == '\t'))
|
|
485 |
i--;
|
|
486 |
|
|
487 |
// check for the known strings
|
|
488 |
int matchlen;
|
|
489 |
|
|
490 |
if (i >= 6 && (a[i-6] == 'c' || a[i-6] == 'C') &&
|
|
491 |
(a[i-5] == 'o' || a[i-5] == 'O') &&
|
|
492 |
(a[i-4] == 'n' || a[i-4] == 'N') &&
|
|
493 |
(a[i-3] == 'n' || a[i-3] == 'N') &&
|
|
494 |
(a[i-2] == 'e' || a[i-2] == 'E') &&
|
|
495 |
(a[i-1] == 'c' || a[i-1] == 'C') &&
|
|
496 |
(a[i] == 't' || a[i] == 'T'))
|
|
497 |
{
|
|
498 |
matchlen = 7;
|
|
499 |
mask |= CONNECT;
|
|
500 |
|
|
501 |
} else if (i >= 6 && (a[i-6] == 'r' || a[i-6] == 'R') &&
|
|
502 |
(a[i-5] == 'e' || a[i-5] == 'E') &&
|
|
503 |
(a[i-4] == 's' || a[i-4] == 'S') &&
|
|
504 |
(a[i-3] == 'o' || a[i-3] == 'O') &&
|
|
505 |
(a[i-2] == 'l' || a[i-2] == 'L') &&
|
|
506 |
(a[i-1] == 'v' || a[i-1] == 'V') &&
|
|
507 |
(a[i] == 'e' || a[i] == 'E'))
|
|
508 |
{
|
|
509 |
matchlen = 7;
|
|
510 |
mask |= RESOLVE;
|
|
511 |
|
|
512 |
} else if (i >= 5 && (a[i-5] == 'l' || a[i-5] == 'L') &&
|
|
513 |
(a[i-4] == 'i' || a[i-4] == 'I') &&
|
|
514 |
(a[i-3] == 's' || a[i-3] == 'S') &&
|
|
515 |
(a[i-2] == 't' || a[i-2] == 'T') &&
|
|
516 |
(a[i-1] == 'e' || a[i-1] == 'E') &&
|
|
517 |
(a[i] == 'n' || a[i] == 'N'))
|
|
518 |
{
|
|
519 |
matchlen = 6;
|
|
520 |
mask |= LISTEN;
|
|
521 |
|
|
522 |
} else if (i >= 5 && (a[i-5] == 'a' || a[i-5] == 'A') &&
|
|
523 |
(a[i-4] == 'c' || a[i-4] == 'C') &&
|
|
524 |
(a[i-3] == 'c' || a[i-3] == 'C') &&
|
|
525 |
(a[i-2] == 'e' || a[i-2] == 'E') &&
|
|
526 |
(a[i-1] == 'p' || a[i-1] == 'P') &&
|
|
527 |
(a[i] == 't' || a[i] == 'T'))
|
|
528 |
{
|
|
529 |
matchlen = 6;
|
|
530 |
mask |= ACCEPT;
|
|
531 |
|
|
532 |
} else {
|
|
533 |
// parse error
|
|
534 |
throw new IllegalArgumentException(
|
|
535 |
"invalid permission: " + action);
|
|
536 |
}
|
|
537 |
|
|
538 |
// make sure we didn't just match the tail of a word
|
|
539 |
// like "ackbarfaccept". Also, skip to the comma.
|
|
540 |
boolean seencomma = false;
|
|
541 |
while (i >= matchlen && !seencomma) {
|
|
542 |
switch(a[i-matchlen]) {
|
|
543 |
case ',':
|
|
544 |
seencomma = true;
|
|
545 |
/*FALLTHROUGH*/
|
|
546 |
case ' ': case '\r': case '\n':
|
|
547 |
case '\f': case '\t':
|
|
548 |
break;
|
|
549 |
default:
|
|
550 |
throw new IllegalArgumentException(
|
|
551 |
"invalid permission: " + action);
|
|
552 |
}
|
|
553 |
i--;
|
|
554 |
}
|
|
555 |
|
|
556 |
// point i at the location of the comma minus one (or -1).
|
|
557 |
i -= matchlen;
|
|
558 |
}
|
|
559 |
|
|
560 |
return mask;
|
|
561 |
}
|
|
562 |
|
|
563 |
/**
|
|
564 |
* attempt to get the fully qualified domain name
|
|
565 |
*
|
|
566 |
*/
|
|
567 |
void getCanonName()
|
|
568 |
throws UnknownHostException
|
|
569 |
{
|
|
570 |
if (cname != null || invalid) return;
|
|
571 |
|
|
572 |
// attempt to get the canonical name
|
|
573 |
|
|
574 |
try {
|
|
575 |
// first get the IP addresses if we don't have them yet
|
|
576 |
// this is because we need the IP address to then get
|
|
577 |
// FQDN.
|
|
578 |
if (addresses == null) {
|
|
579 |
getIP();
|
|
580 |
}
|
|
581 |
|
|
582 |
// we have to do this check, otherwise we might not
|
|
583 |
// get the fully qualified domain name
|
|
584 |
if (init_with_ip) {
|
|
585 |
cname = addresses[0].getHostName(false).toLowerCase();
|
|
586 |
} else {
|
|
587 |
cname = InetAddress.getByName(addresses[0].getHostAddress()).
|
|
588 |
getHostName(false).toLowerCase();
|
|
589 |
}
|
|
590 |
} catch (UnknownHostException uhe) {
|
|
591 |
invalid = true;
|
|
592 |
throw uhe;
|
|
593 |
}
|
|
594 |
}
|
|
595 |
|
|
596 |
/**
|
|
597 |
* get IP addresses. Sets invalid to true if we can't get them.
|
|
598 |
*
|
|
599 |
*/
|
|
600 |
void getIP()
|
|
601 |
throws UnknownHostException
|
|
602 |
{
|
|
603 |
if (addresses != null || wildcard || invalid) return;
|
|
604 |
|
|
605 |
try {
|
|
606 |
// now get all the IP addresses
|
|
607 |
String host;
|
|
608 |
if (getName().charAt(0) == '[') {
|
|
609 |
// Literal IPv6 address
|
|
610 |
host = getName().substring(1, getName().indexOf(']'));
|
|
611 |
} else {
|
|
612 |
int i = getName().indexOf(":");
|
|
613 |
if (i == -1)
|
|
614 |
host = getName();
|
|
615 |
else {
|
|
616 |
host = getName().substring(0,i);
|
|
617 |
}
|
|
618 |
}
|
|
619 |
|
|
620 |
addresses =
|
|
621 |
new InetAddress[] {InetAddress.getAllByName0(host, false)[0]};
|
|
622 |
|
|
623 |
} catch (UnknownHostException uhe) {
|
|
624 |
invalid = true;
|
|
625 |
throw uhe;
|
|
626 |
} catch (IndexOutOfBoundsException iobe) {
|
|
627 |
invalid = true;
|
|
628 |
throw new UnknownHostException(getName());
|
|
629 |
}
|
|
630 |
}
|
|
631 |
|
|
632 |
/**
|
|
633 |
* Checks if this socket permission object "implies" the
|
|
634 |
* specified permission.
|
|
635 |
* <P>
|
|
636 |
* More specifically, this method first ensures that all of the following
|
|
637 |
* are true (and returns false if any of them are not):<p>
|
|
638 |
* <ul>
|
|
639 |
* <li> <i>p</i> is an instanceof SocketPermission,<p>
|
|
640 |
* <li> <i>p</i>'s actions are a proper subset of this
|
|
641 |
* object's actions, and<p>
|
|
642 |
* <li> <i>p</i>'s port range is included in this port range. Note:
|
|
643 |
* port range is ignored when p only contains the action, 'resolve'.<p>
|
|
644 |
* </ul>
|
|
645 |
*
|
|
646 |
* Then <code>implies</code> checks each of the following, in order,
|
|
647 |
* and for each returns true if the stated condition is true:<p>
|
|
648 |
* <ul>
|
|
649 |
* <li> If this object was initialized with a single IP address and one of <i>p</i>'s
|
|
650 |
* IP addresses is equal to this object's IP address.<p>
|
|
651 |
* <li>If this object is a wildcard domain (such as *.sun.com), and
|
|
652 |
* <i>p</i>'s canonical name (the name without any preceding *)
|
|
653 |
* ends with this object's canonical host name. For example, *.sun.com
|
|
654 |
* implies *.eng.sun.com..<p>
|
|
655 |
* <li>If this object was not initialized with a single IP address, and one of this
|
|
656 |
* object's IP addresses equals one of <i>p</i>'s IP addresses.<p>
|
|
657 |
* <li>If this canonical name equals <i>p</i>'s canonical name.<p>
|
|
658 |
* </ul>
|
|
659 |
*
|
|
660 |
* If none of the above are true, <code>implies</code> returns false.
|
|
661 |
* @param p the permission to check against.
|
|
662 |
*
|
|
663 |
* @return true if the specified permission is implied by this object,
|
|
664 |
* false if not.
|
|
665 |
*/
|
|
666 |
|
|
667 |
public boolean implies(Permission p) {
|
|
668 |
int i,j;
|
|
669 |
|
|
670 |
if (!(p instanceof SocketPermission))
|
|
671 |
return false;
|
|
672 |
|
|
673 |
if (p == this)
|
|
674 |
return true;
|
|
675 |
|
|
676 |
SocketPermission that = (SocketPermission) p;
|
|
677 |
|
|
678 |
return ((this.mask & that.mask) == that.mask) &&
|
|
679 |
impliesIgnoreMask(that);
|
|
680 |
}
|
|
681 |
|
|
682 |
/**
|
|
683 |
* Checks if the incoming Permission's action are a proper subset of
|
|
684 |
* the this object's actions.
|
|
685 |
* <P>
|
|
686 |
* Check, in the following order:
|
|
687 |
* <ul>
|
|
688 |
* <li> Checks that "p" is an instanceof a SocketPermission
|
|
689 |
* <li> Checks that "p"'s actions are a proper subset of the
|
|
690 |
* current object's actions.
|
|
691 |
* <li> Checks that "p"'s port range is included in this port range
|
|
692 |
* <li> If this object was initialized with an IP address, checks that
|
|
693 |
* one of "p"'s IP addresses is equal to this object's IP address.
|
|
694 |
* <li> If either object is a wildcard domain (i.e., "*.sun.com"),
|
|
695 |
* attempt to match based on the wildcard.
|
|
696 |
* <li> If this object was not initialized with an IP address, attempt
|
|
697 |
* to find a match based on the IP addresses in both objects.
|
|
698 |
* <li> Attempt to match on the canonical hostnames of both objects.
|
|
699 |
* </ul>
|
|
700 |
* @param p the incoming permission request
|
|
701 |
*
|
|
702 |
* @return true if "permission" is a proper subset of the current object,
|
|
703 |
* false if not.
|
|
704 |
*/
|
|
705 |
|
|
706 |
boolean impliesIgnoreMask(SocketPermission that) {
|
|
707 |
int i,j;
|
|
708 |
|
|
709 |
if ((that.mask & RESOLVE) != that.mask) {
|
|
710 |
// check port range
|
|
711 |
if ((that.portrange[0] < this.portrange[0]) ||
|
|
712 |
(that.portrange[1] > this.portrange[1])) {
|
|
713 |
return false;
|
|
714 |
}
|
|
715 |
}
|
|
716 |
|
|
717 |
// allow a "*" wildcard to always match anything
|
|
718 |
if (this.wildcard && "".equals(this.cname))
|
|
719 |
return true;
|
|
720 |
|
|
721 |
// return if either one of these NetPerm objects are invalid...
|
|
722 |
if (this.invalid || that.invalid) {
|
|
723 |
return (trustProxy ? inProxyWeTrust(that) : false);
|
|
724 |
}
|
|
725 |
|
|
726 |
|
|
727 |
if (this.getName().equalsIgnoreCase(that.getName())) {
|
|
728 |
return true;
|
|
729 |
}
|
|
730 |
|
|
731 |
try {
|
|
732 |
if (this.init_with_ip) { // we only check IP addresses
|
|
733 |
if (that.wildcard)
|
|
734 |
return false;
|
|
735 |
|
|
736 |
if (that.init_with_ip) {
|
|
737 |
return (this.addresses[0].equals(that.addresses[0]));
|
|
738 |
} else {
|
|
739 |
if (that.addresses == null) {
|
|
740 |
that.getIP();
|
|
741 |
}
|
|
742 |
for (i=0; i < that.addresses.length; i++) {
|
|
743 |
if (this.addresses[0].equals(that.addresses[i]))
|
|
744 |
return true;
|
|
745 |
}
|
|
746 |
}
|
|
747 |
// since "this" was initialized with an IP address, we
|
|
748 |
// don't check any other cases
|
|
749 |
return false;
|
|
750 |
}
|
|
751 |
|
|
752 |
// check and see if we have any wildcards...
|
|
753 |
if (this.wildcard || that.wildcard) {
|
|
754 |
// if they are both wildcards, return true iff
|
|
755 |
// that's cname ends with this cname (i.e., *.sun.com
|
|
756 |
// implies *.eng.sun.com)
|
|
757 |
if (this.wildcard && that.wildcard)
|
|
758 |
return (that.cname.endsWith(this.cname));
|
|
759 |
|
|
760 |
// a non-wildcard can't imply a wildcard
|
|
761 |
if (that.wildcard)
|
|
762 |
return false;
|
|
763 |
|
|
764 |
// this is a wildcard, lets see if that's cname ends with
|
|
765 |
// it...
|
|
766 |
if (that.cname == null) {
|
|
767 |
that.getCanonName();
|
|
768 |
}
|
|
769 |
return (that.cname.endsWith(this.cname));
|
|
770 |
}
|
|
771 |
|
|
772 |
// comapare IP addresses
|
|
773 |
if (this.addresses == null) {
|
|
774 |
this.getIP();
|
|
775 |
}
|
|
776 |
|
|
777 |
if (that.addresses == null) {
|
|
778 |
that.getIP();
|
|
779 |
}
|
|
780 |
|
|
781 |
for (j = 0; j < this.addresses.length; j++) {
|
|
782 |
for (i=0; i < that.addresses.length; i++) {
|
|
783 |
if (this.addresses[j].equals(that.addresses[i]))
|
|
784 |
return true;
|
|
785 |
}
|
|
786 |
}
|
|
787 |
|
|
788 |
// XXX: if all else fails, compare hostnames?
|
|
789 |
// Do we really want this?
|
|
790 |
if (this.cname == null) {
|
|
791 |
this.getCanonName();
|
|
792 |
}
|
|
793 |
|
|
794 |
if (that.cname == null) {
|
|
795 |
that.getCanonName();
|
|
796 |
}
|
|
797 |
|
|
798 |
return (this.cname.equalsIgnoreCase(that.cname));
|
|
799 |
|
|
800 |
} catch (UnknownHostException uhe) {
|
|
801 |
if (trustProxy)
|
|
802 |
return inProxyWeTrust(that);
|
|
803 |
}
|
|
804 |
|
|
805 |
// make sure the first thing that is done here is to return
|
|
806 |
// false. If not, uncomment the return false in the above catch.
|
|
807 |
|
|
808 |
return false;
|
|
809 |
}
|
|
810 |
|
|
811 |
private boolean inProxyWeTrust(SocketPermission that) {
|
|
812 |
// if we trust the proxy, we see if the original names/IPs passed
|
|
813 |
// in were equal.
|
|
814 |
|
|
815 |
String thisHost = hostname;
|
|
816 |
String thatHost = that.hostname;
|
|
817 |
|
|
818 |
if (thisHost == null)
|
|
819 |
return false;
|
|
820 |
else
|
|
821 |
return thisHost.equalsIgnoreCase(thatHost);
|
|
822 |
|
|
823 |
}
|
|
824 |
/**
|
|
825 |
* Checks two SocketPermission objects for equality.
|
|
826 |
* <P>
|
|
827 |
* @param obj the object to test for equality with this object.
|
|
828 |
*
|
|
829 |
* @return true if <i>obj</i> is a SocketPermission, and has the
|
|
830 |
* same hostname, port range, and actions as this
|
|
831 |
* SocketPermission object. However, port range will be ignored
|
|
832 |
* in the comparison if <i>obj</i> only contains the action, 'resolve'.
|
|
833 |
*/
|
|
834 |
public boolean equals(Object obj) {
|
|
835 |
if (obj == this)
|
|
836 |
return true;
|
|
837 |
|
|
838 |
if (! (obj instanceof SocketPermission))
|
|
839 |
return false;
|
|
840 |
|
|
841 |
SocketPermission that = (SocketPermission) obj;
|
|
842 |
|
|
843 |
//this is (overly?) complex!!!
|
|
844 |
|
|
845 |
// check the mask first
|
|
846 |
if (this.mask != that.mask) return false;
|
|
847 |
|
|
848 |
if ((that.mask & RESOLVE) != that.mask) {
|
|
849 |
// now check the port range...
|
|
850 |
if ((this.portrange[0] != that.portrange[0]) ||
|
|
851 |
(this.portrange[1] != that.portrange[1])) {
|
|
852 |
return false;
|
|
853 |
}
|
|
854 |
}
|
|
855 |
|
|
856 |
// short cut. This catches:
|
|
857 |
// "crypto" equal to "crypto", or
|
|
858 |
// "1.2.3.4" equal to "1.2.3.4.", or
|
|
859 |
// "*.edu" equal to "*.edu", but it
|
|
860 |
// does not catch "crypto" equal to
|
|
861 |
// "crypto.eng.sun.com".
|
|
862 |
|
|
863 |
if (this.getName().equalsIgnoreCase(that.getName())) {
|
|
864 |
return true;
|
|
865 |
}
|
|
866 |
|
|
867 |
// we now attempt to get the Canonical (FQDN) name and
|
|
868 |
// compare that. If this fails, about all we can do is return
|
|
869 |
// false.
|
|
870 |
|
|
871 |
try {
|
|
872 |
this.getCanonName();
|
|
873 |
that.getCanonName();
|
|
874 |
} catch (UnknownHostException uhe) {
|
|
875 |
return false;
|
|
876 |
}
|
|
877 |
|
|
878 |
if (this.invalid || that.invalid)
|
|
879 |
return false;
|
|
880 |
|
|
881 |
if (this.cname != null) {
|
|
882 |
return this.cname.equalsIgnoreCase(that.cname);
|
|
883 |
}
|
|
884 |
|
|
885 |
return false;
|
|
886 |
}
|
|
887 |
|
|
888 |
/**
|
|
889 |
* Returns the hash code value for this object.
|
|
890 |
*
|
|
891 |
* @return a hash code value for this object.
|
|
892 |
*/
|
|
893 |
|
|
894 |
public int hashCode() {
|
|
895 |
/*
|
|
896 |
* If this SocketPermission was initialized with an IP address
|
|
897 |
* or a wildcard, use getName().hashCode(), otherwise use
|
|
898 |
* the hashCode() of the host name returned from
|
|
899 |
* java.net.InetAddress.getHostName method.
|
|
900 |
*/
|
|
901 |
|
|
902 |
if (init_with_ip || wildcard) {
|
|
903 |
return this.getName().hashCode();
|
|
904 |
}
|
|
905 |
|
|
906 |
try {
|
|
907 |
getCanonName();
|
|
908 |
} catch (UnknownHostException uhe) {
|
|
909 |
|
|
910 |
}
|
|
911 |
|
|
912 |
if (invalid || cname == null)
|
|
913 |
return this.getName().hashCode();
|
|
914 |
else
|
|
915 |
return this.cname.hashCode();
|
|
916 |
}
|
|
917 |
|
|
918 |
/**
|
|
919 |
* Return the current action mask.
|
|
920 |
*
|
|
921 |
* @return the actions mask.
|
|
922 |
*/
|
|
923 |
|
|
924 |
int getMask() {
|
|
925 |
return mask;
|
|
926 |
}
|
|
927 |
|
|
928 |
/**
|
|
929 |
* Returns the "canonical string representation" of the actions in the
|
|
930 |
* specified mask.
|
|
931 |
* Always returns present actions in the following order:
|
|
932 |
* connect, listen, accept, resolve.
|
|
933 |
*
|
|
934 |
* @param mask a specific integer action mask to translate into a string
|
|
935 |
* @return the canonical string representation of the actions
|
|
936 |
*/
|
|
937 |
private static String getActions(int mask)
|
|
938 |
{
|
|
939 |
StringBuilder sb = new StringBuilder();
|
|
940 |
boolean comma = false;
|
|
941 |
|
|
942 |
if ((mask & CONNECT) == CONNECT) {
|
|
943 |
comma = true;
|
|
944 |
sb.append("connect");
|
|
945 |
}
|
|
946 |
|
|
947 |
if ((mask & LISTEN) == LISTEN) {
|
|
948 |
if (comma) sb.append(',');
|
|
949 |
else comma = true;
|
|
950 |
sb.append("listen");
|
|
951 |
}
|
|
952 |
|
|
953 |
if ((mask & ACCEPT) == ACCEPT) {
|
|
954 |
if (comma) sb.append(',');
|
|
955 |
else comma = true;
|
|
956 |
sb.append("accept");
|
|
957 |
}
|
|
958 |
|
|
959 |
|
|
960 |
if ((mask & RESOLVE) == RESOLVE) {
|
|
961 |
if (comma) sb.append(',');
|
|
962 |
else comma = true;
|
|
963 |
sb.append("resolve");
|
|
964 |
}
|
|
965 |
|
|
966 |
return sb.toString();
|
|
967 |
}
|
|
968 |
|
|
969 |
/**
|
|
970 |
* Returns the canonical string representation of the actions.
|
|
971 |
* Always returns present actions in the following order:
|
|
972 |
* connect, listen, accept, resolve.
|
|
973 |
*
|
|
974 |
* @return the canonical string representation of the actions.
|
|
975 |
*/
|
|
976 |
public String getActions()
|
|
977 |
{
|
|
978 |
if (actions == null)
|
|
979 |
actions = getActions(this.mask);
|
|
980 |
|
|
981 |
return actions;
|
|
982 |
}
|
|
983 |
|
|
984 |
/**
|
|
985 |
* Returns a new PermissionCollection object for storing SocketPermission
|
|
986 |
* objects.
|
|
987 |
* <p>
|
|
988 |
* SocketPermission objects must be stored in a manner that allows them
|
|
989 |
* to be inserted into the collection in any order, but that also enables the
|
|
990 |
* PermissionCollection <code>implies</code>
|
|
991 |
* method to be implemented in an efficient (and consistent) manner.
|
|
992 |
*
|
|
993 |
* @return a new PermissionCollection object suitable for storing SocketPermissions.
|
|
994 |
*/
|
|
995 |
|
|
996 |
public PermissionCollection newPermissionCollection() {
|
|
997 |
return new SocketPermissionCollection();
|
|
998 |
}
|
|
999 |
|
|
1000 |
/**
|
|
1001 |
* WriteObject is called to save the state of the SocketPermission
|
|
1002 |
* to a stream. The actions are serialized, and the superclass
|
|
1003 |
* takes care of the name.
|
|
1004 |
*/
|
|
1005 |
private synchronized void writeObject(java.io.ObjectOutputStream s)
|
|
1006 |
throws IOException
|
|
1007 |
{
|
|
1008 |
// Write out the actions. The superclass takes care of the name
|
|
1009 |
// call getActions to make sure actions field is initialized
|
|
1010 |
if (actions == null)
|
|
1011 |
getActions();
|
|
1012 |
s.defaultWriteObject();
|
|
1013 |
}
|
|
1014 |
|
|
1015 |
/**
|
|
1016 |
* readObject is called to restore the state of the SocketPermission from
|
|
1017 |
* a stream.
|
|
1018 |
*/
|
|
1019 |
private synchronized void readObject(java.io.ObjectInputStream s)
|
|
1020 |
throws IOException, ClassNotFoundException
|
|
1021 |
{
|
|
1022 |
// Read in the action, then initialize the rest
|
|
1023 |
s.defaultReadObject();
|
|
1024 |
init(getName(),getMask(actions));
|
|
1025 |
}
|
|
1026 |
|
|
1027 |
/*
|
|
1028 |
public String toString()
|
|
1029 |
{
|
|
1030 |
StringBuffer s = new StringBuffer(super.toString() + "\n" +
|
|
1031 |
"cname = " + cname + "\n" +
|
|
1032 |
"wildcard = " + wildcard + "\n" +
|
|
1033 |
"invalid = " + invalid + "\n" +
|
|
1034 |
"portrange = " + portrange[0] + "," + portrange[1] + "\n");
|
|
1035 |
if (addresses != null) for (int i=0; i<addresses.length; i++) {
|
|
1036 |
s.append( addresses[i].getHostAddress());
|
|
1037 |
s.append("\n");
|
|
1038 |
} else {
|
|
1039 |
s.append("(no addresses)\n");
|
|
1040 |
}
|
|
1041 |
|
|
1042 |
return s.toString();
|
|
1043 |
}
|
|
1044 |
|
|
1045 |
public static void main(String args[]) throws Exception {
|
|
1046 |
SocketPermission this_ = new SocketPermission(args[0], "connect");
|
|
1047 |
SocketPermission that_ = new SocketPermission(args[1], "connect");
|
|
1048 |
System.out.println("-----\n");
|
|
1049 |
System.out.println("this.implies(that) = " + this_.implies(that_));
|
|
1050 |
System.out.println("-----\n");
|
|
1051 |
System.out.println("this = "+this_);
|
|
1052 |
System.out.println("-----\n");
|
|
1053 |
System.out.println("that = "+that_);
|
|
1054 |
System.out.println("-----\n");
|
|
1055 |
|
|
1056 |
SocketPermissionCollection nps = new SocketPermissionCollection();
|
|
1057 |
nps.add(this_);
|
|
1058 |
nps.add(new SocketPermission("www-leland.stanford.edu","connect"));
|
|
1059 |
nps.add(new SocketPermission("www-sun.com","connect"));
|
|
1060 |
System.out.println("nps.implies(that) = " + nps.implies(that_));
|
|
1061 |
System.out.println("-----\n");
|
|
1062 |
}
|
|
1063 |
*/
|
|
1064 |
}
|
|
1065 |
|
|
1066 |
/**
|
|
1067 |
|
|
1068 |
if (init'd with IP, key is IP as string)
|
|
1069 |
if wildcard, its the wild card
|
|
1070 |
else its the cname?
|
|
1071 |
|
|
1072 |
*
|
|
1073 |
* @see java.security.Permission
|
|
1074 |
* @see java.security.Permissions
|
|
1075 |
* @see java.security.PermissionCollection
|
|
1076 |
*
|
|
1077 |
*
|
|
1078 |
* @author Roland Schemers
|
|
1079 |
*
|
|
1080 |
* @serial include
|
|
1081 |
*/
|
|
1082 |
|
|
1083 |
final class SocketPermissionCollection extends PermissionCollection
|
|
1084 |
implements Serializable
|
|
1085 |
{
|
|
1086 |
// Not serialized; see serialization section at end of class
|
|
1087 |
private transient List perms;
|
|
1088 |
|
|
1089 |
/**
|
|
1090 |
* Create an empty SocketPermissions object.
|
|
1091 |
*
|
|
1092 |
*/
|
|
1093 |
|
|
1094 |
public SocketPermissionCollection() {
|
|
1095 |
perms = new ArrayList();
|
|
1096 |
}
|
|
1097 |
|
|
1098 |
/**
|
|
1099 |
* Adds a permission to the SocketPermissions. The key for the hash is
|
|
1100 |
* the name in the case of wildcards, or all the IP addresses.
|
|
1101 |
*
|
|
1102 |
* @param permission the Permission object to add.
|
|
1103 |
*
|
|
1104 |
* @exception IllegalArgumentException - if the permission is not a
|
|
1105 |
* SocketPermission
|
|
1106 |
*
|
|
1107 |
* @exception SecurityException - if this SocketPermissionCollection object
|
|
1108 |
* has been marked readonly
|
|
1109 |
*/
|
|
1110 |
|
|
1111 |
public void add(Permission permission)
|
|
1112 |
{
|
|
1113 |
if (! (permission instanceof SocketPermission))
|
|
1114 |
throw new IllegalArgumentException("invalid permission: "+
|
|
1115 |
permission);
|
|
1116 |
if (isReadOnly())
|
|
1117 |
throw new SecurityException(
|
|
1118 |
"attempt to add a Permission to a readonly PermissionCollection");
|
|
1119 |
|
|
1120 |
// optimization to ensure perms most likely to be tested
|
|
1121 |
// show up early (4301064)
|
|
1122 |
synchronized (this) {
|
|
1123 |
perms.add(0, permission);
|
|
1124 |
}
|
|
1125 |
}
|
|
1126 |
|
|
1127 |
/**
|
|
1128 |
* Check and see if this collection of permissions implies the permissions
|
|
1129 |
* expressed in "permission".
|
|
1130 |
*
|
|
1131 |
* @param p the Permission object to compare
|
|
1132 |
*
|
|
1133 |
* @return true if "permission" is a proper subset of a permission in
|
|
1134 |
* the collection, false if not.
|
|
1135 |
*/
|
|
1136 |
|
|
1137 |
public boolean implies(Permission permission)
|
|
1138 |
{
|
|
1139 |
if (! (permission instanceof SocketPermission))
|
|
1140 |
return false;
|
|
1141 |
|
|
1142 |
SocketPermission np = (SocketPermission) permission;
|
|
1143 |
|
|
1144 |
int desired = np.getMask();
|
|
1145 |
int effective = 0;
|
|
1146 |
int needed = desired;
|
|
1147 |
|
|
1148 |
synchronized (this) {
|
|
1149 |
int len = perms.size();
|
|
1150 |
//System.out.println("implies "+np);
|
|
1151 |
for (int i = 0; i < len; i++) {
|
|
1152 |
SocketPermission x = (SocketPermission) perms.get(i);
|
|
1153 |
//System.out.println(" trying "+x);
|
|
1154 |
if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(np)) {
|
|
1155 |
effective |= x.getMask();
|
|
1156 |
if ((effective & desired) == desired)
|
|
1157 |
return true;
|
|
1158 |
needed = (desired ^ effective);
|
|
1159 |
}
|
|
1160 |
}
|
|
1161 |
}
|
|
1162 |
return false;
|
|
1163 |
}
|
|
1164 |
|
|
1165 |
/**
|
|
1166 |
* Returns an enumeration of all the SocketPermission objects in the
|
|
1167 |
* container.
|
|
1168 |
*
|
|
1169 |
* @return an enumeration of all the SocketPermission objects.
|
|
1170 |
*/
|
|
1171 |
|
|
1172 |
public Enumeration elements() {
|
|
1173 |
// Convert Iterator into Enumeration
|
|
1174 |
synchronized (this) {
|
|
1175 |
return Collections.enumeration(perms);
|
|
1176 |
}
|
|
1177 |
}
|
|
1178 |
|
|
1179 |
private static final long serialVersionUID = 2787186408602843674L;
|
|
1180 |
|
|
1181 |
// Need to maintain serialization interoperability with earlier releases,
|
|
1182 |
// which had the serializable field:
|
|
1183 |
|
|
1184 |
//
|
|
1185 |
// The SocketPermissions for this set.
|
|
1186 |
// @serial
|
|
1187 |
//
|
|
1188 |
// private Vector permissions;
|
|
1189 |
|
|
1190 |
/**
|
|
1191 |
* @serialField permissions java.util.Vector
|
|
1192 |
* A list of the SocketPermissions for this set.
|
|
1193 |
*/
|
|
1194 |
private static final ObjectStreamField[] serialPersistentFields = {
|
|
1195 |
new ObjectStreamField("permissions", Vector.class),
|
|
1196 |
};
|
|
1197 |
|
|
1198 |
/**
|
|
1199 |
* @serialData "permissions" field (a Vector containing the SocketPermissions).
|
|
1200 |
*/
|
|
1201 |
/*
|
|
1202 |
* Writes the contents of the perms field out as a Vector for
|
|
1203 |
* serialization compatibility with earlier releases.
|
|
1204 |
*/
|
|
1205 |
private void writeObject(ObjectOutputStream out) throws IOException {
|
|
1206 |
// Don't call out.defaultWriteObject()
|
|
1207 |
|
|
1208 |
// Write out Vector
|
|
1209 |
Vector permissions = new Vector(perms.size());
|
|
1210 |
|
|
1211 |
synchronized (this) {
|
|
1212 |
permissions.addAll(perms);
|
|
1213 |
}
|
|
1214 |
|
|
1215 |
ObjectOutputStream.PutField pfields = out.putFields();
|
|
1216 |
pfields.put("permissions", permissions);
|
|
1217 |
out.writeFields();
|
|
1218 |
}
|
|
1219 |
|
|
1220 |
/*
|
|
1221 |
* Reads in a Vector of SocketPermissions and saves them in the perms field.
|
|
1222 |
*/
|
|
1223 |
private void readObject(ObjectInputStream in) throws IOException,
|
|
1224 |
ClassNotFoundException {
|
|
1225 |
// Don't call in.defaultReadObject()
|
|
1226 |
|
|
1227 |
// Read in serialized fields
|
|
1228 |
ObjectInputStream.GetField gfields = in.readFields();
|
|
1229 |
|
|
1230 |
// Get the one we want
|
|
1231 |
Vector permissions = (Vector)gfields.get("permissions", null);
|
|
1232 |
perms = new ArrayList(permissions.size());
|
|
1233 |
perms.addAll(permissions);
|
|
1234 |
}
|
|
1235 |
}
|