jdk/src/share/classes/com/sun/security/auth/SubjectCodeSource.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 7179 4afb81e50183
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2006, 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 com.sun.security.auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p> This <code>SubjectCodeSource</code> class contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * a <code>URL</code>, signer certificates, and either a <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * (that represents the <code>Subject</code> in the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <code>AccessControlContext</code>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * or a linked list of Principals/PrincipalComparators
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * (that represent a "subject" in a <code>Policy</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class SubjectCodeSource extends CodeSource implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final long serialVersionUID = 6039418085604715275L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final java.util.ResourceBundle rb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        (new java.security.PrivilegedAction<java.util.ResourceBundle>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            public java.util.ResourceBundle run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                return (java.util.ResourceBundle.getBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                        ("sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private LinkedList<PolicyParser.PrincipalEntry> principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final Class[] PARAMS = { String.class };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        sun.security.util.Debug.getInstance("auth", "\t[Auth Access]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private ClassLoader sysClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates a new <code>SubjectCodeSource</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * with the given <code>Subject</code>, principals, <code>URL</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * and signers (Certificates).  The <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * represents the <code>Subject</code> associated with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * The Principals are given as a <code>LinkedList</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * of <code>PolicyParser.PrincipalEntry</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Typically either a <code>Subject</code> will be provided,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * or a list of <code>principals</code> will be provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * (not both).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param subject the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *                  <code>SubjectCodeSource</code> <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param url the <code>URL</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *                  <code>SubjectCodeSource</code> <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param certs the signers associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *                  <code>SubjectCodeSource</code> <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    SubjectCodeSource(Subject subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        LinkedList<PolicyParser.PrincipalEntry> principals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        URL url, Certificate[] certs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        super(url, certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.principals = (principals == null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                new LinkedList<PolicyParser.PrincipalEntry>() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                new LinkedList<PolicyParser.PrincipalEntry>(principals));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        sysClassLoader = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        (new java.security.PrivilegedAction<ClassLoader>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            public ClassLoader run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    return ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        });
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
     * Get the Principals associated with this <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The Principals are retrieved as a <code>LinkedList</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * of <code>PolicyParser.PrincipalEntry</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @return the Principals associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *          <code>SubjectCodeSource</code> as a <code>LinkedList</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *          of <code>PolicyParser.PrincipalEntry</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    LinkedList<PolicyParser.PrincipalEntry> getPrincipals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Get the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <code>SubjectCodeSource</code>.  The <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * represents the <code>Subject</code> associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * current <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @return the <code>Subject</code> associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *          <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    Subject getSubject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return subject;
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
     * Returns true if this <code>SubjectCodeSource</code> object "implies"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * the specified <code>CodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * More specifically, this method makes the following checks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * If any fail, it returns false.  If they all succeed, it returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <li> The provided codesource must not be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <li> codesource must be an instance of <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <li> super.implies(codesource) must return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <li> for each principal in this codesource's principal list:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <li>     if the principal is an instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *          <code>PrincipalComparator</code>, then the principal must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *          imply the provided codesource's <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <li>     if the principal is not an instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *          <code>PrincipalComparator</code>, then the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *          codesource's <code>Subject</code> must have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *          associated <code>Principal</code>, <i>P</i>, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *          P.getClass().getName equals principal.principalClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *          and P.getName() equals principal.principalName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param codesource the <code>CodeSource</code> to compare against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return true if this <code>SubjectCodeSource</code> implies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *          the specified <code>CodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public boolean implies(CodeSource codesource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        LinkedList<PolicyParser.PrincipalEntry> subjectList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (codesource == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            !(codesource instanceof SubjectCodeSource) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            !(super.implies(codesource))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                debug.println("\tSubjectCodeSource.implies: FAILURE 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        SubjectCodeSource that = (SubjectCodeSource)codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // if the principal list in the policy "implies"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // the Subject associated with the current AccessControlContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // then return true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (this.principals == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                debug.println("\tSubjectCodeSource.implies: PASS 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (that.getSubject() == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            that.getSubject().getPrincipals().size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                debug.println("\tSubjectCodeSource.implies: FAILURE 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        ListIterator<PolicyParser.PrincipalEntry> li =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                this.principals.listIterator(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        while (li.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            PolicyParser.PrincipalEntry pppe = li.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                // handle PrincipalComparators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                Class principalComparator = Class.forName(pppe.principalClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                                        true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                                        sysClassLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                Constructor c = principalComparator.getConstructor(PARAMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                PrincipalComparator pc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        (PrincipalComparator)c.newInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                        (new Object[] { pppe.principalName });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (!pc.implies(that.getSubject())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        debug.println("\tSubjectCodeSource.implies: FAILURE 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                        debug.println("\tSubjectCodeSource.implies: PASS 2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                // no PrincipalComparator, simply compare Principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (subjectList == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    if (that.getSubject() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                            debug.println("\tSubjectCodeSource.implies: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                        "FAILURE 4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    Iterator<Principal> i =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                that.getSubject().getPrincipals().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    subjectList = new LinkedList<PolicyParser.PrincipalEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        Principal p = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        PolicyParser.PrincipalEntry spppe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                new PolicyParser.PrincipalEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                (p.getClass().getName(), p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        subjectList.add(spppe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                if (!subjectListImpliesPrincipalEntry(subjectList, pppe)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        debug.println("\tSubjectCodeSource.implies: FAILURE 5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            debug.println("\tSubjectCodeSource.implies: PASS 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return true;
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
     * This method returns, true, if the provided <i>subjectList</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * "contains" the <code>Principal</code> specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * in the provided <i>pppe</i> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Note that the provided <i>pppe</i> argument may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * wildcards (*) for the <code>Principal</code> class and name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * which need to be considered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @param subjectList a list of PolicyParser.PrincipalEntry objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *          that correspond to all the Principals in the Subject currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *          on this thread's AccessControlContext. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @param pppe the Principals specified in a grant entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return true if the provided <i>subjectList</i> "contains"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *          the <code>Principal</code> specified in the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *          <i>pppe</i> argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private boolean subjectListImpliesPrincipalEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                LinkedList<PolicyParser.PrincipalEntry> subjectList,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                PolicyParser.PrincipalEntry pppe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        ListIterator<PolicyParser.PrincipalEntry> li =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                        subjectList.listIterator(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        while (li.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            PolicyParser.PrincipalEntry listPppe = li.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (pppe.principalClass.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                        (PolicyParser.PrincipalEntry.WILDCARD_CLASS) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                pppe.principalClass.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        (listPppe.principalClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                if (pppe.principalName.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                        (PolicyParser.PrincipalEntry.WILDCARD_NAME) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    pppe.principalName.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        (listPppe.principalName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return false;
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
     * Tests for equality between the specified object and this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * object. Two <code>SubjectCodeSource</code> objects are considered equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * if their locations are of identical value, if the two sets of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Certificates are of identical values, and if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Subjects are equal, and if the PolicyParser.PrincipalEntry values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * are of identical values.  It is not required that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * the Certificates or PolicyParser.PrincipalEntry values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * be in the same order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @return true if the objects are considered equal, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (super.equals(obj) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (!(obj instanceof SubjectCodeSource))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        SubjectCodeSource that = (SubjectCodeSource)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // the principal lists must match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (this.getSubject() != that.getSubject())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if ((this.principals == null && that.principals != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            (this.principals != null && that.principals == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (this.principals != null && that.principals != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (!this.principals.containsAll(that.principals) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                !that.principals.containsAll(this.principals))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Return a hashcode for this <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return a hashcode for this <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return super.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Return a String representation of this <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @return a String representation of this <code>SubjectCodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        String returnMe = super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (getSubject() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                final Subject finalSubject = getSubject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                returnMe = returnMe + "\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                (new java.security.PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                    return finalSubject.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                returnMe = returnMe + "\n" + getSubject().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (principals != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            ListIterator<PolicyParser.PrincipalEntry> li =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                        principals.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            while (li.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                PolicyParser.PrincipalEntry pppe = li.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                returnMe = returnMe + rb.getString("\n") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                        pppe.principalClass + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                        pppe.principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return returnMe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
}