jdk/src/java.base/share/classes/java/net/SocketPermission.java
author darcy
Thu, 23 Apr 2015 09:32:35 -0700
changeset 29986 97167d851fc4
parent 26219 1a19360ff122
child 31080 00a25f4c4d44
permissions -rw-r--r--
8078467: Update core libraries to use diamond with anonymous classes Reviewed-by: mchung, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2177
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2177
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2177
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2177
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2177
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Collections;
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
    33
import java.util.StringJoiner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PermissionCollection;
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
    38
import java.security.PrivilegedAction;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
    39
import java.security.AccessController;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
    40
import java.security.Security;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.net.util.IPAddressUtil;
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
    47
import sun.net.RegisteredDomain;
22341
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents: 22339
diff changeset
    48
import sun.net.PortConfig;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.security.util.SecurityConstants;
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
    50
import sun.security.util.Debug;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * This class represents access to a network via sockets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * A SocketPermission consists of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * host specification and a set of "actions" specifying ways to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * connect to that host. The host is specified as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *    host = (hostname | IPv4address | iPv6reference) [:portrange]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *    portrange = portnumber | -portnumber | portnumber-[portnumber]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * The host is expressed as a DNS name, as a numerical IP address,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * or as "localhost" (for the local machine).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The wildcard "*" may be included once in a DNS name host
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * specification. If it is included, it must be in the leftmost
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * position, as in "*.sun.com".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * The format of the IPv6reference should follow that specified in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC&nbsp;2732: Format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * for Literal IPv6 Addresses in URLs</i></a>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *    ipv6reference = "[" IPv6address "]"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * For example, you can construct a SocketPermission instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * as the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *    String hostAddress = inetaddress.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *    if (inetaddress instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *        sp = new SocketPermission("[" + hostAddress + "]:" + port, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *        sp = new SocketPermission(hostAddress + ":" + port, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *    String host = url.getHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *    sp = new SocketPermission(host + ":" + port, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * The <A HREF="Inet6Address.html#lform">full uncompressed form</A> of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * an IPv6 literal address is also valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * The port or portrange is optional. A port specification of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * form "N-", where <i>N</i> is a port number, signifies all ports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * numbered <i>N</i> and above, while a specification of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * form "-N" indicates all ports numbered <i>N</i> and below.
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
    97
 * The special port value {@code 0} refers to the entire <i>ephemeral</i>
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
    98
 * port range. This is a fixed range of ports a system may use to
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
    99
 * allocate dynamic ports from. The actual range may be system dependent.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * The possible ways to connect to the host are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * listen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * </pre>
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   108
 * The "listen" action is only meaningful when used with "localhost" and
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   109
 * means the ability to bind to a specified port.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * The "resolve" action is implied when any of the other actions are present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * The action "resolve" refers to host/ip name service lookups.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * The actions string is converted to lowercase before processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * <p>As an example of the creation and meaning of SocketPermissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * note that if the following permission:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *   p1 = new SocketPermission("puffin.eng.sun.com:7777", "connect,accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * is granted to some code, it allows that code to connect to port 7777 on
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   122
 * {@code puffin.eng.sun.com}, and to accept connections on that port.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <p>Similarly, if the following permission:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *   p2 = new SocketPermission("localhost:1024-", "accept,connect,listen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * is granted to some code, it allows that code to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * accept connections on, connect to, or listen on any port between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * 1024 and 65535 on the local host.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <p>Note: Granting code permission to accept or make connections to remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * hosts may be dangerous because malevolent code can then more easily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * transfer and share confidential data among parties who may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * otherwise have access to the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * @see SocketPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * @author Marianne Mueller
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * @serial exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
public final class SocketPermission extends Permission
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   150
    implements java.io.Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private static final long serialVersionUID = -7204263841984476862L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Connect to host:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private final static int CONNECT    = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Listen on host:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private final static int LISTEN     = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Accept a connection from host:port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private final static int ACCEPT     = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Resolve DNS queries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private final static int RESOLVE    = 0x8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * No actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private final static int NONE               = 0x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * All actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private final static int ALL        = CONNECT|LISTEN|ACCEPT|RESOLVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    // various port constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private static final int PORT_MIN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private static final int PORT_MAX = 65535;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private static final int PRIV_PORT_MAX = 1023;
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   188
    private static final int DEF_EPH_LOW = 49152;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    // the actions mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private transient int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * the actions string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private String actions; // Left null as long as possible, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            // created and re-used in the getAction function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    // hostname part as it is passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private transient String hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    // the canonical name of the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    // in the case of "*.foo.com", cname is ".foo.com".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private transient String cname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    // all the IP addresses of the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private transient InetAddress[] addresses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    // true if the hostname is a wildcard (e.g. "*.sun.com")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private transient boolean wildcard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // true if we were initialized with a single numeric IP address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private transient boolean init_with_ip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    // true if this SocketPermission represents an invalid/unknown host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    // used for implies when the delayed lookup has already failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private transient boolean invalid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    // port range on host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private transient int[] portrange;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   226
    private transient boolean defaultDeny = false;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   227
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   228
    // true if this SocketPermission represents a hostname
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   229
    // that failed our reverse mapping heuristic test
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   230
    private transient boolean untrusted;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   231
    private transient boolean trusted;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   232
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   233
    // true if the sun.net.trustNameService system property is set
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   234
    private static boolean trustNameService;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   235
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   236
    private static Debug debug = null;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   237
    private static boolean debugInit = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
22633
303b60d0f31a 8033425: Delay loading of net library in PortConfig initialization (workaround for for 8033367)
michaelm
parents: 22341
diff changeset
   239
    // lazy initializer
303b60d0f31a 8033425: Delay loading of net library in PortConfig initialization (workaround for for 8033367)
michaelm
parents: 22341
diff changeset
   240
    private static class EphemeralRange {
303b60d0f31a 8033425: Delay loading of net library in PortConfig initialization (workaround for for 8033367)
michaelm
parents: 22341
diff changeset
   241
        static final int low = initEphemeralPorts("low", DEF_EPH_LOW);
303b60d0f31a 8033425: Delay loading of net library in PortConfig initialization (workaround for for 8033367)
michaelm
parents: 22341
diff changeset
   242
            static final int high = initEphemeralPorts("high", PORT_MAX);
303b60d0f31a 8033425: Delay loading of net library in PortConfig initialization (workaround for for 8033367)
michaelm
parents: 22341
diff changeset
   243
    };
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   244
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        Boolean tmp = java.security.AccessController.doPrivileged(
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   247
                new sun.security.action.GetBooleanAction("sun.net.trustNameService"));
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   248
        trustNameService = tmp.booleanValue();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   249
    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   250
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   251
    private static synchronized Debug getDebug() {
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   252
        if (!debugInit) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   253
            debug = Debug.getInstance("access");
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   254
            debugInit = true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   255
        }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   256
        return debug;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Creates a new SocketPermission object with the specified actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * The host is expressed as a DNS name, or as a numerical IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Optionally, a port or a portrange may be supplied (separated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * from the DNS name or IP address by a colon).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * To specify the local machine, use "localhost" as the <i>host</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Also note: An empty <i>host</i> String ("") is equivalent to "localhost".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * The <i>actions</i> parameter contains a comma-separated list of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * actions granted for the specified host (and port(s)). Possible actions are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * "connect", "listen", "accept", "resolve", or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * any combination of those. "resolve" is automatically added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * when any of the other three are specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Examples of SocketPermission instantiation are the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *    nr = new SocketPermission("www.catalog.com", "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *    nr = new SocketPermission("www.sun.com:80", "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *    nr = new SocketPermission("*.sun.com", "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *    nr = new SocketPermission("*.edu", "resolve");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *    nr = new SocketPermission("204.160.241.0", "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *    nr = new SocketPermission("localhost:1024-65535", "listen");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *    nr = new SocketPermission("204.160.241.0:1024-65535", "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @param host the hostname or IPaddress of the computer, optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * including a colon followed by a port or port range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param action the action string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public SocketPermission(String host, String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        super(getHost(host));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        // name initialized to getHost(host); NPE detected in getHost()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        init(getName(), getMask(action));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    SocketPermission(String host, int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        super(getHost(host));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // name initialized to getHost(host); NPE detected in getHost()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        init(getName(), mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   302
    private void setDeny() {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   303
        defaultDeny = true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   304
    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   305
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   306
    private static String getHost(String host) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (host.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            /* IPv6 literal address used in this context should follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
             * the format specified in RFC 2732;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
             * if not, we try to solve the unambiguous case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            int ind;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (host.charAt(0) != '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if ((ind = host.indexOf(':')) != host.lastIndexOf(':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    /* More than one ":", meaning IPv6 address is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                     * in RFC 2732 format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                     * We will rectify user errors for all unambiguious cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    StringTokenizer st = new StringTokenizer(host, ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    int tokens = st.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    if (tokens == 9) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        // IPv6 address followed by port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        ind = host.lastIndexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        host = "[" + host.substring(0, ind) + "]" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                            host.substring(ind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    } else if (tokens == 8 && host.indexOf("::") == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        // IPv6 address only, not followed by port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        host = "[" + host + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        // could be ambiguous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                        throw new IllegalArgumentException("Ambiguous"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                                           " hostport part");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private int[] parsePort(String port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (port == null || port.equals("") || port.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            return new int[] {PORT_MIN, PORT_MAX};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        int dash = port.indexOf('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (dash == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            int p = Integer.parseInt(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            return new int[] {p, p};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            String low = port.substring(0, dash);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            String high = port.substring(dash+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            int l,h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (low.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                l = PORT_MIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                l = Integer.parseInt(low);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (high.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                h = PORT_MAX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                h = Integer.parseInt(high);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            if (l < 0 || h < 0 || h<l)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                throw new IllegalArgumentException("invalid port range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return new int[] {l, h};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   379
     * Returns true if the permission has specified zero
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   380
     * as its value (or lower bound) signifying the ephemeral range
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   381
     */
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   382
    private boolean includesEphemerals() {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   383
        return portrange[0] == 0;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   384
    }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   385
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   386
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Initialize the SocketPermission object. We don't do any DNS lookups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * as this point, instead we hold off until the implies method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    private void init(String host, int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // Set the integer mask that represents the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if ((mask & ALL) != mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            throw new IllegalArgumentException("invalid actions mask");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // always OR in RESOLVE if we allow any of the others
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        this.mask = mask | RESOLVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        // Parse the host name.  A name has up to three components, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        // hostname, a port number, or two numbers representing a port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        // range.   "www.sun.com:8080-9090" is a valid host name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // With IPv6 an address can be 2010:836B:4179::836B:4179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        // An IPv6 address needs to be enclose in []
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // For ex: [2010:836B:4179::836B:4179]:8080-9090
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // Refer to RFC 2732 for more information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        int rb = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        int start = 0, end = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        int sep = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        String hostport = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if (host.charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            start = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            rb = host.indexOf(']');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            if (rb != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                host = host.substring(start, rb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    IllegalArgumentException("invalid host/port: "+host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            sep = hostport.indexOf(':', rb+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            start = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            sep = host.indexOf(':', rb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            end = sep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (sep != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                host = host.substring(start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (sep != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            String port = hostport.substring(sep+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                portrange = parsePort(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    IllegalArgumentException("invalid port range: "+port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            portrange = new int[] { PORT_MIN, PORT_MAX };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        hostname = host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        // is this a domain wildcard specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (host.lastIndexOf('*') > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
               IllegalArgumentException("invalid host wildcard specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        } else if (host.startsWith("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            wildcard = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (host.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                cname = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            } else if (host.startsWith("*.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                cname = host.substring(1).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
              throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
               IllegalArgumentException("invalid host wildcard specification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            if (host.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                // see if we are being initialized with an IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                char ch = host.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                if (ch == ':' || Character.digit(ch, 16) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    byte ip[] = IPAddressUtil.textToNumericFormatV4(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    if (ip == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        ip = IPAddressUtil.textToNumericFormatV6(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    if (ip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                            addresses =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                new InetAddress[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                {InetAddress.getByAddress(ip) };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                            init_with_ip = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        } catch (UnknownHostException uhe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                            // this shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                            invalid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * Convert an action string to an integer actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @param action the action string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @return the action mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    private static int getMask(String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (action == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            throw new NullPointerException("action can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (action.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            throw new IllegalArgumentException("action can't be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        int mask = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 10596
diff changeset
   504
        // Use object identity comparison against known-interned strings for
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 10596
diff changeset
   505
        // performance benefit (these values are used heavily within the JDK).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (action == SecurityConstants.SOCKET_RESOLVE_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            return RESOLVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        } else if (action == SecurityConstants.SOCKET_CONNECT_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            return CONNECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        } else if (action == SecurityConstants.SOCKET_LISTEN_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            return LISTEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        } else if (action == SecurityConstants.SOCKET_ACCEPT_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        } else if (action == SecurityConstants.SOCKET_CONNECT_ACCEPT_ACTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            return CONNECT|ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        char[] a = action.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        int i = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        while (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            // skip whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            while ((i!=-1) && ((c = a[i]) == ' ' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                               c == '\r' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                               c == '\n' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                               c == '\f' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                               c == '\t'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            // check for the known strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            int matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            if (i >= 6 && (a[i-6] == 'c' || a[i-6] == 'C') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                          (a[i-5] == 'o' || a[i-5] == 'O') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                          (a[i-4] == 'n' || a[i-4] == 'N') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                          (a[i-3] == 'n' || a[i-3] == 'N') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                          (a[i-2] == 'e' || a[i-2] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                          (a[i-1] == 'c' || a[i-1] == 'C') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                          (a[i] == 't' || a[i] == 'T'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                matchlen = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                mask |= CONNECT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            } else if (i >= 6 && (a[i-6] == 'r' || a[i-6] == 'R') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                                 (a[i-5] == 'e' || a[i-5] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                                 (a[i-4] == 's' || a[i-4] == 'S') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                 (a[i-3] == 'o' || a[i-3] == 'O') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                 (a[i-2] == 'l' || a[i-2] == 'L') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                 (a[i-1] == 'v' || a[i-1] == 'V') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                 (a[i] == 'e' || a[i] == 'E'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                matchlen = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                mask |= RESOLVE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            } else if (i >= 5 && (a[i-5] == 'l' || a[i-5] == 'L') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                                 (a[i-4] == 'i' || a[i-4] == 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                 (a[i-3] == 's' || a[i-3] == 'S') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                                 (a[i-2] == 't' || a[i-2] == 'T') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                 (a[i-1] == 'e' || a[i-1] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                                 (a[i] == 'n' || a[i] == 'N'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                matchlen = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                mask |= LISTEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            } else if (i >= 5 && (a[i-5] == 'a' || a[i-5] == 'A') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                                 (a[i-4] == 'c' || a[i-4] == 'C') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                                 (a[i-3] == 'c' || a[i-3] == 'C') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                 (a[i-2] == 'e' || a[i-2] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                 (a[i-1] == 'p' || a[i-1] == 'P') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                                 (a[i] == 't' || a[i] == 'T'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                matchlen = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                mask |= ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                // parse error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        "invalid permission: " + action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            // make sure we didn't just match the tail of a word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            // like "ackbarfaccept".  Also, skip to the comma.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            boolean seencomma = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            while (i >= matchlen && !seencomma) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                switch(a[i-matchlen]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    seencomma = true;
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 10596
diff changeset
   593
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                case ' ': case '\r': case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                case '\f': case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                            "invalid permission: " + action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            // point i at the location of the comma minus one (or -1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            i -= matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   611
    private boolean isUntrusted()
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   612
        throws UnknownHostException
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   613
    {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   614
        if (trusted) return false;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   615
        if (invalid || untrusted) return true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   616
        try {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   617
            if (!trustNameService && (defaultDeny ||
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   618
                sun.net.www.URLConnection.isProxiedHost(hostname))) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   619
                if (this.cname == null) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   620
                    this.getCanonName();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   621
                }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   622
                if (!match(cname, hostname)) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   623
                    // Last chance
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   624
                    if (!authorized(hostname, addresses[0].getAddress())) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   625
                        untrusted = true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   626
                        Debug debug = getDebug();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   627
                        if (debug != null && Debug.isOn("failure")) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   628
                            debug.println("socket access restriction: proxied host " + "(" + addresses[0] + ")" + " does not match " + cname + " from reverse lookup");
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   629
                        }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   630
                        return true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   631
                    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   632
                }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   633
                trusted = true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   634
            }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   635
        } catch (UnknownHostException uhe) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   636
            invalid = true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   637
            throw uhe;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   638
        }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   639
        return false;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   640
    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   641
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * attempt to get the fully qualified domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    void getCanonName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        throws UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    {
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   649
        if (cname != null || invalid || untrusted) return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        // attempt to get the canonical name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            // first get the IP addresses if we don't have them yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            // this is because we need the IP address to then get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            // FQDN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            if (addresses == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                getIP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            // we have to do this check, otherwise we might not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            // get the fully qualified domain name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            if (init_with_ip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                cname = addresses[0].getHostName(false).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
             cname = InetAddress.getByName(addresses[0].getHostAddress()).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                                              getHostName(false).toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        } catch (UnknownHostException uhe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            invalid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            throw uhe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   675
    private transient String cdomain, hdomain;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   676
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   677
    private boolean match(String cname, String hname) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   678
        String a = cname.toLowerCase();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   679
        String b = hname.toLowerCase();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   680
        if (a.startsWith(b)  &&
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   681
            ((a.length() == b.length()) || (a.charAt(b.length()) == '.')))
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   682
            return true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   683
        if (cdomain == null) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   684
            cdomain = RegisteredDomain.getRegisteredDomain(a);
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   685
        }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   686
        if (hdomain == null) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   687
            hdomain = RegisteredDomain.getRegisteredDomain(b);
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   688
        }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   689
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   690
        return cdomain.length() != 0 && hdomain.length() != 0
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   691
                        && cdomain.equals(hdomain);
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   692
    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   693
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   694
    private boolean authorized(String cname, byte[] addr) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   695
        if (addr.length == 4)
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   696
            return authorizedIPv4(cname, addr);
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   697
        else if (addr.length == 16)
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   698
            return authorizedIPv6(cname, addr);
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   699
        else
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   700
            return false;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   701
    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   702
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   703
    private boolean authorizedIPv4(String cname, byte[] addr) {
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   704
        String authHost = "";
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   705
        InetAddress auth;
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   706
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   707
        try {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   708
            authHost = "auth." +
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   709
                        (addr[3] & 0xff) + "." + (addr[2] & 0xff) + "." +
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   710
                        (addr[1] & 0xff) + "." + (addr[0] & 0xff) +
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   711
                        ".in-addr.arpa";
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   712
            // Following check seems unnecessary
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   713
            // auth = InetAddress.getAllByName0(authHost, false)[0];
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   714
            authHost = hostname + '.' + authHost;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   715
            auth = InetAddress.getAllByName0(authHost, false)[0];
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   716
            if (auth.equals(InetAddress.getByAddress(addr))) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   717
                return true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   718
            }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   719
            Debug debug = getDebug();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   720
            if (debug != null && Debug.isOn("failure")) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   721
                debug.println("socket access restriction: IP address of " + auth + " != " + InetAddress.getByAddress(addr));
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   722
            }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   723
        } catch (UnknownHostException uhe) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   724
            Debug debug = getDebug();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   725
            if (debug != null && Debug.isOn("failure")) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   726
                debug.println("socket access restriction: forward lookup failed for " + authHost);
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   727
            }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   728
        }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   729
        return false;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   730
    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   731
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   732
    private boolean authorizedIPv6(String cname, byte[] addr) {
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   733
        String authHost = "";
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   734
        InetAddress auth;
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   735
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   736
        try {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 24685
diff changeset
   737
            StringBuilder sb = new StringBuilder(39);
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   738
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   739
            for (int i = 15; i >= 0; i--) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   740
                sb.append(Integer.toHexString(((addr[i]) & 0x0f)));
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   741
                sb.append('.');
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   742
                sb.append(Integer.toHexString(((addr[i] >> 4) & 0x0f)));
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   743
                sb.append('.');
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   744
            }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   745
            authHost = "auth." + sb.toString() + "IP6.ARPA";
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   746
            //auth = InetAddress.getAllByName0(authHost, false)[0];
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   747
            authHost = hostname + '.' + authHost;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   748
            auth = InetAddress.getAllByName0(authHost, false)[0];
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   749
            if (auth.equals(InetAddress.getByAddress(addr)))
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   750
                return true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   751
            Debug debug = getDebug();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   752
            if (debug != null && Debug.isOn("failure")) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   753
                debug.println("socket access restriction: IP address of " + auth + " != " + InetAddress.getByAddress(addr));
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   754
            }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   755
        } catch (UnknownHostException uhe) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   756
            Debug debug = getDebug();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   757
            if (debug != null && Debug.isOn("failure")) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   758
                debug.println("socket access restriction: forward lookup failed for " + authHost);
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   759
            }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   760
        }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   761
        return false;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   762
    }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   763
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   764
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * get IP addresses. Sets invalid to true if we can't get them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    void getIP()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        throws UnknownHostException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (addresses != null || wildcard || invalid) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            // now get all the IP addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            String host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            if (getName().charAt(0) == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                // Literal IPv6 address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                host = getName().substring(1, getName().indexOf(']'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            } else {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23720
diff changeset
   781
                int i = getName().indexOf(':');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                if (i == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    host = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    host = getName().substring(0,i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            addresses =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                new InetAddress[] {InetAddress.getAllByName0(host, false)[0]};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        } catch (UnknownHostException uhe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            invalid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            throw uhe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }  catch (IndexOutOfBoundsException iobe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            invalid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            throw new UnknownHostException(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * Checks if this socket permission object "implies" the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * More specifically, this method first ensures that all of the following
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   806
     * are true (and returns false if any of them are not):
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * <ul>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   808
     * <li> <i>p</i> is an instanceof SocketPermission,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * <li> <i>p</i>'s actions are a proper subset of this
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   810
     * object's actions, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * <li> <i>p</i>'s port range is included in this port range. Note:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   812
     * port range is ignored when p only contains the action, 'resolve'.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   815
     * Then {@code implies} checks each of the following, in order,
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   816
     * and for each returns true if the stated condition is true:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * <li> If this object was initialized with a single IP address and one of <i>p</i>'s
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   819
     * IP addresses is equal to this object's IP address.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * <li>If this object is a wildcard domain (such as *.sun.com), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * <i>p</i>'s canonical name (the name without any preceding *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * ends with this object's canonical host name. For example, *.sun.com
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   823
     * implies *.eng.sun.com.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * <li>If this object was not initialized with a single IP address, and one of this
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   825
     * object's IP addresses equals one of <i>p</i>'s IP addresses.
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 19069
diff changeset
   826
     * <li>If this canonical name equals <i>p</i>'s canonical name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
   829
     * If none of the above are true, {@code implies} returns false.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @return true if the specified permission is implied by this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        int i,j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (!(p instanceof SocketPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        if (p == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        SocketPermission that = (SocketPermission) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        return ((this.mask & that.mask) == that.mask) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                        impliesIgnoreMask(that);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Checks if the incoming Permission's action are a proper subset of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * the this object's actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * Check, in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * <li> Checks that "p" is an instanceof a SocketPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * <li> Checks that "p"'s actions are a proper subset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * current object's actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * <li> Checks that "p"'s port range is included in this port range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * <li> If this object was initialized with an IP address, checks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *      one of "p"'s IP addresses is equal to this object's IP address.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * <li> If either object is a wildcard domain (i.e., "*.sun.com"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *      attempt to match based on the wildcard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * <li> If this object was not initialized with an IP address, attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *      to find a match based on the IP addresses in both objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * <li> Attempt to match on the canonical hostnames of both objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * </ul>
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
   868
     * @param that the incoming permission request
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @return true if "permission" is a proper subset of the current object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    boolean impliesIgnoreMask(SocketPermission that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        int i,j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        if ((that.mask & RESOLVE) != that.mask) {
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   877
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   878
            // check simple port range
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            if ((that.portrange[0] < this.portrange[0]) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                    (that.portrange[1] > this.portrange[1])) {
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   881
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   882
                // if either includes the ephemeral range, do full check
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   883
                if (this.includesEphemerals() || that.includesEphemerals()) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   884
                    if (!inRange(this.portrange[0], this.portrange[1],
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   885
                                     that.portrange[0], that.portrange[1]))
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   886
                    {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   887
                                return false;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   888
                    }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   889
                } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                    return false;
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
   891
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        // allow a "*" wildcard to always match anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        if (this.wildcard && "".equals(this.cname))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        // return if either one of these NetPerm objects are invalid...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (this.invalid || that.invalid) {
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   901
            return compareHostnames(that);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            if (this.init_with_ip) { // we only check IP addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                if (that.wildcard)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                if (that.init_with_ip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    return (this.addresses[0].equals(that.addresses[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                    if (that.addresses == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                        that.getIP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                    for (i=0; i < that.addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                        if (this.addresses[0].equals(that.addresses[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                // since "this" was initialized with an IP address, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                // don't check any other cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            // check and see if we have any wildcards...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            if (this.wildcard || that.wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                // if they are both wildcards, return true iff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                // that's cname ends with this cname (i.e., *.sun.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                // implies *.eng.sun.com)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                if (this.wildcard && that.wildcard)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    return (that.cname.endsWith(this.cname));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                // a non-wildcard can't imply a wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                if (that.wildcard)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                // this is a wildcard, lets see if that's cname ends with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                // it...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                if (that.cname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    that.getCanonName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                return (that.cname.endsWith(this.cname));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            // comapare IP addresses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            if (this.addresses == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                this.getIP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            if (that.addresses == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                that.getIP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   954
            if (!(that.init_with_ip && this.isUntrusted())) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   955
                for (j = 0; j < this.addresses.length; j++) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   956
                    for (i=0; i < that.addresses.length; i++) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   957
                        if (this.addresses[j].equals(that.addresses[i]))
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   958
                            return true;
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   959
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                }
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   961
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   962
                // XXX: if all else fails, compare hostnames?
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   963
                // Do we really want this?
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   964
                if (this.cname == null) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   965
                    this.getCanonName();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   966
                }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   967
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   968
                if (that.cname == null) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   969
                    that.getCanonName();
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   970
                }
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   971
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   972
                return (this.cname.equalsIgnoreCase(that.cname));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        } catch (UnknownHostException uhe) {
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   976
            return compareHostnames(that);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        // make sure the first thing that is done here is to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        // false. If not, uncomment the return false in the above catch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   985
    private boolean compareHostnames(SocketPermission that) {
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
   986
        // we see if the original names/IPs passed in were equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        String thisHost = hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        String thatHost = that.hostname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
10124
a00341fc41db 7021280: SocketPermission should accept wildcards
chegar
parents: 9775
diff changeset
   991
        if (thisHost == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            return false;
10124
a00341fc41db 7021280: SocketPermission should accept wildcards
chegar
parents: 9775
diff changeset
   993
        } else if (this.wildcard) {
a00341fc41db 7021280: SocketPermission should accept wildcards
chegar
parents: 9775
diff changeset
   994
            final int cnameLength = this.cname.length();
a00341fc41db 7021280: SocketPermission should accept wildcards
chegar
parents: 9775
diff changeset
   995
            return thatHost.regionMatches(true,
a00341fc41db 7021280: SocketPermission should accept wildcards
chegar
parents: 9775
diff changeset
   996
                                          (thatHost.length() - cnameLength),
a00341fc41db 7021280: SocketPermission should accept wildcards
chegar
parents: 9775
diff changeset
   997
                                          this.cname, 0, cnameLength);
a00341fc41db 7021280: SocketPermission should accept wildcards
chegar
parents: 9775
diff changeset
   998
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            return thisHost.equalsIgnoreCase(thatHost);
10124
a00341fc41db 7021280: SocketPermission should accept wildcards
chegar
parents: 9775
diff changeset
  1000
        }
9775
1b128726e887 7042550: Reintegrate 6569621
michaelm
parents: 9550
diff changeset
  1001
    }
9550
c3a275ce56d3 7041044: InetAddress.getByName(String,InetAddress) added in error
michaelm
parents: 9544
diff changeset
  1002
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * Checks two SocketPermission objects for equality.
23720
7d5147c21927 8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents: 22633
diff changeset
  1005
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * @return true if <i>obj</i> is a SocketPermission, and has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *  same hostname, port range, and actions as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *  SocketPermission object. However, port range will be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *  in the comparison if <i>obj</i> only contains the action, 'resolve'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        if (! (obj instanceof SocketPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        SocketPermission that = (SocketPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        //this is (overly?) complex!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        // check the mask first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        if (this.mask != that.mask) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        if ((that.mask & RESOLVE) != that.mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            // now check the port range...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            if ((this.portrange[0] != that.portrange[0]) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                (this.portrange[1] != that.portrange[1])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        // short cut. This catches:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        //  "crypto" equal to "crypto", or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        // "1.2.3.4" equal to "1.2.3.4.", or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        //  "*.edu" equal to "*.edu", but it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        //  does not catch "crypto" equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        // "crypto.eng.sun.com".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        if (this.getName().equalsIgnoreCase(that.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        // we now attempt to get the Canonical (FQDN) name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        // compare that. If this fails, about all we can do is return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        // false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            this.getCanonName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            that.getCanonName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        } catch (UnknownHostException uhe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        if (this.invalid || that.invalid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        if (this.cname != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            return this.cname.equalsIgnoreCase(that.cname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
         * If this SocketPermission was initialized with an IP address
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
         * or a wildcard, use getName().hashCode(), otherwise use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
         * the hashCode() of the host name returned from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         * java.net.InetAddress.getHostName method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        if (init_with_ip || wildcard) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            return this.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            getCanonName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        } catch (UnknownHostException uhe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        if (invalid || cname == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            return this.getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            return this.cname.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * Return the current action mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * @return the actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    int getMask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * Returns the "canonical string representation" of the actions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * specified mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * Always returns present actions in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * connect, listen, accept, resolve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @param mask a specific integer action mask to translate into a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * @return the canonical string representation of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     */
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  1116
    private static String getActions(int mask) {
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  1117
        StringJoiner sj = new StringJoiner(",");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        if ((mask & CONNECT) == CONNECT) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  1119
            sj.add("connect");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        if ((mask & LISTEN) == LISTEN) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  1122
            sj.add("listen");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        if ((mask & ACCEPT) == ACCEPT) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  1125
            sj.add("accept");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
        if ((mask & RESOLVE) == RESOLVE) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  1128
            sj.add("resolve");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
  1130
        return sj.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * Returns the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * Always returns present actions in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * connect, listen, accept, resolve.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    public String getActions()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            actions = getActions(this.mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * Returns a new PermissionCollection object for storing SocketPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * SocketPermission objects must be stored in a manner that allows them
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * to be inserted into the collection in any order, but that also enables the
19069
1d9cb0d080e3 8021833: javadoc cleanup in java.net
juh
parents: 14342
diff changeset
  1154
     * PermissionCollection {@code implies}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * method to be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * @return a new PermissionCollection object suitable for storing SocketPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        return new SocketPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * WriteObject is called to save the state of the SocketPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * to a stream. The actions are serialized, and the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * takes care of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        // Write out the actions. The superclass takes care of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        // call getActions to make sure actions field is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * readObject is called to restore the state of the SocketPermission from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    private synchronized void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        // Read in the action, then initialize the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        init(getName(),getMask(actions));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1191
    /**
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1192
     * Check the system/security property for the ephemeral port range
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1193
     * for this system. The suffix is either "high" or "low"
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1194
     */
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1195
    private static int initEphemeralPorts(String suffix, int defval) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1196
        return AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26219
diff changeset
  1197
            new PrivilegedAction<>(){
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1198
                public Integer run() {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1199
                    int val = Integer.getInteger(
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1200
                            "jdk.net.ephemeralPortRange."+suffix, -1
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1201
                    );
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1202
                    if (val != -1) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1203
                        return val;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1204
                    } else {
22341
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents: 22339
diff changeset
  1205
                        return suffix.equals("low") ?
4689530d03b9 8028293: Check local configuration for actual ephemeral port range
michaelm
parents: 22339
diff changeset
  1206
                            PortConfig.getLower() : PortConfig.getUpper();
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1207
                    }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1208
                }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1209
            }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1210
        );
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1211
    }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1212
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1213
    /**
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1214
     * Check if the target range is within the policy range
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1215
     * together with the ephemeral range for this platform
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1216
     * (if policy includes ephemeral range)
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1217
     */
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1218
    private static boolean inRange(
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1219
        int policyLow, int policyHigh, int targetLow, int targetHigh
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1220
    )
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1221
    {
22633
303b60d0f31a 8033425: Delay loading of net library in PortConfig initialization (workaround for for 8033367)
michaelm
parents: 22341
diff changeset
  1222
        final int ephemeralLow = EphemeralRange.low;
303b60d0f31a 8033425: Delay loading of net library in PortConfig initialization (workaround for for 8033367)
michaelm
parents: 22341
diff changeset
  1223
        final int ephemeralHigh = EphemeralRange.high;
303b60d0f31a 8033425: Delay loading of net library in PortConfig initialization (workaround for for 8033367)
michaelm
parents: 22341
diff changeset
  1224
22339
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1225
        if (targetLow == 0) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1226
            // check policy includes ephemeral range
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1227
            if (!inRange(policyLow, policyHigh, ephemeralLow, ephemeralHigh)) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1228
                return false;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1229
            }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1230
            if (targetHigh == 0) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1231
                // nothing left to do
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1232
                return true;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1233
            }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1234
            // continue check with first real port number
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1235
            targetLow = 1;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1236
        }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1237
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1238
        if (policyLow == 0 && policyHigh == 0) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1239
            // ephemeral range only
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1240
            return targetLow >= ephemeralLow && targetHigh <= ephemeralHigh;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1241
        }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1242
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1243
        if (policyLow != 0) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1244
            // simple check of policy only
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1245
            return targetLow >= policyLow && targetHigh <= policyHigh;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1246
        }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1247
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1248
        // policyLow == 0 which means possibly two ranges to check
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1249
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1250
        // first check if policy and ephem range overlap/contiguous
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1251
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1252
        if (policyHigh >= ephemeralLow - 1) {
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1253
            return targetHigh <= ephemeralHigh;
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1254
        }
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1255
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1256
        // policy and ephem range do not overlap
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1257
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1258
        // target range must lie entirely inside policy range or eph range
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1259
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1260
        return  (targetLow <= policyHigh && targetHigh <= policyHigh) ||
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1261
                (targetLow >= ephemeralLow && targetHigh <= ephemeralHigh);
e91bfaf4360d 8011786: Better applet networking
michaelm
parents: 21334
diff changeset
  1262
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    public String toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        StringBuffer s = new StringBuffer(super.toString() + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            "cname = " + cname + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            "wildcard = " + wildcard + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            "invalid = " + invalid + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            "portrange = " + portrange[0] + "," + portrange[1] + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        if (addresses != null) for (int i=0; i<addresses.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            s.append( addresses[i].getHostAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            s.append("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            s.append("(no addresses)\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        return s.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        SocketPermission this_ = new SocketPermission(args[0], "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        SocketPermission that_ = new SocketPermission(args[1], "connect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        System.out.println("this.implies(that) = " + this_.implies(that_));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        System.out.println("this = "+this_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        System.out.println("that = "+that_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        SocketPermissionCollection nps = new SocketPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        nps.add(this_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        nps.add(new SocketPermission("www-leland.stanford.edu","connect"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        nps.add(new SocketPermission("www-sun.com","connect"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        System.out.println("nps.implies(that) = " + nps.implies(that_));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
if (init'd with IP, key is IP as string)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
if wildcard, its the wild card
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
else its the cname?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
 * @see java.security.PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
final class SocketPermissionCollection extends PermissionCollection
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
  1320
    implements Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    // Not serialized; see serialization section at end of class
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10124
diff changeset
  1323
    private transient List<SocketPermission> perms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * Create an empty SocketPermissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    public SocketPermissionCollection() {
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26219
diff changeset
  1331
        perms = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * Adds a permission to the SocketPermissions. The key for the hash is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * the name in the case of wildcards, or all the IP addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     *                                       SocketPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * @exception SecurityException - if this SocketPermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     *                                has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
  1346
    public void add(Permission permission) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        if (! (permission instanceof SocketPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
            throw new SecurityException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                "attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        // optimization to ensure perms most likely to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        // show up early (4301064)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        synchronized (this) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10124
diff changeset
  1357
            perms.add(0, (SocketPermission)permission);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * Check and see if this collection of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
  1365
     * @param permission the Permission object to compare
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * the collection, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    public boolean implies(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        if (! (permission instanceof SocketPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        SocketPermission np = (SocketPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        int desired = np.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        int effective = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        int needed = desired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            int len = perms.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            //System.out.println("implies "+np);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
            for (int i = 0; i < len; i++) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10124
diff changeset
  1386
                SocketPermission x = perms.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                //System.out.println("  trying "+x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(np)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    effective |=  x.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                    if ((effective & desired) == desired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                    needed = (desired ^ effective);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * Returns an enumeration of all the SocketPermission objects in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * @return an enumeration of all the SocketPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10124
diff changeset
  1406
    @SuppressWarnings("unchecked")
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10124
diff changeset
  1407
    public Enumeration<Permission> elements() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        // Convert Iterator into Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        synchronized (this) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10124
diff changeset
  1410
            return Collections.enumeration((List<Permission>)(List)perms);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    private static final long serialVersionUID = 2787186408602843674L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    // The SocketPermissions for this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    // @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    // private Vector permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * @serialField permissions java.util.Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     *     A list of the SocketPermissions for this set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        new ObjectStreamField("permissions", Vector.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * @serialData "permissions" field (a Vector containing the SocketPermissions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * Writes the contents of the perms field out as a Vector for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * serialization compatibility with earlier releases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        // Write out Vector
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10124
diff changeset
  1444
        Vector<SocketPermission> permissions = new Vector<>(perms.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            permissions.addAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * Reads in a Vector of SocketPermissions and saves them in the perms field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     */
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
  1458
    private void readObject(ObjectInputStream in)
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
  1459
        throws IOException, ClassNotFoundException
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 13795
diff changeset
  1460
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        // Don't call in.defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        // Get the one we want
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10124
diff changeset
  1467
        @SuppressWarnings("unchecked")
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 10124
diff changeset
  1468
        Vector<SocketPermission> permissions = (Vector<SocketPermission>)gfields.get("permissions", null);
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 26219
diff changeset
  1469
        perms = new ArrayList<>(permissions.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        perms.addAll(permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
}