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