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