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