jdk/src/share/classes/com/sun/security/auth/PolicyFile.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 4350 2a593a20d962
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.security.auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.RuntimePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Identity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.IdentityScope;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.Permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.UnresolvedPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.security.auth.PrivateCredentialPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.security.util.PropertyExpander;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * This class represents a default implementation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <code>javax.security.auth.Policy</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p> This object stores the policy for entire Java runtime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * and is the amalgamation of multiple static policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * configurations that resides in files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * The algorithm for locating the policy file(s) and reading their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * information into this <code>Policy</code> object is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   Loop through the <code>java.security.Security</code> properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   <i>auth.policy.url.1</i>, <i>auth.policy.url.2</i>, ...,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   <i>auth.policy.url.X</i>".  These properties are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   in the Java security properties file, which is located in the file named
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   &lt;JAVA_HOME&gt;/lib/security/java.security.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   &lt;JAVA_HOME&gt; refers to the value of the java.home system property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *   and specifies the directory where the JRE is installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *   Each property value specifies a <code>URL</code> pointing to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   policy file to be loaded.  Read in and load each policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   The <code>java.lang.System</code> property <i>java.security.auth.policy</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   may also be set to a <code>URL</code> pointing to another policy file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   (which is the case when a user uses the -D switch at runtime).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *   If this property is defined, and its use is allowed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *   security property file (the Security property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *   <i>policy.allowSystemProperty</i> is set to <i>true</i>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *   also load that policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *   If the <i>java.security.auth.policy</i> property is defined using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   "==" (rather than "="), then ignore all other specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   policies and only load this policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * Each policy file consists of one or more grant entries, each of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * which consists of a number of permission entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   grant signedBy "<b>alias</b>", codeBase "<b>URL</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *         principal <b>principalClass</b> "<b>principalName</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *         principal <b>principalClass</b> "<b>principalName</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *         ... {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *     permission <b>Type</b> "<b>name</b> "<b>action</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *         signedBy "<b>alias</b>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *     permission <b>Type</b> "<b>name</b> "<b>action</b>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *         signedBy "<b>alias</b>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *     ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *   };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * All non-bold items above must appear as is (although case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * doesn't matter and some are optional, as noted below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * Italicized items represent variable values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <p> A grant entry must begin with the word <code>grant</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * The <code>signedBy</code> and <code>codeBase</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * name/value pairs are optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * If they are not present, then any signer (including unsigned code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * will match, and any codeBase will match.  Note that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <code>principal</code> name/value pair is not optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * This <code>Policy</code> implementation only permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * Principal-based grant entries.  Note that the <i>principalClass</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * may be set to the wildcard value, *, which allows it to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * any <code>Principal</code> class.  In addition, the <i>principalName</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * may also be set to the wildcard value, *, allowing it to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * any <code>Principal</code> name.  When setting the <i>principalName</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * to the *, do not surround the * with quotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <p> A permission entry must begin with the word <code>permission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * The word <code><i>Type</i></code> in the template above is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * a specific permission type, such as <code>java.io.FilePermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * or <code>java.lang.RuntimePermission</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * <p> The "<i>action</i>" is required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * many permission types, such as <code>java.io.FilePermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * (where it specifies what type of file access that is permitted).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * It is not required for categories such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <code>java.lang.RuntimePermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * where it is not necessary - you either have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * permission specified by the <code>"<i>name</i>"</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * value following the type name or you don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <p> The <code>signedBy</code> name/value pair for a permission entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * is optional. If present, it indicates a signed permission. That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * the permission class itself must be signed by the given alias in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * order for it to be granted. For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * suppose you have the following grant entry:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *   grant principal foo.com.Principal "Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *     permission Foo "foobar", signedBy "FooSoft";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <p> Then this permission of type <i>Foo</i> is granted if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * <code>Foo.class</code> permission has been signed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * "FooSoft" alias, or if <code>Foo.class</code> is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * system class (i.e., is found on the CLASSPATH).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * <p> Items that appear in an entry must appear in the specified order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * (<code>permission</code>, <i>Type</i>, "<i>name</i>", and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * "<i>action</i>"). An entry is terminated with a semicolon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * <p> Case is unimportant for the identifiers (<code>permission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <code>signedBy</code>, <code>codeBase</code>, etc.) but is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * significant for the <i>Type</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * or for any string that is passed in as a value. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <p> An example of two entries in a policy configuration file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *   // if the code is comes from "foo.com" and is running as "Duke",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *   // grant it read/write to all files in /tmp.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *   grant codeBase "foo.com", principal foo.com.Principal "Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *              permission java.io.FilePermission "/tmp/*", "read,write";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *   };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *   // grant any code running as "Duke" permission to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *   // the "java.vendor" Property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *   grant principal foo.com.Principal "Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *         permission java.util.PropertyPermission "java.vendor";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * <p> This <code>Policy</code> implementation supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * special handling for PrivateCredentialPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * If a grant entry is configured with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * <code>PrivateCredentialPermission</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * and the "Principal Class/Principal Name" for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * <code>PrivateCredentialPermission</code> is "self",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * then the entry grants the specified <code>Subject</code> permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * access its own private Credential.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * the following grants the <code>Subject</code> "Duke"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * access to its own a.b.Credential.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *   grant principal foo.com.Principal "Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *      permission javax.security.auth.PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *              "a.b.Credential self",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *              "read";
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
 * The following grants the <code>Subject</code> "Duke"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * access to all of its own private Credentials:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *   grant principal foo.com.Principal "Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *      permission javax.security.auth.PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *              "* self",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *              "read";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * The following grants all Subjects authenticated as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * <code>SolarisPrincipal</code> (regardless of their respective names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * permission to access their own private Credentials:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 *   grant principal com.sun.security.auth.SolarisPrincipal * {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 *      permission javax.security.auth.PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *              "* self",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 *              "read";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * The following grants all Subjects permission to access their own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * private Credentials:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *   grant principal * * {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *      permission javax.security.auth.PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *              "* self",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *              "read";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * @deprecated As of JDK&nbsp;1.4, replaced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *             <code>sun.security.provider.PolicyFile</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *             This class is entirely deprecated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 * @see java.security.CodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * @see java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
public class PolicyFile extends javax.security.auth.Policy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    static final java.util.ResourceBundle rb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        (new java.security.PrivilegedAction<java.util.ResourceBundle>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            public java.util.ResourceBundle run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                return (java.util.ResourceBundle.getBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        ("sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    // needs to be package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private static final sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        sun.security.util.Debug.getInstance("policy", "\t[Auth Policy]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private static final String AUTH_POLICY = "java.security.auth.policy";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static final String SECURITY_MANAGER = "java.security.manager";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private static final String AUTH_POLICY_URL = "auth.policy.url.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private Vector<PolicyEntry> policyEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private Hashtable aliasMapping;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private boolean expandProperties = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private boolean ignoreIdentityScope = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    // for use with the reflection API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private static final Class[] PARAMS = { String.class, String.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Initializes the Policy object and reads the default policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * configuration file(s) into the Policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public PolicyFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // initialize Policy if either the AUTH_POLICY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // SECURITY_MANAGER properties are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        String prop = System.getProperty(AUTH_POLICY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (prop == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            prop = System.getProperty(SECURITY_MANAGER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        if (prop != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    private synchronized void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        policyEntries = new Vector<PolicyEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        aliasMapping = new Hashtable(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        initPolicyFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Refreshes the policy object by re-reading all the policy files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @exception SecurityException if the caller doesn't have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *          to refresh the <code>Policy</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public synchronized void refresh()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            sm.checkPermission(new javax.security.auth.AuthPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                ("refreshPolicy"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // 1)   if code instantiates PolicyFile directly, then it will need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        //      all the permissions required for the PolicyFile initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // 2)   if code calls Policy.getPolicy, then it simply needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        //      AuthPermission(getPolicy), and the javax.security.auth.Policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        //      implementation instantiates PolicyFile in a doPrivileged block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // 3)   if after instantiating a Policy (either via #1 or #2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        //      code calls refresh, it simply needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        //      AuthPermission(refreshPolicy).  then PolicyFile wraps
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        //      the refresh in a doPrivileged block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            (new java.security.PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private KeyStore initKeyStore(URL policyUrl, String keyStoreName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                  String keyStoreType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (keyStoreName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                 * location of keystore is specified as absolute URL in policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                 * file, or is relative to URL of policy file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                URL keyStoreUrl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    keyStoreUrl = new URL(keyStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    // absolute URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                } catch (java.net.MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    // relative URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    keyStoreUrl = new URL(policyUrl, keyStoreName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    debug.println("reading keystore"+keyStoreUrl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                InputStream inStream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    new BufferedInputStream(getInputStream(keyStoreUrl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                if (keyStoreType != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    ks = KeyStore.getInstance(keyStoreType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    ks = KeyStore.getInstance(KeyStore.getDefaultType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                ks.load(inStream, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                inStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                // ignore, treat it like we have no keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    private void initPolicyFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        String prop = Security.getProperty("policy.expandProperties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if (prop != null) expandProperties = prop.equalsIgnoreCase("true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        String iscp = Security.getProperty("policy.ignoreIdentityScope");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (iscp != null) ignoreIdentityScope = iscp.equalsIgnoreCase("true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        String allowSys  = Security.getProperty("policy.allowSystemProperty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if ((allowSys!=null) && allowSys.equalsIgnoreCase("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            String extra_policy = System.getProperty(AUTH_POLICY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (extra_policy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                boolean overrideAll = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                if (extra_policy.startsWith("=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    overrideAll = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    extra_policy = extra_policy.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    extra_policy = PropertyExpander.expand(extra_policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    URL policyURL;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    File policyFile = new File(extra_policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    if (policyFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        policyURL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                            new URL("file:" + policyFile.getCanonicalPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        policyURL = new URL(extra_policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        debug.println("reading "+policyURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    init(policyURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    // ignore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        debug.println("caught exception: "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                if (overrideAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        debug.println("overriding other policies!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        int n = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        boolean loaded_one = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        String policy_url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        while ((policy_url = Security.getProperty(AUTH_POLICY_URL+n)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                policy_url = PropertyExpander.expand(policy_url).replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                                (File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    debug.println("reading "+policy_url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                init(new URL(policy_url));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                loaded_one = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    debug.println("error reading policy "+e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                // ignore that policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (loaded_one == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            // do not load a static policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /** the scope to check */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    private static IdentityScope scope = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Checks public key. If it is marked as trusted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * the identity database, add it to the policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * with the AllPermission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    private boolean checkForTrustedIdentity(final Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // XXX  JAAS has no way to access the SUN package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        //      we'll add this back in when JAAS goes into core.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Reads a policy configuration into the Policy object using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Reader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param policyFile the policy Reader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private void init(URL policy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        PolicyParser pp = new PolicyParser(expandProperties);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            InputStreamReader isr
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                = new InputStreamReader(getInputStream(policy));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            pp.read(isr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            isr.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            KeyStore keyStore = initKeyStore(policy, pp.getKeyStoreUrl(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                                             pp.getKeyStoreType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            Enumeration<PolicyParser.GrantEntry> enum_ = pp.grantElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                PolicyParser.GrantEntry ge = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                addGrantEntry(ge, keyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        } catch (PolicyParser.ParsingException pe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            System.err.println(AUTH_POLICY +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                                rb.getString(": error parsing ") + policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            System.err.println(AUTH_POLICY +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                                rb.getString(": ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                                pe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                pe.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                debug.println("error parsing "+policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                debug.println(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Fast path reading from file urls in order to avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * FileURLConnection.connect() which can be quite slow the first time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * it is called. We really should clean up FileURLConnection so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * this is not a problem but in the meantime this fix helps reduce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * start up time noticeably for the new launcher. -- DAC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    private InputStream getInputStream(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if ("file".equals(url.getProtocol())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            String path = url.getFile().replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            return new FileInputStream(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            return url.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Given a PermissionEntry, create a codeSource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @return null if signedBy alias is not recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    CodeSource getCodeSource(PolicyParser.GrantEntry ge, KeyStore keyStore)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        throws java.net.MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        Certificate[] certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (ge.signedBy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            certs = getCertificates(keyStore, ge.signedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            if (certs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                // we don't have a key for this alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                // just return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                    debug.println(" no certs for alias " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                                       ge.signedBy + ", ignoring.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        URL location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (ge.codeBase != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            location = new URL(ge.codeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            location = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (ge.principals == null || ge.principals.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            return (canonicalizeCodebase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        (new CodeSource(location, certs),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            return (canonicalizeCodebase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                (new SubjectCodeSource(null, ge.principals, location, certs),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Add one policy entry to the vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    private void addGrantEntry(PolicyParser.GrantEntry ge,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                               KeyStore keyStore) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            debug.println("Adding policy entry: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            debug.println("  signedBy " + ge.signedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            debug.println("  codeBase " + ge.codeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            if (ge.principals != null && ge.principals.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                ListIterator<PolicyParser.PrincipalEntry> li =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                                                ge.principals.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                while (li.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    PolicyParser.PrincipalEntry pppe = li.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    debug.println("  " + pppe.principalClass +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                        " " + pppe.principalName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            debug.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            CodeSource codesource = getCodeSource(ge, keyStore);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            // skip if signedBy alias was unknown...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (codesource == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            PolicyEntry entry = new PolicyEntry(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            Enumeration<PolicyParser.PermissionEntry> enum_ =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                                                ge.permissionElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                PolicyParser.PermissionEntry pe = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    // XXX special case PrivateCredentialPermission-SELF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    Permission perm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    if (pe.permission.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                        ("javax.security.auth.PrivateCredentialPermission") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        pe.name.endsWith(" self")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        perm = getInstance(pe.permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                                         pe.name + " \"self\"",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                                         pe.action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        perm = getInstance(pe.permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                                         pe.name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                         pe.action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    entry.add(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                        debug.println("  "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    Certificate certs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    if (pe.signedBy != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                        certs = getCertificates(keyStore, pe.signedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                    // only add if we had no signer or we had a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    // a signer and found the keys for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    if (certs != null || pe.signedBy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                            Permission perm = new UnresolvedPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                             pe.permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                             pe.name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                                             pe.action,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                                             certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                            entry.add(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                                debug.println("  "+perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                } catch (java.lang.reflect.InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    System.err.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                        (AUTH_POLICY +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        rb.getString(": error adding Permission ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        pe.permission +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        rb.getString(" ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                        ite.getTargetException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    System.err.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        (AUTH_POLICY +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        rb.getString(": error adding Permission ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        pe.permission +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                        rb.getString(" ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            policyEntries.addElement(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            System.err.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                (AUTH_POLICY +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                rb.getString(": error adding Entry ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                ge +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                rb.getString(" ") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            debug.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Returns a new Permission object of the given Type. The Permission is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * created by getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Class object using the <code>Class.forName</code> method, and using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * the reflection API to invoke the (String name, String actions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * constructor on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @param type the type of Permission being created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @param name the name of the Permission being created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @param actions the actions of the Permission being created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @exception  ClassNotFoundException  if the particular Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *             class could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @exception  IllegalAccessException  if the class or initializer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *               not accessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @exception  InstantiationException  if getInstance tries to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *               instantiate an abstract class or an interface, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *               instantiation fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @exception  NoSuchMethodException if the (String, String) constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *               is not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @exception  InvocationTargetException if the underlying Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *               constructor throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    private static final Permission getInstance(String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                    String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                                    String actions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        throws ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
               InstantiationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
               IllegalAccessException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
               NoSuchMethodException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
               InvocationTargetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        //XXX we might want to keep a hash of created factories...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        Class pc = Class.forName(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        Constructor c = pc.getConstructor(PARAMS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        return (Permission) c.newInstance(new Object[] { name, actions });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Fetch all certs associated with this alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    Certificate[] getCertificates(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                                    KeyStore keyStore, String aliases) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        Vector<Certificate> vcerts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        StringTokenizer st = new StringTokenizer(aliases, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
            String alias = st.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            Certificate cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            //See if this alias's cert has already been cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            cert = (Certificate) aliasMapping.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            if (cert == null && keyStore != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    cert = keyStore.getCertificate(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                } catch (KeyStoreException kse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    // never happens, because keystore has already been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    // when we call this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                if (cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    aliasMapping.put(alias, cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    aliasMapping.put(cert, alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            if (cert != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                if (vcerts == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    vcerts = new Vector<Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                vcerts.addElement(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        // make sure n == vcerts.size, since we are doing a logical *and*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (vcerts != null && n == vcerts.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            Certificate[] certs = new Certificate[vcerts.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            vcerts.copyInto(certs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * Enumerate all the entries in the global policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * This method is used by policy admin tools.   The tools
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * should use the Enumeration methods on the returned object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * to fetch the elements sequentially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    private final synchronized Enumeration<PolicyEntry> elements(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        return policyEntries.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * Examines this <code>Policy</code> and returns the Permissions granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * to the specified <code>Subject</code> and <code>CodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * <p> Permissions for a particular <i>grant</i> entry are returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * if the <code>CodeSource</code> constructed using the codebase and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * signedby values specified in the entry <code>implies</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * the <code>CodeSource</code> provided to this method, and if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * <code>Subject</code> provided to this method contains all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * Principals specified in the entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * <p> The <code>Subject</code> provided to this method contains all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * of the Principals specified in the entry if, for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * <code>Principal</code>, "P1", specified in the <i>grant</i> entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * one of the following two conditions is met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * <li> the <code>Subject</code> has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *      <code>Principal</code>, "P2", where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *      <code>P2.getClass().getName()</code> equals the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *      P1's class name, and where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *      <code>P2.getName()</code> equals the P1's name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * <li> P1 implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *      <code>com.sun.security.auth.PrincipalComparator</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     *      and <code>P1.implies</code> the provided <code>Subject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * <p> Note that this <code>Policy</code> implementation has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * special handling for PrivateCredentialPermissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * When this method encounters a <code>PrivateCredentialPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * which specifies "self" as the <code>Principal</code> class and name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * it does not add that <code>Permission</code> to the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * <code>PermissionCollection</code>.  Instead, it builds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * a new <code>PrivateCredentialPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * for each <code>Principal</code> associated with the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * <code>Subject</code>.  Each new <code>PrivateCredentialPermission</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * contains the same Credential class as specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * originally granted permission, as well as the Class and name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * for the respective <code>Principal</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * @param subject the Permissions granted to this <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *          and the additionally provided <code>CodeSource</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *          are returned. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @param codesource the Permissions granted to this <code>CodeSource</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *          and the additionally provided <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *          are returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @return the Permissions granted to the provided <code>Subject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     *          <code>CodeSource</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    public PermissionCollection getPermissions(final Subject subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                                        final CodeSource codesource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        // XXX  when JAAS goes into the JDK core,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        //      we can remove this method and simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        //      rely on the getPermissions variant that takes a codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        //      which no one can use at this point in time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        //      at that time, we can also make SubjectCodeSource a public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        //      class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        // 1)   if code instantiates PolicyFile directly, then it will need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        //      all the permissions required for the PolicyFile initialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        // 2)   if code calls Policy.getPolicy, then it simply needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        //      AuthPermission(getPolicy), and the javax.security.auth.Policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        //      implementation instantiates PolicyFile in a doPrivileged block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        // 3)   if after instantiating a Policy (either via #1 or #2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        //      code calls getPermissions, PolicyFile wraps the call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        //      in a doPrivileged block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        return java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            (new java.security.PrivilegedAction<PermissionCollection>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            public PermissionCollection run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                SubjectCodeSource scs = new SubjectCodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    (subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                    null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    codesource == null ? null : codesource.getLocation(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    codesource == null ? null : codesource.getCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                if (initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    return getPermissions(new Permissions(), scs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    return new PolicyPermissions(PolicyFile.this, scs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * Examines the global policy for the specified CodeSource, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * creates a PermissionCollection object with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * the set of permissions for that principal's protection domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * @param CodeSource the codesource associated with the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * This encapsulates the original location of the code (where the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * came from) and the public key(s) of its signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * @return the set of permissions according to the policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    PermissionCollection getPermissions(CodeSource codesource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        if (initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            return getPermissions(new Permissions(), codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            return new PolicyPermissions(this, codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * Examines the global policy for the specified CodeSource, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * creates a PermissionCollection object with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * the set of permissions for that principal's protection domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @param permissions the permissions to populate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @param codesource the codesource associated with the caller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * This encapsulates the original location of the code (where the code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * came from) and the public key(s) of its signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @return the set of permissions according to the policy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    Permissions getPermissions(final Permissions perms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                               final CodeSource cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        final CodeSource codesource[] = {null};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        codesource[0] = canonicalizeCodebase(cs, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            debug.println("evaluate("+codesource[0]+")\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        // needs to be in a begin/endPrivileged block because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        // codesource.implies calls URL.equals which does an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        // InetAddress lookup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        for (int i = 0; i < policyEntries.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
           PolicyEntry entry = policyEntries.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
           if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                debug.println("PolicyFile CodeSource implies: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                        entry.codesource.toString() + "\n\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                        "\t" + codesource[0].toString() + "\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
           if (entry.codesource.implies(codesource[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
               for (int j = 0; j < entry.permissions.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                    Permission p = entry.permissions.elementAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                       debug.println("  granting " + p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    if (!addSelfPermissions(p, entry.codesource,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                                        codesource[0], perms)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                        // we could check for duplicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                        // before adding new permissions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                        // but the SubjectDomainCombiner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                        // already checks for duplicates later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                        perms.add(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        // now see if any of the keys are trusted ids.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        if (!ignoreIdentityScope) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            Certificate certs[] = codesource[0].getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            if (certs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                for (int k=0; k < certs.length; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    if ((aliasMapping.get(certs[k]) == null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                        checkForTrustedIdentity(certs[k])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                        // checkForTrustedIdentity added it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                        // to the policy for us. next time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                        // around we'll find it. This time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                        // around we need to add it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                        perms.add(new java.security.AllPermission());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * Returns true if 'Self' permissions were added to the provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * 'perms', and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * @param p check to see if this Permission is a "SELF"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     *                  PrivateCredentialPermission. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @param entryCs the codesource for the Policy entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * @param accCs the codesource for from the current AccessControlContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * @param perms the PermissionCollection where the individual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *                  PrivateCredentialPermissions will be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    private boolean addSelfPermissions(final Permission p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                                CodeSource entryCs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                                CodeSource accCs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                                Permissions perms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        if (!(p instanceof PrivateCredentialPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        if (!(entryCs instanceof SubjectCodeSource))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        PrivateCredentialPermission pcp = (PrivateCredentialPermission)p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        SubjectCodeSource scs = (SubjectCodeSource)entryCs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
        // see if it is a SELF permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        String[][] pPrincipals = pcp.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        if (pPrincipals.length <= 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            !pPrincipals[0][0].equalsIgnoreCase("self") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            !pPrincipals[0][1].equalsIgnoreCase("self")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            // regular PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            // granted a SELF permission - create a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            // PrivateCredentialPermission for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            // of the Policy entry's CodeSource Principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            if (scs.getPrincipals() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                // XXX SubjectCodeSource has no Subject???
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            ListIterator<PolicyParser.PrincipalEntry> pli =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                                        scs.getPrincipals().listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            while (pli.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                PolicyParser.PrincipalEntry principal = pli.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                // XXX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                //      if the Policy entry's Principal does not contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                //              WILDCARD for the Principal name, then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                //              new PrivateCredentialPermission is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                //              for the Principal listed in the Policy entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                //      if the Policy entry's Principal contains a WILDCARD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                //              for the Principal name, then a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                //              PrivateCredentialPermission is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                //              for each Principal associated with the Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                //              in the current ACC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                String[][] principalInfo = getPrincipalInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                                                (principal, accCs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                for (int i = 0; i < principalInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                    // here's the new PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    PrivateCredentialPermission newPcp =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                        new PrivateCredentialPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                                (pcp.getCredentialClass() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                                        " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                                        principalInfo[i][0] +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                                        " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                                        "\"" + principalInfo[i][1] + "\"",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                                "read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                        debug.println("adding SELF permission: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                                        newPcp.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    perms.add(newPcp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * return the principal class/name pair in the 2D array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * array[x][y]:     x corresponds to the array length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     *                  if (y == 0), it's the principal class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *                  if (y == 1), it's the principal name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    private String[][] getPrincipalInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                (PolicyParser.PrincipalEntry principal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                final CodeSource accCs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        // there are 3 possibilities:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        // 1) the entry's Principal class and name are not wildcarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        // 2) the entry's Principal name is wildcarded only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        // 3) the entry's Principal class and name are wildcarded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        if (!principal.principalClass.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                (PolicyParser.PrincipalEntry.WILDCARD_CLASS) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            !principal.principalName.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                (PolicyParser.PrincipalEntry.WILDCARD_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            // build a PrivateCredentialPermission for the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            // from the Policy entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            String[][] info = new String[1][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            info[0][0] = principal.principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            info[0][1] = principal.principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        } else if (!principal.principalClass.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                (PolicyParser.PrincipalEntry.WILDCARD_CLASS) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            principal.principalName.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                (PolicyParser.PrincipalEntry.WILDCARD_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            // build a PrivateCredentialPermission for all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            // the Subject's principals that are instances of principalClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            // the accCs is guaranteed to be a SubjectCodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            // because the earlier CodeSource.implies succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            SubjectCodeSource scs = (SubjectCodeSource)accCs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            Set<Principal> principalSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                Class pClass = Class.forName(principal.principalClass, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                                ClassLoader.getSystemClassLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                principalSet = scs.getSubject().getPrincipals(pClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                    debug.println("problem finding Principal Class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                                "when expanding SELF permission: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                                e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
            if (principalSet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                // error
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                return new String[0][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            String[][] info = new String[principalSet.size()][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            java.util.Iterator<Principal> pIterator = principalSet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            while (pIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                Principal p = pIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                info[i][0] = p.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                info[i][1] = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            // build a PrivateCredentialPermission for every
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            // one of the current Subject's principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            // the accCs is guaranteed to be a SubjectCodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            // because the earlier CodeSource.implies succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            SubjectCodeSource scs = (SubjectCodeSource)accCs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            Set<Principal> principalSet = scs.getSubject().getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            String[][] info = new String[principalSet.size()][2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            java.util.Iterator<Principal> pIterator = principalSet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            while (pIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                Principal p = pIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                info[i][0] = p.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                info[i][1] = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * Returns the signer certificates from the list of certificates associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * with the given code source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * The signer certificates are those certificates that were used to verify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * signed code originating from the codesource location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * This method assumes that in the given code source, each signer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * certificate is followed by its supporting certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * (which may be empty), and that the signer certificate and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * supporting certificate chain are ordered bottom-to-top (i.e., with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * signer certificate first and the (root) certificate authority last).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    Certificate[] getSignerCertificates(CodeSource cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        Certificate[] certs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        if ((certs = cs.getCertificates()) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        for (int i=0; i<certs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            if (!(certs[i] instanceof X509Certificate))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
                return cs.getCertificates();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        // Do we have to do anything?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        while (i < certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            while (((i+1) < certs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                   && ((X509Certificate)certs[i]).getIssuerDN().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                           ((X509Certificate)certs[i+1]).getSubjectDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
        if (count == certs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            // Done
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            return certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        ArrayList<Certificate> userCertList = new ArrayList<Certificate>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        while (i < certs.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            userCertList.add(certs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            while (((i+1) < certs.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                   && ((X509Certificate)certs[i]).getIssuerDN().equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                           ((X509Certificate)certs[i+1]).getSubjectDN())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        Certificate[] userCerts = new Certificate[userCertList.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        userCertList.toArray(userCerts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        return userCerts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
    private CodeSource canonicalizeCodebase(CodeSource cs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                                            boolean extractSignerCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        CodeSource canonCs = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        if (cs.getLocation() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            cs.getLocation().getProtocol().equalsIgnoreCase("file")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                String path = cs.getLocation().getFile().replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                                                        ('/',
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                                                        File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                URL csUrl = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                if (path.endsWith("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                    // remove trailing '*' because it causes canonicalization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                    // to fail on win32
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    path = path.substring(0, path.length()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    boolean appendFileSep = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                    if (path.endsWith(File.separator))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
                        appendFileSep = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                    if (path.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                        path = System.getProperty("user.dir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
                    File f = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
                    path = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                    StringBuffer sb = new StringBuffer(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                    // reappend '*' to canonicalized filename (note that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                    // canonicalization may have removed trailing file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                    // separator, so we have to check for that, too)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    if (!path.endsWith(File.separator) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                        (appendFileSep || f.isDirectory()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                        sb.append(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                    sb.append('*');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                    path = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                    path = new File(path).getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                csUrl = new File(path).toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
                if (cs instanceof SubjectCodeSource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                    SubjectCodeSource scs = (SubjectCodeSource)cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
                    if (extractSignerCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                        canonCs = new SubjectCodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
                                                (scs.getSubject(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                                                scs.getPrincipals(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                                                csUrl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                                                getSignerCertificates(scs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                        canonCs = new SubjectCodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                                                (scs.getSubject(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                                                scs.getPrincipals(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                                                csUrl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                                                scs.getCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                    if (extractSignerCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                        canonCs = new CodeSource(csUrl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                                                getSignerCertificates(cs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                        canonCs = new CodeSource(csUrl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                                                cs.getCertificates());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                // leave codesource as it is, unless we have to extract its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                // signer certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                if (extractSignerCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    if (!(cs instanceof SubjectCodeSource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                        canonCs = new CodeSource(cs.getLocation(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                                                getSignerCertificates(cs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                        SubjectCodeSource scs = (SubjectCodeSource)cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                        canonCs = new SubjectCodeSource(scs.getSubject(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                                                scs.getPrincipals(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                                                scs.getLocation(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                                                getSignerCertificates(scs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            if (extractSignerCerts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                if (!(cs instanceof SubjectCodeSource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                    canonCs = new CodeSource(cs.getLocation(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                                        getSignerCertificates(cs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    SubjectCodeSource scs = (SubjectCodeSource)cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    canonCs = new SubjectCodeSource(scs.getSubject(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                                        scs.getPrincipals(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                                        scs.getLocation(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                                        getSignerCertificates(scs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        return canonCs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * Each entry in the policy configuration file is represented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * PolicyEntry object.  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * A PolicyEntry is a (CodeSource,Permission) pair.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * CodeSource contains the (URL, PublicKey) that together identify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * where the Java bytecodes come from and who (if anyone) signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * them.  The URL could refer to localhost.  The URL could also be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * null, meaning that this policy entry is given to all comers, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * long as they match the signer field.  The signer could be null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * meaning the code is not signed. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * The Permission contains the (Type, Name, Action) triplet. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     * For now, the Policy object retrieves the public key from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * X.509 certificate on disk that corresponds to the signedBy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * alias specified in the Policy config file.  For reasons of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * efficiency, the Policy object keeps a hashtable of certs already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * read in.  This could be replaced by a secure internal key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * For example, the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *          permission java.io.File "/tmp", "read,write",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     *          signedBy "Duke";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * is represented internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * FilePermission f = new FilePermission("/tmp", "read,write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * PublicKey p = publickeys.get("Duke");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * URL u = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * CodeBase c = new CodeBase( p, u );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * pe = new PolicyEntry(f, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * @author Marianne Mueller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @see java.security.CodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @see java.security.Policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * @see java.security.ProtectionDomain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    private static class PolicyEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        CodeSource codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        Vector<Permission> permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
         * Given a Permission and a CodeSource, create a policy entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
         * XXX Decide if/how to add validity fields and "purpose" fields to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
         * XXX policy entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
         * @param cs the CodeSource, which encapsulates the URL and the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
         *        key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
         *        attributes from the policy config file.   Validity checks are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
         *        performed on the public key before PolicyEntry is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        PolicyEntry(CodeSource cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            this.codesource = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
            this.permissions = new Vector<Permission>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
         * add a Permission object to this entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        void add(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
            permissions.addElement(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
         * Return the CodeSource for this policy entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        CodeSource getCodeSource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            return this.codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        public String toString(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
            sb.append(rb.getString("("));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            sb.append(getCodeSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            sb.append("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            for (int j = 0; j < permissions.size(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                Permission p = permissions.elementAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                sb.append(rb.getString(" "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                sb.append(rb.getString(" "));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                sb.append(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                sb.append(rb.getString("\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            sb.append(rb.getString(")"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            sb.append(rb.getString("\n"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
class PolicyPermissions extends PermissionCollection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    private static final long serialVersionUID = -1954188373270545523L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
    private CodeSource codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    private Permissions perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    private PolicyFile policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    private boolean notInit; // have we pulled in the policy permissions yet?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    private Vector<Permission> additionalPerms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    PolicyPermissions(PolicyFile policy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                      CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        this.codesource = codesource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        this.policy = policy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        this.perms = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        this.notInit = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        this.additionalPerms = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    public void add(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        if (isReadOnly())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            (PolicyFile.rb.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            ("attempt to add a Permission to a readonly PermissionCollection"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        if (perms == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            if (additionalPerms == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                additionalPerms = new Vector<Permission>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            additionalPerms.add(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            perms.add(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
    private synchronized void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        if (notInit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            if (perms == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
                perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            if (additionalPerms != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                Enumeration<Permission> e = additionalPerms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
                    perms.add(e.nextElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                additionalPerms = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            policy.getPermissions(perms,codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            notInit=false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        if (notInit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        return perms.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    public Enumeration<Permission> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        if (notInit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        return perms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        if (notInit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        return perms.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
}