jdk/src/share/classes/java/security/AccessController.java
author mchung
Tue, 16 Apr 2013 21:39:52 -0700
changeset 16906 44dfee24cb71
parent 14213 91ba926457c6
child 17946 7791613dcbfd
child 18222 2b50015e08db
permissions -rw-r--r--
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive Reviewed-by: jrose, alanb, twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14208
fd8e875982e9 7172522: Improve DomainCombiner checking
mullan
parents: 10336
diff changeset
     2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2589
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: 2589
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: 2589
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2589
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2589
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 java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.security.util.Debug;
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
    29
import sun.reflect.CallerSensitive;
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
    30
import sun.reflect.Reflection;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p> The AccessController class is used for access control operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * and decisions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p> More specifically, the AccessController class is used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * three purposes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <li> to decide whether an access to a critical system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * resource is to be allowed or denied, based on the security policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * currently in effect,<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <li>to mark code as being "privileged", thus affecting subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * access determinations, and<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <li>to obtain a "snapshot" of the current calling context so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * access-control decisions from a different context can be made with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * respect to the saved context. </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p> The {@link #checkPermission(Permission) checkPermission} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * determines whether the access request indicated by a specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * permission should be granted or denied. A sample call appears
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * below. In this example, <code>checkPermission</code> will determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * whether or not to grant "read" access to the file named "testFile" in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the "/temp" directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * FilePermission perm = new FilePermission("/temp/testFile", "read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * AccessController.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p> If a requested access is allowed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <code>checkPermission</code> returns quietly. If denied, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * AccessControlException is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * thrown. AccessControlException can also be thrown if the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * permission is of an incorrect type or contains an invalid value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Such information is given whenever possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Suppose the current thread traversed m callers, in the order of caller 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * to caller 2 to caller m. Then caller m invoked the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <code>checkPermission</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * The <code>checkPermission </code>method determines whether access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * is granted or denied based on the following algorithm:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * for (int i = m; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     if (caller i's domain does not have the permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *         throw AccessControlException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *     else if (caller i is marked as privileged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *         if (a context was specified in the call to doPrivileged)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *             context.checkPermission(permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *         return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * // Next, check the context inherited when the thread was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * // Whenever a new thread is created, the AccessControlContext at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * // that time is stored and associated with the new thread, as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * // "inherited" context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * inheritedContext.checkPermission(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * }</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <p> A caller can be marked as being "privileged"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * (see {@link #doPrivileged(PrivilegedAction) doPrivileged} and below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * When making access control decisions, the <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * method stops checking if it reaches a caller that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * was marked as "privileged" via a <code>doPrivileged</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * call without a context argument (see below for information about a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * context argument). If that caller's domain has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * specified permission, no further checking is done and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <code>checkPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * returns quietly, indicating that the requested access is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * If that domain does not have the specified permission, an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * is thrown, as usual.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <p> The normal use of the "privileged" feature is as follows. If you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * don't need to return a value from within the "privileged" block, do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * somemethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *     AccessController.doPrivileged(new PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *         public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *             // privileged code goes here, for example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *             System.loadLibrary("awt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *             return null; // nothing to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *     });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * }}</pre>
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
 * PrivilegedAction is an interface with a single method, named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <code>run</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * The above example shows creation of an implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * of that interface; a concrete implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <code>run</code> method is supplied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * When the call to <code>doPrivileged</code> is made, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * instance of the PrivilegedAction implementation is passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * to it. The <code>doPrivileged</code> method calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <code>run</code> method from the PrivilegedAction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * implementation after enabling privileges, and returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <code>run</code> method's return value as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <code>doPrivileged</code> return value (which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * ignored in this example).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * <p> If you need to return a value, you can do something like the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * somemethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *     String user = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *         new PrivilegedAction<String>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *         public String run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *             return System.getProperty("user.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *         });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <p>If the action performed in your <code>run</code> method could
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * throw a "checked" exception (those listed in the <code>throws</code> clause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * of a method), then you need to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <code>PrivilegedExceptionAction</code> interface instead of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <code>PrivilegedAction</code> interface:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * somemethod() throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *         FileInputStream fis = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *         new PrivilegedExceptionAction<FileInputStream>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *             public FileInputStream run() throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *                 return new FileInputStream("someFile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *         });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *     } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *         // e.getException() should be an instance of FileNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *         // as only "checked" exceptions will be "wrapped" in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *         // PrivilegedActionException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *         throw (FileNotFoundException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *  }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * <p> Be *very* careful in your use of the "privileged" construct, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * always remember to make the privileged code section as small as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * <p> Note that <code>checkPermission</code> always performs security checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * within the context of the currently executing thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * Sometimes a security check that should be made within a given context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * will actually need to be done from within a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * <i>different</i> context (for example, from within a worker thread).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * The {@link #getContext() getContext} method and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * AccessControlContext class are provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * for this situation. The <code>getContext</code> method takes a "snapshot"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * of the current calling context, and places
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * it in an AccessControlContext object, which it returns. A sample call is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * AccessControlContext acc = AccessController.getContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * AccessControlContext itself has a <code>checkPermission</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * that makes access decisions based on the context <i>it</i> encapsulates,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * rather than that of the current execution thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * Code within a different context can thus call that method on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * previously-saved AccessControlContext object. A sample call is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * acc.checkPermission(permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * <p> There are also times where you don't know a priori which permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * to check the context against. In these cases you can use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * doPrivileged method that takes a context:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * somemethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *     AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *         public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *             // Code goes here. Any permission checks within this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 *             // run method will require that the intersection of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *             // callers protection domain and the snapshot's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *             // context have the desired permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *     }, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *     ...normal code here...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * @see AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * @author Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
public final class AccessController {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Don't allow anyone to instantiate an AccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private AccessController() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Performs the specified <code>PrivilegedAction</code> with privileges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * enabled. The action is performed with <i>all</i> of the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * possessed by the caller's protection domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <p> If the action's <code>run</code> method throws an (unchecked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * exception, it will propagate through this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <p> Note that any DomainCombiner associated with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * AccessControlContext will be ignored while the action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param action the action to be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @return the value returned by the action's <code>run</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @exception NullPointerException if the action is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @see #doPrivileged(PrivilegedAction,AccessControlContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @see #doPrivileged(PrivilegedExceptionAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @see #doPrivilegedWithCombiner(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @see java.security.DomainCombiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   269
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public static native <T> T doPrivileged(PrivilegedAction<T> action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Performs the specified <code>PrivilegedAction</code> with privileges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * enabled. The action is performed with <i>all</i> of the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * possessed by the caller's protection domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * <p> If the action's <code>run</code> method throws an (unchecked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * exception, it will propagate through this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <p> This method preserves the current AccessControlContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * DomainCombiner (which may be null) while the action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param action the action to be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return the value returned by the action's <code>run</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @exception NullPointerException if the action is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @see #doPrivileged(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @see java.security.DomainCombiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   294
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    public static <T> T doPrivilegedWithCombiner(PrivilegedAction<T> action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        AccessControlContext acc = getStackAccessControlContext();
14213
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   297
        if (acc == null) {
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   298
            return AccessController.doPrivileged(action);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
14213
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   300
        DomainCombiner dc = acc.getAssignedCombiner();
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   301
        return AccessController.doPrivileged(action,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   302
                                             preserveCombiner(dc, Reflection.getCallerClass()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Performs the specified <code>PrivilegedAction</code> with privileges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * enabled and restricted by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * The action is performed with the intersection of the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * possessed by the caller's protection domain, and those possessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * by the domains represented by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * If the action's <code>run</code> method throws an (unchecked) exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * it will propagate through this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param action the action to be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param context an <i>access control context</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *                representing the restriction to be applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *                caller's domain's privileges before performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *                the specified action.  If the context is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *                <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *                then no additional restriction is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @return the value returned by the action's <code>run</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @exception NullPointerException if the action is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @see #doPrivileged(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   333
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public static native <T> T doPrivileged(PrivilegedAction<T> action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                            AccessControlContext context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Performs the specified <code>PrivilegedExceptionAction</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * privileges enabled.  The action is performed with <i>all</i> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * permissions possessed by the caller's protection domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p> If the action's <code>run</code> method throws an <i>unchecked</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * exception, it will propagate through this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p> Note that any DomainCombiner associated with the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * AccessControlContext will be ignored while the action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param action the action to be performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @return the value returned by the action's <code>run</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @exception PrivilegedActionException if the specified action's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *         <code>run</code> method threw a <i>checked</i> exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @exception NullPointerException if the action is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @see #doPrivileged(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @see #doPrivilegedWithCombiner(PrivilegedExceptionAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @see java.security.DomainCombiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   361
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public static native <T> T
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        doPrivileged(PrivilegedExceptionAction<T> action)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        throws PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Performs the specified <code>PrivilegedExceptionAction</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * privileges enabled.  The action is performed with <i>all</i> of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * permissions possessed by the caller's protection domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p> If the action's <code>run</code> method throws an <i>unchecked</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * exception, it will propagate through this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * <p> This method preserves the current AccessControlContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * DomainCombiner (which may be null) while the action is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @param action the action to be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @return the value returned by the action's <code>run</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @exception PrivilegedActionException if the specified action's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *         <code>run</code> method threw a <i>checked</i> exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @exception NullPointerException if the action is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @see #doPrivileged(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @see java.security.DomainCombiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   392
    @CallerSensitive
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   393
    public static <T> T doPrivilegedWithCombiner(PrivilegedExceptionAction<T> action)
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   394
        throws PrivilegedActionException
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   395
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        AccessControlContext acc = getStackAccessControlContext();
14213
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   397
        if (acc == null) {
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   398
            return AccessController.doPrivileged(action);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
14213
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   400
        DomainCombiner dc = acc.getAssignedCombiner();
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   401
        return AccessController.doPrivileged(action,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   402
                                             preserveCombiner(dc, Reflection.getCallerClass()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * preserve the combiner across the doPrivileged call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   408
    private static AccessControlContext preserveCombiner(DomainCombiner combiner,
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   409
                                                         Class<?> caller)
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   410
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        ProtectionDomain callerPd = doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            (new PrivilegedAction<ProtectionDomain>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            public ProtectionDomain run() {
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   414
                return caller.getProtectionDomain();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        // perform 'combine' on the caller of doPrivileged,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // even if the caller is from the bootclasspath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        ProtectionDomain[] pds = new ProtectionDomain[] {callerPd};
14213
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   421
        if (combiner == null) {
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   422
            return new AccessControlContext(pds);
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   423
        } else {
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   424
            return new AccessControlContext(combiner.combine(pds, null),
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   425
                                            combiner);
91ba926457c6 7189490: More improvements to DomainCombiner checking
mullan
parents: 14208
diff changeset
   426
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * Performs the specified <code>PrivilegedExceptionAction</code> with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * privileges enabled and restricted by the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <code>AccessControlContext</code>.  The action is performed with the
2589
af4853bc7e87 6827153: Miscellaneous typos in javadoc
martin
parents: 2
diff changeset
   434
     * intersection of the permissions possessed by the caller's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * protection domain, and those possessed by the domains represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * specified <code>AccessControlContext</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * If the action's <code>run</code> method throws an <i>unchecked</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * exception, it will propagate through this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param action the action to be performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param context an <i>access control context</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *                representing the restriction to be applied to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *                caller's domain's privileges before performing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *                the specified action.  If the context is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *                <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *                then no additional restriction is applied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @return the value returned by the action's <code>run</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @exception PrivilegedActionException if the specified action's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *         <code>run</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *         threw a <i>checked</i> exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @exception NullPointerException if the action is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @see #doPrivileged(PrivilegedAction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @see #doPrivileged(PrivilegedExceptionAction,AccessControlContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 14213
diff changeset
   459
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public static native <T> T
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        doPrivileged(PrivilegedExceptionAction<T> action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                     AccessControlContext context)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        throws PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Returns the AccessControl context. i.e., it gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * the protection domains of all the callers on the stack,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * starting at the first class with a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @return the access control context based on the current stack or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *         null if there was only privileged system code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    private static native AccessControlContext getStackAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Returns the "inherited" AccessControl context. This is the context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * that existed when the thread was created. Package private so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * AccessControlContext can use it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    static native AccessControlContext getInheritedAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * This method takes a "snapshot" of the current calling context, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * includes the current Thread's inherited AccessControlContext,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * and places it in an AccessControlContext object. This context may then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * be checked at a later point, possibly in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @see AccessControlContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @return the AccessControlContext based on the current context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public static AccessControlContext getContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        AccessControlContext acc = getStackAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (acc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            // all we had was privileged system code. We don't want
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            // to return null though, so we construct a real ACC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            return new AccessControlContext(null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return acc.optimize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Determines whether the access request indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * specified permission should be allowed or denied, based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * the current AccessControlContext and security policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * This method quietly returns if the access request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * is permitted, or throws an AccessControlException otherwise. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * getPermission method of the AccessControlException returns the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <code>perm</code> Permission object instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @param perm the requested permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @exception AccessControlException if the specified permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *            is not permitted, based on the current security policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @exception NullPointerException if the specified permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *            is <code>null</code> and is checked based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *            security policy currently in effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public static void checkPermission(Permission perm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                 throws AccessControlException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        //System.err.println("checkPermission "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        //Thread.currentThread().dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if (perm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            throw new NullPointerException("permission can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        AccessControlContext stack = getStackAccessControlContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        // if context is null, we had privileged system code on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (stack == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            Debug debug = AccessControlContext.getDebug();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            boolean dumpDebug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                dumpDebug = !Debug.isOn("codebase=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                dumpDebug &= !Debug.isOn("permission=") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    Debug.isOn("permission=" + perm.getClass().getCanonicalName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if (dumpDebug && Debug.isOn("stack")) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   548
                Thread.dumpStack();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            if (dumpDebug && Debug.isOn("domain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                debug.println("domain (context is null)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if (dumpDebug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                debug.println("access allowed "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        AccessControlContext acc = stack.optimize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        acc.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
}