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