Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/classes/com/sun/security/auth/PolicyFile.java
83414 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.security.auth;2627import java.security.CodeSource;28import java.security.PermissionCollection;29import javax.security.auth.Subject;3031/**32* This class represents a default implementation for33* <code>javax.security.auth.Policy</code>.34*35* <p> This object stores the policy for entire Java runtime,36* and is the amalgamation of multiple static policy37* configurations that resides in files.38* The algorithm for locating the policy file(s) and reading their39* information into this <code>Policy</code> object is:40*41* <ol>42* <li>43* Loop through the security properties,44* <i>auth.policy.url.1</i>, <i>auth.policy.url.2</i>, ...,45* <i>auth.policy.url.X</i>".46* Each property value specifies a <code>URL</code> pointing to a47* policy file to be loaded. Read in and load each policy.48*49* <li>50* The <code>java.lang.System</code> property <i>java.security.auth.policy</i>51* may also be set to a <code>URL</code> pointing to another policy file52* (which is the case when a user uses the -D switch at runtime).53* If this property is defined, and its use is allowed by the54* security property file (the Security property,55* <i>policy.allowSystemProperty</i> is set to <i>true</i>),56* also load that policy.57*58* <li>59* If the <i>java.security.auth.policy</i> property is defined using60* "==" (rather than "="), then ignore all other specified61* policies and only load this policy.62* </ol>63*64* Each policy file consists of one or more grant entries, each of65* which consists of a number of permission entries.66*67* <pre>68* grant signedBy "<b>alias</b>", codeBase "<b>URL</b>",69* principal <b>principalClass</b> "<b>principalName</b>",70* principal <b>principalClass</b> "<b>principalName</b>",71* ... {72*73* permission <b>Type</b> "<b>name</b> "<b>action</b>",74* signedBy "<b>alias</b>";75* permission <b>Type</b> "<b>name</b> "<b>action</b>",76* signedBy "<b>alias</b>";77* ....78* };79* </pre>80*81* All non-bold items above must appear as is (although case82* doesn't matter and some are optional, as noted below).83* Italicized items represent variable values.84*85* <p> A grant entry must begin with the word <code>grant</code>.86* The <code>signedBy</code> and <code>codeBase</code>87* name/value pairs are optional.88* If they are not present, then any signer (including unsigned code)89* will match, and any codeBase will match. Note that the90* <code>principal</code> name/value pair is not optional.91* This <code>Policy</code> implementation only permits92* Principal-based grant entries. Note that the <i>principalClass</i>93* may be set to the wildcard value, *, which allows it to match94* any <code>Principal</code> class. In addition, the <i>principalName</i>95* may also be set to the wildcard value, *, allowing it to match96* any <code>Principal</code> name. When setting the <i>principalName</i>97* to the *, do not surround the * with quotes.98*99* <p> A permission entry must begin with the word <code>permission</code>.100* The word <code><i>Type</i></code> in the template above is101* a specific permission type, such as <code>java.io.FilePermission</code>102* or <code>java.lang.RuntimePermission</code>.103*104* <p> The "<i>action</i>" is required for105* many permission types, such as <code>java.io.FilePermission</code>106* (where it specifies what type of file access that is permitted).107* It is not required for categories such as108* <code>java.lang.RuntimePermission</code>109* where it is not necessary - you either have the110* permission specified by the <code>"<i>name</i>"</code>111* value following the type name or you don't.112*113* <p> The <code>signedBy</code> name/value pair for a permission entry114* is optional. If present, it indicates a signed permission. That is,115* the permission class itself must be signed by the given alias in116* order for it to be granted. For example,117* suppose you have the following grant entry:118*119* <pre>120* grant principal foo.com.Principal "Duke" {121* permission Foo "foobar", signedBy "FooSoft";122* }123* </pre>124*125* <p> Then this permission of type <i>Foo</i> is granted if the126* <code>Foo.class</code> permission has been signed by the127* "FooSoft" alias, or if <code>Foo.class</code> is a128* system class (i.e., is found on the CLASSPATH).129*130* <p> Items that appear in an entry must appear in the specified order131* (<code>permission</code>, <i>Type</i>, "<i>name</i>", and132* "<i>action</i>"). An entry is terminated with a semicolon.133*134* <p> Case is unimportant for the identifiers (<code>permission</code>,135* <code>signedBy</code>, <code>codeBase</code>, etc.) but is136* significant for the <i>Type</i>137* or for any string that is passed in as a value. <p>138*139* <p> An example of two entries in a policy configuration file is140* <pre>141* // if the code is comes from "foo.com" and is running as "Duke",142* // grant it read/write to all files in /tmp.143*144* grant codeBase "foo.com", principal foo.com.Principal "Duke" {145* permission java.io.FilePermission "/tmp/*", "read,write";146* };147*148* // grant any code running as "Duke" permission to read149* // the "java.vendor" Property.150*151* grant principal foo.com.Principal "Duke" {152* permission java.util.PropertyPermission "java.vendor";153* </pre>154*155* <p> This <code>Policy</code> implementation supports156* special handling for PrivateCredentialPermissions.157* If a grant entry is configured with a158* <code>PrivateCredentialPermission</code>,159* and the "Principal Class/Principal Name" for that160* <code>PrivateCredentialPermission</code> is "self",161* then the entry grants the specified <code>Subject</code> permission to162* access its own private Credential. For example,163* the following grants the <code>Subject</code> "Duke"164* access to its own a.b.Credential.165*166* <pre>167* grant principal foo.com.Principal "Duke" {168* permission javax.security.auth.PrivateCredentialPermission169* "a.b.Credential self",170* "read";171* };172* </pre>173*174* The following grants the <code>Subject</code> "Duke"175* access to all of its own private Credentials:176*177* <pre>178* grant principal foo.com.Principal "Duke" {179* permission javax.security.auth.PrivateCredentialPermission180* "* self",181* "read";182* };183* </pre>184*185* The following grants all Subjects authenticated as a186* <code>SolarisPrincipal</code> (regardless of their respective names)187* permission to access their own private Credentials:188*189* <pre>190* grant principal com.sun.security.auth.SolarisPrincipal * {191* permission javax.security.auth.PrivateCredentialPermission192* "* self",193* "read";194* };195* </pre>196*197* The following grants all Subjects permission to access their own198* private Credentials:199*200* <pre>201* grant principal * * {202* permission javax.security.auth.PrivateCredentialPermission203* "* self",204* "read";205* };206* </pre>207208* @deprecated As of JDK 1.4, replaced by209* <code>sun.security.provider.PolicyFile</code>.210* This class is entirely deprecated.211*212* @see java.security.CodeSource213* @see java.security.Permissions214* @see java.security.ProtectionDomain215* @see java.security.Security security properties216*/217@jdk.Exported(false)218@Deprecated219public class PolicyFile extends javax.security.auth.Policy {220221private final sun.security.provider.AuthPolicyFile apf;222223/**224* Initializes the Policy object and reads the default policy225* configuration file(s) into the Policy object.226*/227public PolicyFile() {228apf = new sun.security.provider.AuthPolicyFile();229}230231/**232* Refreshes the policy object by re-reading all the policy files.233*234* <p>235*236* @exception SecurityException if the caller doesn't have permission237* to refresh the <code>Policy</code>.238*/239@Override240public void refresh() {241apf.refresh();242}243244/**245* Examines this <code>Policy</code> and returns the Permissions granted246* to the specified <code>Subject</code> and <code>CodeSource</code>.247*248* <p> Permissions for a particular <i>grant</i> entry are returned249* if the <code>CodeSource</code> constructed using the codebase and250* signedby values specified in the entry <code>implies</code>251* the <code>CodeSource</code> provided to this method, and if the252* <code>Subject</code> provided to this method contains all of the253* Principals specified in the entry.254*255* <p> The <code>Subject</code> provided to this method contains all256* of the Principals specified in the entry if, for each257* <code>Principal</code>, "P1", specified in the <i>grant</i> entry258* one of the following two conditions is met:259*260* <p>261* <ol>262* <li> the <code>Subject</code> has a263* <code>Principal</code>, "P2", where264* <code>P2.getClass().getName()</code> equals the265* P1's class name, and where266* <code>P2.getName()</code> equals the P1's name.267*268* <li> P1 implements269* <code>com.sun.security.auth.PrincipalComparator</code>,270* and <code>P1.implies</code> the provided <code>Subject</code>.271* </ol>272*273* <p> Note that this <code>Policy</code> implementation has274* special handling for PrivateCredentialPermissions.275* When this method encounters a <code>PrivateCredentialPermission</code>276* which specifies "self" as the <code>Principal</code> class and name,277* it does not add that <code>Permission</code> to the returned278* <code>PermissionCollection</code>. Instead, it builds279* a new <code>PrivateCredentialPermission</code>280* for each <code>Principal</code> associated with the provided281* <code>Subject</code>. Each new <code>PrivateCredentialPermission</code>282* contains the same Credential class as specified in the283* originally granted permission, as well as the Class and name284* for the respective <code>Principal</code>.285*286* <p>287*288* @param subject the Permissions granted to this <code>Subject</code>289* and the additionally provided <code>CodeSource</code>290* are returned. <p>291*292* @param codesource the Permissions granted to this <code>CodeSource</code>293* and the additionally provided <code>Subject</code>294* are returned.295*296* @return the Permissions granted to the provided <code>Subject</code>297* <code>CodeSource</code>.298*/299@Override300public PermissionCollection getPermissions(final Subject subject,301final CodeSource codesource) {302return apf.getPermissions(subject, codesource);303}304}305306307