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