src/java.security.jgss/share/classes/javax/security/auth/kerberos/DelegationPermission.java
author weijun
Tue, 29 Oct 2019 09:34:23 +0800
changeset 58831 b026a43e1809
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231365: ServicePermission::equals doesn't comply to the spec 8231196: DelegationPermission allows to create an instance that thows NPE on ::equals call Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58831
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2019, 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;
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
    32
import java.security.BasicPermission;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.PermissionCollection;
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
    35
import java.util.*;
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
    36
import java.util.concurrent.ConcurrentHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class is used to restrict the usage of the Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * delegation model, ie: forwardable and proxiable tickets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
    42
 * The target name of this {@code Permission} specifies a pair of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * kerberos service principals. The first is the subordinate service principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * being entrusted to use the TGT. The second service principal designates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the target service the subordinate service principal is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * interact with on behalf of the initiating KerberosPrincipal. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * latter service principal is specified to restrict the use of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * proxiable ticket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * For example, to specify the "host" service use of a forwardable TGT the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * target permission is specified as follows:
21955
abc02575919c 8029475: Fix more doclint issues in javax.security
darcy
parents: 18830
diff changeset
    52
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *  DelegationPermission("\"host/foo.example.com@EXAMPLE.COM\" \"krbtgt/EXAMPLE.COM@EXAMPLE.COM\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * To give the "backup" service a proxiable nfs service ticket the target permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * might be specified:
21955
abc02575919c 8029475: Fix more doclint issues in javax.security
darcy
parents: 18830
diff changeset
    59
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *  DelegationPermission("\"backup/bar.example.com@EXAMPLE.COM\" \"nfs/home.EXAMPLE.COM@EXAMPLE.COM\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
public final class DelegationPermission extends BasicPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final long serialVersionUID = 883133252142523922L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private transient String subordinate, service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
    75
     * Create a new {@code DelegationPermission}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * with the specified subordinate and target principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param principals the name of the subordinate and target principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
    80
     * @throws NullPointerException if {@code principals} is {@code null}.
58831
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
    81
     * @throws IllegalArgumentException if {@code principals} is empty,
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
    82
     *      or does not contain a pair of principals, or is improperly quoted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public DelegationPermission(String principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        super(principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        init(principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
    90
     * Create a new {@code DelegationPermission}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * with the specified subordinate and target principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param principals the name of the subordinate and target principals
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
    94
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param actions should be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14342
diff changeset
    97
     * @throws NullPointerException if {@code principals} is {@code null}.
58831
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
    98
     * @throws IllegalArgumentException if {@code principals} is empty,
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
    99
     *      or does not contain a pair of principals, or is improperly quoted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public DelegationPermission(String principals, String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        super(principals, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        init(principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Initialize the DelegationPermission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private void init(String target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        StringTokenizer t = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (!target.startsWith("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                ("service principal [" + target +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                 "] syntax invalid: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                 "improperly quoted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            t = new StringTokenizer(target, "\"", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            subordinate = t.nextToken();
58831
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   121
            switch (t.countTokens()) {
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   122
                case 2:
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   123
                    t.nextToken();  // bypass whitespace
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   124
                    service = t.nextToken();
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   125
                    break;
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   126
                case 0:
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   127
                    throw new IllegalArgumentException
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   128
                            ("service principal not provided");
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   129
                default:
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   130
                    throw new IllegalArgumentException
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   131
                            ("service principal [" + t.nextToken() +
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   132
                            "] syntax invalid: " +
b026a43e1809 8231365: ServicePermission::equals doesn't comply to the spec
weijun
parents: 47216
diff changeset
   133
                            "improperly quoted");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Checks if this Kerberos delegation permission object "implies" the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <P>
32424
2d9d66d0519f 8129789: implies() of ServicePermission and DelegationPermission underspecified
weijun
parents: 31265
diff changeset
   142
     * This method returns true if this {@code DelegationPermission}
2d9d66d0519f 8129789: implies() of ServicePermission and DelegationPermission underspecified
weijun
parents: 31265
diff changeset
   143
     * is equal to {@code p}, and returns false otherwise.
2d9d66d0519f 8129789: implies() of ServicePermission and DelegationPermission underspecified
weijun
parents: 31265
diff changeset
   144
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return true if the specified permission is implied by this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   150
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public boolean implies(Permission p) {
31265
2eabccc392ed 8129575: Equal DelegationPermission instances may return different hash codes
asmotrak
parents: 31080
diff changeset
   152
        return equals(p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Checks two DelegationPermission objects for equality.
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   157
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
29492
a4bf9a570035 8028266: Tidy warnings cleanup for packages java.security/javax.security
avstepan
parents: 25859
diff changeset
   160
     * @return true if {@code obj} is a DelegationPermission, and
31265
2eabccc392ed 8129575: Equal DelegationPermission instances may return different hash codes
asmotrak
parents: 31080
diff changeset
   161
     *  has the same subordinate and service principal as this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *  DelegationPermission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   164
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public boolean equals(Object obj) {
31265
2eabccc392ed 8129575: Equal DelegationPermission instances may return different hash codes
asmotrak
parents: 31080
diff changeset
   166
        if (obj == this) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return true;
31265
2eabccc392ed 8129575: Equal DelegationPermission instances may return different hash codes
asmotrak
parents: 31080
diff changeset
   168
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
31265
2eabccc392ed 8129575: Equal DelegationPermission instances may return different hash codes
asmotrak
parents: 31080
diff changeset
   170
        if (!(obj instanceof DelegationPermission)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return false;
31265
2eabccc392ed 8129575: Equal DelegationPermission instances may return different hash codes
asmotrak
parents: 31080
diff changeset
   172
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        DelegationPermission that = (DelegationPermission) obj;
31265
2eabccc392ed 8129575: Equal DelegationPermission instances may return different hash codes
asmotrak
parents: 31080
diff changeset
   175
2eabccc392ed 8129575: Equal DelegationPermission instances may return different hash codes
asmotrak
parents: 31080
diff changeset
   176
        return this.subordinate.equals(that.subordinate) &&
2eabccc392ed 8129575: Equal DelegationPermission instances may return different hash codes
asmotrak
parents: 31080
diff changeset
   177
                this.service.equals(that.service);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   185
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public int hashCode() {
31265
2eabccc392ed 8129575: Equal DelegationPermission instances may return different hash codes
asmotrak
parents: 31080
diff changeset
   187
        return 17 * subordinate.hashCode() + 31 * service.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Returns a PermissionCollection object for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * DelegationPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * DelegationPermission objects must be stored in a manner that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * allows them to be inserted into the collection in any order, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * that also enables the PermissionCollection implies method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return a new PermissionCollection object suitable for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * DelegationPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   202
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return new KrbDelegationPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * WriteObject is called to save the state of the DelegationPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * to a stream. The actions are serialized, and the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * takes care of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * readObject is called to restore the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * DelegationPermission from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private synchronized void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        // Read in the action, then initialize the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        init(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
final class KrbDelegationPermissionCollection extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // 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
   237
    private transient ConcurrentHashMap<Permission, Boolean> perms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public KrbDelegationPermissionCollection() {
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   240
        perms = new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Check and see if this collection of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10709
diff changeset
   247
     * @param permission the Permission object to compare
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * the collection, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   252
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (! (permission instanceof DelegationPermission))
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   255
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   257
        // if map contains key, then it automatically implies it
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   258
        return perms.containsKey(permission);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Adds a permission to the DelegationPermissions. The key for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * the hash is the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *                                       DelegationPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @exception SecurityException - if this PermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *                                has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   273
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public void add(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (! (permission instanceof DelegationPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   281
        perms.put(permission, Boolean.TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Returns an enumeration of all the DelegationPermission objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * in the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @return an enumeration of all the DelegationPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   290
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public Enumeration<Permission> elements() {
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   292
        return perms.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private static final long serialVersionUID = -3383936936589966948L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    //    private Vector permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @serialField permissions java.util.Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *     A list of DelegationPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        new ObjectStreamField("permissions", Vector.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @serialData "permissions" field (a Vector containing the DelegationPermissions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Writes the contents of the perms field out as a Vector for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * serialization compatibility with earlier releases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // Write out Vector
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   319
        Vector<Permission> permissions = new Vector<>(perms.keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Reads in a Vector of DelegationPermissions and saves them in the perms field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   329
    @SuppressWarnings("unchecked")
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10709
diff changeset
   330
    private void readObject(ObjectInputStream in)
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10709
diff changeset
   331
        throws IOException, ClassNotFoundException
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10709
diff changeset
   332
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // Get the one we want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        Vector<Permission> permissions =
31080
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   340
            (Vector<Permission>)gfields.get("permissions", null);
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   341
        perms = new ConcurrentHashMap<>(permissions.size());
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   342
        for (Permission perm : permissions) {
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   343
            perms.put(perm, Boolean.TRUE);
00a25f4c4d44 8056179: Store permissions in concurrent collections in PermissionCollection subclasses
mullan
parents: 29492
diff changeset
   344
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}