jdk/src/share/classes/com/sun/security/auth/PolicyParser.java
author mullan
Tue, 29 Mar 2011 10:39:00 -0400
changeset 9011 c08eb9697ee4
parent 7179 4afb81e50183
permissions -rw-r--r--
7019937: Translatability bug - Remove Unused String - String ID , read end of file 7019938: Translatability bug - Remove Unused String - String ID can not specify Principal with a 7019940: Translatability bug - Remove unused string - String ID: provided null name 7019942: Translatability bug - String ID: trustedCertEntry, 7019945: Translatability bug - Translatability issue - String ID: * has NOT been verified! In order to veri 7019947: Translatability bug - Translatability issue - String ID: * The integrity of the information stored i 7019949: Translatability bug - Translatability issue - String ID: * you must provide your keystore password. Reviewed-by: weijun, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 7179
diff changeset
     2
 * Copyright (c) 1998, 2011, 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 com.sun.security.auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.RuntimePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 7179
diff changeset
    33
import java.security.GeneralSecurityException;
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 7179
diff changeset
    34
import java.text.MessageFormat;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.ListIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.security.util.PropertyExpander;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * The policy for a Java runtime (specifying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * which permissions are available for code from various principals)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * is represented as a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * persistent configuration.  The configuration may be stored as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * flat ASCII file, as a serialized binary file of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the Policy class, or as a database. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>The Java runtime creates one global Policy object, which is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * represent the static policy configuration file.  It is consulted by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * a ProtectionDomain when the protection domain initializes its set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * permissions. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>The Policy <code>init</code> method parses the policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * configuration file, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * populates the Policy object.  The Policy object is agnostic in that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * it is not involved in making policy decisions.  It is merely the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Java runtime representation of the persistent policy configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * file. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>When a protection domain needs to initialize its set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * permissions, it executes code such as the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * to ask the global Policy object to populate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Permissions object with the appropriate permissions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *  policy = Policy.getPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *  Permissions perms = policy.getPermissions(MyCodeSource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p>The protection domain passes in a CodeSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * object, which encapsulates its codebase (URL) and public key attributes.
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 returns an appropriate Permissions object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * @deprecated As of JDK&nbsp;1.4, replaced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *             {@link sun.security.provider.PolicyParser}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *             This class is entirely deprecated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
class PolicyParser {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final java.util.ResourceBundle rb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        (new java.security.PrivilegedAction<java.util.ResourceBundle>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            public java.util.ResourceBundle run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    return (java.util.ResourceBundle.getBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                ("sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private Vector<GrantEntry> grantEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // Convenience variables for parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static final sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        sun.security.util.Debug.getInstance("parser", "\t[Auth Policy Parser]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private StreamTokenizer st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private int lookahead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private int linenum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private boolean expandProp = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private String keyStoreUrlString = null; // unexpanded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private String keyStoreType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private String expand(String value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        throws PropertyExpander.ExpandException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (expandProp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return PropertyExpander.expand(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Creates a PolicyParser object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public PolicyParser() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        grantEntries = new Vector<GrantEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public PolicyParser(boolean expandProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.expandProp = expandProp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Reads a policy configuration into the Policy object using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Reader object. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param policy the policy Reader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @exception ParsingException if the policy configuration contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *          a syntax error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception IOException if an error occurs while reading the policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *          configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void read(Reader policy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (!(policy instanceof BufferedReader)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            policy = new BufferedReader(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * Configure the stream tokenizer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         *      Recognize strings between "..."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
         *      Don't convert words to lowercase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
         *      Recognize both C-style and C++-style comments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
         *      Treat end-of-line as white space, not as a token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        st   = new StreamTokenizer(policy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        st.resetSyntax();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        st.wordChars('a', 'z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        st.wordChars('A', 'Z');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        st.wordChars('.', '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        st.wordChars('0', '9');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        st.wordChars('_', '_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        st.wordChars('$', '$');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        st.wordChars(128 + 32, 255);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        st.whitespaceChars(0, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        st.commentChar('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        st.quoteChar('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        st.quoteChar('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        st.lowerCaseMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        st.ordinaryChar('/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        st.slashSlashComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        st.slashStarComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * The main parsing loop.  The loop is executed once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * for each entry in the config file.      The entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * are delimited by semicolons.   Once we've read in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         * the information for an entry, go ahead and try to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         * add it to the policy vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        while (lookahead != StreamTokenizer.TT_EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (peek("grant")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                GrantEntry ge = parseGrantEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                // could be null if we couldn't expand a property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if (ge != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    add(ge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } else if (peek("keystore") && keyStoreUrlString==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                // only one keystore entry per policy file, others will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                parseKeyStoreEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                // error?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            match(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public void add(GrantEntry ge)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        grantEntries.addElement(ge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public void replace(GrantEntry origGe, GrantEntry newGe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        grantEntries.setElementAt(newGe, grantEntries.indexOf(origGe));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public boolean remove(GrantEntry ge)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return grantEntries.removeElement(ge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Returns the (possibly expanded) keystore location, or null if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * expansion fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public String getKeyStoreUrl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (keyStoreUrlString!=null && keyStoreUrlString.length()!=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                return expand(keyStoreUrlString).replace(File.separatorChar,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                                         '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public void setKeyStoreUrl(String url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        keyStoreUrlString = url;
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 String getKeyStoreType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return keyStoreType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public void setKeyStoreType(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        keyStoreType = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Enumerate all the entries in the global policy object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * This method is used by policy admin tools.   The tools
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * should use the Enumeration methods on the returned object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * to fetch the elements sequentially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    public Enumeration<GrantEntry> grantElements(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return grantEntries.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * write out the policy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public void write(Writer policy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        PrintWriter out = new PrintWriter(new BufferedWriter(policy));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        Enumeration<GrantEntry> enum_ = grantElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        out.println("/* AUTOMATICALLY GENERATED ON "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    (new java.util.Date()) + "*/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        out.println("/* DO NOT EDIT */");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // write the (unexpanded) keystore entry as the first entry of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // policy file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (keyStoreUrlString != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            writeKeyStoreEntry(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // write "grant" entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            GrantEntry ge = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            ge.write(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * parses a keystore entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private void parseKeyStoreEntry() throws ParsingException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        match("keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        keyStoreUrlString = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        // parse keystore type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!peek(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return; // default type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        match(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            keyStoreType = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            throw new ParsingException(st.lineno(),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   304
                        rb.getString("expected.keystore.type"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * writes the (unexpanded) keystore entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private void writeKeyStoreEntry(PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        out.print("keystore \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        out.print(keyStoreUrlString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (keyStoreType != null && keyStoreType.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            out.print(", \"" + keyStoreType + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        out.println(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * parse a Grant entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private GrantEntry parseGrantEntry()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        GrantEntry e = new GrantEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        LinkedList<PrincipalEntry> principals = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        boolean ignoreEntry = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        match("grant");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        while(!peek("{")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if (peekAndMatch("Codebase")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                e.codeBase = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                peekAndMatch(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            } else if (peekAndMatch("SignedBy")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                e.signedBy = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                peekAndMatch(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            } else if (peekAndMatch("Principal")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                if (principals == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    principals = new LinkedList<PrincipalEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                // check for principalClass wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                String principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (peek("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    match("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    principalClass = PrincipalEntry.WILDCARD_CLASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    principalClass = match("principal type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                // check for principalName wildcard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                String principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                if (peek("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    match("*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    principalName = PrincipalEntry.WILDCARD_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    principalName = match("quoted string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                // disallow WILDCARD_CLASS && actual name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                if (principalClass.equals(PrincipalEntry.WILDCARD_CLASS) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    !principalName.equals(PrincipalEntry.WILDCARD_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        debug.println("disallowing principal that has " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                "WILDCARD class but no WILDCARD name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    throw new ParsingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        (st.lineno(),
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 7179
diff changeset
   372
                        rb.getString("can.not.specify.Principal.with.a." +
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 7179
diff changeset
   373
                                     "wildcard.class.without.a.wildcard.name"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    principalName = expand(principalName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    principals.add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        (new PrincipalEntry(principalClass, principalName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    // ignore the entire policy entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    // but continue parsing all the info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    // so we can get to the next entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    if (debug != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        debug.println("principal name expansion failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                        principalName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    ignoreEntry = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                peekAndMatch(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                 ParsingException(st.lineno(),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   393
                        rb.getString("expected.codeBase.or.SignedBy"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // disallow non principal-based grant entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (principals == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            throw new ParsingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                (st.lineno(),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   401
                rb.getString("only.Principal.based.grant.entries.permitted"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        e.principals = principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        match("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        while(!peek("}")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (peek("Permission")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    PermissionEntry pe = parsePermissionEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    e.add(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    // ignore. The add never happened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    skipEntry();  // BugId 4219343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                match(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                throw new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    ParsingException(st.lineno(),
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   420
                    rb.getString("expected.permission.entry"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        match("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (e.codeBase != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
              e.codeBase = expand(e.codeBase).replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            e.signedBy = expand(e.signedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        return (ignoreEntry == true) ? null : e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * parse a Permission entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    private PermissionEntry parsePermissionEntry()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        throws ParsingException, IOException, PropertyExpander.ExpandException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        PermissionEntry e = new PermissionEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        // Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        match("Permission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        e.permission = match("permission type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            // Permission name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            e.name = expand(match("quoted string"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (!peek(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        match(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (peek("\"")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                e.action = expand(match("quoted string"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                if (!peek(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                match(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (peekAndMatch("SignedBy")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            e.signedBy = expand(match("quoted string"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        return e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    private boolean peekAndMatch(String expect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (peek(expect)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            match(expect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    private boolean peek(String expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        switch (lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        case StreamTokenizer.TT_WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (expect.equalsIgnoreCase(st.sval))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (expect.equalsIgnoreCase(","))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            if (expect.equalsIgnoreCase("{"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            if (expect.equalsIgnoreCase("}"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        case '"':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            if (expect.equalsIgnoreCase("\""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if (expect.equalsIgnoreCase("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    private String match(String expect)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        switch (lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        case StreamTokenizer.TT_NUMBER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            throw new ParsingException(st.lineno(), expect,
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   526
                                        rb.getString("number.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                        String.valueOf(st.nval));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        case StreamTokenizer.TT_EOF:
9011
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 7179
diff changeset
   529
            MessageFormat form = new MessageFormat(
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 7179
diff changeset
   530
                    rb.getString("expected.expect.read.end.of.file."));
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 7179
diff changeset
   531
            Object[] source = {expect};
c08eb9697ee4 7019937: Translatability bug - Remove Unused String - String ID , read end of file
mullan
parents: 7179
diff changeset
   532
            throw new ParsingException(form.format(source));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        case StreamTokenizer.TT_WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            if (expect.equalsIgnoreCase(st.sval)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            } else if (expect.equalsIgnoreCase("permission type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            } else if (expect.equalsIgnoreCase("principal type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                throw new ParsingException(st.lineno(), expect, st.sval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        case '"':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if (expect.equalsIgnoreCase("quoted string")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            } else if (expect.equalsIgnoreCase("permission type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            } else if (expect.equalsIgnoreCase("principal type")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                throw new ParsingException(st.lineno(), expect, st.sval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            if (expect.equalsIgnoreCase(","))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                throw new ParsingException(st.lineno(), expect, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            if (expect.equalsIgnoreCase("{"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                throw new ParsingException(st.lineno(), expect, "{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (expect.equalsIgnoreCase("}"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                throw new ParsingException(st.lineno(), expect, "}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        case ';':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            if (expect.equalsIgnoreCase(";"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                throw new ParsingException(st.lineno(), expect, ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            if (expect.equalsIgnoreCase("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                throw new ParsingException(st.lineno(), expect, "*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            throw new ParsingException(st.lineno(), expect,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                               new String(new char[] {(char)lookahead}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * skip all tokens for this entry leaving the delimiter ";"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    private void skipEntry()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        throws ParsingException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
      while(lookahead != ';') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        switch (lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        case StreamTokenizer.TT_NUMBER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            throw new ParsingException(st.lineno(), ";",
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   608
                                       rb.getString("number.") +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                                        String.valueOf(st.nval));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        case StreamTokenizer.TT_EOF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
          throw new ParsingException
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   612
                (rb.getString("expected.read.end.of.file"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
          lookahead = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
      }
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
     * Each grant entry in the policy configuration file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * represented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * GrantEntry object.  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * For example, the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *      grant signedBy "Duke" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *          permission java.io.FilePermission "/tmp", "read,write";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *      };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * is represented internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * pe = new PermissionEntry("java.io.FilePermission",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *                           "/tmp", "read,write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * ge = new GrantEntry("Duke", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * ge.add(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * version 1.19, 05/21/98
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    static class GrantEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        public String signedBy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        public String codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        public LinkedList<PrincipalEntry> principals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        public Vector<PermissionEntry> permissionEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        public GrantEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            permissionEntries = new Vector<PermissionEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        public GrantEntry(String signedBy, String codeBase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            this.codeBase = codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            this.signedBy = signedBy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            permissionEntries = new Vector<PermissionEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        public void add(PermissionEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            permissionEntries.addElement(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        public boolean remove(PermissionEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return permissionEntries.removeElement(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        public boolean contains(PermissionEntry pe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            return permissionEntries.contains(pe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
         * Enumerate all the permission entries in this GrantEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        public Enumeration<PermissionEntry> permissionElements(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return permissionEntries.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        public void write(PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            out.print("grant");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            if (signedBy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                out.print(" signedBy \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                out.print(signedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                if (codeBase != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                    out.print(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            if (codeBase != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                out.print(" codeBase \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                out.print(codeBase);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                if (principals != null && principals.size() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                    out.print(",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            if (principals != null && principals.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                ListIterator<PrincipalEntry> pli = principals.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                while (pli.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                    out.print("\tPrincipal ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                    PrincipalEntry pe = pli.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    out.print(pe.principalClass +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                                " \"" + pe.principalName + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    if (pli.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                        out.print(",\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            out.println(" {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            Enumeration<PermissionEntry> enum_ = permissionEntries.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            while (enum_.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                PermissionEntry pe = enum_.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                out.write("  ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                pe.write(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            out.println("};");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * Principal info (class and name) in a grant entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    static class PrincipalEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        static final String WILDCARD_CLASS = "WILDCARD_PRINCIPAL_CLASS";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        static final String WILDCARD_NAME = "WILDCARD_PRINCIPAL_NAME";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        String principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        String principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
         * A PrincipalEntry consists of the <code>Principal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
         * class and <code>Principal</code> name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
         * @param principalClass the <code>Principal</code> class. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
         * @param principalName the <code>Principal</code> name. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        public PrincipalEntry(String principalClass, String principalName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            if (principalClass == null || principalName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                throw new NullPointerException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                        ("null principalClass or principalName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            this.principalClass = principalClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            this.principalName = principalName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         * Test for equality between the specified object and this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         * Two PrincipalEntries are equal if their PrincipalClass and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         * PrincipalName values are equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
         * @param obj the object to test for equality with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
         * @return true if the objects are equal, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            if (!(obj instanceof PrincipalEntry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            PrincipalEntry that = (PrincipalEntry)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            if (this.principalClass.equals(that.principalClass) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                this.principalName.equals(that.principalName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
         * Return a hashcode for this <code>PrincipalEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         * @return a hashcode for this <code>PrincipalEntry</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            return principalClass.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * Each permission entry in the policy configuration file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * represented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * PermissionEntry object.  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * For example, the entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *          permission java.io.FilePermission "/tmp", "read,write";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * is represented internally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * pe = new PermissionEntry("java.io.FilePermission",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *                           "/tmp", "read,write");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * version 1.19, 05/21/98
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    static class PermissionEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        public String permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        public String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        public String action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        public String signedBy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        public PermissionEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        public PermissionEntry(String permission,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                        String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                        String action) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            this.permission = permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            this.action = action;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * Calculates a hash code value for the object.  Objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         * which are equal will also have the same hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            int retval = permission.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (name != null) retval ^= name.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            if (action != null) retval ^= action.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            if (! (obj instanceof PermissionEntry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            PermissionEntry that = (PermissionEntry) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            if (this.permission == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                if (that.permission != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                if (!this.permission.equals(that.permission)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            if (this.name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                if (that.name != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                if (!this.name.equals(that.name)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            if (this.action == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                if (that.action != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                if (!this.action.equals(that.action)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            if (this.signedBy == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                if (that.signedBy != null) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                if (!this.signedBy.equals(that.signedBy)) return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            // everything matched -- the 2 objects are equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        public void write(PrintWriter out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            out.print("permission ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            out.print(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                out.print(" \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                // have to add escape chars for quotes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                if (name.indexOf("\"") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    int numQuotes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                    char[] chars = name.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                    // count the number of quote chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    for (int i = 0; i < chars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                        if (chars[i] == '"')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                            numQuotes++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    // now, add an escape char before each quote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    char[] newChars = new char[chars.length + numQuotes];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                    for (int i = 0, j = 0; i < chars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                        if (chars[i] != '"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                            newChars[j++] = chars[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                            newChars[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                            newChars[j++] = chars[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    name = new String(newChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                out.print(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            if (action != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                out.print(", \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                out.print(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            if (signedBy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                out.print(", signedBy \"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                out.print(signedBy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                out.print('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            out.println(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    static class ParsingException extends GeneralSecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        private static final long serialVersionUID = 8240970523155877400L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
         * Constructs a ParsingException with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
         * detail message. A detail message is a String that describes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
         * this particular exception, which may, for example, specify which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
         * algorithm is not available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
         * @param msg the detail message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        public ParsingException(String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            super(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        public ParsingException(int line, String msg) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   947
            super(rb.getString("line.") + line + rb.getString("COLON") + msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        public ParsingException(int line, String expect, String actual) {
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   951
            super(rb.getString("line.") + line + rb.getString(".expected.") +
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   952
                expect + rb.getString(".found.") + actual +
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   953
                rb.getString("QUOTE"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    public static void main(String arg[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        PolicyParser pp = new PolicyParser(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        pp.read(new FileReader(arg[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        FileWriter fr = new FileWriter(arg[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        pp.write(fr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        fr.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
}