jdk/src/share/classes/javax/security/auth/kerberos/DelegationPermission.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 14014 da3648e13e67
child 18830 90956ead732f
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14014
diff changeset
     2
 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.security.auth.kerberos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.BasicPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
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 restrict the usage of the Kerberos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * delegation model, ie: forwardable and proxiable tickets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The target name of this <code>Permission</code> specifies a pair of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * kerberos service principals. The first is the subordinate service principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * being entrusted to use the TGT. The second service principal designates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the target service the subordinate service principal is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * interact with on behalf of the initiating KerberosPrincipal. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * latter service principal is specified to restrict the use of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * proxiable ticket.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * For example, to specify the "host" service use of a forwardable TGT the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * target permission is specified as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *  DelegationPermission("\"host/foo.example.com@EXAMPLE.COM\" \"krbtgt/EXAMPLE.COM@EXAMPLE.COM\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * To give the "backup" service a proxiable nfs service ticket the target permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * might be specified:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *  DelegationPermission("\"backup/bar.example.com@EXAMPLE.COM\" \"nfs/home.EXAMPLE.COM@EXAMPLE.COM\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public final class DelegationPermission extends BasicPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final long serialVersionUID = 883133252142523922L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private transient String subordinate, service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Create a new <code>DelegationPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * with the specified subordinate and target principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param principals the name of the subordinate and target principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @throws NullPointerException if <code>principals</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @throws IllegalArgumentException if <code>principals</code> is empty.
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Create a new <code>DelegationPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * with the specified subordinate and target principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param principals the name of the subordinate and target principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param actions should be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @throws NullPointerException if <code>principals</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @throws IllegalArgumentException if <code>principals</code> is empty.
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();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (t.countTokens() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                t.nextToken();  // bypass whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                service = t.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } else if (t.countTokens() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    ("service principal [" + t.nextToken() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                     "] syntax invalid: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                     "improperly quoted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Checks if this Kerberos delegation permission object "implies" the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * If none of the above are true, <code>implies</code> returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param p the permission to check against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return true if the specified permission is implied by this object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public boolean implies(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (!(p instanceof DelegationPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        DelegationPermission that = (DelegationPermission) p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (this.subordinate.equals(that.subordinate) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            this.service.equals(that.service))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return false;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Checks two DelegationPermission objects for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return true if <i>obj</i> is a DelegationPermission, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *  has the same subordinate and service principal as this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *  DelegationPermission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (! (obj instanceof DelegationPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        DelegationPermission that = (DelegationPermission) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return implies(that);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Returns the hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns a PermissionCollection object for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * DelegationPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * DelegationPermission objects must be stored in a manner that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * allows them to be inserted into the collection in any order, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * that also enables the PermissionCollection implies method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * be implemented in an efficient (and consistent) manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @return a new PermissionCollection object suitable for storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * DelegationPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public PermissionCollection newPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return new KrbDelegationPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * WriteObject is called to save the state of the DelegationPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * to a stream. The actions are serialized, and the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * takes care of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * readObject is called to restore the state of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * DelegationPermission from a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private synchronized void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        // Read in the action, then initialize the rest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        init(getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
      public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
      DelegationPermission this_ =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
      new DelegationPermission(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
      DelegationPermission that_ =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
      new DelegationPermission(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
      System.out.println("this.implies(that) = " + this_.implies(that_));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
      System.out.println("this = "+this_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
      System.out.println("that = "+that_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
      KrbDelegationPermissionCollection nps =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
      new KrbDelegationPermissionCollection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
      nps.add(this_);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
      nps.add(new DelegationPermission("\"host/foo.example.com@EXAMPLE.COM\" \"CN=Gary Ellison/OU=JSN/O=SUNW/L=Palo Alto/ST=CA/C=US\""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
      try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
      nps.add(new DelegationPermission("host/foo.example.com@EXAMPLE.COM \"CN=Gary Ellison/OU=JSN/O=SUNW/L=Palo Alto/ST=CA/C=US\""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
      } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
      System.err.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
      System.out.println("nps.implies(that) = " + nps.implies(that_));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
      System.out.println("-----\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
      Enumeration e = nps.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
      while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
      DelegationPermission x =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
      (DelegationPermission) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
      System.out.println("nps.e = " + x);
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
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
final class KrbDelegationPermissionCollection extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    // Not serialized; see serialization section at end of class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private transient List<Permission> perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public KrbDelegationPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        perms = new ArrayList<Permission>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Check and see if this collection of permissions implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * expressed in "permission".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10709
diff changeset
   280
     * @param permission the Permission object to compare
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @return true if "permission" is a proper subset of a permission in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * the collection, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (! (permission instanceof DelegationPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            for (Permission x : perms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (x.implies(permission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Adds a permission to the DelegationPermissions. The key for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * the hash is the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @exception IllegalArgumentException - if the permission is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *                                       DelegationPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @exception SecurityException - if this PermissionCollection object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *                                has been marked readonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public void add(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (! (permission instanceof DelegationPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            throw new SecurityException("attempt to add a Permission to a readonly PermissionCollection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            perms.add(0, permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Returns an enumeration of all the DelegationPermission objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * in the container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return an enumeration of all the DelegationPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public Enumeration<Permission> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // Convert Iterator into Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return Collections.enumeration(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private static final long serialVersionUID = -3383936936589966948L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    //    private Vector permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @serialField permissions java.util.Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *     A list of DelegationPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        new ObjectStreamField("permissions", Vector.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @serialData "permissions" field (a Vector containing the DelegationPermissions).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Writes the contents of the perms field out as a Vector for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * serialization compatibility with earlier releases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // Write out Vector
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   360
        Vector<Permission> permissions = new Vector<>(perms.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            permissions.addAll(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Reads in a Vector of DelegationPermissions and saves them in the perms field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   374
    @SuppressWarnings("unchecked")
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10709
diff changeset
   375
    private void readObject(ObjectInputStream in)
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10709
diff changeset
   376
        throws IOException, ClassNotFoundException
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 10709
diff changeset
   377
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // Get the one we want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        Vector<Permission> permissions =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                (Vector<Permission>)gfields.get("permissions", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        perms = new ArrayList<Permission>(permissions.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        perms.addAll(permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
}