jdk/src/share/classes/javax/security/auth/kerberos/ServicePermission.java
author juh
Tue, 16 Jul 2013 12:19:41 -0700
changeset 18830 90956ead732f
parent 14342 8435a30053c1
child 21955 abc02575919c
permissions -rw-r--r--
8020557: javadoc cleanup in javax.security Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package 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
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
    53
 * {@code KereberosPrincipal} supplying the service, that is
2
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
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   138
     * Create a new {@code ServicePermission}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   139
     * with the specified {@code servicePrincipal}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   140
     * and {@code action}.
2
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>
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
   172
     * If none of the above are true, {@code implies} returns false.
2
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
    public String getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            actions = getActions(this.mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
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
     * Returns a PermissionCollection object for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * ServicePermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * ServicePermission objects must be stored in a manner that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * allows them to be inserted into the collection in any order, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * that also enables the PermissionCollection implies method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @return a new PermissionCollection object suitable for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * ServicePermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return new KrbServicePermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Return the current action mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @return the actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    int getMask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Convert an action string to an integer actions mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param action the action string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return the action mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    private static int getMask(String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (action == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            throw new NullPointerException("action can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (action.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            throw new IllegalArgumentException("action can't be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        int mask = NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        char[] a = action.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        int i = a.length - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (i < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        while (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // skip whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            while ((i!=-1) && ((c = a[i]) == ' ' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                               c == '\r' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                               c == '\n' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                               c == '\f' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                               c == '\t'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // check for the known strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            int matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            if (i >= 7 && (a[i-7] == 'i' || a[i-7] == 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                          (a[i-6] == 'n' || a[i-6] == 'N') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                          (a[i-5] == 'i' || a[i-5] == 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                          (a[i-4] == 't' || a[i-4] == 'T') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                          (a[i-3] == 'i' || a[i-3] == 'I') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                          (a[i-2] == 'a' || a[i-2] == 'A') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                          (a[i-1] == 't' || a[i-1] == 'T') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                          (a[i] == 'e' || a[i] == 'E'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                matchlen = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                mask |= INITIATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            } else if (i >= 5 && (a[i-5] == 'a' || a[i-5] == 'A') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                 (a[i-4] == 'c' || a[i-4] == 'C') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                 (a[i-3] == 'c' || a[i-3] == 'C') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                 (a[i-2] == 'e' || a[i-2] == 'E') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                 (a[i-1] == 'p' || a[i-1] == 'P') &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                 (a[i] == 't' || a[i] == 'T'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                matchlen = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                mask |= ACCEPT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                // parse error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        "invalid permission: " + action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            // make sure we didn't just match the tail of a word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            // like "ackbarfaccept".  Also, skip to the comma.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            boolean seencomma = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            while (i >= matchlen && !seencomma) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                switch(a[i-matchlen]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    seencomma = true;
11131
27747ee5a62a 7116857: Warnings in javax.security and some sun.misc
weijun
parents: 10709
diff changeset
   368
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                case ' ': case '\r': case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                case '\f': case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                            "invalid permission: " + action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                i--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            // point i at the location of the comma minus one (or -1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            i -= matchlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * WriteObject is called to save the state of the ServicePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * to a stream. The actions are serialized, and the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * takes care of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // Write out the actions. The superclass takes care of the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        // call getActions to make sure actions field is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (actions == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * readObject is called to restore the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * ServicePermission from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // Read in the action, then initialize the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        init(getName(),getMask(actions));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
      public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
      ServicePermission this_ =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
      new ServicePermission(args[0], "accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
      ServicePermission that_ =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
      new ServicePermission(args[1], "accept,initiate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
      System.out.println("this.implies(that) = " + this_.implies(that_));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
      System.out.println("this = "+this_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
      System.out.println("that = "+that_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
      KrbServicePermissionCollection nps =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
      new KrbServicePermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
      nps.add(this_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
      nps.add(new ServicePermission("nfs/example.com@EXAMPLE.COM",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
      "accept"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
      nps.add(new ServicePermission("host/example.com@EXAMPLE.COM",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
      "initiate"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
      System.out.println("nps.implies(that) = " + nps.implies(that_));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
      Enumeration e = nps.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
      while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
      ServicePermission x =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
      (ServicePermission) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
      System.out.println("nps.e = " + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    */
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
final class KrbServicePermissionCollection extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    // Not serialized; see serialization section at end of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private transient List<Permission> perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    public KrbServicePermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        perms = new ArrayList<Permission>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Check and see if this collection of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 11131
diff changeset
   467
     * @param permission the Permission object to compare
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * the collection, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (! (permission instanceof ServicePermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        ServicePermission np = (ServicePermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        int desired = np.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        int effective = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        int needed = desired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            int len = perms.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            // need to deal with the case where the needed permission has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            // more than one action and the collection has individual permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            // that sum up to the needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                ServicePermission x = (ServicePermission) perms.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                //System.out.println("  trying "+x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                if (((needed & x.getMask()) != 0) && x.impliesIgnoreMask(np)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    effective |=  x.getMask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    if ((effective & desired) == desired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    needed = (desired ^ effective);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Adds a permission to the ServicePermissions. The key for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * the hash is the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *                                       ServicePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @exception SecurityException - if this PermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *                                has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public void add(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        if (! (permission instanceof ServicePermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            perms.add(0, permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Returns an enumeration of all the ServicePermission objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * in the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @return an enumeration of all the ServicePermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public Enumeration<Permission> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        // Convert Iterator into Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            return Collections.enumeration(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    private static final long serialVersionUID = -4118834211490102011L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    // private Vector permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @serialField permissions java.util.Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *     A list of ServicePermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        new ObjectStreamField("permissions", Vector.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @serialData "permissions" field (a Vector containing the ServicePermissions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Writes the contents of the perms field out as a Vector for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * serialization compatibility with earlier releases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        // Write out Vector
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   566
        Vector<Permission> permissions = new Vector<>(perms.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            permissions.addAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Reads in a Vector of ServicePermissions and saves them in the perms field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   580
    @SuppressWarnings("unchecked")
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 11131
diff changeset
   581
    private void readObject(ObjectInputStream in)
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 11131
diff changeset
   582
        throws IOException, ClassNotFoundException
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 11131
diff changeset
   583
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        // Get the one we want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        Vector<Permission> permissions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                (Vector<Permission>)gfields.get("permissions", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        perms = new ArrayList<Permission>(permissions.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        perms.addAll(permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
}