jdk/src/share/classes/java/security/ProtectionDomain.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 7970 af1579474d16
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
     2
 * Copyright (c) 1997, 2009, 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: 5183
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: 5183
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: 5183
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5183
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
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    28
import java.util.ArrayList;
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    29
import java.util.Collections;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.List;
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    32
import java.util.Map;
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    33
import java.util.WeakHashMap;
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    34
import sun.misc.JavaSecurityProtectionDomainAccess;
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    35
import static sun.misc.JavaSecurityProtectionDomainAccess.ProtectionDomainCache;
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    36
import sun.misc.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * This ProtectionDomain class encapsulates the characteristics of a domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * which encloses a set of classes whose instances are granted a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * of permissions when being executed on behalf of a given set of Principals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * A static set of permissions can be bound to a ProtectionDomain when it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * constructed; such permissions are granted to the domain regardless of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Policy in force. However, to support dynamic security policies, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * ProtectionDomain can also be constructed such that it is dynamically
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * mapped to a set of permissions by the current Policy whenever a permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Li Gong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Gary Ellison
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class ProtectionDomain {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* CodeSource */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private CodeSource codesource ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /* ClassLoader the protection domain was consed from */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private ClassLoader classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /* Principals running-as within this protection domain */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private Principal[] principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* the rights this protection domain is granted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private PermissionCollection permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /* if the permissions object has AllPermission */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private boolean hasAllPerm = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /* the PermissionCollection is static (pre 1.4 constructor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
       or dynamic (via a policy refresh) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean staticPermissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    81
    /*
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    82
     * An object used as a key when the ProtectionDomain is stored in a Map.
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    83
     */
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    84
    final Key key = new Key();
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
    85
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final Debug debug = Debug.getInstance("domain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Creates a new ProtectionDomain with the given CodeSource and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Permissions. If the permissions object is not null, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *  <code>setReadOnly())</code> will be called on the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Permissions object. The only permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * are the ones specified; the current Policy will not be consulted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param codesource the codesource associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param permissions the permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public ProtectionDomain(CodeSource codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                            PermissionCollection permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        this.codesource = codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            this.permissions = permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            this.permissions.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (permissions instanceof Permissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                ((Permissions)permissions).allPermission != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                hasAllPerm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this.classloader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this.principals = new Principal[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        staticPermissions = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Creates a new ProtectionDomain qualified by the given CodeSource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Permissions, ClassLoader and array of Principals. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * permissions object is not null, then <code>setReadOnly()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * will be called on the passed in Permissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * The permissions granted to this domain are dynamic; they include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * both the static permissions passed to this constructor, and any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * permissions granted to this domain by the current Policy at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * time a permission is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * This constructor is typically used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * {@link SecureClassLoader ClassLoaders}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * and {@link DomainCombiner DomainCombiners} which delegate to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <code>Policy</code> to actively associate the permissions granted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * this domain. This constructor affords the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Policy provider the opportunity to augment the supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * PermissionCollection to reflect policy changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param codesource the CodeSource associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param permissions the permissions granted to this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param classloader the ClassLoader associated with this domain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param principals the array of Principals associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * domain. The contents of the array are copied to protect against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * subsequent modification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @see Policy#refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @see Policy#getPermissions(ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public ProtectionDomain(CodeSource codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                            PermissionCollection permissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                            ClassLoader classloader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                            Principal[] principals) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        this.codesource = codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            this.permissions = permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            this.permissions.setReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            if (permissions instanceof Permissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                ((Permissions)permissions).allPermission != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                hasAllPerm = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.classloader = classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.principals = (principals != null ? principals.clone():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                           new Principal[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        staticPermissions = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Returns the CodeSource of this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return the CodeSource of this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public final CodeSource getCodeSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return this.codesource;
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
     * Returns the ClassLoader of this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return the ClassLoader of this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public final ClassLoader getClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return this.classloader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Returns an array of principals for this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @return a non-null array of principals for this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Returns a new array each time this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public final Principal[] getPrincipals() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return this.principals.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Returns the static permissions granted to this domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @return the static set of permissions for this domain which may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @see Policy#refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see Policy#getPermissions(ProtectionDomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public final PermissionCollection getPermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Check and see if this ProtectionDomain implies the permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * expressed in the Permission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * The set of permissions evaluated is a function of whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * ProtectionDomain was constructed with a static set of permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * or it was bound to a dynamically mapped set of permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * If the ProtectionDomain was constructed to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * {@link #ProtectionDomain(CodeSource, PermissionCollection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * statically bound} PermissionCollection then the permission will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * only be checked against the PermissionCollection supplied at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * However, if the ProtectionDomain was constructed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * the constructor variant which supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * {@link #ProtectionDomain(CodeSource, PermissionCollection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * ClassLoader, java.security.Principal[]) dynamically binding}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * permissions, then the permission will be checked against the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * combination of the PermissionCollection supplied at construction and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * the current Policy binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param permission the Permission object to check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @return true if "permission" is implicit to this ProtectionDomain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (hasAllPerm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            // internal permission collection already has AllPermission -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            // no need to go to policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (!staticPermissions &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            Policy.getPolicyNoCheck().implies(this, permission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (permissions != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return permissions.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Convert a ProtectionDomain to a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   252
    @Override public String toString() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        String pals = "<no principals>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (principals != null && principals.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            StringBuilder palBuf = new StringBuilder("(principals ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            for (int i = 0; i < principals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                palBuf.append(principals[i].getClass().getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                            " \"" + principals[i].getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (i < principals.length-1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    palBuf.append(",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    palBuf.append(")\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            pals = palBuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        // Check if policy is set; we don't want to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // the policy prematurely here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        PermissionCollection pc = Policy.isSet() && seeAllp() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                      mergePermissions():
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                      getPermissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return "ProtectionDomain "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            " "+codesource+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            " "+classloader+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            " "+pals+"\n"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            " "+pc+"\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Return true (merge policy permissions) in the following cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * . SecurityManager is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * . SecurityManager is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *          debug is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *          SecurityManager impelmentation is in bootclasspath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *          Policy implementation is in bootclasspath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *          (the bootclasspath restrictions avoid recursion)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * . SecurityManager is not null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *          debug is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *          caller has Policy.getPolicy permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    private static boolean seeAllp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (sm.getClass().getClassLoader() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    Policy.getPolicyNoCheck().getClass().getClassLoader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                                                == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    sm.checkPermission(SecurityConstants.GET_POLICY_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    // fall thru and return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private PermissionCollection mergePermissions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (staticPermissions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            return permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        PermissionCollection perms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            (new java.security.PrivilegedAction<PermissionCollection>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    public PermissionCollection run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        Policy p = Policy.getPolicyNoCheck();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        return p.getPermissions(ProtectionDomain.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        Permissions mergedPerms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int swag = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        int vcap = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        Enumeration<Permission> e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        List<Permission> pdVector = new ArrayList<Permission>(vcap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        List<Permission> plVector = new ArrayList<Permission>(swag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // Build a vector of domain permissions for subsequent merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            synchronized (permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                e = permissions.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    pdVector.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        // Build a vector of Policy permissions for subsequent merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (perms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            synchronized (perms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                e = perms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    plVector.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    vcap++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (perms != null && permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            // Weed out the duplicates from the policy. Unless a refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            // has occured since the pd was consed this should result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            // an empty vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            synchronized (permissions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                e = permissions.elements();   // domain vs policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    Permission pdp = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    Class pdpClass = pdp.getClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    String pdpActions = pdp.getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    String pdpName = pdp.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    for (int i = 0; i < plVector.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        Permission pp = plVector.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        if (pdpClass.isInstance(pp)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                            // The equals() method on some permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                            // have some side effects so this manual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                            // comparison is sufficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                            if (pdpName.equals(pp.getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                pdpActions.equals(pp.getActions())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                plVector.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (perms !=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            // the order of adding to merged perms and permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            // needs to preserve the bugfix 4301064
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            for (int i = plVector.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                mergedPerms.add(plVector.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        if (permissions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            for (int i = pdVector.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                mergedPerms.add(pdVector.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return mergedPerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
5183
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   410
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   411
    /**
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   412
     * Used for storing ProtectionDomains as keys in a Map.
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   413
     */
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   414
    final class Key {}
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   415
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   416
    static {
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   417
        SharedSecrets.setJavaSecurityProtectionDomainAccess(
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   418
            new JavaSecurityProtectionDomainAccess() {
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   419
                public ProtectionDomainCache getProtectionDomainCache() {
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   420
                    return new ProtectionDomainCache() {
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   421
                        private final Map<Key, PermissionCollection> map =
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   422
                            Collections.synchronizedMap
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   423
                                (new WeakHashMap<Key, PermissionCollection>());
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   424
                        public void put(ProtectionDomain pd,
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   425
                            PermissionCollection pc) {
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   426
                            map.put((pd == null ? null : pd.key), pc);
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   427
                        }
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   428
                        public PermissionCollection get(ProtectionDomain pd) {
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   429
                            return pd == null ? map.get(null) : map.get(pd.key);
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   430
                        }
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   431
                    };
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   432
                }
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   433
            });
7825d97c4cf2 6633872: Policy/PolicyFile leak dynamic ProtectionDomains.
mullan
parents: 2
diff changeset
   434
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
}