jdk/src/share/classes/javax/security/auth/kerberos/ServicePermission.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
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 2000-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 javax.security.auth.kerberos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class is used to protect Kerberos services and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * credentials necessary to access those services. There is a one to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * one mapping of a service principal and the credentials necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * to access the service. Therefore granting access to a service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * principal implicitly grants access to the credential necessary to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * establish a security context with the service principal. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * applies regardless of whether the credentials are in a cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * or acquired via an exchange with the KDC. The credential can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * be either a ticket granting ticket, a service ticket or a secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * key from a key table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * A ServicePermission contains a service principal name and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a list of actions which specify the context the credential can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * used within.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The service principal name is the canonical name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>KereberosPrincipal</code> supplying the service, that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the KerberosPrincipal represents a Kerberos service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * principal. This name is treated in a case sensitive manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * An asterisk may appear by itself, to signify any service principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Granting this permission implies that the caller can use a cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * credential (TGT, service ticket or secret key) within the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * designated by the action. In the case of the TGT, granting this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * permission also implies that the TGT can be obtained by an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Authentication Service exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The possible actions are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *    initiate -              allow the caller to use the credential to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *                            initiate a security context with a service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *                            principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *    accept -                allow the caller to use the credential to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *                            accept security context as a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *                            principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * For example, to specify the permission to access to the TGT to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * initiate a security context the permission is constructed as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     ServicePermission("krbtgt/EXAMPLE.COM@EXAMPLE.COM", "initiate");
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
 * To obtain a service ticket to initiate a context with the "host"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * service the permission is constructed as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     ServicePermission("host/foo.example.com@EXAMPLE.COM", "initiate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * For a Kerberized server the action is "accept". For example, the permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * necessary to access and use the secret key of the  Kerberized "host"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * service (telnet and the likes)  would be constructed as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *     ServicePermission("host/foo.example.com@EXAMPLE.COM", "accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
public final class ServicePermission extends Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private static final long serialVersionUID = -1227585031618624935L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Initiate a security context to the specified service
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private final static int INITIATE   = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Accept a security context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private final static int ACCEPT     = 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * All actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private final static int ALL        = INITIATE|ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * No actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private final static int NONE    = 0x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    // the actions mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private transient int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * the actions string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private String actions; // Left null as long as possible, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                            // created and re-used in the getAction function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Create a new <code>ServicePermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * with the specified <code>servicePrincipal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * and <code>action</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @param servicePrincipal the name of the service principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * An asterisk may appear by itself, to signify any service principal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param action the action string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public ServicePermission(String servicePrincipal, String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        super(servicePrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        init(servicePrincipal, getMask(action));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Initialize the ServicePermission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private void init(String servicePrincipal, int mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (servicePrincipal == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                throw new NullPointerException("service principal can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if ((mask & ALL) != mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new IllegalArgumentException("invalid actions mask");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        this.mask = mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Checks if this Kerberos service permission object "implies" the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * If none of the above are true, <code>implies</code> returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return true if the specified permission is implied by this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (!(p instanceof ServicePermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        ServicePermission that = (ServicePermission) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return ((this.mask & that.mask) == that.mask) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            impliesIgnoreMask(that);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    boolean impliesIgnoreMask(ServicePermission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return ((this.getName().equals("*")) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                this.getName().equals(p.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Checks two ServicePermission objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return true if <i>obj</i> is a ServicePermission, and has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *  same service principal, and actions as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * ServicePermission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (! (obj instanceof ServicePermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        ServicePermission that = (ServicePermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return ((this.mask & that.mask) == that.mask) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            this.getName().equals(that.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return (getName().hashCode() ^ mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Returns the "canonical string representation" of the actions in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * specified mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Always returns present actions in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * initiate, accept.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param mask a specific integer action mask to translate into a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return the canonical string representation of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private static String getActions(int mask)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        boolean comma = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if ((mask & INITIATE) == INITIATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (comma) sb.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            else comma = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            sb.append("initiate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if ((mask & ACCEPT) == ACCEPT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (comma) sb.append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            else comma = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            sb.append("accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Returns the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Always returns present actions in the following order:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * initiate, accept.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public String getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            actions = getActions(this.mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Returns a PermissionCollection object for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * ServicePermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * ServicePermission objects must be stored in a manner that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * allows them to be inserted into the collection in any order, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * that also enables the PermissionCollection implies method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return a new PermissionCollection object suitable for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * ServicePermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return new KrbServicePermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Return the current action mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @return the actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    int getMask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Convert an action string to an integer actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param action the action string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @return the action mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private static int getMask(String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (action == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            throw new NullPointerException("action can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (action.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            throw new IllegalArgumentException("action can't be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int mask = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        char[] a = action.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int i = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        while (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            // skip whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            while ((i!=-1) && ((c = a[i]) == ' ' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                               c == '\r' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                               c == '\n' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                               c == '\f' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                               c == '\t'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // check for the known strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            int matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            if (i >= 7 && (a[i-7] == 'i' || a[i-7] == 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                          (a[i-6] == 'n' || a[i-6] == 'N') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                          (a[i-5] == 'i' || a[i-5] == 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                          (a[i-4] == 't' || a[i-4] == 'T') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                          (a[i-3] == 'i' || a[i-3] == 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                          (a[i-2] == 'a' || a[i-2] == 'A') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                          (a[i-1] == 't' || a[i-1] == 'T') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                          (a[i] == 'e' || a[i] == 'E'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                matchlen = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                mask |= INITIATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            } else if (i >= 5 && (a[i-5] == 'a' || a[i-5] == 'A') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                 (a[i-4] == 'c' || a[i-4] == 'C') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                 (a[i-3] == 'c' || a[i-3] == 'C') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                 (a[i-2] == 'e' || a[i-2] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                 (a[i-1] == 'p' || a[i-1] == 'P') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                 (a[i] == 't' || a[i] == 'T'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                matchlen = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                mask |= ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                // parse error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        "invalid permission: " + action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            // make sure we didn't just match the tail of a word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            // like "ackbarfaccept".  Also, skip to the comma.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            boolean seencomma = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            while (i >= matchlen && !seencomma) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                switch(a[i-matchlen]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    seencomma = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    /*FALLTHROUGH*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                case ' ': case '\r': case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                case '\f': case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                            "invalid permission: " + action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            // point i at the location of the comma minus one (or -1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            i -= matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * WriteObject is called to save the state of the ServicePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * to a stream. The actions are serialized, and the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * takes care of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // Write out the actions. The superclass takes care of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        // call getActions to make sure actions field is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * readObject is called to restore the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * ServicePermission from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // Read in the action, then initialize the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        init(getName(),getMask(actions));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
      public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
      ServicePermission this_ =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
      new ServicePermission(args[0], "accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
      ServicePermission that_ =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
      new ServicePermission(args[1], "accept,initiate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
      System.out.println("this.implies(that) = " + this_.implies(that_));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
      System.out.println("this = "+this_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
      System.out.println("that = "+that_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
      KrbServicePermissionCollection nps =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
      new KrbServicePermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
      nps.add(this_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
      nps.add(new ServicePermission("nfs/example.com@EXAMPLE.COM",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
      "accept"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
      nps.add(new ServicePermission("host/example.com@EXAMPLE.COM",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
      "initiate"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
      System.out.println("nps.implies(that) = " + nps.implies(that_));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
      Enumeration e = nps.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
      while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
      ServicePermission x =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
      (ServicePermission) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
      System.out.println("nps.e = " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
final class KrbServicePermissionCollection extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    // Not serialized; see serialization section at end of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private transient List<Permission> perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public KrbServicePermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        perms = new ArrayList<Permission>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Check and see if this collection of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param p the Permission object to compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * the collection, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if (! (permission instanceof ServicePermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        ServicePermission np = (ServicePermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        int desired = np.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        int effective = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        int needed = desired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            int len = perms.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // need to deal with the case where the needed permission has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            // more than one action and the collection has individual permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            // that sum up to the needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                ServicePermission x = (ServicePermission) perms.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                //System.out.println("  trying "+x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(np)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    effective |=  x.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    if ((effective & desired) == desired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    needed = (desired ^ effective);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Adds a permission to the ServicePermissions. The key for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * the hash is the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *                                       ServicePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @exception SecurityException - if this PermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *                                has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public void add(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (! (permission instanceof ServicePermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            perms.add(0, permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * Returns an enumeration of all the ServicePermission objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * in the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @return an enumeration of all the ServicePermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    public Enumeration<Permission> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        // Convert Iterator into Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            return Collections.enumeration(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    private static final long serialVersionUID = -4118834211490102011L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    // private Vector permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @serialField permissions java.util.Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *     A list of ServicePermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        new ObjectStreamField("permissions", Vector.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @serialData "permissions" field (a Vector containing the ServicePermissions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Writes the contents of the perms field out as a Vector for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * serialization compatibility with earlier releases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        // Write out Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        Vector<Permission> permissions = new Vector<Permission>(perms.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            permissions.addAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * Reads in a Vector of ServicePermissions and saves them in the perms field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    private void readObject(ObjectInputStream in) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        // Get the one we want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        Vector<Permission> permissions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                (Vector<Permission>)gfields.get("permissions", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        perms = new ArrayList<Permission>(permissions.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        perms.addAll(permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
}