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