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