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