jdk/src/share/classes/javax/security/auth/Policy.java
author juh
Tue, 16 Jul 2013 12:19:41 -0700
changeset 18830 90956ead732f
parent 14775 2ed01c760aea
child 19439 57876ed3c426
permissions -rw-r--r--
8020557: javadoc cleanup in javax.security Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
     2
 * Copyright (c) 1998, 2013, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
7291
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
    28
import java.security.Security;
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
    29
import sun.security.util.Debug;
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <p> This is an abstract class for representing the system policy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Subject-based authorization.  A subclass implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * of this class provides a means to specify a Subject-based
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    35
 * access control {@code Policy}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    37
 * <p> A {@code Policy} object can be queried for the set of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Permissions granted to code running as a
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    39
 * {@code Principal} in the following manner:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      policy = Policy.getPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      PermissionCollection perms = policy.getPermissions(subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *                                                      codeSource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    47
 * The {@code Policy} object consults the local policy and returns
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    48
 * and appropriate {@code Permissions} object with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Permissions granted to the Principals associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * provided <i>subject</i>, and granted to the code specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * by the provided <i>codeSource</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    53
 * <p> A {@code Policy} contains the following information.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Note that this example only represents the syntax for the default
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    55
 * {@code Policy} implementation. Subclass implementations of this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * may implement alternative syntaxes and may retrieve the
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    57
 * {@code Policy} from any source such as files, databases,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * or servers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    60
 * <p> Each entry in the {@code Policy} is represented as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * a <b><i>grant</i></b> entry.  Each <b><i>grant</i></b> entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * specifies a codebase, code signers, and Principals triplet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * as well as the Permissions granted to that triplet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *      grant CodeBase ["URL"], Signedby ["signers"],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *            Principal [Principal_Class] "Principal_Name" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *          Permission Permission_Class ["Target_Name"]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *                                      [, "Permission_Actions"]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *                                      [, signedBy "SignerName"];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *      };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The CodeBase and Signedby components of the triplet name/value pairs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * are optional.  If they are not present, then any any codebase will match,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * and any signer (including unsigned code) will match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * For Example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *      grant CodeBase "foo.com", Signedby "foo",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *            Principal com.sun.security.auth.SolarisPrincipal "duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *          permission java.io.FilePermission "/home/duke", "read, write";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *      };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * This <b><i>grant</i></b> entry specifies that code from "foo.com",
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    87
 * signed by "foo', and running as a {@code SolarisPrincipal} with the
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    88
 * name, duke, has one {@code Permission}.  This {@code Permission}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * permits the executing code to read and write files in the directory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * "/home/duke".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    92
 * <p> To "run" as a particular {@code Principal},
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    93
 * code invokes the {@code Subject.doAs(subject, ...)} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * After invoking that method, the code runs as all the Principals
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    95
 * associated with the specified {@code Subject}.
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    96
 * Note that this {@code Policy} (and the Permissions
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    97
 * granted in this {@code Policy}) only become effective
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
    98
 * after the call to {@code Subject.doAs} has occurred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p> Multiple Principals may be listed within one <b><i>grant</i></b> entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * All the Principals in the grant entry must be associated with
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   102
 * the {@code Subject} provided to {@code Subject.doAs}
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   103
 * for that {@code Subject} to be granted the specified Permissions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *      grant Principal com.sun.security.auth.SolarisPrincipal "duke",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *            Principal com.sun.security.auth.SolarisNumericUserPrincipal "0" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *          permission java.io.FilePermission "/home/duke", "read, write";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *          permission java.net.SocketPermission "duke.com", "connect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *      };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * This entry grants any code running as both "duke" and "0"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * permission to read and write files in duke's home directory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * as well as permission to make socket connections to "duke.com".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p> Note that non Principal-based grant entries are not permitted
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   118
 * in this {@code Policy}.  Therefore, grant entries such as:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *      grant CodeBase "foo.com", Signedby "foo" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *          permission java.io.FilePermission "/tmp/scratch", "read, write";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *      };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * are rejected.  Such permission must be listed in the
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   127
 * {@code java.security.Policy}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 7291
diff changeset
   129
 * <p> The default {@code Policy} implementation can be changed by
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 7291
diff changeset
   130
 * setting the value of the {@code auth.policy.provider} security property to
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 7291
diff changeset
   131
 * the fully qualified name of the desired {@code Policy} implementation class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @deprecated  as of JDK version 1.4 -- Replaced by java.security.Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *              java.security.Policy has a method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *      public PermissionCollection getPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *          (java.security.ProtectionDomain pd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * and ProtectionDomain has a constructor:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *      public ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *          (CodeSource cs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *           PermissionCollection permissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *           ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *           Principal[] principals)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * These two APIs provide callers the means to query the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * Policy for Principal-based Permission entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *
14775
2ed01c760aea 8004064: Downgrade normative references to ${java.home}/lib/security/java.security
mullan
parents: 7291
diff changeset
   152
 * @see java.security.Security security properties
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
public abstract class Policy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static Policy policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static ClassLoader contextClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
7291
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   160
    // true if a custom (not com.sun.security.auth.PolicyFile) system-wide
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   161
    // policy object is set
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   162
    private static boolean isCustomPolicy;
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   163
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        contextClassLoader = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                (new java.security.PrivilegedAction<ClassLoader>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                public ClassLoader run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    return Thread.currentThread().getContextClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    protected Policy() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Returns the installed Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * This method first calls
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   182
     * {@code SecurityManager.checkPermission} with the
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   183
     * {@code AuthPermission("getPolicy")} permission
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * to ensure the caller has permission to get the Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @return the installed Policy.  The return value cannot be
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   189
     *          {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @exception java.lang.SecurityException if the current thread does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *      have permission to get the Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see #setPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public static Policy getPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (sm != null) sm.checkPermission(new AuthPermission("getPolicy"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return getPolicyNoCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Returns the installed Policy object, skipping the security check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @return the installed Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    static Policy getPolicyNoCheck() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (policy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            synchronized(Policy.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (policy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    String policy_class = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    policy_class = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        (new java.security.PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                            return java.security.Security.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                ("auth.policy.provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    if (policy_class == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        policy_class = "com.sun.security.auth.PolicyFile";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        final String finalClass = policy_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                        policy = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                            (new java.security.PrivilegedExceptionAction<Policy>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                            public Policy run() throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                                InstantiationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                                IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                return (Policy) Class.forName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                        (finalClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                                        true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                        contextClassLoader).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        });
7291
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   239
                        isCustomPolicy =
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   240
                            !finalClass.equals("com.sun.security.auth.PolicyFile");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                (sun.security.util.ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   244
                                ("unable.to.instantiate.Subject.based.policy"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        return policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Sets the system-wide Policy object. This method first calls
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   255
     * {@code SecurityManager.checkPermission} with the
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   256
     * {@code AuthPermission("setPolicy")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * permission to ensure the caller has permission to set the Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param policy the new system Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @exception java.lang.SecurityException if the current thread does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *          have permission to set the Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @see #getPolicy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public static void setPolicy(Policy policy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (sm != null) sm.checkPermission(new AuthPermission("setPolicy"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        Policy.policy = policy;
7291
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   272
        // all non-null policy objects are assumed to be custom
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   273
        isCustomPolicy = policy != null ? true : false;
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   274
    }
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   275
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   276
    /**
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   277
     * Returns true if a custom (not com.sun.security.auth.PolicyFile)
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   278
     * system-wide policy object has been set or installed. This method is
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   279
     * called by SubjectDomainCombiner to provide backwards compatibility for
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   280
     * developers that provide their own javax.security.auth.Policy
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   281
     * implementations.
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   282
     *
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   283
     * @return true if a custom (not com.sun.security.auth.PolicyFile)
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   284
     * system-wide policy object has been set; false otherwise
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   285
     */
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   286
    static boolean isCustomPolicySet(Debug debug) {
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   287
        if (policy != null) {
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   288
            if (debug != null && isCustomPolicy) {
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   289
                debug.println("Providing backwards compatibility for " +
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   290
                              "javax.security.auth.policy implementation: " +
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   291
                              policy.toString());
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   292
            }
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   293
            return isCustomPolicy;
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   294
        }
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   295
        // check if custom policy has been set using auth.policy.provider prop
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   296
        String policyClass = java.security.AccessController.doPrivileged
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   297
            (new java.security.PrivilegedAction<String>() {
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   298
                public String run() {
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   299
                    return Security.getProperty("auth.policy.provider");
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   300
                }
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   301
        });
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   302
        if (policyClass != null
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   303
            && !policyClass.equals("com.sun.security.auth.PolicyFile")) {
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   304
            if (debug != null) {
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   305
                debug.println("Providing backwards compatibility for " +
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   306
                              "javax.security.auth.policy implementation: " +
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   307
                              policyClass);
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   308
            }
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   309
            return true;
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   310
        }
9fefa2786251 6995424: Eliminate dependency to a deprecated API com.sun.security.auth.PolicyFile
mullan
parents: 7179
diff changeset
   311
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Retrieve the Permissions granted to the Principals associated with
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   316
     * the specified {@code CodeSource}.
2
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
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   320
     * @param subject the {@code Subject}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *                  whose associated Principals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *                  in conjunction with the provided
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   323
     *                  {@code CodeSource}, determines the Permissions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *                  returned by this method.  This parameter
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   325
     *                  may be {@code null}. <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   327
     * @param cs the code specified by its {@code CodeSource}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *                  that determines, in conjunction with the provided
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   329
     *                  {@code Subject}, the Permissions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *                  returned by this method.  This parameter may be
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   331
     *                  {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @return the Collection of Permissions granted to all the
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   334
     *                  {@code Subject} and code specified in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *                  the provided <i>subject</i> and <i>cs</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *                  parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public abstract java.security.PermissionCollection getPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                        (Subject subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                        java.security.CodeSource cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Refresh and reload the Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p>This method causes this object to refresh/reload its current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Policy. This is implementation-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * For example, if the Policy object is stored in
18830
90956ead732f 8020557: javadoc cleanup in javax.security
juh
parents: 14775
diff changeset
   348
     * a file, calling {@code refresh} will cause the file to be re-read.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *                          to refresh the Policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public abstract void refresh();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}