src/java.base/share/classes/sun/security/provider/PolicyParser.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58519 6e017b301287
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 53018
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
    29
import java.security.GeneralSecurityException;
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
    30
import java.security.Principal;
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
    31
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.util.PropertyExpander;
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
    36
import sun.security.util.LocalizedMessage;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The policy for a Java runtime (specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * which permissions are available for code from various principals)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * is represented as a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * persistent configuration.  The configuration may be stored as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * flat ASCII file, as a serialized binary file of
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 27565
diff changeset
    44
 * the Policy class, or as a database.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>The Java runtime creates one global Policy object, which is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * represent the static policy configuration file.  It is consulted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * a ProtectionDomain when the protection domain initializes its set of
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 27565
diff changeset
    49
 * permissions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>The Policy <code>init</code> method parses the policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * configuration file, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * populates the Policy object.  The Policy object is agnostic in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * it is not involved in making policy decisions.  It is merely the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Java runtime representation of the persistent policy configuration
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 27565
diff changeset
    56
 * file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>When a protection domain needs to initialize its set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * permissions, it executes code such as the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * to ask the global Policy object to populate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Permissions object with the appropriate permissions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *  policy = Policy.getPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *  Permissions perms = policy.getPermissions(protectiondomain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
    67
 * <p>The protection domain contains a CodeSource
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * object, which encapsulates its codebase (URL) and public key attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * It also contains the principals associated with the domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * The Policy object evaluates the global policy in light of who the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * principal is and what the code source is and returns an appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Permissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @author Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
public class PolicyParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private Vector<GrantEntry> grantEntries;
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
    83
    private Map<String, DomainEntry> domainEntries;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Convenience variables for parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static final Debug debug = Debug.getInstance("parser",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                                "\t[Policy Parser]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private StreamTokenizer st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private int lookahead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private boolean expandProp = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private String keyStoreUrlString = null; // unexpanded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private String keyStoreType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private String keyStoreProvider = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private String storePassURL = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private String expand(String value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        throws PropertyExpander.ExpandException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return expand(value, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private String expand(String value, boolean encodeURL)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        throws PropertyExpander.ExpandException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (!expandProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return PropertyExpander.expand(value, encodeURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Creates a PolicyParser object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public PolicyParser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        grantEntries = new Vector<GrantEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public PolicyParser(boolean expandProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        this.expandProp = expandProp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Reads a policy configuration into the Policy object using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Reader object. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param policy the policy Reader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @exception ParsingException if the policy configuration contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *          a syntax error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @exception IOException if an error occurs while reading the policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *          configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public void read(Reader policy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (!(policy instanceof BufferedReader)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            policy = new BufferedReader(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
         * Configure the stream tokenizer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         *      Recognize strings between "..."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         *      Don't convert words to lowercase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
         *      Recognize both C-style and C++-style comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
         *      Treat end-of-line as white space, not as a token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        st   = new StreamTokenizer(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        st.resetSyntax();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        st.wordChars('a', 'z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        st.wordChars('A', 'Z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        st.wordChars('.', '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        st.wordChars('0', '9');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        st.wordChars('_', '_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        st.wordChars('$', '$');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        st.wordChars(128 + 32, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        st.whitespaceChars(0, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        st.commentChar('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        st.quoteChar('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        st.quoteChar('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        st.lowerCaseMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        st.ordinaryChar('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        st.slashSlashComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        st.slashStarComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * The main parsing loop.  The loop is executed once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         * for each entry in the config file.      The entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         * are delimited by semicolons.   Once we've read in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * the information for an entry, go ahead and try to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * add it to the policy vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        lookahead = st.nextToken();
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   182
        GrantEntry ge = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        while (lookahead != StreamTokenizer.TT_EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (peek("grant")) {
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   185
                ge = parseGrantEntry();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // could be null if we couldn't expand a property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (ge != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    add(ge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            } else if (peek("keystore") && keyStoreUrlString==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                // only one keystore entry per policy file, others will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                // ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                parseKeyStoreEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } else if (peek("keystorePasswordURL") && storePassURL==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                // only one keystore passwordURL per policy file, others will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                parseStorePassURL();
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   197
            } else if (ge == null && keyStoreUrlString == null &&
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   198
                storePassURL == null && peek("domain")) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   199
                if (domainEntries == null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   200
                    domainEntries = new TreeMap<>();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   201
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   202
                DomainEntry de = parseDomainEntry();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   203
                if (de != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   204
                    String domainName = de.getName();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   205
                    if (!domainEntries.containsKey(domainName)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   206
                        domainEntries.put(domainName, de);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   207
                    } else {
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   208
                        LocalizedMessage localizedMsg = new LocalizedMessage(
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   209
                            "duplicate.keystore.domain.name");
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   210
                        Object[] source = {domainName};
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
   211
                        String msg = "duplicate keystore domain name: " +
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
   212
                                     domainName;
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   213
                        throw new ParsingException(msg, localizedMsg, source);
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   214
                    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   215
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                // error?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            match(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (keyStoreUrlString == null && storePassURL != null) {
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   223
            throw new ParsingException(LocalizedMessage.getNonlocalized
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   224
                ("keystorePasswordURL.can.not.be.specified.without.also.specifying.keystore"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public void add(GrantEntry ge)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        grantEntries.addElement(ge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public void replace(GrantEntry origGe, GrantEntry newGe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        grantEntries.setElementAt(newGe, grantEntries.indexOf(origGe));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public boolean remove(GrantEntry ge)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return grantEntries.removeElement(ge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Returns the (possibly expanded) keystore location, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * expansion fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public String getKeyStoreUrl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (keyStoreUrlString!=null && keyStoreUrlString.length()!=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                return expand(keyStoreUrlString, true).replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                                (File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                debug.println(peee.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public void setKeyStoreUrl(String url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        keyStoreUrlString = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public String getKeyStoreType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return keyStoreType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public void setKeyStoreType(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        keyStoreType = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public String getKeyStoreProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return keyStoreProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public void setKeyStoreProvider(String provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        keyStoreProvider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public String getStorePassURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            if (storePassURL!=null && storePassURL.length()!=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                return expand(storePassURL, true).replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                                (File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                debug.println(peee.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public void setStorePassURL(String storePassURL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        this.storePassURL = storePassURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Enumerate all the entries in the global policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * This method is used by policy admin tools.   The tools
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * should use the Enumeration methods on the returned object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * to fetch the elements sequentially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public Enumeration<GrantEntry> grantElements(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return grantEntries.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   311
    public Collection<DomainEntry> getDomainEntries() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   312
        return domainEntries.values();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   313
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   314
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * write out the policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public void write(Writer policy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        PrintWriter out = new PrintWriter(new BufferedWriter(policy));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Enumeration<GrantEntry> enum_ = grantElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        out.println("/* AUTOMATICALLY GENERATED ON "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    (new java.util.Date()) + "*/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        out.println("/* DO NOT EDIT */");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // write the (unexpanded) keystore entry as the first entry of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // policy file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (keyStoreUrlString != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            writeKeyStoreEntry(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (storePassURL != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            writeStorePassURL(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // write "grant" entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            GrantEntry ge = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            ge.write(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * parses a keystore entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    private void parseKeyStoreEntry() throws ParsingException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        match("keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        keyStoreUrlString = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        // parse keystore type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (!peek(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            return; // default type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        match(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            keyStoreType = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            throw new ParsingException(st.lineno(),
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   365
                LocalizedMessage.getNonlocalized("expected.keystore.type"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        // parse keystore provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        if (!peek(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return; // provider optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        match(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            keyStoreProvider = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            throw new ParsingException(st.lineno(),
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   378
                LocalizedMessage.getNonlocalized("expected.keystore.provider"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    private void parseStorePassURL() throws ParsingException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        match("keyStorePasswordURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        storePassURL = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * writes the (unexpanded) keystore entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    private void writeKeyStoreEntry(PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        out.print("keystore \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        out.print(keyStoreUrlString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        out.print('"');
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 48757
diff changeset
   394
        if (keyStoreType != null && !keyStoreType.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            out.print(", \"" + keyStoreType + "\"");
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 48757
diff changeset
   396
        if (keyStoreProvider != null && !keyStoreProvider.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            out.print(", \"" + keyStoreProvider + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        out.println(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private void writeStorePassURL(PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        out.print("keystorePasswordURL \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        out.print(storePassURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        out.println(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * parse a Grant entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private GrantEntry parseGrantEntry()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        GrantEntry e = new GrantEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        LinkedList<PrincipalEntry> principals = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        boolean ignoreEntry = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        match("grant");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        while(!peek("{")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (peekAndMatch("Codebase")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                if (e.codeBase != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    throw new ParsingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                            st.lineno(),
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   428
                            LocalizedMessage.getNonlocalized
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   429
                                ("multiple.Codebase.expressions"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                e.codeBase = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                peekAndMatch(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            } else if (peekAndMatch("SignedBy")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                if (e.signedBy != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    throw new ParsingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                            st.lineno(),
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   436
                            LocalizedMessage.getNonlocalized
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   437
                                ("multiple.SignedBy.expressions"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                e.signedBy = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                // verify syntax of the aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                StringTokenizer aliases = new StringTokenizer(e.signedBy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                                              ",", true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                int actr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                int cctr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                while (aliases.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    String alias = aliases.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    if (alias.equals(","))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        cctr++;
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 48757
diff changeset
   449
                    else if (!alias.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        actr++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                if (actr <= cctr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    throw new ParsingException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                            st.lineno(),
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   455
                            LocalizedMessage.getNonlocalized
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   456
                                ("SignedBy.has.empty.alias"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                peekAndMatch(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            } else if (peekAndMatch("Principal")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                if (principals == null) {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   461
                    principals = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                String principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                String principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    // both the principalClass and principalName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    // will be replaced later
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   470
                    principalClass = PrincipalEntry.REPLACE_NAME;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    principalName = match("principal type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    // check for principalClass wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    if (peek("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                        match("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                        principalClass = PrincipalEntry.WILDCARD_CLASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        principalClass = match("principal type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    // check for principalName wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    if (peek("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                        match("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        principalName = PrincipalEntry.WILDCARD_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        principalName = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    // disallow WILDCARD_CLASS && actual name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    if (principalClass.equals(PrincipalEntry.WILDCARD_CLASS) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        !principalName.equals(PrincipalEntry.WILDCARD_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                        if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                debug.println("disallowing principal that " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                                    "has WILDCARD class but no WILDCARD name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                        throw new ParsingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                                (st.lineno(),
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   498
                                LocalizedMessage.getNonlocalized
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   499
                                    ("can.not.specify.Principal.with.a.wildcard.class.without.a.wildcard.name"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    principalName = expand(principalName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    if (principalClass.equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                                ("javax.security.auth.x500.X500Principal") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                        !principalName.equals(PrincipalEntry.WILDCARD_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                        // 4702543:  X500 names with an EmailAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                        // were encoded incorrectly.  construct a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                        // X500Principal with correct encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        X500Principal p = new X500Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                ((new X500Principal(principalName)).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                        principalName = p.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    principals.add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                        (new PrincipalEntry(principalClass, principalName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    // ignore the entire policy entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    // but continue parsing all the info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    // so we can get to the next entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                        debug.println("principal name expansion failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                        principalName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    ignoreEntry = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                peekAndMatch(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                throw new ParsingException(st.lineno(),
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   535
                    LocalizedMessage.getNonlocalized
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   536
                        ("expected.codeBase.or.SignedBy.or.Principal"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if (principals != null) e.principals = principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        match("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        while(!peek("}")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            if (peek("Permission")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    PermissionEntry pe = parsePermissionEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    e.add(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    // ignore. The add never happened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        debug.println(peee.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    skipEntry();  // BugId 4219343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                match(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    ParsingException(st.lineno(),
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   559
                        LocalizedMessage.getNonlocalized
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   560
                            ("expected.permission.entry"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        match("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            if (e.signedBy != null) e.signedBy = expand(e.signedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            if (e.codeBase != null) {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   568
                e.codeBase = expand(e.codeBase, true).replace
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   569
                                    (File.separatorChar, '/');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                debug.println(peee.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return (ignoreEntry == true) ? null : e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * parse a Permission entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private PermissionEntry parsePermissionEntry()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        throws ParsingException, IOException, PropertyExpander.ExpandException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        PermissionEntry e = new PermissionEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        // Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        match("Permission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        e.permission = match("permission type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            // Permission name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            e.name = expand(match("quoted string"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        if (!peek(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        match(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                e.action = expand(match("quoted string"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                if (!peek(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                match(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (peekAndMatch("SignedBy")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            e.signedBy = expand(match("quoted string"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   617
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   618
     * parse a domain entry
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   619
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   620
    private DomainEntry parseDomainEntry()
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   621
        throws ParsingException, IOException
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   622
    {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   623
        boolean ignoreEntry = false;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   624
        DomainEntry domainEntry;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   625
        String name = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   626
        Map<String, String> properties = new HashMap<>();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   627
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   628
        match("domain");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   629
        name = match("domain name");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   630
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   631
        while(!peek("{")) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   632
            // get the domain properties
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   633
            properties = parseProperties("{");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   634
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   635
        match("{");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   636
        domainEntry = new DomainEntry(name, properties);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   637
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   638
        while(!peek("}")) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   639
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   640
            match("keystore");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   641
            name = match("keystore name");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   642
            // get the keystore properties
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   643
            if (!peek("}")) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   644
                properties = parseProperties(";");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   645
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   646
            match(";");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   647
            domainEntry.add(new KeyStoreEntry(name, properties));
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   648
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   649
        match("}");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   650
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   651
        return (ignoreEntry == true) ? null : domainEntry;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   652
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   653
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   654
    /*
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   655
     * Return a collection of domain properties or keystore properties.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   656
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   657
    private Map<String, String> parseProperties(String terminator)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   658
        throws ParsingException, IOException {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   659
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   660
        Map<String, String> properties = new HashMap<>();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   661
        String key;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   662
        String value;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   663
        while (!peek(terminator)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   664
            key = match("property name");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   665
            match("=");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   666
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   667
            try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   668
                value = expand(match("quoted string"));
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   669
            } catch (PropertyExpander.ExpandException peee) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   670
                throw new IOException(peee.getLocalizedMessage());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   671
            }
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 15664
diff changeset
   672
            properties.put(key.toLowerCase(Locale.ENGLISH), value);
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   673
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   674
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   675
        return properties;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   676
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   677
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    private boolean peekAndMatch(String expect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (peek(expect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            match(expect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    private boolean peek(String expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        switch (lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        case StreamTokenizer.TT_WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            if (expect.equalsIgnoreCase(st.sval))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            if (expect.equalsIgnoreCase(","))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            if (expect.equalsIgnoreCase("{"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            if (expect.equalsIgnoreCase("}"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        case '"':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            if (expect.equalsIgnoreCase("\""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            if (expect.equalsIgnoreCase("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            break;
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   718
        case ';':
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   719
            if (expect.equalsIgnoreCase(";"))
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   720
                found = true;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   721
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        return found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    private String match(String expect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        switch (lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        case StreamTokenizer.TT_NUMBER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            throw new ParsingException(st.lineno(), expect,
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   736
                LocalizedMessage.getNonlocalized("number.") +
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   737
                    String.valueOf(st.nval));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        case StreamTokenizer.TT_EOF:
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   739
            LocalizedMessage localizedMsg = new LocalizedMessage
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   740
                ("expected.expect.read.end.of.file.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            Object[] source = {expect};
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
   742
            String msg = "expected [" + expect + "], read [end of file]";
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   743
            throw new ParsingException(msg, localizedMsg, source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        case StreamTokenizer.TT_WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            if (expect.equalsIgnoreCase(st.sval)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            } else if (expect.equalsIgnoreCase("permission type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            } else if (expect.equalsIgnoreCase("principal type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                lookahead = st.nextToken();
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   753
            } else if (expect.equalsIgnoreCase("domain name") ||
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   754
                       expect.equalsIgnoreCase("keystore name") ||
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   755
                       expect.equalsIgnoreCase("property name")) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   756
                value = st.sval;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   757
                lookahead = st.nextToken();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                 throw new ParsingException(st.lineno(), expect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                                            st.sval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        case '"':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (expect.equalsIgnoreCase("quoted string")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            } else if (expect.equalsIgnoreCase("permission type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            } else if (expect.equalsIgnoreCase("principal type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                throw new ParsingException(st.lineno(), expect, st.sval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            if (expect.equalsIgnoreCase(","))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                throw new ParsingException(st.lineno(), expect, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            if (expect.equalsIgnoreCase("{"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                throw new ParsingException(st.lineno(), expect, "{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            if (expect.equalsIgnoreCase("}"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                throw new ParsingException(st.lineno(), expect, "}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        case ';':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            if (expect.equalsIgnoreCase(";"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                throw new ParsingException(st.lineno(), expect, ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            if (expect.equalsIgnoreCase("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                throw new ParsingException(st.lineno(), expect, "*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            break;
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   807
        case '=':
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   808
            if (expect.equalsIgnoreCase("="))
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   809
                lookahead = st.nextToken();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   810
            else
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   811
                throw new ParsingException(st.lineno(), expect, "=");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
   812
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            throw new ParsingException(st.lineno(), expect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                               new String(new char[] {(char)lookahead}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * skip all tokens for this entry leaving the delimiter ";"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    private void skipEntry() throws ParsingException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        while(lookahead != ';') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            switch (lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            case StreamTokenizer.TT_NUMBER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                throw new ParsingException(st.lineno(), ";",
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   829
                        LocalizedMessage.getNonlocalized("number.") +
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   830
                            String.valueOf(st.nval));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            case StreamTokenizer.TT_EOF:
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   832
                throw new ParsingException(LocalizedMessage.getNonlocalized
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   833
                        ("expected.read.end.of.file."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * Each grant entry in the policy configuration file is
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 27565
diff changeset
   842
     * represented by a GrantEntry object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * For example, the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *      grant signedBy "Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *          permission java.io.FilePermission "/tmp", "read,write";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *      };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * is represented internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * pe = new PermissionEntry("java.io.FilePermission",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *                           "/tmp", "read,write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * ge = new GrantEntry("Duke", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * ge.add(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * version 1.19, 05/21/98
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    public static class GrantEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        public String signedBy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        public String codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        public LinkedList<PrincipalEntry> principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        public Vector<PermissionEntry> permissionEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        public GrantEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            principals = new LinkedList<PrincipalEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            permissionEntries = new Vector<PermissionEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        public GrantEntry(String signedBy, String codeBase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            this.codeBase = codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            this.signedBy = signedBy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            principals = new LinkedList<PrincipalEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            permissionEntries = new Vector<PermissionEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        public void add(PermissionEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            permissionEntries.addElement(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        public boolean remove(PrincipalEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            return principals.remove(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        public boolean remove(PermissionEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            return permissionEntries.removeElement(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        public boolean contains(PrincipalEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            return principals.contains(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        public boolean contains(PermissionEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            return permissionEntries.contains(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
         * Enumerate all the permission entries in this GrantEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        public Enumeration<PermissionEntry> permissionElements(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            return permissionEntries.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        public void write(PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            out.print("grant");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            if (signedBy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                out.print(" signedBy \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                out.print(signedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                if (codeBase != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                    out.print(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            if (codeBase != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                out.print(" codeBase \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                out.print(codeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                if (principals != null && principals.size() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    out.print(",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            if (principals != null && principals.size() > 0) {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   938
                Iterator<PrincipalEntry> pli = principals.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                while (pli.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                    out.print("      ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                    PrincipalEntry pe = pli.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                    pe.write(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    if (pli.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                        out.print(",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            out.println(" {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            Enumeration<PermissionEntry> enum_ = permissionEntries.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                PermissionEntry pe = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                out.write("  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                pe.write(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            out.println("};");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            GrantEntry ge = new GrantEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            ge.codeBase = this.codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            ge.signedBy = this.signedBy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            ge.principals = new LinkedList<PrincipalEntry>(this.principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            ge.permissionEntries =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                        new Vector<PermissionEntry>(this.permissionEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            return ge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * Principal info (class and name) in a grant entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     */
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   971
    public static class PrincipalEntry implements Principal {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        public static final String WILDCARD_CLASS = "WILDCARD_PRINCIPAL_CLASS";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        public static final String WILDCARD_NAME = "WILDCARD_PRINCIPAL_NAME";
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   975
        public static final String REPLACE_NAME = "PolicyParser.REPLACE_NAME";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        String principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        String principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        /**
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   981
         * A PrincipalEntry consists of the Principal class and Principal name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         *
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   983
         * @param principalClass the Principal class
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   984
         * @param principalName the Principal name
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   985
         * @throws NullPointerException if principalClass or principalName
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   986
         *                              are null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        public PrincipalEntry(String principalClass, String principalName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            if (principalClass == null || principalName == null)
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
   990
                throw new NullPointerException(LocalizedMessage.getNonlocalized
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
   991
                    ("null.principalClass.or.principalName"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            this.principalClass = principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            this.principalName = principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   996
        boolean isWildcardName() {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   997
            return principalName.equals(WILDCARD_NAME);
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   998
        }
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
   999
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1000
        boolean isWildcardClass() {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1001
            return principalClass.equals(WILDCARD_CLASS);
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1002
        }
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1003
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1004
        boolean isReplaceName() {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1005
            return principalClass.equals(REPLACE_NAME);
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1006
        }
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1007
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        public String getPrincipalClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            return principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        public String getPrincipalName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            return principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        public String getDisplayClass() {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1017
            if (isWildcardClass()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                return "*";
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1019
            } else if (isReplaceName()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            else return principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        public String getDisplayName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            return getDisplayName(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        public String getDisplayName(boolean addQuote) {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1030
            if (isWildcardName()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                return "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                if (addQuote) return "\"" + principalName + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                else return principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1039
        @Override
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1040
        public String getName() {
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1041
            return principalName;
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1042
        }
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1043
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1044
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        public String toString() {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1046
            if (!isReplaceName()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                return getDisplayClass() + "/" + getDisplayName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                return getDisplayName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            }
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
         * Test for equality between the specified object and this object.
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1055
         * Two PrincipalEntries are equal if their class and name values
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1056
         * are equal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
         *
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1058
         * @param obj the object to test for equality with this object
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1059
         * @return true if the objects are equal, false otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
         */
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1061
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            if (!(obj instanceof PrincipalEntry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            PrincipalEntry that = (PrincipalEntry)obj;
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1070
            return (principalClass.equals(that.principalClass) &&
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1071
                    principalName.equals(that.principalName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        /**
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1075
         * Return a hashcode for this PrincipalEntry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
         *
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1077
         * @return a hashcode for this PrincipalEntry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         */
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1079
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            return principalClass.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1083
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        public void write(PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            out.print("principal " + getDisplayClass() + " " +
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1086
                      getDisplayName(true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * Each permission entry in the policy configuration file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * represented by a
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 27565
diff changeset
  1093
     * PermissionEntry object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * For example, the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *          permission java.io.FilePermission "/tmp", "read,write";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * is represented internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * pe = new PermissionEntry("java.io.FilePermission",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *                           "/tmp", "read,write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * version 1.19, 05/21/98
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    public static class PermissionEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        public String permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        public String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        public String action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        public String signedBy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        public PermissionEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        public PermissionEntry(String permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                        String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                        String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            this.permission = permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            this.action = action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
         * Calculates a hash code value for the object.  Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
         * which are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
         */
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1134
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            int retval = permission.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            if (name != null) retval ^= name.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            if (action != null) retval ^= action.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1142
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            if (! (obj instanceof PermissionEntry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            PermissionEntry that = (PermissionEntry) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            if (this.permission == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                if (that.permission != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                if (!this.permission.equals(that.permission)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
            if (this.name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                if (that.name != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                if (!this.name.equals(that.name)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            if (this.action == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                if (that.action != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
                if (!this.action.equals(that.action)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            if (this.signedBy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                if (that.signedBy != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                if (!this.signedBy.equals(that.signedBy)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            // everything matched -- the 2 objects are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        public void write(PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            out.print("permission ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            out.print(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                out.print(" \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                // ATTENTION: regex with double escaping,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                // the normal forms look like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                // $name =~ s/\\/\\\\/g; and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                // $name =~ s/\"/\\\"/g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                // and then in a java string, it's escaped again
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                out.print(name.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\\"", "\\\\\\\""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                out.print(", \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                out.print(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            if (signedBy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                out.print(", signedBy \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                out.print(signedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            out.println(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1209
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1210
     * Each domain entry in the keystore domain configuration file is
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1211
     * represented by a DomainEntry object.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1212
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1213
    static class DomainEntry {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1214
        private final String name;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1215
        private final Map<String, String> properties;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1216
        private final Map<String, KeyStoreEntry> entries;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1217
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1218
        DomainEntry(String name, Map<String, String> properties) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1219
            this.name = name;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1220
            this.properties = properties;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1221
            entries = new HashMap<>();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1222
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1223
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1224
        String getName() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1225
            return name;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1226
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1227
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1228
        Map<String, String> getProperties() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1229
            return properties;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1230
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1231
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1232
        Collection<KeyStoreEntry> getEntries() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1233
            return entries.values();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1234
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1235
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1236
        void add(KeyStoreEntry entry) throws ParsingException {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1237
            String keystoreName = entry.getName();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1238
            if (!entries.containsKey(keystoreName)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1239
                entries.put(keystoreName, entry);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1240
            } else {
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
  1241
                LocalizedMessage localizedMsg = new LocalizedMessage
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
  1242
                    ("duplicate.keystore.name");
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1243
                Object[] source = {keystoreName};
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
  1244
                String msg = "duplicate keystore name: " + keystoreName;
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
  1245
                throw new ParsingException(msg, localizedMsg, source);
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1246
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1247
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1248
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1249
        @Override
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1250
        public String toString() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1251
            StringBuilder s =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1252
                new StringBuilder("\ndomain ").append(name);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1253
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1254
            if (properties != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1255
                for (Map.Entry<String, String> property :
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1256
                    properties.entrySet()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1257
                    s.append("\n        ").append(property.getKey()).append('=')
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1258
                        .append(property.getValue());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1259
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1260
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1261
            s.append(" {\n");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1262
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1263
            if (entries != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1264
                for (KeyStoreEntry entry : entries.values()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1265
                    s.append(entry).append("\n");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1266
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1267
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1268
            s.append("}");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1269
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1270
            return s.toString();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1271
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1272
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1273
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1274
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1275
     * Each keystore entry in the keystore domain configuration file is
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1276
     * represented by a KeyStoreEntry object.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1277
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1278
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1279
    static class KeyStoreEntry {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1280
        private final String name;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1281
        private final Map<String, String> properties;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1282
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1283
        KeyStoreEntry(String name, Map<String, String> properties) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1284
            this.name = name;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1285
            this.properties = properties;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1286
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1287
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1288
        String getName() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1289
            return name;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1290
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1291
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1292
        Map<String, String>  getProperties() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1293
            return properties;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1294
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1295
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1296
        @Override
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1297
        public String toString() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1298
            StringBuilder s = new StringBuilder("\n    keystore ").append(name);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1299
            if (properties != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1300
                for (Map.Entry<String, String> property :
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1301
                    properties.entrySet()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1302
                    s.append("\n        ").append(property.getKey()).append('=')
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1303
                        .append(property.getValue());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1304
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1305
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1306
            s.append(";");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1307
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1308
            return s.toString();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1309
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1310
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents: 15013
diff changeset
  1311
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    public static class ParsingException extends GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
57950
4612a3cfb927 8229999: Apply java.io.Serial annotations to security types in java.base
darcy
parents: 53018
diff changeset
  1314
        @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        private static final long serialVersionUID = -4330692689482574072L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        private String i18nMessage;
58519
6e017b301287 8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes
darcy
parents: 57950
diff changeset
  1318
        @SuppressWarnings("serial") // Not statically typed as Serializable
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
  1319
        private LocalizedMessage localizedMsg;
58519
6e017b301287 8231262: Suppress warnings on non-serializable instance fields in security libs serializable classes
darcy
parents: 57950
diff changeset
  1320
        @SuppressWarnings("serial") // Not statically typed as Serializable
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
  1321
        private Object[] source;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
         * Constructs a ParsingException with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
         * detail message. A detail message is a String that describes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
         * this particular exception, which may, for example, specify which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
         * algorithm is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
         * @param msg the detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        public ParsingException(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            super(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            i18nMessage = msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
  1336
        public ParsingException(String msg, LocalizedMessage localizedMsg,
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
  1337
                                Object[] source) {
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
  1338
            super(msg);
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
  1339
            this.localizedMsg = localizedMsg;
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
  1340
            this.source = source;
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
  1341
        }
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
  1342
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        public ParsingException(int line, String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            super("line " + line + ": " + msg);
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
  1345
            localizedMsg = new LocalizedMessage("line.number.msg");
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
  1346
            source = new Object[] {line, msg};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        public ParsingException(int line, String expect, String actual) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            super("line " + line + ": expected [" + expect +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                "], found [" + actual + "]");
43297
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
  1352
            localizedMsg = new LocalizedMessage
05ad35b943d0 8168075: Custom system class loader + security manager + malformed policy file = recursive initialization
apetcher
parents: 38324
diff changeset
  1353
                ("line.number.expected.expect.found.actual.");
38324
1ecea77815a8 8150468: ClassCircularityError on error in security policy file
mullan
parents: 31538
diff changeset
  1354
            source = new Object[] {line, expect, actual};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
48757
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
  1357
        public String getNonlocalizedMessage() {
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
  1358
            return i18nMessage != null ? i18nMessage :
8cc67294ec56 8194251: Deadlock between UsageTracker and System.getProperty() when using a malformed security policy
apetcher
parents: 47216
diff changeset
  1359
                localizedMsg.formatNonlocalized(source);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 30374
diff changeset
  1363
    public static void main(String[] arg) throws Exception {
15013
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1364
        try (FileReader fr = new FileReader(arg[0]);
4a17ca431caf 7019834: Eliminate dependency from PolicyFile to com.sun.security.auth.PrincipalComparator
mullan
parents: 7179
diff changeset
  1365
             FileWriter fw = new FileWriter(arg[1])) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            PolicyParser pp = new PolicyParser(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            pp.read(fr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            pp.write(fw);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
}