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