0N/A/*
3050N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.security.auth;
0N/A
0N/Aimport java.net.URL;
0N/Aimport java.util.*;
0N/Aimport java.security.CodeSource;
0N/Aimport java.security.Principal;
0N/Aimport java.security.cert.Certificate;
0N/Aimport java.lang.reflect.Constructor;
0N/A
0N/Aimport javax.security.auth.Subject;
0N/A
0N/A/**
0N/A * <p> This <code>SubjectCodeSource</code> class contains
0N/A * a <code>URL</code>, signer certificates, and either a <code>Subject</code>
0N/A * (that represents the <code>Subject</code> in the current
0N/A * <code>AccessControlContext</code>),
0N/A * or a linked list of Principals/PrincipalComparators
0N/A * (that represent a "subject" in a <code>Policy</code>).
0N/A *
0N/A */
0N/Aclass SubjectCodeSource extends CodeSource implements java.io.Serializable {
0N/A
0N/A private static final long serialVersionUID = 6039418085604715275L;
0N/A
0N/A private static final java.util.ResourceBundle rb =
0N/A java.security.AccessController.doPrivileged
0N/A (new java.security.PrivilegedAction<java.util.ResourceBundle>() {
0N/A public java.util.ResourceBundle run() {
0N/A return (java.util.ResourceBundle.getBundle
0N/A ("sun.security.util.AuthResources"));
0N/A }
0N/A });
0N/A
0N/A private Subject subject;
0N/A private LinkedList<PolicyParser.PrincipalEntry> principals;
0N/A private static final Class[] PARAMS = { String.class };
0N/A private static final sun.security.util.Debug debug =
0N/A sun.security.util.Debug.getInstance("auth", "\t[Auth Access]");
0N/A private ClassLoader sysClassLoader;
0N/A
0N/A /**
0N/A * Creates a new <code>SubjectCodeSource</code>
0N/A * with the given <code>Subject</code>, principals, <code>URL</code>,
0N/A * and signers (Certificates). The <code>Subject</code>
0N/A * represents the <code>Subject</code> associated with the current
0N/A * <code>AccessControlContext</code>.
0N/A * The Principals are given as a <code>LinkedList</code>
0N/A * of <code>PolicyParser.PrincipalEntry</code> objects.
0N/A * Typically either a <code>Subject</code> will be provided,
0N/A * or a list of <code>principals</code> will be provided
0N/A * (not both).
0N/A *
0N/A * <p>
0N/A *
0N/A * @param subject the <code>Subject</code> associated with this
0N/A * <code>SubjectCodeSource</code> <p>
0N/A *
0N/A * @param url the <code>URL</code> associated with this
0N/A * <code>SubjectCodeSource</code> <p>
0N/A *
0N/A * @param certs the signers associated with this
0N/A * <code>SubjectCodeSource</code> <p>
0N/A */
0N/A SubjectCodeSource(Subject subject,
0N/A LinkedList<PolicyParser.PrincipalEntry> principals,
0N/A URL url, Certificate[] certs) {
0N/A
0N/A super(url, certs);
0N/A this.subject = subject;
0N/A this.principals = (principals == null ?
0N/A new LinkedList<PolicyParser.PrincipalEntry>() :
0N/A new LinkedList<PolicyParser.PrincipalEntry>(principals));
0N/A sysClassLoader = java.security.AccessController.doPrivileged
0N/A (new java.security.PrivilegedAction<ClassLoader>() {
0N/A public ClassLoader run() {
0N/A return ClassLoader.getSystemClassLoader();
0N/A }
0N/A });
0N/A }
0N/A
0N/A /**
0N/A * Get the Principals associated with this <code>SubjectCodeSource</code>.
0N/A * The Principals are retrieved as a <code>LinkedList</code>
0N/A * of <code>PolicyParser.PrincipalEntry</code> objects.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the Principals associated with this
0N/A * <code>SubjectCodeSource</code> as a <code>LinkedList</code>
0N/A * of <code>PolicyParser.PrincipalEntry</code> objects.
0N/A */
0N/A LinkedList<PolicyParser.PrincipalEntry> getPrincipals() {
0N/A return principals;
0N/A }
0N/A
0N/A /**
0N/A * Get the <code>Subject</code> associated with this
0N/A * <code>SubjectCodeSource</code>. The <code>Subject</code>
0N/A * represents the <code>Subject</code> associated with the
0N/A * current <code>AccessControlContext</code>.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return the <code>Subject</code> associated with this
0N/A * <code>SubjectCodeSource</code>.
0N/A */
0N/A Subject getSubject() {
0N/A return subject;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this <code>SubjectCodeSource</code> object "implies"
0N/A * the specified <code>CodeSource</code>.
0N/A * More specifically, this method makes the following checks.
0N/A * If any fail, it returns false. If they all succeed, it returns true.
0N/A *
0N/A * <p>
0N/A * <ol>
0N/A * <li> The provided codesource must not be <code>null</code>.
0N/A * <li> codesource must be an instance of <code>SubjectCodeSource</code>.
0N/A * <li> super.implies(codesource) must return true.
0N/A * <li> for each principal in this codesource's principal list:
0N/A * <ol>
0N/A * <li> if the principal is an instanceof
0N/A * <code>PrincipalComparator</code>, then the principal must
0N/A * imply the provided codesource's <code>Subject</code>.
0N/A * <li> if the principal is not an instanceof
0N/A * <code>PrincipalComparator</code>, then the provided
0N/A * codesource's <code>Subject</code> must have an
0N/A * associated <code>Principal</code>, <i>P</i>, where
0N/A * P.getClass().getName equals principal.principalClass,
0N/A * and P.getName() equals principal.principalName.
0N/A * </ol>
0N/A * </ol>
0N/A *
0N/A * <p>
0N/A *
0N/A * @param codesource the <code>CodeSource</code> to compare against.
0N/A *
0N/A * @return true if this <code>SubjectCodeSource</code> implies the
0N/A * the specified <code>CodeSource</code>.
0N/A */
0N/A public boolean implies(CodeSource codesource) {
0N/A
0N/A LinkedList<PolicyParser.PrincipalEntry> subjectList = null;
0N/A
0N/A if (codesource == null ||
0N/A !(codesource instanceof SubjectCodeSource) ||
0N/A !(super.implies(codesource))) {
0N/A
0N/A if (debug != null)
0N/A debug.println("\tSubjectCodeSource.implies: FAILURE 1");
0N/A return false;
0N/A }
0N/A
0N/A SubjectCodeSource that = (SubjectCodeSource)codesource;
0N/A
0N/A // if the principal list in the policy "implies"
0N/A // the Subject associated with the current AccessControlContext,
0N/A // then return true
0N/A
0N/A if (this.principals == null) {
0N/A if (debug != null)
0N/A debug.println("\tSubjectCodeSource.implies: PASS 1");
0N/A return true;
0N/A }
0N/A
0N/A if (that.getSubject() == null ||
0N/A that.getSubject().getPrincipals().size() == 0) {
0N/A if (debug != null)
0N/A debug.println("\tSubjectCodeSource.implies: FAILURE 2");
0N/A return false;
0N/A }
0N/A
0N/A ListIterator<PolicyParser.PrincipalEntry> li =
0N/A this.principals.listIterator(0);
0N/A while (li.hasNext()) {
0N/A PolicyParser.PrincipalEntry pppe = li.next();
0N/A try {
0N/A
0N/A // handle PrincipalComparators
0N/A
0N/A Class principalComparator = Class.forName(pppe.principalClass,
0N/A true,
0N/A sysClassLoader);
0N/A Constructor c = principalComparator.getConstructor(PARAMS);
0N/A PrincipalComparator pc =
0N/A (PrincipalComparator)c.newInstance
0N/A (new Object[] { pppe.principalName });
0N/A
0N/A if (!pc.implies(that.getSubject())) {
0N/A if (debug != null)
0N/A debug.println("\tSubjectCodeSource.implies: FAILURE 3");
0N/A return false;
0N/A } else {
0N/A if (debug != null)
0N/A debug.println("\tSubjectCodeSource.implies: PASS 2");
0N/A return true;
0N/A }
0N/A } catch (Exception e) {
0N/A
0N/A // no PrincipalComparator, simply compare Principals
0N/A
0N/A if (subjectList == null) {
0N/A
0N/A if (that.getSubject() == null) {
0N/A if (debug != null)
0N/A debug.println("\tSubjectCodeSource.implies: " +
0N/A "FAILURE 4");
0N/A return false;
0N/A }
0N/A Iterator<Principal> i =
0N/A that.getSubject().getPrincipals().iterator();
0N/A
0N/A subjectList = new LinkedList<PolicyParser.PrincipalEntry>();
0N/A while (i.hasNext()) {
0N/A Principal p = i.next();
0N/A PolicyParser.PrincipalEntry spppe =
0N/A new PolicyParser.PrincipalEntry
0N/A (p.getClass().getName(), p.getName());
0N/A subjectList.add(spppe);
0N/A }
0N/A }
0N/A
0N/A if (!subjectListImpliesPrincipalEntry(subjectList, pppe)) {
0N/A if (debug != null)
0N/A debug.println("\tSubjectCodeSource.implies: FAILURE 5");
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A if (debug != null)
0N/A debug.println("\tSubjectCodeSource.implies: PASS 3");
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * This method returns, true, if the provided <i>subjectList</i>
0N/A * "contains" the <code>Principal</code> specified
0N/A * in the provided <i>pppe</i> argument.
0N/A *
0N/A * Note that the provided <i>pppe</i> argument may have
0N/A * wildcards (*) for the <code>Principal</code> class and name,
0N/A * which need to be considered.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param subjectList a list of PolicyParser.PrincipalEntry objects
0N/A * that correspond to all the Principals in the Subject currently
0N/A * on this thread's AccessControlContext. <p>
0N/A *
0N/A * @param pppe the Principals specified in a grant entry.
0N/A *
0N/A * @return true if the provided <i>subjectList</i> "contains"
0N/A * the <code>Principal</code> specified in the provided
0N/A * <i>pppe</i> argument.
0N/A */
0N/A private boolean subjectListImpliesPrincipalEntry(
0N/A LinkedList<PolicyParser.PrincipalEntry> subjectList,
0N/A PolicyParser.PrincipalEntry pppe) {
0N/A
0N/A ListIterator<PolicyParser.PrincipalEntry> li =
0N/A subjectList.listIterator(0);
0N/A while (li.hasNext()) {
0N/A PolicyParser.PrincipalEntry listPppe = li.next();
0N/A
0N/A if (pppe.principalClass.equals
0N/A (PolicyParser.PrincipalEntry.WILDCARD_CLASS) ||
0N/A pppe.principalClass.equals
0N/A (listPppe.principalClass)) {
0N/A
0N/A if (pppe.principalName.equals
0N/A (PolicyParser.PrincipalEntry.WILDCARD_NAME) ||
0N/A pppe.principalName.equals
0N/A (listPppe.principalName))
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Tests for equality between the specified object and this
0N/A * object. Two <code>SubjectCodeSource</code> objects are considered equal
0N/A * if their locations are of identical value, if the two sets of
0N/A * Certificates are of identical values, and if the
0N/A * Subjects are equal, and if the PolicyParser.PrincipalEntry values
0N/A * are of identical values. It is not required that
0N/A * the Certificates or PolicyParser.PrincipalEntry values
0N/A * be in the same order.
0N/A *
0N/A * <p>
0N/A *
0N/A * @param obj the object to test for equality with this object.
0N/A *
0N/A * @return true if the objects are considered equal, false otherwise.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A
0N/A if (obj == this)
0N/A return true;
0N/A
0N/A if (super.equals(obj) == false)
0N/A return false;
0N/A
0N/A if (!(obj instanceof SubjectCodeSource))
0N/A return false;
0N/A
0N/A SubjectCodeSource that = (SubjectCodeSource)obj;
0N/A
0N/A // the principal lists must match
0N/A try {
0N/A if (this.getSubject() != that.getSubject())
0N/A return false;
0N/A } catch (SecurityException se) {
0N/A return false;
0N/A }
0N/A
0N/A if ((this.principals == null && that.principals != null) ||
0N/A (this.principals != null && that.principals == null))
0N/A return false;
0N/A
0N/A if (this.principals != null && that.principals != null) {
0N/A if (!this.principals.containsAll(that.principals) ||
0N/A !that.principals.containsAll(this.principals))
0N/A
0N/A return false;
0N/A }
0N/A
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Return a hashcode for this <code>SubjectCodeSource</code>.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return a hashcode for this <code>SubjectCodeSource</code>.
0N/A */
0N/A public int hashCode() {
0N/A return super.hashCode();
0N/A }
0N/A
0N/A /**
0N/A * Return a String representation of this <code>SubjectCodeSource</code>.
0N/A *
0N/A * <p>
0N/A *
0N/A * @return a String representation of this <code>SubjectCodeSource</code>.
0N/A */
0N/A public String toString() {
0N/A String returnMe = super.toString();
0N/A if (getSubject() != null) {
0N/A if (debug != null) {
0N/A final Subject finalSubject = getSubject();
0N/A returnMe = returnMe + "\n" +
0N/A java.security.AccessController.doPrivileged
0N/A (new java.security.PrivilegedAction<String>() {
0N/A public String run() {
0N/A return finalSubject.toString();
0N/A }
0N/A });
0N/A } else {
0N/A returnMe = returnMe + "\n" + getSubject().toString();
0N/A }
0N/A }
0N/A if (principals != null) {
0N/A ListIterator<PolicyParser.PrincipalEntry> li =
0N/A principals.listIterator();
0N/A while (li.hasNext()) {
0N/A PolicyParser.PrincipalEntry pppe = li.next();
3050N/A returnMe = returnMe + rb.getString("NEWLINE") +
0N/A pppe.principalClass + " " +
0N/A pppe.principalName;
0N/A }
0N/A }
0N/A return returnMe;
0N/A }
0N/A}